Skip to content

Commit

Permalink
Improve sentinel values documentation (#440)
Browse files Browse the repository at this point in the history
  • Loading branch information
kainino0x authored Nov 26, 2024
1 parent 5538c58 commit 4b95e76
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
31 changes: 19 additions & 12 deletions doc/articles/SentinelValues.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Sentinel Values {#SentinelValues}

## Undefined
## Undefined and Null

Since WebGPU is defined first as a JavaScript API, it uses the JavaScript value
`undefined` in many places to indicate the lack of a value.
Expand All @@ -14,7 +14,8 @@ It may also be used in functions/methods. For example, `GPUBuffer`'s
can be called equivalently as `b.getMappedRange()`, `b.getMappedRange(0)`,
`b.getMappedRange(undefined)`, or `b.getMappedRange(undefined, undefined)`.

To represent `undefined` in C, `webgpu.h` uses `*_UNDEFINED` sentinel numeric values
To represent `undefined` in C, `webgpu.h` uses `NULL` where possible (anything
behind a pointer, including objects), `*_UNDEFINED` sentinel numeric values
(generally `UINT32_MAX`, etc.) and `*_Undefined` enum values (generally `0`).

The place that uses the type will define what to do with an undefined value.
Expand All @@ -26,13 +27,19 @@ It may be:
(usually this is either a special value or it has more complex defaulting,
for example depending on other values).

## Other sentinel values

Undefined values are also be used in C-specific ways in place of WebIDL's
more flexible typing:

- \ref WGPUVertexStepMode_VertexBufferNotUsed
- \ref WGPUBufferBindingType_BindingNotUsed
- \ref WGPUSamplerBindingType_BindingNotUsed
- \ref WGPUTextureSampleType_BindingNotUsed
- \ref WGPUStorageTextureAccess_BindingNotUsed
## C-Specific Sentinel Values

Undefined and null values are also used in C-specific ways in place of
WebIDL's more flexible typing:

- \ref WGPUStringView has a special null value
- \ref WGPUFuture has a special null value
- Special cases to indicate the parent struct is null, avoiding extra layers of
pointers just for nullability:
- \ref WGPUVertexBufferLayout::stepMode = \ref WGPUVertexStepMode_VertexBufferNotUsed
- \ref WGPUBufferBindingLayout::type = \ref WGPUBufferBindingType_BindingNotUsed
- \ref WGPUSamplerBindingLayout::type = \ref WGPUSamplerBindingType_BindingNotUsed
- \ref WGPUTextureBindingLayout::sampleType = \ref WGPUTextureSampleType_BindingNotUsed
- \ref WGPUStorageTextureBindingLayout::access = \ref WGPUStorageTextureAccess_BindingNotUsed
- \ref WGPURenderPassColorAttachment::view = `NULL`
- \ref WGPUColorTargetState::format = \ref WGPUTextureFormat_Undefined
3 changes: 3 additions & 0 deletions webgpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -3668,6 +3668,9 @@ typedef struct WGPUInstanceDescriptor {
typedef struct WGPURenderPassColorAttachment {
WGPUChainedStruct const * nextInChain;
/**
* If `NULL`, indicates a hole in the parent
* @ref WGPURenderPassDescriptor::colorAttachments array.
*
* The `INIT` macro sets this to `NULL`.
*/
WGPU_NULLABLE WGPUTextureView view;
Expand Down
3 changes: 2 additions & 1 deletion webgpu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2374,7 +2374,8 @@ structs:
members:
- name: view
doc: |
TODO
If `NULL`, indicates a hole in the parent
@ref WGPURenderPassDescriptor::colorAttachments array.
type: object.texture_view
optional: true
- name: depth_slice
Expand Down

0 comments on commit 4b95e76

Please sign in to comment.