Skip to content

Commit

Permalink
MetalDevice: Fix initial depth state not being bound
Browse files Browse the repository at this point in the history
  • Loading branch information
stenzek committed Aug 26, 2023
1 parent 3e82409 commit 106c136
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
1 change: 0 additions & 1 deletion src/util/metal_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,6 @@ class MetalDevice final : public GPUDevice
Common::Rectangle<s32> ClampToFramebufferSize(const Common::Rectangle<s32>& rc) const;
void PreDrawCheck();
void SetInitialEncoderState();
void SetUniformBufferInRenderEncoder();
void SetViewportInRenderEncoder();
void SetScissorInRenderEncoder();

Expand Down
29 changes: 19 additions & 10 deletions src/util/metal_device.mm
Original file line number Diff line number Diff line change
Expand Up @@ -849,8 +849,11 @@ static void DumpShader(u32 n, const std::string_view& suffix, const std::string_
desc.rasterSampleCount = config.per_sample_shading ? config.samples : 1;

// Metal-specific stuff
desc.vertexBuffers[1].mutability = MTLMutabilityImmutable;
desc.fragmentBuffers[1].mutability = MTLMutabilityImmutable;
desc.vertexBuffers[0].mutability = MTLMutabilityImmutable;
if (!config.input_layout.vertex_attributes.empty())
desc.vertexBuffers[1].mutability = MTLMutabilityImmutable;
if (config.layout == GPUPipeline::Layout::SingleTextureBufferAndPushConstants)
desc.fragmentBuffers[1].mutability = MTLMutabilityImmutable;

ca.blendingEnabled = config.blend.enable;
if (config.blend.enable)
Expand Down Expand Up @@ -1668,7 +1671,10 @@ static void DumpShader(u32 n, const std::string_view& suffix, const std::string_
m_current_uniform_buffer_position = m_uniform_buffer.GetCurrentOffset();
m_uniform_buffer.CommitMemory(size);
if (InRenderPass())
SetUniformBufferInRenderEncoder();
{
[m_render_encoder setVertexBufferOffset:m_current_uniform_buffer_position atIndex:0];
[m_render_encoder setFragmentBufferOffset:m_current_uniform_buffer_position atIndex:0];
}
}

void MetalDevice::SetFramebuffer(GPUFramebuffer* fb)
Expand Down Expand Up @@ -1713,6 +1719,7 @@ static void DumpShader(u32 n, const std::string_view& suffix, const std::string_

void MetalDevice::SetPipeline(GPUPipeline* pipeline)
{
DebugAssert(pipeline);
if (m_current_pipeline == pipeline)
return;

Expand All @@ -1732,6 +1739,12 @@ static void DumpShader(u32 n, const std::string_view& suffix, const std::string_
[m_render_encoder setCullMode:m_current_cull_mode];
}
}
else
{
// Still need to set depth state before the draw begins.
m_current_depth_state = m_current_pipeline->GetDepthState();
m_current_cull_mode = m_current_pipeline->GetCullMode();
}
}

void MetalDevice::UnbindPipeline(MetalPipeline* pl)
Expand All @@ -1740,6 +1753,7 @@ static void DumpShader(u32 n, const std::string_view& suffix, const std::string_
return;

m_current_pipeline = nullptr;
m_current_depth_state = nil;
}

void MetalDevice::SetTextureSampler(u32 slot, GPUTexture* texture, GPUSampler* sampler)
Expand Down Expand Up @@ -1888,7 +1902,8 @@ static void DumpShader(u32 n, const std::string_view& suffix, const std::string_
// Set initial state.
// TODO: avoid uniform set here? it's probably going to get changed...
// Might be better off just deferring all the init until the first draw...
SetUniformBufferInRenderEncoder();
[m_render_encoder setVertexBuffer:m_uniform_buffer.GetBuffer() offset:m_current_uniform_buffer_position atIndex:0];
[m_render_encoder setFragmentBuffer:m_uniform_buffer.GetBuffer() offset:m_current_uniform_buffer_position atIndex:0];
[m_render_encoder setVertexBuffer:m_vertex_buffer.GetBuffer() offset:0 atIndex:1];
[m_render_encoder setCullMode:m_current_cull_mode];
if (m_current_depth_state != nil)
Expand All @@ -1903,12 +1918,6 @@ static void DumpShader(u32 n, const std::string_view& suffix, const std::string_
SetScissorInRenderEncoder();
}

void MetalDevice::SetUniformBufferInRenderEncoder()
{
[m_render_encoder setVertexBuffer:m_uniform_buffer.GetBuffer() offset:m_current_uniform_buffer_position atIndex:0];
[m_render_encoder setFragmentBuffer:m_uniform_buffer.GetBuffer() offset:m_current_uniform_buffer_position atIndex:0];
}

void MetalDevice::SetViewportInRenderEncoder()
{
const Common::Rectangle<s32> rc = ClampToFramebufferSize(m_current_viewport);
Expand Down

0 comments on commit 106c136

Please sign in to comment.