-
Notifications
You must be signed in to change notification settings - Fork 1
/
mtl-iosurface-dual-gpu.mm
573 lines (509 loc) · 19.5 KB
/
mtl-iosurface-dual-gpu.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
// clang++ mtl-iosurface-dual-gpu.mm -framework Metal -framework MetalKit -framework Cocoa -framework QuartzCore -framework IOSurface -fobjc-arc && ./a.out
#include <IOSurface/IOSurface.h>
#include <Metal/Metal.h>
#include <MetalKit/MetalKit.h>
#include <QuartzCore/CALayer.h>
#include <QuartzCore/QuartzCore.h>
#include <CoreVideo/CoreVideo.h>
#include <algorithm>
#include <list>
#include <map>
#include <vector>
// This test program shares an IOSurface between two MTLDevices.
//
// It draws the Mandlebrot set into the IOSurface on the "src" device, and
// then blits the IOSurface into the CAMetalLayer on the left (which is also on
// the "src" device).
//
// It then blits the IOSurface into the CAMetalLayer on the right (which is on
// the separate "dst" device).
// Use a MTLSharedEvent to synchronize between the two MTLCommandBuffers. This is
// sufficient for correct behavior in the single-GPU case, but insufficient when
// there are two separate MTLDevices.
bool g_use_event = true;
// Make the source command buffer do waitUntilSchedule before committing the
// command buffer to read from the source. If this is done, then the IOSurfaces
// are always synchronized correctly (regardless of the other flags).
bool g_wait_until_scheduled = false;
// If this is true, then re-bind the IOSurface to a texture on both devices
// every frame. If this is used with MTLSharedEvent, that improves
// synchronization for single-stepping frames, but does not fix double-stepping
// frames.
bool g_rebind_iosurface_to_texture_every_frame = false;
// The number of iterations to use in the Mandlebrot rendering. Increase
// or decrease this to simulate more or less GPU work.
// The - and + keys will adjust this,
uint32_t g_mandlebrot_iters = 1024;
// The IOSurface to share.
IOSurfaceRef g_iosurface = nullptr;
// The width and height of the IOSurface.
const int g_iosurface_width = 1024;
const int g_iosurface_height = 1024;
// The width and height of the CAMetalLayers.
const int width = 512;
const int height = 512;
id<MTLDevice> src_device = nil;
id<MTLDevice> dst_device = nil;
id<MTLCommandQueue> src_commandQueue = nil;
id<MTLCommandQueue> dst_commandQueue = nil;
id<MTLRenderPipelineState> mandlebrotRenderPipelineState = nil;
std::map<id<MTLDevice>, id<MTLRenderPipelineState>> blitRenderPipelineState;
CAMetalLayer* src_layer = nil;
CAMetalLayer* dst_layer = nil;
NSWindow* window = nil;
CALayer* superlayer = nil;
#define CHECK(x) \
do { \
if (!(x)) { \
fprintf(stderr, "Failed: '%s' at %s:%d\n", #x, __FILE__, __LINE__); \
exit(1); \
} \
} while (0)
void CreateMandlebrotRenderPipelineState(id<MTLDevice> device) {
const char* cSource = ""
"#include <metal_stdlib>\n"
"#include <simd/simd.h>\n"
"using namespace metal;\n"
"typedef struct {\n"
" float4 clipSpacePosition [[position]];\n"
" float2 p;\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.p = positions[vertexID].xy;\n"
" out.color = colors[vertexID];\n"
" return out;\n"
"}\n"
"\n"
"fragment float4 fragmentShader(RasterizerData in [[stage_in]],\n"
" constant uint32_t& max_iter [[buffer(1)]]) {\n"
" float x0 = 0.125 * in.p.x - 1.31;\n"
" float y0 = 0.125 * in.p.y - 0.05;\n"
" float x = 0;\n"
" float y = 0;\n"
" float iter = 0;\n"
" while (iter < max_iter and x*x + y*y <= 4) {\n"
" float x_temp = x*x - y*y + x0;\n"
" y = 2*x*y + y0;\n"
" x = x_temp;\n"
" iter += 1;\n"
" }\n"
" if (iter < max_iter)\n"
" return float4(in.color.rgb * sqrt(iter / 100), 1.0);\n"
" return float4(in.color.rgb, 1.0);\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 = MTLPixelFormatBGRA8Unorm;
mandlebrotRenderPipelineState = [device newRenderPipelineStateWithDescriptor:desc
error:&error];
if (error)
NSLog(@"Failed to create render pipeline state: %@", error);
}
}
void DrawMandlebrot(id<MTLCommandBuffer> commandBuffer, id<MTLTexture> target_texture) {
id<MTLRenderCommandEncoder> encoder = nil;
{
MTLRenderPassDescriptor* desc = [MTLRenderPassDescriptor renderPassDescriptor];
desc.colorAttachments[0].texture = target_texture;
desc.colorAttachments[0].loadAction = MTLLoadActionClear;
desc.colorAttachments[0].storeAction = MTLStoreActionStore;
desc.colorAttachments[0].clearColor = MTLClearColorMake(0.0, 0.0, 0.0, 1.0);
encoder = [commandBuffer renderCommandEncoderWithDescriptor:desc];
}
{
const int kColorCount = 7;
const float colors_r[kColorCount] = {1, 0, 0, 0, 1, 1, 1};
const float colors_g[kColorCount] = {0, 1, 0, 1, 0, 1, 1};
const float colors_b[kColorCount] = {0, 0, 1, 1, 1, 0, 1};
static int color_index = 0;
float r = colors_r[color_index];
float g = colors_g[color_index];
float b = colors_b[color_index];
color_index = (color_index + 1) % kColorCount;
MTLViewport viewport;
viewport.originX = 0;
viewport.originY = 0;
viewport.width = [target_texture width];
viewport.height = [target_texture height];
viewport.znear = -1.0;
viewport.zfar = 1.0;
[encoder setViewport:viewport];
[encoder setRenderPipelineState:mandlebrotRenderPipelineState];
vector_float2 positions[3] = {
{ 0.9, -0.9 },
{ -0.9, -0.9 },
{ 0.0, 0.9 },
};
[encoder setVertexBytes:positions
length:sizeof(positions)
atIndex:0];
vector_float4 colors[3] = {
{ r, g, b, 1 },
{ r, g, b, 1 },
{ r, g, b, 1 },
};
[encoder setVertexBytes:colors
length:sizeof(colors)
atIndex:1];
[encoder setFragmentBytes:&g_mandlebrot_iters
length:sizeof(g_mandlebrot_iters)
atIndex:1];
[encoder drawPrimitives:MTLPrimitiveTypeTriangle
vertexStart:0
vertexCount:3];
}
[encoder endEncoding];
}
void CreateBlitRenderPipelineState(id<MTLDevice> device) {
const char* cSource = ""
"#include <metal_stdlib>\n"
"#include <simd/simd.h>\n"
"using metal::float4;\n"
"using metal::texture2d;\n"
"using metal::sampler;\n"
"\n"
"typedef struct {\n"
" float4 clipSpacePosition [[position]];\n"
" float2 texcoord;\n"
"} RasterizerData;\n"
"\n"
"vertex RasterizerData vertexShader(\n"
" uint vertexID [[vertex_id]],\n"
" constant vector_float2 *positions[[buffer(0)]],\n"
" constant vector_float2 *texcoords[[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.texcoord = texcoords[vertexID].xy;\n"
" return out;\n"
"}\n"
"\n"
"fragment float4 fragmentShader(RasterizerData in [[stage_in]],\n"
" texture2d<float> t [[texture(0)]]) {\n"
" constexpr sampler s(metal::mag_filter::nearest,\n"
" metal::min_filter::nearest);\n"
" float4 color = t.sample(s, in.texcoord);\n"
" return float4(color.rgb, 1);\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 = MTLPixelFormatBGRA8Unorm;
blitRenderPipelineState[device] = [device newRenderPipelineStateWithDescriptor:desc
error:&error];
if (error)
NSLog(@"Failed to create render pipeline state: %@", error);
}
}
void DrawBlit(id<MTLCommandBuffer> commandBuffer, id<MTLTexture> src_texture, id<MTLTexture> dst_texture) {
id<MTLRenderCommandEncoder> encoder = nil;
{
MTLRenderPassDescriptor* desc = [MTLRenderPassDescriptor renderPassDescriptor];
desc.colorAttachments[0].texture = dst_texture;
desc.colorAttachments[0].loadAction = MTLLoadActionClear;
desc.colorAttachments[0].storeAction = MTLStoreActionStore;
desc.colorAttachments[0].clearColor = MTLClearColorMake(0.0, 0.0, 0.0, 1.0);
encoder = [commandBuffer renderCommandEncoderWithDescriptor:desc];
}
{
MTLViewport viewport;
viewport.originX = 0;
viewport.originY = 0;
viewport.width = [dst_texture width];
viewport.height = [dst_texture height];
viewport.znear = -1.0;
viewport.zfar = 1.0;
[encoder setViewport:viewport];
[encoder setRenderPipelineState:blitRenderPipelineState[[commandBuffer device]]];
vector_float2 positions[6] = {
{ -0.9, -0.9 }, { 0.9, -0.9 }, { 0.9, 0.9 },
{ -0.9, -0.9 }, { -0.9, 0.9 }, { 0.9, 0.9 },
};
vector_float2 texcoords[6] = {
{ 0, 0 }, { 1, 0 }, { 1, 1 },
{ 0, 0 }, { 0, 1 }, { 1, 1 },
};
[encoder setVertexBytes:positions
length:sizeof(positions)
atIndex:0];
[encoder setVertexBytes:texcoords
length:sizeof(texcoords)
atIndex:1];
[encoder setFragmentTexture:src_texture atIndex:0];
[encoder drawPrimitives:MTLPrimitiveTypeTriangle
vertexStart:0
vertexCount:6];
}
[encoder endEncoding];
}
void CreateIOSurface() {
uint32_t io_format = 'BGRA';
size_t bytes_per_pixel = 4;
size_t bytes_per_row = IOSurfaceAlignProperty(
kIOSurfaceBytesPerRow, g_iosurface_width * bytes_per_pixel);
size_t bytes_total = IOSurfaceAlignProperty(
kIOSurfaceAllocSize, g_iosurface_height * bytes_per_row);
NSDictionary *options = @{
(id)kIOSurfaceWidth: @(g_iosurface_width),
(id)kIOSurfaceHeight: @(g_iosurface_height),
(id)kIOSurfacePixelFormat: @(io_format),
(id)kIOSurfaceBytesPerElement: @(bytes_per_pixel),
(id)kIOSurfaceBytesPerRow: @(bytes_per_row),
(id)kIOSurfaceAllocSize: @(bytes_total),
};
g_iosurface = IOSurfaceCreate((CFDictionaryRef)options);
CHECK(g_iosurface);
}
id<MTLTexture> WrapIOSurface(id<MTLDevice> device, IOSurfaceRef iosurface, uint64_t usage) {
CHECK(iosurface);
int iosurface_width = IOSurfaceGetWidth(iosurface);
int iosurface_height = IOSurfaceGetHeight(iosurface);
MTLTextureDescriptor* tex_desc = [MTLTextureDescriptor new];
[tex_desc setTextureType:MTLTextureType2D];
[tex_desc setUsage:usage];
[tex_desc setPixelFormat:MTLPixelFormatBGRA8Unorm];
[tex_desc setWidth:iosurface_width];
[tex_desc setHeight:iosurface_height];
[tex_desc setDepth:1];
[tex_desc setMipmapLevelCount:1];
[tex_desc setArrayLength:1];
[tex_desc setSampleCount:1];
[tex_desc setStorageMode:MTLStorageModeManaged];
// This doesn't make a difference.
// [tex_desc setHazardTrackingMode:MTLResourceHazardTrackingModeTracked];
id<MTLTexture> texture = [device newTextureWithDescriptor:tex_desc
iosurface:iosurface
plane:0];
CHECK(texture);
return texture;
}
void Draw() {
if (!src_device) {
printf("Initialize devices first!\n");
return;
}
// This event is signalled after the "src" device write is completed,
// and is waited on by the "dst" device before reading.
static id<MTLSharedEvent> src_event = nil;
// This event is signalled after the "dst" device read is completed,
// and is waited on by the "src" device before writing.
static id<MTLSharedEvent> dst_event = nil;
// Both events are kept in lock-step values, starting at 0.
static uint64_t event_value = 0;
if (!src_event)
src_event = [src_device newSharedEvent];
if (!dst_event)
dst_event = [dst_device newSharedEvent];
static id<MTLTexture> src_iosurface_texture = nil;
static id<MTLTexture> dst_iosurface_texture = nil;
{
id<MTLCommandBuffer> src_commandBuffer = [src_commandQueue commandBuffer];
if (g_use_event) {
// Wait on the event so that we don't write to the IOSurface while
// the other device is reading from it.
[src_commandBuffer encodeWaitForEvent:dst_event value:event_value];
}
// Draw the Mandlebrot set to the shared IOSurface.
if (!src_iosurface_texture) {
src_iosurface_texture = WrapIOSurface(
src_device, g_iosurface, MTLTextureUsageShaderRead | MTLTextureUsageRenderTarget);
}
DrawMandlebrot(src_commandBuffer, src_iosurface_texture);
if (g_use_event) {
// Signal the event after our write is done.
event_value += 1;
[src_commandBuffer encodeSignalEvent:src_event value:event_value];
}
// Draw the shared IOSurface to the CAMetalLayer (this read can overlay with
// the other device reading).
id<CAMetalDrawable> src_drawable = [src_layer nextDrawable];
DrawBlit(src_commandBuffer, src_iosurface_texture, [src_drawable texture]);
// Present and commit.
[src_commandBuffer presentDrawable:src_drawable];
[src_commandBuffer commit];
if (g_wait_until_scheduled) {
[src_commandBuffer waitUntilScheduled];
}
if (g_rebind_iosurface_to_texture_every_frame) {
src_iosurface_texture = nil;
}
}
{
id<MTLCommandBuffer> dst_commandBuffer = [dst_commandQueue commandBuffer];
if (g_use_event) {
// Wait for the write to the IOSurface to complete.
[dst_commandBuffer encodeWaitForEvent:src_event value:event_value];
}
if (!dst_iosurface_texture) {
dst_iosurface_texture = WrapIOSurface(
dst_device, g_iosurface, MTLTextureUsageShaderRead);
}
// Draw the shared IOSurface to the CAMetalLayer.
id<CAMetalDrawable> dst_drawable = [dst_layer nextDrawable];
DrawBlit(dst_commandBuffer, dst_iosurface_texture, [dst_drawable texture]);
if (g_use_event) {
// Signal that our read from the IOSurface is complete, so the src device
// can safely overwrite it.
[dst_commandBuffer encodeSignalEvent:dst_event value:event_value];
}
[dst_commandBuffer presentDrawable:dst_drawable];
[dst_commandBuffer commit];
if (g_wait_until_scheduled) {
[dst_commandBuffer waitUntilScheduled];
}
if (g_rebind_iosurface_to_texture_every_frame) {
dst_iosurface_texture = nil;
}
}
}
void InitializeMetal() {
NSArray<id<MTLDevice>>* devices = MTLCopyAllDevices();
for (id<MTLDevice> device in devices) {
if (![device isLowPower] || !src_device)
src_device = device;
}
for (id<MTLDevice> device in devices) {
if (device != src_device || !dst_device) {
dst_device = device;
}
}
if (src_device == dst_device) {
printf("Using a single GPU\n");
printf("**** THIS MEANS THAT THIS TESTS NOTHING ****\n");
} else
printf("Using multiple GPUs(!)\n");
if (g_use_event)
printf("Using MTLSharedEvent\n");
else
printf("Not using MTLSharedEvent\n");
if (g_wait_until_scheduled)
printf("Using waitUntilScheduled\n");
else
printf("Not using waitUntilScheduled\n");
if (g_rebind_iosurface_to_texture_every_frame)
printf("Rebinding IOSurface to MTLTexture every frame\n");
else
printf("Not rebinding IOSurface to MTLTexture every frame\n");
src_commandQueue = [src_device newCommandQueue];
dst_commandQueue = [dst_device newCommandQueue];
src_layer = [[CAMetalLayer alloc] init];
[src_layer setDevice:src_device];
[src_layer setFramebufferOnly:YES];
[src_layer setPixelFormat:MTLPixelFormatBGRA8Unorm];
dst_layer = [[CAMetalLayer alloc] init];
[dst_layer setDevice:dst_device];
[dst_layer setFramebufferOnly:YES];
[dst_layer setPixelFormat:MTLPixelFormatBGRA8Unorm];
[superlayer addSublayer:src_layer];
[superlayer addSublayer:dst_layer];
[src_layer setFrame:CGRectMake(0, 0, width, height)];
[dst_layer setFrame:CGRectMake(width, 0, width, height)];
CreateMandlebrotRenderPipelineState(src_device);
CreateBlitRenderPipelineState(src_device);
CreateBlitRenderPipelineState(dst_device);
CreateIOSurface();
}
@interface MainWindow : NSWindow
@end
@implementation MainWindow
- (void)keyDown:(NSEvent *)event {
if ([event isARepeat])
return;
NSString *characters = [event charactersIgnoringModifiers];
if ([characters length] != 1)
return;
int c = [characters characterAtIndex:0];
switch (c) {
case '1':
Draw();
break;
case '2':
Draw();
Draw();
break;
case '-':
if (g_mandlebrot_iters > 1)
g_mandlebrot_iters /= 2;
printf("Mandlebrot iters: %d\n", g_mandlebrot_iters);
break;
case '=':
g_mandlebrot_iters *= 2;
printf("Mandlebrot iters: %d\n", g_mandlebrot_iters);
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];
window = [[MainWindow alloc]
initWithContentRect:NSMakeRect(0, 0, 2*width, height)
styleMask:NSWindowStyleMaskResizable | NSWindowStyleMaskTitled
backing:NSBackingStoreBuffered
defer:NO];
[window setOpaque:YES];
superlayer = [[CALayer alloc] init];
[[window contentView] setLayer:superlayer];
[[window contentView] setWantsLayer:YES];
InitializeMetal();
printf("Press '1' to draw 1 frame\n");
printf("Press '2' to draw 2 frames\n");
printf("Press '-' and '+' to decrease or increase the Mandlebrot GPU work\n");
[window setTitle:@"Test"];
[window makeKeyAndOrderFront:nil];
[NSApp activateIgnoringOtherApps:YES];
[NSApp run];
return 0;
}