Skip to content

Latest commit

 

History

History
1578 lines (1350 loc) · 59.9 KB

SDL_render_metal.m

File metadata and controls

1578 lines (1350 loc) · 59.9 KB
 
1
2
/*
Simple DirectMedia Layer
Jan 3, 2018
Jan 3, 2018
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"
Dec 8, 2017
Dec 8, 2017
31
#ifdef __MACOSX__
Dec 31, 2017
Dec 31, 2017
32
#include "../../video/cocoa/SDL_cocoametalview.h"
Dec 8, 2017
Dec 8, 2017
33
34
35
#else
#include "../../video/uikit/SDL_uikitmetalview.h"
#endif
Jan 8, 2018
Jan 8, 2018
36
#include <Availability.h>
Dec 31, 2017
Dec 31, 2017
37
38
#import <Metal/Metal.h>
#import <QuartzCore/CAMetalLayer.h>
Dec 8, 2017
Dec 8, 2017
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
49
50
51
52
/* Apple Metal renderer implementation */
static SDL_Renderer *METAL_CreateRenderer(SDL_Window * window, Uint32 flags);
static void METAL_WindowEvent(SDL_Renderer * renderer,
const SDL_WindowEvent *event);
static int METAL_GetOutputSize(SDL_Renderer * renderer, int *w, int *h);
Jan 1, 2018
Jan 1, 2018
53
static SDL_bool METAL_SupportsBlendMode(SDL_Renderer * renderer, SDL_BlendMode blendMode);
54
55
56
57
58
59
60
61
62
63
64
65
66
static int METAL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
static int METAL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, const void *pixels,
int pitch);
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);
static int METAL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, void **pixels, int *pitch);
static void METAL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
static int METAL_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture);
Sep 24, 2018
Sep 24, 2018
67
68
static int METAL_QueueSetViewport(SDL_Renderer * renderer, SDL_RenderCommand *cmd);
static int METAL_QueueSetDrawColor(SDL_Renderer * renderer, SDL_RenderCommand *cmd);
Sep 20, 2018
Sep 20, 2018
69
70
71
72
73
74
75
76
77
78
static int METAL_QueueDrawPoints(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL_FPoint * points,
int count);
static int METAL_QueueFillRects(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL_FRect * rects,
int count);
static int METAL_QueueCopy(SDL_Renderer * renderer, SDL_RenderCommand *cmd, SDL_Texture * texture,
const SDL_Rect * srcrect, const SDL_FRect * dstrect);
static int 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);
static int METAL_RunCommandQueue(SDL_Renderer * renderer, SDL_RenderCommand *cmd, void *vertices, size_t vertsize);
79
80
81
82
83
static int METAL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
Uint32 pixel_format, void * pixels, int pitch);
static void METAL_RenderPresent(SDL_Renderer * renderer);
static void METAL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture);
static void METAL_DestroyRenderer(SDL_Renderer * renderer);
Dec 8, 2017
Dec 8, 2017
84
85
static void *METAL_GetMetalLayer(SDL_Renderer * renderer);
static void *METAL_GetMetalCommandEncoder(SDL_Renderer * renderer);
86
87
88
89
SDL_RenderDriver METAL_RenderDriver = {
METAL_CreateRenderer,
{
Jan 8, 2018
Jan 8, 2018
90
91
92
93
94
95
96
97
98
99
100
101
"metal",
(SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE),
6,
{
SDL_PIXELFORMAT_ARGB8888,
SDL_PIXELFORMAT_ABGR8888,
SDL_PIXELFORMAT_YV12,
SDL_PIXELFORMAT_IYUV,
SDL_PIXELFORMAT_NV12,
SDL_PIXELFORMAT_NV21
},
0, 0,
Dec 9, 2017
Dec 9, 2017
102
}
Jan 3, 2018
Jan 3, 2018
105
106
107
108
109
110
111
112
113
/* 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)))
Sep 20, 2018
Sep 20, 2018
114
static const size_t CONSTANTS_OFFSET_INVALID = 0xFFFFFFFF;
Jan 3, 2018
Jan 3, 2018
115
116
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);
Jan 6, 2018
Jan 6, 2018
117
118
119
120
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);
static const size_t CONSTANTS_OFFSET_CLEAR_VERTS = ALIGN_CONSTANTS(CONSTANTS_OFFSET_DECODE_BT709 + sizeof(float) * 4 * 4);
Jan 3, 2018
Jan 3, 2018
121
122
static const size_t CONSTANTS_LENGTH = CONSTANTS_OFFSET_CLEAR_VERTS + sizeof(float) * 6;
Jan 1, 2018
Jan 1, 2018
123
124
125
126
127
128
129
130
typedef enum SDL_MetalVertexFunction
{
SDL_METAL_VERTEX_SOLID,
SDL_METAL_VERTEX_COPY,
} SDL_MetalVertexFunction;
typedef enum SDL_MetalFragmentFunction
{
Jan 6, 2018
Jan 6, 2018
131
SDL_METAL_FRAGMENT_SOLID = 0,
Jan 1, 2018
Jan 1, 2018
132
SDL_METAL_FRAGMENT_COPY,
Jan 6, 2018
Jan 6, 2018
133
134
135
136
SDL_METAL_FRAGMENT_YUV,
SDL_METAL_FRAGMENT_NV12,
SDL_METAL_FRAGMENT_NV21,
SDL_METAL_FRAGMENT_COUNT,
Jan 1, 2018
Jan 1, 2018
137
138
139
140
141
142
143
144
145
146
147
148
149
150
} 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;
Jan 7, 2018
Jan 7, 2018
151
MTLPixelFormat renderTargetFormat;
Jan 1, 2018
Jan 1, 2018
152
153
154
const char *label;
} METAL_PipelineCache;
Jan 6, 2018
Jan 6, 2018
155
/* Each shader combination used by drawing functions has a separate pipeline
Jan 7, 2018
Jan 7, 2018
156
157
158
159
160
* 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. */
Jan 6, 2018
Jan 6, 2018
161
162
typedef struct METAL_ShaderPipelines
{
Jan 7, 2018
Jan 7, 2018
163
MTLPixelFormat renderTargetFormat;
Jan 6, 2018
Jan 6, 2018
164
165
166
METAL_PipelineCache caches[SDL_METAL_FRAGMENT_COUNT];
} METAL_ShaderPipelines;
Dec 8, 2017
Dec 8, 2017
167
@interface METAL_RenderData : NSObject
Dec 8, 2017
Dec 8, 2017
168
169
170
171
172
173
@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;
Jan 1, 2018
Jan 1, 2018
174
175
@property (nonatomic, retain) id<MTLSamplerState> mtlsamplernearest;
@property (nonatomic, retain) id<MTLSamplerState> mtlsamplerlinear;
Jan 3, 2018
Jan 3, 2018
176
@property (nonatomic, retain) id<MTLBuffer> mtlbufconstants;
Dec 8, 2017
Dec 8, 2017
177
178
@property (nonatomic, retain) CAMetalLayer *mtllayer;
@property (nonatomic, retain) MTLRenderPassDescriptor *mtlpassdesc;
Jan 7, 2018
Jan 7, 2018
179
180
181
@property (nonatomic, assign) METAL_ShaderPipelines *activepipelines;
@property (nonatomic, assign) METAL_ShaderPipelines *allpipelines;
@property (nonatomic, assign) int pipelinescount;
Dec 8, 2017
Dec 8, 2017
182
183
184
@end
@implementation METAL_RenderData
Jan 2, 2018
Jan 2, 2018
185
#if !__has_feature(objc_arc)
Jan 2, 2018
Jan 2, 2018
186
187
188
189
190
191
192
193
194
195
- (void)dealloc
{
[_mtldevice release];
[_mtlcmdqueue release];
[_mtlcmdbuffer release];
[_mtlcmdencoder release];
[_mtllibrary release];
[_mtlbackbuffer release];
[_mtlsamplernearest release];
[_mtlsamplerlinear release];
Jan 3, 2018
Jan 3, 2018
196
[_mtlbufconstants release];
Jan 2, 2018
Jan 2, 2018
197
198
199
200
201
[_mtllayer release];
[_mtlpassdesc release];
[super dealloc];
}
#endif
Dec 8, 2017
Dec 8, 2017
202
@end
Dec 9, 2017
Dec 9, 2017
204
205
@interface METAL_TextureData : NSObject
@property (nonatomic, retain) id<MTLTexture> mtltexture;
Jan 6, 2018
Jan 6, 2018
206
@property (nonatomic, retain) id<MTLTexture> mtltexture_uv;
Jan 1, 2018
Jan 1, 2018
207
@property (nonatomic, retain) id<MTLSamplerState> mtlsampler;
Jan 6, 2018
Jan 6, 2018
208
209
210
211
@property (nonatomic, assign) SDL_MetalFragmentFunction fragmentFunction;
@property (nonatomic, assign) BOOL yuv;
@property (nonatomic, assign) BOOL nv12;
@property (nonatomic, assign) size_t conversionBufferOffset;
Dec 9, 2017
Dec 9, 2017
212
213
214
@end
@implementation METAL_TextureData
Jan 2, 2018
Jan 2, 2018
215
#if !__has_feature(objc_arc)
Jan 2, 2018
Jan 2, 2018
216
217
218
- (void)dealloc
{
[_mtltexture release];
Jan 6, 2018
Jan 6, 2018
219
[_mtltexture_uv release];
Jan 2, 2018
Jan 2, 2018
220
221
222
223
[_mtlsampler release];
[super dealloc];
}
#endif
Dec 9, 2017
Dec 9, 2017
224
225
@end
226
227
228
static int
IsMetalAvailable(const SDL_SysWMinfo *syswm)
{
Dec 8, 2017
Dec 8, 2017
229
230
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.");
231
232
233
}
// this checks a weak symbol.
Dec 8, 2017
Dec 8, 2017
234
#if (defined(__MACOSX__) && (MAC_OS_X_VERSION_MIN_REQUIRED < 101100))
235
236
237
238
239
240
241
242
if (MTLCreateSystemDefaultDevice == NULL) { // probably on 10.10 or lower.
return SDL_SetError("Metal framework not available on this system");
}
#endif
return 0;
}
Jan 1, 2018
Jan 1, 2018
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
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";
Jan 1, 2018
Jan 1, 2018
292
case SDL_METAL_FRAGMENT_COPY: return @"SDL_Copy_fragment";
Jan 6, 2018
Jan 6, 2018
293
294
295
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";
Jan 1, 2018
Jan 1, 2018
296
297
298
299
default: return nil;
}
}
300
static id<MTLRenderPipelineState>
Jan 1, 2018
Jan 1, 2018
301
302
MakePipelineState(METAL_RenderData *data, METAL_PipelineCache *cache,
NSString *blendlabel, SDL_BlendMode blendmode)
Jan 1, 2018
Jan 1, 2018
304
305
id<MTLFunction> mtlvertfn = [data.mtllibrary newFunctionWithName:GetVertexFunctionName(cache->vertexFunction)];
id<MTLFunction> mtlfragfn = [data.mtllibrary newFunctionWithName:GetFragmentFunctionName(cache->fragmentFunction)];
306
307
308
309
310
311
SDL_assert(mtlvertfn != nil);
SDL_assert(mtlfragfn != nil);
MTLRenderPipelineDescriptor *mtlpipedesc = [[MTLRenderPipelineDescriptor alloc] init];
mtlpipedesc.vertexFunction = mtlvertfn;
mtlpipedesc.fragmentFunction = mtlfragfn;
Jan 1, 2018
Jan 1, 2018
312
313
314
MTLRenderPipelineColorAttachmentDescriptor *rtdesc = mtlpipedesc.colorAttachments[0];
Jan 7, 2018
Jan 7, 2018
315
rtdesc.pixelFormat = cache->renderTargetFormat;
Jan 1, 2018
Jan 1, 2018
316
317
318
319
320
321
322
323
324
325
326
327
328
329
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];
330
331
NSError *err = nil;
Jan 1, 2018
Jan 1, 2018
332
id<MTLRenderPipelineState> state = [data.mtldevice newRenderPipelineStateWithDescriptor:mtlpipedesc error:&err];
333
SDL_assert(err == nil);
Jan 1, 2018
Jan 1, 2018
334
335
336
337
338
339
340
METAL_PipelineState pipeline;
pipeline.blendMode = blendmode;
pipeline.pipe = (void *)CFBridgingRetain(state);
METAL_PipelineState *states = SDL_realloc(cache->states, (cache->count + 1) * sizeof(pipeline));
Dec 8, 2017
Dec 8, 2017
341
#if !__has_feature(objc_arc)
342
343
344
[mtlpipedesc release]; // !!! FIXME: can these be reused for each creation, or does the pipeline obtain it?
[mtlvertfn release];
[mtlfragfn release];
Jan 1, 2018
Jan 1, 2018
345
[state release];
Dec 8, 2017
Dec 8, 2017
346
#endif
Jan 1, 2018
Jan 1, 2018
347
348
349
350
351
352
353
354
355
356
357
358
if (states) {
states[cache->count++] = pipeline;
cache->states = states;
return (__bridge id<MTLRenderPipelineState>)pipeline.pipe;
} else {
CFBridgingRelease(pipeline.pipe);
SDL_OutOfMemory();
return NULL;
}
}
Jan 6, 2018
Jan 6, 2018
359
static void
Jan 7, 2018
Jan 7, 2018
360
361
MakePipelineCache(METAL_RenderData *data, METAL_PipelineCache *cache, const char *label,
MTLPixelFormat rtformat, SDL_MetalVertexFunction vertfn, SDL_MetalFragmentFunction fragfn)
Jan 1, 2018
Jan 1, 2018
362
363
364
365
366
{
SDL_zerop(cache);
cache->vertexFunction = vertfn;
cache->fragmentFunction = fragfn;
Jan 7, 2018
Jan 7, 2018
367
cache->renderTargetFormat = rtformat;
Jan 1, 2018
Jan 1, 2018
368
369
370
371
cache->label = label;
/* Create pipeline states for the default blend modes. Custom blend modes
* will be added to the cache on-demand. */
Jan 6, 2018
Jan 6, 2018
372
373
374
375
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);
376
377
378
}
static void
Jan 1, 2018
Jan 1, 2018
379
DestroyPipelineCache(METAL_PipelineCache *cache)
Jan 1, 2018
Jan 1, 2018
381
382
383
384
385
386
if (cache != NULL) {
for (int i = 0; i < cache->count; i++) {
CFBridgingRelease(cache->states[i].pipe);
}
SDL_free(cache->states);
Jan 6, 2018
Jan 6, 2018
387
388
389
}
}
Jan 7, 2018
Jan 7, 2018
390
391
392
393
394
395
396
397
398
399
400
401
402
403
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);
}
Jan 6, 2018
Jan 6, 2018
404
static METAL_ShaderPipelines *
Jan 7, 2018
Jan 7, 2018
405
ChooseShaderPipelines(METAL_RenderData *data, MTLPixelFormat rtformat)
Jan 6, 2018
Jan 6, 2018
406
{
Jan 7, 2018
Jan 7, 2018
407
408
409
410
411
412
413
414
415
416
417
418
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) {
Jan 6, 2018
Jan 6, 2018
419
420
421
422
SDL_OutOfMemory();
return NULL;
}
Jan 7, 2018
Jan 7, 2018
423
424
425
426
MakeShaderPipelines(data, &allpipelines[count], rtformat);
data.allpipelines = allpipelines;
data.pipelinescount = count + 1;
Jan 6, 2018
Jan 6, 2018
427
Jan 7, 2018
Jan 7, 2018
428
return &data.allpipelines[count];
Jan 6, 2018
Jan 6, 2018
429
430
431
}
static void
Jan 7, 2018
Jan 7, 2018
432
DestroyAllPipelines(METAL_ShaderPipelines *allpipelines, int count)
Jan 6, 2018
Jan 6, 2018
433
{
Jan 7, 2018
Jan 7, 2018
434
435
436
437
438
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]);
}
Jan 6, 2018
Jan 6, 2018
439
440
}
Jan 7, 2018
Jan 7, 2018
441
SDL_free(allpipelines);
Jan 1, 2018
Jan 1, 2018
442
}
443
444
445
}
static inline id<MTLRenderPipelineState>
Jan 6, 2018
Jan 6, 2018
446
ChoosePipelineState(METAL_RenderData *data, METAL_ShaderPipelines *pipelines, SDL_MetalFragmentFunction fragfn, SDL_BlendMode blendmode)
Jan 6, 2018
Jan 6, 2018
448
449
METAL_PipelineCache *cache = &pipelines->caches[fragfn];
Jan 1, 2018
Jan 1, 2018
450
451
452
453
for (int i = 0; i < cache->count; i++) {
if (cache->states[i].blendMode == blendmode) {
return (__bridge id<MTLRenderPipelineState>)cache->states[i].pipe;
}
Jan 1, 2018
Jan 1, 2018
455
Jan 6, 2018
Jan 6, 2018
456
return MakePipelineState(data, cache, [NSString stringWithFormat:@" (blend=custom 0x%x)", blendmode], blendmode);
457
458
459
460
}
static SDL_Renderer *
METAL_CreateRenderer(SDL_Window * window, Uint32 flags)
Jan 8, 2018
Jan 8, 2018
461
{ @autoreleasepool {
462
463
SDL_Renderer *renderer = NULL;
METAL_RenderData *data = NULL;
Jan 8, 2018
Jan 8, 2018
464
id<MTLDevice> mtldevice = nil;
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
SDL_SysWMinfo syswm;
SDL_VERSION(&syswm.version);
if (!SDL_GetWindowWMInfo(window, &syswm)) {
return NULL;
}
if (IsMetalAvailable(&syswm) == -1) {
return NULL;
}
renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
if (!renderer) {
SDL_OutOfMemory();
return NULL;
}
Jan 8, 2018
Jan 8, 2018
482
483
// !!! FIXME: MTLCopyAllDevices() can find other GPUs on macOS...
mtldevice = MTLCreateSystemDefaultDevice();
Dec 8, 2017
Dec 8, 2017
484
Dec 8, 2017
Dec 8, 2017
485
if (mtldevice == nil) {
486
487
488
489
490
491
SDL_free(renderer);
SDL_SetError("Failed to obtain Metal device");
return NULL;
}
// !!! FIXME: error checking on all of this.
Jan 8, 2018
Jan 8, 2018
492
data = [[METAL_RenderData alloc] init];
Jan 8, 2018
Jan 8, 2018
494
495
496
497
renderer->driverdata = (void*)CFBridgingRetain(data);
renderer->window = window;
#ifdef __MACOSX__
Dec 31, 2017
Dec 31, 2017
498
499
NSView *view = Cocoa_Mtl_AddMetalView(window);
CAMetalLayer *layer = (CAMetalLayer *)[view layer];
Dec 8, 2017
Dec 8, 2017
501
layer.device = mtldevice;
Dec 31, 2017
Dec 31, 2017
502
503
504
//layer.colorspace = nil;
Dec 8, 2017
Dec 8, 2017
505
#else
Dec 8, 2017
Dec 8, 2017
506
507
UIView *view = UIKit_Mtl_AddMetalView(window);
CAMetalLayer *layer = (CAMetalLayer *)[view layer];
Dec 8, 2017
Dec 8, 2017
508
509
#endif
Jan 1, 2018
Jan 1, 2018
510
511
512
// Necessary for RenderReadPixels.
layer.framebufferOnly = NO;
Dec 8, 2017
Dec 8, 2017
513
514
data.mtldevice = layer.device;
data.mtllayer = layer;
Jan 2, 2018
Jan 2, 2018
515
516
id<MTLCommandQueue> mtlcmdqueue = [data.mtldevice newCommandQueue];
data.mtlcmdqueue = mtlcmdqueue;
Dec 8, 2017
Dec 8, 2017
517
data.mtlcmdqueue.label = @"SDL Metal Renderer";
Dec 9, 2017
Dec 9, 2017
518
data.mtlpassdesc = [MTLRenderPassDescriptor renderPassDescriptor];
Dec 8, 2017
Dec 8, 2017
520
521
522
523
524
NSError *err = nil;
// The compiled .metallib is embedded in a static array in a header file
// but the original shader source code is in SDL_shaders_metal.metal.
dispatch_data_t mtllibdata = dispatch_data_create(sdl_metallib, sdl_metallib_len, dispatch_get_global_queue(0, 0), ^{});
Jan 2, 2018
Jan 2, 2018
525
526
id<MTLLibrary> mtllibrary = [data.mtldevice newLibraryWithData:mtllibdata error:&err];
data.mtllibrary = mtllibrary;
Dec 8, 2017
Dec 8, 2017
527
528
529
530
531
532
SDL_assert(err == nil);
#if !__has_feature(objc_arc)
dispatch_release(mtllibdata);
#endif
data.mtllibrary.label = @"SDL Metal renderer shader library";
Jan 7, 2018
Jan 7, 2018
533
534
535
536
/* Do some shader pipeline state loading up-front rather than on demand. */
data.pipelinescount = 0;
data.allpipelines = NULL;
ChooseShaderPipelines(data, MTLPixelFormatBGRA8Unorm);
Jan 1, 2018
Jan 1, 2018
537
Jan 1, 2018
Jan 1, 2018
538
MTLSamplerDescriptor *samplerdesc = [[MTLSamplerDescriptor alloc] init];
Jan 1, 2018
Jan 1, 2018
539
540
541
samplerdesc.minFilter = MTLSamplerMinMagFilterNearest;
samplerdesc.magFilter = MTLSamplerMinMagFilterNearest;
Jan 2, 2018
Jan 2, 2018
542
543
id<MTLSamplerState> mtlsamplernearest = [data.mtldevice newSamplerStateWithDescriptor:samplerdesc];
data.mtlsamplernearest = mtlsamplernearest;
Jan 1, 2018
Jan 1, 2018
544
545
546
samplerdesc.minFilter = MTLSamplerMinMagFilterLinear;
samplerdesc.magFilter = MTLSamplerMinMagFilterLinear;
Jan 2, 2018
Jan 2, 2018
547
548
id<MTLSamplerState> mtlsamplerlinear = [data.mtldevice newSamplerStateWithDescriptor:samplerdesc];
data.mtlsamplerlinear = mtlsamplerlinear;
Jan 1, 2018
Jan 1, 2018
549
Jan 3, 2018
Jan 3, 2018
550
551
552
553
554
555
556
/* Note: matrices are column major. */
float identitytransform[16] = {
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f,
};
Jan 3, 2018
Jan 3, 2018
558
559
560
561
562
563
564
float halfpixeltransform[16] = {
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.0f, 1.0f,
};
Jan 6, 2018
Jan 6, 2018
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
/* Metal pads float3s to 16 bytes. */
float decodetransformJPEG[4*4] = {
0.0, -0.501960814, -0.501960814, 0.0, /* offset */
1.0000, 0.0000, 1.4020, 0.0, /* Rcoeff */
1.0000, -0.3441, -0.7141, 0.0, /* Gcoeff */
1.0000, 1.7720, 0.0000, 0.0, /* Bcoeff */
};
float decodetransformBT601[4*4] = {
-0.0627451017, -0.501960814, -0.501960814, 0.0, /* offset */
1.1644, 0.0000, 1.5960, 0.0, /* Rcoeff */
1.1644, -0.3918, -0.8130, 0.0, /* Gcoeff */
1.1644, 2.0172, 0.0000, 0.0, /* Bcoeff */
};
float decodetransformBT709[4*4] = {
0.0, -0.501960814, -0.501960814, 0.0, /* offset */
1.0000, 0.0000, 1.4020, 0.0, /* Rcoeff */
1.0000, -0.3441, -0.7141, 0.0, /* Gcoeff */
1.0000, 1.7720, 0.0000, 0.0, /* Bcoeff */
};
Jan 3, 2018
Jan 3, 2018
587
588
float clearverts[6] = {0.0f, 0.0f, 0.0f, 2.0f, 2.0f, 0.0f};
Jan 5, 2018
Jan 5, 2018
589
id<MTLBuffer> mtlbufconstantstaging = [data.mtldevice newBufferWithLength:CONSTANTS_LENGTH options:MTLResourceStorageModeShared];
Sep 20, 2018
Sep 20, 2018
590
591
592
#if !__has_feature(objc_arc)
[mtlbufconstantstaging autorelease];
#endif
Jan 5, 2018
Jan 5, 2018
593
mtlbufconstantstaging.label = @"SDL constant staging data";
Jan 3, 2018
Jan 3, 2018
594
Jan 5, 2018
Jan 5, 2018
595
id<MTLBuffer> mtlbufconstants = [data.mtldevice newBufferWithLength:CONSTANTS_LENGTH options:MTLResourceStorageModePrivate];
Jan 3, 2018
Jan 3, 2018
596
597
598
data.mtlbufconstants = mtlbufconstants;
data.mtlbufconstants.label = @"SDL constant data";
Jan 5, 2018
Jan 5, 2018
599
char *constantdata = [mtlbufconstantstaging contents];
Jan 3, 2018
Jan 3, 2018
600
601
SDL_memcpy(constantdata + CONSTANTS_OFFSET_IDENTITY, identitytransform, sizeof(identitytransform));
SDL_memcpy(constantdata + CONSTANTS_OFFSET_HALF_PIXEL_TRANSFORM, halfpixeltransform, sizeof(halfpixeltransform));
Jan 6, 2018
Jan 6, 2018
602
603
604
SDL_memcpy(constantdata + CONSTANTS_OFFSET_DECODE_JPEG, decodetransformJPEG, sizeof(decodetransformJPEG));
SDL_memcpy(constantdata + CONSTANTS_OFFSET_DECODE_BT601, decodetransformBT601, sizeof(decodetransformBT601));
SDL_memcpy(constantdata + CONSTANTS_OFFSET_DECODE_BT709, decodetransformBT709, sizeof(decodetransformBT709));
Jan 3, 2018
Jan 3, 2018
605
SDL_memcpy(constantdata + CONSTANTS_OFFSET_CLEAR_VERTS, clearverts, sizeof(clearverts));
Jan 5, 2018
Jan 5, 2018
606
607
608
609
610
611
612
613
id<MTLCommandBuffer> cmdbuffer = [data.mtlcmdqueue commandBuffer];
id<MTLBlitCommandEncoder> blitcmd = [cmdbuffer blitCommandEncoder];
[blitcmd copyFromBuffer:mtlbufconstantstaging sourceOffset:0 toBuffer:data.mtlbufconstants destinationOffset:0 size:CONSTANTS_LENGTH];
[blitcmd endEncoding];
[cmdbuffer commit];
Jan 1, 2018
Jan 1, 2018
614
Dec 8, 2017
Dec 8, 2017
615
// !!! FIXME: force more clears here so all the drawables are sane to start, and our static buffers are definitely flushed.
616
617
618
renderer->WindowEvent = METAL_WindowEvent;
renderer->GetOutputSize = METAL_GetOutputSize;
Jan 1, 2018
Jan 1, 2018
619
renderer->SupportsBlendMode = METAL_SupportsBlendMode;
620
621
622
623
624
625
renderer->CreateTexture = METAL_CreateTexture;
renderer->UpdateTexture = METAL_UpdateTexture;
renderer->UpdateTextureYUV = METAL_UpdateTextureYUV;
renderer->LockTexture = METAL_LockTexture;
renderer->UnlockTexture = METAL_UnlockTexture;
renderer->SetRenderTarget = METAL_SetRenderTarget;
Sep 24, 2018
Sep 24, 2018
626
627
renderer->QueueSetViewport = METAL_QueueSetViewport;
renderer->QueueSetDrawColor = METAL_QueueSetDrawColor;
Sep 20, 2018
Sep 20, 2018
628
629
630
631
632
633
renderer->QueueDrawPoints = METAL_QueueDrawPoints;
renderer->QueueDrawLines = METAL_QueueDrawPoints; // lines and points queue the same way.
renderer->QueueFillRects = METAL_QueueFillRects;
renderer->QueueCopy = METAL_QueueCopy;
renderer->QueueCopyEx = METAL_QueueCopyEx;
renderer->RunCommandQueue = METAL_RunCommandQueue;
634
635
636
637
renderer->RenderReadPixels = METAL_RenderReadPixels;
renderer->RenderPresent = METAL_RenderPresent;
renderer->DestroyTexture = METAL_DestroyTexture;
renderer->DestroyRenderer = METAL_DestroyRenderer;
Dec 8, 2017
Dec 8, 2017
638
639
renderer->GetMetalLayer = METAL_GetMetalLayer;
renderer->GetMetalCommandEncoder = METAL_GetMetalCommandEncoder;
640
641
642
643
renderer->info = METAL_RenderDriver.info;
renderer->info.flags = (SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE);
Sep 24, 2018
Sep 24, 2018
644
645
renderer->always_batch = SDL_TRUE;
Dec 31, 2017
Dec 31, 2017
646
647
#if defined(__MACOSX__) && defined(MAC_OS_X_VERSION_10_13)
if (@available(macOS 10.13, *)) {
Dec 31, 2017
Dec 31, 2017
648
data.mtllayer.displaySyncEnabled = (flags & SDL_RENDERER_PRESENTVSYNC) != 0;
Dec 31, 2017
Dec 31, 2017
649
650
651
652
653
} else
#endif
{
renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
}
Jan 8, 2018
Jan 8, 2018
655
656
657
658
659
/* https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf */
int maxtexsize = 4096;
#if defined(__MACOSX__)
maxtexsize = 16384;
#elif defined(__TVOS__)
Jan 10, 2018
Jan 10, 2018
660
maxtexsize = 8192;
Jan 8, 2018
Jan 8, 2018
661
#ifdef __TVOS_11_0
Jan 10, 2018
Jan 10, 2018
662
663
664
665
if (@available(tvOS 11.0, *)) {
if ([mtldevice supportsFeatureSet:MTLFeatureSet_tvOS_GPUFamily2_v1]) {
maxtexsize = 16384;
}
Jan 8, 2018
Jan 8, 2018
666
}
Jan 10, 2018
Jan 10, 2018
667
#endif
Jan 8, 2018
Jan 8, 2018
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
#else
#ifdef __IPHONE_11_0
if ([mtldevice supportsFeatureSet:MTLFeatureSet_iOS_GPUFamily4_v1]) {
maxtexsize = 16384;
} else
#endif
#ifdef __IPHONE_10_0
if ([mtldevice supportsFeatureSet:MTLFeatureSet_iOS_GPUFamily3_v1]) {
maxtexsize = 16384;
} else
#endif
if ([mtldevice supportsFeatureSet:MTLFeatureSet_iOS_GPUFamily2_v2] || [mtldevice supportsFeatureSet:MTLFeatureSet_iOS_GPUFamily1_v2]) {
maxtexsize = 8192;
} else {
maxtexsize = 4096;
}
#endif
renderer->info.max_texture_width = maxtexsize;
renderer->info.max_texture_height = maxtexsize;
Jan 2, 2018
Jan 2, 2018
689
690
691
692
693
694
#if !__has_feature(objc_arc)
[mtlcmdqueue release];
[mtllibrary release];
[samplerdesc release];
[mtlsamplernearest release];
[mtlsamplerlinear release];
Jan 3, 2018
Jan 3, 2018
695
[mtlbufconstants release];
Jan 2, 2018
Jan 2, 2018
696
697
698
699
700
[view release];
[data release];
[mtldevice release];
#endif
Dec 8, 2017
Dec 8, 2017
701
return renderer;
Jan 8, 2018
Jan 8, 2018
702
}}
Jan 2, 2018
Jan 2, 2018
704
static void
Sep 20, 2018
Sep 20, 2018
705
METAL_ActivateRenderCommandEncoder(SDL_Renderer * renderer, MTLLoadAction load, MTLClearColor *clear_color)
Dec 8, 2017
Dec 8, 2017
706
707
{
METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
Jan 4, 2018
Jan 4, 2018
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
/* 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;
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;
}
SDL_assert(mtltexture);
if (load == MTLLoadActionClear) {
Sep 20, 2018
Sep 20, 2018
732
733
SDL_assert(clear_color != NULL);
data.mtlpassdesc.colorAttachments[0].clearColor = *clear_color;
Jan 4, 2018
Jan 4, 2018
734
735
736
737
738
}
data.mtlpassdesc.colorAttachments[0].loadAction = load;
data.mtlpassdesc.colorAttachments[0].texture = mtltexture;
Dec 8, 2017
Dec 8, 2017
739
740
741
data.mtlcmdbuffer = [data.mtlcmdqueue commandBuffer];
data.mtlcmdencoder = [data.mtlcmdbuffer renderCommandEncoderWithDescriptor:data.mtlpassdesc];
Jan 4, 2018
Jan 4, 2018
742
743
744
745
746
747
if (data.mtlbackbuffer != nil && mtltexture == data.mtlbackbuffer.texture) {
data.mtlcmdencoder.label = @"SDL metal renderer backbuffer";
} else {
data.mtlcmdencoder.label = @"SDL metal renderer render target";
}
Jan 7, 2018
Jan 7, 2018
748
data.activepipelines = ChooseShaderPipelines(data, mtltexture.pixelFormat);
Sep 24, 2018
Sep 24, 2018
749
750
751
752
753
// 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];
Dec 8, 2017
Dec 8, 2017
754
}
755
756
757
758
759
}
static void
METAL_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
{
Sep 6, 2018
Sep 6, 2018
760
761
762
763
764
765
if (event->event == SDL_WINDOWEVENT_SIZE_CHANGED) {
METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
data.mtllayer.drawableSize = CGSizeMake(event->data1, event->data2);
}
if (event->event == SDL_WINDOWEVENT_SHOWN ||
766
767
768
769
770
771
772
event->event == SDL_WINDOWEVENT_HIDDEN) {
// !!! FIXME: write me
}
}
static int
METAL_GetOutputSize(SDL_Renderer * renderer, int *w, int *h)
Dec 8, 2017
Dec 8, 2017
773
{ @autoreleasepool {
Dec 8, 2017
Dec 8, 2017
774
METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
Dec 31, 2017
Dec 31, 2017
775
776
777
778
779
780
if (w) {
*w = (int)data.mtllayer.drawableSize.width;
}
if (h) {
*h = (int)data.mtllayer.drawableSize.height;
}
Dec 8, 2017
Dec 8, 2017
782
}}
Jan 1, 2018
Jan 1, 2018
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
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);
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;
}
805
806
static int
METAL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
Dec 8, 2017
Dec 8, 2017
807
{ @autoreleasepool {
Dec 8, 2017
Dec 8, 2017
808
METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
Jan 6, 2018
Jan 6, 2018
809
MTLPixelFormat pixfmt;
810
811
switch (texture->format) {
Jan 6, 2018
Jan 6, 2018
812
813
814
815
816
817
818
819
820
821
822
823
824
825
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));
Jan 6, 2018
Jan 6, 2018
828
MTLTextureDescriptor *mtltexdesc = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:pixfmt
829
width:(NSUInteger)texture->w height:(NSUInteger)texture->h mipmapped:NO];
Jan 3, 2018
Jan 3, 2018
830
831
832
833
834
835
836
837
/* Not available in iOS 8. */
if ([mtltexdesc respondsToSelector:@selector(usage)]) {
if (texture->access == SDL_TEXTUREACCESS_TARGET) {
mtltexdesc.usage = MTLTextureUsageShaderRead | MTLTextureUsageRenderTarget;
} else {
mtltexdesc.usage = MTLTextureUsageShaderRead;
}
Dec 9, 2017
Dec 9, 2017
838
839
}
Dec 8, 2017
Dec 8, 2017
840
id<MTLTexture> mtltexture = [data.mtldevice newTextureWithDescriptor:mtltexdesc];
841
842
843
844
if (mtltexture == nil) {
return SDL_SetError("Texture allocation failed");
}
Jan 6, 2018
Jan 6, 2018
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
id<MTLTexture> mtltexture_uv = nil;
BOOL yuv = (texture->format == SDL_PIXELFORMAT_IYUV) || (texture->format == SDL_PIXELFORMAT_YV12);
BOOL nv12 = (texture->format == SDL_PIXELFORMAT_NV12) || (texture->format == SDL_PIXELFORMAT_NV21);
if (yuv) {
mtltexdesc.pixelFormat = MTLPixelFormatR8Unorm;
mtltexdesc.width = (texture->w + 1) / 2;
mtltexdesc.height = (texture->h + 1) / 2;
mtltexdesc.textureType = MTLTextureType2DArray;
mtltexdesc.arrayLength = 2;
mtltexture_uv = [data.mtldevice newTextureWithDescriptor:mtltexdesc];
} else if (nv12) {
mtltexdesc.pixelFormat = MTLPixelFormatRG8Unorm;
mtltexdesc.width = (texture->w + 1) / 2;
mtltexdesc.height = (texture->h + 1) / 2;
mtltexture_uv = [data.mtldevice newTextureWithDescriptor:mtltexdesc];
}
Dec 9, 2017
Dec 9, 2017
864
METAL_TextureData *texturedata = [[METAL_TextureData alloc] init];
May 8, 2018
May 8, 2018
865
if (texture->scaleMode == SDL_ScaleModeNearest) {
Jan 1, 2018
Jan 1, 2018
866
texturedata.mtlsampler = data.mtlsamplernearest;
Dec 9, 2017
Dec 9, 2017
867
} else {
Jan 1, 2018
Jan 1, 2018
868
texturedata.mtlsampler = data.mtlsamplerlinear;
Dec 9, 2017
Dec 9, 2017
869
870
}
texturedata.mtltexture = mtltexture;
Jan 6, 2018
Jan 6, 2018
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
texturedata.mtltexture_uv = mtltexture_uv;
texturedata.yuv = yuv;
texturedata.nv12 = nv12;
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;
}
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;
}
Dec 9, 2017
Dec 9, 2017
897
898
texture->driverdata = (void*)CFBridgingRetain(texturedata);
Jan 1, 2018
Jan 1, 2018
900
#if !__has_feature(objc_arc)
Jan 2, 2018
Jan 2, 2018
901
[texturedata release];
Jan 1, 2018
Jan 1, 2018
902
[mtltexture release];
Jan 6, 2018
Jan 6, 2018
903
[mtltexture_uv release];
Jan 1, 2018
Jan 1, 2018
904
905
#endif
Dec 8, 2017
Dec 8, 2017
907
}}
908
909
910
911
static int
METAL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, const void *pixels, int pitch)
Dec 8, 2017
Dec 8, 2017
912
{ @autoreleasepool {
Jan 6, 2018
Jan 6, 2018
913
914
METAL_TextureData *texturedata = (__bridge METAL_TextureData *)texture->driverdata;
Jan 7, 2018
Jan 7, 2018
915
916
917
/* !!! 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. */
Jan 6, 2018
Jan 6, 2018
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
[texturedata.mtltexture replaceRegion:MTLRegionMake2D(rect->x, rect->y, rect->w, rect->h)
mipmapLevel:0
withBytes:pixels
bytesPerRow:pitch];
if (texturedata.yuv) {
int Uslice = texture->format == SDL_PIXELFORMAT_YV12 ? 1 : 0;
int Vslice = texture->format == SDL_PIXELFORMAT_YV12 ? 0 : 1;
/* 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];
/* 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];
}
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];
}
Dec 8, 2017
Dec 8, 2017
958
}}
959
960
961
962
963
964
965
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)
Jan 6, 2018
Jan 6, 2018
966
967
{ @autoreleasepool {
METAL_TextureData *texturedata = (__bridge METAL_TextureData *)texture->driverdata;
Sep 2, 2018
Sep 2, 2018
968
969
const int Uslice = 0;
const int Vslice = 1;
Jan 6, 2018
Jan 6, 2018
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
/* Bail out if we're supposed to update an empty rectangle */
if (rect->w <= 0 || rect->h <= 0) {
return 0;
}
[texturedata.mtltexture replaceRegion:MTLRegionMake2D(rect->x, rect->y, rect->w, rect->h)
mipmapLevel:0
withBytes:Yplane
bytesPerRow:Ypitch];
[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];
[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;
}}
997
998
999
1000
static int
METAL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, void **pixels, int *pitch)