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"
|
2021-02-26 19:40:36 +01:00
|
|
|
"math"
|
2021-07-06 10:25:11 +02:00
|
|
|
"sort"
|
2018-11-12 16:00:10 +01:00
|
|
|
"unsafe"
|
|
|
|
|
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
|
|
|
|
2021-07-07 18:56:12 +02:00
|
|
|
lastDst *Image
|
2021-07-06 07:05:01 +02:00
|
|
|
lastStencilMode stencilMode
|
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 (
|
|
|
|
prepareStencil stencilMode = iota
|
|
|
|
drawWithStencil
|
|
|
|
noStencil
|
|
|
|
)
|
|
|
|
|
2022-06-17 07:56:31 +02:00
|
|
|
var creatingSystemDefaultDeviceSucceeded bool
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
// mtl.CreateSystemDefaultDevice must be called on the main thread (#2147).
|
|
|
|
_, ok := mtl.CreateSystemDefaultDevice()
|
|
|
|
creatingSystemDefaultDeviceSucceeded = ok
|
|
|
|
}
|
|
|
|
|
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.
|
2022-06-17 07:56:31 +02:00
|
|
|
if !creatingSystemDefaultDeviceSucceeded {
|
2022-06-16 19:10:29 +02:00
|
|
|
return nil, fmt.Errorf("metal: mtl.CreateSystemDefaultDevice failed")
|
2022-02-11 15:11:49 +01:00
|
|
|
}
|
2022-06-16 19:10:29 +02:00
|
|
|
|
|
|
|
return &Graphics{}, 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 {
|
|
|
|
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{}) {
|
|
|
|
g.cb = g.cq.MakeCommandBuffer()
|
|
|
|
}
|
|
|
|
|
|
|
|
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{}) {
|
|
|
|
newBuf = g.view.getMTLDevice().MakeBufferWithLength(pow2(length), resourceStorageMode)
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
2022-03-21 14:23:07 +01:00
|
|
|
func (g *Graphics) SetVertices(vertices []float32, indices []uint16) 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) {
|
2020-10-12 17:39:45 +02:00
|
|
|
if g.cb == (mtl.CommandBuffer{}) {
|
|
|
|
return
|
|
|
|
}
|
2021-07-04 11:37:14 +02:00
|
|
|
g.flushRenderCommandEncoderIfNeeded()
|
2018-11-12 16:00:10 +01:00
|
|
|
|
2021-07-07 21:21:20 +02:00
|
|
|
if !g.view.presentsWithTransaction() && present && g.screenDrawable != (ca.MetalDrawable{}) {
|
2020-10-12 17:39:45 +02:00
|
|
|
g.cb.PresentDrawable(g.screenDrawable)
|
|
|
|
}
|
|
|
|
g.cb.Commit()
|
2021-07-07 21:21:20 +02:00
|
|
|
if g.view.presentsWithTransaction() && present && g.screenDrawable != (ca.MetalDrawable{}) {
|
|
|
|
g.cb.WaitUntilScheduled()
|
|
|
|
g.screenDrawable.Present()
|
|
|
|
}
|
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
|
|
|
}
|
2020-10-12 17:39:45 +02:00
|
|
|
t := g.view.getMTLDevice().MakeTexture(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-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.BlendFactorDestinationAlpha:
|
2020-08-03 17:56:45 +02:00
|
|
|
return mtl.BlendFactorDestinationAlpha
|
2022-10-15 14:14:59 +02:00
|
|
|
case graphicsdriver.BlendFactorOneMinusSourceAlpha:
|
2020-08-03 17:56:45 +02:00
|
|
|
return mtl.BlendFactorOneMinusSourceAlpha
|
2022-10-15 14:14:59 +02:00
|
|
|
case graphicsdriver.BlendFactorOneMinusDestinationAlpha:
|
2020-08-03 17:56:45 +02:00
|
|
|
return mtl.BlendFactorOneMinusDestinationAlpha
|
2022-10-15 14:14:59 +02:00
|
|
|
case graphicsdriver.BlendFactorDestinationColor:
|
2020-08-03 17:56:45 +02:00
|
|
|
return mtl.BlendFactorDestinationColor
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("metal: invalid operation: %d", c))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-03-22 17:22:39 +01:00
|
|
|
if err := g.view.initialize(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
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).
|
|
|
|
g.dsss[prepareStencil] = g.view.getMTLDevice().MakeDepthStencilState(mtl.DepthStencilDescriptor{
|
|
|
|
BackFaceStencil: mtl.StencilDescriptor{
|
|
|
|
StencilFailureOperation: mtl.StencilOperationKeep,
|
|
|
|
DepthFailureOperation: mtl.StencilOperationKeep,
|
|
|
|
DepthStencilPassOperation: mtl.StencilOperationInvert,
|
|
|
|
StencilCompareFunction: mtl.CompareFunctionAlways,
|
|
|
|
},
|
|
|
|
FrontFaceStencil: mtl.StencilDescriptor{
|
|
|
|
StencilFailureOperation: mtl.StencilOperationKeep,
|
|
|
|
DepthFailureOperation: mtl.StencilOperationKeep,
|
|
|
|
DepthStencilPassOperation: mtl.StencilOperationInvert,
|
|
|
|
StencilCompareFunction: mtl.CompareFunctionAlways,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
g.dsss[drawWithStencil] = g.view.getMTLDevice().MakeDepthStencilState(mtl.DepthStencilDescriptor{
|
|
|
|
BackFaceStencil: mtl.StencilDescriptor{
|
|
|
|
StencilFailureOperation: mtl.StencilOperationKeep,
|
|
|
|
DepthFailureOperation: mtl.StencilOperationKeep,
|
2022-01-05 19:19:46 +01:00
|
|
|
DepthStencilPassOperation: mtl.StencilOperationKeep,
|
2021-07-06 08:05:44 +02:00
|
|
|
StencilCompareFunction: mtl.CompareFunctionNotEqual,
|
|
|
|
},
|
|
|
|
FrontFaceStencil: mtl.StencilDescriptor{
|
|
|
|
StencilFailureOperation: mtl.StencilOperationKeep,
|
|
|
|
DepthFailureOperation: mtl.StencilOperationKeep,
|
2022-01-05 19:19:46 +01:00
|
|
|
DepthStencilPassOperation: mtl.StencilOperationKeep,
|
2021-07-06 08:05:44 +02:00
|
|
|
StencilCompareFunction: mtl.CompareFunctionNotEqual,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
g.dsss[noStencil] = g.view.getMTLDevice().MakeDepthStencilState(mtl.DepthStencilDescriptor{
|
|
|
|
BackFaceStencil: mtl.StencilDescriptor{
|
|
|
|
StencilFailureOperation: mtl.StencilOperationKeep,
|
|
|
|
DepthFailureOperation: mtl.StencilOperationKeep,
|
|
|
|
DepthStencilPassOperation: mtl.StencilOperationKeep,
|
|
|
|
StencilCompareFunction: mtl.CompareFunctionAlways,
|
|
|
|
},
|
|
|
|
FrontFaceStencil: mtl.StencilDescriptor{
|
|
|
|
StencilFailureOperation: mtl.StencilOperationKeep,
|
|
|
|
DepthFailureOperation: mtl.StencilOperationKeep,
|
|
|
|
DepthStencilPassOperation: mtl.StencilOperationKeep,
|
|
|
|
StencilCompareFunction: mtl.CompareFunctionAlways,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2020-10-12 17:39:45 +02:00
|
|
|
g.cq = g.view.getMTLDevice().MakeCommandQueue()
|
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
|
|
|
}
|
|
|
|
|
2022-07-12 18:46:02 +02:00
|
|
|
func (g *Graphics) draw(rps mtl.RenderPipelineState, dst *Image, dstRegion graphicsdriver.Region, srcs [graphics.ShaderImageCount]*Image, indexLen int, indexOffset int, uniforms [][]float32, stencilMode stencilMode) error {
|
2021-07-05 11:08:52 +02:00
|
|
|
// When prepareing a stencil buffer, flush the current render command encoder
|
|
|
|
// to make sure the stencil buffer is cleared when loading.
|
|
|
|
// TODO: What about clearing the stencil buffer by vertices?
|
2021-07-07 18:56:12 +02:00
|
|
|
if g.lastDst != dst || (g.lastStencilMode == noStencil) != (stencilMode == noStencil) || stencilMode == prepareStencil {
|
2021-07-04 11:13:18 +02:00
|
|
|
g.flushRenderCommandEncoderIfNeeded()
|
|
|
|
}
|
2021-07-07 18:56:12 +02:00
|
|
|
g.lastDst = dst
|
2021-07-06 07:05:01 +02:00
|
|
|
g.lastStencilMode = stencilMode
|
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
|
|
|
|
2021-07-05 11:08:52 +02:00
|
|
|
if stencilMode == prepareStencil {
|
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{}) {
|
|
|
|
g.cb = g.cq.MakeCommandBuffer()
|
2018-11-12 16:00:10 +01:00
|
|
|
}
|
2021-07-04 11:13:18 +02:00
|
|
|
g.rce = g.cb.MakeRenderCommandEncoder(rpd)
|
2020-08-07 20:01:23 +02:00
|
|
|
}
|
2018-11-12 16:00:10 +01:00
|
|
|
|
2021-07-04 11:13:18 +02:00
|
|
|
g.rce.SetRenderPipelineState(rps)
|
2020-08-07 20:01:23 +02: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.SetScissorRect(mtl.ScissorRect{
|
2020-11-07 11:14:06 +01:00
|
|
|
X: int(dstRegion.X),
|
|
|
|
Y: int(dstRegion.Y),
|
|
|
|
Width: int(dstRegion.Width),
|
|
|
|
Height: int(dstRegion.Height),
|
|
|
|
})
|
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-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
|
|
|
|
2021-07-06 08:05:44 +02:00
|
|
|
g.rce.SetDepthStencilState(g.dsss[stencilMode])
|
2021-07-02 12:26:09 +02:00
|
|
|
|
2021-07-04 11:13:18 +02:00
|
|
|
g.rce.DrawIndexedPrimitives(mtl.PrimitiveTypeTriangle, indexLen, mtl.IndexTypeUInt16, g.ib, indexOffset*2)
|
2021-07-02 12:26:09 +02:00
|
|
|
|
2020-08-07 20:01:23 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-02 15:13:29 +02:00
|
|
|
func (g *Graphics) DrawTriangles(dstID graphicsdriver.ImageID, srcIDs [graphics.ShaderImageCount]graphicsdriver.ImageID, offsets [graphics.ShaderImageCount - 1][2]float32, shaderID graphicsdriver.ShaderID, indexLen int, indexOffset int, mode graphicsdriver.CompositeMode, dstRegion, srcRegion graphicsdriver.Region, uniforms [][]float32, evenOdd bool) error {
|
2022-10-02 12:23:54 +02:00
|
|
|
if shaderID == graphicsdriver.InvalidShaderID {
|
|
|
|
return fmt.Errorf("metal: shader ID is invalid")
|
|
|
|
}
|
|
|
|
|
2020-08-07 20:01:23 +02:00
|
|
|
dst := g.images[dstID]
|
2020-11-03 18:45:41 +01:00
|
|
|
|
2021-07-04 10:24:39 +02:00
|
|
|
if dst.screen {
|
|
|
|
g.view.update()
|
|
|
|
}
|
|
|
|
|
2022-07-12 18:46:02 +02:00
|
|
|
var srcs [graphics.ShaderImageCount]*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-10-03 17:29:18 +02:00
|
|
|
uniformVars := make([][]float32, graphics.PreservedUniformVariablesCount+len(uniforms))
|
2021-07-01 17:25:09 +02:00
|
|
|
|
2022-10-02 12:23:54 +02:00
|
|
|
// Set the destination texture size.
|
|
|
|
dw, dh := dst.internalSize()
|
|
|
|
uniformVars[graphics.TextureDestinationSizeUniformVariableIndex] = []float32{float32(dw), float32(dh)}
|
2021-07-01 17:25:09 +02:00
|
|
|
|
2022-10-02 12:23:54 +02:00
|
|
|
// Set the source texture sizes.
|
|
|
|
usizes := make([]float32, 2*len(srcs))
|
|
|
|
for i, src := range srcs {
|
|
|
|
if src != nil {
|
|
|
|
w, h := src.internalSize()
|
|
|
|
usizes[2*i] = float32(w)
|
|
|
|
usizes[2*i+1] = float32(h)
|
2022-04-03 18:55:51 +02:00
|
|
|
}
|
2022-10-02 12:23:54 +02:00
|
|
|
}
|
|
|
|
uniformVars[graphics.TextureSourceSizesUniformVariableIndex] = usizes
|
|
|
|
|
|
|
|
// Set the destination region's origin.
|
|
|
|
udorigin := []float32{float32(dstRegion.X) / float32(dw), float32(dstRegion.Y) / float32(dh)}
|
|
|
|
uniformVars[graphics.TextureDestinationRegionOriginUniformVariableIndex] = udorigin
|
|
|
|
|
|
|
|
// Set the destination region's size.
|
|
|
|
udsize := []float32{float32(dstRegion.Width) / float32(dw), float32(dstRegion.Height) / float32(dh)}
|
|
|
|
uniformVars[graphics.TextureDestinationRegionSizeUniformVariableIndex] = udsize
|
|
|
|
|
|
|
|
// Set the source offsets.
|
|
|
|
uoffsets := make([]float32, 2*len(offsets))
|
|
|
|
for i, offset := range offsets {
|
|
|
|
uoffsets[2*i] = offset[0]
|
|
|
|
uoffsets[2*i+1] = offset[1]
|
|
|
|
}
|
|
|
|
uniformVars[graphics.TextureSourceOffsetsUniformVariableIndex] = uoffsets
|
|
|
|
|
|
|
|
// Set the source region's origin of texture0.
|
|
|
|
usorigin := []float32{float32(srcRegion.X), float32(srcRegion.Y)}
|
|
|
|
uniformVars[graphics.TextureSourceRegionOriginUniformVariableIndex] = usorigin
|
|
|
|
|
|
|
|
// Set the source region's size of texture0.
|
|
|
|
ussize := []float32{float32(srcRegion.Width), float32(srcRegion.Height)}
|
|
|
|
uniformVars[graphics.TextureSourceRegionSizeUniformVariableIndex] = ussize
|
|
|
|
|
|
|
|
uniformVars[graphics.ProjectionMatrixUniformVariableIndex] = []float32{
|
|
|
|
2 / float32(dw), 0, 0, 0,
|
|
|
|
0, -2 / float32(dh), 0, 0,
|
|
|
|
0, 0, 1, 0,
|
|
|
|
-1, 1, 0, 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the additional uniform variables.
|
|
|
|
for i, v := range uniforms {
|
|
|
|
const offset = graphics.PreservedUniformVariablesCount
|
|
|
|
t := g.shaders[shaderID].ir.Uniforms[offset+i]
|
|
|
|
switch t.Main {
|
|
|
|
case shaderir.Mat3:
|
|
|
|
// float3x3 requires 16-byte alignment (#2036).
|
|
|
|
v1 := make([]float32, 12)
|
|
|
|
copy(v1[0:3], v[0:3])
|
|
|
|
copy(v1[4:7], v[3:6])
|
|
|
|
copy(v1[8:11], v[6:9])
|
|
|
|
uniformVars[offset+i] = v1
|
|
|
|
case shaderir.Array:
|
|
|
|
switch t.Sub[0].Main {
|
2022-03-26 15:46:37 +01:00
|
|
|
case shaderir.Mat3:
|
2022-10-02 12:23:54 +02:00
|
|
|
v1 := make([]float32, t.Length*12)
|
|
|
|
for j := 0; j < t.Length; j++ {
|
|
|
|
offset0 := j * 9
|
|
|
|
offset1 := j * 12
|
|
|
|
copy(v1[offset1:offset1+3], v[offset0:offset0+3])
|
|
|
|
copy(v1[offset1+4:offset1+7], v[offset0+3:offset0+6])
|
|
|
|
copy(v1[offset1+8:offset1+11], v[offset0+6:offset0+9])
|
2022-03-26 16:12:10 +01:00
|
|
|
}
|
2022-10-02 12:23:54 +02:00
|
|
|
uniformVars[offset+i] = v1
|
2022-03-26 15:46:37 +01:00
|
|
|
default:
|
|
|
|
uniformVars[offset+i] = v
|
|
|
|
}
|
2022-10-02 12:23:54 +02:00
|
|
|
default:
|
|
|
|
uniformVars[offset+i] = v
|
2021-07-01 17:25:09 +02:00
|
|
|
}
|
2020-08-07 20:01:23 +02:00
|
|
|
}
|
|
|
|
|
2021-07-02 12:26:09 +02:00
|
|
|
if evenOdd {
|
2022-10-03 17:29:18 +02:00
|
|
|
prepareStencilRpss, err := g.shaders[shaderID].RenderPipelineState(&g.view, mode, prepareStencil, dst.screen)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := g.draw(prepareStencilRpss, dst, dstRegion, srcs, indexLen, indexOffset, uniformVars, prepareStencil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
drawWithStencilRpss, err := g.shaders[shaderID].RenderPipelineState(&g.view, mode, drawWithStencil, dst.screen)
|
|
|
|
if err != nil {
|
2021-07-02 12:26:09 +02:00
|
|
|
return err
|
|
|
|
}
|
2022-10-03 17:29:18 +02:00
|
|
|
if err := g.draw(drawWithStencilRpss, dst, dstRegion, srcs, indexLen, indexOffset, uniformVars, drawWithStencil); err != nil {
|
2021-07-02 12:26:09 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2022-10-03 17:29:18 +02:00
|
|
|
rpss, err := g.shaders[shaderID].RenderPipelineState(&g.view, mode, noStencil, dst.screen)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := g.draw(rpss, dst, dstRegion, srcs, indexLen, indexOffset, uniformVars, noStencil); err != nil {
|
2021-07-02 12:26:09 +02: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-08-08 08:38:41 +02:00
|
|
|
func (g *Graphics) SetFullscreen(fullscreen bool) {
|
|
|
|
g.view.setFullscreen(fullscreen)
|
|
|
|
}
|
|
|
|
|
2020-04-04 10:12:24 +02:00
|
|
|
func (g *Graphics) NeedsRestoring() bool {
|
2019-05-26 12:08:46 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
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) IsGL() bool {
|
2018-11-12 16:00:10 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-02-11 12:38:45 +01:00
|
|
|
func (g *Graphics) IsDirectX() 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
|
|
|
|
}
|
|
|
|
|
|
|
|
g.maxImageSize = 4096
|
|
|
|
// https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf
|
|
|
|
switch {
|
|
|
|
case g.view.getMTLDevice().SupportsFeatureSet(mtl.FeatureSet_iOS_GPUFamily5_v1):
|
|
|
|
g.maxImageSize = 16384
|
|
|
|
case g.view.getMTLDevice().SupportsFeatureSet(mtl.FeatureSet_iOS_GPUFamily4_v1):
|
|
|
|
g.maxImageSize = 16384
|
|
|
|
case g.view.getMTLDevice().SupportsFeatureSet(mtl.FeatureSet_iOS_GPUFamily3_v1):
|
|
|
|
g.maxImageSize = 16384
|
|
|
|
case g.view.getMTLDevice().SupportsFeatureSet(mtl.FeatureSet_iOS_GPUFamily2_v2):
|
|
|
|
g.maxImageSize = 8192
|
|
|
|
case g.view.getMTLDevice().SupportsFeatureSet(mtl.FeatureSet_iOS_GPUFamily2_v1):
|
2020-10-12 17:39:45 +02:00
|
|
|
g.maxImageSize = 4096
|
2021-07-04 13:35:56 +02:00
|
|
|
case g.view.getMTLDevice().SupportsFeatureSet(mtl.FeatureSet_iOS_GPUFamily1_v2):
|
|
|
|
g.maxImageSize = 8192
|
|
|
|
case g.view.getMTLDevice().SupportsFeatureSet(mtl.FeatureSet_iOS_GPUFamily1_v1):
|
|
|
|
g.maxImageSize = 4096
|
|
|
|
case g.view.getMTLDevice().SupportsFeatureSet(mtl.FeatureSet_tvOS_GPUFamily2_v1):
|
|
|
|
g.maxImageSize = 16384
|
|
|
|
case g.view.getMTLDevice().SupportsFeatureSet(mtl.FeatureSet_tvOS_GPUFamily1_v1):
|
|
|
|
g.maxImageSize = 8192
|
|
|
|
case g.view.getMTLDevice().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
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Image) IsInvalidated() bool {
|
|
|
|
// TODO: Does Metal cause context lost?
|
|
|
|
// https://developer.apple.com/documentation/metal/mtlresource/1515898-setpurgeablestate
|
|
|
|
// https://developer.apple.com/documentation/metal/mtldevicenotificationhandler
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2020-10-12 17:39:45 +02:00
|
|
|
cb := i.graphics.cq.MakeCommandBuffer()
|
|
|
|
bce := cb.MakeBlitCommandEncoder()
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-08-27 14:22:31 +02:00
|
|
|
func (i *Image) ReadPixels(buf []byte, x, y, width, height int) error {
|
|
|
|
if got, want := len(buf), 4*width*height; got != want {
|
2022-02-27 09:41:19 +01:00
|
|
|
return fmt.Errorf("metal: len(buf) must be %d but %d at ReadPixels", want, got)
|
|
|
|
}
|
|
|
|
|
2021-02-26 15:00:10 +01:00
|
|
|
i.graphics.flushIfNeeded(false)
|
|
|
|
i.syncTexture()
|
2018-11-12 16:00:10 +01:00
|
|
|
|
2022-08-27 14:22:31 +02:00
|
|
|
i.texture.GetBytes(&buf[0], uintptr(4*width), mtl.Region{
|
|
|
|
Origin: mtl.Origin{X: x, Y: y},
|
|
|
|
Size: mtl.Size{Width: width, Height: height, Depth: 1},
|
2020-10-12 17:39:45 +02:00
|
|
|
}, 0)
|
2022-02-27 09:41:19 +01:00
|
|
|
return nil
|
2018-11-12 16:00:10 +01:00
|
|
|
}
|
|
|
|
|
2022-08-07 20:02:12 +02:00
|
|
|
func (i *Image) WritePixels(args []*graphicsdriver.WritePixelsArgs) 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.
|
|
|
|
minX := math.MaxInt32
|
|
|
|
minY := math.MaxInt32
|
|
|
|
maxX := 0
|
|
|
|
maxY := 0
|
|
|
|
for _, a := range args {
|
|
|
|
if minX > a.X {
|
|
|
|
minX = a.X
|
|
|
|
}
|
|
|
|
if maxX < a.X+a.Width {
|
|
|
|
maxX = a.X + a.Width
|
|
|
|
}
|
|
|
|
if minY > a.Y {
|
|
|
|
minY = a.Y
|
|
|
|
}
|
|
|
|
if maxY < a.Y+a.Height {
|
|
|
|
maxY = a.Y + a.Height
|
|
|
|
}
|
|
|
|
}
|
|
|
|
w := maxX - minX
|
|
|
|
h := maxY - minY
|
|
|
|
|
2020-11-10 14:48:01 +01:00
|
|
|
// Use a temporary texture to send pixels asynchrounsly, 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,
|
|
|
|
Width: w,
|
|
|
|
Height: h,
|
|
|
|
StorageMode: storageMode,
|
|
|
|
Usage: mtl.TextureUsageShaderRead | mtl.TextureUsageRenderTarget,
|
2020-10-12 17:39:45 +02:00
|
|
|
}
|
2020-11-10 14:48:01 +01:00
|
|
|
t := g.view.getMTLDevice().MakeTexture(td)
|
|
|
|
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{
|
2021-02-26 19:40:36 +01:00
|
|
|
Origin: mtl.Origin{X: a.X - minX, Y: a.Y - minY, Z: 0},
|
2020-10-12 17:39:45 +02:00
|
|
|
Size: mtl.Size{Width: a.Width, Height: a.Height, Depth: 1},
|
|
|
|
}, 0, unsafe.Pointer(&a.Pixels[0]), 4*a.Width)
|
|
|
|
}
|
|
|
|
|
|
|
|
if g.cb == (mtl.CommandBuffer{}) {
|
|
|
|
g.cb = i.graphics.cq.MakeCommandBuffer()
|
|
|
|
}
|
|
|
|
bce := g.cb.MakeBlitCommandEncoder()
|
|
|
|
for _, a := range args {
|
2021-02-26 19:40:36 +01:00
|
|
|
so := mtl.Origin{X: a.X - minX, Y: a.Y - minY, Z: 0}
|
|
|
|
ss := mtl.Size{Width: a.Width, Height: a.Height, Depth: 1}
|
|
|
|
do := mtl.Origin{X: a.X, Y: a.Y, Z: 0}
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
i.stencil = i.graphics.view.getMTLDevice().MakeTexture(td)
|
|
|
|
}
|