Skip to content

Commit

Permalink
* Fixed shader errors on older Radeon drivers due to having '0' const…
Browse files Browse the repository at this point in the history
…ants instead of '0.0'.

* Implemented a per-texture adjustment based on texture hue shift at 50% light level.
  • Loading branch information
luciusDXL committed Sep 26, 2023
1 parent ff8ddd1 commit 8820787
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 13 deletions.
16 changes: 16 additions & 0 deletions TheForceEngine/Shaders/gpu_render_wall.frag
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,29 @@ void main()
else
#endif
{
#ifdef OPT_TRUE_COLOR
vec3 tint;
baseColor = sampleTexture(Frag_TextureId, uv, sky, flip, applyFlatWarp, tint);
// Per-texture color correction factor.
if (light < 31.0)
{
float tintFactor = clamp((31.0 - light) / 15.0, 0.0, 1.0) * (1.0 - clamp((baseColor.a - 0.5) * 2.0, 0.0, 1.0));
baseColor.rgb *= mix(vec3(1.0), tint, tintFactor);
}
#else
baseColor = sampleTexture(Frag_TextureId, uv, sky, flip, applyFlatWarp);
#endif
}

// Handle sky fading.
if (skyFade > 0.0 && getBayerIndex(uv) < skyFade)
{
#ifdef OPT_TRUE_COLOR
vec3 tint; // ignore.
baseColor = sampleTexture(Frag_TextureId, vec2(256.0, yLimit), sky, flip, false, tint);
#else
baseColor = sampleTexture(Frag_TextureId, vec2(256.0, yLimit), sky, flip, false);
#endif
}

// Compute emissive.
Expand Down
43 changes: 38 additions & 5 deletions TheForceEngine/Shaders/textureSampleFunc.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ uniform isamplerBuffer TextureTable;
uniform sampler2D Colormap; // The color map has full RGB pre-backed in.
uniform sampler2D Palette;

uniform uint TextureSettings;

#ifdef OPT_TRUE_COLOR
uniform vec4 TexSamplingParam;
#endif
Expand Down Expand Up @@ -58,8 +60,8 @@ vec2 wrapCoordF(vec2 uv, vec2 edge)
uv += vec2(0.5,-0.5);

uv = fmod(uv, edge);
uv.x += (uv.x < 0) ? edge.x : 0;
uv.y += (uv.y < 0) ? edge.y : 0;
uv.x += (uv.x < 0.0) ? edge.x : 0.0;
uv.y += (uv.y < 0.0) ? edge.y : 0.0;

uv -= vec2(0.5,-0.5);
return uv;
Expand All @@ -70,8 +72,8 @@ vec2 wrapCoordFlatF(vec2 uv, vec2 edge)
{
float coord = fmod(uv.x, 64.0) * 64.0 + fmod(uv.y, 64.0);
uv = wrapCoordF(vec2(coord / edge.y, coord), edge);
uv.x += (uv.x < 0) ? edge.x : 0;
uv.y += (uv.y < 0) ? edge.y : 0;
uv.x += (uv.x < 0.0) ? edge.x : 0.0;
uv.y += (uv.y < 0.0) ? edge.y : 0.0;
return uv;
}

Expand All @@ -94,9 +96,25 @@ vec2 bilinearDither(vec2 uv)
// TODO: Make this optional.
#define OPT_MIPMAPPING 1

vec3 getHalfTint(ivec2 packedColor)
{
int r = (packedColor.x >> 15) & 255;
int g = (packedColor.x >> 23) & 255;
int b = (packedColor.y >> 15) & 255;

float scale = 1.0 / 255.0;
vec3 tint;
tint.x = float(r) * scale;
tint.y = float(g) * scale;
tint.z = float(b) * scale;
return tint;
}

vec4 sampleTexture(int id, vec2 uv)
{
ivec4 sampleData = texelFetch(TextureTable, id);
sampleData.zw &= ivec2(32767);

vec2 baseUv = uv;
uv = bilinearSharpness(uv, TexSamplingParam.x);

Expand All @@ -113,9 +131,12 @@ vec4 sampleTexture(int id, vec2 uv)
#endif
}

vec4 sampleTexture(int id, vec2 uv, bool sky, bool flip, bool applyFlatWarp)
vec4 sampleTexture(int id, vec2 uv, bool sky, bool flip, bool applyFlatWarp, out vec3 tint)
{
ivec4 sampleData = texelFetch(TextureTable, id);
tint = TextureSettings == 0u ? vec3(1.0) : getHalfTint(sampleData.zw);
sampleData.zw &= ivec2(32767);

vec2 baseUv = uv;
uv = bilinearSharpness(uv, TexSamplingParam.x);

Expand Down Expand Up @@ -172,6 +193,8 @@ vec4 sampleTexture(int id, vec2 uv, bool sky, bool flip, bool applyFlatWarp)
vec4 sampleTextureClamp(int id, vec2 uv)
{
ivec4 sampleData = texelFetch(TextureTable, id);
sampleData.zw &= ivec2(32767);

uv = bilinearSharpness(uv, TexSamplingParam.x);

vec3 uv3 = vec3(uv, 0.0);
Expand All @@ -184,6 +207,8 @@ vec4 sampleTextureClamp(int id, vec2 uv)
vec4 sampleTextureClamp(int id, vec2 uv, bool opaque)
{
ivec4 sampleData = texelFetch(TextureTable, id);
sampleData.zw &= ivec2(32767);

vec2 baseUv = uv;
uv = bilinearSharpness(uv, TexSamplingParam.x);

Expand All @@ -208,6 +233,8 @@ vec4 sampleTextureClamp(int id, vec2 uv, bool opaque)
float sampleTexture(int id, vec2 uv)
{
ivec4 sampleData = texelFetch(TextureTable, id);
sampleData.zw &= ivec2(32767);

ivec3 iuv;

#ifdef OPT_BILINEAR_DITHER
Expand All @@ -227,6 +254,8 @@ float sampleTexture(int id, vec2 uv)
float sampleTexture(int id, vec2 uv, bool sky, bool flip, bool applyFlatWarp)
{
ivec4 sampleData = texelFetch(TextureTable, id);
sampleData.zw &= ivec2(32767);

ivec3 iuv;

#ifdef OPT_BILINEAR_DITHER
Expand Down Expand Up @@ -274,6 +303,8 @@ float sampleTexture(int id, vec2 uv, bool sky, bool flip, bool applyFlatWarp)
float sampleTextureClamp(int id, vec2 uv)
{
ivec4 sampleData = texelFetch(TextureTable, id);
sampleData.zw &= ivec2(32767);

ivec3 iuv;

#ifdef OPT_BILINEAR_DITHER
Expand All @@ -297,6 +328,8 @@ float sampleTextureClamp(int id, vec2 uv)
float sampleTextureClamp(int id, vec2 uv, bool opaque)
{
ivec4 sampleData = texelFetch(TextureTable, id);
sampleData.zw &= ivec2(32767);

ivec3 iuv;

#ifdef OPT_BILINEAR_DITHER
Expand Down
7 changes: 7 additions & 0 deletions TheForceEngine/TFE_Jedi/Renderer/RClassic_GPU/modelGPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ namespace TFE_Jedi
{
extern s32 s_drawnObjCount;
extern SecObject* s_drawnObj[];
extern u32 s_textureSettings;

enum ModelShader
{
Expand Down Expand Up @@ -116,6 +117,7 @@ namespace TFE_Jedi
s32 texSamplingParamId;
s32 palFxLumMask;
s32 palFxFlash;
s32 textureSettings;
};
static ShaderInputs s_shaderInputs[MGPU_SHADER_COUNT];
static std::vector<ModelDraw> s_modelDrawList[MGPU_SHADER_COUNT];
Expand Down Expand Up @@ -196,6 +198,7 @@ namespace TFE_Jedi
s_shaderInputs[variant].texSamplingParamId = shader->getVariableId("TexSamplingParam");
s_shaderInputs[variant].palFxLumMask = shader->getVariableId("PalFxLumMask");
s_shaderInputs[variant].palFxFlash = shader->getVariableId("PalFxFlash");
s_shaderInputs[variant].textureSettings = shader->getVariableId("TextureSettings");

shader->bindTextureNameToSlot("Palette", 0);
shader->bindTextureNameToSlot("Colormap", 1);
Expand Down Expand Up @@ -827,6 +830,10 @@ namespace TFE_Jedi
shader->setVariable(s_shaderInputs[s].palFxLumMask, SVT_VEC3, lumMask.m);
shader->setVariable(s_shaderInputs[s].palFxFlash, SVT_VEC3, palFx.m);
}
if (s_shaderInputs[s].textureSettings >= 0)
{
shader->setVariable(s_shaderInputs[s].textureSettings, SVT_USCALAR, &s_textureSettings);
}

if (s == MGPU_SHADER_TRANS && s_shaderSettings.trueColor)
{
Expand Down
20 changes: 19 additions & 1 deletion TheForceEngine/TFE_Jedi/Renderer/RClassic_GPU/rsectorGPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ namespace TFE_Jedi
s32 texSamplingParamId;
s32 palFxLumMask;
s32 palFxFlash;
s32 textureSettings;
};
struct ShaderSkyInputs
{
Expand Down Expand Up @@ -144,6 +145,7 @@ namespace TFE_Jedi
static Vec3f s_clipObjPos;

static JBool s_flushCache = JFALSE;
u32 s_textureSettings = 1u;

extern Mat3 s_cameraMtx;
extern Mat4 s_cameraProj;
Expand Down Expand Up @@ -171,6 +173,7 @@ namespace TFE_Jedi
s_shaderInputs[SPRITE_PASS].texSamplingParamId = s_spriteShader.getVariableId("TexSamplingParam");
s_shaderInputs[SPRITE_PASS].palFxLumMask = s_spriteShader.getVariableId("PalFxLumMask");
s_shaderInputs[SPRITE_PASS].palFxFlash = s_spriteShader.getVariableId("PalFxFlash");
s_shaderInputs[SPRITE_PASS].textureSettings = s_spriteShader.getVariableId("TextureSettings");
s_cameraRightId = s_spriteShader.getVariableId("CameraRight");

s_spriteShader.bindTextureNameToSlot("DrawListPosXZ_Texture", 0);
Expand Down Expand Up @@ -206,6 +209,7 @@ namespace TFE_Jedi
s_shaderInputs[index].texSamplingParamId = s_wallShader[index].getVariableId("TexSamplingParam");
s_shaderInputs[index].palFxLumMask = s_wallShader[index].getVariableId("PalFxLumMask");
s_shaderInputs[index].palFxFlash = s_wallShader[index].getVariableId("PalFxFlash");
s_shaderInputs[index].textureSettings = s_wallShader[index].getVariableId("TextureSettings");

s_shaderSkyInputs[index].skyParallaxId = s_wallShader[index].getVariableId("SkyParallax");
s_shaderSkyInputs[index].skyParam0Id = s_wallShader[index].getVariableId("SkyParam0");
Expand Down Expand Up @@ -426,7 +430,6 @@ namespace TFE_Jedi
TFE_Image::writeImage("ColorMap.png", 256-32, 32, outImage);
#endif

//
for (s32 i = 1; i < 32; i++)
{
palIndex = getColormapWhiterampColor(i);
Expand Down Expand Up @@ -474,6 +477,7 @@ namespace TFE_Jedi
// RGB = Multiply color, A = ?
// RGB = Fog color, A = blendFactor
Vec4f mulRamp[32], fogRamp[32];
bool ignoreTextureTint = false;
for (s32 i = 0; i < fogRegionCount; i++)
{
curRegion = &regions[i];
Expand All @@ -483,6 +487,11 @@ namespace TFE_Jedi
Vec3f mulColor = curRegion->start == 31 ? white : curRegion->startColor;
Vec3f fogColor = curRegion->endColor;

if (fogColor.x > 0.1f || fogColor.y > 0.1f || fogColor.z > 0.1f)
{
ignoreTextureTint = true;
}

for (s32 j = curRegion->start; j >= curRegion->end; j--)
{
f32 fogFactor = f32(curRegion->start - j) / f32(curRegion->start - curRegion->end);
Expand Down Expand Up @@ -522,6 +531,7 @@ namespace TFE_Jedi
#endif
TFE_RenderBackend::freeTexture(s_trueColorMapping);
s_trueColorMapping = TFE_RenderBackend::createTexture(64, 1, mappingTable);
s_textureSettings = (ignoreTextureTint) ? 0 : 1;

// Generate a comparison color map.
#if SHOW_TRUE_COLOR_COMPARISION
Expand Down Expand Up @@ -1912,6 +1922,10 @@ namespace TFE_Jedi
shader->setVariable(s_shaderInputs[pass].palFxLumMask, SVT_VEC3, lumMask.m);
shader->setVariable(s_shaderInputs[pass].palFxFlash, SVT_VEC3, palFx.m);
}
if (s_shaderInputs[pass].textureSettings >= 0)
{
shader->setVariable(s_shaderInputs[pass].textureSettings, SVT_USCALAR, &s_textureSettings);
}

// Draw the sector display list.
sdisplayList_draw(pass);
Expand Down Expand Up @@ -1998,6 +2012,10 @@ namespace TFE_Jedi
s_spriteShader.setVariable(s_shaderInputs[SPRITE_PASS].palFxLumMask, SVT_VEC3, lumMask.m);
s_spriteShader.setVariable(s_shaderInputs[SPRITE_PASS].palFxFlash, SVT_VEC3, palFx.m);
}
if (s_shaderInputs[SPRITE_PASS].textureSettings >= 0)
{
s_spriteShader.setVariable(s_shaderInputs[SPRITE_PASS].textureSettings, SVT_USCALAR, &s_textureSettings);
}

// Draw the sector display list.
sprdisplayList_draw();
Expand Down
Loading

0 comments on commit 8820787

Please sign in to comment.