Unhack Brightmap handling for Palette Rendering

Turns out the Issue was just both the palette and lighttable textures were already bound to the texture unit the brightmap will be bound to, which led the shader to just try get the brightmap data from the lighttable texture instead of the brightmap texture
This commit is contained in:
Alug 2025-12-07 00:42:05 +01:00
parent 4c491d8647
commit fea8fe2505
6 changed files with 42 additions and 119 deletions

View file

@ -1809,8 +1809,8 @@ UINT32 HWR_GetLightTableID(extracolormap_t *colormap)
if (default_colormap)
colormap_pointer = colormaps; // don't actually use the data from the "default colormap"
else
colormap_pointer = colormap->colormap;
else
colormap_pointer = colormap->colormap;
colormap->gl_lighttable_id = HWR_CreateLightTable(colormap_pointer);
}

View file

@ -150,14 +150,11 @@ enum
SHADER_NONE = -1,
SHADER_FLOOR = 0,
SHADER_BRIGHTMAP_FLOOR,
SHADER_WALL,
SHADER_BRIGHTMAP_WALL,
SHADER_SPRITE,
SHADER_MODEL,
SHADER_MODEL_LIGHTING,
SHADER_WATER,
SHADER_BRIGHTMAP_WATER,
SHADER_FOG,
SHADER_SKY,
SHADER_PALETTE_POSTPROCESS,

View file

@ -547,12 +547,8 @@ static void HWR_RenderPlane(subsector_t *subsector, extrasubsector_t *xsub, bool
{
if (PolyFlags & PF_Fog)
shader = SHADER_FOG;
else if ((PolyFlags & PF_Ripple) && levelflat != NULL && levelflat->type == LEVELFLAT_TEXTURE && R_GetTextureBrightmap(levelflat->texture_id) && HWR_ShouldUsePaletteRendering())
shader = SHADER_BRIGHTMAP_WATER;
else if (PolyFlags & PF_Ripple)
shader = SHADER_WATER;
else if (levelflat != NULL && levelflat->type == LEVELFLAT_TEXTURE && R_GetTextureBrightmap(levelflat->texture_id) && HWR_ShouldUsePaletteRendering())
shader = SHADER_BRIGHTMAP_FLOOR;
else
shader = SHADER_FLOOR;
@ -744,7 +740,7 @@ static void HWR_AddTransparentWall(FOutVector *wallVerts, FSurfaceInfo *pSurf, I
//
// HWR_ProjectWall
//
static void HWR_ProjectWall(FOutVector *wallVerts, FSurfaceInfo *pSurf, FBITFIELD blendmode, INT32 lightlevel, extracolormap_t *wallcolormap, INT32 texnum)
static void HWR_ProjectWall(FOutVector *wallVerts, FSurfaceInfo *pSurf, FBITFIELD blendmode, INT32 lightlevel, extracolormap_t *wallcolormap)
{
INT32 shader = SHADER_NONE;
@ -752,11 +748,7 @@ static void HWR_ProjectWall(FOutVector *wallVerts, FSurfaceInfo *pSurf, FBITFIEL
if (HWR_UseShader())
{
if (R_GetTextureBrightmap(texnum) && HWR_ShouldUsePaletteRendering())
shader = SHADER_BRIGHTMAP_WALL;
else
shader = SHADER_WALL;
shader = SHADER_WALL;
blendmode |= PF_ColorMapped;
}
@ -919,7 +911,7 @@ static void HWR_SplitWall(sector_t *sector, FOutVector *wallVerts, INT32 texnum,
else if (polyflags & PF_EnvironmentTrans)
HWR_AddTransparentWall(wallVerts, Surf, texnum, noencore, polyflags, false, HWR_CalcWallLight(lightnum, gl_curline), colormap);
else
HWR_ProjectWall(wallVerts, Surf, polyflags, HWR_CalcWallLight(lightnum, gl_curline), colormap, texnum);
HWR_ProjectWall(wallVerts, Surf, polyflags, HWR_CalcWallLight(lightnum, gl_curline), colormap);
top = bot;
endtop = endbot;
@ -948,7 +940,7 @@ static void HWR_SplitWall(sector_t *sector, FOutVector *wallVerts, INT32 texnum,
else if (polyflags & PF_EnvironmentTrans)
HWR_AddTransparentWall(wallVerts, Surf, texnum, noencore, polyflags, false, HWR_CalcWallLight(lightnum, gl_curline), colormap);
else
HWR_ProjectWall(wallVerts, Surf, polyflags, HWR_CalcWallLight(lightnum, gl_curline), colormap, texnum);
HWR_ProjectWall(wallVerts, Surf, polyflags, HWR_CalcWallLight(lightnum, gl_curline), colormap);
}
// HWR_DrawSkyWall
@ -961,8 +953,8 @@ static void HWR_DrawSkyWall(FOutVector *wallVerts, FSurfaceInfo *Surf)
wallVerts[0].t = wallVerts[1].t = 0;
wallVerts[0].s = wallVerts[3].s = 0;
wallVerts[2].s = wallVerts[1].s = 0;
// this no longer sets top/bottom coords, this should be done before caling the function
HWR_ProjectWall(wallVerts, Surf, PF_Invisible|PF_NoTexture, 255, NULL, 0);
// this no longer sets top/bottom coords, this should be done before calling the function
HWR_ProjectWall(wallVerts, Surf, PF_Invisible|PF_NoTexture, 255, NULL);
// PF_Invisible so it's not drawn into the colour buffer
// PF_NoTexture for no texture
// PF_Occlude is set in HWR_ProjectWall to draw into the depth buffer
@ -1279,7 +1271,7 @@ static void HWR_ProcessSeg(void) // Sort of like GLWall::Process in GZDoom
else if (grTex->mipmap.flags & TF_TRANSPARENT)
HWR_AddTransparentWall(wallVerts, &Surf, gl_toptexture, noencore, polyflags, false, lightnum, colormap);
else
HWR_ProjectWall(wallVerts, &Surf, polyflags, lightnum, colormap, gl_toptexture);
HWR_ProjectWall(wallVerts, &Surf, polyflags, lightnum, colormap);
}
// check BOTTOM TEXTURE
@ -1343,7 +1335,7 @@ static void HWR_ProcessSeg(void) // Sort of like GLWall::Process in GZDoom
else if (grTex->mipmap.flags & TF_TRANSPARENT)
HWR_AddTransparentWall(wallVerts, &Surf, gl_bottomtexture, noencore, polyflags, false, lightnum, colormap);
else
HWR_ProjectWall(wallVerts, &Surf, polyflags, lightnum, colormap, gl_bottomtexture);
HWR_ProjectWall(wallVerts, &Surf, polyflags, lightnum, colormap);
}
// Render midtexture if there's one. Determine if it's visible first, though
@ -1566,7 +1558,7 @@ static void HWR_ProcessSeg(void) // Sort of like GLWall::Process in GZDoom
else if (!(blendmode & PF_Masked))
HWR_AddTransparentWall(wallVerts, &Surf, gl_midtexture, noencore, blendmode, false, lightnum, colormap);
else
HWR_ProjectWall(wallVerts, &Surf, blendmode, lightnum, colormap, gl_midtexture);
HWR_ProjectWall(wallVerts, &Surf, blendmode, lightnum, colormap);
}
// Sky culling
@ -1657,7 +1649,7 @@ static void HWR_ProcessSeg(void) // Sort of like GLWall::Process in GZDoom
if (grTex->mipmap.flags & TF_TRANSPARENT)
HWR_AddTransparentWall(wallVerts, &Surf, gl_midtexture, noencore, blendmode, false, lightnum, colormap);
else
HWR_ProjectWall(wallVerts, &Surf, blendmode, lightnum, colormap, gl_midtexture);
HWR_ProjectWall(wallVerts, &Surf, blendmode, lightnum, colormap);
}
}
}
@ -1865,7 +1857,7 @@ static void HWR_ProcessSeg(void) // Sort of like GLWall::Process in GZDoom
if (blendmode != PF_Masked)
HWR_AddTransparentWall(wallVerts, &Surf, texnum, noencore, blendmode, false, lightnum, colormap);
else
HWR_ProjectWall(wallVerts, &Surf, PF_Masked, lightnum, colormap, texnum);
HWR_ProjectWall(wallVerts, &Surf, PF_Masked, lightnum, colormap);
}
}
}
@ -1991,7 +1983,7 @@ static void HWR_ProcessSeg(void) // Sort of like GLWall::Process in GZDoom
if (blendmode != PF_Masked)
HWR_AddTransparentWall(wallVerts, &Surf, texnum, noencore, blendmode, false, lightnum, colormap);
else
HWR_ProjectWall(wallVerts, &Surf, PF_Masked, lightnum, colormap, texnum);
HWR_ProjectWall(wallVerts, &Surf, PF_Masked, lightnum, colormap);
}
}
}
@ -4242,7 +4234,7 @@ typedef struct
static wallinfo_t *wallinfo = NULL;
static size_t numwalls = 0; // a list of transparent walls to be drawn
void HWR_RenderWall(FOutVector *wallVerts, FSurfaceInfo *pSurf, FBITFIELD blend, boolean fogwall, INT32 lightlevel, extracolormap_t *wallcolormap, INT32 texnum);
void HWR_RenderWall(FOutVector *wallVerts, FSurfaceInfo *pSurf, FBITFIELD blend, boolean fogwall, INT32 lightlevel, extracolormap_t *wallcolormap);
#define MAX_TRANSPARENTWALL 256
@ -4506,7 +4498,7 @@ static void HWR_CreateDrawNodes(void)
if (!(sortnode[sortindex[i]].wall->blend & PF_NoTexture))
HWR_GetTexture(sortnode[sortindex[i]].wall->texnum, sortnode[sortindex[i]].wall->noencore);
HWR_RenderWall(sortnode[sortindex[i]].wall->wallVerts, &sortnode[sortindex[i]].wall->Surf, sortnode[sortindex[i]].wall->blend, sortnode[sortindex[i]].wall->fogwall,
sortnode[sortindex[i]].wall->lightlevel, sortnode[sortindex[i]].wall->wallcolormap, sortnode[sortindex[i]].wall->texnum);
sortnode[sortindex[i]].wall->lightlevel, sortnode[sortindex[i]].wall->wallcolormap);
}
}
@ -6506,7 +6498,7 @@ static void HWR_AddTransparentWall(FOutVector *wallVerts, FSurfaceInfo *pSurf, I
numwalls++;
}
void HWR_RenderWall(FOutVector *wallVerts, FSurfaceInfo *pSurf, FBITFIELD blend, boolean fogwall, INT32 lightlevel, extracolormap_t *wallcolormap, INT32 texnum)
void HWR_RenderWall(FOutVector *wallVerts, FSurfaceInfo *pSurf, FBITFIELD blend, boolean fogwall, INT32 lightlevel, extracolormap_t *wallcolormap)
{
FBITFIELD blendmode = blend;
UINT8 alpha = pSurf->PolyColor.s.alpha; // retain the alpha
@ -6525,8 +6517,6 @@ void HWR_RenderWall(FOutVector *wallVerts, FSurfaceInfo *pSurf, FBITFIELD blend,
{
if (fogwall)
shader = SHADER_FOG;
else if (R_GetTextureBrightmap(texnum) && HWR_ShouldUsePaletteRendering())
shader = SHADER_BRIGHTMAP_WALL;
else
shader = SHADER_WALL;

View file

@ -27,15 +27,9 @@ static struct {
// Floor shader
{GLSL_DEFAULT_VERTEX_SHADER, GLSL_FLOOR_FRAGMENT_SHADER},
// Brightmap Floor shader
{GLSL_DEFAULT_VERTEX_SHADER, GLSL_BRIGHTMAP_FLOOR_FRAGMENT_SHADER},
// Wall shader
{GLSL_DEFAULT_VERTEX_SHADER, GLSL_WALL_FRAGMENT_SHADER},
// Brightmap Wall shader
{GLSL_DEFAULT_VERTEX_SHADER, GLSL_BRIGHTMAP_WALL_FRAGMENT_SHADER},
// Sprite shader
{GLSL_DEFAULT_VERTEX_SHADER, GLSL_WALL_FRAGMENT_SHADER},
@ -48,9 +42,6 @@ static struct {
// Water shader
{GLSL_DEFAULT_VERTEX_SHADER, GLSL_WATER_FRAGMENT_SHADER},
// Brightmap Water shader
{GLSL_DEFAULT_VERTEX_SHADER, GLSL_WATER_FRAGMENT_SHADER_NOPAL},
// Fog shader
{GLSL_DEFAULT_VERTEX_SHADER, GLSL_FOG_FRAGMENT_SHADER},
@ -440,14 +431,11 @@ static inline UINT16 HWR_FindShaderDefs(UINT16 wadnum)
customshaderxlat_t shaderxlat[] =
{
{"Flat", SHADER_FLOOR},
{"BrightmapFloor", SHADER_BRIGHTMAP_FLOOR},
{"WallTexture", SHADER_WALL},
{"BrightmapWall", SHADER_BRIGHTMAP_WALL},
{"Sprite", SHADER_SPRITE},
{"Model", SHADER_MODEL},
{"ModelLight", SHADER_MODEL_LIGHTING},
{"WaterRipple", SHADER_WATER},
{"BrightmapWaterRipple", SHADER_BRIGHTMAP_WATER},
{"Fog", SHADER_FOG},
{"Sky", SHADER_SKY},
{"PalettePostprocess", SHADER_PALETTE_POSTPROCESS},
@ -457,11 +445,13 @@ customshaderxlat_t shaderxlat[] =
void HWR_LoadAllCustomShaders(void)
{
/*
INT32 i;
// read every custom shader
//for (i = 0; i < numwadfiles; i++)
// HWR_LoadCustomShadersFromFile(i, (type == RET_PK3)));
for (i = 0; i < numwadfiles; i++)
HWR_LoadCustomShadersFromFile(i, (type == RET_PK3)));
*/
}
void HWR_LoadCustomShadersFromFile(UINT16 wadnum, boolean PK3)

View file

@ -151,26 +151,26 @@
#define GLSL_PALETTE_RENDERING \
"float tex_pal_idx = texture3D(palette_lookup_tex, vec3((texel * 63.0 + 0.5) / 64.0))[0] * 255.0;\n" \
"float z = gl_FragCoord.z / gl_FragCoord.w;\n" \
"float light_y = clamp(floor(R_DoomColormap(lighting, z)), 0.0, 31.0);\n" \
"float light_y = clamp(floor(R_DoomColormap(final_lighting, z)), 0.0, 31.0);\n" \
"vec2 lighttable_coord = vec2((tex_pal_idx + 0.5) / 256.0, (light_y + 0.5) / 32.0);\n" \
"vec4 final_color = texture2D(lighttable_tex, lighttable_coord);\n" \
"final_color.a = texel.a * poly_color.a;\n" \
"float brightmap_mix = floor(texture2D(brightmap, gl_TexCoord[0].st).r);\n" \
"float light_gain = (255.0 - lighting) * brightmap_mix;\n" \
"float final_lighting = lighting + light_gain;\n" \
"gl_FragColor = final_color;\n" \
#define GLSL_SOFTWARE_FRAGMENT_SHADER \
"#ifdef SRB2_PALETTE_RENDERING\n" \
"uniform sampler2D tex;\n" \
"uniform sampler2D brightmap;\n" \
"uniform sampler3D palette_lookup_tex;\n" \
"uniform sampler2D lighttable_tex;\n" \
"uniform vec4 poly_color;\n" \
"uniform float lighting;\n" \
"uniform sampler3D palette_lookup_tex;\n" \
"uniform sampler2D lighttable_tex;\n" \
GLSL_DOOM_COLORMAP \
"void main(void) {\n" \
"vec4 texel = texture2D(tex, gl_TexCoord[0].st);\n" \
"float brightmap_mix = floor(texture2D(brightmap, gl_TexCoord[0].st).r);\n" \
"float light_gain = (255.0 - lighting) * brightmap_mix;\n" \
"float final_lighting = lighting + light_gain;\n" \
GLSL_PALETTE_RENDERING \
"}\n" \
"#else\n" \
@ -197,30 +197,6 @@
"gl_FragColor = final_color;\n" \
"}\n" \
"#endif\0"
#define GLSL_SOFTWARE_FRAGMENT_SHADER_NOPAL \
"uniform sampler2D tex;\n" \
"uniform sampler2D brightmap;\n" \
"uniform vec4 poly_color;\n" \
"uniform vec4 tint_color;\n" \
"uniform vec4 fade_color;\n" \
"uniform float lighting;\n" \
"uniform float fade_start;\n" \
"uniform float fade_end;\n" \
GLSL_DOOM_COLORMAP \
GLSL_DOOM_LIGHT_EQUATION \
"void main(void) {\n" \
"vec4 texel = texture2D(tex, gl_TexCoord[0].st);\n" \
"vec4 base_color = texel * poly_color;\n" \
"vec4 final_color = base_color;\n" \
"float brightmap_mix = floor(texture2D(brightmap, gl_TexCoord[0].st).r);\n" \
"float light_gain = (255.0 - lighting) * brightmap_mix;\n" \
"float final_lighting = lighting + light_gain;\n" \
GLSL_SOFTWARE_TINT_EQUATION \
GLSL_SOFTWARE_FADE_EQUATION \
"final_color.a = texel.a * poly_color.a;\n" \
"gl_FragColor = final_color;\n" \
"}\n" \
// hand tuned adjustments for light level calculation
#define GLSL_FLOOR_FUDGES \
@ -241,14 +217,6 @@
GLSL_WALL_FUDGES \
GLSL_SOFTWARE_FRAGMENT_SHADER
#define GLSL_BRIGHTMAP_FLOOR_FRAGMENT_SHADER \
GLSL_FLOOR_FUDGES \
GLSL_SOFTWARE_FRAGMENT_SHADER_NOPAL
#define GLSL_BRIGHTMAP_WALL_FRAGMENT_SHADER \
GLSL_WALL_FUDGES \
GLSL_SOFTWARE_FRAGMENT_SHADER_NOPAL
// same as above but multiplies results with the lighting value from the
// accompanying vertex shader (stored in gl_Color)
#define GLSL_SOFTWARE_MODEL_LIGHTING_FRAGMENT_SHADER \
@ -266,7 +234,11 @@
"#ifdef SRB2_MODEL_LIGHTING\n" \
"texel *= gl_Color;\n" \
"#endif\n" \
GLSL_PALETTE_RENDERING \
"float final_lighting = gl_Color.r * 255.0;\n" \
"float brightmap_mix = floor(texture2D(brightmap, gl_TexCoord[0].st).r);\n" \
"float light_gain = (255.0 - final_lighting) * brightmap_mix;\n" \
"final_lighting += light_gain;\n" \
GLSL_PALETTE_RENDERING \
"}\n" \
"#else\n" \
"uniform sampler2D tex;\n" \
@ -325,6 +297,9 @@
GLSL_DOOM_COLORMAP \
"void main(void) {\n" \
GLSL_WATER_TEXEL \
"float brightmap_mix = floor(texture2D(brightmap, gl_TexCoord[0].st).r);\n" \
"float light_gain = (255.0 - lighting) * brightmap_mix;\n" \
"float final_lighting = lighting + light_gain;\n" \
GLSL_PALETTE_RENDERING \
"}\n" \
"#else\n" \
@ -352,36 +327,6 @@
"gl_FragColor = final_color;\n" \
"}\n" \
"#endif\0"
#define GLSL_WATER_FRAGMENT_SHADER_NOPAL \
GLSL_FLOOR_FUDGES \
"const float freq = 0.025;\n" \
"const float amp = 0.025;\n" \
"const float speed = 2.0;\n" \
"const float pi = 3.14159;\n" \
"uniform sampler2D tex;\n" \
"uniform sampler2D brightmap;\n" \
"uniform vec4 poly_color;\n" \
"uniform vec4 tint_color;\n" \
"uniform vec4 fade_color;\n" \
"uniform float lighting;\n" \
"uniform float fade_start;\n" \
"uniform float fade_end;\n" \
"uniform float leveltime;\n" \
GLSL_DOOM_COLORMAP \
GLSL_DOOM_LIGHT_EQUATION \
"void main(void) {\n" \
GLSL_WATER_TEXEL \
"vec4 base_color = texel * poly_color;\n" \
"vec4 final_color = base_color;\n" \
"float brightmap_mix = floor(texture2D(brightmap, gl_TexCoord[0].st).r);\n" \
"float light_gain = (255.0 - lighting) * brightmap_mix;\n" \
"float final_lighting = lighting + light_gain;\n" \
GLSL_SOFTWARE_TINT_EQUATION \
GLSL_SOFTWARE_FADE_EQUATION \
"final_color.a = texel.a * poly_color.a;\n" \
"gl_FragColor = final_color;\n" \
"}\n" \
//
// Fog block shader

View file

@ -2051,9 +2051,10 @@ static boolean Shader_CompileProgram(gl_shader_t *shader, GLint i)
pglUseProgram(shader->program);
// texture unit numbers for the samplers used for palette rendering
UNIFORM_1(shader->uniforms[gluniform_palette_tex], 2, pglUniform1i);
UNIFORM_1(shader->uniforms[gluniform_palette_lookup_tex], 1, pglUniform1i);
UNIFORM_1(shader->uniforms[gluniform_lighttable_tex], 2, pglUniform1i);
UNIFORM_1(shader->uniforms[gluniform_palette_tex], 3, pglUniform1i);
UNIFORM_1(shader->uniforms[gluniform_palette_lookup_tex], 2, pglUniform1i);
UNIFORM_1(shader->uniforms[gluniform_lighttable_tex], 3, pglUniform1i);
// changed to tex units 2 and 3 due to brightmap handling, binding two things to the same unit will just overwrite each other!
// restore gl shader state
pglUseProgram(gl_shaderstate.program);
@ -2124,7 +2125,7 @@ static void PreparePolygon(FSurfaceInfo *pSurf, FOutVector *pOutVerts, FBITFIELD
if (pSurf->LightTableId && pSurf->LightTableId != lt_downloaded)
{
pglActiveTexture(GL_TEXTURE2);
pglActiveTexture(GL_TEXTURE3);
pglBindTexture(GL_TEXTURE_2D, pSurf->LightTableId);
pglActiveTexture(GL_TEXTURE0);
lt_downloaded = pSurf->LightTableId;
@ -3388,7 +3389,7 @@ EXPORT void HWRAPI(SetPaletteLookup)(UINT8 *lut)
}
if (!paletteLookupTex)
pglGenTextures(1, &paletteLookupTex);
pglActiveTexture(GL_TEXTURE1);
pglActiveTexture(GL_TEXTURE2);
pglBindTexture(GL_TEXTURE_3D, paletteLookupTex);
pglTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
pglTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
@ -3447,7 +3448,7 @@ EXPORT void HWRAPI(SetScreenPalette)(RGBA_t *palette)
memcpy(screenPalette, palette, sizeof(screenPalette));
if (!screenPaletteTex)
pglGenTextures(1, &screenPaletteTex);
pglActiveTexture(GL_TEXTURE2);
pglActiveTexture(GL_TEXTURE3);
pglBindTexture(GL_TEXTURE_1D, screenPaletteTex);
pglTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
pglTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);