Skip to content

Commit

Permalink
Metal: better shader compilation error reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
Try committed Jan 6, 2025
1 parent da9a4f2 commit 2e85a4c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
10 changes: 9 additions & 1 deletion Engine/gapi/metal/mtpipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include "mtpipeline.h"

#include <Tempest/Log>

#include "mtdevice.h"
#include "mtshader.h"
#include "mtfbolayout.h"
Expand Down Expand Up @@ -226,7 +228,13 @@ MtCompPipeline::MtCompPipeline(MtDevice &device, const MtPipelineLay& lay, const

NS::Error* error = nullptr;
impl = NsPtr<MTL::ComputePipelineState>(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 {
Expand Down
19 changes: 17 additions & 2 deletions Engine/gapi/metal/mtshader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>(NS::String::string("main0",NS::UTF8StringEncoding));
Expand Down Expand Up @@ -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<const char*>(&r[2]);
break;
}
}
break;
}
}

#endif
4 changes: 4 additions & 0 deletions Engine/gapi/metal/mtshader.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down

0 comments on commit 2e85a4c

Please sign in to comment.