Skip to content

Commit

Permalink
Merge pull request #1111 from floooh/issue1037_bindings_cleanup
Browse files Browse the repository at this point in the history
sokol_gfx.h resource bindings cleanup (issue #1037)
  • Loading branch information
floooh authored Nov 7, 2024
2 parents 76ded2d + 71d744d commit eed7517
Show file tree
Hide file tree
Showing 14 changed files with 9,188 additions and 6,069 deletions.
90 changes: 90 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,95 @@
## Updates

### 07-Nov-2024

The sokol-gfx 'bindings cleanup update'. This is a breaking change.

Please read this blog post to get an idea what the update is about
and how existing code needs to be changed:

https://floooh.github.io/2024/11/04/sokol-fall-2024-update.html

TL;DR:

- sokol-shdc input shader sources now require to annotate uniform blocks,
textures, samplers and storage buffers with `layout(binding=N)` where `N`
directly maps to sokol-gfx bindslots
- the concept of 'shader stages' has mostly been removed from the sokol-gfx API
- in the `sg_bindings` struct, the nested per-shader-stage binding arrays have
been replaced with unified binding arrays
- in the function `sg_apply_uniforms()`, the shader stage parameter has been
removed
- only relevant if you don't use sokol-shdc: the interior of the `sg_shader_desc`
struct has been changed according to the new binding model, and additional
information must be provided for each resource binding:
- what shader change the binding appears in
- backend 3D API specific resource bindslots to map sokol-gfx
bindslots to 3D API bindslots

The update is implemented in PR https://github.com/floooh/sokol/pull/1111.

The detailed API changes in the sokol_gfx.h API:

- the `sg_apply_uniforms()` function no longer has a shader stage parameter:
```c
void sg_apply_uniforms(int ub_slot, const sg_range* data);
```
- the `sg_bindings` struct interior no longer separates resource
bindings by shader stages:
```c
typedef struct sg_bindings {
uint32_t _start_canary;
sg_buffer vertex_buffers[SG_MAX_VERTEXBUFFER_BINDSLOTS];
int vertex_buffer_offsets[SG_MAX_VERTEXBUFFER_BINDSLOTS];
sg_buffer index_buffer;
int index_buffer_offset;
sg_image images[SG_MAX_IMAGE_BINDSLOTS];
sg_sampler samplers[SG_MAX_SAMPLER_BINDSLOTS];
sg_buffer storage_buffers[SG_MAX_STORAGEBUFFER_BINDSLOTS];
uint32_t _end_canary;
} sg_bindings;
```
- some public constants starting with `SG_NUM_*` or `SG_MAX_*`
have been removed or renamed (those typically shouldn't show up
in user code)
- similar to the `sg_bindings` struct, the bindings reflection is no
longer split between shader stages:
```c
typedef struct sg_shader_desc {
uint32_t _start_canary;
sg_shader_function vertex_func;
sg_shader_function fragment_func;
sg_shader_vertex_attr attrs[SG_MAX_VERTEX_ATTRIBUTES];
sg_shader_uniform_block uniform_blocks[SG_MAX_UNIFORMBLOCK_BINDSLOTS];
sg_shader_storage_buffer storage_buffers[SG_MAX_STORAGEBUFFER_BINDSLOTS];
sg_shader_image images[SG_MAX_IMAGE_BINDSLOTS];
sg_shader_sampler samplers[SG_MAX_SAMPLER_BINDSLOTS];
sg_shader_image_sampler_pair image_sampler_pairs[SG_MAX_IMAGE_SAMPLER_PAIRS];
const char* label;
uint32_t _end_canary;
} sg_shader_desc;
```
Behaviour changes:
- Resource bindings can now have gaps, and validation of `sg_apply_bindings()`
has been relaxed to allow bindslots in `sg_bindings` to be occupied even
if those bindings are not used by the current shader. This allows to use the
same `sg_bindings` struct for different but related shader variants.
- Likewise, uniform block bindslots can now have gaps (but currently it's still
an error trying to apply uniform block data for a bindslot that's not used
by the current shader)
- In debug mode, `sg_draw()` now checks that `sg_apply_bindings()` and/or
`sg_apply_uniforms()` has been called after `sg_apply_pipeline()` when required.
- Lots of new validation checks for the new reflection information in `sg_shader_desc`.
Drive by fixes:
- the sokol_gfx.h WebGPU backend is now compatible again with the latest
Google Dawn library (Chrome's native WebGPU implementation)
- the [fips-dawn glue repository](https://github.com/fips-libs/fips-dawn) has been fixed to work with the latest
Dawn build system changes (still only tested on macOS though)
### 24-Oct-2024
- sokol_nuklear.h: Merged https://github.com/floooh/sokol/pull/1138 which
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

# Sokol

[**See what's new**](https://github.com/floooh/sokol/blob/master/CHANGELOG.md) (**17-Sep-2024** sokol_app.h: clipboard support for Linux)
[**See what's new**](https://github.com/floooh/sokol/blob/master/CHANGELOG.md) (**07-Nov-2024** sokol_gfx.h: the 'bindings cleanup' update

[![Build](/../../actions/workflows/main.yml/badge.svg)](/../../actions/workflows/main.yml) [![Bindings](/../../actions/workflows/gen_bindings.yml/badge.svg)](/../../actions/workflows/gen_bindings.yml) [![build](https://github.com/floooh/sokol-zig/actions/workflows/main.yml/badge.svg)](https://github.com/floooh/sokol-zig/actions/workflows/main.yml) [![build](https://github.com/floooh/sokol-nim/actions/workflows/main.yml/badge.svg)](https://github.com/floooh/sokol-nim/actions/workflows/main.yml) [![Odin](https://github.com/floooh/sokol-odin/actions/workflows/main.yml/badge.svg)](https://github.com/floooh/sokol-odin/actions/workflows/main.yml)[![Rust](https://github.com/floooh/sokol-rust/actions/workflows/main.yml/badge.svg)](https://github.com/floooh/sokol-rust/actions/workflows/main.yml)[![Dlang](https://github.com/kassane/sokol-d/actions/workflows/build.yml/badge.svg)](https://github.com/kassane/sokol-d/actions/workflows/build.yml)

Expand Down Expand Up @@ -155,8 +155,8 @@ static void init(void) {
.shader = sg_make_shader(triangle_shader_desc(sg_query_backend())),
.layout = {
.attrs = {
[ATTR_vs_position].format = SG_VERTEXFORMAT_FLOAT3,
[ATTR_vs_color0].format = SG_VERTEXFORMAT_FLOAT4
[ATTR_triangle_position].format = SG_VERTEXFORMAT_FLOAT3,
[ATTR_triangle_color0].format = SG_VERTEXFORMAT_FLOAT4
}
},
});
Expand Down
4 changes: 1 addition & 3 deletions bindgen/gen_d.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@
'sgl_error': 'sgl_get_error', # 'error' is reserved in Dlang
'sgl_deg': 'sgl_as_degrees',
'sgl_rad': 'sgl_as_radians',
'sg_context_desc.color_format': 'int',
'sg_context_desc.depth_format': 'int',
'sg_apply_uniforms.ub_index': 'uint32_t',
'sg_apply_uniforms.ub_slot': 'uint32_t',
'sg_draw.base_element': 'uint32_t',
'sg_draw.num_elements': 'uint32_t',
'sg_draw.num_instances': 'uint32_t',
Expand Down
9 changes: 1 addition & 8 deletions bindgen/gen_rust.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"type": "_type",
"ref": "_ref",

"sg_apply_uniforms.ub_index": "uintptr_t",
"sg_apply_uniforms.ub_slot": "uintptr_t",
"sg_draw.base_element": "uintptr_t",
"sg_draw.num_elements": "uintptr_t",
"sg_draw.num_instances": "uintptr_t",
Expand All @@ -83,13 +83,6 @@
"sapp_keycode::SAPP_KEYCODE_7": "SAPP_KEYCODE_NUM7",
"sapp_keycode::SAPP_KEYCODE_8": "SAPP_KEYCODE_NUM8",
"sapp_keycode::SAPP_KEYCODE_9": "SAPP_KEYCODE_NUM9",

# "sgl_error": "sgl_get_error", # 'error' is reserved in zig
# "sgl_deg": "sgl_as_degrees",
# "sgl_rad": "sgl_as_radians",
# "sg_context_desc.color_format": "int",
# "SGL_NO_ERROR": "SGL_ERROR_NO_ERROR",
# "sg_context_desc.depth_format": "int",
}

prim_types = {
Expand Down
2 changes: 1 addition & 1 deletion bindgen/gen_zig.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
'sgl_error': 'sgl_get_error', # 'error' is reserved in Zig
'sgl_deg': 'sgl_as_degrees',
'sgl_rad': 'sgl_as_radians',
'sg_apply_uniforms.ub_index': 'uint32_t',
'sg_apply_uniforms.ub_slot': 'uint32_t',
'sg_draw.base_element': 'uint32_t',
'sg_draw.num_elements': 'uint32_t',
'sg_draw.num_instances': 'uint32_t',
Expand Down
Loading

0 comments on commit eed7517

Please sign in to comment.