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

Add INIT macros with default values #422

Merged
merged 24 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
8753009
Add INIT macros with default values
eliemichel Nov 16, 2024
384b717
Fix docstring trimming issue for enums
eliemichel Nov 16, 2024
08ce163
Minor changes following review
eliemichel Nov 20, 2024
ce11135
Use BindingNotUsed by default
eliemichel Nov 20, 2024
0e4241a
Initialize STypes
eliemichel Nov 20, 2024
0d12155
Add default values to docstrings
eliemichel Nov 20, 2024
243b808
Merge branch 'main' into eliemichel/default-values
eliemichel Nov 20, 2024
b78dac1
Fix typo in default value
eliemichel Nov 20, 2024
f9089c7
Add test for initializers and fix issues in default values
eliemichel Nov 21, 2024
c587ff8
Further remove unused-variable warning with MSVC
eliemichel Nov 21, 2024
5c54324
Remove any use of GNU empty initializer extension
eliemichel Nov 21, 2024
1947b2f
Roll back undefined vertex_format and use _wgpu_ZERO_INIT
eliemichel Nov 21, 2024
d543b8e
Use first non-null enum entry as default value for enum members
eliemichel Nov 21, 2024
a376c86
Always initialize pointer members to NULL
eliemichel Nov 21, 2024
c4e6545
Fix incorrect default values reported by Kai
eliemichel Nov 21, 2024
6dfb899
Remove unused tentative _wgpu_ZERO_INIT
eliemichel Nov 21, 2024
e127ef3
Revert "Use first non-null enum entry as default value for enum members"
kainino0x Nov 21, 2024
53cf21f
Use _wgpu_ENUM_ZERO_INIT
kainino0x Nov 21, 2024
1085f75
nit: remove comment that could become outdated later
kainino0x Nov 21, 2024
b84e311
Use 0xFFFFFFFF as string
kainino0x Nov 21, 2024
3b0992f
Comment formatting nits
kainino0x Nov 21, 2024
cc2fcde
Add docs to INIT macros
kainino0x Nov 21, 2024
3da031e
change featureLevel and adapterType back to 0
kainino0x Nov 21, 2024
7d6a3f0
revert temporary change
kainino0x Nov 21, 2024
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
24 changes: 24 additions & 0 deletions gen/cheader.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@
#else
# define _wgpu_MAKE_INIT_STRUCT(type, value) value
#endif
#define _wgpu_COMMA ,
#define _wgpu_TRUE 1
#define _wgpu_FALSE 0
{{end}}

/**
Expand Down Expand Up @@ -294,6 +297,16 @@ typedef struct WGPU{{.Name | PascalCase}}CallbackInfo{{$.ExtSuffix}} {
WGPU_NULLABLE void* userdata1;
WGPU_NULLABLE void* userdata2;
} WGPU{{.Name | PascalCase}}CallbackInfo{{$.ExtSuffix}} WGPU_STRUCTURE_ATTRIBUTE;

#define WGPU_{{.Name | ConstantCase}}_CALLBACK_INFO{{$.ExtSuffix}}_INIT _wgpu_MAKE_INIT_STRUCT(WGPU{{.Name | PascalCase}}CallbackInfo{{$.ExtSuffix}}, { \
/*.nextInChain=*/NULL _wgpu_COMMA \
{{- if eq .Style "callback_mode" }}
/*.mode=*/WGPUCallbackMode_AllowProcessEvents _wgpu_COMMA \
eliemichel marked this conversation as resolved.
Show resolved Hide resolved
{{- end}}
/*.callback=*/NULL _wgpu_COMMA \
/*.userdata1=*/NULL _wgpu_COMMA \
/*.userdata2=*/NULL _wgpu_COMMA \
})
{{ end}}{{"\n" -}}

/** @} */
Expand Down Expand Up @@ -322,6 +335,17 @@ typedef struct WGPU{{.Name | PascalCase}}{{$.ExtSuffix}} {
{{ StructMember $struct $memberIndex}}
{{- end}}
} WGPU{{.Name | PascalCase}}{{$.ExtSuffix}} WGPU_STRUCTURE_ATTRIBUTE;

#define WGPU_{{.Name | ConstantCase}}{{if $.ExtSuffix}}_{{$.ExtSuffix}}{{end}}_INIT _wgpu_MAKE_INIT_STRUCT(WGPU{{.Name | PascalCase}}{{$.ExtSuffix}}, { \
{{- if eq .Type "base_in" "base_out" "base_in_or_out" }}
/*.nextInChain=*/NULL _wgpu_COMMA \
{{- else if eq .Type "extension_in" "extension_out" "extension_in_or_out" }}
/*.chain=*/NULL _wgpu_COMMA \
{{- end }}
{{- range $memberIndex, $_ := .Members}}
{{ StructMemberInitializer $struct $memberIndex}}
{{- end}}
})
{{ end}}{{"\n" -}}

/** @} */
Expand Down
93 changes: 92 additions & 1 deletion gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (g *Generator) Gen(dst io.Writer) error {
return ""
}
value, _ := g.EnumValue(prefix, e, entryIndex)
return Comment("`"+value+"`.\n"+v, CommentTypeMultiLine, indent, true)
return Comment("`"+value+"`.\n"+strings.TrimSpace(v), CommentTypeMultiLine, indent, true)
},
"MCommentBitflag": func(v string, indent int, b Bitflag, entryIndex int) string {
if v == "" || strings.TrimSpace(v) == "TODO" {
Expand Down Expand Up @@ -159,6 +159,7 @@ func (g *Generator) Gen(dst io.Writer) error {
"FunctionArgs": g.FunctionArgs,
"CallbackArgs": g.CallbackArgs,
"StructMember": g.StructMember,
"StructMemberInitializer": g.StructMemberInitializer,
})
t, err := t.Parse(tmpl)
if err != nil {
Expand Down Expand Up @@ -446,3 +447,93 @@ func (g *Generator) StructMember(s Struct, memberIndex int) (string, error) {
}
return sb.String(), nil
}

func (g *Generator) StructMemberInitializer(s Struct, memberIndex int) (string, error) {
member := s.Members[memberIndex]
sb := &strings.Builder{}
matches := arrayTypeRegexp.FindStringSubmatch(member.Type)
if len(matches) == 2 {
fmt.Fprintf(sb, "/*.%sCount=*/0 _wgpu_COMMA \\\n", CamelCase(Singularize(member.Name)))
fmt.Fprintf(sb, " /*.%s=*/NULL _wgpu_COMMA \\", CamelCase(member.Name))
} else {
fmt.Fprintf(sb, "/*.%s=*/%s _wgpu_COMMA \\", CamelCase(member.Name), g.DefaultValue(member))
}
return sb.String(), nil
}

func (g *Generator) DefaultValue(member ParameterType) string {
switch {
case strings.HasPrefix(member.Type, "enum."):
if member.Default == "" {
return "{}"
} else {
return "WGPU" + PascalCase(strings.TrimPrefix(member.Type, "enum.")) + "_" + PascalCase(member.Default)
}
case strings.HasPrefix(member.Type, "typedef."):
if member.Default == "" {
return "{}"
} else {
return "WGPU" + PascalCase(strings.TrimPrefix(member.Type, "typedef.")) + "_" + PascalCase(member.Default)
}
case strings.HasPrefix(member.Type, "bitflag."):
if member.Default == "" {
return "{}"
} else {
return "WGPU" + PascalCase(strings.TrimPrefix(member.Type, "bitflag.")) + "_" + PascalCase(member.Default)
}
case strings.HasPrefix(member.Type, "struct."):
if member.Optional {
return "NULL"
} else {
typ := strings.TrimPrefix(member.Type, "struct.");
return "WGPU_" + ConstantCase(typ) + "_INIT"
}
case strings.HasPrefix(member.Type, "callback."):
return "NULL"
case strings.HasPrefix(member.Type, "object."):
return "NULL"
case member.Type == "out_string", member.Type == "string_with_default_empty", member.Type == "nullable_string":
return "WGPU_STRING_VIEW_INIT"
case member.Type == "uint16", member.Type == "uint32", member.Type == "uint64", member.Type == "usize", member.Type == "int32":
if member.Default == "" {
return "{}"
} else if strings.HasPrefix(member.Default, "constant.") {
return "WGPU_" + ConstantCase(strings.TrimPrefix(member.Default, "constant."))
} else {
return member.Default
}
case member.Type == "float32":
if member.Default == "" {
return "{}"
} else if strings.HasPrefix(member.Default, "constant.") {
return "WGPU_" + ConstantCase(strings.TrimPrefix(member.Default, "constant."))
} else {
return member.Default + "f"
}
case member.Type == "float64":
if member.Default == "" {
return "{}"
} else if strings.HasPrefix(member.Default, "constant.") {
return "WGPU_" + ConstantCase(strings.TrimPrefix(member.Default, "constant."))
} else {
return member.Default
}
case member.Type == "bool":
if member.Default == "" {
return "{}"
} else if strings.HasPrefix(member.Default, "constant.") {
return "WGPU_" + ConstantCase(strings.TrimPrefix(member.Default, "constant."))
} else if member.Default == "true" {
return "_wgpu_TRUE"
} else if member.Default == "false" {
return "_wgpu_FALSE"
} else {
return member.Default
}
case member.Type == "c_void":
return "NULL"
default:
panic("invalid prefix: " + member.Type + " in member " + member.Name)
return ""
}
}
1 change: 1 addition & 0 deletions gen/yml.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ type ParameterType struct {
Pointer PointerType `yaml:"pointer"`
Optional bool `yaml:"optional"`
Namespace string `yaml:"namespace"`
Default string `yaml:"default"`
}

type Callback struct {
Expand Down
4 changes: 4 additions & 0 deletions schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@
"type": "string",
"description": "Optional property, specifying the external namespace where this type is defined",
"pattern": "^[a-z]+$"
},
"default": {
"type": [ "string", "number", "boolean" ],
"description": "Default value assigned to this parameter when using initializer macro"
}
},
"required": [
Expand Down
Loading
Loading