From b872b9e1c7835c0e44d30169968af79dad75c9b8 Mon Sep 17 00:00:00 2001 From: Valerio Santinelli Date: Fri, 11 Oct 2024 10:03:14 +0200 Subject: [PATCH] Fixed the metal pipeline and made the Metal backend the default on macOS --- .../gl33/default.glsl_default_glsl410_vs.glsl | 4 +-- .../default.glsl_default_glsl300es_vs.glsl | 4 +-- .../hlsl/default.glsl_default_hlsl4_vs.hlsl | 6 ++-- .../default.glsl_default_metal_ios_vs.metal | 4 +-- .../default.glsl_default_metal_macos_vs.metal | 4 +-- assets/shaders/src/default.glsl | 4 +-- example/main.c | 8 +++-- src/binocle/core/binocle_gd.c | 32 +++++++++---------- 8 files changed, 35 insertions(+), 31 deletions(-) diff --git a/assets/shaders/dst/gl33/default.glsl_default_glsl410_vs.glsl b/assets/shaders/dst/gl33/default.glsl_default_glsl410_vs.glsl index 1665a3a4..ffae802e 100644 --- a/assets/shaders/dst/gl33/default.glsl_default_glsl410_vs.glsl +++ b/assets/shaders/dst/gl33/default.glsl_default_glsl410_vs.glsl @@ -1,7 +1,7 @@ #version 410 uniform vec4 vs_params[12]; -layout(location = 0) in vec2 vertexPosition; +layout(location = 0) in vec3 vertexPosition; layout(location = 0) out vec2 tcoord; layout(location = 2) in vec2 vertexTCoord; layout(location = 1) out vec4 color; @@ -10,7 +10,7 @@ layout(location = 3) in vec3 vertexNormal; void main() { - gl_Position = ((mat4(vs_params[0], vs_params[1], vs_params[2], vs_params[3]) * mat4(vs_params[4], vs_params[5], vs_params[6], vs_params[7])) * mat4(vs_params[8], vs_params[9], vs_params[10], vs_params[11])) * vec4(vertexPosition, 1.0, 1.0); + gl_Position = ((mat4(vs_params[0], vs_params[1], vs_params[2], vs_params[3]) * mat4(vs_params[4], vs_params[5], vs_params[6], vs_params[7])) * mat4(vs_params[8], vs_params[9], vs_params[10], vs_params[11])) * vec4(vertexPosition, 1.0); tcoord = vertexTCoord; color = vertexColor; gl_PointSize = 1.0; diff --git a/assets/shaders/dst/gles/default.glsl_default_glsl300es_vs.glsl b/assets/shaders/dst/gles/default.glsl_default_glsl300es_vs.glsl index fb9f864d..b1f152be 100644 --- a/assets/shaders/dst/gles/default.glsl_default_glsl300es_vs.glsl +++ b/assets/shaders/dst/gles/default.glsl_default_glsl300es_vs.glsl @@ -1,7 +1,7 @@ #version 300 es uniform vec4 vs_params[12]; -layout(location = 0) in vec2 vertexPosition; +layout(location = 0) in vec3 vertexPosition; out vec2 tcoord; layout(location = 2) in vec2 vertexTCoord; out vec4 color; @@ -10,7 +10,7 @@ layout(location = 3) in vec3 vertexNormal; void main() { - gl_Position = ((mat4(vs_params[0], vs_params[1], vs_params[2], vs_params[3]) * mat4(vs_params[4], vs_params[5], vs_params[6], vs_params[7])) * mat4(vs_params[8], vs_params[9], vs_params[10], vs_params[11])) * vec4(vertexPosition, 1.0, 1.0); + gl_Position = ((mat4(vs_params[0], vs_params[1], vs_params[2], vs_params[3]) * mat4(vs_params[4], vs_params[5], vs_params[6], vs_params[7])) * mat4(vs_params[8], vs_params[9], vs_params[10], vs_params[11])) * vec4(vertexPosition, 1.0); tcoord = vertexTCoord; color = vertexColor; gl_PointSize = 1.0; diff --git a/assets/shaders/dst/hlsl/default.glsl_default_hlsl4_vs.hlsl b/assets/shaders/dst/hlsl/default.glsl_default_hlsl4_vs.hlsl index d8b531ba..253f00c0 100644 --- a/assets/shaders/dst/hlsl/default.glsl_default_hlsl4_vs.hlsl +++ b/assets/shaders/dst/hlsl/default.glsl_default_hlsl4_vs.hlsl @@ -8,7 +8,7 @@ cbuffer vs_params : register(b0) static float4 gl_Position; static float gl_PointSize; -static float2 vertexPosition; +static float3 vertexPosition; static float2 tcoord; static float2 vertexTCoord; static float4 color; @@ -17,7 +17,7 @@ static float3 vertexNormal; struct SPIRV_Cross_Input { - float2 vertexPosition : TEXCOORD0; + float3 vertexPosition : TEXCOORD0; float4 vertexColor : TEXCOORD1; float2 vertexTCoord : TEXCOORD2; float3 vertexNormal : TEXCOORD3; @@ -32,7 +32,7 @@ struct SPIRV_Cross_Output void vert_main() { - gl_Position = mul(float4(vertexPosition, 1.0f, 1.0f), mul(_19_modelMatrix, mul(_19_viewMatrix, _19_projectionMatrix))); + gl_Position = mul(float4(vertexPosition, 1.0f), mul(_19_modelMatrix, mul(_19_viewMatrix, _19_projectionMatrix))); tcoord = vertexTCoord; color = vertexColor; gl_PointSize = 1.0f; diff --git a/assets/shaders/dst/metal-ios/default.glsl_default_metal_ios_vs.metal b/assets/shaders/dst/metal-ios/default.glsl_default_metal_ios_vs.metal index a1c36523..769b4b9e 100644 --- a/assets/shaders/dst/metal-ios/default.glsl_default_metal_ios_vs.metal +++ b/assets/shaders/dst/metal-ios/default.glsl_default_metal_ios_vs.metal @@ -20,7 +20,7 @@ struct main0_out struct main0_in { - float2 vertexPosition [[attribute(0)]]; + float3 vertexPosition [[attribute(0)]]; float4 vertexColor [[attribute(1)]]; float2 vertexTCoord [[attribute(2)]]; }; @@ -28,7 +28,7 @@ struct main0_in vertex main0_out main0(main0_in in [[stage_in]], constant vs_params& _19 [[buffer(0)]]) { main0_out out = {}; - out.gl_Position = ((_19.projectionMatrix * _19.viewMatrix) * _19.modelMatrix) * float4(in.vertexPosition, 1.0, 1.0); + out.gl_Position = ((_19.projectionMatrix * _19.viewMatrix) * _19.modelMatrix) * float4(in.vertexPosition, 1.0); out.tcoord = in.vertexTCoord; out.color = in.vertexColor; out.gl_PointSize = 1.0; diff --git a/assets/shaders/dst/metal-macos/default.glsl_default_metal_macos_vs.metal b/assets/shaders/dst/metal-macos/default.glsl_default_metal_macos_vs.metal index a1c36523..769b4b9e 100644 --- a/assets/shaders/dst/metal-macos/default.glsl_default_metal_macos_vs.metal +++ b/assets/shaders/dst/metal-macos/default.glsl_default_metal_macos_vs.metal @@ -20,7 +20,7 @@ struct main0_out struct main0_in { - float2 vertexPosition [[attribute(0)]]; + float3 vertexPosition [[attribute(0)]]; float4 vertexColor [[attribute(1)]]; float2 vertexTCoord [[attribute(2)]]; }; @@ -28,7 +28,7 @@ struct main0_in vertex main0_out main0(main0_in in [[stage_in]], constant vs_params& _19 [[buffer(0)]]) { main0_out out = {}; - out.gl_Position = ((_19.projectionMatrix * _19.viewMatrix) * _19.modelMatrix) * float4(in.vertexPosition, 1.0, 1.0); + out.gl_Position = ((_19.projectionMatrix * _19.viewMatrix) * _19.modelMatrix) * float4(in.vertexPosition, 1.0); out.tcoord = in.vertexTCoord; out.color = in.vertexColor; out.gl_PointSize = 1.0; diff --git a/assets/shaders/src/default.glsl b/assets/shaders/src/default.glsl index fb4e273f..28c6774e 100644 --- a/assets/shaders/src/default.glsl +++ b/assets/shaders/src/default.glsl @@ -1,5 +1,5 @@ #pragma sokol @vs vs -in vec2 vertexPosition; +in vec3 vertexPosition; in vec4 vertexColor; in vec2 vertexTCoord; in vec3 vertexNormal; @@ -14,7 +14,7 @@ uniform vs_params { }; void main(void) { - gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(vertexPosition, 1.0, 1.0); + gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(vertexPosition, 1.0); tcoord = vertexTCoord; color = vertexColor; vec3 n = vertexNormal; diff --git a/example/main.c b/example/main.c index 8a4af41a..86644a03 100644 --- a/example/main.c +++ b/example/main.c @@ -487,7 +487,7 @@ int main(int argc, char *argv[]) sprintf(filename, "%s%s", binocle_data_dir, "player.png"); sg_image ball_image = binocle_image_load(filename); - // Default shader + // Default shader (off-screen texture) char vert[1024]; sprintf(vert, "%sshaders/%s/%s", binocle_data_dir, SHADER_PATH, DEFAULT_VS_FILENAME); char frag[1024]; @@ -502,7 +502,7 @@ int main(int argc, char *argv[]) binocle_sdl_load_text_file(frag, &shader_fs_src, &shader_fs_src_size); sg_shader_desc default_shader_desc = { - + .label = "default-shader", .vs.source = shader_vs_src, #if defined(BINOCLE_METAL) .vs.entry = "main0", @@ -515,6 +515,7 @@ int main(int argc, char *argv[]) }, .vs.uniform_blocks[0] = { .size = sizeof(default_shader_params_t), + .layout = SG_UNIFORMLAYOUT_STD140, .uniforms = { [0] = { .name = "vs_params", .type = SG_UNIFORMTYPE_FLOAT4, .array_count = 12}, } @@ -556,6 +557,7 @@ int main(int argc, char *argv[]) binocle_log_info("done reading screen shader"); sg_shader_desc screen_shader_desc = { + .label = "screen-shader", .vs.source = screen_shader_vs_src, #if defined(BINOCLE_METAL) .vs.entry = "main0", @@ -567,6 +569,7 @@ int main(int argc, char *argv[]) }, .vs.uniform_blocks[0] = { .size = sizeof(screen_shader_vs_params_t), + .layout = SG_UNIFORMLAYOUT_STD140, .uniforms = { [0] = { .name = "vs_params", .type = SG_UNIFORMTYPE_FLOAT4, .array_count = 4}, }, @@ -592,6 +595,7 @@ int main(int argc, char *argv[]) }, .fs.uniform_blocks[0] = { .size = sizeof(screen_shader_fs_params_t), + .layout = SG_UNIFORMLAYOUT_STD140, .uniforms = { [0] = { .name = "fs_params", .type = SG_UNIFORMTYPE_FLOAT4, .array_count = 2 }, }, diff --git a/src/binocle/core/binocle_gd.c b/src/binocle/core/binocle_gd.c index c3ffbba3..8d4a4db6 100644 --- a/src/binocle/core/binocle_gd.c +++ b/src/binocle/core/binocle_gd.c @@ -204,14 +204,14 @@ void binocle_gd_setup_default_pipeline(binocle_gd *gd, uint32_t offscreen_width, .shader = offscreen_shader, // .index_type = SG_INDEXTYPE_UINT16, .index_type = SG_INDEXTYPE_NONE, - .depth = { - .pixel_format = SG_PIXELFORMAT_NONE, - .compare = SG_COMPAREFUNC_NEVER, - .write_enabled = false, - }, - .stencil = { - .enabled = false, - }, + // .depth = { + // .pixel_format = SG_PIXELFORMAT_NONE, + // .compare = SG_COMPAREFUNC_NEVER, + // .write_enabled = false, + // }, + // .stencil = { + // .enabled = false, + // }, .colors = { [0] = { #ifdef BINOCLE_GL @@ -288,14 +288,14 @@ void binocle_gd_setup_default_pipeline(binocle_gd *gd, uint32_t offscreen_width, .shader = display_shader, .index_type = SG_INDEXTYPE_UINT16, #if !defined(BINOCLE_GL) - .depth = { - .pixel_format = SG_PIXELFORMAT_NONE, - .compare = SG_COMPAREFUNC_NEVER, - .write_enabled = false, - }, - .stencil = { - .enabled = false, - }, + // .depth = { + // .pixel_format = SG_PIXELFORMAT_NONE, + // .compare = SG_COMPAREFUNC_NEVER, + // .write_enabled = false, + // }, + // .stencil = { + // .enabled = false, + // }, #endif .colors = { #ifdef BINOCLE_GL