Added setAnim to camera. Added global fog set via worldspawn.

This commit is contained in:
Justin Marshall
2026-05-21 06:17:29 -07:00
parent 24e694c240
commit aec93deaa0
26 changed files with 571 additions and 156 deletions
+189 -42
View File
@@ -455,9 +455,9 @@ static const UINT QD3D12_UploadBufferSize = 128 * 1024 * 1024;
// MSAA has been removed from the G-buffer path. Temporal AA now owns edge cleanup
// after lighting, so all scene/deferred render targets remain single-sample.
static const UINT QD3D12_RequestedGBufferSampleCount = 1;
static constexpr UINT QD3D12_PostSrvCount = 9;
static constexpr UINT QD3D12_PostSrvCount = 11;
static constexpr UINT QD3D12_PostRootConstants = QD3D12_PostSrvCount;
static constexpr UINT QD3D12_PostConstantDwords = 8;
static constexpr UINT QD3D12_PostConstantDwords = 24;
static constexpr UINT QD3D12_ToneMapTileDim = 64;
enum QD3D12RTVSlotGroup
@@ -834,6 +834,12 @@ struct BatchKey
float tex1IsLightmap = 0.0f;
float texEnvMode0 = 0.0f;
float texEnvMode1 = 0.0f;
float fogEnabled = 0.0f;
float fogMode = 1.0f;
float fogDensity = 1.0f;
float fogStart = 0.0f;
float fogEnd = 1.0f;
float fogColor[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
GLenum blendSrc = GL_ONE;
GLenum blendDst = GL_ZERO;
@@ -913,6 +919,12 @@ static bool BatchKeyEquals(const BatchKey& a, const BatchKey& b)
a.tex1IsLightmap == b.tex1IsLightmap &&
a.texEnvMode0 == b.texEnvMode0 &&
a.texEnvMode1 == b.texEnvMode1 &&
a.fogEnabled == b.fogEnabled &&
a.fogMode == b.fogMode &&
a.fogDensity == b.fogDensity &&
a.fogStart == b.fogStart &&
a.fogEnd == b.fogEnd &&
memcmp(a.fogColor, b.fogColor, sizeof(a.fogColor)) == 0 &&
a.colorWriteMask == b.colorWriteMask &&
a.cullFaceEnabled == b.cullFaceEnabled &&
a.cullMode == b.cullMode &&
@@ -1262,6 +1274,14 @@ struct GLState
bool sceneResolvedThisFrame = false;
bool gbufferResolvedThisFrame = false;
bool raytracedLightingReadyThisFrame = false;
bool sceneFogValidThisFrame = false;
float sceneFogMode = 1.0f;
float sceneFogDensity = 1.0f;
float sceneFogStart = 0.0f;
float sceneFogEnd = 1.0f;
float sceneFogColor[4] = { 0.0f, 0.0f, 0.0f, 1.0f };
float sceneFogCameraWorldPos[3] = { 0.0f, 0.0f, 0.0f };
float sceneFogCameraValid = 0.0f;
float upscalerSharpness = 1.0f;
float toneMapBrightness = 1.0f;
@@ -3606,7 +3626,9 @@ VSOut VSMain(VSIn i)
o.prevClip = prevClip;
o.objPos = i.pos;
o.objNormal = i.normal;
o.fogCoord = abs(currClip.z / max(abs(currClip.w), 0.00001));
o.fogCoord = (gCameraPomValid > 0.5)
? length(worldPos.xyz - gCameraWorldPos)
: abs(currClip.z / max(abs(currClip.w), 0.00001));
o.uv0 = i.uv0;
o.uv1 = i.uv1;
o.col = i.col;
@@ -3838,7 +3860,9 @@ VSOut DSMain(HSConstOut tessFactors, float3 bary : SV_DomainLocation, const Outp
o.prevClip = prevClip;
o.objPos = displacedObjPos;
o.objNormal = objNormal;
o.fogCoord = abs(currClip.z / max(abs(currClip.w), 0.00001));
o.fogCoord = (gCameraPomValid > 0.5)
? length(worldPos.xyz - gCameraWorldPos)
: abs(currClip.z / max(abs(currClip.w), 0.00001));
o.uv0 = i.uv0;
o.uv1 = i.uv1;
o.col = i.col;
@@ -3930,14 +3954,22 @@ Texture2DMS<float4> gEmissiveMS : register(t5);
Texture2DMS<float4> gSpecularMS : register(t6);
Texture2D gTaaHistory : register(t7);
Texture2D gTaaVelocity : register(t8);
Texture2D<float4> gFogPosition : register(t9);
Texture2D<float> gFogDepth : register(t10);
SamplerState gSamp0 : register(s0);
cbuffer PostCB : register(b0)
{
// gPostParams0: x = sharpen strength, y = history weight, z = TAA reset flag.
// gPostParams1: x = tone-map exposure, y = white-point scale, z = display brightness.
// gPostParams2: x = fog enabled, y = fog mode, z = fog density, w = fog start.
// gPostParams3: x = fog end, y = fog camera valid.
float4 gPostParams0;
float4 gPostParams1;
float4 gPostParams2;
float4 gPostParams3;
float4 gPostFogColor;
float4 gPostFogCameraWorldPos;
};
#define gPostSharpness gPostParams0.x
@@ -3946,6 +3978,12 @@ cbuffer PostCB : register(b0)
#define gToneMapExposure gPostParams1.x
#define gToneMapWhiteScale gPostParams1.y
#define gToneMapBrightness gPostParams1.z
#define gPostFogEnabled gPostParams2.x
#define gPostFogMode gPostParams2.y
#define gPostFogDensity gPostParams2.z
#define gPostFogStart gPostParams2.w
#define gPostFogEnd gPostParams3.x
#define gPostFogCameraValid gPostParams3.y
#define QD3D12_TONEMAP_TILE_DIM 64u
struct VSOut
@@ -3967,9 +4005,49 @@ VSOut VSMain(uint vid : SV_VertexID)
return o;
}
float QD3D12_PostFogFactor(float fogCoord)
{
if (gPostFogEnabled < 0.5)
return 1.0;
float f;
if (gPostFogMode < 0.5)
{
float denom = max(gPostFogEnd - gPostFogStart, 0.00001);
f = (gPostFogEnd - fogCoord) / denom;
}
else if (gPostFogMode < 1.5)
{
f = exp(-gPostFogDensity * fogCoord);
}
else
{
float d = gPostFogDensity * fogCoord;
f = exp(-(d * d));
}
return saturate(f);
}
float4 QD3D12_ApplyPostFog(float4 color, float2 uv)
{
if (gPostFogEnabled < 0.5 || gPostFogCameraValid < 0.5)
return color;
float sceneDepth = gFogDepth.SampleLevel(gSamp0, uv, 0.0);
if (sceneDepth >= 0.99999)
return color;
float3 worldPos = gFogPosition.SampleLevel(gSamp0, uv, 0.0).xyz;
float fogCoord = length(worldPos - gPostFogCameraWorldPos.xyz);
float fogFactor = QD3D12_PostFogFactor(fogCoord);
float3 fogged = lerp(gPostFogColor.rgb, color.rgb, fogFactor);
color.rgb = lerp(color.rgb, fogged, saturate(gPostFogColor.a));
return color;
}
float4 PSCopy(VSOut i) : SV_Target0
{
return gTex0.Sample(gSamp0, i.uv);
return QD3D12_ApplyPostFog(gTex0.Sample(gSamp0, i.uv), i.uv);
}
float4 PSSharpen(VSOut i) : SV_Target0
@@ -3992,7 +4070,7 @@ float4 PSSharpen(VSOut i) : SV_Target0
// Sharpen strictly after temporal reconstruction. Keep it conservative so
// the TAA resolve is not undone by ringing on high-contrast UI/geometry edges.
float3 sharp = center.rgb + (center.rgb - blur) * amount;
return float4(saturate(sharp), center.a);
return QD3D12_ApplyPostFog(float4(saturate(sharp), center.a), i.uv);
}
@@ -4108,7 +4186,7 @@ float4 PSToneMap(VSOut i) : SV_Target0
// UNORM, not sRGB formats, and the legacy renderer already authored/displayed
// color in that space. A gamma conversion here is what made the prior patch
// look blown out.
return float4(mapped, saturate(center.a));
return QD3D12_ApplyPostFog(float4(mapped, saturate(center.a)), i.uv);
}
float4 PSTAA(VSOut i) : SV_Target0
@@ -4848,6 +4926,21 @@ static BatchKey BuildCurrentBatchKey(GLenum originalMode, const TextureResource*
key.tex1IsLightmap = useTex1 ? 1.0f : 0.0f;
key.texEnvMode0 = MapTexEnvMode(g_gl.texEnvMode[0]);
key.texEnvMode1 = MapTexEnvMode(g_gl.texEnvMode[1]);
key.fogEnabled = g_gl.fog ? 1.0f : 0.0f;
switch (g_gl.fogMode)
{
case GL_LINEAR: key.fogMode = 0.0f; break;
case GL_EXP: key.fogMode = 1.0f; break;
case GL_EXP2: key.fogMode = 2.0f; break;
default: key.fogMode = 1.0f; break;
}
key.fogDensity = g_gl.fogDensity;
key.fogStart = g_gl.fogStart;
key.fogEnd = g_gl.fogEnd;
key.fogColor[0] = g_gl.fogColor[0];
key.fogColor[1] = g_gl.fogColor[1];
key.fogColor[2] = g_gl.fogColor[2];
key.fogColor[3] = g_gl.fogColor[3];
QD3D12_FillTexCombineKey(key, 0);
QD3D12_FillTexCombineKey(key, 1);
key.colorWriteMask = QD3D12_CurrentColorWriteMask();
@@ -6320,32 +6413,60 @@ static void QD3D12_SetPostConstants(
float taaReset,
float toneMapExposure = 1.0f,
float toneMapWhiteScale = 1.0f,
float toneMapBrightness = 1.0f)
float toneMapBrightness = 1.0f,
bool applySceneFog = false)
{
if (!cl)
return;
float constants[QD3D12_PostConstantDwords] =
float constants[QD3D12_PostConstantDwords] = {};
constants[0] = sharpness;
constants[1] = taaHistoryWeight;
constants[2] = taaReset;
constants[4] = toneMapExposure;
constants[5] = toneMapWhiteScale;
constants[6] = toneMapBrightness;
if (applySceneFog && g_gl.sceneFogValidThisFrame)
{
sharpness,
taaHistoryWeight,
taaReset,
0.0f,
toneMapExposure,
toneMapWhiteScale,
toneMapBrightness,
0.0f
};
constants[8] = 1.0f;
constants[9] = g_gl.sceneFogMode;
constants[10] = g_gl.sceneFogDensity;
constants[11] = g_gl.sceneFogStart;
constants[12] = g_gl.sceneFogEnd;
constants[13] = g_gl.sceneFogCameraValid;
constants[16] = g_gl.sceneFogColor[0];
constants[17] = g_gl.sceneFogColor[1];
constants[18] = g_gl.sceneFogColor[2];
constants[19] = g_gl.sceneFogColor[3];
constants[20] = g_gl.sceneFogCameraWorldPos[0];
constants[21] = g_gl.sceneFogCameraWorldPos[1];
constants[22] = g_gl.sceneFogCameraWorldPos[2];
}
cl->SetGraphicsRoot32BitConstants(QD3D12_PostRootConstants, QD3D12_PostConstantDwords, constants, 0);
}
static void QD3D12_BindPostFogResources(ID3D12GraphicsCommandList* cl, QD3D12Window& w, bool applySceneFog)
{
if (!cl || !applySceneFog || !g_gl.sceneFogValidThisFrame)
return;
if (!w.positionBuffers[w.frameIndex] || !w.depthBuffer)
return;
if (w.positionSrvIndex[w.frameIndex] == UINT_MAX || w.depthSrvIndex == UINT_MAX)
return;
cl->SetGraphicsRootDescriptorTable(9, w.positionSrvGpu[w.frameIndex]);
cl->SetGraphicsRootDescriptorTable(10, w.depthSrvGpu);
}
static void QD3D12_PostFullscreenPass(ID3D12GraphicsCommandList* cl,
ID3D12PipelineState* pso,
D3D12_GPU_DESCRIPTOR_HANDLE inputSrv,
D3D12_CPU_DESCRIPTOR_HANDLE outputRtv,
const D3D12_VIEWPORT& viewport,
const D3D12_RECT& scissor,
float sharpnessOverride = -1.0f)
float sharpnessOverride = -1.0f,
bool applySceneFog = false,
QD3D12Window* fogWindow = nullptr)
{
if (!cl || !pso)
return;
@@ -6354,13 +6475,15 @@ static void QD3D12_PostFullscreenPass(ID3D12GraphicsCommandList* cl,
cl->SetDescriptorHeaps(_countof(heaps), heaps);
cl->SetGraphicsRootSignature(g_gl.postRootSig.Get());
const float passSharpness = (sharpnessOverride >= 0.0f) ? sharpnessOverride : g_gl.upscalerSharpness;
QD3D12_SetPostConstants(cl, passSharpness, 0.0f, 1.0f);
QD3D12_SetPostConstants(cl, passSharpness, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, applySceneFog);
cl->SetPipelineState(pso);
cl->OMSetRenderTargets(1, &outputRtv, FALSE, nullptr);
cl->RSSetViewports(1, &viewport);
cl->RSSetScissorRects(1, &scissor);
cl->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
cl->SetGraphicsRootDescriptorTable(0, inputSrv);
if (fogWindow)
QD3D12_BindPostFogResources(cl, *fogWindow, applySceneFog);
cl->DrawInstanced(3, 1, 0, 0);
}
@@ -6455,7 +6578,8 @@ static bool QD3D12_ToneMapFullscreenPass(
D3D12_CPU_DESCRIPTOR_HANDLE outputRtv,
const D3D12_VIEWPORT& viewport,
const D3D12_RECT& scissor,
float sharpnessOverride = -1.0f)
float sharpnessOverride = -1.0f,
bool applySceneFog = true)
{
ID3D12GraphicsCommandList* cl = g_gl.cmdList.Get();
const UINT frame = w.frameIndex;
@@ -6470,7 +6594,7 @@ static bool QD3D12_ToneMapFullscreenPass(
cl->SetDescriptorHeaps(_countof(heaps), heaps);
cl->SetGraphicsRootSignature(g_gl.postRootSig.Get());
const float passSharpness = (sharpnessOverride >= 0.0f) ? sharpnessOverride : g_gl.upscalerSharpness;
QD3D12_SetPostConstants(cl, passSharpness, 0.0f, 1.0f, 1.0f, 1.0f, g_gl.toneMapBrightness);
QD3D12_SetPostConstants(cl, passSharpness, 0.0f, 1.0f, 1.0f, 1.0f, g_gl.toneMapBrightness, applySceneFog);
cl->SetPipelineState(g_gl.postToneMapPSO.Get());
cl->OMSetRenderTargets(1, &outputRtv, FALSE, nullptr);
cl->RSSetViewports(1, &viewport);
@@ -6478,6 +6602,7 @@ static bool QD3D12_ToneMapFullscreenPass(
cl->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
cl->SetGraphicsRootDescriptorTable(0, hdrInputSrv);
cl->SetGraphicsRootDescriptorTable(7, w.toneMapSceneMaxSrvGpu[frame]);
QD3D12_BindPostFogResources(cl, w, applySceneFog);
cl->DrawInstanced(3, 1, 0, 0);
return true;
}
@@ -6633,7 +6758,10 @@ static bool QD3D12_FinalBlitToBackBuffer(
inputSrv,
CurrentBackBufferRTV(),
outputViewport,
outputScissor);
outputScissor,
-1.0f,
true,
&w);
return true;
}
@@ -8733,6 +8861,7 @@ void QD3D12_BeginFrame()
g_gl.sceneResolvedThisFrame = false;
g_gl.gbufferResolvedThisFrame = false;
g_gl.raytracedLightingReadyThisFrame = false;
g_gl.sceneFogValidThisFrame = false;
QD3D12_UpdateViewportState();
FrameResources& fr = w.frames[w.frameIndex];
@@ -8811,6 +8940,7 @@ void QD3D12_EndFrame()
g_gl.sceneResolvedThisFrame = false;
g_gl.gbufferResolvedThisFrame = false;
g_gl.raytracedLightingReadyThisFrame = false;
g_gl.sceneFogValidThisFrame = false;
QD3D12_UpdateViewportState();
}
@@ -10670,6 +10800,27 @@ static void QD3D12_FlushQueuedBatches()
if (count <= 0)
continue;
if (batch.key.fogEnabled > 0.5f &&
batch.key.cameraValid > 0.5f &&
batch.key.depthTest &&
batch.key.depthWrite &&
batch.key.colorWriteMask != 0)
{
g_gl.sceneFogValidThisFrame = true;
g_gl.sceneFogMode = batch.key.fogMode;
g_gl.sceneFogDensity = batch.key.fogDensity;
g_gl.sceneFogStart = batch.key.fogStart;
g_gl.sceneFogEnd = batch.key.fogEnd;
g_gl.sceneFogColor[0] = batch.key.fogColor[0];
g_gl.sceneFogColor[1] = batch.key.fogColor[1];
g_gl.sceneFogColor[2] = batch.key.fogColor[2];
g_gl.sceneFogColor[3] = batch.key.fogColor[3];
g_gl.sceneFogCameraWorldPos[0] = batch.key.cameraWorldPos[0];
g_gl.sceneFogCameraWorldPos[1] = batch.key.cameraWorldPos[1];
g_gl.sceneFogCameraWorldPos[2] = batch.key.cameraWorldPos[2];
g_gl.sceneFogCameraValid = batch.key.cameraValid;
}
const UINT vbBytes = (UINT)(count * sizeof(GLVertex));
const UINT cbBytes = (UINT)sizeof(DrawConstants);
@@ -10725,23 +10876,15 @@ static void QD3D12_FlushQueuedBatches()
memcpy(dc->texComb1Operand, batch.key.texComb1Operand, sizeof(dc->texComb1Operand));
memcpy(dc->texEnvColor1, batch.key.texEnvColor1, sizeof(dc->texEnvColor1));
dc->fogEnabled = g_gl.fog ? 1.0f : 0.0f;
switch (g_gl.fogMode)
{
case GL_LINEAR: dc->fogMode = 0.0f; break;
case GL_EXP: dc->fogMode = 1.0f; break;
case GL_EXP2: dc->fogMode = 2.0f; break;
default: dc->fogMode = 1.0f; break;
}
dc->fogDensity = g_gl.fogDensity;
dc->fogStart = g_gl.fogStart;
dc->fogEnd = g_gl.fogEnd;
dc->fogColor[0] = g_gl.fogColor[0];
dc->fogColor[1] = g_gl.fogColor[1];
dc->fogColor[2] = g_gl.fogColor[2];
dc->fogColor[3] = g_gl.fogColor[3];
dc->fogEnabled = batch.key.fogEnabled;
dc->fogMode = batch.key.fogMode;
dc->fogDensity = batch.key.fogDensity;
dc->fogStart = batch.key.fogStart;
dc->fogEnd = batch.key.fogEnd;
dc->fogColor[0] = batch.key.fogColor[0];
dc->fogColor[1] = batch.key.fogColor[1];
dc->fogColor[2] = batch.key.fogColor[2];
dc->fogColor[3] = batch.key.fogColor[3];
const float activeWidth = (float)QD3D12_ActiveRasterWidth(*g_currentWindow);
const float activeHeight = (float)QD3D12_ActiveRasterHeight(*g_currentWindow);
dc->renderSize[0] = activeWidth;
@@ -13056,6 +13199,7 @@ void QD3D12_Resize()
g_gl.sceneResolvedThisFrame = false;
g_gl.gbufferResolvedThisFrame = false;
g_gl.raytracedLightingReadyThisFrame = false;
g_gl.sceneFogValidThisFrame = false;
QD3D12_UpdateViewportState();
}
@@ -13106,6 +13250,7 @@ static void QD3D12_ReconfigureCurrentWindowForUpscalerChange()
g_gl.sceneResolvedThisFrame = false;
g_gl.gbufferResolvedThisFrame = false;
g_gl.raytracedLightingReadyThisFrame = false;
g_gl.sceneFogValidThisFrame = false;
QD3D12_UpdateViewportState();
}
@@ -15095,6 +15240,7 @@ static void QD3D12_ResolveSceneToOutputAndEnterNativePhase(QD3D12Window& w)
QD3D12_CompositeEmissiveIntoSceneColor(w);
QD3D12_TransitionResource(cl, w.sceneColorBuffers[w.frameIndex].Get(), w.sceneColorState[w.frameIndex], D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE);
QD3D12_TransitionResource(cl, w.positionBuffers[w.frameIndex].Get(), w.positionBufferState[w.frameIndex], D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE);
QD3D12_TransitionResource(cl, w.velocityBuffers[w.frameIndex].Get(), w.velocityBufferState[w.frameIndex], D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE);
QD3D12_TransitionResource(cl, w.specularBuffers[w.frameIndex].Get(), w.specularBufferState[w.frameIndex], D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE);
QD3D12_TransitionResource(cl, w.depthBuffer.Get(), w.depthState, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE);
@@ -15368,7 +15514,8 @@ void glLightScene(glRaytracingSceneHandle_t sceneHandle)
CurrentResolvedSceneColorRTV(),
viewport,
scissor,
0.0f))
0.0f,
false))
{
QD3D12_PostFullscreenPass(
cl,
@@ -17185,4 +17332,4 @@ void glUpdateTopLevelAceelStructure(
instDesc.mask = 0xFFu;
topLevelHandle = glRaytracingCreateInstanceInScene(scene, &instDesc);
}
}
}