File | File | |
| | @@ -0,0 +1,196 @@
|
| 1 | // +build darwin
|
| 2 |
|
| 3 | // movingtriangle is an example Metal program that displays a moving triangle in a window.
|
| 4 | package main
|
| 5 |
|
| 6 | import (
|
| 7 | "flag"
|
| 8 | "fmt"
|
| 9 | "log"
|
| 10 | "os"
|
| 11 | "runtime"
|
| 12 | "time"
|
| 13 | "unsafe"
|
| 14 |
|
| 15 | "dmitri.shuralyov.com/gpu/mtl"
|
| 16 | "github.com/go-gl/glfw/v3.2/glfw"
|
| 17 | "golang.org/x/image/math/f32"
|
| 18 | )
|
| 19 |
|
| 20 | func usage() {
|
| 21 | fmt.Fprintln(os.Stderr, "Usage: movingtriangle")
|
| 22 | flag.PrintDefaults()
|
| 23 | }
|
| 24 |
|
| 25 | func init() {
|
| 26 | runtime.LockOSThread()
|
| 27 | }
|
| 28 |
|
| 29 | func main() {
|
| 30 | flag.Usage = usage
|
| 31 | flag.Parse()
|
| 32 |
|
| 33 | err := run()
|
| 34 | if err != nil {
|
| 35 | log.Fatalln(err)
|
| 36 | }
|
| 37 | }
|
| 38 |
|
| 39 | func run() error {
|
| 40 | device, err := mtl.CreateSystemDefaultDevice()
|
| 41 | if err != nil {
|
| 42 | return err
|
| 43 | }
|
| 44 | fmt.Println("Metal device:", device.Name)
|
| 45 |
|
| 46 | err = glfw.Init()
|
| 47 | if err != nil {
|
| 48 | return err
|
| 49 | }
|
| 50 | defer glfw.Terminate()
|
| 51 |
|
| 52 | glfw.WindowHint(glfw.ClientAPI, glfw.NoAPI)
|
| 53 | window, err := glfw.CreateWindow(640, 480, "Metal Example", nil, nil)
|
| 54 | if err != nil {
|
| 55 | return err
|
| 56 | }
|
| 57 | defer window.Destroy()
|
| 58 |
|
| 59 | layer := mtl.MakeLayer()
|
| 60 | layer.SetDevice(device)
|
| 61 | layer.SetPixelFormat(mtl.PixelFormatBGRA8UNorm)
|
| 62 | layer.SetDrawableSize(window.GetFramebufferSize())
|
| 63 | layer.SetMaximumDrawableCount(3)
|
| 64 | layer.SetDisplaySyncEnabled(true)
|
| 65 | mtl.SetWindowContentViewLayer(window.GetCocoaWindow(), layer)
|
| 66 | mtl.SetWindowContentViewWantsLayer(window.GetCocoaWindow(), true)
|
| 67 |
|
| 68 | // Set callbacks.
|
| 69 | window.SetFramebufferSizeCallback(func(_ *glfw.Window, width, height int) {
|
| 70 | layer.SetDrawableSize(width, height)
|
| 71 | })
|
| 72 | var windowSize = [2]int32{640, 480}
|
| 73 | window.SetSizeCallback(func(_ *glfw.Window, width, height int) {
|
| 74 | windowSize[0], windowSize[1] = int32(width), int32(height)
|
| 75 | })
|
| 76 | var pos [2]float32
|
| 77 | window.SetCursorPosCallback(func(_ *glfw.Window, x, y float64) {
|
| 78 | pos[0], pos[1] = float32(x), float32(y)
|
| 79 | })
|
| 80 |
|
| 81 | // Create a render pipeline state.
|
| 82 | const source = `#include <metal_stdlib>
|
| 83 |
|
| 84 | using namespace metal;
|
| 85 |
|
| 86 | struct Vertex {
|
| 87 | float4 position [[position]];
|
| 88 | float4 color;
|
| 89 | };
|
| 90 |
|
| 91 | vertex Vertex VertexShader(
|
| 92 | uint vertexID [[vertex_id]],
|
| 93 | device Vertex * vertices [[buffer(0)]],
|
| 94 | constant int2 * windowSize [[buffer(1)]],
|
| 95 | constant float2 * pos [[buffer(2)]]
|
| 96 | ) {
|
| 97 | Vertex out = vertices[vertexID];
|
| 98 | out.position.xy += *pos;
|
| 99 | float2 viewportSize = float2(*windowSize);
|
| 100 | out.position.xy = float2(-1 + out.position.x / (0.5 * viewportSize.x),
|
| 101 | 1 - out.position.y / (0.5 * viewportSize.y));
|
| 102 | return out;
|
| 103 | }
|
| 104 |
|
| 105 | fragment float4 FragmentShader(Vertex in [[stage_in]]) {
|
| 106 | return in.color;
|
| 107 | }
|
| 108 | `
|
| 109 | lib, err := device.MakeLibrary(source, mtl.CompileOptions{})
|
| 110 | if err != nil {
|
| 111 | return err
|
| 112 | }
|
| 113 | vs, err := lib.MakeFunction("VertexShader")
|
| 114 | if err != nil {
|
| 115 | return err
|
| 116 | }
|
| 117 | fs, err := lib.MakeFunction("FragmentShader")
|
| 118 | if err != nil {
|
| 119 | return err
|
| 120 | }
|
| 121 | var rpld mtl.RenderPipelineDescriptor
|
| 122 | rpld.VertexFunction = vs
|
| 123 | rpld.FragmentFunction = fs
|
| 124 | rpld.ColorAttachments[0].PixelFormat = layer.PixelFormat()
|
| 125 | rps, err := device.MakeRenderPipelineState(rpld)
|
| 126 | if err != nil {
|
| 127 | return err
|
| 128 | }
|
| 129 |
|
| 130 | // Create a vertex buffer.
|
| 131 | type Vertex struct {
|
| 132 | Position f32.Vec4
|
| 133 | Color f32.Vec4
|
| 134 | }
|
| 135 | vertexData := [...]Vertex{
|
| 136 | {f32.Vec4{0, 0, 0, 1}, f32.Vec4{1, 0, 0, 1}},
|
| 137 | {f32.Vec4{300, 100, 0, 1}, f32.Vec4{0, 1, 0, 1}},
|
| 138 | {f32.Vec4{0, 100, 0, 1}, f32.Vec4{0, 0, 1, 1}},
|
| 139 | }
|
| 140 | vertexBuffer := device.MakeBuffer(unsafe.Pointer(&vertexData[0]), unsafe.Sizeof(vertexData), mtl.ResourceStorageModeManaged)
|
| 141 |
|
| 142 | cq := device.MakeCommandQueue()
|
| 143 |
|
| 144 | frame := startFPSCounter()
|
| 145 |
|
| 146 | for !window.ShouldClose() {
|
| 147 | glfw.PollEvents()
|
| 148 |
|
| 149 | // Create a drawable to render into.
|
| 150 | drawable, err := layer.NextDrawable()
|
| 151 | if err != nil {
|
| 152 | return err
|
| 153 | }
|
| 154 |
|
| 155 | cb := cq.MakeCommandBuffer()
|
| 156 |
|
| 157 | // Encode all render commands.
|
| 158 | var rpd mtl.RenderPassDescriptor
|
| 159 | rpd.ColorAttachments[0].LoadAction = mtl.LoadActionClear
|
| 160 | rpd.ColorAttachments[0].StoreAction = mtl.StoreActionStore
|
| 161 | rpd.ColorAttachments[0].ClearColor = mtl.ClearColor{Red: 0.35, Green: 0.65, Blue: 0.85, Alpha: 1}
|
| 162 | rpd.ColorAttachments[0].Texture = drawable.Texture()
|
| 163 | rce := cb.MakeRenderCommandEncoder(rpd)
|
| 164 | rce.SetRenderPipelineState(rps)
|
| 165 | rce.SetVertexBuffer(vertexBuffer, 0, 0)
|
| 166 | rce.SetVertexBytes(unsafe.Pointer(&windowSize[0]), unsafe.Sizeof(windowSize), 1)
|
| 167 | rce.SetVertexBytes(unsafe.Pointer(&pos[0]), unsafe.Sizeof(pos), 2)
|
| 168 | rce.DrawPrimitives(mtl.PrimitiveTypeTriangle, 0, 3)
|
| 169 | rce.EndEncoding()
|
| 170 |
|
| 171 | cb.Present(drawable)
|
| 172 | cb.Commit()
|
| 173 |
|
| 174 | frame <- struct{}{}
|
| 175 | }
|
| 176 |
|
| 177 | return nil
|
| 178 | }
|
| 179 |
|
| 180 | func startFPSCounter() chan struct{} {
|
| 181 | frame := make(chan struct{}, 4)
|
| 182 | go func() {
|
| 183 | second := time.Tick(time.Second)
|
| 184 | frames := 0
|
| 185 | for {
|
| 186 | select {
|
| 187 | case <-second:
|
| 188 | fmt.Println("fps:", frames)
|
| 189 | frames = 0
|
| 190 | case <-frame:
|
| 191 | frames++
|
| 192 | }
|
| 193 | }
|
| 194 | }()
|
| 195 | return frame
|
| 196 | }
|