shader.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. package restorable
  15. import (
  16. "fmt"
  17. "golang.org/x/sync/errgroup"
  18. "github.com/hajimehoshi/ebiten/v2/internal/builtinshader"
  19. "github.com/hajimehoshi/ebiten/v2/internal/graphics"
  20. "github.com/hajimehoshi/ebiten/v2/internal/graphicscommand"
  21. "github.com/hajimehoshi/ebiten/v2/internal/shaderir"
  22. )
  23. type Shader struct {
  24. shader *graphicscommand.Shader
  25. ir *shaderir.Program
  26. name string
  27. }
  28. func NewShader(ir *shaderir.Program, name string) *Shader {
  29. s := &Shader{
  30. shader: graphicscommand.NewShader(ir, name),
  31. ir: ir,
  32. name: name,
  33. }
  34. theImages.addShader(s)
  35. return s
  36. }
  37. func (s *Shader) Dispose() {
  38. theImages.removeShader(s)
  39. s.shader.Dispose()
  40. s.shader = nil
  41. s.ir = nil
  42. }
  43. func (s *Shader) restore() {
  44. s.shader = graphicscommand.NewShader(s.ir, s.name)
  45. }
  46. func (s *Shader) Unit() shaderir.Unit {
  47. return s.ir.Unit
  48. }
  49. var (
  50. NearestFilterShader *Shader
  51. LinearFilterShader *Shader
  52. clearShader *Shader
  53. )
  54. func init() {
  55. var wg errgroup.Group
  56. var nearestIR, linearIR, clearIR *shaderir.Program
  57. wg.Go(func() error {
  58. ir, err := graphics.CompileShader([]byte(builtinshader.ShaderSource(builtinshader.FilterNearest, builtinshader.AddressUnsafe, false)))
  59. if err != nil {
  60. return fmt.Errorf("restorable: compiling the nearest shader failed: %w", err)
  61. }
  62. nearestIR = ir
  63. return nil
  64. })
  65. wg.Go(func() error {
  66. ir, err := graphics.CompileShader([]byte(builtinshader.ShaderSource(builtinshader.FilterLinear, builtinshader.AddressUnsafe, false)))
  67. if err != nil {
  68. return fmt.Errorf("restorable: compiling the linear shader failed: %w", err)
  69. }
  70. linearIR = ir
  71. return nil
  72. })
  73. wg.Go(func() error {
  74. ir, err := graphics.CompileShader([]byte(builtinshader.ClearShaderSource))
  75. if err != nil {
  76. return fmt.Errorf("restorable: compiling the clear shader failed: %w", err)
  77. }
  78. clearIR = ir
  79. return nil
  80. })
  81. if err := wg.Wait(); err != nil {
  82. panic(err)
  83. }
  84. NearestFilterShader = NewShader(nearestIR, "nearest")
  85. LinearFilterShader = NewShader(linearIR, "linear")
  86. clearShader = NewShader(clearIR, "clear")
  87. }