2019-09-02 21:08:58 +02:00
|
|
|
// Copyright 2019 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 (
|
|
|
|
"sync"
|
|
|
|
|
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"
|
2019-09-02 21:08:58 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type view struct {
|
2020-09-03 17:43:51 +02:00
|
|
|
window uintptr
|
2019-09-02 21:08:58 +02:00
|
|
|
uiview uintptr
|
|
|
|
|
2020-06-13 22:06:40 +02:00
|
|
|
windowChanged bool
|
2021-08-08 08:13:44 +02:00
|
|
|
vsyncDisabled bool
|
2020-06-13 22:06:40 +02:00
|
|
|
|
2019-09-02 21:08:58 +02:00
|
|
|
device mtl.Device
|
|
|
|
ml ca.MetalLayer
|
|
|
|
|
|
|
|
once sync.Once
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *view) setDrawableSize(width, height int) {
|
|
|
|
v.ml.SetDrawableSize(width, height)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *view) getMTLDevice() mtl.Device {
|
|
|
|
return v.device
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *view) setDisplaySyncEnabled(enabled bool) {
|
2021-08-08 08:13:44 +02:00
|
|
|
if !v.vsyncDisabled == enabled {
|
2021-08-05 20:08:01 +02:00
|
|
|
return
|
|
|
|
}
|
2021-08-08 08:13:44 +02:00
|
|
|
v.forceSetDisplaySyncEnabled(enabled)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *view) forceSetDisplaySyncEnabled(enabled bool) {
|
2019-09-02 21:08:58 +02:00
|
|
|
v.ml.SetDisplaySyncEnabled(enabled)
|
2021-08-08 08:38:41 +02:00
|
|
|
v.vsyncDisabled = !enabled
|
2019-09-02 21:08:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *view) colorPixelFormat() mtl.PixelFormat {
|
|
|
|
return v.ml.PixelFormat()
|
|
|
|
}
|
|
|
|
|
2022-02-11 14:47:13 +01:00
|
|
|
func (v *view) initialize() error {
|
2023-03-03 17:08:08 +01:00
|
|
|
d, err := mtl.CreateSystemDefaultDevice()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-09-02 21:08:58 +02:00
|
|
|
}
|
2023-03-03 17:08:08 +01:00
|
|
|
v.device = d
|
2019-09-02 21:08:58 +02:00
|
|
|
|
2023-03-03 17:08:08 +01:00
|
|
|
ml, err := ca.MakeMetalLayer()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
v.ml = ml
|
2019-09-02 21:08:58 +02:00
|
|
|
v.ml.SetDevice(v.device)
|
|
|
|
// https://developer.apple.com/documentation/quartzcore/cametallayer/1478155-pixelformat
|
|
|
|
//
|
|
|
|
// The pixel format for a Metal layer must be MTLPixelFormatBGRA8Unorm,
|
|
|
|
// MTLPixelFormatBGRA8Unorm_sRGB, MTLPixelFormatRGBA16Float, MTLPixelFormatBGRA10_XR, or
|
|
|
|
// MTLPixelFormatBGRA10_XR_sRGB.
|
|
|
|
v.ml.SetPixelFormat(mtl.PixelFormatBGRA8UNorm)
|
2021-07-07 21:21:20 +02:00
|
|
|
|
2020-10-13 18:55:32 +02:00
|
|
|
// The vsync state might be reset. Set the state again (#1364).
|
2021-08-08 08:13:44 +02:00
|
|
|
v.forceSetDisplaySyncEnabled(!v.vsyncDisabled)
|
2021-07-06 17:38:50 +02:00
|
|
|
v.ml.SetFramebufferOnly(true)
|
|
|
|
|
2022-12-27 10:53:45 +01:00
|
|
|
// presentsWithTransaction doesn't work in the fullscreen mode (#1745, #1974).
|
|
|
|
// presentsWithTransaction doesn't work with vsync off (#1196).
|
|
|
|
// nextDrawable took more than one second if the window has other controls like NSTextView (#1029).
|
|
|
|
v.ml.SetPresentsWithTransaction(false)
|
|
|
|
v.ml.SetMaximumDrawableCount(3)
|
|
|
|
|
2019-09-02 21:08:58 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-07-06 17:38:50 +02:00
|
|
|
func (v *view) nextDrawable() ca.MetalDrawable {
|
2019-09-02 21:08:58 +02:00
|
|
|
d, err := v.ml.NextDrawable()
|
|
|
|
if err != nil {
|
|
|
|
// Drawable is nil. This can happen at the initial state. Let's wait and see.
|
|
|
|
return ca.MetalDrawable{}
|
|
|
|
}
|
|
|
|
return d
|
|
|
|
}
|