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()
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() {
}

View File

@ -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
}

View File

@ -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 {