metal: avoid an extra buffer allocation and GPU data copy in RunCommandQueue, it's not needed. Improves overall performance.

This commit is contained in:
Alex Szpakowski 2018-11-01 20:24:21 -03:00
parent 4e86dfd8d1
commit 457390fcf8
1 changed files with 9 additions and 14 deletions

View File

@ -1073,24 +1073,19 @@ METAL_RunCommandQueue(SDL_Renderer * renderer, SDL_RenderCommand *cmd, void *ver
// !!! FIXME: have a ring of pre-made MTLBuffers we cycle through? How expensive is creation? // !!! FIXME: have a ring of pre-made MTLBuffers we cycle through? How expensive is creation?
if (vertsize > 0) { if (vertsize > 0) {
id<MTLBuffer> mtlbufvertexstaging = [data.mtldevice newBufferWithLength:vertsize options:MTLResourceStorageModeShared]; /* We can memcpy to a shared buffer from the CPU and read it from the GPU
#if !__has_feature(objc_arc) * without any extra copying. It's a bit slower on macOS to read shared
[mtlbufvertexstaging autorelease]; * data from the GPU than to read managed/private data, but we avoid the
#endif * cost of copying the data and the code's simpler. Apple's best
mtlbufvertexstaging.label = @"SDL vertex staging data"; * practices guide recommends this approach for streamed vertex data.
SDL_memcpy([mtlbufvertexstaging contents], vertices, vertsize); * TODO: this buffer is also used for constants. Is performance still
* good for those, or should we have a managed buffer for them? */
// Move our new vertex buffer from system RAM to GPU memory so any draw calls can use it. mtlbufvertex = [data.mtldevice newBufferWithLength:vertsize options:MTLResourceStorageModeShared];
mtlbufvertex = [data.mtldevice newBufferWithLength:vertsize options:MTLResourceStorageModePrivate];
#if !__has_feature(objc_arc) #if !__has_feature(objc_arc)
[mtlbufvertex autorelease]; [mtlbufvertex autorelease];
#endif #endif
mtlbufvertex.label = @"SDL vertex data"; mtlbufvertex.label = @"SDL vertex data";
id<MTLCommandBuffer> cmdbuffer = [data.mtlcmdqueue commandBuffer]; SDL_memcpy([mtlbufvertex contents], vertices, vertsize);
id<MTLBlitCommandEncoder> blitcmd = [cmdbuffer blitCommandEncoder];
[blitcmd copyFromBuffer:mtlbufvertexstaging sourceOffset:0 toBuffer:mtlbufvertex destinationOffset:0 size:vertsize];
[blitcmd endEncoding];
[cmdbuffer commit];
} }
// If there's a command buffer here unexpectedly (app requested one?). Commit it so we can start fresh. // If there's a command buffer here unexpectedly (app requested one?). Commit it so we can start fresh.