From b5e3756ee8d9b3cf725ac9eee2a5634c5ec74ba6 Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Mon, 20 Sep 2021 09:34:04 +0200 Subject: [PATCH] gpu/internal/driver: get rid of Device.MemoryBarrier MemoryBarrier is meant to stand in for OpenGL ES 3.1's glMemoryBarrier. However, it badly fits with the other backends: Metal and D3D11 have automatic memory barriers, and Vulkan needs barriers for graphics as well. This change removes MemoryBarrier, and puts the burden on the backends. The OpenGL backend simply adds a barrier between every compute dispatch. This change only adds a single memory barrier compared to the manual barriers before this change, which is unlikely to affect performance much.. We can revisit the automatic barriers if they ever become a performance problem. Signed-off-by: Elias Naur --- gpu/compute.go | 4 +--- gpu/internal/d3d11/d3d11_windows.go | 2 -- gpu/internal/driver/driver.go | 1 - gpu/internal/metal/metal_darwin.go | 2 -- gpu/internal/opengl/opengl.go | 6 ++---- 5 files changed, 3 insertions(+), 12 deletions(-) diff --git a/gpu/compute.go b/gpu/compute.go index 6de65c21..c72773c8 100644 --- a/gpu/compute.go +++ b/gpu/compute.go @@ -1340,9 +1340,7 @@ func (g *compute) render(images *textureAtlas, dst driver.Texture, cpuDst cpu.Im } func (g *compute) memoryBarrier() { - if !g.useCPU { - g.ctx.MemoryBarrier() - } else { + if g.useCPU { g.dispatcher.Barrier() } } diff --git a/gpu/internal/d3d11/d3d11_windows.go b/gpu/internal/d3d11/d3d11_windows.go index a30f6a23..08698c33 100644 --- a/gpu/internal/d3d11/d3d11_windows.go +++ b/gpu/internal/d3d11/d3d11_windows.go @@ -591,8 +591,6 @@ func (b *Backend) BindImageTexture(unit int, tex driver.Texture) { } } -func (b *Backend) MemoryBarrier() {} - func (b *Backend) DispatchCompute(x, y, z int) { b.ctx.CSSetShader(b.program.shader) b.ctx.Dispatch(uint32(x), uint32(y), uint32(z)) diff --git a/gpu/internal/driver/driver.go b/gpu/internal/driver/driver.go index b07cbb50..a5a27b9f 100644 --- a/gpu/internal/driver/driver.go +++ b/gpu/internal/driver/driver.go @@ -49,7 +49,6 @@ type Device interface { BeginCompute() EndCompute() CopyTexture(dst Texture, dstOrigin image.Point, src Texture, srcRect image.Rectangle) - MemoryBarrier() DispatchCompute(x, y, z int) Release() diff --git a/gpu/internal/metal/metal_darwin.go b/gpu/internal/metal/metal_darwin.go index 1c29fafb..b915ce5e 100644 --- a/gpu/internal/metal/metal_darwin.go +++ b/gpu/internal/metal/metal_darwin.go @@ -818,8 +818,6 @@ func (b *Backend) BindImageTexture(unit int, tex driver.Texture) { b.BindTexture(unit, tex) } -func (b *Backend) MemoryBarrier() {} - func (b *Backend) BeginCompute() { b.endEncoder() b.ensureCmdBuffer() diff --git a/gpu/internal/opengl/opengl.go b/gpu/internal/opengl/opengl.go index 7436b225..62c28ce4 100644 --- a/gpu/internal/opengl/opengl.go +++ b/gpu/internal/opengl/opengl.go @@ -760,10 +760,6 @@ func (b *Backend) Release() { *b = Backend{} } -func (b *Backend) MemoryBarrier() { - b.funcs.MemoryBarrier(gl.ALL_BARRIER_BITS) -} - func (b *Backend) DispatchCompute(x, y, z int) { for binding, buf := range b.storage { if buf != nil { @@ -771,6 +767,7 @@ func (b *Backend) DispatchCompute(x, y, z int) { } } b.funcs.DispatchCompute(x, y, z) + b.funcs.MemoryBarrier(gl.ALL_BARRIER_BITS) } func (b *Backend) BindImageTexture(unit int, tex driver.Texture) { @@ -1157,6 +1154,7 @@ func (b *Backend) BindPipeline(pl driver.Pipeline) { } func (b *Backend) BeginCompute() { + b.funcs.MemoryBarrier(gl.ALL_BARRIER_BITS) } func (b *Backend) EndCompute() {