Skip to content

Commit

Permalink
Trying to get Metal working again with the latest Sokol
Browse files Browse the repository at this point in the history
  • Loading branch information
tanis2000 committed Oct 11, 2024
1 parent e7da6ec commit 6791d20
Show file tree
Hide file tree
Showing 23 changed files with 131 additions and 96 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ platform/android/android-project/obj
build/
build-web
build-win
build-xcode

# emacs clangd extension cache
.cache/
Expand Down
16 changes: 16 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -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()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#version 330
#version 410

uniform vec4 fs_params[2];
uniform sampler2D tex0_smp;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#version 330
#version 410

uniform vec4 vs_params[4];
layout(location = 0) in vec3 position;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
10 changes: 5 additions & 5 deletions assets/shaders/dst/hlsl/default.glsl_default_hlsl4_vs.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
};

Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions assets/shaders/src/default.glsl
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down
12 changes: 6 additions & 6 deletions cmake/BinocleMac.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
48 changes: 19 additions & 29 deletions example/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,23 @@
#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"
#define SCREEN_VS_FILENAME "screen.glsl_default_glsl300es_vs.glsl"
#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 {
Expand Down Expand Up @@ -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);
Expand All @@ -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"},
Expand All @@ -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,
Expand All @@ -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);
Expand All @@ -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"},
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/binocle/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,4 @@ if (IOS)
)
endif(IOS)

message("${CORE_SOURCE}")
message("${SOURCE_FILES}")
14 changes: 8 additions & 6 deletions src/binocle/core/binocle_gd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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",
Expand All @@ -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},
Expand Down
Loading

0 comments on commit 6791d20

Please sign in to comment.