Skip to content

Latest commit

 

History

History
1886 lines (1598 loc) · 68.7 KB

SDL_render_metal.m

File metadata and controls

1886 lines (1598 loc) · 68.7 KB
 
1
2
/*
Simple DirectMedia Layer
Jan 17, 2020
Jan 17, 2020
3
Copyright (C) 1997-2020 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
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_assert.h"
#include "SDL_syswm.h"
Aug 5, 2019
Aug 5, 2019
28
#include "SDL_metal.h"
29
30
#include "../SDL_sysrender.h"
Jan 8, 2018
Jan 8, 2018
31
#include <Availability.h>
Dec 31, 2017
Dec 31, 2017
32
33
#import <Metal/Metal.h>
#import <QuartzCore/CAMetalLayer.h>
Aug 5, 2019
Aug 5, 2019
35
36
37
38
#ifdef __MACOSX__
#import <AppKit/NSView.h>
#endif
Dec 8, 2017
Dec 8, 2017
39
40
41
/* Regenerate these with build-metal-shaders.sh */
#ifdef __MACOSX__
#include "SDL_shaders_metal_osx.h"
Aug 17, 2019
Aug 17, 2019
42
43
#elif defined(__TVOS__)
#include "SDL_shaders_metal_tvos.h"
Dec 8, 2017
Dec 8, 2017
44
45
46
#else
#include "SDL_shaders_metal_ios.h"
#endif
47
48
49
/* Apple Metal renderer implementation */
Jan 3, 2018
Jan 3, 2018
50
/* macOS requires constants in a buffer to have a 256 byte alignment. */
Aug 17, 2019
Aug 17, 2019
51
/* Use native type alignments from https://developer.apple.com/metal/Metal-Shading-Language-Specification.pdf */
Jan 3, 2018
Jan 3, 2018
52
#ifdef __MACOSX__
Aug 17, 2019
Aug 17, 2019
53
#define CONSTANT_ALIGN(x) (256)
Jan 3, 2018
Jan 3, 2018
54
#else
Aug 17, 2019
Aug 17, 2019
55
#define CONSTANT_ALIGN(x) (x < 4 ? 4 : x)
Jan 3, 2018
Jan 3, 2018
56
57
#endif
Aug 17, 2019
Aug 17, 2019
58
59
60
#define DEVICE_ALIGN(x) (x < 4 ? 4 : x)
#define ALIGN_CONSTANTS(align, size) ((size + CONSTANT_ALIGN(align) - 1) & (~(CONSTANT_ALIGN(align) - 1)))
Jan 3, 2018
Jan 3, 2018
61
Sep 20, 2018
Sep 20, 2018
62
static const size_t CONSTANTS_OFFSET_INVALID = 0xFFFFFFFF;
Jan 3, 2018
Jan 3, 2018
63
static const size_t CONSTANTS_OFFSET_IDENTITY = 0;
Aug 17, 2019
Aug 17, 2019
64
65
66
67
static const size_t CONSTANTS_OFFSET_HALF_PIXEL_TRANSFORM = ALIGN_CONSTANTS(16, CONSTANTS_OFFSET_IDENTITY + sizeof(float) * 16);
static const size_t CONSTANTS_OFFSET_DECODE_JPEG = ALIGN_CONSTANTS(16, CONSTANTS_OFFSET_HALF_PIXEL_TRANSFORM + sizeof(float) * 16);
static const size_t CONSTANTS_OFFSET_DECODE_BT601 = ALIGN_CONSTANTS(16, CONSTANTS_OFFSET_DECODE_JPEG + sizeof(float) * 4 * 4);
static const size_t CONSTANTS_OFFSET_DECODE_BT709 = ALIGN_CONSTANTS(16, CONSTANTS_OFFSET_DECODE_BT601 + sizeof(float) * 4 * 4);
Nov 4, 2018
Nov 4, 2018
68
static const size_t CONSTANTS_LENGTH = CONSTANTS_OFFSET_DECODE_BT709 + sizeof(float) * 4 * 4;
Jan 3, 2018
Jan 3, 2018
69
Jan 1, 2018
Jan 1, 2018
70
71
72
73
74
75
76
77
typedef enum SDL_MetalVertexFunction
{
SDL_METAL_VERTEX_SOLID,
SDL_METAL_VERTEX_COPY,
} SDL_MetalVertexFunction;
typedef enum SDL_MetalFragmentFunction
{
Jan 6, 2018
Jan 6, 2018
78
SDL_METAL_FRAGMENT_SOLID = 0,
Jan 1, 2018
Jan 1, 2018
79
SDL_METAL_FRAGMENT_COPY,
Jan 6, 2018
Jan 6, 2018
80
81
82
83
SDL_METAL_FRAGMENT_YUV,
SDL_METAL_FRAGMENT_NV12,
SDL_METAL_FRAGMENT_NV21,
SDL_METAL_FRAGMENT_COUNT,
Jan 1, 2018
Jan 1, 2018
84
85
86
87
88
89
90
91
92
93
94
95
96
97
} 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
98
MTLPixelFormat renderTargetFormat;
Jan 1, 2018
Jan 1, 2018
99
100
101
const char *label;
} METAL_PipelineCache;
Jan 6, 2018
Jan 6, 2018
102
/* Each shader combination used by drawing functions has a separate pipeline
Jan 7, 2018
Jan 7, 2018
103
104
105
106
107
* 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
108
109
typedef struct METAL_ShaderPipelines
{
Jan 7, 2018
Jan 7, 2018
110
MTLPixelFormat renderTargetFormat;
Jan 6, 2018
Jan 6, 2018
111
112
113
METAL_PipelineCache caches[SDL_METAL_FRAGMENT_COUNT];
} METAL_ShaderPipelines;
Dec 8, 2017
Dec 8, 2017
114
@interface METAL_RenderData : NSObject
Dec 8, 2017
Dec 8, 2017
115
116
117
118
119
120
@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
121
122
@property (nonatomic, retain) id<MTLSamplerState> mtlsamplernearest;
@property (nonatomic, retain) id<MTLSamplerState> mtlsamplerlinear;
Jan 3, 2018
Jan 3, 2018
123
@property (nonatomic, retain) id<MTLBuffer> mtlbufconstants;
Nov 22, 2018
Nov 22, 2018
124
@property (nonatomic, retain) id<MTLBuffer> mtlbufquadindices;
Aug 5, 2019
Aug 5, 2019
125
@property (nonatomic, assign) SDL_MetalView mtlview;
Dec 8, 2017
Dec 8, 2017
126
127
@property (nonatomic, retain) CAMetalLayer *mtllayer;
@property (nonatomic, retain) MTLRenderPassDescriptor *mtlpassdesc;
Jan 7, 2018
Jan 7, 2018
128
129
130
@property (nonatomic, assign) METAL_ShaderPipelines *activepipelines;
@property (nonatomic, assign) METAL_ShaderPipelines *allpipelines;
@property (nonatomic, assign) int pipelinescount;
Dec 8, 2017
Dec 8, 2017
131
132
133
@end
@implementation METAL_RenderData
Jan 2, 2018
Jan 2, 2018
134
#if !__has_feature(objc_arc)
Jan 2, 2018
Jan 2, 2018
135
136
137
138
139
140
141
142
143
144
- (void)dealloc
{
[_mtldevice release];
[_mtlcmdqueue release];
[_mtlcmdbuffer release];
[_mtlcmdencoder release];
[_mtllibrary release];
[_mtlbackbuffer release];
[_mtlsamplernearest release];
[_mtlsamplerlinear release];
Jan 3, 2018
Jan 3, 2018
145
[_mtlbufconstants release];
Nov 22, 2018
Nov 22, 2018
146
[_mtlbufquadindices release];
Jan 2, 2018
Jan 2, 2018
147
148
149
150
151
[_mtllayer release];
[_mtlpassdesc release];
[super dealloc];
}
#endif
Dec 8, 2017
Dec 8, 2017
152
@end
Dec 9, 2017
Dec 9, 2017
154
155
@interface METAL_TextureData : NSObject
@property (nonatomic, retain) id<MTLTexture> mtltexture;
Jan 6, 2018
Jan 6, 2018
156
@property (nonatomic, retain) id<MTLTexture> mtltexture_uv;
Jan 1, 2018
Jan 1, 2018
157
@property (nonatomic, retain) id<MTLSamplerState> mtlsampler;
Jan 6, 2018
Jan 6, 2018
158
159
160
161
@property (nonatomic, assign) SDL_MetalFragmentFunction fragmentFunction;
@property (nonatomic, assign) BOOL yuv;
@property (nonatomic, assign) BOOL nv12;
@property (nonatomic, assign) size_t conversionBufferOffset;
Nov 26, 2018
Nov 26, 2018
162
@property (nonatomic, assign) BOOL hasdata;
Dec 18, 2018
Dec 18, 2018
163
164
165
@property (nonatomic, retain) id<MTLBuffer> lockedbuffer;
@property (nonatomic, assign) SDL_Rect lockedrect;
Dec 9, 2017
Dec 9, 2017
166
167
168
@end
@implementation METAL_TextureData
Jan 2, 2018
Jan 2, 2018
169
#if !__has_feature(objc_arc)
Jan 2, 2018
Jan 2, 2018
170
171
172
- (void)dealloc
{
[_mtltexture release];
Jan 6, 2018
Jan 6, 2018
173
[_mtltexture_uv release];
Jan 2, 2018
Jan 2, 2018
174
[_mtlsampler release];
Oct 26, 2019
Oct 26, 2019
175
[_lockedbuffer release];
Jan 2, 2018
Jan 2, 2018
176
177
178
[super dealloc];
}
#endif
Dec 9, 2017
Dec 9, 2017
179
180
@end
181
182
183
static int
IsMetalAvailable(const SDL_SysWMinfo *syswm)
{
Dec 8, 2017
Dec 8, 2017
184
185
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.");
186
187
188
}
// this checks a weak symbol.
Dec 8, 2017
Dec 8, 2017
189
#if (defined(__MACOSX__) && (MAC_OS_X_VERSION_MIN_REQUIRED < 101100))
190
191
192
193
194
195
196
197
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
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
235
236
237
238
239
240
241
242
243
244
245
246
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
247
case SDL_METAL_FRAGMENT_COPY: return @"SDL_Copy_fragment";
Jan 6, 2018
Jan 6, 2018
248
249
250
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
251
252
253
254
default: return nil;
}
}
255
static id<MTLRenderPipelineState>
Jan 1, 2018
Jan 1, 2018
256
257
MakePipelineState(METAL_RenderData *data, METAL_PipelineCache *cache,
NSString *blendlabel, SDL_BlendMode blendmode)
Jan 1, 2018
Jan 1, 2018
259
260
id<MTLFunction> mtlvertfn = [data.mtllibrary newFunctionWithName:GetVertexFunctionName(cache->vertexFunction)];
id<MTLFunction> mtlfragfn = [data.mtllibrary newFunctionWithName:GetFragmentFunctionName(cache->fragmentFunction)];
261
262
263
264
265
266
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
267
Aug 17, 2019
Aug 17, 2019
268
269
270
271
272
273
MTLVertexDescriptor *vertdesc = [MTLVertexDescriptor vertexDescriptor];
switch (cache->vertexFunction) {
case SDL_METAL_VERTEX_SOLID:
/* position (float2) */
vertdesc.layouts[0].stride = sizeof(float) * 2;
Oct 17, 2019
Oct 17, 2019
274
vertdesc.layouts[0].stepFunction = MTLVertexStepFunctionPerVertex;
Aug 17, 2019
Aug 17, 2019
275
276
277
278
279
280
281
282
vertdesc.attributes[0].format = MTLVertexFormatFloat2;
vertdesc.attributes[0].offset = 0;
vertdesc.attributes[0].bufferIndex = 0;
break;
case SDL_METAL_VERTEX_COPY:
/* position (float2), texcoord (float2) */
vertdesc.layouts[0].stride = sizeof(float) * 4;
Oct 17, 2019
Oct 17, 2019
283
vertdesc.layouts[0].stepFunction = MTLVertexStepFunctionPerVertex;
Aug 17, 2019
Aug 17, 2019
284
285
286
287
288
289
290
291
292
293
vertdesc.attributes[0].format = MTLVertexFormatFloat2;
vertdesc.attributes[0].offset = 0;
vertdesc.attributes[0].bufferIndex = 0;
vertdesc.attributes[1].format = MTLVertexFormatFloat2;
vertdesc.attributes[1].offset = sizeof(float) * 2;
vertdesc.attributes[1].bufferIndex = 0;
break;
}
Jan 1, 2018
Jan 1, 2018
294
Aug 17, 2019
Aug 17, 2019
295
296
297
mtlpipedesc.vertexDescriptor = vertdesc;
MTLRenderPipelineColorAttachmentDescriptor *rtdesc = mtlpipedesc.colorAttachments[0];
Jan 7, 2018
Jan 7, 2018
298
rtdesc.pixelFormat = cache->renderTargetFormat;
Jan 1, 2018
Jan 1, 2018
299
300
301
302
303
304
305
306
307
308
309
310
311
312
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];
313
314
NSError *err = nil;
Jan 1, 2018
Jan 1, 2018
315
id<MTLRenderPipelineState> state = [data.mtldevice newRenderPipelineStateWithDescriptor:mtlpipedesc error:&err];
316
SDL_assert(err == nil);
Jan 1, 2018
Jan 1, 2018
317
318
319
320
321
322
323
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
324
#if !__has_feature(objc_arc)
325
326
327
[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
328
[state release];
Dec 8, 2017
Dec 8, 2017
329
#endif
Jan 1, 2018
Jan 1, 2018
330
331
332
333
334
335
336
337
338
339
340
341
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
342
static void
Jan 7, 2018
Jan 7, 2018
343
344
MakePipelineCache(METAL_RenderData *data, METAL_PipelineCache *cache, const char *label,
MTLPixelFormat rtformat, SDL_MetalVertexFunction vertfn, SDL_MetalFragmentFunction fragfn)
Jan 1, 2018
Jan 1, 2018
345
346
347
348
349
{
SDL_zerop(cache);
cache->vertexFunction = vertfn;
cache->fragmentFunction = fragfn;
Jan 7, 2018
Jan 7, 2018
350
cache->renderTargetFormat = rtformat;
Jan 1, 2018
Jan 1, 2018
351
352
353
354
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
355
356
357
358
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);
Jan 16, 2020
Jan 16, 2020
359
MakePipelineState(data, cache, @" (blend=mul)", SDL_BLENDMODE_MUL);
360
361
362
}
static void
Jan 1, 2018
Jan 1, 2018
363
DestroyPipelineCache(METAL_PipelineCache *cache)
Jan 1, 2018
Jan 1, 2018
365
366
367
368
369
370
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
371
372
373
}
}
Jan 7, 2018
Jan 7, 2018
374
375
376
377
378
379
380
381
382
383
384
385
386
387
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
388
static METAL_ShaderPipelines *
Jan 7, 2018
Jan 7, 2018
389
ChooseShaderPipelines(METAL_RenderData *data, MTLPixelFormat rtformat)
Jan 6, 2018
Jan 6, 2018
390
{
Jan 7, 2018
Jan 7, 2018
391
392
393
394
395
396
397
398
399
400
401
402
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
403
404
405
406
SDL_OutOfMemory();
return NULL;
}
Jan 7, 2018
Jan 7, 2018
407
408
409
410
MakeShaderPipelines(data, &allpipelines[count], rtformat);
data.allpipelines = allpipelines;
data.pipelinescount = count + 1;
Jan 6, 2018
Jan 6, 2018
411
Jan 7, 2018
Jan 7, 2018
412
return &data.allpipelines[count];
Jan 6, 2018
Jan 6, 2018
413
414
415
}
static void
Jan 7, 2018
Jan 7, 2018
416
DestroyAllPipelines(METAL_ShaderPipelines *allpipelines, int count)
Jan 6, 2018
Jan 6, 2018
417
{
Jan 7, 2018
Jan 7, 2018
418
419
420
421
422
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
423
424
}
Jan 7, 2018
Jan 7, 2018
425
SDL_free(allpipelines);
Jan 1, 2018
Jan 1, 2018
426
}
427
428
429
}
static inline id<MTLRenderPipelineState>
Jan 6, 2018
Jan 6, 2018
430
ChoosePipelineState(METAL_RenderData *data, METAL_ShaderPipelines *pipelines, SDL_MetalFragmentFunction fragfn, SDL_BlendMode blendmode)
Jan 6, 2018
Jan 6, 2018
432
433
METAL_PipelineCache *cache = &pipelines->caches[fragfn];
Jan 1, 2018
Jan 1, 2018
434
435
436
437
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
439
Jan 6, 2018
Jan 6, 2018
440
return MakePipelineState(data, cache, [NSString stringWithFormat:@" (blend=custom 0x%x)", blendmode], blendmode);
Sep 24, 2018
Sep 24, 2018
443
static void
Aug 17, 2019
Aug 17, 2019
444
METAL_ActivateRenderCommandEncoder(SDL_Renderer * renderer, MTLLoadAction load, MTLClearColor *clear_color, id<MTLBuffer> vertex_buffer)
Sep 24, 2018
Sep 24, 2018
445
446
{
METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
Sep 24, 2018
Sep 24, 2018
448
449
450
451
/* 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;
Sep 24, 2018
Sep 24, 2018
453
454
455
456
457
458
459
460
461
462
463
464
465
466
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;
}
Sep 24, 2018
Sep 24, 2018
468
SDL_assert(mtltexture);
Sep 24, 2018
Sep 24, 2018
470
471
472
473
if (load == MTLLoadActionClear) {
SDL_assert(clear_color != NULL);
data.mtlpassdesc.colorAttachments[0].clearColor = *clear_color;
}
Dec 8, 2017
Dec 8, 2017
474
Sep 24, 2018
Sep 24, 2018
475
476
data.mtlpassdesc.colorAttachments[0].loadAction = load;
data.mtlpassdesc.colorAttachments[0].texture = mtltexture;
Sep 24, 2018
Sep 24, 2018
478
479
data.mtlcmdbuffer = [data.mtlcmdqueue commandBuffer];
data.mtlcmdencoder = [data.mtlcmdbuffer renderCommandEncoderWithDescriptor:data.mtlpassdesc];
Sep 24, 2018
Sep 24, 2018
481
482
483
484
485
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 8, 2018
Jan 8, 2018
486
Aug 17, 2019
Aug 17, 2019
487
488
489
490
491
492
493
/* Set up buffer bindings for positions, texcoords, and color once here,
* the offsets are adjusted in the code that uses them. */
if (vertex_buffer != nil) {
[data.mtlcmdencoder setVertexBuffer:vertex_buffer offset:0 atIndex:0];
[data.mtlcmdencoder setFragmentBuffer:vertex_buffer offset:0 atIndex:0];
}
Sep 24, 2018
Sep 24, 2018
494
data.activepipelines = ChooseShaderPipelines(data, mtltexture.pixelFormat);
Sep 24, 2018
Sep 24, 2018
496
497
498
499
500
501
// 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 31, 2017
Dec 31, 2017
502
Sep 24, 2018
Sep 24, 2018
503
504
505
506
507
508
509
510
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
}
}
Dec 8, 2017
Dec 8, 2017
511
Sep 24, 2018
Sep 24, 2018
512
513
514
515
516
517
518
519
520
521
522
523
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;
}}
Jan 1, 2018
Jan 1, 2018
524
Sep 24, 2018
Sep 24, 2018
525
526
527
528
529
530
531
532
533
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);
Sep 24, 2018
Sep 24, 2018
535
536
537
538
539
540
541
542
543
544
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;
}
Dec 8, 2017
Dec 8, 2017
545
Sep 24, 2018
Sep 24, 2018
546
547
548
549
550
static int
METAL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
{ @autoreleasepool {
METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
MTLPixelFormat pixfmt;
Dec 8, 2017
Dec 8, 2017
551
Sep 24, 2018
Sep 24, 2018
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
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));
}
Jan 1, 2018
Jan 1, 2018
568
Sep 24, 2018
Sep 24, 2018
569
570
MTLTextureDescriptor *mtltexdesc = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:pixfmt
width:(NSUInteger)texture->w height:(NSUInteger)texture->h mipmapped:NO];
Jan 1, 2018
Jan 1, 2018
571
Sep 24, 2018
Sep 24, 2018
572
573
574
575
576
577
578
579
580
581
582
583
584
/* 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");
}
Jan 1, 2018
Jan 1, 2018
585
Sep 24, 2018
Sep 24, 2018
586
id<MTLTexture> mtltexture_uv = nil;
Jan 1, 2018
Jan 1, 2018
587
Sep 24, 2018
Sep 24, 2018
588
589
BOOL yuv = (texture->format == SDL_PIXELFORMAT_IYUV) || (texture->format == SDL_PIXELFORMAT_YV12);
BOOL nv12 = (texture->format == SDL_PIXELFORMAT_NV12) || (texture->format == SDL_PIXELFORMAT_NV21);
Sep 24, 2018
Sep 24, 2018
591
592
593
594
595
596
597
598
599
600
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;
Oct 12, 2018
Oct 12, 2018
601
602
}
Nov 1, 2018
Nov 1, 2018
603
if (yuv || nv12) {
Sep 24, 2018
Sep 24, 2018
604
mtltexture_uv = [data.mtldevice newTextureWithDescriptor:mtltexdesc];
Oct 12, 2018
Oct 12, 2018
605
606
607
608
609
610
if (mtltexture_uv == nil) {
#if !__has_feature(objc_arc)
[mtltexture release];
#endif
return SDL_SetError("Texture allocation failed");
}
Sep 24, 2018
Sep 24, 2018
611
}
Jan 3, 2018
Jan 3, 2018
612
Sep 24, 2018
Sep 24, 2018
613
614
615
616
617
618
619
620
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;
Jan 6, 2018
Jan 6, 2018
621
Sep 24, 2018
Sep 24, 2018
622
623
texturedata.yuv = yuv;
texturedata.nv12 = nv12;
Jan 6, 2018
Jan 6, 2018
624
Sep 24, 2018
Sep 24, 2018
625
626
627
628
629
630
631
632
633
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;
}
Jan 6, 2018
Jan 6, 2018
634
Sep 24, 2018
Sep 24, 2018
635
636
637
638
639
640
641
642
643
644
645
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;
}
Jan 3, 2018
Jan 3, 2018
646
Sep 24, 2018
Sep 24, 2018
647
texture->driverdata = (void*)CFBridgingRetain(texturedata);
Jan 3, 2018
Jan 3, 2018
648
Sep 24, 2018
Sep 24, 2018
649
650
651
652
653
#if !__has_feature(objc_arc)
[texturedata release];
[mtltexture release];
[mtltexture_uv release];
#endif
Jan 3, 2018
Jan 3, 2018
654
Sep 24, 2018
Sep 24, 2018
655
656
return 0;
}}
Jan 5, 2018
Jan 5, 2018
657
Nov 26, 2018
Nov 26, 2018
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
static void
METAL_UploadTextureData(id<MTLTexture> texture, SDL_Rect rect, int slice,
const void * pixels, int pitch)
{
[texture replaceRegion:MTLRegionMake2D(rect.x, rect.y, rect.w, rect.h)
mipmapLevel:0
slice:slice
withBytes:pixels
bytesPerRow:pitch
bytesPerImage:0];
}
static MTLStorageMode
METAL_GetStorageMode(id<MTLResource> resource)
{
/* iOS 8 does not have this method. */
if ([resource respondsToSelector:@selector(storageMode)]) {
return resource.storageMode;
}
return MTLStorageModeShared;
}
static int
METAL_UpdateTextureInternal(SDL_Renderer * renderer, METAL_TextureData *texturedata,
id<MTLTexture> texture, SDL_Rect rect, int slice,
const void * pixels, int pitch)
{
METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
SDL_Rect stagingrect = {0, 0, rect.w, rect.h};
MTLTextureDescriptor *desc;
/* If the texture is managed or shared and this is the first upload, we can
* use replaceRegion to upload to it directly. Otherwise we upload the data
* to a staging texture and copy that over. */
if (!texturedata.hasdata && METAL_GetStorageMode(texture) != MTLStorageModePrivate) {
METAL_UploadTextureData(texture, rect, slice, pixels, pitch);
return 0;
}
desc = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:texture.pixelFormat
width:rect.w
height:rect.h
mipmapped:NO];
if (desc == nil) {
return SDL_OutOfMemory();
}
/* TODO: We could have a pool of textures or a MTLHeap we allocate from,
* and release a staging texture back to the pool in the command buffer's
* completion handler. */
id<MTLTexture> stagingtex = [data.mtldevice newTextureWithDescriptor:desc];
if (stagingtex == nil) {
return SDL_OutOfMemory();
}
#if !__has_feature(objc_arc)
[stagingtex autorelease];
#endif
METAL_UploadTextureData(stagingtex, stagingrect, 0, pixels, pitch);
if (data.mtlcmdencoder != nil) {
[data.mtlcmdencoder endEncoding];
data.mtlcmdencoder = nil;
}
if (data.mtlcmdbuffer == nil) {
data.mtlcmdbuffer = [data.mtlcmdqueue commandBuffer];
}
id<MTLBlitCommandEncoder> blitcmd = [data.mtlcmdbuffer blitCommandEncoder];
[blitcmd copyFromTexture:stagingtex
sourceSlice:0
sourceLevel:0
sourceOrigin:MTLOriginMake(0, 0, 0)
sourceSize:MTLSizeMake(rect.w, rect.h, 1)
toTexture:texture
destinationSlice:slice
destinationLevel:0
destinationOrigin:MTLOriginMake(rect.x, rect.y, 0)];
[blitcmd endEncoding];
/* TODO: This isn't very efficient for the YUV formats, which call
* UpdateTextureInternal multiple times in a row. */
[data.mtlcmdbuffer commit];
data.mtlcmdbuffer = nil;
return 0;
}
Sep 24, 2018
Sep 24, 2018
751
752
static int
METAL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
Nov 26, 2018
Nov 26, 2018
753
const SDL_Rect * rect, const void *pixels, int pitch)
Sep 24, 2018
Sep 24, 2018
754
755
{ @autoreleasepool {
METAL_TextureData *texturedata = (__bridge METAL_TextureData *)texture->driverdata;
Jan 5, 2018
Jan 5, 2018
756
Nov 26, 2018
Nov 26, 2018
757
758
759
if (METAL_UpdateTextureInternal(renderer, texturedata, texturedata.mtltexture, *rect, 0, pixels, pitch) < 0) {
return -1;
}
Jan 5, 2018
Jan 5, 2018
760
Sep 24, 2018
Sep 24, 2018
761
762
763
if (texturedata.yuv) {
int Uslice = texture->format == SDL_PIXELFORMAT_YV12 ? 1 : 0;
int Vslice = texture->format == SDL_PIXELFORMAT_YV12 ? 0 : 1;
Nov 26, 2018
Nov 26, 2018
764
765
int UVpitch = (pitch + 1) / 2;
SDL_Rect UVrect = {rect->x / 2, rect->y / 2, (rect->w + 1) / 2, (rect->h + 1) / 2};
Jan 1, 2018
Jan 1, 2018
766
Sep 24, 2018
Sep 24, 2018
767
768
/* Skip to the correct offset into the next texture */
pixels = (const void*)((const Uint8*)pixels + rect->h * pitch);
Nov 26, 2018
Nov 26, 2018
769
770
771
if (METAL_UpdateTextureInternal(renderer, texturedata, texturedata.mtltexture_uv, UVrect, Uslice, pixels, UVpitch) < 0) {
return -1;
}
Sep 24, 2018
Sep 24, 2018
773
/* Skip to the correct offset into the next texture */
Nov 26, 2018
Nov 26, 2018
774
775
776
777
pixels = (const void*)((const Uint8*)pixels + UVrect.h * UVpitch);
if (METAL_UpdateTextureInternal(renderer, texturedata, texturedata.mtltexture_uv, UVrect, Vslice, pixels, UVpitch) < 0) {
return -1;
}
Sep 24, 2018
Sep 24, 2018
778
}
Sep 24, 2018
Sep 24, 2018
780
if (texturedata.nv12) {
Nov 26, 2018
Nov 26, 2018
781
782
783
SDL_Rect UVrect = {rect->x / 2, rect->y / 2, (rect->w + 1) / 2, (rect->h + 1) / 2};
int UVpitch = 2 * ((pitch + 1) / 2);
Sep 24, 2018
Sep 24, 2018
784
785
/* Skip to the correct offset into the next texture */
pixels = (const void*)((const Uint8*)pixels + rect->h * pitch);
Nov 26, 2018
Nov 26, 2018
786
787
788
if (METAL_UpdateTextureInternal(renderer, texturedata, texturedata.mtltexture_uv, UVrect, 0, pixels, UVpitch) < 0) {
return -1;
}
Sep 24, 2018
Sep 24, 2018
789
}
Nov 26, 2018
Nov 26, 2018
791
792
texturedata.hasdata = YES;
Sep 24, 2018
Sep 24, 2018
793
794
return 0;
}}
Sep 24, 2018
Sep 24, 2018
795
Sep 24, 2018
Sep 24, 2018
796
797
798
799
800
801
802
803
804
805
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;
Nov 26, 2018
Nov 26, 2018
806
SDL_Rect UVrect = {rect->x / 2, rect->y / 2, (rect->w + 1) / 2, (rect->h + 1) / 2};
Sep 24, 2018
Sep 24, 2018
808
809
810
/* Bail out if we're supposed to update an empty rectangle */
if (rect->w <= 0 || rect->h <= 0) {
return 0;
Jan 8, 2018
Jan 8, 2018
811
812
}
Nov 26, 2018
Nov 26, 2018
813
814
815
816
817
818
819
820
821
822
823
if (METAL_UpdateTextureInternal(renderer, texturedata, texturedata.mtltexture, *rect, 0, Yplane, Ypitch) < 0) {
return -1;
}
if (METAL_UpdateTextureInternal(renderer, texturedata, texturedata.mtltexture_uv, UVrect, Uslice, Uplane, Upitch)) {
return -1;
}
if (METAL_UpdateTextureInternal(renderer, texturedata, texturedata.mtltexture_uv, UVrect, Vslice, Vplane, Vpitch)) {
return -1;
}
texturedata.hasdata = YES;
Sep 24, 2018
Sep 24, 2018
824
825
return 0;
Jan 8, 2018
Jan 8, 2018
826
}}
Sep 24, 2018
Sep 24, 2018
828
829
830
static int
METAL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, void **pixels, int *pitch)
Dec 18, 2018
Dec 18, 2018
831
832
833
{ @autoreleasepool {
METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
METAL_TextureData *texturedata = (__bridge METAL_TextureData *)texture->driverdata;
Dec 19, 2018
Dec 19, 2018
834
int buffersize = 0;
Oct 26, 2019
Oct 26, 2019
835
id<MTLBuffer> lockedbuffer = nil;
Dec 18, 2018
Dec 18, 2018
836
Dec 19, 2018
Dec 19, 2018
837
838
if (rect->w <= 0 || rect->h <= 0) {
return SDL_SetError("Invalid rectangle dimensions for LockTexture.");
Dec 18, 2018
Dec 18, 2018
839
840
841
842
}
*pitch = SDL_BYTESPERPIXEL(texture->format) * rect->w;
Dec 19, 2018
Dec 19, 2018
843
844
845
846
847
848
if (texturedata.yuv || texturedata.nv12) {
buffersize = ((*pitch) * rect->h) + (2 * (*pitch + 1) / 2) * ((rect->h + 1) / 2);
} else {
buffersize = (*pitch) * rect->h;
}
Oct 26, 2019
Oct 26, 2019
849
850
lockedbuffer = [data.mtldevice newBufferWithLength:buffersize options:MTLResourceStorageModeShared];
if (lockedbuffer == nil) {
Dec 18, 2018
Dec 18, 2018
851
852
853
return SDL_OutOfMemory();
}
Oct 26, 2019
Oct 26, 2019
854
855
856
857
858
859
860
861
texturedata.lockedrect = *rect;
texturedata.lockedbuffer = lockedbuffer;
*pixels = [lockedbuffer contents];
/* METAL_TextureData.lockedbuffer retains. */
#if !__has_feature(objc_arc)
[lockedbuffer release];
#endif
Dec 18, 2018
Dec 18, 2018
862
863
864
return 0;
}}
Sep 24, 2018
Sep 24, 2018
865
Jan 2, 2018
Jan 2, 2018
866
static void
Sep 24, 2018
Sep 24, 2018
867
METAL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
Dec 18, 2018
Dec 18, 2018
868
869
870
871
{ @autoreleasepool {
METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
METAL_TextureData *texturedata = (__bridge METAL_TextureData *)texture->driverdata;
SDL_Rect rect = texturedata.lockedrect;
Dec 19, 2018
Dec 19, 2018
872
873
int pitch = SDL_BYTESPERPIXEL(texture->format) * rect.w;
SDL_Rect UVrect = {rect.x / 2, rect.y / 2, (rect.w + 1) / 2, (rect.h + 1) / 2};
Dec 18, 2018
Dec 18, 2018
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
if (texturedata.lockedbuffer == nil) {
return;
}
if (data.mtlcmdencoder != nil) {
[data.mtlcmdencoder endEncoding];
data.mtlcmdencoder = nil;
}
if (data.mtlcmdbuffer == nil) {
data.mtlcmdbuffer = [data.mtlcmdqueue commandBuffer];
}
id<MTLBlitCommandEncoder> blitcmd = [data.mtlcmdbuffer blitCommandEncoder];
[blitcmd copyFromBuffer:texturedata.lockedbuffer
sourceOffset:0
Dec 19, 2018
Dec 19, 2018
892
sourceBytesPerRow:pitch
Dec 18, 2018
Dec 18, 2018
893
894
895
896
897
898
899
sourceBytesPerImage:0
sourceSize:MTLSizeMake(rect.w, rect.h, 1)
toTexture:texturedata.mtltexture
destinationSlice:0
destinationLevel:0
destinationOrigin:MTLOriginMake(rect.x, rect.y, 0)];
Dec 19, 2018
Dec 19, 2018
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
if (texturedata.yuv) {
int Uslice = texture->format == SDL_PIXELFORMAT_YV12 ? 1 : 0;
int Vslice = texture->format == SDL_PIXELFORMAT_YV12 ? 0 : 1;
int UVpitch = (pitch + 1) / 2;
[blitcmd copyFromBuffer:texturedata.lockedbuffer
sourceOffset:rect.h * pitch
sourceBytesPerRow:UVpitch
sourceBytesPerImage:UVpitch * UVrect.h
sourceSize:MTLSizeMake(UVrect.w, UVrect.h, 1)
toTexture:texturedata.mtltexture_uv
destinationSlice:Uslice
destinationLevel:0
destinationOrigin:MTLOriginMake(UVrect.x, UVrect.y, 0)];
[blitcmd copyFromBuffer:texturedata.lockedbuffer
sourceOffset:(rect.h * pitch) + UVrect.h * UVpitch
sourceBytesPerRow:UVpitch
sourceBytesPerImage:UVpitch * UVrect.h
sourceSize:MTLSizeMake(UVrect.w, UVrect.h, 1)
toTexture:texturedata.mtltexture_uv
destinationSlice:Vslice
destinationLevel:0
destinationOrigin:MTLOriginMake(UVrect.x, UVrect.y, 0)];
}
if (texturedata.nv12) {
int UVpitch = 2 * ((pitch + 1) / 2);
[blitcmd copyFromBuffer:texturedata.lockedbuffer
sourceOffset:rect.h * pitch
sourceBytesPerRow:UVpitch
sourceBytesPerImage:0
sourceSize:MTLSizeMake(UVrect.w, UVrect.h, 1)
toTexture:texturedata.mtltexture_uv
destinationSlice:0
destinationLevel:0
destinationOrigin:MTLOriginMake(UVrect.x, UVrect.y, 0)];
}
Dec 18, 2018
Dec 18, 2018
940
941
942
943
944
[blitcmd endEncoding];
[data.mtlcmdbuffer commit];
data.mtlcmdbuffer = nil;
Oct 26, 2019
Oct 26, 2019
945
texturedata.lockedbuffer = nil; /* Retained property, so it calls release. */
Dec 18, 2018
Dec 18, 2018
946
947
texturedata.hasdata = YES;
}}
Dec 22, 2019
Dec 22, 2019
949
950
951
static void
METAL_SetTextureScaleMode(SDL_Renderer * renderer, SDL_Texture * texture, SDL_ScaleMode scaleMode)
{ @autoreleasepool {
Dec 23, 2019
Dec 23, 2019
952
METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
Dec 22, 2019
Dec 22, 2019
953
954
955
956
957
958
959
960
961
METAL_TextureData *texturedata = (__bridge METAL_TextureData *)texture->driverdata;
if (scaleMode == SDL_ScaleModeNearest) {
texturedata.mtlsampler = data.mtlsamplernearest;
} else {
texturedata.mtlsampler = data.mtlsamplerlinear;
}
}}
Sep 24, 2018
Sep 24, 2018
962
963
964
965
static int
METAL_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture)
{ @autoreleasepool {
METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
Jan 4, 2018
Jan 4, 2018
966
Sep 24, 2018
Sep 24, 2018
967
968
969
970
971
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];
Jan 4, 2018
Jan 4, 2018
972
Sep 24, 2018
Sep 24, 2018
973
974
975
data.mtlcmdencoder = nil;
data.mtlcmdbuffer = nil;
}
Dec 8, 2017
Dec 8, 2017
976
Sep 24, 2018
Sep 24, 2018
977
978
979
980
981
/* 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;
}}
Jan 4, 2018
Jan 4, 2018
982
Sep 24, 2018
Sep 24, 2018
983
Sep 24, 2018
Sep 24, 2018
984
985
986
987
988
// 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;
Sep 24, 2018
Sep 24, 2018
991
992
static int
METAL_QueueSetViewport(SDL_Renderer * renderer, SDL_RenderCommand *cmd)
Sep 24, 2018
Sep 24, 2018
994
995
996
997
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);
Aug 17, 2019
Aug 17, 2019
998
float *matrix = (float *) SDL_AllocateRenderVertices(renderer, matrixlen, CONSTANT_ALIGN(16), &cmd->data.viewport.first);
Sep 24, 2018
Sep 24, 2018
999
1000
if (!matrix) {
return -1;