From 015ce2b26213c991240429472270752d0512183e Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Fri, 9 Dec 2022 00:02:04 +0900 Subject: [PATCH] internal/ui: refactoring: reduce APIs --- image.go | 32 ++++++++++++-------------------- internal/ui/shader.go | 28 ++++++++++++++++++---------- shader.go | 8 ++------ 3 files changed, 32 insertions(+), 36 deletions(-) diff --git a/image.go b/image.go index 0bd4687b4..7e64fae90 100644 --- a/image.go +++ b/image.go @@ -255,18 +255,18 @@ func (i *Image) DrawImage(img *Image, options *DrawImageOptions) { useColorM := !colorm.IsIdentity() shader := builtinShader(filter, builtinshader.AddressUnsafe, useColorM) - uniforms := i.ensureTmpUniforms(shader) + i.tmpUniforms = i.tmpUniforms[:0] if useColorM { var body [16]float32 var translation [4]float32 colorm.Elements(body[:], translation[:]) - shader.convertUniforms(uniforms, map[string]any{ + i.tmpUniforms = shader.appendUniforms(i.tmpUniforms, map[string]any{ builtinshader.UniformColorMBody: body[:], builtinshader.UniformColorMTranslation: translation[:], }) } - i.image.DrawTriangles(srcs, vs, is, blend, i.adjustedRegion(), img.adjustedRegion(), [graphics.ShaderImageCount - 1][2]float32{}, shader.shader, uniforms, false, canSkipMipmap(options.GeoM, filter), false) + i.image.DrawTriangles(srcs, vs, is, blend, i.adjustedRegion(), img.adjustedRegion(), [graphics.ShaderImageCount - 1][2]float32{}, shader.shader, i.tmpUniforms, false, canSkipMipmap(options.GeoM, filter), false) } // Vertex represents a vertex passed to DrawTriangles. @@ -484,18 +484,18 @@ func (i *Image) DrawTriangles(vertices []Vertex, indices []uint16, img *Image, o useColorM := !colorm.IsIdentity() shader := builtinShader(filter, address, useColorM) - uniforms := i.ensureTmpUniforms(shader) + i.tmpUniforms = i.tmpUniforms[:0] if useColorM { var body [16]float32 var translation [4]float32 colorm.Elements(body[:], translation[:]) - shader.convertUniforms(uniforms, map[string]any{ + i.tmpUniforms = shader.appendUniforms(i.tmpUniforms, map[string]any{ builtinshader.UniformColorMBody: body[:], builtinshader.UniformColorMTranslation: translation[:], }) } - i.image.DrawTriangles(srcs, vs, is, blend, i.adjustedRegion(), img.adjustedRegion(), [graphics.ShaderImageCount - 1][2]float32{}, shader.shader, uniforms, options.FillRule == EvenOdd, filter != builtinshader.FilterLinear, options.AntiAlias) + i.image.DrawTriangles(srcs, vs, is, blend, i.adjustedRegion(), img.adjustedRegion(), [graphics.ShaderImageCount - 1][2]float32{}, shader.shader, i.tmpUniforms, options.FillRule == EvenOdd, filter != builtinshader.FilterLinear, options.AntiAlias) } // DrawTrianglesShaderOptions represents options for DrawTrianglesShader. @@ -646,10 +646,10 @@ func (i *Image) DrawTrianglesShader(vertices []Vertex, indices []uint16, shader offsets[i][1] = float32(y - sy) } - uniforms := i.ensureTmpUniforms(shader) - shader.convertUniforms(uniforms, options.Uniforms) + i.tmpUniforms = i.tmpUniforms[:0] + i.tmpUniforms = shader.appendUniforms(i.tmpUniforms, options.Uniforms) - i.image.DrawTriangles(imgs, vs, is, blend, i.adjustedRegion(), sr, offsets, shader.shader, uniforms, options.FillRule == EvenOdd, true, options.AntiAlias) + i.image.DrawTriangles(imgs, vs, is, blend, i.adjustedRegion(), sr, offsets, shader.shader, i.tmpUniforms, options.FillRule == EvenOdd, true, options.AntiAlias) } // DrawRectShaderOptions represents options for DrawRectShader. @@ -759,10 +759,10 @@ func (i *Image) DrawRectShader(width, height int, shader *Shader, options *DrawR offsets[i][1] = float32(y - sy) } - uniforms := i.ensureTmpUniforms(shader) - shader.convertUniforms(uniforms, options.Uniforms) + i.tmpUniforms = i.tmpUniforms[:0] + i.tmpUniforms = shader.appendUniforms(i.tmpUniforms, options.Uniforms) - i.image.DrawTriangles(imgs, vs, is, blend, i.adjustedRegion(), sr, offsets, shader.shader, uniforms, false, true, false) + i.image.DrawTriangles(imgs, vs, is, blend, i.adjustedRegion(), sr, offsets, shader.shader, i.tmpUniforms, false, true, false) } // SubImage returns an image representing the portion of the image p visible through r. @@ -1164,14 +1164,6 @@ func (i *Image) ensureTmpVertices(n int) []float32 { return i.tmpVertices[:n] } -func (i *Image) ensureTmpUniforms(shader *Shader) []uint32 { - n := shader.uniformUint32Count() - if cap(i.tmpUniforms) < n { - i.tmpUniforms = make([]uint32, n) - } - return i.tmpUniforms[:n] -} - // private implements FinalScreen. func (*Image) private() { } diff --git a/internal/ui/shader.go b/internal/ui/shader.go index 75cc95f5e..ded2e4af6 100644 --- a/internal/ui/shader.go +++ b/internal/ui/shader.go @@ -27,8 +27,9 @@ import ( type Shader struct { shader *mipmap.Shader - uniformNames []string - uniformTypes []shaderir.Type + uniformNames []string + uniformTypes []shaderir.Type + uniformUint32Count int } func NewShader(ir *shaderir.Program) *Shader { @@ -44,16 +45,21 @@ func (s *Shader) MarkDisposed() { s.shader = nil } -func (s *Shader) UniformUint32Count() int { - var n int - for _, typ := range s.uniformTypes { - n += typ.Uint32Count() +func (s *Shader) AppendUniforms(dst []uint32, uniforms map[string]any) []uint32 { + if s.uniformUint32Count == 0 { + for _, typ := range s.uniformTypes { + s.uniformUint32Count += typ.Uint32Count() + } } - return n -} -func (s *Shader) ConvertUniforms(dst []uint32, uniforms map[string]any) { - var idx int + origLen := len(dst) + if cap(dst)-len(dst) >= s.uniformUint32Count { + dst = dst[:len(dst)+s.uniformUint32Count] + } else { + dst = append(dst, make([]uint32, s.uniformUint32Count)...) + } + + idx := origLen for i, name := range s.uniformNames { typ := s.uniformTypes[i] @@ -93,4 +99,6 @@ func (s *Shader) ConvertUniforms(dst []uint32, uniforms map[string]any) { idx += typ.Uint32Count() } + + return dst } diff --git a/shader.go b/shader.go index a44db6e13..a2af7443d 100644 --- a/shader.go +++ b/shader.go @@ -52,12 +52,8 @@ func (s *Shader) Dispose() { s.shader = nil } -func (s *Shader) uniformUint32Count() int { - return s.shader.UniformUint32Count() -} - -func (s *Shader) convertUniforms(dst []uint32, uniforms map[string]any) { - s.shader.ConvertUniforms(dst, uniforms) +func (s *Shader) appendUniforms(dst []uint32, uniforms map[string]any) []uint32 { + return s.shader.AppendUniforms(dst, uniforms) } type builtinShaderKey struct {