Skip to content

Commit

Permalink
Half the scene (all upper and lower walls) is drawn as masked unfortu…
Browse files Browse the repository at this point in the history
…nately
  • Loading branch information
dpjudas committed Sep 16, 2024
1 parent 76536be commit c3f65fe
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/common/rendering/vulkan/shaders/vk_shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ std::unique_ptr<VulkanShader> VkShaderManager::LoadFragShader(FString shadername
if (!fb->GetDevice()->EnabledFeatures.Features.shaderClipDistance) definesBlock << "#define NO_CLIPDISTANCE_SUPPORT\n";
if (!key.AlphaTest) definesBlock << "#define NO_ALPHATEST\n";
if (key.GBufferPass) definesBlock << "#define GBUFFER_PASS\n";
if (key.AlphaTestOnly) definesBlock << "#define ALPHATEST_ONLY\n";

if (key.DepthFadeThreshold) definesBlock << "#define USE_DEPTHFADETHRESHOLD\n";

Expand Down
3 changes: 2 additions & 1 deletion src/common/rendering/vulkan/shaders/vk_shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ class VkShaderKey
uint64_t FogBalls : 1; // FOGBALLS
uint64_t NoFragmentShader : 1;
uint64_t DepthFadeThreshold : 1;
uint64_t Unused : 41;
uint64_t AlphaTestOnly : 1; // ALPHATEST_ONLY
uint64_t Unused : 40;
};
uint64_t AsQWORD = 0;
};
Expand Down
21 changes: 19 additions & 2 deletions src/common/rendering/vulkan/vk_renderstate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,8 @@ void VkRenderState::RunZMinMaxPass()
auto buffers = fb->GetBuffers();
auto cmdbuffer = fb->GetCommands()->GetDrawCommands();

fb->GetCommands()->PushGroup(cmdbuffer, "zminmax");

int width = ((buffers->GetWidth() + 63) / 64 * 64) >> 1;
int height = ((buffers->GetHeight() + 63) / 64 * 64) >> 1;

Expand Down Expand Up @@ -945,6 +947,8 @@ void VkRenderState::RunZMinMaxPass()
.AddImage(&fb->GetBuffers()->SceneDepthStencil, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, false)
.AddImage(&fb->GetBuffers()->SceneZMinMax[5], VK_IMAGE_LAYOUT_GENERAL, false)
.Execute(cmdbuffer);

fb->GetCommands()->PopGroup(cmdbuffer);
}

void VkRenderState::DispatchLightTiles(const VSMatrix& worldToView, float m5)
Expand All @@ -954,6 +958,8 @@ void VkRenderState::DispatchLightTiles(const VSMatrix& worldToView, float m5)

auto cmdbuffer = fb->GetCommands()->GetDrawCommands();

fb->GetCommands()->PushGroup(cmdbuffer, "lighttiles");

PipelineBarrier()
.AddBuffer(fb->GetBuffers()->SceneLightTiles.get(), VK_ACCESS_SHADER_READ_BIT, VK_ACCESS_SHADER_WRITE_BIT)
.Execute(cmdbuffer, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT);
Expand Down Expand Up @@ -986,6 +992,8 @@ void VkRenderState::DispatchLightTiles(const VSMatrix& worldToView, float m5)
PipelineBarrier()
.AddBuffer(fb->GetBuffers()->SceneLightTiles.get(), VK_ACCESS_SHADER_WRITE_BIT, VK_ACCESS_SHADER_READ_BIT)
.Execute(cmdbuffer, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);

fb->GetCommands()->PopGroup(cmdbuffer);
}

void VkRenderState::DrawLevelMesh(LevelMeshDrawType drawType, bool noFragmentShader)
Expand All @@ -998,7 +1006,7 @@ void VkRenderState::DrawLevelMesh(LevelMeshDrawType drawType, bool noFragmentSha
int pipelineID = it.first;
const VkPipelineKey& key = fb->GetLevelMeshPipelineKey(pipelineID);

ApplyLevelMeshPipeline(mCommandBuffer, key, noFragmentShader);
ApplyLevelMeshPipeline(mCommandBuffer, key, drawType, noFragmentShader);

for (MeshBufferRange& range : it.second)
{
Expand All @@ -1009,6 +1017,8 @@ void VkRenderState::DrawLevelMesh(LevelMeshDrawType drawType, bool noFragmentSha

void VkRenderState::BeginQuery()
{
if (!mCommandBuffer)
ApplyRenderPass(DT_Triangles);
mCommandBuffer->beginQuery(mRSBuffers->OcclusionQuery.QueryPool.get(), mRSBuffers->OcclusionQuery.NextIndex++, 0);
}

Expand Down Expand Up @@ -1039,8 +1049,15 @@ void VkRenderState::GetQueryResults(int queryStart, int queryCount, TArray<bool>
}
}

void VkRenderState::ApplyLevelMeshPipeline(VulkanCommandBuffer* cmdbuffer, VkPipelineKey pipelineKey, bool noFragmentShader)
void VkRenderState::ApplyLevelMeshPipeline(VulkanCommandBuffer* cmdbuffer, VkPipelineKey pipelineKey, LevelMeshDrawType drawType, bool noFragmentShader)
{
if (drawType == LevelMeshDrawType::Masked && noFragmentShader)
{
// We unfortunately have to run the fragment shader to know which pixels are masked. Use a simplified version to reduce the cost.
noFragmentShader = false;
pipelineKey.ShaderKey.AlphaTestOnly = true;
}

// Global state that don't require rebuilding the mesh
pipelineKey.ShaderKey.NoFragmentShader = noFragmentShader;
pipelineKey.ShaderKey.UseShadowmap = gl_light_shadows == 1;
Expand Down
2 changes: 1 addition & 1 deletion src/common/rendering/vulkan/vk_renderstate.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class VkRenderState : public FRenderState

void RunZMinMaxPass();
void ApplyLevelMesh();
void ApplyLevelMeshPipeline(VulkanCommandBuffer* cmdbuffer, VkPipelineKey pipelineKey, bool noFragmentShader);
void ApplyLevelMeshPipeline(VulkanCommandBuffer* cmdbuffer, VkPipelineKey pipelineKey, LevelMeshDrawType drawType, bool noFragmentShader);

VulkanRenderDevice* fb = nullptr;

Expand Down
5 changes: 3 additions & 2 deletions src/rendering/hwrenderer/scene/hw_drawinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -443,8 +443,7 @@ void HWDrawInfo::CreateScene(bool drawpsprites, FRenderState& state)
state.SetColorMask(false);
state.SetCulling(Cull_CW);
state.DrawLevelMesh(LevelMeshDrawType::Opaque, true);
state.DispatchLightTiles(VPUniforms.mViewMatrix, VPUniforms.mProjectionMatrix.get()[5]);
state.DrawLevelMesh(LevelMeshDrawType::Masked, false); // To do: properly mark wall top/bottom as opaque so we don't need this
state.DrawLevelMesh(LevelMeshDrawType::Masked, true);
if (gl_portals)
{
state.SetDepthBias(1, 128);
Expand Down Expand Up @@ -481,6 +480,8 @@ void HWDrawInfo::CreateScene(bool drawpsprites, FRenderState& state)
state.SetDepthMask(true);
int queryEnd = state.GetNextQueryIndex();

state.DispatchLightTiles(VPUniforms.mViewMatrix, VPUniforms.mProjectionMatrix.get()[5]);

// draw opaque level so the GPU has something to do while we examine the query results
state.DrawLevelMesh(LevelMeshDrawType::Opaque, false);
state.DrawLevelMesh(LevelMeshDrawType::Masked, false);
Expand Down
4 changes: 4 additions & 0 deletions wadsrc/static/shaders/scene/frag_main.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ void main()
if (material.Base.a <= uAlphaThreshold) discard;
#endif

#ifndef ALPHATEST_ONLY

#ifdef USE_DEPTHFADETHRESHOLD
float behindFragmentDepth = texelFetch(LinearDepth, uViewOffset + ivec2(gl_FragCoord.xy), 0).r;
material.Base.a *= clamp((behindFragmentDepth - pixelpos.w) / uDepthFadeThreshold, 0.0, 1.0);
Expand Down Expand Up @@ -63,4 +65,6 @@ void main()
FragFog = vec4(AmbientOcclusionColor(), 1.0);
FragNormal = vec4(vEyeNormal.xyz * 0.5 + 0.5, 1.0);
#endif

#endif
}

0 comments on commit c3f65fe

Please sign in to comment.