2018-11-12 16:00:10 +01:00
|
|
|
// Copyright 2018 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 (
|
|
|
|
"fmt"
|
2023-04-27 17:00:51 +02:00
|
|
|
"image"
|
2021-02-26 19:40:36 +01:00
|
|
|
"math"
|
2022-12-30 08:28:07 +01:00
|
|
|
"runtime"
|
2021-07-06 10:25:11 +02:00
|
|
|
"sort"
|
2018-11-12 16:00:10 +01:00
|
|
|
"unsafe"
|
|
|
|
|
2023-01-23 15:38:53 +01:00
|
|
|
"github.com/ebitengine/purego/objc"
|
|
|
|
|
2022-08-17 18:39:34 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/cocoa"
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphics"
|
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/ca"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/metal/mtl"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/shaderir"
|
2018-11-12 16:00:10 +01:00
|
|
|
)
|
|
|
|
|
2020-04-04 10:12:24 +02:00
|
|
|
type Graphics struct {
|
2019-06-18 17:34:05 +02:00
|
|
|
view view
|
2018-11-12 16:00:10 +01:00
|
|
|
|
2022-09-06 11:51:55 +02:00
|
|
|
cq mtl.CommandQueue
|
|
|
|
cb mtl.CommandBuffer
|
|
|
|
rce mtl.RenderCommandEncoder
|
|
|
|
dsss map[stencilMode]mtl.DepthStencilState
|
2018-11-12 16:00:10 +01:00
|
|
|
|
|
|
|
screenDrawable ca.MetalDrawable
|
2021-07-02 12:26:09 +02:00
|
|
|
|
2021-07-07 17:27:33 +02:00
|
|
|
buffers map[mtl.CommandBuffer][]mtl.Buffer
|
|
|
|
unusedBuffers map[mtl.Buffer]struct{}
|
2021-07-06 10:25:11 +02:00
|
|
|
|
2023-11-04 10:09:47 +01:00
|
|
|
lastDst *Image
|
|
|
|
lastFillRule graphicsdriver.FillRule
|
2018-11-12 16:00:10 +01:00
|
|
|
|
2020-05-19 16:48:43 +02:00
|
|
|
vb mtl.Buffer
|
|
|
|
ib mtl.Buffer
|
|
|
|
|
2022-02-06 12:41:32 +01:00
|
|
|
images map[graphicsdriver.ImageID]*Image
|
|
|
|
nextImageID graphicsdriver.ImageID
|
2020-05-19 16:48:43 +02:00
|
|
|
|
2022-02-06 12:41:32 +01:00
|
|
|
shaders map[graphicsdriver.ShaderID]*Shader
|
|
|
|
nextShaderID graphicsdriver.ShaderID
|
2020-08-03 17:56:45 +02:00
|
|
|
|
2022-07-03 05:17:58 +02:00
|
|
|
transparent bool
|
|
|
|
maxImageSize int
|
|
|
|
tmpTextures []mtl.Texture
|
2019-04-20 08:17:59 +02:00
|
|
|
|
2022-08-17 18:39:34 +02:00
|
|
|
pool cocoa.NSAutoreleasePool
|
2018-11-12 16:00:10 +01:00
|
|
|
}
|
|
|
|
|
2021-07-02 12:26:09 +02:00
|
|
|
type stencilMode int
|
|
|
|
|
|
|
|
const (
|
2023-11-06 01:18:08 +01:00
|
|
|
noStencil stencilMode = iota
|
|
|
|
incrementStencil
|
|
|
|
invertStencil
|
2021-07-02 12:26:09 +02:00
|
|
|
drawWithStencil
|
|
|
|
)
|
|
|
|
|
2023-08-26 05:43:16 +02:00
|
|
|
var (
|
|
|
|
systemDefaultDevice mtl.Device
|
|
|
|
systemDefaultDeviceErr error
|
|
|
|
)
|
2022-06-17 07:56:31 +02:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
// mtl.CreateSystemDefaultDevice must be called on the main thread (#2147).
|
2023-03-13 13:22:35 +01:00
|
|
|
d, err := mtl.CreateSystemDefaultDevice()
|
2023-08-26 05:43:16 +02:00
|
|
|
if err != nil {
|
|
|
|
systemDefaultDeviceErr = err
|
|
|
|
return
|
2023-03-13 13:22:35 +01:00
|
|
|
}
|
2023-08-26 05:43:16 +02:00
|
|
|
systemDefaultDevice = d
|
2022-06-17 07:56:31 +02:00
|
|
|
}
|
|
|
|
|
2022-08-03 15:40:39 +02:00
|
|
|
// NewGraphics creates an implementation of graphicsdriver.Graphics for Metal.
|
2022-06-16 19:10:29 +02:00
|
|
|
// The returned graphics value is nil iff the error is not nil.
|
|
|
|
func NewGraphics() (graphicsdriver.Graphics, error) {
|
|
|
|
// On old mac devices like iMac 2011, Metal is not supported (#779).
|
|
|
|
// TODO: Is there a better way to check whether Metal is available or not?
|
|
|
|
// It seems OK to call MTLCreateSystemDefaultDevice multiple times, so this should be fine.
|
2023-08-26 05:43:16 +02:00
|
|
|
if systemDefaultDeviceErr != nil {
|
|
|
|
return nil, fmt.Errorf("metal: mtl.CreateSystemDefaultDevice failed: %w", systemDefaultDeviceErr)
|
2022-02-11 15:11:49 +01:00
|
|
|
}
|
2022-06-16 19:10:29 +02:00
|
|
|
|
2022-12-30 08:28:07 +01:00
|
|
|
g := &Graphics{}
|
|
|
|
|
|
|
|
if runtime.GOOS != "ios" {
|
2023-03-13 14:21:55 +01:00
|
|
|
// Initializing a Metal device and a layer must be done in the main thread on macOS.
|
|
|
|
// Note that this assumes NewGraphics is called on the main thread on desktops.
|
2023-03-13 13:22:35 +01:00
|
|
|
if err := g.view.initialize(systemDefaultDevice); err != nil {
|
2022-12-30 08:28:07 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return g, nil
|
2018-11-12 16:00:10 +01:00
|
|
|
}
|
|
|
|
|
2022-03-21 14:23:07 +01:00
|
|
|
func (g *Graphics) Begin() error {
|
2020-10-12 17:39:45 +02:00
|
|
|
// NSAutoreleasePool is required to release drawable correctly (#847).
|
|
|
|
// https://developer.apple.com/library/archive/documentation/3DDrawing/Conceptual/MTLBestPracticesGuide/Drawables.html
|
2022-08-17 18:39:34 +02:00
|
|
|
g.pool = cocoa.NSAutoreleasePool_new()
|
2022-03-21 14:23:07 +01:00
|
|
|
return nil
|
2019-04-20 08:17:59 +02:00
|
|
|
}
|
|
|
|
|
2022-03-21 14:23:07 +01:00
|
|
|
func (g *Graphics) End(present bool) error {
|
2022-02-21 17:37:13 +01:00
|
|
|
g.flushIfNeeded(present)
|
2020-10-12 17:39:45 +02:00
|
|
|
g.screenDrawable = ca.MetalDrawable{}
|
2022-08-17 18:39:34 +02:00
|
|
|
g.pool.Release()
|
|
|
|
g.pool.ID = 0
|
2022-03-21 14:23:07 +01:00
|
|
|
return nil
|
2019-04-20 08:17:59 +02:00
|
|
|
}
|
|
|
|
|
2020-09-03 17:43:51 +02:00
|
|
|
func (g *Graphics) SetWindow(window uintptr) {
|
2020-10-12 17:39:45 +02:00
|
|
|
// Note that [NSApp mainWindow] returns nil when the window is borderless.
|
|
|
|
// Then the window is needed to be given explicitly.
|
|
|
|
g.view.setWindow(window)
|
2018-11-12 16:00:10 +01:00
|
|
|
}
|
|
|
|
|
2020-04-04 10:12:24 +02:00
|
|
|
func (g *Graphics) SetUIView(uiview uintptr) {
|
2019-09-02 21:08:58 +02:00
|
|
|
// TODO: Should this be called on the main thread?
|
2020-04-04 10:12:24 +02:00
|
|
|
g.view.setUIView(uiview)
|
2019-09-02 21:08:58 +02:00
|
|
|
}
|
|
|
|
|
2021-07-06 10:25:11 +02:00
|
|
|
func pow2(x uintptr) uintptr {
|
2023-03-22 18:47:23 +01:00
|
|
|
if x > (math.MaxUint+1)/2 {
|
|
|
|
return math.MaxUint
|
|
|
|
}
|
|
|
|
|
2021-07-06 10:25:11 +02:00
|
|
|
var p2 uintptr = 1
|
|
|
|
for p2 < x {
|
|
|
|
p2 *= 2
|
|
|
|
}
|
|
|
|
return p2
|
|
|
|
}
|
|
|
|
|
2021-07-07 19:09:04 +02:00
|
|
|
func (g *Graphics) gcBuffers() {
|
2021-07-07 17:27:33 +02:00
|
|
|
for cb, bs := range g.buffers {
|
|
|
|
// If the command buffer still lives, the buffer must not be updated.
|
|
|
|
// TODO: Handle an error?
|
|
|
|
if cb.Status() != mtl.CommandBufferStatusCompleted {
|
2021-07-06 10:25:11 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-07-07 17:27:33 +02:00
|
|
|
for _, b := range bs {
|
|
|
|
if g.unusedBuffers == nil {
|
|
|
|
g.unusedBuffers = map[mtl.Buffer]struct{}{}
|
|
|
|
}
|
|
|
|
g.unusedBuffers[b] = struct{}{}
|
|
|
|
}
|
|
|
|
delete(g.buffers, cb)
|
|
|
|
cb.Release()
|
|
|
|
}
|
|
|
|
|
2021-07-06 14:56:25 +02:00
|
|
|
const maxUnusedBuffers = 10
|
2021-07-07 17:27:33 +02:00
|
|
|
if len(g.unusedBuffers) > maxUnusedBuffers {
|
|
|
|
bufs := make([]mtl.Buffer, 0, len(g.unusedBuffers))
|
|
|
|
for b := range g.unusedBuffers {
|
|
|
|
bufs = append(bufs, b)
|
|
|
|
}
|
|
|
|
sort.Slice(bufs, func(a, b int) bool {
|
|
|
|
return bufs[a].Length() > bufs[b].Length()
|
2021-07-06 10:25:11 +02:00
|
|
|
})
|
2021-07-07 17:27:33 +02:00
|
|
|
for _, b := range bufs[maxUnusedBuffers:] {
|
|
|
|
delete(g.unusedBuffers, b)
|
2021-07-06 10:25:11 +02:00
|
|
|
b.Release()
|
|
|
|
}
|
2020-10-12 17:39:45 +02:00
|
|
|
}
|
2021-07-07 19:09:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Graphics) availableBuffer(length uintptr) mtl.Buffer {
|
|
|
|
if g.cb == (mtl.CommandBuffer{}) {
|
2024-05-06 14:38:59 +02:00
|
|
|
g.cb = g.cq.CommandBuffer()
|
2021-07-07 19:09:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var newBuf mtl.Buffer
|
|
|
|
for b := range g.unusedBuffers {
|
|
|
|
if b.Length() >= length {
|
|
|
|
newBuf = b
|
|
|
|
delete(g.unusedBuffers, b)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2021-07-06 10:25:11 +02:00
|
|
|
|
|
|
|
if newBuf == (mtl.Buffer{}) {
|
2024-05-06 14:38:59 +02:00
|
|
|
newBuf = g.view.getMTLDevice().NewBufferWithLength(pow2(length), resourceStorageMode)
|
2021-07-06 10:25:11 +02:00
|
|
|
}
|
2021-07-07 17:27:33 +02:00
|
|
|
|
2021-07-06 10:25:11 +02:00
|
|
|
if g.buffers == nil {
|
2021-07-07 17:27:33 +02:00
|
|
|
g.buffers = map[mtl.CommandBuffer][]mtl.Buffer{}
|
|
|
|
}
|
|
|
|
if _, ok := g.buffers[g.cb]; !ok {
|
|
|
|
g.cb.Retain()
|
2021-07-06 10:25:11 +02:00
|
|
|
}
|
2021-07-07 17:27:33 +02:00
|
|
|
g.buffers[g.cb] = append(g.buffers[g.cb], newBuf)
|
2021-07-06 10:25:11 +02:00
|
|
|
return newBuf
|
|
|
|
}
|
|
|
|
|
2023-11-03 18:45:16 +01:00
|
|
|
func (g *Graphics) SetVertices(vertices []float32, indices []uint32) error {
|
2021-07-06 10:25:11 +02:00
|
|
|
vbSize := unsafe.Sizeof(vertices[0]) * uintptr(len(vertices))
|
|
|
|
ibSize := unsafe.Sizeof(indices[0]) * uintptr(len(indices))
|
|
|
|
|
|
|
|
g.vb = g.availableBuffer(vbSize)
|
|
|
|
g.vb.CopyToContents(unsafe.Pointer(&vertices[0]), vbSize)
|
|
|
|
|
|
|
|
g.ib = g.availableBuffer(ibSize)
|
|
|
|
g.ib.CopyToContents(unsafe.Pointer(&indices[0]), ibSize)
|
2022-03-21 14:23:07 +01:00
|
|
|
|
|
|
|
return nil
|
2018-11-12 16:00:10 +01:00
|
|
|
}
|
|
|
|
|
2020-11-10 14:48:01 +01:00
|
|
|
func (g *Graphics) flushIfNeeded(present bool) {
|
2022-10-23 18:51:37 +02:00
|
|
|
if g.cb == (mtl.CommandBuffer{}) && !present {
|
2020-10-12 17:39:45 +02:00
|
|
|
return
|
|
|
|
}
|
2023-01-03 14:58:14 +01:00
|
|
|
|
2021-07-04 11:37:14 +02:00
|
|
|
g.flushRenderCommandEncoderIfNeeded()
|
2018-11-12 16:00:10 +01:00
|
|
|
|
2022-12-29 07:27:51 +01:00
|
|
|
if present {
|
|
|
|
// This check is necessary when skipping to render the screen (SetScreenClearedEveryFrame(false)).
|
2024-01-24 04:01:26 +01:00
|
|
|
if g.screenDrawable == (ca.MetalDrawable{}) && g.cb != (mtl.CommandBuffer{}) {
|
|
|
|
g.screenDrawable = g.view.nextDrawable()
|
2022-12-29 07:27:51 +01:00
|
|
|
}
|
|
|
|
if g.screenDrawable != (ca.MetalDrawable{}) {
|
|
|
|
g.cb.PresentDrawable(g.screenDrawable)
|
|
|
|
}
|
2022-10-23 18:51:37 +02:00
|
|
|
}
|
|
|
|
|
2020-10-12 17:39:45 +02:00
|
|
|
g.cb.Commit()
|
2020-11-10 14:48:01 +01:00
|
|
|
|
|
|
|
for _, t := range g.tmpTextures {
|
|
|
|
t.Release()
|
2020-10-12 17:39:45 +02:00
|
|
|
}
|
2021-02-26 18:30:20 +01:00
|
|
|
g.tmpTextures = g.tmpTextures[:0]
|
2020-10-12 19:12:02 +02:00
|
|
|
|
2020-10-12 17:39:45 +02:00
|
|
|
g.cb = mtl.CommandBuffer{}
|
2018-11-12 16:00:10 +01:00
|
|
|
}
|
|
|
|
|
2020-04-04 10:12:24 +02:00
|
|
|
func (g *Graphics) checkSize(width, height int) {
|
2018-11-12 16:00:10 +01:00
|
|
|
if width < 1 {
|
2019-02-14 13:04:34 +01:00
|
|
|
panic(fmt.Sprintf("metal: width (%d) must be equal or more than %d", width, 1))
|
2018-11-12 16:00:10 +01:00
|
|
|
}
|
|
|
|
if height < 1 {
|
2019-02-14 13:04:34 +01:00
|
|
|
panic(fmt.Sprintf("metal: height (%d) must be equal or more than %d", height, 1))
|
2018-11-12 16:00:10 +01:00
|
|
|
}
|
2020-04-04 10:12:24 +02:00
|
|
|
m := g.MaxImageSize()
|
2018-11-12 16:00:10 +01:00
|
|
|
if width > m {
|
|
|
|
panic(fmt.Sprintf("metal: width (%d) must be less than or equal to %d", width, m))
|
|
|
|
}
|
|
|
|
if height > m {
|
|
|
|
panic(fmt.Sprintf("metal: height (%d) must be less than or equal to %d", height, m))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-06 12:41:32 +01:00
|
|
|
func (g *Graphics) genNextImageID() graphicsdriver.ImageID {
|
2020-05-19 16:48:43 +02:00
|
|
|
g.nextImageID++
|
2021-07-01 07:58:55 +02:00
|
|
|
return g.nextImageID
|
2020-05-19 16:48:43 +02:00
|
|
|
}
|
|
|
|
|
2022-02-06 12:41:32 +01:00
|
|
|
func (g *Graphics) genNextShaderID() graphicsdriver.ShaderID {
|
2020-08-03 17:56:45 +02:00
|
|
|
g.nextShaderID++
|
2021-07-01 07:58:55 +02:00
|
|
|
return g.nextShaderID
|
2020-08-03 17:56:45 +02:00
|
|
|
}
|
|
|
|
|
2022-02-06 12:41:32 +01:00
|
|
|
func (g *Graphics) NewImage(width, height int) (graphicsdriver.Image, error) {
|
2020-04-04 10:12:24 +02:00
|
|
|
g.checkSize(width, height)
|
2018-11-12 16:00:10 +01:00
|
|
|
td := mtl.TextureDescriptor{
|
2020-06-26 16:36:23 +02:00
|
|
|
TextureType: mtl.TextureType2D,
|
2018-11-12 16:00:10 +01:00
|
|
|
PixelFormat: mtl.PixelFormatRGBA8UNorm,
|
2019-02-14 13:04:34 +01:00
|
|
|
Width: graphics.InternalImageSize(width),
|
|
|
|
Height: graphics.InternalImageSize(height),
|
2019-09-02 21:08:58 +02:00
|
|
|
StorageMode: storageMode,
|
2020-06-26 20:03:53 +02:00
|
|
|
Usage: mtl.TextureUsageShaderRead | mtl.TextureUsageRenderTarget,
|
2018-11-12 16:00:10 +01:00
|
|
|
}
|
2024-05-06 14:38:59 +02:00
|
|
|
t := g.view.getMTLDevice().NewTextureWithDescriptor(td)
|
2020-05-19 16:48:43 +02:00
|
|
|
i := &Image{
|
|
|
|
id: g.genNextImageID(),
|
2020-04-04 10:12:24 +02:00
|
|
|
graphics: g,
|
|
|
|
width: width,
|
|
|
|
height: height,
|
|
|
|
texture: t,
|
2020-05-19 16:48:43 +02:00
|
|
|
}
|
|
|
|
g.addImage(i)
|
|
|
|
return i, nil
|
2018-11-12 16:00:10 +01:00
|
|
|
}
|
|
|
|
|
2022-02-06 12:41:32 +01:00
|
|
|
func (g *Graphics) NewScreenFramebufferImage(width, height int) (graphicsdriver.Image, error) {
|
2020-10-12 17:39:45 +02:00
|
|
|
g.view.setDrawableSize(width, height)
|
2020-05-19 16:48:43 +02:00
|
|
|
i := &Image{
|
|
|
|
id: g.genNextImageID(),
|
2020-04-04 10:12:24 +02:00
|
|
|
graphics: g,
|
|
|
|
width: width,
|
|
|
|
height: height,
|
|
|
|
screen: true,
|
2020-05-19 16:48:43 +02:00
|
|
|
}
|
|
|
|
g.addImage(i)
|
|
|
|
return i, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Graphics) addImage(img *Image) {
|
|
|
|
if g.images == nil {
|
2022-02-06 12:41:32 +01:00
|
|
|
g.images = map[graphicsdriver.ImageID]*Image{}
|
2020-05-19 16:48:43 +02:00
|
|
|
}
|
|
|
|
if _, ok := g.images[img.id]; ok {
|
2022-02-20 14:01:36 +01:00
|
|
|
panic(fmt.Sprintf("metal: image ID %d was already registered", img.id))
|
2020-05-19 16:48:43 +02:00
|
|
|
}
|
|
|
|
g.images[img.id] = img
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Graphics) removeImage(img *Image) {
|
|
|
|
delete(g.images, img.id)
|
2018-11-12 16:00:10 +01:00
|
|
|
}
|
|
|
|
|
2020-04-04 10:12:24 +02:00
|
|
|
func (g *Graphics) SetTransparent(transparent bool) {
|
|
|
|
g.transparent = transparent
|
2019-11-30 16:07:41 +01:00
|
|
|
}
|
|
|
|
|
2022-10-15 10:27:29 +02:00
|
|
|
func blendFactorToMetalBlendFactor(c graphicsdriver.BlendFactor) mtl.BlendFactor {
|
2020-08-03 17:56:45 +02:00
|
|
|
switch c {
|
2022-10-15 10:27:29 +02:00
|
|
|
case graphicsdriver.BlendFactorZero:
|
2020-08-03 17:56:45 +02:00
|
|
|
return mtl.BlendFactorZero
|
2022-10-15 10:27:29 +02:00
|
|
|
case graphicsdriver.BlendFactorOne:
|
2020-08-03 17:56:45 +02:00
|
|
|
return mtl.BlendFactorOne
|
2022-10-17 04:25:58 +02:00
|
|
|
case graphicsdriver.BlendFactorSourceColor:
|
|
|
|
return mtl.BlendFactorSourceColor
|
|
|
|
case graphicsdriver.BlendFactorOneMinusSourceColor:
|
|
|
|
return mtl.BlendFactorOneMinusSourceColor
|
2022-10-15 14:14:59 +02:00
|
|
|
case graphicsdriver.BlendFactorSourceAlpha:
|
2020-08-03 17:56:45 +02:00
|
|
|
return mtl.BlendFactorSourceAlpha
|
2022-10-15 14:14:59 +02:00
|
|
|
case graphicsdriver.BlendFactorOneMinusSourceAlpha:
|
2020-08-03 17:56:45 +02:00
|
|
|
return mtl.BlendFactorOneMinusSourceAlpha
|
2022-10-16 18:00:02 +02:00
|
|
|
case graphicsdriver.BlendFactorDestinationColor:
|
|
|
|
return mtl.BlendFactorDestinationColor
|
2022-10-17 04:25:58 +02:00
|
|
|
case graphicsdriver.BlendFactorOneMinusDestinationColor:
|
|
|
|
return mtl.BlendFactorOneMinusDestinationColor
|
|
|
|
case graphicsdriver.BlendFactorDestinationAlpha:
|
|
|
|
return mtl.BlendFactorDestinationAlpha
|
|
|
|
case graphicsdriver.BlendFactorOneMinusDestinationAlpha:
|
|
|
|
return mtl.BlendFactorOneMinusDestinationAlpha
|
|
|
|
case graphicsdriver.BlendFactorSourceAlphaSaturated:
|
|
|
|
return mtl.BlendFactorSourceAlphaSaturated
|
2020-08-03 17:56:45 +02:00
|
|
|
default:
|
2022-10-15 11:58:56 +02:00
|
|
|
panic(fmt.Sprintf("metal: invalid blend factor: %d", c))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func blendOperationToMetalBlendOperation(o graphicsdriver.BlendOperation) mtl.BlendOperation {
|
|
|
|
switch o {
|
|
|
|
case graphicsdriver.BlendOperationAdd:
|
|
|
|
return mtl.BlendOperationAdd
|
2022-10-16 16:33:16 +02:00
|
|
|
case graphicsdriver.BlendOperationSubtract:
|
|
|
|
return mtl.BlendOperationSubtract
|
|
|
|
case graphicsdriver.BlendOperationReverseSubtract:
|
|
|
|
return mtl.BlendOperationReverseSubtract
|
2023-10-12 16:55:31 +02:00
|
|
|
case graphicsdriver.BlendOperationMin:
|
|
|
|
return mtl.BlendOperationMin
|
|
|
|
case graphicsdriver.BlendOperationMax:
|
|
|
|
return mtl.BlendOperationMax
|
2022-10-15 11:58:56 +02:00
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("metal: invalid blend operation: %d", o))
|
2020-08-03 17:56:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-07 06:58:42 +02:00
|
|
|
func (g *Graphics) Initialize() error {
|
2021-07-06 08:05:44 +02:00
|
|
|
// Creating *State objects are expensive and reuse them whenever possible.
|
|
|
|
// See https://developer.apple.com/library/archive/documentation/Miscellaneous/Conceptual/MetalProgrammingGuide/Cmd-Submiss/Cmd-Submiss.html
|
|
|
|
|
|
|
|
for _, dss := range g.dsss {
|
|
|
|
dss.Release()
|
|
|
|
}
|
|
|
|
if g.dsss == nil {
|
|
|
|
g.dsss = map[stencilMode]mtl.DepthStencilState{}
|
|
|
|
}
|
|
|
|
|
2022-12-30 08:28:07 +01:00
|
|
|
if runtime.GOOS == "ios" {
|
|
|
|
// Initializing a Metal device and a layer must be done in the render thread on iOS.
|
2023-03-13 13:22:35 +01:00
|
|
|
if err := g.view.initialize(systemDefaultDevice); err != nil {
|
2022-12-30 08:28:07 +01:00
|
|
|
return err
|
|
|
|
}
|
2022-03-22 17:22:39 +01:00
|
|
|
}
|
2020-10-12 17:39:45 +02:00
|
|
|
if g.transparent {
|
|
|
|
g.view.ml.SetOpaque(false)
|
|
|
|
}
|
2018-11-12 16:00:10 +01:00
|
|
|
|
2021-07-06 08:05:44 +02:00
|
|
|
// The stencil reference value is always 0 (default).
|
2024-05-06 14:38:59 +02:00
|
|
|
g.dsss[noStencil] = g.view.getMTLDevice().NewDepthStencilStateWithDescriptor(mtl.DepthStencilDescriptor{
|
2021-07-06 08:05:44 +02:00
|
|
|
BackFaceStencil: mtl.StencilDescriptor{
|
|
|
|
StencilFailureOperation: mtl.StencilOperationKeep,
|
|
|
|
DepthFailureOperation: mtl.StencilOperationKeep,
|
2023-11-06 01:18:08 +01:00
|
|
|
DepthStencilPassOperation: mtl.StencilOperationKeep,
|
2021-07-06 08:05:44 +02:00
|
|
|
StencilCompareFunction: mtl.CompareFunctionAlways,
|
|
|
|
},
|
|
|
|
FrontFaceStencil: mtl.StencilDescriptor{
|
|
|
|
StencilFailureOperation: mtl.StencilOperationKeep,
|
|
|
|
DepthFailureOperation: mtl.StencilOperationKeep,
|
2023-11-06 01:18:08 +01:00
|
|
|
DepthStencilPassOperation: mtl.StencilOperationKeep,
|
2021-07-06 08:05:44 +02:00
|
|
|
StencilCompareFunction: mtl.CompareFunctionAlways,
|
|
|
|
},
|
|
|
|
})
|
2024-05-06 14:38:59 +02:00
|
|
|
g.dsss[incrementStencil] = g.view.getMTLDevice().NewDepthStencilStateWithDescriptor(mtl.DepthStencilDescriptor{
|
2021-07-06 08:05:44 +02:00
|
|
|
BackFaceStencil: mtl.StencilDescriptor{
|
|
|
|
StencilFailureOperation: mtl.StencilOperationKeep,
|
|
|
|
DepthFailureOperation: mtl.StencilOperationKeep,
|
2023-11-06 01:18:08 +01:00
|
|
|
DepthStencilPassOperation: mtl.StencilOperationDecrementWrap,
|
|
|
|
StencilCompareFunction: mtl.CompareFunctionAlways,
|
2021-07-06 08:05:44 +02:00
|
|
|
},
|
|
|
|
FrontFaceStencil: mtl.StencilDescriptor{
|
|
|
|
StencilFailureOperation: mtl.StencilOperationKeep,
|
|
|
|
DepthFailureOperation: mtl.StencilOperationKeep,
|
2023-11-06 01:18:08 +01:00
|
|
|
DepthStencilPassOperation: mtl.StencilOperationIncrementWrap,
|
|
|
|
StencilCompareFunction: mtl.CompareFunctionAlways,
|
2021-07-06 08:05:44 +02:00
|
|
|
},
|
|
|
|
})
|
2024-05-06 14:38:59 +02:00
|
|
|
g.dsss[invertStencil] = g.view.getMTLDevice().NewDepthStencilStateWithDescriptor(mtl.DepthStencilDescriptor{
|
2021-07-06 08:05:44 +02:00
|
|
|
BackFaceStencil: mtl.StencilDescriptor{
|
|
|
|
StencilFailureOperation: mtl.StencilOperationKeep,
|
|
|
|
DepthFailureOperation: mtl.StencilOperationKeep,
|
2023-11-06 01:18:08 +01:00
|
|
|
DepthStencilPassOperation: mtl.StencilOperationInvert,
|
2021-07-06 08:05:44 +02:00
|
|
|
StencilCompareFunction: mtl.CompareFunctionAlways,
|
|
|
|
},
|
|
|
|
FrontFaceStencil: mtl.StencilDescriptor{
|
|
|
|
StencilFailureOperation: mtl.StencilOperationKeep,
|
|
|
|
DepthFailureOperation: mtl.StencilOperationKeep,
|
2023-11-06 01:18:08 +01:00
|
|
|
DepthStencilPassOperation: mtl.StencilOperationInvert,
|
2021-07-06 08:05:44 +02:00
|
|
|
StencilCompareFunction: mtl.CompareFunctionAlways,
|
|
|
|
},
|
|
|
|
})
|
2024-05-06 14:38:59 +02:00
|
|
|
g.dsss[drawWithStencil] = g.view.getMTLDevice().NewDepthStencilStateWithDescriptor(mtl.DepthStencilDescriptor{
|
2023-11-06 01:18:08 +01:00
|
|
|
BackFaceStencil: mtl.StencilDescriptor{
|
|
|
|
StencilFailureOperation: mtl.StencilOperationKeep,
|
|
|
|
DepthFailureOperation: mtl.StencilOperationKeep,
|
|
|
|
DepthStencilPassOperation: mtl.StencilOperationKeep,
|
|
|
|
StencilCompareFunction: mtl.CompareFunctionNotEqual,
|
|
|
|
},
|
|
|
|
FrontFaceStencil: mtl.StencilDescriptor{
|
|
|
|
StencilFailureOperation: mtl.StencilOperationKeep,
|
|
|
|
DepthFailureOperation: mtl.StencilOperationKeep,
|
|
|
|
DepthStencilPassOperation: mtl.StencilOperationKeep,
|
|
|
|
StencilCompareFunction: mtl.CompareFunctionNotEqual,
|
|
|
|
},
|
|
|
|
})
|
2021-07-06 08:05:44 +02:00
|
|
|
|
2024-05-06 14:38:59 +02:00
|
|
|
g.cq = g.view.getMTLDevice().NewCommandQueue()
|
2018-11-12 16:00:10 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-07-04 11:13:18 +02:00
|
|
|
func (g *Graphics) flushRenderCommandEncoderIfNeeded() {
|
|
|
|
if g.rce == (mtl.RenderCommandEncoder{}) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
g.rce.EndEncoding()
|
|
|
|
g.rce = mtl.RenderCommandEncoder{}
|
2021-07-07 18:56:12 +02:00
|
|
|
g.lastDst = nil
|
2021-07-04 11:13:18 +02:00
|
|
|
}
|
|
|
|
|
2024-06-09 19:02:47 +02:00
|
|
|
func (g *Graphics) draw(dst *Image, dstRegions []graphicsdriver.DstRegion, srcs [graphics.ShaderSrcImageCount]*Image, indexOffset int, shader *Shader, uniforms [][]uint32, blend graphicsdriver.Blend, fillRule graphicsdriver.FillRule) error {
|
2023-01-28 11:06:38 +01:00
|
|
|
// When preparing a stencil buffer, flush the current render command encoder
|
2021-07-05 11:08:52 +02:00
|
|
|
// to make sure the stencil buffer is cleared when loading.
|
|
|
|
// TODO: What about clearing the stencil buffer by vertices?
|
2024-06-08 04:54:44 +02:00
|
|
|
if g.lastDst != dst || g.lastFillRule != fillRule || fillRule != graphicsdriver.FillRuleFillAll {
|
2021-07-04 11:13:18 +02:00
|
|
|
g.flushRenderCommandEncoderIfNeeded()
|
|
|
|
}
|
2021-07-07 18:56:12 +02:00
|
|
|
g.lastDst = dst
|
2023-11-04 10:09:47 +01:00
|
|
|
g.lastFillRule = fillRule
|
2021-07-02 12:26:09 +02:00
|
|
|
|
2021-07-04 11:13:18 +02:00
|
|
|
if g.rce == (mtl.RenderCommandEncoder{}) {
|
|
|
|
rpd := mtl.RenderPassDescriptor{}
|
|
|
|
// Even though the destination pixels are not used, mtl.LoadActionDontCare might cause glitches
|
|
|
|
// (#1019). Always using mtl.LoadActionLoad is safe.
|
2021-07-06 18:43:32 +02:00
|
|
|
if dst.screen {
|
|
|
|
rpd.ColorAttachments[0].LoadAction = mtl.LoadActionClear
|
|
|
|
} else {
|
|
|
|
rpd.ColorAttachments[0].LoadAction = mtl.LoadActionLoad
|
|
|
|
}
|
2021-07-04 11:13:18 +02:00
|
|
|
|
2021-07-08 10:39:33 +02:00
|
|
|
// The store action should always be 'store' even for the screen (#1700).
|
|
|
|
rpd.ColorAttachments[0].StoreAction = mtl.StoreActionStore
|
|
|
|
|
2021-07-04 11:13:18 +02:00
|
|
|
t := dst.mtlTexture()
|
|
|
|
if t == (mtl.Texture{}) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
rpd.ColorAttachments[0].Texture = t
|
|
|
|
rpd.ColorAttachments[0].ClearColor = mtl.ClearColor{}
|
2020-05-19 16:48:43 +02:00
|
|
|
|
2024-06-08 04:54:44 +02:00
|
|
|
if fillRule != graphicsdriver.FillRuleFillAll {
|
2021-07-02 12:26:09 +02:00
|
|
|
dst.ensureStencil()
|
|
|
|
rpd.StencilAttachment.LoadAction = mtl.LoadActionClear
|
|
|
|
rpd.StencilAttachment.StoreAction = mtl.StoreActionDontCare
|
|
|
|
rpd.StencilAttachment.Texture = dst.stencil
|
|
|
|
}
|
|
|
|
|
2021-07-04 11:13:18 +02:00
|
|
|
if g.cb == (mtl.CommandBuffer{}) {
|
2024-05-06 14:38:59 +02:00
|
|
|
g.cb = g.cq.CommandBuffer()
|
2018-11-12 16:00:10 +01:00
|
|
|
}
|
2024-05-06 14:38:59 +02:00
|
|
|
g.rce = g.cb.RenderCommandEncoderWithDescriptor(rpd)
|
2020-08-07 20:01:23 +02:00
|
|
|
}
|
2018-11-12 16:00:10 +01:00
|
|
|
|
2020-08-08 09:07:03 +02:00
|
|
|
w, h := dst.internalSize()
|
2021-07-04 11:13:18 +02:00
|
|
|
g.rce.SetViewport(mtl.Viewport{
|
2020-08-07 20:01:23 +02:00
|
|
|
OriginX: 0,
|
2022-02-26 17:49:56 +01:00
|
|
|
OriginY: 0,
|
2020-08-07 20:01:23 +02:00
|
|
|
Width: float64(w),
|
2022-02-26 17:49:56 +01:00
|
|
|
Height: float64(h),
|
2020-08-07 20:01:23 +02:00
|
|
|
ZNear: -1,
|
|
|
|
ZFar: 1,
|
|
|
|
})
|
2021-07-04 11:13:18 +02:00
|
|
|
g.rce.SetVertexBuffer(g.vb, 0, 0)
|
2020-08-07 20:01:23 +02:00
|
|
|
|
|
|
|
for i, u := range uniforms {
|
2022-11-03 10:48:59 +01:00
|
|
|
if u == nil {
|
|
|
|
continue
|
|
|
|
}
|
2022-03-12 19:42:10 +01:00
|
|
|
g.rce.SetVertexBytes(unsafe.Pointer(&u[0]), unsafe.Sizeof(u[0])*uintptr(len(u)), i+1)
|
|
|
|
g.rce.SetFragmentBytes(unsafe.Pointer(&u[0]), unsafe.Sizeof(u[0])*uintptr(len(u)), i+1)
|
2020-08-07 20:01:23 +02:00
|
|
|
}
|
2018-11-12 16:00:10 +01:00
|
|
|
|
2020-08-07 20:01:23 +02:00
|
|
|
for i, src := range srcs {
|
|
|
|
if src != nil {
|
2021-07-04 11:13:18 +02:00
|
|
|
g.rce.SetFragmentTexture(src.texture, i)
|
2018-11-12 16:00:10 +01:00
|
|
|
} else {
|
2021-07-04 11:13:18 +02:00
|
|
|
g.rce.SetFragmentTexture(mtl.Texture{}, i)
|
2018-11-12 16:00:10 +01:00
|
|
|
}
|
2020-08-07 20:01:23 +02:00
|
|
|
}
|
2021-07-02 12:26:09 +02:00
|
|
|
|
2022-11-04 08:17:40 +01:00
|
|
|
var (
|
2023-11-06 01:18:08 +01:00
|
|
|
noStencilRpss mtl.RenderPipelineState
|
|
|
|
incrementStencilRpss mtl.RenderPipelineState
|
|
|
|
invertStencilRpss mtl.RenderPipelineState
|
|
|
|
drawWithStencilRpss mtl.RenderPipelineState
|
2022-11-04 08:17:40 +01:00
|
|
|
)
|
2023-11-04 10:09:47 +01:00
|
|
|
switch fillRule {
|
2024-06-08 04:54:44 +02:00
|
|
|
case graphicsdriver.FillRuleFillAll:
|
2023-11-04 10:09:47 +01:00
|
|
|
s, err := shader.RenderPipelineState(&g.view, blend, noStencil, dst.screen)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
noStencilRpss = s
|
2024-06-08 04:54:44 +02:00
|
|
|
case graphicsdriver.FillRuleNonZero:
|
2023-11-06 01:18:08 +01:00
|
|
|
s, err := shader.RenderPipelineState(&g.view, blend, incrementStencil, dst.screen)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
incrementStencilRpss = s
|
2024-06-08 04:54:44 +02:00
|
|
|
case graphicsdriver.FillRuleEvenOdd:
|
2023-11-06 01:18:08 +01:00
|
|
|
s, err := shader.RenderPipelineState(&g.view, blend, invertStencil, dst.screen)
|
2022-11-04 08:17:40 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-11-06 01:18:08 +01:00
|
|
|
invertStencilRpss = s
|
|
|
|
}
|
2024-06-08 04:54:44 +02:00
|
|
|
if fillRule != graphicsdriver.FillRuleFillAll {
|
2023-11-06 01:18:08 +01:00
|
|
|
s, err := shader.RenderPipelineState(&g.view, blend, drawWithStencil, dst.screen)
|
2022-11-04 08:17:40 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-11-04 08:17:40 +01:00
|
|
|
drawWithStencilRpss = s
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, dstRegion := range dstRegions {
|
|
|
|
g.rce.SetScissorRect(mtl.ScissorRect{
|
2023-09-28 07:29:55 +02:00
|
|
|
X: dstRegion.Region.Min.X,
|
|
|
|
Y: dstRegion.Region.Min.Y,
|
|
|
|
Width: dstRegion.Region.Dx(),
|
|
|
|
Height: dstRegion.Region.Dy(),
|
2022-11-04 08:17:40 +01:00
|
|
|
})
|
|
|
|
|
2023-11-04 10:09:47 +01:00
|
|
|
switch fillRule {
|
2024-06-08 04:54:44 +02:00
|
|
|
case graphicsdriver.FillRuleFillAll:
|
2023-11-04 10:09:47 +01:00
|
|
|
g.rce.SetDepthStencilState(g.dsss[noStencil])
|
|
|
|
g.rce.SetRenderPipelineState(noStencilRpss)
|
|
|
|
g.rce.DrawIndexedPrimitives(mtl.PrimitiveTypeTriangle, dstRegion.IndexCount, mtl.IndexTypeUInt32, g.ib, indexOffset*int(unsafe.Sizeof(uint32(0))))
|
2024-06-08 04:54:44 +02:00
|
|
|
case graphicsdriver.FillRuleNonZero:
|
2023-11-06 01:18:08 +01:00
|
|
|
g.rce.SetDepthStencilState(g.dsss[incrementStencil])
|
|
|
|
g.rce.SetRenderPipelineState(incrementStencilRpss)
|
|
|
|
g.rce.DrawIndexedPrimitives(mtl.PrimitiveTypeTriangle, dstRegion.IndexCount, mtl.IndexTypeUInt32, g.ib, indexOffset*int(unsafe.Sizeof(uint32(0))))
|
2024-06-08 04:54:44 +02:00
|
|
|
case graphicsdriver.FillRuleEvenOdd:
|
2023-11-06 01:18:08 +01:00
|
|
|
g.rce.SetDepthStencilState(g.dsss[invertStencil])
|
|
|
|
g.rce.SetRenderPipelineState(invertStencilRpss)
|
2023-11-03 18:45:16 +01:00
|
|
|
g.rce.DrawIndexedPrimitives(mtl.PrimitiveTypeTriangle, dstRegion.IndexCount, mtl.IndexTypeUInt32, g.ib, indexOffset*int(unsafe.Sizeof(uint32(0))))
|
2023-11-06 01:18:08 +01:00
|
|
|
}
|
2024-06-08 04:54:44 +02:00
|
|
|
if fillRule != graphicsdriver.FillRuleFillAll {
|
2022-11-04 08:17:40 +01:00
|
|
|
g.rce.SetDepthStencilState(g.dsss[drawWithStencil])
|
|
|
|
g.rce.SetRenderPipelineState(drawWithStencilRpss)
|
2023-11-03 18:45:16 +01:00
|
|
|
g.rce.DrawIndexedPrimitives(mtl.PrimitiveTypeTriangle, dstRegion.IndexCount, mtl.IndexTypeUInt32, g.ib, indexOffset*int(unsafe.Sizeof(uint32(0))))
|
2022-11-04 08:17:40 +01:00
|
|
|
}
|
2022-11-04 08:17:40 +01:00
|
|
|
|
2022-11-04 08:17:40 +01:00
|
|
|
indexOffset += dstRegion.IndexCount
|
2022-11-04 08:17:40 +01:00
|
|
|
}
|
2021-07-02 12:26:09 +02:00
|
|
|
|
2020-08-07 20:01:23 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-06-10 18:14:20 +02:00
|
|
|
func (g *Graphics) DrawTriangles(dstIDs [graphics.ShaderDstImageCount]graphicsdriver.ImageID, srcIDs [graphics.ShaderSrcImageCount]graphicsdriver.ImageID, shaderID graphicsdriver.ShaderID, dstRegions []graphicsdriver.DstRegion, indexOffset int, blend graphicsdriver.Blend, uniforms []uint32, fillRule graphicsdriver.FillRule) error {
|
2022-10-02 12:23:54 +02:00
|
|
|
if shaderID == graphicsdriver.InvalidShaderID {
|
|
|
|
return fmt.Errorf("metal: shader ID is invalid")
|
|
|
|
}
|
|
|
|
|
2024-06-10 18:14:20 +02:00
|
|
|
dst := g.images[dstIDs[0]]
|
2020-11-03 18:45:41 +01:00
|
|
|
|
2021-07-04 10:24:39 +02:00
|
|
|
if dst.screen {
|
|
|
|
g.view.update()
|
|
|
|
}
|
|
|
|
|
2024-06-09 19:02:47 +02:00
|
|
|
var srcs [graphics.ShaderSrcImageCount]*Image
|
2021-07-01 17:25:09 +02:00
|
|
|
for i, srcID := range srcIDs {
|
|
|
|
srcs[i] = g.images[srcID]
|
|
|
|
}
|
2020-08-07 20:01:23 +02:00
|
|
|
|
2022-12-03 08:11:26 +01:00
|
|
|
uniformVars := make([][]uint32, len(g.shaders[shaderID].ir.Uniforms))
|
2022-10-02 12:23:54 +02:00
|
|
|
|
|
|
|
// Set the additional uniform variables.
|
2022-12-03 08:11:26 +01:00
|
|
|
var idx int
|
|
|
|
for i, t := range g.shaders[shaderID].ir.Uniforms {
|
2022-10-30 10:31:06 +01:00
|
|
|
if i == graphics.ProjectionMatrixUniformVariableIndex {
|
|
|
|
// In Metal, the NDC's Y direction (upward) and the framebuffer's Y direction (downward) don't
|
|
|
|
// match. Then, the Y direction must be inverted.
|
2022-11-12 11:02:38 +01:00
|
|
|
// Invert the sign bits as float32 values.
|
2022-12-03 08:11:26 +01:00
|
|
|
uniforms[idx+1] ^= 1 << 31
|
|
|
|
uniforms[idx+5] ^= 1 << 31
|
|
|
|
uniforms[idx+9] ^= 1 << 31
|
|
|
|
uniforms[idx+13] ^= 1 << 31
|
2022-10-30 10:31:06 +01:00
|
|
|
}
|
|
|
|
|
2022-12-03 08:11:26 +01:00
|
|
|
n := t.Uint32Count()
|
|
|
|
|
2022-10-02 12:23:54 +02:00
|
|
|
switch t.Main {
|
2022-11-20 07:36:07 +01:00
|
|
|
case shaderir.Vec3, shaderir.IVec3:
|
2022-11-20 15:18:40 +01:00
|
|
|
// float3 requires 16-byte alignment (#2463).
|
2022-11-20 15:14:09 +01:00
|
|
|
v1 := make([]uint32, 4)
|
2022-12-03 08:11:26 +01:00
|
|
|
copy(v1[0:3], uniforms[idx:idx+3])
|
2022-11-20 15:14:09 +01:00
|
|
|
uniformVars[i] = v1
|
2022-10-02 12:23:54 +02:00
|
|
|
case shaderir.Mat3:
|
|
|
|
// float3x3 requires 16-byte alignment (#2036).
|
2022-11-12 11:02:38 +01:00
|
|
|
v1 := make([]uint32, 12)
|
2022-12-03 08:11:26 +01:00
|
|
|
copy(v1[0:3], uniforms[idx:idx+3])
|
|
|
|
copy(v1[4:7], uniforms[idx+3:idx+6])
|
|
|
|
copy(v1[8:11], uniforms[idx+6:idx+9])
|
2022-10-30 10:31:06 +01:00
|
|
|
uniformVars[i] = v1
|
2022-10-02 12:23:54 +02:00
|
|
|
case shaderir.Array:
|
|
|
|
switch t.Sub[0].Main {
|
2022-11-20 07:36:07 +01:00
|
|
|
case shaderir.Vec3, shaderir.IVec3:
|
2022-11-20 15:14:09 +01:00
|
|
|
v1 := make([]uint32, t.Length*4)
|
|
|
|
for j := 0; j < t.Length; j++ {
|
|
|
|
offset0 := j * 3
|
|
|
|
offset1 := j * 4
|
2022-12-03 08:11:26 +01:00
|
|
|
copy(v1[offset1:offset1+3], uniforms[idx+offset0:idx+offset0+3])
|
2022-11-20 15:14:09 +01:00
|
|
|
}
|
|
|
|
uniformVars[i] = v1
|
2022-03-26 15:46:37 +01:00
|
|
|
case shaderir.Mat3:
|
2022-11-12 11:02:38 +01:00
|
|
|
v1 := make([]uint32, t.Length*12)
|
2022-10-02 12:23:54 +02:00
|
|
|
for j := 0; j < t.Length; j++ {
|
|
|
|
offset0 := j * 9
|
|
|
|
offset1 := j * 12
|
2022-12-03 08:11:26 +01:00
|
|
|
copy(v1[offset1:offset1+3], uniforms[idx+offset0:idx+offset0+3])
|
|
|
|
copy(v1[offset1+4:offset1+7], uniforms[idx+offset0+3:idx+offset0+6])
|
|
|
|
copy(v1[offset1+8:offset1+11], uniforms[idx+offset0+6:idx+offset0+9])
|
2022-03-26 16:12:10 +01:00
|
|
|
}
|
2022-10-30 10:31:06 +01:00
|
|
|
uniformVars[i] = v1
|
2022-03-26 15:46:37 +01:00
|
|
|
default:
|
2022-12-03 08:11:26 +01:00
|
|
|
uniformVars[i] = uniforms[idx : idx+n]
|
2022-03-26 15:46:37 +01:00
|
|
|
}
|
2022-10-02 12:23:54 +02:00
|
|
|
default:
|
2022-12-03 08:11:26 +01:00
|
|
|
uniformVars[i] = uniforms[idx : idx+n]
|
2021-07-01 17:25:09 +02:00
|
|
|
}
|
2022-12-03 08:11:26 +01:00
|
|
|
|
|
|
|
idx += n
|
2020-08-07 20:01:23 +02:00
|
|
|
}
|
|
|
|
|
2023-11-04 10:09:47 +01:00
|
|
|
if err := g.draw(dst, dstRegions, srcs, indexOffset, g.shaders[shaderID], uniformVars, blend, fillRule); err != nil {
|
2022-11-04 08:17:40 +01:00
|
|
|
return err
|
2018-11-12 16:00:10 +01:00
|
|
|
}
|
2021-07-04 11:13:18 +02:00
|
|
|
|
2018-11-12 16:00:10 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-04 10:12:24 +02:00
|
|
|
func (g *Graphics) SetVsyncEnabled(enabled bool) {
|
|
|
|
g.view.setDisplaySyncEnabled(enabled)
|
2018-11-12 16:00:10 +01:00
|
|
|
}
|
|
|
|
|
2021-07-06 18:43:32 +02:00
|
|
|
func (g *Graphics) NeedsClearingScreen() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-04-04 10:12:24 +02:00
|
|
|
func (g *Graphics) MaxImageSize() int {
|
2021-07-04 13:35:56 +02:00
|
|
|
if g.maxImageSize != 0 {
|
|
|
|
return g.maxImageSize
|
|
|
|
}
|
|
|
|
|
2023-01-23 15:38:53 +01:00
|
|
|
d := g.view.getMTLDevice()
|
|
|
|
|
|
|
|
// supportsFamily is available as of macOS 10.15+ and iOS 13.0+.
|
|
|
|
// https://developer.apple.com/documentation/metal/mtldevice/3143473-supportsfamily
|
|
|
|
if d.RespondsToSelector(objc.RegisterName("supportsFamily:")) {
|
|
|
|
// https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf
|
|
|
|
g.maxImageSize = 8192
|
|
|
|
switch {
|
|
|
|
case d.SupportsFamily(mtl.GPUFamilyApple3):
|
|
|
|
g.maxImageSize = 16384
|
|
|
|
case d.SupportsFamily(mtl.GPUFamilyMac2):
|
|
|
|
g.maxImageSize = 16384
|
|
|
|
}
|
|
|
|
return g.maxImageSize
|
|
|
|
}
|
|
|
|
|
2023-10-27 19:43:57 +02:00
|
|
|
// supportsFeatureSet is deprecated but some old macOS/iOS versions support only this (#2553).
|
2021-07-04 13:35:56 +02:00
|
|
|
switch {
|
2023-01-23 15:38:53 +01:00
|
|
|
case d.SupportsFeatureSet(mtl.FeatureSet_iOS_GPUFamily5_v1):
|
|
|
|
g.maxImageSize = 16384
|
|
|
|
case d.SupportsFeatureSet(mtl.FeatureSet_iOS_GPUFamily4_v1):
|
|
|
|
g.maxImageSize = 16384
|
|
|
|
case d.SupportsFeatureSet(mtl.FeatureSet_iOS_GPUFamily3_v1):
|
2021-07-04 13:35:56 +02:00
|
|
|
g.maxImageSize = 16384
|
2023-01-23 15:38:53 +01:00
|
|
|
case d.SupportsFeatureSet(mtl.FeatureSet_iOS_GPUFamily2_v2):
|
|
|
|
g.maxImageSize = 8192
|
|
|
|
case d.SupportsFeatureSet(mtl.FeatureSet_iOS_GPUFamily2_v1):
|
|
|
|
g.maxImageSize = 4096
|
|
|
|
case d.SupportsFeatureSet(mtl.FeatureSet_iOS_GPUFamily1_v2):
|
|
|
|
g.maxImageSize = 8192
|
|
|
|
case d.SupportsFeatureSet(mtl.FeatureSet_iOS_GPUFamily1_v1):
|
|
|
|
g.maxImageSize = 4096
|
|
|
|
case d.SupportsFeatureSet(mtl.FeatureSet_tvOS_GPUFamily2_v1):
|
2021-07-04 13:35:56 +02:00
|
|
|
g.maxImageSize = 16384
|
2023-01-23 15:38:53 +01:00
|
|
|
case d.SupportsFeatureSet(mtl.FeatureSet_tvOS_GPUFamily1_v1):
|
|
|
|
g.maxImageSize = 8192
|
|
|
|
case d.SupportsFeatureSet(mtl.FeatureSet_macOS_GPUFamily1_v1):
|
|
|
|
g.maxImageSize = 16384
|
|
|
|
default:
|
|
|
|
panic("metal: there is no supported feature set")
|
2020-10-12 17:39:45 +02:00
|
|
|
}
|
|
|
|
return g.maxImageSize
|
2019-07-03 18:10:08 +02:00
|
|
|
}
|
|
|
|
|
2022-02-06 12:41:32 +01:00
|
|
|
func (g *Graphics) NewShader(program *shaderir.Program) (graphicsdriver.Shader, error) {
|
2020-10-12 17:39:45 +02:00
|
|
|
s, err := newShader(g.view.getMTLDevice(), g.genNextShaderID(), program)
|
|
|
|
if err != nil {
|
2020-08-03 17:56:45 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
g.addShader(s)
|
|
|
|
return s, nil
|
2020-05-17 20:45:58 +02:00
|
|
|
}
|
|
|
|
|
2020-08-03 17:56:45 +02:00
|
|
|
func (g *Graphics) addShader(shader *Shader) {
|
|
|
|
if g.shaders == nil {
|
2022-02-06 12:41:32 +01:00
|
|
|
g.shaders = map[graphicsdriver.ShaderID]*Shader{}
|
2020-08-03 17:56:45 +02:00
|
|
|
}
|
|
|
|
if _, ok := g.shaders[shader.id]; ok {
|
|
|
|
panic(fmt.Sprintf("metal: shader ID %d was already registered", shader.id))
|
|
|
|
}
|
|
|
|
g.shaders[shader.id] = shader
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Graphics) removeShader(shader *Shader) {
|
|
|
|
delete(g.shaders, shader.id)
|
2020-05-17 20:45:58 +02:00
|
|
|
}
|
|
|
|
|
2018-11-12 16:00:10 +01:00
|
|
|
type Image struct {
|
2022-02-06 12:41:32 +01:00
|
|
|
id graphicsdriver.ImageID
|
2020-04-04 10:12:24 +02:00
|
|
|
graphics *Graphics
|
|
|
|
width int
|
|
|
|
height int
|
|
|
|
screen bool
|
|
|
|
texture mtl.Texture
|
2021-07-02 12:26:09 +02:00
|
|
|
stencil mtl.Texture
|
2018-11-12 16:00:10 +01:00
|
|
|
}
|
|
|
|
|
2022-02-06 12:41:32 +01:00
|
|
|
func (i *Image) ID() graphicsdriver.ImageID {
|
2020-05-19 16:48:43 +02:00
|
|
|
return i.id
|
|
|
|
}
|
|
|
|
|
2020-08-08 09:07:03 +02:00
|
|
|
func (i *Image) internalSize() (int, int) {
|
2018-11-12 16:00:10 +01:00
|
|
|
if i.screen {
|
|
|
|
return i.width, i.height
|
|
|
|
}
|
2019-02-14 13:04:34 +01:00
|
|
|
return graphics.InternalImageSize(i.width), graphics.InternalImageSize(i.height)
|
2018-11-12 16:00:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Image) Dispose() {
|
2021-07-02 12:26:09 +02:00
|
|
|
if i.stencil != (mtl.Texture{}) {
|
|
|
|
i.stencil.Release()
|
|
|
|
i.stencil = mtl.Texture{}
|
|
|
|
}
|
2020-10-12 17:39:45 +02:00
|
|
|
if i.texture != (mtl.Texture{}) {
|
|
|
|
i.texture.Release()
|
|
|
|
i.texture = mtl.Texture{}
|
|
|
|
}
|
2020-05-19 16:48:43 +02:00
|
|
|
i.graphics.removeImage(i)
|
2018-11-12 16:00:10 +01:00
|
|
|
}
|
|
|
|
|
2021-02-26 15:00:10 +01:00
|
|
|
func (i *Image) syncTexture() {
|
2021-07-04 11:13:18 +02:00
|
|
|
i.graphics.flushRenderCommandEncoderIfNeeded()
|
|
|
|
|
2020-11-03 15:44:40 +01:00
|
|
|
// Calling SynchronizeTexture is ignored on iOS (see mtl.m), but it looks like committing BlitCommandEncoder
|
2020-09-06 09:51:44 +02:00
|
|
|
// is necessary (#1337).
|
2020-10-12 17:39:45 +02:00
|
|
|
if i.graphics.cb != (mtl.CommandBuffer{}) {
|
|
|
|
panic("metal: command buffer must be empty at syncTexture: flushIfNeeded is not called yet?")
|
|
|
|
}
|
2018-11-12 16:00:10 +01:00
|
|
|
|
2024-05-06 14:38:59 +02:00
|
|
|
cb := i.graphics.cq.CommandBuffer()
|
|
|
|
bce := cb.BlitCommandEncoder()
|
2020-10-12 17:39:45 +02:00
|
|
|
bce.SynchronizeTexture(i.texture, 0, 0)
|
|
|
|
bce.EndEncoding()
|
2020-11-03 18:45:41 +01:00
|
|
|
|
2020-10-12 17:39:45 +02:00
|
|
|
cb.Commit()
|
2022-07-04 04:50:06 +02:00
|
|
|
// TODO: Are fences available here?
|
2021-02-26 15:00:10 +01:00
|
|
|
cb.WaitUntilCompleted()
|
2018-11-12 16:00:10 +01:00
|
|
|
}
|
|
|
|
|
2023-08-16 16:18:37 +02:00
|
|
|
func (i *Image) ReadPixels(args []graphicsdriver.PixelsArgs) error {
|
2021-02-26 15:00:10 +01:00
|
|
|
i.graphics.flushIfNeeded(false)
|
|
|
|
i.syncTexture()
|
2018-11-12 16:00:10 +01:00
|
|
|
|
2023-08-16 16:18:37 +02:00
|
|
|
for _, arg := range args {
|
|
|
|
if got, want := len(arg.Pixels), 4*arg.Region.Dx()*arg.Region.Dy(); got != want {
|
|
|
|
return fmt.Errorf("metal: len(buf) must be %d but %d at ReadPixels", want, got)
|
|
|
|
}
|
|
|
|
i.texture.GetBytes(&arg.Pixels[0], uintptr(4*arg.Region.Dx()), mtl.Region{
|
|
|
|
Origin: mtl.Origin{X: arg.Region.Min.X, Y: arg.Region.Min.Y},
|
|
|
|
Size: mtl.Size{Width: arg.Region.Dx(), Height: arg.Region.Dy(), Depth: 1},
|
|
|
|
}, 0)
|
|
|
|
}
|
2022-02-27 09:41:19 +01:00
|
|
|
return nil
|
2018-11-12 16:00:10 +01:00
|
|
|
}
|
|
|
|
|
2023-08-16 16:18:37 +02:00
|
|
|
func (i *Image) WritePixels(args []graphicsdriver.PixelsArgs) error {
|
2020-04-04 10:12:24 +02:00
|
|
|
g := i.graphics
|
2020-10-12 17:39:45 +02:00
|
|
|
|
2021-07-04 11:13:18 +02:00
|
|
|
g.flushRenderCommandEncoderIfNeeded()
|
|
|
|
|
2021-02-26 19:40:36 +01:00
|
|
|
// Calculate the smallest texture size to include all the values in args.
|
2023-04-27 17:00:51 +02:00
|
|
|
var region image.Rectangle
|
2021-02-26 19:40:36 +01:00
|
|
|
for _, a := range args {
|
2023-04-27 17:00:51 +02:00
|
|
|
region = region.Union(a.Region)
|
2021-02-26 19:40:36 +01:00
|
|
|
}
|
|
|
|
|
2023-01-28 11:06:38 +01:00
|
|
|
// Use a temporary texture to send pixels asynchronously, whichever the memory is shared (e.g., iOS) or
|
2021-02-26 20:03:59 +01:00
|
|
|
// managed (e.g., macOS). A temporary texture is needed since ReplaceRegion tries to sync the pixel
|
|
|
|
// data between CPU and GPU, and doing it on the existing texture is inefficient (#1418).
|
2021-02-26 19:40:36 +01:00
|
|
|
// The texture cannot be reused until sending the pixels finishes, then create new ones for each call.
|
2020-11-10 14:48:01 +01:00
|
|
|
td := mtl.TextureDescriptor{
|
|
|
|
TextureType: mtl.TextureType2D,
|
|
|
|
PixelFormat: mtl.PixelFormatRGBA8UNorm,
|
2023-04-27 17:00:51 +02:00
|
|
|
Width: region.Dx(),
|
|
|
|
Height: region.Dy(),
|
2020-11-10 14:48:01 +01:00
|
|
|
StorageMode: storageMode,
|
|
|
|
Usage: mtl.TextureUsageShaderRead | mtl.TextureUsageRenderTarget,
|
2020-10-12 17:39:45 +02:00
|
|
|
}
|
2024-05-06 14:38:59 +02:00
|
|
|
t := g.view.getMTLDevice().NewTextureWithDescriptor(td)
|
2020-11-10 14:48:01 +01:00
|
|
|
g.tmpTextures = append(g.tmpTextures, t)
|
2020-10-12 19:12:02 +02:00
|
|
|
|
2020-10-12 17:39:45 +02:00
|
|
|
for _, a := range args {
|
2020-11-10 14:48:01 +01:00
|
|
|
t.ReplaceRegion(mtl.Region{
|
2023-04-27 17:00:51 +02:00
|
|
|
Origin: mtl.Origin{X: a.Region.Min.X - region.Min.X, Y: a.Region.Min.Y - region.Min.Y, Z: 0},
|
|
|
|
Size: mtl.Size{Width: a.Region.Dx(), Height: a.Region.Dy(), Depth: 1},
|
|
|
|
}, 0, unsafe.Pointer(&a.Pixels[0]), 4*a.Region.Dx())
|
2020-10-12 17:39:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if g.cb == (mtl.CommandBuffer{}) {
|
2024-05-06 14:38:59 +02:00
|
|
|
g.cb = i.graphics.cq.CommandBuffer()
|
2020-10-12 17:39:45 +02:00
|
|
|
}
|
2024-05-06 14:38:59 +02:00
|
|
|
bce := g.cb.BlitCommandEncoder()
|
2020-10-12 17:39:45 +02:00
|
|
|
for _, a := range args {
|
2023-04-27 17:00:51 +02:00
|
|
|
so := mtl.Origin{X: a.Region.Min.X - region.Min.X, Y: a.Region.Min.Y - region.Min.Y, Z: 0}
|
|
|
|
ss := mtl.Size{Width: a.Region.Dx(), Height: a.Region.Dy(), Depth: 1}
|
|
|
|
do := mtl.Origin{X: a.Region.Min.X, Y: a.Region.Min.Y, Z: 0}
|
2021-02-26 19:40:36 +01:00
|
|
|
bce.CopyFromTexture(t, 0, 0, so, ss, i.texture, 0, 0, do)
|
2020-10-12 17:39:45 +02:00
|
|
|
}
|
|
|
|
bce.EndEncoding()
|
2022-03-21 14:23:07 +01:00
|
|
|
|
|
|
|
return nil
|
2018-11-12 16:00:10 +01:00
|
|
|
}
|
2021-07-04 11:13:18 +02:00
|
|
|
|
|
|
|
func (i *Image) mtlTexture() mtl.Texture {
|
|
|
|
if i.screen {
|
|
|
|
g := i.graphics
|
|
|
|
if g.screenDrawable == (ca.MetalDrawable{}) {
|
2021-07-06 17:38:50 +02:00
|
|
|
drawable := g.view.nextDrawable()
|
2021-07-04 11:13:18 +02:00
|
|
|
if drawable == (ca.MetalDrawable{}) {
|
|
|
|
return mtl.Texture{}
|
|
|
|
}
|
|
|
|
g.screenDrawable = drawable
|
2021-07-07 19:09:04 +02:00
|
|
|
// After nextDrawable, it is expected some command buffers are completed.
|
|
|
|
g.gcBuffers()
|
2021-07-04 11:13:18 +02:00
|
|
|
}
|
|
|
|
return g.screenDrawable.Texture()
|
|
|
|
}
|
|
|
|
return i.texture
|
|
|
|
}
|
2021-07-02 12:26:09 +02:00
|
|
|
|
|
|
|
func (i *Image) ensureStencil() {
|
|
|
|
if i.stencil != (mtl.Texture{}) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
td := mtl.TextureDescriptor{
|
|
|
|
TextureType: mtl.TextureType2D,
|
|
|
|
PixelFormat: mtl.PixelFormatStencil8,
|
|
|
|
Width: graphics.InternalImageSize(i.width),
|
|
|
|
Height: graphics.InternalImageSize(i.height),
|
|
|
|
StorageMode: mtl.StorageModePrivate,
|
|
|
|
Usage: mtl.TextureUsageRenderTarget,
|
|
|
|
}
|
2024-05-06 14:38:59 +02:00
|
|
|
i.stencil = i.graphics.view.getMTLDevice().NewTextureWithDescriptor(td)
|
2021-07-02 12:26:09 +02:00
|
|
|
}
|