Skip to content

Commit

Permalink
shiny/driver/mtldriver: add darwin/arm64 (macOS) support
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
dmitshur committed Dec 21, 2020
1 parent b5a6e24 commit 660891f
Show file tree
Hide file tree
Showing 8 changed files with 11 additions and 26 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module golang.org/x/exp
go 1.12

require (
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9
dmitri.shuralyov.com/gpu/mtl v0.0.0-20201218220906-28db891af037
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4
golang.org/x/image v0.0.0-20190802002840-cff245a6509b
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9 h1:VpgP7xuJadIUuKccphEpTJnWhS2jkQyMt6Y7pJCD7fY=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20201218220906-28db891af037 h1:+PdD6GLKejR9DizMAKT5DpSAkKswvZrurk1/eEt9+pw=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20201218220906-28db891af037/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802 h1:1BDTz0u9nC3//pOCMdNH+CiXJVYJh5UQNCOBG7jbELc=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4 h1:WtGNWLvXpe6ZudgnXrq0barxBImvnnJoMEhXAzcbM0I=
Expand Down
10 changes: 2 additions & 8 deletions shiny/driver/mtldriver/internal/appkit/appkit.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
)

/*
#include <stdbool.h>
#include "appkit.h"
*/
import "C"
Expand Down Expand Up @@ -63,12 +64,5 @@ func (v View) SetLayer(l coreanim.Layer) {
//
// Reference: https://developer.apple.com/documentation/appkit/nsview/1483695-wantslayer.
func (v View) SetWantsLayer(wantsLayer bool) {
C.View_SetWantsLayer(v.view, toCBool(wantsLayer))
}

func toCBool(b bool) C.BOOL {
if b {
return 1
}
return 0
C.View_SetWantsLayer(v.view, C.bool(wantsLayer))
}
4 changes: 1 addition & 3 deletions shiny/driver/mtldriver/internal/appkit/appkit.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

// +build darwin

typedef signed char BOOL;

void * Window_ContentView(void * window);

void View_SetLayer(void * view, void * layer);
void View_SetWantsLayer(void * view, BOOL wantsLayer);
void View_SetWantsLayer(void * view, bool wantsLayer);
2 changes: 1 addition & 1 deletion shiny/driver/mtldriver/internal/appkit/appkit.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ void View_SetLayer(void * view, void * layer) {
((NSView *)view).layer = (CALayer *)layer;
}

void View_SetWantsLayer(void * view, BOOL wantsLayer) {
void View_SetWantsLayer(void * view, bool wantsLayer) {
((NSView *)view).wantsLayer = wantsLayer;
}
10 changes: 2 additions & 8 deletions shiny/driver/mtldriver/internal/coreanim/coreanim.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

/*
#cgo LDFLAGS: -framework QuartzCore -framework Foundation
#include <stdbool.h>
#include "coreanim.h"
*/
import "C"
Expand Down Expand Up @@ -99,7 +100,7 @@ func (ml MetalLayer) SetMaximumDrawableCount(count int) {
//
// Reference: https://developer.apple.com/documentation/quartzcore/cametallayer/2887087-displaysyncenabled.
func (ml MetalLayer) SetDisplaySyncEnabled(enabled bool) {
C.MetalLayer_SetDisplaySyncEnabled(ml.metalLayer, toCBool(enabled))
C.MetalLayer_SetDisplaySyncEnabled(ml.metalLayer, C.bool(enabled))
}

// SetDrawableSize sets the size, in pixels, of textures for rendering layer content.
Expand Down Expand Up @@ -137,10 +138,3 @@ func (md MetalDrawable) Drawable() unsafe.Pointer { return md.metalDrawable }
func (md MetalDrawable) Texture() mtl.Texture {
return mtl.NewTexture(C.MetalDrawable_Texture(md.metalDrawable))
}

func toCBool(b bool) C.BOOL {
if b {
return 1
}
return 0
}
3 changes: 1 addition & 2 deletions shiny/driver/mtldriver/internal/coreanim/coreanim.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

// +build darwin

typedef signed char BOOL;
typedef unsigned long uint_t;
typedef unsigned short uint16_t;

Expand All @@ -14,7 +13,7 @@ uint16_t MetalLayer_PixelFormat(void * metalLayer);
void MetalLayer_SetDevice(void * metalLayer, void * device);
const char * MetalLayer_SetPixelFormat(void * metalLayer, uint16_t pixelFormat);
const char * MetalLayer_SetMaximumDrawableCount(void * metalLayer, uint_t maximumDrawableCount);
void MetalLayer_SetDisplaySyncEnabled(void * metalLayer, BOOL displaySyncEnabled);
void MetalLayer_SetDisplaySyncEnabled(void * metalLayer, bool displaySyncEnabled);
void MetalLayer_SetDrawableSize(void * metalLayer, double width, double height);
void * MetalLayer_NextDrawable(void * metalLayer);

Expand Down
2 changes: 1 addition & 1 deletion shiny/driver/mtldriver/internal/coreanim/coreanim.m
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void MetalLayer_SetDevice(void * metalLayer, void * device) {
return NULL;
}

void MetalLayer_SetDisplaySyncEnabled(void * metalLayer, BOOL displaySyncEnabled) {
void MetalLayer_SetDisplaySyncEnabled(void * metalLayer, bool displaySyncEnabled) {
((CAMetalLayer *)metalLayer).displaySyncEnabled = displaySyncEnabled;
}

Expand Down

0 comments on commit 660891f

Please sign in to comment.