2019-06-18 17:34:05 +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.
|
|
|
|
|
2021-06-10 18:03:35 +02:00
|
|
|
//go:build darwin && !ios
|
|
|
|
// +build darwin,!ios
|
2019-06-18 17:34:05 +02:00
|
|
|
|
|
|
|
package metal
|
|
|
|
|
2021-11-26 18:52:45 +01:00
|
|
|
// #cgo CFLAGS: -x objective-c
|
|
|
|
// #cgo LDFLAGS: -framework Foundation
|
|
|
|
//
|
|
|
|
// #import <Foundation/Foundation.h>
|
|
|
|
//
|
|
|
|
// static int getMacOSMajorVersion() {
|
|
|
|
// NSOperatingSystemVersion version = [[NSProcessInfo processInfo] operatingSystemVersion];
|
|
|
|
// return (int)version.majorVersion;
|
|
|
|
// }
|
|
|
|
import "C"
|
|
|
|
|
2019-06-18 17:34:05 +02:00
|
|
|
import (
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/metal/mtl"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/metal/ns"
|
2019-06-18 17:34:05 +02:00
|
|
|
)
|
|
|
|
|
2021-11-26 18:52:45 +01:00
|
|
|
var macOSMajorVersion = int(C.getMacOSMajorVersion())
|
|
|
|
|
2020-09-03 17:43:51 +02:00
|
|
|
func (v *view) setWindow(window uintptr) {
|
2020-06-13 22:06:40 +02:00
|
|
|
// NSView can be updated e.g., fullscreen-state is switched.
|
2019-06-18 17:34:05 +02:00
|
|
|
v.window = window
|
2020-06-13 22:06:40 +02:00
|
|
|
v.windowChanged = true
|
2019-06-18 17:34:05 +02:00
|
|
|
}
|
|
|
|
|
2019-09-02 21:08:58 +02:00
|
|
|
func (v *view) setUIView(uiview uintptr) {
|
|
|
|
panic("metal: setUIView is not available on macOS")
|
2019-06-18 17:34:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *view) update() {
|
2020-06-13 22:06:40 +02:00
|
|
|
if !v.windowChanged {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-22 13:02:37 +02:00
|
|
|
cocoaWindow := ns.NewWindow(v.window)
|
2019-06-18 17:34:05 +02:00
|
|
|
cocoaWindow.ContentView().SetLayer(v.ml)
|
|
|
|
cocoaWindow.ContentView().SetWantsLayer(true)
|
2020-06-13 22:06:40 +02:00
|
|
|
v.windowChanged = false
|
2019-06-18 17:34:05 +02:00
|
|
|
}
|
|
|
|
|
2021-09-06 17:02:55 +02:00
|
|
|
func (v *view) usePresentsWithTransaction() bool {
|
2021-11-26 18:52:45 +01:00
|
|
|
// On macOS 12 (or later), do not use presentsWithTransaction, or vsync doesn't work (#1885).
|
|
|
|
// This works only for Metal. Unfortunately, there is not a good solution for OpenGL.
|
|
|
|
if macOSMajorVersion >= 12 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-09-06 17:02:55 +02:00
|
|
|
// Disable presentsWithTransaction on the fullscreen mode (#1745).
|
2021-09-18 12:20:45 +02:00
|
|
|
return !v.vsyncDisabled
|
2021-09-06 17:02:55 +02:00
|
|
|
}
|
|
|
|
|
2021-09-18 10:22:04 +02:00
|
|
|
func (v *view) maximumDrawableCount() int {
|
|
|
|
// When presentsWithTransaction is YES and triple buffering is enabled, nextDrawing returns immediately once every two times.
|
|
|
|
// This makes FPS doubled. To avoid this, disable the triple buffering.
|
|
|
|
if v.usePresentsWithTransaction() {
|
|
|
|
return 2
|
|
|
|
}
|
|
|
|
return 3
|
|
|
|
}
|
|
|
|
|
2019-09-02 21:08:58 +02:00
|
|
|
const (
|
|
|
|
storageMode = mtl.StorageModeManaged
|
|
|
|
resourceStorageMode = mtl.ResourceStorageModeManaged
|
|
|
|
)
|