From 19ee79e2cb19b2a3aecb5ce60ec929ea9a90a58b Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Wed, 18 Oct 2023 23:27:26 +0900 Subject: [PATCH] internal/buffered: remove unnecessary deferred functions for shaders --- internal/atlas/image.go | 4 ++-- internal/atlas/shader.go | 20 +++++++++++--------- internal/buffered/image.go | 30 ++---------------------------- 3 files changed, 15 insertions(+), 39 deletions(-) diff --git a/internal/atlas/image.go b/internal/atlas/image.go index 77c6264ef..ec0b9535b 100644 --- a/internal/atlas/image.go +++ b/internal/atlas/image.go @@ -393,7 +393,7 @@ func (i *Image) drawTriangles(srcs [graphics.ShaderImageCount]*Image, vertices [ vertices[i+2] += oxf vertices[i+3] += oyf } - if shader.unit() == shaderir.Texels { + if shader.ensureShader().Unit() == shaderir.Texels { sw, sh := srcs[0].backend.restorable.InternalSize() swf, shf := float32(sw), float32(sh) for i := 0; i < n; i += graphics.VertexFloatCount { @@ -432,7 +432,7 @@ func (i *Image) drawTriangles(srcs [graphics.ShaderImageCount]*Image, vertices [ imgs[i] = src.backend.restorable } - i.backend.restorable.DrawTriangles(imgs, vertices, indices, blend, dstRegion, srcRegions, shader.shader, uniforms, evenOdd) + i.backend.restorable.DrawTriangles(imgs, vertices, indices, blend, dstRegion, srcRegions, shader.ensureShader(), uniforms, evenOdd) for _, src := range srcs { if src == nil { diff --git a/internal/atlas/shader.go b/internal/atlas/shader.go index 77585c10a..6790c0018 100644 --- a/internal/atlas/shader.go +++ b/internal/atlas/shader.go @@ -22,22 +22,24 @@ import ( ) type Shader struct { + ir *shaderir.Program shader *restorable.Shader } func NewShader(ir *shaderir.Program) *Shader { - backendsM.Lock() - defer backendsM.Unlock() - - s := &Shader{ - shader: restorable.NewShader(ir), + // A shader is initialized lazily, and the lock is not needed. + return &Shader{ + ir: ir, } - runtime.SetFinalizer(s, (*Shader).MarkDisposed) - return s } -func (s *Shader) unit() shaderir.Unit { - return s.shader.Unit() +func (s *Shader) ensureShader() *restorable.Shader { + if s.shader != nil { + return s.shader + } + s.shader = restorable.NewShader(s.ir) + s.ir = nil + return s.shader } // MarkDisposed marks the shader as disposed. The actual operation is deferred. diff --git a/internal/buffered/image.go b/internal/buffered/image.go index 878c006d5..bcf82229c 100644 --- a/internal/buffered/image.go +++ b/internal/buffered/image.go @@ -162,38 +162,12 @@ type Shader struct { } func NewShader(ir *shaderir.Program) *Shader { - s := &Shader{} - s.initialize(ir) - return s -} - -func (s *Shader) initialize(ir *shaderir.Program) { - if maybeCanAddDelayedCommand() { - if tryAddDelayedCommand(func() { - s.initializeImpl(ir) - }) { - return - } + return &Shader{ + shader: atlas.NewShader(ir), } - s.initializeImpl(ir) -} - -func (s *Shader) initializeImpl(ir *shaderir.Program) { - s.shader = atlas.NewShader(ir) } func (s *Shader) MarkDisposed() { - if maybeCanAddDelayedCommand() { - if tryAddDelayedCommand(func() { - s.markDisposedImpl() - }) { - return - } - } - s.markDisposedImpl() -} - -func (s *Shader) markDisposedImpl() { s.shader.MarkDisposed() s.shader = nil }