Skip to content

Commit 2eeacb4

Browse files
committed
sokol_imgui.h: ported to texture views (breaking change)
1 parent d184e8b commit 2eeacb4

1 file changed

Lines changed: 58 additions & 40 deletions

File tree

util/sokol_imgui.h

Lines changed: 58 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -248,37 +248,42 @@
248248
ON USER-PROVIDED IMAGES AND SAMPLERS
249249
====================================
250250
To render your own images via ImGui::Image() you need to create a Dear ImGui
251-
compatible texture handle (ImTextureID) from a sokol-gfx image handle
252-
or optionally an image handle and a compatible sampler handle.
251+
compatible texture handle (ImTextureID) from a sokol-gfx texture view handle
252+
or optionally a texture view handle and a compatible sampler handle.
253253
254254
To create a ImTextureID from a sokol-gfx image handle, call:
255255
256-
sg_image img= ...;
257-
ImTextureID imtex_id = simgui_imtextureid(img);
256+
sg_view tex_view = sg_make_view(&(sg_view_desc){ .texture_binding.image = img });
257+
ImTextureID imtex_id = simgui_imtextureid(tex_view);
258258
259259
Since no sampler is provided, such a texture handle will use a default
260260
sampler with nearest filtering and clamp-to-edge.
261261
262262
If you need to render with a different sampler, do this instead:
263263
264-
sg_image img = ...;
264+
sg_view tex_view = ...;
265265
sg_sampler smp = ...;
266-
ImTextureID imtex_id = simgui_imtextureid_with_sampler(img, smp);
266+
ImTextureID imtex_id = simgui_imtextureid_with_sampler(tex_img, smp);
267267
268268
You don't need to 'release' the ImTextureID handle, the ImTextureID
269-
bits is simply a combination of the sg_image and sg_sampler bits.
269+
bits is simply a combination of the sg_view and sg_sampler bits.
270270
271271
Once you have constructed an ImTextureID handle via simgui_imtextureid()
272272
or simgui_imtextureid_with_sampler(), it used in the ImGui::Image()
273273
call like this:
274274
275275
ImGui::Image(imtex_id, ...);
276276
277-
To extract the sg_image and sg_sampler handle from an ImTextureID:
277+
To extract the sg_view and sg_sampler handle from an ImTextureID:
278278
279-
sg_image img = simgui_image_from_imtextureid(imtex_id);
279+
sg_view tex_view = simgui_texture_view_from_imtextureid(imtex_id);
280280
sg_sampler smp = simgui_sampler_from_imtextureid(imtex_id);
281281
282+
...use the sokol-gfx function sg_query_view_image() if you need to
283+
extract the texture view's image object:
284+
285+
sg_image img = sg_query_view_image(tex_view);
286+
282287
NOTE on C bindings since 1.92.0:
283288
284289
Since Dear ImGui v1.92.0 the ImGui::Image function takes an
@@ -523,9 +528,9 @@ SOKOL_IMGUI_API_DECL void simgui_setup(const simgui_desc_t* desc);
523528
SOKOL_IMGUI_API_DECL void simgui_new_frame(const simgui_frame_desc_t* desc);
524529
SOKOL_IMGUI_API_DECL void simgui_render(void);
525530

526-
SOKOL_IMGUI_API_DECL uint64_t simgui_imtextureid(sg_image img);
527-
SOKOL_IMGUI_API_DECL uint64_t simgui_imtextureid_with_sampler(sg_image img, sg_sampler smp);
528-
SOKOL_IMGUI_API_DECL sg_image simgui_image_from_imtextureid(uint64_t imtex_id);
531+
SOKOL_IMGUI_API_DECL uint64_t simgui_imtextureid(sg_view tex_view);
532+
SOKOL_IMGUI_API_DECL uint64_t simgui_imtextureid_with_sampler(sg_view tex_view, sg_sampler smp);
533+
SOKOL_IMGUI_API_DECL sg_view simgui_texture_view_from_imtextureid(uint64_t imtex_id);
529534
SOKOL_IMGUI_API_DECL sg_sampler simgui_sampler_from_imtextureid(uint64_t imtex_id);
530535

531536
SOKOL_IMGUI_API_DECL void simgui_add_focus_event(bool focus);
@@ -2595,8 +2600,11 @@ static ImDrawData* _simgui_imgui_get_draw_data(void) {
25952600

25962601
static void _simgui_destroy_texture(ImTextureData* tex) {
25972602
SOKOL_ASSERT(tex);
2598-
const sg_image img = simgui_image_from_imtextureid(_simgui_imtexturedata_gettexid(tex));
2603+
const sg_view view = simgui_texture_view_from_imtextureid(_simgui_imtexturedata_gettexid(tex));
2604+
const sg_image img = sg_query_view_image(view);
2605+
SOKOL_ASSERT(img.id != SG_INVALID_ID);
25992606
const sg_sampler smp = simgui_sampler_from_imtextureid(_simgui_imtexturedata_gettexid(tex));
2607+
sg_destroy_view(view);
26002608
sg_destroy_image(img);
26012609
sg_destroy_sampler(smp);
26022610
_simgui_imtexturedata_settexid(tex, ImTextureID_Invalid);
@@ -2607,7 +2615,7 @@ static void _simgui_update_texture(ImTextureData* tex) {
26072615
SOKOL_ASSERT(tex);
26082616
SOKOL_ASSERT(tex->Format == ImTextureFormat_RGBA32);
26092617
if (tex->Status == ImTextureStatus_WantCreate) {
2610-
// create new sokol-gfx texture
2618+
// create new sokol-gfx image, view and sampler
26112619
SOKOL_ASSERT(tex->TexID == 0);
26122620
sg_image_desc img_desc;
26132621
_simgui_clear(&img_desc, sizeof(img_desc));
@@ -2618,6 +2626,12 @@ static void _simgui_update_texture(ImTextureData* tex) {
26182626
img_desc.label = "sokol-imgui-texture";
26192627
sg_image img = sg_make_image(&img_desc);
26202628

2629+
sg_view_desc view_desc;
2630+
_simgui_clear(&view_desc, sizeof(view_desc));
2631+
view_desc.texture_binding.image = img;
2632+
view_desc.label = "sokol-imgui-texture-view";
2633+
sg_view view = sg_make_view(&view_desc);
2634+
26212635
sg_sampler_desc smp_desc;
26222636
_simgui_clear(&smp_desc, sizeof(smp_desc));
26232637
smp_desc.wrap_u = SG_WRAP_CLAMP_TO_EDGE;
@@ -2627,11 +2641,13 @@ static void _simgui_update_texture(ImTextureData* tex) {
26272641
smp_desc.label = "sokol-imgui-sampler";
26282642
sg_sampler smp = sg_make_sampler(&smp_desc);
26292643

2630-
_simgui_imtexturedata_settexid(tex, simgui_imtextureid_with_sampler(img, smp));
2644+
_simgui_imtexturedata_settexid(tex, simgui_imtextureid_with_sampler(view, smp));
26312645
}
26322646
if ((tex->Status == ImTextureStatus_WantCreate) || (tex->Status == ImTextureStatus_WantUpdates)) {
26332647
SOKOL_ASSERT(tex->TexID != 0);
2634-
const sg_image img = simgui_image_from_imtextureid(_simgui_imtexturedata_gettexid(tex));
2648+
const sg_view view = simgui_texture_view_from_imtextureid(_simgui_imtexturedata_gettexid(tex));
2649+
const sg_image img = sg_query_view_image(view);
2650+
SOKOL_ASSERT(img.id != SG_INVALID_ID);
26352651
sg_image_data img_data;
26362652
_simgui_clear(&img_data, sizeof(img_data));
26372653
img_data.subimage[0][0].ptr = _simgui_imtexturedata_getpixels(tex);
@@ -2715,21 +2731,21 @@ SOKOL_API_IMPL void simgui_setup(const simgui_desc_t* desc) {
27152731
shd_desc.uniform_blocks[0].glsl_uniforms[0].glsl_name = "vs_params";
27162732
shd_desc.uniform_blocks[0].glsl_uniforms[0].type = SG_UNIFORMTYPE_FLOAT4;
27172733
shd_desc.uniform_blocks[0].glsl_uniforms[0].array_count = 1;
2718-
shd_desc.images[0].stage = SG_SHADERSTAGE_FRAGMENT;
2719-
shd_desc.images[0].image_type = SG_IMAGETYPE_2D;
2720-
shd_desc.images[0].sample_type = SG_IMAGESAMPLETYPE_FLOAT;
2721-
shd_desc.images[0].hlsl_register_t_n = 0;
2722-
shd_desc.images[0].msl_texture_n = 0;
2723-
shd_desc.images[0].wgsl_group1_binding_n = 64;
2734+
shd_desc.textures[0].stage = SG_SHADERSTAGE_FRAGMENT;
2735+
shd_desc.textures[0].image_type = SG_IMAGETYPE_2D;
2736+
shd_desc.textures[0].sample_type = SG_IMAGESAMPLETYPE_FLOAT;
2737+
shd_desc.textures[0].hlsl_register_t_n = 0;
2738+
shd_desc.textures[0].msl_texture_n = 0;
2739+
shd_desc.textures[0].wgsl_group1_binding_n = 64;
27242740
shd_desc.samplers[0].stage = SG_SHADERSTAGE_FRAGMENT;
27252741
shd_desc.samplers[0].sampler_type = SG_SAMPLERTYPE_FILTERING;
27262742
shd_desc.samplers[0].hlsl_register_s_n = 0;
27272743
shd_desc.samplers[0].msl_sampler_n = 0;
27282744
shd_desc.samplers[0].wgsl_group1_binding_n = 80;
2729-
shd_desc.image_sampler_pairs[0].stage = SG_SHADERSTAGE_FRAGMENT;
2730-
shd_desc.image_sampler_pairs[0].image_slot = 0;
2731-
shd_desc.image_sampler_pairs[0].sampler_slot = 0;
2732-
shd_desc.image_sampler_pairs[0].glsl_name = "tex_smp";
2745+
shd_desc.texture_sampler_pairs[0].stage = SG_SHADERSTAGE_FRAGMENT;
2746+
shd_desc.texture_sampler_pairs[0].texture_slot = 0;
2747+
shd_desc.texture_sampler_pairs[0].sampler_slot = 0;
2748+
shd_desc.texture_sampler_pairs[0].glsl_name = "tex_smp";
27332749
shd_desc.label = "sokol-imgui-shader";
27342750
#if defined(SOKOL_GLCORE)
27352751
shd_desc.vertex_func.source = (const char*)_simgui_vs_source_glsl410;
@@ -2801,8 +2817,8 @@ SOKOL_API_IMPL void simgui_setup(const simgui_desc_t* desc) {
28012817
pip_desc.label = "sokol-imgui-pipeline";
28022818
_simgui.def_pip = sg_make_pipeline(&pip_desc);
28032819

2804-
// create a unfilterable/nonfiltering variants of the shader and pipeline
2805-
shd_desc.images[0].sample_type = SG_IMAGESAMPLETYPE_UNFILTERABLE_FLOAT;
2820+
// create unfilterable/nonfiltering variants of the shader and pipeline
2821+
shd_desc.textures[0].sample_type = SG_IMAGESAMPLETYPE_UNFILTERABLE_FLOAT;
28062822
shd_desc.samplers[0].sampler_type = SG_SAMPLERTYPE_NONFILTERING;
28072823
shd_desc.label = "sokol-imgui-shader-unfilterable";
28082824
_simgui.shd_unfilterable = sg_make_shader(&shd_desc);
@@ -2868,19 +2884,19 @@ SOKOL_API_IMPL void simgui_shutdown(void) {
28682884
_simgui.init_cookie = 0;
28692885
}
28702886

2871-
SOKOL_API_IMPL uint64_t simgui_imtextureid_with_sampler(sg_image img, sg_sampler smp) {
2872-
uint32_t img_id = img.id;
2887+
SOKOL_API_IMPL uint64_t simgui_imtextureid_with_sampler(sg_view tex_view, sg_sampler smp) {
2888+
uint32_t view_id = tex_view.id;
28732889
uint32_t smp_id = smp.id;
2874-
return (((uint64_t)smp_id)<<32) | img_id;
2890+
return (((uint64_t)smp_id)<<32) | view_id;
28752891
}
28762892

2877-
SOKOL_API_IMPL uint64_t simgui_imtextureid(sg_image img) {
2878-
return simgui_imtextureid_with_sampler(img, _simgui.def_smp);
2893+
SOKOL_API_IMPL uint64_t simgui_imtextureid(sg_view tex_view) {
2894+
return simgui_imtextureid_with_sampler(tex_view, _simgui.def_smp);
28792895
}
28802896

2881-
SOKOL_API_IMPL sg_image simgui_image_from_imtextureid(uint64_t imtex_id) {
2882-
sg_image img = { (uint32_t)imtex_id };
2883-
return img;
2897+
SOKOL_API_IMPL sg_view simgui_texture_view_from_imtextureid(uint64_t imtex_id) {
2898+
sg_view view = { (uint32_t)imtex_id };
2899+
return view;
28842900
}
28852901

28862902
SOKOL_API_IMPL sg_sampler simgui_sampler_from_imtextureid(uint64_t imtex_id) {
@@ -2926,10 +2942,12 @@ SOKOL_API_IMPL void simgui_new_frame(const simgui_frame_desc_t* desc) {
29262942
_simgui_imgui_newframe();
29272943
}
29282944

2929-
static sg_pipeline _simgui_bind_image_sampler(sg_bindings* bindings, ImTextureID imtex_id) {
2930-
bindings->images[0] = simgui_image_from_imtextureid(imtex_id);
2945+
static sg_pipeline _simgui_bind_texture_sampler(sg_bindings* bindings, ImTextureID imtex_id) {
2946+
const sg_view tex_view = simgui_texture_view_from_imtextureid(imtex_id);
2947+
const sg_image img = sg_query_view_image(tex_view);
2948+
bindings->textures[0] = tex_view;
29312949
bindings->samplers[0] = simgui_sampler_from_imtextureid(imtex_id);
2932-
if (sg_query_pixelformat(sg_query_image_pixelformat(bindings->images[0])).filter) {
2950+
if (sg_query_pixelformat(sg_query_image_pixelformat(img)).filter) {
29332951
return _simgui.def_pip;
29342952
} else {
29352953
return _simgui.pip_unfilterable;
@@ -3060,7 +3078,7 @@ SOKOL_API_IMPL void simgui_render(void) {
30603078
if ((tex_id != cmd_tex_id) || (vtx_offset != pcmd->VtxOffset)) {
30613079
tex_id = cmd_tex_id;
30623080
vtx_offset = pcmd->VtxOffset;
3063-
sg_pipeline pip = _simgui_bind_image_sampler(&bind, tex_id);
3081+
sg_pipeline pip = _simgui_bind_texture_sampler(&bind, tex_id);
30643082
sg_apply_pipeline(pip);
30653083
sg_apply_uniforms(0, SG_RANGE_REF(vs_params));
30663084
bind.vertex_buffer_offsets[0] = vb_offset + (int)(pcmd->VtxOffset * sizeof(ImDrawVert));

0 commit comments

Comments
 (0)