From 5d71290282ed5f1914e261cb029a10fa0d24589c Mon Sep 17 00:00:00 2001 From: Valerio Santinelli Date: Wed, 9 Oct 2024 21:15:49 +0200 Subject: [PATCH] Trying to get Metal working again with the latest Sokol --- .gitignore | 1 + .idea/misc.xml | 16 +++++++ ...l => default.glsl_default_glsl410_fs.glsl} | 6 +-- ...l => default.glsl_default_glsl410_vs.glsl} | 14 +++--- ...sl => screen.glsl_default_glsl410_fs.glsl} | 2 +- ...sl => screen.glsl_default_glsl410_vs.glsl} | 2 +- .../default.glsl_default_glsl300es_vs.glsl | 8 ++-- .../hlsl/default.glsl_default_hlsl4_vs.hlsl | 10 ++-- .../default.glsl_default_metal_ios_vs.metal | 8 ++-- .../default.glsl_default_metal_macos_vs.metal | 8 ++-- assets/shaders/src/default.glsl | 6 +-- cmake/BinocleMac.cmake | 12 ++--- example/CMakeLists.txt | 2 +- example/main.c | 48 ++++++++----------- src/binocle/CMakeLists.txt | 2 +- src/binocle/core/binocle_gd.c | 14 +++--- src/binocle/core/binocle_sokol.h | 5 +- src/binocle/core/binocle_sokol.m | 27 +++++++---- src/binocle/core/binocle_window.c | 20 +++++++- tools/sokol-shdc/linux/sokol-shdc | 4 +- tools/sokol-shdc/osx/sokol-shdc | 4 +- tools/sokol-shdc/osx_arm64/sokol-shdc | 4 +- tools/sokol-shdc/win32/sokol-shdc.exe | 4 +- 23 files changed, 131 insertions(+), 96 deletions(-) rename assets/shaders/dst/gl33/{default.glsl_default_glsl330_fs.glsl => default.glsl_default_glsl410_fs.glsl} (61%) rename assets/shaders/dst/gl33/{default.glsl_default_glsl330_vs.glsl => default.glsl_default_glsl410_vs.glsl} (56%) rename assets/shaders/dst/gl33/{screen.glsl_default_glsl330_fs.glsl => screen.glsl_default_glsl410_fs.glsl} (94%) rename assets/shaders/dst/gl33/{screen.glsl_default_glsl330_vs.glsl => screen.glsl_default_glsl410_vs.glsl} (93%) diff --git a/.gitignore b/.gitignore index 075c54c3..581f2d21 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ platform/android/android-project/obj build/ build-web build-win +build-xcode # emacs clangd extension cache .cache/ diff --git a/.idea/misc.xml b/.idea/misc.xml index 7c97963d..16b6f421 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,9 +1,25 @@ + + + + + + + + + + diff --git a/assets/shaders/dst/gl33/default.glsl_default_glsl330_fs.glsl b/assets/shaders/dst/gl33/default.glsl_default_glsl410_fs.glsl similarity index 61% rename from assets/shaders/dst/gl33/default.glsl_default_glsl330_fs.glsl rename to assets/shaders/dst/gl33/default.glsl_default_glsl410_fs.glsl index 756c6626..85c1bc4c 100644 --- a/assets/shaders/dst/gl33/default.glsl_default_glsl330_fs.glsl +++ b/assets/shaders/dst/gl33/default.glsl_default_glsl410_fs.glsl @@ -1,10 +1,10 @@ -#version 330 +#version 410 uniform sampler2D tex0_smp; -in vec2 tcoord; +layout(location = 0) in vec2 tcoord; layout(location = 0) out vec4 fragColor; -in vec4 color; +layout(location = 1) in vec4 color; void main() { diff --git a/assets/shaders/dst/gl33/default.glsl_default_glsl330_vs.glsl b/assets/shaders/dst/gl33/default.glsl_default_glsl410_vs.glsl similarity index 56% rename from assets/shaders/dst/gl33/default.glsl_default_glsl330_vs.glsl rename to assets/shaders/dst/gl33/default.glsl_default_glsl410_vs.glsl index 174fec6d..1665a3a4 100644 --- a/assets/shaders/dst/gl33/default.glsl_default_glsl330_vs.glsl +++ b/assets/shaders/dst/gl33/default.glsl_default_glsl410_vs.glsl @@ -1,16 +1,16 @@ -#version 330 +#version 410 uniform vec4 vs_params[12]; -layout(location = 0) in vec3 vertexPosition; -out vec2 tcoord; -layout(location = 1) in vec2 vertexTCoord; -out vec4 color; -layout(location = 2) in vec4 vertexColor; +layout(location = 0) in vec2 vertexPosition; +layout(location = 0) out vec2 tcoord; +layout(location = 2) in vec2 vertexTCoord; +layout(location = 1) out vec4 color; +layout(location = 1) in vec4 vertexColor; 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); + 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); tcoord = vertexTCoord; color = vertexColor; gl_PointSize = 1.0; diff --git a/assets/shaders/dst/gl33/screen.glsl_default_glsl330_fs.glsl b/assets/shaders/dst/gl33/screen.glsl_default_glsl410_fs.glsl similarity index 94% rename from assets/shaders/dst/gl33/screen.glsl_default_glsl330_fs.glsl rename to assets/shaders/dst/gl33/screen.glsl_default_glsl410_fs.glsl index 9cb7d4c8..3b486486 100644 --- a/assets/shaders/dst/gl33/screen.glsl_default_glsl330_fs.glsl +++ b/assets/shaders/dst/gl33/screen.glsl_default_glsl410_fs.glsl @@ -1,4 +1,4 @@ -#version 330 +#version 410 uniform vec4 fs_params[2]; uniform sampler2D tex0_smp; diff --git a/assets/shaders/dst/gl33/screen.glsl_default_glsl330_vs.glsl b/assets/shaders/dst/gl33/screen.glsl_default_glsl410_vs.glsl similarity index 93% rename from assets/shaders/dst/gl33/screen.glsl_default_glsl330_vs.glsl rename to assets/shaders/dst/gl33/screen.glsl_default_glsl410_vs.glsl index 086da99a..9816c398 100644 --- a/assets/shaders/dst/gl33/screen.glsl_default_glsl330_vs.glsl +++ b/assets/shaders/dst/gl33/screen.glsl_default_glsl410_vs.glsl @@ -1,4 +1,4 @@ -#version 330 +#version 410 uniform vec4 vs_params[4]; layout(location = 0) in vec3 position; 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 3428590c..fb9f864d 100644 --- a/assets/shaders/dst/gles/default.glsl_default_glsl300es_vs.glsl +++ b/assets/shaders/dst/gles/default.glsl_default_glsl300es_vs.glsl @@ -1,16 +1,16 @@ #version 300 es uniform vec4 vs_params[12]; -layout(location = 0) in vec3 vertexPosition; +layout(location = 0) in vec2 vertexPosition; out vec2 tcoord; -layout(location = 1) in vec2 vertexTCoord; +layout(location = 2) in vec2 vertexTCoord; out vec4 color; -layout(location = 2) in vec4 vertexColor; +layout(location = 1) in vec4 vertexColor; 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); + 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); 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 25b68b4c..d8b531ba 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 float3 vertexPosition; +static float2 vertexPosition; static float2 tcoord; static float2 vertexTCoord; static float4 color; @@ -17,9 +17,9 @@ static float3 vertexNormal; struct SPIRV_Cross_Input { - float3 vertexPosition : TEXCOORD0; - float2 vertexTCoord : TEXCOORD1; - float4 vertexColor : TEXCOORD2; + float2 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), mul(_19_modelMatrix, mul(_19_viewMatrix, _19_projectionMatrix))); + gl_Position = mul(float4(vertexPosition, 1.0f, 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 2bf6ad39..a1c36523 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,15 +20,15 @@ struct main0_out struct main0_in { - float3 vertexPosition [[attribute(0)]]; - float2 vertexTCoord [[attribute(1)]]; - float4 vertexColor [[attribute(2)]]; + float2 vertexPosition [[attribute(0)]]; + float4 vertexColor [[attribute(1)]]; + float2 vertexTCoord [[attribute(2)]]; }; 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); + out.gl_Position = ((_19.projectionMatrix * _19.viewMatrix) * _19.modelMatrix) * float4(in.vertexPosition, 1.0, 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 2bf6ad39..a1c36523 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,15 +20,15 @@ struct main0_out struct main0_in { - float3 vertexPosition [[attribute(0)]]; - float2 vertexTCoord [[attribute(1)]]; - float4 vertexColor [[attribute(2)]]; + float2 vertexPosition [[attribute(0)]]; + float4 vertexColor [[attribute(1)]]; + float2 vertexTCoord [[attribute(2)]]; }; 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); + out.gl_Position = ((_19.projectionMatrix * _19.viewMatrix) * _19.modelMatrix) * float4(in.vertexPosition, 1.0, 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 9dbb500f..fb4e273f 100644 --- a/assets/shaders/src/default.glsl +++ b/assets/shaders/src/default.glsl @@ -1,7 +1,7 @@ #pragma sokol @vs vs -in vec3 vertexPosition; -in vec2 vertexTCoord; +in vec2 vertexPosition; in vec4 vertexColor; +in vec2 vertexTCoord; in vec3 vertexNormal; out vec2 tcoord; @@ -14,7 +14,7 @@ uniform vs_params { }; void main(void) { - gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(vertexPosition, 1.0); + gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(vertexPosition, 1.0, 1.0); tcoord = vertexTCoord; color = vertexColor; vec3 n = vertexNormal; diff --git a/cmake/BinocleMac.cmake b/cmake/BinocleMac.cmake index 1ecb1c0d..c0830518 100644 --- a/cmake/BinocleMac.cmake +++ b/cmake/BinocleMac.cmake @@ -3,18 +3,18 @@ add_definitions (-D__APPLE__) add_definitions (-DBINOCLE_MACOS) -#add_definitions (-DBINOCLE_METAL) -#set (BINOCLE_METAL true) +add_definitions (-DBINOCLE_METAL) +set (BINOCLE_METAL true) -add_definitions (-DBINOCLE_GL) -set(BINOCLE_GL true) +#add_definitions (-DBINOCLE_GL) +#set(BINOCLE_GL true) list (APPEND BINOCLE_LINK_LIBRARIES "-l iconv") set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-invalid-offsetof -std=gnu++0x") -set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -framework AppKit -framework AudioToolbox -framework AudioUnit -framework Carbon -framework Cocoa -framework CoreAudio -framework CoreHaptics -framework CoreMotion -framework CoreVideo -framework ForceFeedback -framework GameController -framework IOKit -framework OpenGL -framework CoreServices -framework Security -framework Metal") -set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -framework AppKit -framework AudioToolbox -framework AudioUnit -framework Carbon -framework Cocoa -framework CoreAudio -framework CoreHaptics -framework CoreMotion -framework CoreVideo -framework ForceFeedback -framework GameController -framework IOKit -framework OpenGL -framework CoreServices -framework Security -framework Metal") +set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -framework AppKit -framework AudioToolbox -framework AudioUnit -framework Carbon -framework Cocoa -framework CoreAudio -framework CoreHaptics -framework CoreMotion -framework CoreVideo -framework ForceFeedback -framework GameController -framework IOKit -framework OpenGL -framework CoreServices -framework Security -framework Metal -framework MetalKit") +set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -framework AppKit -framework AudioToolbox -framework AudioUnit -framework Carbon -framework Cocoa -framework CoreAudio -framework CoreHaptics -framework CoreMotion -framework CoreVideo -framework ForceFeedback -framework GameController -framework IOKit -framework OpenGL -framework CoreServices -framework Security -framework Metal -framework MetalKit") # set(EXTRA_LIBRARIES "${EXTRA_LIBRARIES} -framework IOKit -framework AppKit -framework GLUT -framework GLKit -framework OpenGL -framework AudioToolbox -framework OpenAL -framework CoreAudio -framework AudioUnit -framework QuartzCore -framework CoreGraphics -framework CoreServices -framework ForceFeedback") set(CMAKE_OSX_DEPLOYMENT_TARGET 10.9) diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index ffcf0ab1..4978c1cd 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -324,7 +324,7 @@ foreach(GLSL ${GLSL_SOURCE_FILES}) COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_SOURCE_DIR}/assets/shaders/dst/metal-macos" COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_SOURCE_DIR}/assets/shaders/dst/metal-ios" COMMAND "${CMAKE_SOURCE_DIR}/tools/sokol-shdc/${SHADER_COMPILER}" - ARGS "--input" "${GLSL}" "--output" "${SPIRV_GL33}" "--slang" "glsl330" "-f" "bare" + ARGS "--input" "${GLSL}" "--output" "${SPIRV_GL33}" "--slang" "glsl410" "-f" "bare" COMMAND "${CMAKE_SOURCE_DIR}/tools/sokol-shdc/${SHADER_COMPILER}" # Enable the following to get a C output of the shaders for debugging purposes # ARGS "--input" "${GLSL}" "--output" "${SPIRV_GL33}" "--slang" "glsl330" "-f" "sokol" diff --git a/example/main.c b/example/main.c index 31c5bfe7..8a4af41a 100644 --- a/example/main.c +++ b/example/main.c @@ -38,11 +38,12 @@ #include "constants.h" #if defined(BINOCLE_MACOS) && defined(BINOCLE_METAL) -#include "../../assets/metal/default-metal-macosx.h" -#include "../../assets/metal/screen-metal-macosx.h" -#endif - -#if defined(__IPHONEOS__) || defined(__ANDROID__) || defined(__EMSCRIPTEN__) +#define SHADER_PATH "dst/metal-macos" +#define DEFAULT_VS_FILENAME "default.glsl_default_metal_macos_vs.metal" +#define DEFAULT_FS_FILENAME "default.glsl_default_metal_macos_fs.metal" +#define SCREEN_VS_FILENAME "screen.glsl_default_metal_macos_vs.metal" +#define SCREEN_FS_FILENAME "screen.glsl_default_metal_macos_fs.metal" +#elif defined(__IPHONEOS__) || defined(__ANDROID__) || defined(__EMSCRIPTEN__) #define SHADER_PATH "dst/gles" #define DEFAULT_VS_FILENAME "default.glsl_default_glsl300es_vs.glsl" #define DEFAULT_FS_FILENAME "default.glsl_default_glsl300es_fs.glsl" @@ -50,10 +51,10 @@ #define SCREEN_FS_FILENAME "screen.glsl_default_glsl300es_fs.glsl" #else #define SHADER_PATH "dst/gl33" -#define DEFAULT_VS_FILENAME "default.glsl_default_glsl330_vs.glsl" -#define DEFAULT_FS_FILENAME "default.glsl_default_glsl330_fs.glsl" -#define SCREEN_VS_FILENAME "screen.glsl_default_glsl330_vs.glsl" -#define SCREEN_FS_FILENAME "screen.glsl_default_glsl330_fs.glsl" +#define DEFAULT_VS_FILENAME "default.glsl_default_glsl410_vs.glsl" +#define DEFAULT_FS_FILENAME "default.glsl_default_glsl410_fs.glsl" +#define SCREEN_VS_FILENAME "screen.glsl_default_glsl410_vs.glsl" +#define SCREEN_FS_FILENAME "screen.glsl_default_glsl410_fs.glsl" #endif typedef struct default_shader_params_t { @@ -486,7 +487,6 @@ int main(int argc, char *argv[]) sprintf(filename, "%s%s", binocle_data_dir, "player.png"); sg_image ball_image = binocle_image_load(filename); -#ifdef BINOCLE_GL // Default shader char vert[1024]; sprintf(vert, "%sshaders/%s/%s", binocle_data_dir, SHADER_PATH, DEFAULT_VS_FILENAME); @@ -500,14 +500,12 @@ int main(int argc, char *argv[]) char *shader_fs_src; size_t shader_fs_src_size; binocle_sdl_load_text_file(frag, &shader_fs_src, &shader_fs_src_size); -#endif sg_shader_desc default_shader_desc = { -#ifdef BINOCLE_GL + .vs.source = shader_vs_src, -#else - .vs.bytecode.ptr = default_vs_bytecode, - .vs.bytecode.size = sizeof(default_vs_bytecode), +#if defined(BINOCLE_METAL) + .vs.entry = "main0", #endif .attrs = { [0] = { .name = "vertexPosition"}, @@ -521,11 +519,9 @@ int main(int argc, char *argv[]) [0] = { .name = "vs_params", .type = SG_UNIFORMTYPE_FLOAT4, .array_count = 12}, } }, -#ifdef BINOCLE_GL .fs.source = shader_fs_src, -#else - .fs.bytecode.ptr = default_fs_bytecode, - .fs.bytecode.size = sizeof(default_fs_bytecode), +#if defined(BINOCLE_METAL) + .fs.entry = "main0", #endif .fs.images[0] = { .used = true, @@ -545,7 +541,6 @@ int main(int argc, char *argv[]) }; default_shader = sg_make_shader(&default_shader_desc); -#ifdef BINOCLE_GL // Screen shader sprintf(vert, "%sshaders/%s/%s", binocle_data_dir, SHADER_PATH, SCREEN_VS_FILENAME); sprintf(frag, "%sshaders/%s/%s", binocle_data_dir, SHADER_PATH, SCREEN_FS_FILENAME); @@ -559,14 +554,11 @@ int main(int argc, char *argv[]) binocle_log_info("reading screen shader"); binocle_sdl_load_text_file(frag, &screen_shader_fs_src, &screen_shader_fs_src_size); binocle_log_info("done reading screen shader"); -#endif sg_shader_desc screen_shader_desc = { -#ifdef BINOCLE_GL .vs.source = screen_shader_vs_src, -#else - .vs.bytecode.ptr = screen_vs_bytecode, - .vs.bytecode.size = sizeof(screen_vs_bytecode), +#if defined(BINOCLE_METAL) + .vs.entry = "main0", #endif .attrs = { [0] = {.name = "position"}, @@ -579,11 +571,9 @@ int main(int argc, char *argv[]) [0] = { .name = "vs_params", .type = SG_UNIFORMTYPE_FLOAT4, .array_count = 4}, }, }, -#ifdef BINOCLE_GL .fs.source = screen_shader_fs_src, -#else - .fs.bytecode.ptr = screen_fs_bytecode, - .fs.bytecode.size = sizeof(screen_fs_bytecode), +#if defined(BINOCLE_METAL) + .fs.entry = "main0", #endif .fs.images[0] = { .used = true, diff --git a/src/binocle/CMakeLists.txt b/src/binocle/CMakeLists.txt index 9cd2ea66..25992d70 100644 --- a/src/binocle/CMakeLists.txt +++ b/src/binocle/CMakeLists.txt @@ -104,4 +104,4 @@ if (IOS) ) endif(IOS) -message("${CORE_SOURCE}") \ No newline at end of file +message("${SOURCE_FILES}") \ No newline at end of file diff --git a/src/binocle/core/binocle_gd.c b/src/binocle/core/binocle_gd.c index 96348c7a..c3ffbba3 100644 --- a/src/binocle/core/binocle_gd.c +++ b/src/binocle/core/binocle_gd.c @@ -162,7 +162,7 @@ void binocle_gd_setup_default_pipeline(binocle_gd *gd, uint32_t offscreen_width, #ifdef BINOCLE_GL .pixel_format = SG_PIXELFORMAT_RGBA8, #else - .pixel_format = SG_PIXELFORMAT_BGRA8, + .pixel_format = SG_PIXELFORMAT_RGBA8, #endif .sample_count = 1, .label = "offscreen" @@ -217,7 +217,7 @@ void binocle_gd_setup_default_pipeline(binocle_gd *gd, uint32_t offscreen_width, #ifdef BINOCLE_GL .pixel_format = SG_PIXELFORMAT_RGBA8, #else - .pixel_format = SG_PIXELFORMAT_BGRA8, + .pixel_format = SG_PIXELFORMAT_RGBA8, #endif .blend = { .enabled = true, @@ -1245,8 +1245,9 @@ sg_shader_desc binocle_gd_create_offscreen_shader_desc(const char *shader_vs_src #ifdef BINOCLE_GL .vs.source = shader_vs_src, #else - .vs.byte_code = default_vs_bytecode, - .vs.byte_code_size = sizeof(default_vs_bytecode), + .vs.source = shader_vs_src, + // .vs.byte_code = default_vs_bytecode, + // .vs.byte_code_size = sizeof(default_vs_bytecode), #endif .attrs = { [0].name = "vertexPosition", @@ -1264,8 +1265,9 @@ sg_shader_desc binocle_gd_create_offscreen_shader_desc(const char *shader_vs_src #ifdef BINOCLE_GL .fs.source = shader_fs_src, #else - .fs.byte_code = default_fs_bytecode, - .fs.byte_code_size = sizeof(default_fs_bytecode), + .fs.source = shader_fs_src, + // .fs.byte_code = default_fs_bytecode, + // .fs.byte_code_size = sizeof(default_fs_bytecode), #endif .fs.images[0] = {.used = true, .image_type = SG_IMAGETYPE_2D}, .fs.samplers[0] = {.used = true, .sampler_type = SG_SAMPLERTYPE_FILTERING}, diff --git a/src/binocle/core/binocle_sokol.h b/src/binocle/core/binocle_sokol.h index bf7ba4ee..e3fcae14 100644 --- a/src/binocle/core/binocle_sokol.h +++ b/src/binocle/core/binocle_sokol.h @@ -5,5 +5,8 @@ void binocle_metal_init(void *mtl_view); // sg_context_desc binocle_metal_get_context(void); const void* binocle_metal_get_device(void); -GLuint binocle_sokol_tex_id(sg_image img_id); +const void* binocle_sokol_mtk_get_drawable(void); +const void* binocle_sokol_mtk_get_depth_stencil_texture(void); +const void* binocle_sokol_mtk_get_msaa_color_texture(void); +// GLuint binocle_sokol_tex_id(sg_image img_id); #endif \ No newline at end of file diff --git a/src/binocle/core/binocle_sokol.m b/src/binocle/core/binocle_sokol.m index 50464b7b..22a76100 100644 --- a/src/binocle/core/binocle_sokol.m +++ b/src/binocle/core/binocle_sokol.m @@ -40,10 +40,17 @@ void binocle_metal_init(void *mtl_view) { return desc; } -static const void* binocle_sokol_mtk_get_drawable(void* user_data) { - return cached_drawable_hack; +const void* binocle_sokol_mtk_get_drawable(void) { + return (__bridge const void*) [metal_layer nextDrawable]; } +const void* binocle_sokol_mtk_get_depth_stencil_texture(void) { + return (__bridge const void*) [metal_layer pixelFormat]; +} + +const void* binocle_sokol_mtk_get_msaa_color_texture(void) { + return (__bridge const void*) [metal_layer pixelFormat]; +} //sg_context_desc binocle_metal_get_context(void) { // return (sg_context_desc) { // .sample_count = 1, @@ -60,11 +67,11 @@ void binocle_metal_init(void *mtl_view) { return (__bridge const void*) device; } -GLuint binocle_sokol_tex_id(sg_image img_id) { - SOKOL_ASSERT(img_id.id != SG_INVALID_ID); - _sg_image_t* img = _sg_lookup_image(&_sg.pools, img_id.id); - SOKOL_ASSERT(img); - SOKOL_ASSERT(img->gl.target == GL_TEXTURE_2D); - SOKOL_ASSERT(0 != img->gl.tex[img->cmn.active_slot]); - return img->gl.tex[img->cmn.active_slot]; -} \ No newline at end of file +//GLuint binocle_sokol_tex_id(sg_image img_id) { +// SOKOL_ASSERT(img_id.id != SG_INVALID_ID); +// _sg_image_t* img = _sg_lookup_image(&_sg.pools, img_id.id); +// SOKOL_ASSERT(img); +// SOKOL_ASSERT(img->gl.target == GL_TEXTURE_2D); +// SOKOL_ASSERT(0 != img->gl.tex[img->cmn.active_slot]); +// return img->gl.tex[img->cmn.active_slot]; +//} \ No newline at end of file diff --git a/src/binocle/core/binocle_window.c b/src/binocle/core/binocle_window.c index 292df883..e1723107 100644 --- a/src/binocle/core/binocle_window.c +++ b/src/binocle/core/binocle_window.c @@ -11,6 +11,9 @@ #include "binocle_sdl.h" #include #include +#if defined(BINOCLE_METAL) +#include "binocle_sokol.h" +#endif binocle_window *binocle_window_new(uint32_t width, uint32_t height, char *title) { binocle_window *res = SDL_malloc(sizeof(binocle_window)); @@ -356,7 +359,11 @@ bool binocle_window_get_display_size(uint32_t *w, uint32_t *h) { sg_environment binocle_window_get_environment(binocle_window *window) { return (sg_environment) { .defaults = { +#if defined(BINOCLE_GL) .color_format = SG_PIXELFORMAT_RGBA8, +#else + .color_format = SG_PIXELFORMAT_BGRA8, +#endif .depth_format = window->has_depth_buffer ? SG_PIXELFORMAT_NONE : SG_PIXELFORMAT_DEPTH_STENCIL, .sample_count = window->sample_count, }, @@ -367,11 +374,20 @@ sg_swapchain binocle_window_get_swapchain(binocle_window *window) { .width = window->width, .height = window->height, .sample_count = window->sample_count, - .color_format = SG_PIXELFORMAT_RGBA8, .depth_format = window->has_depth_buffer ? SG_PIXELFORMAT_NONE : SG_PIXELFORMAT_DEPTH_STENCIL, +#if defined(BINOCLE_GL) + .color_format = SG_PIXELFORMAT_RGBA8, .gl = { // we just assume here that the GL framebuffer is always 0 .framebuffer = 0, - } + } +#else + .color_format = SG_PIXELFORMAT_BGRA8, + .metal = { + .current_drawable = binocle_sokol_mtk_get_drawable(), + //.depth_stencil_texture = binocle_sokol_mtk_get_depth_stencil_texture(), + // .msaa_color_texture = binocle_sokol_mtk_get_msaa_color_texture(), + } +#endif }; } \ No newline at end of file diff --git a/tools/sokol-shdc/linux/sokol-shdc b/tools/sokol-shdc/linux/sokol-shdc index 05cd26be..1f3933de 100755 --- a/tools/sokol-shdc/linux/sokol-shdc +++ b/tools/sokol-shdc/linux/sokol-shdc @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:e955f478ea526f7bc9b4a90393fec63b0080409588d3564bd8e666c4c40258fe -size 13264640 +oid sha256:82031a2c5efa7587b7d484fdefdbcc0ea65313e8aa3d6af8e989b0063cbdbd26 +size 15249168 diff --git a/tools/sokol-shdc/osx/sokol-shdc b/tools/sokol-shdc/osx/sokol-shdc index df9bd907..6e26995e 100755 --- a/tools/sokol-shdc/osx/sokol-shdc +++ b/tools/sokol-shdc/osx/sokol-shdc @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:49989b53861a4138ed22c6807b3a77e1a061efbfb9a439b6f68199555f3c6e2e -size 10965128 +oid sha256:c7bbc5b0c09a8a48731de2dfb8114bd20c6f89ffcf7a5584ffdb5a341300e4e6 +size 13705072 diff --git a/tools/sokol-shdc/osx_arm64/sokol-shdc b/tools/sokol-shdc/osx_arm64/sokol-shdc index 70333389..7ae1274d 100755 --- a/tools/sokol-shdc/osx_arm64/sokol-shdc +++ b/tools/sokol-shdc/osx_arm64/sokol-shdc @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:001440b24fa407c1cdc111d3c8041df4fc516f6810d5b8ee56d25486552f5945 -size 9465776 +oid sha256:e5562b4166a493c18c068014eab6b528f1d748289f0baecc7f678655804c7eea +size 12654328 diff --git a/tools/sokol-shdc/win32/sokol-shdc.exe b/tools/sokol-shdc/win32/sokol-shdc.exe index b3a11311..322a67a3 100644 --- a/tools/sokol-shdc/win32/sokol-shdc.exe +++ b/tools/sokol-shdc/win32/sokol-shdc.exe @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:99f9667555b1bf3e27e9c08489a9c2a06c92daf1b0cf003b431e1559f3c0fbbf -size 8639488 +oid sha256:fb67d82fb3129de978d16ea2e2c6ce61cbaca2450d5a800d94da34a7b5532d7c +size 9434112