From 32b5e3edd8edcc175ab4612c6be8537ddf6c847a Mon Sep 17 00:00:00 2001 From: Zyko <13394516+Zyko0@users.noreply.github.com> Date: Fri, 5 Apr 2024 20:39:14 +0200 Subject: [PATCH 01/36] Saving --- examples/mrt/main.go | 130 +++++++++++++++ image.go | 149 ++++++++++++++++-- internal/atlas/image.go | 144 ++++++++++++++++- internal/atlas/image_test.go | 44 +++--- internal/atlas/shader_test.go | 10 +- internal/buffered/image.go | 47 +++++- internal/buffered/image_test.go | 4 +- internal/graphics/shader.go | 6 +- internal/graphics/vertex.go | 9 +- internal/graphicscommand/command.go | 46 ++++-- internal/graphicscommand/commandqueue.go | 30 ++-- internal/graphicscommand/image.go | 42 ++++- internal/graphicscommand/image_test.go | 10 +- .../directx/graphics11_windows.go | 8 +- .../directx/graphics12_windows.go | 6 +- .../directx/pipeline12_windows.go | 6 +- .../directx/shader11_windows.go | 4 +- internal/graphicsdriver/graphics.go | 2 +- internal/graphicsdriver/opengl/gl/debug.go | 8 + .../opengl/gl/default_purego.go | 6 + .../graphicsdriver/opengl/gl/interface.go | 1 + internal/graphicsdriver/opengl/graphics.go | 59 +++++-- internal/graphicsdriver/opengl/program.go | 14 +- internal/mipmap/mipmap.go | 78 ++++++++- internal/shader/shader.go | 11 +- internal/shaderir/glsl/glsl.go | 24 +-- internal/shaderir/program.go | 19 +-- internal/ui/image.go | 42 +++-- 28 files changed, 805 insertions(+), 154 deletions(-) create mode 100644 examples/mrt/main.go diff --git a/examples/mrt/main.go b/examples/mrt/main.go new file mode 100644 index 000000000..69f955c82 --- /dev/null +++ b/examples/mrt/main.go @@ -0,0 +1,130 @@ +// Copyright 2020 The Ebiten Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package main + +import ( + _ "image/jpeg" + "log" + + "github.com/hajimehoshi/ebiten/v2" +) + +const ( + dstSize = 128 + screenWidth = dstSize * 2 + screenHeight = dstSize * 2 +) + +var ( + dsts = [4]*ebiten.Image{ + ebiten.NewImage(dstSize, dstSize), + ebiten.NewImage(dstSize, dstSize), + ebiten.NewImage(dstSize, dstSize), + ebiten.NewImage(dstSize, dstSize), + /*ebiten.NewImageWithOptions(image.Rect(0, 0, dstSize, dstSize), &ebiten.NewImageOptions{ + Unmanaged: true, + }), + ebiten.NewImageWithOptions(image.Rect(0, 0, dstSize, dstSize), &ebiten.NewImageOptions{ + Unmanaged: true, + }), + ebiten.NewImageWithOptions(image.Rect(0, 0, dstSize, dstSize), &ebiten.NewImageOptions{ + Unmanaged: true, + }), + ebiten.NewImageWithOptions(image.Rect(0, 0, dstSize, dstSize), &ebiten.NewImageOptions{ + Unmanaged: true, + }),*/ + } + + shaderSrc = []byte( + ` +//kage:units pixels + +package main + +func Fragment(dst vec4, src vec2, color vec4) (vec4, vec4, vec4, vec4) { + return vec4(1,0,0,1), vec4(0,1,0,1), vec4(0,0,1,1), vec4(1,0,1,1) +} +`) + s *ebiten.Shader +) + +func init() { + var err error + + s, err = ebiten.NewShader(shaderSrc) + if err != nil { + log.Fatal(err) + } +} + +type Game struct { + count int +} + +func (g *Game) Update() error { + g.count++ + return nil +} + +func (g *Game) Draw(screen *ebiten.Image) { + vertices := []ebiten.Vertex{ + { + DstX: 0, + DstY: 0, + }, + { + DstX: dstSize, + DstY: 0, + }, + { + DstX: 0, + DstY: dstSize, + }, + { + DstX: dstSize, + DstY: dstSize, + }, + } + indices := []uint16{0, 1, 2, 1, 2, 3} + ebiten.DrawTrianglesShaderMRT(dsts, vertices, indices, s, nil) + // Dst 0 + screen.DrawImage(dsts[0], nil) + // Dst 1 + opts := &ebiten.DrawImageOptions{} + opts.GeoM.Translate(dstSize, 0) + screen.DrawImage(dsts[1], opts) + // Dst 2 + opts.GeoM.Reset() + opts.GeoM.Translate(0, dstSize) + screen.DrawImage(dsts[2], opts) + // Dst 3 + opts.GeoM.Reset() + opts.GeoM.Translate(dstSize, dstSize) + screen.DrawImage(dsts[3], opts) +} + +func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) { + return screenWidth, screenHeight +} + +func main() { + ebiten.SetWindowSize(screenWidth, screenHeight) + ebiten.SetWindowTitle("MRT (Ebitengine Demo)") + if err := ebiten.RunGameWithOptions(&Game{}, &ebiten.RunGameOptions{ + GraphicsLibrary: ebiten.GraphicsLibraryOpenGL, + }); err != nil { + log.Fatal(err) + } +} diff --git a/image.go b/image.go index 374a63e6e..d8f3d021c 100644 --- a/image.go +++ b/image.go @@ -247,7 +247,7 @@ func (i *Image) DrawImage(img *Image, options *DrawImageOptions) { graphics.QuadVertices(vs, float32(sx0), float32(sy0), float32(sx1), float32(sy1), a, b, c, d, tx, ty, cr, cg, cb, ca) is := graphics.QuadIndices() - srcs := [graphics.ShaderImageCount]*ui.Image{img.image} + srcs := [graphics.ShaderSrcImageCount]*ui.Image{img.image} useColorM := !colorm.IsIdentity() shader := builtinShader(filter, builtinshader.AddressUnsafe, useColorM) @@ -262,7 +262,7 @@ func (i *Image) DrawImage(img *Image, options *DrawImageOptions) { }) } - i.image.DrawTriangles(srcs, vs, is, blend, i.adjustedBounds(), [graphics.ShaderImageCount]image.Rectangle{img.adjustedBounds()}, shader.shader, i.tmpUniforms, graphicsdriver.FillAll, canSkipMipmap(geoM, filter), false) + i.image.DrawTriangles(srcs, vs, is, blend, i.adjustedBounds(), [graphics.ShaderSrcImageCount]image.Rectangle{img.adjustedBounds()}, shader.shader, i.tmpUniforms, graphicsdriver.FillAll, canSkipMipmap(geoM, filter), false) } // Vertex represents a vertex passed to DrawTriangles. @@ -500,7 +500,7 @@ func (i *Image) DrawTriangles(vertices []Vertex, indices []uint16, img *Image, o is[i] = uint32(indices[i]) } - srcs := [graphics.ShaderImageCount]*ui.Image{img.image} + srcs := [graphics.ShaderSrcImageCount]*ui.Image{img.image} useColorM := !colorm.IsIdentity() shader := builtinShader(filter, address, useColorM) @@ -515,7 +515,7 @@ func (i *Image) DrawTriangles(vertices []Vertex, indices []uint16, img *Image, o }) } - i.image.DrawTriangles(srcs, vs, is, blend, i.adjustedBounds(), [graphics.ShaderImageCount]image.Rectangle{img.adjustedBounds()}, shader.shader, i.tmpUniforms, graphicsdriver.FillRule(options.FillRule), filter != builtinshader.FilterLinear, options.AntiAlias) + i.image.DrawTriangles(srcs, vs, is, blend, i.adjustedBounds(), [graphics.ShaderSrcImageCount]image.Rectangle{img.adjustedBounds()}, shader.shader, i.tmpUniforms, graphicsdriver.FillRule(options.FillRule), filter != builtinshader.FilterLinear, options.AntiAlias) } // DrawTrianglesShaderOptions represents options for DrawTrianglesShader. @@ -565,7 +565,7 @@ type DrawTrianglesShaderOptions struct { } // Check the number of images. -var _ [len(DrawTrianglesShaderOptions{}.Images) - graphics.ShaderImageCount]struct{} = [0]struct{}{} +var _ [len(DrawTrianglesShaderOptions{}.Images) - graphics.ShaderSrcImageCount]struct{} = [0]struct{}{} // DrawTrianglesShader draws triangles with the specified vertices and their indices with the specified shader. // @@ -650,7 +650,7 @@ func (i *Image) DrawTrianglesShader(vertices []Vertex, indices []uint16, shader is[i] = uint32(indices[i]) } - var imgs [graphics.ShaderImageCount]*ui.Image + var imgs [graphics.ShaderSrcImageCount]*ui.Image var imgSize image.Point for i, img := range options.Images { if img == nil { @@ -672,7 +672,7 @@ func (i *Image) DrawTrianglesShader(vertices []Vertex, indices []uint16, shader imgs[i] = img.image } - var srcRegions [graphics.ShaderImageCount]image.Rectangle + var srcRegions [graphics.ShaderSrcImageCount]image.Rectangle for i, img := range options.Images { if img == nil { continue @@ -686,6 +686,135 @@ func (i *Image) DrawTrianglesShader(vertices []Vertex, indices []uint16, shader i.image.DrawTriangles(imgs, vs, is, blend, i.adjustedBounds(), srcRegions, shader.shader, i.tmpUniforms, graphicsdriver.FillRule(options.FillRule), true, options.AntiAlias) } +// DrawTrianglesShader draws triangles with the specified vertices and their indices with the specified shader. +// +// Vertex contains color values, which can be interpreted for any purpose by the shader. +// +// For the details about the shader, see https://ebitengine.org/en/documents/shader.html. +// +// If the shader unit is texels, one of the specified image is non-nil and its size is different from (width, height), +// DrawTrianglesShader panics. +// If one of the specified image is non-nil and is disposed, DrawTrianglesShader panics. +// +// If len(vertices) is more than MaxVertexCount, the exceeding part is ignored. +// +// If len(indices) is not multiple of 3, DrawTrianglesShader panics. +// +// If a value in indices is out of range of vertices, or not less than MaxVertexCount, DrawTrianglesShader panics. +// +// When a specified image is non-nil and is disposed, DrawTrianglesShader panics. +// +// If a specified uniform variable's length or type doesn't match with an expected one, DrawTrianglesShader panics. +// +// Even if a result is an invalid color as a premultiplied-alpha color, i.e. an alpha value exceeds other color values, +// the value is kept and is not clamped. +// +// When the image i is disposed, DrawTrianglesShader does nothing. +func DrawTrianglesShaderMRT(dsts [graphics.ShaderDstImageCount]*Image, vertices []Vertex, indices []uint16, shader *Shader, options *DrawTrianglesShaderOptions) { + if dsts[0] == nil || dsts[0].isDisposed() { + panic("ebiten: the first destination image given to DrawTrianglesShaderMRT must not be nil or disposed") + } + var dstImgs [graphics.ShaderDstImageCount]*ui.Image + for i, dst := range dsts { + if dst == nil { + continue + } + dst.copyCheck() + if dst.isDisposed() { + panic("ebiten: the destination images given to DrawTrianglesShaderMRT must not be disposed") + } + dstImgs[i] = dst.image + } + + if shader.isDisposed() { + panic("ebiten: the given shader to DrawTrianglesShaderMRT must not be disposed") + } + + if len(vertices) > graphicscommand.MaxVertexCount { + // The last part cannot be specified by indices. Just omit them. + vertices = vertices[:graphicscommand.MaxVertexCount] + } + if len(indices)%3 != 0 { + panic("ebiten: len(indices) % 3 must be 0") + } + for i, idx := range indices { + if int(idx) >= len(vertices) { + panic(fmt.Sprintf("ebiten: indices[%d] must be less than len(vertices) (%d) but was %d", i, len(vertices), idx)) + } + } + + if options == nil { + options = &DrawTrianglesShaderOptions{} + } + + var blend graphicsdriver.Blend + if options.CompositeMode == CompositeModeCustom { + blend = options.Blend.internalBlend() + } else { + blend = options.CompositeMode.blend().internalBlend() + } + + dst := dsts[0] + vs := dst.ensureTmpVertices(len(vertices) * graphics.VertexFloatCount) + src := options.Images[0] + for i, v := range vertices { + dx, dy := dst.adjustPositionF32(v.DstX, v.DstY) + vs[i*graphics.VertexFloatCount] = dx + vs[i*graphics.VertexFloatCount+1] = dy + sx, sy := v.SrcX, v.SrcY + if src != nil { + sx, sy = src.adjustPositionF32(sx, sy) + } + vs[i*graphics.VertexFloatCount+2] = sx + vs[i*graphics.VertexFloatCount+3] = sy + vs[i*graphics.VertexFloatCount+4] = v.ColorR + vs[i*graphics.VertexFloatCount+5] = v.ColorG + vs[i*graphics.VertexFloatCount+6] = v.ColorB + vs[i*graphics.VertexFloatCount+7] = v.ColorA + } + + is := make([]uint32, len(indices)) + for i := range is { + is[i] = uint32(indices[i]) + } + + var srcImgs [graphics.ShaderSrcImageCount]*ui.Image + var imgSize image.Point + for i, img := range options.Images { + if img == nil { + continue + } + if img.isDisposed() { + panic("ebiten: the given image to DrawTrianglesShader must not be disposed") + } + if shader.unit == shaderir.Texels { + if i == 0 { + imgSize = img.Bounds().Size() + } else { + // TODO: Check imgw > 0 && imgh > 0 + if img.Bounds().Size() != imgSize { + panic("ebiten: all the source images must be the same size with the rectangle") + } + } + } + srcImgs[i] = img.image + } + + var srcRegions [graphics.ShaderSrcImageCount]image.Rectangle + for i, img := range options.Images { + if img == nil { + continue + } + srcRegions[i] = img.adjustedBounds() + } + + for _, dst := range dsts { + dst.tmpUniforms = dst.tmpUniforms[:0] + dst.tmpUniforms = shader.appendUniforms(dst.tmpUniforms, options.Uniforms) + } + ui.DrawTrianglesMRT(dstImgs, srcImgs, vs, is, blend, dst.adjustedBounds(), srcRegions, shader.shader, dst.tmpUniforms, graphicsdriver.FillRule(options.FillRule), true, options.AntiAlias) +} + // DrawRectShaderOptions represents options for DrawRectShader. type DrawRectShaderOptions struct { // GeoM is a geometry matrix to draw. @@ -724,7 +853,7 @@ type DrawRectShaderOptions struct { } // Check the number of images. -var _ [len(DrawRectShaderOptions{}.Images)]struct{} = [graphics.ShaderImageCount]struct{}{} +var _ [len(DrawRectShaderOptions{}.Images)]struct{} = [graphics.ShaderSrcImageCount]struct{}{} // DrawRectShader draws a rectangle with the specified width and height with the specified shader. // @@ -771,7 +900,7 @@ func (i *Image) DrawRectShader(width, height int, shader *Shader, options *DrawR blend = options.CompositeMode.blend().internalBlend() } - var imgs [graphics.ShaderImageCount]*ui.Image + var imgs [graphics.ShaderSrcImageCount]*ui.Image for i, img := range options.Images { if img == nil { continue @@ -785,7 +914,7 @@ func (i *Image) DrawRectShader(width, height int, shader *Shader, options *DrawR imgs[i] = img.image } - var srcRegions [graphics.ShaderImageCount]image.Rectangle + var srcRegions [graphics.ShaderSrcImageCount]image.Rectangle for i, img := range options.Images { if img == nil { if shader.unit == shaderir.Pixels && i == 0 { diff --git a/internal/atlas/image.go b/internal/atlas/image.go index 9a07d015e..66e140fb6 100644 --- a/internal/atlas/image.go +++ b/internal/atlas/image.go @@ -153,12 +153,12 @@ func (b *backend) extendIfNeeded(width, height int) { // Use DrawTriangles instead of WritePixels because the image i might be stale and not have its pixels // information. - srcs := [graphics.ShaderImageCount]*graphicscommand.Image{b.image} + srcs := [graphics.ShaderSrcImageCount]*graphicscommand.Image{b.image} sw, sh := b.image.InternalSize() vs := quadVertices(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.DrawTriangles(srcs, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, NearestFilterShader.ensureShader(), nil, graphicsdriver.FillAll) + newImg.DrawTriangles(srcs, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderSrcImageCount]image.Rectangle{}, NearestFilterShader.ensureShader(), nil, graphicsdriver.FillAll) b.image.Dispose() b.image = newImg @@ -182,7 +182,7 @@ func newClearedImage(width, height int, screen bool) *graphicscommand.Image { func clearImage(i *graphicscommand.Image, region image.Rectangle) { vs := quadVertices(float32(region.Min.X), float32(region.Min.Y), float32(region.Max.X), float32(region.Max.Y), 0, 0, 0, 0, 0, 0, 0, 0) is := graphics.QuadIndices() - i.DrawTriangles([graphics.ShaderImageCount]*graphicscommand.Image{}, vs, is, graphicsdriver.BlendClear, region, [graphics.ShaderImageCount]image.Rectangle{}, clearShader.ensureShader(), nil, graphicsdriver.FillAll) + i.DrawTriangles([graphics.ShaderSrcImageCount]*graphicscommand.Image{}, vs, is, graphicsdriver.BlendClear, region, [graphics.ShaderSrcImageCount]image.Rectangle{}, clearShader.ensureShader(), nil, graphicsdriver.FillAll) } func (b *backend) clearPixels(region image.Rectangle) { @@ -355,7 +355,7 @@ func (i *Image) ensureIsolatedFromSource(backends []*backend) { is := graphics.QuadIndices() dr := image.Rect(0, 0, i.width, i.height) - newI.drawTriangles([graphics.ShaderImageCount]*Image{i}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, NearestFilterShader, nil, graphicsdriver.FillAll) + newI.drawTriangles([graphics.ShaderSrcImageCount]*Image{i}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderSrcImageCount]image.Rectangle{}, NearestFilterShader, nil, graphicsdriver.FillAll) newI.moveTo(i) } @@ -385,7 +385,7 @@ func (i *Image) putOnSourceBackend(graphicsDriver graphicsdriver.Graphics) { graphics.QuadVertices(vs, 0, 0, w, h, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1) is := graphics.QuadIndices() dr := image.Rect(0, 0, i.width, i.height) - newI.drawTriangles([graphics.ShaderImageCount]*Image{i}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, NearestFilterShader, nil, graphicsdriver.FillAll) + newI.drawTriangles([graphics.ShaderSrcImageCount]*Image{i}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderSrcImageCount]image.Rectangle{}, NearestFilterShader, nil, graphicsdriver.FillAll) newI.moveTo(i) i.usedAsSourceCount = 0 @@ -417,7 +417,7 @@ func (i *Image) regionWithPadding() image.Rectangle { // 5: Color G // 6: Color B // 7: Color Y -func (i *Image) DrawTriangles(srcs [graphics.ShaderImageCount]*Image, vertices []float32, indices []uint32, blend graphicsdriver.Blend, dstRegion image.Rectangle, srcRegions [graphics.ShaderImageCount]image.Rectangle, shader *Shader, uniforms []uint32, fillRule graphicsdriver.FillRule) { +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) { backendsM.Lock() defer backendsM.Unlock() @@ -438,7 +438,28 @@ func (i *Image) DrawTriangles(srcs [graphics.ShaderImageCount]*Image, vertices [ i.drawTriangles(srcs, vertices, indices, blend, dstRegion, srcRegions, shader, uniforms, fillRule) } -func (i *Image) drawTriangles(srcs [graphics.ShaderImageCount]*Image, vertices []float32, indices []uint32, blend graphicsdriver.Blend, dstRegion image.Rectangle, srcRegions [graphics.ShaderImageCount]image.Rectangle, shader *Shader, uniforms []uint32, fillRule graphicsdriver.FillRule) { +func DrawTrianglesMRT(dsts [graphics.ShaderDstImageCount]*Image, 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) { + backendsM.Lock() + defer backendsM.Unlock() + + if !inFrame { + vs := make([]float32, len(vertices)) + copy(vs, vertices) + is := make([]uint32, len(indices)) + copy(is, indices) + us := make([]uint32, len(uniforms)) + copy(us, uniforms) + + appendDeferred(func() { + drawTrianglesMRT(dsts, srcs, vs, is, blend, dstRegion, srcRegions, shader, us, fillRule) + }) + return + } + + drawTrianglesMRT(dsts, srcs, vertices, indices, blend, dstRegion, srcRegions, shader, uniforms, fillRule) +} + +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 } @@ -515,7 +536,7 @@ func (i *Image) drawTriangles(srcs [graphics.ShaderImageCount]*Image, vertices [ srcRegions[i] = srcRegions[i].Add(r.Min) } - var imgs [graphics.ShaderImageCount]*graphicscommand.Image + var imgs [graphics.ShaderSrcImageCount]*graphicscommand.Image for i, src := range srcs { if src == nil { continue @@ -536,6 +557,113 @@ func (i *Image) drawTriangles(srcs [graphics.ShaderImageCount]*Image, vertices [ } } +func drawTrianglesMRT(dsts [graphics.ShaderDstImageCount]*Image, 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 + } + + backends := make([]*backend, 0, len(srcs)) + for _, src := range srcs { + if src == nil { + continue + } + if src.backend == nil { + // It is possible to spcify i.backend as a forbidden backend, but this might prevent a good allocation for a source image. + // If the backend becomes the same as i's, i's backend will be changed at ensureIsolatedFromSource. + src.allocate(nil, true) + } + backends = append(backends, src.backend) + src.backend.sourceInThisFrame = true + } + + var dstImgs [graphics.ShaderDstImageCount]*graphicscommand.Image + for i, dst := range dsts { + if dst == nil { + continue + } + dst.ensureIsolatedFromSource(backends) + dstImgs[i] = dst.backend.image + } + + for _, src := range srcs { + // Compare i and source images after ensuring i is not on an atlas, or + // i and a source image might share the same atlas even though i != src. + for _, dst := range dsts { + if src != nil && dst != nil && dst.backend.image == src.backend.image { + panic("atlas: Image.DrawTriangles: source must be different from the receiver") + } + } + } + + r := dsts[0].regionWithPadding() + // TODO: Check if dstRegion does not to violate the region. + dstRegion = dstRegion.Add(r.Min) + + dx, dy := float32(r.Min.X), float32(r.Min.Y) + + var oxf, oyf float32 + if srcs[0] != nil { + r := srcs[0].regionWithPadding() + oxf, oyf = float32(r.Min.X), float32(r.Min.Y) + n := len(vertices) + for i := 0; i < n; i += graphics.VertexFloatCount { + vertices[i] += dx + vertices[i+1] += dy + vertices[i+2] += oxf + vertices[i+3] += oyf + } + if shader.ir.Unit == shaderir.Texels { + sw, sh := srcs[0].backend.image.InternalSize() + swf, shf := float32(sw), float32(sh) + for i := 0; i < n; i += graphics.VertexFloatCount { + vertices[i+2] /= swf + vertices[i+3] /= shf + } + } + } else { + n := len(vertices) + for i := 0; i < n; i += graphics.VertexFloatCount { + vertices[i] += dx + vertices[i+1] += dy + } + } + + for i, src := range srcs { + if src == nil { + continue + } + + // A source region can be deliberately empty when this is not needed in order to avoid unexpected + // performance issue (#1293). + if srcRegions[i].Empty() { + continue + } + + r := src.regionWithPadding() + srcRegions[i] = srcRegions[i].Add(r.Min) + } + + var srcImgs [graphics.ShaderSrcImageCount]*graphicscommand.Image + for i, src := range srcs { + if src == nil { + continue + } + srcImgs[i] = src.backend.image + } + + graphicscommand.DrawTrianglesMRT(dstImgs, srcImgs, vertices, indices, blend, dstRegion, srcRegions, shader.ensureShader(), uniforms, fillRule) + + for _, src := range srcs { + if src == nil { + continue + } + if !src.isOnSourceBackend() && src.canBePutOnAtlas() { + // src might already registered, but assigning it again is not harmful. + imagesToPutOnSourceBackend.add(src) + } + } +} + // WritePixels replaces the pixels on the image. func (i *Image) WritePixels(pix []byte, region image.Rectangle) { backendsM.Lock() diff --git a/internal/atlas/image_test.go b/internal/atlas/image_test.go index 45689b6be..338874ae8 100644 --- a/internal/atlas/image_test.go +++ b/internal/atlas/image_test.go @@ -105,7 +105,7 @@ func TestEnsureIsolatedFromSourceBackend(t *testing.T) { vs := quadVertices(size/2, size/2, size/4, size/4, 1) is := graphics.QuadIndices() dr := image.Rect(0, 0, size, size) - img4.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{img3}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll) + img4.DrawTriangles([graphics.ShaderSrcImageCount]*atlas.Image{img3}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderSrcImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll) if got, want := img4.IsOnSourceBackendForTesting(), false; got != want { t.Errorf("got: %v, want: %v", got, want) } @@ -113,7 +113,7 @@ func TestEnsureIsolatedFromSourceBackend(t *testing.T) { // img5 is not allocated now, but is allocated at DrawTriangles. vs = quadVertices(0, 0, size/2, size/2, 1) dr = image.Rect(0, 0, size/2, size/2) - img3.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{img5}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll) + img3.DrawTriangles([graphics.ShaderSrcImageCount]*atlas.Image{img5}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderSrcImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll) if got, want := img3.IsOnSourceBackendForTesting(), false; got != want { t.Errorf("got: %v, want: %v", got, want) } @@ -147,7 +147,7 @@ func TestEnsureIsolatedFromSourceBackend(t *testing.T) { // Check further drawing doesn't cause panic. // This bug was fixed by 03dcd948. vs = quadVertices(0, 0, size/2, size/2, 1) - img4.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{img3}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll) + img4.DrawTriangles([graphics.ShaderSrcImageCount]*atlas.Image{img3}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderSrcImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll) } func TestReputOnSourceBackend(t *testing.T) { @@ -191,7 +191,7 @@ func TestReputOnSourceBackend(t *testing.T) { // Render onto img1. The count should not matter. for i := 0; i < 5; i++ { vs := quadVertices(size, size, 0, 0, 1) - img1.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{img2}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll) + img1.DrawTriangles([graphics.ShaderSrcImageCount]*atlas.Image{img2}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderSrcImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll) if got, want := img1.IsOnSourceBackendForTesting(), false; got != want { t.Errorf("got: %v, want: %v", got, want) } @@ -203,7 +203,7 @@ func TestReputOnSourceBackend(t *testing.T) { for i := 0; i < atlas.BaseCountToPutOnSourceBackend*2; i++ { atlas.PutImagesOnSourceBackendForTesting(ui.Get().GraphicsDriverForTesting()) vs := quadVertices(size, size, 0, 0, 1) - img0.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{img1}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll) + img0.DrawTriangles([graphics.ShaderSrcImageCount]*atlas.Image{img1}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderSrcImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll) if got, want := img1.IsOnSourceBackendForTesting(), false; got != want { t.Errorf("got: %v, want: %v", got, want) } @@ -211,7 +211,7 @@ func TestReputOnSourceBackend(t *testing.T) { // Finally, img1 is on a source backend. atlas.PutImagesOnSourceBackendForTesting(ui.Get().GraphicsDriverForTesting()) vs := quadVertices(size, size, 0, 0, 1) - img0.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{img1}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll) + img0.DrawTriangles([graphics.ShaderSrcImageCount]*atlas.Image{img1}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderSrcImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll) if got, want := img1.IsOnSourceBackendForTesting(), true; got != want { t.Errorf("got: %v, want: %v", got, want) } @@ -240,7 +240,7 @@ func TestReputOnSourceBackend(t *testing.T) { } vs = quadVertices(size, size, 0, 0, 1) - img0.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{img1}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll) + img0.DrawTriangles([graphics.ShaderSrcImageCount]*atlas.Image{img1}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderSrcImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll) if got, want := img1.IsOnSourceBackendForTesting(), true; got != want { t.Errorf("got: %v, want: %v", got, want) } @@ -270,7 +270,7 @@ func TestReputOnSourceBackend(t *testing.T) { // Use img1 as a render target again. The count should not matter. for i := 0; i < 5; i++ { vs := quadVertices(size, size, 0, 0, 1) - img1.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{img2}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll) + img1.DrawTriangles([graphics.ShaderSrcImageCount]*atlas.Image{img2}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderSrcImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll) if got, want := img1.IsOnSourceBackendForTesting(), false; got != want { t.Errorf("got: %v, want: %v", got, want) } @@ -282,7 +282,7 @@ func TestReputOnSourceBackend(t *testing.T) { atlas.PutImagesOnSourceBackendForTesting(ui.Get().GraphicsDriverForTesting()) img1.WritePixels(make([]byte, 4*size*size), image.Rect(0, 0, size, size)) vs := quadVertices(size, size, 0, 0, 1) - img0.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{img1}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll) + img0.DrawTriangles([graphics.ShaderSrcImageCount]*atlas.Image{img1}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderSrcImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll) if got, want := img1.IsOnSourceBackendForTesting(), false; got != want { t.Errorf("got: %v, want: %v", got, want) } @@ -291,7 +291,7 @@ func TestReputOnSourceBackend(t *testing.T) { // img1 is not on an atlas due to WritePixels. vs = quadVertices(size, size, 0, 0, 1) - img0.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{img1}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll) + img0.DrawTriangles([graphics.ShaderSrcImageCount]*atlas.Image{img1}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderSrcImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll) if got, want := img1.IsOnSourceBackendForTesting(), false; got != want { t.Errorf("got: %v, want: %v", got, want) } @@ -300,7 +300,7 @@ func TestReputOnSourceBackend(t *testing.T) { for i := 0; i < atlas.BaseCountToPutOnSourceBackend*2; i++ { atlas.PutImagesOnSourceBackendForTesting(ui.Get().GraphicsDriverForTesting()) vs := quadVertices(size, size, 0, 0, 1) - img0.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{img3}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll) + img0.DrawTriangles([graphics.ShaderSrcImageCount]*atlas.Image{img3}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderSrcImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll) if got, want := img3.IsOnSourceBackendForTesting(), false; got != want { t.Errorf("got: %v, want: %v", got, want) } @@ -403,7 +403,7 @@ func TestWritePixelsAfterDrawTriangles(t *testing.T) { vs := quadVertices(w, h, 0, 0, 1) is := graphics.QuadIndices() dr := image.Rect(0, 0, w, h) - dst.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{src}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll) + dst.DrawTriangles([graphics.ShaderSrcImageCount]*atlas.Image{src}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderSrcImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll) dst.WritePixels(pix, image.Rect(0, 0, w, h)) pix = make([]byte, 4*w*h) @@ -450,7 +450,7 @@ func TestSmallImages(t *testing.T) { vs := quadVertices(w, h, 0, 0, 1) is := graphics.QuadIndices() dr := image.Rect(0, 0, w, h) - dst.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{src}, vs, is, graphicsdriver.BlendSourceOver, dr, [graphics.ShaderImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll) + dst.DrawTriangles([graphics.ShaderSrcImageCount]*atlas.Image{src}, vs, is, graphicsdriver.BlendSourceOver, dr, [graphics.ShaderSrcImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll) pix = make([]byte, 4*w*h) ok, err := dst.ReadPixels(ui.Get().GraphicsDriverForTesting(), pix, image.Rect(0, 0, w, h)) @@ -497,7 +497,7 @@ func TestLongImages(t *testing.T) { vs := quadVertices(w, h, 0, 0, scale) is := graphics.QuadIndices() dr := image.Rect(0, 0, dstW, dstH) - dst.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{src}, vs, is, graphicsdriver.BlendSourceOver, dr, [graphics.ShaderImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll) + dst.DrawTriangles([graphics.ShaderSrcImageCount]*atlas.Image{src}, vs, is, graphicsdriver.BlendSourceOver, dr, [graphics.ShaderSrcImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll) pix = make([]byte, 4*dstW*dstH) ok, err := dst.ReadPixels(ui.Get().GraphicsDriverForTesting(), pix, image.Rect(0, 0, dstW, dstH)) @@ -613,7 +613,7 @@ func TestDeallocatedAndReputOnSourceBackend(t *testing.T) { vs := quadVertices(size, size, 0, 0, 1) is := graphics.QuadIndices() dr := image.Rect(0, 0, size, size) - src.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{src2}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll) + src.DrawTriangles([graphics.ShaderSrcImageCount]*atlas.Image{src2}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderSrcImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll) if got, want := src.IsOnSourceBackendForTesting(), false; got != want { t.Errorf("got: %v, want: %v", got, want) } @@ -622,7 +622,7 @@ func TestDeallocatedAndReputOnSourceBackend(t *testing.T) { for i := 0; i < atlas.BaseCountToPutOnSourceBackend/2; i++ { atlas.PutImagesOnSourceBackendForTesting(ui.Get().GraphicsDriverForTesting()) vs := quadVertices(size, size, 0, 0, 1) - dst.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{src}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll) + dst.DrawTriangles([graphics.ShaderSrcImageCount]*atlas.Image{src}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderSrcImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll) if got, want := src.IsOnSourceBackendForTesting(), false; got != want { t.Errorf("got: %v, want: %v", got, want) } @@ -656,7 +656,7 @@ func TestImageIsNotReputOnSourceBackendWithoutUsingAsSource(t *testing.T) { // Call DrawTriangles multiple times. // The number of DrawTriangles doesn't matter as long as these are called in one frame. for i := 0; i < 2; i++ { - src2.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{src}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll) + src2.DrawTriangles([graphics.ShaderSrcImageCount]*atlas.Image{src}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderSrcImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll) } if got, want := src2.IsOnSourceBackendForTesting(), false; got != want { t.Errorf("got: %v, want: %v", got, want) @@ -675,7 +675,7 @@ func TestImageIsNotReputOnSourceBackendWithoutUsingAsSource(t *testing.T) { for i := 0; i < atlas.BaseCountToPutOnSourceBackend; i++ { atlas.PutImagesOnSourceBackendForTesting(ui.Get().GraphicsDriverForTesting()) vs := quadVertices(size, size, 0, 0, 1) - dst.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{src2}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll) + dst.DrawTriangles([graphics.ShaderSrcImageCount]*atlas.Image{src2}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderSrcImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll) if got, want := src2.IsOnSourceBackendForTesting(), false; got != want { t.Errorf("got: %v, want: %v", got, want) } @@ -801,14 +801,14 @@ func TestDestinationCountOverflow(t *testing.T) { // Use dst0 as a destination for a while. for i := 0; i < 31; i++ { - dst0.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{src}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll) + dst0.DrawTriangles([graphics.ShaderSrcImageCount]*atlas.Image{src}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderSrcImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll) atlas.PutImagesOnSourceBackendForTesting(ui.Get().GraphicsDriverForTesting()) } // Use dst0 as a source for a while. // As dst0 is used as a destination too many times (31 is a maximum), dst0's backend should never be a source backend. for i := 0; i < 100; i++ { - dst1.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{dst0}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll) + dst1.DrawTriangles([graphics.ShaderSrcImageCount]*atlas.Image{dst0}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderSrcImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll) atlas.PutImagesOnSourceBackendForTesting(ui.Get().GraphicsDriverForTesting()) if dst0.IsOnSourceBackendForTesting() { t.Errorf("dst0 cannot be on a source backend: %d", i) @@ -834,7 +834,7 @@ func TestIteratingImagesToPutOnSourceBackend(t *testing.T) { is := graphics.QuadIndices() dr := image.Rect(0, 0, w, h) for _, img := range srcs { - img.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{src}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll) + img.DrawTriangles([graphics.ShaderSrcImageCount]*atlas.Image{src}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderSrcImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll) } atlas.PutImagesOnSourceBackendForTesting(ui.Get().GraphicsDriverForTesting()) @@ -842,7 +842,7 @@ func TestIteratingImagesToPutOnSourceBackend(t *testing.T) { // Check iterating the registered image works correctly. for i := 0; i < 100; i++ { for _, src := range srcs { - dst.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{src}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll) + dst.DrawTriangles([graphics.ShaderSrcImageCount]*atlas.Image{src}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderSrcImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll) } atlas.PutImagesOnSourceBackendForTesting(ui.Get().GraphicsDriverForTesting()) } diff --git a/internal/atlas/shader_test.go b/internal/atlas/shader_test.go index b6bcb0bfc..5a7c97619 100644 --- a/internal/atlas/shader_test.go +++ b/internal/atlas/shader_test.go @@ -37,12 +37,12 @@ func TestShaderFillTwice(t *testing.T) { dr := image.Rect(0, 0, w, h) g := ui.Get().GraphicsDriverForTesting() s0 := atlas.NewShader(etesting.ShaderProgramFill(0xff, 0xff, 0xff, 0xff)) - dst.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, s0, nil, graphicsdriver.FillAll) + dst.DrawTriangles([graphics.ShaderSrcImageCount]*atlas.Image{}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderSrcImageCount]image.Rectangle{}, s0, nil, graphicsdriver.FillAll) // Vertices must be recreated (#1755) vs = quadVertices(w, h, 0, 0, 1) s1 := atlas.NewShader(etesting.ShaderProgramFill(0x80, 0x80, 0x80, 0xff)) - dst.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, s1, nil, graphicsdriver.FillAll) + dst.DrawTriangles([graphics.ShaderSrcImageCount]*atlas.Image{}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderSrcImageCount]image.Rectangle{}, s1, nil, graphicsdriver.FillAll) pix := make([]byte, 4*w*h) ok, err := dst.ReadPixels(g, pix, image.Rect(0, 0, w, h)) @@ -69,11 +69,11 @@ func TestImageDrawTwice(t *testing.T) { vs := quadVertices(w, h, 0, 0, 1) is := graphics.QuadIndices() dr := image.Rect(0, 0, w, h) - dst.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{src0}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll) + dst.DrawTriangles([graphics.ShaderSrcImageCount]*atlas.Image{src0}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderSrcImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll) // Vertices must be recreated (#1755) vs = quadVertices(w, h, 0, 0, 1) - dst.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{src1}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll) + dst.DrawTriangles([graphics.ShaderSrcImageCount]*atlas.Image{src1}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderSrcImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll) pix := make([]byte, 4*w*h) ok, err := dst.ReadPixels(ui.Get().GraphicsDriverForTesting(), pix, image.Rect(0, 0, w, h)) @@ -97,7 +97,7 @@ func TestGCShader(t *testing.T) { vs := quadVertices(w, h, 0, 0, 1) is := graphics.QuadIndices() dr := image.Rect(0, 0, w, h) - dst.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, s, nil, graphicsdriver.FillAll) + dst.DrawTriangles([graphics.ShaderSrcImageCount]*atlas.Image{}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderSrcImageCount]image.Rectangle{}, s, nil, graphicsdriver.FillAll) // Ensure other objects are GCed, as GC appends deferred functions for collected objects. ensureGC() diff --git a/internal/buffered/image.go b/internal/buffered/image.go index 7bf2f0f96..1b2c501ac 100644 --- a/internal/buffered/image.go +++ b/internal/buffered/image.go @@ -183,7 +183,7 @@ func (i *Image) WritePixels(pix []byte, region image.Rectangle) { // DrawTriangles draws the src image with the given vertices. // // Copying vertices and indices is the caller's responsibility. -func (i *Image) DrawTriangles(srcs [graphics.ShaderImageCount]*Image, vertices []float32, indices []uint32, blend graphicsdriver.Blend, dstRegion image.Rectangle, srcRegions [graphics.ShaderImageCount]image.Rectangle, shader *atlas.Shader, uniforms []uint32, fillRule graphicsdriver.FillRule) { +func (i *Image) DrawTriangles(srcs [graphics.ShaderSrcImageCount]*Image, vertices []float32, indices []uint32, blend graphicsdriver.Blend, dstRegion image.Rectangle, srcRegions [graphics.ShaderSrcImageCount]image.Rectangle, shader *atlas.Shader, uniforms []uint32, fillRule graphicsdriver.FillRule) { for _, src := range srcs { if i == src { panic("buffered: Image.DrawTriangles: source images must be different from the receiver") @@ -197,7 +197,7 @@ func (i *Image) DrawTriangles(srcs [graphics.ShaderImageCount]*Image, vertices [ i.syncPixelsIfNeeded() - var imgs [graphics.ShaderImageCount]*atlas.Image + var imgs [graphics.ShaderSrcImageCount]*atlas.Image for i, img := range srcs { if img == nil { continue @@ -211,6 +211,45 @@ func (i *Image) DrawTriangles(srcs [graphics.ShaderImageCount]*Image, vertices [ i.pixels = nil } +func DrawTrianglesMRT(dsts [graphics.ShaderDstImageCount]*Image, srcs [graphics.ShaderSrcImageCount]*Image, vertices []float32, indices []uint32, blend graphicsdriver.Blend, dstRegion image.Rectangle, srcRegions [graphics.ShaderSrcImageCount]image.Rectangle, shader *atlas.Shader, uniforms []uint32, fillRule graphicsdriver.FillRule) { + for _, src := range srcs { + for _, dst := range dsts { + if dst == src { + panic("buffered: DrawTrianglesMRT: source images must be different from the destination") + } + } + if src != nil { + // src's pixels have to be synced between CPU and GPU, + // but doesn't have to be cleared since src is not modified in this function. + src.syncPixelsIfNeeded() + } + } + + var dstImgs [graphics.ShaderDstImageCount]*atlas.Image + for i, dst := range dsts { + if dst == nil { + continue + } + dst.syncPixelsIfNeeded() + dstImgs[i] = dst.img + } + + var srcImgs [graphics.ShaderSrcImageCount]*atlas.Image + for i, src := range srcs { + if src == nil { + continue + } + srcImgs[i] = src.img + } + + atlas.DrawTrianglesMRT(dstImgs, srcImgs, vertices, indices, blend, dstRegion, srcRegions, shader, uniforms, fillRule) + + // After rendering, the pixel cache is no longer valid. + for _, dst := range dsts { + dst.pixels = nil + } +} + // syncPixelsIfNeeded syncs the pixels between CPU and GPU. // After syncPixelsIfNeeded, dotsBuffer is cleared, but pixels might remain. func (i *Image) syncPixelsIfNeeded() { @@ -289,10 +328,10 @@ func (i *Image) syncPixelsIfNeeded() { idx++ } - srcs := [graphics.ShaderImageCount]*atlas.Image{whiteImage.img} + srcs := [graphics.ShaderSrcImageCount]*atlas.Image{whiteImage.img} dr := image.Rect(0, 0, i.width, i.height) blend := graphicsdriver.BlendCopy - i.img.DrawTriangles(srcs, vs, is, blend, dr, [graphics.ShaderImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll) + i.img.DrawTriangles(srcs, vs, is, blend, dr, [graphics.ShaderSrcImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillAll) // TODO: Use clear if Go 1.21 is available. for pos := range i.dotsBuffer { diff --git a/internal/buffered/image_test.go b/internal/buffered/image_test.go index 87b0dcb07..0516a9c03 100644 --- a/internal/buffered/image_test.go +++ b/internal/buffered/image_test.go @@ -55,8 +55,8 @@ func TestUnsyncedPixels(t *testing.T) { graphics.QuadVertices(vs, 0, 0, 16, 16, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1) is := graphics.QuadIndices() dr := image.Rect(0, 0, 16, 16) - sr := [graphics.ShaderImageCount]image.Rectangle{image.Rect(0, 0, 16, 16)} - dst.DrawTriangles([graphics.ShaderImageCount]*buffered.Image{src}, vs, is, graphicsdriver.BlendSourceOver, dr, sr, atlas.NearestFilterShader, nil, graphicsdriver.FillAll) + sr := [graphics.ShaderSrcImageCount]image.Rectangle{image.Rect(0, 0, 16, 16)} + dst.DrawTriangles([graphics.ShaderSrcImageCount]*buffered.Image{src}, vs, is, graphicsdriver.BlendSourceOver, dr, sr, atlas.NearestFilterShader, nil, graphicsdriver.FillAll) // Check the result is correct. var got [4]byte diff --git a/internal/graphics/shader.go b/internal/graphics/shader.go index e4e95c701..85d8d1d1c 100644 --- a/internal/graphics/shader.go +++ b/internal/graphics/shader.go @@ -89,9 +89,9 @@ var __imageSrcRegionSizes [%[1]d]vec2 func imageSrcRegionOnTexture() (vec2, vec2) { return __imageSrcRegionOrigins[0], __imageSrcRegionSizes[0] } -`, ShaderImageCount) +`, ShaderSrcImageCount) - for i := 0; i < ShaderImageCount; i++ { + for i := 0; i < ShaderSrcImageCount; i++ { shaderSuffix += fmt.Sprintf(` // imageSrc%[1]dOrigin returns the source image's region origin on its texture. // The unit is the source texture's pixel or texel. @@ -179,7 +179,7 @@ func CompileShader(src []byte) (*shaderir.Program, error) { vert = "__vertex" frag = "Fragment" ) - ir, err := shader.Compile(buf.Bytes(), vert, frag, ShaderImageCount) + ir, err := shader.Compile(buf.Bytes(), vert, frag, ShaderSrcImageCount) if err != nil { return nil, err } diff --git a/internal/graphics/vertex.go b/internal/graphics/vertex.go index 449392cc1..02ba2e9fc 100644 --- a/internal/graphics/vertex.go +++ b/internal/graphics/vertex.go @@ -15,7 +15,8 @@ package graphics const ( - ShaderImageCount = 4 + ShaderSrcImageCount = 4 + ShaderDstImageCount = 4 // PreservedUniformVariablesCount represents the number of preserved uniform variables. // Any shaders in Ebitengine must have these uniform variables. @@ -30,11 +31,11 @@ const ( ProjectionMatrixUniformVariableIndex = 6 PreservedUniformUint32Count = 2 + // the destination texture size - 2*ShaderImageCount + // the source texture sizes array + 2*ShaderSrcImageCount + // the source texture sizes array 2 + // the destination image region origin 2 + // the destination image region size - 2*ShaderImageCount + // the source image region origins array - 2*ShaderImageCount + // the source image region sizes array + 2*ShaderSrcImageCount + // the source image region origins array + 2*ShaderSrcImageCount + // the source image region sizes array 16 // the projection matrix ) diff --git a/internal/graphicscommand/command.go b/internal/graphicscommand/command.go index dae55d6b4..3139a55bc 100644 --- a/internal/graphicscommand/command.go +++ b/internal/graphicscommand/command.go @@ -61,8 +61,8 @@ func (p *drawTrianglesCommandPool) put(v *drawTrianglesCommand) { // drawTrianglesCommand represents a drawing command to draw an image on another image. type drawTrianglesCommand struct { - dst *Image - srcs [graphics.ShaderImageCount]*Image + dsts [graphics.ShaderDstImageCount]*Image + srcs [graphics.ShaderSrcImageCount]*Image vertices []float32 blend graphicsdriver.Blend dstRegions []graphicsdriver.DstRegion @@ -81,12 +81,19 @@ func (c *drawTrianglesCommand) String() string { c.blend.BlendOperationRGB, c.blend.BlendOperationAlpha) - dst := fmt.Sprintf("%d", c.dst.id) - if c.dst.screen { - dst += " (screen)" + var dststrs [graphics.ShaderDstImageCount]string + for i, dst := range c.dsts { + if dst == nil { + dststrs[i] = "(nil)" + continue + } + dststrs[i] = fmt.Sprintf("%d", dst.id) + if dst.screen { + dststrs[i] += " (screen)" + } } - var srcstrs [graphics.ShaderImageCount]string + var srcstrs [graphics.ShaderSrcImageCount]string for i, src := range c.srcs { if src == nil { srcstrs[i] = "(nil)" @@ -98,7 +105,7 @@ func (c *drawTrianglesCommand) String() string { } } - return fmt.Sprintf("draw-triangles: dst: %s <- src: [%s], num of dst regions: %d, num of indices: %d, blend: %s, fill rule: %s, shader id: %d", dst, strings.Join(srcstrs[:], ", "), len(c.dstRegions), c.numIndices(), blend, c.fillRule, c.shader.id) + return fmt.Sprintf("draw-triangles: dst: [%s] <- src: [%s], num of dst regions: %d, num of indices: %d, blend: %s, fill rule: %s, shader id: %d", strings.Join(dststrs[:], ", "), strings.Join(srcstrs[:], ", "), len(c.dstRegions), c.numIndices(), blend, c.fillRule, c.shader.id) } // Exec executes the drawTrianglesCommand. @@ -108,16 +115,25 @@ func (c *drawTrianglesCommand) Exec(commandQueue *commandQueue, graphicsDriver g return nil } - var imgs [graphics.ShaderImageCount]graphicsdriver.ImageID - for i, src := range c.srcs { - if src == nil { - imgs[i] = graphicsdriver.InvalidImageID + var dsts [graphics.ShaderDstImageCount]graphicsdriver.ImageID + for i, dst := range c.dsts { + if dst == nil { + dsts[i] = graphicsdriver.InvalidImageID continue } - imgs[i] = src.image.ID() + dsts[i] = dst.image.ID() } - return graphicsDriver.DrawTriangles(c.dst.image.ID(), imgs, c.shader.shader.ID(), c.dstRegions, indexOffset, c.blend, c.uniforms, c.fillRule) + var srcs [graphics.ShaderSrcImageCount]graphicsdriver.ImageID + for i, src := range c.srcs { + if src == nil { + srcs[i] = graphicsdriver.InvalidImageID + continue + } + srcs[i] = src.image.ID() + } + + return graphicsDriver.DrawTriangles(dsts, srcs, c.shader.shader.ID(), c.dstRegions, indexOffset, c.blend, c.uniforms, c.fillRule) } func (c *drawTrianglesCommand) NeedsSync() bool { @@ -142,7 +158,7 @@ func (c *drawTrianglesCommand) setVertices(vertices []float32) { // CanMergeWithDrawTrianglesCommand returns a boolean value indicating whether the other drawTrianglesCommand can be merged // with the drawTrianglesCommand c. -func (c *drawTrianglesCommand) CanMergeWithDrawTrianglesCommand(dst *Image, srcs [graphics.ShaderImageCount]*Image, vertices []float32, blend graphicsdriver.Blend, shader *Shader, uniforms []uint32, fillRule graphicsdriver.FillRule) bool { +func (c *drawTrianglesCommand) CanMergeWithDrawTrianglesCommand(dsts [graphics.ShaderDstImageCount]*Image, srcs [graphics.ShaderSrcImageCount]*Image, vertices []float32, blend graphicsdriver.Blend, shader *Shader, uniforms []uint32, fillRule graphicsdriver.FillRule) bool { if c.shader != shader { return false } @@ -154,7 +170,7 @@ func (c *drawTrianglesCommand) CanMergeWithDrawTrianglesCommand(dst *Image, srcs return false } } - if c.dst != dst { + if c.dsts != dsts { return false } if c.srcs != srcs { diff --git a/internal/graphicscommand/commandqueue.go b/internal/graphicscommand/commandqueue.go index f91a1a52b..7cadaba24 100644 --- a/internal/graphicscommand/commandqueue.go +++ b/internal/graphicscommand/commandqueue.go @@ -105,7 +105,7 @@ func mustUseDifferentVertexBuffer(nextNumVertexFloats int) bool { } // EnqueueDrawTrianglesCommand enqueues a drawing-image command. -func (q *commandQueue) EnqueueDrawTrianglesCommand(dst *Image, srcs [graphics.ShaderImageCount]*Image, vertices []float32, indices []uint32, blend graphicsdriver.Blend, dstRegion image.Rectangle, srcRegions [graphics.ShaderImageCount]image.Rectangle, shader *Shader, uniforms []uint32, fillRule graphicsdriver.FillRule) { +func (q *commandQueue) EnqueueDrawTrianglesCommand(dsts [graphics.ShaderDstImageCount]*Image, 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) > maxVertexFloatCount { panic(fmt.Sprintf("graphicscommand: len(vertices) must equal to or less than %d but was %d", maxVertexFloatCount, len(vertices))) } @@ -125,7 +125,7 @@ func (q *commandQueue) EnqueueDrawTrianglesCommand(dst *Image, srcs [graphics.Sh // prependPreservedUniforms not only prepends values to the given slice but also creates a new slice. // Allocating a new slice is necessary to make EnqueueDrawTrianglesCommand safe so far. // TODO: This might cause a performance issue (#2601). - uniforms = q.prependPreservedUniforms(uniforms, shader, dst, srcs, dstRegion, srcRegions) + uniforms = q.prependPreservedUniforms(uniforms, shader, dsts, srcs, dstRegion, srcRegions) // Remove unused uniform variables so that more commands can be merged. shader.ir.FilterUniformVariables(uniforms) @@ -133,7 +133,7 @@ func (q *commandQueue) EnqueueDrawTrianglesCommand(dst *Image, srcs [graphics.Sh // TODO: If dst is the screen, reorder the command to be the last. if !split && 0 < len(q.commands) { if last, ok := q.commands[len(q.commands)-1].(*drawTrianglesCommand); ok { - if last.CanMergeWithDrawTrianglesCommand(dst, srcs, vertices, blend, shader, uniforms, fillRule) { + if last.CanMergeWithDrawTrianglesCommand(dsts, srcs, vertices, blend, shader, uniforms, fillRule) { last.setVertices(q.lastVertices(len(vertices) + last.numVertices())) if last.dstRegions[len(last.dstRegions)-1].Region == dstRegion { last.dstRegions[len(last.dstRegions)-1].IndexCount += len(indices) @@ -149,7 +149,7 @@ func (q *commandQueue) EnqueueDrawTrianglesCommand(dst *Image, srcs [graphics.Sh } c := q.drawTrianglesCommandPool.get() - c.dst = dst + c.dsts = dsts c.srcs = srcs c.vertices = q.lastVertices(len(vertices)) c.blend = blend @@ -324,18 +324,18 @@ func imageRectangleToRectangleF32(r image.Rectangle) rectangleF32 { } } -func (q *commandQueue) prependPreservedUniforms(uniforms []uint32, shader *Shader, dst *Image, srcs [graphics.ShaderImageCount]*Image, dstRegion image.Rectangle, srcRegions [graphics.ShaderImageCount]image.Rectangle) []uint32 { +func (q *commandQueue) prependPreservedUniforms(uniforms []uint32, shader *Shader, dsts [graphics.ShaderDstImageCount]*Image, srcs [graphics.ShaderSrcImageCount]*Image, dstRegion image.Rectangle, srcRegions [graphics.ShaderSrcImageCount]image.Rectangle) []uint32 { origUniforms := uniforms uniforms = q.uint32sBuffer.alloc(len(origUniforms) + graphics.PreservedUniformUint32Count) copy(uniforms[graphics.PreservedUniformUint32Count:], origUniforms) // Set the destination texture size. - dw, dh := dst.InternalSize() + dw, dh := dsts[0].InternalSize() uniforms[0] = math.Float32bits(float32(dw)) uniforms[1] = math.Float32bits(float32(dh)) uniformIndex := 2 - for i := 0; i < graphics.ShaderImageCount; i++ { + for i := 0; i < graphics.ShaderSrcImageCount; i++ { var floatW, floatH uint32 if srcs[i] != nil { w, h := srcs[i].InternalSize() @@ -346,7 +346,7 @@ func (q *commandQueue) prependPreservedUniforms(uniforms []uint32, shader *Shade uniforms[uniformIndex+i*2] = floatW uniforms[uniformIndex+1+i*2] = floatH } - uniformIndex += graphics.ShaderImageCount * 2 + uniformIndex += graphics.ShaderSrcImageCount * 2 dr := imageRectangleToRectangleF32(dstRegion) if shader.unit() == shaderir.Texels { @@ -366,7 +366,7 @@ func (q *commandQueue) prependPreservedUniforms(uniforms []uint32, shader *Shade uniforms[uniformIndex+1] = math.Float32bits(dr.height) uniformIndex += 2 - var srs [graphics.ShaderImageCount]rectangleF32 + var srs [graphics.ShaderSrcImageCount]rectangleF32 for i, r := range srcRegions { srs[i] = imageRectangleToRectangleF32(r) } @@ -384,18 +384,18 @@ func (q *commandQueue) prependPreservedUniforms(uniforms []uint32, shader *Shade } // Set the source region origins. - for i := 0; i < graphics.ShaderImageCount; i++ { + for i := 0; i < graphics.ShaderSrcImageCount; i++ { uniforms[uniformIndex+i*2] = math.Float32bits(srs[i].x) uniforms[uniformIndex+1+i*2] = math.Float32bits(srs[i].y) } - uniformIndex += graphics.ShaderImageCount * 2 + uniformIndex += graphics.ShaderSrcImageCount * 2 // Set the source region sizes. - for i := 0; i < graphics.ShaderImageCount; i++ { + for i := 0; i < graphics.ShaderSrcImageCount; i++ { uniforms[uniformIndex+i*2] = math.Float32bits(srs[i].width) uniforms[uniformIndex+1+i*2] = math.Float32bits(srs[i].height) } - uniformIndex += graphics.ShaderImageCount * 2 + uniformIndex += graphics.ShaderSrcImageCount * 2 // Set the projection matrix. uniforms[uniformIndex] = math.Float32bits(2 / float32(dw)) @@ -469,11 +469,11 @@ func (c *commandQueueManager) putCommandQueue(commandQueue *commandQueue) { c.pool.put(commandQueue) } -func (c *commandQueueManager) enqueueDrawTrianglesCommand(dst *Image, srcs [graphics.ShaderImageCount]*Image, vertices []float32, indices []uint32, blend graphicsdriver.Blend, dstRegion image.Rectangle, srcRegions [graphics.ShaderImageCount]image.Rectangle, shader *Shader, uniforms []uint32, fillRule graphicsdriver.FillRule) { +func (c *commandQueueManager) enqueueDrawTrianglesCommand(dsts [graphics.ShaderDstImageCount]*Image, 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 c.current == nil { c.current, _ = c.pool.get() } - c.current.EnqueueDrawTrianglesCommand(dst, srcs, vertices, indices, blend, dstRegion, srcRegions, shader, uniforms, fillRule) + c.current.EnqueueDrawTrianglesCommand(dsts, srcs, vertices, indices, blend, dstRegion, srcRegions, shader, uniforms, fillRule) } func (c *commandQueueManager) flush(graphicsDriver graphicsdriver.Graphics, endFrame bool) error { diff --git a/internal/graphicscommand/image.go b/internal/graphicscommand/image.go index 64ff0abbd..2ac8ae58d 100644 --- a/internal/graphicscommand/image.go +++ b/internal/graphicscommand/image.go @@ -130,7 +130,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.ShaderImageCount]*Image, vertices []float32, indices []uint32, blend graphicsdriver.Blend, dstRegion image.Rectangle, srcRegions [graphics.ShaderImageCount]image.Rectangle, shader *Shader, uniforms []uint32, fillRule graphicsdriver.FillRule) { +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) { for _, src := range srcs { if src == nil { continue @@ -142,7 +142,45 @@ func (i *Image) DrawTriangles(srcs [graphics.ShaderImageCount]*Image, vertices [ } i.flushBufferedWritePixels() - theCommandQueueManager.enqueueDrawTrianglesCommand(i, srcs, vertices, indices, blend, dstRegion, srcRegions, shader, uniforms, fillRule) + theCommandQueueManager.enqueueDrawTrianglesCommand([graphics.ShaderDstImageCount]*Image{i}, srcs, vertices, indices, blend, dstRegion, srcRegions, shader, uniforms, fillRule) +} + +// 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 +// +// src and shader are exclusive and only either is non-nil. +// +// The elements that index is in between 2 and 7 are used for the source images. +// The source image is 1) src argument if non-nil, or 2) an image value in the uniform variables if it exists. +// If there are multiple images in the uniform variables, the smallest ID's value is adopted. +// +// 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 DrawTrianglesMRT(dsts [graphics.ShaderDstImageCount]*Image, 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) { + for _, src := range srcs { + if src == nil { + continue + } + if src.screen { + panic("graphicscommand: the screen image cannot be the rendering des") + } + src.flushBufferedWritePixels() + } + for _, dst := range dsts { + dst.flushBufferedWritePixels() + } + + theCommandQueueManager.enqueueDrawTrianglesCommand(dsts, srcs, vertices, indices, blend, dstRegion, srcRegions, shader, uniforms, fillRule) } // ReadPixels reads the image's pixels. diff --git a/internal/graphicscommand/image_test.go b/internal/graphicscommand/image_test.go index e8d08e032..0ed3d5e31 100644 --- a/internal/graphicscommand/image_test.go +++ b/internal/graphicscommand/image_test.go @@ -59,7 +59,7 @@ func TestClear(t *testing.T) { vs := quadVertices(w/2, h/2) is := graphics.QuadIndices() dr := image.Rect(0, 0, w, h) - dst.DrawTriangles([graphics.ShaderImageCount]*graphicscommand.Image{src}, vs, is, graphicsdriver.BlendClear, dr, [graphics.ShaderImageCount]image.Rectangle{}, nearestFilterShader, nil, graphicsdriver.FillAll) + dst.DrawTriangles([graphics.ShaderSrcImageCount]*graphicscommand.Image{src}, vs, is, graphicsdriver.BlendClear, dr, [graphics.ShaderSrcImageCount]image.Rectangle{}, nearestFilterShader, nil, graphicsdriver.FillAll) pix := make([]byte, 4*w*h) if err := dst.ReadPixels(ui.Get().GraphicsDriverForTesting(), []graphicsdriver.PixelsArgs{ @@ -90,8 +90,8 @@ func TestWritePixelsPartAfterDrawTriangles(t *testing.T) { vs := quadVertices(w/2, h/2) is := graphics.QuadIndices() dr := image.Rect(0, 0, w, h) - dst.DrawTriangles([graphics.ShaderImageCount]*graphicscommand.Image{clr}, vs, is, graphicsdriver.BlendClear, dr, [graphics.ShaderImageCount]image.Rectangle{}, nearestFilterShader, nil, graphicsdriver.FillAll) - dst.DrawTriangles([graphics.ShaderImageCount]*graphicscommand.Image{src}, vs, is, graphicsdriver.BlendSourceOver, dr, [graphics.ShaderImageCount]image.Rectangle{}, nearestFilterShader, nil, graphicsdriver.FillAll) + dst.DrawTriangles([graphics.ShaderSrcImageCount]*graphicscommand.Image{clr}, vs, is, graphicsdriver.BlendClear, dr, [graphics.ShaderSrcImageCount]image.Rectangle{}, nearestFilterShader, nil, graphicsdriver.FillAll) + dst.DrawTriangles([graphics.ShaderSrcImageCount]*graphicscommand.Image{src}, vs, is, graphicsdriver.BlendSourceOver, dr, [graphics.ShaderSrcImageCount]image.Rectangle{}, nearestFilterShader, nil, graphicsdriver.FillAll) bs := graphics.NewManagedBytes(4, func(bs []byte) { for i := range bs { bs[i] = 0 @@ -109,11 +109,11 @@ func TestShader(t *testing.T) { vs := quadVertices(w, h) is := graphics.QuadIndices() dr := image.Rect(0, 0, w, h) - dst.DrawTriangles([graphics.ShaderImageCount]*graphicscommand.Image{clr}, vs, is, graphicsdriver.BlendClear, dr, [graphics.ShaderImageCount]image.Rectangle{}, nearestFilterShader, nil, graphicsdriver.FillAll) + dst.DrawTriangles([graphics.ShaderSrcImageCount]*graphicscommand.Image{clr}, vs, is, graphicsdriver.BlendClear, dr, [graphics.ShaderSrcImageCount]image.Rectangle{}, nearestFilterShader, nil, graphicsdriver.FillAll) g := ui.Get().GraphicsDriverForTesting() s := graphicscommand.NewShader(etesting.ShaderProgramFill(0xff, 0, 0, 0xff)) - dst.DrawTriangles([graphics.ShaderImageCount]*graphicscommand.Image{}, vs, is, graphicsdriver.BlendSourceOver, dr, [graphics.ShaderImageCount]image.Rectangle{}, s, nil, graphicsdriver.FillAll) + dst.DrawTriangles([graphics.ShaderSrcImageCount]*graphicscommand.Image{}, vs, is, graphicsdriver.BlendSourceOver, dr, [graphics.ShaderSrcImageCount]image.Rectangle{}, s, nil, graphicsdriver.FillAll) pix := make([]byte, 4*w*h) if err := dst.ReadPixels(g, []graphicsdriver.PixelsArgs{ diff --git a/internal/graphicsdriver/directx/graphics11_windows.go b/internal/graphicsdriver/directx/graphics11_windows.go index 807009046..1764e1c83 100644 --- a/internal/graphicsdriver/directx/graphics11_windows.go +++ b/internal/graphicsdriver/directx/graphics11_windows.go @@ -515,14 +515,14 @@ func (g *graphics11) removeShader(s *shader11) { delete(g.shaders, s.id) } -func (g *graphics11) DrawTriangles(dstID graphicsdriver.ImageID, srcIDs [graphics.ShaderImageCount]graphicsdriver.ImageID, shaderID graphicsdriver.ShaderID, dstRegions []graphicsdriver.DstRegion, indexOffset int, blend graphicsdriver.Blend, uniforms []uint32, fillRule graphicsdriver.FillRule) error { +func (g *graphics11) DrawTriangles(dstIDs [graphics.ShaderDstImageCount]graphicsdriver.ImageID, srcIDs [graphics.ShaderSrcImageCount]graphicsdriver.ImageID, shaderID graphicsdriver.ShaderID, dstRegions []graphicsdriver.DstRegion, indexOffset int, blend graphicsdriver.Blend, uniforms []uint32, fillRule graphicsdriver.FillRule) error { // Remove bound textures first. This is needed to avoid warnings on the debugger. g.deviceContext.OMSetRenderTargets([]*_ID3D11RenderTargetView{nil}, nil) - srvs := [graphics.ShaderImageCount]*_ID3D11ShaderResourceView{} + srvs := [graphics.ShaderSrcImageCount]*_ID3D11ShaderResourceView{} g.deviceContext.PSSetShaderResources(0, srvs[:]) - dst := g.images[dstID] - var srcs [graphics.ShaderImageCount]*image11 + dst := g.images[dstIDs[0]] // TODO: handle array + var srcs [graphics.ShaderSrcImageCount]*image11 for i, id := range srcIDs { img := g.images[id] if img == nil { diff --git a/internal/graphicsdriver/directx/graphics12_windows.go b/internal/graphicsdriver/directx/graphics12_windows.go index 69740b90e..1827bce44 100644 --- a/internal/graphicsdriver/directx/graphics12_windows.go +++ b/internal/graphicsdriver/directx/graphics12_windows.go @@ -1082,7 +1082,7 @@ func (g *graphics12) NewShader(program *shaderir.Program) (graphicsdriver.Shader return s, nil } -func (g *graphics12) DrawTriangles(dstID graphicsdriver.ImageID, srcs [graphics.ShaderImageCount]graphicsdriver.ImageID, shaderID graphicsdriver.ShaderID, dstRegions []graphicsdriver.DstRegion, indexOffset int, blend graphicsdriver.Blend, uniforms []uint32, fillRule graphicsdriver.FillRule) error { +func (g *graphics12) DrawTriangles(dstIDs [graphics.ShaderDstImageCount]graphicsdriver.ImageID, srcs [graphics.ShaderSrcImageCount]graphicsdriver.ImageID, shaderID graphicsdriver.ShaderID, dstRegions []graphicsdriver.DstRegion, indexOffset int, blend graphicsdriver.Blend, uniforms []uint32, fillRule graphicsdriver.FillRule) error { if shaderID == graphicsdriver.InvalidShaderID { return fmt.Errorf("directx: shader ID is invalid") } @@ -1103,13 +1103,13 @@ func (g *graphics12) DrawTriangles(dstID graphicsdriver.ImageID, srcs [graphics. g.pipelineStates.releaseConstantBuffers(g.frameIndex) } - dst := g.images[dstID] + dst := g.images[dstIDs[0]] // TODO: handle array var resourceBarriers []_D3D12_RESOURCE_BARRIER_Transition if rb, ok := dst.transiteState(_D3D12_RESOURCE_STATE_RENDER_TARGET); ok { resourceBarriers = append(resourceBarriers, rb) } - var srcImages [graphics.ShaderImageCount]*image12 + var srcImages [graphics.ShaderSrcImageCount]*image12 for i, srcID := range srcs { src := g.images[srcID] if src == nil { diff --git a/internal/graphicsdriver/directx/pipeline12_windows.go b/internal/graphicsdriver/directx/pipeline12_windows.go index c06b4d9d4..4a768dec7 100644 --- a/internal/graphicsdriver/directx/pipeline12_windows.go +++ b/internal/graphicsdriver/directx/pipeline12_windows.go @@ -128,7 +128,7 @@ type pipelineStates struct { constantBufferMaps [frameCount][]uintptr } -const numConstantBufferAndSourceTextures = 1 + graphics.ShaderImageCount +const numConstantBufferAndSourceTextures = 1 + graphics.ShaderSrcImageCount func (p *pipelineStates) initialize(device *_ID3D12Device) (ferr error) { // Create a CBV/SRV/UAV descriptor heap. @@ -180,7 +180,7 @@ func (p *pipelineStates) initialize(device *_ID3D12Device) (ferr error) { return nil } -func (p *pipelineStates) drawTriangles(device *_ID3D12Device, commandList *_ID3D12GraphicsCommandList, frameIndex int, screen bool, srcs [graphics.ShaderImageCount]*image12, shader *shader12, dstRegions []graphicsdriver.DstRegion, uniforms []uint32, blend graphicsdriver.Blend, indexOffset int, fillRule graphicsdriver.FillRule) error { +func (p *pipelineStates) drawTriangles(device *_ID3D12Device, commandList *_ID3D12GraphicsCommandList, frameIndex int, screen bool, srcs [graphics.ShaderSrcImageCount]*image12, shader *shader12, dstRegions []graphicsdriver.DstRegion, uniforms []uint32, blend graphicsdriver.Blend, indexOffset int, fillRule graphicsdriver.FillRule) error { idx := len(p.constantBuffers[frameIndex]) if idx >= numDescriptorsPerFrame { return fmt.Errorf("directx: too many constant buffers") @@ -354,7 +354,7 @@ func (p *pipelineStates) ensureRootSignature(device *_ID3D12Device) (rootSignatu } srv := _D3D12_DESCRIPTOR_RANGE{ RangeType: _D3D12_DESCRIPTOR_RANGE_TYPE_SRV, // t0 - NumDescriptors: graphics.ShaderImageCount, + NumDescriptors: graphics.ShaderSrcImageCount, BaseShaderRegister: 0, RegisterSpace: 0, OffsetInDescriptorsFromTableStart: 1, diff --git a/internal/graphicsdriver/directx/shader11_windows.go b/internal/graphicsdriver/directx/shader11_windows.go index b0abe79c2..c56448e6d 100644 --- a/internal/graphicsdriver/directx/shader11_windows.go +++ b/internal/graphicsdriver/directx/shader11_windows.go @@ -78,7 +78,7 @@ func (s *shader11) disposeImpl() { } } -func (s *shader11) use(uniforms []uint32, srcs [graphics.ShaderImageCount]*image11) error { +func (s *shader11) use(uniforms []uint32, srcs [graphics.ShaderSrcImageCount]*image11) error { vs, err := s.ensureVertexShader() if err != nil { return err @@ -114,7 +114,7 @@ func (s *shader11) use(uniforms []uint32, srcs [graphics.ShaderImageCount]*image s.graphics.deviceContext.Unmap(unsafe.Pointer(cb), 0) // Set the render sources. - var srvs [graphics.ShaderImageCount]*_ID3D11ShaderResourceView + var srvs [graphics.ShaderSrcImageCount]*_ID3D11ShaderResourceView for i, src := range srcs { if src == nil { continue diff --git a/internal/graphicsdriver/graphics.go b/internal/graphicsdriver/graphics.go index d15d5defb..2d019f4e0 100644 --- a/internal/graphicsdriver/graphics.go +++ b/internal/graphicsdriver/graphics.go @@ -68,7 +68,7 @@ type Graphics interface { NewShader(program *shaderir.Program) (Shader, error) // DrawTriangles draws an image onto another image with the given parameters. - DrawTriangles(dst ImageID, srcs [graphics.ShaderImageCount]ImageID, shader ShaderID, dstRegions []DstRegion, indexOffset int, blend Blend, uniforms []uint32, fillRule FillRule) error + DrawTriangles(dsts [graphics.ShaderDstImageCount]ImageID, srcs [graphics.ShaderSrcImageCount]ImageID, shader ShaderID, dstRegions []DstRegion, indexOffset int, blend Blend, uniforms []uint32, fillRule FillRule) error } type Resetter interface { diff --git a/internal/graphicsdriver/opengl/gl/debug.go b/internal/graphicsdriver/opengl/gl/debug.go index df71d288a..c1fd7a908 100644 --- a/internal/graphicsdriver/opengl/gl/debug.go +++ b/internal/graphicsdriver/opengl/gl/debug.go @@ -293,6 +293,14 @@ func (d *DebugContext) DisableVertexAttribArray(arg0 uint32) { } } +func (d *DebugContext) DrawBuffers(arg0 []uint32) { + d.Context.DrawBuffers(arg0) + fmt.Fprintln(os.Stderr, "DrawBuffers") + if e := d.Context.GetError(); e != NO_ERROR { + panic(fmt.Sprintf("gl: GetError() returned %d at DrawBuffers", e)) + } +} + func (d *DebugContext) DrawElements(arg0 uint32, arg1 int32, arg2 uint32, arg3 int) { d.Context.DrawElements(arg0, arg1, arg2, arg3) fmt.Fprintln(os.Stderr, "DrawElements") diff --git a/internal/graphicsdriver/opengl/gl/default_purego.go b/internal/graphicsdriver/opengl/gl/default_purego.go index 179cd8f4e..ac7d219df 100644 --- a/internal/graphicsdriver/opengl/gl/default_purego.go +++ b/internal/graphicsdriver/opengl/gl/default_purego.go @@ -51,6 +51,7 @@ type defaultContext struct { gpDeleteVertexArrays uintptr gpDisable uintptr gpDisableVertexAttribArray uintptr + gpDrawBuffers uintptr gpDrawElements uintptr gpEnable uintptr gpEnableVertexAttribArray uintptr @@ -269,6 +270,10 @@ func (c *defaultContext) DrawElements(mode uint32, count int32, xtype uint32, of purego.SyscallN(c.gpDrawElements, uintptr(mode), uintptr(count), uintptr(xtype), uintptr(offset)) } +func (c *defaultContext) DrawBuffers(buffers []uint32) { + purego.SyscallN(c.gpDrawBuffers, uintptr(len(buffers)), uintptr(unsafe.Pointer(&buffers[0]))) +} + func (c *defaultContext) Enable(cap uint32) { purego.SyscallN(c.gpEnable, uintptr(cap)) } @@ -501,6 +506,7 @@ func (c *defaultContext) LoadFunctions() error { c.gpDeleteVertexArrays = g.get("glDeleteVertexArrays") c.gpDisable = g.get("glDisable") c.gpDisableVertexAttribArray = g.get("glDisableVertexAttribArray") + c.gpDrawBuffers = g.get("glDrawBuffers") c.gpDrawElements = g.get("glDrawElements") c.gpEnable = g.get("glEnable") c.gpEnableVertexAttribArray = g.get("glEnableVertexAttribArray") diff --git a/internal/graphicsdriver/opengl/gl/interface.go b/internal/graphicsdriver/opengl/gl/interface.go index 4c42319c7..6565d2a30 100644 --- a/internal/graphicsdriver/opengl/gl/interface.go +++ b/internal/graphicsdriver/opengl/gl/interface.go @@ -60,6 +60,7 @@ type Context interface { Disable(cap uint32) DisableVertexAttribArray(index uint32) DrawElements(mode uint32, count int32, xtype uint32, offset int) + DrawBuffers(buffers []uint32) Enable(cap uint32) EnableVertexAttribArray(index uint32) Flush() diff --git a/internal/graphicsdriver/opengl/graphics.go b/internal/graphicsdriver/opengl/graphics.go index 44b20d620..38a132a90 100644 --- a/internal/graphicsdriver/opengl/graphics.go +++ b/internal/graphicsdriver/opengl/graphics.go @@ -45,8 +45,9 @@ type Graphics struct { // drawCalled is true just after Draw is called. This holds true until WritePixels is called. drawCalled bool - uniformVariableNameCache map[int]string - textureVariableNameCache map[int]string + uniformVariableNameCache map[int]string + textureVariableNameCache map[int]string + colorBufferVariableNameCache map[int]string uniformVars []uniformVariable @@ -198,18 +199,47 @@ func (g *Graphics) uniformVariableName(idx int) string { return name } -func (g *Graphics) DrawTriangles(dstID graphicsdriver.ImageID, srcIDs [graphics.ShaderImageCount]graphicsdriver.ImageID, shaderID graphicsdriver.ShaderID, dstRegions []graphicsdriver.DstRegion, indexOffset int, blend graphicsdriver.Blend, uniforms []uint32, fillRule graphicsdriver.FillRule) error { +func (g *Graphics) DrawTriangles(dstIDs [graphics.ShaderDstImageCount]graphicsdriver.ImageID, srcIDs [graphics.ShaderSrcImageCount]graphicsdriver.ImageID, shaderID graphicsdriver.ShaderID, dstRegions []graphicsdriver.DstRegion, indexOffset int, blend graphicsdriver.Blend, uniforms []uint32, fillRule graphicsdriver.FillRule) error { if shaderID == graphicsdriver.InvalidShaderID { return fmt.Errorf("opengl: shader ID is invalid") } - destination := g.images[dstID] - g.drawCalled = true - if err := destination.setViewport(); err != nil { - return err + dstCount := 0 + var destinations [graphics.ShaderDstImageCount]*Image + for i, dstID := range dstIDs { + if dstID == graphicsdriver.InvalidImageID { + continue + } + dst := g.images[dstIDs[i]] + if dst == nil { + continue + } + if err := dst.setViewport(); err != nil { + return err + } + destinations[i] = dst + dstCount++ } + if dstCount == 0 { + return nil + } + + // Color attachments + var attached []uint32 + for i, dst := range destinations { + if dst == nil { + continue + } + if dstCount > 1 { + //fmt.Println("id:", dst.texture, "ok:", dst.id, "regions:", len(dstRegions)) + } + attached = append(attached, uint32(gl.COLOR_ATTACHMENT0+i)) + g.context.ctx.FramebufferTexture2D(gl.FRAMEBUFFER, uint32(gl.COLOR_ATTACHMENT0+i), gl.TEXTURE_2D, uint32(dst.texture), 0) + } + g.context.ctx.DrawBuffers(attached) + g.context.blend(blend) shader := g.shaders[shaderID] @@ -232,7 +262,7 @@ func (g *Graphics) DrawTriangles(dstID graphicsdriver.ImageID, srcIDs [graphics. } // In OpenGL, the NDC's Y direction is upward, so flip the Y direction for the final framebuffer. - if destination.screen { + if dstCount == 1 && destinations[0] != nil && destinations[0].screen { const idx = graphics.ProjectionMatrixUniformVariableIndex // Invert the sign bits as float32 values. g.uniformVars[idx].value[1] ^= 1 << 31 @@ -241,7 +271,7 @@ func (g *Graphics) DrawTriangles(dstID graphicsdriver.ImageID, srcIDs [graphics. g.uniformVars[idx].value[13] ^= 1 << 31 } - var imgs [graphics.ShaderImageCount]textureVariable + var imgs [graphics.ShaderSrcImageCount]textureVariable for i, srcID := range srcIDs { if srcID == graphicsdriver.InvalidImageID { continue @@ -260,8 +290,13 @@ func (g *Graphics) DrawTriangles(dstID graphicsdriver.ImageID, srcIDs [graphics. g.uniformVars = g.uniformVars[:0] if fillRule != graphicsdriver.FillAll { - if err := destination.ensureStencilBuffer(); err != nil { - return err + for _, dst := range destinations { + if dst == nil { + continue + } + if err := dst.ensureStencilBuffer(); err != nil { + return err + } } g.context.ctx.Enable(gl.STENCIL_TEST) } @@ -273,6 +308,7 @@ func (g *Graphics) DrawTriangles(dstID graphicsdriver.ImageID, srcIDs [graphics. int32(dstRegion.Region.Dx()), int32(dstRegion.Region.Dy()), ) + switch fillRule { case graphicsdriver.NonZero: g.context.ctx.Clear(gl.STENCIL_BUFFER_BIT) @@ -280,6 +316,7 @@ func (g *Graphics) DrawTriangles(dstID graphicsdriver.ImageID, srcIDs [graphics. g.context.ctx.StencilOpSeparate(gl.FRONT, gl.KEEP, gl.KEEP, gl.INCR_WRAP) g.context.ctx.StencilOpSeparate(gl.BACK, gl.KEEP, gl.KEEP, gl.DECR_WRAP) g.context.ctx.ColorMask(false, false, false, false) + g.context.ctx.DrawElements(gl.TRIANGLES, int32(dstRegion.IndexCount), gl.UNSIGNED_INT, indexOffset*int(unsafe.Sizeof(uint32(0)))) case graphicsdriver.EvenOdd: g.context.ctx.Clear(gl.STENCIL_BUFFER_BIT) diff --git a/internal/graphicsdriver/opengl/program.go b/internal/graphicsdriver/opengl/program.go index cfe8bf8d4..fadb6fd9c 100644 --- a/internal/graphicsdriver/opengl/program.go +++ b/internal/graphicsdriver/opengl/program.go @@ -258,8 +258,20 @@ func (g *Graphics) textureVariableName(idx int) string { return name } +func (g *Graphics) colorBufferVariableName(idx int) string { + if v, ok := g.colorBufferVariableNameCache[idx]; ok { + return v + } + if g.colorBufferVariableNameCache == nil { + g.colorBufferVariableNameCache = map[int]string{} + } + name := fmt.Sprintf("gl_FragData[%d]", idx) + g.colorBufferVariableNameCache[idx] = name + return name +} + // useProgram uses the program (programTexture). -func (g *Graphics) useProgram(program program, uniforms []uniformVariable, textures [graphics.ShaderImageCount]textureVariable) error { +func (g *Graphics) useProgram(program program, uniforms []uniformVariable, textures [graphics.ShaderSrcImageCount]textureVariable) error { if g.state.lastProgram != program { g.context.ctx.UseProgram(uint32(program)) diff --git a/internal/mipmap/mipmap.go b/internal/mipmap/mipmap.go index 47ca0cad7..b79b9a079 100644 --- a/internal/mipmap/mipmap.go +++ b/internal/mipmap/mipmap.go @@ -65,7 +65,7 @@ func (m *Mipmap) ReadPixels(graphicsDriver graphicsdriver.Graphics, pixels []byt return m.orig.ReadPixels(graphicsDriver, pixels, region) } -func (m *Mipmap) DrawTriangles(srcs [graphics.ShaderImageCount]*Mipmap, vertices []float32, indices []uint32, blend graphicsdriver.Blend, dstRegion image.Rectangle, srcRegions [graphics.ShaderImageCount]image.Rectangle, shader *atlas.Shader, uniforms []uint32, fillRule graphicsdriver.FillRule, canSkipMipmap bool) { +func (m *Mipmap) DrawTriangles(srcs [graphics.ShaderSrcImageCount]*Mipmap, vertices []float32, indices []uint32, blend graphicsdriver.Blend, dstRegion image.Rectangle, srcRegions [graphics.ShaderSrcImageCount]image.Rectangle, shader *atlas.Shader, uniforms []uint32, fillRule graphicsdriver.FillRule, canSkipMipmap bool) { if len(indices) == 0 { return } @@ -103,7 +103,7 @@ func (m *Mipmap) DrawTriangles(srcs [graphics.ShaderImageCount]*Mipmap, vertices } } - var imgs [graphics.ShaderImageCount]*buffered.Image + var imgs [graphics.ShaderSrcImageCount]*buffered.Image for i, src := range srcs { if src == nil { continue @@ -127,6 +127,78 @@ func (m *Mipmap) DrawTriangles(srcs [graphics.ShaderImageCount]*Mipmap, vertices m.deallocateMipmaps() } +func DrawTrianglesMRT(dsts [graphics.ShaderDstImageCount]*Mipmap, srcs [graphics.ShaderSrcImageCount]*Mipmap, vertices []float32, indices []uint32, blend graphicsdriver.Blend, dstRegion image.Rectangle, srcRegions [graphics.ShaderSrcImageCount]image.Rectangle, shader *atlas.Shader, uniforms []uint32, fillRule graphicsdriver.FillRule, canSkipMipmap bool) { + if len(indices) == 0 { + return + } + + level := 0 + // TODO: Do we need to check all the sources' states of being volatile? + if !canSkipMipmap && srcs[0] != nil && canUseMipmap(srcs[0].imageType) { + level = math.MaxInt32 + for i := 0; i < len(indices)/3; i++ { + const n = graphics.VertexFloatCount + dx0 := vertices[n*indices[3*i]+0] + dy0 := vertices[n*indices[3*i]+1] + sx0 := vertices[n*indices[3*i]+2] + sy0 := vertices[n*indices[3*i]+3] + dx1 := vertices[n*indices[3*i+1]+0] + dy1 := vertices[n*indices[3*i+1]+1] + sx1 := vertices[n*indices[3*i+1]+2] + sy1 := vertices[n*indices[3*i+1]+3] + dx2 := vertices[n*indices[3*i+2]+0] + dy2 := vertices[n*indices[3*i+2]+1] + sx2 := vertices[n*indices[3*i+2]+2] + sy2 := vertices[n*indices[3*i+2]+3] + if l := mipmapLevelFromDistance(dx0, dy0, dx1, dy1, sx0, sy0, sx1, sy1); level > l { + level = l + } + if l := mipmapLevelFromDistance(dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2); level > l { + level = l + } + if l := mipmapLevelFromDistance(dx2, dy2, dx0, dy0, sx2, sy2, sx0, sy0); level > l { + level = l + } + } + if level == math.MaxInt32 { + panic("mipmap: level must be calculated at least once but not") + } + } + + var dstImgs [graphics.ShaderDstImageCount]*buffered.Image + for i, dst := range dsts { + if dst == nil { + continue + } + dstImgs[i] = dst.orig + } + + var srcImgs [graphics.ShaderSrcImageCount]*buffered.Image + for i, src := range srcs { + if src == nil { + continue + } + if level != 0 { + if img := src.level(level); img != nil { + const n = graphics.VertexFloatCount + s := float32(pow2(level)) + for i := 0; i < len(vertices)/n; i++ { + vertices[i*n+2] /= s + vertices[i*n+3] /= s + } + srcImgs[i] = img + continue + } + } + srcImgs[i] = src.orig + } + + buffered.DrawTrianglesMRT(dstImgs, srcImgs, vertices, indices, blend, dstRegion, srcRegions, shader, uniforms, fillRule) + for _, dst := range dsts { + dst.deallocateMipmaps() + } +} + func (m *Mipmap) setImg(level int, img *buffered.Image) { if m.imgs == nil { m.imgs = map[int]*buffered.Image{} @@ -187,7 +259,7 @@ func (m *Mipmap) level(level int) *buffered.Image { s := buffered.NewImage(w2, h2, m.imageType) dstRegion := image.Rect(0, 0, w2, h2) - s.DrawTriangles([graphics.ShaderImageCount]*buffered.Image{src}, vs, is, graphicsdriver.BlendCopy, dstRegion, [graphics.ShaderImageCount]image.Rectangle{}, shader, nil, graphicsdriver.FillAll) + s.DrawTriangles([graphics.ShaderSrcImageCount]*buffered.Image{src}, vs, is, graphicsdriver.BlendCopy, dstRegion, [graphics.ShaderSrcImageCount]image.Rectangle{}, shader, nil, graphicsdriver.FillAll) m.setImg(level, s) return m.imgs[level] diff --git a/internal/shader/shader.go b/internal/shader/shader.go index 12626b12d..2451bd58e 100644 --- a/internal/shader/shader.go +++ b/internal/shader/shader.go @@ -215,6 +215,7 @@ func Compile(src []byte, vertexEntry, fragmentEntry string, textureCount int) (* // TODO: Make a call graph and reorder the elements. s.ir.TextureCount = textureCount + return &s.ir, nil } @@ -741,8 +742,9 @@ func (cs *compileState) parseFuncParams(block *block, fname string, d *ast.FuncD } // If there is only one returning value, it is treated as a returning value. + // Only if not the fragment entrypoint. // An array cannot be a returning value, especially for HLSL (#2923). - if len(out) == 1 && out[0].name == "" && out[0].typ.Main != shaderir.Array { + if fname != cs.fragmentEntry && len(out) == 1 && out[0].name == "" && out[0].typ.Main != shaderir.Array { ret = out[0].typ out = nil } @@ -820,10 +822,13 @@ func (cs *compileState) parseFunc(block *block, d *ast.FuncDecl) (function, bool return function{}, false } - if len(outParams) != 0 || returnType.Main != shaderir.Vec4 { - cs.addError(d.Pos(), "fragment entry point must have one returning vec4 value for a color") + // The first out-param is treated as gl_FragColor in GLSL. + if outParams[0].typ.Main != shaderir.Vec4 { + cs.addError(d.Pos(), "fragment entry point must have at least one returning vec4 value for a color") return function{}, false } + // Adjust the number of textures to write to + cs.ir.ColorsOutCount = len(outParams) if cs.varyingParsed { checkVaryings(inParams[1:]) diff --git a/internal/shaderir/glsl/glsl.go b/internal/shaderir/glsl/glsl.go index e947e0b5c..2504bffe1 100644 --- a/internal/shaderir/glsl/glsl.go +++ b/internal/shaderir/glsl/glsl.go @@ -87,8 +87,7 @@ precision highp int; #define mediump #define highp #endif - -out vec4 fragColor;` +` if version == GLSLVersionDefault { prelude += "\n\n" + utilFunctions } @@ -420,7 +419,7 @@ func (c *compileContext) localVariableName(p *shaderir.Program, topBlock *shader case idx < nv+1: return fmt.Sprintf("V%d", idx-1) default: - return fmt.Sprintf("l%d", idx-(nv+1)) + return fmt.Sprintf("gl_FragData[%d]", idx-(nv+1)) } default: return fmt.Sprintf("l%d", idx) @@ -595,7 +594,7 @@ func (c *compileContext) block(p *shaderir.Program, topBlock, block *shaderir.Bl case shaderir.Return: switch { case topBlock == p.FragmentFunc.Block: - lines = append(lines, fmt.Sprintf("%sfragColor = %s;", idt, expr(&s.Exprs[0]))) + lines = append(lines, fmt.Sprintf("%s%s;", idt, expr(&s.Exprs[0]))) // The 'return' statement is not required so far, as the fragment entrypoint has only one sentence so far. See adjustProgram implementation. case len(s.Exprs) == 0: lines = append(lines, idt+"return;") @@ -645,15 +644,20 @@ func adjustProgram(p *shaderir.Program) *shaderir.Program { Main: shaderir.Vec4, // gl_FragCoord } copy(inParams[1:], newP.Varyings) + // Out parameters of a fragment func are colors + outParams := make([]shaderir.Type, p.ColorsOutCount) + for i := range outParams { + outParams[i] = shaderir.Type{ + Main: shaderir.Vec4, + } + } + newP.FragmentFunc.Block.LocalVarIndexOffset += (p.ColorsOutCount-1) newP.Funcs = append(newP.Funcs, shaderir.Func{ Index: funcIdx, InParams: inParams, - OutParams: nil, - Return: shaderir.Type{ - Main: shaderir.Vec4, - }, - Block: newP.FragmentFunc.Block, + OutParams: outParams, + Block: newP.FragmentFunc.Block, }) // Create an AST to call the new function. @@ -663,7 +667,7 @@ func adjustProgram(p *shaderir.Program) *shaderir.Program { Index: funcIdx, }, } - for i := 0; i < 1+len(newP.Varyings); i++ { + for i := 0; i < 1+len(newP.Varyings)+p.ColorsOutCount; i++ { call = append(call, shaderir.Expr{ Type: shaderir.LocalVariable, Index: i, diff --git a/internal/shaderir/program.go b/internal/shaderir/program.go index 792358ab9..e4bdbe5de 100644 --- a/internal/shaderir/program.go +++ b/internal/shaderir/program.go @@ -30,15 +30,16 @@ const ( ) type Program struct { - UniformNames []string - Uniforms []Type - TextureCount int - Attributes []Type - Varyings []Type - Funcs []Func - VertexFunc VertexFunc - FragmentFunc FragmentFunc - Unit Unit + UniformNames []string + Uniforms []Type + TextureCount int + ColorsOutCount int + Attributes []Type + Varyings []Type + Funcs []Func + VertexFunc VertexFunc + FragmentFunc FragmentFunc + Unit Unit uniformFactors []uint32 } diff --git a/internal/ui/image.go b/internal/ui/image.go index 0a971a8c4..8c4ac4de8 100644 --- a/internal/ui/image.go +++ b/internal/ui/image.go @@ -77,7 +77,7 @@ func (i *Image) Deallocate() { i.mipmap.Deallocate() } -func (i *Image) DrawTriangles(srcs [graphics.ShaderImageCount]*Image, vertices []float32, indices []uint32, blend graphicsdriver.Blend, dstRegion image.Rectangle, srcRegions [graphics.ShaderImageCount]image.Rectangle, shader *Shader, uniforms []uint32, fillRule graphicsdriver.FillRule, canSkipMipmap bool, antialias bool) { +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, canSkipMipmap bool, antialias bool) { if i.modifyCallback != nil { i.modifyCallback() } @@ -104,7 +104,7 @@ func (i *Image) DrawTriangles(srcs [graphics.ShaderImageCount]*Image, vertices [ i.flushBufferIfNeeded() - var srcMipmaps [graphics.ShaderImageCount]*mipmap.Mipmap + var srcMipmaps [graphics.ShaderSrcImageCount]*mipmap.Mipmap for i, src := range srcs { if src == nil { continue @@ -116,6 +116,30 @@ func (i *Image) DrawTriangles(srcs [graphics.ShaderImageCount]*Image, vertices [ i.mipmap.DrawTriangles(srcMipmaps, vertices, indices, blend, dstRegion, srcRegions, shader.shader, uniforms, fillRule, canSkipMipmap) } +func DrawTrianglesMRT(dsts [graphics.ShaderDstImageCount]*Image, 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, canSkipMipmap bool, antialias bool) { + var dstMipmaps [graphics.ShaderDstImageCount]*mipmap.Mipmap + for i, dst := range dsts { + if dst.modifyCallback != nil { + dst.modifyCallback() + } + + dst.lastBlend = blend + dst.flushBufferIfNeeded() + dstMipmaps[i] = dst.mipmap + } + + var srcMipmaps [graphics.ShaderSrcImageCount]*mipmap.Mipmap + for i, src := range srcs { + if src == nil { + continue + } + src.flushBufferIfNeeded() + srcMipmaps[i] = src.mipmap + } + + mipmap.DrawTrianglesMRT(dstMipmaps, srcMipmaps, vertices, indices, blend, dstRegion, srcRegions, shader.shader, uniforms, fillRule, canSkipMipmap) +} + func (i *Image) WritePixels(pix []byte, region image.Rectangle) { if i.modifyCallback != nil { i.modifyCallback() @@ -175,7 +199,7 @@ func (i *Image) Fill(r, g, b, a float32, region image.Rectangle) { r, g, b, a) is := graphics.QuadIndices() - srcs := [graphics.ShaderImageCount]*Image{i.ui.whiteImage} + srcs := [graphics.ShaderSrcImageCount]*Image{i.ui.whiteImage} blend := graphicsdriver.BlendCopy // If possible, use BlendSourceOver to encourage batching (#2817). @@ -183,7 +207,7 @@ func (i *Image) Fill(r, g, b, a float32, region image.Rectangle) { blend = graphicsdriver.BlendSourceOver } // i.lastBlend is updated in DrawTriangles. - i.DrawTriangles(srcs, i.tmpVerticesForFill, is, blend, region, [graphics.ShaderImageCount]image.Rectangle{}, NearestFilterShader, nil, graphicsdriver.FillAll, true, false) + i.DrawTriangles(srcs, i.tmpVerticesForFill, is, blend, region, [graphics.ShaderSrcImageCount]image.Rectangle{}, NearestFilterShader, nil, graphicsdriver.FillAll, true, false) } type bigOffscreenImage struct { @@ -217,7 +241,7 @@ func (i *bigOffscreenImage) deallocate() { i.dirty = false } -func (i *bigOffscreenImage) drawTriangles(srcs [graphics.ShaderImageCount]*Image, vertices []float32, indices []uint32, blend graphicsdriver.Blend, dstRegion image.Rectangle, srcRegions [graphics.ShaderImageCount]image.Rectangle, shader *Shader, uniforms []uint32, fillRule graphicsdriver.FillRule, canSkipMipmap bool, antialias bool) { +func (i *bigOffscreenImage) 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, canSkipMipmap bool, antialias bool) { if i.blend != blend { i.flush() } @@ -240,7 +264,7 @@ func (i *bigOffscreenImage) drawTriangles(srcs [graphics.ShaderImageCount]*Image // Copy the current rendering result to get the correct blending result. if blend != graphicsdriver.BlendSourceOver && !i.dirty { - srcs := [graphics.ShaderImageCount]*Image{i.orig} + srcs := [graphics.ShaderSrcImageCount]*Image{i.orig} if len(i.tmpVerticesForCopying) < 4*graphics.VertexFloatCount { i.tmpVerticesForCopying = make([]float32, 4*graphics.VertexFloatCount) } @@ -252,7 +276,7 @@ func (i *bigOffscreenImage) drawTriangles(srcs [graphics.ShaderImageCount]*Image 1, 1, 1, 1) is := graphics.QuadIndices() dstRegion := image.Rect(0, 0, i.region.Dx()*bigOffscreenScale, i.region.Dy()*bigOffscreenScale) - i.image.DrawTriangles(srcs, i.tmpVerticesForCopying, is, graphicsdriver.BlendCopy, dstRegion, [graphics.ShaderImageCount]image.Rectangle{}, NearestFilterShader, nil, graphicsdriver.FillAll, true, false) + i.image.DrawTriangles(srcs, i.tmpVerticesForCopying, is, graphicsdriver.BlendCopy, dstRegion, [graphics.ShaderSrcImageCount]image.Rectangle{}, NearestFilterShader, nil, graphicsdriver.FillAll, true, false) } for idx := 0; idx < len(vertices); idx += graphics.VertexFloatCount { @@ -284,7 +308,7 @@ func (i *bigOffscreenImage) flush() { // Mark the offscreen clean earlier to avoid recursive calls. i.dirty = false - srcs := [graphics.ShaderImageCount]*Image{i.image} + srcs := [graphics.ShaderSrcImageCount]*Image{i.image} if len(i.tmpVerticesForFlushing) < 4*graphics.VertexFloatCount { i.tmpVerticesForFlushing = make([]float32, 4*graphics.VertexFloatCount) } @@ -300,7 +324,7 @@ func (i *bigOffscreenImage) flush() { if i.blend != graphicsdriver.BlendSourceOver { blend = graphicsdriver.BlendCopy } - i.orig.DrawTriangles(srcs, i.tmpVerticesForFlushing, is, blend, dstRegion, [graphics.ShaderImageCount]image.Rectangle{}, LinearFilterShader, nil, graphicsdriver.FillAll, true, false) + i.orig.DrawTriangles(srcs, i.tmpVerticesForFlushing, is, blend, dstRegion, [graphics.ShaderSrcImageCount]image.Rectangle{}, LinearFilterShader, nil, graphicsdriver.FillAll, true, false) i.image.clear() i.dirty = false From 900369263094c5d07201be66665e52274482a69e Mon Sep 17 00:00:00 2001 From: Zyko <13394516+Zyko0@users.noreply.github.com> Date: Sat, 6 Apr 2024 11:21:04 +0200 Subject: [PATCH 02/36] Restore unmanaged images --- examples/mrt/main.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/examples/mrt/main.go b/examples/mrt/main.go index 69f955c82..38a13119f 100644 --- a/examples/mrt/main.go +++ b/examples/mrt/main.go @@ -15,6 +15,7 @@ package main import ( + "image" _ "image/jpeg" "log" @@ -29,11 +30,11 @@ const ( var ( dsts = [4]*ebiten.Image{ + /*ebiten.NewImage(dstSize, dstSize), ebiten.NewImage(dstSize, dstSize), ebiten.NewImage(dstSize, dstSize), - ebiten.NewImage(dstSize, dstSize), - ebiten.NewImage(dstSize, dstSize), - /*ebiten.NewImageWithOptions(image.Rect(0, 0, dstSize, dstSize), &ebiten.NewImageOptions{ + ebiten.NewImage(dstSize, dstSize),*/ + ebiten.NewImageWithOptions(image.Rect(0, 0, dstSize, dstSize), &ebiten.NewImageOptions{ Unmanaged: true, }), ebiten.NewImageWithOptions(image.Rect(0, 0, dstSize, dstSize), &ebiten.NewImageOptions{ @@ -44,7 +45,7 @@ var ( }), ebiten.NewImageWithOptions(image.Rect(0, 0, dstSize, dstSize), &ebiten.NewImageOptions{ Unmanaged: true, - }),*/ + }), } shaderSrc = []byte( From c247da0f05295ba07e52eea38847d279c6910244 Mon Sep 17 00:00:00 2001 From: Zyko <13394516+Zyko0@users.noreply.github.com> Date: Sat, 6 Apr 2024 11:40:12 +0200 Subject: [PATCH 03/36] Remove useless debug + setviewport only once --- internal/graphicsdriver/opengl/graphics.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/internal/graphicsdriver/opengl/graphics.go b/internal/graphicsdriver/opengl/graphics.go index 38a132a90..4a7c0a6d4 100644 --- a/internal/graphicsdriver/opengl/graphics.go +++ b/internal/graphicsdriver/opengl/graphics.go @@ -216,15 +216,16 @@ func (g *Graphics) DrawTriangles(dstIDs [graphics.ShaderDstImageCount]graphicsdr if dst == nil { continue } - if err := dst.setViewport(); err != nil { - return err - } destinations[i] = dst dstCount++ } if dstCount == 0 { return nil } + // Only necessary for the same shared framebuffer + if err := destinations[0].setViewport(); err != nil { + return err + } // Color attachments var attached []uint32 @@ -232,9 +233,6 @@ func (g *Graphics) DrawTriangles(dstIDs [graphics.ShaderDstImageCount]graphicsdr if dst == nil { continue } - if dstCount > 1 { - //fmt.Println("id:", dst.texture, "ok:", dst.id, "regions:", len(dstRegions)) - } attached = append(attached, uint32(gl.COLOR_ATTACHMENT0+i)) g.context.ctx.FramebufferTexture2D(gl.FRAMEBUFFER, uint32(gl.COLOR_ATTACHMENT0+i), gl.TEXTURE_2D, uint32(dst.texture), 0) } From 577664c1cbb5d8731536283839b7ed3095ef5f79 Mon Sep 17 00:00:00 2001 From: Zyko <13394516+Zyko0@users.noreply.github.com> Date: Sat, 6 Apr 2024 14:54:32 +0200 Subject: [PATCH 04/36] Dirty directx11 --- examples/mrt/main.go | 4 +- .../directx/graphics11_windows.go | 14 ++++- .../graphicsdriver/directx/image11_windows.go | 63 +++++++++++++++++++ internal/shaderir/hlsl/hlsl.go | 23 ++++--- 4 files changed, 92 insertions(+), 12 deletions(-) diff --git a/examples/mrt/main.go b/examples/mrt/main.go index 38a13119f..5ad46f979 100644 --- a/examples/mrt/main.go +++ b/examples/mrt/main.go @@ -1,4 +1,4 @@ -// Copyright 2020 The Ebiten Authors +// Copyright 2024 The Ebiten Authors // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -124,7 +124,7 @@ func main() { ebiten.SetWindowSize(screenWidth, screenHeight) ebiten.SetWindowTitle("MRT (Ebitengine Demo)") if err := ebiten.RunGameWithOptions(&Game{}, &ebiten.RunGameOptions{ - GraphicsLibrary: ebiten.GraphicsLibraryOpenGL, + GraphicsLibrary: ebiten.GraphicsLibraryDirectX, }); err != nil { log.Fatal(err) } diff --git a/internal/graphicsdriver/directx/graphics11_windows.go b/internal/graphicsdriver/directx/graphics11_windows.go index 1764e1c83..56db26ac9 100644 --- a/internal/graphicsdriver/directx/graphics11_windows.go +++ b/internal/graphicsdriver/directx/graphics11_windows.go @@ -521,7 +521,15 @@ func (g *graphics11) DrawTriangles(dstIDs [graphics.ShaderDstImageCount]graphics srvs := [graphics.ShaderSrcImageCount]*_ID3D11ShaderResourceView{} g.deviceContext.PSSetShaderResources(0, srvs[:]) - dst := g.images[dstIDs[0]] // TODO: handle array + var dsts []*image11 + for _, id := range dstIDs { + img := g.images[id] + if img == nil { + continue + } + dsts = append(dsts, img) + } + var srcs [graphics.ShaderSrcImageCount]*image11 for i, id := range srcIDs { img := g.images[id] @@ -531,7 +539,7 @@ func (g *graphics11) DrawTriangles(dstIDs [graphics.ShaderDstImageCount]graphics srcs[i] = img } - w, h := dst.internalSize() + w, h := dsts[0].internalSize() g.deviceContext.RSSetViewports([]_D3D11_VIEWPORT{ { TopLeftX: 0, @@ -543,7 +551,7 @@ func (g *graphics11) DrawTriangles(dstIDs [graphics.ShaderDstImageCount]graphics }, }) - if err := dst.setAsRenderTarget(fillRule != graphicsdriver.FillAll); err != nil { + if err := setAsRenderTargets(dsts, fillRule != graphicsdriver.FillAll); err != nil { return err } diff --git a/internal/graphicsdriver/directx/image11_windows.go b/internal/graphicsdriver/directx/image11_windows.go index 1b3adb150..b78f0b317 100644 --- a/internal/graphicsdriver/directx/image11_windows.go +++ b/internal/graphicsdriver/directx/image11_windows.go @@ -204,6 +204,69 @@ func (i *image11) setAsRenderTarget(useStencil bool) error { return nil } +func setAsRenderTargets(dsts []*image11, useStencil bool) error { + for _, i := range dsts { + if i.renderTargetView == nil { + rtv, err := i.graphics.device.CreateRenderTargetView(unsafe.Pointer(i.texture), nil) + if err != nil { + return err + } + i.renderTargetView = rtv + } + + if !useStencil { + continue + } + + if i.screen { + return fmt.Errorf("directx: a stencil buffer is not available for a screen image") + } + if i.stencilView == nil { + sv, err := i.graphics.device.CreateDepthStencilView(unsafe.Pointer(i.stencil), nil) + if err != nil { + return err + } + i.stencilView = sv + } + if i.stencil == nil { + w, h := i.internalSize() + s, err := i.graphics.device.CreateTexture2D(&_D3D11_TEXTURE2D_DESC{ + Width: uint32(w), + Height: uint32(h), + MipLevels: 0, + ArraySize: 1, + Format: _DXGI_FORMAT_D24_UNORM_S8_UINT, + SampleDesc: _DXGI_SAMPLE_DESC{ + Count: 1, + Quality: 0, + }, + Usage: _D3D11_USAGE_DEFAULT, + BindFlags: uint32(_D3D11_BIND_DEPTH_STENCIL), + CPUAccessFlags: 0, + MiscFlags: 0, + }, nil) + if err != nil { + return err + } + i.stencil = s + } + } + + var rtvs []*_ID3D11RenderTargetView + var dsvs []*_ID3D11DepthStencilView + for _, i := range dsts { + rtvs = append(rtvs, i.renderTargetView) + dsvs = append(dsvs, i.stencilView) + } + + dsts[0].graphics.deviceContext.OMSetRenderTargets(rtvs, dsts[0].stencilView) + if useStencil { + dsts[0].graphics.deviceContext.ClearDepthStencilView(dsts[0].stencilView, uint8(_D3D11_CLEAR_STENCIL), 0, 0) + } + + return nil +} + func (i *image11) getShaderResourceView() (*_ID3D11ShaderResourceView, error) { if i.shaderResourceView == nil { srv, err := i.graphics.device.CreateShaderResourceView(unsafe.Pointer(i.texture), nil) diff --git a/internal/shaderir/hlsl/hlsl.go b/internal/shaderir/hlsl/hlsl.go index ca43cad45..4d1aca2c0 100644 --- a/internal/shaderir/hlsl/hlsl.go +++ b/internal/shaderir/hlsl/hlsl.go @@ -22,6 +22,7 @@ import ( "regexp" "strings" + "github.com/hajimehoshi/ebiten/v2/internal/graphics" "github.com/hajimehoshi/ebiten/v2/internal/shaderir" ) @@ -190,7 +191,15 @@ func Compile(p *shaderir.Program) (vertexShader, pixelShader string, offsets []i } if p.FragmentFunc.Block != nil && len(p.FragmentFunc.Block.Stmts) > 0 { pslines = append(pslines, "") - pslines = append(pslines, fmt.Sprintf("float4 PSMain(Varyings %s) : SV_TARGET {", vsOut)) + pslines = append(pslines, "struct PS_OUTPUT") + pslines = append(pslines, "{") + for i := 0; i < graphics.ShaderDstImageCount; i++ { + pslines = append(pslines, fmt.Sprintf("\tfloat4 Color%d: SV_Target%d;", i, i)) + } + pslines = append(pslines, "};") + pslines = append(pslines, "") + pslines = append(pslines, fmt.Sprintf("PS_OUTPUT PSMain(Varyings %s) {", vsOut)) + pslines = append(pslines, "\tPS_OUTPUT output;") pslines = append(pslines, c.block(p, p.FragmentFunc.Block, p.FragmentFunc.Block, 0)...) pslines = append(pslines, "}") } @@ -227,6 +236,8 @@ func Compile(p *shaderir.Program) (vertexShader, pixelShader string, offsets []i vertexShader = shaders[0] pixelShader = shaders[1] + fmt.Println("PS:", pixelShader) + return } @@ -353,7 +364,7 @@ func (c *compileContext) localVariableName(p *shaderir.Program, topBlock *shader case idx < nv+1: return fmt.Sprintf("%s.M%d", vsOut, idx-1) default: - return fmt.Sprintf("l%d", idx-(nv+1)) + return fmt.Sprintf("output.Color%d", idx-(nv+1)) } default: return fmt.Sprintf("l%d", idx) @@ -393,9 +404,6 @@ func (c *compileContext) block(p *shaderir.Program, topBlock, block *shaderir.Bl } var lines []string - for i := range block.LocalVars { - lines = append(lines, c.initVariable(p, topBlock, block, block.LocalVarIndexOffset+i, true, level)...) - } var expr func(e *shaderir.Expr) string expr = func(e *shaderir.Expr) string { @@ -502,7 +510,8 @@ func (c *compileContext) block(p *shaderir.Program, topBlock, block *shaderir.Bl case shaderir.Assign: lhs := s.Exprs[0] rhs := s.Exprs[1] - if lhs.Type == shaderir.LocalVariable { + isOutput := strings.HasPrefix(expr(&lhs), "output.Color") + if !isOutput && lhs.Type == shaderir.LocalVariable { if t := p.LocalVariableType(topBlock, block, lhs.Index); t.Main == shaderir.Array { for i := 0; i < t.Length; i++ { lines = append(lines, fmt.Sprintf("%[1]s%[2]s[%[3]d] = %[4]s[%[3]d];", idt, expr(&lhs), i, expr(&rhs))) @@ -564,7 +573,7 @@ func (c *compileContext) block(p *shaderir.Program, topBlock, block *shaderir.Bl case topBlock == p.VertexFunc.Block: lines = append(lines, fmt.Sprintf("%sreturn %s;", idt, vsOut)) case len(s.Exprs) == 0: - lines = append(lines, idt+"return;") + lines = append(lines, idt+"return output;") default: lines = append(lines, fmt.Sprintf("%sreturn %s;", idt, expr(&s.Exprs[0]))) } From 280cc1a732eaa8049eeb7cd8842825e057c7d911 Mon Sep 17 00:00:00 2001 From: Zyko <13394516+Zyko0@users.noreply.github.com> Date: Mon, 8 Apr 2024 23:43:19 +0200 Subject: [PATCH 05/36] Update glsl --- internal/shader/shader.go | 8 +++++--- internal/shader/stmt.go | 3 ++- internal/shaderir/glsl/glsl.go | 4 ++-- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/internal/shader/shader.go b/internal/shader/shader.go index 2451bd58e..0bd326aef 100644 --- a/internal/shader/shader.go +++ b/internal/shader/shader.go @@ -823,9 +823,11 @@ func (cs *compileState) parseFunc(block *block, d *ast.FuncDecl) (function, bool } // The first out-param is treated as gl_FragColor in GLSL. - if outParams[0].typ.Main != shaderir.Vec4 { - cs.addError(d.Pos(), "fragment entry point must have at least one returning vec4 value for a color") - return function{}, false + for i := range outParams { + if outParams[i].typ.Main != shaderir.Vec4 { + cs.addError(d.Pos(), "fragment entry point must only have vec4 return values for colors") + return function{}, false + } } // Adjust the number of textures to write to cs.ir.ColorsOutCount = len(outParams) diff --git a/internal/shader/stmt.go b/internal/shader/stmt.go index 8d64a9ef2..a3ffa43cd 100644 --- a/internal/shader/stmt.go +++ b/internal/shader/stmt.go @@ -334,7 +334,8 @@ func (cs *compileState) parseStmt(block *block, fname string, stmt ast.Stmt, inP case *ast.ReturnStmt: if len(stmt.Results) != len(outParams) && len(stmt.Results) != 1 { - if !(len(stmt.Results) == 0 && len(outParams) > 0 && outParams[0].name != "") { + // Fragment function does not have to return a value due to discard + if fname != cs.fragmentEntry && !(len(stmt.Results) == 0 && len(outParams) > 0 && outParams[0].name != "") { // TODO: Check variable shadowings. // https://go.dev/ref/spec#Return_statements cs.addError(stmt.Pos(), fmt.Sprintf("the number of returning variables must be %d but %d", len(outParams), len(stmt.Results))) diff --git a/internal/shaderir/glsl/glsl.go b/internal/shaderir/glsl/glsl.go index 2504bffe1..56e89e08e 100644 --- a/internal/shaderir/glsl/glsl.go +++ b/internal/shaderir/glsl/glsl.go @@ -603,7 +603,7 @@ func (c *compileContext) block(p *shaderir.Program, topBlock, block *shaderir.Bl } case shaderir.Discard: // 'discard' is invoked only in the fragment shader entry point. - lines = append(lines, idt+"discard;", idt+"return vec4(0.0);") + lines = append(lines, idt+"discard;") //, idt+"return vec4(0.0);") default: lines = append(lines, fmt.Sprintf("%s?(unexpected stmt: %d)", idt, s.Type)) } @@ -651,7 +651,7 @@ func adjustProgram(p *shaderir.Program) *shaderir.Program { Main: shaderir.Vec4, } } - newP.FragmentFunc.Block.LocalVarIndexOffset += (p.ColorsOutCount-1) + newP.FragmentFunc.Block.LocalVarIndexOffset += (p.ColorsOutCount - 1) newP.Funcs = append(newP.Funcs, shaderir.Func{ Index: funcIdx, From 4536fadebee739fd6c2c883f6bbde507dd0b4362 Mon Sep 17 00:00:00 2001 From: Zyko <13394516+Zyko0@users.noreply.github.com> Date: Mon, 8 Apr 2024 23:48:16 +0200 Subject: [PATCH 06/36] Update hlsl --- internal/shaderir/hlsl/hlsl.go | 105 +++++++++++++++++++++++++++++++-- 1 file changed, 99 insertions(+), 6 deletions(-) diff --git a/internal/shaderir/hlsl/hlsl.go b/internal/shaderir/hlsl/hlsl.go index 4d1aca2c0..a01557338 100644 --- a/internal/shaderir/hlsl/hlsl.go +++ b/internal/shaderir/hlsl/hlsl.go @@ -22,7 +22,6 @@ import ( "regexp" "strings" - "github.com/hajimehoshi/ebiten/v2/internal/graphics" "github.com/hajimehoshi/ebiten/v2/internal/shaderir" ) @@ -89,6 +88,7 @@ float4x4 float4x4FromScalar(float x) { func Compile(p *shaderir.Program) (vertexShader, pixelShader string, offsets []int) { offsets = calculateMemoryOffsets(p.Uniforms) + p = adjustProgram(p) c := &compileContext{ unit: p.Unit, @@ -193,7 +193,7 @@ func Compile(p *shaderir.Program) (vertexShader, pixelShader string, offsets []i pslines = append(pslines, "") pslines = append(pslines, "struct PS_OUTPUT") pslines = append(pslines, "{") - for i := 0; i < graphics.ShaderDstImageCount; i++ { + for i := 0; i < p.ColorsOutCount; i++ { pslines = append(pslines, fmt.Sprintf("\tfloat4 Color%d: SV_Target%d;", i, i)) } pslines = append(pslines, "};") @@ -404,6 +404,9 @@ func (c *compileContext) block(p *shaderir.Program, topBlock, block *shaderir.Bl } var lines []string + for i := range block.LocalVars { + lines = append(lines, c.initVariable(p, topBlock, block, block.LocalVarIndexOffset+i, true, level)...) + } var expr func(e *shaderir.Expr) string expr = func(e *shaderir.Expr) string { @@ -510,8 +513,7 @@ func (c *compileContext) block(p *shaderir.Program, topBlock, block *shaderir.Bl case shaderir.Assign: lhs := s.Exprs[0] rhs := s.Exprs[1] - isOutput := strings.HasPrefix(expr(&lhs), "output.Color") - if !isOutput && lhs.Type == shaderir.LocalVariable { + if lhs.Type == shaderir.LocalVariable { if t := p.LocalVariableType(topBlock, block, lhs.Index); t.Main == shaderir.Array { for i := 0; i < t.Length; i++ { lines = append(lines, fmt.Sprintf("%[1]s%[2]s[%[3]d] = %[4]s[%[3]d];", idt, expr(&lhs), i, expr(&rhs))) @@ -572,14 +574,18 @@ func (c *compileContext) block(p *shaderir.Program, topBlock, block *shaderir.Bl switch { case topBlock == p.VertexFunc.Block: lines = append(lines, fmt.Sprintf("%sreturn %s;", idt, vsOut)) - case len(s.Exprs) == 0: + case topBlock == p.FragmentFunc.Block: + // Call to the pseudo fragment func based on out parameters + lines = append(lines, idt+expr(&s.Exprs[0])+";") lines = append(lines, idt+"return output;") + case len(s.Exprs) == 0: + lines = append(lines, idt+"return;") default: lines = append(lines, fmt.Sprintf("%sreturn %s;", idt, expr(&s.Exprs[0]))) } case shaderir.Discard: // 'discard' is invoked only in the fragment shader entry point. - lines = append(lines, idt+"discard;", idt+"return float4(0.0, 0.0, 0.0, 0.0);") + lines = append(lines, idt+"discard;") default: lines = append(lines, fmt.Sprintf("%s?(unexpected stmt: %d)", idt, s.Type)) } @@ -587,3 +593,90 @@ func (c *compileContext) block(p *shaderir.Program, topBlock, block *shaderir.Bl return lines } + +func adjustProgram(p *shaderir.Program) *shaderir.Program { + if p.FragmentFunc.Block == nil { + return p + } + + // Shallow-clone the program in order not to modify p itself. + newP := *p + + // Create a new slice not to affect the original p. + newP.Funcs = make([]shaderir.Func, len(p.Funcs)) + copy(newP.Funcs, p.Funcs) + + // Create a new function whose body is the same is the fragment shader's entry point. + // The entry point will call this. + // This indirect call is needed for these issues: + // - Assignment to gl_FragColor doesn't work (#2245) + // - There are some odd compilers that don't work with early returns and gl_FragColor (#2247) + + // Determine a unique index of the new function. + var funcIdx int + for _, f := range newP.Funcs { + if funcIdx <= f.Index { + funcIdx = f.Index + 1 + } + } + + // For parameters of a fragment func, see the comment in internal/shaderir/program.go. + inParams := make([]shaderir.Type, 1+len(newP.Varyings)) + inParams[0] = shaderir.Type{ + Main: shaderir.Vec4, // gl_FragCoord + } + copy(inParams[1:], newP.Varyings) + // Out parameters of a fragment func are colors + outParams := make([]shaderir.Type, p.ColorsOutCount) + for i := range outParams { + outParams[i] = shaderir.Type{ + Main: shaderir.Vec4, + } + } + newP.FragmentFunc.Block.LocalVarIndexOffset += (p.ColorsOutCount - 1) + + newP.Funcs = append(newP.Funcs, shaderir.Func{ + Index: funcIdx, + InParams: inParams, + OutParams: outParams, + Block: newP.FragmentFunc.Block, + }) + + // Create an AST to call the new function. + call := []shaderir.Expr{ + { + Type: shaderir.FunctionExpr, + Index: funcIdx, + }, + } + for i := 0; i < 1+len(newP.Varyings)+p.ColorsOutCount; i++ { + call = append(call, shaderir.Expr{ + Type: shaderir.LocalVariable, + Index: i, + }) + } + + // Replace the entry point with just calling the new function. + stmts := []shaderir.Stmt{ + { + // Return: This will be replaced with assignment to gl_FragColor. + Type: shaderir.Return, + Exprs: []shaderir.Expr{ + // The function call + { + Type: shaderir.Call, + Exprs: call, + }, + }, + }, + } + newP.FragmentFunc = shaderir.FragmentFunc{ + Block: &shaderir.Block{ + LocalVars: nil, + LocalVarIndexOffset: 1 + len(newP.Varyings) + 1, + Stmts: stmts, + }, + } + + return &newP +} From c7eeae7189f269eabbe563201673703a9e6134b0 Mon Sep 17 00:00:00 2001 From: Zyko <13394516+Zyko0@users.noreply.github.com> Date: Tue, 9 Apr 2024 01:00:26 +0200 Subject: [PATCH 07/36] Cleanup --- examples/mrt/main.go | 9 ++++++--- internal/graphicsdriver/opengl/context.go | 6 +++++- internal/graphicsdriver/opengl/gl/debug.go | 8 ++++++++ internal/graphicsdriver/opengl/gl/default_purego.go | 8 ++++++++ internal/graphicsdriver/opengl/gl/interface.go | 1 + internal/graphicsdriver/opengl/program.go | 2 +- internal/graphicsdriver/opengl/shader.go | 6 +++++- internal/shader/shader.go | 2 +- internal/shaderir/glsl/glsl.go | 10 +++++++--- internal/shaderir/hlsl/hlsl.go | 8 ++------ 10 files changed, 44 insertions(+), 16 deletions(-) diff --git a/examples/mrt/main.go b/examples/mrt/main.go index 5ad46f979..15c34d949 100644 --- a/examples/mrt/main.go +++ b/examples/mrt/main.go @@ -15,11 +15,13 @@ package main import ( + "fmt" "image" _ "image/jpeg" "log" "github.com/hajimehoshi/ebiten/v2" + "github.com/hajimehoshi/ebiten/v2/ebitenutil" ) const ( @@ -71,11 +73,9 @@ func init() { } type Game struct { - count int } func (g *Game) Update() error { - g.count++ return nil } @@ -114,6 +114,8 @@ func (g *Game) Draw(screen *ebiten.Image) { opts.GeoM.Reset() opts.GeoM.Translate(dstSize, dstSize) screen.DrawImage(dsts[3], opts) + + ebitenutil.DebugPrint(screen, fmt.Sprintf("FPS: %.2f", ebiten.ActualFPS())) } func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) { @@ -122,9 +124,10 @@ func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) { func main() { ebiten.SetWindowSize(screenWidth, screenHeight) + ebiten.SetVsyncEnabled(false) ebiten.SetWindowTitle("MRT (Ebitengine Demo)") if err := ebiten.RunGameWithOptions(&Game{}, &ebiten.RunGameOptions{ - GraphicsLibrary: ebiten.GraphicsLibraryDirectX, + GraphicsLibrary: ebiten.GraphicsLibraryOpenGL, }); err != nil { log.Fatal(err) } diff --git a/internal/graphicsdriver/opengl/context.go b/internal/graphicsdriver/opengl/context.go index 57bc4bffb..0436746f9 100644 --- a/internal/graphicsdriver/opengl/context.go +++ b/internal/graphicsdriver/opengl/context.go @@ -396,7 +396,7 @@ func (c *context) newShader(shaderType uint32, source string) (shader, error) { return shader(s), nil } -func (c *context) newProgram(shaders []shader, attributes []string) (program, error) { +func (c *context) newProgram(shaders []shader, attributes, fragData []string) (program, error) { p := c.ctx.CreateProgram() if p == 0 { return 0, errors.New("opengl: glCreateProgram failed") @@ -410,6 +410,10 @@ func (c *context) newProgram(shaders []shader, attributes []string) (program, er c.ctx.BindAttribLocation(p, uint32(i), name) } + for i, name := range fragData { + c.ctx.BindFragDataLocation(p, uint32(i), name) + } + c.ctx.LinkProgram(p) if c.ctx.GetProgrami(p, gl.LINK_STATUS) == gl.FALSE { info := c.ctx.GetProgramInfoLog(p) diff --git a/internal/graphicsdriver/opengl/gl/debug.go b/internal/graphicsdriver/opengl/gl/debug.go index c1fd7a908..7c77a071e 100644 --- a/internal/graphicsdriver/opengl/gl/debug.go +++ b/internal/graphicsdriver/opengl/gl/debug.go @@ -61,6 +61,14 @@ func (d *DebugContext) BindBuffer(arg0 uint32, arg1 uint32) { } } +func (d *DebugContext) BindFragDataLocation(arg0 uint32, arg1 uint32, arg2 string) { + d.Context.BindFragDataLocation(arg0, arg1, arg2) + fmt.Fprintln(os.Stderr, "BindFragDataLocation") + if e := d.Context.GetError(); e != NO_ERROR { + panic(fmt.Sprintf("gl: GetError() returned %d at BindFragDataLocation", e)) + } +} + func (d *DebugContext) BindFramebuffer(arg0 uint32, arg1 uint32) { d.Context.BindFramebuffer(arg0, arg1) fmt.Fprintln(os.Stderr, "BindFramebuffer") diff --git a/internal/graphicsdriver/opengl/gl/default_purego.go b/internal/graphicsdriver/opengl/gl/default_purego.go index ac7d219df..b4b9b5e5a 100644 --- a/internal/graphicsdriver/opengl/gl/default_purego.go +++ b/internal/graphicsdriver/opengl/gl/default_purego.go @@ -28,6 +28,7 @@ type defaultContext struct { gpAttachShader uintptr gpBindAttribLocation uintptr gpBindBuffer uintptr + gpBindFragDataLocation uintptr gpBindFramebuffer uintptr gpBindRenderbuffer uintptr gpBindTexture uintptr @@ -140,6 +141,12 @@ func (c *defaultContext) BindBuffer(target uint32, buffer uint32) { purego.SyscallN(c.gpBindBuffer, uintptr(target), uintptr(buffer)) } +func (c *defaultContext) BindFragDataLocation(program uint32, index uint32, name string) { + cname, free := cStr(name) + defer free() + purego.SyscallN(c.gpBindFragDataLocation, uintptr(program), uintptr(index), uintptr(unsafe.Pointer(cname))) +} + func (c *defaultContext) BindFramebuffer(target uint32, framebuffer uint32) { purego.SyscallN(c.gpBindFramebuffer, uintptr(target), uintptr(framebuffer)) } @@ -483,6 +490,7 @@ func (c *defaultContext) LoadFunctions() error { c.gpAttachShader = g.get("glAttachShader") c.gpBindAttribLocation = g.get("glBindAttribLocation") c.gpBindBuffer = g.get("glBindBuffer") + c.gpBindFragDataLocation = g.get("glBindFragDataLocation") c.gpBindFramebuffer = g.get("glBindFramebuffer") c.gpBindRenderbuffer = g.get("glBindRenderbuffer") c.gpBindTexture = g.get("glBindTexture") diff --git a/internal/graphicsdriver/opengl/gl/interface.go b/internal/graphicsdriver/opengl/gl/interface.go index 6565d2a30..1825116fd 100644 --- a/internal/graphicsdriver/opengl/gl/interface.go +++ b/internal/graphicsdriver/opengl/gl/interface.go @@ -31,6 +31,7 @@ type Context interface { AttachShader(program uint32, shader uint32) BindAttribLocation(program uint32, index uint32, name string) BindBuffer(target uint32, buffer uint32) + BindFragDataLocation(program uint32, index uint32, name string) BindFramebuffer(target uint32, framebuffer uint32) BindRenderbuffer(target uint32, renderbuffer uint32) BindTexture(target uint32, texture uint32) diff --git a/internal/graphicsdriver/opengl/program.go b/internal/graphicsdriver/opengl/program.go index fadb6fd9c..9ffad216b 100644 --- a/internal/graphicsdriver/opengl/program.go +++ b/internal/graphicsdriver/opengl/program.go @@ -265,7 +265,7 @@ func (g *Graphics) colorBufferVariableName(idx int) string { if g.colorBufferVariableNameCache == nil { g.colorBufferVariableNameCache = map[int]string{} } - name := fmt.Sprintf("gl_FragData[%d]", idx) + name := fmt.Sprintf("fragColor%d", idx) g.colorBufferVariableNameCache[idx] = name return name } diff --git a/internal/graphicsdriver/opengl/shader.go b/internal/graphicsdriver/opengl/shader.go index 8f8890f59..39ced1988 100644 --- a/internal/graphicsdriver/opengl/shader.go +++ b/internal/graphicsdriver/opengl/shader.go @@ -69,7 +69,11 @@ func (s *Shader) compile() error { } defer s.graphics.context.ctx.DeleteShader(uint32(fs)) - p, err := s.graphics.context.newProgram([]shader{vs, fs}, theArrayBufferLayout.names()) + colorNames := make([]string, s.ir.ColorsOutCount) + for i := range colorNames { + colorNames[i] = s.graphics.colorBufferVariableName(i) + } + p, err := s.graphics.context.newProgram([]shader{vs, fs}, theArrayBufferLayout.names(), colorNames) if err != nil { return err } diff --git a/internal/shader/shader.go b/internal/shader/shader.go index 0bd326aef..b58d101ed 100644 --- a/internal/shader/shader.go +++ b/internal/shader/shader.go @@ -822,7 +822,7 @@ func (cs *compileState) parseFunc(block *block, d *ast.FuncDecl) (function, bool return function{}, false } - // The first out-param is treated as gl_FragColor in GLSL. + // The first out-param is treated as fragColor0 in GLSL. for i := range outParams { if outParams[i].typ.Main != shaderir.Vec4 { cs.addError(d.Pos(), "fragment entry point must only have vec4 return values for colors") diff --git a/internal/shaderir/glsl/glsl.go b/internal/shaderir/glsl/glsl.go index 56e89e08e..56611d8e7 100644 --- a/internal/shaderir/glsl/glsl.go +++ b/internal/shaderir/glsl/glsl.go @@ -86,8 +86,7 @@ precision highp int; #define lowp #define mediump #define highp -#endif -` +#endif` if version == GLSLVersionDefault { prelude += "\n\n" + utilFunctions } @@ -230,6 +229,9 @@ func Compile(p *shaderir.Program, version GLSLVersion) (vertexShader, fragmentSh fslines = append(fslines, fmt.Sprintf("in %s;", c.varDecl(p, &t, fmt.Sprintf("V%d", i)))) } } + for i := 0; i < p.ColorsOutCount; i++ { + fslines = append(fslines, fmt.Sprintf("out vec4 fragColor%d;", i)) + } var funcs []*shaderir.Func if p.VertexFunc.Block != nil { @@ -291,6 +293,8 @@ func Compile(p *shaderir.Program, version GLSLVersion) (vertexShader, fragmentSh vs = strings.TrimSpace(vs) + "\n" fs = strings.TrimSpace(fs) + "\n" + fmt.Println("FS:", fs) + return vs, fs } @@ -419,7 +423,7 @@ func (c *compileContext) localVariableName(p *shaderir.Program, topBlock *shader case idx < nv+1: return fmt.Sprintf("V%d", idx-1) default: - return fmt.Sprintf("gl_FragData[%d]", idx-(nv+1)) + return fmt.Sprintf("fragColor%d", idx-(nv+1)) } default: return fmt.Sprintf("l%d", idx) diff --git a/internal/shaderir/hlsl/hlsl.go b/internal/shaderir/hlsl/hlsl.go index a01557338..85b820d26 100644 --- a/internal/shaderir/hlsl/hlsl.go +++ b/internal/shaderir/hlsl/hlsl.go @@ -607,11 +607,6 @@ func adjustProgram(p *shaderir.Program) *shaderir.Program { copy(newP.Funcs, p.Funcs) // Create a new function whose body is the same is the fragment shader's entry point. - // The entry point will call this. - // This indirect call is needed for these issues: - // - Assignment to gl_FragColor doesn't work (#2245) - // - There are some odd compilers that don't work with early returns and gl_FragColor (#2247) - // Determine a unique index of the new function. var funcIdx int for _, f := range newP.Funcs { @@ -659,7 +654,8 @@ func adjustProgram(p *shaderir.Program) *shaderir.Program { // Replace the entry point with just calling the new function. stmts := []shaderir.Stmt{ { - // Return: This will be replaced with assignment to gl_FragColor. + // Return: This will be replaced with a call to the new function. + // Then the output structure containing colors will be returned. Type: shaderir.Return, Exprs: []shaderir.Expr{ // The function call From d1fd70495b23ace419e722cadda63fa7e4e38b54 Mon Sep 17 00:00:00 2001 From: Zyko <13394516+Zyko0@users.noreply.github.com> Date: Tue, 9 Apr 2024 02:26:29 +0200 Subject: [PATCH 08/36] Revert to gl_FragData (future webgl) --- internal/graphicsdriver/opengl/context.go | 6 +----- internal/graphicsdriver/opengl/gl/debug.go | 8 -------- internal/graphicsdriver/opengl/gl/default_cgo.go | 10 ++++++++++ internal/graphicsdriver/opengl/gl/default_purego.go | 8 -------- internal/graphicsdriver/opengl/gl/interface.go | 1 - internal/graphicsdriver/opengl/graphics.go | 1 - internal/graphicsdriver/opengl/program.go | 12 ------------ internal/graphicsdriver/opengl/shader.go | 6 +----- internal/shaderir/glsl/glsl.go | 5 +---- 9 files changed, 13 insertions(+), 44 deletions(-) diff --git a/internal/graphicsdriver/opengl/context.go b/internal/graphicsdriver/opengl/context.go index 0436746f9..57bc4bffb 100644 --- a/internal/graphicsdriver/opengl/context.go +++ b/internal/graphicsdriver/opengl/context.go @@ -396,7 +396,7 @@ func (c *context) newShader(shaderType uint32, source string) (shader, error) { return shader(s), nil } -func (c *context) newProgram(shaders []shader, attributes, fragData []string) (program, error) { +func (c *context) newProgram(shaders []shader, attributes []string) (program, error) { p := c.ctx.CreateProgram() if p == 0 { return 0, errors.New("opengl: glCreateProgram failed") @@ -410,10 +410,6 @@ func (c *context) newProgram(shaders []shader, attributes, fragData []string) (p c.ctx.BindAttribLocation(p, uint32(i), name) } - for i, name := range fragData { - c.ctx.BindFragDataLocation(p, uint32(i), name) - } - c.ctx.LinkProgram(p) if c.ctx.GetProgrami(p, gl.LINK_STATUS) == gl.FALSE { info := c.ctx.GetProgramInfoLog(p) diff --git a/internal/graphicsdriver/opengl/gl/debug.go b/internal/graphicsdriver/opengl/gl/debug.go index 7c77a071e..c1fd7a908 100644 --- a/internal/graphicsdriver/opengl/gl/debug.go +++ b/internal/graphicsdriver/opengl/gl/debug.go @@ -61,14 +61,6 @@ func (d *DebugContext) BindBuffer(arg0 uint32, arg1 uint32) { } } -func (d *DebugContext) BindFragDataLocation(arg0 uint32, arg1 uint32, arg2 string) { - d.Context.BindFragDataLocation(arg0, arg1, arg2) - fmt.Fprintln(os.Stderr, "BindFragDataLocation") - if e := d.Context.GetError(); e != NO_ERROR { - panic(fmt.Sprintf("gl: GetError() returned %d at BindFragDataLocation", e)) - } -} - func (d *DebugContext) BindFramebuffer(arg0 uint32, arg1 uint32) { d.Context.BindFramebuffer(arg0, arg1) fmt.Fprintln(os.Stderr, "BindFramebuffer") diff --git a/internal/graphicsdriver/opengl/gl/default_cgo.go b/internal/graphicsdriver/opengl/gl/default_cgo.go index c9f5a58aa..4e18b41f7 100644 --- a/internal/graphicsdriver/opengl/gl/default_cgo.go +++ b/internal/graphicsdriver/opengl/gl/default_cgo.go @@ -128,6 +128,10 @@ package gl // typedef void (*fn)(GLuint index); // ((fn)(fnptr))(index); // } +// static void glowDrawBuffers(GLsizei n, const GLenum* bufs) { +// typedef void (*fn)(GLsizei n, const GLenum* bufs); +// ((fn)(fnptr))(n, bufs); +// } // static void glowDrawElements(uintptr_t fnptr, GLenum mode, GLsizei count, GLenum type, const uintptr_t indices) { // typedef void (*fn)(GLenum mode, GLsizei count, GLenum type, const uintptr_t indices); // ((fn)(fnptr))(mode, count, type, indices); @@ -351,6 +355,7 @@ type defaultContext struct { gpDeleteVertexArrays C.uintptr_t gpDisable C.uintptr_t gpDisableVertexAttribArray C.uintptr_t + gpDrawBuffers C.uintptr_t gpDrawElements C.uintptr_t gpEnable C.uintptr_t gpEnableVertexAttribArray C.uintptr_t @@ -565,6 +570,10 @@ func (c *defaultContext) DisableVertexAttribArray(index uint32) { C.glowDisableVertexAttribArray(c.gpDisableVertexAttribArray, C.GLuint(index)) } +func (c *defaultContext) DrawBuffers(bufs []uint32) { + C.glowDrawBuffers(c.gpDrawBuffers, C.GLsizei(len(bufs)), (*C.GLenum)(unsafe.Pointer(&bufs[0]))) +} + func (c *defaultContext) DrawElements(mode uint32, count int32, xtype uint32, offset int) { C.glowDrawElements(c.gpDrawElements, C.GLenum(mode), C.GLsizei(count), C.GLenum(xtype), C.uintptr_t(offset)) } @@ -801,6 +810,7 @@ func (c *defaultContext) LoadFunctions() error { c.gpDeleteVertexArrays = C.uintptr_t(g.get("glDeleteVertexArrays")) c.gpDisable = C.uintptr_t(g.get("glDisable")) c.gpDisableVertexAttribArray = C.uintptr_t(g.get("glDisableVertexAttribArray")) + c.gpDrawBuffers = C.uintptr_t(g.get("glDrawBuffers")) c.gpDrawElements = C.uintptr_t(g.get("glDrawElements")) c.gpEnable = C.uintptr_t(g.get("glEnable")) c.gpEnableVertexAttribArray = C.uintptr_t(g.get("glEnableVertexAttribArray")) diff --git a/internal/graphicsdriver/opengl/gl/default_purego.go b/internal/graphicsdriver/opengl/gl/default_purego.go index b4b9b5e5a..ac7d219df 100644 --- a/internal/graphicsdriver/opengl/gl/default_purego.go +++ b/internal/graphicsdriver/opengl/gl/default_purego.go @@ -28,7 +28,6 @@ type defaultContext struct { gpAttachShader uintptr gpBindAttribLocation uintptr gpBindBuffer uintptr - gpBindFragDataLocation uintptr gpBindFramebuffer uintptr gpBindRenderbuffer uintptr gpBindTexture uintptr @@ -141,12 +140,6 @@ func (c *defaultContext) BindBuffer(target uint32, buffer uint32) { purego.SyscallN(c.gpBindBuffer, uintptr(target), uintptr(buffer)) } -func (c *defaultContext) BindFragDataLocation(program uint32, index uint32, name string) { - cname, free := cStr(name) - defer free() - purego.SyscallN(c.gpBindFragDataLocation, uintptr(program), uintptr(index), uintptr(unsafe.Pointer(cname))) -} - func (c *defaultContext) BindFramebuffer(target uint32, framebuffer uint32) { purego.SyscallN(c.gpBindFramebuffer, uintptr(target), uintptr(framebuffer)) } @@ -490,7 +483,6 @@ func (c *defaultContext) LoadFunctions() error { c.gpAttachShader = g.get("glAttachShader") c.gpBindAttribLocation = g.get("glBindAttribLocation") c.gpBindBuffer = g.get("glBindBuffer") - c.gpBindFragDataLocation = g.get("glBindFragDataLocation") c.gpBindFramebuffer = g.get("glBindFramebuffer") c.gpBindRenderbuffer = g.get("glBindRenderbuffer") c.gpBindTexture = g.get("glBindTexture") diff --git a/internal/graphicsdriver/opengl/gl/interface.go b/internal/graphicsdriver/opengl/gl/interface.go index 1825116fd..6565d2a30 100644 --- a/internal/graphicsdriver/opengl/gl/interface.go +++ b/internal/graphicsdriver/opengl/gl/interface.go @@ -31,7 +31,6 @@ type Context interface { AttachShader(program uint32, shader uint32) BindAttribLocation(program uint32, index uint32, name string) BindBuffer(target uint32, buffer uint32) - BindFragDataLocation(program uint32, index uint32, name string) BindFramebuffer(target uint32, framebuffer uint32) BindRenderbuffer(target uint32, renderbuffer uint32) BindTexture(target uint32, texture uint32) diff --git a/internal/graphicsdriver/opengl/graphics.go b/internal/graphicsdriver/opengl/graphics.go index 4a7c0a6d4..bc49b45aa 100644 --- a/internal/graphicsdriver/opengl/graphics.go +++ b/internal/graphicsdriver/opengl/graphics.go @@ -47,7 +47,6 @@ type Graphics struct { uniformVariableNameCache map[int]string textureVariableNameCache map[int]string - colorBufferVariableNameCache map[int]string uniformVars []uniformVariable diff --git a/internal/graphicsdriver/opengl/program.go b/internal/graphicsdriver/opengl/program.go index 9ffad216b..1a6aa05b0 100644 --- a/internal/graphicsdriver/opengl/program.go +++ b/internal/graphicsdriver/opengl/program.go @@ -258,18 +258,6 @@ func (g *Graphics) textureVariableName(idx int) string { return name } -func (g *Graphics) colorBufferVariableName(idx int) string { - if v, ok := g.colorBufferVariableNameCache[idx]; ok { - return v - } - if g.colorBufferVariableNameCache == nil { - g.colorBufferVariableNameCache = map[int]string{} - } - name := fmt.Sprintf("fragColor%d", idx) - g.colorBufferVariableNameCache[idx] = name - return name -} - // useProgram uses the program (programTexture). func (g *Graphics) useProgram(program program, uniforms []uniformVariable, textures [graphics.ShaderSrcImageCount]textureVariable) error { if g.state.lastProgram != program { diff --git a/internal/graphicsdriver/opengl/shader.go b/internal/graphicsdriver/opengl/shader.go index 39ced1988..8f8890f59 100644 --- a/internal/graphicsdriver/opengl/shader.go +++ b/internal/graphicsdriver/opengl/shader.go @@ -69,11 +69,7 @@ func (s *Shader) compile() error { } defer s.graphics.context.ctx.DeleteShader(uint32(fs)) - colorNames := make([]string, s.ir.ColorsOutCount) - for i := range colorNames { - colorNames[i] = s.graphics.colorBufferVariableName(i) - } - p, err := s.graphics.context.newProgram([]shader{vs, fs}, theArrayBufferLayout.names(), colorNames) + p, err := s.graphics.context.newProgram([]shader{vs, fs}, theArrayBufferLayout.names()) if err != nil { return err } diff --git a/internal/shaderir/glsl/glsl.go b/internal/shaderir/glsl/glsl.go index 56611d8e7..b3392c885 100644 --- a/internal/shaderir/glsl/glsl.go +++ b/internal/shaderir/glsl/glsl.go @@ -229,9 +229,6 @@ func Compile(p *shaderir.Program, version GLSLVersion) (vertexShader, fragmentSh fslines = append(fslines, fmt.Sprintf("in %s;", c.varDecl(p, &t, fmt.Sprintf("V%d", i)))) } } - for i := 0; i < p.ColorsOutCount; i++ { - fslines = append(fslines, fmt.Sprintf("out vec4 fragColor%d;", i)) - } var funcs []*shaderir.Func if p.VertexFunc.Block != nil { @@ -423,7 +420,7 @@ func (c *compileContext) localVariableName(p *shaderir.Program, topBlock *shader case idx < nv+1: return fmt.Sprintf("V%d", idx-1) default: - return fmt.Sprintf("fragColor%d", idx-(nv+1)) + return fmt.Sprintf("gl_FragData[%d]", idx-(nv+1)) } default: return fmt.Sprintf("l%d", idx) From c3a358b44b7fb6099956bfad4f9a0435f8c5c4f3 Mon Sep 17 00:00:00 2001 From: Zyko <13394516+Zyko0@users.noreply.github.com> Date: Tue, 9 Apr 2024 02:40:21 +0200 Subject: [PATCH 09/36] Fixed missing arg on gl with CGo --- internal/graphicsdriver/opengl/gl/default_cgo.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/graphicsdriver/opengl/gl/default_cgo.go b/internal/graphicsdriver/opengl/gl/default_cgo.go index 4e18b41f7..efe9271f6 100644 --- a/internal/graphicsdriver/opengl/gl/default_cgo.go +++ b/internal/graphicsdriver/opengl/gl/default_cgo.go @@ -128,7 +128,7 @@ package gl // typedef void (*fn)(GLuint index); // ((fn)(fnptr))(index); // } -// static void glowDrawBuffers(GLsizei n, const GLenum* bufs) { +// static void glowDrawBuffers(uintptr_t fnptr, GLsizei n, const GLenum* bufs) { // typedef void (*fn)(GLsizei n, const GLenum* bufs); // ((fn)(fnptr))(n, bufs); // } From fc3a6ed373d2cf8b5af7406126907c1736f464a8 Mon Sep 17 00:00:00 2001 From: Zyko <13394516+Zyko0@users.noreply.github.com> Date: Wed, 10 Apr 2024 18:56:25 +0200 Subject: [PATCH 10/36] Fixed nil dst image checks --- image.go | 3 +++ internal/buffered/image.go | 6 ++++++ internal/graphicscommand/image.go | 3 +++ internal/mipmap/mipmap.go | 3 +++ internal/ui/image.go | 3 +++ 5 files changed, 18 insertions(+) diff --git a/image.go b/image.go index d8f3d021c..27937d38c 100644 --- a/image.go +++ b/image.go @@ -809,6 +809,9 @@ func DrawTrianglesShaderMRT(dsts [graphics.ShaderDstImageCount]*Image, vertices } for _, dst := range dsts { + if dst == nil { + continue + } dst.tmpUniforms = dst.tmpUniforms[:0] dst.tmpUniforms = shader.appendUniforms(dst.tmpUniforms, options.Uniforms) } diff --git a/internal/buffered/image.go b/internal/buffered/image.go index 1b2c501ac..185bee7bc 100644 --- a/internal/buffered/image.go +++ b/internal/buffered/image.go @@ -214,6 +214,9 @@ func (i *Image) DrawTriangles(srcs [graphics.ShaderSrcImageCount]*Image, vertice func DrawTrianglesMRT(dsts [graphics.ShaderDstImageCount]*Image, srcs [graphics.ShaderSrcImageCount]*Image, vertices []float32, indices []uint32, blend graphicsdriver.Blend, dstRegion image.Rectangle, srcRegions [graphics.ShaderSrcImageCount]image.Rectangle, shader *atlas.Shader, uniforms []uint32, fillRule graphicsdriver.FillRule) { for _, src := range srcs { for _, dst := range dsts { + if dst == nil { + continue + } if dst == src { panic("buffered: DrawTrianglesMRT: source images must be different from the destination") } @@ -246,6 +249,9 @@ func DrawTrianglesMRT(dsts [graphics.ShaderDstImageCount]*Image, srcs [graphics. // After rendering, the pixel cache is no longer valid. for _, dst := range dsts { + if dst == nil { + continue + } dst.pixels = nil } } diff --git a/internal/graphicscommand/image.go b/internal/graphicscommand/image.go index 2ac8ae58d..0057f97ca 100644 --- a/internal/graphicscommand/image.go +++ b/internal/graphicscommand/image.go @@ -177,6 +177,9 @@ func DrawTrianglesMRT(dsts [graphics.ShaderDstImageCount]*Image, srcs [graphics. src.flushBufferedWritePixels() } for _, dst := range dsts { + if dst == nil { + continue + } dst.flushBufferedWritePixels() } diff --git a/internal/mipmap/mipmap.go b/internal/mipmap/mipmap.go index b79b9a079..4868e1dd8 100644 --- a/internal/mipmap/mipmap.go +++ b/internal/mipmap/mipmap.go @@ -195,6 +195,9 @@ func DrawTrianglesMRT(dsts [graphics.ShaderDstImageCount]*Mipmap, srcs [graphics buffered.DrawTrianglesMRT(dstImgs, srcImgs, vertices, indices, blend, dstRegion, srcRegions, shader, uniforms, fillRule) for _, dst := range dsts { + if dst == nil { + continue + } dst.deallocateMipmaps() } } diff --git a/internal/ui/image.go b/internal/ui/image.go index 8c4ac4de8..a51daf882 100644 --- a/internal/ui/image.go +++ b/internal/ui/image.go @@ -119,6 +119,9 @@ func (i *Image) DrawTriangles(srcs [graphics.ShaderSrcImageCount]*Image, vertice func DrawTrianglesMRT(dsts [graphics.ShaderDstImageCount]*Image, 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, canSkipMipmap bool, antialias bool) { var dstMipmaps [graphics.ShaderDstImageCount]*mipmap.Mipmap for i, dst := range dsts { + if dst == nil { + continue + } if dst.modifyCallback != nil { dst.modifyCallback() } From 55f1a5d32e75adaef54943fae87e90c193a6a9c5 Mon Sep 17 00:00:00 2001 From: Zyko <13394516+Zyko0@users.noreply.github.com> Date: Wed, 10 Apr 2024 18:57:55 +0200 Subject: [PATCH 11/36] (Fixed) webgl --- internal/graphicsdriver/opengl/context.go | 28 ++----- internal/graphicsdriver/opengl/gl/const.go | 1 + .../graphicsdriver/opengl/gl/default_js.go | 7 ++ internal/graphicsdriver/opengl/graphics.go | 83 ++++++++++++++----- internal/graphicsdriver/opengl/image.go | 17 +--- internal/jsutil/buf_js.go | 15 +++- internal/shaderir/glsl/glsl.go | 9 ++ 7 files changed, 106 insertions(+), 54 deletions(-) diff --git a/internal/graphicsdriver/opengl/context.go b/internal/graphicsdriver/opengl/context.go index 57bc4bffb..448b8efee 100644 --- a/internal/graphicsdriver/opengl/context.go +++ b/internal/graphicsdriver/opengl/context.go @@ -102,6 +102,7 @@ type context struct { locationCache *locationCache screenFramebuffer framebufferNative // This might not be the default frame buffer '0' (e.g. iOS). + mrtFramebuffer framebufferNative // The dynamic framebuffer used for MRT operations lastFramebuffer framebufferNative lastTexture textureNative lastRenderbuffer renderbufferNative @@ -110,8 +111,6 @@ type context struct { lastBlend graphicsdriver.Blend maxTextureSize int maxTextureSizeOnce sync.Once - highp bool - highpOnce sync.Once initOnce sync.Once } @@ -139,26 +138,25 @@ func (c *context) bindFramebuffer(f framebufferNative) { c.lastFramebuffer = f } -func (c *context) setViewport(f *framebuffer) { - c.bindFramebuffer(f.native) - if c.lastViewportWidth == f.width && c.lastViewportHeight == f.height { +func (c *context) setViewport(width, height int, screen bool) { + if c.lastViewportWidth == width && c.lastViewportHeight == height { return } // On some environments, viewport size must be within the framebuffer size. // e.g. Edge (#71), Chrome on GPD Pocket (#420), macOS Mojave (#691). // Use the same size of the framebuffer here. - c.ctx.Viewport(0, 0, int32(f.width), int32(f.height)) + c.ctx.Viewport(0, 0, int32(width), int32(height)) // glViewport must be called at least at every frame on iOS. // As the screen framebuffer is the last render target, next SetViewport should be // the first call at a frame. - if f.native == c.screenFramebuffer { + if screen { c.lastViewportWidth = 0 c.lastViewportHeight = 0 } else { - c.lastViewportWidth = f.width - c.lastViewportHeight = f.height + c.lastViewportWidth = width + c.lastViewportHeight = height } } @@ -264,16 +262,6 @@ func (c *context) framebufferPixels(buf []byte, f *framebuffer, region image.Rec return nil } -func (c *context) framebufferPixelsToBuffer(f *framebuffer, buffer buffer, width, height int) { - c.ctx.Flush() - - c.bindFramebuffer(f.native) - - c.ctx.BindBuffer(gl.PIXEL_PACK_BUFFER, uint32(buffer)) - c.ctx.ReadPixels(nil, 0, 0, int32(width), int32(height), gl.RGBA, gl.UNSIGNED_BYTE) - c.ctx.BindBuffer(gl.PIXEL_PACK_BUFFER, 0) -} - func (c *context) deleteTexture(t textureNative) { if c.lastTexture == t { c.lastTexture = 0 @@ -357,7 +345,7 @@ func (c *context) bindStencilBuffer(f framebufferNative, r renderbufferNative) e c.ctx.FramebufferRenderbuffer(gl.FRAMEBUFFER, gl.STENCIL_ATTACHMENT, gl.RENDERBUFFER, uint32(r)) if s := c.ctx.CheckFramebufferStatus(gl.FRAMEBUFFER); s != gl.FRAMEBUFFER_COMPLETE { - return errors.New(fmt.Sprintf("opengl: glFramebufferRenderbuffer failed: %d", s)) + return fmt.Errorf("opengl: glFramebufferRenderbuffer failed: %d", s) } return nil } diff --git a/internal/graphicsdriver/opengl/gl/const.go b/internal/graphicsdriver/opengl/gl/const.go index d59809cc1..def2933fa 100644 --- a/internal/graphicsdriver/opengl/gl/const.go +++ b/internal/graphicsdriver/opengl/gl/const.go @@ -52,6 +52,7 @@ const ( MIN = 0x8007 NEAREST = 0x2600 NO_ERROR = 0 + NONE = 0 NOTEQUAL = 0x0205 ONE = 1 ONE_MINUS_DST_ALPHA = 0x0305 diff --git a/internal/graphicsdriver/opengl/gl/default_js.go b/internal/graphicsdriver/opengl/gl/default_js.go index 4926dcd7c..308089039 100644 --- a/internal/graphicsdriver/opengl/gl/default_js.go +++ b/internal/graphicsdriver/opengl/gl/default_js.go @@ -54,6 +54,7 @@ type defaultContext struct { fnDeleteVertexArray js.Value fnDisable js.Value fnDisableVertexAttribArray js.Value + fnDrawBuffers js.Value fnDrawElements js.Value fnEnable js.Value fnEnableVertexAttribArray js.Value @@ -184,6 +185,7 @@ func NewDefaultContext(v js.Value) (Context, error) { fnDeleteVertexArray: v.Get("deleteVertexArray").Call("bind", v), fnDisable: v.Get("disable").Call("bind", v), fnDisableVertexAttribArray: v.Get("disableVertexAttribArray").Call("bind", v), + fnDrawBuffers: v.Get("drawBuffers").Call("bind", v), fnDrawElements: v.Get("drawElements").Call("bind", v), fnEnable: v.Get("enable").Call("bind", v), fnEnableVertexAttribArray: v.Get("enableVertexAttribArray").Call("bind", v), @@ -384,6 +386,11 @@ func (c *defaultContext) DisableVertexAttribArray(index uint32) { c.fnDisableVertexAttribArray.Invoke(index) } +func (c *defaultContext) DrawBuffers(bufs []uint32) { + arr := jsutil.NewUint32Array(bufs) + c.fnDrawBuffers.Invoke(arr) +} + func (c *defaultContext) DrawElements(mode uint32, count int32, xtype uint32, offset int) { c.fnDrawElements.Invoke(mode, count, xtype, offset) } diff --git a/internal/graphicsdriver/opengl/graphics.go b/internal/graphicsdriver/opengl/graphics.go index bc49b45aa..8d8d7ec5c 100644 --- a/internal/graphicsdriver/opengl/graphics.go +++ b/internal/graphicsdriver/opengl/graphics.go @@ -45,8 +45,8 @@ type Graphics struct { // drawCalled is true just after Draw is called. This holds true until WritePixels is called. drawCalled bool - uniformVariableNameCache map[int]string - textureVariableNameCache map[int]string + uniformVariableNameCache map[int]string + textureVariableNameCache map[int]string uniformVars []uniformVariable @@ -204,9 +204,9 @@ func (g *Graphics) DrawTriangles(dstIDs [graphics.ShaderDstImageCount]graphicsdr } g.drawCalled = true - + g.context.ctx.BindTexture(gl.TEXTURE_2D, 0) dstCount := 0 - var destinations [graphics.ShaderDstImageCount]*Image + var dsts [graphics.ShaderDstImageCount]*Image for i, dstID := range dstIDs { if dstID == graphicsdriver.InvalidImageID { continue @@ -215,27 +215,66 @@ func (g *Graphics) DrawTriangles(dstIDs [graphics.ShaderDstImageCount]graphicsdr if dst == nil { continue } - destinations[i] = dst + dst.ensureFramebuffer() + dsts[i] = dst dstCount++ } if dstCount == 0 { return nil } + g.context.bindFramebuffer(0) + // Only necessary for the same shared framebuffer - if err := destinations[0].setViewport(); err != nil { - return err + f := uint32(dsts[0].framebuffer.native) + if dstCount > 1 { + if g.context.mrtFramebuffer == 0 { + f = g.context.ctx.CreateFramebuffer() + if f <= 0 { + return fmt.Errorf("opengl: creating framebuffer failed: the returned value is not positive but %d", f) + } + g.context.mrtFramebuffer = framebufferNative(f) + } else { + f = uint32(g.context.mrtFramebuffer) + } + + g.context.bindFramebuffer(framebufferNative(f)) + //g.context.ctx.BindFramebuffer(gl.FRAMEBUFFER, f) + + // Reset color attachments + if s := g.context.ctx.CheckFramebufferStatus(gl.FRAMEBUFFER); s == gl.FRAMEBUFFER_COMPLETE { + g.context.ctx.Clear(16384 | gl.STENCIL_BUFFER_BIT) + } + for i, dst := range dsts { + if dst == nil { + continue + } + g.context.ctx.FramebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0+uint32(i), gl.TEXTURE_2D, uint32(dst.texture), 0) + } + if s := g.context.ctx.CheckFramebufferStatus(gl.FRAMEBUFFER); s != gl.FRAMEBUFFER_COMPLETE { + if s != 0 { + return fmt.Errorf("opengl: creating framebuffer failed: %v", s) + } + if e := g.context.ctx.GetError(); e != gl.NO_ERROR { + return fmt.Errorf("opengl: creating framebuffer failed: (glGetError) %d", e) + } + return fmt.Errorf("opengl: creating framebuffer failed: unknown error") + } + // Color attachments + var attached []uint32 + for i, dst := range dsts { + if dst == nil { + attached = append(attached, gl.NONE) + continue + } + attached = append(attached, uint32(gl.COLOR_ATTACHMENT0+i)) + } + g.context.ctx.DrawBuffers(attached) + } else { + g.context.bindFramebuffer(framebufferNative(f)) } - // Color attachments - var attached []uint32 - for i, dst := range destinations { - if dst == nil { - continue - } - attached = append(attached, uint32(gl.COLOR_ATTACHMENT0+i)) - g.context.ctx.FramebufferTexture2D(gl.FRAMEBUFFER, uint32(gl.COLOR_ATTACHMENT0+i), gl.TEXTURE_2D, uint32(dst.texture), 0) - } - g.context.ctx.DrawBuffers(attached) + w, h := dsts[0].framebufferSize() + g.context.setViewport(w, h, dsts[0].screen) g.context.blend(blend) @@ -259,7 +298,7 @@ func (g *Graphics) DrawTriangles(dstIDs [graphics.ShaderDstImageCount]graphicsdr } // In OpenGL, the NDC's Y direction is upward, so flip the Y direction for the final framebuffer. - if dstCount == 1 && destinations[0] != nil && destinations[0].screen { + if dstCount == 1 && dsts[0] != nil && dsts[0].screen { const idx = graphics.ProjectionMatrixUniformVariableIndex // Invert the sign bits as float32 values. g.uniformVars[idx].value[1] ^= 1 << 31 @@ -287,11 +326,11 @@ func (g *Graphics) DrawTriangles(dstIDs [graphics.ShaderDstImageCount]graphicsdr g.uniformVars = g.uniformVars[:0] if fillRule != graphicsdriver.FillAll { - for _, dst := range destinations { + for _, dst := range dsts { if dst == nil { continue } - if err := dst.ensureStencilBuffer(); err != nil { + if err := dst.ensureStencilBuffer(framebufferNative(f)); err != nil { return err } } @@ -336,6 +375,10 @@ func (g *Graphics) DrawTriangles(dstIDs [graphics.ShaderDstImageCount]graphicsdr g.context.ctx.Disable(gl.STENCIL_TEST) } + // Detach existing color attachments + //g.context.bindFramebuffer(fb) + //TODO: + return nil } diff --git a/internal/graphicsdriver/opengl/image.go b/internal/graphicsdriver/opengl/image.go index 11862411b..46876d63e 100644 --- a/internal/graphicsdriver/opengl/image.go +++ b/internal/graphicsdriver/opengl/image.go @@ -37,7 +37,6 @@ type Image struct { // framebuffer is a wrapper of OpenGL's framebuffer. type framebuffer struct { - graphics *Graphics native framebufferNative width int height int @@ -61,14 +60,6 @@ func (i *Image) Dispose() { i.graphics.removeImage(i) } -func (i *Image) setViewport() error { - if err := i.ensureFramebuffer(); err != nil { - return err - } - i.graphics.context.setViewport(i.framebuffer) - return nil -} - func (i *Image) ReadPixels(args []graphicsdriver.PixelsArgs) error { if err := i.ensureFramebuffer(); err != nil { return err @@ -109,14 +100,14 @@ func (i *Image) ensureFramebuffer() error { return nil } -func (i *Image) ensureStencilBuffer() error { +func (i *Image) ensureStencilBuffer(f framebufferNative) error { if i.stencil != 0 { return nil } - if err := i.ensureFramebuffer(); err != nil { + /*if err := i.ensureFramebuffer(); err != nil { return err - } + }*/ r, err := i.graphics.context.newRenderbuffer(i.framebufferSize()) if err != nil { @@ -124,7 +115,7 @@ func (i *Image) ensureStencilBuffer() error { } i.stencil = r - if err := i.graphics.context.bindStencilBuffer(i.framebuffer.native, i.stencil); err != nil { + if err := i.graphics.context.bindStencilBuffer(f, i.stencil); err != nil { return err } return nil diff --git a/internal/jsutil/buf_js.go b/internal/jsutil/buf_js.go index 50c334a20..b0f465778 100644 --- a/internal/jsutil/buf_js.go +++ b/internal/jsutil/buf_js.go @@ -24,6 +24,7 @@ var ( uint8Array = js.Global().Get("Uint8Array") float32Array = js.Global().Get("Float32Array") int32Array = js.Global().Get("Int32Array") + uint32Array = js.Global().Get("Uint32Array") ) var ( @@ -40,8 +41,11 @@ var ( // temporaryFloat32Array is a Float32ArrayBuffer whose underlying buffer is always temporaryArrayBuffer. temporaryFloat32Array = float32Array.New(temporaryArrayBuffer) - // temporaryInt32Array is a Float32ArrayBuffer whose underlying buffer is always temporaryArrayBuffer. + // temporaryInt32Array is a Int32ArrayBuffer whose underlying buffer is always temporaryArrayBuffer. temporaryInt32Array = int32Array.New(temporaryArrayBuffer) + + // temporaryUint32Array is a Uint32ArrayBuffer whose underlying buffer is always temporaryArrayBuffer. + temporaryUint32Array = uint32Array.New(temporaryArrayBuffer) ) func ensureTemporaryArrayBufferSize(byteLength int) { @@ -54,6 +58,7 @@ func ensureTemporaryArrayBufferSize(byteLength int) { temporaryUint8Array = uint8Array.New(temporaryArrayBuffer) temporaryFloat32Array = float32Array.New(temporaryArrayBuffer) temporaryInt32Array = int32Array.New(temporaryArrayBuffer) + temporaryUint32Array = uint32Array.New(temporaryArrayBuffer) } } @@ -101,3 +106,11 @@ func TemporaryInt32Array(minLength int, data []int32) js.Value { copySliceToTemporaryArrayBuffer(data) return temporaryInt32Array } + +// NewUint32Array returns a Uint32Array whose length is equal to the length of data. +func NewUint32Array(data []uint32) js.Value { + ensureTemporaryArrayBufferSize(len(data) * 4) + copySliceToTemporaryArrayBuffer(data) + a := temporaryUint32Array.Call("slice", 0, len(data)) + return a +} diff --git a/internal/shaderir/glsl/glsl.go b/internal/shaderir/glsl/glsl.go index b3392c885..7cb61841c 100644 --- a/internal/shaderir/glsl/glsl.go +++ b/internal/shaderir/glsl/glsl.go @@ -229,6 +229,12 @@ func Compile(p *shaderir.Program, version GLSLVersion) (vertexShader, fragmentSh fslines = append(fslines, fmt.Sprintf("in %s;", c.varDecl(p, &t, fmt.Sprintf("V%d", i)))) } } + // If ES300 out colors need to be defined explicitely + if version == GLSLVersionES300 { + for i := 0; i < p.ColorsOutCount; i++ { + fslines = append(fslines, fmt.Sprintf("layout(location = %d) out vec4 glFragColor%d;", i, i)) + } + } var funcs []*shaderir.Func if p.VertexFunc.Block != nil { @@ -420,6 +426,9 @@ func (c *compileContext) localVariableName(p *shaderir.Program, topBlock *shader case idx < nv+1: return fmt.Sprintf("V%d", idx-1) default: + if c.version == GLSLVersionES300 { + return fmt.Sprintf("glFragColor%d", idx-(nv+1)) + } return fmt.Sprintf("gl_FragData[%d]", idx-(nv+1)) } default: From c9eb30d66fab2082de79a410576c7353b58819ca Mon Sep 17 00:00:00 2001 From: Zyko <13394516+Zyko0@users.noreply.github.com> Date: Wed, 10 Apr 2024 18:59:21 +0200 Subject: [PATCH 12/36] Fixed magic number --- internal/graphicsdriver/opengl/gl/const.go | 1 + internal/graphicsdriver/opengl/graphics.go | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/internal/graphicsdriver/opengl/gl/const.go b/internal/graphicsdriver/opengl/gl/const.go index def2933fa..517aa9cdb 100644 --- a/internal/graphicsdriver/opengl/gl/const.go +++ b/internal/graphicsdriver/opengl/gl/const.go @@ -23,6 +23,7 @@ const ( BLEND = 0x0BE2 CLAMP_TO_EDGE = 0x812F COLOR_ATTACHMENT0 = 0x8CE0 + COLOR_BUFFER_BIT = 0x4000 COMPILE_STATUS = 0x8B81 DECR_WRAP = 0x8508 DEPTH24_STENCIL8 = 0x88F0 diff --git a/internal/graphicsdriver/opengl/graphics.go b/internal/graphicsdriver/opengl/graphics.go index 8d8d7ec5c..a55a74cae 100644 --- a/internal/graphicsdriver/opengl/graphics.go +++ b/internal/graphicsdriver/opengl/graphics.go @@ -242,7 +242,7 @@ func (g *Graphics) DrawTriangles(dstIDs [graphics.ShaderDstImageCount]graphicsdr // Reset color attachments if s := g.context.ctx.CheckFramebufferStatus(gl.FRAMEBUFFER); s == gl.FRAMEBUFFER_COMPLETE { - g.context.ctx.Clear(16384 | gl.STENCIL_BUFFER_BIT) + g.context.ctx.Clear(gl.COLOR_BUFFER_BIT | gl.STENCIL_BUFFER_BIT) } for i, dst := range dsts { if dst == nil { From 1b3bfda17b22745990d214526288dcf23019318b Mon Sep 17 00:00:00 2001 From: Zyko <13394516+Zyko0@users.noreply.github.com> Date: Wed, 10 Apr 2024 19:10:21 +0200 Subject: [PATCH 13/36] Fixed empty target for directx11 --- examples/mrt/main.go | 2 +- .../directx/graphics11_windows.go | 89 ++++++++++++++++--- .../graphicsdriver/directx/image11_windows.go | 63 ------------- 3 files changed, 78 insertions(+), 76 deletions(-) diff --git a/examples/mrt/main.go b/examples/mrt/main.go index 15c34d949..3435bcd71 100644 --- a/examples/mrt/main.go +++ b/examples/mrt/main.go @@ -127,7 +127,7 @@ func main() { ebiten.SetVsyncEnabled(false) ebiten.SetWindowTitle("MRT (Ebitengine Demo)") if err := ebiten.RunGameWithOptions(&Game{}, &ebiten.RunGameOptions{ - GraphicsLibrary: ebiten.GraphicsLibraryOpenGL, + GraphicsLibrary: ebiten.GraphicsLibraryDirectX, }); err != nil { log.Fatal(err) } diff --git a/internal/graphicsdriver/directx/graphics11_windows.go b/internal/graphicsdriver/directx/graphics11_windows.go index 56db26ac9..262de28e4 100644 --- a/internal/graphicsdriver/directx/graphics11_windows.go +++ b/internal/graphicsdriver/directx/graphics11_windows.go @@ -515,6 +515,69 @@ func (g *graphics11) removeShader(s *shader11) { delete(g.shaders, s.id) } +func (g *graphics11) setAsRenderTargets(dsts []*image11, useStencil bool) error { + var rtvs []*_ID3D11RenderTargetView + for _, i := range dsts { + // Ignore a nil image in case of MRT + if i == nil { + rtvs = append(rtvs, nil) + continue + } + if i.renderTargetView == nil { + rtv, err := g.device.CreateRenderTargetView(unsafe.Pointer(i.texture), nil) + if err != nil { + return err + } + i.renderTargetView = rtv + } + rtvs = append(rtvs, i.renderTargetView) + + if !useStencil { + continue + } + + if i.screen { + return fmt.Errorf("directx: a stencil buffer is not available for a screen image") + } + if i.stencilView == nil { + sv, err := g.device.CreateDepthStencilView(unsafe.Pointer(i.stencil), nil) + if err != nil { + return err + } + i.stencilView = sv + } + if i.stencil == nil { + w, h := i.internalSize() + s, err := g.device.CreateTexture2D(&_D3D11_TEXTURE2D_DESC{ + Width: uint32(w), + Height: uint32(h), + MipLevels: 0, + ArraySize: 1, + Format: _DXGI_FORMAT_D24_UNORM_S8_UINT, + SampleDesc: _DXGI_SAMPLE_DESC{ + Count: 1, + Quality: 0, + }, + Usage: _D3D11_USAGE_DEFAULT, + BindFlags: uint32(_D3D11_BIND_DEPTH_STENCIL), + CPUAccessFlags: 0, + MiscFlags: 0, + }, nil) + if err != nil { + return err + } + i.stencil = s + } + } + + g.deviceContext.OMSetRenderTargets(rtvs, dsts[0].stencilView) + if useStencil { + g.deviceContext.ClearDepthStencilView(dsts[0].stencilView, uint8(_D3D11_CLEAR_STENCIL), 0, 0) + } + + return nil +} + func (g *graphics11) DrawTriangles(dstIDs [graphics.ShaderDstImageCount]graphicsdriver.ImageID, srcIDs [graphics.ShaderSrcImageCount]graphicsdriver.ImageID, shaderID graphicsdriver.ShaderID, dstRegions []graphicsdriver.DstRegion, indexOffset int, blend graphicsdriver.Blend, uniforms []uint32, fillRule graphicsdriver.FillRule) error { // Remove bound textures first. This is needed to avoid warnings on the debugger. g.deviceContext.OMSetRenderTargets([]*_ID3D11RenderTargetView{nil}, nil) @@ -522,12 +585,24 @@ func (g *graphics11) DrawTriangles(dstIDs [graphics.ShaderDstImageCount]graphics g.deviceContext.PSSetShaderResources(0, srvs[:]) var dsts []*image11 + var viewports []_D3D11_VIEWPORT for _, id := range dstIDs { img := g.images[id] if img == nil { + dsts = append(dsts, nil) + viewports = append(viewports, _D3D11_VIEWPORT{}) continue } dsts = append(dsts, img) + w, h := img.internalSize() + viewports = append(viewports, _D3D11_VIEWPORT{ + TopLeftX: 0, + TopLeftY: 0, + Width: float32(w), + Height: float32(h), + MinDepth: 0, + MaxDepth: 1, + }) } var srcs [graphics.ShaderSrcImageCount]*image11 @@ -539,19 +614,9 @@ func (g *graphics11) DrawTriangles(dstIDs [graphics.ShaderDstImageCount]graphics srcs[i] = img } - w, h := dsts[0].internalSize() - g.deviceContext.RSSetViewports([]_D3D11_VIEWPORT{ - { - TopLeftX: 0, - TopLeftY: 0, - Width: float32(w), - Height: float32(h), - MinDepth: 0, - MaxDepth: 1, - }, - }) + g.deviceContext.RSSetViewports(viewports) - if err := setAsRenderTargets(dsts, fillRule != graphicsdriver.FillAll); err != nil { + if err := g.setAsRenderTargets(dsts, fillRule != graphicsdriver.FillAll); err != nil { return err } diff --git a/internal/graphicsdriver/directx/image11_windows.go b/internal/graphicsdriver/directx/image11_windows.go index b78f0b317..1b3adb150 100644 --- a/internal/graphicsdriver/directx/image11_windows.go +++ b/internal/graphicsdriver/directx/image11_windows.go @@ -204,69 +204,6 @@ func (i *image11) setAsRenderTarget(useStencil bool) error { return nil } -func setAsRenderTargets(dsts []*image11, useStencil bool) error { - for _, i := range dsts { - if i.renderTargetView == nil { - rtv, err := i.graphics.device.CreateRenderTargetView(unsafe.Pointer(i.texture), nil) - if err != nil { - return err - } - i.renderTargetView = rtv - } - - if !useStencil { - continue - } - - if i.screen { - return fmt.Errorf("directx: a stencil buffer is not available for a screen image") - } - if i.stencilView == nil { - sv, err := i.graphics.device.CreateDepthStencilView(unsafe.Pointer(i.stencil), nil) - if err != nil { - return err - } - i.stencilView = sv - } - if i.stencil == nil { - w, h := i.internalSize() - s, err := i.graphics.device.CreateTexture2D(&_D3D11_TEXTURE2D_DESC{ - Width: uint32(w), - Height: uint32(h), - MipLevels: 0, - ArraySize: 1, - Format: _DXGI_FORMAT_D24_UNORM_S8_UINT, - SampleDesc: _DXGI_SAMPLE_DESC{ - Count: 1, - Quality: 0, - }, - Usage: _D3D11_USAGE_DEFAULT, - BindFlags: uint32(_D3D11_BIND_DEPTH_STENCIL), - CPUAccessFlags: 0, - MiscFlags: 0, - }, nil) - if err != nil { - return err - } - i.stencil = s - } - } - - var rtvs []*_ID3D11RenderTargetView - var dsvs []*_ID3D11DepthStencilView - for _, i := range dsts { - rtvs = append(rtvs, i.renderTargetView) - dsvs = append(dsvs, i.stencilView) - } - - dsts[0].graphics.deviceContext.OMSetRenderTargets(rtvs, dsts[0].stencilView) - if useStencil { - dsts[0].graphics.deviceContext.ClearDepthStencilView(dsts[0].stencilView, uint8(_D3D11_CLEAR_STENCIL), 0, 0) - } - - return nil -} - func (i *image11) getShaderResourceView() (*_ID3D11ShaderResourceView, error) { if i.shaderResourceView == nil { srv, err := i.graphics.device.CreateShaderResourceView(unsafe.Pointer(i.texture), nil) From 65646df8edb692458f43439055a1654e4f28f432 Mon Sep 17 00:00:00 2001 From: Zyko <13394516+Zyko0@users.noreply.github.com> Date: Wed, 10 Apr 2024 20:12:10 +0200 Subject: [PATCH 14/36] Temporary fix for directx11 --- .../directx/graphics11_windows.go | 38 ++++++++++--------- internal/shaderir/glsl/glsl.go | 2 - internal/shaderir/hlsl/hlsl.go | 2 - 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/internal/graphicsdriver/directx/graphics11_windows.go b/internal/graphicsdriver/directx/graphics11_windows.go index 262de28e4..2a9344010 100644 --- a/internal/graphicsdriver/directx/graphics11_windows.go +++ b/internal/graphicsdriver/directx/graphics11_windows.go @@ -539,13 +539,6 @@ func (g *graphics11) setAsRenderTargets(dsts []*image11, useStencil bool) error if i.screen { return fmt.Errorf("directx: a stencil buffer is not available for a screen image") } - if i.stencilView == nil { - sv, err := g.device.CreateDepthStencilView(unsafe.Pointer(i.stencil), nil) - if err != nil { - return err - } - i.stencilView = sv - } if i.stencil == nil { w, h := i.internalSize() s, err := g.device.CreateTexture2D(&_D3D11_TEXTURE2D_DESC{ @@ -568,6 +561,13 @@ func (g *graphics11) setAsRenderTargets(dsts []*image11, useStencil bool) error } i.stencil = s } + if i.stencilView == nil { + sv, err := g.device.CreateDepthStencilView(unsafe.Pointer(i.stencil), nil) + if err != nil { + return err + } + i.stencilView = sv + } } g.deviceContext.OMSetRenderTargets(rtvs, dsts[0].stencilView) @@ -584,25 +584,25 @@ func (g *graphics11) DrawTriangles(dstIDs [graphics.ShaderDstImageCount]graphics srvs := [graphics.ShaderSrcImageCount]*_ID3D11ShaderResourceView{} g.deviceContext.PSSetShaderResources(0, srvs[:]) - var dsts []*image11 - var viewports []_D3D11_VIEWPORT - for _, id := range dstIDs { + var dsts [graphics.ShaderDstImageCount]*image11 + var viewports [graphics.ShaderDstImageCount]_D3D11_VIEWPORT + var targetCount int + for i, id := range dstIDs { img := g.images[id] if img == nil { - dsts = append(dsts, nil) - viewports = append(viewports, _D3D11_VIEWPORT{}) continue } - dsts = append(dsts, img) + dsts[i] = img w, h := img.internalSize() - viewports = append(viewports, _D3D11_VIEWPORT{ + viewports[i] = _D3D11_VIEWPORT{ TopLeftX: 0, TopLeftY: 0, Width: float32(w), Height: float32(h), MinDepth: 0, MaxDepth: 1, - }) + } + targetCount++ } var srcs [graphics.ShaderSrcImageCount]*image11 @@ -614,9 +614,13 @@ func (g *graphics11) DrawTriangles(dstIDs [graphics.ShaderDstImageCount]graphics srcs[i] = img } - g.deviceContext.RSSetViewports(viewports) + // TODO: this is not correct, we can't assume that a single image means no MRT! + if targetCount > 1 { + targetCount = graphics.ShaderDstImageCount + } + g.deviceContext.RSSetViewports(viewports[:targetCount]) - if err := g.setAsRenderTargets(dsts, fillRule != graphicsdriver.FillAll); err != nil { + if err := g.setAsRenderTargets(dsts[:targetCount], fillRule != graphicsdriver.FillAll); err != nil { return err } diff --git a/internal/shaderir/glsl/glsl.go b/internal/shaderir/glsl/glsl.go index 7cb61841c..830b9f982 100644 --- a/internal/shaderir/glsl/glsl.go +++ b/internal/shaderir/glsl/glsl.go @@ -296,8 +296,6 @@ func Compile(p *shaderir.Program, version GLSLVersion) (vertexShader, fragmentSh vs = strings.TrimSpace(vs) + "\n" fs = strings.TrimSpace(fs) + "\n" - fmt.Println("FS:", fs) - return vs, fs } diff --git a/internal/shaderir/hlsl/hlsl.go b/internal/shaderir/hlsl/hlsl.go index 85b820d26..9454024d5 100644 --- a/internal/shaderir/hlsl/hlsl.go +++ b/internal/shaderir/hlsl/hlsl.go @@ -236,8 +236,6 @@ func Compile(p *shaderir.Program) (vertexShader, pixelShader string, offsets []i vertexShader = shaders[0] pixelShader = shaders[1] - fmt.Println("PS:", pixelShader) - return } From 92a257a5574fbaa394b3876eaa7907b415d246fa Mon Sep 17 00:00:00 2001 From: Zyko <13394516+Zyko0@users.noreply.github.com> Date: Wed, 10 Apr 2024 20:17:20 +0200 Subject: [PATCH 15/36] directx: Better logic to assume MRT --- .../graphicsdriver/directx/graphics11_windows.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/internal/graphicsdriver/directx/graphics11_windows.go b/internal/graphicsdriver/directx/graphics11_windows.go index 2a9344010..9c07c6405 100644 --- a/internal/graphicsdriver/directx/graphics11_windows.go +++ b/internal/graphicsdriver/directx/graphics11_windows.go @@ -587,11 +587,15 @@ func (g *graphics11) DrawTriangles(dstIDs [graphics.ShaderDstImageCount]graphics var dsts [graphics.ShaderDstImageCount]*image11 var viewports [graphics.ShaderDstImageCount]_D3D11_VIEWPORT var targetCount int + firstTarget := -1 for i, id := range dstIDs { img := g.images[id] if img == nil { continue } + if firstTarget == -1 { + firstTarget = i + } dsts[i] = img w, h := img.internalSize() viewports[i] = _D3D11_VIEWPORT{ @@ -614,8 +618,12 @@ func (g *graphics11) DrawTriangles(dstIDs [graphics.ShaderDstImageCount]graphics srcs[i] = img } - // TODO: this is not correct, we can't assume that a single image means no MRT! - if targetCount > 1 { + // If the number of targets is more than one, or if the only target is the first one, then + // it is safe to assume that MRT is used. + // Also, it only matters in order to specify empty targets/viewports when not all slots are + // being filled. + usesMRT := targetCount > 1 || firstTarget > 0 + if usesMRT { targetCount = graphics.ShaderDstImageCount } g.deviceContext.RSSetViewports(viewports[:targetCount]) From 6a8c00e0aa5aeaf57de9d19f5e391072d650b95e Mon Sep 17 00:00:00 2001 From: Zyko <13394516+Zyko0@users.noreply.github.com> Date: Wed, 10 Apr 2024 21:08:03 +0200 Subject: [PATCH 16/36] Fixed opengl tests --- examples/mrt/main.go | 2 +- .../directx/graphics11_windows.go | 3 +- internal/graphicsdriver/opengl/graphics.go | 45 +++++++++---------- 3 files changed, 23 insertions(+), 27 deletions(-) diff --git a/examples/mrt/main.go b/examples/mrt/main.go index 3435bcd71..15c34d949 100644 --- a/examples/mrt/main.go +++ b/examples/mrt/main.go @@ -127,7 +127,7 @@ func main() { ebiten.SetVsyncEnabled(false) ebiten.SetWindowTitle("MRT (Ebitengine Demo)") if err := ebiten.RunGameWithOptions(&Game{}, &ebiten.RunGameOptions{ - GraphicsLibrary: ebiten.GraphicsLibraryDirectX, + GraphicsLibrary: ebiten.GraphicsLibraryOpenGL, }); err != nil { log.Fatal(err) } diff --git a/internal/graphicsdriver/directx/graphics11_windows.go b/internal/graphicsdriver/directx/graphics11_windows.go index 9c07c6405..e5c5ea292 100644 --- a/internal/graphicsdriver/directx/graphics11_windows.go +++ b/internal/graphicsdriver/directx/graphics11_windows.go @@ -622,8 +622,7 @@ func (g *graphics11) DrawTriangles(dstIDs [graphics.ShaderDstImageCount]graphics // it is safe to assume that MRT is used. // Also, it only matters in order to specify empty targets/viewports when not all slots are // being filled. - usesMRT := targetCount > 1 || firstTarget > 0 - if usesMRT { + if targetCount > 1 || firstTarget > 0 { targetCount = graphics.ShaderDstImageCount } g.deviceContext.RSSetViewports(viewports[:targetCount]) diff --git a/internal/graphicsdriver/opengl/graphics.go b/internal/graphicsdriver/opengl/graphics.go index b7c560178..a0b10fa91 100644 --- a/internal/graphicsdriver/opengl/graphics.go +++ b/internal/graphicsdriver/opengl/graphics.go @@ -204,8 +204,8 @@ func (g *Graphics) DrawTriangles(dstIDs [graphics.ShaderDstImageCount]graphicsdr } g.drawCalled = true - g.context.ctx.BindTexture(gl.TEXTURE_2D, 0) - dstCount := 0 + targetCount := 0 + firstTarget := -1 var dsts [graphics.ShaderDstImageCount]*Image for i, dstID := range dstIDs { if dstID == graphicsdriver.InvalidImageID { @@ -215,30 +215,32 @@ func (g *Graphics) DrawTriangles(dstIDs [graphics.ShaderDstImageCount]graphicsdr if dst == nil { continue } + if firstTarget == -1 { + firstTarget = i + } dst.ensureFramebuffer() dsts[i] = dst - dstCount++ + targetCount++ } - if dstCount == 0 { - return nil - } - g.context.bindFramebuffer(0) - // Only necessary for the same shared framebuffer - f := uint32(dsts[0].framebuffer.native) - if dstCount > 1 { - if g.context.mrtFramebuffer == 0 { + f := uint32(dsts[firstTarget].framebuffer.native) + // If the number of targets is more than one, or if the only target is the first one, then + // it is safe to assume that MRT is used. + // Also, it only matters in order to specify empty targets/viewports when not all slots are + // being filled. + usesMRT := firstTarget > 0 || targetCount > 1 + if usesMRT { + f = uint32(g.context.mrtFramebuffer) + // Create the initial MRT framebuffer + if f == 0 { f = g.context.ctx.CreateFramebuffer() if f <= 0 { return fmt.Errorf("opengl: creating framebuffer failed: the returned value is not positive but %d", f) } g.context.mrtFramebuffer = framebufferNative(f) - } else { - f = uint32(g.context.mrtFramebuffer) } g.context.bindFramebuffer(framebufferNative(f)) - //g.context.ctx.BindFramebuffer(gl.FRAMEBUFFER, f) // Reset color attachments if s := g.context.ctx.CheckFramebufferStatus(gl.FRAMEBUFFER); s == gl.FRAMEBUFFER_COMPLETE { @@ -273,8 +275,8 @@ func (g *Graphics) DrawTriangles(dstIDs [graphics.ShaderDstImageCount]graphicsdr g.context.bindFramebuffer(framebufferNative(f)) } - w, h := dsts[0].framebuffer.viewportWidth, dsts[0].framebuffer.viewportHeight - g.context.setViewport(w, h, dsts[0].screen) + w, h := dsts[firstTarget].viewportSize() //.framebuffer.viewportWidth, dsts[firstTarget].framebuffer.viewportHeight + g.context.setViewport(w, h, dsts[firstTarget].screen) g.context.blend(blend) @@ -298,7 +300,7 @@ func (g *Graphics) DrawTriangles(dstIDs [graphics.ShaderDstImageCount]graphicsdr } // In OpenGL, the NDC's Y direction is upward, so flip the Y direction for the final framebuffer. - if dstCount == 1 && dsts[0] != nil && dsts[0].screen { + if !usesMRT && dsts[firstTarget].screen { const idx = graphics.ProjectionMatrixUniformVariableIndex // Invert the sign bits as float32 values. g.uniformVars[idx].value[1] ^= 1 << 31 @@ -326,13 +328,8 @@ func (g *Graphics) DrawTriangles(dstIDs [graphics.ShaderDstImageCount]graphicsdr g.uniformVars = g.uniformVars[:0] if fillRule != graphicsdriver.FillAll { - for _, dst := range dsts { - if dst == nil { - continue - } - if err := dst.ensureStencilBuffer(framebufferNative(f)); err != nil { - return err - } + if err := dsts[firstTarget].ensureStencilBuffer(framebufferNative(f)); err != nil { + return err } g.context.ctx.Enable(gl.STENCIL_TEST) } From ced0c62827b8526ac10182a033149bc517060708 Mon Sep 17 00:00:00 2001 From: Zyko <13394516+Zyko0@users.noreply.github.com> Date: Wed, 10 Apr 2024 21:11:32 +0200 Subject: [PATCH 17/36] go vet error check --- internal/graphicsdriver/opengl/graphics.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/internal/graphicsdriver/opengl/graphics.go b/internal/graphicsdriver/opengl/graphics.go index a0b10fa91..5881cd7f6 100644 --- a/internal/graphicsdriver/opengl/graphics.go +++ b/internal/graphicsdriver/opengl/graphics.go @@ -218,7 +218,9 @@ func (g *Graphics) DrawTriangles(dstIDs [graphics.ShaderDstImageCount]graphicsdr if firstTarget == -1 { firstTarget = i } - dst.ensureFramebuffer() + if err := dst.ensureFramebuffer(); err != nil { + return err + } dsts[i] = dst targetCount++ } From 8c0bc0a2e084bd1bdd961a6d4ac0f4ac840082a8 Mon Sep 17 00:00:00 2001 From: Zyko <13394516+Zyko0@users.noreply.github.com> Date: Wed, 10 Apr 2024 21:55:00 +0200 Subject: [PATCH 18/36] Implement the correct DrawTriangles definition on metal --- internal/graphicsdriver/metal/graphics_darwin.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/graphicsdriver/metal/graphics_darwin.go b/internal/graphicsdriver/metal/graphics_darwin.go index 2d7c2948a..3cc8a6a5a 100644 --- a/internal/graphicsdriver/metal/graphics_darwin.go +++ b/internal/graphicsdriver/metal/graphics_darwin.go @@ -467,7 +467,7 @@ func (g *Graphics) flushRenderCommandEncoderIfNeeded() { g.lastDst = nil } -func (g *Graphics) draw(dst *Image, dstRegions []graphicsdriver.DstRegion, srcs [graphics.ShaderImageCount]*Image, indexOffset int, shader *Shader, uniforms [][]uint32, blend graphicsdriver.Blend, fillRule graphicsdriver.FillRule) error { +func (g *Graphics) draw(dst *Image, dstRegions []graphicsdriver.DstRegion, srcs [graphics.ShaderSrcImageCount]*Image, indexOffset int, shader *Shader, uniforms [][]uint32, blend graphicsdriver.Blend, fillRule graphicsdriver.FillRule) error { // When preparing a stencil buffer, flush the current render command encoder // to make sure the stencil buffer is cleared when loading. // TODO: What about clearing the stencil buffer by vertices? @@ -605,18 +605,18 @@ func (g *Graphics) draw(dst *Image, dstRegions []graphicsdriver.DstRegion, srcs return nil } -func (g *Graphics) DrawTriangles(dstID graphicsdriver.ImageID, srcIDs [graphics.ShaderImageCount]graphicsdriver.ImageID, shaderID graphicsdriver.ShaderID, dstRegions []graphicsdriver.DstRegion, indexOffset int, blend graphicsdriver.Blend, uniforms []uint32, fillRule graphicsdriver.FillRule) error { +func (g *Graphics) DrawTriangles(dstIDs [graphics.ShaderDstImageCount]graphicsdriver.ImageID, srcIDs [graphics.ShaderSrcImageCount]graphicsdriver.ImageID, shaderID graphicsdriver.ShaderID, dstRegions []graphicsdriver.DstRegion, indexOffset int, blend graphicsdriver.Blend, uniforms []uint32, fillRule graphicsdriver.FillRule) error { if shaderID == graphicsdriver.InvalidShaderID { return fmt.Errorf("metal: shader ID is invalid") } - dst := g.images[dstID] + dst := g.images[dstIDs[0]] if dst.screen { g.view.update() } - var srcs [graphics.ShaderImageCount]*Image + var srcs [graphics.ShaderSrcImageCount]*Image for i, srcID := range srcIDs { srcs[i] = g.images[srcID] } From 15b5c888ca2752705f83bc3ebfa81698e3fee2a2 Mon Sep 17 00:00:00 2001 From: Zyko <13394516+Zyko0@users.noreply.github.com> Date: Wed, 10 Apr 2024 22:00:37 +0200 Subject: [PATCH 19/36] Fixed DrawTriangles for ps5 --- internal/graphicsdriver/playstation5/graphics_playstation5.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/graphicsdriver/playstation5/graphics_playstation5.go b/internal/graphicsdriver/playstation5/graphics_playstation5.go index a9bc03279..d5d59308c 100644 --- a/internal/graphicsdriver/playstation5/graphics_playstation5.go +++ b/internal/graphicsdriver/playstation5/graphics_playstation5.go @@ -116,7 +116,7 @@ func (g *Graphics) NewShader(program *shaderir.Program) (graphicsdriver.Shader, }, nil } -func (g *Graphics) DrawTriangles(dst graphicsdriver.ImageID, srcs [graphics.ShaderImageCount]graphicsdriver.ImageID, shader graphicsdriver.ShaderID, dstRegions []graphicsdriver.DstRegion, indexOffset int, blend graphicsdriver.Blend, uniforms []uint32, fillRule graphicsdriver.FillRule) error { +func (g *Graphics) DrawTriangles(dsts [graphics.ShaderDstImageCount]graphicsdriver.ImageID, srcs [graphics.ShaderImageCount]graphicsdriver.ImageID, shader graphicsdriver.ShaderID, dstRegions []graphicsdriver.DstRegion, indexOffset int, blend graphicsdriver.Blend, uniforms []uint32, fillRule graphicsdriver.FillRule) error { return nil } From 15ccaf1998d06c6eaf00e53c70ba16f9d8d00ec8 Mon Sep 17 00:00:00 2001 From: Zyko <13394516+Zyko0@users.noreply.github.com> Date: Wed, 10 Apr 2024 22:06:56 +0200 Subject: [PATCH 20/36] Fixed ps5 src argument constant name --- internal/graphicsdriver/playstation5/graphics_playstation5.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/graphicsdriver/playstation5/graphics_playstation5.go b/internal/graphicsdriver/playstation5/graphics_playstation5.go index d5d59308c..f787bc981 100644 --- a/internal/graphicsdriver/playstation5/graphics_playstation5.go +++ b/internal/graphicsdriver/playstation5/graphics_playstation5.go @@ -116,7 +116,7 @@ func (g *Graphics) NewShader(program *shaderir.Program) (graphicsdriver.Shader, }, nil } -func (g *Graphics) DrawTriangles(dsts [graphics.ShaderDstImageCount]graphicsdriver.ImageID, srcs [graphics.ShaderImageCount]graphicsdriver.ImageID, shader graphicsdriver.ShaderID, dstRegions []graphicsdriver.DstRegion, indexOffset int, blend graphicsdriver.Blend, uniforms []uint32, fillRule graphicsdriver.FillRule) error { +func (g *Graphics) DrawTriangles(dsts [graphics.ShaderDstImageCount]graphicsdriver.ImageID, srcs [graphics.ShaderSrcImageCount]graphicsdriver.ImageID, shader graphicsdriver.ShaderID, dstRegions []graphicsdriver.DstRegion, indexOffset int, blend graphicsdriver.Blend, uniforms []uint32, fillRule graphicsdriver.FillRule) error { return nil } From 042fdab3d07f29807e340435d4bb0d72b9e63b8e Mon Sep 17 00:00:00 2001 From: Zyko <13394516+Zyko0@users.noreply.github.com> Date: Fri, 12 Apr 2024 00:09:36 +0200 Subject: [PATCH 21/36] Fixed shader testdata (except msl) --- internal/shader/testdata/for5.expected.fs | 35 ++++++++++--------- .../shader/testdata/issue1238.expected.fs | 12 ++++--- .../shader/testdata/issue1245.expected.fs | 15 ++++---- .../shader/testdata/issue1701.expected.fs | 9 ++--- .../testdata/vertex_fragment.expected.fs | 9 ++--- 5 files changed, 43 insertions(+), 37 deletions(-) diff --git a/internal/shader/testdata/for5.expected.fs b/internal/shader/testdata/for5.expected.fs index 954d264a4..f609f4939 100644 --- a/internal/shader/testdata/for5.expected.fs +++ b/internal/shader/testdata/for5.expected.fs @@ -3,31 +3,32 @@ uniform float U1; uniform float U2; int F0(in int l0); -vec4 F1(in vec4 l0); +void F1(in vec4 l0, out vec4 l1); int F0(in int l0) { return l0; } -vec4 F1(in vec4 l0) { - int l1 = 0; - int l3 = 0; - l1 = 0; - for (int l2 = 0; l2 < 10; l2++) { - int l3 = 0; - l3 = F0(l2); - l1 = (l1) + (l3); - for (int l4 = 0; l4 < 10; l4++) { - int l5 = 0; - l5 = F0(l4); - l1 = (l1) + (l5); +void F1(in vec4 l0, out vec4 l1) { + int l2 = 0; + int l4 = 0; + l2 = 0; + for (int l3 = 0; l3 < 10; l3++) { + int l4 = 0; + l4 = F0(l3); + l2 = (l2) + (l4); + for (int l5 = 0; l5 < 10; l5++) { + int l6 = 0; + l6 = F0(l5); + l2 = (l2) + (l6); } } - l3 = 0; - l1 = (l1) + (l3); - return vec4(float(l1)); + l4 = 0; + l2 = (l2) + (l4); + l1 = vec4(float(l2)); + return; } void main(void) { - fragColor = F1(gl_FragCoord); + F1(gl_FragCoord, gl_FragData[0]); } diff --git a/internal/shader/testdata/issue1238.expected.fs b/internal/shader/testdata/issue1238.expected.fs index af6a3c49f..6c3e28e4e 100644 --- a/internal/shader/testdata/issue1238.expected.fs +++ b/internal/shader/testdata/issue1238.expected.fs @@ -1,12 +1,14 @@ -vec4 F0(in vec4 l0); +void F0(in vec4 l0, out vec4 l1); -vec4 F0(in vec4 l0) { +void F0(in vec4 l0, out vec4 l1) { if (true) { - return l0; + l1 = l0; + return; } - return l0; + l1 = l0; + return; } void main(void) { - fragColor = F0(gl_FragCoord); + F0(gl_FragCoord, gl_FragData[0]); } diff --git a/internal/shader/testdata/issue1245.expected.fs b/internal/shader/testdata/issue1245.expected.fs index a964a0a03..18a354cb5 100644 --- a/internal/shader/testdata/issue1245.expected.fs +++ b/internal/shader/testdata/issue1245.expected.fs @@ -1,13 +1,14 @@ -vec4 F0(in vec4 l0); +void F0(in vec4 l0, out vec4 l1); -vec4 F0(in vec4 l0) { - vec4 l1 = vec4(0); - for (float l2 = 0.0; l2 < 4.0; l2++) { - (l1).x = ((l1).x) + ((l2) * (1.0000000000e-02)); +void F0(in vec4 l0, out vec4 l1) { + vec4 l2 = vec4(0); + for (float l3 = 0.0; l3 < 4.0; l3++) { + (l2).x = ((l2).x) + ((l3) * (1.0000000000e-02)); } - return l1; + l1 = l2; + return; } void main(void) { - fragColor = F0(gl_FragCoord); + F0(gl_FragCoord, gl_FragData[0]); } diff --git a/internal/shader/testdata/issue1701.expected.fs b/internal/shader/testdata/issue1701.expected.fs index c25af6e4e..1a9b3376d 100644 --- a/internal/shader/testdata/issue1701.expected.fs +++ b/internal/shader/testdata/issue1701.expected.fs @@ -1,6 +1,6 @@ void F2(void); void F3(void); -vec4 F5(in vec4 l0); +void F5(in vec4 l0, out vec4 l1); void F2(void) { } @@ -9,11 +9,12 @@ void F3(void) { F2(); } -vec4 F5(in vec4 l0) { +void F5(in vec4 l0, out vec4 l1) { F3(); - return vec4(0.0); + l1 = vec4(0.0); + return; } void main(void) { - fragColor = F5(gl_FragCoord); + F5(gl_FragCoord, gl_FragData[0]); } diff --git a/internal/shader/testdata/vertex_fragment.expected.fs b/internal/shader/testdata/vertex_fragment.expected.fs index a95f49229..f28f8c9b8 100644 --- a/internal/shader/testdata/vertex_fragment.expected.fs +++ b/internal/shader/testdata/vertex_fragment.expected.fs @@ -2,12 +2,13 @@ uniform vec2 U0; in vec2 V0; in vec4 V1; -vec4 F0(in vec4 l0, in vec2 l1, in vec4 l2); +void F0(in vec4 l0, in vec2 l1, in vec4 l2, out vec4 l3); -vec4 F0(in vec4 l0, in vec2 l1, in vec4 l2) { - return vec4((l0).x, (l1).y, (l2).z, 1.0); +void F0(in vec4 l0, in vec2 l1, in vec4 l2, out vec4 l3) { + l3 = vec4((l0).x, (l1).y, (l2).z, 1.0); + return; } void main(void) { - fragColor = F0(gl_FragCoord, V0, V1); + F0(gl_FragCoord, V0, V1, gl_FragData[0]); } From b2b88f4bdd9551c3b1b3c0aec05a870a42739fe4 Mon Sep 17 00:00:00 2001 From: Zyko <13394516+Zyko0@users.noreply.github.com> Date: Fri, 12 Apr 2024 00:17:12 +0200 Subject: [PATCH 22/36] Disable metal shader compilation tests tmp --- internal/shader/shader_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/shader/shader_test.go b/internal/shader/shader_test.go index fd4abaee1..6a855e332 100644 --- a/internal/shader/shader_test.go +++ b/internal/shader/shader_test.go @@ -194,7 +194,7 @@ func TestCompile(t *testing.T) { } } - if tc.Metal != nil { + /*if tc.Metal != nil { m := msl.Compile(s) if got, want := metalNormalize(m), metalNormalize(string(tc.Metal)); got != want { compare(t, "Metal", got, want) @@ -203,7 +203,7 @@ func TestCompile(t *testing.T) { // Just check that Compile doesn't cause panic. // TODO: Should the results be tested? - msl.Compile(s) + msl.Compile(s)*/ }) } } From f021b5ded8331f75359ac2f89106fe004bd898b7 Mon Sep 17 00:00:00 2001 From: Zyko <13394516+Zyko0@users.noreply.github.com> Date: Fri, 12 Apr 2024 18:34:29 +0200 Subject: [PATCH 23/36] Fixed IR tests + skipping metal for now --- internal/shaderir/ir_test.go | 68 ++++++++++++++++++++---------------- 1 file changed, 37 insertions(+), 31 deletions(-) diff --git a/internal/shaderir/ir_test.go b/internal/shaderir/ir_test.go index f9cf78596..c0e42b9f6 100644 --- a/internal/shaderir/ir_test.go +++ b/internal/shaderir/ir_test.go @@ -938,12 +938,12 @@ void F0(float l0, float l1, thread float& l2) { }, Attributes: []shaderir.Type{ {Main: shaderir.Vec4}, - {Main: shaderir.Float}, {Main: shaderir.Vec2}, + {Main: shaderir.Vec4}, }, Varyings: []shaderir.Type{ - {Main: shaderir.Float}, {Main: shaderir.Vec2}, + {Main: shaderir.Vec4}, }, VertexFunc: shaderir.VertexFunc{ Block: block( @@ -967,10 +967,10 @@ void F0(float l0, float l1, thread float& l2) { GlslVS: glslVertexPrelude + ` uniform float U0; in vec4 A0; -in float A1; -in vec2 A2; -out float V0; -out vec2 V1; +in vec2 A1; +in vec4 A2; +out vec2 V0; +out vec4 V1; void main(void) { gl_Position = A0; @@ -979,8 +979,8 @@ void main(void) { }`, GlslFS: glslFragmentPrelude + ` uniform float U0; -in float V0; -in vec2 V1;`, +in vec2 V0; +in vec4 V1;`, }, { Name: "FragmentFunc", @@ -991,12 +991,12 @@ in vec2 V1;`, }, Attributes: []shaderir.Type{ {Main: shaderir.Vec4}, - {Main: shaderir.Float}, {Main: shaderir.Vec2}, + {Main: shaderir.Vec4}, }, Varyings: []shaderir.Type{ - {Main: shaderir.Float}, {Main: shaderir.Vec2}, + {Main: shaderir.Vec4}, }, VertexFunc: shaderir.VertexFunc{ Block: block( @@ -1016,34 +1016,39 @@ in vec2 V1;`, ), ), }, + ColorsOutCount: 1, FragmentFunc: shaderir.FragmentFunc{ Block: block( []shaderir.Type{ - {Main: shaderir.Float}, {Main: shaderir.Vec2}, + {Main: shaderir.Vec4}, }, - 3, - assignStmt( - localVariableExpr(3), - localVariableExpr(0), - ), + 3+1, assignStmt( localVariableExpr(4), localVariableExpr(1), ), - returnStmt( + assignStmt( + localVariableExpr(5), localVariableExpr(2), ), + assignStmt( + localVariableExpr(3), + localVariableExpr(0), + ), + shaderir.Stmt{ + Type: shaderir.Return, + }, ), }, }, GlslVS: glslVertexPrelude + ` uniform float U0; in vec4 A0; -in float A1; -in vec2 A2; -out float V0; -out vec2 V1; +in vec2 A1; +in vec4 A2; +out vec2 V0; +out vec4 V1; void main(void) { gl_Position = A0; @@ -1052,21 +1057,22 @@ void main(void) { }`, GlslFS: glslFragmentPrelude + ` uniform float U0; -in float V0; -in vec2 V1; +in vec2 V0; +in vec4 V1; -vec4 F0(in vec4 l0, in float l1, in vec2 l2); +void F0(in vec4 l0, in vec2 l1, in vec4 l2, out vec4 l3); -vec4 F0(in vec4 l0, in float l1, in vec2 l2) { - float l3 = float(0); +void F0(in vec4 l0, in vec2 l1, in vec4 l2, out vec4 l3) { vec2 l4 = vec2(0); - l3 = l0; + vec4 l5 = vec4(0); l4 = l1; - return l2; + l5 = l2; + l3 = l0; + return; } void main(void) { - fragColor = F0(gl_FragCoord, V0, V1); + F0(gl_FragCoord, V0, V1, gl_FragData[0]); }`, }, } @@ -1088,14 +1094,14 @@ void main(void) { t.Errorf("%s fragment: got: %s, want: %s", tc.Name, got, want) } } - m := msl.Compile(&tc.Program) + /*m := msl.Compile(&tc.Program) if tc.Metal != "" { got := m want := tc.Metal + "\n" if got != want { t.Errorf("%s metal: got: %s, want: %s", tc.Name, got, want) } - } + }*/ }) } } From 2faf8a551d29c4c6714a4b89714511d0ba940a70 Mon Sep 17 00:00:00 2001 From: Zyko <13394516+Zyko0@users.noreply.github.com> Date: Fri, 12 Apr 2024 19:19:17 +0200 Subject: [PATCH 24/36] Set max dst images to 8 + some wording --- examples/mrt/main.go | 2 +- internal/atlas/image.go | 2 +- internal/buffered/image.go | 2 +- internal/graphics/vertex.go | 6 +++++- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/examples/mrt/main.go b/examples/mrt/main.go index 15c34d949..10e3e53a9 100644 --- a/examples/mrt/main.go +++ b/examples/mrt/main.go @@ -31,7 +31,7 @@ const ( ) var ( - dsts = [4]*ebiten.Image{ + dsts = [8]*ebiten.Image{ /*ebiten.NewImage(dstSize, dstSize), ebiten.NewImage(dstSize, dstSize), ebiten.NewImage(dstSize, dstSize), diff --git a/internal/atlas/image.go b/internal/atlas/image.go index 66e140fb6..d7915d577 100644 --- a/internal/atlas/image.go +++ b/internal/atlas/image.go @@ -590,7 +590,7 @@ func drawTrianglesMRT(dsts [graphics.ShaderDstImageCount]*Image, srcs [graphics. // i and a source image might share the same atlas even though i != src. for _, dst := range dsts { if src != nil && dst != nil && dst.backend.image == src.backend.image { - panic("atlas: Image.DrawTriangles: source must be different from the receiver") + panic("atlas: DrawTrianglesMRT: source must be different from the destination images") } } } diff --git a/internal/buffered/image.go b/internal/buffered/image.go index 185bee7bc..955e0fd51 100644 --- a/internal/buffered/image.go +++ b/internal/buffered/image.go @@ -218,7 +218,7 @@ func DrawTrianglesMRT(dsts [graphics.ShaderDstImageCount]*Image, srcs [graphics. continue } if dst == src { - panic("buffered: DrawTrianglesMRT: source images must be different from the destination") + panic("buffered: DrawTrianglesMRT: source images must be different from the destination images") } } if src != nil { diff --git a/internal/graphics/vertex.go b/internal/graphics/vertex.go index 02ba2e9fc..585e967aa 100644 --- a/internal/graphics/vertex.go +++ b/internal/graphics/vertex.go @@ -16,7 +16,11 @@ package graphics const ( ShaderSrcImageCount = 4 - ShaderDstImageCount = 4 + // The minimum guaranteed value for the number of target seems to be 8 + // OpenGL(8): https://www.khronos.org/opengl/wiki/Framebuffer_Object#Framebuffer_Object_Structure + // DirectX11(8): https://learn.microsoft.com/en-us/windows/win32/direct3d11/d3d10-graphics-programming-guide-output-merger-stage#multiple-rendertargets-overview + // Metal(8): Page 7 of 15: https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf + ShaderDstImageCount = 8 // PreservedUniformVariablesCount represents the number of preserved uniform variables. // Any shaders in Ebitengine must have these uniform variables. From 7dd4aa91508dbda4e82b7f3aada32fc0d59c0552 Mon Sep 17 00:00:00 2001 From: Zyko <13394516+Zyko0@users.noreply.github.com> Date: Sat, 13 Apr 2024 01:11:48 +0200 Subject: [PATCH 25/36] Directx 12 mrt working - 1st iteration --- examples/mrt/main.go | 2 +- .../directx/graphics11_windows.go | 1 + .../directx/graphics12_windows.go | 146 +++++++++++++++--- .../directx/pipeline12_windows.go | 89 ++++++++++- 4 files changed, 211 insertions(+), 27 deletions(-) diff --git a/examples/mrt/main.go b/examples/mrt/main.go index 10e3e53a9..11c7d1b3e 100644 --- a/examples/mrt/main.go +++ b/examples/mrt/main.go @@ -127,7 +127,7 @@ func main() { ebiten.SetVsyncEnabled(false) ebiten.SetWindowTitle("MRT (Ebitengine Demo)") if err := ebiten.RunGameWithOptions(&Game{}, &ebiten.RunGameOptions{ - GraphicsLibrary: ebiten.GraphicsLibraryOpenGL, + GraphicsLibrary: ebiten.GraphicsLibraryDirectX, }); err != nil { log.Fatal(err) } diff --git a/internal/graphicsdriver/directx/graphics11_windows.go b/internal/graphicsdriver/directx/graphics11_windows.go index e5c5ea292..2b90cdc32 100644 --- a/internal/graphicsdriver/directx/graphics11_windows.go +++ b/internal/graphicsdriver/directx/graphics11_windows.go @@ -625,6 +625,7 @@ func (g *graphics11) DrawTriangles(dstIDs [graphics.ShaderDstImageCount]graphics if targetCount > 1 || firstTarget > 0 { targetCount = graphics.ShaderDstImageCount } + g.deviceContext.RSSetViewports(viewports[:targetCount]) if err := g.setAsRenderTargets(dsts[:targetCount], fillRule != graphicsdriver.FillAll); err != nil { diff --git a/internal/graphicsdriver/directx/graphics12_windows.go b/internal/graphicsdriver/directx/graphics12_windows.go index 1827bce44..250c5b410 100644 --- a/internal/graphicsdriver/directx/graphics12_windows.go +++ b/internal/graphicsdriver/directx/graphics12_windows.go @@ -1082,7 +1082,78 @@ func (g *graphics12) NewShader(program *shaderir.Program) (graphicsdriver.Shader return s, nil } -func (g *graphics12) DrawTriangles(dstIDs [graphics.ShaderDstImageCount]graphicsdriver.ImageID, srcs [graphics.ShaderSrcImageCount]graphicsdriver.ImageID, shaderID graphicsdriver.ShaderID, dstRegions []graphicsdriver.DstRegion, indexOffset int, blend graphicsdriver.Blend, uniforms []uint32, fillRule graphicsdriver.FillRule) error { +func (g *graphics12) setAsRenderTargets(dsts []*image12, useStencil bool) error { + var rtvs []_D3D12_CPU_DESCRIPTOR_HANDLE + var dsvPtr *_D3D12_CPU_DESCRIPTOR_HANDLE + + for i, img := range dsts { + // Ignore a nil image in case of MRT + if img == nil { + _ = i + rtvBase, err := g.rtvDescriptorHeap.GetCPUDescriptorHandleForHeapStart() + if err != nil { + return err + } + rtv := rtvBase + rtv.Offset(int32(g.frameIndex), g.rtvDescriptorSize) + rtvs = append(rtvs, rtv) + continue + } + + if err := img.ensureRenderTargetView(g.device); err != nil { + return err + } + + if img.screen { + if useStencil { + return fmt.Errorf("directx: stencils are not available on the screen framebuffer") + } + + rtvBase, err := g.rtvDescriptorHeap.GetCPUDescriptorHandleForHeapStart() + if err != nil { + return err + } + rtv := rtvBase + rtv.Offset(int32(g.frameIndex), g.rtvDescriptorSize) + rtvs = append(rtvs, rtv) + continue + } + + rtvBase, err := img.rtvDescriptorHeap.GetCPUDescriptorHandleForHeapStart() + if err != nil { + return err + } + + rtv := rtvBase + rtvs = append(rtvs, rtv) + + if !useStencil || dsvPtr != nil { + continue + } + + if err := img.ensureDepthStencilView(g.device); err != nil { + return err + } + dsv, err := img.dsvDescriptorHeap.GetCPUDescriptorHandleForHeapStart() + if err != nil { + return err + } + dsvPtr = &dsv + } + + if !useStencil { + g.drawCommandList.OMSetRenderTargets(rtvs, false, nil) + return nil + } + + g.drawCommandList.OMSetStencilRef(0) + g.drawCommandList.OMSetRenderTargets(rtvs, false, dsvPtr) + g.drawCommandList.ClearDepthStencilView(*dsvPtr, _D3D12_CLEAR_FLAG_STENCIL, 0, 0, nil) + + return nil +} + +func (g *graphics12) DrawTriangles(dstIDs [graphics.ShaderDstImageCount]graphicsdriver.ImageID, srcIDs [graphics.ShaderSrcImageCount]graphicsdriver.ImageID, shaderID graphicsdriver.ShaderID, dstRegions []graphicsdriver.DstRegion, indexOffset int, blend graphicsdriver.Blend, uniforms []uint32, fillRule graphicsdriver.FillRule) error { if shaderID == graphicsdriver.InvalidShaderID { return fmt.Errorf("directx: shader ID is invalid") } @@ -1103,20 +1174,46 @@ func (g *graphics12) DrawTriangles(dstIDs [graphics.ShaderDstImageCount]graphics g.pipelineStates.releaseConstantBuffers(g.frameIndex) } - dst := g.images[dstIDs[0]] // TODO: handle array var resourceBarriers []_D3D12_RESOURCE_BARRIER_Transition - if rb, ok := dst.transiteState(_D3D12_RESOURCE_STATE_RENDER_TARGET); ok { - resourceBarriers = append(resourceBarriers, rb) - } - var srcImages [graphics.ShaderSrcImageCount]*image12 - for i, srcID := range srcs { - src := g.images[srcID] - if src == nil { + var dsts [graphics.ShaderDstImageCount]*image12 + var viewports [graphics.ShaderDstImageCount]_D3D12_VIEWPORT + var targetCount int + firstTarget := -1 + for i, id := range dstIDs { + img := g.images[id] + if img == nil { continue } - srcImages[i] = src - if rb, ok := src.transiteState(_D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE); ok { + if firstTarget == -1 { + firstTarget = i + } + dsts[i] = img + w, h := img.internalSize() + viewports[i] = _D3D12_VIEWPORT{ + TopLeftX: 0, + TopLeftY: 0, + Width: float32(w), + Height: float32(h), + MinDepth: _D3D12_MIN_DEPTH, + MaxDepth: _D3D12_MAX_DEPTH, + } + + if rb, ok := img.transiteState(_D3D12_RESOURCE_STATE_RENDER_TARGET); ok { + resourceBarriers = append(resourceBarriers, rb) + } + + targetCount++ + } + + var srcs [graphics.ShaderSrcImageCount]*image12 + for i, srcID := range srcIDs { + img := g.images[srcID] + if img == nil { + continue + } + srcs[i] = img + if rb, ok := img.transiteState(_D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE); ok { resourceBarriers = append(resourceBarriers, rb) } } @@ -1125,25 +1222,26 @@ func (g *graphics12) DrawTriangles(dstIDs [graphics.ShaderDstImageCount]graphics g.drawCommandList.ResourceBarrier(resourceBarriers) } - if err := dst.setAsRenderTarget(g.drawCommandList, g.device, fillRule != graphicsdriver.FillAll); err != nil { + // If the number of targets is more than one, or if the only target is the first one, then + // it is safe to assume that MRT is used. + // Also, it only matters in order to specify empty targets/viewports when not all slots are + // being filled. + usesMRT := targetCount > 1 || firstTarget > 0 + if usesMRT { + targetCount = graphics.ShaderDstImageCount + } + + g.drawCommandList.RSSetViewports(viewports[:targetCount]) + + if err := g.setAsRenderTargets(dsts[:targetCount], fillRule != graphicsdriver.FillAll); err != nil { return err } shader := g.shaders[shaderID] adjustedUniforms := adjustUniforms(shader.uniformTypes, shader.uniformOffsets, uniforms) - w, h := dst.internalSize() g.needFlushDrawCommandList = true - g.drawCommandList.RSSetViewports([]_D3D12_VIEWPORT{ - { - TopLeftX: 0, - TopLeftY: 0, - Width: float32(w), - Height: float32(h), - MinDepth: _D3D12_MIN_DEPTH, - MaxDepth: _D3D12_MAX_DEPTH, - }, - }) + g.drawCommandList.IASetPrimitiveTopology(_D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST) g.drawCommandList.IASetVertexBuffers(0, []_D3D12_VERTEX_BUFFER_VIEW{ { @@ -1158,7 +1256,7 @@ func (g *graphics12) DrawTriangles(dstIDs [graphics.ShaderDstImageCount]graphics Format: _DXGI_FORMAT_R32_UINT, }) - if err := g.pipelineStates.drawTriangles(g.device, g.drawCommandList, g.frameIndex, dst.screen, srcImages, shader, dstRegions, adjustedUniforms, blend, indexOffset, fillRule); err != nil { + if err := g.pipelineStates.drawTriangles(g.device, g.drawCommandList, g.frameIndex, !usesMRT && dsts[firstTarget].screen, srcs, shader, dstRegions, adjustedUniforms, blend, indexOffset, fillRule); err != nil { return err } diff --git a/internal/graphicsdriver/directx/pipeline12_windows.go b/internal/graphicsdriver/directx/pipeline12_windows.go index 4a768dec7..7ede40675 100644 --- a/internal/graphicsdriver/directx/pipeline12_windows.go +++ b/internal/graphicsdriver/directx/pipeline12_windows.go @@ -510,6 +510,90 @@ func (p *pipelineStates) newPipelineState(device *_ID3D12Device, vsh, psh *_ID3D LogicOp: _D3D12_LOGIC_OP_NOOP, RenderTargetWriteMask: writeMask, }, + { + BlendEnable: 1, + LogicOpEnable: 0, + SrcBlend: blendFactorToBlend12(blend.BlendFactorSourceRGB, false), + DestBlend: blendFactorToBlend12(blend.BlendFactorDestinationRGB, false), + BlendOp: blendOperationToBlendOp12(blend.BlendOperationRGB), + SrcBlendAlpha: blendFactorToBlend12(blend.BlendFactorSourceAlpha, true), + DestBlendAlpha: blendFactorToBlend12(blend.BlendFactorDestinationAlpha, true), + BlendOpAlpha: blendOperationToBlendOp12(blend.BlendOperationAlpha), + LogicOp: _D3D12_LOGIC_OP_NOOP, + RenderTargetWriteMask: writeMask, + }, + { + BlendEnable: 1, + LogicOpEnable: 0, + SrcBlend: blendFactorToBlend12(blend.BlendFactorSourceRGB, false), + DestBlend: blendFactorToBlend12(blend.BlendFactorDestinationRGB, false), + BlendOp: blendOperationToBlendOp12(blend.BlendOperationRGB), + SrcBlendAlpha: blendFactorToBlend12(blend.BlendFactorSourceAlpha, true), + DestBlendAlpha: blendFactorToBlend12(blend.BlendFactorDestinationAlpha, true), + BlendOpAlpha: blendOperationToBlendOp12(blend.BlendOperationAlpha), + LogicOp: _D3D12_LOGIC_OP_NOOP, + RenderTargetWriteMask: writeMask, + }, + { + BlendEnable: 1, + LogicOpEnable: 0, + SrcBlend: blendFactorToBlend12(blend.BlendFactorSourceRGB, false), + DestBlend: blendFactorToBlend12(blend.BlendFactorDestinationRGB, false), + BlendOp: blendOperationToBlendOp12(blend.BlendOperationRGB), + SrcBlendAlpha: blendFactorToBlend12(blend.BlendFactorSourceAlpha, true), + DestBlendAlpha: blendFactorToBlend12(blend.BlendFactorDestinationAlpha, true), + BlendOpAlpha: blendOperationToBlendOp12(blend.BlendOperationAlpha), + LogicOp: _D3D12_LOGIC_OP_NOOP, + RenderTargetWriteMask: writeMask, + }, + { + BlendEnable: 1, + LogicOpEnable: 0, + SrcBlend: blendFactorToBlend12(blend.BlendFactorSourceRGB, false), + DestBlend: blendFactorToBlend12(blend.BlendFactorDestinationRGB, false), + BlendOp: blendOperationToBlendOp12(blend.BlendOperationRGB), + SrcBlendAlpha: blendFactorToBlend12(blend.BlendFactorSourceAlpha, true), + DestBlendAlpha: blendFactorToBlend12(blend.BlendFactorDestinationAlpha, true), + BlendOpAlpha: blendOperationToBlendOp12(blend.BlendOperationAlpha), + LogicOp: _D3D12_LOGIC_OP_NOOP, + RenderTargetWriteMask: writeMask, + }, + { + BlendEnable: 1, + LogicOpEnable: 0, + SrcBlend: blendFactorToBlend12(blend.BlendFactorSourceRGB, false), + DestBlend: blendFactorToBlend12(blend.BlendFactorDestinationRGB, false), + BlendOp: blendOperationToBlendOp12(blend.BlendOperationRGB), + SrcBlendAlpha: blendFactorToBlend12(blend.BlendFactorSourceAlpha, true), + DestBlendAlpha: blendFactorToBlend12(blend.BlendFactorDestinationAlpha, true), + BlendOpAlpha: blendOperationToBlendOp12(blend.BlendOperationAlpha), + LogicOp: _D3D12_LOGIC_OP_NOOP, + RenderTargetWriteMask: writeMask, + }, + { + BlendEnable: 1, + LogicOpEnable: 0, + SrcBlend: blendFactorToBlend12(blend.BlendFactorSourceRGB, false), + DestBlend: blendFactorToBlend12(blend.BlendFactorDestinationRGB, false), + BlendOp: blendOperationToBlendOp12(blend.BlendOperationRGB), + SrcBlendAlpha: blendFactorToBlend12(blend.BlendFactorSourceAlpha, true), + DestBlendAlpha: blendFactorToBlend12(blend.BlendFactorDestinationAlpha, true), + BlendOpAlpha: blendOperationToBlendOp12(blend.BlendOperationAlpha), + LogicOp: _D3D12_LOGIC_OP_NOOP, + RenderTargetWriteMask: writeMask, + }, + { + BlendEnable: 1, + LogicOpEnable: 0, + SrcBlend: blendFactorToBlend12(blend.BlendFactorSourceRGB, false), + DestBlend: blendFactorToBlend12(blend.BlendFactorDestinationRGB, false), + BlendOp: blendOperationToBlendOp12(blend.BlendOperationRGB), + SrcBlendAlpha: blendFactorToBlend12(blend.BlendFactorSourceAlpha, true), + DestBlendAlpha: blendFactorToBlend12(blend.BlendFactorDestinationAlpha, true), + BlendOpAlpha: blendOperationToBlendOp12(blend.BlendOperationAlpha), + LogicOp: _D3D12_LOGIC_OP_NOOP, + RenderTargetWriteMask: writeMask, + }, }, }, SampleMask: math.MaxUint32, @@ -532,9 +616,10 @@ func (p *pipelineStates) newPipelineState(device *_ID3D12Device, vsh, psh *_ID3D NumElements: uint32(len(inputElementDescsForDX12)), }, PrimitiveTopologyType: _D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE, - NumRenderTargets: 1, + NumRenderTargets: graphics.ShaderDstImageCount, RTVFormats: [8]_DXGI_FORMAT{ - rtvFormat, + rtvFormat, rtvFormat, rtvFormat, rtvFormat, + rtvFormat, rtvFormat, rtvFormat, rtvFormat, }, DSVFormat: dsvFormat, SampleDesc: _DXGI_SAMPLE_DESC{ From 0b5352580845b3c8e899d438e4256d748a92101a Mon Sep 17 00:00:00 2001 From: Zyko <13394516+Zyko0@users.noreply.github.com> Date: Sat, 13 Apr 2024 11:40:31 +0200 Subject: [PATCH 26/36] Add a basic MRT test --- shader_test.go | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/shader_test.go b/shader_test.go index e04e7749e..5f2f32a9e 100644 --- a/shader_test.go +++ b/shader_test.go @@ -2595,3 +2595,86 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 { } } } + +// Issue #2930 +func TestShaderMRT(t *testing.T) { + const w, h = 16, 16 + + s, err := ebiten.NewShader([]byte(`//kage:unit pixels + +package main + +func Fragment(dstPos vec4, srcPos vec2, color vec4) (vec4, vec4, vec4, vec4, vec4, vec4, vec4, vec4) { + return vec4(1, 0, 0, 1), + vec4(0, 1, 0, 1), + vec4(0, 0, 1, 1), + vec4(1, 0, 1, 1), + vec4(1, 1, 0, 1), + vec4(0, 1, 1, 1), + vec4(1, 1, 1, 1), + vec4(1, 1, 1, 0) +} +`)) + if err != nil { + t.Fatal(err) + } + + bounds := image.Rect(0, 0, w, h) + opts := &ebiten.NewImageOptions{ + Unmanaged: true, + } + imgs := [8]*ebiten.Image{ + ebiten.NewImageWithOptions(bounds, opts), + ebiten.NewImageWithOptions(bounds, opts), + ebiten.NewImageWithOptions(bounds, opts), + ebiten.NewImageWithOptions(bounds, opts), + ebiten.NewImageWithOptions(bounds, opts), + ebiten.NewImageWithOptions(bounds, opts), + ebiten.NewImageWithOptions(bounds, opts), + ebiten.NewImageWithOptions(bounds, opts), + } + vertices := []ebiten.Vertex{ + { + DstX: 0, + DstY: 0, + }, + { + DstX: w, + DstY: 0, + }, + { + DstX: 0, + DstY: h, + }, + { + DstX: w, + DstY: h, + }, + } + indices := []uint16{0, 1, 2, 1, 2, 3} + t.Run("8 slots", func(t *testing.T) { + wantColors := [8]color.RGBA{ + {R: 0xff, G: 0, B: 0, A: 0xff}, + {R: 0, G: 0xff, B: 0, A: 0xff}, + {R: 0, G: 0, B: 0xff, A: 0xff}, + {R: 0xff, G: 0, B: 0xff, A: 0xff}, + {R: 0xff, G: 0xff, B: 0, A: 0xff}, + {R: 0, G: 0xff, B: 0xff, A: 0xff}, + {R: 0xff, G: 0xff, B: 0xff, A: 0xff}, + {R: 0xff, G: 0xff, B: 0xff, A: 0}, + } + dsts := imgs + ebiten.DrawTrianglesShaderMRT(dsts, vertices, indices, s, nil) + for k, dst := range dsts { + for j := 0; j < h; j++ { + for i := 0; i < w; i++ { + got := dst.At(i, j).(color.RGBA) + want := wantColors[k] + if !sameColors(got, want, 1) { + t.Errorf("dst.At(%d, %d): got: %v, want: %v", i, j, got, want) + } + } + } + } + }) +} From d706d38adafb56e4633d798d25984889a1594db2 Mon Sep 17 00:00:00 2001 From: Zyko <13394516+Zyko0@users.noreply.github.com> Date: Sat, 13 Apr 2024 13:56:13 +0200 Subject: [PATCH 27/36] Add test with empty locations + fix directx 11 && 12 nil first argument --- examples/mrt/main.go | 6 +-- image.go | 9 +++-- internal/atlas/image.go | 4 +- internal/graphicscommand/commandqueue.go | 9 ++++- .../directx/graphics11_windows.go | 17 +++++---- .../directx/graphics12_windows.go | 7 ++-- shader_test.go | 38 +++++++++++++++++-- 7 files changed, 65 insertions(+), 25 deletions(-) diff --git a/examples/mrt/main.go b/examples/mrt/main.go index 11c7d1b3e..2cdbce402 100644 --- a/examples/mrt/main.go +++ b/examples/mrt/main.go @@ -36,9 +36,9 @@ var ( ebiten.NewImage(dstSize, dstSize), ebiten.NewImage(dstSize, dstSize), ebiten.NewImage(dstSize, dstSize),*/ - ebiten.NewImageWithOptions(image.Rect(0, 0, dstSize, dstSize), &ebiten.NewImageOptions{ + nil,/*ebiten.NewImageWithOptions(image.Rect(0, 0, dstSize, dstSize), &ebiten.NewImageOptions{ Unmanaged: true, - }), + }),*/ ebiten.NewImageWithOptions(image.Rect(0, 0, dstSize, dstSize), &ebiten.NewImageOptions{ Unmanaged: true, }), @@ -101,7 +101,7 @@ func (g *Game) Draw(screen *ebiten.Image) { indices := []uint16{0, 1, 2, 1, 2, 3} ebiten.DrawTrianglesShaderMRT(dsts, vertices, indices, s, nil) // Dst 0 - screen.DrawImage(dsts[0], nil) + //screen.DrawImage(dsts[0], nil) // Dst 1 opts := &ebiten.DrawImageOptions{} opts.GeoM.Translate(dstSize, 0) diff --git a/image.go b/image.go index 27937d38c..c8d1143ce 100644 --- a/image.go +++ b/image.go @@ -711,10 +711,8 @@ func (i *Image) DrawTrianglesShader(vertices []Vertex, indices []uint16, shader // // When the image i is disposed, DrawTrianglesShader does nothing. func DrawTrianglesShaderMRT(dsts [graphics.ShaderDstImageCount]*Image, vertices []Vertex, indices []uint16, shader *Shader, options *DrawTrianglesShaderOptions) { - if dsts[0] == nil || dsts[0].isDisposed() { - panic("ebiten: the first destination image given to DrawTrianglesShaderMRT must not be nil or disposed") - } var dstImgs [graphics.ShaderDstImageCount]*ui.Image + var firstDst *Image for i, dst := range dsts { if dst == nil { continue @@ -723,6 +721,9 @@ func DrawTrianglesShaderMRT(dsts [graphics.ShaderDstImageCount]*Image, vertices if dst.isDisposed() { panic("ebiten: the destination images given to DrawTrianglesShaderMRT must not be disposed") } + if firstDst == nil { + firstDst = dst + } dstImgs[i] = dst.image } @@ -754,7 +755,7 @@ func DrawTrianglesShaderMRT(dsts [graphics.ShaderDstImageCount]*Image, vertices blend = options.CompositeMode.blend().internalBlend() } - dst := dsts[0] + dst := firstDst vs := dst.ensureTmpVertices(len(vertices) * graphics.VertexFloatCount) src := options.Images[0] for i, v := range vertices { diff --git a/internal/atlas/image.go b/internal/atlas/image.go index d7915d577..d446d8c4c 100644 --- a/internal/atlas/image.go +++ b/internal/atlas/image.go @@ -576,12 +576,14 @@ func drawTrianglesMRT(dsts [graphics.ShaderDstImageCount]*Image, srcs [graphics. src.backend.sourceInThisFrame = true } + var firstDst *Image var dstImgs [graphics.ShaderDstImageCount]*graphicscommand.Image for i, dst := range dsts { if dst == nil { continue } dst.ensureIsolatedFromSource(backends) + firstDst = dst dstImgs[i] = dst.backend.image } @@ -595,7 +597,7 @@ func drawTrianglesMRT(dsts [graphics.ShaderDstImageCount]*Image, srcs [graphics. } } - r := dsts[0].regionWithPadding() + r := firstDst.regionWithPadding() // TODO: Check if dstRegion does not to violate the region. dstRegion = dstRegion.Add(r.Min) diff --git a/internal/graphicscommand/commandqueue.go b/internal/graphicscommand/commandqueue.go index 7cadaba24..3d74c9a09 100644 --- a/internal/graphicscommand/commandqueue.go +++ b/internal/graphicscommand/commandqueue.go @@ -330,7 +330,14 @@ func (q *commandQueue) prependPreservedUniforms(uniforms []uint32, shader *Shade copy(uniforms[graphics.PreservedUniformUint32Count:], origUniforms) // Set the destination texture size. - dw, dh := dsts[0].InternalSize() + var firstDst *Image + for _, dst := range dsts { + if dst != nil { + firstDst = dst + break + } + } + dw, dh := firstDst.InternalSize() uniforms[0] = math.Float32bits(float32(dw)) uniforms[1] = math.Float32bits(float32(dh)) uniformIndex := 2 diff --git a/internal/graphicsdriver/directx/graphics11_windows.go b/internal/graphicsdriver/directx/graphics11_windows.go index 2b90cdc32..23778bb0e 100644 --- a/internal/graphicsdriver/directx/graphics11_windows.go +++ b/internal/graphicsdriver/directx/graphics11_windows.go @@ -517,6 +517,7 @@ func (g *graphics11) removeShader(s *shader11) { func (g *graphics11) setAsRenderTargets(dsts []*image11, useStencil bool) error { var rtvs []*_ID3D11RenderTargetView + var dsv *_ID3D11DepthStencilView for _, i := range dsts { // Ignore a nil image in case of MRT if i == nil { @@ -532,7 +533,7 @@ func (g *graphics11) setAsRenderTargets(dsts []*image11, useStencil bool) error } rtvs = append(rtvs, i.renderTargetView) - if !useStencil { + if !useStencil || dsv != nil { continue } @@ -567,12 +568,13 @@ func (g *graphics11) setAsRenderTargets(dsts []*image11, useStencil bool) error return err } i.stencilView = sv + dsv = sv } } - g.deviceContext.OMSetRenderTargets(rtvs, dsts[0].stencilView) + g.deviceContext.OMSetRenderTargets(rtvs, dsv) if useStencil { - g.deviceContext.ClearDepthStencilView(dsts[0].stencilView, uint8(_D3D11_CLEAR_STENCIL), 0, 0) + g.deviceContext.ClearDepthStencilView(dsv, uint8(_D3D11_CLEAR_STENCIL), 0, 0) } return nil @@ -585,7 +587,7 @@ func (g *graphics11) DrawTriangles(dstIDs [graphics.ShaderDstImageCount]graphics g.deviceContext.PSSetShaderResources(0, srvs[:]) var dsts [graphics.ShaderDstImageCount]*image11 - var viewports [graphics.ShaderDstImageCount]_D3D11_VIEWPORT + var vp _D3D11_VIEWPORT var targetCount int firstTarget := -1 for i, id := range dstIDs { @@ -598,7 +600,7 @@ func (g *graphics11) DrawTriangles(dstIDs [graphics.ShaderDstImageCount]graphics } dsts[i] = img w, h := img.internalSize() - viewports[i] = _D3D11_VIEWPORT{ + vp = _D3D11_VIEWPORT{ TopLeftX: 0, TopLeftY: 0, Width: float32(w), @@ -621,13 +623,12 @@ func (g *graphics11) DrawTriangles(dstIDs [graphics.ShaderDstImageCount]graphics // If the number of targets is more than one, or if the only target is the first one, then // it is safe to assume that MRT is used. // Also, it only matters in order to specify empty targets/viewports when not all slots are - // being filled. + // being filled, even though it's not a MRT scenario. if targetCount > 1 || firstTarget > 0 { targetCount = graphics.ShaderDstImageCount } - - g.deviceContext.RSSetViewports(viewports[:targetCount]) + g.deviceContext.RSSetViewports([]_D3D11_VIEWPORT{vp}) if err := g.setAsRenderTargets(dsts[:targetCount], fillRule != graphicsdriver.FillAll); err != nil { return err } diff --git a/internal/graphicsdriver/directx/graphics12_windows.go b/internal/graphicsdriver/directx/graphics12_windows.go index 250c5b410..99129fafa 100644 --- a/internal/graphicsdriver/directx/graphics12_windows.go +++ b/internal/graphicsdriver/directx/graphics12_windows.go @@ -1177,7 +1177,7 @@ func (g *graphics12) DrawTriangles(dstIDs [graphics.ShaderDstImageCount]graphics var resourceBarriers []_D3D12_RESOURCE_BARRIER_Transition var dsts [graphics.ShaderDstImageCount]*image12 - var viewports [graphics.ShaderDstImageCount]_D3D12_VIEWPORT + var vp _D3D12_VIEWPORT var targetCount int firstTarget := -1 for i, id := range dstIDs { @@ -1190,7 +1190,7 @@ func (g *graphics12) DrawTriangles(dstIDs [graphics.ShaderDstImageCount]graphics } dsts[i] = img w, h := img.internalSize() - viewports[i] = _D3D12_VIEWPORT{ + vp = _D3D12_VIEWPORT{ TopLeftX: 0, TopLeftY: 0, Width: float32(w), @@ -1231,8 +1231,7 @@ func (g *graphics12) DrawTriangles(dstIDs [graphics.ShaderDstImageCount]graphics targetCount = graphics.ShaderDstImageCount } - g.drawCommandList.RSSetViewports(viewports[:targetCount]) - + g.drawCommandList.RSSetViewports([]_D3D12_VIEWPORT{vp}) if err := g.setAsRenderTargets(dsts[:targetCount], fillRule != graphicsdriver.FillAll); err != nil { return err } diff --git a/shader_test.go b/shader_test.go index 5f2f32a9e..b325bb8d7 100644 --- a/shader_test.go +++ b/shader_test.go @@ -2652,7 +2652,7 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) (vec4, vec4, vec4, vec4, vec }, } indices := []uint16{0, 1, 2, 1, 2, 3} - t.Run("8 slots", func(t *testing.T) { + t.Run("8 locations", func(t *testing.T) { wantColors := [8]color.RGBA{ {R: 0xff, G: 0, B: 0, A: 0xff}, {R: 0, G: 0xff, B: 0, A: 0xff}, @@ -2663,9 +2663,8 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) (vec4, vec4, vec4, vec4, vec {R: 0xff, G: 0xff, B: 0xff, A: 0xff}, {R: 0xff, G: 0xff, B: 0xff, A: 0}, } - dsts := imgs - ebiten.DrawTrianglesShaderMRT(dsts, vertices, indices, s, nil) - for k, dst := range dsts { + ebiten.DrawTrianglesShaderMRT(imgs, vertices, indices, s, nil) + for k, dst := range imgs { for j := 0; j < h; j++ { for i := 0; i < w; i++ { got := dst.At(i, j).(color.RGBA) @@ -2677,4 +2676,35 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) (vec4, vec4, vec4, vec4, vec } } }) + + for _, img := range imgs { + img.Clear() + } + t.Run("Empty locations", func(t *testing.T) { + wantColors := [8]color.RGBA{ + {}, + {R: 0, G: 0xff, B: 0, A: 0xff}, + {}, + {R: 0xff, G: 0, B: 0xff, A: 0xff}, + {}, + {R: 0, G: 0xff, B: 0xff, A: 0xff}, + {}, + {R: 0xff, G: 0xff, B: 0xff, A: 0}, + } + dsts := [8]*ebiten.Image{ + nil, imgs[1], nil, imgs[3], nil, imgs[5], nil, imgs[7], + } + ebiten.DrawTrianglesShaderMRT(dsts, vertices, indices, s, nil) + for k, dst := range imgs { + for j := 0; j < h; j++ { + for i := 0; i < w; i++ { + got := dst.At(i, j).(color.RGBA) + want := wantColors[k] + if !sameColors(got, want, 1) { + t.Errorf("%d dst.At(%d, %d): got: %v, want: %v", k, i, j, got, want) + } + } + } + } + }) } From dbe06ec468f6fefe792faea6b391ef1e8a9f9b14 Mon Sep 17 00:00:00 2001 From: Zyko <13394516+Zyko0@users.noreply.github.com> Date: Sat, 13 Apr 2024 14:04:39 +0200 Subject: [PATCH 28/36] Add a test when a single image is used with DrawMRT --- .../directx/graphics12_windows.go | 2 +- shader_test.go | 27 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/internal/graphicsdriver/directx/graphics12_windows.go b/internal/graphicsdriver/directx/graphics12_windows.go index 99129fafa..6a5d2eff3 100644 --- a/internal/graphicsdriver/directx/graphics12_windows.go +++ b/internal/graphicsdriver/directx/graphics12_windows.go @@ -1225,7 +1225,7 @@ func (g *graphics12) DrawTriangles(dstIDs [graphics.ShaderDstImageCount]graphics // If the number of targets is more than one, or if the only target is the first one, then // it is safe to assume that MRT is used. // Also, it only matters in order to specify empty targets/viewports when not all slots are - // being filled. + // being filled, even though it's not a MRT scenario. usesMRT := targetCount > 1 || firstTarget > 0 if usesMRT { targetCount = graphics.ShaderDstImageCount diff --git a/shader_test.go b/shader_test.go index b325bb8d7..be5990706 100644 --- a/shader_test.go +++ b/shader_test.go @@ -2677,6 +2677,7 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) (vec4, vec4, vec4, vec4, vec } }) + // Clear images for _, img := range imgs { img.Clear() } @@ -2707,4 +2708,30 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) (vec4, vec4, vec4, vec4, vec } } }) + + // Clear images + for _, img := range imgs { + img.Clear() + } + t.Run("1 location (first slot)", func(t *testing.T) { + wantColors := [8]color.RGBA{ + {R: 0xff, G: 0, B: 0, A: 0xff}, + {}, {}, {}, {}, {}, {}, {}, + } + dsts := [8]*ebiten.Image{ + imgs[0], nil, nil, nil, nil, nil, nil, nil, + } + ebiten.DrawTrianglesShaderMRT(dsts, vertices, indices, s, nil) + for k, dst := range imgs { + for j := 0; j < h; j++ { + for i := 0; i < w; i++ { + got := dst.At(i, j).(color.RGBA) + want := wantColors[k] + if !sameColors(got, want, 1) { + t.Errorf("dst.At(%d, %d): got: %v, want: %v", i, j, got, want) + } + } + } + } + }) } From dc6074bec912ee6f74da26f1fac0f0a13221d011 Mon Sep 17 00:00:00 2001 From: Zyko <13394516+Zyko0@users.noreply.github.com> Date: Sat, 13 Apr 2024 14:23:17 +0200 Subject: [PATCH 29/36] Fixed stencil tests for directx --- .../directx/graphics11_windows.go | 28 +++++++++---------- .../directx/graphics12_windows.go | 12 ++++---- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/internal/graphicsdriver/directx/graphics11_windows.go b/internal/graphicsdriver/directx/graphics11_windows.go index 23778bb0e..8f37a8831 100644 --- a/internal/graphicsdriver/directx/graphics11_windows.go +++ b/internal/graphicsdriver/directx/graphics11_windows.go @@ -518,30 +518,30 @@ func (g *graphics11) removeShader(s *shader11) { func (g *graphics11) setAsRenderTargets(dsts []*image11, useStencil bool) error { var rtvs []*_ID3D11RenderTargetView var dsv *_ID3D11DepthStencilView - for _, i := range dsts { + for _, dst := range dsts { // Ignore a nil image in case of MRT - if i == nil { + if dst == nil { rtvs = append(rtvs, nil) continue } - if i.renderTargetView == nil { - rtv, err := g.device.CreateRenderTargetView(unsafe.Pointer(i.texture), nil) + if dst.renderTargetView == nil { + rtv, err := g.device.CreateRenderTargetView(unsafe.Pointer(dst.texture), nil) if err != nil { return err } - i.renderTargetView = rtv + dst.renderTargetView = rtv } - rtvs = append(rtvs, i.renderTargetView) + rtvs = append(rtvs, dst.renderTargetView) if !useStencil || dsv != nil { continue } - if i.screen { + if dst.screen { return fmt.Errorf("directx: a stencil buffer is not available for a screen image") } - if i.stencil == nil { - w, h := i.internalSize() + if dst.stencil == nil { + w, h := dst.internalSize() s, err := g.device.CreateTexture2D(&_D3D11_TEXTURE2D_DESC{ Width: uint32(w), Height: uint32(h), @@ -560,16 +560,16 @@ func (g *graphics11) setAsRenderTargets(dsts []*image11, useStencil bool) error if err != nil { return err } - i.stencil = s + dst.stencil = s } - if i.stencilView == nil { - sv, err := g.device.CreateDepthStencilView(unsafe.Pointer(i.stencil), nil) + if dst.stencilView == nil { + sv, err := g.device.CreateDepthStencilView(unsafe.Pointer(dst.stencil), nil) if err != nil { return err } - i.stencilView = sv - dsv = sv + dst.stencilView = sv } + dsv = dst.stencilView } g.deviceContext.OMSetRenderTargets(rtvs, dsv) diff --git a/internal/graphicsdriver/directx/graphics12_windows.go b/internal/graphicsdriver/directx/graphics12_windows.go index 6a5d2eff3..e2670cf52 100644 --- a/internal/graphicsdriver/directx/graphics12_windows.go +++ b/internal/graphicsdriver/directx/graphics12_windows.go @@ -1084,7 +1084,7 @@ func (g *graphics12) NewShader(program *shaderir.Program) (graphicsdriver.Shader func (g *graphics12) setAsRenderTargets(dsts []*image12, useStencil bool) error { var rtvs []_D3D12_CPU_DESCRIPTOR_HANDLE - var dsvPtr *_D3D12_CPU_DESCRIPTOR_HANDLE + var dsv *_D3D12_CPU_DESCRIPTOR_HANDLE for i, img := range dsts { // Ignore a nil image in case of MRT @@ -1127,18 +1127,18 @@ func (g *graphics12) setAsRenderTargets(dsts []*image12, useStencil bool) error rtv := rtvBase rtvs = append(rtvs, rtv) - if !useStencil || dsvPtr != nil { + if !useStencil || dsv != nil { continue } if err := img.ensureDepthStencilView(g.device); err != nil { return err } - dsv, err := img.dsvDescriptorHeap.GetCPUDescriptorHandleForHeapStart() + sv, err := img.dsvDescriptorHeap.GetCPUDescriptorHandleForHeapStart() if err != nil { return err } - dsvPtr = &dsv + dsv = &sv } if !useStencil { @@ -1147,8 +1147,8 @@ func (g *graphics12) setAsRenderTargets(dsts []*image12, useStencil bool) error } g.drawCommandList.OMSetStencilRef(0) - g.drawCommandList.OMSetRenderTargets(rtvs, false, dsvPtr) - g.drawCommandList.ClearDepthStencilView(*dsvPtr, _D3D12_CLEAR_FLAG_STENCIL, 0, 0, nil) + g.drawCommandList.OMSetRenderTargets(rtvs, false, dsv) + g.drawCommandList.ClearDepthStencilView(*dsv, _D3D12_CLEAR_FLAG_STENCIL, 0, 0, nil) return nil } From 483059184f8c2f0f695321520daaf015f3f6cedb Mon Sep 17 00:00:00 2001 From: Zyko <13394516+Zyko0@users.noreply.github.com> Date: Sat, 13 Apr 2024 14:55:41 +0200 Subject: [PATCH 30/36] Debug image entry index --- shader_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/shader_test.go b/shader_test.go index be5990706..fb52bf419 100644 --- a/shader_test.go +++ b/shader_test.go @@ -2697,6 +2697,7 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) (vec4, vec4, vec4, vec4, vec } ebiten.DrawTrianglesShaderMRT(dsts, vertices, indices, s, nil) for k, dst := range imgs { + t.Log("image index:", k) for j := 0; j < h; j++ { for i := 0; i < w; i++ { got := dst.At(i, j).(color.RGBA) From 9881530b46a923938bb2e088e3c3cc6c75376dfb Mon Sep 17 00:00:00 2001 From: Zyko <13394516+Zyko0@users.noreply.github.com> Date: Sat, 13 Apr 2024 15:19:46 +0200 Subject: [PATCH 31/36] Attempt at fixing Directx 12 --- examples/mrt/main.go | 6 +- .../directx/graphics12_windows.go | 10 +- .../graphicsdriver/directx/image12_windows.go | 2 +- .../directx/pipeline12_windows.go | 109 +++--------------- shader_test.go | 49 +++++--- 5 files changed, 55 insertions(+), 121 deletions(-) diff --git a/examples/mrt/main.go b/examples/mrt/main.go index 2cdbce402..11c7d1b3e 100644 --- a/examples/mrt/main.go +++ b/examples/mrt/main.go @@ -36,9 +36,9 @@ var ( ebiten.NewImage(dstSize, dstSize), ebiten.NewImage(dstSize, dstSize), ebiten.NewImage(dstSize, dstSize),*/ - nil,/*ebiten.NewImageWithOptions(image.Rect(0, 0, dstSize, dstSize), &ebiten.NewImageOptions{ + ebiten.NewImageWithOptions(image.Rect(0, 0, dstSize, dstSize), &ebiten.NewImageOptions{ Unmanaged: true, - }),*/ + }), ebiten.NewImageWithOptions(image.Rect(0, 0, dstSize, dstSize), &ebiten.NewImageOptions{ Unmanaged: true, }), @@ -101,7 +101,7 @@ func (g *Game) Draw(screen *ebiten.Image) { indices := []uint16{0, 1, 2, 1, 2, 3} ebiten.DrawTrianglesShaderMRT(dsts, vertices, indices, s, nil) // Dst 0 - //screen.DrawImage(dsts[0], nil) + screen.DrawImage(dsts[0], nil) // Dst 1 opts := &ebiten.DrawImageOptions{} opts.GeoM.Translate(dstSize, 0) diff --git a/internal/graphicsdriver/directx/graphics12_windows.go b/internal/graphicsdriver/directx/graphics12_windows.go index e2670cf52..89ec19638 100644 --- a/internal/graphicsdriver/directx/graphics12_windows.go +++ b/internal/graphicsdriver/directx/graphics12_windows.go @@ -1127,13 +1127,18 @@ func (g *graphics12) setAsRenderTargets(dsts []*image12, useStencil bool) error rtv := rtvBase rtvs = append(rtvs, rtv) - if !useStencil || dsv != nil { + if !useStencil { continue } if err := img.ensureDepthStencilView(g.device); err != nil { return err } + + if dsv != nil { + continue + } + sv, err := img.dsvDescriptorHeap.GetCPUDescriptorHandleForHeapStart() if err != nil { return err @@ -1231,7 +1236,6 @@ func (g *graphics12) DrawTriangles(dstIDs [graphics.ShaderDstImageCount]graphics targetCount = graphics.ShaderDstImageCount } - g.drawCommandList.RSSetViewports([]_D3D12_VIEWPORT{vp}) if err := g.setAsRenderTargets(dsts[:targetCount], fillRule != graphicsdriver.FillAll); err != nil { return err } @@ -1241,6 +1245,8 @@ func (g *graphics12) DrawTriangles(dstIDs [graphics.ShaderDstImageCount]graphics g.needFlushDrawCommandList = true + g.drawCommandList.RSSetViewports([]_D3D12_VIEWPORT{vp}) + g.drawCommandList.IASetPrimitiveTopology(_D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST) g.drawCommandList.IASetVertexBuffers(0, []_D3D12_VERTEX_BUFFER_VIEW{ { diff --git a/internal/graphicsdriver/directx/image12_windows.go b/internal/graphicsdriver/directx/image12_windows.go index aab796833..6c3c1bdb8 100644 --- a/internal/graphicsdriver/directx/image12_windows.go +++ b/internal/graphicsdriver/directx/image12_windows.go @@ -70,7 +70,7 @@ func (i *image12) disposeImpl() { func (i *image12) ReadPixels(args []graphicsdriver.PixelsArgs) error { if i.screen { - return errors.New("directx: Pixels cannot be called on the screen") + return errors.New("directx: ReadPixels cannot be called on the screen") } if err := i.graphics.flushCommandList(i.graphics.drawCommandList); err != nil { diff --git a/internal/graphicsdriver/directx/pipeline12_windows.go b/internal/graphicsdriver/directx/pipeline12_windows.go index 7ede40675..eed7cff73 100644 --- a/internal/graphicsdriver/directx/pipeline12_windows.go +++ b/internal/graphicsdriver/directx/pipeline12_windows.go @@ -484,6 +484,18 @@ func (p *pipelineStates) newPipelineState(device *_ID3D12Device, vsh, psh *_ID3D } // Create a pipeline state. + rtBlendDesc := _D3D12_RENDER_TARGET_BLEND_DESC{ + BlendEnable: 1, + LogicOpEnable: 0, + SrcBlend: blendFactorToBlend12(blend.BlendFactorSourceRGB, false), + DestBlend: blendFactorToBlend12(blend.BlendFactorDestinationRGB, false), + BlendOp: blendOperationToBlendOp12(blend.BlendOperationRGB), + SrcBlendAlpha: blendFactorToBlend12(blend.BlendFactorSourceAlpha, true), + DestBlendAlpha: blendFactorToBlend12(blend.BlendFactorDestinationAlpha, true), + BlendOpAlpha: blendOperationToBlendOp12(blend.BlendOperationAlpha), + LogicOp: _D3D12_LOGIC_OP_NOOP, + RenderTargetWriteMask: writeMask, + } psoDesc := _D3D12_GRAPHICS_PIPELINE_STATE_DESC{ pRootSignature: rootSignature, VS: _D3D12_SHADER_BYTECODE{ @@ -498,102 +510,7 @@ func (p *pipelineStates) newPipelineState(device *_ID3D12Device, vsh, psh *_ID3D AlphaToCoverageEnable: 0, IndependentBlendEnable: 0, RenderTarget: [8]_D3D12_RENDER_TARGET_BLEND_DESC{ - { - BlendEnable: 1, - LogicOpEnable: 0, - SrcBlend: blendFactorToBlend12(blend.BlendFactorSourceRGB, false), - DestBlend: blendFactorToBlend12(blend.BlendFactorDestinationRGB, false), - BlendOp: blendOperationToBlendOp12(blend.BlendOperationRGB), - SrcBlendAlpha: blendFactorToBlend12(blend.BlendFactorSourceAlpha, true), - DestBlendAlpha: blendFactorToBlend12(blend.BlendFactorDestinationAlpha, true), - BlendOpAlpha: blendOperationToBlendOp12(blend.BlendOperationAlpha), - LogicOp: _D3D12_LOGIC_OP_NOOP, - RenderTargetWriteMask: writeMask, - }, - { - BlendEnable: 1, - LogicOpEnable: 0, - SrcBlend: blendFactorToBlend12(blend.BlendFactorSourceRGB, false), - DestBlend: blendFactorToBlend12(blend.BlendFactorDestinationRGB, false), - BlendOp: blendOperationToBlendOp12(blend.BlendOperationRGB), - SrcBlendAlpha: blendFactorToBlend12(blend.BlendFactorSourceAlpha, true), - DestBlendAlpha: blendFactorToBlend12(blend.BlendFactorDestinationAlpha, true), - BlendOpAlpha: blendOperationToBlendOp12(blend.BlendOperationAlpha), - LogicOp: _D3D12_LOGIC_OP_NOOP, - RenderTargetWriteMask: writeMask, - }, - { - BlendEnable: 1, - LogicOpEnable: 0, - SrcBlend: blendFactorToBlend12(blend.BlendFactorSourceRGB, false), - DestBlend: blendFactorToBlend12(blend.BlendFactorDestinationRGB, false), - BlendOp: blendOperationToBlendOp12(blend.BlendOperationRGB), - SrcBlendAlpha: blendFactorToBlend12(blend.BlendFactorSourceAlpha, true), - DestBlendAlpha: blendFactorToBlend12(blend.BlendFactorDestinationAlpha, true), - BlendOpAlpha: blendOperationToBlendOp12(blend.BlendOperationAlpha), - LogicOp: _D3D12_LOGIC_OP_NOOP, - RenderTargetWriteMask: writeMask, - }, - { - BlendEnable: 1, - LogicOpEnable: 0, - SrcBlend: blendFactorToBlend12(blend.BlendFactorSourceRGB, false), - DestBlend: blendFactorToBlend12(blend.BlendFactorDestinationRGB, false), - BlendOp: blendOperationToBlendOp12(blend.BlendOperationRGB), - SrcBlendAlpha: blendFactorToBlend12(blend.BlendFactorSourceAlpha, true), - DestBlendAlpha: blendFactorToBlend12(blend.BlendFactorDestinationAlpha, true), - BlendOpAlpha: blendOperationToBlendOp12(blend.BlendOperationAlpha), - LogicOp: _D3D12_LOGIC_OP_NOOP, - RenderTargetWriteMask: writeMask, - }, - { - BlendEnable: 1, - LogicOpEnable: 0, - SrcBlend: blendFactorToBlend12(blend.BlendFactorSourceRGB, false), - DestBlend: blendFactorToBlend12(blend.BlendFactorDestinationRGB, false), - BlendOp: blendOperationToBlendOp12(blend.BlendOperationRGB), - SrcBlendAlpha: blendFactorToBlend12(blend.BlendFactorSourceAlpha, true), - DestBlendAlpha: blendFactorToBlend12(blend.BlendFactorDestinationAlpha, true), - BlendOpAlpha: blendOperationToBlendOp12(blend.BlendOperationAlpha), - LogicOp: _D3D12_LOGIC_OP_NOOP, - RenderTargetWriteMask: writeMask, - }, - { - BlendEnable: 1, - LogicOpEnable: 0, - SrcBlend: blendFactorToBlend12(blend.BlendFactorSourceRGB, false), - DestBlend: blendFactorToBlend12(blend.BlendFactorDestinationRGB, false), - BlendOp: blendOperationToBlendOp12(blend.BlendOperationRGB), - SrcBlendAlpha: blendFactorToBlend12(blend.BlendFactorSourceAlpha, true), - DestBlendAlpha: blendFactorToBlend12(blend.BlendFactorDestinationAlpha, true), - BlendOpAlpha: blendOperationToBlendOp12(blend.BlendOperationAlpha), - LogicOp: _D3D12_LOGIC_OP_NOOP, - RenderTargetWriteMask: writeMask, - }, - { - BlendEnable: 1, - LogicOpEnable: 0, - SrcBlend: blendFactorToBlend12(blend.BlendFactorSourceRGB, false), - DestBlend: blendFactorToBlend12(blend.BlendFactorDestinationRGB, false), - BlendOp: blendOperationToBlendOp12(blend.BlendOperationRGB), - SrcBlendAlpha: blendFactorToBlend12(blend.BlendFactorSourceAlpha, true), - DestBlendAlpha: blendFactorToBlend12(blend.BlendFactorDestinationAlpha, true), - BlendOpAlpha: blendOperationToBlendOp12(blend.BlendOperationAlpha), - LogicOp: _D3D12_LOGIC_OP_NOOP, - RenderTargetWriteMask: writeMask, - }, - { - BlendEnable: 1, - LogicOpEnable: 0, - SrcBlend: blendFactorToBlend12(blend.BlendFactorSourceRGB, false), - DestBlend: blendFactorToBlend12(blend.BlendFactorDestinationRGB, false), - BlendOp: blendOperationToBlendOp12(blend.BlendOperationRGB), - SrcBlendAlpha: blendFactorToBlend12(blend.BlendFactorSourceAlpha, true), - DestBlendAlpha: blendFactorToBlend12(blend.BlendFactorDestinationAlpha, true), - BlendOpAlpha: blendOperationToBlendOp12(blend.BlendOperationAlpha), - LogicOp: _D3D12_LOGIC_OP_NOOP, - RenderTargetWriteMask: writeMask, - }, + rtBlendDesc, // TODO: need to fill them all? }, }, SampleMask: math.MaxUint32, diff --git a/shader_test.go b/shader_test.go index fb52bf419..b87fc7f19 100644 --- a/shader_test.go +++ b/shader_test.go @@ -2623,16 +2623,6 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) (vec4, vec4, vec4, vec4, vec opts := &ebiten.NewImageOptions{ Unmanaged: true, } - imgs := [8]*ebiten.Image{ - ebiten.NewImageWithOptions(bounds, opts), - ebiten.NewImageWithOptions(bounds, opts), - ebiten.NewImageWithOptions(bounds, opts), - ebiten.NewImageWithOptions(bounds, opts), - ebiten.NewImageWithOptions(bounds, opts), - ebiten.NewImageWithOptions(bounds, opts), - ebiten.NewImageWithOptions(bounds, opts), - ebiten.NewImageWithOptions(bounds, opts), - } vertices := []ebiten.Vertex{ { DstX: 0, @@ -2653,6 +2643,16 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) (vec4, vec4, vec4, vec4, vec } indices := []uint16{0, 1, 2, 1, 2, 3} t.Run("8 locations", func(t *testing.T) { + imgs := [8]*ebiten.Image{ + ebiten.NewImageWithOptions(bounds, opts), + ebiten.NewImageWithOptions(bounds, opts), + ebiten.NewImageWithOptions(bounds, opts), + ebiten.NewImageWithOptions(bounds, opts), + ebiten.NewImageWithOptions(bounds, opts), + ebiten.NewImageWithOptions(bounds, opts), + ebiten.NewImageWithOptions(bounds, opts), + ebiten.NewImageWithOptions(bounds, opts), + } wantColors := [8]color.RGBA{ {R: 0xff, G: 0, B: 0, A: 0xff}, {R: 0, G: 0xff, B: 0, A: 0xff}, @@ -2677,11 +2677,17 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) (vec4, vec4, vec4, vec4, vec } }) - // Clear images - for _, img := range imgs { - img.Clear() - } t.Run("Empty locations", func(t *testing.T) { + imgs := [8]*ebiten.Image{ + ebiten.NewImageWithOptions(bounds, opts), + ebiten.NewImageWithOptions(bounds, opts), + ebiten.NewImageWithOptions(bounds, opts), + ebiten.NewImageWithOptions(bounds, opts), + ebiten.NewImageWithOptions(bounds, opts), + ebiten.NewImageWithOptions(bounds, opts), + ebiten.NewImageWithOptions(bounds, opts), + ebiten.NewImageWithOptions(bounds, opts), + } wantColors := [8]color.RGBA{ {}, {R: 0, G: 0xff, B: 0, A: 0xff}, @@ -2697,7 +2703,6 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) (vec4, vec4, vec4, vec4, vec } ebiten.DrawTrianglesShaderMRT(dsts, vertices, indices, s, nil) for k, dst := range imgs { - t.Log("image index:", k) for j := 0; j < h; j++ { for i := 0; i < w; i++ { got := dst.At(i, j).(color.RGBA) @@ -2710,11 +2715,17 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) (vec4, vec4, vec4, vec4, vec } }) - // Clear images - for _, img := range imgs { - img.Clear() - } t.Run("1 location (first slot)", func(t *testing.T) { + imgs := [8]*ebiten.Image{ + ebiten.NewImageWithOptions(bounds, opts), + ebiten.NewImageWithOptions(bounds, opts), + ebiten.NewImageWithOptions(bounds, opts), + ebiten.NewImageWithOptions(bounds, opts), + ebiten.NewImageWithOptions(bounds, opts), + ebiten.NewImageWithOptions(bounds, opts), + ebiten.NewImageWithOptions(bounds, opts), + ebiten.NewImageWithOptions(bounds, opts), + } wantColors := [8]color.RGBA{ {R: 0xff, G: 0, B: 0, A: 0xff}, {}, {}, {}, {}, {}, {}, {}, From ec592390e730e4970cb1905ae007ab67c0f95d2b Mon Sep 17 00:00:00 2001 From: Zyko <13394516+Zyko0@users.noreply.github.com> Date: Sun, 14 Apr 2024 18:11:24 +0200 Subject: [PATCH 32/36] nth attempt at fixing directx 12 --- .../directx/graphics12_windows.go | 55 ++++++++++++++----- .../directx/pipeline12_windows.go | 3 +- 2 files changed, 44 insertions(+), 14 deletions(-) diff --git a/internal/graphicsdriver/directx/graphics12_windows.go b/internal/graphicsdriver/directx/graphics12_windows.go index 89ec19638..d857bb8be 100644 --- a/internal/graphicsdriver/directx/graphics12_windows.go +++ b/internal/graphicsdriver/directx/graphics12_windows.go @@ -40,13 +40,15 @@ func (r *resourceWithSize) release() { } type graphics12 struct { - debug *_ID3D12Debug - device *_ID3D12Device - commandQueue *_ID3D12CommandQueue - rtvDescriptorHeap *_ID3D12DescriptorHeap - rtvDescriptorSize uint32 - renderTargets [frameCount]*_ID3D12Resource - framePipelineToken _D3D12XBOX_FRAME_PIPELINE_TOKEN + debug *_ID3D12Debug + device *_ID3D12Device + commandQueue *_ID3D12CommandQueue + rtvDescriptorHeap *_ID3D12DescriptorHeap + rtvEmptyDescriptorHeap *_ID3D12DescriptorHeap + rtvDescriptorSize uint32 + emptyRenderTarget *_ID3D12Resource + renderTargets [frameCount]*_ID3D12Resource + framePipelineToken _D3D12XBOX_FRAME_PIPELINE_TOKEN fence *_ID3D12Fence fenceValues [frameCount]uint64 @@ -418,6 +420,34 @@ func (g *graphics12) initializeMembers(frameIndex int) (ferr error) { g.rtvDescriptorHeap = nil } }() + + // Create a descriptor heap for empty RTV in case of MRT with empty locations. + h, err = g.device.CreateDescriptorHeap(&_D3D12_DESCRIPTOR_HEAP_DESC{ + Type: _D3D12_DESCRIPTOR_HEAP_TYPE_RTV, + NumDescriptors: frameCount, + Flags: _D3D12_DESCRIPTOR_HEAP_FLAG_NONE, + NodeMask: 0, + }) + if err != nil { + return err + } + g.rtvEmptyDescriptorHeap = h + defer func() { + if ferr != nil { + g.rtvEmptyDescriptorHeap.Release() + g.rtvEmptyDescriptorHeap = nil + } + }() + hnd, err := g.rtvEmptyDescriptorHeap.GetCPUDescriptorHandleForHeapStart() + if err != nil { + return err + } + // Create an empty render target for empty destinations at DrawTriangles + g.device.CreateRenderTargetView(nil, &_D3D12_RENDER_TARGET_VIEW_DESC{ + Format: _DXGI_FORMAT_R8G8B8A8_UNORM, + ViewDimension: _D3D12_RTV_DIMENSION_TEXTURE2D, + }, hnd) + g.rtvDescriptorSize = g.device.GetDescriptorHandleIncrementSize(_D3D12_DESCRIPTOR_HEAP_TYPE_RTV) if err := g.pipelineStates.initialize(g.device); err != nil { @@ -1090,20 +1120,15 @@ func (g *graphics12) setAsRenderTargets(dsts []*image12, useStencil bool) error // Ignore a nil image in case of MRT if img == nil { _ = i - rtvBase, err := g.rtvDescriptorHeap.GetCPUDescriptorHandleForHeapStart() + rtv, err := g.rtvEmptyDescriptorHeap.GetCPUDescriptorHandleForHeapStart() if err != nil { return err } - rtv := rtvBase rtv.Offset(int32(g.frameIndex), g.rtvDescriptorSize) rtvs = append(rtvs, rtv) continue } - if err := img.ensureRenderTargetView(g.device); err != nil { - return err - } - if img.screen { if useStencil { return fmt.Errorf("directx: stencils are not available on the screen framebuffer") @@ -1119,6 +1144,10 @@ func (g *graphics12) setAsRenderTargets(dsts []*image12, useStencil bool) error continue } + if err := img.ensureRenderTargetView(g.device); err != nil { + return err + } + rtvBase, err := img.rtvDescriptorHeap.GetCPUDescriptorHandleForHeapStart() if err != nil { return err diff --git a/internal/graphicsdriver/directx/pipeline12_windows.go b/internal/graphicsdriver/directx/pipeline12_windows.go index eed7cff73..8dbe78e9a 100644 --- a/internal/graphicsdriver/directx/pipeline12_windows.go +++ b/internal/graphicsdriver/directx/pipeline12_windows.go @@ -510,7 +510,8 @@ func (p *pipelineStates) newPipelineState(device *_ID3D12Device, vsh, psh *_ID3D AlphaToCoverageEnable: 0, IndependentBlendEnable: 0, RenderTarget: [8]_D3D12_RENDER_TARGET_BLEND_DESC{ - rtBlendDesc, // TODO: need to fill them all? + rtBlendDesc, rtBlendDesc, rtBlendDesc, rtBlendDesc, + rtBlendDesc, rtBlendDesc, rtBlendDesc, rtBlendDesc, // TODO: need to fill them all? }, }, SampleMask: math.MaxUint32, From c180c3c9d19dbf3b58b9f7a86fa215b7fe561011 Mon Sep 17 00:00:00 2001 From: Zyko <13394516+Zyko0@users.noreply.github.com> Date: Sun, 9 Jun 2024 17:16:28 +0200 Subject: [PATCH 33/36] Little tweak to dx12 --- internal/graphicsdriver/directx/graphics12_windows.go | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/graphicsdriver/directx/graphics12_windows.go b/internal/graphicsdriver/directx/graphics12_windows.go index d857bb8be..953689260 100644 --- a/internal/graphicsdriver/directx/graphics12_windows.go +++ b/internal/graphicsdriver/directx/graphics12_windows.go @@ -46,7 +46,6 @@ type graphics12 struct { rtvDescriptorHeap *_ID3D12DescriptorHeap rtvEmptyDescriptorHeap *_ID3D12DescriptorHeap rtvDescriptorSize uint32 - emptyRenderTarget *_ID3D12Resource renderTargets [frameCount]*_ID3D12Resource framePipelineToken _D3D12XBOX_FRAME_PIPELINE_TOKEN From b405003a3565017b1b59edfc6653937a9f9c9483 Mon Sep 17 00:00:00 2001 From: Zyko <13394516+Zyko0@users.noreply.github.com> Date: Sun, 9 Jun 2024 17:41:11 +0200 Subject: [PATCH 34/36] Fixed misses --- internal/atlas/image_test.go | 4 ++-- internal/ui/image.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/atlas/image_test.go b/internal/atlas/image_test.go index fabce8142..3bc0d796f 100644 --- a/internal/atlas/image_test.go +++ b/internal/atlas/image_test.go @@ -802,14 +802,14 @@ func TestDestinationCountOverflow(t *testing.T) { // Use dst0 as a destination for a while. for i := 0; i < 31; i++ { dst0.DrawTriangles([graphics.ShaderSrcImageCount]*atlas.Image{src}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderSrcImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillRuleFillAll) - atlas.PutImagesOnSourceBackendForTesting(ui.Get().GraphicsDriverForTesting()) + atlas.PutImagesOnSourceBackendForTesting() } // Use dst0 as a source for a while. // As dst0 is used as a destination too many times (31 is a maximum), dst0's backend should never be a source backend. for i := 0; i < 100; i++ { dst1.DrawTriangles([graphics.ShaderSrcImageCount]*atlas.Image{dst0}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderSrcImageCount]image.Rectangle{}, atlas.NearestFilterShader, nil, graphicsdriver.FillRuleFillAll) - atlas.PutImagesOnSourceBackendForTesting(ui.Get().GraphicsDriverForTesting()) + atlas.PutImagesOnSourceBackendForTesting() if dst0.IsOnSourceBackendForTesting() { t.Errorf("dst0 cannot be on a source backend: %d", i) } diff --git a/internal/ui/image.go b/internal/ui/image.go index c16c5351e..11f655ec9 100644 --- a/internal/ui/image.go +++ b/internal/ui/image.go @@ -244,7 +244,7 @@ func (i *bigOffscreenImage) deallocate() { i.dirty = false } -func (i *bigOffscreenImage) 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.FillRuleFillAll, canSkipMipmap bool, antialias bool) { +func (i *bigOffscreenImage) 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, canSkipMipmap bool) { if i.blend != blend { i.flush() } From 5e40bce5c9f530056ca8fd22f9f051ea4189929b Mon Sep 17 00:00:00 2001 From: Zyko <13394516+Zyko0@users.noreply.github.com> Date: Fri, 14 Jun 2024 19:00:33 +0200 Subject: [PATCH 35/36] Fixed authors name --- examples/mrt/main.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/examples/mrt/main.go b/examples/mrt/main.go index 11c7d1b3e..924b3f5ce 100644 --- a/examples/mrt/main.go +++ b/examples/mrt/main.go @@ -1,4 +1,4 @@ -// Copyright 2024 The Ebiten Authors +// Copyright 2024 The Ebitengine Authors // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -32,10 +32,6 @@ const ( var ( dsts = [8]*ebiten.Image{ - /*ebiten.NewImage(dstSize, dstSize), - ebiten.NewImage(dstSize, dstSize), - ebiten.NewImage(dstSize, dstSize), - ebiten.NewImage(dstSize, dstSize),*/ ebiten.NewImageWithOptions(image.Rect(0, 0, dstSize, dstSize), &ebiten.NewImageOptions{ Unmanaged: true, }), From ea462f4cb6742e7f9a124fbfe18d8bf1f2a357d9 Mon Sep 17 00:00:00 2001 From: Zyko <13394516+Zyko0@users.noreply.github.com> Date: Fri, 14 Jun 2024 20:20:15 +0200 Subject: [PATCH 36/36] Fixed comment --- internal/graphicsdriver/directx/graphics11_windows.go | 2 +- internal/graphicsdriver/directx/graphics12_windows.go | 2 +- internal/graphicsdriver/opengl/graphics.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/graphicsdriver/directx/graphics11_windows.go b/internal/graphicsdriver/directx/graphics11_windows.go index ea5591fc3..e5f88a2aa 100644 --- a/internal/graphicsdriver/directx/graphics11_windows.go +++ b/internal/graphicsdriver/directx/graphics11_windows.go @@ -619,7 +619,7 @@ func (g *graphics11) DrawTriangles(dstIDs [graphics.ShaderDstImageCount]graphics srcs[i] = img } - // If the number of targets is more than one, or if the only target is the first one, then + // If the number of targets is more than one, or if the only target is not the first one, then // it is safe to assume that MRT is used. // Also, it only matters in order to specify empty targets/viewports when not all slots are // being filled, even though it's not a MRT scenario. diff --git a/internal/graphicsdriver/directx/graphics12_windows.go b/internal/graphicsdriver/directx/graphics12_windows.go index 5c7bfa7d5..066f78a88 100644 --- a/internal/graphicsdriver/directx/graphics12_windows.go +++ b/internal/graphicsdriver/directx/graphics12_windows.go @@ -1254,7 +1254,7 @@ func (g *graphics12) DrawTriangles(dstIDs [graphics.ShaderDstImageCount]graphics g.drawCommandList.ResourceBarrier(resourceBarriers) } - // If the number of targets is more than one, or if the only target is the first one, then + // If the number of targets is more than one, or if the only target is not the first one, then // it is safe to assume that MRT is used. // Also, it only matters in order to specify empty targets/viewports when not all slots are // being filled, even though it's not a MRT scenario. diff --git a/internal/graphicsdriver/opengl/graphics.go b/internal/graphicsdriver/opengl/graphics.go index 3dea16128..b816eb27a 100644 --- a/internal/graphicsdriver/opengl/graphics.go +++ b/internal/graphicsdriver/opengl/graphics.go @@ -226,7 +226,7 @@ func (g *Graphics) DrawTriangles(dstIDs [graphics.ShaderDstImageCount]graphicsdr } f := uint32(dsts[firstTarget].framebuffer.native) - // If the number of targets is more than one, or if the only target is the first one, then + // If the number of targets is more than one, or if the only target is not the first one, then // it is safe to assume that MRT is used. // Also, it only matters in order to specify empty targets/viewports when not all slots are // being filled.