gpu: introduce driver.Device.PrepareTexture and use it in renderers

Vulkan textures (VkImage) are always in a particular layout, where each
layout is optimized for a particular use (transfer, sampling, compute
storage). Vulkan allows layout transitions everywhere except inside
render passes. This change adds driver.Device.PrepareTexture for
instructing the driver to switch a texture to a layout for sampling
in preparation for using it in a render pass.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2021-09-16 14:32:07 +02:00
parent 0ba984c19c
commit b599a6d1c5
9 changed files with 60 additions and 24 deletions
+5 -7
View File
@@ -862,6 +862,7 @@ func (g *compute) blitLayers(d driver.LoadDesc, fbo driver.Framebuffer, viewport
{posX: float32(r.Min.X), posY: float32(r.Max.Y), u: placef.X, v: placef.Y + sizef.Y},
}
g.output.layerVertices = append(g.output.layerVertices, quad[0], quad[1], quad[3], quad[3], quad[2], quad[1])
g.ctx.PrepareTexture(l.alloc.atlas.image)
}
if len(g.output.layerVertices) > 0 {
vertexData := byteslice.Slice(g.output.layerVertices)
@@ -893,9 +894,6 @@ func (g *compute) blitLayers(d driver.LoadDesc, fbo driver.Framebuffer, viewport
// Transform positions to clip space: [-1, -1] - [1, 1], and texture
// coordinates to texture space: [0, 0] - [1, 1].
clip := f32.Affine2D{}.Scale(f32.Pt(0, 0), f32.Pt(2/float32(viewport.X), 2/float32(viewport.Y))).Offset(f32.Pt(-1, -1))
// Flip y-axis to match framebuffer output space.
flipY := f32.Affine2D{}.Scale(f32.Pt(0, 0), f32.Pt(1, -1)).Offset(f32.Pt(0, float32(viewport.Y)))
clip = clip.Mul(flipY)
sx, _, ox, _, sy, oy := clip.Elems()
g.output.uniforms.scale = [2]float32{sx, sy}
g.output.uniforms.pos = [2]float32{ox, oy}
@@ -992,11 +990,10 @@ func (g *compute) renderMaterials() error {
if err := g.realizeAtlas(atlas, g.useCPU, atlas.packer.sizes[0]); err != nil {
return err
}
// Transform to clip space: [-1, -1] - [1, 1] and flip Y-axis to cancel the implied transformation
// between framebuffer and texture space.
// Transform to clip space: [-1, -1] - [1, 1].
*m.uniforms.u = materialUniforms{
scale: [2]float32{2, -2},
pos: [2]float32{-1, +1},
scale: [2]float32{2, 2},
pos: [2]float32{-1, -1},
}
if !g.srgb {
m.uniforms.u.emulatesRGB = 1.0
@@ -1010,6 +1007,7 @@ func (g *compute) renderMaterials() error {
if !realized {
d.Action = driver.LoadActionClear
}
g.ctx.PrepareTexture(imgAtlas.image)
g.ctx.BeginRenderPass(atlas.fbo, d)
g.ctx.BindTexture(0, imgAtlas.image)
g.ctx.BindPipeline(m.pipeline)