From d09356b7032295daf9c9aae024b4d34d3e99a4ae Mon Sep 17 00:00:00 2001 From: Andre Weissflog Date: Mon, 16 Dec 2024 18:43:01 +0100 Subject: [PATCH] updated for sokol_imgui.h ImTextureID cleanup --- sapp/imgui-images-sapp.c | 78 ++++++++++++++------------------ sapp/ozz-skin-sapp.cc | 9 +--- sapp/pixelformats-sapp.c | 98 ++++++++++++++-------------------------- 3 files changed, 69 insertions(+), 116 deletions(-) diff --git a/sapp/imgui-images-sapp.c b/sapp/imgui-images-sapp.c index 80b04e19..fbafada0 100644 --- a/sapp/imgui-images-sapp.c +++ b/sapp/imgui-images-sapp.c @@ -40,11 +40,11 @@ static struct { sg_pass_action pass_action; } display; struct { - simgui_image_t img_nearest_clamp; - simgui_image_t img_linear_clamp; - simgui_image_t img_nearest_repeat; - simgui_image_t img_linear_mirror; - } ui; + sg_sampler nearest_clamp; + sg_sampler linear_clamp; + sg_sampler nearest_repeat; + sg_sampler linear_mirror; + } smp; } state; static void draw_cube(void); @@ -112,43 +112,30 @@ static void init(void) { .colors[0] = { .load_action = SG_LOADACTION_CLEAR, .clear_value = { 0.5f, 0.5f, 1.0f, 1.0f } }, }; - // sokol-imgui image-sampler-pair wrappers which combine the offscreen - // render target texture with different sampler types - state.ui.img_nearest_clamp = simgui_make_image(&(simgui_image_desc_t){ - .image = state.offscreen.color_img, - .sampler = sg_make_sampler(&(sg_sampler_desc){ - .min_filter = SG_FILTER_NEAREST, - .mag_filter = SG_FILTER_NEAREST, - .wrap_u = SG_WRAP_CLAMP_TO_EDGE, - .wrap_v = SG_WRAP_CLAMP_TO_EDGE, - }) + // create various samplers which we'll use later during UI rendering + state.smp.nearest_clamp = sg_make_sampler(&(sg_sampler_desc){ + .min_filter = SG_FILTER_NEAREST, + .mag_filter = SG_FILTER_NEAREST, + .wrap_u = SG_WRAP_CLAMP_TO_EDGE, + .wrap_v = SG_WRAP_CLAMP_TO_EDGE, }); - state.ui.img_linear_clamp = simgui_make_image(&(simgui_image_desc_t){ - .image = state.offscreen.color_img, - .sampler = sg_make_sampler(&(sg_sampler_desc){ - .min_filter = SG_FILTER_LINEAR, - .mag_filter = SG_FILTER_LINEAR, - .wrap_u = SG_WRAP_CLAMP_TO_EDGE, - .wrap_v = SG_WRAP_CLAMP_TO_EDGE, - }) + state.smp.linear_clamp = sg_make_sampler(&(sg_sampler_desc){ + .min_filter = SG_FILTER_LINEAR, + .mag_filter = SG_FILTER_LINEAR, + .wrap_u = SG_WRAP_CLAMP_TO_EDGE, + .wrap_v = SG_WRAP_CLAMP_TO_EDGE, }); - state.ui.img_nearest_repeat = simgui_make_image(&(simgui_image_desc_t){ - .image = state.offscreen.color_img, - .sampler = sg_make_sampler(&(sg_sampler_desc){ - .min_filter = SG_FILTER_NEAREST, - .mag_filter = SG_FILTER_NEAREST, - .wrap_u = SG_WRAP_REPEAT, - .wrap_v = SG_WRAP_REPEAT, - }) + state.smp.nearest_repeat = sg_make_sampler(&(sg_sampler_desc){ + .min_filter = SG_FILTER_NEAREST, + .mag_filter = SG_FILTER_NEAREST, + .wrap_u = SG_WRAP_REPEAT, + .wrap_v = SG_WRAP_REPEAT, }); - state.ui.img_linear_mirror = simgui_make_image(&(simgui_image_desc_t){ - .image = state.offscreen.color_img, - .sampler = sg_make_sampler(&(sg_sampler_desc){ - .min_filter = SG_FILTER_LINEAR, - .mag_filter = SG_FILTER_LINEAR, - .wrap_u = SG_WRAP_MIRRORED_REPEAT, - .wrap_v = SG_WRAP_MIRRORED_REPEAT, - }) + state.smp.linear_mirror = sg_make_sampler(&(sg_sampler_desc){ + .min_filter = SG_FILTER_LINEAR, + .mag_filter = SG_FILTER_LINEAR, + .wrap_u = SG_WRAP_MIRRORED_REPEAT, + .wrap_v = SG_WRAP_MIRRORED_REPEAT, }); } @@ -184,10 +171,15 @@ static void frame(void) { const ImVec2 uv0 = { 0, 0 }; const ImVec2 uv1 = { 1, 1 }; const ImVec2 uv2 = { 4, 4 }; - igImageEx(simgui_imtextureid(state.ui.img_nearest_clamp), size, uv0, uv1, white, white); igSameLineEx(0, 4); - igImageEx(simgui_imtextureid(state.ui.img_linear_clamp), size, uv0, uv1, white, white); - igImageEx(simgui_imtextureid(state.ui.img_nearest_repeat), size, uv0, uv2, white, white); igSameLineEx(0, 4); - igImageEx(simgui_imtextureid(state.ui.img_linear_mirror), size, uv0, uv2, white, white); + sg_image img = state.offscreen.color_img; + ImTextureID texid0 = simgui_imtextureid_with_sampler(img, state.smp.nearest_clamp); + ImTextureID texid1 = simgui_imtextureid_with_sampler(img, state.smp.linear_clamp); + ImTextureID texid2 = simgui_imtextureid_with_sampler(img, state.smp.nearest_repeat); + ImTextureID texid3 = simgui_imtextureid_with_sampler(img, state.smp.linear_mirror); + igImageEx(texid0, size, uv0, uv1, white, white); igSameLineEx(0, 4); + igImageEx(texid1, size, uv0, uv1, white, white); + igImageEx(texid2, size, uv0, uv2, white, white); igSameLineEx(0, 4); + igImageEx(texid3, size, uv0, uv2, white, white); } igEnd(); diff --git a/sapp/ozz-skin-sapp.cc b/sapp/ozz-skin-sapp.cc index 7bfe283a..c514661f 100644 --- a/sapp/ozz-skin-sapp.cc +++ b/sapp/ozz-skin-sapp.cc @@ -126,7 +126,6 @@ static struct { sgimgui_t sgimgui; bool joint_texture_shown; int joint_texture_scale; - simgui_image_t joint_texture; } ui; } state; @@ -240,12 +239,6 @@ static void init(void) { state.smp = sg_make_sampler(&smp_desc); state.bind.samplers[SMP_smp] = state.smp; - // create an sokol-imgui wrapper for the joint texture - simgui_image_desc_t simgui_img_desc = { }; - simgui_img_desc.image = state.joint_texture; - simgui_img_desc.sampler = state.smp; - state.ui.joint_texture = simgui_make_image(&simgui_img_desc); - // create a static instance-data buffer, in this demo, character instances // don't move around and also are not clipped against the view volume, // so we can just initialize a static instance data buffer upfront @@ -478,7 +471,7 @@ static void draw_ui(void) { ImGui::SameLine(); if (ImGui::Button("4x")) { state.ui.joint_texture_scale = 4; } ImGui::BeginChild("##frame", {0,0}, true, ImGuiWindowFlags_HorizontalScrollbar); - ImGui::Image(simgui_imtextureid(state.ui.joint_texture), + ImGui::Image(simgui_imtextureid(state.joint_texture), { (float)(state.joint_texture_width * state.ui.joint_texture_scale), (float)(state.joint_texture_height * state.ui.joint_texture_scale) }, { 0.0f, 0.0f }, { 1.0f, 1.0f }); diff --git a/sapp/pixelformats-sapp.c b/sapp/pixelformats-sapp.c index 8b6348b8..5c2c7981 100644 --- a/sapp/pixelformats-sapp.c +++ b/sapp/pixelformats-sapp.c @@ -17,11 +17,11 @@ static struct { struct { bool valid; - simgui_image_t unfiltered; - simgui_image_t filtered; - simgui_image_t render; - simgui_image_t blend; - simgui_image_t msaa_resolve; + sg_image unfiltered; + sg_image filtered; + sg_image render; + sg_image blend; + sg_image msaa_resolve; sg_pipeline cube_render_pip; sg_pipeline cube_blend_pip; sg_pipeline cube_msaa_pip; @@ -31,7 +31,6 @@ static struct { sg_attachments blend_atts; sg_attachments msaa_atts; } fmt[_SG_PIXELFORMAT_NUM]; - sg_sampler smp_nearest; sg_sampler smp_linear; sg_bindings cube_bindings; sg_bindings bg_bindings; @@ -42,7 +41,22 @@ static struct { static const char* pixelformat_string(sg_pixel_format fmt); static sg_range gen_pixels(sg_pixel_format fmt); -static simgui_image_t setup_invalid_texture(void); + +// a 'disabled' texture pattern with a cross +#define X 0xFF0000FF +#define o 0xFFCCCCCC +static const uint32_t disabled_texture_pixels[8][8] = { + { X, o, o, o, o, o, o, X }, + { o, X, o, o, o, o, X, o }, + { o, o, X, o, o, X, o, o }, + { o, o, o, X, X, o, o, o }, + { o, o, o, X, X, o, o, o }, + { o, o, X, o, o, X, o, o }, + { o, X, o, o, o, o, X, o }, + { X, o, o, o, o, o, o, X } +}; +#undef X +#undef o static void init(void) { // setup sokol-gfx @@ -56,7 +70,6 @@ static void init(void) { // setup cimgui simgui_setup(&(simgui_desc_t){ - .image_pool_size = 256, .logger.func = slog_func, }); @@ -75,7 +88,11 @@ static void init(void) { .pixel_format = SG_PIXELFORMAT_DEPTH, .sample_count = 4, }); - simgui_image_t invalid_img = setup_invalid_texture(); + sg_image invalid_img = sg_make_image(&(sg_image_desc){ + .width = 8, + .height = 8, + .data.subimage[0][0] = SG_RANGE(disabled_texture_pixels) + }); for (int i = 0; i < _SG_PIXELFORMAT_NUM; i++) { state.fmt[i].unfiltered = invalid_img; state.fmt[i].filtered = invalid_img; @@ -84,11 +101,6 @@ static void init(void) { state.fmt[i].msaa_resolve = invalid_img; } - state.smp_nearest = sg_make_sampler(&(sg_sampler_desc){ - .min_filter = SG_FILTER_NEAREST, - .mag_filter = SG_FILTER_NEAREST, - }); - state.smp_linear = sg_make_sampler(&(sg_sampler_desc){ .min_filter = SG_FILTER_LINEAR, .mag_filter = SG_FILTER_LINEAR, @@ -143,17 +155,9 @@ static void init(void) { .pixel_format = fmt, .data.subimage[0][0] = img_data, }); - if (fmt_info.sample) { - state.fmt[i].unfiltered = simgui_make_image(&(simgui_image_desc_t){ - .image = img, - .sampler = state.smp_nearest, - }); - if (fmt_info.filter) { - state.fmt[i].filtered = simgui_make_image(&(simgui_image_desc_t){ - .image = img, - .sampler = state.smp_linear, - }); - } + state.fmt[i].unfiltered = img; + if (fmt_info.filter) { + state.fmt[i].filtered = img; } } @@ -166,10 +170,7 @@ static void init(void) { .pixel_format = fmt, .sample_count = 1, }); - state.fmt[i].render = simgui_make_image(&(simgui_image_desc_t){ - .image = img, - .sampler = state.smp_nearest - }); + state.fmt[i].render = img; cube_render_pip_desc.colors[0].pixel_format = fmt; bg_render_pip_desc.colors[0].pixel_format = fmt; state.fmt[i].cube_render_pip = sg_make_pipeline(&cube_render_pip_desc); @@ -188,10 +189,7 @@ static void init(void) { .pixel_format = fmt, .sample_count = 1, }); - state.fmt[i].blend = simgui_make_image(&(simgui_image_desc_t){ - .image = img, - .sampler = state.smp_nearest, - }); + state.fmt[i].blend = img; cube_blend_pip_desc.colors[0].pixel_format = fmt; state.fmt[i].cube_blend_pip = sg_make_pipeline(&cube_blend_pip_desc); state.fmt[i].blend_atts = sg_make_attachments(&(sg_attachments_desc){ @@ -215,10 +213,7 @@ static void init(void) { .pixel_format = fmt, .sample_count = 1, }); - state.fmt[i].msaa_resolve = simgui_make_image(&(simgui_image_desc_t){ - .image = resolve_img, - .sampler = state.smp_nearest, - }); + state.fmt[i].msaa_resolve = resolve_img; cube_msaa_pip_desc.colors[0].pixel_format = fmt; bg_msaa_pip_desc.colors[0].pixel_format = fmt; state.fmt[i].cube_msaa_pip = sg_make_pipeline(&cube_msaa_pip_desc); @@ -377,7 +372,7 @@ static void frame(void) { igSameLineEx(256, 0); igImage(simgui_imtextureid(state.fmt[i].unfiltered), (ImVec2){64,64}); igSameLine(); - igImage(simgui_imtextureid(state.fmt[i].filtered), (ImVec2){64,64}); + igImage(simgui_imtextureid_with_sampler(state.fmt[i].filtered, state.smp_linear), (ImVec2){64,64}); igSameLine(); igImage(simgui_imtextureid(state.fmt[i].render), (ImVec2){64,64}); igSameLine(); @@ -427,33 +422,6 @@ sapp_desc sokol_main(int argc, char* argv[]) { }; } -/* create a texture for "feature disabled) */ -#define X 0xFF0000FF -#define o 0xFFCCCCCC -static const uint32_t disabled_texture_pixels[8][8] = { - { X, o, o, o, o, o, o, X }, - { o, X, o, o, o, o, X, o }, - { o, o, X, o, o, X, o, o }, - { o, o, o, X, X, o, o, o }, - { o, o, o, X, X, o, o, o }, - { o, o, X, o, o, X, o, o }, - { o, X, o, o, o, o, X, o }, - { X, o, o, o, o, o, o, X } -}; -#undef X -#undef o - -static simgui_image_t setup_invalid_texture(void) { - return simgui_make_image(&(simgui_image_desc_t){ - .image = sg_make_image(&(sg_image_desc){ - .width = 8, - .height = 8, - .data.subimage[0][0] = SG_RANGE(disabled_texture_pixels) - }), - .sampler = state.smp_nearest, - }); -} - /* generate checkerboard pixel values */ static uint8_t pixels[8 * 8 * 16];