From 8d11102006f6e5cb886c54c34354b7df43c1993e Mon Sep 17 00:00:00 2001 From: jakubtomsu <66876057+jakubtomsu@users.noreply.github.com> Date: Mon, 15 Apr 2024 12:31:10 +0200 Subject: [PATCH 1/4] [windows] Add /Z7 arg to include debug info --- sokol/build_clibs_windows.cmd | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sokol/build_clibs_windows.cmd b/sokol/build_clibs_windows.cmd index 72b0992..2d33813 100644 --- a/sokol/build_clibs_windows.cmd +++ b/sokol/build_clibs_windows.cmd @@ -4,7 +4,7 @@ set sources=log app gfx glue time audio debugtext shape gl REM D3D11 Debug for %%s in (%sources%) do ( - cl /c /D_DEBUG /DIMPL /DSOKOL_D3D11 c\sokol_%%s.c + cl /c /D_DEBUG /DIMPL /DSOKOL_D3D11 c\sokol_%%s.c /Z7 lib /OUT:%%s\sokol_%%s_windows_x64_d3d11_debug.lib sokol_%%s.obj del sokol_%%s.obj ) @@ -18,7 +18,7 @@ for %%s in (%sources%) do ( REM GL Debug for %%s in (%sources%) do ( - cl /c /D_DEBUG /DIMPL /DSOKOL_GLCORE33 c\sokol_%%s.c + cl /c /D_DEBUG /DIMPL /DSOKOL_GLCORE33 c\sokol_%%s.c /Z7 lib /OUT:%%s\sokol_%%s_windows_x64_gl_debug.lib sokol_%%s.obj del sokol_%%s.obj ) From e2658085b0a953f8e8914f96ff8e38464812dc08 Mon Sep 17 00:00:00 2001 From: jakubtomsu <66876057+jakubtomsu@users.noreply.github.com> Date: Mon, 15 Apr 2024 12:57:02 +0200 Subject: [PATCH 2/4] glue, logger, WIP allocator --- sokol/utils/allocator.odin | 25 ++++++++++++++++ sokol/utils/glue.odin | 37 +++++++++++++++++++++++ sokol/utils/logger.odin | 61 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 123 insertions(+) create mode 100644 sokol/utils/allocator.odin create mode 100644 sokol/utils/glue.odin create mode 100644 sokol/utils/logger.odin diff --git a/sokol/utils/allocator.odin b/sokol/utils/allocator.odin new file mode 100644 index 0000000..9454677 --- /dev/null +++ b/sokol/utils/allocator.odin @@ -0,0 +1,25 @@ +package sokol_utils + +Allocator :: struct { + +} + +sapp_allocator :: proc() -> sapp.Allocator { + return { + alloc_fn = allocator_alloc_func, + free_fn = allocator_free_func, + user_data = nil, + } +} + +sg_allocator :: proc() { + +} + +allocator_alloc_func :: proc "c" (size: u64, user_data: rawptr) -> rawptr { + +} + +allocator_free_func :: proc "c" (ptr: rawptr, user_data: rawptr) -> rawptr { + +} \ No newline at end of file diff --git a/sokol/utils/glue.odin b/sokol/utils/glue.odin new file mode 100644 index 0000000..9345ffd --- /dev/null +++ b/sokol/utils/glue.odin @@ -0,0 +1,37 @@ +package sokol_utils + +// Alternative native odin implementation of sokol_glue.h, in case you want to minimize C dependencies +// (since sokol_glue is only a few lines of code) + +import gfx "../gfx" +import sapp "../app" + +sglue_environment :: proc() -> (env: sg.Environment) { + env.defaults.color_format = cast(sg.Pixel_Format)sapp.color_format() + env.defaults.depth_format = cast(sg.Pixel_Format)sapp.depth_format() + env.defaults.sample_count = sapp.sample_count() + env.metal.device = sapp.metal_get_device() + env.d3d11.device = sapp.d3d11_get_device() + env.d3d11.device_context = sapp.d3d11_get_device_context() + env.wgpu.device = sapp.wgpu_get_device() + return env +} + +sglue_swapchain :: proc() -> (swapchain: sg.Swapchain) { + swapchain.width = sapp.width() + swapchain.height = sapp.height() + swapchain.sample_count = sapp.sample_count() + swapchain.color_format = cast(sg.Pixel_Format)sapp.color_format() + swapchain.depth_format = cast(sg.Pixel_Format)sapp.depth_format() + swapchain.metal.current_drawable = sapp.metal_get_current_drawable() + swapchain.metal.depth_stencil_texture = sapp.metal_get_depth_stencil_texture() + swapchain.metal.msaa_color_texture = sapp.metal_get_msaa_color_texture() + swapchain.d3d11.render_view = sapp.d3d11_get_render_view() + swapchain.d3d11.resolve_view = sapp.d3d11_get_resolve_view() + swapchain.d3d11.depth_stencil_view = sapp.d3d11_get_depth_stencil_view() + swapchain.wgpu.render_view = sapp.wgpu_get_render_view() + swapchain.wgpu.resolve_view = sapp.wgpu_get_resolve_view() + swapchain.wgpu.depth_stencil_view = sapp.wgpu_get_depth_stencil_view() + swapchain.gl.framebuffer = sapp.gl_get_framebuffer() + return swapchain +} diff --git a/sokol/utils/logger.odin b/sokol/utils/logger.odin new file mode 100644 index 0000000..f12228f --- /dev/null +++ b/sokol/utils/logger.odin @@ -0,0 +1,61 @@ +package sokol_utils + +// Pass sokol logs into native odin logging system + +import sapp "../app" +import gfx "../gfx" +import "base:runtime" +import "core:log" + +// context_ptr: a pointer to a context which persists during the lifetime of sokol_app. +sapp_logger :: proc(context_ptr: ^runtime.Context) -> sapp.Logger { + return { + func = log_func, + user_data = cast(rawptr)context_ptr, + } +} + + +// context_ptr: a pointer to a context which persists during the lifetime of sokol_gfx. +sg_logger :: proc(context_ptr: ^runtime.Context) -> sg.Logger { + return { + func = log_func, + user_data = cast(rawptr)context_ptr, + } +} + +log_func :: proc "c" ( + tag: cstring, + log_level: u32, + log_item: u32, + message: cstring, + line_nr: u32, + filename: cstring, + user_data: rawptr, +) { + context = (cast(^runtime.Context)user_data)^ + + level: log.Level + switch log_level { + case 0: + // Panic + level = .Error + case 1: + level = .Error + case 2: + level = .Warning + case: + level = .Info + } + + loc := runtime.Source_Code_Location { + file_path = string(filename), + line = i32(line_nr), + } + + if log_level == 0 { + log.panicf("Sokol Panic: (%i) %s: %s", log_item, tag, message, location = loc) + } else { + log.logf(level, "(%i) %s: %s", log_item, tag, message, location = loc) + } +} From a89f9d375297077df86e505dc0e6b228accf7139 Mon Sep 17 00:00:00 2001 From: jakubtomsu <66876057+jakubtomsu@users.noreply.github.com> Date: Mon, 15 Apr 2024 17:05:51 +0200 Subject: [PATCH 3/4] Improvements --- sokol/helpers/allocator.odin | 37 +++++++++ sokol/helpers/glue.odin | 37 +++++++++ sokol/{utils => helpers}/logger.odin | 120 +++++++++++++-------------- sokol/utils/allocator.odin | 25 ------ sokol/utils/glue.odin | 37 --------- 5 files changed, 133 insertions(+), 123 deletions(-) create mode 100644 sokol/helpers/allocator.odin create mode 100644 sokol/helpers/glue.odin rename sokol/{utils => helpers}/logger.odin (51%) delete mode 100644 sokol/utils/allocator.odin delete mode 100644 sokol/utils/glue.odin diff --git a/sokol/helpers/allocator.odin b/sokol/helpers/allocator.odin new file mode 100644 index 0000000..5a9995d --- /dev/null +++ b/sokol/helpers/allocator.odin @@ -0,0 +1,37 @@ +package sokol_helpers + +// Use native odin allocators in sokol allocator interface + +import sapp "../app" +import sg "../gfx" +import "base:runtime" + +Allocator :: struct { + alloc_fn: proc "c" (size: u64, user_data: rawptr) -> rawptr, + free_fn: proc "c" (ptr: rawptr, user_data: rawptr), + user_data: rawptr, +} + +// context_ptr: a pointer to a context which persists during the lifetime of the program. +// Note: you can transmute() this into a logger for any specific sokol library. +allocator :: proc(context_ptr: ^runtime.Context) -> Allocator { + return { + alloc_fn = allocator_alloc_proc, + free_fn = allocator_free_proc, + user_data = cast(rawptr)context_ptr, + } +} + +allocator_alloc_proc :: proc "c" (size: u64, user_data: rawptr) -> rawptr { + context = (cast(^runtime.Context)user_data)^ + bytes, err := runtime.mem_alloc(size = int(size)) + if err != nil { + return nil + } + return raw_data(bytes) +} + +allocator_free_proc :: proc "c" (ptr: rawptr, user_data: rawptr) { + context = (cast(^runtime.Context)user_data)^ + runtime.mem_free(ptr) +} diff --git a/sokol/helpers/glue.odin b/sokol/helpers/glue.odin new file mode 100644 index 0000000..8091ec9 --- /dev/null +++ b/sokol/helpers/glue.odin @@ -0,0 +1,37 @@ +package sokol_helpers + +// Alternative native odin implementation of sokol_glue.h, in case you want to minimize C dependencies +// (since sokol_glue is only a few lines of code) + +import sapp "../app" +import sg "../gfx" + +sglue_environment :: proc() -> (env: sg.Environment) { + env.defaults.color_format = cast(sg.Pixel_Format)sapp.color_format() + env.defaults.depth_format = cast(sg.Pixel_Format)sapp.depth_format() + env.defaults.sample_count = sapp.sample_count() + env.metal.device = sapp.metal_get_device() + env.d3d11.device = sapp.d3d11_get_device() + env.d3d11.device_context = sapp.d3d11_get_device_context() + env.wgpu.device = sapp.wgpu_get_device() + return env +} + +sglue_swapchain :: proc() -> (swapchain: sg.Swapchain) { + swapchain.width = sapp.width() + swapchain.height = sapp.height() + swapchain.sample_count = sapp.sample_count() + swapchain.color_format = cast(sg.Pixel_Format)sapp.color_format() + swapchain.depth_format = cast(sg.Pixel_Format)sapp.depth_format() + swapchain.metal.current_drawable = sapp.metal_get_current_drawable() + swapchain.metal.depth_stencil_texture = sapp.metal_get_depth_stencil_texture() + swapchain.metal.msaa_color_texture = sapp.metal_get_msaa_color_texture() + swapchain.d3d11.render_view = sapp.d3d11_get_render_view() + swapchain.d3d11.resolve_view = sapp.d3d11_get_resolve_view() + swapchain.d3d11.depth_stencil_view = sapp.d3d11_get_depth_stencil_view() + swapchain.wgpu.render_view = sapp.wgpu_get_render_view() + swapchain.wgpu.resolve_view = sapp.wgpu_get_resolve_view() + swapchain.wgpu.depth_stencil_view = sapp.wgpu_get_depth_stencil_view() + swapchain.gl.framebuffer = sapp.gl_get_framebuffer() + return swapchain +} diff --git a/sokol/utils/logger.odin b/sokol/helpers/logger.odin similarity index 51% rename from sokol/utils/logger.odin rename to sokol/helpers/logger.odin index f12228f..631de03 100644 --- a/sokol/utils/logger.odin +++ b/sokol/helpers/logger.odin @@ -1,61 +1,59 @@ -package sokol_utils - -// Pass sokol logs into native odin logging system - -import sapp "../app" -import gfx "../gfx" -import "base:runtime" -import "core:log" - -// context_ptr: a pointer to a context which persists during the lifetime of sokol_app. -sapp_logger :: proc(context_ptr: ^runtime.Context) -> sapp.Logger { - return { - func = log_func, - user_data = cast(rawptr)context_ptr, - } -} - - -// context_ptr: a pointer to a context which persists during the lifetime of sokol_gfx. -sg_logger :: proc(context_ptr: ^runtime.Context) -> sg.Logger { - return { - func = log_func, - user_data = cast(rawptr)context_ptr, - } -} - -log_func :: proc "c" ( - tag: cstring, - log_level: u32, - log_item: u32, - message: cstring, - line_nr: u32, - filename: cstring, - user_data: rawptr, -) { - context = (cast(^runtime.Context)user_data)^ - - level: log.Level - switch log_level { - case 0: - // Panic - level = .Error - case 1: - level = .Error - case 2: - level = .Warning - case: - level = .Info - } - - loc := runtime.Source_Code_Location { - file_path = string(filename), - line = i32(line_nr), - } - - if log_level == 0 { - log.panicf("Sokol Panic: (%i) %s: %s", log_item, tag, message, location = loc) - } else { - log.logf(level, "(%i) %s: %s", log_item, tag, message, location = loc) - } -} +package sokol_helpers + +// Pass sokol logs into native odin logging system + +import sapp "../app" +import sg "../gfx" +import "base:runtime" +import "core:log" + +Logger :: struct { + func: proc "c" ( + tag: cstring, + log_level: u32, + log_item: u32, + message: cstring, + line_nr: u32, + filename: cstring, + user_data: rawptr, + ), + user_data: rawptr, +} + +// context_ptr: a pointer to a context which persists during the lifetime of the program. +// Note: you can transmute() this into a logger for any specific sokol library. +logger :: proc "contextless" (context_ptr: ^runtime.Context) -> Logger { + return {func = logger_proc, user_data = cast(rawptr)context_ptr} +} + +logger_proc :: proc "c" ( + tag: cstring, + log_level: u32, + log_item: u32, + message: cstring, + line_nr: u32, + filename: cstring, + user_data: rawptr, +) { + context = (cast(^runtime.Context)user_data)^ + + loc := runtime.Source_Code_Location { + file_path = string(filename), + line = i32(line_nr), + } + + level: log.Level + switch log_level { + case 0: + log.panicf("Sokol Panic: (%i) %s: %s", log_item, tag, message, location = loc) + + case 1: + level = .Error + case 2: + level = .Warning + case: + level = .Info + } + + log.logf(level, "(%i) %s: %s", log_item, tag, message, location = loc) +} diff --git a/sokol/utils/allocator.odin b/sokol/utils/allocator.odin deleted file mode 100644 index 9454677..0000000 --- a/sokol/utils/allocator.odin +++ /dev/null @@ -1,25 +0,0 @@ -package sokol_utils - -Allocator :: struct { - -} - -sapp_allocator :: proc() -> sapp.Allocator { - return { - alloc_fn = allocator_alloc_func, - free_fn = allocator_free_func, - user_data = nil, - } -} - -sg_allocator :: proc() { - -} - -allocator_alloc_func :: proc "c" (size: u64, user_data: rawptr) -> rawptr { - -} - -allocator_free_func :: proc "c" (ptr: rawptr, user_data: rawptr) -> rawptr { - -} \ No newline at end of file diff --git a/sokol/utils/glue.odin b/sokol/utils/glue.odin deleted file mode 100644 index 9345ffd..0000000 --- a/sokol/utils/glue.odin +++ /dev/null @@ -1,37 +0,0 @@ -package sokol_utils - -// Alternative native odin implementation of sokol_glue.h, in case you want to minimize C dependencies -// (since sokol_glue is only a few lines of code) - -import gfx "../gfx" -import sapp "../app" - -sglue_environment :: proc() -> (env: sg.Environment) { - env.defaults.color_format = cast(sg.Pixel_Format)sapp.color_format() - env.defaults.depth_format = cast(sg.Pixel_Format)sapp.depth_format() - env.defaults.sample_count = sapp.sample_count() - env.metal.device = sapp.metal_get_device() - env.d3d11.device = sapp.d3d11_get_device() - env.d3d11.device_context = sapp.d3d11_get_device_context() - env.wgpu.device = sapp.wgpu_get_device() - return env -} - -sglue_swapchain :: proc() -> (swapchain: sg.Swapchain) { - swapchain.width = sapp.width() - swapchain.height = sapp.height() - swapchain.sample_count = sapp.sample_count() - swapchain.color_format = cast(sg.Pixel_Format)sapp.color_format() - swapchain.depth_format = cast(sg.Pixel_Format)sapp.depth_format() - swapchain.metal.current_drawable = sapp.metal_get_current_drawable() - swapchain.metal.depth_stencil_texture = sapp.metal_get_depth_stencil_texture() - swapchain.metal.msaa_color_texture = sapp.metal_get_msaa_color_texture() - swapchain.d3d11.render_view = sapp.d3d11_get_render_view() - swapchain.d3d11.resolve_view = sapp.d3d11_get_resolve_view() - swapchain.d3d11.depth_stencil_view = sapp.d3d11_get_depth_stencil_view() - swapchain.wgpu.render_view = sapp.wgpu_get_render_view() - swapchain.wgpu.resolve_view = sapp.wgpu_get_resolve_view() - swapchain.wgpu.depth_stencil_view = sapp.wgpu_get_depth_stencil_view() - swapchain.gl.framebuffer = sapp.gl_get_framebuffer() - return swapchain -} From 228e90e2845fb1734194c90b837c3de7ecbec0e1 Mon Sep 17 00:00:00 2001 From: jakubtomsu <66876057+jakubtomsu@users.noreply.github.com> Date: Mon, 15 Apr 2024 17:22:12 +0200 Subject: [PATCH 4/4] glue spaces, naming --- sokol/helpers/glue.odin | 52 ++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/sokol/helpers/glue.odin b/sokol/helpers/glue.odin index 8091ec9..5215979 100644 --- a/sokol/helpers/glue.odin +++ b/sokol/helpers/glue.odin @@ -6,32 +6,32 @@ package sokol_helpers import sapp "../app" import sg "../gfx" -sglue_environment :: proc() -> (env: sg.Environment) { - env.defaults.color_format = cast(sg.Pixel_Format)sapp.color_format() - env.defaults.depth_format = cast(sg.Pixel_Format)sapp.depth_format() - env.defaults.sample_count = sapp.sample_count() - env.metal.device = sapp.metal_get_device() - env.d3d11.device = sapp.d3d11_get_device() - env.d3d11.device_context = sapp.d3d11_get_device_context() - env.wgpu.device = sapp.wgpu_get_device() - return env +glue_environment :: proc() -> (env: sg.Environment) { + env.defaults.color_format = cast(sg.Pixel_Format)sapp.color_format() + env.defaults.depth_format = cast(sg.Pixel_Format)sapp.depth_format() + env.defaults.sample_count = sapp.sample_count() + env.metal.device = sapp.metal_get_device() + env.d3d11.device = sapp.d3d11_get_device() + env.d3d11.device_context = sapp.d3d11_get_device_context() + env.wgpu.device = sapp.wgpu_get_device() + return env } -sglue_swapchain :: proc() -> (swapchain: sg.Swapchain) { - swapchain.width = sapp.width() - swapchain.height = sapp.height() - swapchain.sample_count = sapp.sample_count() - swapchain.color_format = cast(sg.Pixel_Format)sapp.color_format() - swapchain.depth_format = cast(sg.Pixel_Format)sapp.depth_format() - swapchain.metal.current_drawable = sapp.metal_get_current_drawable() - swapchain.metal.depth_stencil_texture = sapp.metal_get_depth_stencil_texture() - swapchain.metal.msaa_color_texture = sapp.metal_get_msaa_color_texture() - swapchain.d3d11.render_view = sapp.d3d11_get_render_view() - swapchain.d3d11.resolve_view = sapp.d3d11_get_resolve_view() - swapchain.d3d11.depth_stencil_view = sapp.d3d11_get_depth_stencil_view() - swapchain.wgpu.render_view = sapp.wgpu_get_render_view() - swapchain.wgpu.resolve_view = sapp.wgpu_get_resolve_view() - swapchain.wgpu.depth_stencil_view = sapp.wgpu_get_depth_stencil_view() - swapchain.gl.framebuffer = sapp.gl_get_framebuffer() - return swapchain +glue_swapchain :: proc() -> (swapchain: sg.Swapchain) { + swapchain.width = sapp.width() + swapchain.height = sapp.height() + swapchain.sample_count = sapp.sample_count() + swapchain.color_format = cast(sg.Pixel_Format)sapp.color_format() + swapchain.depth_format = cast(sg.Pixel_Format)sapp.depth_format() + swapchain.metal.current_drawable = sapp.metal_get_current_drawable() + swapchain.metal.depth_stencil_texture = sapp.metal_get_depth_stencil_texture() + swapchain.metal.msaa_color_texture = sapp.metal_get_msaa_color_texture() + swapchain.d3d11.render_view = sapp.d3d11_get_render_view() + swapchain.d3d11.resolve_view = sapp.d3d11_get_resolve_view() + swapchain.d3d11.depth_stencil_view = sapp.d3d11_get_depth_stencil_view() + swapchain.wgpu.render_view = sapp.wgpu_get_render_view() + swapchain.wgpu.resolve_view = sapp.wgpu_get_resolve_view() + swapchain.wgpu.depth_stencil_view = sapp.wgpu_get_depth_stencil_view() + swapchain.gl.framebuffer = sapp.gl_get_framebuffer() + return swapchain }