From f75a70dc403b822f687f764c3f11896635e818d6 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 3 Apr 2022 03:51:52 +0900 Subject: [PATCH] internal/graphicscommand: treat []float32 instead of interface{} for uniform variables --- image.go | 19 +++++++++++++++++-- internal/atlas/image.go | 4 ++-- internal/buffered/image.go | 2 +- internal/graphicscommand/command.go | 4 ++-- internal/graphicscommand/image.go | 2 +- internal/graphicscommand/shader.go | 11 ++--------- internal/mipmap/mipmap.go | 2 +- internal/restorable/image.go | 6 +++--- internal/ui/image.go | 2 +- 9 files changed, 30 insertions(+), 22 deletions(-) diff --git a/image.go b/image.go index d875d8db3..5feca1d06 100644 --- a/image.go +++ b/image.go @@ -497,7 +497,7 @@ func (i *Image) DrawTrianglesShader(vertices []Vertex, indices []uint16, shader offsets[i][1] = -sy + float32(b.Min.Y) } - i.image.DrawTriangles(imgs, vs, is, affine.ColorMIdentity{}, mode, graphicsdriver.FilterNearest, graphicsdriver.AddressUnsafe, dstRegion, sr, offsets, shader.shader, options.Uniforms, options.FillRule == EvenOdd, false) + i.image.DrawTriangles(imgs, vs, is, affine.ColorMIdentity{}, mode, graphicsdriver.FilterNearest, graphicsdriver.AddressUnsafe, dstRegion, sr, offsets, shader.shader, convertUniforms(options.Uniforms), options.FillRule == EvenOdd, false) } // DrawRectShaderOptions represents options for DrawRectShader. @@ -608,7 +608,7 @@ func (i *Image) DrawRectShader(width, height int, shader *Shader, options *DrawR offsets[i][1] = -sy + float32(b.Min.Y) } - i.image.DrawTriangles(imgs, vs, is, affine.ColorMIdentity{}, mode, graphicsdriver.FilterNearest, graphicsdriver.AddressUnsafe, dstRegion, sr, offsets, shader.shader, options.Uniforms, false, canSkipMipmap(options.GeoM, graphicsdriver.FilterNearest)) + i.image.DrawTriangles(imgs, vs, is, affine.ColorMIdentity{}, mode, graphicsdriver.FilterNearest, graphicsdriver.AddressUnsafe, dstRegion, sr, offsets, shader.shader, convertUniforms(options.Uniforms), false, canSkipMipmap(options.GeoM, graphicsdriver.FilterNearest)) } // SubImage returns an image representing the portion of the image p visible through r. @@ -883,3 +883,18 @@ func colorMToScale(colorm affine.ColorM) (newColorM affine.ColorM, r, g, b, a fl return affine.ColorMIdentity{}, r, g, b, a } + +func convertUniforms(uniforms map[string]interface{}) map[string][]float32 { + us := map[string][]float32{} + for name, v := range uniforms { + switch v := v.(type) { + case float32: + us[name] = []float32{v} + case []float32: + us[name] = v + default: + panic(fmt.Sprintf("ebiten: unexpected uniform value type: %s, %T", name, v)) + } + } + return us +} diff --git a/internal/atlas/image.go b/internal/atlas/image.go index 736dc2671..256f3b51d 100644 --- a/internal/atlas/image.go +++ b/internal/atlas/image.go @@ -404,13 +404,13 @@ func (i *Image) processSrc(src *Image) { // 5: Color G // 6: Color B // 7: Color Y -func (i *Image) DrawTriangles(srcs [graphics.ShaderImageNum]*Image, vertices []float32, indices []uint16, colorm affine.ColorM, mode graphicsdriver.CompositeMode, filter graphicsdriver.Filter, address graphicsdriver.Address, dstRegion, srcRegion graphicsdriver.Region, subimageOffsets [graphics.ShaderImageNum - 1][2]float32, shader *Shader, uniforms map[string]interface{}, evenOdd bool) { +func (i *Image) DrawTriangles(srcs [graphics.ShaderImageNum]*Image, vertices []float32, indices []uint16, colorm affine.ColorM, mode graphicsdriver.CompositeMode, filter graphicsdriver.Filter, address graphicsdriver.Address, dstRegion, srcRegion graphicsdriver.Region, subimageOffsets [graphics.ShaderImageNum - 1][2]float32, shader *Shader, uniforms map[string][]float32, evenOdd bool) { backendsM.Lock() defer backendsM.Unlock() i.drawTriangles(srcs, vertices, indices, colorm, mode, filter, address, dstRegion, srcRegion, subimageOffsets, shader, uniforms, evenOdd, false) } -func (i *Image) drawTriangles(srcs [graphics.ShaderImageNum]*Image, vertices []float32, indices []uint16, colorm affine.ColorM, mode graphicsdriver.CompositeMode, filter graphicsdriver.Filter, address graphicsdriver.Address, dstRegion, srcRegion graphicsdriver.Region, subimageOffsets [graphics.ShaderImageNum - 1][2]float32, shader *Shader, uniforms map[string]interface{}, evenOdd bool, keepOnAtlas bool) { +func (i *Image) drawTriangles(srcs [graphics.ShaderImageNum]*Image, vertices []float32, indices []uint16, colorm affine.ColorM, mode graphicsdriver.CompositeMode, filter graphicsdriver.Filter, address graphicsdriver.Address, dstRegion, srcRegion graphicsdriver.Region, subimageOffsets [graphics.ShaderImageNum - 1][2]float32, shader *Shader, uniforms map[string][]float32, evenOdd bool, keepOnAtlas bool) { if i.disposed { panic("atlas: the drawing target image must not be disposed (DrawTriangles)") } diff --git a/internal/buffered/image.go b/internal/buffered/image.go index df9df6656..41ea72a73 100644 --- a/internal/buffered/image.go +++ b/internal/buffered/image.go @@ -228,7 +228,7 @@ func (img *Image) replacePendingPixels(pix []byte, x, y, width, height int) { // DrawTriangles draws the src image with the given vertices. // // Copying vertices and indices is the caller's responsibility. -func (i *Image) DrawTriangles(srcs [graphics.ShaderImageNum]*Image, vertices []float32, indices []uint16, colorm affine.ColorM, mode graphicsdriver.CompositeMode, filter graphicsdriver.Filter, address graphicsdriver.Address, dstRegion, srcRegion graphicsdriver.Region, subimageOffsets [graphics.ShaderImageNum - 1][2]float32, shader *Shader, uniforms map[string]interface{}, evenOdd bool) { +func (i *Image) DrawTriangles(srcs [graphics.ShaderImageNum]*Image, vertices []float32, indices []uint16, colorm affine.ColorM, mode graphicsdriver.CompositeMode, filter graphicsdriver.Filter, address graphicsdriver.Address, dstRegion, srcRegion graphicsdriver.Region, subimageOffsets [graphics.ShaderImageNum - 1][2]float32, shader *Shader, uniforms map[string][]float32, evenOdd bool) { for _, src := range srcs { if i == src { panic("buffered: Image.DrawTriangles: source images must be different from the receiver") diff --git a/internal/graphicscommand/command.go b/internal/graphicscommand/command.go index b84086074..ecfbf9329 100644 --- a/internal/graphicscommand/command.go +++ b/internal/graphicscommand/command.go @@ -117,7 +117,7 @@ func mustUseDifferentVertexBuffer(nextNumVertexFloats, nextNumIndices int) bool } // EnqueueDrawTrianglesCommand enqueues a drawing-image command. -func (q *commandQueue) EnqueueDrawTrianglesCommand(dst *Image, srcs [graphics.ShaderImageNum]*Image, offsets [graphics.ShaderImageNum - 1][2]float32, vertices []float32, indices []uint16, color affine.ColorM, mode graphicsdriver.CompositeMode, filter graphicsdriver.Filter, address graphicsdriver.Address, dstRegion, srcRegion graphicsdriver.Region, shader *Shader, uniforms map[string]interface{}, evenOdd bool) { +func (q *commandQueue) EnqueueDrawTrianglesCommand(dst *Image, srcs [graphics.ShaderImageNum]*Image, offsets [graphics.ShaderImageNum - 1][2]float32, vertices []float32, indices []uint16, color affine.ColorM, mode graphicsdriver.CompositeMode, filter graphicsdriver.Filter, address graphicsdriver.Address, dstRegion, srcRegion graphicsdriver.Region, shader *Shader, uniforms map[string][]float32, evenOdd bool) { if len(indices) > graphics.IndicesNum { panic(fmt.Sprintf("graphicscommand: len(indices) must be <= graphics.IndicesNum but not at EnqueueDrawTrianglesCommand: len(indices): %d, graphics.IndicesNum: %d", len(indices), graphics.IndicesNum)) } @@ -295,7 +295,7 @@ type drawTrianglesCommand struct { dstRegion graphicsdriver.Region srcRegion graphicsdriver.Region shader *Shader - uniforms map[string]interface{} + uniforms map[string][]float32 evenOdd bool } diff --git a/internal/graphicscommand/image.go b/internal/graphicscommand/image.go index ccd84eb00..9f8605ccf 100644 --- a/internal/graphicscommand/image.go +++ b/internal/graphicscommand/image.go @@ -141,7 +141,7 @@ func (i *Image) InternalSize() (int, int) { // // If the source image is not specified, i.e., src is nil and there is no image in the uniform variables, the // elements for the source image are not used. -func (i *Image) DrawTriangles(srcs [graphics.ShaderImageNum]*Image, offsets [graphics.ShaderImageNum - 1][2]float32, vertices []float32, indices []uint16, clr affine.ColorM, mode graphicsdriver.CompositeMode, filter graphicsdriver.Filter, address graphicsdriver.Address, dstRegion, srcRegion graphicsdriver.Region, shader *Shader, uniforms map[string]interface{}, evenOdd bool) { +func (i *Image) DrawTriangles(srcs [graphics.ShaderImageNum]*Image, offsets [graphics.ShaderImageNum - 1][2]float32, vertices []float32, indices []uint16, clr affine.ColorM, mode graphicsdriver.CompositeMode, filter graphicsdriver.Filter, address graphicsdriver.Address, dstRegion, srcRegion graphicsdriver.Region, shader *Shader, uniforms map[string][]float32, evenOdd bool) { if shader == nil { // Fast path for rendering without a shader (#1355). img := srcs[0] diff --git a/internal/graphicscommand/shader.go b/internal/graphicscommand/shader.go index 4f49da13b..05ff7c61c 100644 --- a/internal/graphicscommand/shader.go +++ b/internal/graphicscommand/shader.go @@ -185,7 +185,7 @@ func (s *Shader) Dispose() { theCommandQueue.Enqueue(c) } -func (s *Shader) convertUniforms(uniforms map[string]interface{}) [][]float32 { +func (s *Shader) convertUniforms(uniforms map[string][]float32) [][]float32 { if s.shader == nil { panic("graphicscommand: shader is not compiled yet") } @@ -208,14 +208,7 @@ func (s *Shader) convertUniforms(uniforms map[string]interface{}) [][]float32 { us := make([][]float32, len(s.uniformNameToIndex)) for name, idx := range s.uniformNameToIndex { if v, ok := uniforms[name]; ok { - switch v := v.(type) { - case float32: - us[idx] = []float32{v} - case []float32: - us[idx] = v - default: - panic(fmt.Sprintf("graphicscommand: unexpected uniform value type: %s, %T", name, v)) - } + us[idx] = v continue } t := s.uniformNameToType[name] diff --git a/internal/mipmap/mipmap.go b/internal/mipmap/mipmap.go index de7ce7c3f..005873cd9 100644 --- a/internal/mipmap/mipmap.go +++ b/internal/mipmap/mipmap.go @@ -79,7 +79,7 @@ func (m *Mipmap) At(graphicsDriver graphicsdriver.Graphics, x, y int) (r, g, b, return m.orig.At(graphicsDriver, x, y) } -func (m *Mipmap) DrawTriangles(srcs [graphics.ShaderImageNum]*Mipmap, vertices []float32, indices []uint16, colorm affine.ColorM, mode graphicsdriver.CompositeMode, filter graphicsdriver.Filter, address graphicsdriver.Address, dstRegion, srcRegion graphicsdriver.Region, subimageOffsets [graphics.ShaderImageNum - 1][2]float32, shader *Shader, uniforms map[string]interface{}, evenOdd bool, canSkipMipmap bool) { +func (m *Mipmap) DrawTriangles(srcs [graphics.ShaderImageNum]*Mipmap, vertices []float32, indices []uint16, colorm affine.ColorM, mode graphicsdriver.CompositeMode, filter graphicsdriver.Filter, address graphicsdriver.Address, dstRegion, srcRegion graphicsdriver.Region, subimageOffsets [graphics.ShaderImageNum - 1][2]float32, shader *Shader, uniforms map[string][]float32, evenOdd bool, canSkipMipmap bool) { if len(indices) == 0 { return } diff --git a/internal/restorable/image.go b/internal/restorable/image.go index 830834c6a..d3d5bd522 100644 --- a/internal/restorable/image.go +++ b/internal/restorable/image.go @@ -76,7 +76,7 @@ type drawTrianglesHistoryItem struct { dstRegion graphicsdriver.Region srcRegion graphicsdriver.Region shader *Shader - uniforms map[string]interface{} + uniforms map[string][]float32 evenOdd bool } @@ -365,7 +365,7 @@ func (i *Image) ReplacePixels(pixels []byte, mask []byte, x, y, width, height in // 5: Color G // 6: Color B // 7: Color Y -func (i *Image) DrawTriangles(srcs [graphics.ShaderImageNum]*Image, offsets [graphics.ShaderImageNum - 1][2]float32, vertices []float32, indices []uint16, colorm affine.ColorM, mode graphicsdriver.CompositeMode, filter graphicsdriver.Filter, address graphicsdriver.Address, dstRegion, srcRegion graphicsdriver.Region, shader *Shader, uniforms map[string]interface{}, evenOdd bool) { +func (i *Image) DrawTriangles(srcs [graphics.ShaderImageNum]*Image, offsets [graphics.ShaderImageNum - 1][2]float32, vertices []float32, indices []uint16, colorm affine.ColorM, mode graphicsdriver.CompositeMode, filter graphicsdriver.Filter, address graphicsdriver.Address, dstRegion, srcRegion graphicsdriver.Region, shader *Shader, uniforms map[string][]float32, evenOdd bool) { if i.priority { panic("restorable: DrawTriangles cannot be called on a priority image") } @@ -410,7 +410,7 @@ func (i *Image) DrawTriangles(srcs [graphics.ShaderImageNum]*Image, offsets [gra } // appendDrawTrianglesHistory appends a draw-image history item to the image. -func (i *Image) appendDrawTrianglesHistory(srcs [graphics.ShaderImageNum]*Image, offsets [graphics.ShaderImageNum - 1][2]float32, vertices []float32, indices []uint16, colorm affine.ColorM, mode graphicsdriver.CompositeMode, filter graphicsdriver.Filter, address graphicsdriver.Address, dstRegion, srcRegion graphicsdriver.Region, shader *Shader, uniforms map[string]interface{}, evenOdd bool) { +func (i *Image) appendDrawTrianglesHistory(srcs [graphics.ShaderImageNum]*Image, offsets [graphics.ShaderImageNum - 1][2]float32, vertices []float32, indices []uint16, colorm affine.ColorM, mode graphicsdriver.CompositeMode, filter graphicsdriver.Filter, address graphicsdriver.Address, dstRegion, srcRegion graphicsdriver.Region, shader *Shader, uniforms map[string][]float32, evenOdd bool) { if i.stale || i.volatile || i.screen { return } diff --git a/internal/ui/image.go b/internal/ui/image.go index 12b3ec1d5..3e4e2941c 100644 --- a/internal/ui/image.go +++ b/internal/ui/image.go @@ -59,7 +59,7 @@ func (i *Image) MarkDisposed() { i.mipmap = nil } -func (i *Image) DrawTriangles(srcs [graphics.ShaderImageNum]*Image, vertices []float32, indices []uint16, colorm affine.ColorM, mode graphicsdriver.CompositeMode, filter graphicsdriver.Filter, address graphicsdriver.Address, dstRegion, srcRegion graphicsdriver.Region, subimageOffsets [graphics.ShaderImageNum - 1][2]float32, shader *Shader, uniforms map[string]interface{}, evenOdd bool, canSkipMipmap bool) { +func (i *Image) DrawTriangles(srcs [graphics.ShaderImageNum]*Image, vertices []float32, indices []uint16, colorm affine.ColorM, mode graphicsdriver.CompositeMode, filter graphicsdriver.Filter, address graphicsdriver.Address, dstRegion, srcRegion graphicsdriver.Region, subimageOffsets [graphics.ShaderImageNum - 1][2]float32, shader *Shader, uniforms map[string][]float32, evenOdd bool, canSkipMipmap bool) { var srcMipmaps [graphics.ShaderImageNum]*mipmap.Mipmap for i, src := range srcs { if src == nil {