metal: Don't try to create a zero-byte vertex buffer. SDL-ryan-batching-renderer
authorRyan C. Gordon <icculus@icculus.org>
Thu, 04 Oct 2018 20:21:23 -0400
branchSDL-ryan-batching-renderer
changeset 12293d027a83b1386
parent 12290 a82837d659d6
child 12294 86b7b00ea2b8
metal: Don't try to create a zero-byte vertex buffer.

(Which will cause a crash in Metal, or an assert in the validation layer.)
src/render/metal/SDL_render_metal.m
     1.1 --- a/src/render/metal/SDL_render_metal.m	Thu Oct 04 16:34:44 2018 -0400
     1.2 +++ b/src/render/metal/SDL_render_metal.m	Thu Oct 04 20:21:23 2018 -0400
     1.3 @@ -1052,6 +1052,7 @@
     1.4  { @autoreleasepool {
     1.5      METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
     1.6      METAL_DrawStateCache statecache;
     1.7 +    id<MTLBuffer> mtlbufvertex = nil;
     1.8  
     1.9      statecache.pipeline = nil;
    1.10      statecache.constants_offset = CONSTANTS_OFFSET_INVALID;
    1.11 @@ -1063,24 +1064,26 @@
    1.12      statecache.color_offset = 0;
    1.13  
    1.14      // !!! FIXME: have a ring of pre-made MTLBuffers we cycle through? How expensive is creation?
    1.15 -    id<MTLBuffer> mtlbufvertexstaging = [data.mtldevice newBufferWithLength:vertsize options:MTLResourceStorageModeShared];
    1.16 -    #if !__has_feature(objc_arc)
    1.17 -    [mtlbufvertexstaging autorelease];
    1.18 -    #endif
    1.19 -    mtlbufvertexstaging.label = @"SDL vertex staging data";
    1.20 -    SDL_memcpy([mtlbufvertexstaging contents], vertices, vertsize);
    1.21 +    if (vertsize > 0) {
    1.22 +        id<MTLBuffer> mtlbufvertexstaging = [data.mtldevice newBufferWithLength:vertsize options:MTLResourceStorageModeShared];
    1.23 +        #if !__has_feature(objc_arc)
    1.24 +        [mtlbufvertexstaging autorelease];
    1.25 +        #endif
    1.26 +        mtlbufvertexstaging.label = @"SDL vertex staging data";
    1.27 +        SDL_memcpy([mtlbufvertexstaging contents], vertices, vertsize);
    1.28  
    1.29 -    // Move our new vertex buffer from system RAM to GPU memory so any draw calls can use it.
    1.30 -    id<MTLBuffer> mtlbufvertex = [data.mtldevice newBufferWithLength:vertsize options:MTLResourceStorageModePrivate];
    1.31 -    #if !__has_feature(objc_arc)
    1.32 -    [mtlbufvertex autorelease];
    1.33 -    #endif
    1.34 -    mtlbufvertex.label = @"SDL vertex data";
    1.35 -    id<MTLCommandBuffer> cmdbuffer = [data.mtlcmdqueue commandBuffer];
    1.36 -    id<MTLBlitCommandEncoder> blitcmd = [cmdbuffer blitCommandEncoder];
    1.37 -    [blitcmd copyFromBuffer:mtlbufvertexstaging sourceOffset:0 toBuffer:mtlbufvertex destinationOffset:0 size:vertsize];
    1.38 -    [blitcmd endEncoding];
    1.39 -    [cmdbuffer commit];
    1.40 +        // Move our new vertex buffer from system RAM to GPU memory so any draw calls can use it.
    1.41 +        mtlbufvertex = [data.mtldevice newBufferWithLength:vertsize options:MTLResourceStorageModePrivate];
    1.42 +        #if !__has_feature(objc_arc)
    1.43 +        [mtlbufvertex autorelease];
    1.44 +        #endif
    1.45 +        mtlbufvertex.label = @"SDL vertex data";
    1.46 +        id<MTLCommandBuffer> cmdbuffer = [data.mtlcmdqueue commandBuffer];
    1.47 +        id<MTLBlitCommandEncoder> blitcmd = [cmdbuffer blitCommandEncoder];
    1.48 +        [blitcmd copyFromBuffer:mtlbufvertexstaging sourceOffset:0 toBuffer:mtlbufvertex destinationOffset:0 size:vertsize];
    1.49 +        [blitcmd endEncoding];
    1.50 +        [cmdbuffer commit];
    1.51 +    }
    1.52  
    1.53      // If there's a command buffer here unexpectedly (app requested one?). Commit it so we can start fresh.
    1.54      [data.mtlcmdencoder endEncoding];