src/render/metal/SDL_render_metal.m
changeset 12385 45038f8422c9
parent 12384 b1f5162fd621
child 12392 046dcbdbaa74
equal deleted inserted replaced
12384:b1f5162fd621 12385:45038f8422c9
  1071     statecache.projection_offset = 0;
  1071     statecache.projection_offset = 0;
  1072     statecache.color_offset = 0;
  1072     statecache.color_offset = 0;
  1073 
  1073 
  1074     // !!! FIXME: have a ring of pre-made MTLBuffers we cycle through? How expensive is creation?
  1074     // !!! FIXME: have a ring of pre-made MTLBuffers we cycle through? How expensive is creation?
  1075     if (vertsize > 0) {
  1075     if (vertsize > 0) {
  1076         id<MTLBuffer> mtlbufvertexstaging = [data.mtldevice newBufferWithLength:vertsize options:MTLResourceStorageModeShared];
  1076         /* We can memcpy to a shared buffer from the CPU and read it from the GPU
  1077         #if !__has_feature(objc_arc)
  1077          * without any extra copying. It's a bit slower on macOS to read shared
  1078         [mtlbufvertexstaging autorelease];
  1078          * data from the GPU than to read managed/private data, but we avoid the
  1079         #endif
  1079          * cost of copying the data and the code's simpler. Apple's best
  1080         mtlbufvertexstaging.label = @"SDL vertex staging data";
  1080          * practices guide recommends this approach for streamed vertex data.
  1081         SDL_memcpy([mtlbufvertexstaging contents], vertices, vertsize);
  1081          * TODO: this buffer is also used for constants. Is performance still
  1082 
  1082          * good for those, or should we have a managed buffer for them? */
  1083         // Move our new vertex buffer from system RAM to GPU memory so any draw calls can use it.
  1083         mtlbufvertex = [data.mtldevice newBufferWithLength:vertsize options:MTLResourceStorageModeShared];
  1084         mtlbufvertex = [data.mtldevice newBufferWithLength:vertsize options:MTLResourceStorageModePrivate];
       
  1085         #if !__has_feature(objc_arc)
  1084         #if !__has_feature(objc_arc)
  1086         [mtlbufvertex autorelease];
  1085         [mtlbufvertex autorelease];
  1087         #endif
  1086         #endif
  1088         mtlbufvertex.label = @"SDL vertex data";
  1087         mtlbufvertex.label = @"SDL vertex data";
  1089         id<MTLCommandBuffer> cmdbuffer = [data.mtlcmdqueue commandBuffer];
  1088         SDL_memcpy([mtlbufvertex contents], vertices, vertsize);
  1090         id<MTLBlitCommandEncoder> blitcmd = [cmdbuffer blitCommandEncoder];
       
  1091         [blitcmd copyFromBuffer:mtlbufvertexstaging sourceOffset:0 toBuffer:mtlbufvertex destinationOffset:0 size:vertsize];
       
  1092         [blitcmd endEncoding];
       
  1093         [cmdbuffer commit];
       
  1094     }
  1089     }
  1095 
  1090 
  1096     // If there's a command buffer here unexpectedly (app requested one?). Commit it so we can start fresh.
  1091     // If there's a command buffer here unexpectedly (app requested one?). Commit it so we can start fresh.
  1097     [data.mtlcmdencoder endEncoding];
  1092     [data.mtlcmdencoder endEncoding];
  1098     [data.mtlcmdbuffer commit];
  1093     [data.mtlcmdbuffer commit];