Optimise dyn lights a bit

This commit is contained in:
Emile Belanger 2021-03-08 21:18:01 +00:00
parent 40135c4c86
commit 0a28d50b12
4 changed files with 45 additions and 50 deletions

View file

@ -108,6 +108,8 @@ bool FGLRenderState::ApplyShader()
fogset = -gl_fogmode;
}
}
if (mSpecialEffect > EFF_NONE)
{
@ -150,7 +152,10 @@ bool FGLRenderState::ApplyShader()
bool doDesaturate = false;
doDesaturate = mStreamData.uDesaturationFactor != 0;
activeShader->Bind(textureMode, texFlags, blendFlags, twoDFog, fogEnabled, fogEquationRadial, colouredFog, doDesaturate);
bool dynLights = false;
dynLights = (mLightIndex >= 0);
activeShader->Bind(textureMode, texFlags, blendFlags, twoDFog, fogEnabled, fogEquationRadial, colouredFog, doDesaturate, dynLights);
}
@ -255,25 +260,14 @@ bool FGLRenderState::ApplyShader()
matrixToGL(identityMatrix, activeShader->cur->normalmodelmatrix_index);
}
int index = mLightIndex;
// Mess alert for crappy AncientGL!
if (!screen->mLights->GetBufferType() && index >= 0)
if (mLightIndex >= 0)
{
/*
size_t start, size;
index = screen->mLights->GetBinding(index, &start, &size);
if (start != mLastMappedLightIndex)
{
mLastMappedLightIndex = start;
static_cast<GLDataBuffer*>(screen->mLights->GetBuffer())->BindRange(nullptr, start, size);
}
*/
float* ptr = ((float*)screen->mLights->GetBuffer()->Memory());
ptr += ((int64_t)index * 4);
ptr += ((int64_t)mLightIndex * 4);
float array[64];
memcpy(array, ptr, 4 * 64);
glUniform4fv(activeShader->cur->lights_index, 64, ptr);
activeShader->cur->muLightIndex.Set(0);

View file

@ -646,9 +646,9 @@ FShader::~FShader()
//
//==========================================================================
bool FShader::Bind(int textureMode, int texFlags, int blendFlags, bool twoDFog, bool fogEnabled, bool fogEquationRadial, bool colouredFog,bool doDesaturate)
bool FShader::Bind(int textureMode, int texFlags, int blendFlags, bool twoDFog, bool fogEnabled, bool fogEquationRadial, bool colouredFog, bool doDesaturate, bool dynLights)
{
uint32_t tag = CreateShaderTag(textureMode, texFlags, blendFlags, twoDFog, fogEnabled, fogEquationRadial, colouredFog, doDesaturate);
uint32_t tag = CreateShaderTag(textureMode, texFlags, blendFlags, twoDFog, fogEnabled, fogEquationRadial, colouredFog, doDesaturate, dynLights);
cur = variants[tag];
@ -665,6 +665,7 @@ bool FShader::Bind(int textureMode, int texFlags, int blendFlags, bool twoDFog,
variantConfig.AppendFormat("#define DEF_FOG_COLOURED %d\n", colouredFog);
variantConfig.AppendFormat("#define DEF_DO_DESATURATE %d\n", doDesaturate);
variantConfig.AppendFormat("#define DEF_DYNAMIC_LIGHTS %d\n", dynLights);
Printf("Shader: %s", variantConfig.GetChars());
@ -878,7 +879,7 @@ FShader *FShaderCollection::BindEffect(int effect)
{
if (effect >= 0 && effect < MAX_EFFECTS && mEffectShaders[effect] != NULL)
{
mEffectShaders[effect]->Bind(0,0,0,0,0,0,0,0);
mEffectShaders[effect]->Bind(0,0,0,0,0,0,0,0,0);
return mEffectShaders[effect];
}
return NULL;

View file

@ -353,7 +353,7 @@ public:
void LoadVariant();
uint32_t CreateShaderTag(int textureMode, int texf, int blendFlags, bool twoDFog, bool fogEnabled, bool fogEquationRadial, bool colouredFog, bool doDesaturate)
uint32_t CreateShaderTag(int textureMode, int texf, int blendFlags, bool twoDFog, bool fogEnabled, bool fogEquationRadial, bool colouredFog, bool doDesaturate, bool dynLights)
{
uint32_t tag = 0;
tag |= (textureMode & 0x7);
@ -368,12 +368,13 @@ public:
tag |= (colouredFog & 1) << 10;
tag |= (doDesaturate & 1) << 11;
tag |= (dynLights & 1) << 12;
return tag;
}
bool Bind(int textureMode, int texFlags, int blendFlags, bool twoDFog, bool fogEnabled, bool fogEquationRadial, bool colouredFog, bool doDesaturate);
bool Bind(int textureMode, int texFlags, int blendFlags, bool twoDFog, bool fogEnabled, bool fogEquationRadial, bool colouredFog, bool doDesaturate, bool dynLights);
};

View file

@ -42,43 +42,42 @@ vec3 ProcessMaterialLight(Material material, vec3 color)
vec4 dynlight = uDynLightColor;
vec3 normal = material.Normal;
if (uLightIndex >= 0)
{
ivec4 lightRange = ivec4(lights[uLightIndex]) + ivec4(uLightIndex + 1);
if (lightRange.z > lightRange.x)
{
// modulated lights
for(int i=lightRange.x; i<lightRange.y; i+=4)
{
dynlight.rgb += lightContribution(i, normal);
}
#if (DEF_DYNAMIC_LIGHTS == 1)
ivec4 lightRange = ivec4(lights[uLightIndex]) + ivec4(uLightIndex + 1);
// subtractive lights
for(int i=lightRange.y; i<lightRange.z; i+=4)
{
dynlight.rgb -= lightContribution(i, normal);
}
//if (lightRange.z > lightRange.x)
{
// modulated lights
for(int i=lightRange.x; i<lightRange.y; i+=4)
{
dynlight.rgb += lightContribution(i, normal);
}
// subtractive lights
for(int i=lightRange.y; i<lightRange.z; i+=4)
{
dynlight.rgb -= lightContribution(i, normal);
}
}
vec3 frag = material.Base.rgb * clamp(color + desaturate(dynlight).rgb, 0.0, 1.4);
if (uLightIndex >= 0)
//if (lightRange.w > lightRange.z)
{
ivec4 lightRange = ivec4(lights[uLightIndex]) + ivec4(uLightIndex + 1);
if (lightRange.w > lightRange.z)
{
vec4 addlight = vec4(0.0,0.0,0.0,0.0);
vec4 addlight = vec4(0.0,0.0,0.0,0.0);
// additive lights
for(int i=lightRange.z; i<lightRange.w; i+=4)
{
addlight.rgb += lightContribution(i, normal);
}
frag = clamp(frag + desaturate(addlight).rgb, 0.0, 1.0);
// additive lights
for(int i=lightRange.z; i<lightRange.w; i+=4)
{
addlight.rgb += lightContribution(i, normal);
}
frag = clamp(frag + desaturate(addlight).rgb, 0.0, 1.0);
}
return frag;
#else
return material.Base.rgb * clamp(color, 0.0, 1.4);
#endif
}