/
SDL_render_metal.m
1543 lines (1317 loc) · 57 KB
1
2
/*
Simple DirectMedia Layer
3
Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "../../SDL_internal.h"
#if SDL_VIDEO_RENDER_METAL && !SDL_RENDER_DISABLED
#include "SDL_hints.h"
#include "SDL_log.h"
#include "SDL_assert.h"
#include "SDL_syswm.h"
#include "../SDL_sysrender.h"
31
#ifdef __MACOSX__
32
#include "../../video/cocoa/SDL_cocoametalview.h"
33
34
35
#else
#include "../../video/uikit/SDL_uikitmetalview.h"
#endif
36
#include <Availability.h>
37
38
#import <Metal/Metal.h>
#import <QuartzCore/CAMetalLayer.h>
39
40
41
42
43
44
45
/* Regenerate these with build-metal-shaders.sh */
#ifdef __MACOSX__
#include "SDL_shaders_metal_osx.h"
#else
#include "SDL_shaders_metal_ios.h"
#endif
46
47
48
/* Apple Metal renderer implementation */
49
50
51
52
53
54
55
56
57
/* macOS requires constants in a buffer to have a 256 byte alignment. */
#ifdef __MACOSX__
#define CONSTANT_ALIGN 256
#else
#define CONSTANT_ALIGN 4
#endif
#define ALIGN_CONSTANTS(size) ((size + CONSTANT_ALIGN - 1) & (~(CONSTANT_ALIGN - 1)))
58
static const size_t CONSTANTS_OFFSET_INVALID = 0xFFFFFFFF;
59
60
static const size_t CONSTANTS_OFFSET_IDENTITY = 0;
static const size_t CONSTANTS_OFFSET_HALF_PIXEL_TRANSFORM = ALIGN_CONSTANTS(CONSTANTS_OFFSET_IDENTITY + sizeof(float) * 16);
61
62
63
static const size_t CONSTANTS_OFFSET_DECODE_JPEG = ALIGN_CONSTANTS(CONSTANTS_OFFSET_HALF_PIXEL_TRANSFORM + sizeof(float) * 16);
static const size_t CONSTANTS_OFFSET_DECODE_BT601 = ALIGN_CONSTANTS(CONSTANTS_OFFSET_DECODE_JPEG + sizeof(float) * 4 * 4);
static const size_t CONSTANTS_OFFSET_DECODE_BT709 = ALIGN_CONSTANTS(CONSTANTS_OFFSET_DECODE_BT601 + sizeof(float) * 4 * 4);
64
static const size_t CONSTANTS_LENGTH = CONSTANTS_OFFSET_DECODE_BT709 + sizeof(float) * 4 * 4;
65
66
67
68
69
70
71
72
73
typedef enum SDL_MetalVertexFunction
{
SDL_METAL_VERTEX_SOLID,
SDL_METAL_VERTEX_COPY,
} SDL_MetalVertexFunction;
typedef enum SDL_MetalFragmentFunction
{
74
SDL_METAL_FRAGMENT_SOLID = 0,
75
SDL_METAL_FRAGMENT_COPY,
76
77
78
79
SDL_METAL_FRAGMENT_YUV,
SDL_METAL_FRAGMENT_NV12,
SDL_METAL_FRAGMENT_NV21,
SDL_METAL_FRAGMENT_COUNT,
80
81
82
83
84
85
86
87
88
89
90
91
92
93
} SDL_MetalFragmentFunction;
typedef struct METAL_PipelineState
{
SDL_BlendMode blendMode;
void *pipe;
} METAL_PipelineState;
typedef struct METAL_PipelineCache
{
METAL_PipelineState *states;
int count;
SDL_MetalVertexFunction vertexFunction;
SDL_MetalFragmentFunction fragmentFunction;
94
MTLPixelFormat renderTargetFormat;
95
96
97
const char *label;
} METAL_PipelineCache;
98
/* Each shader combination used by drawing functions has a separate pipeline
99
100
101
102
103
* cache, and we have a separate list of caches for each render target pixel
* format. This is more efficient than iterating over a global cache to find
* the pipeline based on the specified shader combination and RT pixel format,
* since we know what the RT pixel format is when we set the render target, and
* we know what the shader combination is inside each drawing function's code. */
104
105
typedef struct METAL_ShaderPipelines
{
106
MTLPixelFormat renderTargetFormat;
107
108
109
METAL_PipelineCache caches[SDL_METAL_FRAGMENT_COUNT];
} METAL_ShaderPipelines;
110
@interface METAL_RenderData : NSObject
111
112
113
114
115
116
@property (nonatomic, retain) id<MTLDevice> mtldevice;
@property (nonatomic, retain) id<MTLCommandQueue> mtlcmdqueue;
@property (nonatomic, retain) id<MTLCommandBuffer> mtlcmdbuffer;
@property (nonatomic, retain) id<MTLRenderCommandEncoder> mtlcmdencoder;
@property (nonatomic, retain) id<MTLLibrary> mtllibrary;
@property (nonatomic, retain) id<CAMetalDrawable> mtlbackbuffer;
117
118
@property (nonatomic, retain) id<MTLSamplerState> mtlsamplernearest;
@property (nonatomic, retain) id<MTLSamplerState> mtlsamplerlinear;
119
@property (nonatomic, retain) id<MTLBuffer> mtlbufconstants;
120
121
@property (nonatomic, retain) CAMetalLayer *mtllayer;
@property (nonatomic, retain) MTLRenderPassDescriptor *mtlpassdesc;
122
123
124
@property (nonatomic, assign) METAL_ShaderPipelines *activepipelines;
@property (nonatomic, assign) METAL_ShaderPipelines *allpipelines;
@property (nonatomic, assign) int pipelinescount;
125
126
127
@end
@implementation METAL_RenderData
128
#if !__has_feature(objc_arc)
129
130
131
132
133
134
135
136
137
138
- (void)dealloc
{
[_mtldevice release];
[_mtlcmdqueue release];
[_mtlcmdbuffer release];
[_mtlcmdencoder release];
[_mtllibrary release];
[_mtlbackbuffer release];
[_mtlsamplernearest release];
[_mtlsamplerlinear release];
139
[_mtlbufconstants release];
140
141
142
143
144
[_mtllayer release];
[_mtlpassdesc release];
[super dealloc];
}
#endif
145
@end
146
147
148
@interface METAL_TextureData : NSObject
@property (nonatomic, retain) id<MTLTexture> mtltexture;
149
@property (nonatomic, retain) id<MTLTexture> mtltexture_uv;
150
@property (nonatomic, retain) id<MTLSamplerState> mtlsampler;
151
152
153
154
@property (nonatomic, assign) SDL_MetalFragmentFunction fragmentFunction;
@property (nonatomic, assign) BOOL yuv;
@property (nonatomic, assign) BOOL nv12;
@property (nonatomic, assign) size_t conversionBufferOffset;
155
156
157
@end
@implementation METAL_TextureData
158
#if !__has_feature(objc_arc)
159
160
161
- (void)dealloc
{
[_mtltexture release];
162
[_mtltexture_uv release];
163
164
165
166
[_mtlsampler release];
[super dealloc];
}
#endif
167
168
@end
169
170
171
static int
IsMetalAvailable(const SDL_SysWMinfo *syswm)
{
172
173
if (syswm->subsystem != SDL_SYSWM_COCOA && syswm->subsystem != SDL_SYSWM_UIKIT) {
return SDL_SetError("Metal render target only supports Cocoa and UIKit video targets at the moment.");
174
175
176
}
// this checks a weak symbol.
177
#if (defined(__MACOSX__) && (MAC_OS_X_VERSION_MIN_REQUIRED < 101100))
178
179
180
181
182
183
184
185
if (MTLCreateSystemDefaultDevice == NULL) { // probably on 10.10 or lower.
return SDL_SetError("Metal framework not available on this system");
}
#endif
return 0;
}
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
static const MTLBlendOperation invalidBlendOperation = (MTLBlendOperation)0xFFFFFFFF;
static const MTLBlendFactor invalidBlendFactor = (MTLBlendFactor)0xFFFFFFFF;
static MTLBlendOperation
GetBlendOperation(SDL_BlendOperation operation)
{
switch (operation) {
case SDL_BLENDOPERATION_ADD: return MTLBlendOperationAdd;
case SDL_BLENDOPERATION_SUBTRACT: return MTLBlendOperationSubtract;
case SDL_BLENDOPERATION_REV_SUBTRACT: return MTLBlendOperationReverseSubtract;
case SDL_BLENDOPERATION_MINIMUM: return MTLBlendOperationMin;
case SDL_BLENDOPERATION_MAXIMUM: return MTLBlendOperationMax;
default: return invalidBlendOperation;
}
}
static MTLBlendFactor
GetBlendFactor(SDL_BlendFactor factor)
{
switch (factor) {
case SDL_BLENDFACTOR_ZERO: return MTLBlendFactorZero;
case SDL_BLENDFACTOR_ONE: return MTLBlendFactorOne;
case SDL_BLENDFACTOR_SRC_COLOR: return MTLBlendFactorSourceColor;
case SDL_BLENDFACTOR_ONE_MINUS_SRC_COLOR: return MTLBlendFactorOneMinusSourceColor;
case SDL_BLENDFACTOR_SRC_ALPHA: return MTLBlendFactorSourceAlpha;
case SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA: return MTLBlendFactorOneMinusSourceAlpha;
case SDL_BLENDFACTOR_DST_COLOR: return MTLBlendFactorDestinationColor;
case SDL_BLENDFACTOR_ONE_MINUS_DST_COLOR: return MTLBlendFactorOneMinusDestinationColor;
case SDL_BLENDFACTOR_DST_ALPHA: return MTLBlendFactorDestinationAlpha;
case SDL_BLENDFACTOR_ONE_MINUS_DST_ALPHA: return MTLBlendFactorOneMinusDestinationAlpha;
default: return invalidBlendFactor;
}
}
static NSString *
GetVertexFunctionName(SDL_MetalVertexFunction function)
{
switch (function) {
case SDL_METAL_VERTEX_SOLID: return @"SDL_Solid_vertex";
case SDL_METAL_VERTEX_COPY: return @"SDL_Copy_vertex";
default: return nil;
}
}
static NSString *
GetFragmentFunctionName(SDL_MetalFragmentFunction function)
{
switch (function) {
case SDL_METAL_FRAGMENT_SOLID: return @"SDL_Solid_fragment";
235
case SDL_METAL_FRAGMENT_COPY: return @"SDL_Copy_fragment";
236
237
238
case SDL_METAL_FRAGMENT_YUV: return @"SDL_YUV_fragment";
case SDL_METAL_FRAGMENT_NV12: return @"SDL_NV12_fragment";
case SDL_METAL_FRAGMENT_NV21: return @"SDL_NV21_fragment";
239
240
241
242
default: return nil;
}
}
243
static id<MTLRenderPipelineState>
244
245
MakePipelineState(METAL_RenderData *data, METAL_PipelineCache *cache,
NSString *blendlabel, SDL_BlendMode blendmode)
246
{
247
248
id<MTLFunction> mtlvertfn = [data.mtllibrary newFunctionWithName:GetVertexFunctionName(cache->vertexFunction)];
id<MTLFunction> mtlfragfn = [data.mtllibrary newFunctionWithName:GetFragmentFunctionName(cache->fragmentFunction)];
249
250
251
252
253
254
SDL_assert(mtlvertfn != nil);
SDL_assert(mtlfragfn != nil);
MTLRenderPipelineDescriptor *mtlpipedesc = [[MTLRenderPipelineDescriptor alloc] init];
mtlpipedesc.vertexFunction = mtlvertfn;
mtlpipedesc.fragmentFunction = mtlfragfn;
255
256
257
MTLRenderPipelineColorAttachmentDescriptor *rtdesc = mtlpipedesc.colorAttachments[0];
258
rtdesc.pixelFormat = cache->renderTargetFormat;
259
260
261
262
263
264
265
266
267
268
269
270
271
272
if (blendmode != SDL_BLENDMODE_NONE) {
rtdesc.blendingEnabled = YES;
rtdesc.sourceRGBBlendFactor = GetBlendFactor(SDL_GetBlendModeSrcColorFactor(blendmode));
rtdesc.destinationRGBBlendFactor = GetBlendFactor(SDL_GetBlendModeDstColorFactor(blendmode));
rtdesc.rgbBlendOperation = GetBlendOperation(SDL_GetBlendModeColorOperation(blendmode));
rtdesc.sourceAlphaBlendFactor = GetBlendFactor(SDL_GetBlendModeSrcAlphaFactor(blendmode));
rtdesc.destinationAlphaBlendFactor = GetBlendFactor(SDL_GetBlendModeDstAlphaFactor(blendmode));
rtdesc.alphaBlendOperation = GetBlendOperation(SDL_GetBlendModeAlphaOperation(blendmode));
} else {
rtdesc.blendingEnabled = NO;
}
mtlpipedesc.label = [@(cache->label) stringByAppendingString:blendlabel];
273
274
NSError *err = nil;
275
id<MTLRenderPipelineState> state = [data.mtldevice newRenderPipelineStateWithDescriptor:mtlpipedesc error:&err];
276
SDL_assert(err == nil);
277
278
279
280
281
282
283
METAL_PipelineState pipeline;
pipeline.blendMode = blendmode;
pipeline.pipe = (void *)CFBridgingRetain(state);
METAL_PipelineState *states = SDL_realloc(cache->states, (cache->count + 1) * sizeof(pipeline));
284
#if !__has_feature(objc_arc)
285
286
287
[mtlpipedesc release]; // !!! FIXME: can these be reused for each creation, or does the pipeline obtain it?
[mtlvertfn release];
[mtlfragfn release];
288
[state release];
289
#endif
290
291
292
293
294
295
296
297
298
299
300
301
if (states) {
states[cache->count++] = pipeline;
cache->states = states;
return (__bridge id<MTLRenderPipelineState>)pipeline.pipe;
} else {
CFBridgingRelease(pipeline.pipe);
SDL_OutOfMemory();
return NULL;
}
}
302
static void
303
304
MakePipelineCache(METAL_RenderData *data, METAL_PipelineCache *cache, const char *label,
MTLPixelFormat rtformat, SDL_MetalVertexFunction vertfn, SDL_MetalFragmentFunction fragfn)
305
306
307
308
309
{
SDL_zerop(cache);
cache->vertexFunction = vertfn;
cache->fragmentFunction = fragfn;
310
cache->renderTargetFormat = rtformat;
311
312
313
314
cache->label = label;
/* Create pipeline states for the default blend modes. Custom blend modes
* will be added to the cache on-demand. */
315
316
317
318
MakePipelineState(data, cache, @" (blend=none)", SDL_BLENDMODE_NONE);
MakePipelineState(data, cache, @" (blend=blend)", SDL_BLENDMODE_BLEND);
MakePipelineState(data, cache, @" (blend=add)", SDL_BLENDMODE_ADD);
MakePipelineState(data, cache, @" (blend=mod)", SDL_BLENDMODE_MOD);
319
320
321
}
static void
322
DestroyPipelineCache(METAL_PipelineCache *cache)
323
{
324
325
326
327
328
329
if (cache != NULL) {
for (int i = 0; i < cache->count; i++) {
CFBridgingRelease(cache->states[i].pipe);
}
SDL_free(cache->states);
330
331
332
}
}
333
334
335
336
337
338
339
340
341
342
343
344
345
346
void
MakeShaderPipelines(METAL_RenderData *data, METAL_ShaderPipelines *pipelines, MTLPixelFormat rtformat)
{
SDL_zerop(pipelines);
pipelines->renderTargetFormat = rtformat;
MakePipelineCache(data, &pipelines->caches[SDL_METAL_FRAGMENT_SOLID], "SDL primitives pipeline", rtformat, SDL_METAL_VERTEX_SOLID, SDL_METAL_FRAGMENT_SOLID);
MakePipelineCache(data, &pipelines->caches[SDL_METAL_FRAGMENT_COPY], "SDL copy pipeline", rtformat, SDL_METAL_VERTEX_COPY, SDL_METAL_FRAGMENT_COPY);
MakePipelineCache(data, &pipelines->caches[SDL_METAL_FRAGMENT_YUV], "SDL YUV pipeline", rtformat, SDL_METAL_VERTEX_COPY, SDL_METAL_FRAGMENT_YUV);
MakePipelineCache(data, &pipelines->caches[SDL_METAL_FRAGMENT_NV12], "SDL NV12 pipeline", rtformat, SDL_METAL_VERTEX_COPY, SDL_METAL_FRAGMENT_NV12);
MakePipelineCache(data, &pipelines->caches[SDL_METAL_FRAGMENT_NV21], "SDL NV21 pipeline", rtformat, SDL_METAL_VERTEX_COPY, SDL_METAL_FRAGMENT_NV21);
}
347
static METAL_ShaderPipelines *
348
ChooseShaderPipelines(METAL_RenderData *data, MTLPixelFormat rtformat)
349
{
350
351
352
353
354
355
356
357
358
359
360
361
METAL_ShaderPipelines *allpipelines = data.allpipelines;
int count = data.pipelinescount;
for (int i = 0; i < count; i++) {
if (allpipelines[i].renderTargetFormat == rtformat) {
return &allpipelines[i];
}
}
allpipelines = SDL_realloc(allpipelines, (count + 1) * sizeof(METAL_ShaderPipelines));
if (allpipelines == NULL) {
362
363
364
365
SDL_OutOfMemory();
return NULL;
}
366
367
368
369
MakeShaderPipelines(data, &allpipelines[count], rtformat);
data.allpipelines = allpipelines;
data.pipelinescount = count + 1;
370
371
return &data.allpipelines[count];
372
373
374
}
static void
375
DestroyAllPipelines(METAL_ShaderPipelines *allpipelines, int count)
376
{
377
378
379
380
381
if (allpipelines != NULL) {
for (int i = 0; i < count; i++) {
for (int cache = 0; cache < SDL_METAL_FRAGMENT_COUNT; cache++) {
DestroyPipelineCache(&allpipelines[i].caches[cache]);
}
382
383
}
384
SDL_free(allpipelines);
385
}
386
387
388
}
static inline id<MTLRenderPipelineState>
389
ChoosePipelineState(METAL_RenderData *data, METAL_ShaderPipelines *pipelines, SDL_MetalFragmentFunction fragfn, SDL_BlendMode blendmode)
390
{
391
392
METAL_PipelineCache *cache = &pipelines->caches[fragfn];
393
394
395
396
for (int i = 0; i < cache->count; i++) {
if (cache->states[i].blendMode == blendmode) {
return (__bridge id<MTLRenderPipelineState>)cache->states[i].pipe;
}
397
}
398
399
return MakePipelineState(data, cache, [NSString stringWithFormat:@" (blend=custom 0x%x)", blendmode], blendmode);
400
401
}
402
403
404
405
static void
METAL_ActivateRenderCommandEncoder(SDL_Renderer * renderer, MTLLoadAction load, MTLClearColor *clear_color)
{
METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
406
407
408
409
410
/* Our SetRenderTarget just signals that the next render operation should
* set up a new render pass. This is where that work happens. */
if (data.mtlcmdencoder == nil) {
id<MTLTexture> mtltexture = nil;
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
if (renderer->target != NULL) {
METAL_TextureData *texdata = (__bridge METAL_TextureData *)renderer->target->driverdata;
mtltexture = texdata.mtltexture;
} else {
if (data.mtlbackbuffer == nil) {
/* The backbuffer's contents aren't guaranteed to persist after
* presenting, so we can leave it undefined when loading it. */
data.mtlbackbuffer = [data.mtllayer nextDrawable];
if (load == MTLLoadActionLoad) {
load = MTLLoadActionDontCare;
}
}
mtltexture = data.mtlbackbuffer.texture;
}
426
427
SDL_assert(mtltexture);
428
429
430
431
432
if (load == MTLLoadActionClear) {
SDL_assert(clear_color != NULL);
data.mtlpassdesc.colorAttachments[0].clearColor = *clear_color;
}
433
434
435
data.mtlpassdesc.colorAttachments[0].loadAction = load;
data.mtlpassdesc.colorAttachments[0].texture = mtltexture;
436
437
438
data.mtlcmdbuffer = [data.mtlcmdqueue commandBuffer];
data.mtlcmdencoder = [data.mtlcmdbuffer renderCommandEncoderWithDescriptor:data.mtlpassdesc];
439
440
441
442
443
444
if (data.mtlbackbuffer != nil && mtltexture == data.mtlbackbuffer.texture) {
data.mtlcmdencoder.label = @"SDL metal renderer backbuffer";
} else {
data.mtlcmdencoder.label = @"SDL metal renderer render target";
}
445
446
data.activepipelines = ChooseShaderPipelines(data, mtltexture.pixelFormat);
447
448
449
450
451
452
453
// make sure this has a definite place in the queue. This way it will
// execute reliably whether the app tries to make its own command buffers
// or whatever. This means we can _always_ batch rendering commands!
[data.mtlcmdbuffer enqueue];
}
}
454
455
456
457
458
459
460
461
462
static void
METAL_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
{
if (event->event == SDL_WINDOWEVENT_SHOWN ||
event->event == SDL_WINDOWEVENT_HIDDEN) {
// !!! FIXME: write me
}
}
463
464
465
466
467
468
469
470
471
472
473
474
475
static int
METAL_GetOutputSize(SDL_Renderer * renderer, int *w, int *h)
{ @autoreleasepool {
METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
if (w) {
*w = (int)data.mtllayer.drawableSize.width;
}
if (h) {
*h = (int)data.mtllayer.drawableSize.height;
}
return 0;
}}
476
477
478
479
480
481
482
483
484
485
static SDL_bool
METAL_SupportsBlendMode(SDL_Renderer * renderer, SDL_BlendMode blendMode)
{
SDL_BlendFactor srcColorFactor = SDL_GetBlendModeSrcColorFactor(blendMode);
SDL_BlendFactor srcAlphaFactor = SDL_GetBlendModeSrcAlphaFactor(blendMode);
SDL_BlendOperation colorOperation = SDL_GetBlendModeColorOperation(blendMode);
SDL_BlendFactor dstColorFactor = SDL_GetBlendModeDstColorFactor(blendMode);
SDL_BlendFactor dstAlphaFactor = SDL_GetBlendModeDstAlphaFactor(blendMode);
SDL_BlendOperation alphaOperation = SDL_GetBlendModeAlphaOperation(blendMode);
486
487
488
489
490
491
492
493
494
495
496
if (GetBlendFactor(srcColorFactor) == invalidBlendFactor ||
GetBlendFactor(srcAlphaFactor) == invalidBlendFactor ||
GetBlendOperation(colorOperation) == invalidBlendOperation ||
GetBlendFactor(dstColorFactor) == invalidBlendFactor ||
GetBlendFactor(dstAlphaFactor) == invalidBlendFactor ||
GetBlendOperation(alphaOperation) == invalidBlendOperation) {
return SDL_FALSE;
}
return SDL_TRUE;
}
497
498
499
500
501
502
static int
METAL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
{ @autoreleasepool {
METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
MTLPixelFormat pixfmt;
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
switch (texture->format) {
case SDL_PIXELFORMAT_ABGR8888:
pixfmt = MTLPixelFormatRGBA8Unorm;
break;
case SDL_PIXELFORMAT_ARGB8888:
pixfmt = MTLPixelFormatBGRA8Unorm;
break;
case SDL_PIXELFORMAT_IYUV:
case SDL_PIXELFORMAT_YV12:
case SDL_PIXELFORMAT_NV12:
case SDL_PIXELFORMAT_NV21:
pixfmt = MTLPixelFormatR8Unorm;
break;
default:
return SDL_SetError("Texture format %s not supported by Metal", SDL_GetPixelFormatName(texture->format));
}
520
521
522
MTLTextureDescriptor *mtltexdesc = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:pixfmt
width:(NSUInteger)texture->w height:(NSUInteger)texture->h mipmapped:NO];
523
524
525
526
527
528
529
530
531
532
533
534
535
536
/* Not available in iOS 8. */
if ([mtltexdesc respondsToSelector:@selector(usage)]) {
if (texture->access == SDL_TEXTUREACCESS_TARGET) {
mtltexdesc.usage = MTLTextureUsageShaderRead | MTLTextureUsageRenderTarget;
} else {
mtltexdesc.usage = MTLTextureUsageShaderRead;
}
}
id<MTLTexture> mtltexture = [data.mtldevice newTextureWithDescriptor:mtltexdesc];
if (mtltexture == nil) {
return SDL_SetError("Texture allocation failed");
}
537
538
id<MTLTexture> mtltexture_uv = nil;
539
540
541
BOOL yuv = (texture->format == SDL_PIXELFORMAT_IYUV) || (texture->format == SDL_PIXELFORMAT_YV12);
BOOL nv12 = (texture->format == SDL_PIXELFORMAT_NV12) || (texture->format == SDL_PIXELFORMAT_NV21);
542
543
544
545
546
547
548
549
550
551
552
if (yuv) {
mtltexdesc.pixelFormat = MTLPixelFormatR8Unorm;
mtltexdesc.width = (texture->w + 1) / 2;
mtltexdesc.height = (texture->h + 1) / 2;
mtltexdesc.textureType = MTLTextureType2DArray;
mtltexdesc.arrayLength = 2;
} else if (nv12) {
mtltexdesc.pixelFormat = MTLPixelFormatRG8Unorm;
mtltexdesc.width = (texture->w + 1) / 2;
mtltexdesc.height = (texture->h + 1) / 2;
553
554
}
555
if (yuv || nv12) {
556
mtltexture_uv = [data.mtldevice newTextureWithDescriptor:mtltexdesc];
557
558
559
560
561
562
if (mtltexture_uv == nil) {
#if !__has_feature(objc_arc)
[mtltexture release];
#endif
return SDL_SetError("Texture allocation failed");
}
563
}
564
565
566
567
568
569
570
571
572
METAL_TextureData *texturedata = [[METAL_TextureData alloc] init];
if (texture->scaleMode == SDL_ScaleModeNearest) {
texturedata.mtlsampler = data.mtlsamplernearest;
} else {
texturedata.mtlsampler = data.mtlsamplerlinear;
}
texturedata.mtltexture = mtltexture;
texturedata.mtltexture_uv = mtltexture_uv;
573
574
575
texturedata.yuv = yuv;
texturedata.nv12 = nv12;
576
577
578
579
580
581
582
583
584
585
if (yuv) {
texturedata.fragmentFunction = SDL_METAL_FRAGMENT_YUV;
} else if (texture->format == SDL_PIXELFORMAT_NV12) {
texturedata.fragmentFunction = SDL_METAL_FRAGMENT_NV12;
} else if (texture->format == SDL_PIXELFORMAT_NV21) {
texturedata.fragmentFunction = SDL_METAL_FRAGMENT_NV21;
} else {
texturedata.fragmentFunction = SDL_METAL_FRAGMENT_COPY;
}
586
587
588
589
590
591
592
593
594
595
596
597
if (yuv || nv12) {
size_t offset = 0;
SDL_YUV_CONVERSION_MODE mode = SDL_GetYUVConversionModeForResolution(texture->w, texture->h);
switch (mode) {
case SDL_YUV_CONVERSION_JPEG: offset = CONSTANTS_OFFSET_DECODE_JPEG; break;
case SDL_YUV_CONVERSION_BT601: offset = CONSTANTS_OFFSET_DECODE_BT601; break;
case SDL_YUV_CONVERSION_BT709: offset = CONSTANTS_OFFSET_DECODE_BT709; break;
default: offset = 0; break;
}
texturedata.conversionBufferOffset = offset;
}
598
599
texture->driverdata = (void*)CFBridgingRetain(texturedata);
600
601
602
603
604
605
#if !__has_feature(objc_arc)
[texturedata release];
[mtltexture release];
[mtltexture_uv release];
#endif
606
607
608
return 0;
}}
609
610
611
612
613
614
static int
METAL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, const void *pixels, int pitch)
{ @autoreleasepool {
METAL_TextureData *texturedata = (__bridge METAL_TextureData *)texture->driverdata;
615
616
617
618
619
620
621
622
/* !!! FIXME: replaceRegion does not do any synchronization, so it might
* !!! FIXME: stomp on a previous frame's data that's currently being read
* !!! FIXME: by the GPU. */
[texturedata.mtltexture replaceRegion:MTLRegionMake2D(rect->x, rect->y, rect->w, rect->h)
mipmapLevel:0
withBytes:pixels
bytesPerRow:pitch];
623
624
625
626
if (texturedata.yuv) {
int Uslice = texture->format == SDL_PIXELFORMAT_YV12 ? 1 : 0;
int Vslice = texture->format == SDL_PIXELFORMAT_YV12 ? 0 : 1;
627
628
629
630
631
632
633
634
635
/* Skip to the correct offset into the next texture */
pixels = (const void*)((const Uint8*)pixels + rect->h * pitch);
[texturedata.mtltexture_uv replaceRegion:MTLRegionMake2D(rect->x / 2, rect->y / 2, (rect->w + 1) / 2, (rect->h + 1) / 2)
mipmapLevel:0
slice:Uslice
withBytes:pixels
bytesPerRow:(pitch + 1) / 2
bytesPerImage:0];
636
637
638
639
640
641
642
643
644
645
/* Skip to the correct offset into the next texture */
pixels = (const void*)((const Uint8*)pixels + ((rect->h + 1) / 2) * ((pitch + 1)/2));
[texturedata.mtltexture_uv replaceRegion:MTLRegionMake2D(rect->x / 2, rect->y / 2, (rect->w + 1) / 2, (rect->h + 1) / 2)
mipmapLevel:0
slice:Vslice
withBytes:pixels
bytesPerRow:(pitch + 1) / 2
bytesPerImage:0];
}
646
647
648
649
650
651
652
653
654
655
656
if (texturedata.nv12) {
/* Skip to the correct offset into the next texture */
pixels = (const void*)((const Uint8*)pixels + rect->h * pitch);
[texturedata.mtltexture_uv replaceRegion:MTLRegionMake2D(rect->x / 2, rect->y / 2, (rect->w + 1) / 2, (rect->h + 1) / 2)
mipmapLevel:0
slice:0
withBytes:pixels
bytesPerRow:2 * ((pitch + 1) / 2)
bytesPerImage:0];
}
657
658
659
return 0;
}}
660
661
662
663
664
665
666
667
668
669
670
static int
METAL_UpdateTextureYUV(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect,
const Uint8 *Yplane, int Ypitch,
const Uint8 *Uplane, int Upitch,
const Uint8 *Vplane, int Vpitch)
{ @autoreleasepool {
METAL_TextureData *texturedata = (__bridge METAL_TextureData *)texture->driverdata;
const int Uslice = 0;
const int Vslice = 1;
671
672
673
674
/* Bail out if we're supposed to update an empty rectangle */
if (rect->w <= 0 || rect->h <= 0) {
return 0;
675
676
}
677
678
679
680
[texturedata.mtltexture replaceRegion:MTLRegionMake2D(rect->x, rect->y, rect->w, rect->h)
mipmapLevel:0
withBytes:Yplane
bytesPerRow:Ypitch];
681
682
683
684
685
686
687
[texturedata.mtltexture_uv replaceRegion:MTLRegionMake2D(rect->x / 2, rect->y / 2, (rect->w + 1) / 2, (rect->h + 1) / 2)
mipmapLevel:0
slice:Uslice
withBytes:Uplane
bytesPerRow:Upitch
bytesPerImage:0];
688
689
690
691
692
693
694
695
696
[texturedata.mtltexture_uv replaceRegion:MTLRegionMake2D(rect->x / 2, rect->y / 2, (rect->w + 1) / 2, (rect->h + 1) / 2)
mipmapLevel:0
slice:Vslice
withBytes:Vplane
bytesPerRow:Vpitch
bytesPerImage:0];
return 0;
697
}}
698
699
700
701
702
703
704
705
static int
METAL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, void **pixels, int *pitch)
{
return SDL_Unsupported(); // !!! FIXME: write me
}
706
static void
707
METAL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
708
{
709
710
// !!! FIXME: write me
}
711
712
713
714
715
static int
METAL_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture)
{ @autoreleasepool {
METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
716
717
718
719
720
721
if (data.mtlcmdencoder) {
/* End encoding for the previous render target so we can set up a new
* render pass for this one. */
[data.mtlcmdencoder endEncoding];
[data.mtlcmdbuffer commit];
722
723
724
725
data.mtlcmdencoder = nil;
data.mtlcmdbuffer = nil;
}
726
727
728
729
730
731
/* We don't begin a new render pass right away - we delay it until an actual
* draw or clear happens. That way we can use hardware clears when possible,
* which are only available when beginning a new render pass. */
return 0;
}}
732
733
734
735
736
737
738
// normalize a value from 0.0f to len into 0.0f to 1.0f.
static inline float
normtex(const float _val, const float len)
{
return _val / len;
739
740
}
741
742
static int
METAL_QueueSetViewport(SDL_Renderer * renderer, SDL_RenderCommand *cmd)
743
{
744
745
746
747
748
749
750
float projection[4][4]; /* Prepare an orthographic projection */
const int w = cmd->data.viewport.rect.w;
const int h = cmd->data.viewport.rect.h;
const size_t matrixlen = sizeof (projection);
float *matrix = (float *) SDL_AllocateRenderVertices(renderer, matrixlen, CONSTANT_ALIGN, &cmd->data.viewport.first);
if (!matrix) {
return -1;
751
752
}
753
754
755
756
757
758
759
SDL_memset(projection, '\0', matrixlen);
if (w && h) {
projection[0][0] = 2.0f / w;
projection[1][1] = -2.0f / h;
projection[3][0] = -1.0f;
projection[3][1] = 1.0f;
projection[3][3] = 1.0f;
760
}
761
762
763
SDL_memcpy(matrix, projection, matrixlen);
return 0;
764
765
766
}
static int
767
768
769
770
771
772
METAL_QueueSetDrawColor(SDL_Renderer *renderer, SDL_RenderCommand *cmd)
{
const size_t vertlen = sizeof (float) * 4;
float *verts = (float *) SDL_AllocateRenderVertices(renderer, vertlen, CONSTANT_ALIGN, &cmd->data.color.first);
if (!verts) {
return -1;
773
}
774
775
776
777
*(verts++) = ((float)cmd->data.color.r) / 255.0f;
*(verts++) = ((float)cmd->data.color.g) / 255.0f;
*(verts++) = ((float)cmd->data.color.b) / 255.0f;
*(verts++) = ((float)cmd->data.color.a) / 255.0f;
778
return 0;
779
}
780
781
782
static int
METAL_QueueDrawPoints(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL_FPoint * points, int count)
783
{
784
785
786
787
const size_t vertlen = (sizeof (float) * 2) * count;
float *verts = (float *) SDL_AllocateRenderVertices(renderer, vertlen, 0, &cmd->data.draw.first);
if (!verts) {
return -1;
788
}
789
790
791
cmd->data.draw.count = count;
SDL_memcpy(verts, points, vertlen);
return 0;
792
793
}
794
static int
795
796
797
798
799
800
801
METAL_QueueFillRects(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL_FRect * rects, int count)
{
// !!! FIXME: use an index buffer
const size_t vertlen = (sizeof (float) * 8) * count;
float *verts = (float *) SDL_AllocateRenderVertices(renderer, vertlen, 0, &cmd->data.draw.first);
if (!verts) {
return -1;
802
803
}
804
cmd->data.draw.count = count;
805
806
807
808
for (int i = 0; i < count; i++, rects++) {
if ((rects->w <= 0.0f) || (rects->h <= 0.0f)) {
cmd->data.draw.count--;
809
} else {
810
811
812
813
814
815
816
817
*(verts++) = rects->x;
*(verts++) = rects->y + rects->h;
*(verts++) = rects->x;
*(verts++) = rects->y;
*(verts++) = rects->x + rects->w;
*(verts++) = rects->y + rects->h;
*(verts++) = rects->x + rects->w;
*(verts++) = rects->y;
818
}
819
}
820
821
822
if (cmd->data.draw.count == 0) {
cmd->command = SDL_RENDERCMD_NO_OP; // nothing to do, just skip this one later.
823
}
824
825
826
return 0;
}
827
828
829
830
831
832
833
834
835
836
837
838
839
static int
METAL_QueueCopy(SDL_Renderer * renderer, SDL_RenderCommand *cmd, SDL_Texture * texture,
const SDL_Rect * srcrect, const SDL_FRect * dstrect)
{
METAL_TextureData *texturedata = (__bridge METAL_TextureData *)texture->driverdata;
const float texw = (float) texturedata.mtltexture.width;
const float texh = (float) texturedata.mtltexture.height;
// !!! FIXME: use an index buffer
const size_t vertlen = (sizeof (float) * 16);
float *verts = (float *) SDL_AllocateRenderVertices(renderer, vertlen, 0, &cmd->data.draw.first);
if (!verts) {
return -1;
840
841
}
842
cmd->data.draw.count = 1;
843
844
845
846
847
848
849
850
851
*(verts++) = dstrect->x;
*(verts++) = dstrect->y + dstrect->h;
*(verts++) = dstrect->x;
*(verts++) = dstrect->y;
*(verts++) = dstrect->x + dstrect->w;
*(verts++) = dstrect->y + dstrect->h;
*(verts++) = dstrect->x + dstrect->w;
*(verts++) = dstrect->y;
852
853
854
855
856
857
858
859
860
*(verts++) = normtex(srcrect->x, texw);
*(verts++) = normtex(srcrect->y + srcrect->h, texh);
*(verts++) = normtex(srcrect->x, texw);
*(verts++) = normtex(srcrect->y, texh);
*(verts++) = normtex(srcrect->x + srcrect->w, texw);
*(verts++) = normtex(srcrect->y + srcrect->h, texh);
*(verts++) = normtex(srcrect->x + srcrect->w, texw);
*(verts++) = normtex(srcrect->y, texh);
861
862
return 0;
863
}
864
865
static int
866
867
868
869
METAL_QueueCopyEx(SDL_Renderer * renderer, SDL_RenderCommand *cmd, SDL_Texture * texture,
const SDL_Rect * srcquad, const SDL_FRect * dstrect,
const double angle, const SDL_FPoint *center, const SDL_RendererFlip flip)
{
870
871
872
873
874
875
876
METAL_TextureData *texturedata = (__bridge METAL_TextureData *)texture->driverdata;
const float texw = (float) texturedata.mtltexture.width;
const float texh = (float) texturedata.mtltexture.height;
const float rads = (float)(M_PI * (float) angle / 180.0f);
const float c = cosf(rads), s = sinf(rads);
float minu, maxu, minv, maxv;
const size_t vertlen = (sizeof (float) * 32);
877
878
879
880
float *verts;
// 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.
verts = (float *) SDL_AllocateRenderVertices(renderer, vertlen, CONSTANT_ALIGN, &cmd->data.draw.count);
881
882
883
if (!verts) {
return -1;
}
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
// transform matrix
SDL_memset(verts, '\0', sizeof (*verts) * 16);
verts[10] = verts[15] = 1.0f;
// rotation
verts[0] = c;
verts[1] = s;
verts[4] = -s;
verts[5] = c;
// translation
verts[12] = dstrect->x + center->x;
verts[13] = dstrect->y + center->y;
// rest of the vertices don't need the aggressive alignment. Pack them in.
verts = (float *) SDL_AllocateRenderVertices(renderer, vertlen, 0, &cmd->data.draw.first);
if (!verts) {
return -1;
}
903
904
905
906
907
minu = normtex(srcquad->x, texw);
maxu = normtex(srcquad->x + srcquad->w, texw);
minv = normtex(srcquad->y, texh);
maxv = normtex(srcquad->y + srcquad->h, texh);
908
909
910
911
912
913
914
915
916
917
918
if (flip & SDL_FLIP_HORIZONTAL) {
float tmp = maxu;
maxu = minu;
minu = tmp;
}
if (flip & SDL_FLIP_VERTICAL) {
float tmp = maxv;
maxv = minv;
minv = tmp;
}
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
// vertices
*(verts++) = -center->x;
*(verts++) = dstrect->h - center->y;
*(verts++) = -center->x;
*(verts++) = -center->y;
*(verts++) = dstrect->w - center->x;
*(verts++) = dstrect->h - center->y;
*(verts++) = dstrect->w - center->x;
*(verts++) = -center->y;
// texcoords
*(verts++) = minu;
*(verts++) = maxv;
*(verts++) = minu;
*(verts++) = minv;
*(verts++) = maxu;
*(verts++) = maxv;
*(verts++) = maxu;
*(verts++) = minv;
940
941
942
return 0;
}
943
944
typedef struct
945
{
946
947
948
#if __has_feature(objc_arc)
__unsafe_unretained id<MTLRenderPipelineState> pipeline;
#else
949
id<MTLRenderPipelineState> pipeline;
950
#endif
951
952
953
954
size_t constants_offset;
SDL_Texture *texture;
SDL_bool cliprect_dirty;
SDL_bool cliprect_enabled;
955
SDL_Rect cliprect;
956
957
SDL_bool viewport_dirty;
SDL_Rect viewport;
958
959
960
size_t projection_offset;
SDL_bool color_dirty;
size_t color_offset;
961
} METAL_DrawStateCache;
962
963
964
965
966
static void
SetDrawState(SDL_Renderer *renderer, const SDL_RenderCommand *cmd, const SDL_MetalFragmentFunction shader,
const size_t constants_offset, id<MTLBuffer> mtlbufvertex, METAL_DrawStateCache *statecache)
{
967
METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
968
const SDL_BlendMode blend = cmd->data.draw.blend;
969
size_t first = cmd->data.draw.first;
970
id<MTLRenderPipelineState> newpipeline;
971
972
METAL_ActivateRenderCommandEncoder(renderer, MTLLoadActionLoad, NULL);
973
974
975
976
977
978
979
980
981
982
if (statecache->viewport_dirty) {
MTLViewport viewport;
viewport.originX = statecache->viewport.x;
viewport.originY = statecache->viewport.y;
viewport.width = statecache->viewport.w;
viewport.height = statecache->viewport.h;
viewport.znear = 0.0;
viewport.zfar = 1.0;
[data.mtlcmdencoder setViewport:viewport];
983
[data.mtlcmdencoder setVertexBuffer:mtlbufvertex offset:statecache->projection_offset atIndex:2]; // projection
984
statecache->viewport_dirty = SDL_FALSE;
985
986
}
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
if (statecache->cliprect_dirty) {
MTLScissorRect mtlrect;
if (statecache->cliprect_enabled) {
const SDL_Rect *rect = &statecache->cliprect;
mtlrect.x = statecache->viewport.x + rect->x;
mtlrect.y = statecache->viewport.y + rect->y;
mtlrect.width = rect->w;
mtlrect.height = rect->h;
} else {
mtlrect.x = statecache->viewport.x;
mtlrect.y = statecache->viewport.y;
mtlrect.width = statecache->viewport.w;
mtlrect.height = statecache->viewport.h;
}