Skip to content

Commit da37690

Browse files
gen: extended headers (#270)
* gen-check: show better error in CI * Generation & Validation for multiple yamls * enum extensions * yaml order matters * add readme section * comments * object extensions * bitflag extensions * add implicit suffix & namespaces * specify namespace in yaml
1 parent 3c31706 commit da37690

File tree

9 files changed

+462
-196
lines changed

9 files changed

+462
-196
lines changed

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@ gen: schema.json webgpu.yml
44
go run ./gen -schema schema.json -yaml webgpu.yml -header webgpu.h
55

66
gen-check: gen
7-
@git diff --quiet -- webgpu.h || { git diff -- webgpu.h; exit 1; }
7+
@git diff --quiet -- webgpu.h || { \
8+
echo "error: The re-generated header from yml doesn't match the checked-in header"; \
9+
git diff -- webgpu.h; \
10+
exit 1; \
11+
}

gen/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@
22

33
The generator for generating `webgpu.h` header and other extension headers or implementation specific headers from the yaml spec files.
44

5+
# Generate implementation specific header
6+
7+
The generator also allows generating custom implementation specific headers that build on top of `webgpu.h` header. The generator accepts the combination of `-yaml` & `-header` flags in sequence, which it uses to validate the specifications and then generate their headers.
8+
9+
For example, if `wgpu.yml` contains the implementation specific API, the header can be generated using:
10+
11+
```shell
12+
> go run ./gen -schema schema.json -yaml webgpu.yml -header webgpu.h -yaml wgpu.yml -header wgpu.h
13+
```
14+
15+
Since the generator does some duplication validation, the order of the files matter, so generator mandates the core `webgpu.yml` to be first in the sequence.
16+
517
# yaml spec
618

719
### Types

gen/cheader.tmpl

Lines changed: 62 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{{- MCommentN .Copyright 0}}
22

3-
#ifndef {{.Name | ConstantCase}}_H_
4-
#define {{.Name | ConstantCase}}_H_
3+
#ifndef {{.HeaderName | ConstantCase}}_H_
4+
#define {{.HeaderName | ConstantCase}}_H_
55

66
#if defined(WGPU_SHARED_LIBRARY)
77
# if defined(_WIN32)
@@ -37,7 +37,17 @@
3737
#define WGPU_NULLABLE
3838
#endif{{"\n" -}}
3939

40-
{{if eq .Name "webgpu"}}
40+
{{- if ne .Name "webgpu"}}
41+
#if !defined(__WGPU_EXTEND_ENUM)
42+
#ifdef __cplusplus
43+
#define __WGPU_EXTEND_ENUM(E, N, V) static const E N = E(V)
44+
#else
45+
#define __WGPU_EXTEND_ENUM(E, N, V) static const E N = (E)(V)
46+
#endif
47+
#endif // !defined(__WGPU_EXTEND_ENUM)
48+
{{ end}}
49+
50+
{{- if eq .Name "webgpu"}}
4151
#include <stdint.h>
4252
#include <stddef.h>
4353
{{else}}
@@ -46,8 +56,8 @@
4656

4757
{{- if .Constants}}
4858
{{- range .Constants}}
49-
{{- MComment .Doc 0}}
50-
#define WGPU_{{.Name | ConstantCase}} ({{.Value | CValue}})
59+
{{- MComment .Doc 0}}
60+
#define WGPU_{{.Name | ConstantCase}}{{if $.ExtSuffix}}_{{$.ExtSuffix}}{{end}} ({{.Value | CValue}})
5161
{{- end}}
5262
{{ end}}
5363

@@ -59,61 +69,51 @@ typedef uint32_t WGPUBool;
5969
{{- if .Objects}}
6070
{{- range .Objects}}
6171
{{- if not .IsStruct}}
62-
typedef struct WGPU{{.Name | PascalCase}}Impl* WGPU{{.Name | PascalCase}} WGPU_OBJECT_ATTRIBUTE;
72+
typedef struct WGPU{{.Name | PascalCase}}{{$.ExtSuffix}}Impl* WGPU{{.Name | PascalCase}}{{$.ExtSuffix}} WGPU_OBJECT_ATTRIBUTE;
6373
{{- end}}
6474
{{- end}}
6575
{{ end}}
6676

6777
{{- if .Structs}}
6878
// Structure forward declarations
6979
{{- range .Structs}}
70-
struct WGPU{{.Name | PascalCase}};
80+
struct WGPU{{.Name | PascalCase}}{{$.ExtSuffix}};
7181
{{- end}}
7282
{{ end}}
7383

74-
{{- range $entry := .Enums}}
75-
{{- MComment .Doc 0}}
76-
typedef enum WGPU{{.Name | PascalCase}} {
77-
{{- range $entryIndex, $_ := .Entries}}
78-
{{- MComment .Doc 4}}
79-
{{- $entryValue := 0}}
80-
{{- if eq .Value ""}}
81-
{{- $entryValue = $entryIndex}}
82-
{{- else}}
83-
{{- $entryValue = ParseUint .Value 16}}
84+
{{- range $enum := .Enums}}
85+
{{- if .Extended}}
86+
{{- range $entryIndex, $_ := .Entries}}
87+
__WGPU_EXTEND_ENUM(WGPU{{$enum.Name | PascalCase}}, WGPU{{$enum.Name | PascalCase}}_{{.Name | PascalCase}}{{if $.ExtSuffix}}_{{$.ExtSuffix}}{{end}}, {{EnumValue $.EnumPrefix $enum $entryIndex}});
88+
{{- end}}
89+
{{- else}}
90+
{{- MComment .Doc 0}}
91+
typedef enum WGPU{{.Name | PascalCase}}{{$.ExtSuffix}} {
92+
{{- range $entryIndex, $_ := .Entries}}
93+
{{- MComment .Doc 4}}
94+
WGPU{{$enum.Name | PascalCase}}_{{.Name | PascalCase}}{{if $.ExtSuffix}}_{{$.ExtSuffix}}{{end}} = {{EnumValue $.EnumPrefix $enum $entryIndex}},
8495
{{- end}}
85-
WGPU{{$entry.Name | PascalCase}}_{{.Name | PascalCase}} = {{printf "%s%.4X," $.EnumPrefix $entryValue}}
96+
WGPU{{$enum.Name | PascalCase}}_Force32{{if $.ExtSuffix}}_{{$.ExtSuffix}}{{end}} = 0x7FFFFFFF
97+
} WGPU{{$enum.Name | PascalCase}}{{$.ExtSuffix}} WGPU_ENUM_ATTRIBUTE;
8698
{{- end}}
87-
WGPU{{.Name | PascalCase}}_Force32 = 0x7FFFFFFF
88-
} WGPU{{.Name | PascalCase}} WGPU_ENUM_ATTRIBUTE;
8999
{{ end}}
90100

91-
{{- range $entry := .Bitflags}}
92-
{{- MComment .Doc 0}}
93-
typedef enum WGPU{{.Name | PascalCase}} {
94-
{{- range $entryIndex, $_ := .Entries}}
95-
{{- MComment .Doc 4}}
96-
{{- $entryValue := ""}}
97-
{{- $valueCombination := .ValueCombination}}
98-
{{- range $valueIndex, $v := .ValueCombination}}
99-
{{- $v = printf "WGPU%s_%s" ($entry.Name | PascalCase) ($v | PascalCase)}}
100-
{{- if IsLast $valueIndex $valueCombination}}
101-
{{- $entryValue = print $entryValue $v}}
102-
{{- else}}
103-
{{- $entryValue = print $entryValue $v " | "}}
104-
{{- end}}
105-
{{- else}}
106-
{{- if eq .Value ""}}
107-
{{- $entryValue = printf "0x%.8X" (BitFlagValue $entryIndex)}}
108-
{{- else}}
109-
{{- $entryValue = printf "0x%.8X" (ParseUint .Value 64)}}
110-
{{- end}}
101+
{{- range $bitflag := .Bitflags}}
102+
{{- if .Extended}}
103+
{{- range $entryIndex, $_ := .Entries}}
104+
__WGPU_EXTEND_ENUM(WGPU{{$bitflag.Name | PascalCase}}, WGPU{{$bitflag.Name | PascalCase}}_{{.Name | PascalCase}}{{if $.ExtSuffix}}_{{$.ExtSuffix}}{{end}}, {{BitflagValue $bitflag $entryIndex}});
111105
{{- end}}
112-
WGPU{{$entry.Name | PascalCase}}_{{.Name | PascalCase}} = {{$entryValue}},
106+
{{- else}}
107+
{{- MComment .Doc 0}}
108+
typedef enum WGPU{{.Name | PascalCase}}{{$.ExtSuffix}} {
109+
{{- range $entryIndex, $_ := .Entries}}
110+
{{- MComment .Doc 4}}
111+
WGPU{{$bitflag.Name | PascalCase}}_{{.Name | PascalCase}}{{if $.ExtSuffix}}_{{$.ExtSuffix}}{{end}} = {{BitflagValue $bitflag $entryIndex}},
112+
{{- end}}
113+
WGPU{{$bitflag.Name | PascalCase}}_Force32{{if $.ExtSuffix}}_{{$.ExtSuffix}}{{end}} = 0x7FFFFFFF
114+
} WGPU{{$bitflag.Name | PascalCase}}{{$.ExtSuffix}} WGPU_ENUM_ATTRIBUTE;
115+
typedef WGPUFlags WGPU{{$bitflag.Name | PascalCase}}Flags{{$.ExtSuffix}} WGPU_ENUM_ATTRIBUTE;
113116
{{- end}}
114-
WGPU{{.Name | PascalCase}}_Force32 = 0x7FFFFFFF
115-
} WGPU{{.Name | PascalCase}} WGPU_ENUM_ATTRIBUTE;
116-
typedef WGPUFlags WGPU{{.Name | PascalCase}}Flags WGPU_ENUM_ATTRIBUTE;
117117
{{ end}}
118118

119119
{{- if eq .Name "webgpu"}}
@@ -122,15 +122,15 @@ typedef void (*WGPUProc)(void) WGPU_FUNCTION_ATTRIBUTE;
122122

123123
{{- range .FunctionTypes}}
124124
{{- MComment .Doc 0}}
125-
typedef {{FunctionReturns .}} (*WGPU{{.Name | PascalCase}})({{FunctionArgs . nil}}) WGPU_FUNCTION_ATTRIBUTE;
125+
typedef {{FunctionReturns .}} (*WGPU{{.Name | PascalCase}}{{$.ExtSuffix}})({{FunctionArgs . nil}}) WGPU_FUNCTION_ATTRIBUTE;
126126
{{- end}}
127127

128128
{{- if .Objects}}
129129
{{ range $object := .Objects}}
130130
{{- range $method := .Methods}}
131131
{{- if .ReturnsAsync}}
132132
{{- MComment .Doc 0}}
133-
typedef void (*WGPU{{$object.Name | PascalCase}}{{$method.Name | PascalCase}}Callback)({{CallbackArgs .}}) WGPU_FUNCTION_ATTRIBUTE;
133+
typedef void (*WGPU{{$object.Name | PascalCase}}{{$method.Name | PascalCase}}Callback{{$.ExtSuffix}})({{CallbackArgs .}}) WGPU_FUNCTION_ATTRIBUTE;
134134
{{- end}}
135135
{{- end}}
136136
{{- end}}
@@ -148,9 +148,9 @@ typedef struct WGPUChainedStructOut {
148148
} WGPUChainedStructOut WGPU_STRUCTURE_ATTRIBUTE;
149149
{{ end}}
150150

151-
{{- range .Structs}}
151+
{{- range $struct := .Structs}}
152152
{{- MComment .Doc 0}}
153-
typedef struct WGPU{{.Name | PascalCase}} {
153+
typedef struct WGPU{{.Name | PascalCase}}{{$.ExtSuffix}} {
154154
{{- if eq .Type "base_in" }}
155155
WGPUChainedStruct const * nextInChain;
156156
{{- else if eq .Type "base_out" }}
@@ -160,17 +160,10 @@ typedef struct WGPU{{.Name | PascalCase}} {
160160
{{- else if eq .Type "extension_out"}}
161161
WGPUChainedStructOut chain;
162162
{{- end}}
163-
{{- range .Members}}
164-
{{- if IsArray .Type}}
165-
size_t {{.Name | CamelCase | Singularize}}Count;
166-
{{- MComment .Doc 4}}
167-
{{ArrayType .Type .Pointer}} {{.Name | CamelCase}};
168-
{{- else}}
169-
{{- MComment .Doc 4}}
170-
{{if .Optional}}WGPU_NULLABLE {{end}}{{CType .Type .Pointer}} {{.Name | CamelCase}};
171-
{{- end}}
163+
{{- range $memberIndex, $_ := .Members}}
164+
{{ StructMember $struct $memberIndex}}
172165
{{- end}}
173-
} WGPU{{.Name | PascalCase}} WGPU_STRUCTURE_ATTRIBUTE;
166+
} WGPU{{.Name | PascalCase}}{{$.ExtSuffix}} WGPU_STRUCTURE_ATTRIBUTE;
174167
{{ end}}{{"\n" -}}
175168

176169
#ifdef __cplusplus
@@ -181,7 +174,7 @@ extern "C" {
181174

182175
{{- range .Functions}}
183176
{{- MComment .Doc 0}}
184-
typedef {{FunctionReturns .}} (*WGPUProc{{.Name | PascalCase}})({{FunctionArgs . nil}}) WGPU_FUNCTION_ATTRIBUTE;
177+
typedef {{FunctionReturns .}} (*WGPUProc{{.Name | PascalCase}}{{$.ExtSuffix}})({{FunctionArgs . nil}}) WGPU_FUNCTION_ATTRIBUTE;
185178
{{- end}}
186179
{{- if eq .Name "webgpu"}}
187180
typedef WGPUProc (*WGPUProcGetProcAddress)(WGPUDevice device, char const * procName) WGPU_FUNCTION_ATTRIBUTE;
@@ -191,11 +184,11 @@ typedef WGPUProc (*WGPUProcGetProcAddress)(WGPUDevice device, char const * procN
191184
// Procs of {{$object.Name | PascalCase}}
192185
{{- range $object.Methods}}
193186
{{- MComment .Doc 0}}
194-
typedef {{FunctionReturns .}} (*WGPUProc{{$object.Name | PascalCase}}{{.Name | PascalCase}})({{FunctionArgs . $object}}) WGPU_FUNCTION_ATTRIBUTE;
187+
typedef {{FunctionReturns .}} (*WGPUProc{{$object.Name | PascalCase}}{{.Name | PascalCase}}{{$.ExtSuffix}})({{FunctionArgs . $object}}) WGPU_FUNCTION_ATTRIBUTE;
195188
{{- end}}
196-
{{- if not .IsStruct}}
197-
typedef void (*WGPUProc{{.Name | PascalCase}}Reference)(WGPU{{.Name | PascalCase}} {{.Name | CamelCase}}) WGPU_FUNCTION_ATTRIBUTE;
198-
typedef void (*WGPUProc{{.Name | PascalCase}}Release)(WGPU{{.Name | PascalCase}} {{.Name | CamelCase}}) WGPU_FUNCTION_ATTRIBUTE;
189+
{{- if not (or .IsStruct .Extended)}}
190+
typedef void (*WGPUProc{{.Name | PascalCase}}Reference{{$.ExtSuffix}})(WGPU{{.Name | PascalCase}} {{.Name | CamelCase}}) WGPU_FUNCTION_ATTRIBUTE;
191+
typedef void (*WGPUProc{{.Name | PascalCase}}Release{{$.ExtSuffix}})(WGPU{{.Name | PascalCase}} {{.Name | CamelCase}}) WGPU_FUNCTION_ATTRIBUTE;
199192
{{- end}}
200193
{{ end}}{{"\n" -}}
201194

@@ -205,7 +198,7 @@ typedef void (*WGPUProc{{.Name | PascalCase}}Release)(WGPU{{.Name | PascalCase}}
205198

206199
{{- range .Functions}}
207200
{{- MComment .Doc 0}}
208-
WGPU_EXPORT {{FunctionReturns .}} wgpu{{.Name | PascalCase}}({{FunctionArgs . nil}}) WGPU_FUNCTION_ATTRIBUTE;
201+
WGPU_EXPORT {{FunctionReturns .}} wgpu{{.Name | PascalCase}}{{$.ExtSuffix}}({{FunctionArgs . nil}}) WGPU_FUNCTION_ATTRIBUTE;
209202
{{- end}}
210203
{{- if eq .Name "webgpu"}}
211204
WGPU_EXPORT WGPUProc wgpuGetProcAddress(WGPUDevice device, char const * procName) WGPU_FUNCTION_ATTRIBUTE;
@@ -215,11 +208,11 @@ WGPU_EXPORT WGPUProc wgpuGetProcAddress(WGPUDevice device, char const * procName
215208
// Methods of {{$object.Name | PascalCase}}
216209
{{- range $object.Methods}}
217210
{{- MComment .Doc 0}}
218-
WGPU_EXPORT {{FunctionReturns .}} wgpu{{$object.Name | PascalCase}}{{.Name | PascalCase}}({{FunctionArgs . $object}}) WGPU_FUNCTION_ATTRIBUTE;
211+
WGPU_EXPORT {{FunctionReturns .}} wgpu{{$object.Name | PascalCase}}{{.Name | PascalCase}}{{$.ExtSuffix}}({{FunctionArgs . $object}}) WGPU_FUNCTION_ATTRIBUTE;
219212
{{- end}}
220-
{{- if not .IsStruct}}
221-
WGPU_EXPORT void wgpu{{.Name | PascalCase}}Reference(WGPU{{.Name | PascalCase}} {{.Name | CamelCase}}) WGPU_FUNCTION_ATTRIBUTE;
222-
WGPU_EXPORT void wgpu{{.Name | PascalCase}}Release(WGPU{{.Name | PascalCase}} {{.Name | CamelCase}}) WGPU_FUNCTION_ATTRIBUTE;
213+
{{- if not (or .IsStruct .Extended)}}
214+
WGPU_EXPORT void wgpu{{.Name | PascalCase}}Reference{{$.ExtSuffix}}(WGPU{{.Name | PascalCase}} {{.Name | CamelCase}}) WGPU_FUNCTION_ATTRIBUTE;
215+
WGPU_EXPORT void wgpu{{.Name | PascalCase}}Release{{$.ExtSuffix}}(WGPU{{.Name | PascalCase}} {{.Name | CamelCase}}) WGPU_FUNCTION_ATTRIBUTE;
223216
{{- end}}
224217
{{ end}}{{"\n" -}}
225218

@@ -229,4 +222,4 @@ WGPU_EXPORT void wgpu{{.Name | PascalCase}}Release(WGPU{{.Name | PascalCase}} {{
229222
} // extern "C"
230223
#endif
231224

232-
#endif // {{.Name | ConstantCase}}_H_
225+
#endif // {{.HeaderName | ConstantCase}}_H_

0 commit comments

Comments
 (0)