From dbc3cd5ff55dfd988dee772cf2d7b7d21983dd9d Mon Sep 17 00:00:00 2001 From: Benau Date: Wed, 27 Dec 2017 11:33:30 +0800 Subject: [PATCH] Normalize in shader for broken drivers --- data/shaders/sp_grass_pass.vert | 8 ++-- data/shaders/sp_grass_shadow.vert | 4 +- data/shaders/sp_normal_visualizer.geom | 2 +- data/shaders/sp_normal_visualizer.vert | 12 +++--- data/shaders/sp_pass.vert | 12 +++--- data/shaders/sp_shadow.vert | 4 +- data/shaders/sp_skinning.vert | 12 +++--- data/shaders/sp_skinning_shadow.vert | 4 +- data/shaders/utils/get_world_location.vert | 22 +++------- src/graphics/sp/sp_base.cpp | 1 - src/graphics/sp/sp_mesh_buffer.cpp | 48 +++++++--------------- 11 files changed, 49 insertions(+), 80 deletions(-) diff --git a/data/shaders/sp_grass_pass.vert b/data/shaders/sp_grass_pass.vert index a7e1e3a0f..18cdb6467 100644 --- a/data/shaders/sp_grass_pass.vert +++ b/data/shaders/sp_grass_pass.vert @@ -3,7 +3,7 @@ uniform vec3 wind_direction; layout(location = 0) in vec3 i_position; #if defined(Converts_10bit_Vector) -layout(location = 1) in int i_normal_pked; +layout(location = 1) in vec4 i_normal_orig; #else layout(location = 1) in vec4 i_normal; #endif @@ -13,7 +13,7 @@ layout(location = 3) in vec2 i_uv; layout(location = 8) in vec3 i_origin; #if defined(Converts_10bit_Vector) -layout(location = 9) in int i_rotation_pked; +layout(location = 9) in vec4 i_rotation_orig; #else layout(location = 9) in vec4 i_rotation; #endif @@ -56,8 +56,8 @@ void main() { #if defined(Converts_10bit_Vector) - vec4 i_normal = convert10BitVector(i_normal_pked); - vec4 i_rotation = convert10BitVector(i_rotation_pked); + vec4 i_normal = convert10BitVector(i_normal_orig); + vec4 i_rotation = convert10BitVector(i_rotation_orig); #endif #if defined(Use_Bindless_Texture) diff --git a/data/shaders/sp_grass_shadow.vert b/data/shaders/sp_grass_shadow.vert index 4d090c679..cc238b805 100644 --- a/data/shaders/sp_grass_shadow.vert +++ b/data/shaders/sp_grass_shadow.vert @@ -7,7 +7,7 @@ layout(location = 3) in vec2 i_uv; layout(location = 8) in vec3 i_origin; #if defined(Converts_10bit_Vector) -layout(location = 9) in int i_rotation_pked; +layout(location = 9) in vec4 i_rotation_orig; #else layout(location = 9) in vec4 i_rotation; #endif @@ -47,7 +47,7 @@ void main() { #if defined(Converts_10bit_Vector) - vec4 i_rotation = convert10BitVector(i_rotation_pked); + vec4 i_rotation = convert10BitVector(i_rotation_orig); #endif #if defined(Use_Bindless_Texture) diff --git a/data/shaders/sp_normal_visualizer.geom b/data/shaders/sp_normal_visualizer.geom index 21f6b32ec..c5693d192 100644 --- a/data/shaders/sp_normal_visualizer.geom +++ b/data/shaders/sp_normal_visualizer.geom @@ -21,7 +21,7 @@ void main() vec3 V1 = gl_in[2].gl_Position.xyz - gl_in[1].gl_Position.xyz; // calculate normal as perpendicular to two vectors of the triangle vec3 V0_V1_crossed = cross(V1, V0); - float normal_scale = clamp(length(V0_V1_crossed) * 10.0, 0.0, 0.25); + float normal_scale = min(length(V0_V1_crossed) * 10.0, 0.25); vec3 N = normalize(V0_V1_crossed); // normals of each vertex of the triangle diff --git a/data/shaders/sp_normal_visualizer.vert b/data/shaders/sp_normal_visualizer.vert index e3dc2a278..f95b1a1a0 100644 --- a/data/shaders/sp_normal_visualizer.vert +++ b/data/shaders/sp_normal_visualizer.vert @@ -3,13 +3,13 @@ uniform samplerBuffer skinning_tex; layout(location = 0) in vec3 i_position; #if defined(Converts_10bit_Vector) -layout(location = 1) in int i_normal_pked; +layout(location = 1) in vec4 i_normal_orig; #else layout(location = 1) in vec4 i_normal; #endif #if defined(Converts_10bit_Vector) -layout(location = 5) in int i_tangent_pked; +layout(location = 5) in vec4 i_tangent_orig; #else layout(location = 5) in vec4 i_tangent; #endif @@ -19,7 +19,7 @@ layout(location = 7) in vec4 i_weight; layout(location = 8) in vec3 i_origin; #if defined(Converts_10bit_Vector) -layout(location = 9) in int i_rotation_pked; +layout(location = 9) in vec4 i_rotation_orig; #else layout(location = 9) in vec4 i_rotation; #endif @@ -37,9 +37,9 @@ void main() { #if defined(Converts_10bit_Vector) - vec4 i_normal = convert10BitVector(i_normal_pked); - vec4 i_tangent = convert10BitVector(i_tangent_pked); - vec4 i_rotation = convert10BitVector(i_rotation_pked); + vec4 i_normal = convert10BitVector(i_normal_orig); + vec4 i_tangent = convert10BitVector(i_tangent_orig); + vec4 i_rotation = convert10BitVector(i_rotation_orig); #endif vec4 idle_position = vec4(i_position, 1.0); diff --git a/data/shaders/sp_pass.vert b/data/shaders/sp_pass.vert index 19477b068..a63d23d38 100644 --- a/data/shaders/sp_pass.vert +++ b/data/shaders/sp_pass.vert @@ -1,7 +1,7 @@ layout(location = 0) in vec3 i_position; #if defined(Converts_10bit_Vector) -layout(location = 1) in int i_normal_pked; +layout(location = 1) in vec4 i_normal_orig; #else layout(location = 1) in vec4 i_normal; #endif @@ -11,7 +11,7 @@ layout(location = 3) in vec2 i_uv; layout(location = 4) in vec2 i_uv_two; #if defined(Converts_10bit_Vector) -layout(location = 5) in int i_tangent_pked; +layout(location = 5) in vec4 i_tangent_orig; #else layout(location = 5) in vec4 i_tangent; #endif @@ -19,7 +19,7 @@ layout(location = 5) in vec4 i_tangent; layout(location = 8) in vec3 i_origin; #if defined(Converts_10bit_Vector) -layout(location = 9) in int i_rotation_pked; +layout(location = 9) in vec4 i_rotation_orig; #else layout(location = 9) in vec4 i_rotation; #endif @@ -68,9 +68,9 @@ void main() { #if defined(Converts_10bit_Vector) - vec4 i_normal = convert10BitVector(i_normal_pked); - vec4 i_tangent = convert10BitVector(i_tangent_pked); - vec4 i_rotation = convert10BitVector(i_rotation_pked); + vec4 i_normal = convert10BitVector(i_normal_orig); + vec4 i_tangent = convert10BitVector(i_tangent_orig); + vec4 i_rotation = convert10BitVector(i_rotation_orig); #endif #if defined(Use_Bindless_Texture) diff --git a/data/shaders/sp_shadow.vert b/data/shaders/sp_shadow.vert index bb29a7de0..755c9ce42 100644 --- a/data/shaders/sp_shadow.vert +++ b/data/shaders/sp_shadow.vert @@ -5,7 +5,7 @@ layout(location = 3) in vec2 i_uv; layout(location = 8) in vec3 i_origin; #if defined(Converts_10bit_Vector) -layout(location = 9) in int i_rotation_pked; +layout(location = 9) in vec4 i_rotation_orig; #else layout(location = 9) in vec4 i_rotation; #endif @@ -45,7 +45,7 @@ void main() { #if defined(Converts_10bit_Vector) - vec4 i_rotation = convert10BitVector(i_rotation_pked); + vec4 i_rotation = convert10BitVector(i_rotation_orig); #endif #if defined(Use_Bindless_Texture) diff --git a/data/shaders/sp_skinning.vert b/data/shaders/sp_skinning.vert index 771a7a044..5a2710eff 100644 --- a/data/shaders/sp_skinning.vert +++ b/data/shaders/sp_skinning.vert @@ -6,7 +6,7 @@ uniform samplerBuffer skinning_tex; layout(location = 0) in vec3 i_position; #if defined(Converts_10bit_Vector) -layout(location = 1) in int i_normal_pked; +layout(location = 1) in vec4 i_normal_orig; #else layout(location = 1) in vec4 i_normal; #endif @@ -16,7 +16,7 @@ layout(location = 3) in vec2 i_uv; layout(location = 4) in vec2 i_uv_two; #if defined(Converts_10bit_Vector) -layout(location = 5) in int i_tangent_pked; +layout(location = 5) in vec4 i_tangent_orig; #else layout(location = 5) in vec4 i_tangent; #endif @@ -26,7 +26,7 @@ layout(location = 7) in vec4 i_weight; layout(location = 8) in vec3 i_origin; #if defined(Converts_10bit_Vector) -layout(location = 9) in int i_rotation_pked; +layout(location = 9) in vec4 i_rotation_orig; #else layout(location = 9) in vec4 i_rotation; #endif @@ -75,9 +75,9 @@ void main() { #if defined(Converts_10bit_Vector) - vec4 i_normal = convert10BitVector(i_normal_pked); - vec4 i_tangent = convert10BitVector(i_tangent_pked); - vec4 i_rotation = convert10BitVector(i_rotation_pked); + vec4 i_normal = convert10BitVector(i_normal_orig); + vec4 i_tangent = convert10BitVector(i_tangent_orig); + vec4 i_rotation = convert10BitVector(i_rotation_orig); #endif #if defined(Use_Bindless_Texture) diff --git a/data/shaders/sp_skinning_shadow.vert b/data/shaders/sp_skinning_shadow.vert index 2c793f88e..3251a8c6d 100644 --- a/data/shaders/sp_skinning_shadow.vert +++ b/data/shaders/sp_skinning_shadow.vert @@ -14,7 +14,7 @@ layout(location = 7) in vec4 i_weight; layout(location = 8) in vec3 i_origin; #if defined(Converts_10bit_Vector) -layout(location = 9) in int i_rotation_pked; +layout(location = 9) in vec4 i_rotation_orig; #else layout(location = 9) in vec4 i_rotation; #endif @@ -55,7 +55,7 @@ void main() { #if defined(Converts_10bit_Vector) - vec4 i_rotation = convert10BitVector(i_rotation_pked); + vec4 i_rotation = convert10BitVector(i_rotation_orig); #endif #if defined(Use_Bindless_Texture) diff --git a/data/shaders/utils/get_world_location.vert b/data/shaders/utils/get_world_location.vert index bc8b7abcb..9ef97c388 100644 --- a/data/shaders/utils/get_world_location.vert +++ b/data/shaders/utils/get_world_location.vert @@ -11,24 +11,12 @@ vec4 getWorldPosition(vec3 origin, vec4 rotation, vec3 scale, vec3 local_pos) return vec4(local_pos, 1.0); } -vec4 convert10BitVector(int pked) +vec4 convert10BitVector(vec4 orig) { vec4 ret; - int part = pked & 1023; - float part_mix = float(clamp(int(part & 512), 0, 1)); - ret.x = mix(float(part), float(-1024 + part), part_mix) * 0.00195694715; - - part = (pked >> 10) & 1023; - part_mix = float(clamp(int(part & 512), 0, 1)); - ret.y = mix(float(part), float(-1024 + part), part_mix) * 0.00195694715; - - part = (pked >> 20) & 1023; - part_mix = float(clamp(int(part & 512), 0, 1)); - ret.z = mix(float(part), float(-1024 + part), part_mix) * 0.00195694715; - - part = pked >> 30; - part_mix = float(clamp(int(part & 2), 0, 1)); - ret.w = mix(float(part), -1.0f, part_mix); - + ret.x = orig.x * 0.00195694715; + ret.y = orig.y * 0.00195694715; + ret.z = orig.z * 0.00195694715; + ret.w = max(orig.w, -1.0); return ret; } diff --git a/src/graphics/sp/sp_base.cpp b/src/graphics/sp/sp_base.cpp index 702821e40..1c3e8088a 100644 --- a/src/graphics/sp/sp_base.cpp +++ b/src/graphics/sp/sp_base.cpp @@ -1,5 +1,4 @@ // SuperTuxKart - a fun racing game with go-kart -// SuperTuxKart - a fun racing game with go-kart // Copyright (C) 2017 SuperTuxKart-Team // // This program is free software; you can redistribute it and/or diff --git a/src/graphics/sp/sp_mesh_buffer.cpp b/src/graphics/sp/sp_mesh_buffer.cpp index 6ec6b57ec..8c0e98a1d 100644 --- a/src/graphics/sp/sp_mesh_buffer.cpp +++ b/src/graphics/sp/sp_mesh_buffer.cpp @@ -317,18 +317,13 @@ void SPMeshBuffer::recreateVAO(unsigned i) glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, pitch, (void*)offset); offset += 12; - // Normal + // Normal, if 10bit vector normalization is wrongly done by drivers, use + // original value and normalize ourselves in shader glEnableVertexAttribArray(1); - if (GraphicsRestrictions::isDisabled - (GraphicsRestrictions::GR_10BIT_VECTOR)) - { - glVertexAttribIPointer(1, 1, GL_INT, pitch, (void*)offset); - } - else - { - glVertexAttribPointer(1, 4, GL_INT_2_10_10_10_REV, GL_TRUE, pitch, - (void*)offset); - } + glVertexAttribPointer(1, 4, GL_INT_2_10_10_10_REV, + GraphicsRestrictions::isDisabled + (GraphicsRestrictions::GR_10BIT_VECTOR) ? GL_FALSE : GL_TRUE, pitch, + (void*)offset); offset += 4; // Vertex color if (m_vertex_color) @@ -363,18 +358,11 @@ void SPMeshBuffer::recreateVAO(unsigned i) { // Tangent and bi-tanget sign glEnableVertexAttribArray(5); - if (GraphicsRestrictions::isDisabled - (GraphicsRestrictions::GR_10BIT_VECTOR)) - { - glVertexAttribIPointer(5, 1, GL_INT, pitch, - (void*)offset); - } - else - { - glVertexAttribPointer(5, 4, GL_INT_2_10_10_10_REV, GL_TRUE, pitch, - (void*)offset); - } - offset += 4; + glVertexAttribPointer(5, 4, GL_INT_2_10_10_10_REV, + GraphicsRestrictions::isDisabled + (GraphicsRestrictions::GR_10BIT_VECTOR) ? GL_FALSE : GL_TRUE, + pitch, (void*)offset); + offset += 4; } if (m_skinned) { @@ -418,16 +406,10 @@ void SPMeshBuffer::recreateVAO(unsigned i) glVertexAttribDivisorARB(8, 1); // Rotation (quaternion .xyz) glEnableVertexAttribArray(9); - if (GraphicsRestrictions::isDisabled - (GraphicsRestrictions::GR_10BIT_VECTOR)) - { - glVertexAttribIPointer(9, 1, GL_INT, 32, (void*)12); - } - else - { - glVertexAttribPointer(9, 4, GL_INT_2_10_10_10_REV, GL_TRUE, 32, - (void*)12); - } + glVertexAttribPointer(9, 4, GL_INT_2_10_10_10_REV, + GraphicsRestrictions::isDisabled + (GraphicsRestrictions::GR_10BIT_VECTOR) ? GL_FALSE : GL_TRUE, 32, + (void*)12); glVertexAttribDivisorARB(9, 1); // Scale (3 half floats and .w for quaternion .w) glEnableVertexAttribArray(10);