Skip to content

C API: Lighting and shadows

A key light, ambient and environment light, shadows on the avatars and the ground, and the host's own shadow map: Lighting and shadows.

sona_lighting_defaults

void sona_lighting_defaults(sona_lighting *l);
Parameter and type Meaning
l
sona_lighting *
The sona_lighting to fill with the defaults (required)

Returns Nothing.

Example (also in Lighting and shadows):

sona_lighting_defaults(&l);
l.key.direction[0] = 0.4f; l.key.direction[1] = -1.0f;
l.key.direction[2] = 0.6f;
l.key.intensity = 0.9f;
/* a crowd's ground shadows for almost nothing */
l.flags = SONA_LIGHTING_GROUND | SONA_LIGHTING_PROXY_CASTERS;
sona_renderer_set_lighting(r, &l);

size set; ambient 0, environment 1, specular 1, rim 1, shadow FIT, shadow_size 2048, shadow_strength 1, shadow_ambient 0.5, shadow_bias 0.03, ground_y 0, ground_opacity 0.6.

sona_renderer_set_lighting

sona_result sona_renderer_set_lighting(sona_renderer *r,
                                       const sona_lighting *l);
Parameter and type Meaning
r
sona_renderer *
The renderer (required)
l
const sona_lighting *
A sona_lighting (required)

Returns SONA_OK. Errors SONA_E_ARGUMENT, SONA_E_UNSUPPORTED, SONA_E_MEMORY (what each means).

Example (also in Lighting and shadows):

sona_lighting_defaults(&l);
l.key.direction[0] = 0.4f; l.key.direction[1] = -1.0f;
l.key.direction[2] = 0.6f;
l.key.intensity = 0.9f;
/* a crowd's ground shadows for almost nothing */
l.flags = SONA_LIGHTING_GROUND | SONA_LIGHTING_PROXY_CASTERS;
sona_renderer_set_lighting(r, &l);

From the next draw. NULL: no lighting, the original look again. SONA_E_ARGUMENT for a size that is not sizeof(sona_lighting), a value out of range, an unknown flag or mode, a zero key direction with shadows on, or a matrix that is not orthographic.

sona_renderer_lighting

sona_result sona_renderer_lighting(const sona_renderer *r,
                                   sona_lighting *out);
Parameter and type Meaning
r
const sona_renderer *
The renderer (required)
out
sona_lighting *
Receives the sona_lighting (required)

Returns SONA_OK. Errors SONA_E_ARGUMENT, SONA_E_NOT_FOUND (what each means).

Example:

sona_lighting x_lighting;
sona_lighting_defaults(&x_lighting);
sona_result result = sona_renderer_lighting(r, &x_lighting);
if (result != SONA_OK) printf("sona_renderer_lighting: %s\n",
    sona_result_name(result));

What is set; SONA_E_NOT_FOUND when none is.

sona_draw_lit

sona_result sona_draw_lit(sona_renderer *r, const sona_view *view,
                          const sona_target *target,
                          const sona_draw_options *opts,
                          const sona_lighting *l);
Parameter and type Meaning
r
sona_renderer *
The renderer (required)
view
const sona_view *
The camera, a sona_view (required)
target
const sona_target *
Where the frame lands, a sona_target (required)
opts
const sona_draw_options *
The options, a sona_draw_options (required)
l
const sona_lighting *
A sona_lighting (required)

Returns SONA_OK. Errors SONA_E_ARGUMENT, SONA_E_UNSUPPORTED, SONA_E_MEMORY, SONA_E_CONTEXT (what each means).

sona_draw and sona_snapshot with l in place of the renderer's lighting for this one frame (one per view of a split screen); l NULL: the renderer's own. The checks of sona_renderer_set_lighting.

sona_snapshot_lit

sona_result sona_snapshot_lit(sona_renderer *r, const sona_view *view,
                              uint32_t width, uint32_t height,
                              const sona_draw_options *opts,
                              const sona_lighting *l, uint8_t *rgba_out);
Parameter and type Meaning
r
sona_renderer *
The renderer (required)
view
const sona_view *
The camera, a sona_view (required)
width
uint32_t
The picture's width in pixels
height
uint32_t
The picture's height in pixels
opts
const sona_draw_options *
The options, a sona_draw_options (required)
l
const sona_lighting *
A sona_lighting (required)
rgba_out
uint8_t *
Receives straight RGBA8, row 0 at the top: width * height * 4 bytes (required)

Returns SONA_OK. Errors SONA_E_ARGUMENT, SONA_E_UNSUPPORTED, SONA_E_MEMORY, SONA_E_CONTEXT, SONA_E_READBACK (what each means).

sona_snapshot with l in place of the renderer's lighting for this one picture; l NULL: the renderer's own. The checks of sona_renderer_set_lighting.

sona_environment_defaults

void sona_environment_defaults(sona_environment *e);
Parameter and type Meaning
e
sona_environment *
The sona_environment to fill with the defaults (required)

Returns Nothing.

Example (also in Lighting and shadows):

sona_environment e;
sona_environment_defaults(&e);
/* or SONA_ENV_CUBE with cube_size and cube */
e.kind = SONA_ENV_SH9;
memcpy(e.sh, my_sh, sizeof e.sh);            /* 9 x RGB, radiance */
sona_renderer_set_environment(r, &e);        /* NULL: the built-in cube */

size set; kind BUILTIN, skin_red 1.2 (the built-in's).

sona_renderer_set_environment

sona_result sona_renderer_set_environment(sona_renderer *r,
                                          const sona_environment *e);
Parameter and type Meaning
r
sona_renderer *
The renderer (required)
e
const sona_environment *
A sona_environment (required)

Returns SONA_OK. Errors SONA_E_ARGUMENT, SONA_E_UNSUPPORTED, SONA_E_MEMORY (what each means).

Example (also in Lighting and shadows):

sona_environment e;
sona_environment_defaults(&e);
/* or SONA_ENV_CUBE with cube_size and cube */
e.kind = SONA_ENV_SH9;
memcpy(e.sh, my_sh, sizeof e.sh);            /* 9 x RGB, radiance */
sona_renderer_set_environment(r, &e);        /* NULL: the built-in cube */

From the next lit draw. NULL: the built-in cube. SONA_E_ARGUMENT for a bad size, kind or cube.

sona_environment_irradiance

sona_result sona_environment_irradiance(const sona_environment *e,
                                        float out_sh[27]);
Parameter and type Meaning
e
const sona_environment *
A sona_environment (required)
out_sh
float [27]
Receives nine RGB spherical-harmonic coefficients (required)

Returns SONA_OK. Errors SONA_E_ARGUMENT (what each means).

Example:

sona_environment x_environment;
sona_environment_defaults(&x_environment);
float x_out_sh2[27];
memset(x_out_sh2, 0, sizeof x_out_sh2);
sona_result result = sona_environment_irradiance(&x_environment, x_out_sh2);
if (result != SONA_OK) printf("sona_environment_irradiance: %s\n",
    sona_result_name(result));

The irradiance the shader evaluates for e (cube projected, convolved with the cosine lobe, divided by pi), in the basis above: a host lights its own scene the same way with it. SONA_E_ARGUMENT as above and for BUILTIN.

sona_renderer_set_host_shadow

sona_result sona_renderer_set_host_shadow(sona_renderer *r,
                                          const sona_host_shadow *s);
Parameter and type Meaning
r
sona_renderer *
The renderer (required)
s
const sona_host_shadow *
A sona_host_shadow (required)

Returns SONA_OK. Errors SONA_E_ARGUMENT, SONA_E_UNSUPPORTED, SONA_E_MEMORY (what each means).

Example (also in Lighting and shadows):

sona_host_shadow h;
memset(&h, 0, sizeof h);
h.size = sizeof h;
/* world point to (u, v, depth, w) */
memcpy(h.matrix, my_light_view_proj_to_uv, sizeof h.matrix);
h.bias = 0.0015f;
h.width = h.height = 4096;
/* .gl.texture, .d3d11.srv, .d3d12.resource + .srv_format */
h.u.vk.view = my_depth_view;
h.u.vk.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL;
sona_renderer_set_host_shadow(r, &h);

NULL: none. SONA_E_UNSUPPORTED on a backend without it (Metal), SONA_E_ARGUMENT for a bad size or handle.

sona_renderer_shadow_map

sona_result sona_renderer_shadow_map(const sona_renderer *r,
                                     sona_shadow_map *out);
Parameter and type Meaning
r
const sona_renderer *
The renderer (required)
out
sona_shadow_map *
Receives the sona_shadow_map (required)

Returns SONA_OK. Errors SONA_E_ARGUMENT, SONA_E_NOT_FOUND, SONA_E_UNSUPPORTED (what each means).

Example (also in Lighting and shadows):

sona_shadow_map m;
m.size = sizeof m;
if (sona_renderer_shadow_map(r, &m) == SONA_OK) {
  /* m.matrix: world point (row vector) to (u, v, depth, 1); m.u.vk.view
     etc: the depth texture to sample */
}

SONA_E_NOT_FOUND before a lit frame with shadows; SONA_E_UNSUPPORTED on a backend without it.

This page as Markdown · All the docs as one file (llms-full.txt)