From 2e85a4c4c49424b550afe152dadaa977d6ea0cfa Mon Sep 17 00:00:00 2001 From: Try Date: Mon, 6 Jan 2025 22:24:32 +0100 Subject: [PATCH] Metal: better shader compilation error reporting #72 --- Engine/gapi/metal/mtpipeline.cpp | 10 +++++++++- Engine/gapi/metal/mtshader.cpp | 19 +++++++++++++++++-- Engine/gapi/metal/mtshader.h | 4 ++++ 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/Engine/gapi/metal/mtpipeline.cpp b/Engine/gapi/metal/mtpipeline.cpp index ec70c95c..1c308ad1 100644 --- a/Engine/gapi/metal/mtpipeline.cpp +++ b/Engine/gapi/metal/mtpipeline.cpp @@ -2,6 +2,8 @@ #include "mtpipeline.h" +#include + #include "mtdevice.h" #include "mtshader.h" #include "mtfbolayout.h" @@ -226,7 +228,13 @@ MtCompPipeline::MtCompPipeline(MtDevice &device, const MtPipelineLay& lay, const NS::Error* error = nullptr; impl = NsPtr(device.impl->newComputePipelineState(desc.get(), MTL::PipelineOptionNone, nullptr, &error)); - mtAssert(impl.get(),error); + if(error!=nullptr) { + const char* e = error->localizedDescription()->utf8String(); +#if !defined(NDEBUG) + Log::d("NSError: \"",e,"\""); +#endif + throw std::system_error(Tempest::GraphicsErrc::InvalidShaderModule, "\""+sh.dbg.source + "\": " + e); + } } IVec3 MtCompPipeline::workGroupSize() const { diff --git a/Engine/gapi/metal/mtshader.cpp b/Engine/gapi/metal/mtshader.cpp index 86264450..3c9ed644 100644 --- a/Engine/gapi/metal/mtshader.cpp +++ b/Engine/gapi/metal/mtshader.cpp @@ -158,8 +158,8 @@ MtShader::MtShader(MtDevice& dev, const void* source, size_t srcSize) { throw std::system_error(Tempest::GraphicsErrc::InvalidShaderModule, e); } - if(stage==ShaderReflection::Stage::Vertex) { - // Log::d(msl); + if(stage==ShaderReflection::Stage::Mesh) { + Log::d(msl); } auto main = NsPtr(NS::String::string("main0",NS::UTF8StringEncoding)); @@ -192,6 +192,21 @@ void MtShader::fetchBindings(const uint32_t *source, size_t size) { } } } + for(auto& i:code) { + if(i.op()!=spv::OpSource) + continue; + uint32_t src = i.length()>3 ? i[3] : 0; + if(src==0) + continue; + + for(auto& r:code) { + if(r.op()==spv::OpString && r[1]==src) { + dbg.source = reinterpret_cast(&r[2]); + break; + } + } + break; + } } #endif diff --git a/Engine/gapi/metal/mtshader.h b/Engine/gapi/metal/mtshader.h index 6fd91516..65448121 100644 --- a/Engine/gapi/metal/mtshader.h +++ b/Engine/gapi/metal/mtshader.h @@ -42,6 +42,10 @@ class MtShader : public AbstractGraphicsApi::Shader { MTL::Size localSize = {}; } comp; + struct { + std::string source; + } dbg; + private: void fetchBindings(const uint32_t *source, size_t size); };