Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: linear fog mode (required for Source Engine compatible fog) #223

Merged
merged 3 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions engine/client/renderer.c
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,7 @@ cvar_t vid_desktopgamma = CVARFD ("vid_desktopgamma", "0",
CVAR_ARCHIVE | CVAR_RENDERERLATCH, "Apply gamma ramps upon the desktop rather than the window.");

cvar_t r_fog_cullentities = CVARD ("r_fog_cullentities", "1", "0: Never cull entities by fog...\n1: Automatically cull entities according to fog.\n2: Force fog culling regardless ");
cvar_t r_fog_linear = CVARD ("r_fog_linear", "0", "0: Use Exp/Exp2 fog. 1: Use linear fog.");
cvar_t r_fog_exp2 = CVARD ("r_fog_exp2", "1", "Expresses how fog fades with distance. 0 (matching DarkPlaces's default) is typically more realistic, while 1 (matching FitzQuake and others) is more common.");
cvar_t r_fog_permutation = CVARFD ("r_fog_permutation", "1", CVAR_SHADERSYSTEM, "Renders fog using a material permutation. 0 plays nicer with q3 shaders, but 1 is otherwise a better choice.");

Expand Down Expand Up @@ -602,6 +603,7 @@ void GLRenderer_Init(void)
Cvar_Register (&gl_overbright_all, GRAPHICALNICETIES);
Cvar_Register (&gl_dither, GRAPHICALNICETIES);
Cvar_Register (&r_fog_cullentities, GRAPHICALNICETIES);
Cvar_Register (&r_fog_linear, GLRENDEREROPTIONS);
Cvar_Register (&r_fog_exp2, GLRENDEREROPTIONS);
Cvar_Register (&r_fog_permutation, GLRENDEREROPTIONS);

Expand Down
5 changes: 4 additions & 1 deletion engine/d3d/vid_d3d.c
Original file line number Diff line number Diff line change
Expand Up @@ -1270,9 +1270,12 @@ static void (D3D9_R_RenderView) (void)

if (!r_refdef.globalfog.density)
{
extern cvar_t r_fog_linear;

int fogtype = ((r_refdef.flags & RDF_UNDERWATER) && cl.fog[FOGTYPE_WATER].density)?FOGTYPE_WATER:FOGTYPE_AIR;
CL_BlendFog(&r_refdef.globalfog, &cl.oldfog[fogtype], realtime, &cl.fog[fogtype]);
r_refdef.globalfog.density /= 64; //FIXME
if (!r_fog_linear.ival)
r_refdef.globalfog.density /= 64; //FIXME
}

//check if we can do underwater warp
Expand Down
17 changes: 11 additions & 6 deletions engine/gl/gl_backend.c
Original file line number Diff line number Diff line change
Expand Up @@ -1441,6 +1441,8 @@
Shader_DefaultScript(ps, shortname, shadertext);
}

extern cvar_t r_fog_linear;
extern cvar_t r_fog_exp2;
void GenerateFogTexture(texid_t *tex, float density, float zscale)
{
#define FOGS 256
Expand All @@ -1460,12 +1462,15 @@
z = (float)s / (FOGS-1);
z *= zscale;

if (0)//q3
f = pow(z, 0.5);
else if (1)//GL_EXP
f = 1-exp(-density * z);
else //GL_EXP2
f = 1-exp(-(density*density) * z);
if (r_fog_linear.ival) {
f = 1.0 - ((density - z) / (density/* - r_refdef.globalfog.depthbias*/)); //pow(z, 0.5);
} else {
if (!r_fog_exp2.ival)//GL_EXP
f = 1-exp(-density * z);
else //GL_EXP2
f = 1-exp(-(density*density) * z);
}

if (f < 0)
f = 0;
if (f > 1)
Expand Down Expand Up @@ -2237,10 +2242,10 @@
}
break;
case RGB_GEN_ENTITY_LIGHTING_DIFFUSE:
R_LightArrays(shaderstate.curentity, mesh->xyz_array, dst, cnt, mesh->normals_array, shaderstate.identitylighting, true);

Check warning on line 2245 in engine/gl/gl_backend.c

View workflow job for this annotation

GitHub Actions / make-macos-arm64

passing 4-byte aligned argument to 16-byte aligned parameter 3 of 'R_LightArrays' may result in an unaligned pointer access [-Walign-mismatch]
break;
case RGB_GEN_LIGHTING_DIFFUSE:
R_LightArrays(shaderstate.curentity, mesh->xyz_array, dst, cnt, mesh->normals_array, shaderstate.identitylighting, false);

Check warning on line 2248 in engine/gl/gl_backend.c

View workflow job for this annotation

GitHub Actions / make-macos-arm64

passing 4-byte aligned argument to 16-byte aligned parameter 3 of 'R_LightArrays' may result in an unaligned pointer access [-Walign-mismatch]
break;
case RGB_GEN_WAVE:
{
Expand Down Expand Up @@ -2799,7 +2804,7 @@
if (pass->flags & SHADER_PASS_NOCOLORARRAY)
{
colourgen(pass, 1, meshlist->colors4f_array[0], &shaderstate.pendingcolourflat, meshlist);
alphagen(pass, 1, meshlist->colors4f_array[0], &shaderstate.pendingcolourflat, meshlist);

Check warning on line 2807 in engine/gl/gl_backend.c

View workflow job for this annotation

GitHub Actions / make-macos-arm64

passing 4-byte aligned argument to 16-byte aligned parameter 4 of 'alphagen' may result in an unaligned pointer access [-Walign-mismatch]
shaderstate.colourarraytype = 0;
shaderstate.pendingcolourvbo = 0;
shaderstate.pendingcolourpointer = NULL;
Expand All @@ -2812,7 +2817,7 @@
if (shaderstate.mode == BEM_DEPTHDARK || shaderstate.mode == BEM_DEPTHONLY)
{
shaderstate.pendingcolourflat[0] = shaderstate.pendingcolourflat[1] = shaderstate.pendingcolourflat[2] = 0;
alphagen(pass, 1, meshlist->colors4f_array[0], &shaderstate.pendingcolourflat, meshlist);

Check warning on line 2820 in engine/gl/gl_backend.c

View workflow job for this annotation

GitHub Actions / make-macos-arm64

passing 4-byte aligned argument to 16-byte aligned parameter 4 of 'alphagen' may result in an unaligned pointer access [-Walign-mismatch]
shaderstate.colourarraytype = 0;
shaderstate.pendingcolourvbo = 0;
shaderstate.pendingcolourpointer = NULL;
Expand All @@ -2821,7 +2826,7 @@
if (shaderstate.mode == BEM_LIGHT)
{
shaderstate.pendingcolourflat[0] = shaderstate.pendingcolourflat[1] = shaderstate.pendingcolourflat[2] = 1;
alphagen(pass, 1, meshlist->colors4f_array[0], &shaderstate.pendingcolourflat, meshlist);

Check warning on line 2829 in engine/gl/gl_backend.c

View workflow job for this annotation

GitHub Actions / make-macos-arm64

passing 4-byte aligned argument to 16-byte aligned parameter 4 of 'alphagen' may result in an unaligned pointer access [-Walign-mismatch]
shaderstate.colourarraytype = 0;
shaderstate.pendingcolourvbo = 0;
shaderstate.pendingcolourpointer = NULL;
Expand All @@ -2830,7 +2835,7 @@
if (r_nolightdir.ival || (!shaderstate.curentity->light_range[0] && !shaderstate.curentity->light_range[1] && !shaderstate.curentity->light_range[2]))
{
VectorCopy(shaderstate.curentity->light_avg, shaderstate.pendingcolourflat);
alphagen(pass, 1, meshlist->colors4f_array[0], &shaderstate.pendingcolourflat, meshlist);

Check warning on line 2838 in engine/gl/gl_backend.c

View workflow job for this annotation

GitHub Actions / make-macos-arm64

passing 4-byte aligned argument to 16-byte aligned parameter 4 of 'alphagen' may result in an unaligned pointer access [-Walign-mismatch]
shaderstate.colourarraytype = 0;
shaderstate.pendingcolourvbo = 0;
shaderstate.pendingcolourpointer = NULL;
Expand Down
6 changes: 5 additions & 1 deletion engine/gl/gl_rmain.c
Original file line number Diff line number Diff line change
Expand Up @@ -2093,9 +2093,13 @@ void GLR_RenderView (void)

if (!r_refdef.globalfog.density)
{
extern cvar_t r_fog_linear;

int fogtype = ((r_refdef.flags & RDF_UNDERWATER) && cl.fog[FOGTYPE_WATER].density)?FOGTYPE_WATER:FOGTYPE_AIR;
CL_BlendFog(&r_refdef.globalfog, &cl.oldfog[fogtype], realtime, &cl.fog[fogtype]);
r_refdef.globalfog.density /= 64; //FIXME

if (!r_fog_linear.ival)
r_refdef.globalfog.density /= 64; //FIXME
}

if (!(r_refdef.flags & RDF_NOWORLDMODEL))
Expand Down
64 changes: 44 additions & 20 deletions engine/gl/gl_vidcommon.c
Original file line number Diff line number Diff line change
Expand Up @@ -1830,23 +1830,35 @@ static const char *glsl_hdrs[] =

"vec3 fog3(in vec3 regularcolour)"
"{"
"float z = w_fogdensity * gl_FragCoord.z / gl_FragCoord.w;\n"
"z = max(0.0,z-w_fogdepthbias);\n"
"#if #include \"cvar/r_fog_exp2\"\n"
"z *= z;\n"
"float z, fac;\n"
"#if #include \"cvar/r_fog_linear\"\n"
"z = gl_FragCoord.z / gl_FragCoord.w;\n"
"fac = (w_fogdensity - z) / (w_fogdensity - w_fogdepthbias);\n"
"#else\n"
"z = w_fogdensity * gl_FragCoord.z / gl_FragCoord.w;\n"
"z = max(0.0,z-w_fogdepthbias);\n"
"#if #include \"cvar/r_fog_exp2\"\n"
"z *= z;\n"
"#endif\n"
"fac = exp2(-(z * 1.442695));\n"
"#endif\n"
"float fac = exp2(-(z * 1.442695));\n"
"fac = (1.0-w_fogalpha) + (clamp(fac, 0.0, 1.0)*w_fogalpha);\n"
"return mix(w_fogcolour, regularcolour, fac);\n"
"}\n"
"vec3 fog3additive(in vec3 regularcolour)"
"{"
"float z = w_fogdensity * gl_FragCoord.z / gl_FragCoord.w;\n"
"z = max(0.0,z-w_fogdepthbias);\n"
"#if #include \"cvar/r_fog_exp2\"\n"
"z *= z;\n"
"float z, fac;\n"
"#if #include \"cvar/r_fog_linear\"\n"
"z = gl_FragCoord.z / gl_FragCoord.w;\n"
"fac = (w_fogdensity - z) / (w_fogdensity - w_fogdepthbias);\n"
"#else\n"
"z = w_fogdensity * gl_FragCoord.z / gl_FragCoord.w;\n"
"z = max(0.0,z-w_fogdepthbias);\n"
"#if #include \"cvar/r_fog_exp2\"\n"
"z *= z;\n"
"#endif\n"
"fac = exp2(-(z * 1.442695));\n"
"#endif\n"
"float fac = exp2(-(z * 1.442695));\n"
"fac = (1.0-w_fogalpha) + (clamp(fac, 0.0, 1.0)*w_fogalpha);\n"
"return regularcolour * fac;\n"
"}\n"
Expand All @@ -1856,23 +1868,35 @@ static const char *glsl_hdrs[] =
"}\n"
"vec4 fog4additive(in vec4 regularcolour)"
"{" //fog function for additive blends
"float z = w_fogdensity * gl_FragCoord.z / gl_FragCoord.w;\n"
"z = max(0.0,z-w_fogdepthbias);\n"
"#if #include \"cvar/r_fog_exp2\"\n"
"z *= z;\n"
"float z, fac;\n"
"#if #include \"cvar/r_fog_linear\"\n"
"z = gl_FragCoord.z / gl_FragCoord.w;\n"
"fac = (w_fogdensity - z) / (w_fogdensity - w_fogdepthbias);\n"
"#else\n"
"z = w_fogdensity * gl_FragCoord.z / gl_FragCoord.w;\n"
"z = max(0.0,z-w_fogdepthbias);\n"
"#if #include \"cvar/r_fog_exp2\"\n"
"z *= z;\n"
"#endif\n"
"fac = exp2(-(z * 1.442695));\n"
"#endif\n"
"float fac = exp2(-(z * 1.442695));\n"
"fac = (1.0-w_fogalpha) + (clamp(fac, 0.0, 1.0)*w_fogalpha);\n"
"return regularcolour * vec4(fac, fac, fac, 1.0);\n"
"}\n"
"vec4 fog4blend(in vec4 regularcolour)"
"{" //fog function for regular alpha blends (uses the blend for fading, to avoid fighting the surface behind)
"float z = w_fogdensity * gl_FragCoord.z / gl_FragCoord.w;\n"
"z = max(0.0,z-w_fogdepthbias);\n"
"#if #include \"cvar/r_fog_exp2\"\n"
"z *= z;\n"
"float z, fac;\n"
"#if #include \"cvar/r_fog_linear\"\n"
"z = gl_FragCoord.z / gl_FragCoord.w;\n"
"fac = (w_fogdensity - z) / (w_fogdensity - w_fogdepthbias);\n"
"#else\n"
"z = w_fogdensity * gl_FragCoord.z / gl_FragCoord.w;\n"
"z = max(0.0,z-w_fogdepthbias);\n"
"#if #include \"cvar/r_fog_exp2\"\n"
"z *= z;\n"
"#endif\n"
"fac = exp2(-(z * 1.442695));\n"
"#endif\n"
"float fac = exp2(-(z * 1.442695));\n"
"fac = (1.0-w_fogalpha) + (clamp(fac, 0.0, 1.0)*w_fogalpha);\n"
"return regularcolour * vec4(1.0, 1.0, 1.0, fac);\n"
"}\n"
Expand Down
80 changes: 60 additions & 20 deletions engine/shaders/vulkan/sys/fog.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,43 @@
{
if (!FOG)
return regularcolour;
float z = w_fogdensity * gl_FragCoord.z / gl_FragCoord.w;
z = max(0.0,z-w_fogdepthbias);
if (cvar_r_fog_exp2)
z *= z;
float fac = exp2(-(z * 1.442695));

float z;
float fac;

if (cvar_r_fog_linear) {
z = gl_FragCoord.z / gl_FragCoord.w;
fac = (w_fogdensity - z) / (w_fogdensity - w_fogdepthbias);
} else {
z = w_fogdensity * gl_FragCoord.z / gl_FragCoord.w;
z = max(0.0,z-w_fogdepthbias);
if (cvar_r_fog_exp2)
z *= z;
fac = exp2(-(z * 1.442695));
}

fac = (1.0-w_fogalpha) + (clamp(fac, 0.0, 1.0)*w_fogalpha);
return mix(w_fogcolour, regularcolour, fac);
}
vec3 fog3additive(in vec3 regularcolour)
{
if (!FOG)
return regularcolour;
float z = w_fogdensity * gl_FragCoord.z / gl_FragCoord.w;
z = max(0.0,z-w_fogdepthbias);
if (cvar_r_fog_exp2)
z *= z;
float fac = exp2(-(z * 1.442695));

float z;
float fac;

if (cvar_r_fog_linear) {
z = gl_FragCoord.z / gl_FragCoord.w;
fac = (w_fogdensity - z) / (w_fogdensity - w_fogdepthbias);
} else {
z = w_fogdensity * gl_FragCoord.z / gl_FragCoord.w;
z = max(0.0,z-w_fogdepthbias);
if (cvar_r_fog_exp2)
z *= z;
fac = exp2(-(z * 1.442695));
}

fac = (1.0-w_fogalpha) + (clamp(fac, 0.0, 1.0)*w_fogalpha);
return regularcolour * fac;
}
Expand All @@ -36,23 +56,43 @@
{
if (!FOG)
return regularcolour;
float z = w_fogdensity * gl_FragCoord.z / gl_FragCoord.w;
z = max(0.0,z-w_fogdepthbias);
if (cvar_r_fog_exp2)
z *= z;
float fac = exp2(-(z * 1.442695));

float z;
float fac;

if (cvar_r_fog_linear) {
z = gl_FragCoord.z / gl_FragCoord.w;
fac = (w_fogdensity - z) / (w_fogdensity - w_fogdepthbias);
} else {
z = w_fogdensity * gl_FragCoord.z / gl_FragCoord.w;
z = max(0.0,z-w_fogdepthbias);
if (cvar_r_fog_exp2)
z *= z;
fac = exp2(-(z * 1.442695));
}

fac = (1.0-w_fogalpha) + (clamp(fac, 0.0, 1.0)*w_fogalpha);
return regularcolour * vec4(fac, fac, fac, 1.0);
}
vec4 fog4blend(in vec4 regularcolour)
{
if (!FOG)
return regularcolour;
float z = w_fogdensity * gl_FragCoord.z / gl_FragCoord.w;
z = max(0.0,z-w_fogdepthbias);
if (cvar_r_fog_exp2)
z *= z;
float fac = exp2(-(z * 1.442695));

float z;
float fac;

if (cvar_r_fog_linear) {
z = gl_FragCoord.z / gl_FragCoord.w;
fac = (w_fogdensity - z) / (w_fogdensity - w_fogdepthbias);
} else {
z = w_fogdensity * gl_FragCoord.z / gl_FragCoord.w;
z = max(0.0,z-w_fogdepthbias);
if (cvar_r_fog_exp2)
z *= z;
fac = exp2(-(z * 1.442695));
}

fac = (1.0-w_fogalpha) + (clamp(fac, 0.0, 1.0)*w_fogalpha);
return regularcolour * vec4(1.0, 1.0, 1.0, fac);
}
Expand Down
5 changes: 4 additions & 1 deletion engine/vk/vk_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -1864,7 +1864,7 @@
cbai.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
cbai.commandBufferCount = 1;
VkAssert(vkAllocateCommandBuffers(vk.device, &cbai, &w->cbuf));
DebugSetName(VK_OBJECT_TYPE_COMMAND_BUFFER, (uint64_t)w->cbuf, "VK_FencedBegin");

Check warning on line 1867 in engine/vk/vk_init.c

View workflow job for this annotation

GitHub Actions / make-win32

cast from pointer to integer of different size [-Wpointer-to-int-cast]
cmdinf.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
cmdinf.pInheritanceInfo = &cmdinh;
vkBeginCommandBuffer(w->cbuf, &cmdinf);
Expand Down Expand Up @@ -2949,9 +2949,12 @@

if (!r_refdef.globalfog.density)
{
extern cvar_t r_fog_linear;

int fogtype = ((r_refdef.flags & RDF_UNDERWATER) && cl.fog[FOGTYPE_WATER].density)?FOGTYPE_WATER:FOGTYPE_AIR;
CL_BlendFog(&r_refdef.globalfog, &cl.oldfog[fogtype], realtime, &cl.fog[fogtype]);
r_refdef.globalfog.density /= 64; //FIXME
if (!r_fog_linear.ival)
r_refdef.globalfog.density /= 64; //FIXME
}

custompostproc = NULL;
Expand Down Expand Up @@ -3636,7 +3639,7 @@
cbai.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
cbai.commandBufferCount = frame->maxcbufs - frame->numcbufs;
VkAssert(vkAllocateCommandBuffers(vk.device, &cbai, frame->cbufs+frame->numcbufs));
DebugSetName(VK_OBJECT_TYPE_COMMAND_BUFFER, (uint64_t)frame->cbufs[frame->numcbufs], "VK_AllocFrameCBuf");

Check warning on line 3642 in engine/vk/vk_init.c

View workflow job for this annotation

GitHub Actions / make-win32

cast from pointer to integer of different size [-Wpointer-to-int-cast]
}
return frame->cbufs[frame->numcbufs++];
}
Expand Down
Loading