Skip to content

Commit

Permalink
Proposal: Allow use of WGPU_BUFFER_BINDING_LAYOUT_INIT/etc for defaul…
Browse files Browse the repository at this point in the history
…ting (#450)
  • Loading branch information
kainino0x authored Dec 9, 2024
1 parent 1d18dcc commit 60ea8a6
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 26 deletions.
2 changes: 2 additions & 0 deletions gen/cheader.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,15 @@
#define _wgpu_COMMA ,
#if defined(__cplusplus)
# define _wgpu_ENUM_ZERO_INIT(type) type(0)
# define _wgpu_STRUCT_ZERO_INIT {}
# if __cplusplus >= 201103L
# define _wgpu_MAKE_INIT_STRUCT(type, value) (type value)
# else
# define _wgpu_MAKE_INIT_STRUCT(type, value) value
# endif
#else
# define _wgpu_ENUM_ZERO_INIT(type) (type)0
# define _wgpu_STRUCT_ZERO_INIT {0}
# if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
# define _wgpu_MAKE_INIT_STRUCT(type, value) ((type) value)
# else
Expand Down
18 changes: 13 additions & 5 deletions gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -581,17 +581,25 @@ func (g *Generator) DefaultValue(member ParameterType, isDocString bool) string
} else {
return *member.Default
}
case member.Default != nil:
panic(fmt.Errorf("type %s should not have a default", member.Type))

// Cases that should not have member.Default
case strings.HasPrefix(member.Type, "struct."):
if member.Optional {
return literal("NULL")
} else {
} else if member.Default == nil {
typ := strings.TrimPrefix(member.Type, "struct.")
return ref("WGPU_" + ConstantCase(typ) + g.ConstantExtSuffix() + "_INIT")
} else if *member.Default == "zero" {
if isDocString {
return "zero (which sets the entry to `BindingNotUsed`)"
} else {
return literal("_wgpu_STRUCT_ZERO_INIT")
}
} else {
panic("unknown default for struct type")
}
case member.Default != nil:
panic(fmt.Errorf("type %s should not have a default", member.Type))

// Cases that should not have member.Default
case strings.HasPrefix(member.Type, "callback."):
typ := strings.TrimPrefix(member.Type, "callback.")
return ref("WGPU_" + ConstantCase(typ) + "_CALLBACK_INFO" + g.ConstantExtSuffix() + "_INIT")
Expand Down
2 changes: 1 addition & 1 deletion schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@
"number",
"boolean"
],
"description": "Default value assigned to this parameter when using initializer macro"
"description": "Default value assigned to this parameter when using initializer macro. Special context-dependent values include constant names (`constant.*`), bitflag names (unprefixed), and `zero` for struct-zero-init (where zero-init is known to have the desired result)."
}
},
"required": [
Expand Down
34 changes: 18 additions & 16 deletions webgpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,15 @@
#define _wgpu_COMMA ,
#if defined(__cplusplus)
# define _wgpu_ENUM_ZERO_INIT(type) type(0)
# define _wgpu_STRUCT_ZERO_INIT {}
# if __cplusplus >= 201103L
# define _wgpu_MAKE_INIT_STRUCT(type, value) (type value)
# else
# define _wgpu_MAKE_INIT_STRUCT(type, value) value
# endif
#else
# define _wgpu_ENUM_ZERO_INIT(type) (type)0
# define _wgpu_STRUCT_ZERO_INIT {0}
# if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
# define _wgpu_MAKE_INIT_STRUCT(type, value) ((type) value)
# else
Expand Down Expand Up @@ -1679,7 +1681,7 @@ typedef struct WGPUBufferBindingLayout {
* If set to @ref WGPUBufferBindingType_Undefined,
* [defaults](@ref SentinelValues) to @ref WGPUBufferBindingType_Uniform.
*
* The `INIT` macro sets this to @ref WGPUBufferBindingType_BindingNotUsed.
* The `INIT` macro sets this to @ref WGPUBufferBindingType_Undefined.
*/
WGPUBufferBindingType type;
/**
Expand All @@ -1697,7 +1699,7 @@ typedef struct WGPUBufferBindingLayout {
*/
#define WGPU_BUFFER_BINDING_LAYOUT_INIT _wgpu_MAKE_INIT_STRUCT(WGPUBufferBindingLayout, { \
/*.nextInChain=*/NULL _wgpu_COMMA \
/*.type=*/WGPUBufferBindingType_BindingNotUsed _wgpu_COMMA \
/*.type=*/WGPUBufferBindingType_Undefined _wgpu_COMMA \
/*.hasDynamicOffset=*/0 _wgpu_COMMA \
/*.minBindingSize=*/0 _wgpu_COMMA \
})
Expand Down Expand Up @@ -2580,7 +2582,7 @@ typedef struct WGPUSamplerBindingLayout {
* If set to @ref WGPUSamplerBindingType_Undefined,
* [defaults](@ref SentinelValues) to @ref WGPUSamplerBindingType_Filtering.
*
* The `INIT` macro sets this to @ref WGPUSamplerBindingType_BindingNotUsed.
* The `INIT` macro sets this to @ref WGPUSamplerBindingType_Undefined.
*/
WGPUSamplerBindingType type;
} WGPUSamplerBindingLayout WGPU_STRUCTURE_ATTRIBUTE;
Expand All @@ -2590,7 +2592,7 @@ typedef struct WGPUSamplerBindingLayout {
*/
#define WGPU_SAMPLER_BINDING_LAYOUT_INIT _wgpu_MAKE_INIT_STRUCT(WGPUSamplerBindingLayout, { \
/*.nextInChain=*/NULL _wgpu_COMMA \
/*.type=*/WGPUSamplerBindingType_BindingNotUsed _wgpu_COMMA \
/*.type=*/WGPUSamplerBindingType_Undefined _wgpu_COMMA \
})

/**
Expand Down Expand Up @@ -2807,7 +2809,7 @@ typedef struct WGPUStorageTextureBindingLayout {
* If set to @ref WGPUStorageTextureAccess_Undefined,
* [defaults](@ref SentinelValues) to @ref WGPUStorageTextureAccess_WriteOnly.
*
* The `INIT` macro sets this to @ref WGPUStorageTextureAccess_BindingNotUsed.
* The `INIT` macro sets this to @ref WGPUStorageTextureAccess_Undefined.
*/
WGPUStorageTextureAccess access;
/**
Expand All @@ -2828,7 +2830,7 @@ typedef struct WGPUStorageTextureBindingLayout {
*/
#define WGPU_STORAGE_TEXTURE_BINDING_LAYOUT_INIT _wgpu_MAKE_INIT_STRUCT(WGPUStorageTextureBindingLayout, { \
/*.nextInChain=*/NULL _wgpu_COMMA \
/*.access=*/WGPUStorageTextureAccess_BindingNotUsed _wgpu_COMMA \
/*.access=*/WGPUStorageTextureAccess_Undefined _wgpu_COMMA \
/*.format=*/WGPUTextureFormat_Undefined _wgpu_COMMA \
/*.viewDimension=*/WGPUTextureViewDimension_Undefined _wgpu_COMMA \
})
Expand Down Expand Up @@ -3304,7 +3306,7 @@ typedef struct WGPUTextureBindingLayout {
* If set to @ref WGPUTextureSampleType_Undefined,
* [defaults](@ref SentinelValues) to @ref WGPUTextureSampleType_Float.
*
* The `INIT` macro sets this to @ref WGPUTextureSampleType_BindingNotUsed.
* The `INIT` macro sets this to @ref WGPUTextureSampleType_Undefined.
*/
WGPUTextureSampleType sampleType;
/**
Expand All @@ -3325,7 +3327,7 @@ typedef struct WGPUTextureBindingLayout {
*/
#define WGPU_TEXTURE_BINDING_LAYOUT_INIT _wgpu_MAKE_INIT_STRUCT(WGPUTextureBindingLayout, { \
/*.nextInChain=*/NULL _wgpu_COMMA \
/*.sampleType=*/WGPUTextureSampleType_BindingNotUsed _wgpu_COMMA \
/*.sampleType=*/WGPUTextureSampleType_Undefined _wgpu_COMMA \
/*.viewDimension=*/WGPUTextureViewDimension_Undefined _wgpu_COMMA \
/*.multisampled=*/0 _wgpu_COMMA \
})
Expand Down Expand Up @@ -3473,19 +3475,19 @@ typedef struct WGPUBindGroupLayoutEntry {
*/
WGPUShaderStage visibility;
/**
* The `INIT` macro sets this to @ref WGPU_BUFFER_BINDING_LAYOUT_INIT.
* The `INIT` macro sets this to zero (which sets the entry to `BindingNotUsed`).
*/
WGPUBufferBindingLayout buffer;
/**
* The `INIT` macro sets this to @ref WGPU_SAMPLER_BINDING_LAYOUT_INIT.
* The `INIT` macro sets this to zero (which sets the entry to `BindingNotUsed`).
*/
WGPUSamplerBindingLayout sampler;
/**
* The `INIT` macro sets this to @ref WGPU_TEXTURE_BINDING_LAYOUT_INIT.
* The `INIT` macro sets this to zero (which sets the entry to `BindingNotUsed`).
*/
WGPUTextureBindingLayout texture;
/**
* The `INIT` macro sets this to @ref WGPU_STORAGE_TEXTURE_BINDING_LAYOUT_INIT.
* The `INIT` macro sets this to zero (which sets the entry to `BindingNotUsed`).
*/
WGPUStorageTextureBindingLayout storageTexture;
} WGPUBindGroupLayoutEntry WGPU_STRUCTURE_ATTRIBUTE;
Expand All @@ -3497,10 +3499,10 @@ typedef struct WGPUBindGroupLayoutEntry {
/*.nextInChain=*/NULL _wgpu_COMMA \
/*.binding=*/0 _wgpu_COMMA \
/*.visibility=*/WGPUShaderStage_None _wgpu_COMMA \
/*.buffer=*/WGPU_BUFFER_BINDING_LAYOUT_INIT _wgpu_COMMA \
/*.sampler=*/WGPU_SAMPLER_BINDING_LAYOUT_INIT _wgpu_COMMA \
/*.texture=*/WGPU_TEXTURE_BINDING_LAYOUT_INIT _wgpu_COMMA \
/*.storageTexture=*/WGPU_STORAGE_TEXTURE_BINDING_LAYOUT_INIT _wgpu_COMMA \
/*.buffer=*/_wgpu_STRUCT_ZERO_INIT _wgpu_COMMA \
/*.sampler=*/_wgpu_STRUCT_ZERO_INIT _wgpu_COMMA \
/*.texture=*/_wgpu_STRUCT_ZERO_INIT _wgpu_COMMA \
/*.storageTexture=*/_wgpu_STRUCT_ZERO_INIT _wgpu_COMMA \
})

/**
Expand Down
24 changes: 20 additions & 4 deletions webgpu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1614,18 +1614,34 @@ structs:
doc: |
TODO
type: struct.buffer_binding_layout
# Use struct-zero-init here to get .type=BindingNotUsed (0), instead of
# Undefined which is the default in WGPU_BUFFER_BINDING_LAYOUT_INIT.
# Zero-init is conveniently correct for the rest of the fields, too.
default: zero
- name: sampler
doc: |
TODO
type: struct.sampler_binding_layout
# Use struct-zero-init here to get .type=BindingNotUsed (0), instead of
# Undefined which is the default in WGPU_SAMPLER_BINDING_LAYOUT_INIT.
# Zero-init is conveniently correct for the rest of the fields, too.
default: zero
- name: texture
doc: |
TODO
type: struct.texture_binding_layout
# Use struct-zero-init here to get .sampleType=BindingNotUsed (0), instead of
# Undefined which is the default in WGPU_TEXTURE_BINDING_LAYOUT_INIT.
# Zero-init is conveniently correct for the rest of the fields, too.
default: zero
- name: storage_texture
doc: |
TODO
type: struct.storage_texture_binding_layout
# Use struct-zero-init here to get .access=BindingNotUsed (0), instead of
# Undefined which is the default in WGPU_STORAGE_TEXTURE_BINDING_LAYOUT_INIT.
# Zero-init is conveniently correct for the rest of the fields, too.
default: zero
- name: blend_component
doc: |
TODO
Expand Down Expand Up @@ -1672,7 +1688,7 @@ structs:
If set to @ref WGPUBufferBindingType_Undefined,
[defaults](@ref SentinelValues) to @ref WGPUBufferBindingType_Uniform.
type: enum.buffer_binding_type
default: binding_not_used
default: undefined
- name: has_dynamic_offset
doc: |
TODO
Expand Down Expand Up @@ -2577,7 +2593,7 @@ structs:
If set to @ref WGPUSamplerBindingType_Undefined,
[defaults](@ref SentinelValues) to @ref WGPUSamplerBindingType_Filtering.
type: enum.sampler_binding_type
default: binding_not_used
default: undefined
- name: sampler_descriptor
doc: |
TODO
Expand Down Expand Up @@ -2719,7 +2735,7 @@ structs:
If set to @ref WGPUStorageTextureAccess_Undefined,
[defaults](@ref SentinelValues) to @ref WGPUStorageTextureAccess_WriteOnly.
type: enum.storage_texture_access
default: binding_not_used
default: undefined
- name: format
doc: |
TODO
Expand Down Expand Up @@ -2990,7 +3006,7 @@ structs:
If set to @ref WGPUTextureSampleType_Undefined,
[defaults](@ref SentinelValues) to @ref WGPUTextureSampleType_Float.
type: enum.texture_sample_type
default: binding_not_used
default: undefined
- name: view_dimension
doc: |
If set to @ref WGPUTextureViewDimension_Undefined,
Expand Down

0 comments on commit 60ea8a6

Please sign in to comment.