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.
|
|
|
|
|
2021-06-10 18:03:35 +02:00
|
|
|
//go:build darwin
|
2020-08-03 17:56:45 +02:00
|
|
|
// +build darwin
|
|
|
|
|
|
|
|
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"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/shaderir/metal"
|
2020-08-03 17:56:45 +02:00
|
|
|
)
|
|
|
|
|
2021-07-02 12:26:09 +02:00
|
|
|
type shaderRpsKey struct {
|
2022-02-06 12:41:32 +01:00
|
|
|
compositeMode graphicsdriver.CompositeMode
|
2021-07-06 07:05:01 +02:00
|
|
|
stencilMode stencilMode
|
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
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Shader) init(device mtl.Device) error {
|
|
|
|
const (
|
|
|
|
v = "Vertex"
|
|
|
|
f = "Fragment"
|
|
|
|
)
|
|
|
|
|
|
|
|
src := metal.Compile(s.ir, v, f)
|
|
|
|
lib, err := device.MakeLibrary(src, mtl.CompileOptions{})
|
|
|
|
if err != nil {
|
2020-08-08 21:06:30 +02:00
|
|
|
return fmt.Errorf("metal: device.MakeLibrary failed: %v, source: %s", err, src)
|
2020-08-03 17:56:45 +02:00
|
|
|
}
|
|
|
|
vs, err := lib.MakeFunction(v)
|
|
|
|
if err != nil {
|
2020-08-08 21:06:30 +02:00
|
|
|
return fmt.Errorf("metal: lib.MakeFunction for vertex failed: %v, source: %s", err, src)
|
2020-08-03 17:56:45 +02:00
|
|
|
}
|
|
|
|
fs, err := lib.MakeFunction(f)
|
|
|
|
if err != nil {
|
2020-08-08 21:06:30 +02:00
|
|
|
return fmt.Errorf("metal: lib.MakeFunction for fragment failed: %v, source: %s", err, src)
|
2020-08-03 17:56:45 +02:00
|
|
|
}
|
|
|
|
s.fs = fs
|
|
|
|
s.vs = vs
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-06 12:41:32 +01:00
|
|
|
func (s *Shader) RenderPipelineState(device mtl.Device, compositeMode graphicsdriver.CompositeMode, stencilMode stencilMode) (mtl.RenderPipelineState, error) {
|
2021-07-02 12:26:09 +02:00
|
|
|
if rps, ok := s.rpss[shaderRpsKey{
|
2021-07-06 07:05:01 +02:00
|
|
|
compositeMode: compositeMode,
|
|
|
|
stencilMode: stencilMode,
|
2021-07-02 12:26:09 +02:00
|
|
|
}]; 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.
|
|
|
|
rpld.ColorAttachments[0].PixelFormat = mtl.PixelFormatRGBA8UNorm
|
|
|
|
rpld.ColorAttachments[0].BlendingEnabled = true
|
|
|
|
|
2021-07-02 12:26:09 +02:00
|
|
|
src, dst := compositeMode.Operations()
|
2020-08-03 17:56:45 +02:00
|
|
|
rpld.ColorAttachments[0].DestinationAlphaBlendFactor = operationToBlendFactor(dst)
|
|
|
|
rpld.ColorAttachments[0].DestinationRGBBlendFactor = operationToBlendFactor(dst)
|
|
|
|
rpld.ColorAttachments[0].SourceAlphaBlendFactor = operationToBlendFactor(src)
|
|
|
|
rpld.ColorAttachments[0].SourceRGBBlendFactor = operationToBlendFactor(src)
|
2021-07-06 07:05:01 +02:00
|
|
|
if stencilMode == prepareStencil {
|
2021-07-02 12:26:09 +02:00
|
|
|
rpld.ColorAttachments[0].WriteMask = mtl.ColorWriteMaskNone
|
2021-07-06 07:05:01 +02:00
|
|
|
} else {
|
|
|
|
rpld.ColorAttachments[0].WriteMask = mtl.ColorWriteMaskAll
|
2021-07-02 12:26:09 +02:00
|
|
|
}
|
2020-08-03 17:56:45 +02:00
|
|
|
|
|
|
|
rps, err := device.MakeRenderPipelineState(rpld)
|
|
|
|
if err != nil {
|
|
|
|
return mtl.RenderPipelineState{}, err
|
|
|
|
}
|
|
|
|
|
2021-07-02 12:26:09 +02:00
|
|
|
s.rpss[shaderRpsKey{
|
2021-07-06 07:05:01 +02:00
|
|
|
compositeMode: compositeMode,
|
|
|
|
stencilMode: stencilMode,
|
2021-07-02 12:26:09 +02:00
|
|
|
}] = rps
|
2020-08-03 17:56:45 +02:00
|
|
|
return rps, nil
|
|
|
|
}
|