Skip to content

Commit

Permalink
Performance optimization for the path without a shader
Browse files Browse the repository at this point in the history
  • Loading branch information
hajimehoshi committed Sep 20, 2020
1 parent cb73230 commit f3ef2e2
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 48 deletions.
30 changes: 14 additions & 16 deletions internal/buffered/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,26 +265,24 @@ func (i *Image) DrawTriangles(srcs [graphics.ShaderImageNum]*Image, vertices []f
}
}

for _, src := range srcs {
if src == nil {
continue
}
src.resolvePendingPixels(true)
}
i.resolvePendingPixels(false)

var s *shareable.Shader
if shader != nil {
s = shader.shader
}

var imgs [graphics.ShaderImageNum]*shareable.Image
for i, img := range srcs {
if img == nil {
continue
if shader == nil {
// Fast path for rendering without a shader (#1355).
img := srcs[0]
img.resolvePendingPixels(true)
imgs[0] = img.img
} else {
for i, img := range srcs {
if img == nil {
continue
}
img.resolvePendingPixels(true)
imgs[i] = img.img
}
imgs[i] = img.img
s = shader.shader
}
i.resolvePendingPixels(false)

i.img.DrawTriangles(imgs, vertices, indices, colorm, mode, filter, address, sourceRegion, subimageOffsets, s, uniforms)
i.invalidatePendingPixels()
Expand Down
21 changes: 15 additions & 6 deletions internal/graphicscommand/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,23 @@ func (i *Image) DrawTriangles(srcs [graphics.ShaderImageNum]*Image, offsets [gra
}
}

for _, src := range srcs {
if src == nil {
continue
}
if src.screen {
if shader == nil {
// Fast path for rendering without a shader (#1355).
img := srcs[0]
if img.screen {
panic("graphicscommand: the screen image cannot be the rendering source")
}
src.resolveBufferedReplacePixels()
img.resolveBufferedReplacePixels()
} else {
for _, src := range srcs {
if src == nil {
continue
}
if src.screen {
panic("graphicscommand: the screen image cannot be the rendering source")
}
src.resolveBufferedReplacePixels()
}
}
i.resolveBufferedReplacePixels()

Expand Down
19 changes: 11 additions & 8 deletions internal/restorable/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,17 +389,20 @@ func (i *Image) DrawTriangles(srcs [graphics.ShaderImageNum]*Image, offsets [gra
} else {
i.appendDrawTrianglesHistory(srcs, offsets, vertices, indices, colorm, mode, filter, address, sourceRegion, shader, uniforms)
}
var s *graphicscommand.Shader
if shader != nil {
s = shader.shader
}

var s *graphicscommand.Shader
var imgs [graphics.ShaderImageNum]*graphicscommand.Image
for i, src := range srcs {
if src == nil {
continue
if shader == nil {
// Fast path for rendering without a shader (#1355).
imgs[0] = srcs[0].image
} else {
for i, src := range srcs {
if src == nil {
continue
}
imgs[i] = src.image
}
imgs[i] = src.image
s = shader.shader
}
i.image.DrawTriangles(imgs, offsets, vertices, indices, colorm, mode, filter, address, sourceRegion, s, uniforms)
}
Expand Down
37 changes: 19 additions & 18 deletions internal/shareable/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,27 +354,28 @@ func (i *Image) DrawTriangles(srcs [graphics.ShaderImageNum]*Image, vertices []f
}

var offsets [graphics.ShaderImageNum - 1][2]float32
for i, subimageOffset := range subimageOffsets {
src := srcs[i+1]
if src == nil {
continue
}
ox, oy, _, _ := src.regionWithPadding()
offsets[i][0] = float32(ox) + paddingSize - oxf + subimageOffset[0]
offsets[i][1] = float32(oy) + paddingSize - oyf + subimageOffset[1]
}

var s *restorable.Shader
if shader != nil {
s = shader.shader
}

var imgs [graphics.ShaderImageNum]*restorable.Image
for i, src := range srcs {
if src == nil {
continue
if shader == nil {
// Fast path for rendering without a shader (#1355).
imgs[0] = srcs[0].backend.restorable
} else {
for i, subimageOffset := range subimageOffsets {
src := srcs[i+1]
if src == nil {
continue
}
ox, oy, _, _ := src.regionWithPadding()
offsets[i][0] = float32(ox) + paddingSize - oxf + subimageOffset[0]
offsets[i][1] = float32(oy) + paddingSize - oyf + subimageOffset[1]
}
s = shader.shader
for i, src := range srcs {
if src == nil {
continue
}
imgs[i] = src.backend.restorable
}
imgs[i] = src.backend.restorable
}

i.backend.restorable.DrawTriangles(imgs, offsets, vertices, indices, colorm, mode, filter, address, sourceRegion, s, uniforms)
Expand Down

0 comments on commit f3ef2e2

Please sign in to comment.