2020-05-29 21:36:24 +02:00
|
|
|
// Copyright 2020 The Ebiten Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package ebiten
|
|
|
|
|
|
|
|
import (
|
2022-10-02 07:51:01 +02:00
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/builtinshader"
|
2022-04-03 19:15:33 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphics"
|
2023-07-31 18:24:23 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/shaderir"
|
2022-03-19 17:24:47 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/ui"
|
2020-05-29 21:36:24 +02:00
|
|
|
)
|
|
|
|
|
2020-09-23 10:13:18 +02:00
|
|
|
// Shader represents a compiled shader program.
|
|
|
|
//
|
2022-09-23 12:08:35 +02:00
|
|
|
// For the details about the shader, see https://ebitengine.org/en/documents/shader.html.
|
2020-05-29 21:36:24 +02:00
|
|
|
type Shader struct {
|
2022-03-19 18:00:44 +01:00
|
|
|
shader *ui.Shader
|
2023-07-31 18:24:23 +02:00
|
|
|
unit shaderir.Unit
|
2020-05-29 21:36:24 +02:00
|
|
|
}
|
|
|
|
|
2023-01-28 11:06:38 +01:00
|
|
|
// NewShader compiles a shader program in the shading language Kage, and returns the result.
|
2020-09-23 10:13:18 +02:00
|
|
|
//
|
2022-04-03 19:15:33 +02:00
|
|
|
// If the compilation fails, NewShader returns an error.
|
2020-09-23 10:13:18 +02:00
|
|
|
//
|
2022-09-23 12:08:35 +02:00
|
|
|
// For the details about the shader, see https://ebitengine.org/en/documents/shader.html.
|
2020-07-17 18:09:58 +02:00
|
|
|
func NewShader(src []byte) (*Shader, error) {
|
2022-04-03 19:15:33 +02:00
|
|
|
ir, err := graphics.CompileShader(src)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-29 21:36:24 +02:00
|
|
|
return &Shader{
|
2022-08-26 10:03:17 +02:00
|
|
|
shader: ui.NewShader(ir),
|
2023-07-31 18:24:23 +02:00
|
|
|
unit: ir.Unit,
|
2020-05-29 21:36:24 +02:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-09-23 10:13:18 +02:00
|
|
|
// Dispose disposes the shader program.
|
|
|
|
// After disposing, the shader is no longer available.
|
2023-11-03 08:05:41 +01:00
|
|
|
//
|
|
|
|
// Deprecated: as of v2.7. Use Deallocate instead.
|
2020-05-29 21:36:24 +02:00
|
|
|
func (s *Shader) Dispose() {
|
2023-11-03 08:05:41 +01:00
|
|
|
s.shader.Deallocate()
|
2020-05-29 21:36:24 +02:00
|
|
|
s.shader = nil
|
|
|
|
}
|
2022-04-03 19:15:33 +02:00
|
|
|
|
2023-11-03 16:28:14 +01:00
|
|
|
func (s *Shader) isDisposed() bool {
|
|
|
|
return s.shader == nil
|
|
|
|
}
|
|
|
|
|
2023-11-03 07:06:22 +01:00
|
|
|
// Deallocate deallocates the internal state of the shader.
|
2023-11-03 08:05:41 +01:00
|
|
|
// Even after Deallocate is called, the shader is still available.
|
|
|
|
// In this case, the shader's internal state is allocated again.
|
|
|
|
//
|
|
|
|
// Usually, you don't have to call Deallocate since the internal state is automatically released by GC.
|
|
|
|
// However, if you are sure that the shader is no longer used but not sure how this shader object is referred,
|
|
|
|
// you can call Deallocate to make sure that the internal state is deallocated.
|
2023-11-03 07:06:22 +01:00
|
|
|
//
|
|
|
|
// If the shader is disposed, Deallocate does nothing.
|
2023-11-03 08:05:41 +01:00
|
|
|
func (s *Shader) Deallocate() {
|
2023-11-03 07:06:22 +01:00
|
|
|
if s.shader == nil {
|
|
|
|
return
|
|
|
|
}
|
2023-11-03 08:05:41 +01:00
|
|
|
s.shader.Deallocate()
|
|
|
|
}
|
|
|
|
|
2022-12-08 16:02:04 +01:00
|
|
|
func (s *Shader) appendUniforms(dst []uint32, uniforms map[string]any) []uint32 {
|
|
|
|
return s.shader.AppendUniforms(dst, uniforms)
|
2022-04-03 19:15:33 +02:00
|
|
|
}
|
2022-10-02 07:51:01 +02:00
|
|
|
|
|
|
|
var (
|
2023-08-19 21:58:36 +02:00
|
|
|
builtinShaders [builtinshader.FilterCount][builtinshader.AddressCount][2]*Shader
|
2022-10-02 07:51:01 +02:00
|
|
|
builtinShadersM sync.Mutex
|
|
|
|
)
|
|
|
|
|
2022-10-02 16:24:15 +02:00
|
|
|
func builtinShader(filter builtinshader.Filter, address builtinshader.Address, useColorM bool) *Shader {
|
2022-10-02 07:51:01 +02:00
|
|
|
builtinShadersM.Lock()
|
|
|
|
defer builtinShadersM.Unlock()
|
|
|
|
|
2023-08-19 21:58:36 +02:00
|
|
|
var c int
|
|
|
|
if useColorM {
|
|
|
|
c = 1
|
2022-10-02 07:51:01 +02:00
|
|
|
}
|
2023-08-19 21:58:36 +02:00
|
|
|
if s := builtinShaders[filter][address][c]; s != nil {
|
2022-10-02 07:51:01 +02:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2022-10-02 13:33:43 +02:00
|
|
|
var shader *Shader
|
2022-10-02 16:24:15 +02:00
|
|
|
if address == builtinshader.AddressUnsafe && !useColorM {
|
2022-10-02 13:33:43 +02:00
|
|
|
switch filter {
|
2022-10-02 16:24:15 +02:00
|
|
|
case builtinshader.FilterNearest:
|
2022-10-02 13:33:43 +02:00
|
|
|
shader = &Shader{shader: ui.NearestFilterShader}
|
2022-10-02 16:24:15 +02:00
|
|
|
case builtinshader.FilterLinear:
|
2022-10-02 13:33:43 +02:00
|
|
|
shader = &Shader{shader: ui.LinearFilterShader}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
src := builtinshader.Shader(filter, address, useColorM)
|
|
|
|
s, err := NewShader(src)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("ebiten: NewShader for a built-in shader failed: %v", err))
|
|
|
|
}
|
|
|
|
shader = s
|
2022-10-02 07:51:01 +02:00
|
|
|
}
|
2022-10-02 13:33:43 +02:00
|
|
|
|
2023-08-19 21:58:36 +02:00
|
|
|
builtinShaders[filter][address][c] = shader
|
2022-10-02 13:33:43 +02:00
|
|
|
return shader
|
2022-10-02 07:51:01 +02:00
|
|
|
}
|