Handmade Hero»Forums»Code
14 posts
OSX Metal Vsync
I've been playing around with an OSX platform layer that uses Metal for rendering. I was wondering if there is a way to vsync without using a CVDisplayLink. In OpenGL you can use CGLFlushDrawable to wait on the vsync and I was wondering if there is a similar thing you can do in Metal.
Adrian
46 posts
None
OSX Metal Vsync
Edited by Adrian on
Could you post your code? I searched the web for guides on how to use metal but didn't came very far. Would love to see how you did it!
14 posts
OSX Metal Vsync
Edited by bimbinel on
I was only playing around with it so I'm just drawing a full-screen quad at the moment:

  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
static const r32 GlobalQuadVertices[6][4] =
{
    { -1.0f,  -1.0f, 0.0f, 1.0f },
    {  1.0f,  -1.0f, 0.0f, 1.0f },
    { -1.0f,   1.0f, 0.0f, 1.0f },
    
    {  1.0f,  -1.0f, 0.0f, 1.0f },
    { -1.0f,   1.0f, 0.0f, 1.0f },
    {  1.0f,   1.0f, 0.0f, 1.0f }
};
static const r32 GlobalQuadTexCoords[6][2] =
{
    { 0.0f, 0.0f },
    { 1.0f, 0.0f },
    { 0.0f, 1.0f },
    
    { 1.0f, 0.0f },
    { 0.0f, 1.0f },
    { 1.0f, 1.0f }
};

@interface OSXMetalView : NSView
@end

@implementation OSXMetalView
{
@public
    CVDisplayLinkRef	  	    _displayLink;
    dispatch_semaphore_t 	    _renderSemaphore;

	__weak CAMetalLayer 	   *_metalLayer;

	MTLRenderPassDescriptor    *_renderPassDesc;
    id<MTLDevice> 			    _device;
    id<MTLCommandQueue>        _commandQueue;
    id<MTLLibrary>             _shaderLibrary;
    id<MTLRenderPipelineState> _pipelineState;
    id<MTLDepthStencilState>   _depthState;
    id<MTLTexture>  	       _texture;
    id<MTLBuffer>  			   _vertexBuffer;
    id<MTLBuffer>  			   _texCoordBuffer;
    id<CAMetalDrawable> 	   _currentDrawable;

    BOOL 				        _layerSizeDidUpdate;
    char 					   *_pixels;
}

static CVReturn OnDisplayLinkFrame(CVDisplayLinkRef displayLink,
                                     const CVTimeStamp *now,
                                     const CVTimeStamp *outputTime,
                                     CVOptionFlags flagsIn,
                                     CVOptionFlags *flagsOut,
                                     void *displayLinkContext)
{
	OSXMetalView *view = (__bridge OSXMetalView *)displayLinkContext;

	@autoreleasepool
	{
		[view update];
	}

    return kCVReturnSuccess;
}

+ (Class)layerClass
{
    return [CAMetalLayer class];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
	if (self)
    {
    	self.wantsLayer = YES;
    	self.layer = _metalLayer = [CAMetalLayer layer];

    	_device = MTLCreateSystemDefaultDevice();
    
    	_metalLayer.device          = _device;
    	_metalLayer.pixelFormat     = MTLPixelFormatBGRA8Unorm;
    
    	_metalLayer.framebufferOnly = YES;

    	_commandQueue = [_device newCommandQueue];
    	if (!_commandQueue) 
    	{
        	printf("ERROR: Couldn't create a command queue.");
        	return nil;
        }

	    NSError *error = nil;

	    _shaderLibrary = [_device newLibraryWithFile: @"shaders.metallib" error:&error];
	    if (!_shaderLibrary) 
	    {
	        printf("ERROR: Failed to load shader library.");
	        return nil;
	    }

	    id<MTLFunction> fragmentProgram = [_shaderLibrary newFunctionWithName:@"TexturedQuadFragment"];
	    if (!fragmentProgram)
	    {
	        printf("ERROR: Couldn't load fragment function from default library.");
	       	return nil;
	    }
	    
	    id<MTLFunction> vertexProgram = [_shaderLibrary newFunctionWithName:@"TexturedQuadVertex"];
	    if (!vertexProgram)
	    {
	        printf("ERROR: Couldn't load vertex function from default library.");
	        return nil;
	    }

	   	MTLRenderPipelineDescriptor *pipelineStateDesc = [MTLRenderPipelineDescriptor new];
	    
	    if (!pipelineStateDesc)
	    {
	        printf("ERROR: Failed creating a pipeline state descriptor!");
	        return nil;
	    }
	    
	    pipelineStateDesc.depthAttachmentPixelFormat      = MTLPixelFormatInvalid;
	    pipelineStateDesc.stencilAttachmentPixelFormat    = MTLPixelFormatInvalid;
	    pipelineStateDesc.colorAttachments[0].pixelFormat = MTLPixelFormatBGRA8Unorm;
	    
	    pipelineStateDesc.sampleCount      = 1;
	    pipelineStateDesc.vertexFunction   = vertexProgram;
	    pipelineStateDesc.fragmentFunction = fragmentProgram;
	    
	    _pipelineState = [_device newRenderPipelineStateWithDescriptor:pipelineStateDesc
	                                                             error:&error];
	    if (!_pipelineState)
	    {
	        printf("ERROR: Failed acquiring pipeline state descriptor.");
	        return nil;
	    }

	   	MTLTextureDescriptor *texDesc = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatRGBA8Unorm
                                                                                           width:frame.size.width
                                                                                          height:frame.size.height
                                                                                       mipmapped:NO];
    	if (!texDesc)
    	{
        	printf("ERROR: Failed to create texture descriptor.");
        	return nil;
    	}

    	_texture = [_device newTextureWithDescriptor:texDesc];

    	if (!_texture)
    	{
        	printf("ERROR: Failed to create texture.");
        	return nil;    		
    	}

    	_vertexBuffer = [_device newBufferWithBytes:GlobalQuadVertices
                                             length:6 * sizeof(r32) * 4
                                            options:MTLResourceOptionCPUCacheModeDefault];
        if (!_vertexBuffer)
        {
            printf("ERROR: Failed to create quad vertex buffer.");
            return nil;
        }
        _vertexBuffer.label = @"quad vertices";
        
        _texCoordBuffer = [_device newBufferWithBytes:GlobalQuadTexCoords
                                               length:6 * sizeof(r32) * 2
                                              options:MTLResourceOptionCPUCacheModeDefault];
        if (!_texCoordBuffer)
        {
            printf("ERROR: Failed to create 2d texture coordinate buffer.");
            return nil;
        }
        _texCoordBuffer.label = @"quad texcoords";

    	_pixels = (char*)malloc(4 * frame.size.width * frame.size.height);
    	char *pixelIterator = _pixels;

    	for (u32 y = 0; y < frame.size.height; ++y)
    	{
    		for (u32 x = 0; x < frame.size.width; ++x)
    		{
    			*pixelIterator++ = 255;
    			*pixelIterator++ = 0;
    			*pixelIterator++ = 255;
    			*pixelIterator++ = 255;
    		}
    	}

        _renderSemaphore = dispatch_semaphore_create(2);

    	CVReturn cvReturn = CVDisplayLinkCreateWithActiveCGDisplays(&_displayLink);

    	Assert(cvReturn == kCVReturnSuccess);

    	cvReturn = CVDisplayLinkSetOutputCallback(_displayLink, &OnDisplayLinkFrame, (__bridge void *)self);

    	Assert(cvReturn == kCVReturnSuccess);

    	cvReturn = CVDisplayLinkSetCurrentCGDisplay(_displayLink, CGMainDisplayID());

		CVDisplayLinkStart(_displayLink);
    	
    	NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];

    	[notificationCenter addObserver:self
        	                   selector:@selector(windowWillClose:)
            	                   name:NSWindowWillCloseNotification
                	             object:self.window];
    }
    
    return self;
}

- (void)dealloc
{
    if (_displayLink)
    {
        [self stopUpdate];
    }
}

- (void)windowWillClose:(NSNotification*)notification
{
    // Stop the display link when the window is closing because we will
    // not be able to get a drawable, but the display link may continue
    // to fire
    if (notification.object == self.window)
    {
        CVDisplayLinkStop(_displayLink);
    }
}

- (void)update
{
    if (_layerSizeDidUpdate)
    {
        // Set the metal layer to the drawable size in case orientation or size changes.
        CGSize drawableSize = self.bounds.size;

        // Scale drawableSize so that drawable is 1:1 width pixels not 1:1 to points.
        NSScreen* screen = self.window.screen ?: [NSScreen mainScreen];
        drawableSize.width *= screen.backingScaleFactor;
        drawableSize.height *= screen.backingScaleFactor;
        
        _metalLayer.drawableSize = drawableSize;
        
        _layerSizeDidUpdate = NO;
    }
    
    dispatch_semaphore_wait(_renderSemaphore, DISPATCH_TIME_FOREVER);

	if (!_currentDrawable)
	{
    	_currentDrawable = [_metalLayer nextDrawable];
	}

    if (!_currentDrawable)
    {
        printf("ERROR: Failed to get a valid drawable.");
        _renderPassDesc = nil;
    }
    else
    {
	    if (_renderPassDesc == nil)
	    {
	    	_renderPassDesc = [MTLRenderPassDescriptor renderPassDescriptor];
	    }
	    
	    MTLRenderPassColorAttachmentDescriptor *colorAttachment = _renderPassDesc.colorAttachments[0];
	    colorAttachment.texture = _currentDrawable.texture;
	    
	    colorAttachment.loadAction = MTLLoadActionClear;
	    colorAttachment.clearColor = MTLClearColorMake(1.0f, 0.0f, 1.0f, 1.0f);
	    
	    colorAttachment.storeAction = MTLStoreActionStore;
    }

   	MTLRegion region = MTLRegionMake2D(0, 0, self.frame.size.width, self.frame.size.height);
	u32 rowBytes = self.frame.size.width * 4;
    [_texture replaceRegion:region
                mipmapLevel:0
                  withBytes:_pixels
                bytesPerRow:rowBytes];

	id<MTLCommandBuffer> commandBuffer = [_commandQueue commandBuffer];

    if (_renderPassDesc)
    {
        id<MTLRenderCommandEncoder> renderEncoder = [commandBuffer renderCommandEncoderWithDescriptor:_renderPassDesc];
        
	    [renderEncoder pushDebugGroup:@"encode quad"];

	    [renderEncoder setFrontFacingWinding:MTLWindingCounterClockwise];	    
	    [renderEncoder setRenderPipelineState:_pipelineState];
	    [renderEncoder setVertexBuffer:_vertexBuffer
	                            offset:0
	                           atIndex:0];
	    [renderEncoder setVertexBuffer:_texCoordBuffer
                        		offset:0
                       		   atIndex:1];
	    [renderEncoder setFragmentTexture:_texture
	                              atIndex:0];
	    [renderEncoder drawPrimitives:MTLPrimitiveTypeTriangle
	                      vertexStart:0
	                      vertexCount:6
	                    instanceCount:1];
	    [renderEncoder endEncoding];

	    [renderEncoder popDebugGroup];
        
        [commandBuffer presentDrawable:_currentDrawable];
    }

	__block dispatch_semaphore_t blockRenderSemaphore = _renderSemaphore;
	[commandBuffer addCompletedHandler:^(id<MTLCommandBuffer> cmdBuff){
	    dispatch_semaphore_signal(blockRenderSemaphore);
	}];

	[commandBuffer commit];
    
    _currentDrawable = nil;
}

- (void)stopUpdate
{
    if (_displayLink)
    {
        CVDisplayLinkStop(_displayLink);
        CVDisplayLinkRelease(_displayLink);
    }
}

@end




---- Shader ----

#include <metal_graphics>
#include <metal_geometric>
#include <metal_texture>

using namespace metal;

struct VertexInOut
{
    float4 pos [[position]];
    float2 texCoord [[user(texturecoord)]];
};

vertex VertexInOut TexturedQuadVertex(constant float4         *pos        [[ buffer(0) ]],
                                      constant packed_float2  *texCoords  [[ buffer(1) ]],
                                      uint                     vid        [[ vertex_id ]])
{
    VertexInOut outVertices;
    
    outVertices.pos      = pos[vid];
    outVertices.texCoord = texCoords[vid];
    
    return outVertices;
}

fragment half4 TexturedQuadFragment(VertexInOut      inFrag    [[ stage_in ]],
                                    texture2d<half>  tex2D     [[ texture(0) ]])
{
    constexpr sampler quadSampler;
    half4 color = tex2D.sample(quadSampler, inFrag.texCoord);
    
    return color;
}


The code is based on the Apple samples.
Adrian
46 posts
None
OSX Metal Vsync
Wow this seems like a lot of stuff. Isn't there a shorter way on blitting a bitmap to the screen with the metal api?
14 posts
OSX Metal Vsync
Well you can simplify things by not having an NSView and just passing the metal layer to the window (so most of the code will be moved to main), however, I don't think there is a way to blit a texture without using a shader and drawing something.
Mārtiņš Možeiko
2559 posts / 2 projects
OSX Metal Vsync
Metal is not meant for blitting pixels from CPU to GPU. Same with modern OpenGL - it will require bunch of code, including uploading data to texture and then drawing texture using GLSL shader. This is expected.

There's really no advantage of using Metal instead of legacy OpenGL (glBegin/glEnd) for just uploading texture and blitting it to screen.
Adrian
46 posts
None
OSX Metal Vsync
Could you point me to some resources on blitting a bitmap to the screen? Wanted to change my code from CoreGraphics to OpenGL to get a little bit in touch with it. Tried to distill it from Jeff Buck's OS X platform layer but there is so much going on and it's hard for me to understand all the code there.

I got some bitmap memory that I simply want to render to the screen using OpenGL.
Mārtiņš Možeiko
2559 posts / 2 projects
OSX Metal Vsync
HandmadeHero has code for blitting memory to screen using OpenGL.
Take a look at OpenGLDisplayBitmap function in handmade_opengl.cpp file.