Clay Sokol Module
GX includes a clay_sokol module that renders Clay UI layouts using Sokol GP
2D drawing functions. It provides a single function call per frame to draw
rectangles, borders, images, and scissor regions.
Limitations: Sokol GP has no built-in text rendering or rounded rectangles.
Text commands are silently skipped, and rounded corner radii are ignored
(drawn as sharp rectangles). For full UI with text, use clay_raylib instead.
Prerequisites
Requires both the clay and sokol modules.
Usage
import sokol.app
import sokol.gfx
import sokol.glue
import sokol.gp
import clay
import clay_sokol
Import the sokol modules first. clay_sokol draws through sokol.gp, whose
header has to come after sokol_gfx.h.
Build with -I modules:
gx myapp.gx -I modules -o myapp.exe
Quick Example
import sokol.app
import sokol.gfx
import sokol.glue
import sokol.gp
import clay
import clay_sokol
@c_abi fn init() {
var gfx_desc: sg_desc
gfx_desc.environment = sglue_environment()
sg_setup(&gfx_desc)
var gp_desc: sgp_desc
sgp_setup(&gp_desc)
clay.gx_clay_init(800.0, 600.0)
clay.gx_clay_use_default_measure()
}
@c_abi fn frame() {
var w = sapp_width()
var h = sapp_height()
var dt = sapp_frame_duration()
clay.gx_clay_set_dimensions(w, h)
clay.gx_clay_begin_layout()
// Root panel
clay.gx_clay_open_id("root")
clay.gx_clay_config_full(
clay.COLUMN,
clay.SIZING_GROW, 0.0, 0.0,
clay.SIZING_GROW, 0.0, 0.0,
16, 16, 16, 16, 8,
clay.ALIGN_LEFT, clay.ALIGN_TOP,
40.0, 40.0, 45.0, 255.0,
0.0, 0.0, 0.0, 0.0
)
// Sidebar
clay.gx_clay_open_id("sidebar")
clay.gx_clay_config_full(
clay.COLUMN,
clay.SIZING_FIXED, 200.0, 200.0,
clay.SIZING_GROW, 0.0, 0.0,
8, 8, 8, 8, 4,
clay.ALIGN_LEFT, clay.ALIGN_TOP,
55.0, 55.0, 60.0, 255.0,
0.0, 0.0, 0.0, 0.0
)
clay.gx_clay_close()
clay.gx_clay_close()
var count: i32 = 0
var cmds = clay.gx_clay_end_layout(dt, &count)
// Render with Sokol GP
sgp_begin(w, h)
sgp_viewport(0, 0, w, h)
sgp_project(0.0, w, 0.0, h)
sgp_set_color(0.0, 0.0, 0.0, 1.0)
sgp_clear()
clay_sokol.gx_clay_render_sokol(cmds, count)
var pass: sg_pass
pass.swapchain = sglue_swapchain()
sg_begin_pass(&pass)
sgp_flush()
sgp_end()
sg_end_pass()
sg_commit()
}
@c_abi fn cleanup() {
sgp_shutdown()
sg_shutdown()
}
fn main() {
var desc: sapp_desc
desc.init_cb = init
desc.frame_cb = frame
desc.cleanup_cb = cleanup
desc.width = 800
desc.height = 600
desc.window_title = "Clay + Sokol"
sapp_run(&desc)
}
Rendering
| Function | Signature | Description |
|---|---|---|
gx_clay_render_sokol | (*void, i32) | Render all commands with Sokol GP |
Call between sgp_begin(w, h) and sgp_flush() / sgp_end():
sgp_begin(w, h)
clay_sokol.gx_clay_render_sokol(cmds, count)
// ... can draw additional Sokol GP shapes here ...
sgp_flush()
sgp_end()
Render Command Mapping
| Clay Command | Sokol GP Call | Notes |
|---|---|---|
CMD_RECTANGLE | sgp_set_color + sgp_draw_filled_rect | Corner radius ignored |
CMD_TEXT | Skipped | No text in Sokol GP |
CMD_BORDER | 4x sgp_draw_filled_rect | Thin rects per side |
CMD_IMAGE | sgp_set_image + sgp_draw_textured_rect | imageData = sg_image ID |
CMD_SCISSOR_START | sgp_scissor | Clip region |
CMD_SCISSOR_END | sgp_reset_scissor | End clipping |
Color Conversion
Clay stores colors as 0-255 floats. Sokol GP expects 0.0-1.0 floats. The renderer divides by 255 automatically.
When to Use This vs clay_raylib
clay_raylib | clay_sokol | |
|---|---|---|
| Text rendering | Yes | No |
| Rounded corners | Yes | No |
| Web export | No | Yes (Emscripten) |
| GPU backends | OpenGL only | OpenGL, D3D11, Metal, WebGPU |
| Text measurement | Automatic | Use gx_clay_use_default_measure() |
Use clay_sokol when targeting web or multi-backend rendering. Use
clay_raylib when you need text and rounded corners.
Platform Notes
- Color: Automatically converted from Clay’s 0-255 to Sokol’s 0.0-1.0.
- Images: Pass
sg_image.idas theimageDatapointer when building Clay elements withgx_clay_config_image. - Web: Works with Emscripten since Sokol compiles to WebGL2/WebGPU.
- TCC: Compiled with
-DCLAY_DISABLE_SIMD(TCC lacks SSE intrinsics).
Module Layout
modules/
clay_sokol/
c/
gx_clay_render_sokol.h Render loop with Sokol GP calls
gx/
clay_sokol.gx Module: 1 extern function