SDL_render_metal.m 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 Sam Lantinga <[email protected]>
  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. #ifdef SDL_VIDEO_RENDER_METAL
  20. #include "../SDL_sysrender.h"
  21. #include "../../video/SDL_pixels_c.h"
  22. #import <CoreVideo/CoreVideo.h>
  23. #import <Metal/Metal.h>
  24. #import <QuartzCore/CAMetalLayer.h>
  25. #ifdef SDL_VIDEO_DRIVER_COCOA
  26. #import <AppKit/NSWindow.h>
  27. #import <AppKit/NSView.h>
  28. #endif
  29. #ifdef SDL_VIDEO_DRIVER_UIKIT
  30. #import <UIKit/UIKit.h>
  31. #endif
  32. // Regenerate these with build-metal-shaders.sh
  33. #ifdef SDL_PLATFORM_MACOS
  34. #include "SDL_shaders_metal_macos.h"
  35. #elif defined(SDL_PLATFORM_TVOS)
  36. #if TARGET_OS_SIMULATOR
  37. #include "SDL_shaders_metal_tvsimulator.h"
  38. #else
  39. #include "SDL_shaders_metal_tvos.h"
  40. #endif
  41. #else
  42. #if TARGET_OS_SIMULATOR
  43. #include "SDL_shaders_metal_iphonesimulator.h"
  44. #else
  45. #include "SDL_shaders_metal_ios.h"
  46. #endif
  47. #endif
  48. // Apple Metal renderer implementation
  49. // macOS requires constants in a buffer to have a 256 byte alignment.
  50. // Use native type alignments from https://developer.apple.com/metal/Metal-Shading-Language-Specification.pdf
  51. #if defined(SDL_PLATFORM_MACOS) || TARGET_OS_SIMULATOR || TARGET_OS_MACCATALYST
  52. #define CONSTANT_ALIGN(x) (256)
  53. #else
  54. #define CONSTANT_ALIGN(x) (x < 4 ? 4 : x)
  55. #endif
  56. #define DEVICE_ALIGN(x) (x < 4 ? 4 : x)
  57. #define ALIGN_CONSTANTS(align, size) ((size + CONSTANT_ALIGN(align) - 1) & (~(CONSTANT_ALIGN(align) - 1)))
  58. static const size_t CONSTANTS_OFFSET_INVALID = 0xFFFFFFFF;
  59. static const size_t CONSTANTS_OFFSET_IDENTITY = 0;
  60. static const size_t CONSTANTS_OFFSET_HALF_PIXEL_TRANSFORM = ALIGN_CONSTANTS(16, CONSTANTS_OFFSET_IDENTITY + sizeof(float) * 16);
  61. static const size_t CONSTANTS_OFFSET_DECODE_BT601_LIMITED = ALIGN_CONSTANTS(16, CONSTANTS_OFFSET_HALF_PIXEL_TRANSFORM + sizeof(float) * 16);
  62. static const size_t CONSTANTS_OFFSET_DECODE_BT601_FULL = ALIGN_CONSTANTS(16, CONSTANTS_OFFSET_DECODE_BT601_LIMITED + sizeof(float) * 4 * 4);
  63. static const size_t CONSTANTS_OFFSET_DECODE_BT709_LIMITED = ALIGN_CONSTANTS(16, CONSTANTS_OFFSET_DECODE_BT601_FULL + sizeof(float) * 4 * 4);
  64. static const size_t CONSTANTS_OFFSET_DECODE_BT709_FULL = ALIGN_CONSTANTS(16, CONSTANTS_OFFSET_DECODE_BT709_LIMITED + sizeof(float) * 4 * 4);
  65. static const size_t CONSTANTS_OFFSET_DECODE_BT2020_LIMITED = ALIGN_CONSTANTS(16, CONSTANTS_OFFSET_DECODE_BT709_FULL + sizeof(float) * 4 * 4);
  66. static const size_t CONSTANTS_OFFSET_DECODE_BT2020_FULL = ALIGN_CONSTANTS(16, CONSTANTS_OFFSET_DECODE_BT2020_LIMITED + sizeof(float) * 4 * 4);
  67. static const size_t CONSTANTS_LENGTH = CONSTANTS_OFFSET_DECODE_BT2020_FULL + sizeof(float) * 4 * 4;
  68. // Sampler types
  69. typedef enum
  70. {
  71. SDL_METAL_SAMPLER_NEAREST_CLAMP,
  72. SDL_METAL_SAMPLER_NEAREST_WRAP,
  73. SDL_METAL_SAMPLER_LINEAR_CLAMP,
  74. SDL_METAL_SAMPLER_LINEAR_WRAP,
  75. SDL_NUM_METAL_SAMPLERS
  76. } SDL_METAL_sampler_type;
  77. typedef enum SDL_MetalVertexFunction
  78. {
  79. SDL_METAL_VERTEX_SOLID,
  80. SDL_METAL_VERTEX_COPY,
  81. } SDL_MetalVertexFunction;
  82. typedef enum SDL_MetalFragmentFunction
  83. {
  84. SDL_METAL_FRAGMENT_SOLID = 0,
  85. SDL_METAL_FRAGMENT_COPY,
  86. SDL_METAL_FRAGMENT_YUV,
  87. SDL_METAL_FRAGMENT_NV12,
  88. SDL_METAL_FRAGMENT_COUNT,
  89. } SDL_MetalFragmentFunction;
  90. typedef struct METAL_PipelineState
  91. {
  92. SDL_BlendMode blendMode;
  93. void *pipe;
  94. } METAL_PipelineState;
  95. typedef struct METAL_PipelineCache
  96. {
  97. METAL_PipelineState *states;
  98. int count;
  99. SDL_MetalVertexFunction vertexFunction;
  100. SDL_MetalFragmentFunction fragmentFunction;
  101. MTLPixelFormat renderTargetFormat;
  102. const char *label;
  103. } METAL_PipelineCache;
  104. /* Each shader combination used by drawing functions has a separate pipeline
  105. * cache, and we have a separate list of caches for each render target pixel
  106. * format. This is more efficient than iterating over a global cache to find
  107. * the pipeline based on the specified shader combination and RT pixel format,
  108. * since we know what the RT pixel format is when we set the render target, and
  109. * we know what the shader combination is inside each drawing function's code. */
  110. typedef struct METAL_ShaderPipelines
  111. {
  112. MTLPixelFormat renderTargetFormat;
  113. METAL_PipelineCache caches[SDL_METAL_FRAGMENT_COUNT];
  114. } METAL_ShaderPipelines;
  115. @interface SDL3METAL_RenderData : NSObject
  116. @property(nonatomic, retain) id<MTLDevice> mtldevice;
  117. @property(nonatomic, retain) id<MTLCommandQueue> mtlcmdqueue;
  118. @property(nonatomic, retain) id<MTLCommandBuffer> mtlcmdbuffer;
  119. @property(nonatomic, retain) id<MTLRenderCommandEncoder> mtlcmdencoder;
  120. @property(nonatomic, retain) id<MTLLibrary> mtllibrary;
  121. @property(nonatomic, retain) id<CAMetalDrawable> mtlbackbuffer;
  122. @property(nonatomic, retain) NSMutableArray<id<MTLSamplerState>> *mtlsamplers;
  123. @property(nonatomic, retain) id<MTLBuffer> mtlbufconstants;
  124. @property(nonatomic, retain) id<MTLBuffer> mtlbufquadindices;
  125. @property(nonatomic, assign) SDL_MetalView mtlview;
  126. @property(nonatomic, retain) CAMetalLayer *mtllayer;
  127. @property(nonatomic, retain) MTLRenderPassDescriptor *mtlpassdesc;
  128. @property(nonatomic, assign) METAL_ShaderPipelines *activepipelines;
  129. @property(nonatomic, assign) METAL_ShaderPipelines *allpipelines;
  130. @property(nonatomic, assign) int pipelinescount;
  131. @end
  132. @implementation SDL3METAL_RenderData
  133. @end
  134. @interface SDL3METAL_TextureData : NSObject
  135. @property(nonatomic, retain) id<MTLTexture> mtltexture;
  136. @property(nonatomic, retain) id<MTLTexture> mtltextureUv;
  137. @property(nonatomic, assign) SDL_MetalFragmentFunction fragmentFunction;
  138. #ifdef SDL_HAVE_YUV
  139. @property(nonatomic, assign) BOOL yuv;
  140. @property(nonatomic, assign) BOOL nv12;
  141. @property(nonatomic, assign) size_t conversionBufferOffset;
  142. #endif
  143. @property(nonatomic, assign) BOOL hasdata;
  144. @property(nonatomic, retain) id<MTLBuffer> lockedbuffer;
  145. @property(nonatomic, assign) SDL_Rect lockedrect;
  146. @end
  147. @implementation SDL3METAL_TextureData
  148. @end
  149. static const MTLBlendOperation invalidBlendOperation = (MTLBlendOperation)0xFFFFFFFF;
  150. static const MTLBlendFactor invalidBlendFactor = (MTLBlendFactor)0xFFFFFFFF;
  151. static MTLBlendOperation GetBlendOperation(SDL_BlendOperation operation)
  152. {
  153. switch (operation) {
  154. case SDL_BLENDOPERATION_ADD:
  155. return MTLBlendOperationAdd;
  156. case SDL_BLENDOPERATION_SUBTRACT:
  157. return MTLBlendOperationSubtract;
  158. case SDL_BLENDOPERATION_REV_SUBTRACT:
  159. return MTLBlendOperationReverseSubtract;
  160. case SDL_BLENDOPERATION_MINIMUM:
  161. return MTLBlendOperationMin;
  162. case SDL_BLENDOPERATION_MAXIMUM:
  163. return MTLBlendOperationMax;
  164. default:
  165. return invalidBlendOperation;
  166. }
  167. }
  168. static MTLBlendFactor GetBlendFactor(SDL_BlendFactor factor)
  169. {
  170. switch (factor) {
  171. case SDL_BLENDFACTOR_ZERO:
  172. return MTLBlendFactorZero;
  173. case SDL_BLENDFACTOR_ONE:
  174. return MTLBlendFactorOne;
  175. case SDL_BLENDFACTOR_SRC_COLOR:
  176. return MTLBlendFactorSourceColor;
  177. case SDL_BLENDFACTOR_ONE_MINUS_SRC_COLOR:
  178. return MTLBlendFactorOneMinusSourceColor;
  179. case SDL_BLENDFACTOR_SRC_ALPHA:
  180. return MTLBlendFactorSourceAlpha;
  181. case SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA:
  182. return MTLBlendFactorOneMinusSourceAlpha;
  183. case SDL_BLENDFACTOR_DST_COLOR:
  184. return MTLBlendFactorDestinationColor;
  185. case SDL_BLENDFACTOR_ONE_MINUS_DST_COLOR:
  186. return MTLBlendFactorOneMinusDestinationColor;
  187. case SDL_BLENDFACTOR_DST_ALPHA:
  188. return MTLBlendFactorDestinationAlpha;
  189. case SDL_BLENDFACTOR_ONE_MINUS_DST_ALPHA:
  190. return MTLBlendFactorOneMinusDestinationAlpha;
  191. default:
  192. return invalidBlendFactor;
  193. }
  194. }
  195. static NSString *GetVertexFunctionName(SDL_MetalVertexFunction function)
  196. {
  197. switch (function) {
  198. case SDL_METAL_VERTEX_SOLID:
  199. return @"SDL_Solid_vertex";
  200. case SDL_METAL_VERTEX_COPY:
  201. return @"SDL_Copy_vertex";
  202. default:
  203. return nil;
  204. }
  205. }
  206. static NSString *GetFragmentFunctionName(SDL_MetalFragmentFunction function)
  207. {
  208. switch (function) {
  209. case SDL_METAL_FRAGMENT_SOLID:
  210. return @"SDL_Solid_fragment";
  211. case SDL_METAL_FRAGMENT_COPY:
  212. return @"SDL_Copy_fragment";
  213. case SDL_METAL_FRAGMENT_YUV:
  214. return @"SDL_YUV_fragment";
  215. case SDL_METAL_FRAGMENT_NV12:
  216. return @"SDL_NV12_fragment";
  217. default:
  218. return nil;
  219. }
  220. }
  221. static id<MTLRenderPipelineState> MakePipelineState(SDL3METAL_RenderData *data, METAL_PipelineCache *cache,
  222. NSString *blendlabel, SDL_BlendMode blendmode)
  223. {
  224. MTLRenderPipelineDescriptor *mtlpipedesc;
  225. MTLVertexDescriptor *vertdesc;
  226. MTLRenderPipelineColorAttachmentDescriptor *rtdesc;
  227. NSError *err = nil;
  228. id<MTLRenderPipelineState> state;
  229. METAL_PipelineState pipeline;
  230. METAL_PipelineState *states;
  231. id<MTLFunction> mtlvertfn = [data.mtllibrary newFunctionWithName:GetVertexFunctionName(cache->vertexFunction)];
  232. id<MTLFunction> mtlfragfn = [data.mtllibrary newFunctionWithName:GetFragmentFunctionName(cache->fragmentFunction)];
  233. SDL_assert(mtlvertfn != nil);
  234. SDL_assert(mtlfragfn != nil);
  235. mtlpipedesc = [[MTLRenderPipelineDescriptor alloc] init];
  236. mtlpipedesc.vertexFunction = mtlvertfn;
  237. mtlpipedesc.fragmentFunction = mtlfragfn;
  238. vertdesc = [MTLVertexDescriptor vertexDescriptor];
  239. switch (cache->vertexFunction) {
  240. case SDL_METAL_VERTEX_SOLID:
  241. // position (float2), color (float4)
  242. vertdesc.layouts[0].stride = sizeof(float) * 2 + sizeof(float) * 4;
  243. vertdesc.layouts[0].stepFunction = MTLVertexStepFunctionPerVertex;
  244. vertdesc.attributes[0].format = MTLVertexFormatFloat2;
  245. vertdesc.attributes[0].offset = 0;
  246. vertdesc.attributes[0].bufferIndex = 0;
  247. vertdesc.attributes[1].format = MTLVertexFormatFloat4;
  248. vertdesc.attributes[1].offset = sizeof(float) * 2;
  249. vertdesc.attributes[1].bufferIndex = 0;
  250. break;
  251. case SDL_METAL_VERTEX_COPY:
  252. // position (float2), color (float4), texcoord (float2)
  253. vertdesc.layouts[0].stride = sizeof(float) * 2 + sizeof(float) * 4 + sizeof(float) * 2;
  254. vertdesc.layouts[0].stepFunction = MTLVertexStepFunctionPerVertex;
  255. vertdesc.attributes[0].format = MTLVertexFormatFloat2;
  256. vertdesc.attributes[0].offset = 0;
  257. vertdesc.attributes[0].bufferIndex = 0;
  258. vertdesc.attributes[1].format = MTLVertexFormatFloat4;
  259. vertdesc.attributes[1].offset = sizeof(float) * 2;
  260. vertdesc.attributes[1].bufferIndex = 0;
  261. vertdesc.attributes[2].format = MTLVertexFormatFloat2;
  262. vertdesc.attributes[2].offset = sizeof(float) * 2 + sizeof(float) * 4;
  263. vertdesc.attributes[2].bufferIndex = 0;
  264. break;
  265. }
  266. mtlpipedesc.vertexDescriptor = vertdesc;
  267. rtdesc = mtlpipedesc.colorAttachments[0];
  268. rtdesc.pixelFormat = cache->renderTargetFormat;
  269. if (blendmode != SDL_BLENDMODE_NONE) {
  270. rtdesc.blendingEnabled = YES;
  271. rtdesc.sourceRGBBlendFactor = GetBlendFactor(SDL_GetBlendModeSrcColorFactor(blendmode));
  272. rtdesc.destinationRGBBlendFactor = GetBlendFactor(SDL_GetBlendModeDstColorFactor(blendmode));
  273. rtdesc.rgbBlendOperation = GetBlendOperation(SDL_GetBlendModeColorOperation(blendmode));
  274. rtdesc.sourceAlphaBlendFactor = GetBlendFactor(SDL_GetBlendModeSrcAlphaFactor(blendmode));
  275. rtdesc.destinationAlphaBlendFactor = GetBlendFactor(SDL_GetBlendModeDstAlphaFactor(blendmode));
  276. rtdesc.alphaBlendOperation = GetBlendOperation(SDL_GetBlendModeAlphaOperation(blendmode));
  277. } else {
  278. rtdesc.blendingEnabled = NO;
  279. }
  280. mtlpipedesc.label = [@(cache->label) stringByAppendingString:blendlabel];
  281. state = [data.mtldevice newRenderPipelineStateWithDescriptor:mtlpipedesc error:&err];
  282. SDL_assert(err == nil);
  283. pipeline.blendMode = blendmode;
  284. pipeline.pipe = (void *)CFBridgingRetain(state);
  285. states = SDL_realloc(cache->states, (cache->count + 1) * sizeof(pipeline));
  286. if (states) {
  287. states[cache->count++] = pipeline;
  288. cache->states = states;
  289. return (__bridge id<MTLRenderPipelineState>)pipeline.pipe;
  290. } else {
  291. CFBridgingRelease(pipeline.pipe);
  292. return NULL;
  293. }
  294. }
  295. static void MakePipelineCache(SDL3METAL_RenderData *data, METAL_PipelineCache *cache, const char *label,
  296. MTLPixelFormat rtformat, SDL_MetalVertexFunction vertfn, SDL_MetalFragmentFunction fragfn)
  297. {
  298. SDL_zerop(cache);
  299. cache->vertexFunction = vertfn;
  300. cache->fragmentFunction = fragfn;
  301. cache->renderTargetFormat = rtformat;
  302. cache->label = label;
  303. /* Create pipeline states for the default blend modes. Custom blend modes
  304. * will be added to the cache on-demand. */
  305. MakePipelineState(data, cache, @" (blend=none)", SDL_BLENDMODE_NONE);
  306. MakePipelineState(data, cache, @" (blend=blend)", SDL_BLENDMODE_BLEND);
  307. MakePipelineState(data, cache, @" (blend=add)", SDL_BLENDMODE_ADD);
  308. MakePipelineState(data, cache, @" (blend=mod)", SDL_BLENDMODE_MOD);
  309. MakePipelineState(data, cache, @" (blend=mul)", SDL_BLENDMODE_MUL);
  310. }
  311. static void DestroyPipelineCache(METAL_PipelineCache *cache)
  312. {
  313. if (cache != NULL) {
  314. for (int i = 0; i < cache->count; i++) {
  315. CFBridgingRelease(cache->states[i].pipe);
  316. }
  317. SDL_free(cache->states);
  318. }
  319. }
  320. void MakeShaderPipelines(SDL3METAL_RenderData *data, METAL_ShaderPipelines *pipelines, MTLPixelFormat rtformat)
  321. {
  322. SDL_zerop(pipelines);
  323. pipelines->renderTargetFormat = rtformat;
  324. MakePipelineCache(data, &pipelines->caches[SDL_METAL_FRAGMENT_SOLID], "SDL primitives pipeline", rtformat, SDL_METAL_VERTEX_SOLID, SDL_METAL_FRAGMENT_SOLID);
  325. MakePipelineCache(data, &pipelines->caches[SDL_METAL_FRAGMENT_COPY], "SDL copy pipeline", rtformat, SDL_METAL_VERTEX_COPY, SDL_METAL_FRAGMENT_COPY);
  326. MakePipelineCache(data, &pipelines->caches[SDL_METAL_FRAGMENT_YUV], "SDL YUV pipeline", rtformat, SDL_METAL_VERTEX_COPY, SDL_METAL_FRAGMENT_YUV);
  327. MakePipelineCache(data, &pipelines->caches[SDL_METAL_FRAGMENT_NV12], "SDL NV12 pipeline", rtformat, SDL_METAL_VERTEX_COPY, SDL_METAL_FRAGMENT_NV12);
  328. }
  329. static METAL_ShaderPipelines *ChooseShaderPipelines(SDL3METAL_RenderData *data, MTLPixelFormat rtformat)
  330. {
  331. METAL_ShaderPipelines *allpipelines = data.allpipelines;
  332. int count = data.pipelinescount;
  333. for (int i = 0; i < count; i++) {
  334. if (allpipelines[i].renderTargetFormat == rtformat) {
  335. return &allpipelines[i];
  336. }
  337. }
  338. allpipelines = SDL_realloc(allpipelines, (count + 1) * sizeof(METAL_ShaderPipelines));
  339. if (allpipelines == NULL) {
  340. return NULL;
  341. }
  342. MakeShaderPipelines(data, &allpipelines[count], rtformat);
  343. data.allpipelines = allpipelines;
  344. data.pipelinescount = count + 1;
  345. return &data.allpipelines[count];
  346. }
  347. static void DestroyAllPipelines(METAL_ShaderPipelines *allpipelines, int count)
  348. {
  349. if (allpipelines != NULL) {
  350. for (int i = 0; i < count; i++) {
  351. for (int cache = 0; cache < SDL_METAL_FRAGMENT_COUNT; cache++) {
  352. DestroyPipelineCache(&allpipelines[i].caches[cache]);
  353. }
  354. }
  355. SDL_free(allpipelines);
  356. }
  357. }
  358. static inline id<MTLRenderPipelineState> ChoosePipelineState(SDL3METAL_RenderData *data, METAL_ShaderPipelines *pipelines, SDL_MetalFragmentFunction fragfn, SDL_BlendMode blendmode)
  359. {
  360. METAL_PipelineCache *cache = &pipelines->caches[fragfn];
  361. for (int i = 0; i < cache->count; i++) {
  362. if (cache->states[i].blendMode == blendmode) {
  363. return (__bridge id<MTLRenderPipelineState>)cache->states[i].pipe;
  364. }
  365. }
  366. return MakePipelineState(data, cache, [NSString stringWithFormat:@" (blend=custom 0x%x)", blendmode], blendmode);
  367. }
  368. static bool METAL_ActivateRenderCommandEncoder(SDL_Renderer *renderer, MTLLoadAction load, MTLClearColor *clear_color, id<MTLBuffer> vertex_buffer)
  369. {
  370. SDL3METAL_RenderData *data = (__bridge SDL3METAL_RenderData *)renderer->internal;
  371. /* Our SetRenderTarget just signals that the next render operation should
  372. * set up a new render pass. This is where that work happens. */
  373. if (data.mtlcmdencoder == nil) {
  374. id<MTLTexture> mtltexture = nil;
  375. if (renderer->target != NULL) {
  376. SDL3METAL_TextureData *texdata = (__bridge SDL3METAL_TextureData *)renderer->target->internal;
  377. mtltexture = texdata.mtltexture;
  378. } else {
  379. if (data.mtlbackbuffer == nil) {
  380. /* The backbuffer's contents aren't guaranteed to persist after
  381. * presenting, so we can leave it undefined when loading it. */
  382. data.mtlbackbuffer = [data.mtllayer nextDrawable];
  383. if (load == MTLLoadActionLoad) {
  384. load = MTLLoadActionDontCare;
  385. }
  386. }
  387. if (data.mtlbackbuffer != nil) {
  388. mtltexture = data.mtlbackbuffer.texture;
  389. }
  390. }
  391. /* mtltexture can be nil here if macOS refused to give us a drawable,
  392. which apparently can happen for minimized windows, etc. */
  393. if (mtltexture == nil) {
  394. return false;
  395. }
  396. if (load == MTLLoadActionClear) {
  397. SDL_assert(clear_color != NULL);
  398. data.mtlpassdesc.colorAttachments[0].clearColor = *clear_color;
  399. }
  400. data.mtlpassdesc.colorAttachments[0].loadAction = load;
  401. data.mtlpassdesc.colorAttachments[0].texture = mtltexture;
  402. data.mtlcmdbuffer = [data.mtlcmdqueue commandBuffer];
  403. data.mtlcmdencoder = [data.mtlcmdbuffer renderCommandEncoderWithDescriptor:data.mtlpassdesc];
  404. if (data.mtlbackbuffer != nil && mtltexture == data.mtlbackbuffer.texture) {
  405. data.mtlcmdencoder.label = @"SDL metal renderer backbuffer";
  406. } else {
  407. data.mtlcmdencoder.label = @"SDL metal renderer render target";
  408. }
  409. /* Set up buffer bindings for positions, texcoords, and color once here,
  410. * the offsets are adjusted in the code that uses them. */
  411. if (vertex_buffer != nil) {
  412. [data.mtlcmdencoder setVertexBuffer:vertex_buffer offset:0 atIndex:0];
  413. [data.mtlcmdencoder setFragmentBuffer:vertex_buffer offset:0 atIndex:0];
  414. }
  415. data.activepipelines = ChooseShaderPipelines(data, mtltexture.pixelFormat);
  416. // make sure this has a definite place in the queue. This way it will
  417. // execute reliably whether the app tries to make its own command buffers
  418. // or whatever. This means we can _always_ batch rendering commands!
  419. [data.mtlcmdbuffer enqueue];
  420. }
  421. return true;
  422. }
  423. static void METAL_WindowEvent(SDL_Renderer *renderer, const SDL_WindowEvent *event)
  424. {
  425. }
  426. static bool METAL_GetOutputSize(SDL_Renderer *renderer, int *w, int *h)
  427. {
  428. @autoreleasepool {
  429. SDL3METAL_RenderData *data = (__bridge SDL3METAL_RenderData *)renderer->internal;
  430. if (w) {
  431. *w = (int)data.mtllayer.drawableSize.width;
  432. }
  433. if (h) {
  434. *h = (int)data.mtllayer.drawableSize.height;
  435. }
  436. return true;
  437. }
  438. }
  439. static bool METAL_SupportsBlendMode(SDL_Renderer *renderer, SDL_BlendMode blendMode)
  440. {
  441. SDL_BlendFactor srcColorFactor = SDL_GetBlendModeSrcColorFactor(blendMode);
  442. SDL_BlendFactor srcAlphaFactor = SDL_GetBlendModeSrcAlphaFactor(blendMode);
  443. SDL_BlendOperation colorOperation = SDL_GetBlendModeColorOperation(blendMode);
  444. SDL_BlendFactor dstColorFactor = SDL_GetBlendModeDstColorFactor(blendMode);
  445. SDL_BlendFactor dstAlphaFactor = SDL_GetBlendModeDstAlphaFactor(blendMode);
  446. SDL_BlendOperation alphaOperation = SDL_GetBlendModeAlphaOperation(blendMode);
  447. if (GetBlendFactor(srcColorFactor) == invalidBlendFactor ||
  448. GetBlendFactor(srcAlphaFactor) == invalidBlendFactor ||
  449. GetBlendOperation(colorOperation) == invalidBlendOperation ||
  450. GetBlendFactor(dstColorFactor) == invalidBlendFactor ||
  451. GetBlendFactor(dstAlphaFactor) == invalidBlendFactor ||
  452. GetBlendOperation(alphaOperation) == invalidBlendOperation) {
  453. return false;
  454. }
  455. return true;
  456. }
  457. size_t GetBT601ConversionMatrix(SDL_Colorspace colorspace)
  458. {
  459. switch (SDL_COLORSPACERANGE(colorspace)) {
  460. case SDL_COLOR_RANGE_LIMITED:
  461. case SDL_COLOR_RANGE_UNKNOWN:
  462. return CONSTANTS_OFFSET_DECODE_BT601_LIMITED;
  463. case SDL_COLOR_RANGE_FULL:
  464. return CONSTANTS_OFFSET_DECODE_BT601_FULL;
  465. default:
  466. break;
  467. }
  468. return 0;
  469. }
  470. size_t GetBT709ConversionMatrix(SDL_Colorspace colorspace)
  471. {
  472. switch (SDL_COLORSPACERANGE(colorspace)) {
  473. case SDL_COLOR_RANGE_LIMITED:
  474. case SDL_COLOR_RANGE_UNKNOWN:
  475. return CONSTANTS_OFFSET_DECODE_BT709_LIMITED;
  476. case SDL_COLOR_RANGE_FULL:
  477. return CONSTANTS_OFFSET_DECODE_BT709_FULL;
  478. default:
  479. break;
  480. }
  481. return 0;
  482. }
  483. size_t GetBT2020ConversionMatrix(SDL_Colorspace colorspace)
  484. {
  485. switch (SDL_COLORSPACERANGE(colorspace)) {
  486. case SDL_COLOR_RANGE_LIMITED:
  487. case SDL_COLOR_RANGE_UNKNOWN:
  488. return CONSTANTS_OFFSET_DECODE_BT2020_LIMITED;
  489. case SDL_COLOR_RANGE_FULL:
  490. return CONSTANTS_OFFSET_DECODE_BT2020_FULL;
  491. default:
  492. break;
  493. }
  494. return 0;
  495. }
  496. size_t GetYCbCRtoRGBConversionMatrix(SDL_Colorspace colorspace, int w, int h, int bits_per_pixel)
  497. {
  498. const int YUV_SD_THRESHOLD = 576;
  499. switch (SDL_COLORSPACEMATRIX(colorspace)) {
  500. case SDL_MATRIX_COEFFICIENTS_BT470BG:
  501. case SDL_MATRIX_COEFFICIENTS_BT601:
  502. return GetBT601ConversionMatrix(colorspace);
  503. case SDL_MATRIX_COEFFICIENTS_BT709:
  504. return GetBT709ConversionMatrix(colorspace);
  505. case SDL_MATRIX_COEFFICIENTS_BT2020_NCL:
  506. return GetBT2020ConversionMatrix(colorspace);
  507. case SDL_MATRIX_COEFFICIENTS_UNSPECIFIED:
  508. switch (bits_per_pixel) {
  509. case 8:
  510. if (h <= YUV_SD_THRESHOLD) {
  511. return GetBT601ConversionMatrix(colorspace);
  512. } else {
  513. return GetBT709ConversionMatrix(colorspace);
  514. }
  515. case 10:
  516. case 16:
  517. return GetBT2020ConversionMatrix(colorspace);
  518. default:
  519. break;
  520. }
  521. break;
  522. default:
  523. break;
  524. }
  525. return 0;
  526. }
  527. static bool METAL_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture, SDL_PropertiesID create_props)
  528. {
  529. @autoreleasepool {
  530. SDL3METAL_RenderData *data = (__bridge SDL3METAL_RenderData *)renderer->internal;
  531. MTLPixelFormat pixfmt;
  532. MTLTextureDescriptor *mtltexdesc;
  533. id<MTLTexture> mtltexture = nil, mtltextureUv = nil;
  534. SDL3METAL_TextureData *texturedata;
  535. CVPixelBufferRef pixelbuffer = nil;
  536. IOSurfaceRef surface = nil;
  537. pixelbuffer = SDL_GetPointerProperty(create_props, SDL_PROP_TEXTURE_CREATE_METAL_PIXELBUFFER_POINTER, nil);
  538. if (pixelbuffer) {
  539. surface = CVPixelBufferGetIOSurface(pixelbuffer);
  540. if (!surface) {
  541. return SDL_SetError("CVPixelBufferGetIOSurface() failed");
  542. }
  543. }
  544. switch (texture->format) {
  545. case SDL_PIXELFORMAT_ABGR8888:
  546. if (renderer->output_colorspace == SDL_COLORSPACE_SRGB_LINEAR) {
  547. pixfmt = MTLPixelFormatRGBA8Unorm_sRGB;
  548. } else {
  549. pixfmt = MTLPixelFormatRGBA8Unorm;
  550. }
  551. break;
  552. case SDL_PIXELFORMAT_ARGB8888:
  553. if (renderer->output_colorspace == SDL_COLORSPACE_SRGB_LINEAR) {
  554. pixfmt = MTLPixelFormatBGRA8Unorm_sRGB;
  555. } else {
  556. pixfmt = MTLPixelFormatBGRA8Unorm;
  557. }
  558. break;
  559. case SDL_PIXELFORMAT_ABGR2101010:
  560. pixfmt = MTLPixelFormatRGB10A2Unorm;
  561. break;
  562. case SDL_PIXELFORMAT_IYUV:
  563. case SDL_PIXELFORMAT_YV12:
  564. case SDL_PIXELFORMAT_NV12:
  565. case SDL_PIXELFORMAT_NV21:
  566. pixfmt = MTLPixelFormatR8Unorm;
  567. break;
  568. case SDL_PIXELFORMAT_P010:
  569. pixfmt = MTLPixelFormatR16Unorm;
  570. break;
  571. case SDL_PIXELFORMAT_RGBA64_FLOAT:
  572. pixfmt = MTLPixelFormatRGBA16Float;
  573. break;
  574. case SDL_PIXELFORMAT_RGBA128_FLOAT:
  575. pixfmt = MTLPixelFormatRGBA32Float;
  576. break;
  577. default:
  578. return SDL_SetError("Texture format %s not supported by Metal", SDL_GetPixelFormatName(texture->format));
  579. }
  580. mtltexdesc = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:pixfmt
  581. width:(NSUInteger)texture->w
  582. height:(NSUInteger)texture->h
  583. mipmapped:NO];
  584. if (texture->access == SDL_TEXTUREACCESS_TARGET) {
  585. mtltexdesc.usage = MTLTextureUsageShaderRead | MTLTextureUsageRenderTarget;
  586. } else {
  587. mtltexdesc.usage = MTLTextureUsageShaderRead;
  588. }
  589. if (surface) {
  590. mtltexture = [data.mtldevice newTextureWithDescriptor:mtltexdesc iosurface:surface plane:0];
  591. } else {
  592. mtltexture = [data.mtldevice newTextureWithDescriptor:mtltexdesc];
  593. }
  594. if (mtltexture == nil) {
  595. return SDL_SetError("Texture allocation failed");
  596. }
  597. mtltextureUv = nil;
  598. #ifdef SDL_HAVE_YUV
  599. BOOL yuv = (texture->format == SDL_PIXELFORMAT_IYUV || texture->format == SDL_PIXELFORMAT_YV12);
  600. BOOL nv12 = (texture->format == SDL_PIXELFORMAT_NV12 || texture->format == SDL_PIXELFORMAT_NV21 || texture->format == SDL_PIXELFORMAT_P010);
  601. if (yuv) {
  602. mtltexdesc.pixelFormat = MTLPixelFormatR8Unorm;
  603. mtltexdesc.width = (texture->w + 1) / 2;
  604. mtltexdesc.height = (texture->h + 1) / 2;
  605. mtltexdesc.textureType = MTLTextureType2DArray;
  606. mtltexdesc.arrayLength = 2;
  607. } else if (texture->format == SDL_PIXELFORMAT_P010) {
  608. mtltexdesc.pixelFormat = MTLPixelFormatRG16Unorm;
  609. mtltexdesc.width = (texture->w + 1) / 2;
  610. mtltexdesc.height = (texture->h + 1) / 2;
  611. } else if (nv12) {
  612. mtltexdesc.pixelFormat = MTLPixelFormatRG8Unorm;
  613. mtltexdesc.width = (texture->w + 1) / 2;
  614. mtltexdesc.height = (texture->h + 1) / 2;
  615. }
  616. if (yuv || nv12) {
  617. if (surface) {
  618. mtltextureUv = [data.mtldevice newTextureWithDescriptor:mtltexdesc iosurface:surface plane:1];
  619. } else {
  620. mtltextureUv = [data.mtldevice newTextureWithDescriptor:mtltexdesc];
  621. }
  622. if (mtltextureUv == nil) {
  623. return SDL_SetError("Texture allocation failed");
  624. }
  625. }
  626. #endif // SDL_HAVE_YUV
  627. texturedata = [[SDL3METAL_TextureData alloc] init];
  628. #ifdef SDL_HAVE_YUV
  629. if (yuv) {
  630. texturedata.fragmentFunction = SDL_METAL_FRAGMENT_YUV;
  631. } else if (nv12) {
  632. texturedata.fragmentFunction = SDL_METAL_FRAGMENT_NV12;
  633. } else
  634. #endif
  635. {
  636. texturedata.fragmentFunction = SDL_METAL_FRAGMENT_COPY;
  637. }
  638. texturedata.mtltexture = mtltexture;
  639. texturedata.mtltextureUv = mtltextureUv;
  640. #ifdef SDL_HAVE_YUV
  641. texturedata.yuv = yuv;
  642. texturedata.nv12 = nv12;
  643. if (yuv || nv12) {
  644. size_t offset = GetYCbCRtoRGBConversionMatrix(texture->colorspace, texture->w, texture->h, 8);
  645. if (offset == 0) {
  646. return SDL_SetError("Unsupported YUV colorspace");
  647. }
  648. texturedata.conversionBufferOffset = offset;
  649. }
  650. #endif
  651. texture->internal = (void *)CFBridgingRetain(texturedata);
  652. return true;
  653. }
  654. }
  655. static void METAL_UploadTextureData(id<MTLTexture> texture, SDL_Rect rect, int slice,
  656. const void *pixels, int pitch)
  657. {
  658. [texture replaceRegion:MTLRegionMake2D(rect.x, rect.y, rect.w, rect.h)
  659. mipmapLevel:0
  660. slice:slice
  661. withBytes:pixels
  662. bytesPerRow:pitch
  663. bytesPerImage:0];
  664. }
  665. static MTLStorageMode METAL_GetStorageMode(id<MTLResource> resource)
  666. {
  667. return resource.storageMode;
  668. }
  669. static bool METAL_UpdateTextureInternal(SDL_Renderer *renderer, SDL3METAL_TextureData *texturedata,
  670. id<MTLTexture> texture, SDL_Rect rect, int slice,
  671. const void *pixels, int pitch)
  672. {
  673. SDL3METAL_RenderData *data = (__bridge SDL3METAL_RenderData *)renderer->internal;
  674. SDL_Rect stagingrect = { 0, 0, rect.w, rect.h };
  675. MTLTextureDescriptor *desc;
  676. id<MTLTexture> stagingtex;
  677. id<MTLBlitCommandEncoder> blitcmd;
  678. /* If the texture is managed or shared and this is the first upload, we can
  679. * use replaceRegion to upload to it directly. Otherwise we upload the data
  680. * to a staging texture and copy that over. */
  681. if (!texturedata.hasdata && METAL_GetStorageMode(texture) != MTLStorageModePrivate) {
  682. METAL_UploadTextureData(texture, rect, slice, pixels, pitch);
  683. return true;
  684. }
  685. desc = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:texture.pixelFormat
  686. width:rect.w
  687. height:rect.h
  688. mipmapped:NO];
  689. if (desc == nil) {
  690. return SDL_OutOfMemory();
  691. }
  692. /* TODO: We could have a pool of textures or a MTLHeap we allocate from,
  693. * and release a staging texture back to the pool in the command buffer's
  694. * completion handler. */
  695. stagingtex = [data.mtldevice newTextureWithDescriptor:desc];
  696. if (stagingtex == nil) {
  697. return SDL_OutOfMemory();
  698. }
  699. METAL_UploadTextureData(stagingtex, stagingrect, 0, pixels, pitch);
  700. if (data.mtlcmdencoder != nil) {
  701. [data.mtlcmdencoder endEncoding];
  702. data.mtlcmdencoder = nil;
  703. }
  704. if (data.mtlcmdbuffer == nil) {
  705. data.mtlcmdbuffer = [data.mtlcmdqueue commandBuffer];
  706. }
  707. blitcmd = [data.mtlcmdbuffer blitCommandEncoder];
  708. [blitcmd copyFromTexture:stagingtex
  709. sourceSlice:0
  710. sourceLevel:0
  711. sourceOrigin:MTLOriginMake(0, 0, 0)
  712. sourceSize:MTLSizeMake(rect.w, rect.h, 1)
  713. toTexture:texture
  714. destinationSlice:slice
  715. destinationLevel:0
  716. destinationOrigin:MTLOriginMake(rect.x, rect.y, 0)];
  717. [blitcmd endEncoding];
  718. /* TODO: This isn't very efficient for the YUV formats, which call
  719. * UpdateTextureInternal multiple times in a row. */
  720. [data.mtlcmdbuffer commit];
  721. data.mtlcmdbuffer = nil;
  722. return true;
  723. }
  724. static bool METAL_UpdateTexture(SDL_Renderer *renderer, SDL_Texture *texture,
  725. const SDL_Rect *rect, const void *pixels, int pitch)
  726. {
  727. @autoreleasepool {
  728. SDL3METAL_TextureData *texturedata = (__bridge SDL3METAL_TextureData *)texture->internal;
  729. if (!METAL_UpdateTextureInternal(renderer, texturedata, texturedata.mtltexture, *rect, 0, pixels, pitch)) {
  730. return false;
  731. }
  732. #ifdef SDL_HAVE_YUV
  733. if (texturedata.yuv) {
  734. int Uslice = texture->format == SDL_PIXELFORMAT_YV12 ? 1 : 0;
  735. int Vslice = texture->format == SDL_PIXELFORMAT_YV12 ? 0 : 1;
  736. int UVpitch = (pitch + 1) / 2;
  737. SDL_Rect UVrect = { rect->x / 2, rect->y / 2, (rect->w + 1) / 2, (rect->h + 1) / 2 };
  738. // Skip to the correct offset into the next texture
  739. pixels = (const void *)((const Uint8 *)pixels + rect->h * pitch);
  740. if (!METAL_UpdateTextureInternal(renderer, texturedata, texturedata.mtltextureUv, UVrect, Uslice, pixels, UVpitch)) {
  741. return false;
  742. }
  743. // Skip to the correct offset into the next texture
  744. pixels = (const void *)((const Uint8 *)pixels + UVrect.h * UVpitch);
  745. if (!METAL_UpdateTextureInternal(renderer, texturedata, texturedata.mtltextureUv, UVrect, Vslice, pixels, UVpitch)) {
  746. return false;
  747. }
  748. }
  749. if (texturedata.nv12) {
  750. SDL_Rect UVrect = { rect->x / 2, rect->y / 2, (rect->w + 1) / 2, (rect->h + 1) / 2 };
  751. int UVpitch = 2 * ((pitch + 1) / 2);
  752. // Skip to the correct offset into the next texture
  753. pixels = (const void *)((const Uint8 *)pixels + rect->h * pitch);
  754. if (!METAL_UpdateTextureInternal(renderer, texturedata, texturedata.mtltextureUv, UVrect, 0, pixels, UVpitch)) {
  755. return false;
  756. }
  757. }
  758. #endif
  759. texturedata.hasdata = YES;
  760. return true;
  761. }
  762. }
  763. #ifdef SDL_HAVE_YUV
  764. static bool METAL_UpdateTextureYUV(SDL_Renderer *renderer, SDL_Texture *texture,
  765. const SDL_Rect *rect,
  766. const Uint8 *Yplane, int Ypitch,
  767. const Uint8 *Uplane, int Upitch,
  768. const Uint8 *Vplane, int Vpitch)
  769. {
  770. @autoreleasepool {
  771. SDL3METAL_TextureData *texturedata = (__bridge SDL3METAL_TextureData *)texture->internal;
  772. const int Uslice = 0;
  773. const int Vslice = 1;
  774. SDL_Rect UVrect = { rect->x / 2, rect->y / 2, (rect->w + 1) / 2, (rect->h + 1) / 2 };
  775. // Bail out if we're supposed to update an empty rectangle
  776. if (rect->w <= 0 || rect->h <= 0) {
  777. return true;
  778. }
  779. if (!METAL_UpdateTextureInternal(renderer, texturedata, texturedata.mtltexture, *rect, 0, Yplane, Ypitch)) {
  780. return false;
  781. }
  782. if (!METAL_UpdateTextureInternal(renderer, texturedata, texturedata.mtltextureUv, UVrect, Uslice, Uplane, Upitch)) {
  783. return false;
  784. }
  785. if (!METAL_UpdateTextureInternal(renderer, texturedata, texturedata.mtltextureUv, UVrect, Vslice, Vplane, Vpitch)) {
  786. return false;
  787. }
  788. texturedata.hasdata = YES;
  789. return true;
  790. }
  791. }
  792. static bool METAL_UpdateTextureNV(SDL_Renderer *renderer, SDL_Texture *texture,
  793. const SDL_Rect *rect,
  794. const Uint8 *Yplane, int Ypitch,
  795. const Uint8 *UVplane, int UVpitch)
  796. {
  797. @autoreleasepool {
  798. SDL3METAL_TextureData *texturedata = (__bridge SDL3METAL_TextureData *)texture->internal;
  799. SDL_Rect UVrect = { rect->x / 2, rect->y / 2, (rect->w + 1) / 2, (rect->h + 1) / 2 };
  800. // Bail out if we're supposed to update an empty rectangle
  801. if (rect->w <= 0 || rect->h <= 0) {
  802. return true;
  803. }
  804. if (!METAL_UpdateTextureInternal(renderer, texturedata, texturedata.mtltexture, *rect, 0, Yplane, Ypitch)) {
  805. return false;
  806. }
  807. if (!METAL_UpdateTextureInternal(renderer, texturedata, texturedata.mtltextureUv, UVrect, 0, UVplane, UVpitch)) {
  808. return false;
  809. }
  810. texturedata.hasdata = YES;
  811. return true;
  812. }
  813. }
  814. #endif
  815. static bool METAL_LockTexture(SDL_Renderer *renderer, SDL_Texture *texture,
  816. const SDL_Rect *rect, void **pixels, int *pitch)
  817. {
  818. @autoreleasepool {
  819. SDL3METAL_RenderData *data = (__bridge SDL3METAL_RenderData *)renderer->internal;
  820. SDL3METAL_TextureData *texturedata = (__bridge SDL3METAL_TextureData *)texture->internal;
  821. int buffersize = 0;
  822. id<MTLBuffer> lockedbuffer = nil;
  823. if (rect->w <= 0 || rect->h <= 0) {
  824. return SDL_SetError("Invalid rectangle dimensions for LockTexture.");
  825. }
  826. *pitch = SDL_BYTESPERPIXEL(texture->format) * rect->w;
  827. #ifdef SDL_HAVE_YUV
  828. if (texturedata.yuv || texturedata.nv12) {
  829. buffersize = ((*pitch) * rect->h) + (2 * (*pitch + 1) / 2) * ((rect->h + 1) / 2);
  830. } else
  831. #endif
  832. {
  833. buffersize = (*pitch) * rect->h;
  834. }
  835. lockedbuffer = [data.mtldevice newBufferWithLength:buffersize options:MTLResourceStorageModeShared];
  836. if (lockedbuffer == nil) {
  837. return SDL_OutOfMemory();
  838. }
  839. texturedata.lockedrect = *rect;
  840. texturedata.lockedbuffer = lockedbuffer;
  841. *pixels = [lockedbuffer contents];
  842. return true;
  843. }
  844. }
  845. static void METAL_UnlockTexture(SDL_Renderer *renderer, SDL_Texture *texture)
  846. {
  847. @autoreleasepool {
  848. SDL3METAL_RenderData *data = (__bridge SDL3METAL_RenderData *)renderer->internal;
  849. SDL3METAL_TextureData *texturedata = (__bridge SDL3METAL_TextureData *)texture->internal;
  850. id<MTLBlitCommandEncoder> blitcmd;
  851. SDL_Rect rect = texturedata.lockedrect;
  852. int pitch = SDL_BYTESPERPIXEL(texture->format) * rect.w;
  853. #ifdef SDL_HAVE_YUV
  854. SDL_Rect UVrect = { rect.x / 2, rect.y / 2, (rect.w + 1) / 2, (rect.h + 1) / 2 };
  855. #endif
  856. if (texturedata.lockedbuffer == nil) {
  857. return;
  858. }
  859. if (data.mtlcmdencoder != nil) {
  860. [data.mtlcmdencoder endEncoding];
  861. data.mtlcmdencoder = nil;
  862. }
  863. if (data.mtlcmdbuffer == nil) {
  864. data.mtlcmdbuffer = [data.mtlcmdqueue commandBuffer];
  865. }
  866. blitcmd = [data.mtlcmdbuffer blitCommandEncoder];
  867. [blitcmd copyFromBuffer:texturedata.lockedbuffer
  868. sourceOffset:0
  869. sourceBytesPerRow:pitch
  870. sourceBytesPerImage:0
  871. sourceSize:MTLSizeMake(rect.w, rect.h, 1)
  872. toTexture:texturedata.mtltexture
  873. destinationSlice:0
  874. destinationLevel:0
  875. destinationOrigin:MTLOriginMake(rect.x, rect.y, 0)];
  876. #ifdef SDL_HAVE_YUV
  877. if (texturedata.yuv) {
  878. int Uslice = texture->format == SDL_PIXELFORMAT_YV12 ? 1 : 0;
  879. int Vslice = texture->format == SDL_PIXELFORMAT_YV12 ? 0 : 1;
  880. int UVpitch = (pitch + 1) / 2;
  881. [blitcmd copyFromBuffer:texturedata.lockedbuffer
  882. sourceOffset:rect.h * pitch
  883. sourceBytesPerRow:UVpitch
  884. sourceBytesPerImage:UVpitch * UVrect.h
  885. sourceSize:MTLSizeMake(UVrect.w, UVrect.h, 1)
  886. toTexture:texturedata.mtltextureUv
  887. destinationSlice:Uslice
  888. destinationLevel:0
  889. destinationOrigin:MTLOriginMake(UVrect.x, UVrect.y, 0)];
  890. [blitcmd copyFromBuffer:texturedata.lockedbuffer
  891. sourceOffset:(rect.h * pitch) + UVrect.h * UVpitch
  892. sourceBytesPerRow:UVpitch
  893. sourceBytesPerImage:UVpitch * UVrect.h
  894. sourceSize:MTLSizeMake(UVrect.w, UVrect.h, 1)
  895. toTexture:texturedata.mtltextureUv
  896. destinationSlice:Vslice
  897. destinationLevel:0
  898. destinationOrigin:MTLOriginMake(UVrect.x, UVrect.y, 0)];
  899. }
  900. if (texturedata.nv12) {
  901. int UVpitch = 2 * ((pitch + 1) / 2);
  902. [blitcmd copyFromBuffer:texturedata.lockedbuffer
  903. sourceOffset:rect.h * pitch
  904. sourceBytesPerRow:UVpitch
  905. sourceBytesPerImage:0
  906. sourceSize:MTLSizeMake(UVrect.w, UVrect.h, 1)
  907. toTexture:texturedata.mtltextureUv
  908. destinationSlice:0
  909. destinationLevel:0
  910. destinationOrigin:MTLOriginMake(UVrect.x, UVrect.y, 0)];
  911. }
  912. #endif
  913. [blitcmd endEncoding];
  914. [data.mtlcmdbuffer commit];
  915. data.mtlcmdbuffer = nil;
  916. texturedata.lockedbuffer = nil; // Retained property, so it calls release.
  917. texturedata.hasdata = YES;
  918. }
  919. }
  920. static void METAL_SetTextureScaleMode(SDL_Renderer *renderer, SDL_Texture *texture, SDL_ScaleMode scaleMode)
  921. {
  922. }
  923. static bool METAL_SetRenderTarget(SDL_Renderer *renderer, SDL_Texture *texture)
  924. {
  925. @autoreleasepool {
  926. SDL3METAL_RenderData *data = (__bridge SDL3METAL_RenderData *)renderer->internal;
  927. if (data.mtlcmdencoder) {
  928. /* End encoding for the previous render target so we can set up a new
  929. * render pass for this one. */
  930. [data.mtlcmdencoder endEncoding];
  931. [data.mtlcmdbuffer commit];
  932. data.mtlcmdencoder = nil;
  933. data.mtlcmdbuffer = nil;
  934. }
  935. /* We don't begin a new render pass right away - we delay it until an actual
  936. * draw or clear happens. That way we can use hardware clears when possible,
  937. * which are only available when beginning a new render pass. */
  938. return true;
  939. }
  940. }
  941. static bool METAL_QueueSetViewport(SDL_Renderer *renderer, SDL_RenderCommand *cmd)
  942. {
  943. float projection[4][4]; // Prepare an orthographic projection
  944. const int w = cmd->data.viewport.rect.w;
  945. const int h = cmd->data.viewport.rect.h;
  946. const size_t matrixlen = sizeof(projection);
  947. float *matrix = (float *)SDL_AllocateRenderVertices(renderer, matrixlen, CONSTANT_ALIGN(16), &cmd->data.viewport.first);
  948. if (!matrix) {
  949. return false;
  950. }
  951. SDL_memset(projection, '\0', matrixlen);
  952. if (w && h) {
  953. projection[0][0] = 2.0f / w;
  954. projection[1][1] = -2.0f / h;
  955. projection[3][0] = -1.0f;
  956. projection[3][1] = 1.0f;
  957. projection[3][3] = 1.0f;
  958. }
  959. SDL_memcpy(matrix, projection, matrixlen);
  960. return true;
  961. }
  962. static bool METAL_QueueNoOp(SDL_Renderer *renderer, SDL_RenderCommand *cmd)
  963. {
  964. return true; // nothing to do in this backend.
  965. }
  966. static bool METAL_QueueDrawPoints(SDL_Renderer *renderer, SDL_RenderCommand *cmd, const SDL_FPoint *points, int count)
  967. {
  968. SDL_FColor color = cmd->data.draw.color;
  969. bool convert_color = SDL_RenderingLinearSpace(renderer);
  970. const size_t vertlen = (2 * sizeof(float) + 4 * sizeof(float)) * count;
  971. float *verts = (float *)SDL_AllocateRenderVertices(renderer, vertlen, DEVICE_ALIGN(8), &cmd->data.draw.first);
  972. if (!verts) {
  973. return false;
  974. }
  975. cmd->data.draw.count = count;
  976. if (convert_color) {
  977. SDL_ConvertToLinear(&color);
  978. }
  979. for (int i = 0; i < count; i++, points++) {
  980. *(verts++) = points->x;
  981. *(verts++) = points->y;
  982. *(verts++) = color.r;
  983. *(verts++) = color.g;
  984. *(verts++) = color.b;
  985. *(verts++) = color.a;
  986. }
  987. return true;
  988. }
  989. static bool METAL_QueueDrawLines(SDL_Renderer *renderer, SDL_RenderCommand *cmd, const SDL_FPoint *points, int count)
  990. {
  991. SDL_FColor color = cmd->data.draw.color;
  992. bool convert_color = SDL_RenderingLinearSpace(renderer);
  993. size_t vertlen;
  994. float *verts;
  995. SDL_assert(count >= 2); // should have been checked at the higher level.
  996. vertlen = (2 * sizeof(float) + 4 * sizeof(float)) * count;
  997. verts = (float *)SDL_AllocateRenderVertices(renderer, vertlen, DEVICE_ALIGN(8), &cmd->data.draw.first);
  998. if (!verts) {
  999. return false;
  1000. }
  1001. cmd->data.draw.count = count;
  1002. if (convert_color) {
  1003. SDL_ConvertToLinear(&color);
  1004. }
  1005. for (int i = 0; i < count; i++, points++) {
  1006. *(verts++) = points->x;
  1007. *(verts++) = points->y;
  1008. *(verts++) = color.r;
  1009. *(verts++) = color.g;
  1010. *(verts++) = color.b;
  1011. *(verts++) = color.a;
  1012. }
  1013. /* If the line segment is completely horizontal or vertical,
  1014. make it one pixel longer, to satisfy the diamond-exit rule.
  1015. We should probably do this for diagonal lines too, but we'd have to
  1016. do some trigonometry to figure out the correct pixel and generally
  1017. when we have problems with pixel perfection, it's for straight lines
  1018. that are missing a pixel that frames something and not arbitrary
  1019. angles. Maybe !!! FIXME for later, though. */
  1020. points -= 2; // update the last line.
  1021. verts -= 2 + 1;
  1022. {
  1023. const float xstart = points[0].x;
  1024. const float ystart = points[0].y;
  1025. const float xend = points[1].x;
  1026. const float yend = points[1].y;
  1027. if (ystart == yend) { // horizontal line
  1028. verts[0] += (xend > xstart) ? 1.0f : -1.0f;
  1029. } else if (xstart == xend) { // vertical line
  1030. verts[1] += (yend > ystart) ? 1.0f : -1.0f;
  1031. }
  1032. }
  1033. return true;
  1034. }
  1035. static bool METAL_QueueGeometry(SDL_Renderer *renderer, SDL_RenderCommand *cmd, SDL_Texture *texture,
  1036. const float *xy, int xy_stride, const SDL_FColor *color, int color_stride, const float *uv, int uv_stride,
  1037. int num_vertices, const void *indices, int num_indices, int size_indices,
  1038. float scale_x, float scale_y)
  1039. {
  1040. bool convert_color = SDL_RenderingLinearSpace(renderer);
  1041. int count = indices ? num_indices : num_vertices;
  1042. const size_t vertlen = (2 * sizeof(float) + 4 * sizeof(float) + (texture ? 2 : 0) * sizeof(float)) * count;
  1043. float *verts = (float *)SDL_AllocateRenderVertices(renderer, vertlen, DEVICE_ALIGN(8), &cmd->data.draw.first);
  1044. if (!verts) {
  1045. return false;
  1046. }
  1047. cmd->data.draw.count = count;
  1048. size_indices = indices ? size_indices : 0;
  1049. for (int i = 0; i < count; i++) {
  1050. int j;
  1051. float *xy_;
  1052. SDL_FColor col_;
  1053. if (size_indices == 4) {
  1054. j = ((const Uint32 *)indices)[i];
  1055. } else if (size_indices == 2) {
  1056. j = ((const Uint16 *)indices)[i];
  1057. } else if (size_indices == 1) {
  1058. j = ((const Uint8 *)indices)[i];
  1059. } else {
  1060. j = i;
  1061. }
  1062. xy_ = (float *)((char *)xy + j * xy_stride);
  1063. *(verts++) = xy_[0] * scale_x;
  1064. *(verts++) = xy_[1] * scale_y;
  1065. col_ = *(SDL_FColor *)((char *)color + j * color_stride);
  1066. if (convert_color) {
  1067. SDL_ConvertToLinear(&col_);
  1068. }
  1069. *(verts++) = col_.r;
  1070. *(verts++) = col_.g;
  1071. *(verts++) = col_.b;
  1072. *(verts++) = col_.a;
  1073. if (texture) {
  1074. float *uv_ = (float *)((char *)uv + j * uv_stride);
  1075. *(verts++) = uv_[0];
  1076. *(verts++) = uv_[1];
  1077. }
  1078. }
  1079. return true;
  1080. }
  1081. // These should mirror the definitions in SDL_shaders_metal.metal
  1082. //static const float TONEMAP_NONE = 0;
  1083. //static const float TONEMAP_LINEAR = 1;
  1084. static const float TONEMAP_CHROME = 2;
  1085. //static const float TEXTURETYPE_NONE = 0;
  1086. static const float TEXTURETYPE_RGB = 1;
  1087. static const float TEXTURETYPE_NV12 = 2;
  1088. static const float TEXTURETYPE_NV21 = 3;
  1089. static const float TEXTURETYPE_YUV = 4;
  1090. //static const float INPUTTYPE_UNSPECIFIED = 0;
  1091. static const float INPUTTYPE_SRGB = 1;
  1092. static const float INPUTTYPE_SCRGB = 2;
  1093. static const float INPUTTYPE_HDR10 = 3;
  1094. typedef struct
  1095. {
  1096. float scRGB_output;
  1097. float texture_type;
  1098. float input_type;
  1099. float color_scale;
  1100. float tonemap_method;
  1101. float tonemap_factor1;
  1102. float tonemap_factor2;
  1103. float sdr_white_point;
  1104. } PixelShaderConstants;
  1105. typedef struct
  1106. {
  1107. __unsafe_unretained id<MTLRenderPipelineState> pipeline;
  1108. __unsafe_unretained id<MTLBuffer> vertex_buffer;
  1109. size_t constants_offset;
  1110. SDL_Texture *texture;
  1111. bool cliprect_dirty;
  1112. bool cliprect_enabled;
  1113. SDL_Rect cliprect;
  1114. bool viewport_dirty;
  1115. SDL_Rect viewport;
  1116. size_t projection_offset;
  1117. bool shader_constants_dirty;
  1118. PixelShaderConstants shader_constants;
  1119. } METAL_DrawStateCache;
  1120. static void SetupShaderConstants(SDL_Renderer *renderer, const SDL_RenderCommand *cmd, const SDL_Texture *texture, PixelShaderConstants *constants)
  1121. {
  1122. float output_headroom;
  1123. SDL_zerop(constants);
  1124. constants->scRGB_output = (float)SDL_RenderingLinearSpace(renderer);
  1125. constants->color_scale = cmd->data.draw.color_scale;
  1126. if (texture) {
  1127. switch (texture->format) {
  1128. case SDL_PIXELFORMAT_YV12:
  1129. case SDL_PIXELFORMAT_IYUV:
  1130. constants->texture_type = TEXTURETYPE_YUV;
  1131. break;
  1132. case SDL_PIXELFORMAT_NV12:
  1133. constants->texture_type = TEXTURETYPE_NV12;
  1134. break;
  1135. case SDL_PIXELFORMAT_NV21:
  1136. constants->texture_type = TEXTURETYPE_NV21;
  1137. break;
  1138. case SDL_PIXELFORMAT_P010:
  1139. constants->texture_type = TEXTURETYPE_NV12;
  1140. break;
  1141. default:
  1142. constants->texture_type = TEXTURETYPE_RGB;
  1143. }
  1144. switch (SDL_COLORSPACETRANSFER(texture->colorspace)) {
  1145. case SDL_TRANSFER_CHARACTERISTICS_LINEAR:
  1146. constants->input_type = INPUTTYPE_SCRGB;
  1147. break;
  1148. case SDL_TRANSFER_CHARACTERISTICS_PQ:
  1149. constants->input_type = INPUTTYPE_HDR10;
  1150. break;
  1151. default:
  1152. constants->input_type = INPUTTYPE_SRGB;
  1153. break;
  1154. }
  1155. constants->sdr_white_point = texture->SDR_white_point;
  1156. if (renderer->target) {
  1157. output_headroom = renderer->target->HDR_headroom;
  1158. } else {
  1159. output_headroom = renderer->HDR_headroom;
  1160. }
  1161. if (texture->HDR_headroom > output_headroom) {
  1162. constants->tonemap_method = TONEMAP_CHROME;
  1163. constants->tonemap_factor1 = (output_headroom / (texture->HDR_headroom * texture->HDR_headroom));
  1164. constants->tonemap_factor2 = (1.0f / output_headroom);
  1165. }
  1166. }
  1167. }
  1168. static bool SetDrawState(SDL_Renderer *renderer, const SDL_RenderCommand *cmd, const SDL_MetalFragmentFunction shader, PixelShaderConstants *shader_constants, const size_t constants_offset, id<MTLBuffer> mtlbufvertex, METAL_DrawStateCache *statecache)
  1169. {
  1170. SDL3METAL_RenderData *data = (__bridge SDL3METAL_RenderData *)renderer->internal;
  1171. const SDL_BlendMode blend = cmd->data.draw.blend;
  1172. size_t first = cmd->data.draw.first;
  1173. id<MTLRenderPipelineState> newpipeline;
  1174. PixelShaderConstants solid_constants;
  1175. if (!METAL_ActivateRenderCommandEncoder(renderer, MTLLoadActionLoad, NULL, statecache->vertex_buffer)) {
  1176. return false;
  1177. }
  1178. if (statecache->viewport_dirty) {
  1179. MTLViewport viewport;
  1180. viewport.originX = statecache->viewport.x;
  1181. viewport.originY = statecache->viewport.y;
  1182. viewport.width = statecache->viewport.w;
  1183. viewport.height = statecache->viewport.h;
  1184. viewport.znear = 0.0;
  1185. viewport.zfar = 1.0;
  1186. [data.mtlcmdencoder setViewport:viewport];
  1187. [data.mtlcmdencoder setVertexBuffer:mtlbufvertex offset:statecache->projection_offset atIndex:2]; // projection
  1188. statecache->viewport_dirty = false;
  1189. }
  1190. if (statecache->cliprect_dirty) {
  1191. SDL_Rect output;
  1192. SDL_Rect clip;
  1193. if (statecache->cliprect_enabled) {
  1194. clip = statecache->cliprect;
  1195. clip.x += statecache->viewport.x;
  1196. clip.y += statecache->viewport.y;
  1197. } else {
  1198. clip = statecache->viewport;
  1199. }
  1200. // Set Scissor Rect Validation: w/h must be <= render pass
  1201. SDL_zero(output);
  1202. if (renderer->target) {
  1203. output.w = renderer->target->w;
  1204. output.h = renderer->target->h;
  1205. } else {
  1206. METAL_GetOutputSize(renderer, &output.w, &output.h);
  1207. }
  1208. if (SDL_GetRectIntersection(&output, &clip, &clip)) {
  1209. MTLScissorRect mtlrect;
  1210. mtlrect.x = clip.x;
  1211. mtlrect.y = clip.y;
  1212. mtlrect.width = clip.w;
  1213. mtlrect.height = clip.h;
  1214. [data.mtlcmdencoder setScissorRect:mtlrect];
  1215. }
  1216. statecache->cliprect_dirty = false;
  1217. }
  1218. newpipeline = ChoosePipelineState(data, data.activepipelines, shader, blend);
  1219. if (newpipeline != statecache->pipeline) {
  1220. [data.mtlcmdencoder setRenderPipelineState:newpipeline];
  1221. statecache->pipeline = newpipeline;
  1222. }
  1223. if (!shader_constants) {
  1224. SetupShaderConstants(renderer, cmd, NULL, &solid_constants);
  1225. shader_constants = &solid_constants;
  1226. }
  1227. if (statecache->shader_constants_dirty ||
  1228. SDL_memcmp(shader_constants, &statecache->shader_constants, sizeof(*shader_constants)) != 0) {
  1229. id<MTLBuffer> mtlbufconstants = [data.mtldevice newBufferWithLength:sizeof(*shader_constants) options:MTLResourceStorageModeShared];
  1230. mtlbufconstants.label = @"SDL shader constants data";
  1231. SDL_memcpy([mtlbufconstants contents], shader_constants, sizeof(*shader_constants));
  1232. [data.mtlcmdencoder setFragmentBuffer:mtlbufconstants offset:0 atIndex:0];
  1233. SDL_memcpy(&statecache->shader_constants, shader_constants, sizeof(*shader_constants));
  1234. statecache->shader_constants_dirty = false;
  1235. }
  1236. if (constants_offset != statecache->constants_offset) {
  1237. if (constants_offset != CONSTANTS_OFFSET_INVALID) {
  1238. [data.mtlcmdencoder setVertexBuffer:data.mtlbufconstants offset:constants_offset atIndex:3];
  1239. }
  1240. statecache->constants_offset = constants_offset;
  1241. }
  1242. [data.mtlcmdencoder setVertexBufferOffset:first atIndex:0]; // position/texcoords
  1243. return true;
  1244. }
  1245. static bool SetCopyState(SDL_Renderer *renderer, const SDL_RenderCommand *cmd, const size_t constants_offset,
  1246. id<MTLBuffer> mtlbufvertex, METAL_DrawStateCache *statecache)
  1247. {
  1248. SDL3METAL_RenderData *data = (__bridge SDL3METAL_RenderData *)renderer->internal;
  1249. SDL_Texture *texture = cmd->data.draw.texture;
  1250. SDL3METAL_TextureData *texturedata = (__bridge SDL3METAL_TextureData *)texture->internal;
  1251. PixelShaderConstants constants;
  1252. SetupShaderConstants(renderer, cmd, texture, &constants);
  1253. if (!SetDrawState(renderer, cmd, texturedata.fragmentFunction, &constants, constants_offset, mtlbufvertex, statecache)) {
  1254. return false;
  1255. }
  1256. if (texture != statecache->texture) {
  1257. id<MTLSamplerState> mtlsampler;
  1258. if (texture->scaleMode == SDL_SCALEMODE_NEAREST) {
  1259. switch (cmd->data.draw.texture_address_mode) {
  1260. case SDL_TEXTURE_ADDRESS_CLAMP:
  1261. mtlsampler = data.mtlsamplers[SDL_METAL_SAMPLER_NEAREST_CLAMP];
  1262. break;
  1263. case SDL_TEXTURE_ADDRESS_WRAP:
  1264. mtlsampler = data.mtlsamplers[SDL_METAL_SAMPLER_NEAREST_WRAP];
  1265. break;
  1266. default:
  1267. return SDL_SetError("Unknown texture address mode: %d", cmd->data.draw.texture_address_mode);
  1268. }
  1269. } else {
  1270. switch (cmd->data.draw.texture_address_mode) {
  1271. case SDL_TEXTURE_ADDRESS_CLAMP:
  1272. mtlsampler = data.mtlsamplers[SDL_METAL_SAMPLER_LINEAR_CLAMP];
  1273. break;
  1274. case SDL_TEXTURE_ADDRESS_WRAP:
  1275. mtlsampler = data.mtlsamplers[SDL_METAL_SAMPLER_LINEAR_WRAP];
  1276. break;
  1277. default:
  1278. return SDL_SetError("Unknown texture address mode: %d", cmd->data.draw.texture_address_mode);
  1279. }
  1280. }
  1281. [data.mtlcmdencoder setFragmentSamplerState:mtlsampler atIndex:0];
  1282. [data.mtlcmdencoder setFragmentTexture:texturedata.mtltexture atIndex:0];
  1283. #ifdef SDL_HAVE_YUV
  1284. if (texturedata.yuv || texturedata.nv12) {
  1285. [data.mtlcmdencoder setFragmentTexture:texturedata.mtltextureUv atIndex:1];
  1286. [data.mtlcmdencoder setFragmentBuffer:data.mtlbufconstants offset:texturedata.conversionBufferOffset atIndex:1];
  1287. }
  1288. #endif
  1289. statecache->texture = texture;
  1290. }
  1291. return true;
  1292. }
  1293. static void METAL_InvalidateCachedState(SDL_Renderer *renderer)
  1294. {
  1295. // METAL_DrawStateCache only exists during a run of METAL_RunCommandQueue, so there's nothing to invalidate!
  1296. }
  1297. static bool METAL_RunCommandQueue(SDL_Renderer *renderer, SDL_RenderCommand *cmd, void *vertices, size_t vertsize)
  1298. {
  1299. @autoreleasepool {
  1300. SDL3METAL_RenderData *data = (__bridge SDL3METAL_RenderData *)renderer->internal;
  1301. id<MTLBuffer> mtlbufvertex = nil;
  1302. METAL_DrawStateCache statecache;
  1303. SDL_zero(statecache);
  1304. statecache.pipeline = nil;
  1305. statecache.vertex_buffer = nil;
  1306. statecache.constants_offset = CONSTANTS_OFFSET_INVALID;
  1307. statecache.texture = NULL;
  1308. statecache.shader_constants_dirty = true;
  1309. statecache.cliprect_dirty = true;
  1310. statecache.viewport_dirty = true;
  1311. statecache.projection_offset = 0;
  1312. // !!! FIXME: have a ring of pre-made MTLBuffers we cycle through? How expensive is creation?
  1313. if (vertsize > 0) {
  1314. /* We can memcpy to a shared buffer from the CPU and read it from the GPU
  1315. * without any extra copying. It's a bit slower on macOS to read shared
  1316. * data from the GPU than to read managed/private data, but we avoid the
  1317. * cost of copying the data and the code's simpler. Apple's best
  1318. * practices guide recommends this approach for streamed vertex data.
  1319. */
  1320. mtlbufvertex = [data.mtldevice newBufferWithLength:vertsize options:MTLResourceStorageModeShared];
  1321. mtlbufvertex.label = @"SDL vertex data";
  1322. SDL_memcpy([mtlbufvertex contents], vertices, vertsize);
  1323. statecache.vertex_buffer = mtlbufvertex;
  1324. }
  1325. // If there's a command buffer here unexpectedly (app requested one?). Commit it so we can start fresh.
  1326. [data.mtlcmdencoder endEncoding];
  1327. [data.mtlcmdbuffer commit];
  1328. data.mtlcmdencoder = nil;
  1329. data.mtlcmdbuffer = nil;
  1330. while (cmd) {
  1331. switch (cmd->command) {
  1332. case SDL_RENDERCMD_SETVIEWPORT:
  1333. {
  1334. SDL_memcpy(&statecache.viewport, &cmd->data.viewport.rect, sizeof(statecache.viewport));
  1335. statecache.projection_offset = cmd->data.viewport.first;
  1336. statecache.viewport_dirty = true;
  1337. statecache.cliprect_dirty = true;
  1338. break;
  1339. }
  1340. case SDL_RENDERCMD_SETCLIPRECT:
  1341. {
  1342. SDL_memcpy(&statecache.cliprect, &cmd->data.cliprect.rect, sizeof(statecache.cliprect));
  1343. statecache.cliprect_enabled = cmd->data.cliprect.enabled;
  1344. statecache.cliprect_dirty = true;
  1345. break;
  1346. }
  1347. case SDL_RENDERCMD_SETDRAWCOLOR:
  1348. {
  1349. break;
  1350. }
  1351. case SDL_RENDERCMD_CLEAR:
  1352. {
  1353. /* If we're already encoding a command buffer, dump it without committing it. We'd just
  1354. clear all its work anyhow, and starting a new encoder will let us use a hardware clear
  1355. operation via MTLLoadActionClear. */
  1356. if (data.mtlcmdencoder != nil) {
  1357. [data.mtlcmdencoder endEncoding];
  1358. // !!! FIXME: have to commit, or an uncommitted but enqueued buffer will prevent the frame from finishing.
  1359. [data.mtlcmdbuffer commit];
  1360. data.mtlcmdencoder = nil;
  1361. data.mtlcmdbuffer = nil;
  1362. }
  1363. // force all this state to be reconfigured on next command buffer.
  1364. statecache.pipeline = nil;
  1365. statecache.constants_offset = CONSTANTS_OFFSET_INVALID;
  1366. statecache.texture = NULL;
  1367. statecache.shader_constants_dirty = true;
  1368. statecache.cliprect_dirty = true;
  1369. statecache.viewport_dirty = true;
  1370. {
  1371. bool convert_color = SDL_RenderingLinearSpace(renderer);
  1372. SDL_FColor color = cmd->data.color.color;
  1373. if (convert_color) {
  1374. SDL_ConvertToLinear(&color);
  1375. }
  1376. color.r *= cmd->data.color.color_scale;
  1377. color.g *= cmd->data.color.color_scale;
  1378. color.b *= cmd->data.color.color_scale;
  1379. MTLClearColor mtlcolor = MTLClearColorMake(color.r, color.g, color.b, color.a);
  1380. // get new command encoder, set up with an initial clear operation.
  1381. // (this might fail, and future draw operations will notice.)
  1382. METAL_ActivateRenderCommandEncoder(renderer, MTLLoadActionClear, &mtlcolor, mtlbufvertex);
  1383. }
  1384. break;
  1385. }
  1386. case SDL_RENDERCMD_DRAW_POINTS:
  1387. case SDL_RENDERCMD_DRAW_LINES:
  1388. {
  1389. const size_t count = cmd->data.draw.count;
  1390. const MTLPrimitiveType primtype = (cmd->command == SDL_RENDERCMD_DRAW_POINTS) ? MTLPrimitiveTypePoint : MTLPrimitiveTypeLineStrip;
  1391. if (SetDrawState(renderer, cmd, SDL_METAL_FRAGMENT_SOLID, NULL, CONSTANTS_OFFSET_HALF_PIXEL_TRANSFORM, mtlbufvertex, &statecache)) {
  1392. [data.mtlcmdencoder drawPrimitives:primtype vertexStart:0 vertexCount:count];
  1393. }
  1394. break;
  1395. }
  1396. case SDL_RENDERCMD_FILL_RECTS: // unused
  1397. break;
  1398. case SDL_RENDERCMD_COPY: // unused
  1399. break;
  1400. case SDL_RENDERCMD_COPY_EX: // unused
  1401. break;
  1402. case SDL_RENDERCMD_GEOMETRY:
  1403. {
  1404. const size_t count = cmd->data.draw.count;
  1405. SDL_Texture *texture = cmd->data.draw.texture;
  1406. if (texture) {
  1407. if (SetCopyState(renderer, cmd, CONSTANTS_OFFSET_IDENTITY, mtlbufvertex, &statecache)) {
  1408. [data.mtlcmdencoder drawPrimitives:MTLPrimitiveTypeTriangle vertexStart:0 vertexCount:count];
  1409. }
  1410. } else {
  1411. if (SetDrawState(renderer, cmd, SDL_METAL_FRAGMENT_SOLID, NULL, CONSTANTS_OFFSET_IDENTITY, mtlbufvertex, &statecache)) {
  1412. [data.mtlcmdencoder drawPrimitives:MTLPrimitiveTypeTriangle vertexStart:0 vertexCount:count];
  1413. }
  1414. }
  1415. break;
  1416. }
  1417. case SDL_RENDERCMD_NO_OP:
  1418. break;
  1419. }
  1420. cmd = cmd->next;
  1421. }
  1422. return true;
  1423. }
  1424. }
  1425. static SDL_Surface *METAL_RenderReadPixels(SDL_Renderer *renderer, const SDL_Rect *rect)
  1426. {
  1427. @autoreleasepool {
  1428. SDL3METAL_RenderData *data = (__bridge SDL3METAL_RenderData *)renderer->internal;
  1429. id<MTLTexture> mtltexture;
  1430. MTLRegion mtlregion;
  1431. Uint32 format;
  1432. SDL_Surface *surface;
  1433. if (!METAL_ActivateRenderCommandEncoder(renderer, MTLLoadActionLoad, NULL, nil)) {
  1434. SDL_SetError("Failed to activate render command encoder (is your window in the background?");
  1435. return NULL;
  1436. }
  1437. [data.mtlcmdencoder endEncoding];
  1438. mtltexture = data.mtlpassdesc.colorAttachments[0].texture;
  1439. #ifdef SDL_PLATFORM_MACOS
  1440. /* on macOS with managed-storage textures, we need to tell the driver to
  1441. * update the CPU-side copy of the texture data.
  1442. * NOTE: Currently all of our textures are managed on macOS. We'll need some
  1443. * extra copying for any private textures. */
  1444. if (METAL_GetStorageMode(mtltexture) == MTLStorageModeManaged) {
  1445. id<MTLBlitCommandEncoder> blit = [data.mtlcmdbuffer blitCommandEncoder];
  1446. [blit synchronizeResource:mtltexture];
  1447. [blit endEncoding];
  1448. }
  1449. #endif
  1450. /* Commit the current command buffer and wait until it's completed, to make
  1451. * sure the GPU has finished rendering to it by the time we read it. */
  1452. [data.mtlcmdbuffer commit];
  1453. [data.mtlcmdbuffer waitUntilCompleted];
  1454. data.mtlcmdencoder = nil;
  1455. data.mtlcmdbuffer = nil;
  1456. mtlregion = MTLRegionMake2D(rect->x, rect->y, rect->w, rect->h);
  1457. switch (mtltexture.pixelFormat) {
  1458. case MTLPixelFormatBGRA8Unorm:
  1459. case MTLPixelFormatBGRA8Unorm_sRGB:
  1460. format = SDL_PIXELFORMAT_ARGB8888;
  1461. break;
  1462. case MTLPixelFormatRGBA8Unorm:
  1463. case MTLPixelFormatRGBA8Unorm_sRGB:
  1464. format = SDL_PIXELFORMAT_ABGR8888;
  1465. break;
  1466. case MTLPixelFormatRGB10A2Unorm:
  1467. format = SDL_PIXELFORMAT_ABGR2101010;
  1468. break;
  1469. case MTLPixelFormatRGBA16Float:
  1470. format = SDL_PIXELFORMAT_RGBA64_FLOAT;
  1471. break;
  1472. default:
  1473. SDL_SetError("Unknown framebuffer pixel format");
  1474. return NULL;
  1475. }
  1476. surface = SDL_CreateSurface(rect->w, rect->h, format);
  1477. if (surface) {
  1478. [mtltexture getBytes:surface->pixels bytesPerRow:surface->pitch fromRegion:mtlregion mipmapLevel:0];
  1479. }
  1480. return surface;
  1481. }
  1482. }
  1483. static bool METAL_RenderPresent(SDL_Renderer *renderer)
  1484. {
  1485. @autoreleasepool {
  1486. SDL3METAL_RenderData *data = (__bridge SDL3METAL_RenderData *)renderer->internal;
  1487. bool ready = true;
  1488. // If we don't have a command buffer, we can't present, so activate to get one.
  1489. if (data.mtlcmdencoder == nil) {
  1490. // We haven't even gotten a backbuffer yet? Load and clear it. Otherwise, load the existing data.
  1491. if (data.mtlbackbuffer == nil) {
  1492. float alpha = (SDL_GetWindowFlags(renderer->window) & SDL_WINDOW_TRANSPARENT) ? 0.0f : 1.0f;
  1493. MTLClearColor color = MTLClearColorMake(0.0f, 0.0f, 0.0f, alpha);
  1494. ready = METAL_ActivateRenderCommandEncoder(renderer, MTLLoadActionClear, &color, nil);
  1495. } else {
  1496. ready = METAL_ActivateRenderCommandEncoder(renderer, MTLLoadActionLoad, NULL, nil);
  1497. }
  1498. }
  1499. [data.mtlcmdencoder endEncoding];
  1500. // If we don't have a drawable to present, don't try to present it.
  1501. // But we'll still try to commit the command buffer in case it was already enqueued.
  1502. if (ready) {
  1503. SDL_assert(data.mtlbackbuffer != nil);
  1504. [data.mtlcmdbuffer presentDrawable:data.mtlbackbuffer];
  1505. }
  1506. [data.mtlcmdbuffer commit];
  1507. data.mtlcmdencoder = nil;
  1508. data.mtlcmdbuffer = nil;
  1509. data.mtlbackbuffer = nil;
  1510. if (renderer->hidden || !ready) {
  1511. return false;
  1512. }
  1513. return true;
  1514. }
  1515. }
  1516. static void METAL_DestroyTexture(SDL_Renderer *renderer, SDL_Texture *texture)
  1517. {
  1518. @autoreleasepool {
  1519. CFBridgingRelease(texture->internal);
  1520. texture->internal = NULL;
  1521. }
  1522. }
  1523. static void METAL_DestroyRenderer(SDL_Renderer *renderer)
  1524. {
  1525. @autoreleasepool {
  1526. if (renderer->internal) {
  1527. SDL3METAL_RenderData *data = CFBridgingRelease(renderer->internal);
  1528. if (data.mtlcmdencoder != nil) {
  1529. [data.mtlcmdencoder endEncoding];
  1530. }
  1531. DestroyAllPipelines(data.allpipelines, data.pipelinescount);
  1532. /* Release the metal view instead of destroying it,
  1533. in case we want to use it later (recreating the renderer)
  1534. */
  1535. // SDL_Metal_DestroyView(data.mtlview);
  1536. CFBridgingRelease(data.mtlview);
  1537. }
  1538. }
  1539. }
  1540. static void *METAL_GetMetalLayer(SDL_Renderer *renderer)
  1541. {
  1542. @autoreleasepool {
  1543. SDL3METAL_RenderData *data = (__bridge SDL3METAL_RenderData *)renderer->internal;
  1544. return (__bridge void *)data.mtllayer;
  1545. }
  1546. }
  1547. static void *METAL_GetMetalCommandEncoder(SDL_Renderer *renderer)
  1548. {
  1549. @autoreleasepool {
  1550. // note that data.mtlcmdencoder can be nil if METAL_ActivateRenderCommandEncoder fails.
  1551. // Before SDL 2.0.18, it might have returned a non-nil encoding that might not have been
  1552. // usable for presentation. Check your return values!
  1553. SDL3METAL_RenderData *data;
  1554. METAL_ActivateRenderCommandEncoder(renderer, MTLLoadActionLoad, NULL, nil);
  1555. data = (__bridge SDL3METAL_RenderData *)renderer->internal;
  1556. return (__bridge void *)data.mtlcmdencoder;
  1557. }
  1558. }
  1559. static bool METAL_SetVSync(SDL_Renderer *renderer, const int vsync)
  1560. {
  1561. #if defined(SDL_PLATFORM_MACOS) || TARGET_OS_MACCATALYST
  1562. SDL3METAL_RenderData *data = (__bridge SDL3METAL_RenderData *)renderer->internal;
  1563. switch (vsync) {
  1564. case 0:
  1565. data.mtllayer.displaySyncEnabled = NO;
  1566. break;
  1567. case 1:
  1568. data.mtllayer.displaySyncEnabled = YES;
  1569. break;
  1570. default:
  1571. return SDL_Unsupported();
  1572. }
  1573. return true;
  1574. #else
  1575. switch (vsync) {
  1576. case 1:
  1577. return true;
  1578. default:
  1579. return SDL_Unsupported();
  1580. }
  1581. #endif
  1582. }
  1583. static SDL_MetalView GetWindowView(SDL_Window *window)
  1584. {
  1585. #ifdef SDL_VIDEO_DRIVER_COCOA
  1586. NSWindow *nswindow = (__bridge NSWindow *)SDL_GetPointerProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_COCOA_WINDOW_POINTER, NULL);
  1587. NSInteger tag = (NSInteger)SDL_GetNumberProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_COCOA_METAL_VIEW_TAG_NUMBER, 0);
  1588. if (nswindow && tag) {
  1589. NSView *view = nswindow.contentView;
  1590. if (view.subviews.count > 0) {
  1591. view = view.subviews[0];
  1592. if (view.tag == tag) {
  1593. return (SDL_MetalView)CFBridgingRetain(view);
  1594. }
  1595. }
  1596. }
  1597. #endif
  1598. #ifdef SDL_VIDEO_DRIVER_UIKIT
  1599. UIWindow *uiwindow = (__bridge UIWindow *)SDL_GetPointerProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_UIKIT_WINDOW_POINTER, NULL);
  1600. NSInteger tag = (NSInteger)SDL_GetNumberProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_UIKIT_METAL_VIEW_TAG_NUMBER, 0);
  1601. if (uiwindow && tag) {
  1602. UIView *view = uiwindow.rootViewController.view;
  1603. if (view.tag == tag) {
  1604. return (SDL_MetalView)CFBridgingRetain(view);
  1605. }
  1606. }
  1607. #endif
  1608. return nil;
  1609. }
  1610. static bool METAL_CreateRenderer(SDL_Renderer *renderer, SDL_Window *window, SDL_PropertiesID create_props)
  1611. {
  1612. @autoreleasepool {
  1613. SDL3METAL_RenderData *data = NULL;
  1614. id<MTLDevice> mtldevice = nil;
  1615. SDL_MetalView view = NULL;
  1616. CAMetalLayer *layer = nil;
  1617. NSError *err = nil;
  1618. dispatch_data_t mtllibdata;
  1619. char *constantdata;
  1620. int maxtexsize, quadcount = UINT16_MAX / 4;
  1621. UInt16 *indexdata;
  1622. size_t indicessize = sizeof(UInt16) * quadcount * 6;
  1623. MTLSamplerDescriptor *samplerdesc;
  1624. id<MTLCommandQueue> mtlcmdqueue;
  1625. id<MTLLibrary> mtllibrary;
  1626. id<MTLBuffer> mtlbufconstantstaging, mtlbufquadindicesstaging, mtlbufconstants, mtlbufquadindices;
  1627. id<MTLCommandBuffer> cmdbuffer;
  1628. id<MTLBlitCommandEncoder> blitcmd;
  1629. bool scRGB_supported = false;
  1630. // Note: matrices are column major.
  1631. float identitytransform[16] = {
  1632. 1.0f,
  1633. 0.0f,
  1634. 0.0f,
  1635. 0.0f,
  1636. 0.0f,
  1637. 1.0f,
  1638. 0.0f,
  1639. 0.0f,
  1640. 0.0f,
  1641. 0.0f,
  1642. 1.0f,
  1643. 0.0f,
  1644. 0.0f,
  1645. 0.0f,
  1646. 0.0f,
  1647. 1.0f,
  1648. };
  1649. float halfpixeltransform[16] = {
  1650. 1.0f,
  1651. 0.0f,
  1652. 0.0f,
  1653. 0.0f,
  1654. 0.0f,
  1655. 1.0f,
  1656. 0.0f,
  1657. 0.0f,
  1658. 0.0f,
  1659. 0.0f,
  1660. 1.0f,
  1661. 0.0f,
  1662. 0.5f,
  1663. 0.5f,
  1664. 0.0f,
  1665. 1.0f,
  1666. };
  1667. const size_t YCbCr_shader_matrix_size = 4 * 4 * sizeof(float);
  1668. SDL_SetupRendererColorspace(renderer, create_props);
  1669. #ifndef SDL_PLATFORM_TVOS
  1670. if (@available(macos 10.11, iOS 16.0, *)) {
  1671. scRGB_supported = true;
  1672. }
  1673. #endif
  1674. if (renderer->output_colorspace != SDL_COLORSPACE_SRGB) {
  1675. if (renderer->output_colorspace == SDL_COLORSPACE_SRGB_LINEAR && scRGB_supported) {
  1676. // This colorspace is supported
  1677. } else {
  1678. return SDL_SetError("Unsupported output colorspace");
  1679. }
  1680. }
  1681. #ifdef SDL_PLATFORM_MACOS
  1682. if (SDL_GetHintBoolean(SDL_HINT_RENDER_METAL_PREFER_LOW_POWER_DEVICE, true)) {
  1683. NSArray<id<MTLDevice>> *devices = MTLCopyAllDevices();
  1684. for (id<MTLDevice> device in devices) {
  1685. if (device.isLowPower) {
  1686. mtldevice = device;
  1687. break;
  1688. }
  1689. }
  1690. }
  1691. #endif
  1692. if (mtldevice == nil) {
  1693. mtldevice = MTLCreateSystemDefaultDevice();
  1694. }
  1695. if (mtldevice == nil) {
  1696. return SDL_SetError("Failed to obtain Metal device");
  1697. }
  1698. view = GetWindowView(window);
  1699. if (view == nil) {
  1700. view = SDL_Metal_CreateView(window);
  1701. }
  1702. if (view == NULL) {
  1703. return false;
  1704. }
  1705. // !!! FIXME: error checking on all of this.
  1706. data = [[SDL3METAL_RenderData alloc] init];
  1707. if (data == nil) {
  1708. /* Release the metal view instead of destroying it,
  1709. in case we want to use it later (recreating the renderer)
  1710. */
  1711. // SDL_Metal_DestroyView(view);
  1712. CFBridgingRelease(view);
  1713. return SDL_SetError("SDL3METAL_RenderData alloc/init failed");
  1714. }
  1715. renderer->internal = (void *)CFBridgingRetain(data);
  1716. METAL_InvalidateCachedState(renderer);
  1717. renderer->window = window;
  1718. data.mtlview = view;
  1719. #ifdef SDL_PLATFORM_MACOS
  1720. layer = (CAMetalLayer *)[(__bridge NSView *)view layer];
  1721. #else
  1722. layer = (CAMetalLayer *)[(__bridge UIView *)view layer];
  1723. #endif
  1724. #ifndef SDL_PLATFORM_TVOS
  1725. if (renderer->output_colorspace == SDL_COLORSPACE_SRGB_LINEAR) {
  1726. if (@available(macos 10.11, iOS 16.0, *)) {
  1727. layer.wantsExtendedDynamicRangeContent = YES;
  1728. } else {
  1729. SDL_assert(!"Logic error, scRGB is not actually supported");
  1730. }
  1731. layer.pixelFormat = MTLPixelFormatRGBA16Float;
  1732. const CFStringRef name = kCGColorSpaceExtendedLinearSRGB;
  1733. CGColorSpaceRef colorspace = CGColorSpaceCreateWithName(name);
  1734. layer.colorspace = colorspace;
  1735. CGColorSpaceRelease(colorspace);
  1736. }
  1737. #endif // !SDL_PLATFORM_TVOS
  1738. layer.device = mtldevice;
  1739. // Necessary for RenderReadPixels.
  1740. layer.framebufferOnly = NO;
  1741. data.mtldevice = layer.device;
  1742. data.mtllayer = layer;
  1743. mtlcmdqueue = [data.mtldevice newCommandQueue];
  1744. data.mtlcmdqueue = mtlcmdqueue;
  1745. data.mtlcmdqueue.label = @"SDL Metal Renderer";
  1746. data.mtlpassdesc = [MTLRenderPassDescriptor renderPassDescriptor];
  1747. // The compiled .metallib is embedded in a static array in a header file
  1748. // but the original shader source code is in SDL_shaders_metal.metal.
  1749. mtllibdata = dispatch_data_create(sdl_metallib, sdl_metallib_len, dispatch_get_global_queue(0, 0), ^{
  1750. });
  1751. mtllibrary = [data.mtldevice newLibraryWithData:mtllibdata error:&err];
  1752. data.mtllibrary = mtllibrary;
  1753. SDL_assert(err == nil);
  1754. data.mtllibrary.label = @"SDL Metal renderer shader library";
  1755. // Do some shader pipeline state loading up-front rather than on demand.
  1756. data.pipelinescount = 0;
  1757. data.allpipelines = NULL;
  1758. ChooseShaderPipelines(data, MTLPixelFormatBGRA8Unorm);
  1759. static struct
  1760. {
  1761. MTLSamplerMinMagFilter filter;
  1762. MTLSamplerAddressMode address;
  1763. } samplerParams[] = {
  1764. { MTLSamplerMinMagFilterNearest, MTLSamplerAddressModeClampToEdge },
  1765. { MTLSamplerMinMagFilterNearest, MTLSamplerAddressModeRepeat },
  1766. { MTLSamplerMinMagFilterLinear, MTLSamplerAddressModeClampToEdge },
  1767. { MTLSamplerMinMagFilterLinear, MTLSamplerAddressModeRepeat },
  1768. };
  1769. SDL_COMPILE_TIME_ASSERT(samplerParams_SIZE, SDL_arraysize(samplerParams) == SDL_NUM_METAL_SAMPLERS);
  1770. data.mtlsamplers = [[NSMutableArray<id<MTLSamplerState>> alloc] init];
  1771. samplerdesc = [[MTLSamplerDescriptor alloc] init];
  1772. for (int i = 0; i < SDL_arraysize(samplerParams); ++i) {
  1773. samplerdesc.minFilter = samplerParams[i].filter;
  1774. samplerdesc.magFilter = samplerParams[i].filter;
  1775. samplerdesc.sAddressMode = samplerParams[i].address;
  1776. samplerdesc.tAddressMode = samplerParams[i].address;
  1777. [data.mtlsamplers addObject:[data.mtldevice newSamplerStateWithDescriptor:samplerdesc]];
  1778. }
  1779. mtlbufconstantstaging = [data.mtldevice newBufferWithLength:CONSTANTS_LENGTH options:MTLResourceStorageModeShared];
  1780. constantdata = [mtlbufconstantstaging contents];
  1781. SDL_memcpy(constantdata + CONSTANTS_OFFSET_IDENTITY, identitytransform, sizeof(identitytransform));
  1782. SDL_memcpy(constantdata + CONSTANTS_OFFSET_HALF_PIXEL_TRANSFORM, halfpixeltransform, sizeof(halfpixeltransform));
  1783. SDL_memcpy(constantdata + CONSTANTS_OFFSET_DECODE_BT601_LIMITED, SDL_GetYCbCRtoRGBConversionMatrix(SDL_COLORSPACE_BT601_LIMITED, 0, 0, 8), YCbCr_shader_matrix_size);
  1784. SDL_memcpy(constantdata + CONSTANTS_OFFSET_DECODE_BT601_FULL, SDL_GetYCbCRtoRGBConversionMatrix(SDL_COLORSPACE_BT601_FULL, 0, 0, 8), YCbCr_shader_matrix_size);
  1785. SDL_memcpy(constantdata + CONSTANTS_OFFSET_DECODE_BT709_LIMITED, SDL_GetYCbCRtoRGBConversionMatrix(SDL_COLORSPACE_BT709_LIMITED, 0, 0, 8), YCbCr_shader_matrix_size);
  1786. SDL_memcpy(constantdata + CONSTANTS_OFFSET_DECODE_BT709_FULL, SDL_GetYCbCRtoRGBConversionMatrix(SDL_COLORSPACE_BT709_FULL, 0, 0, 8), YCbCr_shader_matrix_size);
  1787. SDL_memcpy(constantdata + CONSTANTS_OFFSET_DECODE_BT2020_LIMITED, SDL_GetYCbCRtoRGBConversionMatrix(SDL_COLORSPACE_BT2020_LIMITED, 0, 0, 10), YCbCr_shader_matrix_size);
  1788. SDL_memcpy(constantdata + CONSTANTS_OFFSET_DECODE_BT2020_FULL, SDL_GetYCbCRtoRGBConversionMatrix(SDL_COLORSPACE_BT2020_FULL, 0, 0, 10), YCbCr_shader_matrix_size);
  1789. mtlbufquadindicesstaging = [data.mtldevice newBufferWithLength:indicessize options:MTLResourceStorageModeShared];
  1790. /* Quads in the following vertex order (matches the FillRects vertices):
  1791. * 1---3
  1792. * | \ |
  1793. * 0---2
  1794. */
  1795. indexdata = [mtlbufquadindicesstaging contents];
  1796. for (int i = 0; i < quadcount; i++) {
  1797. indexdata[i * 6 + 0] = i * 4 + 0;
  1798. indexdata[i * 6 + 1] = i * 4 + 1;
  1799. indexdata[i * 6 + 2] = i * 4 + 2;
  1800. indexdata[i * 6 + 3] = i * 4 + 2;
  1801. indexdata[i * 6 + 4] = i * 4 + 1;
  1802. indexdata[i * 6 + 5] = i * 4 + 3;
  1803. }
  1804. mtlbufconstants = [data.mtldevice newBufferWithLength:CONSTANTS_LENGTH options:MTLResourceStorageModePrivate];
  1805. data.mtlbufconstants = mtlbufconstants;
  1806. data.mtlbufconstants.label = @"SDL constant data";
  1807. mtlbufquadindices = [data.mtldevice newBufferWithLength:indicessize options:MTLResourceStorageModePrivate];
  1808. data.mtlbufquadindices = mtlbufquadindices;
  1809. data.mtlbufquadindices.label = @"SDL quad index buffer";
  1810. cmdbuffer = [data.mtlcmdqueue commandBuffer];
  1811. blitcmd = [cmdbuffer blitCommandEncoder];
  1812. [blitcmd copyFromBuffer:mtlbufconstantstaging sourceOffset:0 toBuffer:mtlbufconstants destinationOffset:0 size:CONSTANTS_LENGTH];
  1813. [blitcmd copyFromBuffer:mtlbufquadindicesstaging sourceOffset:0 toBuffer:mtlbufquadindices destinationOffset:0 size:indicessize];
  1814. [blitcmd endEncoding];
  1815. [cmdbuffer commit];
  1816. // !!! FIXME: force more clears here so all the drawables are sane to start, and our static buffers are definitely flushed.
  1817. renderer->WindowEvent = METAL_WindowEvent;
  1818. renderer->GetOutputSize = METAL_GetOutputSize;
  1819. renderer->SupportsBlendMode = METAL_SupportsBlendMode;
  1820. renderer->CreateTexture = METAL_CreateTexture;
  1821. renderer->UpdateTexture = METAL_UpdateTexture;
  1822. #ifdef SDL_HAVE_YUV
  1823. renderer->UpdateTextureYUV = METAL_UpdateTextureYUV;
  1824. renderer->UpdateTextureNV = METAL_UpdateTextureNV;
  1825. #endif
  1826. renderer->LockTexture = METAL_LockTexture;
  1827. renderer->UnlockTexture = METAL_UnlockTexture;
  1828. renderer->SetTextureScaleMode = METAL_SetTextureScaleMode;
  1829. renderer->SetRenderTarget = METAL_SetRenderTarget;
  1830. renderer->QueueSetViewport = METAL_QueueSetViewport;
  1831. renderer->QueueSetDrawColor = METAL_QueueNoOp;
  1832. renderer->QueueDrawPoints = METAL_QueueDrawPoints;
  1833. renderer->QueueDrawLines = METAL_QueueDrawLines;
  1834. renderer->QueueGeometry = METAL_QueueGeometry;
  1835. renderer->InvalidateCachedState = METAL_InvalidateCachedState;
  1836. renderer->RunCommandQueue = METAL_RunCommandQueue;
  1837. renderer->RenderReadPixels = METAL_RenderReadPixels;
  1838. renderer->RenderPresent = METAL_RenderPresent;
  1839. renderer->DestroyTexture = METAL_DestroyTexture;
  1840. renderer->DestroyRenderer = METAL_DestroyRenderer;
  1841. renderer->SetVSync = METAL_SetVSync;
  1842. renderer->GetMetalLayer = METAL_GetMetalLayer;
  1843. renderer->GetMetalCommandEncoder = METAL_GetMetalCommandEncoder;
  1844. renderer->name = METAL_RenderDriver.name;
  1845. SDL_AddSupportedTextureFormat(renderer, SDL_PIXELFORMAT_ARGB8888);
  1846. SDL_AddSupportedTextureFormat(renderer, SDL_PIXELFORMAT_ABGR8888);
  1847. SDL_AddSupportedTextureFormat(renderer, SDL_PIXELFORMAT_ABGR2101010);
  1848. SDL_AddSupportedTextureFormat(renderer, SDL_PIXELFORMAT_RGBA64_FLOAT);
  1849. SDL_AddSupportedTextureFormat(renderer, SDL_PIXELFORMAT_RGBA128_FLOAT);
  1850. SDL_AddSupportedTextureFormat(renderer, SDL_PIXELFORMAT_YV12);
  1851. SDL_AddSupportedTextureFormat(renderer, SDL_PIXELFORMAT_IYUV);
  1852. SDL_AddSupportedTextureFormat(renderer, SDL_PIXELFORMAT_NV12);
  1853. SDL_AddSupportedTextureFormat(renderer, SDL_PIXELFORMAT_NV21);
  1854. SDL_AddSupportedTextureFormat(renderer, SDL_PIXELFORMAT_P010);
  1855. #if defined(SDL_PLATFORM_MACOS) || TARGET_OS_MACCATALYST
  1856. data.mtllayer.displaySyncEnabled = NO;
  1857. #endif
  1858. // https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf
  1859. maxtexsize = 4096;
  1860. #if defined(SDL_PLATFORM_MACOS) || TARGET_OS_MACCATALYST
  1861. maxtexsize = 16384;
  1862. #elif defined(SDL_PLATFORM_TVOS)
  1863. maxtexsize = 8192;
  1864. if ([mtldevice supportsFeatureSet:MTLFeatureSet_tvOS_GPUFamily2_v1]) {
  1865. maxtexsize = 16384;
  1866. }
  1867. #else
  1868. if ([mtldevice supportsFeatureSet:MTLFeatureSet_iOS_GPUFamily4_v1]) {
  1869. maxtexsize = 16384;
  1870. } else if ([mtldevice supportsFeatureSet:MTLFeatureSet_iOS_GPUFamily3_v1]) {
  1871. maxtexsize = 16384;
  1872. } else if ([mtldevice supportsFeatureSet:MTLFeatureSet_iOS_GPUFamily2_v2] || [mtldevice supportsFeatureSet:MTLFeatureSet_iOS_GPUFamily1_v2]) {
  1873. maxtexsize = 8192;
  1874. } else {
  1875. maxtexsize = 4096;
  1876. }
  1877. #endif
  1878. SDL_SetNumberProperty(SDL_GetRendererProperties(renderer), SDL_PROP_RENDERER_MAX_TEXTURE_SIZE_NUMBER, maxtexsize);
  1879. return true;
  1880. }
  1881. }
  1882. SDL_RenderDriver METAL_RenderDriver = {
  1883. METAL_CreateRenderer, "metal"
  1884. };
  1885. #endif // SDL_VIDEO_RENDER_METAL