Metal: Recreate the compiler everytime we use it.

This commit is contained in:
Corentin Wallez 2017-08-18 11:44:56 -04:00 committed by Corentin Wallez
parent 2f96e129ef
commit 9a72ea09a9
2 changed files with 11 additions and 11 deletions

View File

@ -31,7 +31,6 @@ namespace metal {
class ShaderModule : public ShaderModuleBase { class ShaderModule : public ShaderModuleBase {
public: public:
ShaderModule(ShaderModuleBuilder* builder); ShaderModule(ShaderModuleBuilder* builder);
~ShaderModule();
struct MetalFunctionData { struct MetalFunctionData {
id<MTLFunction> function; id<MTLFunction> function;
@ -40,7 +39,10 @@ namespace metal {
MetalFunctionData GetFunction(const char* functionName, const PipelineLayout* layout) const; MetalFunctionData GetFunction(const char* functionName, const PipelineLayout* layout) const;
private: private:
spirv_cross::CompilerMSL* compiler = nullptr; // Calling compile on CompilerMSL somehow changes internal state that makes subsequent
// compiles return invalid MSL. We keep the spirv around and recreate the compiler everytime
// we need to use it.
std::vector<uint32_t> spirv;
}; };
} }

View File

@ -42,17 +42,15 @@ namespace metal {
} }
ShaderModule::ShaderModule(ShaderModuleBuilder* builder) ShaderModule::ShaderModule(ShaderModuleBuilder* builder)
: ShaderModuleBase(builder) { : ShaderModuleBase(builder), spirv(builder->AcquireSpirv()) {
compiler = new spirv_cross::CompilerMSL(builder->AcquireSpirv()); spirv_cross::CompilerMSL compiler(spirv);
ExtractSpirvInfo(*compiler); ExtractSpirvInfo(compiler);
}
ShaderModule::~ShaderModule() {
delete compiler;
} }
ShaderModule::MetalFunctionData ShaderModule::GetFunction(const char* functionName, ShaderModule::MetalFunctionData ShaderModule::GetFunction(const char* functionName,
const PipelineLayout* layout) const { const PipelineLayout* layout) const {
spirv_cross::CompilerMSL compiler(spirv);
// By default SPIRV-Cross will give MSL resources indices in increasing order. // By default SPIRV-Cross will give MSL resources indices in increasing order.
// To make the MSL indices match the indices chosen in the PipelineLayout, we build // To make the MSL indices match the indices chosen in the PipelineLayout, we build
// a table of MSLResourceBinding to give to SPIRV-Cross // a table of MSLResourceBinding to give to SPIRV-Cross
@ -92,14 +90,14 @@ namespace metal {
MetalFunctionData result; MetalFunctionData result;
{ {
auto size = compiler->get_entry_point(functionName).workgroup_size; auto size = compiler.get_entry_point(functionName).workgroup_size;
result.localWorkgroupSize = MTLSizeMake(size.x, size.y, size.z); result.localWorkgroupSize = MTLSizeMake(size.x, size.y, size.z);
} }
{ {
// SPIRV-Cross also supports re-ordering attributes but it seems to do the correct thing // SPIRV-Cross also supports re-ordering attributes but it seems to do the correct thing
// by default. // by default.
std::string msl = compiler->compile(nullptr, &mslBindings); std::string msl = compiler.compile(nullptr, &mslBindings);
NSString* mslSource = [NSString stringWithFormat:@"%s", msl.c_str()]; NSString* mslSource = [NSString stringWithFormat:@"%s", msl.c_str()];
auto mtlDevice = ToBackend(GetDevice())->GetMTLDevice(); auto mtlDevice = ToBackend(GetDevice())->GetMTLDevice();