Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
hajimehoshi committed Aug 27, 2023
1 parent 4b98752 commit df16f36
Show file tree
Hide file tree
Showing 15 changed files with 228 additions and 225 deletions.
64 changes: 23 additions & 41 deletions image.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ func (i *Image) DrawImage(img *Image, options *DrawImageOptions) {
})
}

i.image.DrawTriangles(srcs, vs, is, blend, i.adjustedRegion(), img.adjustedRegion(), [graphics.ShaderImageCount - 1][2]float32{}, shader.shader, i.tmpUniforms, false, canSkipMipmap(geoM, filter), false)
i.image.DrawTriangles(srcs, vs, is, blend, i.adjustedRegion(), [graphics.ShaderImageCount]graphicsdriver.Region{img.adjustedRegion()}, shader.shader, i.tmpUniforms, false, canSkipMipmap(geoM, filter), false)
}

// Vertex represents a vertex passed to DrawTriangles.
Expand Down Expand Up @@ -515,7 +515,7 @@ func (i *Image) DrawTriangles(vertices []Vertex, indices []uint16, img *Image, o
})
}

i.image.DrawTriangles(srcs, vs, is, blend, i.adjustedRegion(), img.adjustedRegion(), [graphics.ShaderImageCount - 1][2]float32{}, shader.shader, i.tmpUniforms, options.FillRule == EvenOdd, filter != builtinshader.FilterLinear, options.AntiAlias)
i.image.DrawTriangles(srcs, vs, is, blend, i.adjustedRegion(), [graphics.ShaderImageCount]graphicsdriver.Region{img.adjustedRegion()}, shader.shader, i.tmpUniforms, options.FillRule == EvenOdd, filter != builtinshader.FilterLinear, options.AntiAlias)
}

// DrawTrianglesShaderOptions represents options for DrawTrianglesShader.
Expand Down Expand Up @@ -659,28 +659,18 @@ func (i *Image) DrawTrianglesShader(vertices []Vertex, indices []uint16, shader
imgs[i] = img.image
}

var sr graphicsdriver.Region
if img := options.Images[0]; img != nil {
sr = img.adjustedRegion()
}

var offsets [graphics.ShaderImageCount - 1][2]float32
for i, img := range options.Images[1:] {
var srcRegions [graphics.ShaderImageCount]graphicsdriver.Region
for i, img := range options.Images {
if img == nil {
continue
}
b := img.Bounds()
x, y := img.adjustPosition(b.Min.X, b.Min.Y)
// (sr.X, sr.Y) is the upper-left position of the first image.
// Calculate the distance between the current image's upper-left position and the first one's.
offsets[i][0] = float32(x) - sr.X
offsets[i][1] = float32(y) - sr.Y
srcRegions[i] = img.adjustedRegion()
}

i.tmpUniforms = i.tmpUniforms[:0]
i.tmpUniforms = shader.appendUniforms(i.tmpUniforms, options.Uniforms)

i.image.DrawTriangles(imgs, vs, is, blend, i.adjustedRegion(), sr, offsets, shader.shader, i.tmpUniforms, options.FillRule == EvenOdd, true, options.AntiAlias)
i.image.DrawTriangles(imgs, vs, is, blend, i.adjustedRegion(), srcRegions, shader.shader, i.tmpUniforms, options.FillRule == EvenOdd, true, options.AntiAlias)
}

// DrawRectShaderOptions represents options for DrawRectShader.
Expand Down Expand Up @@ -775,16 +765,20 @@ func (i *Image) DrawRectShader(width, height int, shader *Shader, options *DrawR
imgs[i] = img.image
}

var sr graphicsdriver.Region
if img := options.Images[0]; img != nil {
sr = img.adjustedRegion()
} else if shader.unit == shaderir.Pixels {
// Give the source size as pixels only when the unit is pixels so that users can get the source size via imageSrcRegionOnTexture (#2166).
// With the texel mode, the imageSrcRegionOnTexture values should be in texels so the source position in pixels would not match.
sr = graphicsdriver.Region{
Width: float32(width),
Height: float32(height),
var srcRegions [graphics.ShaderImageCount]graphicsdriver.Region
for i, img := range options.Images {
if img == nil {
if shader.unit == shaderir.Pixels && i == 0 {
// Give the source size as pixels only when the unit is pixels so that users can get the source size via imageSrcRegionOnTexture (#2166).
// With the texel mode, the imageSrcRegionOnTexture values should be in texels so the source position in pixels would not match.
srcRegions[i] = graphicsdriver.Region{
Width: float32(width),
Height: float32(height),
}
}
continue
}
srcRegions[i] = img.adjustedRegion()
}

geoM := options.GeoM
Expand All @@ -794,27 +788,15 @@ func (i *Image) DrawRectShader(width, height int, shader *Shader, options *DrawR
a, b, c, d, tx, ty := geoM.elements32()
cr, cg, cb, ca := options.ColorScale.elements()
vs := i.ensureTmpVertices(4 * graphics.VertexFloatCount)
// Do not use sr.Width and sr.Height as these might be empty.
graphics.QuadVertices(vs, sr.X, sr.Y, sr.X+float32(width), sr.Y+float32(height), a, b, c, d, tx, ty, cr, cg, cb, ca)
is := graphics.QuadIndices()

var offsets [graphics.ShaderImageCount - 1][2]float32
for i, img := range options.Images[1:] {
if img == nil {
continue
}
b := img.Bounds()
x, y := img.adjustPosition(b.Min.X, b.Min.Y)
// (sr.X, sr.Y) is the upper-left position of the first image.
// Calculate the distance between the current image's upper-left position and the first one's.
offsets[i][0] = float32(x) - sr.X
offsets[i][1] = float32(y) - sr.Y
}
// Do not use srcRegions[0].Width and srcRegions[-].Height as these might be empty.
graphics.QuadVertices(vs, srcRegions[0].X, srcRegions[0].Y, srcRegions[0].X+float32(width), srcRegions[0].Y+float32(height), a, b, c, d, tx, ty, cr, cg, cb, ca)
is := graphics.QuadIndices()

i.tmpUniforms = i.tmpUniforms[:0]
i.tmpUniforms = shader.appendUniforms(i.tmpUniforms, options.Uniforms)

i.image.DrawTriangles(imgs, vs, is, blend, i.adjustedRegion(), sr, offsets, shader.shader, i.tmpUniforms, false, true, false)
i.image.DrawTriangles(imgs, vs, is, blend, i.adjustedRegion(), srcRegions, shader.shader, i.tmpUniforms, false, true, false)
}

// SubImage returns an image representing the portion of the image p visible through r.
Expand Down
36 changes: 18 additions & 18 deletions internal/atlas/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ func (i *Image) ensureIsolatedFromSource(backends []*backend) {
Height: h,
}

newI.drawTriangles([graphics.ShaderImageCount]*Image{i}, vs, is, graphicsdriver.BlendCopy, dr, graphicsdriver.Region{}, [graphics.ShaderImageCount - 1][2]float32{}, NearestFilterShader, nil, false, true)
newI.drawTriangles([graphics.ShaderImageCount]*Image{i}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]graphicsdriver.Region{}, NearestFilterShader, nil, false, true)
newI.moveTo(i)
}

Expand Down Expand Up @@ -317,7 +317,7 @@ func (i *Image) putOnSourceBackend(graphicsDriver graphicsdriver.Graphics) {
Width: w,
Height: h,
}
newI.drawTriangles([graphics.ShaderImageCount]*Image{i}, vs, is, graphicsdriver.BlendCopy, dr, graphicsdriver.Region{}, [graphics.ShaderImageCount - 1][2]float32{}, NearestFilterShader, nil, false, true)
newI.drawTriangles([graphics.ShaderImageCount]*Image{i}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]graphicsdriver.Region{}, NearestFilterShader, nil, false, true)

newI.moveTo(i)
i.usedAsSourceCount = 0
Expand Down Expand Up @@ -349,13 +349,13 @@ 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 []uint16, blend graphicsdriver.Blend, dstRegion, srcRegion graphicsdriver.Region, subimageOffsets [graphics.ShaderImageCount - 1][2]float32, shader *Shader, uniforms []uint32, evenOdd bool) {
func (i *Image) DrawTriangles(srcs [graphics.ShaderImageCount]*Image, vertices []float32, indices []uint16, blend graphicsdriver.Blend, dstRegion graphicsdriver.Region, srcRegions [graphics.ShaderImageCount]graphicsdriver.Region, shader *Shader, uniforms []uint32, evenOdd bool) {
backendsM.Lock()
defer backendsM.Unlock()
i.drawTriangles(srcs, vertices, indices, blend, dstRegion, srcRegion, subimageOffsets, shader, uniforms, evenOdd, false)
i.drawTriangles(srcs, vertices, indices, blend, dstRegion, srcRegions, shader, uniforms, evenOdd, false)
}

func (i *Image) drawTriangles(srcs [graphics.ShaderImageCount]*Image, vertices []float32, indices []uint16, blend graphicsdriver.Blend, dstRegion, srcRegion graphicsdriver.Region, subimageOffsets [graphics.ShaderImageCount - 1][2]float32, shader *Shader, uniforms []uint32, evenOdd bool, keepOnAtlas bool) {
func (i *Image) drawTriangles(srcs [graphics.ShaderImageCount]*Image, vertices []float32, indices []uint16, blend graphicsdriver.Blend, dstRegion graphicsdriver.Region, srcRegions [graphics.ShaderImageCount]graphicsdriver.Region, shader *Shader, uniforms []uint32, evenOdd bool, keepOnAtlas bool) {
if i.disposed {
panic("atlas: the drawing target image must not be disposed (DrawTriangles)")
}
Expand Down Expand Up @@ -413,12 +413,6 @@ func (i *Image) drawTriangles(srcs [graphics.ShaderImageCount]*Image, vertices [
vertices[i+3] /= shf
}
}
// srcRegion can be deliberately empty when this is not needed in order to avoid unexpected
// performance issue (#1293).
if srcRegion.Width != 0 && srcRegion.Height != 0 {
srcRegion.X += oxf
srcRegion.Y += oyf
}
} else {
n := len(vertices)
for i := 0; i < n; i += graphics.VertexFloatCount {
Expand All @@ -427,25 +421,31 @@ func (i *Image) drawTriangles(srcs [graphics.ShaderImageCount]*Image, vertices [
}
}

var offsets [graphics.ShaderImageCount - 1][2]float32
var imgs [graphics.ShaderImageCount]*restorable.Image
for i, subimageOffset := range subimageOffsets {
src := srcs[i+1]
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].Width == 0 || srcRegions[i].Height == 0 {
continue
}

r := src.regionWithPadding()
offsets[i][0] = float32(r.Min.X) - oxf + subimageOffset[0]
offsets[i][1] = float32(r.Min.Y) - oyf + subimageOffset[1]
srcRegions[i].X += float32(r.Min.X)
srcRegions[i].Y += float32(r.Min.Y)
}

var imgs [graphics.ShaderImageCount]*restorable.Image
for i, src := range srcs {
if src == nil {
continue
}
imgs[i] = src.backend.restorable
}

i.backend.restorable.DrawTriangles(imgs, offsets, vertices, indices, blend, dstRegion, srcRegion, shader.shader, uniforms, evenOdd)
i.backend.restorable.DrawTriangles(imgs, vertices, indices, blend, dstRegion, srcRegions, shader.shader, uniforms, evenOdd)

for _, src := range srcs {
if src == nil {
Expand Down
Loading

0 comments on commit df16f36

Please sign in to comment.