Skip to content

Commit 660891f

Browse files
committed
shiny/driver/mtldriver: add darwin/arm64 (macOS) support
The Objective-C BOOL type is defined as a signed char on on Intel-based Mac computers, but on Apple silicon, it is defined as as a native bool.¹ This makes it hard to write portable Cgo code across darwin/amd64 and darwin/arm64 ports. So, stop relying on it, and use the bool type from <stdbool.h> instead. Update to a newer version of the mtl package with a similar change. ¹ https://developer.apple.com/documentation/apple_silicon/addressing_architectural_differences_in_your_macos_code#3616879 Fixes golang/go#43221. Change-Id: I63334dc646a8f9b74cd6a969eadf133cdbc84fb7 Reviewed-on: https://go-review.googlesource.com/c/exp/+/279293 Trust: Dmitri Shuralyov <[email protected]> Trust: Nigel Tao <[email protected]> Reviewed-by: Nigel Tao <[email protected]>
1 parent b5a6e24 commit 660891f

File tree

8 files changed

+11
-26
lines changed

8 files changed

+11
-26
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module golang.org/x/exp
33
go 1.12
44

55
require (
6-
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9
6+
dmitri.shuralyov.com/gpu/mtl v0.0.0-20201218220906-28db891af037
77
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802
88
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4
99
golang.org/x/image v0.0.0-20190802002840-cff245a6509b

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9 h1:VpgP7xuJadIUuKccphEpTJnWhS2jkQyMt6Y7pJCD7fY=
2-
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
1+
dmitri.shuralyov.com/gpu/mtl v0.0.0-20201218220906-28db891af037 h1:+PdD6GLKejR9DizMAKT5DpSAkKswvZrurk1/eEt9+pw=
2+
dmitri.shuralyov.com/gpu/mtl v0.0.0-20201218220906-28db891af037/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
33
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802 h1:1BDTz0u9nC3//pOCMdNH+CiXJVYJh5UQNCOBG7jbELc=
44
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
55
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4 h1:WtGNWLvXpe6ZudgnXrq0barxBImvnnJoMEhXAzcbM0I=

shiny/driver/mtldriver/internal/appkit/appkit.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
)
2222

2323
/*
24+
#include <stdbool.h>
2425
#include "appkit.h"
2526
*/
2627
import "C"
@@ -63,12 +64,5 @@ func (v View) SetLayer(l coreanim.Layer) {
6364
//
6465
// Reference: https://developer.apple.com/documentation/appkit/nsview/1483695-wantslayer.
6566
func (v View) SetWantsLayer(wantsLayer bool) {
66-
C.View_SetWantsLayer(v.view, toCBool(wantsLayer))
67-
}
68-
69-
func toCBool(b bool) C.BOOL {
70-
if b {
71-
return 1
72-
}
73-
return 0
67+
C.View_SetWantsLayer(v.view, C.bool(wantsLayer))
7468
}

shiny/driver/mtldriver/internal/appkit/appkit.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44

55
// +build darwin
66

7-
typedef signed char BOOL;
8-
97
void * Window_ContentView(void * window);
108

119
void View_SetLayer(void * view, void * layer);
12-
void View_SetWantsLayer(void * view, BOOL wantsLayer);
10+
void View_SetWantsLayer(void * view, bool wantsLayer);

shiny/driver/mtldriver/internal/appkit/appkit.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ void View_SetLayer(void * view, void * layer) {
1515
((NSView *)view).layer = (CALayer *)layer;
1616
}
1717

18-
void View_SetWantsLayer(void * view, BOOL wantsLayer) {
18+
void View_SetWantsLayer(void * view, bool wantsLayer) {
1919
((NSView *)view).wantsLayer = wantsLayer;
2020
}

shiny/driver/mtldriver/internal/coreanim/coreanim.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323

2424
/*
2525
#cgo LDFLAGS: -framework QuartzCore -framework Foundation
26+
#include <stdbool.h>
2627
#include "coreanim.h"
2728
*/
2829
import "C"
@@ -99,7 +100,7 @@ func (ml MetalLayer) SetMaximumDrawableCount(count int) {
99100
//
100101
// Reference: https://developer.apple.com/documentation/quartzcore/cametallayer/2887087-displaysyncenabled.
101102
func (ml MetalLayer) SetDisplaySyncEnabled(enabled bool) {
102-
C.MetalLayer_SetDisplaySyncEnabled(ml.metalLayer, toCBool(enabled))
103+
C.MetalLayer_SetDisplaySyncEnabled(ml.metalLayer, C.bool(enabled))
103104
}
104105

105106
// SetDrawableSize sets the size, in pixels, of textures for rendering layer content.
@@ -137,10 +138,3 @@ func (md MetalDrawable) Drawable() unsafe.Pointer { return md.metalDrawable }
137138
func (md MetalDrawable) Texture() mtl.Texture {
138139
return mtl.NewTexture(C.MetalDrawable_Texture(md.metalDrawable))
139140
}
140-
141-
func toCBool(b bool) C.BOOL {
142-
if b {
143-
return 1
144-
}
145-
return 0
146-
}

shiny/driver/mtldriver/internal/coreanim/coreanim.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
// +build darwin
66

7-
typedef signed char BOOL;
87
typedef unsigned long uint_t;
98
typedef unsigned short uint16_t;
109

@@ -14,7 +13,7 @@ uint16_t MetalLayer_PixelFormat(void * metalLayer);
1413
void MetalLayer_SetDevice(void * metalLayer, void * device);
1514
const char * MetalLayer_SetPixelFormat(void * metalLayer, uint16_t pixelFormat);
1615
const char * MetalLayer_SetMaximumDrawableCount(void * metalLayer, uint_t maximumDrawableCount);
17-
void MetalLayer_SetDisplaySyncEnabled(void * metalLayer, BOOL displaySyncEnabled);
16+
void MetalLayer_SetDisplaySyncEnabled(void * metalLayer, bool displaySyncEnabled);
1817
void MetalLayer_SetDrawableSize(void * metalLayer, double width, double height);
1918
void * MetalLayer_NextDrawable(void * metalLayer);
2019

shiny/driver/mtldriver/internal/coreanim/coreanim.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ void MetalLayer_SetDevice(void * metalLayer, void * device) {
4141
return NULL;
4242
}
4343

44-
void MetalLayer_SetDisplaySyncEnabled(void * metalLayer, BOOL displaySyncEnabled) {
44+
void MetalLayer_SetDisplaySyncEnabled(void * metalLayer, bool displaySyncEnabled) {
4545
((CAMetalLayer *)metalLayer).displaySyncEnabled = displaySyncEnabled;
4646
}
4747

0 commit comments

Comments
 (0)