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+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 {

View File

@ -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.

View File

@ -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
}