compiling metal shader modules

This commit is contained in:
Greg Wells
2025-05-28 07:11:30 -04:00
parent 13f2d14cad
commit 102da0f902
7 changed files with 178 additions and 68 deletions

View File

@@ -14,78 +14,78 @@ gnReturnCode gnCreateOutputDeviceFn(gnOutputDevice* outputDevice, gnInstance* in
outputDevice->outputDevice->queues[i] = outputDevice->outputDevice->device.newCommandQueue;
}
{
// {
// NSError* error = nil;
// MTLCompileOptions* options = nil;
// NSString *shaderSource = @"#include <metal_stdlib>\
// using namespace metal;\
// struct VertexOut {\
// float4 position [[position]];\
// float2 uv;\
// };\
// vertex VertexOut vs_main(uint vertexID [[vertex_id]]) {\
// float2 positions[4] = {\
// {-1.0, -1.0},\
// { 1.0, -1.0},\
// {-1.0, 1.0},\
// { 1.0, 1.0}\
// };\
// float2 uvs[4] = {\
// {0.0, 1.0},\
// {1.0, 1.0},\
// {0.0, 0.0},\
// {1.0, 0.0}\
// };\
// VertexOut out;\
// out.position = float4(positions[vertexID], 0.0, 1.0);\
// out.uv = uvs[vertexID];\
// return out;\
// }\
// fragment float4 fs_main(VertexOut in [[stage_in]],\
// texture2d<float> colorTex [[texture(0)]],\
// sampler samp [[sampler(0)]]) {\
// return colorTex.sample(samp, in.uv);\
// }\
// ";
// id<MTLLibrary> library = [outputDevice->outputDevice->device newLibraryWithSource:shaderSource options:nil error:&error];
NSError* error = nil;
MTLCompileOptions* options = nil;
NSString *shaderSource = @"#include <metal_stdlib>\
using namespace metal;\
struct VertexOut {\
float4 position [[position]];\
float2 uv;\
};\
vertex VertexOut vs_main(uint vertexID [[vertex_id]]) {\
float2 positions[4] = {\
{-1.0, -1.0},\
{ 1.0, -1.0},\
{-1.0, 1.0},\
{ 1.0, 1.0}\
};\
float2 uvs[4] = {\
{0.0, 1.0},\
{1.0, 1.0},\
{0.0, 0.0},\
{1.0, 0.0}\
};\
VertexOut out;\
out.position = float4(positions[vertexID], 0.0, 1.0);\
out.uv = uvs[vertexID];\
return out;\
}\
fragment float4 fs_main(VertexOut in [[stage_in]],\
texture2d<float> colorTex [[texture(0)]],\
sampler samp [[sampler(0)]]) {\
return colorTex.sample(samp, in.uv);\
}\
";
id<MTLLibrary> library = [outputDevice->outputDevice->device newLibraryWithSource:shaderSource options:nil error:&error];
if (!library) {
gnDebuggerSetErrorMessage(instance->debugger, (gnMessageData){
.message = gnCreateString("Failed to compile framebuffer shader")
});
return GN_FAILED_TO_CREATE_DEVICE;
}
// id<MTLFunction> vs = library->newFunction(NS::String::string("vs_main", NS::UTF8StringEncoding));
id<MTLFunction> vs = [library newFunctionWithName:@"vs_main"];
id<MTLFunction> fs = [library newFunctionWithName:@"fs_main"];
// if (!library) {
// gnDebuggerSetErrorMessage(instance->debugger, (gnMessageData){
// .message = gnCreateString("Failed to compile framebuffer shader")
// });
// return GN_FAILED_TO_CREATE_DEVICE;
// }
// // id<MTLFunction> vs = library->newFunction(NS::String::string("vs_main", NS::UTF8StringEncoding));
// id<MTLFunction> vs = [library newFunctionWithName:@"vs_main"];
// id<MTLFunction> fs = [library newFunctionWithName:@"fs_main"];
if (vs == nil) {
gnDebuggerSetErrorMessage(instance->debugger, (gnMessageData){
.message = gnCreateString("Failed to load frambuffer vertex shader")
});
return GN_FAILED_TO_CREATE_DEVICE;
}
// if (vs == nil) {
// gnDebuggerSetErrorMessage(instance->debugger, (gnMessageData){
// .message = gnCreateString("Failed to compile frambuffer vertex shader")
// });
// return GN_FAILED_TO_CREATE_DEVICE;
// }
if (fs == nil) {
gnDebuggerSetErrorMessage(instance->debugger, (gnMessageData){
.message = gnCreateString("Failed to load frambuffer fragment shader")
});
return GN_FAILED_TO_CREATE_DEVICE;
}
// if (fs == nil) {
// gnDebuggerSetErrorMessage(instance->debugger, (gnMessageData){
// .message = gnCreateString("Failed to compile frambuffer fragment shader")
// });
// return GN_FAILED_TO_CREATE_DEVICE;
// }
MTLRenderPipelineDescriptor* pipelineDesc =[[MTLRenderPipelineDescriptor alloc] init];;
pipelineDesc.vertexFunction = vs;
pipelineDesc.fragmentFunction = fs;
pipelineDesc.colorAttachments[0].pixelFormat = MTLPixelFormatBGRA8Unorm_sRGB;
// MTLRenderPipelineDescriptor* pipelineDesc =[[MTLRenderPipelineDescriptor alloc] init];;
// pipelineDesc.vertexFunction = vs;
// pipelineDesc.fragmentFunction = fs;
// pipelineDesc.colorAttachments[0].pixelFormat = MTLPixelFormatBGRA8Unorm_sRGB;
outputDevice->outputDevice->framebuffer = [outputDevice->outputDevice->device newRenderPipelineStateWithDescriptor:pipelineDesc error:&error];
if (!outputDevice->outputDevice->framebuffer) {
gnDebuggerSetErrorMessage(instance->debugger, (gnMessageData){
.message = gnCreateString("Failed to create frambuffer render pipeline")
});
return GN_FAILED_TO_CREATE_DEVICE;
}
}
// outputDevice->outputDevice->framebuffer = [outputDevice->outputDevice->device newRenderPipelineStateWithDescriptor:pipelineDesc error:&error];
// if (!outputDevice->outputDevice->framebuffer) {
// gnDebuggerSetErrorMessage(instance->debugger, (gnMessageData){
// .message = gnCreateString("Failed to create frambuffer render pipeline")
// });
// return GN_FAILED_TO_CREATE_DEVICE;
// }
// }
return GN_SUCCESS;
}

View File

@@ -23,6 +23,7 @@ gnPhysicalDevice* gnGetPhysicalDevicesFn(gnInstance* instance, uint32_t* deviceC
devicesList[i].properties.deviceType = GN_EXTERNAL_DEVICE;
// below I am going to fake that there is one queue that can support graphics, compute, and transfer queues
devicesList[i].queueProperties.queueCount = 1;
devicesList[i].queueProperties.queueProperties = malloc(sizeof(gnQueueProperties));
devicesList[i].queueProperties.queueProperties[0] = (gnQueueProperties){
.queueCount = 1,