Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sokol_imgui.h: simplify ImTextureID handling. #1169

Merged
merged 4 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
## Updates

### 17-Dec-2024

- sokol_imgui.h (breaking change): user-provided images and samplers are now
stashed directly in a Dear ImGui ImTextureID handle instead of in a separate backing
object (this is possible now because ImTextureID is now guaranteed to be 64 bits,
so it can directly hold two 32-bit sokol-gfx handles). This change drastically
simplifies the sokol_imgui.h implementatation, but requires some breaking API
changes (please read the updated doc section `ON USER-PROVIDED IMAGES AND SAMPLERS`
in sokol_imgui.h)
- sokol_gfx.h: a couple of new functions to get granular buffer and image properties
called `sg_query_buffer_[property]()` and `sg_query_image_[property]()`. Those are
cheaper than the similar `sg_query_buffer_desc()` and `sg_query_image_desc()`
functions if you only need to get one or few properties.
- sokol_gfx_imgui.h: internal non-breaking fixes for the sokol_imgui.h API update.

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

### 14-Dec-2024

- sokol_app.h win32: merged PR https://github.com/floooh/sokol/pull/1167, this
Expand Down
112 changes: 112 additions & 0 deletions sokol_gfx.h
Original file line number Diff line number Diff line change
Expand Up @@ -4249,6 +4249,18 @@ SOKOL_GFX_API_DECL sg_sampler_desc sg_query_sampler_defaults(const sg_sampler_de
SOKOL_GFX_API_DECL sg_shader_desc sg_query_shader_defaults(const sg_shader_desc* desc);
SOKOL_GFX_API_DECL sg_pipeline_desc sg_query_pipeline_defaults(const sg_pipeline_desc* desc);
SOKOL_GFX_API_DECL sg_attachments_desc sg_query_attachments_defaults(const sg_attachments_desc* desc);
// assorted query functions
SOKOL_GFX_API_DECL size_t sg_query_buffer_size(sg_buffer buf);
SOKOL_GFX_API_DECL sg_buffer_type sg_query_buffer_type(sg_buffer buf);
SOKOL_GFX_API_DECL sg_usage sg_query_buffer_usage(sg_buffer buf);
SOKOL_GFX_API_DECL sg_image_type sg_query_image_type(sg_image img);
SOKOL_GFX_API_DECL int sg_query_image_width(sg_image img);
SOKOL_GFX_API_DECL int sg_query_image_height(sg_image img);
SOKOL_GFX_API_DECL int sg_query_image_num_slices(sg_image img);
SOKOL_GFX_API_DECL int sg_query_image_num_mipmaps(sg_image img);
SOKOL_GFX_API_DECL sg_pixel_format sg_query_image_pixelformat(sg_image img);
SOKOL_GFX_API_DECL sg_usage sg_query_image_usage(sg_image img);
SOKOL_GFX_API_DECL int sg_query_image_sample_count(sg_image img);

// separate resource allocation and initialization (for async setup)
SOKOL_GFX_API_DECL sg_buffer sg_alloc_buffer(void);
Expand Down Expand Up @@ -19228,6 +19240,33 @@ SOKOL_API_IMPL sg_buffer_desc sg_query_buffer_desc(sg_buffer buf_id) {
return desc;
}

SOKOL_API_IMPL size_t sg_query_buffer_size(sg_buffer buf_id) {
SOKOL_ASSERT(_sg.valid);
const _sg_buffer_t* buf = _sg_lookup_buffer(&_sg.pools, buf_id.id);
if (buf) {
return (size_t)buf->cmn.size;
}
return 0;
}

SOKOL_API_IMPL sg_buffer_type sg_query_buffer_type(sg_buffer buf_id) {
SOKOL_ASSERT(_sg.valid);
const _sg_buffer_t* buf = _sg_lookup_buffer(&_sg.pools, buf_id.id);
if (buf) {
return buf->cmn.type;
}
return _SG_BUFFERTYPE_DEFAULT;
}

SOKOL_API_IMPL sg_usage sg_query_buffer_usage(sg_buffer buf_id) {
SOKOL_ASSERT(_sg.valid);
const _sg_buffer_t* buf = _sg_lookup_buffer(&_sg.pools, buf_id.id);
if (buf) {
return buf->cmn.usage;
}
return _SG_USAGE_DEFAULT;
}

SOKOL_API_IMPL sg_image_desc sg_query_image_desc(sg_image img_id) {
SOKOL_ASSERT(_sg.valid);
sg_image_desc desc;
Expand All @@ -19247,6 +19286,79 @@ SOKOL_API_IMPL sg_image_desc sg_query_image_desc(sg_image img_id) {
return desc;
}

SOKOL_API_IMPL sg_image_type sg_query_image_type(sg_image img_id) {
SOKOL_ASSERT(_sg.valid);
const _sg_image_t* img = _sg_lookup_image(&_sg.pools, img_id.id);
if (img) {
return img->cmn.type;
}
return _SG_IMAGETYPE_DEFAULT;
}

SOKOL_API_IMPL int sg_query_image_width(sg_image img_id) {
SOKOL_ASSERT(_sg.valid);
const _sg_image_t* img = _sg_lookup_image(&_sg.pools, img_id.id);
if (img) {
return img->cmn.width;
}
return 0;
}

SOKOL_API_IMPL int sg_query_image_height(sg_image img_id) {
SOKOL_ASSERT(_sg.valid);
const _sg_image_t* img = _sg_lookup_image(&_sg.pools, img_id.id);
if (img) {
return img->cmn.height;
}
return 0;
}

SOKOL_API_IMPL int sg_query_image_num_slices(sg_image img_id) {
SOKOL_ASSERT(_sg.valid);
const _sg_image_t* img = _sg_lookup_image(&_sg.pools, img_id.id);
if (img) {
return img->cmn.num_slices;
}
return 0;
}

SOKOL_API_IMPL int sg_query_image_num_mipmaps(sg_image img_id) {
SOKOL_ASSERT(_sg.valid);
const _sg_image_t* img = _sg_lookup_image(&_sg.pools, img_id.id);
if (img) {
return img->cmn.num_mipmaps;
}
return 0;
}

SOKOL_API_IMPL sg_pixel_format sg_query_image_pixelformat(sg_image img_id) {
SOKOL_ASSERT(_sg.valid);
const _sg_image_t* img = _sg_lookup_image(&_sg.pools, img_id.id);
if (img) {
return img->cmn.pixel_format;
}
return _SG_PIXELFORMAT_DEFAULT;
}

SOKOL_API_IMPL sg_usage sg_query_image_usage(sg_image img_id) {
SOKOL_ASSERT(_sg.valid);
const _sg_image_t* img = _sg_lookup_image(&_sg.pools, img_id.id);
if (img) {
return img->cmn.usage;
}
return _SG_USAGE_DEFAULT;
}

SOKOL_API_IMPL int sg_query_image_sample_count(sg_image img_id) {
SOKOL_ASSERT(_sg.valid);
SOKOL_ASSERT(_sg.valid);
const _sg_image_t* img = _sg_lookup_image(&_sg.pools, img_id.id);
if (img) {
return img->cmn.sample_count;
}
return 0;
}

SOKOL_API_IMPL sg_sampler_desc sg_query_sampler_desc(sg_sampler smp_id) {
SOKOL_ASSERT(_sg.valid);
sg_sampler_desc desc;
Expand Down
26 changes: 26 additions & 0 deletions tests/functional/sokol_gfx_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,9 @@ UTEST(sokol_gfx, query_buffer_desc) {
T(b0_desc.mtl_buffers[0] == 0);
T(b0_desc.d3d11_buffer == 0);
T(b0_desc.wgpu_buffer == 0);
T(sg_query_buffer_size(b0) == 32);
T(sg_query_buffer_type(b0) == SG_BUFFERTYPE_VERTEXBUFFER);
T(sg_query_buffer_usage(b0) == SG_USAGE_STREAM);

float vtx_data[16];
sg_buffer b1 = sg_make_buffer(&(sg_buffer_desc){
Expand All @@ -929,6 +932,9 @@ UTEST(sokol_gfx, query_buffer_desc) {
T(b1_desc.usage == SG_USAGE_IMMUTABLE);
T(b1_desc.data.ptr == 0);
T(b1_desc.data.size == 0);
T(sg_query_buffer_size(b1) == sizeof(vtx_data));
T(sg_query_buffer_type(b1) == SG_BUFFERTYPE_VERTEXBUFFER);
T(sg_query_buffer_usage(b1) == SG_USAGE_IMMUTABLE);

uint16_t idx_data[8];
sg_buffer b2 = sg_make_buffer(&(sg_buffer_desc){
Expand All @@ -941,6 +947,9 @@ UTEST(sokol_gfx, query_buffer_desc) {
T(b2_desc.usage == SG_USAGE_IMMUTABLE);
T(b2_desc.data.ptr == 0);
T(b2_desc.data.size == 0);
T(sg_query_buffer_size(b2) == sizeof(idx_data));
T(sg_query_buffer_type(b2) == SG_BUFFERTYPE_INDEXBUFFER);
T(sg_query_buffer_usage(b2) == SG_USAGE_IMMUTABLE);

// invalid buffer (returns zeroed desc)
sg_buffer b3 = sg_make_buffer(&(sg_buffer_desc){
Expand All @@ -953,6 +962,9 @@ UTEST(sokol_gfx, query_buffer_desc) {
T(b3_desc.size == 0);
T(b3_desc.type == 0);
T(b3_desc.usage == 0);
T(sg_query_buffer_size(b3) == 0);
T(sg_query_buffer_type(b3) == _SG_BUFFERTYPE_DEFAULT);
T(sg_query_buffer_usage(b3) == _SG_USAGE_DEFAULT);

sg_shutdown();
}
Expand Down Expand Up @@ -984,6 +996,13 @@ UTEST(sokol_gfx, query_image_desc) {
T(i0_desc.d3d11_texture == 0);
T(i0_desc.d3d11_shader_resource_view == 0);
T(i0_desc.wgpu_texture == 0);
T(sg_query_image_type(i0) == SG_IMAGETYPE_2D);
T(sg_query_image_width(i0) == 256);
T(sg_query_image_height(i0) == 512);
T(sg_query_image_num_slices(i0) == 1);
T(sg_query_image_num_mipmaps(i0) == 1);
T(sg_query_image_pixelformat(i0) == SG_PIXELFORMAT_R8);
T(sg_query_image_sample_count(i0) == 1);

sg_destroy_image(i0);
const sg_image_desc i0_desc_x = sg_query_image_desc(i0);
Expand All @@ -996,6 +1015,13 @@ UTEST(sokol_gfx, query_image_desc) {
T(i0_desc_x.usage == 0);
T(i0_desc_x.pixel_format == 0);
T(i0_desc_x.sample_count == 0);
T(sg_query_image_type(i0) == _SG_IMAGETYPE_DEFAULT);
T(sg_query_image_width(i0) == 0);
T(sg_query_image_height(i0) == 0);
T(sg_query_image_num_slices(i0) == 0);
T(sg_query_image_num_mipmaps(i0) == 0);
T(sg_query_image_pixelformat(i0) == _SG_PIXELFORMAT_DEFAULT);
T(sg_query_image_sample_count(i0) == 0);

sg_shutdown();
}
Expand Down
9 changes: 1 addition & 8 deletions util/sokol_gfx_imgui.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,6 @@ typedef struct sgimgui_image_t {
float ui_scale;
sgimgui_str_t label;
sg_image_desc desc;
simgui_image_t simgui_img;
} sgimgui_image_t;

typedef struct sgimgui_sampler_t {
Expand Down Expand Up @@ -1577,18 +1576,12 @@ _SOKOL_PRIVATE void _sgimgui_image_created(sgimgui_t* ctx, sg_image res_id, int
img->desc = *desc;
img->ui_scale = 1.0f;
img->label = _sgimgui_make_str(desc->label);
simgui_image_desc_t simgui_img_desc;
_sgimgui_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 _sgimgui_image_destroyed(sgimgui_t* ctx, int slot_index) {
SOKOL_ASSERT((slot_index > 0) && (slot_index < ctx->image_window.num_slots));
sgimgui_image_t* img = &ctx->image_window.slots[slot_index];
img->res_id.id = SG_INVALID_ID;
simgui_destroy_image(img->simgui_img);
}

_SOKOL_PRIVATE void _sgimgui_sampler_created(sgimgui_t* ctx, sg_sampler res_id, int slot_index, const sg_sampler_desc* desc) {
Expand Down Expand Up @@ -3362,7 +3355,7 @@ _SOKOL_PRIVATE void _sgimgui_draw_embedded_image(sgimgui_t* ctx, sg_image img, f
igSliderFloatEx("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(simgui_imtextureid(img_ui->simgui_img), IMVEC2(w, h));
igImage(simgui_imtextureid(img_ui->res_id), IMVEC2(w, h));
igPopID();
} else {
igText("Image not renderable.");
Expand Down
Loading
Loading