-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
metal.m
162 lines (136 loc) · 5 KB
/
metal.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//========================================================================
// Simple GLFW+Metal example
// Copyright (c) Camilla Berglund <elmindreda@elmindreda.org>
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would
// be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not
// be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source
// distribution.
//
//========================================================================
//! [code]
#define GLFW_INCLUDE_NONE
#import <GLFW/glfw3.h>
#define GLFW_EXPOSE_NATIVE_COCOA
#import <GLFW/glfw3native.h>
#import <Metal/Metal.h>
#import <QuartzCore/QuartzCore.h>
#import <simd/simd.h>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
static void error_callback(int error, const char* description)
{
fputs(description, stderr);
}
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GLFW_TRUE);
}
int main(void)
{
id<MTLDevice> device = MTLCreateSystemDefaultDevice();
if (!device)
exit(EXIT_FAILURE);
glfwSetErrorCallback(error_callback);
if (!glfwInit())
exit(EXIT_FAILURE);
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
GLFWwindow* window = glfwCreateWindow(640, 480, "Metal Example", NULL, NULL);
if (!window)
{
glfwTerminate();
exit(EXIT_FAILURE);
}
NSWindow* nswin = glfwGetCocoaWindow(window);
CAMetalLayer* layer = [CAMetalLayer layer];
layer.device = device;
layer.pixelFormat = MTLPixelFormatBGRA8Unorm;
nswin.contentView.layer = layer;
nswin.contentView.wantsLayer = YES;
MTLCompileOptions* compileOptions = [MTLCompileOptions new];
compileOptions.languageVersion = MTLLanguageVersion1_1;
NSError* compileError;
id<MTLLibrary> lib = [device newLibraryWithSource:
@"#include <metal_stdlib>\n"
"using namespace metal;\n"
"vertex float4 v_simple(\n"
" constant float4* in [[buffer(0)]],\n"
" uint vid [[vertex_id]])\n"
"{\n"
" return in[vid];\n"
"}\n"
"fragment float4 f_simple(\n"
" float4 in [[stage_in]])\n"
"{\n"
" return float4(1, 0, 0, 1);\n"
"}\n"
options:compileOptions error:&compileError];
if (!lib)
{
NSLog(@"can't create library: %@", compileError);
glfwTerminate();
exit(EXIT_FAILURE);
}
id<MTLFunction> vs = [lib newFunctionWithName:@"v_simple"];
assert(vs);
id<MTLFunction> fs = [lib newFunctionWithName:@"f_simple"];
assert(fs);
id<MTLCommandQueue> cq = [device newCommandQueue];
assert(cq);
MTLRenderPipelineDescriptor* rpd = [MTLRenderPipelineDescriptor new];
rpd.vertexFunction = vs;
rpd.fragmentFunction = fs;
rpd.colorAttachments[0].pixelFormat = layer.pixelFormat;
id<MTLRenderPipelineState> rps = [device newRenderPipelineStateWithDescriptor:rpd error:NULL];
assert(rps);
glfwSetKeyCallback(window, key_callback);
while (!glfwWindowShouldClose(window))
{
float ratio;
int width, height;
glfwGetFramebufferSize(window, &width, &height);
ratio = width / (float) height;
layer.drawableSize = CGSizeMake(width, height);
id<CAMetalDrawable> drawable = [layer nextDrawable];
assert(drawable);
id<MTLCommandBuffer> cb = [cq commandBuffer];
MTLRenderPassDescriptor* rpd = [MTLRenderPassDescriptor new];
MTLRenderPassColorAttachmentDescriptor* cd = rpd.colorAttachments[0];
cd.texture = drawable.texture;
cd.loadAction = MTLLoadActionClear;
cd.clearColor = MTLClearColorMake(1.0, 1.0, 1.0, 1.0);
cd.storeAction = MTLStoreActionStore;
id<MTLRenderCommandEncoder> rce = [cb renderCommandEncoderWithDescriptor:rpd];
[rce setRenderPipelineState:rps];
[rce setVertexBytes:(vector_float4[]){
{ 0, 0, 0, 1 },
{ -1, 1, 0, 1 },
{ 1, 1, 0, 1 },
} length:3 * sizeof(vector_float4) atIndex:0];
[rce drawPrimitives:MTLPrimitiveTypeTriangle vertexStart:0 vertexCount:3];
[rce endEncoding];
[cb presentDrawable:drawable];
[cb commit];
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
exit(EXIT_SUCCESS);
}
//! [code]