Skip to content

Commit

Permalink
Define depthWriteEnabled as WGPUOptionalBool
Browse files Browse the repository at this point in the history
  • Loading branch information
beaufortfrancois committed Jul 8, 2024
1 parent effd3c0 commit 3628718
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 13 deletions.
1 change: 1 addition & 0 deletions gen/cheader.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
{{- if eq .Name "webgpu"}}
typedef uint32_t WGPUFlags;
typedef uint32_t WGPUBool;
typedef uint32_t WGPUOptionalBool;
{{ end}}
{{- range .Typedefs}}
{{- MComment .Doc 0}}
Expand Down
27 changes: 15 additions & 12 deletions gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ func (g *Generator) Gen(dst io.Writer) error {
"ArrayType": func(typ string, pointer PointerType) string {
matches := arrayTypeRegexp.FindStringSubmatch(typ)
if len(matches) == 2 {
return g.CType(matches[1], pointer, "")
return g.CType(matches[1], false, pointer, "")
}
return ""
},
"Singularize": Singularize,
"IsLast": func(i int, s any) bool { return i == reflect.ValueOf(s).Len()-1 },
"FunctionReturns": func(f Function) string {
if f.Returns != nil {
return g.CType(f.Returns.Type, f.Returns.Pointer, "")
return g.CType(f.Returns.Type, false, f.Returns.Pointer, "")
}
return "void"
},
Expand Down Expand Up @@ -109,7 +109,7 @@ func (g *Generator) CValue(s string) (string, error) {
}
}

func (g *Generator) CType(typ string, pointerType PointerType, suffix string) string {
func (g *Generator) CType(typ string, optional bool, pointerType PointerType, suffix string) string {
appendModifiers := func(s string, pointerType PointerType) string {
var sb strings.Builder
sb.WriteString(s)
Expand All @@ -123,6 +123,9 @@ func (g *Generator) CType(typ string, pointerType PointerType, suffix string) st
}
switch typ {
case "bool":
if optional {
return appendModifiers("WGPUOptionalBool", pointerType)
}
return appendModifiers("WGPUBool", pointerType)
case "string":
return appendModifiers("char", PointerTypeImmutable)
Expand Down Expand Up @@ -186,7 +189,7 @@ func (g *Generator) FunctionArgs(f Function, o *Object) string {
}
}
for i, arg := range f.Args {
if arg.Optional {
if arg.Optional && arg.Type != "bool" {
sb.WriteString("WGPU_NULLABLE ")
}
var typeSuffix string
Expand All @@ -198,9 +201,9 @@ func (g *Generator) FunctionArgs(f Function, o *Object) string {
matches := arrayTypeRegexp.FindStringSubmatch(arg.Type)
if len(matches) == 2 {
fmt.Fprintf(sb, "size_t %sCount, ", CamelCase(Singularize(arg.Name)))
fmt.Fprintf(sb, "%s %s", g.CType(matches[1], arg.Pointer, typeSuffix), CamelCase(arg.Name))
fmt.Fprintf(sb, "%s %s", g.CType(matches[1], arg.Optional, arg.Pointer, typeSuffix), CamelCase(arg.Name))
} else {
fmt.Fprintf(sb, "%s %s", g.CType(arg.Type, arg.Pointer, typeSuffix), CamelCase(arg.Name))
fmt.Fprintf(sb, "%s %s", g.CType(arg.Type, arg.Optional, arg.Pointer, typeSuffix), CamelCase(arg.Name))
}
if i != len(f.Args)-1 {
sb.WriteString(", ")
Expand All @@ -221,7 +224,7 @@ func (g *Generator) FunctionArgs(f Function, o *Object) string {
func (g *Generator) CallbackArgs(f Function) string {
sb := &strings.Builder{}
for _, arg := range f.ReturnsAsync {
if arg.Optional {
if arg.Optional && arg.Type != "bool" {
sb.WriteString("WGPU_NULLABLE ")
}
var typeSuffix string
Expand All @@ -237,9 +240,9 @@ func (g *Generator) CallbackArgs(f Function) string {
matches := arrayTypeRegexp.FindStringSubmatch(arg.Type)
if len(matches) == 2 {
fmt.Fprintf(sb, "size_t %sCount, ", CamelCase(Singularize(arg.Name)))
fmt.Fprintf(sb, "%s%s %s, ", structPrefix, g.CType(matches[1], arg.Pointer, typeSuffix), CamelCase(arg.Name))
fmt.Fprintf(sb, "%s%s %s, ", structPrefix, g.CType(matches[1], arg.Optional, arg.Pointer, typeSuffix), CamelCase(arg.Name))
} else {
fmt.Fprintf(sb, "%s%s %s, ", structPrefix, g.CType(arg.Type, arg.Pointer, typeSuffix), CamelCase(arg.Name))
fmt.Fprintf(sb, "%s%s %s, ", structPrefix, g.CType(arg.Type, arg.Optional, arg.Pointer, typeSuffix), CamelCase(arg.Name))
}
}
sb.WriteString("WGPU_NULLABLE void * userdata")
Expand Down Expand Up @@ -310,7 +313,7 @@ func (g *Generator) BitflagValue(b Bitflag, entryIndex int) (string, error) {
func (g *Generator) StructMember(s Struct, memberIndex int) (string, error) {
member := s.Members[memberIndex]
sb := &strings.Builder{}
if member.Optional {
if member.Optional && member.Type != "bool" {
sb.WriteString("WGPU_NULLABLE ")
}
var typeSuffix string
Expand All @@ -322,9 +325,9 @@ func (g *Generator) StructMember(s Struct, memberIndex int) (string, error) {
matches := arrayTypeRegexp.FindStringSubmatch(member.Type)
if len(matches) == 2 {
fmt.Fprintf(sb, "size_t %sCount;\n", CamelCase(Singularize(member.Name)))
fmt.Fprintf(sb, " %s %s;", g.CType(matches[1], member.Pointer, typeSuffix), CamelCase(member.Name))
fmt.Fprintf(sb, " %s %s;", g.CType(matches[1], member.Optional, member.Pointer, typeSuffix), CamelCase(member.Name))
} else {
fmt.Fprintf(sb, "%s %s;", g.CType(member.Type, member.Pointer, typeSuffix), CamelCase(member.Name))
fmt.Fprintf(sb, "%s %s;", g.CType(member.Type, member.Optional, member.Pointer, typeSuffix), CamelCase(member.Name))
}
return sb.String(), nil
}
3 changes: 2 additions & 1 deletion webgpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
*/
typedef uint32_t WGPUFlags;
typedef uint32_t WGPUBool;
typedef uint32_t WGPUOptionalBool;


/** @} */
Expand Down Expand Up @@ -1244,7 +1245,7 @@ typedef struct WGPUComputePassDescriptor {
typedef struct WGPUDepthStencilState {
WGPUChainedStruct const * nextInChain;
WGPUTextureFormat format;
WGPUBool depthWriteEnabled;
WGPUOptionalBool depthWriteEnabled;
WGPUCompareFunction depthCompare;
WGPUStencilFaceState stencilFront;
WGPUStencilFaceState stencilBack;
Expand Down
1 change: 1 addition & 0 deletions webgpu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1760,6 +1760,7 @@ structs:
doc: |
TODO
type: bool
optional: true
- name: depth_compare
doc: |
TODO
Expand Down

0 comments on commit 3628718

Please sign in to comment.