Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix default for buffer binding type #445

Merged
merged 2 commits into from
Nov 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 50 additions & 41 deletions gen/gen.go
Original file line number Diff line number Diff line change
@@ -492,11 +492,17 @@ func (g *Generator) DefaultValue(member ParameterType, isDocString bool) string
return s
}
}

switch {
case member.Pointer != "":
if member.Default != nil {
panic("pointer type should not have a default")
}
return literal("NULL")

// Cases that may have member.Default
case strings.HasPrefix(member.Type, "enum."):
if member.Default == "" {
if member.Default == nil {
if member.Type == "enum.optional_bool" {
// This Undefined is a special one that is not the zero-value, so that
// a stdbool.h bool cast correctly to WGPUOptionalBool; this means we
@@ -508,72 +514,75 @@ func (g *Generator) DefaultValue(member ParameterType, isDocString bool) string
return "_wgpu_ENUM_ZERO_INIT(WGPU" + PascalCase(strings.TrimPrefix(member.Type, "enum.")) + ")"
}
} else {
return ref("WGPU" + PascalCase(strings.TrimPrefix(member.Type, "enum.")) + "_" + PascalCase(member.Default))
return ref("WGPU" + PascalCase(strings.TrimPrefix(member.Type, "enum.")) + "_" + PascalCase(*member.Default))
}
case strings.HasPrefix(member.Type, "bitflag."):
if member.Default == "" {
if member.Default == nil {
return ref("WGPU" + PascalCase(strings.TrimPrefix(member.Type, "bitflag.")) + "_None")
} else {
return ref("WGPU" + PascalCase(strings.TrimPrefix(member.Type, "bitflag.")) + "_" + PascalCase(member.Default))
}
case strings.HasPrefix(member.Type, "struct."):
if member.Optional {
return literal("NULL")
} else {
typ := strings.TrimPrefix(member.Type, "struct.")
return ref("WGPU_" + ConstantCase(typ) + "_INIT")
return ref("WGPU" + PascalCase(strings.TrimPrefix(member.Type, "bitflag.")) + "_" + PascalCase(*member.Default))
}
case strings.HasPrefix(member.Type, "callback."):
typ := strings.TrimPrefix(member.Type, "callback.")
return ref("WGPU_" + ConstantCase(typ) + "_CALLBACK_INFO_INIT")
case strings.HasPrefix(member.Type, "object."):
return literal("NULL")
case strings.HasPrefix(member.Type, "array<"):
return literal("NULL")
case member.Type == "out_string", member.Type == "string_with_default_empty", member.Type == "nullable_string":
return ref("WGPU_STRING_VIEW_INIT")
case member.Type == "uint16", member.Type == "uint32", member.Type == "uint64", member.Type == "usize", member.Type == "int32":
if member.Default == "" {
if member.Default == nil {
return literal("0")
} else if strings.HasPrefix(member.Default, "constant.") {
return ref("WGPU_" + ConstantCase(strings.TrimPrefix(member.Default, "constant.")))
} else if strings.HasPrefix(*member.Default, "constant.") {
return ref("WGPU_" + ConstantCase(strings.TrimPrefix(*member.Default, "constant.")))
} else {
return literal(member.Default)
return literal(*member.Default)
}
case member.Type == "float32":
if member.Default == "" {
if member.Default == nil {
return literal("0.f")
} else if strings.HasPrefix(member.Default, "constant.") {
return ref("WGPU_" + ConstantCase(strings.TrimPrefix(member.Default, "constant.")))
} else if strings.Contains(member.Default, ".") {
return literal(member.Default + "f")
} else if strings.HasPrefix(*member.Default, "constant.") {
return ref("WGPU_" + ConstantCase(strings.TrimPrefix(*member.Default, "constant.")))
} else if strings.Contains(*member.Default, ".") {
return literal(*member.Default + "f")
} else {
return literal(member.Default + ".f")
return literal(*member.Default + ".f")
}
case member.Type == "float64":
if member.Default == "" {
if member.Default == nil {
return literal("0.")
} else if strings.HasPrefix(member.Default, "constant.") {
return ref("WGPU_" + ConstantCase(strings.TrimPrefix(member.Default, "constant.")))
} else if strings.HasPrefix(*member.Default, "constant.") {
return ref("WGPU_" + ConstantCase(strings.TrimPrefix(*member.Default, "constant.")))
} else {
return literal(member.Default)
return literal(*member.Default)
}
case member.Type == "bool":
if member.Default == "" {
if member.Default == nil {
return literal("0")
} else if strings.HasPrefix(member.Default, "constant.") {
return ref("WGPU_" + ConstantCase(strings.TrimPrefix(member.Default, "constant.")))
} else if member.Default == "true" {
} else if strings.HasPrefix(*member.Default, "constant.") {
return ref("WGPU_" + ConstantCase(strings.TrimPrefix(*member.Default, "constant.")))
} else if *member.Default == "true" {
return literal("1")
} else if member.Default == "false" {
} else if *member.Default == "false" {
return literal("0")
} else {
return member.Default
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 {
typ := strings.TrimPrefix(member.Type, "struct.")
return ref("WGPU_" + ConstantCase(typ) + "_INIT")
}
case strings.HasPrefix(member.Type, "callback."):
typ := strings.TrimPrefix(member.Type, "callback.")
return ref("WGPU_" + ConstantCase(typ) + "_CALLBACK_INFO_INIT")
case strings.HasPrefix(member.Type, "object."):
return literal("NULL")
case strings.HasPrefix(member.Type, "array<"):
return literal("NULL")
case member.Type == "out_string", member.Type == "string_with_default_empty", member.Type == "nullable_string":
return ref("WGPU_STRING_VIEW_INIT")
case member.Type == "c_void":
return literal("NULL")
default:
panic("invalid prefix: " + member.Type + " in member " + member.Name)
return ""
}
}
2 changes: 1 addition & 1 deletion gen/yml.go
Original file line number Diff line number Diff line change
@@ -74,7 +74,7 @@ type ParameterType struct {
Pointer PointerType `yaml:"pointer"`
Optional bool `yaml:"optional"`
Namespace string `yaml:"namespace"`
Default string `yaml:"default"`
Default *string `yaml:"default"`
}

type Callback struct {
4 changes: 2 additions & 2 deletions webgpu.h
Original file line number Diff line number Diff line change
@@ -1667,7 +1667,7 @@ typedef struct WGPUBlendComponent {
typedef struct WGPUBufferBindingLayout {
WGPUChainedStruct const * nextInChain;
/**
* The `INIT` macro sets this to @ref WGPUBufferBindingType_Uniform.
* The `INIT` macro sets this to @ref WGPUBufferBindingType_BindingNotUsed.
*/
WGPUBufferBindingType type;
/**
@@ -1685,7 +1685,7 @@ typedef struct WGPUBufferBindingLayout {
*/
#define WGPU_BUFFER_BINDING_LAYOUT_INIT _wgpu_MAKE_INIT_STRUCT(WGPUBufferBindingLayout, { \
/*.nextInChain=*/NULL _wgpu_COMMA \
/*.type=*/WGPUBufferBindingType_Uniform _wgpu_COMMA \
/*.type=*/WGPUBufferBindingType_BindingNotUsed _wgpu_COMMA \
/*.hasDynamicOffset=*/0 _wgpu_COMMA \
/*.minBindingSize=*/0 _wgpu_COMMA \
})
6 changes: 1 addition & 5 deletions webgpu.yml
Original file line number Diff line number Diff line change
@@ -1618,22 +1618,18 @@ structs:
doc: |
TODO
type: struct.buffer_binding_layout
default: constant.binding_not_used
- name: sampler
doc: |
TODO
type: struct.sampler_binding_layout
default: constant.binding_not_used
- name: texture
doc: |
TODO
type: struct.texture_binding_layout
default: constant.binding_not_used
- name: storage_texture
doc: |
TODO
type: struct.storage_texture_binding_layout
default: constant.binding_not_used
- name: blend_component
doc: |
TODO
@@ -1676,7 +1672,7 @@ structs:
doc: |
TODO
type: enum.buffer_binding_type
default: uniform
default: binding_not_used
- name: has_dynamic_offset
doc: |
TODO