Archived
1
Fork 0

Fix a bunch of Metal backend errors

This commit is contained in:
Joshua Goins 2022-02-15 12:33:01 -05:00
parent fcbf526615
commit f4090d2b24

View file

@ -261,7 +261,11 @@ GFXTexture* GFXMetal::create_texture(const GFXTextureCreateInfo& info) {
#if defined(PLATFORM_IOS) || defined(PLATFORM_TVOS) #if defined(PLATFORM_IOS) || defined(PLATFORM_TVOS)
textureDescriptor->setStorageMode(MTL::StorageModeShared); textureDescriptor->setStorageMode(MTL::StorageModeShared);
#else #else
textureDescriptor->setStorageMode(MTL::StorageModeManaged); if(info.format == GFXPixelFormat::DEPTH_32F) {
textureDescriptor->setStorageMode(MTL::StorageModePrivate);
} else {
textureDescriptor->setStorageMode(MTL::StorageModeManaged);
}
#endif #endif
} }
@ -430,6 +434,7 @@ GFXPipeline* GFXMetal::create_graphics_pipeline(const GFXGraphicsPipelineCreateI
const bool has_vertex_stage = !info.shaders.vertex_src.empty(); const bool has_vertex_stage = !info.shaders.vertex_src.empty();
const bool has_fragment_stage = !info.shaders.fragment_src.empty(); const bool has_fragment_stage = !info.shaders.fragment_src.empty();
MTL::Function* vertexFunc;
if(has_vertex_stage) { if(has_vertex_stage) {
MTL::Library* vertexLibrary; MTL::Library* vertexLibrary;
{ {
@ -437,7 +442,7 @@ GFXPipeline* GFXMetal::create_graphics_pipeline(const GFXGraphicsPipelineCreateI
if(info.shaders.vertex_src.is_string()) { if(info.shaders.vertex_src.is_string()) {
vertex_src = info.shaders.vertex_src.as_string(); vertex_src = info.shaders.vertex_src.as_string();
} else { } else {
const auto vertex_path = info.shaders.vertex_src.as_path().string(); const auto vertex_path = info.shaders.vertex_src.as_path().string() + ".msl";
auto file = prism::open_file(prism::internal_domain / vertex_path); auto file = prism::open_file(prism::internal_domain / vertex_path);
if(file != std::nullopt) { if(file != std::nullopt) {
@ -447,13 +452,13 @@ GFXPipeline* GFXMetal::create_graphics_pipeline(const GFXGraphicsPipelineCreateI
} }
} }
vertexLibrary = device->newLibrary(NS::String::string(vertex_src.c_str(), NS::ASCIIStringEncoding), &error); vertexLibrary = device->newLibrary(NS::String::string(vertex_src.c_str(), NS::ASCIIStringEncoding), nullptr,&error);
if(vertexLibrary == nullptr) if(vertexLibrary == nullptr)
prism::log("Metal shader compiler error: {}", error->debugDescription()->cString(NS::ASCIIStringEncoding)); prism::log("Metal shader compiler error: {}", error->debugDescription()->cString(NS::ASCIIStringEncoding));
auto vertex_constants = get_constant_values(info.shaders.vertex_constants); auto vertex_constants = get_constant_values(info.shaders.vertex_constants);
MTL::Function* vertexFunc = vertexLibrary->newFunction(NS::String::string("main0", NS::ASCIIStringEncoding), vertex_constants, (NS::Error**)nullptr); vertexFunc = vertexLibrary->newFunction(NS::String::string("main0", NS::ASCIIStringEncoding), vertex_constants, (NS::Error**)nullptr);
if(debug_enabled && info.shaders.vertex_src.is_path()) if(debug_enabled && info.shaders.vertex_src.is_path())
vertexFunc->setLabel(NS::String::string(info.shaders.vertex_src.as_path().string().data(), NS::ASCIIStringEncoding)); vertexFunc->setLabel(NS::String::string(info.shaders.vertex_src.as_path().string().data(), NS::ASCIIStringEncoding));
@ -462,6 +467,7 @@ GFXPipeline* GFXMetal::create_graphics_pipeline(const GFXGraphicsPipelineCreateI
} }
} }
MTL::Function* fragmentFunc;
if(has_fragment_stage) { if(has_fragment_stage) {
MTL::Library* fragmentLibrary; MTL::Library* fragmentLibrary;
{ {
@ -469,7 +475,7 @@ GFXPipeline* GFXMetal::create_graphics_pipeline(const GFXGraphicsPipelineCreateI
if(info.shaders.fragment_src.is_string()) { if(info.shaders.fragment_src.is_string()) {
fragment_src = info.shaders.fragment_src.as_string(); fragment_src = info.shaders.fragment_src.as_string();
} else { } else {
const auto fragment_path = info.shaders.fragment_src.as_path().string(); const auto fragment_path = info.shaders.fragment_src.as_path().string() + ".msl";
auto file = prism::open_file(prism::internal_domain / fragment_path); auto file = prism::open_file(prism::internal_domain / fragment_path);
if(file != std::nullopt) { if(file != std::nullopt) {
@ -479,14 +485,14 @@ GFXPipeline* GFXMetal::create_graphics_pipeline(const GFXGraphicsPipelineCreateI
} }
} }
fragmentLibrary = device->newLibrary(NS::String::string(fragment_src.c_str(), NS::ASCIIStringEncoding), &error); fragmentLibrary = device->newLibrary(NS::String::string(fragment_src.c_str(), NS::ASCIIStringEncoding), nullptr,&error);
if(fragmentLibrary == nullptr) if(fragmentLibrary == nullptr)
prism::log("Metal shader compiler error: {}", error->debugDescription()->cString(NS::ASCIIStringEncoding)); prism::log("Metal shader compiler error: {}", error->debugDescription()->cString(NS::ASCIIStringEncoding));
} }
auto fragment_constants = get_constant_values(info.shaders.fragment_constants); auto fragment_constants = get_constant_values(info.shaders.fragment_constants);
MTL::Function* fragmentFunc = fragmentLibrary->newFunction(NS::String::string("main0", NS::ASCIIStringEncoding), fragment_constants, (NS::Error**)nullptr); fragmentFunc = fragmentLibrary->newFunction(NS::String::string("main0", NS::ASCIIStringEncoding), fragment_constants, (NS::Error**)nullptr);
if(debug_enabled && info.shaders.fragment_src.is_path()) if(debug_enabled && info.shaders.fragment_src.is_path())
fragmentFunc->setLabel(NS::String::string(info.shaders.fragment_src.as_path().string().data(), NS::ASCIIStringEncoding)); fragmentFunc->setLabel(NS::String::string(info.shaders.fragment_src.as_path().string().data(), NS::ASCIIStringEncoding));
@ -496,24 +502,19 @@ GFXPipeline* GFXMetal::create_graphics_pipeline(const GFXGraphicsPipelineCreateI
MTL::VertexDescriptor* descriptor = MTL::VertexDescriptor::alloc()->init(); MTL::VertexDescriptor* descriptor = MTL::VertexDescriptor::alloc()->init();
int i = 0;
for(auto input : info.vertex_input.inputs) { for(auto input : info.vertex_input.inputs) {
MTL::VertexBufferLayoutDescriptor* inputDescriptor = MTL::VertexBufferLayoutDescriptor::alloc()->init(); MTL::VertexBufferLayoutDescriptor* inputDescriptor = descriptor->layouts()->object(input.location);
inputDescriptor->setStride(input.stride); inputDescriptor->setStride(input.stride);
inputDescriptor->setStepFunction(MTL::VertexStepFunctionPerVertex); inputDescriptor->setStepFunction(MTL::VertexStepFunctionPerVertex);
inputDescriptor->setStepRate(1);
descriptor->layouts()->setObject(inputDescriptor, i);
GFXMetalPipeline::VertexStride vs; GFXMetalPipeline::VertexStride vs;
vs.location = input.location; vs.location = input.location;
vs.stride = input.stride; vs.stride = input.stride;
pipeline->vertexStrides.push_back(vs); pipeline->vertexStrides.push_back(vs);
i++;
} }
i = 0;
for(auto attribute : info.vertex_input.attributes) { for(auto attribute : info.vertex_input.attributes) {
MTL::VertexFormat format = MTL::VertexFormatFloat3; MTL::VertexFormat format = MTL::VertexFormatFloat3;
switch(attribute.format) { switch(attribute.format) {
@ -537,16 +538,14 @@ GFXPipeline* GFXMetal::create_graphics_pipeline(const GFXGraphicsPipelineCreateI
break; break;
} }
MTL::VertexAttributeDescriptor* attributeDescriptor = MTL::VertexAttributeDescriptor::alloc()->init(); MTL::VertexAttributeDescriptor* attributeDescriptor = descriptor->attributes()->object(attribute.location);
attributeDescriptor->setFormat(format); attributeDescriptor->setFormat(format);
attributeDescriptor->setBufferIndex(attribute.binding); attributeDescriptor->setBufferIndex(attribute.binding);
attributeDescriptor->setOffset(attribute.offset); attributeDescriptor->setOffset(attribute.offset);
descriptor->attributes()->setObject(attributeDescriptor, i);
i++;
} }
pipelineDescriptor->setVertexDescriptor(descriptor);
if(info.render_pass != nullptr) { if(info.render_pass != nullptr) {
GFXMetalRenderPass* metalRenderPass = (GFXMetalRenderPass*)info.render_pass; GFXMetalRenderPass* metalRenderPass = (GFXMetalRenderPass*)info.render_pass;
@ -584,8 +583,6 @@ GFXPipeline* GFXMetal::create_graphics_pipeline(const GFXGraphicsPipelineCreateI
pipelineDescriptor->colorAttachments()->setObject(colorAttachmentDescriptor, 0); pipelineDescriptor->colorAttachments()->setObject(colorAttachmentDescriptor, 0);
} }
pipelineDescriptor->setVertexDescriptor(descriptor);
if(debug_enabled) { if(debug_enabled) {
pipelineDescriptor->setLabel(NS::String::string(info.label.data(), NS::ASCIIStringEncoding)); pipelineDescriptor->setLabel(NS::String::string(info.label.data(), NS::ASCIIStringEncoding));
pipeline->label = info.label; pipeline->label = info.label;
@ -661,7 +658,7 @@ GFXPipeline* GFXMetal::create_compute_pipeline(const GFXComputePipelineCreateInf
if(info.compute_src.is_string()) { if(info.compute_src.is_string()) {
compute_src = info.compute_src.as_string(); compute_src = info.compute_src.as_string();
} else { } else {
const auto compute_path = info.compute_src.as_path().string(); const auto compute_path = info.compute_src.as_path().string() + ".msl";
auto file = prism::open_file(prism::internal_domain / compute_path); auto file = prism::open_file(prism::internal_domain / compute_path);
if(file != std::nullopt) { if(file != std::nullopt) {
@ -671,7 +668,7 @@ GFXPipeline* GFXMetal::create_compute_pipeline(const GFXComputePipelineCreateInf
} }
} }
computeLibrary = device->newLibrary(NS::String::string(compute_src.c_str(), NS::ASCIIStringEncoding), &error); computeLibrary = device->newLibrary(NS::String::string(compute_src.c_str(), NS::ASCIIStringEncoding), nullptr, &error);
if(!computeLibrary) if(!computeLibrary)
prism::log("Compute library compilation error: {}", error->debugDescription()->cString(NS::ASCIIStringEncoding)); prism::log("Compute library compilation error: {}", error->debugDescription()->cString(NS::ASCIIStringEncoding));
} }
@ -689,7 +686,7 @@ GFXPipeline* GFXMetal::create_compute_pipeline(const GFXComputePipelineCreateInf
} }
pipeline->compute_handle = device->newComputePipelineState(pipelineDescriptor, MTL::PipelineOptionNone, nullptr, &error); pipeline->compute_handle = device->newComputePipelineState(pipelineDescriptor, MTL::PipelineOptionNone, nullptr, &error);
if(!pipeline->handle) if(!pipeline->compute_handle)
prism::log("Compute pipeline error: {}", error->debugDescription()->cString(NS::ASCIIStringEncoding)); prism::log("Compute pipeline error: {}", error->debugDescription()->cString(NS::ASCIIStringEncoding));
for(auto& binding : info.shader_input.bindings) { for(auto& binding : info.shader_input.bindings) {
@ -953,7 +950,7 @@ void GFXMetal::submit(GFXCommandBuffer* command_buffer, const platform::window_p
renderEncoder->drawPrimitives(currentPipeline->primitiveType, renderEncoder->drawPrimitives(currentPipeline->primitiveType,
command.data.draw.vertex_offset, command.data.draw.vertex_offset,
command.data.draw.vertex_offset, command.data.draw.vertex_count,
command.data.draw.instance_count, command.data.draw.instance_count,
command.data.draw.base_instance); command.data.draw.base_instance);
} }