diff --git a/gen/cheader.tmpl b/gen/cheader.tmpl index 7926bcf6..84597714 100644 --- a/gen/cheader.tmpl +++ b/gen/cheader.tmpl @@ -290,7 +290,7 @@ typedef struct WGPUChainedStructOut { * @{ */ - /** +/** * \defgroup WGPUCallbackInfo Callback Info Structs * \brief Callback info structures that are used in asynchronous functions. * @@ -323,7 +323,7 @@ typedef struct WGPU{{.Name | PascalCase}}CallbackInfo{{$.ExtSuffix}} { }) {{ end}}{{"\n" -}} - /** @} */ +/** @} */ {{- "\n"}} {{- range $struct := .Structs}} @@ -345,8 +345,17 @@ typedef struct WGPU{{.Name | PascalCase}}{{$.ExtSuffix}} { WGPUChainedStructOut chain; {{- end}} {{- range $memberIndex, $_ := .Members}} +{{- if (.Type | IsArray)}} + /** + * Array count for `{{.Name | CamelCase}}`. The `INIT` macro sets this to 0. + */ + {{ StructMemberArrayCount $struct $memberIndex}} + {{- MCommentMember . 4 }} + {{ StructMemberArrayData $struct $memberIndex}} +{{- else}} {{- MCommentMember . 4 }} {{ StructMember $struct $memberIndex}} +{{- end}} {{- end}} } WGPU{{.Name | PascalCase}}{{$.ExtSuffix}} WGPU_STRUCTURE_ATTRIBUTE; diff --git a/gen/gen.go b/gen/gen.go index 1ce0c62e..e82b3159 100644 --- a/gen/gen.go +++ b/gen/gen.go @@ -173,6 +173,8 @@ func (g *Generator) Gen(dst io.Writer) error { "FunctionArgs": g.FunctionArgs, "CallbackArgs": g.CallbackArgs, "StructMember": g.StructMember, + "StructMemberArrayCount": g.StructMemberArrayCount, + "StructMemberArrayData": g.StructMemberArrayData, "StructMemberInitializer": g.StructMemberInitializer, }) t, err := t.Parse(tmpl) @@ -286,12 +288,7 @@ func (g *Generator) CType(typ string, pointerType PointerType, suffix string) st func (g *Generator) FunctionArgs(f Function, o *Object) string { sb := &strings.Builder{} if o != nil { - var typeSuffix string - if o.Namespace == "" { - typeSuffix = ConstantCase(g.ExtSuffix) - } else if o.Namespace != "webgpu" { - typeSuffix = ConstantCase(o.Namespace) - } + typeSuffix := g.TypeSuffixForNamespace(o.Namespace) if len(f.Args) > 0 { fmt.Fprintf(sb, "WGPU%s%s %s, ", PascalCase(o.Name), typeSuffix, CamelCase(o.Name)) } else { @@ -302,12 +299,7 @@ func (g *Generator) FunctionArgs(f Function, o *Object) string { if arg.Optional { sb.WriteString("WGPU_NULLABLE ") } - var typeSuffix string - if arg.Namespace == "" { - typeSuffix = ConstantCase(g.ExtSuffix) - } else if arg.Namespace != "webgpu" { - typeSuffix = ConstantCase(arg.Namespace) - } + typeSuffix := g.TypeSuffixForNamespace(arg.Namespace) matches := arrayTypeRegexp.FindStringSubmatch(arg.Type) if len(matches) == 2 { fmt.Fprintf(sb, "size_t %sCount, ", CamelCase(Singularize(arg.Name))) @@ -331,12 +323,7 @@ func (g *Generator) CallbackArgs(f Callback) string { if arg.Optional { sb.WriteString("WGPU_NULLABLE ") } - var typeSuffix string - if arg.Namespace == "" { - typeSuffix = ConstantCase(g.ExtSuffix) - } else if arg.Namespace != "webgpu" { - typeSuffix = ConstantCase(arg.Namespace) - } + typeSuffix := g.TypeSuffixForNamespace(arg.Namespace) var structPrefix string if strings.HasPrefix(arg.Type, "struct.") { structPrefix = "struct " @@ -438,29 +425,63 @@ func (g *Generator) BitflagValue(b Bitflag, entryIndex int) (string, error) { return entryValue, nil } +func (g *Generator) TypeSuffixForNamespace(namespace string) string { + switch namespace { + case "": + return ConstantCase(g.ExtSuffix) + case "webgpu": + return "" + default: + return ConstantCase(namespace) + } +} + func (g *Generator) StructMember(s Struct, memberIndex int) (string, error) { member := s.Members[memberIndex] + typeSuffix := g.TypeSuffixForNamespace(member.Namespace) + + matches := arrayTypeRegexp.FindStringSubmatch(member.Type) + if len(matches) == 2 { + panic("StructMember used on array type") + } + sb := &strings.Builder{} if member.Optional { sb.WriteString("WGPU_NULLABLE ") } - var typeSuffix string - if member.Namespace == "" { - typeSuffix = ConstantCase(g.ExtSuffix) - } else if member.Namespace != "webgpu" { - typeSuffix = ConstantCase(member.Namespace) + if strings.HasPrefix(member.Type, "callback.") { + fmt.Fprintf(sb, "%s %s;", g.CType(member.Type, "", "Info"), CamelCase(member.Name)) + } else { + fmt.Fprintf(sb, "%s %s;", g.CType(member.Type, member.Pointer, typeSuffix), CamelCase(member.Name)) } + return sb.String(), nil +} + +func (g *Generator) StructMemberArrayCount(s Struct, memberIndex int) (string, error) { + member := s.Members[memberIndex] + 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)) - } else { - if strings.HasPrefix(member.Type, "callback.") { - fmt.Fprintf(sb, "%s %s;", g.CType(member.Type, "", "Info"), CamelCase(member.Name)) - } else { - fmt.Fprintf(sb, "%s %s;", g.CType(member.Type, member.Pointer, typeSuffix), CamelCase(member.Name)) - } + if len(matches) != 2 { + panic("StructMemberArrayCount used on non-array") + } + + return fmt.Sprintf("size_t %sCount;", CamelCase(Singularize(member.Name))), nil +} + +func (g *Generator) StructMemberArrayData(s Struct, memberIndex int) (string, error) { + member := s.Members[memberIndex] + typeSuffix := g.TypeSuffixForNamespace(member.Namespace) + + matches := arrayTypeRegexp.FindStringSubmatch(member.Type) + if len(matches) != 2 { + panic("StructMemberArrayCount used on non-array") + } + + sb := &strings.Builder{} + if member.Optional { + sb.WriteString("WGPU_NULLABLE ") } + fmt.Fprintf(sb, "%s %s;", g.CType(matches[1], member.Pointer, typeSuffix), CamelCase(member.Name)) return sb.String(), nil } diff --git a/webgpu.h b/webgpu.h index 19f0d1d9..f465b748 100644 --- a/webgpu.h +++ b/webgpu.h @@ -1314,7 +1314,7 @@ typedef struct WGPUChainedStructOut { * @{ */ - /** +/** * \defgroup WGPUCallbackInfo Callback Info Structs * \brief Callback info structures that are used in asynchronous functions. * @@ -2238,9 +2238,12 @@ typedef struct WGPUPipelineLayoutDescriptor { */ WGPUStringView label; /** - * The `INIT` macro sets this to `NULL`. + * Array count for `bindGroupLayouts`. The `INIT` macro sets this to 0. */ size_t bindGroupLayoutCount; + /** + * The `INIT` macro sets this to `NULL`. + */ WGPUBindGroupLayout const * bindGroupLayouts; } WGPUPipelineLayoutDescriptor WGPU_STRUCTURE_ATTRIBUTE; @@ -2384,9 +2387,12 @@ typedef struct WGPURenderBundleEncoderDescriptor { */ WGPUStringView label; /** - * The `INIT` macro sets this to `NULL`. + * Array count for `colorFormats`. The `INIT` macro sets this to 0. */ size_t colorFormatCount; + /** + * The `INIT` macro sets this to `NULL`. + */ WGPUTextureFormat const * colorFormats; /** * The `INIT` macro sets this to @ref WGPUTextureFormat_Undefined. @@ -2805,9 +2811,12 @@ typedef struct WGPUStorageTextureBindingLayout { */ typedef struct WGPUSupportedFeatures { /** - * The `INIT` macro sets this to `NULL`. + * Array count for `features`. The `INIT` macro sets this to 0. */ size_t featureCount; + /** + * The `INIT` macro sets this to `NULL`. + */ WGPUFeatureName const * features; } WGPUSupportedFeatures WGPU_STRUCTURE_ATTRIBUTE; @@ -2824,9 +2833,12 @@ typedef struct WGPUSupportedFeatures { */ typedef struct WGPUSupportedWGSLLanguageFeatures { /** - * The `INIT` macro sets this to `NULL`. + * Array count for `features`. The `INIT` macro sets this to 0. */ size_t featureCount; + /** + * The `INIT` macro sets this to `NULL`. + */ WGPUWGSLLanguageFeatureName const * features; } WGPUSupportedWGSLLanguageFeatures WGPU_STRUCTURE_ATTRIBUTE; @@ -2852,28 +2864,37 @@ typedef struct WGPUSurfaceCapabilities { * The `INIT` macro sets this to @ref WGPUTextureUsage_None. */ WGPUTextureUsage usages; + /** + * Array count for `formats`. The `INIT` macro sets this to 0. + */ + size_t formatCount; /** * A list of supported @ref WGPUTextureFormat values, in order of preference. * * The `INIT` macro sets this to `NULL`. */ - size_t formatCount; WGPUTextureFormat const * formats; + /** + * Array count for `presentModes`. The `INIT` macro sets this to 0. + */ + size_t presentModeCount; /** * A list of supported @ref WGPUPresentMode values. * Guaranteed to contain @ref WGPUPresentMode_Fifo. * * The `INIT` macro sets this to `NULL`. */ - size_t presentModeCount; WGPUPresentMode const * presentModes; + /** + * Array count for `alphaModes`. The `INIT` macro sets this to 0. + */ + size_t alphaModeCount; /** * A list of supported @ref WGPUCompositeAlphaMode values. * @ref WGPUCompositeAlphaMode_Auto will be an alias for the first element and will never be present in this array. * * The `INIT` macro sets this to `NULL`. */ - size_t alphaModeCount; WGPUCompositeAlphaMode const * alphaModes; } WGPUSurfaceCapabilities WGPU_STRUCTURE_ATTRIBUTE; @@ -2929,12 +2950,15 @@ typedef struct WGPUSurfaceConfiguration { * The `INIT` macro sets this to `0`. */ uint32_t height; + /** + * Array count for `viewFormats`. The `INIT` macro sets this to 0. + */ + size_t viewFormatCount; /** * The additional @ref WGPUTextureFormat for @ref WGPUTextureView format reinterpretation of the surface's textures. * * The `INIT` macro sets this to `NULL`. */ - size_t viewFormatCount; WGPUTextureFormat const * viewFormats; /** * How the surface's frames will be composited on the screen. @@ -3378,9 +3402,12 @@ typedef struct WGPUBindGroupDescriptor { */ WGPUBindGroupLayout layout; /** - * The `INIT` macro sets this to `NULL`. + * Array count for `entries`. The `INIT` macro sets this to 0. */ size_t entryCount; + /** + * The `INIT` macro sets this to `NULL`. + */ WGPUBindGroupEntry const * entries; } WGPUBindGroupDescriptor WGPU_STRUCTURE_ATTRIBUTE; @@ -3467,9 +3494,12 @@ typedef struct WGPUBlendState { typedef struct WGPUCompilationInfo { WGPUChainedStruct const * nextInChain; /** - * The `INIT` macro sets this to `NULL`. + * Array count for `messages`. The `INIT` macro sets this to 0. */ size_t messageCount; + /** + * The `INIT` macro sets this to `NULL`. + */ WGPUCompilationMessage const * messages; } WGPUCompilationInfo WGPU_STRUCTURE_ATTRIBUTE; @@ -3524,9 +3554,12 @@ typedef struct WGPUComputeState { */ WGPUStringView entryPoint; /** - * The `INIT` macro sets this to `NULL`. + * Array count for `constants`. The `INIT` macro sets this to 0. */ size_t constantCount; + /** + * The `INIT` macro sets this to `NULL`. + */ WGPUConstantEntry const * constants; } WGPUComputeState WGPU_STRUCTURE_ATTRIBUTE; @@ -3617,9 +3650,12 @@ typedef struct WGPUDeviceDescriptor { */ WGPUStringView label; /** - * The `INIT` macro sets this to `NULL`. + * Array count for `requiredFeatures`. The `INIT` macro sets this to 0. */ size_t requiredFeatureCount; + /** + * The `INIT` macro sets this to `NULL`. + */ WGPUFeatureName const * requiredFeatures; /** * The `INIT` macro sets this to `NULL`. @@ -3843,9 +3879,12 @@ typedef struct WGPUTextureDescriptor { */ uint32_t sampleCount; /** - * The `INIT` macro sets this to `NULL`. + * Array count for `viewFormats`. The `INIT` macro sets this to 0. */ size_t viewFormatCount; + /** + * The `INIT` macro sets this to `NULL`. + */ WGPUTextureFormat const * viewFormats; } WGPUTextureDescriptor WGPU_STRUCTURE_ATTRIBUTE; @@ -3892,9 +3931,12 @@ typedef struct WGPUVertexBufferLayout { */ uint64_t arrayStride; /** - * The `INIT` macro sets this to `NULL`. + * Array count for `attributes`. The `INIT` macro sets this to 0. */ size_t attributeCount; + /** + * The `INIT` macro sets this to `NULL`. + */ WGPUVertexAttribute const * attributes; } WGPUVertexBufferLayout WGPU_STRUCTURE_ATTRIBUTE; @@ -3921,9 +3963,12 @@ typedef struct WGPUBindGroupLayoutDescriptor { */ WGPUStringView label; /** - * The `INIT` macro sets this to `NULL`. + * Array count for `entries`. The `INIT` macro sets this to 0. */ size_t entryCount; + /** + * The `INIT` macro sets this to `NULL`. + */ WGPUBindGroupLayoutEntry const * entries; } WGPUBindGroupLayoutDescriptor WGPU_STRUCTURE_ATTRIBUTE; @@ -4013,9 +4058,12 @@ typedef struct WGPURenderPassDescriptor { */ WGPUStringView label; /** - * The `INIT` macro sets this to `NULL`. + * Array count for `colorAttachments`. The `INIT` macro sets this to 0. */ size_t colorAttachmentCount; + /** + * The `INIT` macro sets this to `NULL`. + */ WGPURenderPassColorAttachment const * colorAttachments; /** * The `INIT` macro sets this to `NULL`. @@ -4060,14 +4108,20 @@ typedef struct WGPUVertexState { */ WGPUStringView entryPoint; /** - * The `INIT` macro sets this to `NULL`. + * Array count for `constants`. The `INIT` macro sets this to 0. */ size_t constantCount; - WGPUConstantEntry const * constants; /** * The `INIT` macro sets this to `NULL`. */ + WGPUConstantEntry const * constants; + /** + * Array count for `buffers`. The `INIT` macro sets this to 0. + */ size_t bufferCount; + /** + * The `INIT` macro sets this to `NULL`. + */ WGPUVertexBufferLayout const * buffers; } WGPUVertexState WGPU_STRUCTURE_ATTRIBUTE; @@ -4100,14 +4154,20 @@ typedef struct WGPUFragmentState { */ WGPUStringView entryPoint; /** - * The `INIT` macro sets this to `NULL`. + * Array count for `constants`. The `INIT` macro sets this to 0. */ size_t constantCount; - WGPUConstantEntry const * constants; /** * The `INIT` macro sets this to `NULL`. */ + WGPUConstantEntry const * constants; + /** + * Array count for `targets`. The `INIT` macro sets this to 0. + */ size_t targetCount; + /** + * The `INIT` macro sets this to `NULL`. + */ WGPUColorTargetState const * targets; } WGPUFragmentState WGPU_STRUCTURE_ATTRIBUTE;