dawn-cmake/test/intrinsics/intrinsics.wgsl.tmpl
Ben Clayton 93e8f527ee wgsl: Deprecate [[access]] decorations
Handle access control on var declarations instead of via [[access]]
decorations. This change does the minimal work to migrate the WGSL
parser over to the new syntax. Additional changes will be needed
to correctly generate defaulted access qualifiers, as well as
validating access usage.

The [[access]] decorations are still supported by the WGSL parser,
with new deprecated warnings, but not for aliases. Example:
   var x : [[access(x)]] alias_to_struct;

Making this work is far more effort than I want to dedicate to backwards
compatibility, and I do not beleive any real-world usage will be doing
this.

Still TODO:
* Adding access control as the optional, third parameter to ptr<>.
* Calculating default accesses for the various storage types.
* Validating usage of variables against the different accesses.

Bug: tint:846
Change-Id: If8ca82e5d16ec319ecd01f9a2cafffd930963bde
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/53088
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: James Price <jrprice@google.com>
Reviewed-by: David Neto <dneto@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
2021-06-04 20:41:47 +00:00

181 lines
6.9 KiB
Cheetah

{{- /*
--------------------------------------------------------------------------------
Template file for use with tools/intrinsic-gen to generate the wgsl files in the
./gen/... subdirectories
See:
* tools/cmd/intrinsic-gen/gen for structures used by this template
* https://golang.org/pkg/text/template/ for documentation on the template syntax
--------------------------------------------------------------------------------
*/ -}}
{{- /* For each permutation of each overload of each function... */ -}}
{{- range .Sem.Functions -}}
{{- range .Overloads -}}
{{- range Permute . -}}
{{- /* Generate a ./gen/<function>/<permuataion-hash>.wgsl file using
the Permutation macro defined below */ -}}
{{- $file := printf "./gen/%v/%v.wgsl" .Function.Name .Hash -}}
{{- $content := Eval "Permutation" . -}}
{{- WriteFile $file $content -}}
{{- end }}
{{- end }}
{{- end }}
{{- /* ------------------------------------------------------------------ */ -}}
{{- define "Permutation" -}}
{{- /* Emits the body of the intrinsic permuation .wgsl file */ -}}
{{- /* ------------------------------------------------------------------ */ -}}
{{- $function := .Function.Name -}}
{{- $permutation := printf "%v_%v" $function .Hash -}}
{{- $args := Map -}}
{{- /* Generate a storage buffer for runtime sized array parameters */ -}}
{{- if (Eval "HasStorageBufferArgs" .) -}}
[[block]]
struct SB {
{{- range $i, $p := .Parameters }}
{{- $class := Eval "StorageClass" $p.Type -}}
{{- if eq "STORAGE_BUFFER" $class }}
arg_{{$i}}: {{template "Type" $p.Type}};
{{ $args.Put $i (printf "sb.arg_%v" $i) -}}
{{- end -}}
{{- end -}}
};
[[group(0), binding(0)]] var<storage, read> sb : SB;
{{ end -}}
{{- /* Generate module-scoped resource variables (textures, samplers, etc) */ -}}
{{- range $i, $p := .Parameters }}
{{- $class := Eval "StorageClass" $p.Type -}}
{{- if eq "RESOURCE" $class -}}
[[group(1), binding({{$i}})]] var arg_{{$i}}: {{template "Type" $p.Type}};
{{ $args.Put $i (printf "arg_%v" $i) -}}
{{- end -}}
{{- end -}}
{{- /* Generate the function that calls the intrinsic */ -}}
fn {{$permutation}}() {
{{/* Build the parameters either as 'var' or inline values */ -}}
{{- range $i, $p := .Parameters -}}
{{- $class := Eval "StorageClass" $p.Type -}}
{{- if eq "PRIVATE" $class -}}
{{- if eq "ptr" $p.Type.Target.Name -}}
{{- /*indent*/}} var arg_{{$i}}: {{template "Type" index $p.Type.TemplateArguments 1}};
{{ $args.Put $i (printf "&arg_%v" $i) -}}
{{- else -}}
{{- $args.Put $i (Eval "ValueOf" $p.Type) -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- /* Make the call to the intrinsic */ -}}
{{- /*indent*/}} {{/*indent*/ -}}
{{- if .ReturnType -}}
var res: {{template "Type" .ReturnType}} = {{/* preserve space after = */ -}}
{{- end -}}
{{$function}}(
{{- range $i, $p := .Parameters -}}
{{- if $i -}}, {{end}}{{$args.Get $i -}}
{{- end -}}
);
}
{{/*new line*/ -}}
{{- if .CanBeUsedInStage.Vertex }}
[[stage(vertex)]]
fn vertex_main() -> [[builtin(position)]] vec4<f32> {
{{$permutation}}();
return vec4<f32>();
}
{{ end -}}
{{- if .CanBeUsedInStage.Fragment }}
[[stage(fragment)]]
fn fragment_main() {
{{$permutation}}();
}
{{ end -}}
{{- if .CanBeUsedInStage.Compute }}
[[stage(compute)]]
fn compute_main() {
{{$permutation}}();
}
{{ end -}}
{{- end -}}
{{- /* ------------------------------------------------------------------ */ -}}
{{- define "HasStorageBufferArgs" -}}
{{- /* Returns a non-empty string if the given overload has parameters */ -}}
{{- /* that are in the STORAGE_BUFFER storage class */ -}}
{{- /* ------------------------------------------------------------------ */ -}}
{{- range $i, $p := .Parameters }}
{{- $class := Eval "StorageClass" $p.Type -}}
{{- if eq "STORAGE_BUFFER" $class -}}1
{{ end -}}
{{- end -}}
{{- end -}}
{{- /* ------------------------------------------------------------------ */ -}}
{{- define "StorageClass" -}}
{{- /* Returns the storage classs STORAGE_BUFFER, RESOURCE, or PRIVATE */ -}}
{{- /* for the given Fully Qualified Name */ -}}
{{- /* ------------------------------------------------------------------ */ -}}
{{- $name := .Target.Name -}}
{{- if eq $name "array" -}}STORAGE_BUFFER
{{- else if HasPrefix $name "texture" -}}RESOURCE
{{- else if HasPrefix $name "sampler" -}}RESOURCE
{{- else -}}PRIVATE
{{- end -}}
{{- end -}}
{{- /* ------------------------------------------------------------------ */ -}}
{{- define "ValueOf" -}}
{{- /* Returns a value of the given Fully Qualified Name argument */ -}}
{{- /* ------------------------------------------------------------------ */ -}}
{{- if eq .Target.Name "i32" -}}1
{{- else if eq .Target.Name "u32" -}}1u
{{- else if eq .Target.Name "f32" -}}1.0
{{- else -}}{{template "Type" .}}()
{{- end -}}
{{- end -}}
{{- /* ------------------------------------------------------------------ */ -}}
{{- define "Type" -}}
{{- /* Emits the WGSL for the Fully Qualified Name argument */ -}}
{{- /* ------------------------------------------------------------------ */ -}}
{{- if IsType .Target -}}
{{- if eq .Target.Name "vec" -}}vec{{index .TemplateArguments 0}}<{{template "Type" index .TemplateArguments 1}}>
{{- else if eq .Target.Name "mat" -}}mat{{index .TemplateArguments 0}}x{{index .TemplateArguments 1}}<{{template "Type" index .TemplateArguments 2}}>
{{- else -}}{{.Target.Name}}{{template "TemplateArguments" .TemplateArguments}}
{{- end -}}
{{- else if IsEnumEntry .Target -}}{{.Target.Name}}
{{- else if IsEnumMatcher .Target -}}{{(index .Target.Options 0).Name}}
{{- else -}}<unhandled-fully-qualified-name-target={{- printf "%T" .Target -}}>
{{- end -}}
{{- end -}}
{{- /* ------------------------------------------------------------------ */ -}}
{{- define "TemplateArguments" -}}
{{- /* Emits the WGSL for the template argument list */ -}}
{{- /* ------------------------------------------------------------------ */ -}}
{{- if . -}}
<
{{- range $i, $a := . -}}
{{- if $i -}}, {{ end -}}
{{- if IsInt $a -}}{{- . -}}
{{- else -}}{{- template "Type" $a -}}
{{- end -}}
{{- end -}}
>
{{- end -}}
{{- end -}}