internal/buffered: remove unnecessary deferred functions for shaders

This commit is contained in:
Hajime Hoshi 2023-10-18 23:27:26 +09:00
parent 3f1b7e78b8
commit 19ee79e2cb
3 changed files with 15 additions and 39 deletions

View File

@ -393,7 +393,7 @@ func (i *Image) drawTriangles(srcs [graphics.ShaderImageCount]*Image, vertices [
vertices[i+2] += oxf vertices[i+2] += oxf
vertices[i+3] += oyf vertices[i+3] += oyf
} }
if shader.unit() == shaderir.Texels { if shader.ensureShader().Unit() == shaderir.Texels {
sw, sh := srcs[0].backend.restorable.InternalSize() sw, sh := srcs[0].backend.restorable.InternalSize()
swf, shf := float32(sw), float32(sh) swf, shf := float32(sw), float32(sh)
for i := 0; i < n; i += graphics.VertexFloatCount { 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 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 { for _, src := range srcs {
if src == nil { if src == nil {

View File

@ -22,22 +22,24 @@ import (
) )
type Shader struct { type Shader struct {
ir *shaderir.Program
shader *restorable.Shader shader *restorable.Shader
} }
func NewShader(ir *shaderir.Program) *Shader { func NewShader(ir *shaderir.Program) *Shader {
backendsM.Lock() // A shader is initialized lazily, and the lock is not needed.
defer backendsM.Unlock() return &Shader{
ir: ir,
s := &Shader{
shader: restorable.NewShader(ir),
} }
runtime.SetFinalizer(s, (*Shader).MarkDisposed)
return s
} }
func (s *Shader) unit() shaderir.Unit { func (s *Shader) ensureShader() *restorable.Shader {
return s.shader.Unit() 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. // MarkDisposed marks the shader as disposed. The actual operation is deferred.

View File

@ -162,38 +162,12 @@ type Shader struct {
} }
func NewShader(ir *shaderir.Program) *Shader { func NewShader(ir *shaderir.Program) *Shader {
s := &Shader{} return &Shader{
s.initialize(ir) shader: atlas.NewShader(ir),
return s
}
func (s *Shader) initialize(ir *shaderir.Program) {
if maybeCanAddDelayedCommand() {
if tryAddDelayedCommand(func() {
s.initializeImpl(ir)
}) {
return
} }
}
s.initializeImpl(ir)
}
func (s *Shader) initializeImpl(ir *shaderir.Program) {
s.shader = atlas.NewShader(ir)
} }
func (s *Shader) MarkDisposed() { func (s *Shader) MarkDisposed() {
if maybeCanAddDelayedCommand() {
if tryAddDelayedCommand(func() {
s.markDisposedImpl()
}) {
return
}
}
s.markDisposedImpl()
}
func (s *Shader) markDisposedImpl() {
s.shader.MarkDisposed() s.shader.MarkDisposed()
s.shader = nil s.shader = nil
} }