OpenGL2: "Fix" cg_shadows 4.

This commit is contained in:
SmileTheory 2017-07-14 16:15:02 -07:00
parent 6a77f4e363
commit 6f1712dafe
5 changed files with 29 additions and 50 deletions

View file

@ -8,12 +8,6 @@ uniform float u_LightRadius;
varying vec3 var_Position; varying vec3 var_Position;
varying vec3 var_Normal; varying vec3 var_Normal;
float sampleDistMap(sampler2D texMap, vec2 uv, float scale)
{
vec3 distv = texture2D(texMap, uv).xyz;
return dot(distv, vec3(1.0 / (256.0 * 256.0), 1.0 / 256.0, 1.0)) * scale;
}
void main() void main()
{ {
vec3 lightToPos = var_Position - u_LightOrigin.xyz; vec3 lightToPos = var_Position - u_LightOrigin.xyz;
@ -57,40 +51,26 @@ void main()
#endif #endif
intensity *= fade; intensity *= fade;
#if defined(USE_PCF)
float part; float part;
#if defined(USE_PCF)
part = float(texture2D(u_ShadowMap, st + vec2(-1.0/512.0, -1.0/512.0)).r != 1.0);
part += float(texture2D(u_ShadowMap, st + vec2( 1.0/512.0, -1.0/512.0)).r != 1.0);
part += float(texture2D(u_ShadowMap, st + vec2(-1.0/512.0, 1.0/512.0)).r != 1.0);
part += float(texture2D(u_ShadowMap, st + vec2( 1.0/512.0, 1.0/512.0)).r != 1.0);
#else
part = float(texture2D(u_ShadowMap, st).r != 1.0);
#endif
dist = sampleDistMap(u_ShadowMap, st + vec2(-1.0/512.0, -1.0/512.0), u_LightRadius);
part = max(sign(lightDist - dist), 0.0);
dist = sampleDistMap(u_ShadowMap, st + vec2( 1.0/512.0, -1.0/512.0), u_LightRadius);
part += max(sign(lightDist - dist), 0.0);
dist = sampleDistMap(u_ShadowMap, st + vec2(-1.0/512.0, 1.0/512.0), u_LightRadius);
part += max(sign(lightDist - dist), 0.0);
dist = sampleDistMap(u_ShadowMap, st + vec2( 1.0/512.0, 1.0/512.0), u_LightRadius);
part += max(sign(lightDist - dist), 0.0);
#if defined(USE_DISCARD)
if (part <= 0.0) if (part <= 0.0)
{ {
discard; discard;
} }
#endif
#if defined(USE_PCF)
intensity *= part * 0.25; intensity *= part * 0.25;
#else #else
dist = sampleDistMap(u_ShadowMap, st, u_LightRadius); intensity *= part;
#if defined(USE_DISCARD)
if (lightDist - dist <= 0.0)
{
discard;
}
#endif
intensity *= max(sign(lightDist - dist), 0.0);
#endif #endif
gl_FragColor.rgb = vec3(0); gl_FragColor.rgb = vec3(0);

View file

@ -1604,26 +1604,26 @@ const void *RB_PostProcess(const void *data)
if (0 && r_sunlightMode->integer) if (0 && r_sunlightMode->integer)
{ {
ivec4_t dstBox; ivec4_t dstBox;
VectorSet4(dstBox, 0, 0, 128, 128); VectorSet4(dstBox, 0, glConfig.vidHeight - 128, 128, 128);
FBO_BlitFromTexture(tr.sunShadowDepthImage[0], NULL, NULL, NULL, dstBox, NULL, NULL, 0); FBO_BlitFromTexture(tr.sunShadowDepthImage[0], NULL, NULL, NULL, dstBox, NULL, NULL, 0);
VectorSet4(dstBox, 128, 0, 128, 128); VectorSet4(dstBox, 128, glConfig.vidHeight - 128, 128, 128);
FBO_BlitFromTexture(tr.sunShadowDepthImage[1], NULL, NULL, NULL, dstBox, NULL, NULL, 0); FBO_BlitFromTexture(tr.sunShadowDepthImage[1], NULL, NULL, NULL, dstBox, NULL, NULL, 0);
VectorSet4(dstBox, 256, 0, 128, 128); VectorSet4(dstBox, 256, glConfig.vidHeight - 128, 128, 128);
FBO_BlitFromTexture(tr.sunShadowDepthImage[2], NULL, NULL, NULL, dstBox, NULL, NULL, 0); FBO_BlitFromTexture(tr.sunShadowDepthImage[2], NULL, NULL, NULL, dstBox, NULL, NULL, 0);
VectorSet4(dstBox, 384, 0, 128, 128); VectorSet4(dstBox, 384, glConfig.vidHeight - 128, 128, 128);
FBO_BlitFromTexture(tr.sunShadowDepthImage[3], NULL, NULL, NULL, dstBox, NULL, NULL, 0); FBO_BlitFromTexture(tr.sunShadowDepthImage[3], NULL, NULL, NULL, dstBox, NULL, NULL, 0);
} }
if (0 && r_shadows->integer == 4) if (1 && r_shadows->integer == 4)
{ {
ivec4_t dstBox; ivec4_t dstBox;
VectorSet4(dstBox, 0, glConfig.vidHeight - 128, 128, 128); VectorSet4(dstBox, 512 + 0, glConfig.vidHeight - 128, 128, 128);
FBO_BlitFromTexture(tr.pshadowMaps[0], NULL, NULL, NULL, dstBox, NULL, NULL, 0); FBO_BlitFromTexture(tr.pshadowMaps[0], NULL, NULL, NULL, dstBox, NULL, NULL, 0);
VectorSet4(dstBox, 128, glConfig.vidHeight - 128, 128, 128); VectorSet4(dstBox, 512 + 128, glConfig.vidHeight - 128, 128, 128);
FBO_BlitFromTexture(tr.pshadowMaps[1], NULL, NULL, NULL, dstBox, NULL, NULL, 0); FBO_BlitFromTexture(tr.pshadowMaps[1], NULL, NULL, NULL, dstBox, NULL, NULL, 0);
VectorSet4(dstBox, 256, glConfig.vidHeight - 128, 128, 128); VectorSet4(dstBox, 512 + 256, glConfig.vidHeight - 128, 128, 128);
FBO_BlitFromTexture(tr.pshadowMaps[2], NULL, NULL, NULL, dstBox, NULL, NULL, 0); FBO_BlitFromTexture(tr.pshadowMaps[2], NULL, NULL, NULL, dstBox, NULL, NULL, 0);
VectorSet4(dstBox, 384, glConfig.vidHeight - 128, 128, 128); VectorSet4(dstBox, 512 + 384, glConfig.vidHeight - 128, 128, 128);
FBO_BlitFromTexture(tr.pshadowMaps[3], NULL, NULL, NULL, dstBox, NULL, NULL, 0); FBO_BlitFromTexture(tr.pshadowMaps[3], NULL, NULL, NULL, dstBox, NULL, NULL, 0);
} }

View file

@ -322,14 +322,14 @@ void FBO_Init(void)
R_CheckFBO(tr.sunRaysFbo); R_CheckFBO(tr.sunRaysFbo);
} }
// FIXME: Don't use separate color/depth buffers for a shadow buffer
if (MAX_DRAWN_PSHADOWS && tr.pshadowMaps[0]) if (MAX_DRAWN_PSHADOWS && tr.pshadowMaps[0])
{ {
for( i = 0; i < MAX_DRAWN_PSHADOWS; i++) for( i = 0; i < MAX_DRAWN_PSHADOWS; i++)
{ {
tr.pshadowFbos[i] = FBO_Create(va("_shadowmap%d", i), tr.pshadowMaps[i]->width, tr.pshadowMaps[i]->height); tr.pshadowFbos[i] = FBO_Create(va("_shadowmap%d", i), tr.pshadowMaps[i]->width, tr.pshadowMaps[i]->height);
FBO_AttachImage(tr.pshadowFbos[i], tr.pshadowMaps[i], GL_COLOR_ATTACHMENT0, 0); // FIXME: this next line wastes 16mb with 16x512x512 sun shadow maps, skip if OpenGL 4.3+ or ARB_framebuffer_no_attachments
FBO_CreateBuffer(tr.pshadowFbos[i], GL_DEPTH_COMPONENT24_ARB, 0, 0); FBO_CreateBuffer(tr.pshadowFbos[i], GL_RGBA8, 0, 0);
FBO_AttachImage(tr.pshadowFbos[i], tr.pshadowMaps[i], GL_DEPTH_ATTACHMENT, 0);
R_CheckFBO(tr.pshadowFbos[i]); R_CheckFBO(tr.pshadowFbos[i]);
} }
} }

View file

@ -2808,12 +2808,11 @@ void R_CreateBuiltinImages( void ) {
tr.screenSsaoImage = R_CreateImage("*screenSsao", NULL, width / 2, height / 2, IMGTYPE_COLORALPHA, IMGFLAG_NO_COMPRESSION | IMGFLAG_CLAMPTOEDGE, GL_RGBA8); tr.screenSsaoImage = R_CreateImage("*screenSsao", NULL, width / 2, height / 2, IMGTYPE_COLORALPHA, IMGFLAG_NO_COMPRESSION | IMGFLAG_CLAMPTOEDGE, GL_RGBA8);
} }
if (r_shadows->integer == 4) for( x = 0; x < MAX_DRAWN_PSHADOWS; x++)
{ {
for( x = 0; x < MAX_DRAWN_PSHADOWS; x++) tr.pshadowMaps[x] = R_CreateImage(va("*shadowmap%i", x), NULL, PSHADOW_MAP_SIZE, PSHADOW_MAP_SIZE, IMGTYPE_COLORALPHA, IMGFLAG_NO_COMPRESSION | IMGFLAG_CLAMPTOEDGE, GL_DEPTH_COMPONENT24);
{ //qglTextureParameterfEXT(tr.pshadowMaps[x]->texnum, GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
tr.pshadowMaps[x] = R_CreateImage(va("*shadowmap%i", x), NULL, PSHADOW_MAP_SIZE, PSHADOW_MAP_SIZE, IMGTYPE_COLORALPHA, IMGFLAG_NO_COMPRESSION | IMGFLAG_CLAMPTOEDGE, GL_RGBA8); //qglTextureParameterfEXT(tr.pshadowMaps[x]->texnum, GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
}
} }
if (r_sunlightMode->integer) if (r_sunlightMode->integer)

View file

@ -2098,7 +2098,7 @@ void R_RenderPshadowMaps(const refdef_t *fd)
if (glRefConfig.framebufferObject) if (glRefConfig.framebufferObject)
shadowParms.targetFbo = tr.pshadowFbos[i]; shadowParms.targetFbo = tr.pshadowFbos[i];
shadowParms.flags = VPF_SHADOWMAP | VPF_DEPTHSHADOW | VPF_NOVIEWMODEL; shadowParms.flags = VPF_DEPTHSHADOW | VPF_NOVIEWMODEL;
shadowParms.zFar = shadow->lightRadius; shadowParms.zFar = shadow->lightRadius;
VectorCopy(shadow->lightOrigin, shadowParms.or.origin); VectorCopy(shadow->lightOrigin, shadowParms.or.origin);