From 0d266c413d58eade4842f5eead71709161608dfd Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Tue, 25 Feb 2020 18:52:28 +0100 Subject: [PATCH] gpu,gpu/backend: implement GLSL 300 es shader variants Signed-off-by: Elias Naur --- app/internal/srgb/srgb.go | 12 ++-- gpu/backend/backend.go | 23 ++++++-- gpu/gl/backend.go | 81 +++++++++++++++++++------- gpu/gl/gl.go | 4 ++ gpu/gl/util.go | 10 ++-- gpu/shaders.go | 88 +++++++++++++++++++---------- internal/cmd/convertshaders/main.go | 81 +++++++++++++------------- 7 files changed, 190 insertions(+), 109 deletions(-) diff --git a/app/internal/srgb/srgb.go b/app/internal/srgb/srgb.go index 6153a19a..c148b04f 100644 --- a/app/internal/srgb/srgb.go +++ b/app/internal/srgb/srgb.go @@ -24,18 +24,18 @@ type FBO struct { blitted bool quad gl.Buffer prog gl.Program - es3 bool + gl3 bool } func New(f *glimpl.Functions) (*FBO, error) { - var es3 bool + var gl3 bool glVer := f.GetString(gl.VERSION) - ver, err := gl.ParseGLVersion(glVer) + ver, _, err := gl.ParseGLVersion(glVer) if err != nil { return nil, err } if ver[0] >= 3 { - es3 = true + gl3 = true } else { exts := f.GetString(gl.EXTENSIONS) if !strings.Contains(exts, "EXT_sRGB") { @@ -44,7 +44,7 @@ func New(f *glimpl.Functions) (*FBO, error) { } s := &FBO{ c: f, - es3: es3, + gl3: gl3, frameBuffer: f.CreateFramebuffer(), colorTex: f.CreateTexture(), depthBuffer: f.CreateRenderbuffer(), @@ -108,7 +108,7 @@ func (s *FBO) Refresh(w, h int) error { return nil } s.c.BindTexture(gl.TEXTURE_2D, s.colorTex) - if s.es3 { + if s.gl3 { s.c.TexImage2D(gl.TEXTURE_2D, 0, gl.SRGB8_ALPHA8, w, h, gl.RGBA, gl.UNSIGNED_BYTE, nil) } else /* EXT_sRGB */ { s.c.TexImage2D(gl.TEXTURE_2D, 0, gl.SRGB_ALPHA_EXT, w, h, gl.SRGB_ALPHA_EXT, gl.UNSIGNED_BYTE, nil) diff --git a/gpu/backend/backend.go b/gpu/backend/backend.go index b65a2dc9..cd67bc89 100644 --- a/gpu/backend/backend.go +++ b/gpu/backend/backend.go @@ -47,12 +47,18 @@ type Device interface { } type ShaderSources struct { - GLES2 string - HLSL []byte - Uniforms []UniformLocation - UniformSize int - Inputs []InputLocation - Textures []TextureBinding + GLSL100ES string + GLSL300ES string + HLSL []byte + Uniforms UniformsReflection + Inputs []InputLocation + Textures []TextureBinding +} + +type UniformsReflection struct { + Blocks []UniformBlock + Locations []UniformLocation + Size int } type TextureBinding struct { @@ -60,6 +66,11 @@ type TextureBinding struct { Binding int } +type UniformBlock struct { + Name string + Binding int +} + type UniformLocation struct { Name string Type DataType diff --git a/gpu/gl/backend.go b/gpu/gl/backend.go index 5b0de58d..1b9f5c08 100644 --- a/gpu/gl/backend.go +++ b/gpu/gl/backend.go @@ -19,7 +19,8 @@ type Backend struct { state glstate - feats backend.Caps + gles300 bool + feats backend.Caps // floatTriple holds the settings for floating point // textures. floatTriple textureTriple @@ -67,6 +68,7 @@ type gpuFramebuffer struct { type gpuBuffer struct { backend *Backend + hasBuffer bool obj Buffer typ backend.BufferBinding size int @@ -115,7 +117,7 @@ type textureTriple struct { func NewBackend(f Functions) (*Backend, error) { exts := strings.Split(f.GetString(EXTENSIONS), " ") glVer := f.GetString(VERSION) - ver, err := ParseGLVersion(glVer) + ver, gles, err := ParseGLVersion(glVer) if err != nil { return nil, err } @@ -127,7 +129,9 @@ func NewBackend(f Functions) (*Backend, error) { if err != nil { return nil, err } + gles300 := gles && ver[0] >= 3 b := &Backend{ + gles300: gles300, funcs: f, floatTriple: floatTriple, alphaTriple: alphaTripleFor(ver), @@ -236,10 +240,13 @@ func (b *Backend) NewBuffer(typ backend.BufferBinding, size int) (backend.Buffer if typ != backend.BufferBindingUniforms { return nil, errors.New("uniforms buffers cannot be bound as anything else") } - // GLES 2 doesn't support uniform buffers. - buf.data = make([]byte, size) + if !b.gles300 { + // GLES 2 doesn't support uniform buffers. + buf.data = make([]byte, size) + } } - if typ&^backend.BufferBindingUniforms != 0 { + if typ&^backend.BufferBindingUniforms != 0 || b.gles300 { + buf.hasBuffer = true buf.obj = b.funcs.CreateBuffer() if err := glErr(b.funcs); err != nil { buf.Release() @@ -252,7 +259,7 @@ func (b *Backend) NewBuffer(typ backend.BufferBinding, size int) (backend.Buffer func (b *Backend) NewImmutableBuffer(typ backend.BufferBinding, data []byte) (backend.Buffer, error) { glErr(b.funcs) obj := b.funcs.CreateBuffer() - buf := &gpuBuffer{backend: b, obj: obj, typ: typ, size: len(data)} + buf := &gpuBuffer{backend: b, obj: obj, typ: typ, size: len(data), hasBuffer: true} buf.Upload(data) buf.immutable = true if err := glErr(b.funcs); err != nil { @@ -419,12 +426,16 @@ func (b *Backend) NewInputLayout(vs backend.ShaderSources, layout []backend.Inpu }, nil } -func (b *Backend) NewProgram(vssrc, fssrc backend.ShaderSources) (backend.Program, error) { - attr := make([]string, len(vssrc.Inputs)) - for _, inp := range vssrc.Inputs { +func (b *Backend) NewProgram(vertShader, fragShader backend.ShaderSources) (backend.Program, error) { + attr := make([]string, len(vertShader.Inputs)) + for _, inp := range vertShader.Inputs { attr[inp.Location] = inp.Name } - p, err := CreateProgram(b.funcs, vssrc.GLES2, fssrc.GLES2, attr) + vsrc, fsrc := vertShader.GLSL100ES, fragShader.GLSL100ES + if b.gles300 { + vsrc, fsrc = vertShader.GLSL300ES, fragShader.GLSL300ES + } + p, err := CreateProgram(b.funcs, vsrc, fsrc, attr) if err != nil { return nil, err } @@ -435,20 +446,39 @@ func (b *Backend) NewProgram(vssrc, fssrc backend.ShaderSources) (backend.Progra } b.BindProgram(gpuProg) // Bind texture uniforms. - for _, tex := range vssrc.Textures { + for _, tex := range vertShader.Textures { u := b.funcs.GetUniformLocation(p, tex.Name) if u.valid() { b.funcs.Uniform1i(u, tex.Binding) } } - for _, tex := range fssrc.Textures { + for _, tex := range fragShader.Textures { u := b.funcs.GetUniformLocation(p, tex.Name) if u.valid() { b.funcs.Uniform1i(u, tex.Binding) } } - gpuProg.vertUniforms.setup(b.funcs, p, vssrc.UniformSize, vssrc.Uniforms) - gpuProg.fragUniforms.setup(b.funcs, p, fssrc.UniformSize, fssrc.Uniforms) + if b.gles300 { + for _, block := range vertShader.Uniforms.Blocks { + blockIdx := b.funcs.GetUniformBlockIndex(p, block.Name) + if blockIdx != INVALID_INDEX { + b.funcs.UniformBlockBinding(p, blockIdx, uint(block.Binding)) + } + } + // To match Direct3D 11 with separate vertex and fragment + // shader uniform buffers, offset all fragment blocks to be + // located after the vertex blocks. + off := len(vertShader.Uniforms.Blocks) + for _, block := range fragShader.Uniforms.Blocks { + blockIdx := b.funcs.GetUniformBlockIndex(p, block.Name) + if blockIdx != INVALID_INDEX { + b.funcs.UniformBlockBinding(p, blockIdx, uint(block.Binding+off)) + } + } + } else { + gpuProg.vertUniforms.setup(b.funcs, p, vertShader.Uniforms.Size, vertShader.Uniforms.Locations) + gpuProg.fragUniforms.setup(b.funcs, p, fragShader.Uniforms.Size, fragShader.Uniforms.Locations) + } return gpuProg, nil } @@ -466,8 +496,18 @@ func (p *gpuProgram) SetFragmentUniforms(buffer backend.Buffer) { } func (p *gpuProgram) updateUniforms() { - p.vertUniforms.update(p.backend.funcs) - p.fragUniforms.update(p.backend.funcs) + f := p.backend.funcs + if p.backend.gles300 { + if b := p.vertUniforms.buf; b != nil { + f.BindBufferBase(UNIFORM_BUFFER, 0, b.obj) + } + if b := p.fragUniforms.buf; b != nil { + f.BindBufferBase(UNIFORM_BUFFER, 1, b.obj) + } + } else { + p.vertUniforms.update(f) + p.fragUniforms.update(f) + } } func (b *Backend) BindProgram(prog backend.Program) { @@ -540,10 +580,8 @@ func (b *gpuBuffer) Upload(data []byte) { panic("buffer size overflow") } b.version++ - if b.typ&backend.BufferBindingUniforms != 0 { - copy(b.data, data) - } - if b.typ&^backend.BufferBindingUniforms != 0 { + copy(b.data, data) + if b.hasBuffer { firstBinding := firstBufferType(b.typ) b.backend.funcs.BindBuffer(firstBinding, b.obj) b.backend.funcs.BufferData(firstBinding, data, STATIC_DRAW) @@ -551,8 +589,9 @@ func (b *gpuBuffer) Upload(data []byte) { } func (b *gpuBuffer) Release() { - if b.typ&^backend.BufferBindingUniforms != 0 { + if b.hasBuffer { b.backend.funcs.DeleteBuffer(b.obj) + b.hasBuffer = false } } diff --git a/gpu/gl/gl.go b/gpu/gl/gl.go index 41cb67eb..1508a1cd 100644 --- a/gpu/gl/gl.go +++ b/gpu/gl/gl.go @@ -33,6 +33,7 @@ const ( HALF_FLOAT = 0x140b HALF_FLOAT_OES = 0x8d61 INFO_LOG_LENGTH = 0x8B84 + INVALID_INDEX = ^uint(0) GREATER = 0x204 LINEAR = 0x2601 LINK_STATUS = 0x8b82 @@ -92,6 +93,7 @@ type Functions interface { BeginQuery(target Enum, query Query) BindAttribLocation(p Program, a Attrib, name string) BindBuffer(target Enum, b Buffer) + BindBufferBase(target Enum, index int, buffer Buffer) BindFramebuffer(target Enum, fb Framebuffer) BindRenderbuffer(target Enum, fb Renderbuffer) BindTexture(target Enum, t Texture) @@ -136,6 +138,7 @@ type Functions interface { GetShaderi(s Shader, pname Enum) int GetShaderInfoLog(s Shader) string GetString(pname Enum) string + GetUniformBlockIndex(p Program, name string) uint GetUniformLocation(p Program, name string) Uniform InvalidateFramebuffer(target, attachment Enum) LinkProgram(p Program) @@ -144,6 +147,7 @@ type Functions interface { ShaderSource(s Shader, src string) TexImage2D(target Enum, level int, internalFormat int, width, height int, format, ty Enum, data []byte) TexParameteri(target, pname Enum, param int) + UniformBlockBinding(p Program, uniformBlockIndex uint, uniformBlockBinding uint) Uniform1f(dst Uniform, v float32) Uniform1i(dst Uniform, v int) Uniform2f(dst Uniform, v0, v1 float32) diff --git a/gpu/gl/util.go b/gpu/gl/util.go index 7a7afa77..65b21557 100644 --- a/gpu/gl/util.go +++ b/gpu/gl/util.go @@ -60,16 +60,16 @@ func createShader(ctx Functions, typ Enum, src string) (Shader, error) { return sh, nil } -func ParseGLVersion(glVer string) ([2]int, error) { +func ParseGLVersion(glVer string) (version [2]int, gles bool, err error) { var ver [2]int if _, err := fmt.Sscanf(glVer, "OpenGL ES %d.%d", &ver[0], &ver[1]); err == nil { - return ver, nil + return ver, true, nil } else if _, err := fmt.Sscanf(glVer, "WebGL %d.%d", &ver[0], &ver[1]); err == nil { // WebGL major version v corresponds to OpenGL ES version v + 1 ver[0]++ - return ver, nil + return ver, true, nil } else if _, err := fmt.Sscanf(glVer, "%d.%d", &ver[0], &ver[1]); err == nil { - return ver, nil + return ver, false, nil } - return ver, fmt.Errorf("failed to parse OpenGL ES version (%s)", glVer) + return ver, false, fmt.Errorf("failed to parse OpenGL ES version (%s)", glVer) } diff --git a/gpu/shaders.go b/gpu/shaders.go index 1b5c2d04..6ae2541f 100644 --- a/gpu/shaders.go +++ b/gpu/shaders.go @@ -7,9 +7,13 @@ import "gioui.org/gpu/backend" var ( shader_blit_frag = [...]backend.ShaderSources{ backend.ShaderSources{ - 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", + Uniforms: backend.UniformsReflection{ + Blocks: []backend.UniformBlock{backend.UniformBlock{Name: "Color", Binding: 0}}, + Locations: []backend.UniformLocation{backend.UniformLocation{Name: "_12._color", Type: 0x0, Size: 4, Offset: 0}}, + Size: 16, + }, + GLSL100ES: "#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", + GLSL300ES: "#version 300 es\nprecision mediump float;\nprecision highp int;\n\nlayout(std140) uniform Color\n{\n vec4 _color;\n} _12;\n\nlayout(location = 0) out vec4 fragColor;\nin vec2 vUV;\n\nvoid main()\n{\n fragColor = _12._color;\n}\n\n", /* cbuffer Color : register(b0) { @@ -48,8 +52,9 @@ var ( HLSL: []byte(nil), }, backend.ShaderSources{ - 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", + Textures: []backend.TextureBinding{backend.TextureBinding{Name: "tex", Binding: 0}}, + GLSL100ES: "#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", + GLSL300ES: "#version 300 es\nprecision mediump float;\nprecision highp int;\n\nuniform mediump sampler2D tex;\n\nlayout(location = 0) out vec4 fragColor;\nin vec2 vUV;\n\nvoid main()\n{\n fragColor = texture(tex, vUV);\n}\n\n", /* Texture2D tex : register(t0); SamplerState _tex_sampler : register(s0); @@ -86,10 +91,14 @@ var ( }, } shader_blit_vert = backend.ShaderSources{ - 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", + 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.UniformsReflection{ + Blocks: []backend.UniformBlock{backend.UniformBlock{Name: "Block", Binding: 0}}, + Locations: []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}}, + Size: 40, + }, + GLSL100ES: "#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", + GLSL300ES: "#version 300 es\n\nlayout(std140) uniform Block\n{\n float z;\n vec2 scale;\n vec2 offset;\n vec2 uvScale;\n vec2 uvOffset;\n} _15;\n\nlayout(location = 0) in vec2 pos;\nout vec2 vUV;\nlayout(location = 1) in 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", /* cbuffer Block : register(b0) { @@ -143,10 +152,14 @@ var ( } shader_cover_frag = [...]backend.ShaderSources{ backend.ShaderSources{ - Uniforms: []backend.UniformLocation{backend.UniformLocation{Name: "_12._color", Type: 0x0, Size: 4, Offset: 0}}, - UniformSize: 16, - 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", + Uniforms: backend.UniformsReflection{ + Blocks: []backend.UniformBlock{backend.UniformBlock{Name: "Color", Binding: 0}}, + Locations: []backend.UniformLocation{backend.UniformLocation{Name: "_12._color", Type: 0x0, Size: 4, Offset: 0}}, + Size: 16, + }, + Textures: []backend.TextureBinding{backend.TextureBinding{Name: "cover", Binding: 1}}, + GLSL100ES: "#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", + GLSL300ES: "#version 300 es\nprecision mediump float;\nprecision highp int;\n\nlayout(std140) uniform Color\n{\n vec4 _color;\n} _12;\n\nuniform mediump sampler2D cover;\n\nlayout(location = 0) out vec4 fragColor;\nin highp vec2 vCoverUV;\nin vec2 vUV;\n\nvoid main()\n{\n fragColor = _12._color;\n float cover_1 = abs(texture(cover, vCoverUV).x);\n fragColor *= cover_1;\n}\n\n", /* cbuffer Color : register(b0) { @@ -192,8 +205,9 @@ var ( HLSL: []byte(nil), }, backend.ShaderSources{ - 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", + Textures: []backend.TextureBinding{backend.TextureBinding{Name: "tex", Binding: 0}, backend.TextureBinding{Name: "cover", Binding: 1}}, + GLSL100ES: "#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", + GLSL300ES: "#version 300 es\nprecision mediump float;\nprecision highp int;\n\nuniform mediump sampler2D tex;\nuniform mediump sampler2D cover;\n\nlayout(location = 0) out vec4 fragColor;\nin vec2 vUV;\nin highp vec2 vCoverUV;\n\nvoid main()\n{\n fragColor = texture(tex, vUV);\n float cover_1 = abs(texture(cover, vCoverUV).x);\n fragColor *= cover_1;\n}\n\n", /* Texture2D tex : register(t0); SamplerState _tex_sampler : register(s0); @@ -237,10 +251,14 @@ var ( }, } shader_cover_vert = backend.ShaderSources{ - 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", + 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.UniformsReflection{ + Blocks: []backend.UniformBlock{backend.UniformBlock{Name: "Block", Binding: 0}}, + Locations: []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}}, + Size: 56, + }, + GLSL100ES: "#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", + GLSL300ES: "#version 300 es\n\nlayout(std140) uniform Block\n{\n float z;\n vec2 scale;\n vec2 offset;\n vec2 uvCoverScale;\n vec2 uvCoverOffset;\n vec2 uvScale;\n vec2 uvOffset;\n} _19;\n\nlayout(location = 0) in vec2 pos;\nout vec2 vUV;\nlayout(location = 1) in vec2 uv;\nout 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", /* cbuffer Block : register(b0) { @@ -296,8 +314,9 @@ var ( HLSL: []byte(nil), } shader_intersect_frag = backend.ShaderSources{ - 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", + Textures: []backend.TextureBinding{backend.TextureBinding{Name: "cover", Binding: 0}}, + GLSL100ES: "#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", + GLSL300ES: "#version 300 es\nprecision mediump float;\nprecision highp int;\n\nuniform mediump sampler2D cover;\n\nin highp vec2 vUV;\nlayout(location = 0) out vec4 fragColor;\n\nvoid main()\n{\n float cover_1 = abs(texture(cover, vUV).x);\n fragColor.x = cover_1;\n}\n\n", /* Texture2D cover : register(t0); SamplerState _cover_sampler : register(s0); @@ -334,10 +353,14 @@ var ( HLSL: []byte(nil), } shader_intersect_vert = backend.ShaderSources{ - 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", + 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.UniformsReflection{ + Blocks: []backend.UniformBlock{backend.UniformBlock{Name: "Block", Binding: 0}}, + Locations: []backend.UniformLocation{backend.UniformLocation{Name: "_40.scale", Type: 0x0, Size: 2, Offset: 0}, backend.UniformLocation{Name: "_40.offset", Type: 0x0, Size: 2, Offset: 8}}, + Size: 16, + }, + GLSL100ES: "#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", + GLSL300ES: "#version 300 es\n\nlayout(std140) uniform Block\n{\n vec2 scale;\n vec2 offset;\n} _40;\n\nlayout(location = 0) in vec2 pos;\nout vec2 vUV;\nlayout(location = 1) in 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", /* cbuffer Block : register(b0) { @@ -386,7 +409,8 @@ var ( HLSL: []byte(nil), } shader_stencil_frag = backend.ShaderSources{ - GLES2: "#version 100\nprecision mediump float;\nprecision highp int;\n\nvarying vec2 vTo;\nvarying vec2 vFrom;\nvarying vec2 vCtrl;\n\nvoid main()\n{\n float dx = vTo.x - vFrom.x;\n bool increasing = vTo.x >= vFrom.x;\n bvec2 _35 = bvec2(increasing);\n vec2 left = vec2(_35.x ? vFrom.x : vTo.x, _35.y ? vFrom.y : vTo.y);\n bvec2 _41 = bvec2(increasing);\n vec2 right = vec2(_41.x ? vTo.x : vFrom.x, _41.y ? vTo.y : vFrom.y);\n vec2 extent = clamp(vec2(vFrom.x, vTo.x), vec2(-0.5), vec2(0.5));\n float midx = mix(extent.x, extent.y, 0.5);\n float x0 = midx - left.x;\n vec2 p1 = vCtrl - left;\n vec2 v = right - vCtrl;\n float t = x0 / (p1.x + sqrt((p1.x * p1.x) + ((v.x - p1.x) * x0)));\n float y = mix(mix(left.y, vCtrl.y, t), mix(vCtrl.y, right.y, t), t);\n vec2 d_half = mix(p1, v, vec2(t));\n float dy = d_half.y / d_half.x;\n float width = extent.y - extent.x;\n dy = abs(dy * width);\n vec4 sides = vec4((dy * 0.5) + y, (dy * (-0.5)) + y, (0.5 - y) / dy, ((-0.5) - y) / dy);\n sides = clamp(sides + vec4(0.5), vec4(0.0), vec4(1.0));\n float area = 0.5 * ((((sides.z - (sides.z * sides.y)) + 1.0) - sides.x) + (sides.x * sides.w));\n area *= width;\n if (width == 0.0)\n {\n area = 0.0;\n }\n gl_FragData[0].x = area;\n}\n\n", + GLSL100ES: "#version 100\nprecision mediump float;\nprecision highp int;\n\nvarying vec2 vTo;\nvarying vec2 vFrom;\nvarying vec2 vCtrl;\n\nvoid main()\n{\n float dx = vTo.x - vFrom.x;\n bool increasing = vTo.x >= vFrom.x;\n bvec2 _35 = bvec2(increasing);\n vec2 left = vec2(_35.x ? vFrom.x : vTo.x, _35.y ? vFrom.y : vTo.y);\n bvec2 _41 = bvec2(increasing);\n vec2 right = vec2(_41.x ? vTo.x : vFrom.x, _41.y ? vTo.y : vFrom.y);\n vec2 extent = clamp(vec2(vFrom.x, vTo.x), vec2(-0.5), vec2(0.5));\n float midx = mix(extent.x, extent.y, 0.5);\n float x0 = midx - left.x;\n vec2 p1 = vCtrl - left;\n vec2 v = right - vCtrl;\n float t = x0 / (p1.x + sqrt((p1.x * p1.x) + ((v.x - p1.x) * x0)));\n float y = mix(mix(left.y, vCtrl.y, t), mix(vCtrl.y, right.y, t), t);\n vec2 d_half = mix(p1, v, vec2(t));\n float dy = d_half.y / d_half.x;\n float width = extent.y - extent.x;\n dy = abs(dy * width);\n vec4 sides = vec4((dy * 0.5) + y, (dy * (-0.5)) + y, (0.5 - y) / dy, ((-0.5) - y) / dy);\n sides = clamp(sides + vec4(0.5), vec4(0.0), vec4(1.0));\n float area = 0.5 * ((((sides.z - (sides.z * sides.y)) + 1.0) - sides.x) + (sides.x * sides.w));\n area *= width;\n if (width == 0.0)\n {\n area = 0.0;\n }\n gl_FragData[0].x = area;\n}\n\n", + GLSL300ES: "#version 300 es\nprecision mediump float;\nprecision highp int;\n\nin vec2 vTo;\nin vec2 vFrom;\nin vec2 vCtrl;\nlayout(location = 0) out vec4 fragCover;\n\nvoid main()\n{\n float dx = vTo.x - vFrom.x;\n bool increasing = vTo.x >= vFrom.x;\n bvec2 _35 = bvec2(increasing);\n vec2 left = vec2(_35.x ? vFrom.x : vTo.x, _35.y ? vFrom.y : vTo.y);\n bvec2 _41 = bvec2(increasing);\n vec2 right = vec2(_41.x ? vTo.x : vFrom.x, _41.y ? vTo.y : vFrom.y);\n vec2 extent = clamp(vec2(vFrom.x, vTo.x), vec2(-0.5), vec2(0.5));\n float midx = mix(extent.x, extent.y, 0.5);\n float x0 = midx - left.x;\n vec2 p1 = vCtrl - left;\n vec2 v = right - vCtrl;\n float t = x0 / (p1.x + sqrt((p1.x * p1.x) + ((v.x - p1.x) * x0)));\n float y = mix(mix(left.y, vCtrl.y, t), mix(vCtrl.y, right.y, t), t);\n vec2 d_half = mix(p1, v, vec2(t));\n float dy = d_half.y / d_half.x;\n float width = extent.y - extent.x;\n dy = abs(dy * width);\n vec4 sides = vec4((dy * 0.5) + y, (dy * (-0.5)) + y, (0.5 - y) / dy, ((-0.5) - y) / dy);\n sides = clamp(sides + vec4(0.5), vec4(0.0), vec4(1.0));\n float area = 0.5 * ((((sides.z - (sides.z * sides.y)) + 1.0) - sides.x) + (sides.x * sides.w));\n area *= width;\n if (width == 0.0)\n {\n area = 0.0;\n }\n fragCover.x = area;\n}\n\n", /* static float2 vTo; static float2 vFrom; @@ -450,10 +474,14 @@ var ( HLSL: []byte(nil), } shader_stencil_vert = backend.ShaderSources{ - 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", + 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.UniformsReflection{ + Blocks: []backend.UniformBlock{backend.UniformBlock{Name: "Block", Binding: 0}}, + Locations: []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}}, + Size: 24, + }, + GLSL100ES: "#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", + GLSL300ES: "#version 300 es\n\nlayout(std140) uniform Block\n{\n vec2 scale;\n vec2 offset;\n vec2 pathOffset;\n} _15;\n\nlayout(location = 2) in vec2 from;\nlayout(location = 3) in vec2 ctrl;\nlayout(location = 4) in vec2 to;\nlayout(location = 1) in float maxy;\nlayout(location = 0) in vec2 corner;\nout vec2 vFrom;\nout vec2 vCtrl;\nout 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", /* cbuffer Block : register(b0) { diff --git a/internal/cmd/convertshaders/main.go b/internal/cmd/convertshaders/main.go index d84cee34..fc3065b4 100644 --- a/internal/cmd/convertshaders/main.go +++ b/internal/cmd/convertshaders/main.go @@ -64,13 +64,8 @@ func generate() error { for _, shader := range shaders { const nvariants = 2 var variants [nvariants]struct { - gles2 string - hlslSrc string - hlsl []byte - inputs []backend.InputLocation - uniforms []backend.UniformLocation - textures []backend.TextureBinding - uniformSize int + backend.ShaderSources + hlslSrc string } args := [nvariants]shaderArgs{ { @@ -83,13 +78,16 @@ func generate() error { }, } for i := range args { - gles2, reflect, err := convertShader(tmp, glslcc, shader, "gles", "100", &args[i], false) + glsl100, reflect, err := convertShader(tmp, glslcc, shader, "gles", "100", &args[i], false) if err != nil { return err } // Make the GL ES 2 source compatible with desktop GL 3. - gles2 = "#version 100\n" + gles2 - inputs, uniforms, textures, uniformSize, err := parseReflection(reflect) + glsl100 = "#version 100\n" + glsl100 + if err := parseReflection(reflect, &variants[i].ShaderSources); err != nil { + return err + } + glsl300, _, err := convertShader(tmp, glslcc, shader, "gles", "300", &args[i], false) if err != nil { return err } @@ -113,40 +111,39 @@ func generate() error { return err } } - variants[i].gles2 = gles2 + variants[i].GLSL100ES = glsl100 + variants[i].GLSL300ES = glsl300 variants[i].hlslSrc = hlsl - variants[i].hlsl = hlslc - variants[i].inputs = inputs - variants[i].uniforms = uniforms - variants[i].textures = textures - variants[i].uniformSize = uniformSize + variants[i].HLSL = hlslc } name := filepath.Base(shader) name = strings.ReplaceAll(name, ".", "_") fmt.Fprintf(&out, "\tshader_%s = ", name) // If the shader don't use the variant arguments, output // only a single version. - multiVariant := variants[0].gles2 != variants[1].gles2 + multiVariant := variants[0].GLSL100ES != variants[1].GLSL100ES if multiVariant { fmt.Fprintf(&out, "[...]backend.ShaderSources{\n") } for _, src := range variants { fmt.Fprintf(&out, "backend.ShaderSources{\n") - if len(src.inputs) > 0 { - fmt.Fprintf(&out, "Inputs: %#v,\n", src.inputs) + if len(src.Inputs) > 0 { + fmt.Fprintf(&out, "Inputs: %#v,\n", src.Inputs) } - if len(src.uniforms) > 0 { - fmt.Fprintf(&out, "Uniforms: %#v,\n", src.uniforms) + if u := src.Uniforms; len(u.Blocks) > 0 { + fmt.Fprintf(&out, "Uniforms: backend.UniformsReflection{\n") + fmt.Fprintf(&out, "Blocks: %#v,\n", u.Blocks) + fmt.Fprintf(&out, "Locations: %#v,\n", u.Locations) + fmt.Fprintf(&out, "Size: %d,\n", u.Size) + fmt.Fprintf(&out, "},\n") } - if src.uniformSize != 0 { - fmt.Fprintf(&out, "UniformSize: %d,\n", src.uniformSize) + if len(src.Textures) > 0 { + fmt.Fprintf(&out, "Textures: %#v,\n", src.Textures) } - if len(src.textures) > 0 { - fmt.Fprintf(&out, "Textures: %#v,\n", src.textures) - } - fmt.Fprintf(&out, "GLES2: %#v,\n", src.gles2) + fmt.Fprintf(&out, "GLSL100ES: %#v,\n", src.GLSL100ES) + fmt.Fprintf(&out, "GLSL300ES: %#v,\n", src.GLSL300ES) fmt.Fprintf(&out, "/*\n%s\n*/\n", src.hlslSrc) - fmt.Fprintf(&out, "HLSL: %#v,\n", src.hlsl) + fmt.Fprintf(&out, "HLSL: %#v,\n", src.HLSL) fmt.Fprintf(&out, "}") if multiVariant { fmt.Fprintf(&out, ",") @@ -168,7 +165,7 @@ func generate() error { return ioutil.WriteFile("shaders.go", gosrc, 0644) } -func parseReflection(jsonData []byte) ([]backend.InputLocation, []backend.UniformLocation, []backend.TextureBinding, int, error) { +func parseReflection(jsonData []byte, info *backend.ShaderSources) error { type InputReflection struct { ID int `json:"id"` Name string `json:"name"` @@ -210,16 +207,15 @@ func parseReflection(jsonData []byte) ([]backend.InputLocation, []backend.Unifor } var reflect shaderMetadata if err := json.Unmarshal(jsonData, &reflect); err != nil { - return nil, nil, nil, 0, fmt.Errorf("parseReflection: %v", err) + return fmt.Errorf("parseReflection: %v", err) } - 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) + return fmt.Errorf("parseReflection: %v", err) } - inputs = append(inputs, backend.InputLocation{ + info.Inputs = append(info.Inputs, backend.InputLocation{ Name: input.Name, Location: input.Location, Semantic: input.Semantic, @@ -228,22 +224,25 @@ func parseReflection(jsonData []byte) ([]backend.InputLocation, []backend.Unifor Size: dataSize, }) } - sort.Slice(inputs, func(i, j int) bool { - return inputs[i].Location < inputs[j].Location + sort.Slice(info.Inputs, func(i, j int) bool { + return info.Inputs[i].Location < info.Inputs[j].Location }) - var ublocks []backend.UniformLocation shaderBlocks := reflect.VS.UniformBuffers if len(shaderBlocks) == 0 { shaderBlocks = reflect.FS.UniformBuffers } blockOffset := 0 for _, block := range shaderBlocks { + info.Uniforms.Blocks = append(info.Uniforms.Blocks, backend.UniformBlock{ + Name: block.Name, + Binding: block.Binding, + }) for _, member := range block.Members { dataType, size, err := parseDataType(member.Type) if err != nil { - return nil, nil, nil, 0, fmt.Errorf("parseReflection: %v", err) + return fmt.Errorf("parseReflection: %v", err) } - ublocks = append(ublocks, backend.UniformLocation{ + info.Uniforms.Locations = append(info.Uniforms.Locations, backend.UniformLocation{ // Synthetic name generated by glslcc. Name: fmt.Sprintf("_%d.%s", block.ID, member.Name), Type: dataType, @@ -253,18 +252,18 @@ func parseReflection(jsonData []byte) ([]backend.InputLocation, []backend.Unifor } blockOffset += block.Size } + info.Uniforms.Size = blockOffset textures := reflect.VS.Textures if len(textures) == 0 { textures = reflect.FS.Textures } - var texBinds []backend.TextureBinding for _, texture := range textures { - texBinds = append(texBinds, backend.TextureBinding{ + info.Textures = append(info.Textures, backend.TextureBinding{ Name: texture.Name, Binding: texture.Binding, }) } - return inputs, ublocks, texBinds, blockOffset, nil + return nil } func parseDataType(t string) (backend.DataType, int, error) {