SDL_render_metal.m 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "../../SDL_internal.h"
  19. #if SDL_VIDEO_RENDER_METAL && !SDL_RENDER_DISABLED
  20. #include "SDL_hints.h"
  21. #include "SDL_syswm.h"
  22. #include "SDL_metal.h"
  23. #include "../SDL_sysrender.h"
  24. #include <Availability.h>
  25. #import <Metal/Metal.h>
  26. #import <QuartzCore/CAMetalLayer.h>
  27. #ifdef __MACOSX__
  28. #import <AppKit/NSView.h>
  29. #endif
  30. /* Regenerate these with build-metal-shaders.sh */
  31. #ifdef __MACOSX__
  32. #include "SDL_shaders_metal_osx.h"
  33. #elif defined(__TVOS__)
  34. #include "SDL_shaders_metal_tvos.h"
  35. #else
  36. #include "SDL_shaders_metal_ios.h"
  37. #endif
  38. /* Apple Metal renderer implementation */
  39. /* Used to re-create the window with Metal capability */
  40. extern int SDL_RecreateWindow(SDL_Window * window, Uint32 flags);
  41. /* macOS requires constants in a buffer to have a 256 byte alignment. */
  42. /* Use native type alignments from https://developer.apple.com/metal/Metal-Shading-Language-Specification.pdf */
  43. #ifdef __MACOSX__
  44. #define CONSTANT_ALIGN(x) (256)
  45. #else
  46. #define CONSTANT_ALIGN(x) (x < 4 ? 4 : x)
  47. #endif
  48. #define DEVICE_ALIGN(x) (x < 4 ? 4 : x)
  49. #define ALIGN_CONSTANTS(align, size) ((size + CONSTANT_ALIGN(align) - 1) & (~(CONSTANT_ALIGN(align) - 1)))
  50. static const size_t CONSTANTS_OFFSET_INVALID = 0xFFFFFFFF;
  51. static const size_t CONSTANTS_OFFSET_IDENTITY = 0;
  52. static const size_t CONSTANTS_OFFSET_HALF_PIXEL_TRANSFORM = ALIGN_CONSTANTS(16, CONSTANTS_OFFSET_IDENTITY + sizeof(float) * 16);
  53. static const size_t CONSTANTS_OFFSET_DECODE_JPEG = ALIGN_CONSTANTS(16, CONSTANTS_OFFSET_HALF_PIXEL_TRANSFORM + sizeof(float) * 16);
  54. static const size_t CONSTANTS_OFFSET_DECODE_BT601 = ALIGN_CONSTANTS(16, CONSTANTS_OFFSET_DECODE_JPEG + sizeof(float) * 4 * 4);
  55. static const size_t CONSTANTS_OFFSET_DECODE_BT709 = ALIGN_CONSTANTS(16, CONSTANTS_OFFSET_DECODE_BT601 + sizeof(float) * 4 * 4);
  56. static const size_t CONSTANTS_LENGTH = CONSTANTS_OFFSET_DECODE_BT709 + sizeof(float) * 4 * 4;
  57. typedef enum SDL_MetalVertexFunction
  58. {
  59. SDL_METAL_VERTEX_SOLID,
  60. SDL_METAL_VERTEX_COPY,
  61. } SDL_MetalVertexFunction;
  62. typedef enum SDL_MetalFragmentFunction
  63. {
  64. SDL_METAL_FRAGMENT_SOLID = 0,
  65. SDL_METAL_FRAGMENT_COPY,
  66. SDL_METAL_FRAGMENT_YUV,
  67. SDL_METAL_FRAGMENT_NV12,
  68. SDL_METAL_FRAGMENT_NV21,
  69. SDL_METAL_FRAGMENT_COUNT,
  70. } SDL_MetalFragmentFunction;
  71. typedef struct METAL_PipelineState
  72. {
  73. SDL_BlendMode blendMode;
  74. void *pipe;
  75. } METAL_PipelineState;
  76. typedef struct METAL_PipelineCache
  77. {
  78. METAL_PipelineState *states;
  79. int count;
  80. SDL_MetalVertexFunction vertexFunction;
  81. SDL_MetalFragmentFunction fragmentFunction;
  82. MTLPixelFormat renderTargetFormat;
  83. const char *label;
  84. } METAL_PipelineCache;
  85. /* Each shader combination used by drawing functions has a separate pipeline
  86. * cache, and we have a separate list of caches for each render target pixel
  87. * format. This is more efficient than iterating over a global cache to find
  88. * the pipeline based on the specified shader combination and RT pixel format,
  89. * since we know what the RT pixel format is when we set the render target, and
  90. * we know what the shader combination is inside each drawing function's code. */
  91. typedef struct METAL_ShaderPipelines
  92. {
  93. MTLPixelFormat renderTargetFormat;
  94. METAL_PipelineCache caches[SDL_METAL_FRAGMENT_COUNT];
  95. } METAL_ShaderPipelines;
  96. @interface METAL_RenderData : NSObject
  97. @property (nonatomic, retain) id<MTLDevice> mtldevice;
  98. @property (nonatomic, retain) id<MTLCommandQueue> mtlcmdqueue;
  99. @property (nonatomic, retain) id<MTLCommandBuffer> mtlcmdbuffer;
  100. @property (nonatomic, retain) id<MTLRenderCommandEncoder> mtlcmdencoder;
  101. @property (nonatomic, retain) id<MTLLibrary> mtllibrary;
  102. @property (nonatomic, retain) id<CAMetalDrawable> mtlbackbuffer;
  103. @property (nonatomic, retain) id<MTLSamplerState> mtlsamplernearest;
  104. @property (nonatomic, retain) id<MTLSamplerState> mtlsamplerlinear;
  105. @property (nonatomic, retain) id<MTLBuffer> mtlbufconstants;
  106. @property (nonatomic, retain) id<MTLBuffer> mtlbufquadindices;
  107. @property (nonatomic, assign) SDL_MetalView mtlview;
  108. @property (nonatomic, retain) CAMetalLayer *mtllayer;
  109. @property (nonatomic, retain) MTLRenderPassDescriptor *mtlpassdesc;
  110. @property (nonatomic, assign) METAL_ShaderPipelines *activepipelines;
  111. @property (nonatomic, assign) METAL_ShaderPipelines *allpipelines;
  112. @property (nonatomic, assign) int pipelinescount;
  113. @end
  114. @implementation METAL_RenderData
  115. #if !__has_feature(objc_arc)
  116. - (void)dealloc
  117. {
  118. [_mtldevice release];
  119. [_mtlcmdqueue release];
  120. [_mtlcmdbuffer release];
  121. [_mtlcmdencoder release];
  122. [_mtllibrary release];
  123. [_mtlbackbuffer release];
  124. [_mtlsamplernearest release];
  125. [_mtlsamplerlinear release];
  126. [_mtlbufconstants release];
  127. [_mtlbufquadindices release];
  128. [_mtllayer release];
  129. [_mtlpassdesc release];
  130. [super dealloc];
  131. }
  132. #endif
  133. @end
  134. @interface METAL_TextureData : NSObject
  135. @property (nonatomic, retain) id<MTLTexture> mtltexture;
  136. @property (nonatomic, retain) id<MTLTexture> mtltexture_uv;
  137. @property (nonatomic, retain) id<MTLSamplerState> mtlsampler;
  138. @property (nonatomic, assign) SDL_MetalFragmentFunction fragmentFunction;
  139. @property (nonatomic, assign) BOOL yuv;
  140. @property (nonatomic, assign) BOOL nv12;
  141. @property (nonatomic, assign) size_t conversionBufferOffset;
  142. @property (nonatomic, assign) BOOL hasdata;
  143. @property (nonatomic, retain) id<MTLBuffer> lockedbuffer;
  144. @property (nonatomic, assign) SDL_Rect lockedrect;
  145. @end
  146. @implementation METAL_TextureData
  147. #if !__has_feature(objc_arc)
  148. - (void)dealloc
  149. {
  150. [_mtltexture release];
  151. [_mtltexture_uv release];
  152. [_mtlsampler release];
  153. [_lockedbuffer release];
  154. [super dealloc];
  155. }
  156. #endif
  157. @end
  158. static int
  159. IsMetalAvailable(const SDL_SysWMinfo *syswm)
  160. {
  161. if (syswm->subsystem != SDL_SYSWM_COCOA && syswm->subsystem != SDL_SYSWM_UIKIT) {
  162. return SDL_SetError("Metal render target only supports Cocoa and UIKit video targets at the moment.");
  163. }
  164. // this checks a weak symbol.
  165. #if (defined(__MACOSX__) && (MAC_OS_X_VERSION_MIN_REQUIRED < 101100))
  166. if (MTLCreateSystemDefaultDevice == NULL) { // probably on 10.10 or lower.
  167. return SDL_SetError("Metal framework not available on this system");
  168. }
  169. #endif
  170. return 0;
  171. }
  172. static const MTLBlendOperation invalidBlendOperation = (MTLBlendOperation)0xFFFFFFFF;
  173. static const MTLBlendFactor invalidBlendFactor = (MTLBlendFactor)0xFFFFFFFF;
  174. static MTLBlendOperation
  175. GetBlendOperation(SDL_BlendOperation operation)
  176. {
  177. switch (operation) {
  178. case SDL_BLENDOPERATION_ADD: return MTLBlendOperationAdd;
  179. case SDL_BLENDOPERATION_SUBTRACT: return MTLBlendOperationSubtract;
  180. case SDL_BLENDOPERATION_REV_SUBTRACT: return MTLBlendOperationReverseSubtract;
  181. case SDL_BLENDOPERATION_MINIMUM: return MTLBlendOperationMin;
  182. case SDL_BLENDOPERATION_MAXIMUM: return MTLBlendOperationMax;
  183. default: return invalidBlendOperation;
  184. }
  185. }
  186. static MTLBlendFactor
  187. GetBlendFactor(SDL_BlendFactor factor)
  188. {
  189. switch (factor) {
  190. case SDL_BLENDFACTOR_ZERO: return MTLBlendFactorZero;
  191. case SDL_BLENDFACTOR_ONE: return MTLBlendFactorOne;
  192. case SDL_BLENDFACTOR_SRC_COLOR: return MTLBlendFactorSourceColor;
  193. case SDL_BLENDFACTOR_ONE_MINUS_SRC_COLOR: return MTLBlendFactorOneMinusSourceColor;
  194. case SDL_BLENDFACTOR_SRC_ALPHA: return MTLBlendFactorSourceAlpha;
  195. case SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA: return MTLBlendFactorOneMinusSourceAlpha;
  196. case SDL_BLENDFACTOR_DST_COLOR: return MTLBlendFactorDestinationColor;
  197. case SDL_BLENDFACTOR_ONE_MINUS_DST_COLOR: return MTLBlendFactorOneMinusDestinationColor;
  198. case SDL_BLENDFACTOR_DST_ALPHA: return MTLBlendFactorDestinationAlpha;
  199. case SDL_BLENDFACTOR_ONE_MINUS_DST_ALPHA: return MTLBlendFactorOneMinusDestinationAlpha;
  200. default: return invalidBlendFactor;
  201. }
  202. }
  203. static NSString *
  204. GetVertexFunctionName(SDL_MetalVertexFunction function)
  205. {
  206. switch (function) {
  207. case SDL_METAL_VERTEX_SOLID: return @"SDL_Solid_vertex";
  208. case SDL_METAL_VERTEX_COPY: return @"SDL_Copy_vertex";
  209. default: return nil;
  210. }
  211. }
  212. static NSString *
  213. GetFragmentFunctionName(SDL_MetalFragmentFunction function)
  214. {
  215. switch (function) {
  216. case SDL_METAL_FRAGMENT_SOLID: return @"SDL_Solid_fragment";
  217. case SDL_METAL_FRAGMENT_COPY: return @"SDL_Copy_fragment";
  218. case SDL_METAL_FRAGMENT_YUV: return @"SDL_YUV_fragment";
  219. case SDL_METAL_FRAGMENT_NV12: return @"SDL_NV12_fragment";
  220. case SDL_METAL_FRAGMENT_NV21: return @"SDL_NV21_fragment";
  221. default: return nil;
  222. }
  223. }
  224. static id<MTLRenderPipelineState>
  225. MakePipelineState(METAL_RenderData *data, METAL_PipelineCache *cache,
  226. NSString *blendlabel, SDL_BlendMode blendmode)
  227. {
  228. id<MTLFunction> mtlvertfn = [data.mtllibrary newFunctionWithName:GetVertexFunctionName(cache->vertexFunction)];
  229. id<MTLFunction> mtlfragfn = [data.mtllibrary newFunctionWithName:GetFragmentFunctionName(cache->fragmentFunction)];
  230. SDL_assert(mtlvertfn != nil);
  231. SDL_assert(mtlfragfn != nil);
  232. MTLRenderPipelineDescriptor *mtlpipedesc = [[MTLRenderPipelineDescriptor alloc] init];
  233. mtlpipedesc.vertexFunction = mtlvertfn;
  234. mtlpipedesc.fragmentFunction = mtlfragfn;
  235. MTLVertexDescriptor *vertdesc = [MTLVertexDescriptor vertexDescriptor];
  236. switch (cache->vertexFunction) {
  237. case SDL_METAL_VERTEX_SOLID:
  238. /* position (float2) */
  239. vertdesc.layouts[0].stride = sizeof(float) * 2;
  240. vertdesc.layouts[0].stepFunction = MTLVertexStepFunctionPerVertex;
  241. vertdesc.attributes[0].format = MTLVertexFormatFloat2;
  242. vertdesc.attributes[0].offset = 0;
  243. vertdesc.attributes[0].bufferIndex = 0;
  244. break;
  245. case SDL_METAL_VERTEX_COPY:
  246. /* position (float2), texcoord (float2) */
  247. vertdesc.layouts[0].stride = sizeof(float) * 4;
  248. vertdesc.layouts[0].stepFunction = MTLVertexStepFunctionPerVertex;
  249. vertdesc.attributes[0].format = MTLVertexFormatFloat2;
  250. vertdesc.attributes[0].offset = 0;
  251. vertdesc.attributes[0].bufferIndex = 0;
  252. vertdesc.attributes[1].format = MTLVertexFormatFloat2;
  253. vertdesc.attributes[1].offset = sizeof(float) * 2;
  254. vertdesc.attributes[1].bufferIndex = 0;
  255. break;
  256. }
  257. mtlpipedesc.vertexDescriptor = vertdesc;
  258. MTLRenderPipelineColorAttachmentDescriptor *rtdesc = mtlpipedesc.colorAttachments[0];
  259. rtdesc.pixelFormat = cache->renderTargetFormat;
  260. if (blendmode != SDL_BLENDMODE_NONE) {
  261. rtdesc.blendingEnabled = YES;
  262. rtdesc.sourceRGBBlendFactor = GetBlendFactor(SDL_GetBlendModeSrcColorFactor(blendmode));
  263. rtdesc.destinationRGBBlendFactor = GetBlendFactor(SDL_GetBlendModeDstColorFactor(blendmode));
  264. rtdesc.rgbBlendOperation = GetBlendOperation(SDL_GetBlendModeColorOperation(blendmode));
  265. rtdesc.sourceAlphaBlendFactor = GetBlendFactor(SDL_GetBlendModeSrcAlphaFactor(blendmode));
  266. rtdesc.destinationAlphaBlendFactor = GetBlendFactor(SDL_GetBlendModeDstAlphaFactor(blendmode));
  267. rtdesc.alphaBlendOperation = GetBlendOperation(SDL_GetBlendModeAlphaOperation(blendmode));
  268. } else {
  269. rtdesc.blendingEnabled = NO;
  270. }
  271. mtlpipedesc.label = [@(cache->label) stringByAppendingString:blendlabel];
  272. NSError *err = nil;
  273. id<MTLRenderPipelineState> state = [data.mtldevice newRenderPipelineStateWithDescriptor:mtlpipedesc error:&err];
  274. SDL_assert(err == nil);
  275. METAL_PipelineState pipeline;
  276. pipeline.blendMode = blendmode;
  277. pipeline.pipe = (void *)CFBridgingRetain(state);
  278. METAL_PipelineState *states = SDL_realloc(cache->states, (cache->count + 1) * sizeof(pipeline));
  279. #if !__has_feature(objc_arc)
  280. [mtlpipedesc release]; // !!! FIXME: can these be reused for each creation, or does the pipeline obtain it?
  281. [mtlvertfn release];
  282. [mtlfragfn release];
  283. [state release];
  284. #endif
  285. if (states) {
  286. states[cache->count++] = pipeline;
  287. cache->states = states;
  288. return (__bridge id<MTLRenderPipelineState>)pipeline.pipe;
  289. } else {
  290. CFBridgingRelease(pipeline.pipe);
  291. SDL_OutOfMemory();
  292. return NULL;
  293. }
  294. }
  295. static void
  296. MakePipelineCache(METAL_RenderData *data, METAL_PipelineCache *cache, const char *label,
  297. MTLPixelFormat rtformat, SDL_MetalVertexFunction vertfn, SDL_MetalFragmentFunction fragfn)
  298. {
  299. SDL_zerop(cache);
  300. cache->vertexFunction = vertfn;
  301. cache->fragmentFunction = fragfn;
  302. cache->renderTargetFormat = rtformat;
  303. cache->label = label;
  304. /* Create pipeline states for the default blend modes. Custom blend modes
  305. * will be added to the cache on-demand. */
  306. MakePipelineState(data, cache, @" (blend=none)", SDL_BLENDMODE_NONE);
  307. MakePipelineState(data, cache, @" (blend=blend)", SDL_BLENDMODE_BLEND);
  308. MakePipelineState(data, cache, @" (blend=add)", SDL_BLENDMODE_ADD);
  309. MakePipelineState(data, cache, @" (blend=mod)", SDL_BLENDMODE_MOD);
  310. MakePipelineState(data, cache, @" (blend=mul)", SDL_BLENDMODE_MUL);
  311. }
  312. static void
  313. DestroyPipelineCache(METAL_PipelineCache *cache)
  314. {
  315. if (cache != NULL) {
  316. for (int i = 0; i < cache->count; i++) {
  317. CFBridgingRelease(cache->states[i].pipe);
  318. }
  319. SDL_free(cache->states);
  320. }
  321. }
  322. void
  323. MakeShaderPipelines(METAL_RenderData *data, METAL_ShaderPipelines *pipelines, MTLPixelFormat rtformat)
  324. {
  325. SDL_zerop(pipelines);
  326. pipelines->renderTargetFormat = rtformat;
  327. MakePipelineCache(data, &pipelines->caches[SDL_METAL_FRAGMENT_SOLID], "SDL primitives pipeline", rtformat, SDL_METAL_VERTEX_SOLID, SDL_METAL_FRAGMENT_SOLID);
  328. MakePipelineCache(data, &pipelines->caches[SDL_METAL_FRAGMENT_COPY], "SDL copy pipeline", rtformat, SDL_METAL_VERTEX_COPY, SDL_METAL_FRAGMENT_COPY);
  329. MakePipelineCache(data, &pipelines->caches[SDL_METAL_FRAGMENT_YUV], "SDL YUV pipeline", rtformat, SDL_METAL_VERTEX_COPY, SDL_METAL_FRAGMENT_YUV);
  330. MakePipelineCache(data, &pipelines->caches[SDL_METAL_FRAGMENT_NV12], "SDL NV12 pipeline", rtformat, SDL_METAL_VERTEX_COPY, SDL_METAL_FRAGMENT_NV12);
  331. MakePipelineCache(data, &pipelines->caches[SDL_METAL_FRAGMENT_NV21], "SDL NV21 pipeline", rtformat, SDL_METAL_VERTEX_COPY, SDL_METAL_FRAGMENT_NV21);
  332. }
  333. static METAL_ShaderPipelines *
  334. ChooseShaderPipelines(METAL_RenderData *data, MTLPixelFormat rtformat)
  335. {
  336. METAL_ShaderPipelines *allpipelines = data.allpipelines;
  337. int count = data.pipelinescount;
  338. for (int i = 0; i < count; i++) {
  339. if (allpipelines[i].renderTargetFormat == rtformat) {
  340. return &allpipelines[i];
  341. }
  342. }
  343. allpipelines = SDL_realloc(allpipelines, (count + 1) * sizeof(METAL_ShaderPipelines));
  344. if (allpipelines == NULL) {
  345. SDL_OutOfMemory();
  346. return NULL;
  347. }
  348. MakeShaderPipelines(data, &allpipelines[count], rtformat);
  349. data.allpipelines = allpipelines;
  350. data.pipelinescount = count + 1;
  351. return &data.allpipelines[count];
  352. }
  353. static void
  354. DestroyAllPipelines(METAL_ShaderPipelines *allpipelines, int count)
  355. {
  356. if (allpipelines != NULL) {
  357. for (int i = 0; i < count; i++) {
  358. for (int cache = 0; cache < SDL_METAL_FRAGMENT_COUNT; cache++) {
  359. DestroyPipelineCache(&allpipelines[i].caches[cache]);
  360. }
  361. }
  362. SDL_free(allpipelines);
  363. }
  364. }
  365. static inline id<MTLRenderPipelineState>
  366. ChoosePipelineState(METAL_RenderData *data, METAL_ShaderPipelines *pipelines, SDL_MetalFragmentFunction fragfn, SDL_BlendMode blendmode)
  367. {
  368. METAL_PipelineCache *cache = &pipelines->caches[fragfn];
  369. for (int i = 0; i < cache->count; i++) {
  370. if (cache->states[i].blendMode == blendmode) {
  371. return (__bridge id<MTLRenderPipelineState>)cache->states[i].pipe;
  372. }
  373. }
  374. return MakePipelineState(data, cache, [NSString stringWithFormat:@" (blend=custom 0x%x)", blendmode], blendmode);
  375. }
  376. static void
  377. METAL_ActivateRenderCommandEncoder(SDL_Renderer * renderer, MTLLoadAction load, MTLClearColor *clear_color, id<MTLBuffer> vertex_buffer)
  378. {
  379. METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
  380. /* Our SetRenderTarget just signals that the next render operation should
  381. * set up a new render pass. This is where that work happens. */
  382. if (data.mtlcmdencoder == nil) {
  383. id<MTLTexture> mtltexture = nil;
  384. if (renderer->target != NULL) {
  385. METAL_TextureData *texdata = (__bridge METAL_TextureData *)renderer->target->driverdata;
  386. mtltexture = texdata.mtltexture;
  387. } else {
  388. if (data.mtlbackbuffer == nil) {
  389. /* The backbuffer's contents aren't guaranteed to persist after
  390. * presenting, so we can leave it undefined when loading it. */
  391. data.mtlbackbuffer = [data.mtllayer nextDrawable];
  392. if (load == MTLLoadActionLoad) {
  393. load = MTLLoadActionDontCare;
  394. }
  395. }
  396. mtltexture = data.mtlbackbuffer.texture;
  397. }
  398. SDL_assert(mtltexture);
  399. if (load == MTLLoadActionClear) {
  400. SDL_assert(clear_color != NULL);
  401. data.mtlpassdesc.colorAttachments[0].clearColor = *clear_color;
  402. }
  403. data.mtlpassdesc.colorAttachments[0].loadAction = load;
  404. data.mtlpassdesc.colorAttachments[0].texture = mtltexture;
  405. data.mtlcmdbuffer = [data.mtlcmdqueue commandBuffer];
  406. data.mtlcmdencoder = [data.mtlcmdbuffer renderCommandEncoderWithDescriptor:data.mtlpassdesc];
  407. if (data.mtlbackbuffer != nil && mtltexture == data.mtlbackbuffer.texture) {
  408. data.mtlcmdencoder.label = @"SDL metal renderer backbuffer";
  409. } else {
  410. data.mtlcmdencoder.label = @"SDL metal renderer render target";
  411. }
  412. /* Set up buffer bindings for positions, texcoords, and color once here,
  413. * the offsets are adjusted in the code that uses them. */
  414. if (vertex_buffer != nil) {
  415. [data.mtlcmdencoder setVertexBuffer:vertex_buffer offset:0 atIndex:0];
  416. [data.mtlcmdencoder setFragmentBuffer:vertex_buffer offset:0 atIndex:0];
  417. }
  418. data.activepipelines = ChooseShaderPipelines(data, mtltexture.pixelFormat);
  419. // make sure this has a definite place in the queue. This way it will
  420. // execute reliably whether the app tries to make its own command buffers
  421. // or whatever. This means we can _always_ batch rendering commands!
  422. [data.mtlcmdbuffer enqueue];
  423. }
  424. }
  425. static void
  426. METAL_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
  427. {
  428. if (event->event == SDL_WINDOWEVENT_SHOWN ||
  429. event->event == SDL_WINDOWEVENT_HIDDEN) {
  430. // !!! FIXME: write me
  431. }
  432. }
  433. static int
  434. METAL_GetOutputSize(SDL_Renderer * renderer, int *w, int *h)
  435. { @autoreleasepool {
  436. METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
  437. if (w) {
  438. *w = (int)data.mtllayer.drawableSize.width;
  439. }
  440. if (h) {
  441. *h = (int)data.mtllayer.drawableSize.height;
  442. }
  443. return 0;
  444. }}
  445. static SDL_bool
  446. METAL_SupportsBlendMode(SDL_Renderer * renderer, SDL_BlendMode blendMode)
  447. {
  448. SDL_BlendFactor srcColorFactor = SDL_GetBlendModeSrcColorFactor(blendMode);
  449. SDL_BlendFactor srcAlphaFactor = SDL_GetBlendModeSrcAlphaFactor(blendMode);
  450. SDL_BlendOperation colorOperation = SDL_GetBlendModeColorOperation(blendMode);
  451. SDL_BlendFactor dstColorFactor = SDL_GetBlendModeDstColorFactor(blendMode);
  452. SDL_BlendFactor dstAlphaFactor = SDL_GetBlendModeDstAlphaFactor(blendMode);
  453. SDL_BlendOperation alphaOperation = SDL_GetBlendModeAlphaOperation(blendMode);
  454. if (GetBlendFactor(srcColorFactor) == invalidBlendFactor ||
  455. GetBlendFactor(srcAlphaFactor) == invalidBlendFactor ||
  456. GetBlendOperation(colorOperation) == invalidBlendOperation ||
  457. GetBlendFactor(dstColorFactor) == invalidBlendFactor ||
  458. GetBlendFactor(dstAlphaFactor) == invalidBlendFactor ||
  459. GetBlendOperation(alphaOperation) == invalidBlendOperation) {
  460. return SDL_FALSE;
  461. }
  462. return SDL_TRUE;
  463. }
  464. static int
  465. METAL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
  466. { @autoreleasepool {
  467. METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
  468. MTLPixelFormat pixfmt;
  469. switch (texture->format) {
  470. case SDL_PIXELFORMAT_ABGR8888:
  471. pixfmt = MTLPixelFormatRGBA8Unorm;
  472. break;
  473. case SDL_PIXELFORMAT_ARGB8888:
  474. pixfmt = MTLPixelFormatBGRA8Unorm;
  475. break;
  476. case SDL_PIXELFORMAT_IYUV:
  477. case SDL_PIXELFORMAT_YV12:
  478. case SDL_PIXELFORMAT_NV12:
  479. case SDL_PIXELFORMAT_NV21:
  480. pixfmt = MTLPixelFormatR8Unorm;
  481. break;
  482. default:
  483. return SDL_SetError("Texture format %s not supported by Metal", SDL_GetPixelFormatName(texture->format));
  484. }
  485. MTLTextureDescriptor *mtltexdesc = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:pixfmt
  486. width:(NSUInteger)texture->w height:(NSUInteger)texture->h mipmapped:NO];
  487. /* Not available in iOS 8. */
  488. if ([mtltexdesc respondsToSelector:@selector(usage)]) {
  489. if (texture->access == SDL_TEXTUREACCESS_TARGET) {
  490. mtltexdesc.usage = MTLTextureUsageShaderRead | MTLTextureUsageRenderTarget;
  491. } else {
  492. mtltexdesc.usage = MTLTextureUsageShaderRead;
  493. }
  494. }
  495. id<MTLTexture> mtltexture = [data.mtldevice newTextureWithDescriptor:mtltexdesc];
  496. if (mtltexture == nil) {
  497. return SDL_SetError("Texture allocation failed");
  498. }
  499. id<MTLTexture> mtltexture_uv = nil;
  500. BOOL yuv = (texture->format == SDL_PIXELFORMAT_IYUV) || (texture->format == SDL_PIXELFORMAT_YV12);
  501. BOOL nv12 = (texture->format == SDL_PIXELFORMAT_NV12) || (texture->format == SDL_PIXELFORMAT_NV21);
  502. if (yuv) {
  503. mtltexdesc.pixelFormat = MTLPixelFormatR8Unorm;
  504. mtltexdesc.width = (texture->w + 1) / 2;
  505. mtltexdesc.height = (texture->h + 1) / 2;
  506. mtltexdesc.textureType = MTLTextureType2DArray;
  507. mtltexdesc.arrayLength = 2;
  508. } else if (nv12) {
  509. mtltexdesc.pixelFormat = MTLPixelFormatRG8Unorm;
  510. mtltexdesc.width = (texture->w + 1) / 2;
  511. mtltexdesc.height = (texture->h + 1) / 2;
  512. }
  513. if (yuv || nv12) {
  514. mtltexture_uv = [data.mtldevice newTextureWithDescriptor:mtltexdesc];
  515. if (mtltexture_uv == nil) {
  516. #if !__has_feature(objc_arc)
  517. [mtltexture release];
  518. #endif
  519. return SDL_SetError("Texture allocation failed");
  520. }
  521. }
  522. METAL_TextureData *texturedata = [[METAL_TextureData alloc] init];
  523. if (texture->scaleMode == SDL_ScaleModeNearest) {
  524. texturedata.mtlsampler = data.mtlsamplernearest;
  525. } else {
  526. texturedata.mtlsampler = data.mtlsamplerlinear;
  527. }
  528. texturedata.mtltexture = mtltexture;
  529. texturedata.mtltexture_uv = mtltexture_uv;
  530. texturedata.yuv = yuv;
  531. texturedata.nv12 = nv12;
  532. if (yuv) {
  533. texturedata.fragmentFunction = SDL_METAL_FRAGMENT_YUV;
  534. } else if (texture->format == SDL_PIXELFORMAT_NV12) {
  535. texturedata.fragmentFunction = SDL_METAL_FRAGMENT_NV12;
  536. } else if (texture->format == SDL_PIXELFORMAT_NV21) {
  537. texturedata.fragmentFunction = SDL_METAL_FRAGMENT_NV21;
  538. } else {
  539. texturedata.fragmentFunction = SDL_METAL_FRAGMENT_COPY;
  540. }
  541. if (yuv || nv12) {
  542. size_t offset = 0;
  543. SDL_YUV_CONVERSION_MODE mode = SDL_GetYUVConversionModeForResolution(texture->w, texture->h);
  544. switch (mode) {
  545. case SDL_YUV_CONVERSION_JPEG: offset = CONSTANTS_OFFSET_DECODE_JPEG; break;
  546. case SDL_YUV_CONVERSION_BT601: offset = CONSTANTS_OFFSET_DECODE_BT601; break;
  547. case SDL_YUV_CONVERSION_BT709: offset = CONSTANTS_OFFSET_DECODE_BT709; break;
  548. default: offset = 0; break;
  549. }
  550. texturedata.conversionBufferOffset = offset;
  551. }
  552. texture->driverdata = (void*)CFBridgingRetain(texturedata);
  553. #if !__has_feature(objc_arc)
  554. [texturedata release];
  555. [mtltexture release];
  556. [mtltexture_uv release];
  557. #endif
  558. return 0;
  559. }}
  560. static void
  561. METAL_UploadTextureData(id<MTLTexture> texture, SDL_Rect rect, int slice,
  562. const void * pixels, int pitch)
  563. {
  564. [texture replaceRegion:MTLRegionMake2D(rect.x, rect.y, rect.w, rect.h)
  565. mipmapLevel:0
  566. slice:slice
  567. withBytes:pixels
  568. bytesPerRow:pitch
  569. bytesPerImage:0];
  570. }
  571. static MTLStorageMode
  572. METAL_GetStorageMode(id<MTLResource> resource)
  573. {
  574. /* iOS 8 does not have this method. */
  575. if ([resource respondsToSelector:@selector(storageMode)]) {
  576. return resource.storageMode;
  577. }
  578. return MTLStorageModeShared;
  579. }
  580. static int
  581. METAL_UpdateTextureInternal(SDL_Renderer * renderer, METAL_TextureData *texturedata,
  582. id<MTLTexture> texture, SDL_Rect rect, int slice,
  583. const void * pixels, int pitch)
  584. {
  585. METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
  586. SDL_Rect stagingrect = {0, 0, rect.w, rect.h};
  587. MTLTextureDescriptor *desc;
  588. /* If the texture is managed or shared and this is the first upload, we can
  589. * use replaceRegion to upload to it directly. Otherwise we upload the data
  590. * to a staging texture and copy that over. */
  591. if (!texturedata.hasdata && METAL_GetStorageMode(texture) != MTLStorageModePrivate) {
  592. METAL_UploadTextureData(texture, rect, slice, pixels, pitch);
  593. return 0;
  594. }
  595. desc = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:texture.pixelFormat
  596. width:rect.w
  597. height:rect.h
  598. mipmapped:NO];
  599. if (desc == nil) {
  600. return SDL_OutOfMemory();
  601. }
  602. /* TODO: We could have a pool of textures or a MTLHeap we allocate from,
  603. * and release a staging texture back to the pool in the command buffer's
  604. * completion handler. */
  605. id<MTLTexture> stagingtex = [data.mtldevice newTextureWithDescriptor:desc];
  606. if (stagingtex == nil) {
  607. return SDL_OutOfMemory();
  608. }
  609. #if !__has_feature(objc_arc)
  610. [stagingtex autorelease];
  611. #endif
  612. METAL_UploadTextureData(stagingtex, stagingrect, 0, pixels, pitch);
  613. if (data.mtlcmdencoder != nil) {
  614. [data.mtlcmdencoder endEncoding];
  615. data.mtlcmdencoder = nil;
  616. }
  617. if (data.mtlcmdbuffer == nil) {
  618. data.mtlcmdbuffer = [data.mtlcmdqueue commandBuffer];
  619. }
  620. id<MTLBlitCommandEncoder> blitcmd = [data.mtlcmdbuffer blitCommandEncoder];
  621. [blitcmd copyFromTexture:stagingtex
  622. sourceSlice:0
  623. sourceLevel:0
  624. sourceOrigin:MTLOriginMake(0, 0, 0)
  625. sourceSize:MTLSizeMake(rect.w, rect.h, 1)
  626. toTexture:texture
  627. destinationSlice:slice
  628. destinationLevel:0
  629. destinationOrigin:MTLOriginMake(rect.x, rect.y, 0)];
  630. [blitcmd endEncoding];
  631. /* TODO: This isn't very efficient for the YUV formats, which call
  632. * UpdateTextureInternal multiple times in a row. */
  633. [data.mtlcmdbuffer commit];
  634. data.mtlcmdbuffer = nil;
  635. return 0;
  636. }
  637. static int
  638. METAL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
  639. const SDL_Rect * rect, const void *pixels, int pitch)
  640. { @autoreleasepool {
  641. METAL_TextureData *texturedata = (__bridge METAL_TextureData *)texture->driverdata;
  642. if (METAL_UpdateTextureInternal(renderer, texturedata, texturedata.mtltexture, *rect, 0, pixels, pitch) < 0) {
  643. return -1;
  644. }
  645. if (texturedata.yuv) {
  646. int Uslice = texture->format == SDL_PIXELFORMAT_YV12 ? 1 : 0;
  647. int Vslice = texture->format == SDL_PIXELFORMAT_YV12 ? 0 : 1;
  648. int UVpitch = (pitch + 1) / 2;
  649. SDL_Rect UVrect = {rect->x / 2, rect->y / 2, (rect->w + 1) / 2, (rect->h + 1) / 2};
  650. /* Skip to the correct offset into the next texture */
  651. pixels = (const void*)((const Uint8*)pixels + rect->h * pitch);
  652. if (METAL_UpdateTextureInternal(renderer, texturedata, texturedata.mtltexture_uv, UVrect, Uslice, pixels, UVpitch) < 0) {
  653. return -1;
  654. }
  655. /* Skip to the correct offset into the next texture */
  656. pixels = (const void*)((const Uint8*)pixels + UVrect.h * UVpitch);
  657. if (METAL_UpdateTextureInternal(renderer, texturedata, texturedata.mtltexture_uv, UVrect, Vslice, pixels, UVpitch) < 0) {
  658. return -1;
  659. }
  660. }
  661. if (texturedata.nv12) {
  662. SDL_Rect UVrect = {rect->x / 2, rect->y / 2, (rect->w + 1) / 2, (rect->h + 1) / 2};
  663. int UVpitch = 2 * ((pitch + 1) / 2);
  664. /* Skip to the correct offset into the next texture */
  665. pixels = (const void*)((const Uint8*)pixels + rect->h * pitch);
  666. if (METAL_UpdateTextureInternal(renderer, texturedata, texturedata.mtltexture_uv, UVrect, 0, pixels, UVpitch) < 0) {
  667. return -1;
  668. }
  669. }
  670. texturedata.hasdata = YES;
  671. return 0;
  672. }}
  673. static int
  674. METAL_UpdateTextureYUV(SDL_Renderer * renderer, SDL_Texture * texture,
  675. const SDL_Rect * rect,
  676. const Uint8 *Yplane, int Ypitch,
  677. const Uint8 *Uplane, int Upitch,
  678. const Uint8 *Vplane, int Vpitch)
  679. { @autoreleasepool {
  680. METAL_TextureData *texturedata = (__bridge METAL_TextureData *)texture->driverdata;
  681. const int Uslice = 0;
  682. const int Vslice = 1;
  683. SDL_Rect UVrect = {rect->x / 2, rect->y / 2, (rect->w + 1) / 2, (rect->h + 1) / 2};
  684. /* Bail out if we're supposed to update an empty rectangle */
  685. if (rect->w <= 0 || rect->h <= 0) {
  686. return 0;
  687. }
  688. if (METAL_UpdateTextureInternal(renderer, texturedata, texturedata.mtltexture, *rect, 0, Yplane, Ypitch) < 0) {
  689. return -1;
  690. }
  691. if (METAL_UpdateTextureInternal(renderer, texturedata, texturedata.mtltexture_uv, UVrect, Uslice, Uplane, Upitch)) {
  692. return -1;
  693. }
  694. if (METAL_UpdateTextureInternal(renderer, texturedata, texturedata.mtltexture_uv, UVrect, Vslice, Vplane, Vpitch)) {
  695. return -1;
  696. }
  697. texturedata.hasdata = YES;
  698. return 0;
  699. }}
  700. static int
  701. METAL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
  702. const SDL_Rect * rect, void **pixels, int *pitch)
  703. { @autoreleasepool {
  704. METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
  705. METAL_TextureData *texturedata = (__bridge METAL_TextureData *)texture->driverdata;
  706. int buffersize = 0;
  707. id<MTLBuffer> lockedbuffer = nil;
  708. if (rect->w <= 0 || rect->h <= 0) {
  709. return SDL_SetError("Invalid rectangle dimensions for LockTexture.");
  710. }
  711. *pitch = SDL_BYTESPERPIXEL(texture->format) * rect->w;
  712. if (texturedata.yuv || texturedata.nv12) {
  713. buffersize = ((*pitch) * rect->h) + (2 * (*pitch + 1) / 2) * ((rect->h + 1) / 2);
  714. } else {
  715. buffersize = (*pitch) * rect->h;
  716. }
  717. lockedbuffer = [data.mtldevice newBufferWithLength:buffersize options:MTLResourceStorageModeShared];
  718. if (lockedbuffer == nil) {
  719. return SDL_OutOfMemory();
  720. }
  721. texturedata.lockedrect = *rect;
  722. texturedata.lockedbuffer = lockedbuffer;
  723. *pixels = [lockedbuffer contents];
  724. /* METAL_TextureData.lockedbuffer retains. */
  725. #if !__has_feature(objc_arc)
  726. [lockedbuffer release];
  727. #endif
  728. return 0;
  729. }}
  730. static void
  731. METAL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
  732. { @autoreleasepool {
  733. METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
  734. METAL_TextureData *texturedata = (__bridge METAL_TextureData *)texture->driverdata;
  735. SDL_Rect rect = texturedata.lockedrect;
  736. int pitch = SDL_BYTESPERPIXEL(texture->format) * rect.w;
  737. SDL_Rect UVrect = {rect.x / 2, rect.y / 2, (rect.w + 1) / 2, (rect.h + 1) / 2};
  738. if (texturedata.lockedbuffer == nil) {
  739. return;
  740. }
  741. if (data.mtlcmdencoder != nil) {
  742. [data.mtlcmdencoder endEncoding];
  743. data.mtlcmdencoder = nil;
  744. }
  745. if (data.mtlcmdbuffer == nil) {
  746. data.mtlcmdbuffer = [data.mtlcmdqueue commandBuffer];
  747. }
  748. id<MTLBlitCommandEncoder> blitcmd = [data.mtlcmdbuffer blitCommandEncoder];
  749. [blitcmd copyFromBuffer:texturedata.lockedbuffer
  750. sourceOffset:0
  751. sourceBytesPerRow:pitch
  752. sourceBytesPerImage:0
  753. sourceSize:MTLSizeMake(rect.w, rect.h, 1)
  754. toTexture:texturedata.mtltexture
  755. destinationSlice:0
  756. destinationLevel:0
  757. destinationOrigin:MTLOriginMake(rect.x, rect.y, 0)];
  758. if (texturedata.yuv) {
  759. int Uslice = texture->format == SDL_PIXELFORMAT_YV12 ? 1 : 0;
  760. int Vslice = texture->format == SDL_PIXELFORMAT_YV12 ? 0 : 1;
  761. int UVpitch = (pitch + 1) / 2;
  762. [blitcmd copyFromBuffer:texturedata.lockedbuffer
  763. sourceOffset:rect.h * pitch
  764. sourceBytesPerRow:UVpitch
  765. sourceBytesPerImage:UVpitch * UVrect.h
  766. sourceSize:MTLSizeMake(UVrect.w, UVrect.h, 1)
  767. toTexture:texturedata.mtltexture_uv
  768. destinationSlice:Uslice
  769. destinationLevel:0
  770. destinationOrigin:MTLOriginMake(UVrect.x, UVrect.y, 0)];
  771. [blitcmd copyFromBuffer:texturedata.lockedbuffer
  772. sourceOffset:(rect.h * pitch) + UVrect.h * UVpitch
  773. sourceBytesPerRow:UVpitch
  774. sourceBytesPerImage:UVpitch * UVrect.h
  775. sourceSize:MTLSizeMake(UVrect.w, UVrect.h, 1)
  776. toTexture:texturedata.mtltexture_uv
  777. destinationSlice:Vslice
  778. destinationLevel:0
  779. destinationOrigin:MTLOriginMake(UVrect.x, UVrect.y, 0)];
  780. }
  781. if (texturedata.nv12) {
  782. int UVpitch = 2 * ((pitch + 1) / 2);
  783. [blitcmd copyFromBuffer:texturedata.lockedbuffer
  784. sourceOffset:rect.h * pitch
  785. sourceBytesPerRow:UVpitch
  786. sourceBytesPerImage:0
  787. sourceSize:MTLSizeMake(UVrect.w, UVrect.h, 1)
  788. toTexture:texturedata.mtltexture_uv
  789. destinationSlice:0
  790. destinationLevel:0
  791. destinationOrigin:MTLOriginMake(UVrect.x, UVrect.y, 0)];
  792. }
  793. [blitcmd endEncoding];
  794. [data.mtlcmdbuffer commit];
  795. data.mtlcmdbuffer = nil;
  796. texturedata.lockedbuffer = nil; /* Retained property, so it calls release. */
  797. texturedata.hasdata = YES;
  798. }}
  799. static void
  800. METAL_SetTextureScaleMode(SDL_Renderer * renderer, SDL_Texture * texture, SDL_ScaleMode scaleMode)
  801. { @autoreleasepool {
  802. METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
  803. METAL_TextureData *texturedata = (__bridge METAL_TextureData *)texture->driverdata;
  804. if (scaleMode == SDL_ScaleModeNearest) {
  805. texturedata.mtlsampler = data.mtlsamplernearest;
  806. } else {
  807. texturedata.mtlsampler = data.mtlsamplerlinear;
  808. }
  809. }}
  810. static int
  811. METAL_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture)
  812. { @autoreleasepool {
  813. METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
  814. if (data.mtlcmdencoder) {
  815. /* End encoding for the previous render target so we can set up a new
  816. * render pass for this one. */
  817. [data.mtlcmdencoder endEncoding];
  818. [data.mtlcmdbuffer commit];
  819. data.mtlcmdencoder = nil;
  820. data.mtlcmdbuffer = nil;
  821. }
  822. /* We don't begin a new render pass right away - we delay it until an actual
  823. * draw or clear happens. That way we can use hardware clears when possible,
  824. * which are only available when beginning a new render pass. */
  825. return 0;
  826. }}
  827. // normalize a value from 0.0f to len into 0.0f to 1.0f.
  828. static inline float
  829. normtex(const float _val, const float len)
  830. {
  831. return _val / len;
  832. }
  833. static int
  834. METAL_QueueSetViewport(SDL_Renderer * renderer, SDL_RenderCommand *cmd)
  835. {
  836. float projection[4][4]; /* Prepare an orthographic projection */
  837. const int w = cmd->data.viewport.rect.w;
  838. const int h = cmd->data.viewport.rect.h;
  839. const size_t matrixlen = sizeof (projection);
  840. float *matrix = (float *) SDL_AllocateRenderVertices(renderer, matrixlen, CONSTANT_ALIGN(16), &cmd->data.viewport.first);
  841. if (!matrix) {
  842. return -1;
  843. }
  844. SDL_memset(projection, '\0', matrixlen);
  845. if (w && h) {
  846. projection[0][0] = 2.0f / w;
  847. projection[1][1] = -2.0f / h;
  848. projection[3][0] = -1.0f;
  849. projection[3][1] = 1.0f;
  850. projection[3][3] = 1.0f;
  851. }
  852. SDL_memcpy(matrix, projection, matrixlen);
  853. return 0;
  854. }
  855. static int
  856. METAL_QueueSetDrawColor(SDL_Renderer *renderer, SDL_RenderCommand *cmd)
  857. {
  858. const size_t vertlen = sizeof (float) * 4;
  859. float *verts = (float *) SDL_AllocateRenderVertices(renderer, vertlen, DEVICE_ALIGN(16), &cmd->data.color.first);
  860. if (!verts) {
  861. return -1;
  862. }
  863. *(verts++) = ((float)cmd->data.color.r) / 255.0f;
  864. *(verts++) = ((float)cmd->data.color.g) / 255.0f;
  865. *(verts++) = ((float)cmd->data.color.b) / 255.0f;
  866. *(verts++) = ((float)cmd->data.color.a) / 255.0f;
  867. return 0;
  868. }
  869. static int
  870. METAL_QueueDrawPoints(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL_FPoint * points, int count)
  871. {
  872. const size_t vertlen = (sizeof (float) * 2) * count;
  873. float *verts = (float *) SDL_AllocateRenderVertices(renderer, vertlen, DEVICE_ALIGN(8), &cmd->data.draw.first);
  874. if (!verts) {
  875. return -1;
  876. }
  877. cmd->data.draw.count = count;
  878. SDL_memcpy(verts, points, vertlen);
  879. return 0;
  880. }
  881. static int
  882. METAL_QueueDrawLines(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL_FPoint * points, int count)
  883. {
  884. SDL_assert(count >= 2); /* should have been checked at the higher level. */
  885. const size_t vertlen = (sizeof (float) * 2) * count;
  886. float *verts = (float *) SDL_AllocateRenderVertices(renderer, vertlen, DEVICE_ALIGN(8), &cmd->data.draw.first);
  887. if (!verts) {
  888. return -1;
  889. }
  890. cmd->data.draw.count = count;
  891. SDL_memcpy(verts, points, vertlen);
  892. /* If the line segment is completely horizontal or vertical,
  893. make it one pixel longer, to satisfy the diamond-exit rule.
  894. We should probably do this for diagonal lines too, but we'd have to
  895. do some trigonometry to figure out the correct pixel and generally
  896. when we have problems with pixel perfection, it's for straight lines
  897. that are missing a pixel that frames something and not arbitrary
  898. angles. Maybe !!! FIXME for later, though. */
  899. points += count - 2; /* update the last line. */
  900. verts += (count * 2) - 2;
  901. const float xstart = points[0].x;
  902. const float ystart = points[0].y;
  903. const float xend = points[1].x;
  904. const float yend = points[1].y;
  905. if (ystart == yend) { /* horizontal line */
  906. verts[0] += (xend > xstart) ? 1.0f : -1.0f;
  907. } else if (xstart == xend) { /* vertical line */
  908. verts[1] += (yend > ystart) ? 1.0f : -1.0f;
  909. }
  910. return 0;
  911. }
  912. static int
  913. METAL_QueueFillRects(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL_FRect * rects, int count)
  914. {
  915. const size_t vertlen = (sizeof (float) * 8) * count;
  916. float *verts = (float *) SDL_AllocateRenderVertices(renderer, vertlen, DEVICE_ALIGN(8), &cmd->data.draw.first);
  917. if (!verts) {
  918. return -1;
  919. }
  920. cmd->data.draw.count = count;
  921. /* Quads in the following vertex order (matches the quad index buffer):
  922. * 1---3
  923. * | \ |
  924. * 0---2
  925. */
  926. for (int i = 0; i < count; i++, rects++) {
  927. if ((rects->w <= 0.0f) || (rects->h <= 0.0f)) {
  928. cmd->data.draw.count--;
  929. } else {
  930. *(verts++) = rects->x;
  931. *(verts++) = rects->y + rects->h;
  932. *(verts++) = rects->x;
  933. *(verts++) = rects->y;
  934. *(verts++) = rects->x + rects->w;
  935. *(verts++) = rects->y + rects->h;
  936. *(verts++) = rects->x + rects->w;
  937. *(verts++) = rects->y;
  938. }
  939. }
  940. if (cmd->data.draw.count == 0) {
  941. cmd->command = SDL_RENDERCMD_NO_OP; // nothing to do, just skip this one later.
  942. }
  943. return 0;
  944. }
  945. static int
  946. METAL_QueueCopy(SDL_Renderer * renderer, SDL_RenderCommand *cmd, SDL_Texture * texture,
  947. const SDL_Rect * srcrect, const SDL_FRect * dstrect)
  948. {
  949. const float texw = (float) texture->w;
  950. const float texh = (float) texture->h;
  951. // !!! FIXME: use an index buffer
  952. const size_t vertlen = (sizeof (float) * 16);
  953. float *verts = (float *) SDL_AllocateRenderVertices(renderer, vertlen, DEVICE_ALIGN(8), &cmd->data.draw.first);
  954. if (!verts) {
  955. return -1;
  956. }
  957. cmd->data.draw.count = 1;
  958. /* Interleaved positions and texture coordinates */
  959. *(verts++) = dstrect->x;
  960. *(verts++) = dstrect->y + dstrect->h;
  961. *(verts++) = normtex(srcrect->x, texw);
  962. *(verts++) = normtex(srcrect->y + srcrect->h, texh);
  963. *(verts++) = dstrect->x;
  964. *(verts++) = dstrect->y;
  965. *(verts++) = normtex(srcrect->x, texw);
  966. *(verts++) = normtex(srcrect->y, texh);
  967. *(verts++) = dstrect->x + dstrect->w;
  968. *(verts++) = dstrect->y + dstrect->h;
  969. *(verts++) = normtex(srcrect->x + srcrect->w, texw);
  970. *(verts++) = normtex(srcrect->y + srcrect->h, texh);
  971. *(verts++) = dstrect->x + dstrect->w;
  972. *(verts++) = dstrect->y;
  973. *(verts++) = normtex(srcrect->x + srcrect->w, texw);
  974. *(verts++) = normtex(srcrect->y, texh);
  975. return 0;
  976. }
  977. static int
  978. METAL_QueueCopyEx(SDL_Renderer * renderer, SDL_RenderCommand *cmd, SDL_Texture * texture,
  979. const SDL_Rect * srcquad, const SDL_FRect * dstrect,
  980. const double angle, const SDL_FPoint *center, const SDL_RendererFlip flip)
  981. {
  982. const float texw = (float) texture->w;
  983. const float texh = (float) texture->h;
  984. const float rads = (float)(M_PI * (float) angle / 180.0f);
  985. const float c = cosf(rads), s = sinf(rads);
  986. float minu, maxu, minv, maxv;
  987. const size_t vertlen = (sizeof (float) * 32);
  988. float *verts;
  989. // cheat and store this offset in (count) because it needs to be aligned in ways other fields don't and we aren't using count otherwise.
  990. verts = (float *) SDL_AllocateRenderVertices(renderer, vertlen, CONSTANT_ALIGN(16), &cmd->data.draw.count);
  991. if (!verts) {
  992. return -1;
  993. }
  994. // transform matrix
  995. SDL_memset(verts, '\0', sizeof (*verts) * 16);
  996. verts[10] = verts[15] = 1.0f;
  997. // rotation
  998. verts[0] = c;
  999. verts[1] = s;
  1000. verts[4] = -s;
  1001. verts[5] = c;
  1002. // translation
  1003. verts[12] = dstrect->x + center->x;
  1004. verts[13] = dstrect->y + center->y;
  1005. // rest of the vertices don't need the aggressive alignment. Pack them in.
  1006. verts = (float *) SDL_AllocateRenderVertices(renderer, vertlen, DEVICE_ALIGN(8), &cmd->data.draw.first);
  1007. if (!verts) {
  1008. return -1;
  1009. }
  1010. minu = normtex(srcquad->x, texw);
  1011. maxu = normtex(srcquad->x + srcquad->w, texw);
  1012. minv = normtex(srcquad->y, texh);
  1013. maxv = normtex(srcquad->y + srcquad->h, texh);
  1014. if (flip & SDL_FLIP_HORIZONTAL) {
  1015. float tmp = maxu;
  1016. maxu = minu;
  1017. minu = tmp;
  1018. }
  1019. if (flip & SDL_FLIP_VERTICAL) {
  1020. float tmp = maxv;
  1021. maxv = minv;
  1022. minv = tmp;
  1023. }
  1024. /* Interleaved positions and texture coordinates */
  1025. *(verts++) = -center->x;
  1026. *(verts++) = dstrect->h - center->y;
  1027. *(verts++) = minu;
  1028. *(verts++) = maxv;
  1029. *(verts++) = -center->x;
  1030. *(verts++) = -center->y;
  1031. *(verts++) = minu;
  1032. *(verts++) = minv;
  1033. *(verts++) = dstrect->w - center->x;
  1034. *(verts++) = dstrect->h - center->y;
  1035. *(verts++) = maxu;
  1036. *(verts++) = maxv;
  1037. *(verts++) = dstrect->w - center->x;
  1038. *(verts++) = -center->y;
  1039. *(verts++) = maxu;
  1040. *(verts++) = minv;
  1041. return 0;
  1042. }
  1043. typedef struct
  1044. {
  1045. #if __has_feature(objc_arc)
  1046. __unsafe_unretained id<MTLRenderPipelineState> pipeline;
  1047. __unsafe_unretained id<MTLBuffer> vertex_buffer;
  1048. #else
  1049. id<MTLRenderPipelineState> pipeline;
  1050. id<MTLBuffer> vertex_buffer;
  1051. #endif
  1052. size_t constants_offset;
  1053. SDL_Texture *texture;
  1054. SDL_bool cliprect_dirty;
  1055. SDL_bool cliprect_enabled;
  1056. SDL_Rect cliprect;
  1057. SDL_bool viewport_dirty;
  1058. SDL_Rect viewport;
  1059. size_t projection_offset;
  1060. SDL_bool color_dirty;
  1061. size_t color_offset;
  1062. } METAL_DrawStateCache;
  1063. static void
  1064. SetDrawState(SDL_Renderer *renderer, const SDL_RenderCommand *cmd, const SDL_MetalFragmentFunction shader,
  1065. const size_t constants_offset, id<MTLBuffer> mtlbufvertex, METAL_DrawStateCache *statecache)
  1066. {
  1067. METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
  1068. const SDL_BlendMode blend = cmd->data.draw.blend;
  1069. size_t first = cmd->data.draw.first;
  1070. id<MTLRenderPipelineState> newpipeline;
  1071. METAL_ActivateRenderCommandEncoder(renderer, MTLLoadActionLoad, NULL, statecache->vertex_buffer);
  1072. if (statecache->viewport_dirty) {
  1073. MTLViewport viewport;
  1074. viewport.originX = statecache->viewport.x;
  1075. viewport.originY = statecache->viewport.y;
  1076. viewport.width = statecache->viewport.w;
  1077. viewport.height = statecache->viewport.h;
  1078. viewport.znear = 0.0;
  1079. viewport.zfar = 1.0;
  1080. [data.mtlcmdencoder setViewport:viewport];
  1081. [data.mtlcmdencoder setVertexBuffer:mtlbufvertex offset:statecache->projection_offset atIndex:2]; // projection
  1082. statecache->viewport_dirty = SDL_FALSE;
  1083. }
  1084. if (statecache->cliprect_dirty) {
  1085. MTLScissorRect mtlrect;
  1086. if (statecache->cliprect_enabled) {
  1087. const SDL_Rect *rect = &statecache->cliprect;
  1088. mtlrect.x = statecache->viewport.x + rect->x;
  1089. mtlrect.y = statecache->viewport.y + rect->y;
  1090. mtlrect.width = rect->w;
  1091. mtlrect.height = rect->h;
  1092. } else {
  1093. mtlrect.x = statecache->viewport.x;
  1094. mtlrect.y = statecache->viewport.y;
  1095. mtlrect.width = statecache->viewport.w;
  1096. mtlrect.height = statecache->viewport.h;
  1097. }
  1098. if (mtlrect.width > 0 && mtlrect.height > 0) {
  1099. [data.mtlcmdencoder setScissorRect:mtlrect];
  1100. }
  1101. statecache->cliprect_dirty = SDL_FALSE;
  1102. }
  1103. if (statecache->color_dirty) {
  1104. [data.mtlcmdencoder setFragmentBufferOffset:statecache->color_offset atIndex:0];
  1105. statecache->color_dirty = SDL_FALSE;
  1106. }
  1107. newpipeline = ChoosePipelineState(data, data.activepipelines, shader, blend);
  1108. if (newpipeline != statecache->pipeline) {
  1109. [data.mtlcmdencoder setRenderPipelineState:newpipeline];
  1110. statecache->pipeline = newpipeline;
  1111. }
  1112. if (constants_offset != statecache->constants_offset) {
  1113. if (constants_offset != CONSTANTS_OFFSET_INVALID) {
  1114. [data.mtlcmdencoder setVertexBuffer:data.mtlbufconstants offset:constants_offset atIndex:3];
  1115. }
  1116. statecache->constants_offset = constants_offset;
  1117. }
  1118. [data.mtlcmdencoder setVertexBufferOffset:first atIndex:0]; /* position/texcoords */
  1119. }
  1120. static void
  1121. SetCopyState(SDL_Renderer *renderer, const SDL_RenderCommand *cmd, const size_t constants_offset,
  1122. id<MTLBuffer> mtlbufvertex, METAL_DrawStateCache *statecache)
  1123. {
  1124. METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
  1125. SDL_Texture *texture = cmd->data.draw.texture;
  1126. METAL_TextureData *texturedata = (__bridge METAL_TextureData *)texture->driverdata;
  1127. SetDrawState(renderer, cmd, texturedata.fragmentFunction, constants_offset, mtlbufvertex, statecache);
  1128. if (texture != statecache->texture) {
  1129. METAL_TextureData *oldtexturedata = NULL;
  1130. if (statecache->texture) {
  1131. oldtexturedata = (__bridge METAL_TextureData *) statecache->texture->driverdata;
  1132. }
  1133. if (!oldtexturedata || (texturedata.mtlsampler != oldtexturedata.mtlsampler)) {
  1134. [data.mtlcmdencoder setFragmentSamplerState:texturedata.mtlsampler atIndex:0];
  1135. }
  1136. [data.mtlcmdencoder setFragmentTexture:texturedata.mtltexture atIndex:0];
  1137. if (texturedata.yuv || texturedata.nv12) {
  1138. [data.mtlcmdencoder setFragmentTexture:texturedata.mtltexture_uv atIndex:1];
  1139. [data.mtlcmdencoder setFragmentBuffer:data.mtlbufconstants offset:texturedata.conversionBufferOffset atIndex:1];
  1140. }
  1141. statecache->texture = texture;
  1142. }
  1143. }
  1144. static int
  1145. METAL_RunCommandQueue(SDL_Renderer * renderer, SDL_RenderCommand *cmd, void *vertices, size_t vertsize)
  1146. { @autoreleasepool {
  1147. METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
  1148. METAL_DrawStateCache statecache;
  1149. SDL_zero(statecache);
  1150. id<MTLBuffer> mtlbufvertex = nil;
  1151. statecache.pipeline = nil;
  1152. statecache.vertex_buffer = nil;
  1153. statecache.constants_offset = CONSTANTS_OFFSET_INVALID;
  1154. statecache.texture = NULL;
  1155. statecache.color_dirty = SDL_TRUE;
  1156. statecache.cliprect_dirty = SDL_TRUE;
  1157. statecache.viewport_dirty = SDL_TRUE;
  1158. statecache.projection_offset = 0;
  1159. statecache.color_offset = 0;
  1160. // !!! FIXME: have a ring of pre-made MTLBuffers we cycle through? How expensive is creation?
  1161. if (vertsize > 0) {
  1162. /* We can memcpy to a shared buffer from the CPU and read it from the GPU
  1163. * without any extra copying. It's a bit slower on macOS to read shared
  1164. * data from the GPU than to read managed/private data, but we avoid the
  1165. * cost of copying the data and the code's simpler. Apple's best
  1166. * practices guide recommends this approach for streamed vertex data.
  1167. * TODO: this buffer is also used for constants. Is performance still
  1168. * good for those, or should we have a managed buffer for them? */
  1169. mtlbufvertex = [data.mtldevice newBufferWithLength:vertsize options:MTLResourceStorageModeShared];
  1170. #if !__has_feature(objc_arc)
  1171. [mtlbufvertex autorelease];
  1172. #endif
  1173. mtlbufvertex.label = @"SDL vertex data";
  1174. SDL_memcpy([mtlbufvertex contents], vertices, vertsize);
  1175. statecache.vertex_buffer = mtlbufvertex;
  1176. }
  1177. // If there's a command buffer here unexpectedly (app requested one?). Commit it so we can start fresh.
  1178. [data.mtlcmdencoder endEncoding];
  1179. [data.mtlcmdbuffer commit];
  1180. data.mtlcmdencoder = nil;
  1181. data.mtlcmdbuffer = nil;
  1182. while (cmd) {
  1183. switch (cmd->command) {
  1184. case SDL_RENDERCMD_SETVIEWPORT: {
  1185. SDL_memcpy(&statecache.viewport, &cmd->data.viewport.rect, sizeof (statecache.viewport));
  1186. statecache.projection_offset = cmd->data.viewport.first;
  1187. statecache.viewport_dirty = SDL_TRUE;
  1188. statecache.cliprect_dirty = SDL_TRUE;
  1189. break;
  1190. }
  1191. case SDL_RENDERCMD_SETCLIPRECT: {
  1192. SDL_memcpy(&statecache.cliprect, &cmd->data.cliprect.rect, sizeof (statecache.cliprect));
  1193. statecache.cliprect_enabled = cmd->data.cliprect.enabled;
  1194. statecache.cliprect_dirty = SDL_TRUE;
  1195. break;
  1196. }
  1197. case SDL_RENDERCMD_SETDRAWCOLOR: {
  1198. statecache.color_offset = cmd->data.color.first;
  1199. statecache.color_dirty = SDL_TRUE;
  1200. break;
  1201. }
  1202. case SDL_RENDERCMD_CLEAR: {
  1203. /* If we're already encoding a command buffer, dump it without committing it. We'd just
  1204. clear all its work anyhow, and starting a new encoder will let us use a hardware clear
  1205. operation via MTLLoadActionClear. */
  1206. if (data.mtlcmdencoder != nil) {
  1207. [data.mtlcmdencoder endEncoding];
  1208. // !!! FIXME: have to commit, or an uncommitted but enqueued buffer will prevent the frame from finishing.
  1209. [data.mtlcmdbuffer commit];
  1210. data.mtlcmdencoder = nil;
  1211. data.mtlcmdbuffer = nil;
  1212. }
  1213. // force all this state to be reconfigured on next command buffer.
  1214. statecache.pipeline = nil;
  1215. statecache.constants_offset = CONSTANTS_OFFSET_INVALID;
  1216. statecache.texture = NULL;
  1217. statecache.color_dirty = SDL_TRUE;
  1218. statecache.cliprect_dirty = SDL_TRUE;
  1219. statecache.viewport_dirty = SDL_TRUE;
  1220. const Uint8 r = cmd->data.color.r;
  1221. const Uint8 g = cmd->data.color.g;
  1222. const Uint8 b = cmd->data.color.b;
  1223. const Uint8 a = cmd->data.color.a;
  1224. MTLClearColor color = MTLClearColorMake(r / 255.0f, g / 255.0f, b / 255.0f, a / 255.0f);
  1225. // get new command encoder, set up with an initial clear operation.
  1226. METAL_ActivateRenderCommandEncoder(renderer, MTLLoadActionClear, &color, mtlbufvertex);
  1227. break;
  1228. }
  1229. case SDL_RENDERCMD_DRAW_POINTS:
  1230. case SDL_RENDERCMD_DRAW_LINES: {
  1231. const size_t count = cmd->data.draw.count;
  1232. const MTLPrimitiveType primtype = (cmd->command == SDL_RENDERCMD_DRAW_POINTS) ? MTLPrimitiveTypePoint : MTLPrimitiveTypeLineStrip;
  1233. SetDrawState(renderer, cmd, SDL_METAL_FRAGMENT_SOLID, CONSTANTS_OFFSET_HALF_PIXEL_TRANSFORM, mtlbufvertex, &statecache);
  1234. [data.mtlcmdencoder drawPrimitives:primtype vertexStart:0 vertexCount:count];
  1235. break;
  1236. }
  1237. case SDL_RENDERCMD_FILL_RECTS: {
  1238. const size_t count = cmd->data.draw.count;
  1239. const size_t maxcount = UINT16_MAX / 4;
  1240. SetDrawState(renderer, cmd, SDL_METAL_FRAGMENT_SOLID, CONSTANTS_OFFSET_IDENTITY, mtlbufvertex, &statecache);
  1241. if (count == 1) {
  1242. [data.mtlcmdencoder drawPrimitives:MTLPrimitiveTypeTriangleStrip vertexStart:0 vertexCount:4];
  1243. } else {
  1244. /* Our index buffer has 16 bit indices, so we can only draw
  1245. * 65k vertices (16k rects) at a time. */
  1246. for (size_t i = 0; i < count; i += maxcount) {
  1247. /* Set the vertex buffer offset for our current positions.
  1248. * The vertex buffer itself was bound in SetDrawState. */
  1249. [data.mtlcmdencoder setVertexBufferOffset:cmd->data.draw.first + i*sizeof(float)*8 atIndex:0];
  1250. [data.mtlcmdencoder drawIndexedPrimitives:MTLPrimitiveTypeTriangle
  1251. indexCount:SDL_min(maxcount, count - i) * 6
  1252. indexType:MTLIndexTypeUInt16
  1253. indexBuffer:data.mtlbufquadindices
  1254. indexBufferOffset:0];
  1255. }
  1256. }
  1257. break;
  1258. }
  1259. case SDL_RENDERCMD_COPY: {
  1260. SetCopyState(renderer, cmd, CONSTANTS_OFFSET_IDENTITY, mtlbufvertex, &statecache);
  1261. [data.mtlcmdencoder drawPrimitives:MTLPrimitiveTypeTriangleStrip vertexStart:0 vertexCount:4];
  1262. break;
  1263. }
  1264. case SDL_RENDERCMD_COPY_EX: {
  1265. SetCopyState(renderer, cmd, CONSTANTS_OFFSET_INVALID, mtlbufvertex, &statecache);
  1266. [data.mtlcmdencoder setVertexBuffer:mtlbufvertex offset:cmd->data.draw.count atIndex:3]; // transform
  1267. [data.mtlcmdencoder drawPrimitives:MTLPrimitiveTypeTriangleStrip vertexStart:0 vertexCount:4];
  1268. break;
  1269. }
  1270. case SDL_RENDERCMD_NO_OP:
  1271. break;
  1272. }
  1273. cmd = cmd->next;
  1274. }
  1275. return 0;
  1276. }}
  1277. static int
  1278. METAL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
  1279. Uint32 pixel_format, void * pixels, int pitch)
  1280. { @autoreleasepool {
  1281. METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
  1282. METAL_ActivateRenderCommandEncoder(renderer, MTLLoadActionLoad, NULL, nil);
  1283. [data.mtlcmdencoder endEncoding];
  1284. id<MTLTexture> mtltexture = data.mtlpassdesc.colorAttachments[0].texture;
  1285. #ifdef __MACOSX__
  1286. /* on macOS with managed-storage textures, we need to tell the driver to
  1287. * update the CPU-side copy of the texture data.
  1288. * NOTE: Currently all of our textures are managed on macOS. We'll need some
  1289. * extra copying for any private textures. */
  1290. if (METAL_GetStorageMode(mtltexture) == MTLStorageModeManaged) {
  1291. id<MTLBlitCommandEncoder> blit = [data.mtlcmdbuffer blitCommandEncoder];
  1292. [blit synchronizeResource:mtltexture];
  1293. [blit endEncoding];
  1294. }
  1295. #endif
  1296. /* Commit the current command buffer and wait until it's completed, to make
  1297. * sure the GPU has finished rendering to it by the time we read it. */
  1298. [data.mtlcmdbuffer commit];
  1299. [data.mtlcmdbuffer waitUntilCompleted];
  1300. data.mtlcmdencoder = nil;
  1301. data.mtlcmdbuffer = nil;
  1302. MTLRegion mtlregion = MTLRegionMake2D(rect->x, rect->y, rect->w, rect->h);
  1303. // we only do BGRA8 or RGBA8 at the moment, so 4 will do.
  1304. const int temp_pitch = rect->w * 4;
  1305. void *temp_pixels = SDL_malloc(temp_pitch * rect->h);
  1306. if (!temp_pixels) {
  1307. return SDL_OutOfMemory();
  1308. }
  1309. [mtltexture getBytes:temp_pixels bytesPerRow:temp_pitch fromRegion:mtlregion mipmapLevel:0];
  1310. const Uint32 temp_format = (mtltexture.pixelFormat == MTLPixelFormatBGRA8Unorm) ? SDL_PIXELFORMAT_ARGB8888 : SDL_PIXELFORMAT_ABGR8888;
  1311. const int status = SDL_ConvertPixels(rect->w, rect->h, temp_format, temp_pixels, temp_pitch, pixel_format, pixels, pitch);
  1312. SDL_free(temp_pixels);
  1313. return status;
  1314. }}
  1315. static void
  1316. METAL_RenderPresent(SDL_Renderer * renderer)
  1317. { @autoreleasepool {
  1318. METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
  1319. // If we don't have a command buffer, we can't present, so activate to get one.
  1320. if (data.mtlcmdencoder == nil) {
  1321. // We haven't even gotten a backbuffer yet? Clear it to black. Otherwise, load the existing data.
  1322. if (data.mtlbackbuffer == nil) {
  1323. MTLClearColor color = MTLClearColorMake(0.0f, 0.0f, 0.0f, 1.0f);
  1324. METAL_ActivateRenderCommandEncoder(renderer, MTLLoadActionClear, &color, nil);
  1325. } else {
  1326. METAL_ActivateRenderCommandEncoder(renderer, MTLLoadActionLoad, NULL, nil);
  1327. }
  1328. }
  1329. [data.mtlcmdencoder endEncoding];
  1330. [data.mtlcmdbuffer presentDrawable:data.mtlbackbuffer];
  1331. [data.mtlcmdbuffer commit];
  1332. data.mtlcmdencoder = nil;
  1333. data.mtlcmdbuffer = nil;
  1334. data.mtlbackbuffer = nil;
  1335. }}
  1336. static void
  1337. METAL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
  1338. { @autoreleasepool {
  1339. CFBridgingRelease(texture->driverdata);
  1340. texture->driverdata = NULL;
  1341. }}
  1342. static void
  1343. METAL_DestroyRenderer(SDL_Renderer * renderer)
  1344. { @autoreleasepool {
  1345. if (renderer->driverdata) {
  1346. METAL_RenderData *data = CFBridgingRelease(renderer->driverdata);
  1347. if (data.mtlcmdencoder != nil) {
  1348. [data.mtlcmdencoder endEncoding];
  1349. }
  1350. DestroyAllPipelines(data.allpipelines, data.pipelinescount);
  1351. SDL_Metal_DestroyView(data.mtlview);
  1352. }
  1353. SDL_free(renderer);
  1354. }}
  1355. static void *
  1356. METAL_GetMetalLayer(SDL_Renderer * renderer)
  1357. { @autoreleasepool {
  1358. METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
  1359. return (__bridge void*)data.mtllayer;
  1360. }}
  1361. static void *
  1362. METAL_GetMetalCommandEncoder(SDL_Renderer * renderer)
  1363. { @autoreleasepool {
  1364. METAL_ActivateRenderCommandEncoder(renderer, MTLLoadActionLoad, NULL, nil);
  1365. METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
  1366. return (__bridge void*)data.mtlcmdencoder;
  1367. }}
  1368. static SDL_Renderer *
  1369. METAL_CreateRenderer(SDL_Window * window, Uint32 flags)
  1370. { @autoreleasepool {
  1371. SDL_Renderer *renderer = NULL;
  1372. METAL_RenderData *data = NULL;
  1373. id<MTLDevice> mtldevice = nil;
  1374. SDL_MetalView view = NULL;
  1375. CAMetalLayer *layer = nil;
  1376. SDL_SysWMinfo syswm;
  1377. Uint32 window_flags;
  1378. SDL_bool changed_window = SDL_FALSE;
  1379. SDL_VERSION(&syswm.version);
  1380. if (!SDL_GetWindowWMInfo(window, &syswm)) {
  1381. return NULL;
  1382. }
  1383. if (IsMetalAvailable(&syswm) == -1) {
  1384. return NULL;
  1385. }
  1386. window_flags = SDL_GetWindowFlags(window);
  1387. if (!(window_flags & SDL_WINDOW_METAL)) {
  1388. changed_window = SDL_TRUE;
  1389. if (SDL_RecreateWindow(window, (window_flags & ~(SDL_WINDOW_VULKAN | SDL_WINDOW_OPENGL)) | SDL_WINDOW_METAL) < 0) {
  1390. return NULL;
  1391. }
  1392. }
  1393. renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
  1394. if (!renderer) {
  1395. SDL_OutOfMemory();
  1396. if (changed_window) {
  1397. SDL_RecreateWindow(window, window_flags);
  1398. }
  1399. return NULL;
  1400. }
  1401. // !!! FIXME: MTLCopyAllDevices() can find other GPUs on macOS...
  1402. mtldevice = MTLCreateSystemDefaultDevice();
  1403. if (mtldevice == nil) {
  1404. SDL_free(renderer);
  1405. SDL_SetError("Failed to obtain Metal device");
  1406. if (changed_window) {
  1407. SDL_RecreateWindow(window, window_flags);
  1408. }
  1409. return NULL;
  1410. }
  1411. view = SDL_Metal_CreateView(window);
  1412. if (view == NULL) {
  1413. #if !__has_feature(objc_arc)
  1414. [mtldevice release];
  1415. #endif
  1416. SDL_free(renderer);
  1417. if (changed_window) {
  1418. SDL_RecreateWindow(window, window_flags);
  1419. }
  1420. return NULL;
  1421. }
  1422. // !!! FIXME: error checking on all of this.
  1423. data = [[METAL_RenderData alloc] init];
  1424. if (data == nil) {
  1425. #if !__has_feature(objc_arc)
  1426. [mtldevice release];
  1427. #endif
  1428. SDL_Metal_DestroyView(view);
  1429. SDL_free(renderer);
  1430. if (changed_window) {
  1431. SDL_RecreateWindow(window, window_flags);
  1432. }
  1433. return NULL;
  1434. }
  1435. renderer->driverdata = (void*)CFBridgingRetain(data);
  1436. renderer->window = window;
  1437. data.mtlview = view;
  1438. #ifdef __MACOSX__
  1439. layer = (CAMetalLayer *)[(NSView *)view layer];
  1440. #else
  1441. layer = (CAMetalLayer *)[(__bridge UIView *)view layer];
  1442. #endif
  1443. layer.device = mtldevice;
  1444. /* Necessary for RenderReadPixels. */
  1445. layer.framebufferOnly = NO;
  1446. data.mtldevice = layer.device;
  1447. data.mtllayer = layer;
  1448. id<MTLCommandQueue> mtlcmdqueue = [data.mtldevice newCommandQueue];
  1449. data.mtlcmdqueue = mtlcmdqueue;
  1450. data.mtlcmdqueue.label = @"SDL Metal Renderer";
  1451. data.mtlpassdesc = [MTLRenderPassDescriptor renderPassDescriptor];
  1452. NSError *err = nil;
  1453. // The compiled .metallib is embedded in a static array in a header file
  1454. // but the original shader source code is in SDL_shaders_metal.metal.
  1455. dispatch_data_t mtllibdata = dispatch_data_create(sdl_metallib, sdl_metallib_len, dispatch_get_global_queue(0, 0), ^{});
  1456. id<MTLLibrary> mtllibrary = [data.mtldevice newLibraryWithData:mtllibdata error:&err];
  1457. data.mtllibrary = mtllibrary;
  1458. SDL_assert(err == nil);
  1459. #if !__has_feature(objc_arc)
  1460. dispatch_release(mtllibdata);
  1461. #endif
  1462. data.mtllibrary.label = @"SDL Metal renderer shader library";
  1463. /* Do some shader pipeline state loading up-front rather than on demand. */
  1464. data.pipelinescount = 0;
  1465. data.allpipelines = NULL;
  1466. ChooseShaderPipelines(data, MTLPixelFormatBGRA8Unorm);
  1467. MTLSamplerDescriptor *samplerdesc = [[MTLSamplerDescriptor alloc] init];
  1468. samplerdesc.minFilter = MTLSamplerMinMagFilterNearest;
  1469. samplerdesc.magFilter = MTLSamplerMinMagFilterNearest;
  1470. id<MTLSamplerState> mtlsamplernearest = [data.mtldevice newSamplerStateWithDescriptor:samplerdesc];
  1471. data.mtlsamplernearest = mtlsamplernearest;
  1472. samplerdesc.minFilter = MTLSamplerMinMagFilterLinear;
  1473. samplerdesc.magFilter = MTLSamplerMinMagFilterLinear;
  1474. id<MTLSamplerState> mtlsamplerlinear = [data.mtldevice newSamplerStateWithDescriptor:samplerdesc];
  1475. data.mtlsamplerlinear = mtlsamplerlinear;
  1476. /* Note: matrices are column major. */
  1477. float identitytransform[16] = {
  1478. 1.0f, 0.0f, 0.0f, 0.0f,
  1479. 0.0f, 1.0f, 0.0f, 0.0f,
  1480. 0.0f, 0.0f, 1.0f, 0.0f,
  1481. 0.0f, 0.0f, 0.0f, 1.0f,
  1482. };
  1483. float halfpixeltransform[16] = {
  1484. 1.0f, 0.0f, 0.0f, 0.0f,
  1485. 0.0f, 1.0f, 0.0f, 0.0f,
  1486. 0.0f, 0.0f, 1.0f, 0.0f,
  1487. 0.5f, 0.5f, 0.0f, 1.0f,
  1488. };
  1489. /* Metal pads float3s to 16 bytes. */
  1490. float decodetransformJPEG[4*4] = {
  1491. 0.0, -0.501960814, -0.501960814, 0.0, /* offset */
  1492. 1.0000, 0.0000, 1.4020, 0.0, /* Rcoeff */
  1493. 1.0000, -0.3441, -0.7141, 0.0, /* Gcoeff */
  1494. 1.0000, 1.7720, 0.0000, 0.0, /* Bcoeff */
  1495. };
  1496. float decodetransformBT601[4*4] = {
  1497. -0.0627451017, -0.501960814, -0.501960814, 0.0, /* offset */
  1498. 1.1644, 0.0000, 1.5960, 0.0, /* Rcoeff */
  1499. 1.1644, -0.3918, -0.8130, 0.0, /* Gcoeff */
  1500. 1.1644, 2.0172, 0.0000, 0.0, /* Bcoeff */
  1501. };
  1502. float decodetransformBT709[4*4] = {
  1503. 0.0, -0.501960814, -0.501960814, 0.0, /* offset */
  1504. 1.0000, 0.0000, 1.4020, 0.0, /* Rcoeff */
  1505. 1.0000, -0.3441, -0.7141, 0.0, /* Gcoeff */
  1506. 1.0000, 1.7720, 0.0000, 0.0, /* Bcoeff */
  1507. };
  1508. id<MTLBuffer> mtlbufconstantstaging = [data.mtldevice newBufferWithLength:CONSTANTS_LENGTH options:MTLResourceStorageModeShared];
  1509. #if !__has_feature(objc_arc)
  1510. [mtlbufconstantstaging autorelease];
  1511. #endif
  1512. char *constantdata = [mtlbufconstantstaging contents];
  1513. SDL_memcpy(constantdata + CONSTANTS_OFFSET_IDENTITY, identitytransform, sizeof(identitytransform));
  1514. SDL_memcpy(constantdata + CONSTANTS_OFFSET_HALF_PIXEL_TRANSFORM, halfpixeltransform, sizeof(halfpixeltransform));
  1515. SDL_memcpy(constantdata + CONSTANTS_OFFSET_DECODE_JPEG, decodetransformJPEG, sizeof(decodetransformJPEG));
  1516. SDL_memcpy(constantdata + CONSTANTS_OFFSET_DECODE_BT601, decodetransformBT601, sizeof(decodetransformBT601));
  1517. SDL_memcpy(constantdata + CONSTANTS_OFFSET_DECODE_BT709, decodetransformBT709, sizeof(decodetransformBT709));
  1518. int quadcount = UINT16_MAX / 4;
  1519. size_t indicessize = sizeof(UInt16) * quadcount * 6;
  1520. id<MTLBuffer> mtlbufquadindicesstaging = [data.mtldevice newBufferWithLength:indicessize options:MTLResourceStorageModeShared];
  1521. #if !__has_feature(objc_arc)
  1522. [mtlbufquadindicesstaging autorelease];
  1523. #endif
  1524. /* Quads in the following vertex order (matches the FillRects vertices):
  1525. * 1---3
  1526. * | \ |
  1527. * 0---2
  1528. */
  1529. UInt16 *indexdata = [mtlbufquadindicesstaging contents];
  1530. for (int i = 0; i < quadcount; i++) {
  1531. indexdata[i * 6 + 0] = i * 4 + 0;
  1532. indexdata[i * 6 + 1] = i * 4 + 1;
  1533. indexdata[i * 6 + 2] = i * 4 + 2;
  1534. indexdata[i * 6 + 3] = i * 4 + 2;
  1535. indexdata[i * 6 + 4] = i * 4 + 1;
  1536. indexdata[i * 6 + 5] = i * 4 + 3;
  1537. }
  1538. id<MTLBuffer> mtlbufconstants = [data.mtldevice newBufferWithLength:CONSTANTS_LENGTH options:MTLResourceStorageModePrivate];
  1539. data.mtlbufconstants = mtlbufconstants;
  1540. data.mtlbufconstants.label = @"SDL constant data";
  1541. id<MTLBuffer> mtlbufquadindices = [data.mtldevice newBufferWithLength:indicessize options:MTLResourceStorageModePrivate];
  1542. data.mtlbufquadindices = mtlbufquadindices;
  1543. data.mtlbufquadindices.label = @"SDL quad index buffer";
  1544. id<MTLCommandBuffer> cmdbuffer = [data.mtlcmdqueue commandBuffer];
  1545. id<MTLBlitCommandEncoder> blitcmd = [cmdbuffer blitCommandEncoder];
  1546. [blitcmd copyFromBuffer:mtlbufconstantstaging sourceOffset:0 toBuffer:mtlbufconstants destinationOffset:0 size:CONSTANTS_LENGTH];
  1547. [blitcmd copyFromBuffer:mtlbufquadindicesstaging sourceOffset:0 toBuffer:mtlbufquadindices destinationOffset:0 size:indicessize];
  1548. [blitcmd endEncoding];
  1549. [cmdbuffer commit];
  1550. // !!! FIXME: force more clears here so all the drawables are sane to start, and our static buffers are definitely flushed.
  1551. renderer->WindowEvent = METAL_WindowEvent;
  1552. renderer->GetOutputSize = METAL_GetOutputSize;
  1553. renderer->SupportsBlendMode = METAL_SupportsBlendMode;
  1554. renderer->CreateTexture = METAL_CreateTexture;
  1555. renderer->UpdateTexture = METAL_UpdateTexture;
  1556. renderer->UpdateTextureYUV = METAL_UpdateTextureYUV;
  1557. renderer->LockTexture = METAL_LockTexture;
  1558. renderer->UnlockTexture = METAL_UnlockTexture;
  1559. renderer->SetTextureScaleMode = METAL_SetTextureScaleMode;
  1560. renderer->SetRenderTarget = METAL_SetRenderTarget;
  1561. renderer->QueueSetViewport = METAL_QueueSetViewport;
  1562. renderer->QueueSetDrawColor = METAL_QueueSetDrawColor;
  1563. renderer->QueueDrawPoints = METAL_QueueDrawPoints;
  1564. renderer->QueueDrawLines = METAL_QueueDrawLines;
  1565. renderer->QueueFillRects = METAL_QueueFillRects;
  1566. renderer->QueueCopy = METAL_QueueCopy;
  1567. renderer->QueueCopyEx = METAL_QueueCopyEx;
  1568. renderer->RunCommandQueue = METAL_RunCommandQueue;
  1569. renderer->RenderReadPixels = METAL_RenderReadPixels;
  1570. renderer->RenderPresent = METAL_RenderPresent;
  1571. renderer->DestroyTexture = METAL_DestroyTexture;
  1572. renderer->DestroyRenderer = METAL_DestroyRenderer;
  1573. renderer->GetMetalLayer = METAL_GetMetalLayer;
  1574. renderer->GetMetalCommandEncoder = METAL_GetMetalCommandEncoder;
  1575. renderer->info = METAL_RenderDriver.info;
  1576. renderer->info.flags = (SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE);
  1577. renderer->always_batch = SDL_TRUE;
  1578. #if defined(__MACOSX__) && defined(MAC_OS_X_VERSION_10_13)
  1579. if (@available(macOS 10.13, *)) {
  1580. data.mtllayer.displaySyncEnabled = (flags & SDL_RENDERER_PRESENTVSYNC) != 0;
  1581. if (data.mtllayer.displaySyncEnabled) {
  1582. renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
  1583. }
  1584. } else
  1585. #endif
  1586. {
  1587. renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
  1588. }
  1589. /* https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf */
  1590. int maxtexsize = 4096;
  1591. #if defined(__MACOSX__)
  1592. maxtexsize = 16384;
  1593. #elif defined(__TVOS__)
  1594. maxtexsize = 8192;
  1595. #ifdef __TVOS_11_0
  1596. if (@available(tvOS 11.0, *)) {
  1597. if ([mtldevice supportsFeatureSet:MTLFeatureSet_tvOS_GPUFamily2_v1]) {
  1598. maxtexsize = 16384;
  1599. }
  1600. }
  1601. #endif
  1602. #else
  1603. #ifdef __IPHONE_11_0
  1604. #pragma clang diagnostic push
  1605. #pragma clang diagnostic ignored "-Wunguarded-availability-new"
  1606. if ([mtldevice supportsFeatureSet:MTLFeatureSet_iOS_GPUFamily4_v1]) {
  1607. maxtexsize = 16384;
  1608. } else
  1609. #pragma clang diagnostic pop
  1610. #endif
  1611. #ifdef __IPHONE_10_0
  1612. if ([mtldevice supportsFeatureSet:MTLFeatureSet_iOS_GPUFamily3_v1]) {
  1613. maxtexsize = 16384;
  1614. } else
  1615. #endif
  1616. if ([mtldevice supportsFeatureSet:MTLFeatureSet_iOS_GPUFamily2_v2] || [mtldevice supportsFeatureSet:MTLFeatureSet_iOS_GPUFamily1_v2]) {
  1617. maxtexsize = 8192;
  1618. } else {
  1619. maxtexsize = 4096;
  1620. }
  1621. #endif
  1622. renderer->info.max_texture_width = maxtexsize;
  1623. renderer->info.max_texture_height = maxtexsize;
  1624. #if !__has_feature(objc_arc)
  1625. [mtlcmdqueue release];
  1626. [mtllibrary release];
  1627. [samplerdesc release];
  1628. [mtlsamplernearest release];
  1629. [mtlsamplerlinear release];
  1630. [mtlbufconstants release];
  1631. [mtlbufquadindices release];
  1632. [data release];
  1633. [mtldevice release];
  1634. #endif
  1635. return renderer;
  1636. }}
  1637. SDL_RenderDriver METAL_RenderDriver = {
  1638. METAL_CreateRenderer,
  1639. {
  1640. "metal",
  1641. (SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE),
  1642. 6,
  1643. {
  1644. SDL_PIXELFORMAT_ARGB8888,
  1645. SDL_PIXELFORMAT_ABGR8888,
  1646. SDL_PIXELFORMAT_YV12,
  1647. SDL_PIXELFORMAT_IYUV,
  1648. SDL_PIXELFORMAT_NV12,
  1649. SDL_PIXELFORMAT_NV21
  1650. },
  1651. 0, 0,
  1652. }
  1653. };
  1654. #endif /* SDL_VIDEO_RENDER_METAL && !SDL_RENDER_DISABLED */
  1655. /* vi: set ts=4 sw=4 expandtab: */