shader.go (1509B)
1 // Copyright 2020 The Ebiten Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package shareable 16 17 import ( 18 "runtime" 19 20 "github.com/hajimehoshi/ebiten/v2/internal/restorable" 21 "github.com/hajimehoshi/ebiten/v2/internal/shaderir" 22 ) 23 24 type Shader struct { 25 shader *restorable.Shader 26 } 27 28 func NewShader(program *shaderir.Program) *Shader { 29 s := &Shader{ 30 shader: restorable.NewShader(program), 31 } 32 runtime.SetFinalizer(s, (*Shader).MarkDisposed) 33 return s 34 } 35 36 // MarkDisposed marks the shader as disposed. The actual operation is deferred. 37 // MarkDisposed can be called from finalizers. 38 // 39 // A function from finalizer must not be blocked, but disposing operation can be blocked. 40 // Defer this operation until it becomes safe. (#913) 41 func (s *Shader) MarkDisposed() { 42 deferredM.Lock() 43 deferred = append(deferred, func() { 44 s.dispose() 45 }) 46 deferredM.Unlock() 47 } 48 49 func (s *Shader) dispose() { 50 runtime.SetFinalizer(s, nil) 51 if s.shader == nil { 52 return 53 } 54 s.shader.Dispose() 55 s.shader = nil 56 }