From e7d392dac8549cac026a2e8fd209631ee2dc32dd Mon Sep 17 00:00:00 2001 From: Valerio Santinelli Date: Fri, 11 Oct 2024 17:07:00 +0200 Subject: [PATCH] Changes to improve the flat pipeline --- src/binocle/core/binocle_app_wrap.c | 34 +++++++- src/binocle/core/binocle_gd.c | 125 +++++----------------------- src/binocle/core/binocle_gd.h | 4 +- src/binocle/core/binocle_gd_wrap.c | 7 +- 4 files changed, 58 insertions(+), 112 deletions(-) diff --git a/src/binocle/core/binocle_app_wrap.c b/src/binocle/core/binocle_app_wrap.c index 4a89b7ff..e7457023 100644 --- a/src/binocle/core/binocle_app_wrap.c +++ b/src/binocle/core/binocle_app_wrap.c @@ -12,10 +12,36 @@ int l_binocle_app_assets_dir(lua_State *L) { } int l_binocle_app_shader_prefix(lua_State *L) { -#if defined(__IPHONEOS__) || defined(__ANDROID__) || defined(__EMSCRIPTEN__) - char *s = "gles"; +#if defined(BINOCLE_MACOS) && defined(BINOCLE_METAL) + char *s = "dst/metal-macos"; +#elif defined(__IPHONEOS__) || defined(__ANDROID__) || defined(__EMSCRIPTEN__) + char *s = "dst/gles"; #else - char *s = "gl33"; + char *s = "dst/gl33"; +#endif + lua_pushstring(L, s); + return 1; +} + +int l_binocle_app_shader_vs_suffix(lua_State *L) { +#if defined(BINOCLE_MACOS) && defined(BINOCLE_METAL) + char *s = "_metal_macos_vs.metal"; +#elif defined(__IPHONEOS__) || defined(__ANDROID__) || defined(__EMSCRIPTEN__) + char *s = "_glsl300es_vs.glsl"; +#else + char *s = "_glsl410_vs.glsl"; +#endif + lua_pushstring(L, s); + return 1; +} + +int l_binocle_app_shader_fs_suffix(lua_State *L) { +#if defined(BINOCLE_MACOS) && defined(BINOCLE_METAL) + char *s = "_metal_macos_fs.metal"; +#elif defined(__IPHONEOS__) || defined(__ANDROID__) || defined(__EMSCRIPTEN__) + char *s = "_glsl300es_fs.glsl"; +#else + char *s = "_glsl410_fs.glsl"; #endif lua_pushstring(L, s); return 1; @@ -24,6 +50,8 @@ int l_binocle_app_shader_prefix(lua_State *L) { static const struct luaL_Reg app [] = { {"assets_dir", l_binocle_app_assets_dir}, {"shader_prefix", l_binocle_app_shader_prefix}, + {"shader_vs_suffix", l_binocle_app_shader_vs_suffix}, + {"shader_fs_suffix", l_binocle_app_shader_fs_suffix}, {NULL, NULL} }; diff --git a/src/binocle/core/binocle_gd.c b/src/binocle/core/binocle_gd.c index 8d4a4db6..8938e635 100644 --- a/src/binocle/core/binocle_gd.c +++ b/src/binocle/core/binocle_gd.c @@ -27,67 +27,6 @@ typedef struct binocle_gd_flat_shader_fs_params_t { float color[4]; } binocle_gd_flat_shader_fs_params_t; -const char *binocle_shader_flat_vertex_src_gles = -"#version 300 es\n" -"precision mediump float;\n" -"precision mediump int;\n" -"in vec3 vertexPosition;\n" -"in vec4 vertexColor;\n" -"in vec2 vertexTexture;\n" -"\n" -"uniform mat4 projectionMatrix;\n" -"uniform mat4 viewMatrix;\n" -"uniform mat4 modelMatrix;\n" -"\n" -"out vec4 color;\n" -"\n" -"void main(void) {\n" -" gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(vertexPosition, 1.0);\n" -" gl_PointSize = 1.0;\n" -" color = vertexColor;\n" -"}\n"; - -const char *binocle_shader_flat_frag_src_gles = -"#version 300 es\n" -"precision mediump float;\n" -"precision mediump int;\n" -"in vec4 color;\n" -"out vec4 fragColor;\n" -"void main(void)\n" -"{\n" -" fragColor = color;\n" -" //gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);\n" -"}\n"; - -const char *binocle_shader_flat_vertex_src_gl33 = -"#version 330\n" -"in vec3 vertexPosition;\n" -"in vec4 vertexColor;\n" -"in vec2 vertexTexture;\n" -"\n" -"uniform mat4 projectionMatrix;\n" -"uniform mat4 viewMatrix;\n" -"uniform mat4 modelMatrix;\n" -"\n" -"out vec4 color;" -"\n" -"void main(void) {\n" -" gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(vertexPosition, 1.0);\n" -" //gl_Position = vec4(0.0, 0.0, 0.0, 1.0);\n" -" gl_PointSize = 1.0;\n" -" color = vertexColor;\n" -"}\n"; - -const char *binocle_shader_flat_src_gl33 = -"#version 330\n" -"in vec4 color;\n" -"out vec4 fragColor;\n" -"void main(void)\n" -"{\n" -" fragColor = color;\n" -" //fragColor = vec4(1.0, 1.0, 1.0, 1.0);\n" -"}\n"; - binocle_gd binocle_gd_new() { binocle_gd res = {0}; res.vertices = malloc(sizeof(binocle_vpct) * BINOCLE_GD_MAX_VERTICES); @@ -491,17 +430,11 @@ void binocle_gd_render(binocle_gd *gd, struct binocle_window *window, float desi binocle_gd_render_screen(gd, window, design_width, design_height, viewport, matrix, scale); } -void binocle_gd_setup_flat_pipeline(binocle_gd *gd) { -#if defined(BINOCLE_MACOS) && defined(BINOCLE_METAL) -#include "../../assets/metal/default-metal-macosx.h" -#include "../../assets/metal/screen-metal-macosx.h" -#endif +void binocle_gd_setup_flat_pipeline(binocle_gd *gd, const char *vs_src, const char *fs_src) { sg_shader_desc screen_shader_desc = { -#ifdef BINOCLE_GL -#if defined(__IPHONEOS__) || defined(__ANDROID__) || defined(__EMSCRIPTEN__) - .vs.source = binocle_shader_flat_vertex_src_gles, -#else - .vs.source = binocle_shader_flat_vertex_src_gl33, + .vs.source = vs_src, +#if defined(BINOCLE_METAL) + .vs.entry = "main0", #endif .vs.uniform_blocks[0] = { .size = sizeof(struct binocle_gd_flat_shader_vs_params_t), @@ -511,24 +444,14 @@ void binocle_gd_setup_flat_pipeline(binocle_gd *gd) { [2] = { .name = "modelMatrix", .type = SG_UNIFORMTYPE_MAT4 }, }, }, -#else - .vs.bytecode.ptr = screen_vs_bytecode, - .vs.bytecode.size = sizeof(screen_vs_bytecode), -#endif .attrs = { [0].name = "vertexPosition", [1].name = "vertexColor", [2].name = "vertexTexture", }, -#ifdef BINOCLE_GL -#if defined(__IPHONEOS__) || defined(__ANDROID__) || defined(__EMSCRIPTEN__) - .fs.source = binocle_shader_flat_frag_src_gles, -#else - .fs.source = binocle_shader_flat_src_gl33, -#endif -#else - .fs.bytecode.ptr = screen_fs_bytecode, - .fs.bytecode.size = sizeof(screen_fs_bytecode), + .fs.source = fs_src, +#if defined(BINOCLE_METAL) + .fs.entry = "main0", #endif // .fs.uniform_blocks[0] = { // .size = sizeof(struct binocle_gd_flat_shader_fs_params_t), @@ -577,7 +500,7 @@ void binocle_gd_setup_flat_pipeline(binocle_gd *gd) { #ifdef BINOCLE_GL .pixel_format = SG_PIXELFORMAT_RGBA8, #else - .pixel_format = SG_PIXELFORMAT_BGRA8, + .pixel_format = SG_PIXELFORMAT_RGBA8, #endif .blend = { .enabled = true, @@ -1240,14 +1163,12 @@ void binocle_gd_draw_test_cube(struct sg_shader *shader) { // glCheck(glDeleteBuffers(1, &quad_indexbuffer)); } -sg_shader_desc binocle_gd_create_offscreen_shader_desc(const char *shader_vs_src, const char *shader_fs_src) { +sg_shader_desc binocle_gd_create_offscreen_shader_desc(const char *name, const char *shader_vs_src, const char *shader_fs_src) { sg_shader_desc shader_desc = { -#ifdef BINOCLE_GL - .vs.source = shader_vs_src, -#else + .label = name, .vs.source = shader_vs_src, - // .vs.byte_code = default_vs_bytecode, - // .vs.byte_code_size = sizeof(default_vs_bytecode), +#if defined(BINOCLE_METAL) + .vs.entry = "main0", #endif .attrs = { [0].name = "vertexPosition", @@ -1256,23 +1177,19 @@ sg_shader_desc binocle_gd_create_offscreen_shader_desc(const char *shader_vs_src }, .vs.uniform_blocks[0] = { .size = sizeof(float) * 16 * 3, + .layout = SG_UNIFORMLAYOUT_STD140, .uniforms = { - [0] = { .name = "projectionMatrix", .type = SG_UNIFORMTYPE_MAT4}, - [1] = { .name = "viewMatrix", .type = SG_UNIFORMTYPE_MAT4}, - [2] = { .name = "modelMatrix", .type = SG_UNIFORMTYPE_MAT4}, + [0] = { .name = "vs_params", .type = SG_UNIFORMTYPE_FLOAT4, .array_count = 12}, } }, -#ifdef BINOCLE_GL - .fs.source = shader_fs_src, -#else .fs.source = shader_fs_src, - // .fs.byte_code = default_fs_bytecode, - // .fs.byte_code_size = sizeof(default_fs_bytecode), +#if defined(BINOCLE_METAL) + .fs.entry = "main0", #endif - .fs.images[0] = {.used = true, .image_type = SG_IMAGETYPE_2D}, + .fs.images[0] = {.used = true, .image_type = SG_IMAGETYPE_2D, .sample_type = SG_IMAGESAMPLETYPE_FLOAT,}, .fs.samplers[0] = {.used = true, .sampler_type = SG_SAMPLERTYPE_FILTERING}, .fs.image_sampler_pairs[0] = {.used = true, - .glsl_name = "tex0", + .glsl_name = "tex0_smp", .image_slot = 0, .sampler_slot = 0}, }; @@ -1324,16 +1241,16 @@ void binocle_gd_add_uniform_to_shader_desc(sg_shader_desc *shader_desc, sg_shade case SG_SHADERSTAGE_VS: shader_desc->vs.uniform_blocks[0].uniforms[idx].name = SDL_strdup(uniform_name);; shader_desc->vs.uniform_blocks[0].uniforms[idx].type = uniform_type; + shader_desc->vs.uniform_blocks[0].size = binocle_gd_compute_uniform_block_size(shader_desc->vs.uniform_blocks[0]); break; case SG_SHADERSTAGE_FS: shader_desc->fs.uniform_blocks[0].uniforms[idx].name = SDL_strdup(uniform_name);; shader_desc->fs.uniform_blocks[0].uniforms[idx].type = uniform_type; + shader_desc->fs.uniform_blocks[0].size = binocle_gd_compute_uniform_block_size(shader_desc->fs.uniform_blocks[0]); break; default: break; } - shader_desc->vs.uniform_blocks[0].size = binocle_gd_compute_uniform_block_size(shader_desc->vs.uniform_blocks[0]); - shader_desc->fs.uniform_blocks[0].size = binocle_gd_compute_uniform_block_size(shader_desc->fs.uniform_blocks[0]); } sg_shader binocle_gd_create_shader(sg_shader_desc desc) { @@ -1365,7 +1282,7 @@ sg_pipeline binocle_gd_create_offscreen_pipeline(sg_shader shader) { #ifdef BINOCLE_GL .pixel_format = SG_PIXELFORMAT_RGBA8, #else - .pixel_format = SG_PIXELFORMAT_BGRA8, + .pixel_format = SG_PIXELFORMAT_RGBA8, #endif .blend = { .enabled = true, diff --git a/src/binocle/core/binocle_gd.h b/src/binocle/core/binocle_gd.h index 9ac1ab94..66cb381e 100644 --- a/src/binocle/core/binocle_gd.h +++ b/src/binocle/core/binocle_gd.h @@ -332,10 +332,10 @@ void binocle_gd_draw_with_state(binocle_gd *gd, const struct binocle_vpct *verti void binocle_gd_draw_mesh(binocle_gd *gd, const struct binocle_mesh *mesh, kmAABB2 viewport, struct binocle_camera_3d *camera); void binocle_gd_draw_test_triangle(struct sg_shader *shader); void binocle_gd_draw_test_cube(struct sg_shader *shader); -void binocle_gd_setup_flat_pipeline(binocle_gd *gd); +void binocle_gd_setup_flat_pipeline(binocle_gd *gd, const char *vs_src, const char *fs_src); void binocle_gd_render_flat(binocle_gd *gd); -sg_shader_desc binocle_gd_create_offscreen_shader_desc(const char *shader_vs_src, const char *shader_fs_src); +sg_shader_desc binocle_gd_create_offscreen_shader_desc(const char *name, const char *shader_vs_src, const char *shader_fs_src); size_t binocle_gd_compute_uniform_block_size(sg_shader_uniform_block_desc desc); void binocle_gd_add_uniform_to_shader_desc(sg_shader_desc *shader_desc, sg_shader_stage stage, size_t idx, const char *uniform_name, sg_uniform_type uniform_type); sg_shader binocle_gd_create_shader(sg_shader_desc desc); diff --git a/src/binocle/core/binocle_gd_wrap.c b/src/binocle/core/binocle_gd_wrap.c index 61ed9223..384e56e1 100644 --- a/src/binocle/core/binocle_gd_wrap.c +++ b/src/binocle/core/binocle_gd_wrap.c @@ -160,9 +160,10 @@ int l_binocle_gd_render_screen(lua_State *L) { } int l_create_offscreen_shader_desc(lua_State *L) { - const char *vs = luaL_checkstring(L, 1); - const char *fs = luaL_checkstring(L, 2); - sg_shader_desc desc = binocle_gd_create_offscreen_shader_desc(vs, fs); + const char *name = luaL_checkstring(L, 1); + const char *vs = luaL_checkstring(L, 2); + const char *fs = luaL_checkstring(L, 3); + sg_shader_desc desc = binocle_gd_create_offscreen_shader_desc(name, vs, fs); l_binocle_shader_t *shader = lua_newuserdata(L, sizeof(l_binocle_shader_t)); lua_getfield(L, LUA_REGISTRYINDEX, "binocle_shader"); lua_setmetatable(L, -2);