From 5a8c57040f2bd5c72686701deab0c66668607362 Mon Sep 17 00:00:00 2001 From: Emile Belanger Date: Fri, 3 Dec 2021 18:06:50 +0000 Subject: [PATCH] GLES: Palette mode now GLES2 compliant. Interpolation not yet working. --- .../common/rendering/gles/gles_hwtexture.cpp | 25 ++- .../static/shaders_gles/glsl/func_paletted.fp | 149 ++++++++++-------- 2 files changed, 101 insertions(+), 73 deletions(-) diff --git a/source/common/rendering/gles/gles_hwtexture.cpp b/source/common/rendering/gles/gles_hwtexture.cpp index cd756b287..3bf889087 100644 --- a/source/common/rendering/gles/gles_hwtexture.cpp +++ b/source/common/rendering/gles/gles_hwtexture.cpp @@ -132,14 +132,23 @@ unsigned int FHardwareTexture::CreateTexture(unsigned char * buffer, int w, int #if USE_GLES2 - sourcetype = GL_BGRA; - texformat = GL_BGRA; + if (glTextureBytes == 1) + { + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); + sourcetype = GL_ALPHA; + texformat = GL_ALPHA; + } + else + { + sourcetype = GL_BGRA; + texformat = GL_BGRA; + } #else if (glTextureBytes == 1) { glPixelStorei(GL_UNPACK_ALIGNMENT, 1); sourcetype = GL_RED; - texformat = GL_R8; + texformat = GL_RED; } else { @@ -150,6 +159,16 @@ unsigned int FHardwareTexture::CreateTexture(unsigned char * buffer, int w, int glTexImage2D(GL_TEXTURE_2D, 0, texformat, rw, rh, 0, sourcetype, GL_UNSIGNED_BYTE, buffer); +#if !(USE_GLES2) + // The shader is using the alpha channel instead of red, this work on GLES but not on GL + // So the texture uses GL_RED and this swizzels the red channel into the alpha channel + if (glTextureBytes == 1) + { + GLint swizzleMask[] = { GL_ZERO, GL_ZERO, GL_ZERO, GL_RED }; + glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_RGBA, swizzleMask); + } +#endif + if (deletebuffer && buffer) free(buffer); if (mipmap && TexFilter[gl_texture_filter].mipmapping) diff --git a/wadsrc/static/shaders_gles/glsl/func_paletted.fp b/wadsrc/static/shaders_gles/glsl/func_paletted.fp index 8104185c1..50f9ddb6a 100644 --- a/wadsrc/static/shaders_gles/glsl/func_paletted.fp +++ b/wadsrc/static/shaders_gles/glsl/func_paletted.fp @@ -1,70 +1,79 @@ -const int RF_ShadeInterpolate = 64; - -vec4 ProcessTexel() -{ - float fullbright = 0.0; - vec4 color; - float coordX = vTexCoord.x; - float coordY = vTexCoord.y; - vec2 newCoord; - - // z is the depth in view space, positive going into the screen - float z = abs(pixelpos.w); - -#ifdef NPOT_EMULATION - // Coordinate adjustment for NPOT textures. It is somehow fitting that Build games exploited this texture wrapping quirk of the software rendering engine... -#if (DEF_NPOT_EMULATION == 1) - { - float period = floor(coordY / uNpotEmulation.y); - coordX += uNpotEmulation.x * floor(mod(coordY, uNpotEmulation.y)); - coordY = period + mod(coordY, uNpotEmulation.y); - } -#endif - -#endif - newCoord = vec2(coordX, coordY); - - color = texture(tex, newCoord); - -#if ((DEF_BLEND_FLAGS & 16384) != 0) - float visibility = max(uGlobVis * uLightFactor * z - 0.5, 0.0); -#else - float visibility = max(uGlobVis * uLightFactor * z, 0.0); -#endif - - float numShades = float(uPalLightLevels & 255); - float shade = (1.0 - uLightLevel) * (numShades); - shade = clamp((shade + visibility), 0.0, numShades - 1.0); - - int palindex = int(color.r * 255.0 + 0.1); // The 0.1 is for roundoff error compensation. - int shadeindex = int(floor(shade)); - float colorIndexF = texelFetch(texture3, ivec2(palindex, shadeindex), 0).r; - int colorIndex = int(colorIndexF * 255.0 + 0.1); // The 0.1 is for roundoff error compensation. - vec4 palettedColor = texelFetch(texture2, ivec2(colorIndex, 0), 0); - -#if ((DEF_BLEND_FLAGS & 16384) != 0) - { - // Get the next shaded palette index for interpolation - colorIndexF = texelFetch(texture3, ivec2(palindex, shadeindex+1), 0).r; - colorIndex = int(colorIndexF * 255.0 + 0.1); // The 0.1 is for roundoff error compensation. - vec4 palettedColorNext = texelFetch(texture2, ivec2(colorIndex, 0), 0); - float shadeFrac = mod(shade, 1.0); - palettedColor.rgb = mix(palettedColor.rgb, palettedColorNext.rgb, shadeFrac); - } -#endif - - //palettedColor.a = color.r == 0.0? 0.0 : 1.0;// 1.0-floor(color.r); - - // Replaces above line without branch - palettedColor.a = floor(color.r + 0.999); - - color = palettedColor; - - //if (color.a < uAlphaThreshold) discard; // it's only here that we have the alpha value available to be able to perform the alpha test. - - // This replaces the above line to avoid the branch and the discard.. Seems to look the same but could be unforeseen issues. - float alpha = step(uAlphaThreshold, color.a); - - return vec4(color.rgb, alpha); -} - +const int RF_ShadeInterpolate = 64; + +vec4 ProcessTexel() +{ + float fullbright = 0.0; + vec4 color; + float coordX = vTexCoord.x; + float coordY = vTexCoord.y; + vec2 newCoord; + + // z is the depth in view space, positive going into the screen + float z = abs(pixelpos.w); + +#ifdef NPOT_EMULATION + // Coordinate adjustment for NPOT textures. It is somehow fitting that Build games exploited this texture wrapping quirk of the software rendering engine... +#if (DEF_NPOT_EMULATION == 1) + float period = floor(coordY / uNpotEmulation.y); + coordX += uNpotEmulation.x * floor(mod(coordY, uNpotEmulation.y)); + coordY = period + mod(coordY, uNpotEmulation.y); +#endif + +#endif + + newCoord = vec2(coordX, coordY); + color = texture2D(tex, newCoord); + +#if 0 // Disable Interpolation for now + +#if ((DEF_BLEND_FLAGS & 16384) != 0) + float visibility = max(uGlobVis * uLightFactor * z - 0.5, 0.0); +#else + float visibility = max(uGlobVis * uLightFactor * z, 0.0); +#endif + +#else + float visibility = max(uGlobVis * uLightFactor * z, 0.0); +#endif + + float numShades = float(uPalLightLevels); + float shade = (1.0 - uLightLevel) * (numShades); + shade = clamp((shade + visibility), 0.0, numShades - 1.0); + + float comp = (1.0 / 256.0) / 2.0; // Half way through a pixel on 256 sized image + + float palindex = color.a + comp; + float shadeindex = (shade / (numShades)) + comp; + + float colorIndexF = texture2D(texture3, vec2(palindex, shadeindex)).a; + vec4 palettedColor = texture2D(texture2, vec2(colorIndexF + comp, 0.5)); + +#if 0 // Disable Interpolation for now + +#if ((DEF_BLEND_FLAGS & 16384) != 0) + + // Get the next shaded palette index for interpolation + colorIndexF = texelFetch(texture3, ivec2(palindex, shadeindex+1), 0).a; + colorIndex = int(colorIndexF * 255.0 + 0.1); // The 0.1 is for roundoff error compensation. + vec4 palettedColorNext = texelFetch(texture2, ivec2(colorIndex, 0), 0); + float shadeFrac = mod(shade, 1.0); + palettedColor.rgb = mix(palettedColor.rgb, palettedColorNext.rgb, shadeFrac); + +#endif + +#endif + + + //palettedColor.a = color.a == 0.0? 0.0 : 1.0; + // Replaces above line without branch + palettedColor.a = floor(color.a + 0.999); + + color = palettedColor; + + //if (color.a < uAlphaThreshold) discard; // it's only here that we have the alpha value available to be able to perform the alpha test. + // This replaces the above line to avoid the branch and the discard.. Seems to look the same but could be unforeseen issues. + float alpha = step(uAlphaThreshold, color.a); + + return vec4(color.rgb, alpha); +} +