Skip to content

Commit

Permalink
Merge pull request #861 from floooh/simgui-image-sampler
Browse files Browse the repository at this point in the history
sokol_imgui.h: proper support for user images with samplers.
  • Loading branch information
floooh authored Jul 23, 2023
2 parents 58936be + 591b3c5 commit e606e1d
Show file tree
Hide file tree
Showing 4 changed files with 611 additions and 45 deletions.
31 changes: 31 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,36 @@
## Updates

#### 23-Jul-2023

**sokol_imgui.h**: Add proper support for injecting user-provided sokol-gfx
images and samplers into Dear ImGui UIs. With the introduction of separate
sampler objects in sokol_gfx.h there's a temporary feature regression in
sokol_imgui.h and sokol_nuklear.h in that user provided images had to use a
shared sampler that's hardwired into the respective headers. This update fixes
this problem for sokol_imgui.h, with a similar fix for sokol_nuklear.h coming
up next.

The sokol_imgui.h changes in detail are:

- a new object type `simgui_image_t` which wraps a sokol-gfx image and sampler
object under a common handle
- two new function `simgui_make_image()` and `simgui_destroy_image()` to
create and destroy such a new `simgui_image_t` object.
- the existing function `simgui_imtextureid()` has been changed to take
an `simgui_image_t`
- sokol_imgui.h now also uses the same error-handling and logging callback
as the other sokol headers (this was needed because creating an `simgui_image_t`
object may fail because the object pool is exhausted) - don't forget
to provide a logging callback (for instance via sokol_log.h), otherwise
sokol_imgui.h will be entirely silent in case of errors.

Please also read the new documentation section `ON USER-PROVIDED IMAGES AND SAMPLERS`
in sokol_imgui.h, and also check out the new sample:

https://floooh.github.io/sokol-html5/imgui-images-sapp.html

Associated PR: https://github.com/floooh/sokol/pull/861

#### 16-Jul-2023

**BREAKING CHANGES**
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Simple
[STB-style](https://github.com/nothings/stb/blob/master/docs/stb_howto.txt)
cross-platform libraries for C and C++, written in C.

[**See what's new**](https://github.com/floooh/sokol/blob/master/CHANGELOG.md) (**17-Jul-2023** BREAKING CHANGES: sokol_gfx.h now has separate sampler objects!)
[**See what's new**](https://github.com/floooh/sokol/blob/master/CHANGELOG.md) (**23-Jul-2023** proper image/sampler pair support in sokol_imgui.h)

[![Build](/../../actions/workflows/main.yml/badge.svg)](/../../actions/workflows/main.yml) [![Bindings](/../../actions/workflows/gen_bindings.yml/badge.svg)](/../../actions/workflows/gen_bindings.yml) [![build](https://github.com/floooh/sokol-zig/actions/workflows/main.yml/badge.svg)](https://github.com/floooh/sokol-zig/actions/workflows/main.yml) [![build](https://github.com/floooh/sokol-nim/actions/workflows/main.yml/badge.svg)](https://github.com/floooh/sokol-nim/actions/workflows/main.yml) [![Odin](https://github.com/floooh/sokol-odin/actions/workflows/main.yml/badge.svg)](https://github.com/floooh/sokol-odin/actions/workflows/main.yml)[![Rust](https://github.com/floooh/sokol-rust/actions/workflows/main.yml/badge.svg)](https://github.com/floooh/sokol-rust/actions/workflows/main.yml)

Expand Down
9 changes: 8 additions & 1 deletion util/sokol_gfx_imgui.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ typedef struct sg_imgui_image_t {
float ui_scale;
sg_imgui_str_t label;
sg_image_desc desc;
simgui_image_t simgui_img;
} sg_imgui_image_t;

typedef struct sg_imgui_sampler_t {
Expand Down Expand Up @@ -1517,12 +1518,18 @@ _SOKOL_PRIVATE void _sg_imgui_image_created(sg_imgui_t* ctx, sg_image res_id, in
img->desc = *desc;
img->ui_scale = 1.0f;
img->label = _sg_imgui_make_str(desc->label);
simgui_image_desc_t simgui_img_desc;
_sg_imgui_clear(&simgui_img_desc, sizeof(simgui_img_desc));
simgui_img_desc.image = res_id;
// keep sampler at default, which will use sokol_imgui.h's default nearest-filtering sampler
img->simgui_img = simgui_make_image(&simgui_img_desc);
}

_SOKOL_PRIVATE void _sg_imgui_image_destroyed(sg_imgui_t* ctx, int slot_index) {
SOKOL_ASSERT((slot_index > 0) && (slot_index < ctx->images.num_slots));
sg_imgui_image_t* img = &ctx->images.slots[slot_index];
img->res_id.id = SG_INVALID_ID;
simgui_destroy_image(img->simgui_img);
}

_SOKOL_PRIVATE void _sg_imgui_sampler_created(sg_imgui_t* ctx, sg_sampler res_id, int slot_index, const sg_sampler_desc* desc) {
Expand Down Expand Up @@ -3327,7 +3334,7 @@ _SOKOL_PRIVATE void _sg_imgui_draw_embedded_image(sg_imgui_t* ctx, sg_image img,
igSliderFloat("Scale", scale, 0.125f, 8.0f, "%.3f", ImGuiSliderFlags_Logarithmic);
float w = (float)img_ui->desc.width * (*scale);
float h = (float)img_ui->desc.height * (*scale);
igImage((ImTextureID)(intptr_t)img.id, IMVEC2(w, h), IMVEC2(0,0), IMVEC2(1,1), IMVEC4(1,1,1,1), IMVEC4(0,0,0,0));
igImage(simgui_imtextureid(img_ui->simgui_img), IMVEC2(w, h), IMVEC2(0,0), IMVEC2(1,1), IMVEC4(1,1,1,1), IMVEC4(0,0,0,0));
igPopID();
} else {
igText("Image not renderable.");
Expand Down
Loading

0 comments on commit e606e1d

Please sign in to comment.