internal/graphicsdriver/metal: Refactoring: Remove vsyncInited

This commit is contained in:
Hajime Hoshi 2021-08-08 15:13:44 +09:00
parent d37c106cf4
commit ec26b1a43b
4 changed files with 31 additions and 16 deletions

View File

@ -154,6 +154,17 @@ func (ml MetalLayer) PresentsWithTransaction() bool {
return C.MetalLayer_PresentsWithTransaction(ml.metalLayer) != 0
}
// SetPresentsWithTransaction sets a Boolean value that determines whether the layer presents its content using a Core Animation transaction.
//
// Reference: https://developer.apple.com/documentation/quartzcore/cametallayer/1478157-presentswithtransaction
func (ml MetalLayer) SetPresentsWithTransaction(presentsWithTransaction bool) {
if presentsWithTransaction {
C.MetalLayer_SetPresentsWithTransaction(ml.metalLayer, 1)
} else {
C.MetalLayer_SetPresentsWithTransaction(ml.metalLayer, 0)
}
}
// SetFramebufferOnly sets a Boolean value that determines whether the layers textures are used only for rendering.
//
// https://developer.apple.com/documentation/quartzcore/cametallayer/1478168-framebufferonly

View File

@ -29,6 +29,8 @@ const char *MetalLayer_SetMaximumDrawableCount(void *metalLayer,
void MetalLayer_SetDisplaySyncEnabled(void *metalLayer,
uint8_t displaySyncEnabled);
void MetalLayer_SetDrawableSize(void *metalLayer, double width, double height);
void MetalLayer_SetPresentsWithTransaction(void *metalLayer,
uint8_t presentsWithTransaction);
void *MetalLayer_NextDrawable(void *metalLayer);
void MetalLayer_SetFramebufferOnly(void *metalLayer, uint8_t framebufferOnly);
uint8_t MetalLayer_PresentsWithTransaction(void *metalLayer);

View File

@ -85,20 +85,18 @@ void MetalLayer_SetDisplaySyncEnabled(void *metalLayer,
[((CAMetalLayer *)metalLayer) setDisplaySyncEnabled:displaySyncEnabled];
}
#endif
// setting presentsWithTransaction YES makes the FPS stable (#1196). We're not
// sure why...
if (displaySyncEnabled) {
[((CAMetalLayer *)metalLayer) setPresentsWithTransaction:YES];
} else {
[((CAMetalLayer *)metalLayer) setPresentsWithTransaction:NO];
}
}
void MetalLayer_SetDrawableSize(void *metalLayer, double width, double height) {
((CAMetalLayer *)metalLayer).drawableSize = (CGSize){width, height};
}
void MetalLayer_SetPresentsWithTransaction(void *metalLayer,
uint8_t presentsWithTransaction) {
[((CAMetalLayer *)metalLayer)
setPresentsWithTransaction:presentsWithTransaction];
}
void *MetalLayer_NextDrawable(void *metalLayer) {
return [(CAMetalLayer *)metalLayer nextDrawable];
}

View File

@ -29,8 +29,7 @@ type view struct {
uiview uintptr
windowChanged bool
vsync bool
vsyncInited bool
vsyncDisabled bool
device mtl.Device
ml ca.MetalLayer
@ -47,12 +46,19 @@ func (v *view) getMTLDevice() mtl.Device {
}
func (v *view) setDisplaySyncEnabled(enabled bool) {
if v.vsync == enabled && !v.vsyncInited {
if !v.vsyncDisabled == enabled {
return
}
v.forceSetDisplaySyncEnabled(enabled)
}
func (v *view) forceSetDisplaySyncEnabled(enabled bool) {
v.ml.SetDisplaySyncEnabled(enabled)
v.vsync = enabled
v.vsyncInited = true
// setting presentsWithTransaction true makes the FPS stable (#1196). We're not sure why...
v.ml.SetPresentsWithTransaction(enabled)
v.vsyncDisabled = !enabled
}
func (v *view) colorPixelFormat() mtl.PixelFormat {
@ -80,9 +86,7 @@ func (v *view) reset() error {
v.ml.SetMaximumDrawableCount(2)
// The vsync state might be reset. Set the state again (#1364).
if v.vsyncInited {
v.ml.SetDisplaySyncEnabled(v.vsync)
}
v.forceSetDisplaySyncEnabled(!v.vsyncDisabled)
v.ml.SetFramebufferOnly(true)
return nil