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