MetalDevice: Explicitly bind fragment resources

Fixes adaptive downsampling with Metal renderer.
This commit is contained in:
Stenzek 2023-12-18 18:44:22 +10:00
parent 6fe2177ee7
commit f4fb069216
No known key found for this signature in database
4 changed files with 24 additions and 7 deletions

View file

@ -638,7 +638,7 @@ std::unique_ptr<GPUShader> MetalDevice::CreateShaderFromSource(GPUShaderStage st
return {}; return {};
} }
std::optional<std::string> msl = SPIRVCompiler::CompileSPIRVToMSL(spirv.value()); std::optional<std::string> msl = SPIRVCompiler::CompileSPIRVToMSL(stage, spirv.value());
if (!msl.has_value()) if (!msl.has_value())
{ {
Log_ErrorPrintf("Failed to compile SPIR-V to MSL."); Log_ErrorPrintf("Failed to compile SPIR-V to MSL.");

View file

@ -336,8 +336,8 @@ void ShaderGen::DeclareTexture(std::stringstream& ss, const char* name, u32 inde
{ {
if (m_glsl) if (m_glsl)
{ {
if (IsVulkan()) if (m_spirv)
ss << "layout(set = " << (m_has_uniform_buffer ? 1 : 0) << ", binding = " << index << ") "; ss << "layout(set = " << ((m_has_uniform_buffer || IsMetal()) ? 1 : 0) << ", binding = " << index << ") ";
else if (m_use_glsl_binding_layout) else if (m_use_glsl_binding_layout)
ss << "layout(binding = " << index << ") "; ss << "layout(binding = " << index << ") ";
@ -354,8 +354,8 @@ void ShaderGen::DeclareTextureBuffer(std::stringstream& ss, const char* name, u3
{ {
if (m_glsl) if (m_glsl)
{ {
if (IsVulkan()) if (m_spirv)
ss << "layout(set = 0, binding = " << index << ") "; ss << "layout(set = " << ((m_has_uniform_buffer || IsMetal()) ? 1 : 0) << ", binding = " << index << ") ";
else if (m_use_glsl_binding_layout) else if (m_use_glsl_binding_layout)
ss << "layout(binding = " << index << ") "; ss << "layout(binding = " << index << ") ";

View file

@ -173,12 +173,29 @@ std::optional<SPIRVCompiler::SPIRVCodeVector> SPIRVCompiler::CompileShader(GPUSh
#ifdef __APPLE__ #ifdef __APPLE__
std::optional<std::string> SPIRVCompiler::CompileSPIRVToMSL(std::span<const SPIRVCodeType> spv) std::optional<std::string> SPIRVCompiler::CompileSPIRVToMSL(GPUShaderStage stage, std::span<const SPIRVCodeType> spv)
{ {
spirv_cross::CompilerMSL compiler(spv.data(), spv.size()); spirv_cross::CompilerMSL compiler(spv.data(), spv.size());
spirv_cross::CompilerMSL::Options options = compiler.get_msl_options(); spirv_cross::CompilerMSL::Options options = compiler.get_msl_options();
options.pad_fragment_output_components = true; options.pad_fragment_output_components = true;
if (stage == GPUShaderStage::Fragment)
{
for (u32 i = 0; i < GPUDevice::MAX_TEXTURE_SAMPLERS; i++)
{
spirv_cross::MSLResourceBinding rb;
rb.stage = spv::ExecutionModelFragment;
rb.desc_set = 1;
rb.binding = i;
rb.count = 1;
rb.msl_texture = i;
rb.msl_sampler = i;
rb.msl_buffer = i;
compiler.add_msl_resource_binding(rb);
}
}
compiler.set_msl_options(options); compiler.set_msl_options(options);
std::string msl = compiler.compile(); std::string msl = compiler.compile();

View file

@ -42,7 +42,7 @@ std::optional<SPIRVCodeVector> CompileShader(GPUShaderStage stage, std::string_v
#ifdef __APPLE__ #ifdef __APPLE__
// Converts a SPIR-V shader into MSL. // Converts a SPIR-V shader into MSL.
std::optional<std::string> CompileSPIRVToMSL(std::span<const SPIRVCodeType> spv); std::optional<std::string> CompileSPIRVToMSL(GPUShaderStage stage, std::span<const SPIRVCodeType> spv);
#endif #endif