@@ -1,22 +1,23 @@ // +build darwin // Package ns provides access to Apple's AppKit API (https://developer.apple.com/documentation/appkit). // Package appkit provides access to Apple's AppKit API // (https://developer.apple.com/documentation/appkit). // // This package is in very early stages of development. // It's a minimal implementation with scope limited to // supporting the movingtriangle example. package ns package appkit import ( "unsafe" "dmitri.shuralyov.com/gpu/mtl/example/movingtriangle/internal/ca" "dmitri.shuralyov.com/gpu/mtl/example/movingtriangle/internal/coreanim" ) /* #include "ns.h" #include "appkit.h" */ import "C" // Window is a window that an app displays on the screen. // @@ -46,20 +47,22 @@ type View struct { } // SetLayer sets v.layer to l. // // Reference: https://developer.apple.com/documentation/appkit/nsview/1483298-layer. func (v View) SetLayer(l ca.Layer) { func (v View) SetLayer(l coreanim.Layer) { C.View_SetLayer(v.view, l.Layer()) } // SetWantsLayer sets v.wantsLayer to wantsLayer. // // Reference: https://developer.apple.com/documentation/appkit/nsview/1483695-wantslayer. func (v View) SetWantsLayer(wantsLayer bool) { switch wantsLayer { case true: C.View_SetWantsLayer(v.view, 1) case false: C.View_SetWantsLayer(v.view, 0) C.View_SetWantsLayer(v.view, toCBool(wantsLayer)) } func toCBool(b bool) C.BOOL { if b { return 1 } return 0 }
@@ -1,9 +1,9 @@ // +build darwin #import <Cocoa/Cocoa.h> #include "ns.h" #include "appkit.h" void * Window_ContentView(void * window) { return ((NSWindow *)window).contentView; }
@@ -1,24 +1,25 @@ // +build darwin // Package ca provides access to Apple's Core Animation API (https://developer.apple.com/documentation/quartzcore). // Package coreanim provides access to Apple's Core Animation API // (https://developer.apple.com/documentation/quartzcore). // // This package is in very early stages of development. // It's a minimal implementation with scope limited to // supporting the movingtriangle example. package ca package coreanim import ( "errors" "unsafe" "dmitri.shuralyov.com/gpu/mtl" ) /* #cgo LDFLAGS: -framework QuartzCore -framework Foundation #include "ca.h" #include "coreanim.h" */ import "C" // Layer is an object that manages image-based content and // allows you to perform animations on that content. @@ -90,16 +91,11 @@ func (ml MetalLayer) SetMaximumDrawableCount(count int) { // SetDisplaySyncEnabled controls whether the Metal layer and its drawables // are synchronized with the display's refresh rate. // // Reference: https://developer.apple.com/documentation/quartzcore/cametallayer/2887087-displaysyncenabled. func (ml MetalLayer) SetDisplaySyncEnabled(enabled bool) { switch enabled { case true: C.MetalLayer_SetDisplaySyncEnabled(ml.metalLayer, 1) case false: C.MetalLayer_SetDisplaySyncEnabled(ml.metalLayer, 0) } C.MetalLayer_SetDisplaySyncEnabled(ml.metalLayer, toCBool(enabled)) } // SetDrawableSize sets the size, in pixels, of textures for rendering layer content. // // Reference: https://developer.apple.com/documentation/quartzcore/cametallayer/1478174-drawablesize. @@ -133,5 +129,12 @@ func (md MetalDrawable) Drawable() unsafe.Pointer { return md.metalDrawable } // // Reference: https://developer.apple.com/documentation/quartzcore/cametaldrawable/1478159-texture. 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 }
@@ -1,9 +1,9 @@ // +build darwin #import <QuartzCore/QuartzCore.h> #include "ca.h" #include "coreanim.h" void * MakeMetalLayer() { return [[CAMetalLayer alloc] init]; }
@@ -12,12 +12,12 @@ import ( "runtime" "time" "unsafe" "dmitri.shuralyov.com/gpu/mtl" "dmitri.shuralyov.com/gpu/mtl/example/movingtriangle/internal/ca" "dmitri.shuralyov.com/gpu/mtl/example/movingtriangle/internal/ns" "dmitri.shuralyov.com/gpu/mtl/example/movingtriangle/internal/appkit" "dmitri.shuralyov.com/gpu/mtl/example/movingtriangle/internal/coreanim" "github.com/go-gl/glfw/v3.3/glfw" "golang.org/x/image/math/f32" ) func init() { @@ -55,17 +55,17 @@ func run() error { if err != nil { return err } defer window.Destroy() ml := ca.MakeMetalLayer() ml := coreanim.MakeMetalLayer() ml.SetDevice(device) ml.SetPixelFormat(mtl.PixelFormatBGRA8UNorm) ml.SetDrawableSize(window.GetFramebufferSize()) ml.SetMaximumDrawableCount(3) ml.SetDisplaySyncEnabled(true) cv := ns.NewWindow(window.GetCocoaWindow()).ContentView() cv := appkit.NewWindow(window.GetCocoaWindow()).ContentView() cv.SetLayer(ml) cv.SetWantsLayer(true) // Set callbacks. window.SetFramebufferSizeCallback(func(_ *glfw.Window, width, height int) {