2020-08-03 17:56:45 +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 metal
|
|
|
|
|
|
|
|
import (
|
2020-08-08 21:06:30 +02:00
|
|
|
"fmt"
|
2024-04-20 16:48:33 +02:00
|
|
|
"sync"
|
2020-08-08 21:06:30 +02:00
|
|
|
|
2022-02-06 12:41:32 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/metal/mtl"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/shaderir"
|
2022-03-09 16:08:36 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/shaderir/msl"
|
2020-08-03 17:56:45 +02:00
|
|
|
)
|
|
|
|
|
2024-04-20 16:48:33 +02:00
|
|
|
type precompiledLibraries struct {
|
|
|
|
binaries map[shaderir.SourceHash][]byte
|
|
|
|
m sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *precompiledLibraries) put(hash shaderir.SourceHash, bin []byte) {
|
|
|
|
c.m.Lock()
|
|
|
|
defer c.m.Unlock()
|
|
|
|
|
|
|
|
if c.binaries == nil {
|
|
|
|
c.binaries = map[shaderir.SourceHash][]byte{}
|
|
|
|
}
|
|
|
|
if _, ok := c.binaries[hash]; ok {
|
|
|
|
panic(fmt.Sprintf("metal: the precompiled library for the hash %s is already registered", hash.String()))
|
|
|
|
}
|
|
|
|
c.binaries[hash] = bin
|
|
|
|
}
|
|
|
|
|
2024-05-05 10:18:06 +02:00
|
|
|
func (c *precompiledLibraries) get(hash shaderir.SourceHash) []byte {
|
2024-04-20 16:48:33 +02:00
|
|
|
c.m.Lock()
|
|
|
|
defer c.m.Unlock()
|
|
|
|
|
2024-05-05 10:18:06 +02:00
|
|
|
return c.binaries[hash]
|
2024-04-20 16:48:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var thePrecompiledLibraries precompiledLibraries
|
|
|
|
|
2024-05-26 15:45:12 +02:00
|
|
|
func RegisterPrecompiledLibrary(hash shaderir.SourceHash, bin []byte) {
|
|
|
|
thePrecompiledLibraries.put(hash, bin)
|
2024-04-20 16:48:33 +02:00
|
|
|
}
|
|
|
|
|
2021-07-02 12:26:09 +02:00
|
|
|
type shaderRpsKey struct {
|
2022-10-15 11:58:56 +02:00
|
|
|
blend graphicsdriver.Blend
|
|
|
|
stencilMode stencilMode
|
|
|
|
screen bool
|
2021-07-02 12:26:09 +02:00
|
|
|
}
|
|
|
|
|
2020-08-03 17:56:45 +02:00
|
|
|
type Shader struct {
|
2022-02-06 12:41:32 +01:00
|
|
|
id graphicsdriver.ShaderID
|
2020-08-03 17:56:45 +02:00
|
|
|
|
|
|
|
ir *shaderir.Program
|
2024-05-04 12:09:02 +02:00
|
|
|
lib mtl.Library
|
2020-08-03 17:56:45 +02:00
|
|
|
fs mtl.Function
|
|
|
|
vs mtl.Function
|
2021-07-02 12:26:09 +02:00
|
|
|
rpss map[shaderRpsKey]mtl.RenderPipelineState
|
2024-04-20 16:48:33 +02:00
|
|
|
|
|
|
|
libraryPrecompiled bool
|
2020-08-03 17:56:45 +02:00
|
|
|
}
|
|
|
|
|
2022-02-06 12:41:32 +01:00
|
|
|
func newShader(device mtl.Device, id graphicsdriver.ShaderID, program *shaderir.Program) (*Shader, error) {
|
2020-08-03 17:56:45 +02:00
|
|
|
s := &Shader{
|
|
|
|
id: id,
|
|
|
|
ir: program,
|
2021-07-02 12:26:09 +02:00
|
|
|
rpss: map[shaderRpsKey]mtl.RenderPipelineState{},
|
2020-08-03 17:56:45 +02:00
|
|
|
}
|
|
|
|
if err := s.init(device); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2022-02-06 12:41:32 +01:00
|
|
|
func (s *Shader) ID() graphicsdriver.ShaderID {
|
2020-08-03 17:56:45 +02:00
|
|
|
return s.id
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Shader) Dispose() {
|
|
|
|
for _, rps := range s.rpss {
|
|
|
|
rps.Release()
|
|
|
|
}
|
|
|
|
s.vs.Release()
|
|
|
|
s.fs.Release()
|
2024-04-20 16:48:33 +02:00
|
|
|
// Do not release s.lib if this is precompiled. This is a shared precompiled library.
|
|
|
|
if !s.libraryPrecompiled {
|
|
|
|
s.lib.Release()
|
|
|
|
}
|
2020-08-03 17:56:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Shader) init(device mtl.Device) error {
|
2024-04-20 16:48:33 +02:00
|
|
|
var src string
|
2024-05-05 10:18:06 +02:00
|
|
|
if libBin := thePrecompiledLibraries.get(s.ir.SourceHash); len(libBin) > 0 {
|
2024-05-06 14:38:59 +02:00
|
|
|
lib, err := device.NewLibraryWithData(libBin)
|
2024-04-20 16:48:33 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.lib = lib
|
|
|
|
} else {
|
|
|
|
src = msl.Compile(s.ir)
|
2024-05-06 14:38:59 +02:00
|
|
|
lib, err := device.NewLibraryWithSource(src, mtl.CompileOptions{})
|
2024-04-20 16:48:33 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("metal: device.MakeLibrary failed: %w, source: %s", err, src)
|
|
|
|
}
|
|
|
|
s.lib = lib
|
2020-08-03 17:56:45 +02:00
|
|
|
}
|
2024-05-04 12:09:02 +02:00
|
|
|
|
2024-05-06 14:38:59 +02:00
|
|
|
vs, err := s.lib.NewFunctionWithName(msl.VertexName)
|
2020-08-03 17:56:45 +02:00
|
|
|
if err != nil {
|
2024-04-20 16:48:33 +02:00
|
|
|
if src != "" {
|
|
|
|
return fmt.Errorf("metal: lib.MakeFunction for vertex failed: %w, source: %s", err, src)
|
|
|
|
}
|
|
|
|
return fmt.Errorf("metal: lib.MakeFunction for vertex failed: %w", err)
|
2020-08-03 17:56:45 +02:00
|
|
|
}
|
2024-05-06 14:38:59 +02:00
|
|
|
fs, err := s.lib.NewFunctionWithName(msl.FragmentName)
|
2020-08-03 17:56:45 +02:00
|
|
|
if err != nil {
|
2024-04-20 16:48:33 +02:00
|
|
|
if src != "" {
|
|
|
|
return fmt.Errorf("metal: lib.MakeFunction for fragment failed: %w, source: %s", err, src)
|
|
|
|
}
|
|
|
|
return fmt.Errorf("metal: lib.MakeFunction for fragment failed: %w", err)
|
2020-08-03 17:56:45 +02:00
|
|
|
}
|
|
|
|
s.fs = fs
|
|
|
|
s.vs = vs
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-15 11:58:56 +02:00
|
|
|
func (s *Shader) RenderPipelineState(view *view, blend graphicsdriver.Blend, stencilMode stencilMode, screen bool) (mtl.RenderPipelineState, error) {
|
2022-09-06 11:21:33 +02:00
|
|
|
key := shaderRpsKey{
|
2022-10-15 11:58:56 +02:00
|
|
|
blend: blend,
|
|
|
|
stencilMode: stencilMode,
|
|
|
|
screen: screen,
|
2022-09-06 11:21:33 +02:00
|
|
|
}
|
|
|
|
if rps, ok := s.rpss[key]; ok {
|
2020-08-03 17:56:45 +02:00
|
|
|
return rps, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
rpld := mtl.RenderPipelineDescriptor{
|
2021-07-06 07:05:01 +02:00
|
|
|
VertexFunction: s.vs,
|
|
|
|
FragmentFunction: s.fs,
|
|
|
|
}
|
|
|
|
if stencilMode != noStencil {
|
|
|
|
rpld.StencilAttachmentPixelFormat = mtl.PixelFormatStencil8
|
2020-08-03 17:56:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: For the precise pixel format, whether the render target is the screen or not must be considered.
|
2022-09-06 11:21:33 +02:00
|
|
|
pix := mtl.PixelFormatRGBA8UNorm
|
|
|
|
if screen {
|
|
|
|
pix = view.colorPixelFormat()
|
|
|
|
}
|
|
|
|
rpld.ColorAttachments[0].PixelFormat = pix
|
2020-08-03 17:56:45 +02:00
|
|
|
rpld.ColorAttachments[0].BlendingEnabled = true
|
|
|
|
|
2022-10-15 11:58:56 +02:00
|
|
|
rpld.ColorAttachments[0].DestinationAlphaBlendFactor = blendFactorToMetalBlendFactor(blend.BlendFactorDestinationAlpha)
|
2022-10-16 17:49:56 +02:00
|
|
|
rpld.ColorAttachments[0].DestinationRGBBlendFactor = blendFactorToMetalBlendFactor(blend.BlendFactorDestinationRGB)
|
2022-10-15 11:58:56 +02:00
|
|
|
rpld.ColorAttachments[0].SourceAlphaBlendFactor = blendFactorToMetalBlendFactor(blend.BlendFactorSourceAlpha)
|
2022-10-16 17:49:56 +02:00
|
|
|
rpld.ColorAttachments[0].SourceRGBBlendFactor = blendFactorToMetalBlendFactor(blend.BlendFactorSourceRGB)
|
2022-10-15 11:58:56 +02:00
|
|
|
rpld.ColorAttachments[0].AlphaBlendOperation = blendOperationToMetalBlendOperation(blend.BlendOperationAlpha)
|
2022-10-16 17:49:56 +02:00
|
|
|
rpld.ColorAttachments[0].RGBBlendOperation = blendOperationToMetalBlendOperation(blend.BlendOperationRGB)
|
2022-10-15 11:58:56 +02:00
|
|
|
|
2023-11-06 01:18:08 +01:00
|
|
|
if stencilMode == noStencil || stencilMode == drawWithStencil {
|
2021-07-06 07:05:01 +02:00
|
|
|
rpld.ColorAttachments[0].WriteMask = mtl.ColorWriteMaskAll
|
2023-11-06 01:18:08 +01:00
|
|
|
} else {
|
|
|
|
rpld.ColorAttachments[0].WriteMask = mtl.ColorWriteMaskNone
|
2021-07-02 12:26:09 +02:00
|
|
|
}
|
2020-08-03 17:56:45 +02:00
|
|
|
|
2024-05-06 14:38:59 +02:00
|
|
|
rps, err := view.getMTLDevice().NewRenderPipelineStateWithDescriptor(rpld)
|
2020-08-03 17:56:45 +02:00
|
|
|
if err != nil {
|
|
|
|
return mtl.RenderPipelineState{}, err
|
|
|
|
}
|
|
|
|
|
2022-09-06 11:21:33 +02:00
|
|
|
s.rpss[key] = rps
|
2020-08-03 17:56:45 +02:00
|
|
|
return rps, nil
|
|
|
|
}
|