diff options
| author | Andre Weissflog <floooh@gmail.com> | 2023-06-24 18:08:40 +0200 |
|---|---|---|
| committer | Andre Weissflog <floooh@gmail.com> | 2023-06-24 18:09:52 +0200 |
| commit | 159bb444872b5b43cb43bccd816e5ed8bc4db8c9 (patch) | |
| tree | bd429aa06526ae0ad1d126d4957cddb23b090367 /util | |
| parent | 3b69571d76b9108a40914b258e5dedc209be2150 (diff) | |
sokol_imgui.h, sokol_nuklear.h: remove sampler state from texture
handles.
On platforms with 32-bit pointers (e.g. WASM), the default
Dear ImGui and Nuklear texture handles are not wide enough
to store both a sokol-gfx image and sampler handle.
Diffstat (limited to 'util')
| -rw-r--r-- | util/sokol_imgui.h | 25 | ||||
| -rw-r--r-- | util/sokol_nuklear.h | 26 |
2 files changed, 17 insertions, 34 deletions
diff --git a/util/sokol_imgui.h b/util/sokol_imgui.h index 5edb834b..614a9b29 100644 --- a/util/sokol_imgui.h +++ b/util/sokol_imgui.h @@ -209,9 +209,9 @@ simgui_shutdown() --- use the following helper function to create an ImTextureID handle from - a sokol-gfx image and sampler handle: + a sokol-gfx image (note that you cannot provide your own sampler currently): - ImTextureID tex_id = simgui_imtextureid(img, smp); + ImTextureID tex_id = simgui_imtextureid(img); ...an invalid handle {SG_INVALID_ID} will be replaced with the default font texture or default sampler object @@ -340,7 +340,7 @@ typedef struct simgui_frame_desc_t { SOKOL_IMGUI_API_DECL void simgui_setup(const simgui_desc_t* desc); SOKOL_IMGUI_API_DECL void simgui_new_frame(const simgui_frame_desc_t* desc); SOKOL_IMGUI_API_DECL void simgui_render(void); -SOKOL_IMGUI_API_DECL void* simgui_imtextureid(sg_image img, sg_sampler smp); +SOKOL_IMGUI_API_DECL void* simgui_imtextureid(sg_image img); #if !defined(SOKOL_IMGUI_NO_SOKOL_APP) SOKOL_IMGUI_API_DECL bool simgui_handle_event(const sapp_event* ev); SOKOL_IMGUI_API_DECL int simgui_map_keycode(sapp_keycode keycode); // returns ImGuiKey_* @@ -1715,21 +1715,13 @@ static simgui_desc_t _simgui_desc_defaults(const simgui_desc_t* desc) { } static uint32_t _simgui_imageid_from_texid(ImTextureID tex_id) { - uint32_t img_id = (uint32_t)(((uintptr_t)tex_id) & 0xFFFFFFFF); + uint32_t img_id = (uint32_t)(uintptr_t)tex_id; if (0 == img_id) { img_id = _simgui.img.id; } return img_id; } -static uint32_t _simgui_samplerid_from_texid(ImTextureID tex_id) { - uint32_t smp_id = (uint32_t)((((uintptr_t)tex_id) >> 32) & 0xFFFFFFFF); - if (0 == smp_id) { - smp_id = _simgui.smp.id; - } - return smp_id; -} - SOKOL_API_IMPL void simgui_setup(const simgui_desc_t* desc) { SOKOL_ASSERT(desc); _simgui_clear(&_simgui, sizeof(_simgui)); @@ -1826,7 +1818,7 @@ SOKOL_API_IMPL void simgui_setup(const simgui_desc_t* desc) { smp_desc.mag_filter = SG_FILTER_LINEAR; smp_desc.mipmap_filter = SG_FILTER_NONE; _simgui.smp = sg_make_sampler(&smp_desc); - io->Fonts->TexID = simgui_imtextureid(_simgui.img, _simgui.smp); + io->Fonts->TexID = simgui_imtextureid(_simgui.img); // shader object for using the embedded shader source (or bytecode) sg_shader_desc shd_desc; @@ -1949,8 +1941,8 @@ SOKOL_API_IMPL void simgui_shutdown(void) { _simgui_free((void*)_simgui.indices.ptr); } -SOKOL_API_IMPL void* simgui_imtextureid(sg_image img, sg_sampler smp) { - return (void*) ((((uint64_t)smp.id) << 32) | (img.id)); +SOKOL_API_IMPL void* simgui_imtextureid(sg_image img) { + return (void*)(uintptr_t)img.id; } SOKOL_API_IMPL void simgui_new_frame(const simgui_frame_desc_t* desc) { @@ -2090,7 +2082,7 @@ SOKOL_API_IMPL void simgui_render(void) { bind.fs.samplers[0] = _simgui.smp; ImTextureID tex_id = io->Fonts->TexID; bind.fs.images[0].id = _simgui_imageid_from_texid(tex_id); - bind.fs.samplers[0].id = _simgui_samplerid_from_texid(tex_id); + bind.fs.samplers[0] = _simgui.smp; int vb_offset = 0; int ib_offset = 0; for (int cl_index = 0; cl_index < cmd_list_count; cl_index++) { @@ -2120,7 +2112,6 @@ SOKOL_API_IMPL void simgui_render(void) { tex_id = pcmd->TextureId; vtx_offset = pcmd->VtxOffset; bind.fs.images[0].id = _simgui_imageid_from_texid(tex_id); - bind.fs.samplers[0].id = _simgui_samplerid_from_texid(tex_id); bind.vertex_buffer_offsets[0] = vb_offset + (int)(pcmd->VtxOffset * sizeof(ImDrawVert)); sg_apply_bindings(&bind); } diff --git a/util/sokol_nuklear.h b/util/sokol_nuklear.h index 6cd14027..18444034 100644 --- a/util/sokol_nuklear.h +++ b/util/sokol_nuklear.h @@ -156,10 +156,12 @@ and hide the onscreen keyboard as required. --- NOTE: to create a Nuklear image handle, use the helper function - `nk_handle snk_nkhandle(sg_image img, sg_sampler smp)` together with + `nk_handle snk_nkhandle(sg_image img)` together with the Nuklear function nk_image_handle like this: - nk_image_handle(snk_nkhandle(img, smp)) + nk_image_handle(snk_nkhandle(img)) + + Note that it's currently not possible to provide a custom sg_sampler object. LICENSE ======= @@ -227,7 +229,7 @@ typedef struct snk_desc_t { SOKOL_NUKLEAR_API_DECL void snk_setup(const snk_desc_t* desc); SOKOL_NUKLEAR_API_DECL struct nk_context* snk_new_frame(void); SOKOL_NUKLEAR_API_DECL void snk_render(int width, int height); -SOKOL_NUKLEAR_API_DECL nk_handle snk_nkhandle(sg_image img, sg_sampler smp); +SOKOL_NUKLEAR_API_DECL nk_handle snk_nkhandle(sg_image img); #if !defined(SOKOL_NUKLEAR_NO_SOKOL_APP) SOKOL_NUKLEAR_API_DECL void snk_handle_event(const sapp_event* ev); SOKOL_NUKLEAR_API_DECL nk_flags snk_edit_string(struct nk_context *ctx, nk_flags flags, char *memory, int *len, int max, nk_plugin_filter filter); @@ -1825,28 +1827,18 @@ SOKOL_API_IMPL struct nk_context* snk_new_frame(void) { return &_snuklear.ctx; } -SOKOL_API_IMPL nk_handle snk_nkhandle(sg_image img, sg_sampler smp) { - return (nk_handle) { - .ptr = (void*)(uintptr_t)(((uint64_t)img.id << 32) | smp.id) - }; +SOKOL_API_IMPL nk_handle snk_nkhandle(sg_image img) { + return (nk_handle) { .ptr = (void*)(uintptr_t)img.id }; } _SOKOL_PRIVATE uint32_t _snk_imageid_from_nkhandle(nk_handle h) { - uint32_t img_id = (uint32_t)(((uintptr_t)h.ptr) & 0xFFFFFFFF); + uint32_t img_id = (uint32_t)(uintptr_t)h.ptr; if (0 == img_id) { img_id = _snuklear.img.id; } return img_id; } -_SOKOL_PRIVATE uint32_t _snk_samplerid_from_nkhandle(nk_handle h) { - uint32_t smp_id = (uint32_t)((((uintptr_t)h.ptr) >> 32) & 0xFFFFFFFF); - if (0 == smp_id) { - smp_id = _snuklear.smp.id; - } - return smp_id; -} - SOKOL_API_IMPL void snk_render(int width, int height) { static const struct nk_draw_vertex_layout_element vertex_layout[] = { {NK_VERTEX_POSITION, NK_FORMAT_FLOAT, NK_OFFSETOF(struct _snk_vertex_t, pos)}, @@ -1901,7 +1893,7 @@ SOKOL_API_IMPL void snk_render(int width, int height) { if (cmd->elem_count > 0) { sg_apply_bindings(&(sg_bindings){ .fs.images[0].id = _snk_imageid_from_nkhandle(cmd->texture), - .fs.samplers[0].id = _snk_samplerid_from_nkhandle(cmd->texture), + .fs.samplers[0] = _snuklear.smp, .vertex_buffers[0] = _snuklear.vbuf, .index_buffer = _snuklear.ibuf, .vertex_buffer_offsets[0] = 0, |