app/internal/cmd/convertshaders: use backend types directly

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2020-02-23 15:14:24 +01:00
parent fd61c226d4
commit 0be4dd9af0
2 changed files with 38 additions and 130 deletions
+18 -67
View File
@@ -7,9 +7,7 @@ import "gioui.org/gpu/backend"
var (
shader_blit_frag = [...]backend.ShaderSources{
backend.ShaderSources{
Uniforms: []backend.UniformLocation{
{Name: "_12._color", Type: 0, Size: 4, Offset: 0},
},
Uniforms: []backend.UniformLocation{backend.UniformLocation{Name: "_12._color", Type: 0x0, Size: 4, Offset: 0}},
UniformSize: 16,
GLES2: "#version 100\nprecision mediump float;\nprecision highp int;\n\nstruct Color\n{\n vec4 _color;\n};\n\nuniform Color _12;\n\nvarying vec2 vUV;\n\nvoid main()\n{\n gl_FragData[0] = _12._color;\n}\n\n",
/*
@@ -50,10 +48,8 @@ var (
HLSL: []byte(nil),
},
backend.ShaderSources{
Textures: []backend.TextureBinding{
{Name: "tex", Binding: 0},
},
GLES2: "#version 100\nprecision mediump float;\nprecision highp int;\n\nuniform mediump sampler2D tex;\n\nvarying vec2 vUV;\n\nvoid main()\n{\n gl_FragData[0] = texture2D(tex, vUV);\n}\n\n",
Textures: []backend.TextureBinding{backend.TextureBinding{Name: "tex", Binding: 0}},
GLES2: "#version 100\nprecision mediump float;\nprecision highp int;\n\nuniform mediump sampler2D tex;\n\nvarying vec2 vUV;\n\nvoid main()\n{\n gl_FragData[0] = texture2D(tex, vUV);\n}\n\n",
/*
Texture2D<float4> tex : register(t0);
SamplerState _tex_sampler : register(s0);
@@ -90,17 +86,8 @@ var (
},
}
shader_blit_vert = backend.ShaderSources{
Inputs: []backend.InputLocation{
{Name: "pos", Location: 0, Semantic: "POSITION", SemanticIndex: 0, Type: 0, Size: 2},
{Name: "uv", Location: 1, Semantic: "NORMAL", SemanticIndex: 0, Type: 0, Size: 2},
},
Uniforms: []backend.UniformLocation{
{Name: "_15.z", Type: 0, Size: 1, Offset: 0},
{Name: "_15.scale", Type: 0, Size: 2, Offset: 8},
{Name: "_15.offset", Type: 0, Size: 2, Offset: 16},
{Name: "_15.uvScale", Type: 0, Size: 2, Offset: 24},
{Name: "_15.uvOffset", Type: 0, Size: 2, Offset: 32},
},
Inputs: []backend.InputLocation{backend.InputLocation{Name: "pos", Location: 0, Semantic: "POSITION", SemanticIndex: 0, Type: 0x0, Size: 2}, backend.InputLocation{Name: "uv", Location: 1, Semantic: "NORMAL", SemanticIndex: 0, Type: 0x0, Size: 2}},
Uniforms: []backend.UniformLocation{backend.UniformLocation{Name: "_15.z", Type: 0x0, Size: 1, Offset: 0}, backend.UniformLocation{Name: "_15.scale", Type: 0x0, Size: 2, Offset: 8}, backend.UniformLocation{Name: "_15.offset", Type: 0x0, Size: 2, Offset: 16}, backend.UniformLocation{Name: "_15.uvScale", Type: 0x0, Size: 2, Offset: 24}, backend.UniformLocation{Name: "_15.uvOffset", Type: 0x0, Size: 2, Offset: 32}},
UniformSize: 40,
GLES2: "#version 100\n\nstruct Block\n{\n float z;\n vec2 scale;\n vec2 offset;\n vec2 uvScale;\n vec2 uvOffset;\n};\n\nuniform Block _15;\n\nattribute vec2 pos;\nvarying vec2 vUV;\nattribute vec2 uv;\n\nvoid main()\n{\n vec2 p = pos;\n p *= _15.scale;\n p += _15.offset;\n gl_Position = vec4(p, _15.z, 1.0);\n vUV = (uv * _15.uvScale) + _15.uvOffset;\n}\n\n",
/*
@@ -156,14 +143,10 @@ var (
}
shader_cover_frag = [...]backend.ShaderSources{
backend.ShaderSources{
Uniforms: []backend.UniformLocation{
{Name: "_12._color", Type: 0, Size: 4, Offset: 0},
},
Uniforms: []backend.UniformLocation{backend.UniformLocation{Name: "_12._color", Type: 0x0, Size: 4, Offset: 0}},
UniformSize: 16,
Textures: []backend.TextureBinding{
{Name: "cover", Binding: 1},
},
GLES2: "#version 100\nprecision mediump float;\nprecision highp int;\n\nstruct Color\n{\n vec4 _color;\n};\n\nuniform Color _12;\n\nuniform mediump sampler2D cover;\n\nvarying highp vec2 vCoverUV;\nvarying vec2 vUV;\n\nvoid main()\n{\n gl_FragData[0] = _12._color;\n float cover_1 = abs(texture2D(cover, vCoverUV).x);\n gl_FragData[0] *= cover_1;\n}\n\n",
Textures: []backend.TextureBinding{backend.TextureBinding{Name: "cover", Binding: 1}},
GLES2: "#version 100\nprecision mediump float;\nprecision highp int;\n\nstruct Color\n{\n vec4 _color;\n};\n\nuniform Color _12;\n\nuniform mediump sampler2D cover;\n\nvarying highp vec2 vCoverUV;\nvarying vec2 vUV;\n\nvoid main()\n{\n gl_FragData[0] = _12._color;\n float cover_1 = abs(texture2D(cover, vCoverUV).x);\n gl_FragData[0] *= cover_1;\n}\n\n",
/*
cbuffer Color : register(b0)
{
@@ -209,11 +192,8 @@ var (
HLSL: []byte(nil),
},
backend.ShaderSources{
Textures: []backend.TextureBinding{
{Name: "tex", Binding: 0},
{Name: "cover", Binding: 1},
},
GLES2: "#version 100\nprecision mediump float;\nprecision highp int;\n\nuniform mediump sampler2D tex;\nuniform mediump sampler2D cover;\n\nvarying vec2 vUV;\nvarying highp vec2 vCoverUV;\n\nvoid main()\n{\n gl_FragData[0] = texture2D(tex, vUV);\n float cover_1 = abs(texture2D(cover, vCoverUV).x);\n gl_FragData[0] *= cover_1;\n}\n\n",
Textures: []backend.TextureBinding{backend.TextureBinding{Name: "tex", Binding: 0}, backend.TextureBinding{Name: "cover", Binding: 1}},
GLES2: "#version 100\nprecision mediump float;\nprecision highp int;\n\nuniform mediump sampler2D tex;\nuniform mediump sampler2D cover;\n\nvarying vec2 vUV;\nvarying highp vec2 vCoverUV;\n\nvoid main()\n{\n gl_FragData[0] = texture2D(tex, vUV);\n float cover_1 = abs(texture2D(cover, vCoverUV).x);\n gl_FragData[0] *= cover_1;\n}\n\n",
/*
Texture2D<float4> tex : register(t0);
SamplerState _tex_sampler : register(s0);
@@ -257,19 +237,8 @@ var (
},
}
shader_cover_vert = backend.ShaderSources{
Inputs: []backend.InputLocation{
{Name: "pos", Location: 0, Semantic: "POSITION", SemanticIndex: 0, Type: 0, Size: 2},
{Name: "uv", Location: 1, Semantic: "NORMAL", SemanticIndex: 0, Type: 0, Size: 2},
},
Uniforms: []backend.UniformLocation{
{Name: "_19.z", Type: 0, Size: 1, Offset: 0},
{Name: "_19.scale", Type: 0, Size: 2, Offset: 8},
{Name: "_19.offset", Type: 0, Size: 2, Offset: 16},
{Name: "_19.uvCoverScale", Type: 0, Size: 2, Offset: 24},
{Name: "_19.uvCoverOffset", Type: 0, Size: 2, Offset: 32},
{Name: "_19.uvScale", Type: 0, Size: 2, Offset: 40},
{Name: "_19.uvOffset", Type: 0, Size: 2, Offset: 48},
},
Inputs: []backend.InputLocation{backend.InputLocation{Name: "pos", Location: 0, Semantic: "POSITION", SemanticIndex: 0, Type: 0x0, Size: 2}, backend.InputLocation{Name: "uv", Location: 1, Semantic: "NORMAL", SemanticIndex: 0, Type: 0x0, Size: 2}},
Uniforms: []backend.UniformLocation{backend.UniformLocation{Name: "_19.z", Type: 0x0, Size: 1, Offset: 0}, backend.UniformLocation{Name: "_19.scale", Type: 0x0, Size: 2, Offset: 8}, backend.UniformLocation{Name: "_19.offset", Type: 0x0, Size: 2, Offset: 16}, backend.UniformLocation{Name: "_19.uvCoverScale", Type: 0x0, Size: 2, Offset: 24}, backend.UniformLocation{Name: "_19.uvCoverOffset", Type: 0x0, Size: 2, Offset: 32}, backend.UniformLocation{Name: "_19.uvScale", Type: 0x0, Size: 2, Offset: 40}, backend.UniformLocation{Name: "_19.uvOffset", Type: 0x0, Size: 2, Offset: 48}},
UniformSize: 56,
GLES2: "#version 100\n\nstruct Block\n{\n float z;\n vec2 scale;\n vec2 offset;\n vec2 uvCoverScale;\n vec2 uvCoverOffset;\n vec2 uvScale;\n vec2 uvOffset;\n};\n\nuniform Block _19;\n\nattribute vec2 pos;\nvarying vec2 vUV;\nattribute vec2 uv;\nvarying vec2 vCoverUV;\n\nvoid main()\n{\n gl_Position = vec4((pos * _19.scale) + _19.offset, _19.z, 1.0);\n vUV = (uv * _19.uvScale) + _19.uvOffset;\n vCoverUV = (uv * _19.uvCoverScale) + _19.uvCoverOffset;\n}\n\n",
/*
@@ -327,10 +296,8 @@ var (
HLSL: []byte(nil),
}
shader_intersect_frag = backend.ShaderSources{
Textures: []backend.TextureBinding{
{Name: "cover", Binding: 0},
},
GLES2: "#version 100\nprecision mediump float;\nprecision highp int;\n\nuniform mediump sampler2D cover;\n\nvarying highp vec2 vUV;\n\nvoid main()\n{\n float cover_1 = abs(texture2D(cover, vUV).x);\n gl_FragData[0].x = cover_1;\n}\n\n",
Textures: []backend.TextureBinding{backend.TextureBinding{Name: "cover", Binding: 0}},
GLES2: "#version 100\nprecision mediump float;\nprecision highp int;\n\nuniform mediump sampler2D cover;\n\nvarying highp vec2 vUV;\n\nvoid main()\n{\n float cover_1 = abs(texture2D(cover, vUV).x);\n gl_FragData[0].x = cover_1;\n}\n\n",
/*
Texture2D<float4> cover : register(t0);
SamplerState _cover_sampler : register(s0);
@@ -367,14 +334,8 @@ var (
HLSL: []byte(nil),
}
shader_intersect_vert = backend.ShaderSources{
Inputs: []backend.InputLocation{
{Name: "pos", Location: 0, Semantic: "POSITION", SemanticIndex: 0, Type: 0, Size: 2},
{Name: "uv", Location: 1, Semantic: "NORMAL", SemanticIndex: 0, Type: 0, Size: 2},
},
Uniforms: []backend.UniformLocation{
{Name: "_40.scale", Type: 0, Size: 2, Offset: 0},
{Name: "_40.offset", Type: 0, Size: 2, Offset: 8},
},
Inputs: []backend.InputLocation{backend.InputLocation{Name: "pos", Location: 0, Semantic: "POSITION", SemanticIndex: 0, Type: 0x0, Size: 2}, backend.InputLocation{Name: "uv", Location: 1, Semantic: "NORMAL", SemanticIndex: 0, Type: 0x0, Size: 2}},
Uniforms: []backend.UniformLocation{backend.UniformLocation{Name: "_40.scale", Type: 0x0, Size: 2, Offset: 0}, backend.UniformLocation{Name: "_40.offset", Type: 0x0, Size: 2, Offset: 8}},
UniformSize: 16,
GLES2: "#version 100\n\nstruct Block\n{\n vec2 scale;\n vec2 offset;\n};\n\nuniform Block _40;\n\nattribute vec2 pos;\nvarying vec2 vUV;\nattribute vec2 uv;\n\nvoid main()\n{\n vec2 p = pos;\n p.y = -p.y;\n gl_Position = vec4(p, 0.0, 1.0);\n vUV = (uv * _40.scale) + _40.offset;\n}\n\n",
/*
@@ -489,18 +450,8 @@ var (
HLSL: []byte(nil),
}
shader_stencil_vert = backend.ShaderSources{
Inputs: []backend.InputLocation{
{Name: "corner", Location: 0, Semantic: "POSITION", SemanticIndex: 0, Type: 0, Size: 2},
{Name: "maxy", Location: 1, Semantic: "NORMAL", SemanticIndex: 0, Type: 0, Size: 1},
{Name: "from", Location: 2, Semantic: "TEXCOORD", SemanticIndex: 0, Type: 0, Size: 2},
{Name: "ctrl", Location: 3, Semantic: "TEXCOORD", SemanticIndex: 1, Type: 0, Size: 2},
{Name: "to", Location: 4, Semantic: "TEXCOORD", SemanticIndex: 2, Type: 0, Size: 2},
},
Uniforms: []backend.UniformLocation{
{Name: "_15.scale", Type: 0, Size: 2, Offset: 0},
{Name: "_15.offset", Type: 0, Size: 2, Offset: 8},
{Name: "_15.pathOffset", Type: 0, Size: 2, Offset: 16},
},
Inputs: []backend.InputLocation{backend.InputLocation{Name: "corner", Location: 0, Semantic: "POSITION", SemanticIndex: 0, Type: 0x0, Size: 2}, backend.InputLocation{Name: "maxy", Location: 1, Semantic: "NORMAL", SemanticIndex: 0, Type: 0x0, Size: 1}, backend.InputLocation{Name: "from", Location: 2, Semantic: "TEXCOORD", SemanticIndex: 0, Type: 0x0, Size: 2}, backend.InputLocation{Name: "ctrl", Location: 3, Semantic: "TEXCOORD", SemanticIndex: 1, Type: 0x0, Size: 2}, backend.InputLocation{Name: "to", Location: 4, Semantic: "TEXCOORD", SemanticIndex: 2, Type: 0x0, Size: 2}},
Uniforms: []backend.UniformLocation{backend.UniformLocation{Name: "_15.scale", Type: 0x0, Size: 2, Offset: 0}, backend.UniformLocation{Name: "_15.offset", Type: 0x0, Size: 2, Offset: 8}, backend.UniformLocation{Name: "_15.pathOffset", Type: 0x0, Size: 2, Offset: 16}},
UniformSize: 24,
GLES2: "#version 100\n\nstruct Block\n{\n vec2 scale;\n vec2 offset;\n vec2 pathOffset;\n};\n\nuniform Block _15;\n\nattribute vec2 from;\nattribute vec2 ctrl;\nattribute vec2 to;\nattribute float maxy;\nattribute vec2 corner;\nvarying vec2 vFrom;\nvarying vec2 vCtrl;\nvarying vec2 vTo;\n\nvoid main()\n{\n vec2 from_1 = from + _15.pathOffset;\n vec2 ctrl_1 = ctrl + _15.pathOffset;\n vec2 to_1 = to + _15.pathOffset;\n float maxy_1 = maxy + _15.pathOffset.y;\n vec2 pos;\n if (corner.x > 0.0)\n {\n pos.x = max(max(from_1.x, ctrl_1.x), to_1.x) + 1.0;\n }\n else\n {\n pos.x = min(min(from_1.x, ctrl_1.x), to_1.x) - 1.0;\n }\n if (corner.y > 0.0)\n {\n pos.y = maxy_1 + 1.0;\n }\n else\n {\n pos.y = min(min(from_1.y, ctrl_1.y), to_1.y) - 1.0;\n }\n vFrom = from_1 - pos;\n vCtrl = ctrl_1 - pos;\n vTo = to_1 - pos;\n pos *= _15.scale;\n pos += _15.offset;\n gl_Position = vec4(pos, 1.0, 1.0);\n}\n\n",
/*
+20 -63
View File
@@ -15,6 +15,8 @@ import (
"sort"
"strings"
"text/template"
"gioui.org/gpu/backend"
)
// This program generates shader variants for
@@ -28,38 +30,6 @@ type shaderArgs struct {
Header string
}
// TextureBinding matches gpu.TextureBinding.
type TextureBinding struct {
Name string
Binding int
}
// InputLocation matches gpu.InputLocation.
type InputLocation struct {
Name string
Location int
Semantic string
SemanticIndex int
Type DataType
Size int
}
type DataType uint8
// UniformLocation matches gpu.UniformLocation.
type UniformLocation struct {
Name string
Type DataType
Size int
Offset int
}
const (
DataTypeFloat DataType = iota
DataTypeShort
)
func main() {
flag.Parse()
if err := generate(); err != nil {
@@ -97,9 +67,9 @@ func generate() error {
gles2 string
hlslSrc string
hlsl []byte
inputs []InputLocation
uniforms []UniformLocation
textures []TextureBinding
inputs []backend.InputLocation
uniforms []backend.UniformLocation
textures []backend.TextureBinding
uniformSize int
}
args := [nvariants]shaderArgs{
@@ -163,29 +133,16 @@ func generate() error {
for _, src := range variants {
fmt.Fprintf(&out, "backend.ShaderSources{\n")
if len(src.inputs) > 0 {
fmt.Fprintf(&out, "Inputs: []backend.InputLocation{\n")
for _, inp := range src.inputs {
fmt.Fprintf(&out, "{Name: %q, Location: %d, Semantic: %q, ", inp.Name, inp.Location, inp.Semantic)
fmt.Fprintf(&out, "SemanticIndex: %d, Type: %d, Size: %d},\n", inp.SemanticIndex, inp.Type, inp.Size)
}
fmt.Fprintf(&out, "},\n")
fmt.Fprintf(&out, "Inputs: %#v,\n", src.inputs)
}
if len(src.uniforms) > 0 {
fmt.Fprintf(&out, "Uniforms: []backend.UniformLocation{\n")
for _, u := range src.uniforms {
fmt.Fprintf(&out, "{Name: %q, Type: %d, Size: %d, Offset: %d},\n", u.Name, u.Type, u.Size, u.Offset)
}
fmt.Fprintf(&out, "},\n")
fmt.Fprintf(&out, "Uniforms: %#v,\n", src.uniforms)
}
if src.uniformSize != 0 {
fmt.Fprintf(&out, "UniformSize: %d,\n", src.uniformSize)
}
if len(src.textures) > 0 {
fmt.Fprintf(&out, "Textures: []backend.TextureBinding{\n")
for _, t := range src.textures {
fmt.Fprintf(&out, "{Name: %q, Binding: %d},\n", t.Name, t.Binding)
}
fmt.Fprintf(&out, "},\n")
fmt.Fprintf(&out, "Textures: %#v,\n", src.textures)
}
fmt.Fprintf(&out, "GLES2: %#v,\n", src.gles2)
fmt.Fprintf(&out, "/*\n%s\n*/\n", src.hlslSrc)
@@ -211,7 +168,7 @@ func generate() error {
return ioutil.WriteFile("shaders.go", gosrc, 0644)
}
func parseReflection(jsonData []byte) ([]InputLocation, []UniformLocation, []TextureBinding, int, error) {
func parseReflection(jsonData []byte) ([]backend.InputLocation, []backend.UniformLocation, []backend.TextureBinding, int, error) {
type InputReflection struct {
ID int `json:"id"`
Name string `json:"name"`
@@ -255,14 +212,14 @@ func parseReflection(jsonData []byte) ([]InputLocation, []UniformLocation, []Tex
if err := json.Unmarshal(jsonData, &reflect); err != nil {
return nil, nil, nil, 0, fmt.Errorf("parseReflection: %v", err)
}
var inputs []InputLocation
var inputs []backend.InputLocation
inputRef := reflect.VS.Inputs
for _, input := range inputRef {
dataType, dataSize, err := parseDataType(input.Type)
if err != nil {
return nil, nil, nil, 0, fmt.Errorf("parseReflection: %v", err)
}
inputs = append(inputs, InputLocation{
inputs = append(inputs, backend.InputLocation{
Name: input.Name,
Location: input.Location,
Semantic: input.Semantic,
@@ -274,7 +231,7 @@ func parseReflection(jsonData []byte) ([]InputLocation, []UniformLocation, []Tex
sort.Slice(inputs, func(i, j int) bool {
return inputs[i].Location < inputs[j].Location
})
var ublocks []UniformLocation
var ublocks []backend.UniformLocation
shaderBlocks := reflect.VS.UniformBuffers
if len(shaderBlocks) == 0 {
shaderBlocks = reflect.FS.UniformBuffers
@@ -286,7 +243,7 @@ func parseReflection(jsonData []byte) ([]InputLocation, []UniformLocation, []Tex
if err != nil {
return nil, nil, nil, 0, fmt.Errorf("parseReflection: %v", err)
}
ublocks = append(ublocks, UniformLocation{
ublocks = append(ublocks, backend.UniformLocation{
// Synthetic name generated by glslcc.
Name: fmt.Sprintf("_%d.%s", block.ID, member.Name),
Type: dataType,
@@ -300,9 +257,9 @@ func parseReflection(jsonData []byte) ([]InputLocation, []UniformLocation, []Tex
if len(textures) == 0 {
textures = reflect.FS.Textures
}
var texBinds []TextureBinding
var texBinds []backend.TextureBinding
for _, texture := range textures {
texBinds = append(texBinds, TextureBinding{
texBinds = append(texBinds, backend.TextureBinding{
Name: texture.Name,
Binding: texture.Binding,
})
@@ -310,16 +267,16 @@ func parseReflection(jsonData []byte) ([]InputLocation, []UniformLocation, []Tex
return inputs, ublocks, texBinds, blockOffset, nil
}
func parseDataType(t string) (DataType, int, error) {
func parseDataType(t string) (backend.DataType, int, error) {
switch t {
case "float":
return DataTypeFloat, 1, nil
return backend.DataTypeFloat, 1, nil
case "float2":
return DataTypeFloat, 2, nil
return backend.DataTypeFloat, 2, nil
case "float3":
return DataTypeFloat, 3, nil
return backend.DataTypeFloat, 3, nil
case "float4":
return DataTypeFloat, 4, nil
return backend.DataTypeFloat, 4, nil
default:
return 0, 0, fmt.Errorf("unsupported input data type: %s", t)
}