-
Notifications
You must be signed in to change notification settings - Fork 1
/
color-match.mm
202 lines (176 loc) · 6.51 KB
/
color-match.mm
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// clang++ color-match.mm -framework Metal -framework MetalKit -framework Cocoa -framework QuartzCore -fobjc-arc -g && ./a.out
#include <Metal/Metal.h>
#include <MetalKit/MetalKit.h>
#include <IOSurface/IOSurface.h>
const int width = 512;
const int height = 512;
id<MTLDevice> device = nil;
id<MTLRenderPipelineState> renderPipelineState = nil;
CAMetalLayer* metalLayers[2] = {nil, nil};
void CreateRenderPipelineState() {
const char* cSource = ""
"#include <metal_stdlib>\n"
"#include <simd/simd.h>\n"
"using namespace metal;\n"
"typedef struct {\n"
" float4 clipSpacePosition [[position]];\n"
" float4 color;\n"
"} RasterizerData;\n"
"\n"
"vertex RasterizerData vertexShader(\n"
" uint vertexID [[vertex_id]],\n"
" constant vector_float2 *positions[[buffer(0)]],\n"
" constant vector_float4 *colors[[buffer(1)]]) {\n"
" RasterizerData out;\n"
" out.clipSpacePosition = vector_float4(0.0, 0.0, 0.0, 1.0);\n"
" out.clipSpacePosition.xy = positions[vertexID].xy;\n"
" out.color = colors[vertexID];\n"
" return out;\n"
"}\n"
"\n"
"fragment float4 fragmentShader(RasterizerData in [[stage_in]]) {\n"
" return in.color;\n"
"}\n"
"";
id<MTLLibrary> library = nil;
{
NSError* error = nil;
NSString* source = [[NSString alloc] initWithCString:cSource
encoding:NSASCIIStringEncoding];
MTLCompileOptions* options = [[MTLCompileOptions alloc] init];
library = [device newLibraryWithSource:source
options:options
error:&error];
if (error)
NSLog(@"Failed to compile shader: %@", error);
}
id<MTLFunction> vertexFunction = [library newFunctionWithName:@"vertexShader"];
id<MTLFunction> fragmentFunction = [library newFunctionWithName:@"fragmentShader"];
{
NSError* error = nil;
MTLRenderPipelineDescriptor* desc = [[MTLRenderPipelineDescriptor alloc] init];
desc.label = @"Simple Pipeline";
desc.vertexFunction = vertexFunction;
desc.fragmentFunction = fragmentFunction;
desc.colorAttachments[0].pixelFormat = MTLPixelFormatRGBA16Float;
renderPipelineState = [device newRenderPipelineStateWithDescriptor:desc
error:&error];
if (error)
NSLog(@"Failed to create render pipeline state: %@", error);
}
}
void Draw(CAMetalLayer* metalLayer, MTLPixelFormat pixelFormat, float red, float green, float blue) {
device = MTLCreateSystemDefaultDevice();
id<MTLCommandQueue> commandQueue = [device newCommandQueue];
id<MTLCommandBuffer> commandBuffer = [commandQueue commandBuffer];
metalLayer.device = device;
metalLayer.pixelFormat = pixelFormat;
id<CAMetalDrawable> drawable = [metalLayer nextDrawable];
id<MTLRenderCommandEncoder> encoder = nil;
{
MTLRenderPassDescriptor* desc = [MTLRenderPassDescriptor renderPassDescriptor];
desc.colorAttachments[0].texture = drawable.texture;
desc.colorAttachments[0].loadAction = MTLLoadActionClear;
desc.colorAttachments[0].storeAction = MTLStoreActionStore;
desc.colorAttachments[0].clearColor = MTLClearColorMake(0, 0, 0, 1.0);
encoder = [commandBuffer renderCommandEncoderWithDescriptor:desc];
}
CreateRenderPipelineState();
{
MTLViewport viewport;
viewport.originX = 0;
viewport.originY = 0;
viewport.width = width;
viewport.height = height;
viewport.znear = -1.0;
viewport.zfar = 1.0;
[encoder setViewport:viewport];
[encoder setRenderPipelineState:renderPipelineState];
vector_float2 positions[6] = {
{ 1, -1 },
{ -1, -1 },
{ 1, 1 },
{ 1, 1 },
{ -1, 1 },
{ -1, -1 },
};
[encoder setVertexBytes:positions
length:sizeof(positions)
atIndex:0];
vector_float4 colors[6] = {
{ red, green, blue, 1 },
{ red, green, blue, 1 },
{ red, green, blue, 1 },
{ red, green, blue, 1 },
{ red, green, blue, 1 },
{ red, green, blue, 1 },
};
[encoder setVertexBytes:colors
length:sizeof(colors)
atIndex:1];
[encoder drawPrimitives:MTLPrimitiveTypeTriangle
vertexStart:0
vertexCount:6];
}
[encoder endEncoding];
[commandBuffer presentDrawable:drawable];
[commandBuffer commit];
}
@interface MainWindow : NSWindow
@end
@implementation MainWindow
- (void)keyDown:(NSEvent *)event {
if ([event isARepeat])
return;
NSString *characters = [event charactersIgnoringModifiers];
if ([characters length] != 1)
return;
switch ([characters characterAtIndex:0]) {
case ' ':
break;
case 'q':
[NSApp terminate:nil];
break;
default:
break;
}
}
@end
int main(int argc, char* argv[]) {
[NSApplication sharedApplication];
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
NSMenu* menubar = [NSMenu alloc];
[NSApp setMainMenu:menubar];
NSWindow* window = [[MainWindow alloc]
initWithContentRect:NSMakeRect(0, 0, width*2, height)
styleMask:NSWindowStyleMaskResizable | NSWindowStyleMaskTitled
backing:NSBackingStoreBuffered
defer:NO];
[window setOpaque:YES];
metalLayers[0] = [[CAMetalLayer alloc] init];
metalLayers[1] = [[CAMetalLayer alloc] init];
CALayer* rootLayer = [[CALayer alloc] init];
[[window contentView] setLayer:rootLayer];
[[window contentView] setWantsLayer:YES];
[rootLayer addSublayer:metalLayers[0]];
[rootLayer addSublayer:metalLayers[1]];
[metalLayers[0] setFrame:CGRectMake(0, 0, width, height)];
[metalLayers[1] setFrame:CGRectMake(width, 0, width, height)];
[window setTitle:@"Tiny Metal App"];
[window makeKeyAndOrderFront:nil];
// Draw color(srgb 2 0 0) to a floating-point buffer.
metalLayers[0].colorspace = CGColorSpaceCreateWithName(
kCGColorSpaceExtendedSRGB);
Draw(metalLayers[0], MTLPixelFormatRGBA16Float, 2.0, 0.0, 0.0);
metalLayers[1].wantsExtendedDynamicRangeContent = NO;
printf("Left: color(srgb 2 0 0) drawn to floating-pointer buffer\n");
// Draw color(srgb 2 0 0) to a unorm buffer.
metalLayers[0].colorspace = CGColorSpaceCreateWithName(
kCGColorSpaceExtendedSRGB);
metalLayers[1].wantsExtendedDynamicRangeContent = NO;
Draw(metalLayers[1], MTLPixelFormatBGRA8Unorm, 2.0, 0.0, 0.0);
printf("Right: color(srgb 2 0 0) drawn to fixed-point buffer\n");
[NSApp activateIgnoringOtherApps:YES];
[NSApp run];
return 0;
}