pack_client.c revision 977a7ff905883c1b7d4650a211a089684db421f6
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync/* Copyright (c) 2001, Stanford University
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * All rights reserved
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * See the file LICENSE.txt for information on redistributing this software.
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync/*Convert from GLint to GLfloat in [-1.f,1.f]*/
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync#define CRP_I2F_NORM(i) ((2.f*((GLint)(i))+1.f) * (1.f/4294967294.f))
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync/*Convert from GLshort to GLfloat in [-1.f,1.f]*/
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync#define CRP_S2F_NORM(s) ((2.f*((GLshort)(s))+1.f) * (1.f/65535.f))
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsyncstatic void crPackVertexAttrib(const CRVertexArrays *array, unsigned int attr, GLint index)
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync unsigned char *p = array->a[attr].p + index * array->a[attr].stride;
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync if (array->a[attr].buffer && array->a[attr].buffer->data)
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync p = (unsigned char *)(array->a[attr].buffer->data) + (unsigned long)p;
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync crPackVertexAttrib1fARB(attr, CRP_S2F_NORM(sPtr[0]));
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync crPackVertexAttrib2fARB(attr, CRP_S2F_NORM(sPtr[0]), CRP_S2F_NORM(sPtr[1]));
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync crPackVertexAttrib3fARB(attr, CRP_S2F_NORM(sPtr[0]), CRP_S2F_NORM(sPtr[1]), CRP_S2F_NORM(sPtr[2]));
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync crPackVertexAttrib1fARB(attr, CRP_I2F_NORM(iPtr[0]));
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync crPackVertexAttrib2fARB(attr, CRP_I2F_NORM(iPtr[0]), CRP_I2F_NORM(iPtr[1]));
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync crPackVertexAttrib3fARB(attr, CRP_I2F_NORM(iPtr[0]), CRP_I2F_NORM(iPtr[1]), CRP_I2F_NORM(iPtr[2]));
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync crPackVertexAttrib3fARB(attr, iPtr[0], iPtr[1], iPtr[2]);
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync crPackVertexAttrib4fARB(attr, iPtr[0], iPtr[1], iPtr[2], iPtr[3]);
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync case 1: crPackVertexAttrib1fvARB(attr, (GLfloat *)p); break;
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync case 2: crPackVertexAttrib2fvARB(attr, (GLfloat *)p); break;
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync case 3: crPackVertexAttrib3fvARB(attr, (GLfloat *)p); break;
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync case 4: crPackVertexAttrib4fvARB(attr, (GLfloat *)p); break;
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync case 1: crPackVertexAttrib1dvARB(attr, (GLdouble *)p); break;
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync case 2: crPackVertexAttrib2dvARB(attr, (GLdouble *)p); break;
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync case 3: crPackVertexAttrib3dvARB(attr, (GLdouble *)p); break;
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync case 4: crPackVertexAttrib4dvARB(attr, (GLdouble *)p); break;
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync crWarning("Bad datatype for vertex attribute [%d] array: 0x%x\n",
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync * Expand glArrayElement into crPackVertex/Color/Normal/etc.
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsynccrPackExpandArrayElement(GLint index, CRClientState *c)
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync unsigned char *p;
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync const GLboolean vpEnabled = crStateGetCurrent()->program.vpEnabled;
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync if (array->n.enabled && !(vpEnabled && array->a[VERT_ATTRIB_NORMAL].enabled))
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync p = (unsigned char *)(array->n.buffer->data) + (unsigned long)p;
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync case GL_SHORT: crPackNormal3sv((GLshort *)p); break;
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync case GL_FLOAT: crPackNormal3fv((GLfloat *)p); break;
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync case GL_DOUBLE: crPackNormal3dv((GLdouble *)p); break;
977a7ff905883c1b7d4650a211a089684db421f6vboxsync crWarning("Unhandled: crPackExpandArrayElement, array->n.type 0x%x", array->n.type);
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync if (array->c.enabled && !(vpEnabled && array->a[VERT_ATTRIB_COLOR0].enabled))
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync p = (unsigned char *)(array->c.buffer->data) + (unsigned long)p;
977a7ff905883c1b7d4650a211a089684db421f6vboxsync crWarning("Unhandled: crPackExpandArrayElement, array->c.type 0x%x", array->c.type);
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync if (array->s.enabled && !(vpEnabled && array->a[VERT_ATTRIB_COLOR1].enabled))
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync p = (unsigned char *)(array->s.buffer->data) + (unsigned long)p;
977a7ff905883c1b7d4650a211a089684db421f6vboxsync crWarning("Unhandled: crPackExpandArrayElement, array->s.type 0x%x", array->s.type);
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync#endif // CR_EXT_secondary_color
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync if (array->f.enabled && !(vpEnabled && array->a[VERT_ATTRIB_FOG].enabled))
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync p = (unsigned char *)(array->f.buffer->data) + (unsigned long)p;
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync#endif // CR_EXT_fog_coord
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync for (unit = 0 ; unit < CR_MAX_TEXTURE_UNITS ; unit++)
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync if (array->t[unit].enabled && !(vpEnabled && array->a[VERT_ATTRIB_TEX0+unit].enabled))
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync p = array->t[unit].p + index * array->t[unit].stride;
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync if (array->t[unit].buffer && array->t[unit].buffer->data)
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync p = (unsigned char *)(array->t[unit].buffer->data) + (unsigned long)p;
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync case 1: crPackMultiTexCoord1svARB(GL_TEXTURE0_ARB + unit, (GLshort *)p); break;
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync case 2: crPackMultiTexCoord2svARB(GL_TEXTURE0_ARB + unit, (GLshort *)p); break;
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync case 3: crPackMultiTexCoord3svARB(GL_TEXTURE0_ARB + unit, (GLshort *)p); break;
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync case 4: crPackMultiTexCoord4svARB(GL_TEXTURE0_ARB + unit, (GLshort *)p); break;
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync case 1: crPackMultiTexCoord1ivARB(GL_TEXTURE0_ARB + unit, (GLint *)p); break;
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync case 2: crPackMultiTexCoord2ivARB(GL_TEXTURE0_ARB + unit, (GLint *)p); break;
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync case 3: crPackMultiTexCoord3ivARB(GL_TEXTURE0_ARB + unit, (GLint *)p); break;
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync case 4: crPackMultiTexCoord4ivARB(GL_TEXTURE0_ARB + unit, (GLint *)p); break;
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync case 1: crPackMultiTexCoord1fvARB(GL_TEXTURE0_ARB + unit, (GLfloat *)p); break;
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync case 2: crPackMultiTexCoord2fvARB(GL_TEXTURE0_ARB + unit, (GLfloat *)p); break;
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync case 3: crPackMultiTexCoord3fvARB(GL_TEXTURE0_ARB + unit, (GLfloat *)p); break;
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync case 4: crPackMultiTexCoord4fvARB(GL_TEXTURE0_ARB + unit, (GLfloat *)p); break;
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync case 1: crPackMultiTexCoord1dvARB(GL_TEXTURE0_ARB + unit, (GLdouble *)p); break;
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync case 2: crPackMultiTexCoord2dvARB(GL_TEXTURE0_ARB + unit, (GLdouble *)p); break;
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync case 3: crPackMultiTexCoord3dvARB(GL_TEXTURE0_ARB + unit, (GLdouble *)p); break;
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync case 4: crPackMultiTexCoord4dvARB(GL_TEXTURE0_ARB + unit, (GLdouble *)p); break;
977a7ff905883c1b7d4650a211a089684db421f6vboxsync crWarning("Unhandled: crPackExpandArrayElement, array->t[%i].type 0x%x", unit, array->t[unit].type);
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync p = (unsigned char *)(array->i.buffer->data) + (unsigned long)p;
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync case GL_DOUBLE: crPackIndexdv((GLdouble *)p); break;
977a7ff905883c1b7d4650a211a089684db421f6vboxsync crWarning("Unhandled: crPackExpandArrayElement, array->i.type 0x%x", array->i.type);
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync p = (unsigned char *)(array->e.buffer->data) + (unsigned long)p;
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync p = (unsigned char *)(array->v.buffer->data) + (unsigned long)p;
977a7ff905883c1b7d4650a211a089684db421f6vboxsync crWarning("Unhandled: crPackExpandArrayElement, array->v.type 0x%x", array->v.type);
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsynccrPackExpandDrawArrays(GLenum mode, GLint first, GLsizei count, CRClientState *c)
54cdd2a4e7676bc8694fa8db62ece5a1447b3597vboxsync __PackError(__LINE__, __FILE__, GL_INVALID_VALUE, "crPackDrawArrays(negative count)");
54cdd2a4e7676bc8694fa8db62ece5a1447b3597vboxsync __PackError(__LINE__, __FILE__, GL_INVALID_ENUM, "crPackDrawArrays(bad mode)");
54cdd2a4e7676bc8694fa8db62ece5a1447b3597vboxsync for (i=0; i<count; i++)
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync return sizeof(GLubyte);
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync return sizeof(GLushort);
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync return sizeof(GLuint);
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync crError("Unknown type 0x%x in crPackElementsIndexSize", type);
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsynccrPackDrawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices)
4aef33e90cfa798b2e6db5b45f02c2a6c8bbc4aevboxsync int packet_length = sizeof(int) + sizeof(mode) + sizeof(count) + sizeof(type) + sizeof(GLuint);
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync CRBufferObject *elementsBuffer = crStateGetCurrent()->bufferobject.elementsBuffer;
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync /*@todo not sure it's possible, and not sure what to do*/
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync crWarning("crPackDrawElements: trying to use bound but empty elements buffer, ignoring.");
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync start_ptr = data_ptr = (unsigned char *) crPackAlloc(packet_length);
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync WRITE_DATA_AI(GLenum, CR_DRAWELEMENTS_EXTEND_OPCODE );
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsynccrPackDrawRangeElements(GLenum mode, GLuint start, GLuint end, GLsizei count,
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync int packet_length = sizeof(int) + sizeof(mode) + sizeof(start)
4aef33e90cfa798b2e6db5b45f02c2a6c8bbc4aevboxsync + sizeof(end) + sizeof(count) + sizeof(type) + sizeof(GLuint);
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync CRBufferObject *elementsBuffer = crStateGetCurrent()->bufferobject.elementsBuffer;
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync /*@todo not sure it's possible, and not sure what to do*/
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync crWarning("crPackDrawElements: trying to use bound but empty elements buffer, ignoring.");
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync start_ptr = data_ptr = (unsigned char *) crPackAlloc(packet_length);
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync WRITE_DATA_AI(GLenum, CR_DRAWRANGEELEMENTS_EXTEND_OPCODE);
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * Expand glDrawElements into crPackBegin/Vertex/End, etc commands.
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * Note: if mode==999, don't call glBegin/glEnd.
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsynccrPackExpandDrawElements(GLenum mode, GLsizei count, GLenum type,
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync CRBufferObject *elementsBuffer = crStateGetCurrent()->bufferobject.elementsBuffer;
54cdd2a4e7676bc8694fa8db62ece5a1447b3597vboxsync "crPackDrawElements(negative count)");
54cdd2a4e7676bc8694fa8db62ece5a1447b3597vboxsync "crPackDrawElements(bad mode)");
54cdd2a4e7676bc8694fa8db62ece5a1447b3597vboxsync "crPackDrawElements(bad type)");
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync p = (unsigned char *)(elementsBuffer->data) + (unsigned long)p;
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync //crDebug("crPackExpandDrawElements mode:0x%x, count:%d, type:0x%x", mode, count, type);
54cdd2a4e7676bc8694fa8db62ece5a1447b3597vboxsync for (i=0; i<count; i++)
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync for (i=0; i<count; i++)
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync crPackExpandArrayElement((GLint) * (GLushort *) p, c);
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync p+=sizeof (GLushort);
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync for (i=0; i<count; i++)
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync crPackExpandArrayElement((GLint) * (GLuint *) p, c);
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync p+=sizeof (GLuint);
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync crError( "this can't happen: array_spu.self.DrawElements" );
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * Convert a glDrawElements command into a sequence of ArrayElement() calls.
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * NOTE: Caller must issue the glBegin/glEnd.
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsynccrPackUnrollDrawElements(GLsizei count, GLenum type,
54cdd2a4e7676bc8694fa8db62ece5a1447b3597vboxsync for (i = 0; i < count; i++)
54cdd2a4e7676bc8694fa8db62ece5a1447b3597vboxsync for (i = 0; i < count; i++)
54cdd2a4e7676bc8694fa8db62ece5a1447b3597vboxsync for (i = 0; i < count; i++)
54cdd2a4e7676bc8694fa8db62ece5a1447b3597vboxsync "crPackUnrollDrawElements(bad type)");
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * glDrawRangeElements, expanded into crPackBegin/Vertex/End/etc.
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsynccrPackExpandDrawRangeElements(GLenum mode, GLuint start, GLuint end, GLsizei count, GLenum type, const GLvoid *indices, CRClientState *c)
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync crWarning("crPackExpandDrawRangeElements start>end (%d>%d)", start, end);
236b6e0fdf652661ff4c655314fe488998c5c17dvboxsync crPackExpandDrawElements(mode, count, type, indices, c);
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * Pack real DrawArrays commands.
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsynccrPackMultiDrawArraysEXT( GLenum mode, GLint *first, GLsizei *count,
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync for (i = 0; i < primcount; i++) {
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync if (count[i] > 0) {
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsynccrPackExpandMultiDrawArraysEXT( GLenum mode, GLint *first, GLsizei *count,
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync for (i = 0; i < primcount; i++) {
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync if (count[i] > 0) {
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync crPackExpandDrawArrays(mode, first[i], count[i], c);
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * Pack real DrawElements commands.
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsynccrPackMultiDrawElementsEXT( GLenum mode, const GLsizei *count, GLenum type,
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync for (i = 0; i < primcount; i++) {
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync if (count[i] > 0) {
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync crPackDrawElements(mode, count[i], type, indices[i]);
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsynccrPackExpandMultiDrawElementsEXT( GLenum mode, const GLsizei *count,
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync for (i = 0; i < primcount; i++) {
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync if (count[i] > 0) {
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync crPackExpandDrawElements(mode, count[i], type, indices[i], c);
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync#endif /* CR_EXT_multi_draw_arrays */