From 17a5488c4936f62c62d15cb35a5b6dddbd51c927 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Fri, 6 Sep 2024 14:06:39 +0900 Subject: [PATCH] Revert "internal/restorable: integrate Image.DrawTriangles into internal/atlas" This reverts commit 3ee905bc4d95e3bb8fe2fdeb7131030684d324d0. Updates #3083 --- internal/atlas/image.go | 11 +++-------- internal/restorable/image.go | 31 +++++++++++++++++++++++++++++-- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/internal/atlas/image.go b/internal/atlas/image.go index e275b7e7d..51d901ae9 100644 --- a/internal/atlas/image.go +++ b/internal/atlas/image.go @@ -372,11 +372,6 @@ func (i *Image) DrawTriangles(srcs [graphics.ShaderSrcImageCount]*Image, vertice } func (i *Image) drawTriangles(srcs [graphics.ShaderSrcImageCount]*Image, vertices []float32, indices []uint32, blend graphicsdriver.Blend, dstRegion image.Rectangle, srcRegions [graphics.ShaderSrcImageCount]image.Rectangle, shader *Shader, uniforms []uint32, fillRule graphicsdriver.FillRule) { - if len(vertices) == 0 { - return - } - - // This slice is not escaped to the heap. This can be checked by `go build -gcflags=-m`. backends := make([]*backend, 0, len(srcs)) for _, src := range srcs { if src == nil { @@ -449,15 +444,15 @@ func (i *Image) drawTriangles(srcs [graphics.ShaderSrcImageCount]*Image, vertice srcRegions[i] = srcRegions[i].Add(r.Min) } - var imgs [graphics.ShaderSrcImageCount]*graphicscommand.Image + var imgs [graphics.ShaderSrcImageCount]*restorable.Image for i, src := range srcs { if src == nil { continue } - imgs[i] = src.backend.restorable.Image + imgs[i] = src.backend.restorable } - i.backend.restorable.Image.DrawTriangles(imgs, vertices, indices, blend, dstRegion, srcRegions, shader.ensureShader().Shader, uniforms, fillRule) + i.backend.restorable.DrawTriangles(imgs, vertices, indices, blend, dstRegion, srcRegions, shader.ensureShader(), uniforms, fillRule) for _, src := range srcs { if src == nil { diff --git a/internal/restorable/image.go b/internal/restorable/image.go index 4bcda8d52..9f6188c4d 100644 --- a/internal/restorable/image.go +++ b/internal/restorable/image.go @@ -66,13 +66,13 @@ func (i *Image) Extend(width, height int) *Image { // Use DrawTriangles instead of WritePixels because the image i might be stale and not have its pixels // information. - srcs := [graphics.ShaderSrcImageCount]*graphicscommand.Image{i.Image} + srcs := [graphics.ShaderSrcImageCount]*Image{i} sw, sh := i.Image.InternalSize() vs := make([]float32, 4*graphics.VertexFloatCount) graphics.QuadVerticesFromDstAndSrc(vs, 0, 0, float32(sw), float32(sh), 0, 0, float32(sw), float32(sh), 1, 1, 1, 1) is := graphics.QuadIndices() dr := image.Rect(0, 0, sw, sh) - newImg.Image.DrawTriangles(srcs, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderSrcImageCount]image.Rectangle{}, NearestFilterShader.Shader, nil, graphicsdriver.FillRuleFillAll) + newImg.DrawTriangles(srcs, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderSrcImageCount]image.Rectangle{}, NearestFilterShader, nil, graphicsdriver.FillRuleFillAll) i.Image.Dispose() i.Image = nil @@ -108,3 +108,30 @@ func (i *Image) WritePixels(pixels *graphics.ManagedBytes, region image.Rectangl i.Image.WritePixels(pixels, region) } + +// DrawTriangles draws triangles with the given image. +// +// The vertex floats are: +// +// 0: Destination X in pixels +// 1: Destination Y in pixels +// 2: Source X in texels +// 3: Source Y in texels +// 4: Color R [0.0-1.0] +// 5: Color G +// 6: Color B +// 7: Color Y +func (i *Image) DrawTriangles(srcs [graphics.ShaderSrcImageCount]*Image, vertices []float32, indices []uint32, blend graphicsdriver.Blend, dstRegion image.Rectangle, srcRegions [graphics.ShaderSrcImageCount]image.Rectangle, shader *Shader, uniforms []uint32, fillRule graphicsdriver.FillRule) { + if len(vertices) == 0 { + return + } + + var imgs [graphics.ShaderSrcImageCount]*graphicscommand.Image + for i, src := range srcs { + if src == nil { + continue + } + imgs[i] = src.Image + } + i.Image.DrawTriangles(imgs, vertices, indices, blend, dstRegion, srcRegions, shader.Shader, uniforms, fillRule) +}