File | File | |
| | @@ -13,143 +13,19 @@ import (
|
13 | 13 | "fmt"
|
14 | 14 | "unsafe"
|
15 | 15 | )
|
16 | 16 |
|
17 | 17 | /*
|
18 | | #cgo LDFLAGS: -framework Metal -framework QuartzCore -framework Foundation
|
| 18 | #cgo LDFLAGS: -framework Metal -framework Foundation
|
19 | 19 | #include <stdlib.h>
|
20 | 20 | #include "mtl.h"
|
21 | 21 | struct Library Go_Device_MakeLibrary(void * device, _GoString_ source) {
|
22 | 22 | return Device_MakeLibrary(device, _GoStringPtr(source), _GoStringLen(source));
|
23 | 23 | }
|
24 | 24 | */
|
25 | 25 | import "C"
|
26 | 26 |
|
27 | | // Layer is a Core Animation Metal layer, a layer that manages a pool of Metal drawables.
|
28 | | //
|
29 | | // Reference: https://developer.apple.com/documentation/quartzcore/cametallayer.
|
30 | | type Layer struct {
|
31 | | layer unsafe.Pointer
|
32 | | }
|
33 | |
|
34 | | // MakeLayer creates a new Core Animation Metal layer.
|
35 | | //
|
36 | | // Reference: https://developer.apple.com/documentation/quartzcore/cametallayer.
|
37 | | func MakeLayer() Layer {
|
38 | | return Layer{C.MakeLayer()}
|
39 | | }
|
40 | |
|
41 | | // PixelFormat returns the pixel format of textures for rendering layer content.
|
42 | | //
|
43 | | // Reference: https://developer.apple.com/documentation/quartzcore/cametallayer/1478155-pixelformat.
|
44 | | func (l Layer) PixelFormat() PixelFormat {
|
45 | | return PixelFormat(C.Layer_PixelFormat(l.layer))
|
46 | | }
|
47 | |
|
48 | | // SetDevice sets the Metal device responsible for the layer's drawable resources.
|
49 | | //
|
50 | | // Reference: https://developer.apple.com/documentation/quartzcore/cametallayer/1478163-device.
|
51 | | func (l Layer) SetDevice(device Device) {
|
52 | | C.Layer_SetDevice(l.layer, device.device)
|
53 | | }
|
54 | |
|
55 | | // SetPixelFormat controls the pixel format of textures for rendering layer content.
|
56 | | //
|
57 | | // The pixel format for a Metal layer must be PixelFormatBGRA8UNorm, PixelFormatBGRA8UNormSRGB,
|
58 | | // PixelFormatRGBA16Float, PixelFormatBGRA10XR, or PixelFormatBGRA10XRSRGB.
|
59 | | // SetPixelFormat panics for other values.
|
60 | | //
|
61 | | // Reference: https://developer.apple.com/documentation/quartzcore/cametallayer/1478155-pixelformat.
|
62 | | func (l Layer) SetPixelFormat(pf PixelFormat) {
|
63 | | e := C.Layer_SetPixelFormat(l.layer, C.uint16_t(pf))
|
64 | | if e != nil {
|
65 | | panic(errors.New(C.GoString(e)))
|
66 | | }
|
67 | | }
|
68 | |
|
69 | | // SetMaximumDrawableCount controls the number of Metal drawables in the resource pool
|
70 | | // managed by Core Animation.
|
71 | | //
|
72 | | // It can set to 2 or 3 only. SetMaximumDrawableCount panics for other values.
|
73 | | //
|
74 | | // Reference: https://developer.apple.com/documentation/quartzcore/cametallayer/2938720-maximumdrawablecount.
|
75 | | func (l Layer) SetMaximumDrawableCount(count int) {
|
76 | | e := C.Layer_SetMaximumDrawableCount(l.layer, C.uint_t(count))
|
77 | | if e != nil {
|
78 | | panic(errors.New(C.GoString(e)))
|
79 | | }
|
80 | | }
|
81 | |
|
82 | | // SetDisplaySyncEnabled controls whether the Metal layer and its drawables
|
83 | | // are synchronized with the display's refresh rate.
|
84 | | //
|
85 | | // Reference: https://developer.apple.com/documentation/quartzcore/cametallayer/2887087-displaysyncenabled.
|
86 | | func (l Layer) SetDisplaySyncEnabled(enabled bool) {
|
87 | | switch enabled {
|
88 | | case true:
|
89 | | C.Layer_SetDisplaySyncEnabled(l.layer, 1)
|
90 | | case false:
|
91 | | C.Layer_SetDisplaySyncEnabled(l.layer, 0)
|
92 | | }
|
93 | | }
|
94 | |
|
95 | | // SetDrawableSize sets the size, in pixels, of textures for rendering layer content.
|
96 | | //
|
97 | | // Reference: https://developer.apple.com/documentation/quartzcore/cametallayer/1478174-drawablesize.
|
98 | | func (l Layer) SetDrawableSize(width, height int) {
|
99 | | C.Layer_SetDrawableSize(l.layer, C.double(width), C.double(height))
|
100 | | }
|
101 | |
|
102 | | // NextDrawable returns a Metal drawable.
|
103 | | //
|
104 | | // Reference: https://developer.apple.com/documentation/quartzcore/cametallayer/1478172-nextdrawable.
|
105 | | func (l Layer) NextDrawable() (Drawable, error) {
|
106 | | d := C.Layer_NextDrawable(l.layer)
|
107 | | if d == nil {
|
108 | | return Drawable{}, errors.New("nextDrawable returned nil")
|
109 | | }
|
110 | |
|
111 | | return Drawable{d}, nil
|
112 | | }
|
113 | |
|
114 | | // Drawable is a displayable resource that can be rendered or written to.
|
115 | | //
|
116 | | // Reference: https://developer.apple.com/documentation/metal/mtldrawable.
|
117 | | type Drawable struct {
|
118 | | drawable unsafe.Pointer
|
119 | | }
|
120 | |
|
121 | | // Texture returns a Metal texture object representing the drawable object's content.
|
122 | | //
|
123 | | // Reference: https://developer.apple.com/documentation/quartzcore/cametaldrawable/1478159-texture.
|
124 | | func (d Drawable) Texture() Texture {
|
125 | | return Texture{
|
126 | | texture: C.Drawable_Texture(d.drawable),
|
127 | | Width: 0, // TODO: Fetch dimensions of actually created texture.
|
128 | | Height: 0, // TODO: Fetch dimensions of actually created texture.
|
129 | | }
|
130 | | }
|
131 | |
|
132 | | // SetWindowContentViewLayer sets cocoaWindow's contentView's layer to layer.
|
133 | | //
|
134 | | // Reference: https://developer.apple.com/documentation/appkit/nsview/1483298-layer.
|
135 | | func SetWindowContentViewLayer(cocoaWindow uintptr, l Layer) {
|
136 | | C.SetWindowContentViewLayer(unsafe.Pointer(cocoaWindow), l.layer)
|
137 | | }
|
138 | |
|
139 | | // SetWindowContentViewWantsLayer sets cocoaWindow's contentView's wantsLayer to wantsLayer.
|
140 | | //
|
141 | | // Reference: https://developer.apple.com/documentation/appkit/nsview/1483695-wantslayer.
|
142 | | func SetWindowContentViewWantsLayer(cocoaWindow uintptr, wantsLayer bool) {
|
143 | | switch wantsLayer {
|
144 | | case true:
|
145 | | C.SetWindowContentViewWantsLayer(unsafe.Pointer(cocoaWindow), 1)
|
146 | | case false:
|
147 | | C.SetWindowContentViewWantsLayer(unsafe.Pointer(cocoaWindow), 0)
|
148 | | }
|
149 | | }
|
150 | |
|
151 | 27 | // FeatureSet defines a specific platform, hardware, and software configuration.
|
152 | 28 | //
|
153 | 29 | // Reference: https://developer.apple.com/documentation/metal/mtlfeatureset.
|
154 | 30 | type FeatureSet uint16
|
155 | 31 |
|
| | @@ -315,10 +191,11 @@ const (
|
315 | 191 | // Resource represents a memory allocation for storing specialized data
|
316 | 192 | // that is accessible to the GPU.
|
317 | 193 | //
|
318 | 194 | // Reference: https://developer.apple.com/documentation/metal/mtlresource.
|
319 | 195 | type Resource interface {
|
| 196 | // resource returns the underlying id<MTLResource> pointer.
|
320 | 197 | resource() unsafe.Pointer
|
321 | 198 | }
|
322 | 199 |
|
323 | 200 | // RenderPipelineDescriptor configures new RenderPipelineState objects.
|
324 | 201 | //
|
| | @@ -449,10 +326,13 @@ func CopyAllDevices() []Device {
|
449 | 326 | ds[i].Name = C.GoString(d.Name)
|
450 | 327 | }
|
451 | 328 | return ds
|
452 | 329 | }
|
453 | 330 |
|
| 331 | // Device returns the underlying id<MTLDevice> pointer.
|
| 332 | func (d Device) Device() unsafe.Pointer { return d.device }
|
| 333 |
|
454 | 334 | // SupportsFeatureSet reports whether device d supports feature set fs.
|
455 | 335 | //
|
456 | 336 | // Reference: https://developer.apple.com/documentation/metal/mtldevice/1433418-supportsfeatureset.
|
457 | 337 | func (d Device) SupportsFeatureSet(fs FeatureSet) bool {
|
458 | 338 | return C.Device_SupportsFeatureSet(d.device, C.uint16_t(fs)) != 0
|
| | @@ -527,10 +407,18 @@ func (d Device) MakeTexture(td TextureDescriptor) Texture {
|
527 | 407 | // Reference: https://developer.apple.com/documentation/metal/mtlcompileoptions.
|
528 | 408 | type CompileOptions struct {
|
529 | 409 | // TODO.
|
530 | 410 | }
|
531 | 411 |
|
| 412 | // Drawable is a displayable resource that can be rendered or written to.
|
| 413 | //
|
| 414 | // Reference: https://developer.apple.com/documentation/metal/mtldrawable.
|
| 415 | type Drawable interface {
|
| 416 | // Drawable returns the underlying id<MTLDrawable> pointer.
|
| 417 | Drawable() unsafe.Pointer
|
| 418 | }
|
| 419 |
|
532 | 420 | // CommandQueue is a queue that organizes the order
|
533 | 421 | // in which command buffers are executed by the GPU.
|
534 | 422 | //
|
535 | 423 | // Reference: https://developer.apple.com/documentation/metal/mtlcommandqueue.
|
536 | 424 | type CommandQueue struct {
|
| | @@ -550,15 +438,15 @@ func (cq CommandQueue) MakeCommandBuffer() CommandBuffer {
|
550 | 438 | // Reference: https://developer.apple.com/documentation/metal/mtlcommandbuffer.
|
551 | 439 | type CommandBuffer struct {
|
552 | 440 | commandBuffer unsafe.Pointer
|
553 | 441 | }
|
554 | 442 |
|
555 | | // Present registers a drawable presentation to occur as soon as possible.
|
| 443 | // PresentDrawable registers a drawable presentation to occur as soon as possible.
|
556 | 444 | //
|
557 | 445 | // Reference: https://developer.apple.com/documentation/metal/mtlcommandbuffer/1443029-presentdrawable.
|
558 | | func (cb CommandBuffer) Present(d Drawable) {
|
559 | | C.CommandBuffer_Present(cb.commandBuffer, d.drawable)
|
| 446 | func (cb CommandBuffer) PresentDrawable(d Drawable) {
|
| 447 | C.CommandBuffer_PresentDrawable(cb.commandBuffer, d.Drawable())
|
560 | 448 | }
|
561 | 449 |
|
562 | 450 | // Commit commits this command buffer for execution as soon as possible.
|
563 | 451 | //
|
564 | 452 | // Reference: https://developer.apple.com/documentation/metal/mtlcommandbuffer/1443003-commit.
|
| | @@ -693,17 +581,25 @@ func (l Library) MakeFunction(name string) (Function, error) {
|
693 | 581 | //
|
694 | 582 | // Reference: https://developer.apple.com/documentation/metal/mtltexture.
|
695 | 583 | type Texture struct {
|
696 | 584 | texture unsafe.Pointer
|
697 | 585 |
|
| 586 | // TODO: Change these fields into methods.
|
| 587 |
|
698 | 588 | // Width is the width of the texture image for the base level mipmap, in pixels.
|
699 | 589 | Width int
|
700 | 590 |
|
701 | 591 | // Height is the height of the texture image for the base level mipmap, in pixels.
|
702 | 592 | Height int
|
703 | 593 | }
|
704 | 594 |
|
| 595 | // NewTexture returns a Texture that wraps an existing id<MTLTexture> pointer.
|
| 596 | func NewTexture(texture unsafe.Pointer) Texture {
|
| 597 | return Texture{texture: texture}
|
| 598 | }
|
| 599 |
|
| 600 | // resource implements the Resource interface.
|
705 | 601 | func (t Texture) resource() unsafe.Pointer { return t.texture }
|
706 | 602 |
|
707 | 603 | // GetBytes copies a block of pixels from the storage allocation of texture
|
708 | 604 | // slice zero into system memory at a specified address.
|
709 | 605 | //
|