Adapt material shaders to be cache-friendly. (Less defines, more uniforms)

This commit is contained in:
Mari the Deer 2025-02-26 13:37:31 +01:00
commit 1369d85c6b
29 changed files with 532 additions and 680 deletions

View file

@ -1,30 +1,33 @@
// Proxy shader for canvas textures
#define STRAIGHT_UVS ((CanvasFlags&0x01) != 0)
#define DONT_FIX_ALPHA ((CanvasFlags&0x02) != 0)
#define ALPHA_KEY ((CanvasFlags&0x04) != 0)
#define NO_FULLBRIGHT ((CanvasFlags&0x08) != 0)
void SetupMaterial( inout Material mat )
{
// canvas textures are upside-down when used by models
// this is the default use-case here, so for other situations,
// there's a macro for it™
#ifdef STRAIGHT_UVS
mat.Base = texture(scrtex,vTexCoord.st);
#else
mat.Base = texture(scrtex,vec2(vTexCoord.s,1.-vTexCoord.t));
#endif
if ( STRAIGHT_UVS )
mat.Base = texture(scrtex,vTexCoord.st);
else
mat.Base = texture(scrtex,vec2(vTexCoord.s,1.-vTexCoord.t));
// drawing with render styles applied can alter the alpha of the
// canvas itself, which may not be intended, so cap it to 100% here
// unless explicitly disabled
#ifndef DONT_FIX_ALPHA
mat.Base.a = 1.;
#endif
// treat a color (usually pure black) as fully transparent
if ( !DONT_FIX_ALPHA )
mat.Base.a = 1.;
// treat pure black as fully transparent
// used by some special textures
#ifdef ALPHA_KEY
if ( mat.Base.rgb == ALPHA_KEY )
mat.Base.a = 0.;
#endif
if ( ALPHA_KEY )
{
if ( mat.Base.rgb == vec3(0.) )
mat.Base.a = 0.;
}
mat.Normal = ApplyNormalMap(vTexCoord.st);
// by default, canvas textures are treated as fullbright
#ifndef NO_FULLBRIGHT
mat.Bright = vec4(1.);
#endif
if ( !NO_FULLBRIGHT )
mat.Bright = vec4(1.);
}

View file

@ -1,16 +1,6 @@
// equirectangular envmap + gradient rim
#define RECIPROCAL_PI2 0.15915494
#ifndef RIMSTEP
#define RIMSTEP .5
#endif
#ifndef ENVFACT
#define ENVFACT 1.
#endif
#ifndef RIMFACT
#define RIMFACT 1.
#endif
void SetupMaterial( inout Material mat )
{
vec3 norm = normalize(vWorldNormal.xyz);
@ -18,12 +8,10 @@ void SetupMaterial( inout Material mat )
vec3 rvec = normalize(reflect(eye,norm));
vec2 uv = vec2(atan(rvec.z,rvec.x)*RECIPROCAL_PI2+.5,asin(rvec.y)*RECIPROCAL_PI2+.5);
vec2 uv2 = vec2(atan(rvec.z,abs(rvec.x))*RECIPROCAL_PI2+.5,asin(rvec.y)*RECIPROCAL_PI2+.5);
vec3 envcol = textureGrad(tex,uv,dFdx(uv2),dFdy(uv2)).rgb*ENVFACT;
#ifdef RIM_LIGHTING
float rim = smoothstep(RIMSTEP,1.,1.-abs(dot(eye,norm)));
vec3 envcol = textureGrad(tex,uv,dFdx(uv2),dFdy(uv2)).rgb*EnvParams.x;
float rim = smoothstep(EnvParams.z,1.,1.-abs(dot(eye,norm)));
vec3 rimcol = textureLod(rimtex,vec2(.25+.5*rim,.5),0.).rgb;
envcol = mix(envcol,rimcol,rim*RIMFACT);
#endif
envcol = mix(envcol,rimcol,rim*EnvParams.y);
#ifdef BARRIER_MAP
vec3 grad = texture(bartex,vec2(0.,vTexCoord.t*5.+timer)).rgb;
envcol = (envcol+grad)*.25;

View file

@ -2,35 +2,15 @@
// allows for up to three different layers of environment mapping (RGB mask)
#define RECIPROCAL_PI2 0.15915494
#ifndef RIMSTEP
#define RIMSTEP .5
#endif
#ifndef ENVFACT
#define ENVFACT 1.
#endif
#ifndef RIMFACT
#define RIMFACT 1.
// keeping this as a define while using vec3 uniforms for the parameters
// ensures that we only need to compile three versions of this material shader
// (at least, because I don't know for sure how VKDoom handles this stuff)
#ifndef ENV_LAYERS
#define ENV_LAYERS 1
#endif
#ifndef RIMSTEP2
#define RIMSTEP2 .5
#endif
#ifndef ENVFACT2
#define ENVFACT2 1.
#endif
#ifndef RIMFACT2
#define RIMFACT2 1.
#endif
#ifndef RIMSTEP3
#define RIMSTEP3 .5
#endif
#ifndef ENVFACT3
#define ENVFACT3 1.
#endif
#ifndef RIMFACT3
#define RIMFACT3 1.
#endif
#define ENV_TWOLAYER ((ENV_LAYERS == 2) || (ENV_LAYERS == 3))
#define ENV_THREELAYER (ENV_LAYERS == 3)
void SetupMaterial( inout Material mat )
{
@ -44,29 +24,23 @@ void SetupMaterial( inout Material mat )
vec2 dTdx = dFdx(uv2);
vec2 dTdy = dFdy(uv2);
float rf = 1.-abs(dot(eye,norm));
vec3 envcol = textureGrad(envtex,uv,dTdx,dTdy).rgb*ENVFACT;
#ifdef RIM_LIGHTING
float rim = smoothstep(RIMSTEP,1.,rf);
vec3 envcol = textureGrad(envtex,uv,dTdx,dTdy).rgb*EnvFact.x;
float rim = smoothstep(RimStep.x,1.,rf);
vec3 rimcol = textureLod(rimtex,vec2(.25+.5*rim,.5),0.).rgb;
envcol = mix(envcol,rimcol,rim*RIMFACT);
#endif
envcol = mix(envcol,rimcol,rim*RimFact.x);
mat.Base = vec4(base.rgb+envcol*mask.x,base.a);
#if defined(ENV_TWOLAYER) || defined(ENV_THREELAYER)
envcol = textureGrad(envtex2,uv,dTdx,dTdy).rgb*ENVFACT2;
#ifdef RIM_LIGHTING
rim = smoothstep(RIMSTEP2,1.,rf);
#if ( ENV_TWOLAYER )
envcol = textureGrad(envtex2,uv,dTdx,dTdy).rgb*EnvFact.y;
rim = smoothstep(RimStep.y,1.,rf);
rimcol = textureLod(rimtex2,vec2(.25+.5*rim,.5),0.).rgb;
envcol = mix(envcol,rimcol,rim*RIMFACT2);
#endif
envcol = mix(envcol,rimcol,rim*RimFact.y);
mat.Base.rgb += envcol*mask.y;
#endif
#if defined(ENV_THREELAYER)
envcol = textureGrad(envtex3,uv,dTdx,dTdy).rgb*ENVFACT3;
#ifdef RIM_LIGHTING
rim = smoothstep(RIMSTEP3,1.,rf);
#if ( ENV_THREELAYER )
envcol = textureGrad(envtex3,uv,dTdx,dTdy).rgb*EnvFact.z;
rim = smoothstep(RimStep.z,1.,rf);
rimcol = textureLod(rimtex3,vec2(.25+.5*rim,.5),0.).rgb;
envcol = mix(envcol,rimcol,rim*RIMFACT3);
#endif
envcol = mix(envcol,rimcol,rim*RimFact.z);
mat.Base.rgb += envcol*mask.z;
#endif
mat.Normal = ApplyNormalMap(vTexCoord.st);

View file

@ -3,8 +3,8 @@
void SetupMaterial( inout Material mat )
{
mat.Base = getTexel(vTexCoord.st);
mat.Base.rgb *= OVERFACT;
mat.Base.rgb *= OverFact;
mat.Normal = ApplyNormalMap(vTexCoord.st);
mat.Bright = texture(brighttexture,vTexCoord.st);
mat.Bright.rgb *= OVERFACT;
mat.Bright.rgb *= OverFact;
}

View file

@ -2,16 +2,6 @@
// + dynamic glows (Plasma Blaster specific)
#define RECIPROCAL_PI2 0.15915494
#ifndef RIMSTEP
#define RIMSTEP .5
#endif
#ifndef ENVFACT
#define ENVFACT 1.
#endif
#ifndef RIMFACT
#define RIMFACT 1.
#endif
void SetupMaterial( inout Material mat )
{
vec4 base = getTexel(vTexCoord.st);
@ -21,12 +11,10 @@ void SetupMaterial( inout Material mat )
vec3 rvec = normalize(reflect(eye,norm));
vec2 uv = vec2(atan(rvec.z,rvec.x)*RECIPROCAL_PI2+.5,asin(rvec.y)*RECIPROCAL_PI2+.5);
vec2 uv2 = vec2(atan(rvec.z,abs(rvec.x))*RECIPROCAL_PI2+.5,asin(rvec.y)*RECIPROCAL_PI2+.5);
vec3 envcol = textureGrad(envtex,uv,dFdx(uv2),dFdy(uv2)).rgb*ENVFACT;
#ifdef RIM_LIGHTING
float rim = smoothstep(RIMSTEP,1.,1.-abs(dot(eye,norm)));
vec3 envcol = textureGrad(envtex,uv,dFdx(uv2),dFdy(uv2)).rgb*EnvParams.x;
float rim = smoothstep(EnvParams.z,1.,1.-abs(dot(eye,norm)));
vec3 rimcol = textureLod(rimtex,vec2(.25+.5*rim,.5),0.).rgb;
envcol = mix(envcol,rimcol,rim*RIMFACT);
#endif
envcol = mix(envcol,rimcol,rim*EnvParams.y);
mat.Base = vec4(base.rgb+envcol*mask,base.a);
mat.Normal = ApplyNormalMap(vTexCoord.st);
mat.Bright = texture(brighttexture,vTexCoord.st);