internal/ui: refactoring: reduce APIs

This commit is contained in:
Hajime Hoshi 2022-12-09 00:02:04 +09:00
parent c159938910
commit 015ce2b262
3 changed files with 32 additions and 36 deletions

View File

@ -255,18 +255,18 @@ func (i *Image) DrawImage(img *Image, options *DrawImageOptions) {
useColorM := !colorm.IsIdentity() useColorM := !colorm.IsIdentity()
shader := builtinShader(filter, builtinshader.AddressUnsafe, useColorM) shader := builtinShader(filter, builtinshader.AddressUnsafe, useColorM)
uniforms := i.ensureTmpUniforms(shader) i.tmpUniforms = i.tmpUniforms[:0]
if useColorM { if useColorM {
var body [16]float32 var body [16]float32
var translation [4]float32 var translation [4]float32
colorm.Elements(body[:], translation[:]) colorm.Elements(body[:], translation[:])
shader.convertUniforms(uniforms, map[string]any{ i.tmpUniforms = shader.appendUniforms(i.tmpUniforms, map[string]any{
builtinshader.UniformColorMBody: body[:], builtinshader.UniformColorMBody: body[:],
builtinshader.UniformColorMTranslation: translation[:], 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. // 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() useColorM := !colorm.IsIdentity()
shader := builtinShader(filter, address, useColorM) shader := builtinShader(filter, address, useColorM)
uniforms := i.ensureTmpUniforms(shader) i.tmpUniforms = i.tmpUniforms[:0]
if useColorM { if useColorM {
var body [16]float32 var body [16]float32
var translation [4]float32 var translation [4]float32
colorm.Elements(body[:], translation[:]) colorm.Elements(body[:], translation[:])
shader.convertUniforms(uniforms, map[string]any{ i.tmpUniforms = shader.appendUniforms(i.tmpUniforms, map[string]any{
builtinshader.UniformColorMBody: body[:], builtinshader.UniformColorMBody: body[:],
builtinshader.UniformColorMTranslation: translation[:], 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. // DrawTrianglesShaderOptions represents options for DrawTrianglesShader.
@ -646,10 +646,10 @@ func (i *Image) DrawTrianglesShader(vertices []Vertex, indices []uint16, shader
offsets[i][1] = float32(y - sy) offsets[i][1] = float32(y - sy)
} }
uniforms := i.ensureTmpUniforms(shader) i.tmpUniforms = i.tmpUniforms[:0]
shader.convertUniforms(uniforms, options.Uniforms) 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. // 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) offsets[i][1] = float32(y - sy)
} }
uniforms := i.ensureTmpUniforms(shader) i.tmpUniforms = i.tmpUniforms[:0]
shader.convertUniforms(uniforms, options.Uniforms) 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. // 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] 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. // private implements FinalScreen.
func (*Image) private() { func (*Image) private() {
} }

View File

@ -29,6 +29,7 @@ type Shader struct {
uniformNames []string uniformNames []string
uniformTypes []shaderir.Type uniformTypes []shaderir.Type
uniformUint32Count int
} }
func NewShader(ir *shaderir.Program) *Shader { func NewShader(ir *shaderir.Program) *Shader {
@ -44,16 +45,21 @@ func (s *Shader) MarkDisposed() {
s.shader = nil s.shader = nil
} }
func (s *Shader) UniformUint32Count() int { func (s *Shader) AppendUniforms(dst []uint32, uniforms map[string]any) []uint32 {
var n int if s.uniformUint32Count == 0 {
for _, typ := range s.uniformTypes { for _, typ := range s.uniformTypes {
n += typ.Uint32Count() s.uniformUint32Count += typ.Uint32Count()
}
} }
return n
}
func (s *Shader) ConvertUniforms(dst []uint32, uniforms map[string]any) { origLen := len(dst)
var idx int 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 { for i, name := range s.uniformNames {
typ := s.uniformTypes[i] typ := s.uniformTypes[i]
@ -93,4 +99,6 @@ func (s *Shader) ConvertUniforms(dst []uint32, uniforms map[string]any) {
idx += typ.Uint32Count() idx += typ.Uint32Count()
} }
return dst
} }

View File

@ -52,12 +52,8 @@ func (s *Shader) Dispose() {
s.shader = nil s.shader = nil
} }
func (s *Shader) uniformUint32Count() int { func (s *Shader) appendUniforms(dst []uint32, uniforms map[string]any) []uint32 {
return s.shader.UniformUint32Count() return s.shader.AppendUniforms(dst, uniforms)
}
func (s *Shader) convertUniforms(dst []uint32, uniforms map[string]any) {
s.shader.ConvertUniforms(dst, uniforms)
} }
type builtinShaderKey struct { type builtinShaderKey struct {