From d331f63d20c706cba1688d76c68241fc1844f160 Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Wed, 5 May 2021 11:22:05 +0200 Subject: [PATCH] gpu: [compute] move material clip space transformation to the GPU Signed-off-by: Elias Naur --- gpu/compute.go | 32 ++++++++++++++++++++++++-------- gpu/shaders.go | 39 ++++++++++++++++++++++++++++++++++----- gpu/shaders/material.vert | 7 ++++++- 3 files changed, 64 insertions(+), 14 deletions(-) diff --git a/gpu/compute.go b/gpu/compute.go index 50e81460..2167a588 100644 --- a/gpu/compute.go +++ b/gpu/compute.go @@ -77,6 +77,9 @@ type compute struct { quads []materialVertex buffer sizedBuffer + + uniforms *materialUniforms + uniBuf driver.Buffer } timers struct { profile string @@ -97,6 +100,11 @@ type compute struct { conf *config } +type materialUniforms struct { + scale [2]float32 + pos [2]float32 +} + // materialVertex describes a vertex of a quad used to render a transformed // material. type materialVertex struct { @@ -225,11 +233,20 @@ func newCompute(ctx driver.Device) (*compute, error) { return nil, err } g.materials.layout = progLayout + g.materials.uniforms = new(materialUniforms) + + buf, err := ctx.NewBuffer(driver.BufferBindingUniforms, int(unsafe.Sizeof(*g.materials.uniforms))) + if err != nil { + g.Release() + return nil, err + } + g.materials.uniBuf = buf + g.materials.prog.SetVertexUniforms(buf) g.drawOps.pathCache = newOpCache() g.drawOps.compute = true - buf, err := ctx.NewBuffer(driver.BufferBindingShaderStorage, int(unsafe.Sizeof(config{}))) + buf, err = ctx.NewBuffer(driver.BufferBindingShaderStorage, int(unsafe.Sizeof(config{}))) if err != nil { g.Release() return nil, err @@ -447,14 +464,10 @@ restart: } m.fbo = fbo } - // TODO: move to shaders. // Transform to clip space: [-1, -1] - [1, 1]. - clip := f32.Affine2D{}.Scale(f32.Pt(0, 0), f32.Pt(2/float32(texSize), 2/float32(texSize))).Offset(f32.Pt(-1, -1)) - for i, v := range m.quads { - p := clip.Transform(f32.Pt(v.posX, v.posY)) - m.quads[i].posX = p.X - m.quads[i].posY = p.Y - } + g.materials.uniforms.scale = [2]float32{2 / float32(texSize), 2 / float32(texSize)} + g.materials.uniforms.pos = [2]float32{-1, -1} + g.materials.uniBuf.Upload(byteslice.Struct(g.materials.uniforms)) vertexData := byteslice.Slice(m.quads) n := pow2Ceil(len(vertexData)) m.buffer.ensureCapacity(g.ctx, driver.BufferBindingVertices, n) @@ -935,6 +948,9 @@ func (g *compute) Release() { g.materials.tex.Release() } g.materials.buffer.release() + if b := g.materials.uniBuf; b != nil { + b.Release() + } if g.timers.t != nil { g.timers.t.release() } diff --git a/gpu/shaders.go b/gpu/shaders.go index b469bed1..1c59dced 100644 --- a/gpu/shaders.go +++ b/gpu/shaders.go @@ -5176,8 +5176,20 @@ void main() shader_material_vert = driver.ShaderSources{ Name: "material.vert", Inputs: []driver.InputLocation{{Name: "pos", Location: 0, Semantic: "TEXCOORD", SemanticIndex: 0, Type: 0x0, Size: 2}, {Name: "uv", Location: 1, Semantic: "TEXCOORD", SemanticIndex: 1, Type: 0x0, Size: 2}}, + Uniforms: driver.UniformsReflection{ + Blocks: []driver.UniformBlock{{Name: "Block", Binding: 0}}, + Locations: []driver.UniformLocation{{Name: "_block.transform", Type: 0x0, Size: 4, Offset: 0}}, + Size: 16, + }, GLSL100ES: `#version 100 +struct Block +{ + vec4 transform; +}; + +uniform Block _block; + varying vec2 vUV; attribute vec2 uv; attribute vec2 pos; @@ -5185,12 +5197,17 @@ attribute vec2 pos; void main() { vUV = uv; - gl_Position = vec4(pos, 0.0, 1.0); + gl_Position = vec4((pos * _block.transform.xy) + _block.transform.zw, 0.0, 1.0); } `, GLSL300ES: `#version 300 es +layout(std140) uniform Block +{ + vec4 transform; +} _block; + out vec2 vUV; layout(location = 1) in vec2 uv; layout(location = 0) in vec2 pos; @@ -5198,12 +5215,19 @@ layout(location = 0) in vec2 pos; void main() { vUV = uv; - gl_Position = vec4(pos, 0.0, 1.0); + gl_Position = vec4((pos * _block.transform.xy) + _block.transform.zw, 0.0, 1.0); } `, GLSL130: `#version 130 +struct Block +{ + vec4 transform; +}; + +uniform Block _block; + out vec2 vUV; in vec2 uv; in vec2 pos; @@ -5211,12 +5235,17 @@ in vec2 pos; void main() { vUV = uv; - gl_Position = vec4(pos, 0.0, 1.0); + gl_Position = vec4((pos * _block.transform.xy) + _block.transform.zw, 0.0, 1.0); } `, GLSL150: `#version 150 +layout(std140) uniform Block +{ + vec4 transform; +} _block; + out vec2 vUV; in vec2 uv; in vec2 pos; @@ -5224,11 +5253,11 @@ in vec2 pos; void main() { vUV = uv; - gl_Position = vec4(pos, 0.0, 1.0); + gl_Position = vec4((pos * _block.transform.xy) + _block.transform.zw, 0.0, 1.0); } `, - HLSL: "DXBCg\xc0\xae\x16\xd8\xe1\xbdl~ń\xf1\xc4\xf6dV\x01\x00\x00\x00\xc4\x02\x00\x00\x06\x00\x00\x008\x00\x00\x00\xc8\x00\x00\x00X\x01\x00\x00\xd4\x01\x00\x00 \x02\x00\x00l\x02\x00\x00Aon9\x88\x00\x00\x00\x88\x00\x00\x00\x00\x02\xfe\xff`\x00\x00\x00(\x00\x00\x00\x00\x00$\x00\x00\x00$\x00\x00\x00$\x00\x00\x00$\x00\x01\x00$\x00\x00\x00\x00\x00\x00\x02\xfe\xffQ\x00\x00\x05\x01\x00\x0f\xa0\x00\x00\x00\x00\x00\x00\x80?\x00\x00\x00\x00\x00\x00\x00\x00\x1f\x00\x00\x02\x05\x00\x00\x80\x00\x00\x0f\x90\x1f\x00\x00\x02\x05\x00\x01\x80\x01\x00\x0f\x90\x02\x00\x00\x03\x00\x00\x03\xc0\x00\x00\xe4\x90\x00\x00\xe4\xa0\x01\x00\x00\x02\x00\x00\x03\xe0\x01\x00\xe4\x90\x01\x00\x00\x02\x00\x00\f\xc0\x01\x00D\xa0\xff\xff\x00\x00SHDR\x88\x00\x00\x00@\x00\x01\x00\"\x00\x00\x00_\x00\x00\x032\x10\x10\x00\x00\x00\x00\x00_\x00\x00\x032\x10\x10\x00\x01\x00\x00\x00e\x00\x00\x032 \x10\x00\x00\x00\x00\x00g\x00\x00\x04\xf2 \x10\x00\x01\x00\x00\x00\x01\x00\x00\x006\x00\x00\x052 \x10\x00\x00\x00\x00\x00F\x10\x10\x00\x01\x00\x00\x006\x00\x00\x052 \x10\x00\x01\x00\x00\x00F\x10\x10\x00\x00\x00\x00\x006\x00\x00\b\xc2 \x10\x00\x01\x00\x00\x00\x02@\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80?>\x00\x00\x01STATt\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00RDEFD\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1c\x00\x00\x00\x00\x04\xfe\xff\x00\x01\x00\x00\x1c\x00\x00\x00Microsoft (R) HLSL Shader Compiler 10.1\x00ISGND\x00\x00\x00\x02\x00\x00\x00\b\x00\x00\x008\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x03\x03\x00\x008\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x01\x00\x00\x00\x03\x03\x00\x00TEXCOORD\x00\xab\xab\xabOSGNP\x00\x00\x00\x02\x00\x00\x00\b\x00\x00\x008\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x03\f\x00\x00A\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x03\x00\x00\x00\x01\x00\x00\x00\x0f\x00\x00\x00TEXCOORD\x00SV_Position\x00\xab\xab\xab", + HLSL: "DXBCq\x961\x05q\xd1`\xea\xd4\xd2\xe1g\x9e\x84\\0\x01\x00\x00\x00\x88\x03\x00\x00\x06\x00\x00\x008\x00\x00\x00\xe8\x00\x00\x00\xa0\x01\x00\x00\x1c\x02\x00\x00\xe4\x02\x00\x000\x03\x00\x00Aon9\xa8\x00\x00\x00\xa8\x00\x00\x00\x00\x02\xfe\xfft\x00\x00\x004\x00\x00\x00\x01\x00$\x00\x00\x000\x00\x00\x000\x00\x00\x00$\x00\x01\x000\x00\x00\x00\x00\x00\x01\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\xfe\xffQ\x00\x00\x05\x02\x00\x0f\xa0\x00\x00\x00\x00\x00\x00\x80?\x00\x00\x00\x00\x00\x00\x00\x00\x1f\x00\x00\x02\x05\x00\x00\x80\x00\x00\x0f\x90\x1f\x00\x00\x02\x05\x00\x01\x80\x01\x00\x0f\x90\x04\x00\x00\x04\x00\x00\x03\x80\x00\x00\xe4\x90\x01\x00\xe4\xa0\x01\x00\xee\xa0\x02\x00\x00\x03\x00\x00\x03\xc0\x00\x00\xe4\x80\x00\x00\xe4\xa0\x01\x00\x00\x02\x00\x00\x03\xe0\x01\x00\xe4\x90\x01\x00\x00\x02\x00\x00\f\xc0\x02\x00D\xa0\xff\xff\x00\x00SHDR\xb0\x00\x00\x00@\x00\x01\x00,\x00\x00\x00Y\x00\x00\x04F\x8e \x00\x00\x00\x00\x00\x01\x00\x00\x00_\x00\x00\x032\x10\x10\x00\x00\x00\x00\x00_\x00\x00\x032\x10\x10\x00\x01\x00\x00\x00e\x00\x00\x032 \x10\x00\x00\x00\x00\x00g\x00\x00\x04\xf2 \x10\x00\x01\x00\x00\x00\x01\x00\x00\x006\x00\x00\x052 \x10\x00\x00\x00\x00\x00F\x10\x10\x00\x01\x00\x00\x002\x00\x00\v2 \x10\x00\x01\x00\x00\x00F\x10\x10\x00\x00\x00\x00\x00F\x80 \x00\x00\x00\x00\x00\x00\x00\x00\x00\xe6\x8a \x00\x00\x00\x00\x00\x00\x00\x00\x006\x00\x00\b\xc2 \x10\x00\x01\x00\x00\x00\x02@\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80?>\x00\x00\x01STATt\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00RDEF\xc0\x00\x00\x00\x01\x00\x00\x00D\x00\x00\x00\x01\x00\x00\x00\x1c\x00\x00\x00\x00\x04\xfe\xff\x00\x01\x00\x00\x98\x00\x00\x00<\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00Block\x00\xab\xab<\x00\x00\x00\x01\x00\x00\x00\\\x00\x00\x00\x10\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00t\x00\x00\x00\x00\x00\x00\x00\x10\x00\x00\x00\x02\x00\x00\x00\x88\x00\x00\x00\x00\x00\x00\x00_block_transform\x00\xab\xab\xab\x01\x00\x03\x00\x01\x00\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00Microsoft (R) HLSL Shader Compiler 10.1\x00ISGND\x00\x00\x00\x02\x00\x00\x00\b\x00\x00\x008\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x03\x03\x00\x008\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x01\x00\x00\x00\x03\x03\x00\x00TEXCOORD\x00\xab\xab\xabOSGNP\x00\x00\x00\x02\x00\x00\x00\b\x00\x00\x008\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x03\f\x00\x00A\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x03\x00\x00\x00\x01\x00\x00\x00\x0f\x00\x00\x00TEXCOORD\x00SV_Position\x00\xab\xab\xab", } shader_path_coarse_comp = driver.ShaderSources{ Name: "path_coarse.comp", diff --git a/gpu/shaders/material.vert b/gpu/shaders/material.vert index c143acab..11390eb7 100644 --- a/gpu/shaders/material.vert +++ b/gpu/shaders/material.vert @@ -4,6 +4,11 @@ precision highp float; +layout(binding = 0) uniform Block { + vec2 scale; + vec2 pos; +} _block; + layout(location = 0) in vec2 pos; layout(location = 1) in vec2 uv; @@ -11,5 +16,5 @@ layout(location = 0) out vec2 vUV; void main() { vUV = uv; - gl_Position = vec4(pos, 0, 1); + gl_Position = vec4(pos*_block.scale + _block.pos, 0, 1); }