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"
|
|
|
|
|
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
|
|
|
)
|
|
|
|
|
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
|
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-05-04 12:09:02 +02:00
|
|
|
s.lib.Release()
|
2020-08-03 17:56:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Shader) init(device mtl.Device) error {
|
2024-01-07 10:52:26 +01:00
|
|
|
src := msl.Compile(s.ir)
|
2020-08-03 17:56:45 +02:00
|
|
|
lib, err := device.MakeLibrary(src, mtl.CompileOptions{})
|
|
|
|
if err != nil {
|
2023-12-24 14:29:53 +01:00
|
|
|
return fmt.Errorf("metal: device.MakeLibrary failed: %w, source: %s", err, src)
|
2020-08-03 17:56:45 +02:00
|
|
|
}
|
2024-05-04 12:09:02 +02:00
|
|
|
s.lib = lib
|
|
|
|
|
|
|
|
vs, err := s.lib.MakeFunction(msl.VertexName)
|
2020-08-03 17:56:45 +02:00
|
|
|
if err != nil {
|
2023-12-24 14:29:53 +01:00
|
|
|
return fmt.Errorf("metal: lib.MakeFunction for vertex failed: %w, source: %s", err, src)
|
2020-08-03 17:56:45 +02:00
|
|
|
}
|
2024-05-04 12:09:02 +02:00
|
|
|
fs, err := s.lib.MakeFunction(msl.FragmentName)
|
2020-08-03 17:56:45 +02:00
|
|
|
if err != nil {
|
2023-12-24 14:29:53 +01:00
|
|
|
return fmt.Errorf("metal: lib.MakeFunction for fragment failed: %w, source: %s", err, src)
|
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
|
|
|
|
2022-09-06 11:21:33 +02:00
|
|
|
rps, err := view.getMTLDevice().MakeRenderPipelineState(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
|
|
|
|
}
|