From a2aa8af97395eb2043822db19730a3676127b5b1 Mon Sep 17 00:00:00 2001 From: rdb Date: Mon, 2 Jan 2023 10:46:48 +0100 Subject: [PATCH] Correctly support 4-component UV coordinates in generated shaders Supports projective UVs, matching fixed-function pipeline behavior --- .../glstuff/glGraphicsStateGuardian_src.cxx | 30 +++++++++---------- panda/src/pgraphnodes/shaderGenerator.cxx | 5 +++- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/panda/src/glstuff/glGraphicsStateGuardian_src.cxx b/panda/src/glstuff/glGraphicsStateGuardian_src.cxx index 702da1ce2c..687a8bbafb 100644 --- a/panda/src/glstuff/glGraphicsStateGuardian_src.cxx +++ b/panda/src/glstuff/glGraphicsStateGuardian_src.cxx @@ -185,16 +185,16 @@ static const string default_vshader = #endif "in vec4 p3d_Vertex;\n" "in vec4 p3d_Color;\n" - "in vec2 p3d_MultiTexCoord0;\n" - "out vec2 texcoord;\n" + "in vec4 p3d_MultiTexCoord0;\n" + "out vec3 texcoord;\n" "out vec4 color;\n" #else "#version 100\n" "precision mediump float;\n" "attribute vec4 p3d_Vertex;\n" "attribute vec4 p3d_Color;\n" - "attribute vec2 p3d_MultiTexCoord0;\n" - "varying vec2 texcoord;\n" + "attribute vec4 p3d_MultiTexCoord0;\n" + "varying vec3 texcoord;\n" "varying lowp vec4 color;\n" #endif "uniform mat4 p3d_ModelViewProjectionMatrix;\n" @@ -202,7 +202,7 @@ static const string default_vshader = "uniform vec4 p3d_ColorScale;\n" "void main(void) {\n" " gl_Position = p3d_ModelViewProjectionMatrix * p3d_Vertex;\n" - " texcoord = (p3d_TextureMatrix * vec4(p3d_MultiTexCoord0.x, p3d_MultiTexCoord0.y, 0, 1)).xy;\n" + " texcoord = (p3d_TextureMatrix * p3d_MultiTexCoord0).xyw;\n" " color = p3d_Color * p3d_ColorScale;\n" "}\n"; @@ -218,8 +218,8 @@ static const string default_vshader_fp64 = "#extension GL_ARB_gpu_shader_fp64 : require\n" "in dvec3 p3d_Vertex;\n" "in vec4 p3d_Color;\n" - "in dvec2 p3d_MultiTexCoord0;\n" - "out vec2 texcoord;\n" + "in dvec4 p3d_MultiTexCoord0;\n" + "out vec3 texcoord;\n" "out vec4 color;\n" "uniform mat4 p3d_ModelViewMatrix;\n" "uniform mat4 p3d_ProjectionMatrix;\n" @@ -227,7 +227,7 @@ static const string default_vshader_fp64 = "uniform vec4 p3d_ColorScale;\n" "void main(void) {\n" // Apply proj & modelview in two steps, more precise " gl_Position = vec4(dmat4(p3d_ProjectionMatrix) * (dmat4(p3d_ModelViewMatrix) * dvec4(p3d_Vertex, 1)));\n" - " texcoord = (p3d_TextureMatrix * vec4(p3d_MultiTexCoord0.x, p3d_MultiTexCoord0.y, 0, 1)).xy;\n" + " texcoord = (p3d_TextureMatrix * vec4(p3d_MultiTexCoord0)).xyw;\n" " color = p3d_Color * p3d_ColorScale;\n" "}\n"; @@ -236,8 +236,8 @@ static const string default_vshader_fp64_gl41 = "#version 410\n" "in dvec3 p3d_Vertex;\n" "in vec4 p3d_Color;\n" - "in dvec2 p3d_MultiTexCoord0;\n" - "out vec2 texcoord;\n" + "in dvec4 p3d_MultiTexCoord0;\n" + "out vec3 texcoord;\n" "out vec4 color;\n" "uniform mat4 p3d_ModelViewMatrix;\n" "uniform mat4 p3d_ProjectionMatrix;\n" @@ -245,7 +245,7 @@ static const string default_vshader_fp64_gl41 = "uniform vec4 p3d_ColorScale;\n" "void main(void) {\n" // Apply proj & modelview in two steps, more precise " gl_Position = vec4(dmat4(p3d_ProjectionMatrix) * (dmat4(p3d_ModelViewMatrix) * dvec4(p3d_Vertex, 1)));\n" - " texcoord = (p3d_TextureMatrix * vec4(p3d_MultiTexCoord0.x, p3d_MultiTexCoord0.y, 0, 1)).xy;\n" + " texcoord = (p3d_TextureMatrix * vec4(p3d_MultiTexCoord0)).xyw;\n" " color = p3d_Color * p3d_ColorScale;\n" "}\n"; #endif @@ -257,7 +257,7 @@ static const string default_fshader = #else "#version 130\n" #endif - "in vec2 texcoord;\n" + "in vec3 texcoord;\n" "in vec4 color;\n" "out vec4 p3d_FragColor;\n" "uniform sampler2D p3d_Texture0;\n" @@ -265,18 +265,18 @@ static const string default_fshader = #else "#version 100\n" "precision mediump float;\n" - "varying vec2 texcoord;\n" + "varying vec3 texcoord;\n" "varying lowp vec4 color;\n" "uniform lowp sampler2D p3d_Texture0;\n" "uniform lowp vec4 p3d_TexAlphaOnly;\n" #endif "void main(void) {\n" #ifndef OPENGLES - " p3d_FragColor = texture(p3d_Texture0, texcoord);\n" + " p3d_FragColor = textureProj(p3d_Texture0, texcoord);\n" " p3d_FragColor += p3d_TexAlphaOnly;\n" // Hack for text rendering " p3d_FragColor *= color;\n" #else - " gl_FragColor = texture2D(p3d_Texture0, texcoord);\n" + " gl_FragColor = texture2DProj(p3d_Texture0, texcoord);\n" " gl_FragColor += p3d_TexAlphaOnly;\n" // Hack for text rendering " gl_FragColor *= color;\n" #endif diff --git a/panda/src/pgraphnodes/shaderGenerator.cxx b/panda/src/pgraphnodes/shaderGenerator.cxx index 6712ec7f7b..f6e9c559fd 100644 --- a/panda/src/pgraphnodes/shaderGenerator.cxx +++ b/panda/src/pgraphnodes/shaderGenerator.cxx @@ -1218,6 +1218,9 @@ synthesize_shader(const RenderState *rs, const GeomVertexAnimationSpec &anim) { text << "\t texcoord" << i << ".xyz -= parallax_offset;\n"; } text << "\t float4 tex" << i << " = tex" << texture_type_as_string(tex._type); + if (tex._type == Texture::TT_2d_texture) { + text << "proj"; + } text << "(tex_" << i << ", texcoord" << i << "."; switch (tex._type) { case Texture::TT_cube_map: @@ -1226,7 +1229,7 @@ synthesize_shader(const RenderState *rs, const GeomVertexAnimationSpec &anim) { text << "xyz"; break; case Texture::TT_2d_texture: - text << "xy"; + text << "xyw"; break; case Texture::TT_1d_texture: text << "x";