simplified world shader trace and fixed its VSync support

This commit is contained in:
myT 2024-01-18 03:16:35 +01:00
parent 31b960255d
commit 7fab6ea376
9 changed files with 32 additions and 32 deletions

View file

@ -41,7 +41,7 @@ struct OpaquePixelRC
float greyscale; float greyscale;
// shader trace // shader trace
uint32_t shaderTrace; // shader index: 14 - frame index: 2 - enable: 1 uint32_t shaderTrace; // shader index: 14 - enable: 1
uint16_t centerPixelX; uint16_t centerPixelX;
uint16_t centerPixelY; uint16_t centerPixelY;
}; };
@ -319,7 +319,7 @@ void WorldOpaque::EndBatch()
pixelRC.shaderIndexBufferIndex = bufferIndex; pixelRC.shaderIndexBufferIndex = bufferIndex;
pixelRC.alphaTest = alphaTest; pixelRC.alphaTest = alphaTest;
pixelRC.greyscale = tess.greyscale; pixelRC.greyscale = tess.greyscale;
pixelRC.shaderTrace = ((uint32_t)shader->index << 3) | (RHI::GetFrameIndex() << 1) | enableShaderTrace; pixelRC.shaderTrace = ((uint32_t)shader->index << 1) | enableShaderTrace;
pixelRC.centerPixelX = glConfig.vidWidth / 2; pixelRC.centerPixelX = glConfig.vidWidth / 2;
pixelRC.centerPixelY = glConfig.vidHeight / 2; pixelRC.centerPixelY = glConfig.vidHeight / 2;
CmdSetGraphicsRootConstants(sizeof(vertexRC), sizeof(pixelRC), &pixelRC); CmdSetGraphicsRootConstants(sizeof(vertexRC), sizeof(pixelRC), &pixelRC);

View file

@ -333,7 +333,7 @@ void WorldTransp::EndBatch()
pixelRC.samplerIndex = sampIdx; pixelRC.samplerIndex = sampIdx;
pixelRC.stateBits = GetFixedStageBits(stage->stateBits, s); pixelRC.stateBits = GetFixedStageBits(stage->stateBits, s);
pixelRC.textureIndex = texIdx; pixelRC.textureIndex = texIdx;
pixelRC.shaderTrace = ((uint32_t)shader->index << 3) | (RHI::GetFrameIndex() << 1) | enableShaderTrace; pixelRC.shaderTrace = ((uint32_t)shader->index << 1) | enableShaderTrace;
pixelRC.hFadeDistance = f32tof16(shader->dfInvDist); pixelRC.hFadeDistance = f32tof16(shader->dfInvDist);
pixelRC.hFadeOffset = f32tof16(shader->dfBias); pixelRC.hFadeOffset = f32tof16(shader->dfBias);
pixelRC.depthFadeScaleBias = (enableDepthFade << 8) | (uint32_t)r_depthFadeScaleAndBias[shader->dfType]; pixelRC.depthFadeScaleBias = (enableDepthFade << 8) | (uint32_t)r_depthFadeScaleAndBias[shader->dfType];

View file

@ -43,7 +43,7 @@ struct WorldPixelRC
float greyscale; float greyscale;
// r_shaderTrace - dynamically enabled // r_shaderTrace - dynamically enabled
uint32_t shaderTrace; // shader index: 14 - frame index: 2 - enable: 1 uint32_t shaderTrace; // shader index: 14 - enable: 1
uint16_t centerPixelX; uint16_t centerPixelX;
uint16_t centerPixelY; uint16_t centerPixelY;

View file

@ -663,7 +663,7 @@ void World::EndBatch()
pixelRC.hNoiseScale = f32tof16(r_ditherStrength->value); pixelRC.hNoiseScale = f32tof16(r_ditherStrength->value);
pixelRC.hInvGamma = f32tof16(1.0f / r_gamma->value); pixelRC.hInvGamma = f32tof16(1.0f / r_gamma->value);
pixelRC.hInvBrightness = f32tof16(1.0f / r_brightness->value); pixelRC.hInvBrightness = f32tof16(1.0f / r_brightness->value);
pixelRC.shaderTrace = (uint32_t)!!tr.traceWorldShader | (RHI::GetFrameIndex() << 1) | ((uint32_t)shader->index << 3); pixelRC.shaderTrace = ((uint32_t)shader->index << 1) | (uint32_t)(tr.traceWorldShader ? 1 : 0);
pixelRC.centerPixelX = glConfig.vidWidth / 2; pixelRC.centerPixelX = glConfig.vidWidth / 2;
pixelRC.centerPixelY = glConfig.vidHeight / 2; pixelRC.centerPixelY = glConfig.vidHeight / 2;
pixelRC.hFadeDistance = f32tof16(tess.shader->dfInvDist); pixelRC.hFadeDistance = f32tof16(tess.shader->dfInvDist);

View file

@ -45,7 +45,7 @@ struct OIT_Fragment
float depth; // higher is further away from the camera float depth; // higher is further away from the camera
uint stateBits; // GLS_* stage bits + stage index uint stateBits; // GLS_* stage bits + stage index
uint next; uint next;
uint shaderTrace; // shader index: 14 - frame index: 2 - enable: 1 uint shaderTrace; // shader index: 14 - enable: 1
uint depthFadeDistOffset; // offset: fp16 - distance: fp16 uint depthFadeDistOffset; // offset: fp16 - distance: fp16
uint depthFadeScaleBias; // enable: 1 - color bias: 4 - color scale: 4 uint depthFadeScaleBias; // enable: 1 - color bias: 4 - color scale: 4
// @TODO: move the 9 bits from depthFadeScaleBias into shaderTrace // @TODO: move the 9 bits from depthFadeScaleBias into shaderTrace

View file

@ -41,11 +41,8 @@ cbuffer RootConstants
float greyscale; float greyscale;
// shader trace // shader trace
uint shaderTrace; // shader index: 14 - frame index: 2 - enable: 1 uint shaderTrace; // shader index: 14 - enable: 1
uint centerPixel; // y: 16 - x: 16 uint centerPixel; // y: 16 - x: 16
// @TODO: dither
// @TODO: Voronoi tiling
}; };
struct VIn struct VIn
@ -63,8 +60,6 @@ struct VOut
float2 texCoords : TEXCOORD0; float2 texCoords : TEXCOORD0;
float4 color : COLOR0; float4 color : COLOR0;
float clipDist : SV_ClipDistance0; float clipDist : SV_ClipDistance0;
float2 proj2232 : PROJ;
float depthVS : DEPTHVS;
}; };
VOut vs(VIn input) VOut vs(VIn input)
@ -77,12 +72,13 @@ VOut vs(VIn input)
output.texCoords = input.texCoords; output.texCoords = input.texCoords;
output.color = input.color; output.color = input.color;
output.clipDist = dot(positionVS, clipPlane); output.clipDist = dot(positionVS, clipPlane);
output.proj2232 = float2(-projectionMatrix[2][2], projectionMatrix[2][3]);
output.depthVS = -positionVS.z;
return output; return output;
} }
// @TODO: enable early-Z here once the full pre-pass is implemented
// this will prevent fragments failing the depth test from writing to the shader ID buffer
//[earlydepthstencil]
float4 ps(VOut input) : SV_Target float4 ps(VOut input) : SV_Target
{ {
// @TODO: Voronoi tiling // @TODO: Voronoi tiling
@ -106,9 +102,8 @@ float4 ps(VOut input) : SV_Target
if(all(fragmentCoords == centerCoords)) if(all(fragmentCoords == centerCoords))
{ {
RWByteAddressBuffer shaderIndexBuffer = ResourceDescriptorHeap[shaderIndexBufferIndex]; RWByteAddressBuffer shaderIndexBuffer = ResourceDescriptorHeap[shaderIndexBufferIndex];
uint frameIndex = (shaderTrace >> 1) & 3; uint shaderIndex = shaderTrace >> 1;
uint shaderIndex = shaderTrace >> 3; shaderIndexBuffer.Store(0, shaderIndex);
shaderIndexBuffer.Store(frameIndex * 4, shaderIndex);
} }
} }

View file

@ -145,6 +145,7 @@ float4 ps(VOut input) : SV_Target
} }
// blend the results // blend the results
int lastFragmentIndex = -1;
float storedDepthZW = depthTex.Load(int3(input.position.xy, 0)).x; // stored depth, z/w float storedDepthZW = depthTex.Load(int3(input.position.xy, 0)).x; // stored depth, z/w
float dstDepth = 1.0; float dstDepth = 1.0;
for(i = 0; i < fragmentCount; ++i) for(i = 0; i < fragmentCount; ++i)
@ -159,6 +160,7 @@ float4 ps(VOut input) : SV_Target
} }
float4 fragColor = UnpackColor(frag.color); float4 fragColor = UnpackColor(frag.color);
float4 prevColor = color;
fragColor = DepthFadeFragmentColor(fragColor, frag, storedDepthZW); fragColor = DepthFadeFragmentColor(fragColor, frag, storedDepthZW);
color = Blend(fragColor, color, frag.stateBits); color = Blend(fragColor, color, frag.stateBits);
if((stateBits & GLS_DEPTHMASK_TRUE) != 0u && if((stateBits & GLS_DEPTHMASK_TRUE) != 0u &&
@ -166,12 +168,17 @@ float4 ps(VOut input) : SV_Target
{ {
dstDepth = fragDepth; dstDepth = fragDepth;
} }
// we have to not include the alpha channel in this test for it to be correct
if(any(color.rgb != prevColor.rgb))
{
lastFragmentIndex = (int)i;
}
} }
// write out the fragment shader ID of the closest fragment of the center pixel // write out the fragment shader ID of the closest visible fragment of the center pixel
if(fragmentCount > 0) if(lastFragmentIndex >= 0)
{ {
uint lastFragmentIndex = fragmentCount - 1;
OIT_Fragment closest = sorted[lastFragmentIndex]; OIT_Fragment closest = sorted[lastFragmentIndex];
uint shaderTrace = closest.shaderTrace; uint shaderTrace = closest.shaderTrace;
if(shaderTrace & 1) if(shaderTrace & 1)
@ -181,9 +188,8 @@ float4 ps(VOut input) : SV_Target
if(all(fragmentCoords == centerCoords)) if(all(fragmentCoords == centerCoords))
{ {
RWByteAddressBuffer shaderIdBuf = ResourceDescriptorHeap[shaderIndexBuffer]; RWByteAddressBuffer shaderIdBuf = ResourceDescriptorHeap[shaderIndexBuffer];
uint frameIndex = (shaderTrace >> 1) & 3; uint shaderIndex = shaderTrace >> 1;
uint shaderId = shaderTrace >> 3; shaderIdBuf.Store(0, shaderIndex);
shaderIdBuf.Store(frameIndex * 4, shaderId);
} }
} }
} }

View file

@ -177,7 +177,7 @@ cbuffer RootConstants
float greyscale; float greyscale;
// shader trace // shader trace
uint shaderTrace; // shader index: 14 - frame index: 2 - enable: 1 uint shaderTrace; // shader index: 14 - enable: 1
uint centerPixel; // y: 16 - x: 16 uint centerPixel; // y: 16 - x: 16
// depth fade // depth fade
@ -281,9 +281,8 @@ float4 ps(VOut input) : SV_Target
uint2 centerCoords = uint2(centerPixel & 0xFFFF, centerPixel >> 16); uint2 centerCoords = uint2(centerPixel & 0xFFFF, centerPixel >> 16);
if(all(fragmentCoords == centerCoords)) if(all(fragmentCoords == centerCoords))
{ {
uint frameIndex = (shaderTrace >> 1) & 3; uint shaderIndex = shaderTrace >> 1;
uint shaderIndex = shaderTrace >> 3; shaderIndexBuffer.Store(0, shaderIndex);
shaderIndexBuffer.Store(frameIndex * 4, shaderIndex);
} }
} }

View file

@ -496,9 +496,9 @@ void SRP::EndFrame()
CmdBufferBarrier(traceRenderBuffer, ResourceStates::UnorderedAccessBit); CmdBufferBarrier(traceRenderBuffer, ResourceStates::UnorderedAccessBit);
CmdEndBarrier(); CmdEndBarrier();
// grab last frame's result // grab the currently available result
uint32_t* shaderIndices = (uint32_t*)MapBuffer(traceReadbackBuffer); uint32_t* const shaderIndices = (uint32_t*)MapBuffer(traceReadbackBuffer);
const uint32_t shaderIndex = shaderIndices[RHI::GetFrameIndex() ^ 1]; const uint32_t shaderIndex = *shaderIndices;
UnmapBuffer(traceReadbackBuffer); UnmapBuffer(traceReadbackBuffer);
if(shaderIndex < (uint32_t)tr.numShaders) if(shaderIndex < (uint32_t)tr.numShaders)
{ {
@ -527,12 +527,12 @@ void SRP::EndFrame()
void SRP::CreateShaderTraceBuffers() void SRP::CreateShaderTraceBuffers()
{ {
{ {
BufferDesc desc("shader trace opaque", 2 * sizeof(uint32_t), ResourceStates::UnorderedAccessBit); BufferDesc desc("shader trace render", sizeof(uint32_t), ResourceStates::UnorderedAccessBit);
traceRenderBuffer = CreateBuffer(desc); traceRenderBuffer = CreateBuffer(desc);
} }
{ {
BufferDesc desc("shader trace opaque readback", 2 * sizeof(uint32_t), ResourceStates::Common); BufferDesc desc("shader trace readback", sizeof(uint32_t), ResourceStates::Common);
desc.memoryUsage = MemoryUsage::Readback; desc.memoryUsage = MemoryUsage::Readback;
traceReadbackBuffer = CreateBuffer(desc); traceReadbackBuffer = CreateBuffer(desc);
} }