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

FunctionSignatureDescription
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 CommandSokol GP CallNotes
CMD_RECTANGLEsgp_set_color + sgp_draw_filled_rectCorner radius ignored
CMD_TEXTSkippedNo text in Sokol GP
CMD_BORDER4x sgp_draw_filled_rectThin rects per side
CMD_IMAGEsgp_set_image + sgp_draw_textured_rectimageData = sg_image ID
CMD_SCISSOR_STARTsgp_scissorClip region
CMD_SCISSOR_ENDsgp_reset_scissorEnd 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_raylibclay_sokol
Text renderingYesNo
Rounded cornersYesNo
Web exportNoYes (Emscripten)
GPU backendsOpenGL onlyOpenGL, D3D11, Metal, WebGPU
Text measurementAutomaticUse 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.id as the imageData pointer when building Clay elements with gx_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