From e488663e3176b003fbed71407a30ffc8a934e63b Mon Sep 17 00:00:00 2001 From: SmileTheory Date: Tue, 28 Oct 2014 18:15:13 -0700 Subject: [PATCH] OpenGL2: Change normal/tangent vertex encoding. --- code/renderergl2/glsl/dlight_vp.glsl | 2 +- code/renderergl2/glsl/fogpass_vp.glsl | 3 +- code/renderergl2/glsl/generic_vp.glsl | 3 +- code/renderergl2/glsl/lightall_vp.glsl | 9 +- code/renderergl2/glsl/pshadow_vp.glsl | 2 +- code/renderergl2/glsl/shadowfill_vp.glsl | 1 - code/renderergl2/tr_extensions.c | 4 +- code/renderergl2/tr_vbo.c | 140 ++++++++++++++++------- 8 files changed, 108 insertions(+), 56 deletions(-) diff --git a/code/renderergl2/glsl/dlight_vp.glsl b/code/renderergl2/glsl/dlight_vp.glsl index 9566a04c..c326bd78 100644 --- a/code/renderergl2/glsl/dlight_vp.glsl +++ b/code/renderergl2/glsl/dlight_vp.glsl @@ -74,7 +74,7 @@ vec3 DeformPosition(const vec3 pos, const vec3 normal, const vec2 st) void main() { vec3 position = attr_Position; - vec3 normal = attr_Normal * 2.0 - vec3(1.0); + vec3 normal = attr_Normal; #if defined(USE_DEFORM_VERTEXES) position = DeformPosition(position, normal, attr_TexCoord0.st); diff --git a/code/renderergl2/glsl/fogpass_vp.glsl b/code/renderergl2/glsl/fogpass_vp.glsl index 8f7bc728..c8ec9a93 100644 --- a/code/renderergl2/glsl/fogpass_vp.glsl +++ b/code/renderergl2/glsl/fogpass_vp.glsl @@ -102,10 +102,9 @@ void main() #if defined(USE_VERTEX_ANIMATION) vec3 position = mix(attr_Position, attr_Position2, u_VertexLerp); vec3 normal = mix(attr_Normal, attr_Normal2, u_VertexLerp); - normal = normalize(normal - vec3(0.5)); #else vec3 position = attr_Position; - vec3 normal = attr_Normal * 2.0 - vec3(1.0); + vec3 normal = attr_Normal; #endif #if defined(USE_DEFORM_VERTEXES) diff --git a/code/renderergl2/glsl/generic_vp.glsl b/code/renderergl2/glsl/generic_vp.glsl index 8543ffd7..859b4363 100644 --- a/code/renderergl2/glsl/generic_vp.glsl +++ b/code/renderergl2/glsl/generic_vp.glsl @@ -207,10 +207,9 @@ void main() #if defined(USE_VERTEX_ANIMATION) vec3 position = mix(attr_Position, attr_Position2, u_VertexLerp); vec3 normal = mix(attr_Normal, attr_Normal2, u_VertexLerp); - normal = normalize(normal - vec3(0.5)); #else vec3 position = attr_Position; - vec3 normal = attr_Normal * 2.0 - vec3(1.0); + vec3 normal = attr_Normal; #endif #if defined(USE_DEFORM_VERTEXES) diff --git a/code/renderergl2/glsl/lightall_vp.glsl b/code/renderergl2/glsl/lightall_vp.glsl index b9e839de..59051d7c 100644 --- a/code/renderergl2/glsl/lightall_vp.glsl +++ b/code/renderergl2/glsl/lightall_vp.glsl @@ -167,11 +167,6 @@ void main() #endif #endif - normal = normal * 2.0 - vec3(1.0); -#if defined(USE_VERT_TANGENT_SPACE) && defined(USE_LIGHT) && !defined(USE_FAST_LIGHT) - tangent = tangent * 2.0 - vec3(1.0); -#endif - #if defined(USE_TCGEN) vec2 texCoords = GenTexCoords(u_TCGen0, position, normal, u_TCGen0Vector0, u_TCGen0Vector1); #else @@ -195,13 +190,13 @@ void main() #endif #if defined(USE_VERT_TANGENT_SPACE) && defined(USE_LIGHT) && !defined(USE_FAST_LIGHT) - vec3 bitangent = cross(normal, tangent) * (attr_Tangent.w * 2.0 - 1.0); + vec3 bitangent = cross(normal, tangent) * attr_Tangent.w; #endif #if defined(USE_LIGHT_VECTOR) vec3 L = u_LightOrigin.xyz - (position * u_LightOrigin.w); #elif defined(USE_LIGHT) && !defined(USE_FAST_LIGHT) - vec3 L = attr_LightDirection * 2.0 - vec3(1.0); + vec3 L = attr_LightDirection; #if defined(USE_MODELMATRIX) L = (u_ModelMatrix * vec4(L, 0.0)).xyz; #endif diff --git a/code/renderergl2/glsl/pshadow_vp.glsl b/code/renderergl2/glsl/pshadow_vp.glsl index 0f9940cd..07a49855 100644 --- a/code/renderergl2/glsl/pshadow_vp.glsl +++ b/code/renderergl2/glsl/pshadow_vp.glsl @@ -11,5 +11,5 @@ void main() gl_Position = u_ModelViewProjectionMatrix * vec4(attr_Position, 1.0); var_Position = attr_Position; - var_Normal = attr_Normal * 2.0 - vec3(1.0); + var_Normal = attr_Normal; } diff --git a/code/renderergl2/glsl/shadowfill_vp.glsl b/code/renderergl2/glsl/shadowfill_vp.glsl index 7a5cc571..7de901ba 100644 --- a/code/renderergl2/glsl/shadowfill_vp.glsl +++ b/code/renderergl2/glsl/shadowfill_vp.glsl @@ -80,7 +80,6 @@ void main() { vec3 position = mix(attr_Position, attr_Position2, u_VertexLerp); vec3 normal = mix(attr_Normal, attr_Normal2, u_VertexLerp); - normal = normalize(normal - vec3(0.5)); position = DeformPosition(position, normal, attr_TexCoord0.st); diff --git a/code/renderergl2/tr_extensions.c b/code/renderergl2/tr_extensions.c index bc8bb756..b4a8db54 100644 --- a/code/renderergl2/tr_extensions.c +++ b/code/renderergl2/tr_extensions.c @@ -673,11 +673,11 @@ void GLimp_InitExtraExtensions() // GL_ARB_vertex_type_2_10_10_10_rev extension = "GL_ARB_vertex_type_2_10_10_10_rev"; - glRefConfig.packedNormalDataType = GL_UNSIGNED_BYTE; + glRefConfig.packedNormalDataType = GL_BYTE; if( GLimp_HaveExtension( extension ) ) { if (r_arb_vertex_type_2_10_10_10_rev->integer) - glRefConfig.packedNormalDataType = GL_UNSIGNED_INT_2_10_10_10_REV; + glRefConfig.packedNormalDataType = GL_INT_2_10_10_10_REV; ri.Printf(PRINT_ALL, result[r_arb_vertex_type_2_10_10_10_rev->integer ? 1 : 0], extension); } diff --git a/code/renderergl2/tr_vbo.c b/code/renderergl2/tr_vbo.c index 9734c5a7..981741fb 100644 --- a/code/renderergl2/tr_vbo.c +++ b/code/renderergl2/tr_vbo.c @@ -23,71 +23,126 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #include "tr_local.h" +union pack10_u { + struct { + signed int x:10; + signed int y:10; + signed int z:10; + signed int w:2; + } pack; + uint32_t i; +}; + +union pack8_u { + struct { + signed int x:8; + signed int y:8; + signed int z:8; + signed int w:8; + } pack; + uint32_t i; +}; + + uint32_t R_VaoPackTangent(vec4_t v) { - if (glRefConfig.packedNormalDataType == GL_UNSIGNED_INT_2_10_10_10_REV) + if (glRefConfig.packedNormalDataType == GL_INT_2_10_10_10_REV) { - return (((uint32_t)(v[3] * 1.5f + 2.0f )) << 30) - | (((uint32_t)(v[2] * 511.5f + 512.0f)) << 20) - | (((uint32_t)(v[1] * 511.5f + 512.0f)) << 10) - | (((uint32_t)(v[0] * 511.5f + 512.0f))); + union pack10_u num; + + num.pack.x = v[0] * 511.0f; + num.pack.y = v[1] * 511.0f; + num.pack.z = v[2] * 511.0f; + num.pack.w = v[3]; + + return num.i; } else { - return (((uint32_t)(v[3] * 127.5f + 128.0f)) << 24) - | (((uint32_t)(v[2] * 127.5f + 128.0f)) << 16) - | (((uint32_t)(v[1] * 127.5f + 128.0f)) << 8) - | (((uint32_t)(v[0] * 127.5f + 128.0f))); + union pack8_u num; + + num.pack.x = v[0] * 127.0f; + num.pack.y = v[1] * 127.0f; + num.pack.z = v[2] * 127.0f; + num.pack.w = v[3] * 127.0f; + + return num.i; } } uint32_t R_VaoPackNormal(vec3_t v) { - if (glRefConfig.packedNormalDataType == GL_UNSIGNED_INT_2_10_10_10_REV) + if (glRefConfig.packedNormalDataType == GL_INT_2_10_10_10_REV) { - return (((uint32_t)(v[2] * 511.5f + 512.0f)) << 20) - | (((uint32_t)(v[1] * 511.5f + 512.0f)) << 10) - | (((uint32_t)(v[0] * 511.5f + 512.0f))); + union pack10_u num; + + num.pack.x = v[0] * 511.0f; + num.pack.y = v[1] * 511.0f; + num.pack.z = v[2] * 511.0f; + num.pack.w = 0; + + return num.i; } else { - return (((uint32_t)(v[2] * 127.5f + 128.0f)) << 16) - | (((uint32_t)(v[1] * 127.5f + 128.0f)) << 8) - | (((uint32_t)(v[0] * 127.5f + 128.0f))); + union pack8_u num; + + num.pack.x = v[0] * 127.0f; + num.pack.y = v[1] * 127.0f; + num.pack.z = v[2] * 127.0f; + num.pack.w = 0; + + return num.i; } } void R_VaoUnpackTangent(vec4_t v, uint32_t b) { - if (glRefConfig.packedNormalDataType == GL_UNSIGNED_INT_2_10_10_10_REV) + if (glRefConfig.packedNormalDataType == GL_INT_2_10_10_10_REV) { - v[0] = ((b) & 0x3ff) * 1.0f/511.5f - 1.0f; - v[1] = ((b >> 10) & 0x3ff) * 1.0f/511.5f - 1.0f; - v[2] = ((b >> 20) & 0x3ff) * 1.0f/511.5f - 1.0f; - v[3] = ((b >> 30) & 0x3) * 1.0f/1.5f - 1.0f; + union pack10_u num; + + num.i = b; + + v[0] = num.pack.x / 511.0f; + v[1] = num.pack.y / 511.0f; + v[2] = num.pack.z / 511.0f; + v[3] = num.pack.w; } else { - v[0] = ((b) & 0xff) * 1.0f/127.5f - 1.0f; - v[1] = ((b >> 8) & 0xff) * 1.0f/127.5f - 1.0f; - v[2] = ((b >> 16) & 0xff) * 1.0f/127.5f - 1.0f; - v[3] = ((b >> 24) & 0xff) * 1.0f/127.5f - 1.0f; + union pack8_u num; + + num.i = b; + + v[0] = num.pack.x / 127.0f; + v[1] = num.pack.y / 127.0f; + v[2] = num.pack.z / 127.0f; + v[3] = num.pack.w / 127.0f; } } void R_VaoUnpackNormal(vec3_t v, uint32_t b) { - if (glRefConfig.packedNormalDataType == GL_UNSIGNED_INT_2_10_10_10_REV) + if (glRefConfig.packedNormalDataType == GL_INT_2_10_10_10_REV) { - v[0] = ((b) & 0x3ff) * 1.0f/511.5f - 1.0f; - v[1] = ((b >> 10) & 0x3ff) * 1.0f/511.5f - 1.0f; - v[2] = ((b >> 20) & 0x3ff) * 1.0f/511.5f - 1.0f; + union pack10_u num; + + num.i = b; + + v[0] = num.pack.x / 511.0f; + v[1] = num.pack.y / 511.0f; + v[2] = num.pack.z / 511.0f; } else { - v[0] = ((b) & 0xff) * 1.0f/127.5f - 1.0f; - v[1] = ((b >> 8) & 0xff) * 1.0f/127.5f - 1.0f; - v[2] = ((b >> 16) & 0xff) * 1.0f/127.5f - 1.0f; + union pack8_u num; + + num.i = b; + + v[0] = num.pack.x / 127.0f; + v[1] = num.pack.y / 127.0f; + v[2] = num.pack.z / 127.0f; } } @@ -651,6 +706,7 @@ void RB_UpdateTessVao(unsigned int attribBits) if(tess.numVertexes > 0 && tess.numVertexes <= SHADER_MAX_VERTEXES && tess.numIndexes > 0 && tess.numIndexes <= SHADER_MAX_INDEXES) { int attribIndex; + int attribUpload; R_BindVao(tess.vao); @@ -661,25 +717,29 @@ void RB_UpdateTessVao(unsigned int attribBits) if(!(attribBits & ATTR_BITS)) attribBits = ATTR_BITS; - if(attribBits & ATTR_TEXCOORD || attribBits & ATTR_LIGHTCOORD) + attribUpload = attribBits; + + if((attribUpload & ATTR_TEXCOORD) || (attribUpload & ATTR_LIGHTCOORD)) { // these are interleaved, so we update both if either need it // this translates to updating ATTR_TEXCOORD twice as large as it needs - attribBits &= ~ATTR_LIGHTCOORD; - attribBits |= ATTR_TEXCOORD; + attribUpload &= ~ATTR_LIGHTCOORD; + attribUpload |= ATTR_TEXCOORD; } for (attribIndex = 0; attribIndex < ATTR_INDEX_COUNT; attribIndex++) { uint32_t attribBit = 1 << attribIndex; + vaoAttrib_t *vAtb = &tess.vao->attribs[attribIndex]; + + if (attribUpload & attribBit) + { + // note: tess has a VBO where stride == size + qglBufferSubDataARB(GL_ARRAY_BUFFER_ARB, vAtb->offset, tess.numVertexes * vAtb->stride, tess.attribPointers[attribIndex]); + } if (attribBits & attribBit) { - vaoAttrib_t *vAtb = &tess.vao->attribs[attribIndex]; - - // note: tess has a VBO where stride == size - qglBufferSubDataARB(GL_ARRAY_BUFFER_ARB, vAtb->offset, tess.numVertexes * vAtb->stride, tess.attribPointers[attribIndex]); - if (!glRefConfig.vertexArrayObject) qglVertexAttribPointerARB(attribIndex, vAtb->count, vAtb->type, vAtb->normalized, vAtb->stride, BUFFER_OFFSET(vAtb->offset));