forked from joejulian/gio
app/internal/cmd/convertshaders: use backend types directly
Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user