Skip to content

Instantly share code, notes, and snippets.

@waldnercharles
Created September 11, 2026 22:00
Show Gist options
  • Select an option

  • Save waldnercharles/dfd4e13ec2839c0e9caba9ab3ac7731d to your computer and use it in GitHub Desktop.

Select an option

Save waldnercharles/dfd4e13ec2839c0e9caba9ab3ac7731d to your computer and use it in GitHub Desktop.
diff --git a/include/cute_draw.h b/include/cute_draw.h
--- a/include/cute_draw.h
+++ b/include/cute_draw.h
@@ -599,6 +599,24 @@
* @related CF_CustomShape cf_draw_custom_shape cf_draw_custom_shape_fill cf_draw_push_vertex_attributes
*/
CF_API CF_CustomShape CF_CALL cf_make_custom_shape(const char* sdf_src);
+
+/**
+ * @function cf_make_custom_shape_coverage
+ * @category draw
+ * @brief Registers a custom coverage field without a distance-bound requirement.
+ * @param sdf_src GLSL snippet defining float sdf(vec2 p, ShapeParams s).
+ * @remarks Draw with cf_draw_custom_shape or cf_draw_custom_shape_fill and supply
+ * conservative bounds enclosing ALL visible coverage. Tile binning uses
+ * those bounds only; distance rejection and opaque-cover culling are
+ * disabled. The field is evaluated unchanged for fill, stroke, AA and
+ * border shading. Unlike cf_make_custom_shape, angular warps and other
+ * fields that are not Lipschitz distance bounds are supported. These
+ * shapes cannot be used in shape groups: smooth CSG bounds require a
+ * true distance bound. Registration lifetime and shader rules match
+ * cf_make_custom_shape.
+ * @related cf_make_custom_shape cf_draw_custom_shape cf_draw_custom_shape_fill
+ */
+CF_API CF_CustomShape CF_CALL cf_make_custom_shape_coverage(const char* sdf_src);
/**
* @function cf_draw_custom_shape
diff --git a/src/cute_draw.cpp b/src/cute_draw.cpp
--- a/src/cute_draw.cpp
+++ b/src/cute_draw.cpp
@@ -243,6 +243,7 @@
{
CF_Command& cmd = s_draw->cmds.last();
BatchGeometry& g = cmd.geoms.add();
+ g.coverage_only = false;
g.mvp = s_draw->mvp;
g.blend = s_draw->blends.last();
// Stroke/effect state is captured for every geometry here rather than per emitter, so a
@@ -705,6 +706,9 @@
} break;
}
+ // Bit 32 is independent of the low shape bits and dash bit 16.
+ if (geom.coverage_only) tc.type |= 32u;
+
// Shape effects: a trailing payload block (outline rgba, glow rgba, widths) pointed at
// by tc.fx.x. tc.fx.y is how far past the shape the effects reach, so the coverage quad
// and the tile cull can pad for a glow rather than clipping it.
@@ -727,6 +731,9 @@
if (is_sdf && tc.fill == 1.0f && geom.alpha >= 1.0f && geom.color.a >= 1.0f && geom.type != BATCH_GEOMETRY_TYPE_SEGMENT_CLIPPED && blend == CF_DRAW_BLEND_NORMAL && !(tc.type & 16u) && tc.fx[1] == 0 && geom.fx.outline_width >= 0) {
tc.opaque = 1.0f;
}
+
+ // Coverage fields cannot prove that an entire tile is opaque.
+ if (geom.coverage_only) tc.opaque = 0.0f;
if (instanced) {
// Rasterizer coverage: the instanced VS derives quads from the params, so
@@ -2323,7 +2330,7 @@
//--------------------------------------------------------------------------------------------------
// Custom user-registered SDF shapes.
-CF_CustomShape cf_make_custom_shape(const char* sdf_src)
+static CF_CustomShape s_make_custom_shape(const char* sdf_src, bool coverage_only)
{
CF_CustomShape result = { 0 };
if (!s_draw->instanced_available) {
@@ -2331,6 +2338,7 @@
return result;
}
s_draw->custom_shape_srcs.add(String(sdf_src));
+ s_draw->custom_shape_coverage.add(coverage_only);
// Stitch every registered snippet into custom_shapes.shd: rename each sdf() to
// sdf_<i> via the preprocessor, then generate the per-command dispatcher.
@@ -2350,6 +2358,7 @@
CF_Shader old_draw = app->draw_shader;
if (!cf_recompile_draw_pipelines(src.c_str())) {
s_draw->custom_shape_srcs.pop();
+ s_draw->custom_shape_coverage.pop();
fprintf(stderr, "cf_make_custom_shape: failed to compile the custom shape sdf() snippet.\n");
return result;
}
@@ -2367,10 +2376,26 @@
return result;
}
+CF_CustomShape cf_make_custom_shape(const char* sdf_src)
+{
+ return s_make_custom_shape(sdf_src, false);
+}
+
+CF_CustomShape cf_make_custom_shape_coverage(const char* sdf_src)
+{
+ return s_make_custom_shape(sdf_src, true);
+}
+
static void s_draw_custom_shape(CF_CustomShape shape, CF_Aabb bounds, float stroke, bool fill, const float* params, int param_count)
{
if (!shape.id || (int)shape.id > s_draw->custom_shape_srcs.count()) return;
+ bool coverage_only = s_draw->custom_shape_coverage[(int)shape.id - 1];
+ if (coverage_only && s_draw->shape_group_active) {
+ CF_ASSERT(!"Coverage-only custom shapes cannot be used in shape groups.");
+ return;
+ }
BatchGeometry& g = s_push_shape_geom();
+ g.coverage_only = coverage_only;
g.type = BATCH_GEOMETRY_TYPE_CUSTOM;
float pad = stroke + s_draw->aaf;
v2 mn = bounds.min - V2(pad, pad);
diff --git a/src/internal/cute_draw_internal.h b/src/internal/cute_draw_internal.h
--- a/src/internal/cute_draw_internal.h
+++ b/src/internal/cute_draw_internal.h
@@ -77,6 +77,7 @@
struct BatchGeometry
{
BatchGeometryType type;
+ bool coverage_only; // Custom coverage field: its AABB is the only safe culling bound.
CF_Color color; // Premultiplied. Stays float end-to-end so HDR channels (> 1.0) survive.
CF_V2 box[4]; // World-space AABB coverage corners.
int n; // Only needed/used for polygon.
@@ -392,6 +393,7 @@
// User SDF snippets registered via cf_make_custom_shape, in dispatch-index order.
// Stitched into custom_shapes.shd and compiled into every SDF command pipeline.
Cute::Array<Cute::String> custom_shape_srcs;
+ Cute::Array<bool> custom_shape_coverage;
// CSG shape group recording state (cf_draw_shape_group_begin/op/end). While active,
// SDF shape recorders stage into group_geoms instead of emitting commands.
bool shape_group_active = false;
diff --git a/tools/builtin_shaders.h b/tools/builtin_shaders.h
--- a/tools/builtin_shaders.h
+++ b/tools/builtin_shaders.h
@@ -1543,7 +1543,7 @@
"bool tile_touched(Cmd cmd, int tx, int ty)\n" \
"{\n" \
" uint type = cmd.meta.x & 15u;\n" \
-" if (type <= 1u || type == 4u || type == 11u) return true; /* Sprites/text/tris/glyphs: AABB only. */\n" \
+" if ((cmd.meta.x & 32u) != 0u || type <= 1u || type == 4u || type == 11u) return true; /* Coverage fields and sprites/text/tris/glyphs: AABB only. */\n" \
" float r_tile;\n" \
" float d = cmd_distance_at(cmd, tx, ty, r_tile);\n" \
" /* cmd.fx.y is how far an outline or glow reaches past the shape itself. */\n" \
diff --git a/src/cute_draw.cpp b/src/cute_draw.cpp
--- a/src/cute_draw.cpp
+++ b/src/cute_draw.cpp
@@ -2342,6 +2342,16 @@
// Stitch every registered snippet into custom_shapes.shd: rename each sdf() to
// sdf_<i> via the preprocessor, then generate the per-command dispatcher.
+ // Registrations survive application module swaps. Reuse identical source and
+ // coverage mode without growing the dispatcher or rebuilding GPU pipelines.
+ for (int i = 0; i + 1 < s_draw->custom_shape_srcs.count(); ++i) {
+ if (s_draw->custom_shape_coverage[i] != coverage_only ||
+ strcmp(s_draw->custom_shape_srcs[i].c_str(), sdf_src)) continue;
+ s_draw->custom_shape_srcs.pop();
+ s_draw->custom_shape_coverage.pop();
+ result.id = (uint32_t)(i + 1);
+ return result;
+ }
String src;
src.append("struct ShapeParams\n{\n\tvec2 a, b, c, d, e, f, g, h;\n\tvec4 attributes;\n};\n\n");
for (int i = 0; i < s_draw->custom_shape_srcs.count(); ++i) {
diff --git a/src/cute_graphics_sdlgpu.cpp b/src/cute_graphics_sdlgpu.cpp
--- a/src/cute_graphics_sdlgpu.cpp
+++ b/src/cute_graphics_sdlgpu.cpp
@@ -90,6 +90,8 @@
SDL_GPUTexture* tex;
SDL_GPUTransferBuffer* buf;
SDL_GPUSampler* sampler;
+ SDL_GPUSamplerCreateInfo sampler_info = {};
+ SDL_GPUSampler* draw_samplers[2] = {};
CF_PixelFormat pixel_format;
SDL_GPUTextureFormat format;
SDL_GPUTextureSamplerBinding binding;
@@ -216,7 +218,8 @@
bool skip_drawing;
int msaa_sample_count;
CF_CanvasInternal* canvas;
- SDL_GPUSampler* sampler_override;
+ CF_Filter filter_override;
+ bool has_filter_override;
SDL_GPUBuffer* instance_override; // Draw3d baked lists / staged uploads: replaces the
int instance_override_count; // applied mesh's instance buffer for exactly one draw,
int instance_override_offset; // bound at this byte offset (a slice of a shared buffer).
@@ -610,13 +613,13 @@
if (!tex) return { 0 };
SDL_GPUSampler* sampler = NULL;
+ SDL_GPUSamplerCreateInfo sampler_info = SDL_GPUSamplerCreateInfoDefaults();
// Depth/stencil textures don't need their own sampler, as the associated color
// texture in the owning canvas already has a sampler attached -- except comparison
// (shadow) samplers, which exist precisely to sample a depth texture, and depth
// textures that explicitly ask for SAMPLER usage: those are bound as a plain
// sampler2D for non-comparison reads (soft particles, SSAO, depth-aware fog).
if (!s_is_depth(params.pixel_format) || params.compare_enable || (params.usage & CF_TEXTURE_USAGE_SAMPLER_BIT)) {
- SDL_GPUSamplerCreateInfo sampler_info = SDL_GPUSamplerCreateInfoDefaults();
sampler_info.mip_lod_bias = params.mip_lod_bias;
// Without the enable flag max_anisotropy is ignored wholesale (it also overrides
// mipmap_mode filtering per the SDL_GPU/Vulkan rules, so only enable when asked).
@@ -659,6 +662,7 @@
tex_internal->tex = tex;
tex_internal->buf = buf;
tex_internal->sampler = sampler;
+ tex_internal->sampler_info = sampler_info;
tex_internal->pixel_format = params.pixel_format;
tex_internal->type = params.texture_type;
tex_internal->layers = layers;
@@ -1152,6 +1156,9 @@
CF_TextureInternal* tex = (CF_TextureInternal*)texture_handle.id;
SDL_ReleaseGPUTexture(g_ctx.device, tex->tex);
if (tex->sampler) SDL_ReleaseGPUSampler(g_ctx.device, tex->sampler);
+ for (SDL_GPUSampler* sampler : tex->draw_samplers) {
+ if (sampler) SDL_ReleaseGPUSampler(g_ctx.device, sampler);
+ }
if (tex->buf) SDL_ReleaseGPUTransferBuffer(g_ctx.device, tex->buf);
CF_FREE(tex);
}
@@ -2351,6 +2358,23 @@
return key;
}
+// Draw filtering belongs to Cute's atlas/canvas input. Custom bindings retain
+// their authored samplers; changing u_image filtering retains its wrap and mip modes.
+static SDL_GPUSampler* s_draw_sampler(const CF_MaterialTex& binding)
+{
+ if (binding.sampler.id) return (SDL_GPUSampler*)binding.sampler.id;
+ CF_TextureInternal* texture = (CF_TextureInternal*)binding.handle.id;
+ if (!g_ctx.has_filter_override || binding.name != sintern("u_image")) return texture->sampler;
+ int index = g_ctx.filter_override == CF_FILTER_LINEAR;
+ if (!texture->draw_samplers[index]) {
+ SDL_GPUSamplerCreateInfo info = texture->sampler_info;
+ info.min_filter = info.mag_filter = s_wrap(g_ctx.filter_override);
+ texture->draw_samplers[index] = SDL_CreateGPUSampler(g_ctx.device, &info);
+ CF_ASSERT(texture->draw_samplers[index]);
+ }
+ return texture->draw_samplers[index];
+}
+
void cf_sdlgpu_apply_shader(CF_Shader shader_handle, CF_Material material_handle)
{
CF_ASSERT(g_ctx.canvas);
@@ -2468,14 +2492,7 @@
const char* image_name = material->vs.textures[i].name;
for (int j = 0; j < shader->vs_image_names.size(); ++j) {
if (shader->vs_image_names[j] == image_name) {
- if (g_ctx.sampler_override) {
- vs_sampler_bindings[j].sampler = g_ctx.sampler_override;
- } else if (material->vs.textures[i].sampler.id) {
- // Per-binding standalone sampler (cf_material_set_texture_vs_sampler).
- vs_sampler_bindings[j].sampler = (SDL_GPUSampler*)material->vs.textures[i].sampler.id;
- } else {
- vs_sampler_bindings[j].sampler = ((CF_TextureInternal*)material->vs.textures[i].handle.id)->sampler;
- }
+ vs_sampler_bindings[j].sampler = s_draw_sampler(material->vs.textures[i]);
vs_sampler_bindings[j].texture = ((CF_TextureInternal*)material->vs.textures[i].handle.id)->tex;
found_vs_image_count++;
}
@@ -2507,14 +2524,7 @@
const char* image_name = material->fs.textures[i].name;
for (int j = 0; j < shader->fs_image_names.size(); ++j) {
if (shader->fs_image_names[j] == image_name) {
- if (g_ctx.sampler_override) {
- fs_sampler_bindings[j].sampler = g_ctx.sampler_override;
- } else if (material->fs.textures[i].sampler.id) {
- // Per-binding standalone sampler (cf_material_set_texture_fs_sampler).
- fs_sampler_bindings[j].sampler = (SDL_GPUSampler*)material->fs.textures[i].sampler.id;
- } else {
- fs_sampler_bindings[j].sampler = ((CF_TextureInternal*)material->fs.textures[i].handle.id)->sampler;
- }
+ fs_sampler_bindings[j].sampler = s_draw_sampler(material->fs.textures[i]);
fs_sampler_bindings[j].texture = ((CF_TextureInternal*)material->fs.textures[i].handle.id)->tex;
found_fs_image_count++;
}
@@ -2535,7 +2545,7 @@
}
// Clear sampler override after use.
- g_ctx.sampler_override = NULL;
+ g_ctx.has_filter_override = false;
// Copy over uniform data.
s_copy_uniforms(cmd, &material->block_arena, shader, &material->vs, true);
@@ -2615,23 +2625,19 @@
void* cf_sdlgpu_create_draw_sampler(CF_Filter filter)
{
- SDL_GPUSamplerCreateInfo sampler_info = SDL_GPUSamplerCreateInfoDefaults();
- sampler_info.min_filter = s_wrap(filter);
- sampler_info.mag_filter = s_wrap(filter);
- SDL_GPUSampler* sampler = SDL_CreateGPUSampler(g_ctx.device, &sampler_info);
- return (void*)sampler;
+ // Zero means no override, so encode NEAREST as one.
+ return (void*)((uintptr_t)filter + 1);
}
void cf_sdlgpu_destroy_draw_sampler(void* sampler)
{
- if (sampler) {
- SDL_ReleaseGPUSampler(g_ctx.device, (SDL_GPUSampler*)sampler);
- }
+ CF_UNUSED(sampler); // Filtered sampler variants belong to their textures.
}
void cf_sdlgpu_set_sampler_override(void* sampler)
{
- g_ctx.sampler_override = (SDL_GPUSampler*)sampler;
+ g_ctx.has_filter_override = sampler != NULL;
+ if (sampler) g_ctx.filter_override = (CF_Filter)((uintptr_t)sampler - 1);
}
//--------------------------------------------------------------------------------------------------
diff --git a/src/cute_graphics_gles.cpp b/src/cute_graphics_gles.cpp
--- a/src/cute_graphics_gles.cpp
+++ b/src/cute_graphics_gles.cpp
@@ -165,6 +165,7 @@
GLint mag_filter = GL_LINEAR;
GLint wrap_u = GL_REPEAT;
GLint wrap_v = GL_REPEAT;
+ GLuint draw_samplers[2] = {};
GLenum target = GL_TEXTURE_2D; // GL_TEXTURE_CUBE_MAP / GL_TEXTURE_3D / GL_TEXTURE_2D_ARRAY.
int layers = 1; // Array layers, 3D depth, or 6 for cube maps.
bool compare = false; // Comparison (shadow) sampling.
@@ -1172,6 +1173,7 @@
{
if (!tex.id) return;
CF_GL_Texture* t = (CF_GL_Texture*)(uintptr_t)tex.id;
+ glDeleteSamplers(2, t->draw_samplers);
for (int i = 0; i < t->ring.count; ++i) {
CF_GL_Slot& slot = t->ring.slots[i];
if (slot.fence) {
@@ -2196,6 +2198,35 @@
CF_POLL_OPENGL_ERROR();
}
+// Keep custom texture bindings independent of Cute's dynamic atlas/canvas filter.
+// Sampler objects avoid mutating a texture that another binding samples differently.
+static GLuint s_draw_sampler(const CF_MaterialTex& binding, CF_GL_Texture* texture)
+{
+ if (binding.sampler.id) return (GLuint)binding.sampler.id;
+ if (!g_ctx.has_filter_override || binding.name != cf_sintern("u_image")) return 0;
+ int index = g_ctx.filter_override == CF_FILTER_LINEAR;
+ GLuint& sampler = texture->draw_samplers[index];
+ if (sampler) return sampler;
+ glGenSamplers(1, &sampler);
+ CF_ASSERT(sampler);
+ GLenum filter = s_wrap(g_ctx.filter_override), min_filter = filter;
+ if (texture->has_mips) {
+ bool linear_mips = texture->min_filter == GL_LINEAR_MIPMAP_LINEAR || texture->min_filter == GL_NEAREST_MIPMAP_LINEAR;
+ min_filter = index ? (linear_mips ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR_MIPMAP_NEAREST)
+ : (linear_mips ? GL_NEAREST_MIPMAP_LINEAR : GL_NEAREST_MIPMAP_NEAREST);
+ }
+ glSamplerParameteri(sampler, GL_TEXTURE_MIN_FILTER, min_filter);
+ glSamplerParameteri(sampler, GL_TEXTURE_MAG_FILTER, filter);
+ glSamplerParameteri(sampler, GL_TEXTURE_WRAP_S, texture->wrap_u);
+ glSamplerParameteri(sampler, GL_TEXTURE_WRAP_T, texture->wrap_v);
+ glSamplerParameteri(sampler, GL_TEXTURE_WRAP_R, texture->wrap_u);
+ if (texture->compare) {
+ glSamplerParameteri(sampler, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
+ glSamplerParameteri(sampler, GL_TEXTURE_COMPARE_FUNC, texture->compare_func);
+ }
+ return sampler;
+}
+
void cf_gles_apply_shader(CF_Shader shader_handle, CF_Material material_handle)
{
CF_GL_Shader* shader = (CF_GL_Shader*)(uintptr_t)shader_handle.id;
@@ -2289,14 +2320,7 @@
if (binding->name == material_tex.name) {
glActiveTexture(GL_TEXTURE0 + texture_unit);
glBindTexture(texture->target, texture->id);
- // Per-binding standalone sampler; zero rebinds sampler 0 so the texture's
- // own parameters apply (and stale bindings from earlier draws reset).
- glBindSampler((GLuint)texture_unit, (GLuint)material_tex.sampler.id);
- if (g_ctx.has_filter_override) {
- GLenum gl_filter = (g_ctx.filter_override == CF_FILTER_NEAREST) ? GL_NEAREST : GL_LINEAR;
- glTexParameteri(texture->target, GL_TEXTURE_MIN_FILTER, gl_filter);
- glTexParameteri(texture->target, GL_TEXTURE_MAG_FILTER, gl_filter);
- }
+ glBindSampler((GLuint)texture_unit, s_draw_sampler(material_tex, texture));
glUniform1i(binding->location, texture_unit);
++texture_unit;
break;
@@ -2313,14 +2337,7 @@
if (binding->name == material_tex.name) {
glActiveTexture(GL_TEXTURE0 + texture_unit);
glBindTexture(texture->target, texture->id);
- // Per-binding standalone sampler; zero rebinds sampler 0 so the texture's
- // own parameters apply (and stale bindings from earlier draws reset).
- glBindSampler((GLuint)texture_unit, (GLuint)material_tex.sampler.id);
- if (g_ctx.has_filter_override) {
- GLenum gl_filter = (g_ctx.filter_override == CF_FILTER_NEAREST) ? GL_NEAREST : GL_LINEAR;
- glTexParameteri(texture->target, GL_TEXTURE_MIN_FILTER, gl_filter);
- glTexParameteri(texture->target, GL_TEXTURE_MAG_FILTER, gl_filter);
- }
+ glBindSampler((GLuint)texture_unit, s_draw_sampler(material_tex, texture));
glUniform1i(binding->location, texture_unit);
++texture_unit;
break;
@@ -2477,27 +2494,21 @@
g_ctx.target_state.blend_constants = { r, g, b, a };
}
-// For GLES, samplers are just stored filter values since GLES sets filter mode directly on textures.
void* cf_gles_create_draw_sampler(CF_Filter filter)
{
- // Store the filter value as a pointer (we only need NEAREST=0 or LINEAR=1).
- return (void*)(uintptr_t)filter;
+ // Zero means no override, so encode NEAREST as one.
+ return (void*)((uintptr_t)filter + 1);
}
void cf_gles_destroy_draw_sampler(void* sampler)
{
- // Nothing to do for GLES - filter values don't need cleanup.
- CF_UNUSED(sampler);
+ CF_UNUSED(sampler); // Filtered sampler variants belong to their textures.
}
void cf_gles_set_sampler_override(void* sampler)
{
- if (sampler) {
- g_ctx.filter_override = (CF_Filter)(uintptr_t)sampler;
- g_ctx.has_filter_override = true;
- } else {
- g_ctx.has_filter_override = false;
- }
+ g_ctx.has_filter_override = sampler != NULL;
+ if (sampler) g_ctx.filter_override = (CF_Filter)((uintptr_t)sampler - 1);
}
//--------------------------------------------------------------------------------------------------
diff --git a/src/cute_graphics_gles.cpp b/src/cute_graphics_gles.cpp
--- a/src/cute_graphics_gles.cpp
+++ b/src/cute_graphics_gles.cpp
@@ -1065,8 +1065,26 @@
static inline CF_GL_Slot* s_prepare_buffer_slot(CF_GL_Buffer* buffer, GLsizeiptr required_bytes, int* out_index)
{
- CF_GL_Slot* slot = s_acquire_or_wait(&buffer->ring, g_ctx.frame_index, out_index);
- if (!slot) return NULL;
+ // Appending past every prior upload cannot overwrite data queued draws read.
+ // Use this existing byte range before looking for a wholly idle buffer.
+ CF_GL_Ring& ring = buffer->ring;
+ CF_GL_Slot* slot = NULL;
+ int index = buffer->active_slot;
+ if (index >= 0 && index < ring.count) {
+ CF_GL_Slot& active = ring.slots[index];
+ if (required_bytes > 0 && required_bytes <= active.size - active.offset) slot = &active;
+ }
+ if (!slot) slot = s_acquire_slot(&ring, g_ctx.frame_index, &index);
+ bool orphan = slot == NULL;
+ if (orphan) {
+ // Re-specifying storage lets GL retain the old allocation for queued
+ // draws. A CPU fence wait here would serialize every few small uploads.
+ index = ring.head;
+ slot = &ring.slots[index];
+ ring.head = (index + 1) % ring.count;
+ }
+ slot->last_use_frame = g_ctx.frame_index;
+ if (out_index) *out_index = index;
if (!slot->handle) {
glGenBuffers(1, &slot->handle);
slot->size = 0;
@@ -1076,14 +1094,16 @@
if (buffer->target == GL_ARRAY_BUFFER) s_bind_array_buffer(slot->handle);
else if (buffer->target == GL_ELEMENT_ARRAY_BUFFER) s_bind_element_buffer(slot->handle);
else glBindBuffer(buffer->target, slot->handle);
- if (slot->size < capacity) {
+ if (orphan || slot->size < capacity || (required_bytes > 0 && required_bytes > slot->size - slot->offset)) {
glBufferData(buffer->target, capacity, NULL, GL_DYNAMIC_DRAW);
slot->size = capacity;
slot->offset = 0;
buffer->capacity = capacity;
- } else if (required_bytes > 0 && slot->offset + required_bytes > slot->size) {
- glBufferData(buffer->target, slot->size, NULL, GL_DYNAMIC_DRAW);
- slot->offset = 0;
+ if (slot->fence) {
+ glDeleteSync(slot->fence);
+ slot->fence = 0;
+ }
+ slot->in_flight_frame = 0;
}
return slot;
}
diff --git a/src/cute_graphics_gles.cpp b/src/cute_graphics_gles.cpp
--- a/src/cute_graphics_gles.cpp
+++ b/src/cute_graphics_gles.cpp
@@ -489,6 +489,9 @@
if (status != GL_ALREADY_SIGNALED && status != GL_CONDITION_SATISFIED) return false;
glDeleteSync(slot.fence);
slot.fence = 0;
+ // This fence covers the slot's current-frame use. Waiting for the
+ // frame-end fence after it signals would deadlock same-frame reuse.
+ slot.in_flight_frame = 0;
}
if (slot.in_flight_frame == 0 || slot.in_flight_frame == g_ctx.frame_index) {
// Never used, or used THIS frame (no covering fence exists yet).
@@ -555,6 +558,7 @@
glClientWaitSync(slot.fence, GL_SYNC_FLUSH_COMMANDS_BIT, GL_TIMEOUT_IGNORED);
glDeleteSync(slot.fence);
slot.fence = 0;
+ slot.in_flight_frame = 0;
}
while (!s_slot_ready(slot)) cf_sleep(0);
#endif
diff --git a/libraries/imgui/imgui_widgets.cpp b/libraries/imgui/imgui_widgets.cpp
--- a/libraries/imgui/imgui_widgets.cpp
+++ b/libraries/imgui/imgui_widgets.cpp
@@ -926,7 +926,7 @@
RenderNavCursor(bb, id, ImGuiNavRenderCursorFlags_Compact);
const ImU32 cross_col = GetColorU32(ImGuiCol_Text);
const ImVec2 cross_center = bb.GetCenter() - ImVec2(0.5f, 0.5f);
- const float cross_extent = g.FontSize * 0.5f * 0.7071f - 1.0f;
+ const float cross_extent = (g.FontSize * 0.5f * 0.7071f - 1.0f) * 0.75f;
const float cross_thickness = 1.0f * (float)(int)g.Style._MainScale; // FIXME-DPI
window->DrawList->AddLine(cross_center + ImVec2(+cross_extent, +cross_extent), cross_center + ImVec2(-cross_extent, -cross_extent), cross_col, cross_thickness);
window->DrawList->AddLine(cross_center + ImVec2(+cross_extent, -cross_extent), cross_center + ImVec2(-cross_extent, +cross_extent), cross_col, cross_thickness);
diff --git a/include/cute_app.h b/include/cute_app.h
index ead3b798..308eb88f 100644
--- a/include/cute_app.h
+++ b/include/cute_app.h
@@ -363,6 +363,20 @@ CF_API void CF_CALL cf_app_update(CF_OnUpdateFn* on_update);
*/
CF_API int CF_CALL cf_app_draw_onto_screen(bool clear);
+/**
+ * @function cf_app_set_imgui_render_target
+ * @category app
+ * @brief Sets the canvas that receives Dear ImGui before the app canvas is blitted to the screen.
+ * @param target The target canvas, or a zero canvas to restore the normal swapchain path.
+ * @return Returns true when the target can be used by the active graphics backend.
+ * @remarks Set this to `cf_app_get_canvas()` when a readback should include both the app canvas and
+ * Dear ImGui. The target is sampled and written by the active frame, so it must remain valid
+ * until `cf_app_draw_onto_screen` returns. The app canvas target is refreshed automatically when
+ * the default canvas is recreated.
+ * @related cf_app_draw_onto_screen cf_app_get_canvas cf_canvas_readback
+ */
+CF_API bool CF_CALL cf_app_set_imgui_render_target(CF_Canvas target);
+
/**
* @function cf_app_get_size
* @category app
@@ -963,6 +977,7 @@ CF_INLINE bool app_is_running() { return cf_app_is_running(); }
CF_INLINE void app_signal_shutdown() { cf_app_signal_shutdown(); }
CF_INLINE void app_update(CF_OnUpdateFn* on_update = NULL) { cf_app_update(on_update); }
CF_INLINE int app_draw_onto_screen(bool clear = false) { return cf_app_draw_onto_screen(clear); }
+CF_INLINE bool app_set_imgui_render_target(CF_Canvas target) { return cf_app_set_imgui_render_target(target); }
CF_INLINE void app_get_size(int* w, int* h) { return cf_app_get_size(w, h); }
CF_INLINE void app_set_size(int w, int h) { return cf_app_set_size(w, h); }
CF_INLINE void app_get_position(int* x, int* y) { return cf_app_get_position(x, y); }
diff --git a/src/cute_app.cpp b/src/cute_app.cpp
index fe7a0784..653ea95d 100644
--- a/src/cute_app.cpp
+++ b/src/cute_app.cpp
@@ -146,6 +146,7 @@ using namespace Cute;
static void s_canvas(int w, int h)
{
+ bool target_app_canvas = app->offscreen_canvas.id && app->imgui_render_target.id == app->offscreen_canvas.id;
CF_CanvasParams params = cf_canvas_defaults(w, h);
params.target.filter = CF_FILTER_LINEAR;
if (app->offscreen_canvas.id) {
@@ -154,6 +155,7 @@ static void s_canvas(int w, int h)
params.depth_stencil_enable = true;
params.sample_count = (CF_SampleCount)cf_clamp((app->sample_count >> 1), 0, 3);
app->offscreen_canvas = cf_make_canvas(params);
+ if (target_app_canvas) app->imgui_render_target = app->offscreen_canvas;
app->canvas_w = w;
app->canvas_h = h;
cf_draw_on_app_canvas_resized(w, h);
@@ -487,6 +489,12 @@ void cf_app_update(CF_OnUpdateFn* on_update)
ImGui_ImplSDLGPU3_NewFrame();
}
ImGui_ImplSDL3_NewFrame();
+ if (app->imgui_render_target.id) {
+ int width;
+ int height;
+ cf_canvas_get_size(app->imgui_render_target, &width, &height);
+ ImGui::GetIO().DisplaySize = ImVec2((float)width, (float)height);
+ }
ImGui::NewFrame();
}
@@ -554,6 +562,17 @@ int cf_app_draw_onto_screen(bool clear)
// Render any remaining geometry in the draw API.
cf_render_to(app->offscreen_canvas, clear);
+ // Dear ImGui draw. The default path keeps the existing swapchain order. When a target is
+ // selected, draw ImGui into it before blitting the app canvas so the target contains the
+ // complete composed frame.
+ if (app->using_imgui) {
+ ImGui::EndFrame();
+ ImGui::Render();
+ if (app->imgui_render_target.id) {
+ CF_ASSERT(cf_imgui_draw_to_canvas(app->imgui_render_target));
+ }
+ }
+
// Draw the app canvas
if (app->gfx_backend_type == CF_BACKEND_TYPE_GLES3) {
cf_gles_blit_canvas(app->offscreen_canvas);
@@ -563,10 +582,7 @@ int cf_app_draw_onto_screen(bool clear)
#endif
}
- // Dear ImGui draw.
- if (app->using_imgui) {
- ImGui::EndFrame();
- ImGui::Render();
+ if (app->using_imgui && !app->imgui_render_target.id) {
cf_imgui_draw();
}
@@ -784,6 +800,17 @@ CF_Canvas cf_app_get_canvas()
return app->offscreen_canvas;
}
+bool cf_app_set_imgui_render_target(CF_Canvas target)
+{
+#if defined(CF_EMSCRIPTEN)
+ if (target.id) return false;
+#else
+ if (target.id && !app->gfx_enabled) return false;
+#endif
+ app->imgui_render_target = target;
+ return true;
+}
+
void cf_app_set_canvas_size(int w, int h)
{
s_canvas(w, h);
diff --git a/src/cute_graphics_sdlgpu.cpp b/src/cute_graphics_sdlgpu.cpp
index 3ea8eec5..5a50db9f 100644
--- a/src/cute_graphics_sdlgpu.cpp
+++ b/src/cute_graphics_sdlgpu.cpp
@@ -941,6 +941,59 @@ SDL_GPUCommandBuffer* cf_sdlgpu_get_command_buffer()
return g_ctx.cmd;
}
+SDL_GPURenderPass* cf_sdlgpu_begin_imgui_render_pass(CF_Canvas canvas_handle)
+{
+ CF_CanvasInternal* canvas = (CF_CanvasInternal*)canvas_handle.id;
+ CF_ASSERT(canvas);
+ CF_ASSERT(g_ctx.cmd);
+ if (g_ctx.active_pass && g_ctx.canvas != canvas) s_end_active_pass();
+ g_ctx.canvas = canvas;
+ if (g_ctx.active_pass) return g_ctx.active_pass;
+
+ int target_count = canvas->attached_depth ? 0 : (canvas->target_count > 1 ? canvas->target_count : 1);
+ SDL_GPUColorTargetInfo color_infos[CF_MAX_CANVAS_TARGETS];
+ CF_MEMSET(color_infos, 0, sizeof(color_infos));
+ for (int i = 0; i < target_count; ++i) {
+ SDL_GPUColorTargetInfo& color = color_infos[i];
+ color.texture = i == 0 ? canvas->texture : canvas->textures_mrt[i];
+ color.load_op = SDL_GPU_LOADOP_LOAD;
+ color.store_op = canvas->sample_count == CF_SAMPLE_COUNT_1
+ ? SDL_GPU_STOREOP_STORE
+ : SDL_GPU_STOREOP_RESOLVE_AND_STORE;
+ color.resolve_texture = canvas->resolve_texture;
+ if (canvas->attached) {
+ color.layer_or_depth_plane = (Uint32)canvas->attach_layer;
+ color.mip_level = (Uint32)canvas->attach_mip;
+ }
+ }
+
+ SDL_GPUDepthStencilTargetInfo depth_info;
+ CF_MEMSET(&depth_info, 0, sizeof(depth_info));
+ SDL_GPUDepthStencilTargetInfo* depth = NULL;
+ if (canvas->depth_stencil) {
+ depth_info.texture = canvas->depth_stencil;
+ depth_info.load_op = SDL_GPU_LOADOP_LOAD;
+ depth_info.store_op = SDL_GPU_STOREOP_STORE;
+ depth_info.stencil_load_op = SDL_GPU_LOADOP_LOAD;
+ depth_info.stencil_store_op = SDL_GPU_STOREOP_STORE;
+ depth_info.mip_level = (Uint8)(canvas->attached_depth ? canvas->attach_mip : 0);
+ depth_info.layer = (Uint8)(canvas->attached_depth ? canvas->attach_layer : 0);
+ depth = &depth_info;
+ }
+
+ SDL_GPURenderPass* pass = SDL_BeginGPURenderPass(g_ctx.cmd, color_infos, (Uint32)target_count, depth);
+ CF_ASSERT(pass);
+ g_ctx.active_pass = pass;
+ canvas->pass = pass;
+ canvas->clear = false;
+ return pass;
+}
+
+void cf_sdlgpu_end_imgui_render_pass()
+{
+ s_end_active_pass();
+}
+
void cf_sdlgpu_attach(SDL_Window* window)
{
SDL_ClaimWindowForGPUDevice(g_ctx.device, window);
diff --git a/src/cute_imgui.cpp b/src/cute_imgui.cpp
index 807ed855..daf7b84f 100644
--- a/src/cute_imgui.cpp
+++ b/src/cute_imgui.cpp
@@ -80,3 +80,26 @@ void cf_imgui_draw()
#endif
}
}
+
+bool cf_imgui_draw_to_canvas(CF_Canvas canvas)
+{
+ ImDrawData* draw_data = ImGui::GetDrawData();
+ if (!canvas.id || draw_data->DisplaySize.x <= 0.0f || draw_data->DisplaySize.y <= 0.0f) {
+ return !canvas.id;
+ }
+ if (app->gfx_backend_type == CF_BACKEND_TYPE_GLES3) {
+ cf_gles_apply_canvas(canvas, false);
+ ImGui_ImplOpenGL3_RenderDrawData(draw_data);
+ return true;
+ }
+#ifndef CF_EMSCRIPTEN
+ SDL_GPUCommandBuffer* cmd = cf_sdlgpu_get_command_buffer();
+ ImGui_ImplSDLGPU3_PrepareDrawData(draw_data, cmd);
+ SDL_GPURenderPass* render_pass = cf_sdlgpu_begin_imgui_render_pass(canvas);
+ ImGui_ImplSDLGPU3_RenderDrawData(draw_data, cmd, render_pass);
+ cf_sdlgpu_end_imgui_render_pass();
+ return true;
+#else
+ return false;
+#endif
+}
diff --git a/src/internal/cute_app_internal.h b/src/internal/cute_app_internal.h
index d5f674ba..bd3278d6 100644
--- a/src/internal/cute_app_internal.h
+++ b/src/internal/cute_app_internal.h
@@ -160,6 +160,7 @@ struct CF_App
// Dear ImGui stuff.
bool using_imgui = false;
+ CF_Canvas imgui_render_target = { };
// Font stuff.
uint64_t font_image_id_gen = CF_FONT_ID_RANGE_LO;
diff --git a/src/internal/cute_graphics_internal.h b/src/internal/cute_graphics_internal.h
index 19315465..5cbcab76 100644
--- a/src/internal/cute_graphics_internal.h
+++ b/src/internal/cute_graphics_internal.h
@@ -257,6 +257,8 @@ bool cf_sdlgpu_wants_msl();
SDL_GPUDevice* cf_sdlgpu_get_device();
SDL_GPUTexture* cf_sdlgpu_get_swapchain_texture();
SDL_GPUCommandBuffer* cf_sdlgpu_get_command_buffer();
+SDL_GPURenderPass* cf_sdlgpu_begin_imgui_render_pass(CF_Canvas canvas);
+void cf_sdlgpu_end_imgui_render_pass();
void cf_sdlgpu_attach(SDL_Window* window);
bool cf_sdlgpu_supports_msaa(int sample_count);
void cf_sdlgpu_flush();
@@ -277,6 +279,7 @@ void cf_gles_flush();
void cf_gles_gpu_sync();
bool cf_gles_set_present_mode(CF_PresentMode mode);
void cf_gles_begin_frame();
+void cf_gles_apply_canvas(CF_Canvas canvas, bool clear);
void cf_gles_blit_canvas(CF_Canvas canvas);
void cf_gles_end_frame();
void cf_gles_cleanup();
diff --git a/src/internal/cute_imgui_internal.h b/src/internal/cute_imgui_internal.h
index 5198426e..05e1a886 100644
--- a/src/internal/cute_imgui_internal.h
+++ b/src/internal/cute_imgui_internal.h
@@ -18,5 +18,6 @@
void cf_imgui_init();
void cf_imgui_shutdown();
void cf_imgui_draw();
+bool cf_imgui_draw_to_canvas(CF_Canvas canvas);
#endif // CF_IMGUI_INTERNAL_H
diff --git a/libraries/imgui/dcimgui.h b/libraries/imgui/dcimgui.h
index b142663c..67a0b412 100644
--- a/libraries/imgui/dcimgui.h
+++ b/libraries/imgui/dcimgui.h
@@ -2122,5 +2122,6 @@ typedef enum
ImGuiStyleVar_SeparatorTextBorderSize, // float SeparatorTextBorderSize
ImGuiStyleVar_SeparatorTextAlign, // ImVec2 SeparatorTextAlign
ImGuiStyleVar_SeparatorTextPadding, // ImVec2 SeparatorTextPadding
+ ImGuiStyleVar_ScrollSmooth, // float ScrollSmooth
ImGuiStyleVar_DockingSeparatorSize, // float DockingSeparatorSize
ImGuiStyleVar_COUNT,
@@ -2599,5 +2600,7 @@ struct ImGuiStyle_t
ImGuiHoveredFlags HoverFlagsForTooltipMouse; // Default flags when using IsItemHovered(ImGuiHoveredFlags_ForTooltip) or BeginItemTooltip()/SetItemTooltip() while using mouse.
ImGuiHoveredFlags HoverFlagsForTooltipNav; // Default flags when using IsItemHovered(ImGuiHoveredFlags_ForTooltip) or BeginItemTooltip()/SetItemTooltip() while using keyboard/gamepad.
+ float ScrollSmooth; // Smooth scrolling amount. 1.0f is an immediate jump; larger values ease toward the target.
+
// [Internal]
float _MainScale; // FIXME-WIP: Reference scale, as applied by ScaleAllSizes(). PLEASE DO NOT USE THIS FOR NOW.
diff --git a/libraries/imgui/dcimgui_internal.h b/libraries/imgui/dcimgui_internal.h
index 75c16708..300023a9 100644
--- a/libraries/imgui/dcimgui_internal.h
+++ b/libraries/imgui/dcimgui_internal.h
@@ -2868,5 +2868,6 @@ struct ImGuiWindow_t
ImGuiID ChildId; // ID of corresponding item in parent window (for navigation to return from child window to parent window)
ImGuiID PopupId; // ID in the popup stack when this window is used as a popup/menu (because we use generic Name/ID for recycling)
ImVec2 Scroll;
+ ImVec2 ScrollExpected; // Target scroll position used by the scrollbar and public scroll queries.
ImVec2 ScrollMax;
ImVec2 ScrollTarget; // target scroll position. stored as cursor position with scrolling canceled out, so the highest point is always 0.0f. (FLT_MAX for no change)
diff --git a/libraries/imgui/imgui.cpp b/libraries/imgui/imgui.cpp
index 306ca570..3f6041a1 100644
--- a/libraries/imgui/imgui.cpp
+++ b/libraries/imgui/imgui.cpp
@@ -1516,4 +1516,5 @@ ImGuiStyle::ImGuiStyle()
ScrollbarSize = 14.0f; // Width of the vertical scrollbar, Height of the horizontal scrollbar
ScrollbarRounding = 9.0f; // Radius of grab corners rounding for scrollbar
ScrollbarPadding = 2.0f; // Padding of scrollbar grab within its frame (same for both axes)
+ ScrollSmooth = 1.0f; // Smooth scrolling amount. 1.0f is an immediate jump.
GrabMinSize = 12.0f; // Minimum width/height of a grab box for slider/scrollbar
@@ -3706,4 +3707,5 @@ static const ImGuiStyleVarInfo GStyleVarsInfo[] =
{ 1, ImGuiDataType_Float, (ImU32)offsetof(ImGuiStyle, ScrollbarSize) }, // ImGuiStyleVar_ScrollbarSize
{ 1, ImGuiDataType_Float, (ImU32)offsetof(ImGuiStyle, ScrollbarRounding) }, // ImGuiStyleVar_ScrollbarRounding
{ 1, ImGuiDataType_Float, (ImU32)offsetof(ImGuiStyle, ScrollbarPadding) }, // ImGuiStyleVar_ScrollbarPadding
+ { 1, ImGuiDataType_Float, (ImU32)offsetof(ImGuiStyle, ScrollSmooth) }, // ImGuiStyleVar_ScrollSmooth
{ 1, ImGuiDataType_Float, (ImU32)offsetof(ImGuiStyle, GrabMinSize) }, // ImGuiStyleVar_GrabMinSize
@@ -4674,4 +4676,5 @@ ImGuiWindow::ImGuiWindow(ImGuiContext* ctx, const char* name) : DrawListInst(NULL)
MoveId = GetID("#MOVE");
TabId = GetID("#TAB");
ScrollTarget = ImVec2(FLT_MAX, FLT_MAX);
+ ScrollExpected = ImVec2(0.0f, 0.0f);
ScrollTargetCenterRatio = ImVec2(0.5f, 0.5f);
@@ -10997,7 +11000,7 @@ void ImGui::UpdateMouseWheel()
LockWheelingWindow(window, wheel.x);
float max_step = window->InnerRect.GetWidth() * 0.67f;
float scroll_step = ImTrunc(ImMin(2 * window->FontRefSize, max_step));
- SetScrollX(window, window->Scroll.x - wheel.x * scroll_step);
+ SetScrollX(window, window->ScrollExpected.x - wheel.x * scroll_step);
g.WheelingWindowScrolledFrame = g.FrameCount;
}
if (do_scroll[ImGuiAxis_Y])
@@ -11005,7 +11008,7 @@ void ImGui::UpdateMouseWheel()
LockWheelingWindow(window, wheel.y);
float max_step = window->InnerRect.GetHeight() * 0.67f;
float scroll_step = ImTrunc(ImMin(5 * window->FontRefSize, max_step));
- SetScrollY(window, window->Scroll.y - wheel.y * scroll_step);
+ SetScrollY(window, window->ScrollExpected.y - wheel.y * scroll_step);
g.WheelingWindowScrolledFrame = g.FrameCount;
}
}
@@ -12453,3 +12456,4 @@ static float CalcScrollEdgeSnap(float target, float snap_min, float snap_max, float snap_threshold, float center_ratio)
static ImVec2 CalcNextScrollFromScrollTargetAndClamp(ImGuiWindow* window)
{
+ ImGuiContext& g = *GImGui;
ImVec2 scroll = window->Scroll;
@@ -12467,11 +12471,29 @@ static ImVec2 CalcNextScrollFromScrollTargetAndClamp(ImGuiWindow* window)
float snap_max = window->ScrollMax[axis] + window->SizeFull[axis] - decoration_size[axis];
scroll_target = CalcScrollEdgeSnap(scroll_target, snap_min, snap_max, window->ScrollTargetEdgeSnapDist[axis], center_ratio);
}
- scroll[axis] = scroll_target - center_ratio * (window->SizeFull[axis] - decoration_size[axis]);
+ window->ScrollExpected[axis] = scroll_target - center_ratio * (window->SizeFull[axis] - decoration_size[axis]);
}
- scroll[axis] = ImRound64(ImMax(scroll[axis], 0.0f));
+ window->ScrollExpected[axis] = ImRound64(ImMax(window->ScrollExpected[axis], 0.0f));
if (!window->Collapsed && !window->SkipItems)
- scroll[axis] = ImMin(scroll[axis], window->ScrollMax[axis]);
+ window->ScrollExpected[axis] = ImMin(window->ScrollExpected[axis], window->ScrollMax[axis]);
+ if (window->ScrollExpected[axis] != scroll[axis])
+ {
+ if (g.Style.ScrollSmooth <= 1.0f)
+ scroll[axis] = window->ScrollExpected[axis];
+ else
+ {
+ float frame_factor = ImPow(
+ 1.0f - 1.0f / g.Style.ScrollSmooth,
+ g.IO.DeltaTime * 60.0f
+ );
+ scroll[axis] = ImLinearSweep(
+ scroll[axis],
+ window->ScrollExpected[axis],
+ ImFabs(window->ScrollExpected[axis] - scroll[axis]) *
+ (1.0f - frame_factor)
+ );
+ }
+ }
}
return scroll;
}
@@ -12564,11 +12579,11 @@ ImVec2 ImGui::ScrollToRectEx(ImGuiWindow* window, const ImRect& item_rect, ImGuiScrollFlags flags)
float ImGui::GetScrollX()
{
ImGuiWindow* window = GImGui->CurrentWindow;
- return window->Scroll.x;
+ return window->ScrollExpected.x;
}
float ImGui::GetScrollY()
{
ImGuiWindow* window = GImGui->CurrentWindow;
- return window->Scroll.y;
+ return window->ScrollExpected.y;
}
diff --git a/libraries/imgui/imgui.h b/libraries/imgui/imgui.h
index 7bf34c84..4be88da7 100644
--- a/libraries/imgui/imgui.h
+++ b/libraries/imgui/imgui.h
@@ -1943,4 +1943,5 @@ enum ImGuiStyleVar_
ImGuiStyleVar_SeparatorTextBorderSize, // float SeparatorTextBorderSize
ImGuiStyleVar_SeparatorTextAlign, // ImVec2 SeparatorTextAlign
ImGuiStyleVar_SeparatorTextPadding, // ImVec2 SeparatorTextPadding
+ ImGuiStyleVar_ScrollSmooth, // float ScrollSmooth
ImGuiStyleVar_DockingSeparatorSize, // float DockingSeparatorSize
@@ -2447,5 +2448,7 @@ struct ImGuiStyle
ImGuiHoveredFlags HoverFlagsForTooltipMouse;// Default flags when using IsItemHovered(ImGuiHoveredFlags_ForTooltip) or BeginItemTooltip()/SetItemTooltip() while using mouse.
ImGuiHoveredFlags HoverFlagsForTooltipNav; // Default flags when using IsItemHovered(ImGuiHoveredFlags_ForTooltip) or BeginItemTooltip()/SetItemTooltip() while using keyboard/gamepad.
+ float ScrollSmooth; // Smooth scrolling amount. 1.0f is an immediate jump; larger values ease toward the target.
+
// [Internal]
float _MainScale; // FIXME-WIP: Reference scale, as applied by ScaleAllSizes(). PLEASE DO NOT USE THIS FOR NOW.
diff --git a/libraries/imgui/imgui_internal.h b/libraries/imgui/imgui_internal.h
index 9f532abc..21e81211 100644
--- a/libraries/imgui/imgui_internal.h
+++ b/libraries/imgui/imgui_internal.h
@@ -2892,4 +2892,5 @@ struct IMGUI_API ImGuiWindow
ImGuiID ChildId; // ID of corresponding item in parent window (for navigation to return from child window to parent window)
ImGuiID PopupId; // ID in the popup stack when this window is used as a popup/menu (because we use generic Name/ID for recycling)
ImVec2 Scroll;
+ ImVec2 ScrollExpected; // Target scroll position used by the scrollbar and public scroll queries.
ImVec2 ScrollMax;
diff --git a/libraries/imgui/imgui_tables.cpp b/libraries/imgui/imgui_tables.cpp
index 9b523bf3..b8712aa8 100644
--- a/libraries/imgui/imgui_tables.cpp
+++ b/libraries/imgui/imgui_tables.cpp
@@ -1496,4 +1496,5 @@ void ImGui::EndTable()
if ((table->Flags & ImGuiTableFlags_ScrollX) == 0 && inner_window != outer_window)
{
inner_window->Scroll.x = 0.0f;
+ inner_window->ScrollExpected.x = 0.0f;
}
diff --git a/libraries/imgui/imgui_widgets.cpp b/libraries/imgui/imgui_widgets.cpp
index b404d391..28c9e5d9 100644
--- a/libraries/imgui/imgui_widgets.cpp
+++ b/libraries/imgui/imgui_widgets.cpp
@@ -1010,9 +1010,9 @@ void ImGui::Scrollbar(ImGuiAxis axis)
ImDrawFlags rounding_corners = CalcRoundingFlagsForRectInRect(bb, host_rect, g.Style.WindowBorderSize);
float size_visible = window->InnerRect.Max[axis] - window->InnerRect.Min[axis];
float size_contents = window->ContentSize[axis] + window->WindowPadding[axis] * 2.0f;
- ImS64 scroll = (ImS64)window->Scroll[axis];
+ ImS64 scroll = (ImS64)window->ScrollExpected[axis];
ScrollbarEx(bb, id, axis, &scroll, (ImS64)size_visible, (ImS64)size_contents, rounding_corners);
- window->Scroll[axis] = (float)scroll;
+ window->ScrollExpected[axis] = (float)scroll;
}
// Vertical/Horizontal scrollbar
@@ -5551,5 +5551,6 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_size, const ImVec2& size_arg, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data)
scroll_y = ImClamp(new_scroll_y, 0.0f, scroll_max_y);
draw_pos.y += (draw_window->Scroll.y - scroll_y); // Manipulate cursor pos immediately avoid a frame of lag
draw_window->Scroll.y = scroll_y;
+ draw_window->ScrollExpected.y = scroll_y;
CalcClipRectVisibleItemsY(clip_rect, draw_pos, g.FontSize, &line_visible_n0, &line_visible_n1);
line_visible_n1 = ImMin(line_visible_n1, line_count);
diff --git a/include/cute_draw.h b/include/cute_draw.h
--- a/include/cute_draw.h
+++ b/include/cute_draw.h
@@ -809,6 +809,20 @@
* @related cf_draw_pop_outline cf_draw_push_glow cf_draw_push_color
*/
CF_API void CF_CALL cf_draw_push_outline(CF_Color color, float width);
+
+/**
+ * @function cf_draw_push_interior_border
+ * @category draw
+ * @brief Draws a border inside subsequent SDF shapes.
+ * @param color Border color, composited over the interior fill.
+ * @param width Inward border width in world units. Nonpositive values disable it.
+ * @remarks The outer silhouette is unchanged. The fill is inset by width and the
+ * remaining edge coverage receives the border color. Works with filled
+ * and stroked SDF shapes, and composes with glow. Sprites and text ignore
+ * it. Shares the outline stack: pair with cf_draw_pop_outline.
+ * @related cf_draw_pop_outline cf_draw_push_outline cf_draw_push_glow
+ */
+CF_API void CF_CALL cf_draw_push_interior_border(CF_Color color, float width);
/**
* @function cf_draw_pop_outline
@@ -2487,6 +2501,7 @@
CF_INLINE void draw_push_dash(float on_length, float off_length, float phase) { cf_draw_push_dash(on_length, off_length, phase); }
CF_INLINE void draw_pop_dash() { cf_draw_pop_dash(); }
CF_INLINE void draw_push_outline(CF_Color color, float width) { cf_draw_push_outline(color, width); }
+CF_INLINE void draw_push_interior_border(CF_Color color, float width) { cf_draw_push_interior_border(color, width); }
CF_INLINE void draw_pop_outline() { cf_draw_pop_outline(); }
CF_INLINE void draw_push_glow(CF_Color color, float radius) { cf_draw_push_glow(color, radius); }
CF_INLINE void draw_pop_glow() { cf_draw_pop_glow(); }
diff --git a/src/cute_draw.cpp b/src/cute_draw.cpp
--- a/src/cute_draw.cpp
+++ b/src/cute_draw.cpp
@@ -708,7 +708,7 @@
// Shape effects: a trailing payload block (outline rgba, glow rgba, widths) pointed at
// by tc.fx.x. tc.fx.y is how far past the shape the effects reach, so the coverage quad
// and the tile cull can pad for a glow rather than clipping it.
- if (is_sdf && (geom.fx.outline_width > 0 || geom.fx.glow_radius > 0)) {
+ if (is_sdf && (geom.fx.outline_width != 0 || geom.fx.glow_radius > 0)) {
uint32_t fx_offset = (uint32_t)pay.count();
CF_MEMCPY(&tc.fx[0], &fx_offset, sizeof(fx_offset));
tc.fx[1] = cf_max(geom.fx.outline_width, geom.fx.glow_radius);
@@ -722,8 +722,9 @@
// Clipped segments are excluded: their planes can cut mid-tile, so "interior
// covers the tile" cannot be decided from the SDF alone.
// Dashed strokes never claim opaque cover: their gaps don't hide what's beneath.
- // Neither do effects: an outline or glow extends past the shape's own interior.
- if (is_sdf && tc.fill == 1.0f && geom.alpha >= 1.0f && geom.color.a >= 1.0f && geom.type != BATCH_GEOMETRY_TYPE_SEGMENT_CLIPPED && blend == CF_DRAW_BLEND_NORMAL && !(tc.type & 16u) && tc.fx[1] == 0) {
+ // Neither do effects: outer effects extend coverage, and an interior border
+ // can make an otherwise opaque fill translucent near its edge.
+ if (is_sdf && tc.fill == 1.0f && geom.alpha >= 1.0f && geom.color.a >= 1.0f && geom.type != BATCH_GEOMETRY_TYPE_SEGMENT_CLIPPED && blend == CF_DRAW_BLEND_NORMAL && !(tc.type & 16u) && tc.fx[1] == 0 && geom.fx.outline_width >= 0) {
tc.opaque = 1.0f;
}
@@ -4597,7 +4598,15 @@
void cf_draw_push_outline(CF_Color color, float width)
{
s_draw->outlines.add(color);
- s_draw->outline_widths.add(width);
+ // Negative widths previously disabled outlines; reserve their encoding for
+ // the explicit interior-border API without changing this API's behavior.
+ s_draw->outline_widths.add(width > 0 ? width : 0);
+}
+
+void cf_draw_push_interior_border(CF_Color color, float width)
+{
+ s_draw->outlines.add(color);
+ s_draw->outline_widths.add(width > 0 ? -width : 0);
}
void cf_draw_pop_outline()
diff --git a/tools/builtin_shaders.h b/tools/builtin_shaders.h
--- a/tools/builtin_shaders.h
+++ b/tools/builtin_shaders.h
@@ -449,10 +449,21 @@
// surface, a glow a falloff past it. Both are exact and cost one smoothstep each: no second
// pass, no blur kernel, no extra geometry. Globals are set per command by the tile walk and from
// varyings in the instanced path; a zero width/radius disables that effect.
-vec4 sdf_effects(vec4 shape_color, float d)
-{
- if (v_outline_width <= 0.0 && v_glow_radius <= 0.0) return shape_color;
+vec4 sdf_effects(vec4 shape_color, float d, vec4 fill_color, float border_d)
+{
+ if (v_outline_width == 0.0 && v_glow_radius <= 0.0) return shape_color;
float aa = max(v_aa, 1.0e-6);
+ if (v_outline_width < 0.0) {
+ // Interior edge coverage matches Neon-Swarm's entity raster: erode the
+ // fill, assign the remaining shape coverage to the border, then place
+ // premultiplied border over fill. A translucent fill never leaks outside.
+ float width = -v_outline_width;
+ float shape_coverage = 1.0 - smoothstep(0.0, aa, border_d);
+ float fill_coverage = 1.0 - smoothstep(-width, -width + aa, border_d);
+ vec4 fill = fill_color * fill_coverage;
+ vec4 border = v_outline * (shape_coverage - fill_coverage);
+ shape_color = border + fill * (1.0 - border.a);
+ }
vec4 under = vec4(0.0);
// Glow first (furthest back): a falloff from the surface outward, squared so it reads like
// light rather than a flat halo.
@@ -492,7 +503,7 @@
vec4 fill = mix(fill_no_aa, fill_aa, v_aa > 0.0 ? 1.0 : 0.0);
result = mix(stroke, fill, v_fill);
- result = sdf_effects(result, d);
+ result = sdf_effects(result, d, b, mix(wire_d, d, v_fill));
return result;
}
)";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment