Skip to content

Latest commit

 

History

History
1121 lines (958 loc) · 42 KB

SDL_render_metal.m

File metadata and controls

1121 lines (958 loc) · 42 KB
 
1
2
/*
Simple DirectMedia Layer
Dec 8, 2017
Dec 8, 2017
3
Copyright (C) 1997-2017 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
Dec 31, 2017
Dec 31, 2017
36
37
#import <Metal/Metal.h>
#import <QuartzCore/CAMetalLayer.h>
Dec 8, 2017
Dec 8, 2017
39
40
41
42
43
44
/* Regenerate these with build-metal-shaders.sh */
#ifdef __MACOSX__
#include "SDL_shaders_metal_osx.h"
#else
#include "SDL_shaders_metal_ios.h"
#endif
45
46
47
48
49
50
51
/* 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
52
static SDL_bool METAL_SupportsBlendMode(SDL_Renderer * renderer, SDL_BlendMode blendMode);
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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);
static int METAL_UpdateViewport(SDL_Renderer * renderer);
static int METAL_UpdateClipRect(SDL_Renderer * renderer);
static int METAL_RenderClear(SDL_Renderer * renderer);
static int METAL_RenderDrawPoints(SDL_Renderer * renderer,
const SDL_FPoint * points, int count);
static int METAL_RenderDrawLines(SDL_Renderer * renderer,
const SDL_FPoint * points, int count);
static int METAL_RenderFillRects(SDL_Renderer * renderer,
const SDL_FRect * rects, int count);
static int METAL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * srcrect, const SDL_FRect * dstrect);
static int METAL_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * srcrect, const SDL_FRect * dstrect,
const double angle, const SDL_FPoint *center, const SDL_RendererFlip flip);
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
85
86
static void *METAL_GetMetalLayer(SDL_Renderer * renderer);
static void *METAL_GetMetalCommandEncoder(SDL_Renderer * renderer);
87
88
89
90
91
92
93
94
SDL_RenderDriver METAL_RenderDriver = {
METAL_CreateRenderer,
{
"metal",
(SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE),
2,
{SDL_PIXELFORMAT_ARGB8888, SDL_PIXELFORMAT_ABGR8888},
Dec 9, 2017
Dec 9, 2017
95
96
97
98
99
// !!! FIXME: how do you query Metal for this?
// (the weakest GPU supported by Metal on iOS has 4k texture max, and
// other models might be 2x or 4x more. On macOS, it's 16k across the
// board right now.)
Dec 9, 2017
Dec 9, 2017
100
101
102
103
104
105
#ifdef __MACOSX__
16384, 16384
#else
4096, 4096
#endif
}
Jan 1, 2018
Jan 1, 2018
108
109
110
111
112
113
114
115
116
typedef enum SDL_MetalVertexFunction
{
SDL_METAL_VERTEX_SOLID,
SDL_METAL_VERTEX_COPY,
} SDL_MetalVertexFunction;
typedef enum SDL_MetalFragmentFunction
{
SDL_METAL_FRAGMENT_SOLID,
Jan 1, 2018
Jan 1, 2018
117
SDL_METAL_FRAGMENT_COPY,
Jan 1, 2018
Jan 1, 2018
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
} 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;
const char *label;
} METAL_PipelineCache;
Dec 8, 2017
Dec 8, 2017
135
@interface METAL_RenderData : NSObject
Dec 8, 2017
Dec 8, 2017
136
137
138
139
140
141
142
@property (nonatomic, assign) BOOL beginScene;
@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 2, 2018
Jan 2, 2018
143
144
@property (nonatomic, assign) METAL_PipelineCache *mtlpipelineprims;
@property (nonatomic, assign) METAL_PipelineCache *mtlpipelinecopy;
Jan 1, 2018
Jan 1, 2018
145
146
@property (nonatomic, retain) id<MTLSamplerState> mtlsamplernearest;
@property (nonatomic, retain) id<MTLSamplerState> mtlsamplerlinear;
Dec 8, 2017
Dec 8, 2017
147
@property (nonatomic, retain) id<MTLBuffer> mtlbufclearverts;
Jan 1, 2018
Jan 1, 2018
148
@property (nonatomic, retain) id<MTLBuffer> mtlbufidentitytransform;
Dec 8, 2017
Dec 8, 2017
149
150
@property (nonatomic, retain) CAMetalLayer *mtllayer;
@property (nonatomic, retain) MTLRenderPassDescriptor *mtlpassdesc;
Dec 8, 2017
Dec 8, 2017
151
152
153
@end
@implementation METAL_RenderData
Jan 2, 2018
Jan 2, 2018
154
#if !__has_feature(objc_arc)
Jan 2, 2018
Jan 2, 2018
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
- (void)dealloc
{
[_mtldevice release];
[_mtlcmdqueue release];
[_mtlcmdbuffer release];
[_mtlcmdencoder release];
[_mtllibrary release];
[_mtlbackbuffer release];
[_mtlsamplernearest release];
[_mtlsamplerlinear release];
[_mtlbufclearverts release];
[_mtlbufidentitytransform release];
[_mtllayer release];
[_mtlpassdesc release];
[super dealloc];
}
#endif
Dec 8, 2017
Dec 8, 2017
172
@end
Dec 9, 2017
Dec 9, 2017
174
175
@interface METAL_TextureData : NSObject
@property (nonatomic, retain) id<MTLTexture> mtltexture;
Jan 1, 2018
Jan 1, 2018
176
@property (nonatomic, retain) id<MTLSamplerState> mtlsampler;
Dec 9, 2017
Dec 9, 2017
177
178
179
@end
@implementation METAL_TextureData
Jan 2, 2018
Jan 2, 2018
180
#if !__has_feature(objc_arc)
Jan 2, 2018
Jan 2, 2018
181
182
183
184
185
186
187
- (void)dealloc
{
[_mtltexture release];
[_mtlsampler release];
[super dealloc];
}
#endif
Dec 9, 2017
Dec 9, 2017
188
189
@end
190
191
192
static int
IsMetalAvailable(const SDL_SysWMinfo *syswm)
{
Dec 8, 2017
Dec 8, 2017
193
194
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.");
195
196
197
}
// this checks a weak symbol.
Dec 8, 2017
Dec 8, 2017
198
#if (defined(__MACOSX__) && (MAC_OS_X_VERSION_MIN_REQUIRED < 101100))
199
200
201
202
203
204
205
206
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
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
247
248
249
250
251
252
253
254
255
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
256
case SDL_METAL_FRAGMENT_COPY: return @"SDL_Copy_fragment";
Jan 1, 2018
Jan 1, 2018
257
258
259
260
default: return nil;
}
}
261
static id<MTLRenderPipelineState>
Jan 1, 2018
Jan 1, 2018
262
263
MakePipelineState(METAL_RenderData *data, METAL_PipelineCache *cache,
NSString *blendlabel, SDL_BlendMode blendmode)
Jan 1, 2018
Jan 1, 2018
265
266
id<MTLFunction> mtlvertfn = [data.mtllibrary newFunctionWithName:GetVertexFunctionName(cache->vertexFunction)];
id<MTLFunction> mtlfragfn = [data.mtllibrary newFunctionWithName:GetFragmentFunctionName(cache->fragmentFunction)];
267
268
269
270
271
272
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
MTLRenderPipelineColorAttachmentDescriptor *rtdesc = mtlpipedesc.colorAttachments[0];
// !!! FIXME: This should be part of the pipeline state cache.
rtdesc.pixelFormat = data.mtllayer.pixelFormat;
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];
292
293
NSError *err = nil;
Jan 1, 2018
Jan 1, 2018
294
id<MTLRenderPipelineState> state = [data.mtldevice newRenderPipelineStateWithDescriptor:mtlpipedesc error:&err];
295
SDL_assert(err == nil);
Jan 1, 2018
Jan 1, 2018
296
297
298
299
300
301
302
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
303
#if !__has_feature(objc_arc)
304
305
306
[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
307
[state release];
Dec 8, 2017
Dec 8, 2017
308
#endif
Jan 1, 2018
Jan 1, 2018
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
if (states) {
states[cache->count++] = pipeline;
cache->states = states;
return (__bridge id<MTLRenderPipelineState>)pipeline.pipe;
} else {
CFBridgingRelease(pipeline.pipe);
SDL_OutOfMemory();
return NULL;
}
}
static METAL_PipelineCache *
MakePipelineCache(METAL_RenderData *data, const char *label, SDL_MetalVertexFunction vertfn, SDL_MetalFragmentFunction fragfn)
{
METAL_PipelineCache *cache = SDL_malloc(sizeof(METAL_PipelineCache));
if (!cache) {
SDL_OutOfMemory();
return NULL;
}
SDL_zerop(cache);
cache->vertexFunction = vertfn;
cache->fragmentFunction = fragfn;
cache->label = label;
/* Create pipeline states for the default blend modes. Custom blend modes
* will be added to the cache on-demand. */
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);
return cache;
345
346
347
}
static void
Jan 1, 2018
Jan 1, 2018
348
DestroyPipelineCache(METAL_PipelineCache *cache)
Jan 1, 2018
Jan 1, 2018
350
351
352
353
354
355
356
357
if (cache != NULL) {
for (int i = 0; i < cache->count; i++) {
CFBridgingRelease(cache->states[i].pipe);
}
SDL_free(cache->states);
SDL_free(cache);
}
358
359
360
}
static inline id<MTLRenderPipelineState>
Jan 1, 2018
Jan 1, 2018
361
ChoosePipelineState(METAL_RenderData *data, METAL_PipelineCache *cache, const SDL_BlendMode blendmode)
Jan 1, 2018
Jan 1, 2018
363
364
365
366
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
368
369
return MakePipelineState(data, cache, [NSString stringWithFormat:@"(blend=custom 0x%x)", blendmode], blendmode);
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
}
static SDL_Renderer *
METAL_CreateRenderer(SDL_Window * window, Uint32 flags)
{
SDL_Renderer *renderer = NULL;
METAL_RenderData *data = NULL;
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;
}
Dec 8, 2017
Dec 8, 2017
394
data = [[METAL_RenderData alloc] init];
Dec 8, 2017
Dec 8, 2017
395
data.beginScene = YES;
Dec 8, 2017
Dec 8, 2017
396
397
renderer->driverdata = (void*)CFBridgingRetain(data);
398
399
renderer->window = window;
Dec 8, 2017
Dec 8, 2017
400
401
402
#ifdef __MACOSX__
id<MTLDevice> mtldevice = MTLCreateSystemDefaultDevice(); // !!! FIXME: MTLCopyAllDevices() can find other GPUs...
if (mtldevice == nil) {
403
SDL_free(renderer);
Dec 8, 2017
Dec 8, 2017
404
405
406
#if !__has_feature(objc_arc)
[data release];
#endif
407
408
409
410
411
412
SDL_SetError("Failed to obtain Metal device");
return NULL;
}
// !!! FIXME: error checking on all of this.
Dec 31, 2017
Dec 31, 2017
413
414
NSView *view = Cocoa_Mtl_AddMetalView(window);
CAMetalLayer *layer = (CAMetalLayer *)[view layer];
Dec 8, 2017
Dec 8, 2017
416
layer.device = mtldevice;
Dec 31, 2017
Dec 31, 2017
417
418
419
//layer.colorspace = nil;
Dec 8, 2017
Dec 8, 2017
420
#else
Dec 8, 2017
Dec 8, 2017
421
422
UIView *view = UIKit_Mtl_AddMetalView(window);
CAMetalLayer *layer = (CAMetalLayer *)[view layer];
Dec 8, 2017
Dec 8, 2017
423
424
#endif
Jan 1, 2018
Jan 1, 2018
425
426
427
// Necessary for RenderReadPixels.
layer.framebufferOnly = NO;
Dec 8, 2017
Dec 8, 2017
428
429
data.mtldevice = layer.device;
data.mtllayer = layer;
Jan 2, 2018
Jan 2, 2018
430
431
id<MTLCommandQueue> mtlcmdqueue = [data.mtldevice newCommandQueue];
data.mtlcmdqueue = mtlcmdqueue;
Dec 8, 2017
Dec 8, 2017
432
data.mtlcmdqueue.label = @"SDL Metal Renderer";
Dec 9, 2017
Dec 9, 2017
433
data.mtlpassdesc = [MTLRenderPassDescriptor renderPassDescriptor];
Dec 8, 2017
Dec 8, 2017
435
436
437
438
439
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
440
441
id<MTLLibrary> mtllibrary = [data.mtldevice newLibraryWithData:mtllibdata error:&err];
data.mtllibrary = mtllibrary;
Dec 8, 2017
Dec 8, 2017
442
443
444
445
446
447
SDL_assert(err == nil);
#if !__has_feature(objc_arc)
dispatch_release(mtllibdata);
#endif
data.mtllibrary.label = @"SDL Metal renderer shader library";
Jan 1, 2018
Jan 1, 2018
448
data.mtlpipelineprims = MakePipelineCache(data, "SDL primitives pipeline ", SDL_METAL_VERTEX_SOLID, SDL_METAL_FRAGMENT_SOLID);
Jan 1, 2018
Jan 1, 2018
449
450
data.mtlpipelinecopy = MakePipelineCache(data, "SDL texture pipeline ", SDL_METAL_VERTEX_COPY, SDL_METAL_FRAGMENT_COPY);
Jan 1, 2018
Jan 1, 2018
451
MTLSamplerDescriptor *samplerdesc = [[MTLSamplerDescriptor alloc] init];
Jan 1, 2018
Jan 1, 2018
452
453
454
samplerdesc.minFilter = MTLSamplerMinMagFilterNearest;
samplerdesc.magFilter = MTLSamplerMinMagFilterNearest;
Jan 2, 2018
Jan 2, 2018
455
456
id<MTLSamplerState> mtlsamplernearest = [data.mtldevice newSamplerStateWithDescriptor:samplerdesc];
data.mtlsamplernearest = mtlsamplernearest;
Jan 1, 2018
Jan 1, 2018
457
458
459
samplerdesc.minFilter = MTLSamplerMinMagFilterLinear;
samplerdesc.magFilter = MTLSamplerMinMagFilterLinear;
Jan 2, 2018
Jan 2, 2018
460
461
id<MTLSamplerState> mtlsamplerlinear = [data.mtldevice newSamplerStateWithDescriptor:samplerdesc];
data.mtlsamplerlinear = mtlsamplerlinear;
Jan 1, 2018
Jan 1, 2018
462
Dec 31, 2017
Dec 31, 2017
463
static const float clearverts[] = { 0, 0, 0, 3, 3, 0 };
Jan 2, 2018
Jan 2, 2018
464
465
id<MTLBuffer> mtlbufclearverts = [data.mtldevice newBufferWithBytes:clearverts length:sizeof(clearverts) options:MTLResourceCPUCacheModeWriteCombined];
data.mtlbufclearverts = mtlbufclearverts;
Dec 8, 2017
Dec 8, 2017
466
data.mtlbufclearverts.label = @"SDL_RenderClear vertices";
Jan 1, 2018
Jan 1, 2018
468
469
470
float identitytx[16];
SDL_memset(identitytx, 0, sizeof(identitytx));
identitytx[0] = identitytx[5] = identitytx[10] = identitytx[15] = 1.0f;
Jan 2, 2018
Jan 2, 2018
471
472
id<MTLBuffer> mtlbufidentitytransform = [data.mtldevice newBufferWithBytes:identitytx length:sizeof(identitytx) options:0];
data.mtlbufidentitytransform = mtlbufidentitytransform;
Jan 1, 2018
Jan 1, 2018
473
474
data.mtlbufidentitytransform.label = @"SDL_RenderCopy identity transform";
Dec 8, 2017
Dec 8, 2017
475
// !!! FIXME: force more clears here so all the drawables are sane to start, and our static buffers are definitely flushed.
476
477
478
renderer->WindowEvent = METAL_WindowEvent;
renderer->GetOutputSize = METAL_GetOutputSize;
Jan 1, 2018
Jan 1, 2018
479
renderer->SupportsBlendMode = METAL_SupportsBlendMode;
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
renderer->CreateTexture = METAL_CreateTexture;
renderer->UpdateTexture = METAL_UpdateTexture;
renderer->UpdateTextureYUV = METAL_UpdateTextureYUV;
renderer->LockTexture = METAL_LockTexture;
renderer->UnlockTexture = METAL_UnlockTexture;
renderer->SetRenderTarget = METAL_SetRenderTarget;
renderer->UpdateViewport = METAL_UpdateViewport;
renderer->UpdateClipRect = METAL_UpdateClipRect;
renderer->RenderClear = METAL_RenderClear;
renderer->RenderDrawPoints = METAL_RenderDrawPoints;
renderer->RenderDrawLines = METAL_RenderDrawLines;
renderer->RenderFillRects = METAL_RenderFillRects;
renderer->RenderCopy = METAL_RenderCopy;
renderer->RenderCopyEx = METAL_RenderCopyEx;
renderer->RenderReadPixels = METAL_RenderReadPixels;
renderer->RenderPresent = METAL_RenderPresent;
renderer->DestroyTexture = METAL_DestroyTexture;
renderer->DestroyRenderer = METAL_DestroyRenderer;
Dec 8, 2017
Dec 8, 2017
498
499
renderer->GetMetalLayer = METAL_GetMetalLayer;
renderer->GetMetalCommandEncoder = METAL_GetMetalCommandEncoder;
500
501
502
503
renderer->info = METAL_RenderDriver.info;
renderer->info.flags = (SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE);
Dec 31, 2017
Dec 31, 2017
504
505
#if defined(__MACOSX__) && defined(MAC_OS_X_VERSION_10_13)
if (@available(macOS 10.13, *)) {
Dec 31, 2017
Dec 31, 2017
506
data.mtllayer.displaySyncEnabled = (flags & SDL_RENDERER_PRESENTVSYNC) != 0;
Dec 31, 2017
Dec 31, 2017
507
508
509
510
511
} else
#endif
{
renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
}
Jan 2, 2018
Jan 2, 2018
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
#if !__has_feature(objc_arc)
[mtlcmdqueue release];
[mtllibrary release];
[samplerdesc release];
[mtlsamplernearest release];
[mtlsamplerlinear release];
[mtlbufclearverts release];
[mtlbufidentitytransform release];
[view release];
[data release];
#ifdef __MACOSX__
[mtldevice release];
#endif
#endif
Dec 8, 2017
Dec 8, 2017
528
529
return renderer;
}
Jan 2, 2018
Jan 2, 2018
531
532
static void
METAL_ActivateRenderer(SDL_Renderer * renderer)
Dec 8, 2017
Dec 8, 2017
533
534
{
METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
Dec 8, 2017
Dec 8, 2017
536
537
538
539
540
541
542
543
if (data.beginScene) {
data.beginScene = NO;
data.mtlbackbuffer = [data.mtllayer nextDrawable];
SDL_assert(data.mtlbackbuffer);
data.mtlpassdesc.colorAttachments[0].texture = data.mtlbackbuffer.texture;
data.mtlpassdesc.colorAttachments[0].loadAction = MTLLoadActionDontCare;
data.mtlcmdbuffer = [data.mtlcmdqueue commandBuffer];
data.mtlcmdencoder = [data.mtlcmdbuffer renderCommandEncoderWithDescriptor:data.mtlpassdesc];
Dec 9, 2017
Dec 9, 2017
544
data.mtlcmdencoder.label = @"SDL metal renderer start of frame";
Dec 8, 2017
Dec 8, 2017
545
546
547
548
549
// Set up our current renderer state for the next frame...
METAL_UpdateViewport(renderer);
METAL_UpdateClipRect(renderer);
}
550
551
552
553
554
555
556
557
558
559
560
561
562
563
}
static void
METAL_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
{
if (event->event == SDL_WINDOWEVENT_SIZE_CHANGED ||
event->event == SDL_WINDOWEVENT_SHOWN ||
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
564
{ @autoreleasepool {
Dec 8, 2017
Dec 8, 2017
565
METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
Dec 31, 2017
Dec 31, 2017
566
567
568
569
570
571
572
573
574
// !!! FIXME: We shouldn't need ActivateRenderer, but drawableSize is 0
// in the first frame without it.
METAL_ActivateRenderer(renderer);
if (w) {
*w = (int)data.mtllayer.drawableSize.width;
}
if (h) {
*h = (int)data.mtllayer.drawableSize.height;
}
Dec 8, 2017
Dec 8, 2017
576
}}
Jan 1, 2018
Jan 1, 2018
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
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;
}
599
600
static int
METAL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
Dec 8, 2017
Dec 8, 2017
601
{ @autoreleasepool {
Dec 8, 2017
Dec 8, 2017
602
METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
603
604
605
606
607
608
609
610
611
612
MTLPixelFormat mtlpixfmt;
switch (texture->format) {
case SDL_PIXELFORMAT_ABGR8888: mtlpixfmt = MTLPixelFormatRGBA8Unorm; break;
case SDL_PIXELFORMAT_ARGB8888: mtlpixfmt = MTLPixelFormatBGRA8Unorm; break;
default: return SDL_SetError("Texture format %s not supported by Metal", SDL_GetPixelFormatName(texture->format));
}
MTLTextureDescriptor *mtltexdesc = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:mtlpixfmt
width:(NSUInteger)texture->w height:(NSUInteger)texture->h mipmapped:NO];
Dec 9, 2017
Dec 9, 2017
613
614
615
616
617
618
619
620
621
if (texture->access == SDL_TEXTUREACCESS_TARGET) {
mtltexdesc.usage = MTLTextureUsageShaderRead | MTLTextureUsageRenderTarget;
} else {
mtltexdesc.usage = MTLTextureUsageShaderRead;
}
//mtltexdesc.resourceOptions = MTLResourceCPUCacheModeDefaultCache | MTLResourceStorageModeManaged;
//mtltexdesc.storageMode = MTLStorageModeManaged;
Dec 8, 2017
Dec 8, 2017
622
id<MTLTexture> mtltexture = [data.mtldevice newTextureWithDescriptor:mtltexdesc];
623
624
625
626
if (mtltexture == nil) {
return SDL_SetError("Texture allocation failed");
}
Dec 9, 2017
Dec 9, 2017
627
628
629
METAL_TextureData *texturedata = [[METAL_TextureData alloc] init];
const char *hint = SDL_GetHint(SDL_HINT_RENDER_SCALE_QUALITY);
if (!hint || *hint == '0' || SDL_strcasecmp(hint, "nearest") == 0) {
Jan 1, 2018
Jan 1, 2018
630
texturedata.mtlsampler = data.mtlsamplernearest;
Dec 9, 2017
Dec 9, 2017
631
} else {
Jan 1, 2018
Jan 1, 2018
632
texturedata.mtlsampler = data.mtlsamplerlinear;
Dec 9, 2017
Dec 9, 2017
633
634
635
636
}
texturedata.mtltexture = mtltexture;
texture->driverdata = (void*)CFBridgingRetain(texturedata);
Jan 1, 2018
Jan 1, 2018
638
#if !__has_feature(objc_arc)
Jan 2, 2018
Jan 2, 2018
639
[texturedata release];
Jan 1, 2018
Jan 1, 2018
640
641
642
[mtltexture release];
#endif
Dec 8, 2017
Dec 8, 2017
644
}}
645
646
647
648
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
649
{ @autoreleasepool {
650
651
652
653
// !!! FIXME: this is a synchronous call; it doesn't return until data is uploaded in some form.
// !!! FIXME: Maybe move this off to a thread that marks the texture as uploaded and only stall the main thread if we try to
// !!! FIXME: use this texture before the marking is done? Is it worth it? Or will we basically always be uploading a bunch of
// !!! FIXME: stuff way ahead of time and/or using it immediately after upload?
Dec 9, 2017
Dec 9, 2017
654
id<MTLTexture> mtltexture = ((__bridge METAL_TextureData *)texture->driverdata).mtltexture;
655
656
[mtltexture replaceRegion:MTLRegionMake2D(rect->x, rect->y, rect->w, rect->h) mipmapLevel:0 withBytes:pixels bytesPerRow:pitch];
return 0;
Dec 8, 2017
Dec 8, 2017
657
}}
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
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)
{
return SDL_Unsupported(); // !!! FIXME
}
static int
METAL_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, void **pixels, int *pitch)
{
return SDL_Unsupported(); // !!! FIXME: write me
}
static void
METAL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
{
// !!! FIXME: write me
}
static int
METAL_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture)
Dec 8, 2017
Dec 8, 2017
684
{ @autoreleasepool {
Dec 8, 2017
Dec 8, 2017
685
METAL_ActivateRenderer(renderer);
Dec 8, 2017
Dec 8, 2017
686
METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
Dec 9, 2017
Dec 9, 2017
687
688
689
690
691
692
// commit the current command buffer, so that any work on a render target
// will be available to the next one we're about to queue up.
[data.mtlcmdencoder endEncoding];
[data.mtlcmdbuffer commit];
Dec 9, 2017
Dec 9, 2017
693
id<MTLTexture> mtltexture = texture ? ((__bridge METAL_TextureData *)texture->driverdata).mtltexture : data.mtlbackbuffer.texture;
Dec 8, 2017
Dec 8, 2017
694
data.mtlpassdesc.colorAttachments[0].texture = mtltexture;
Dec 9, 2017
Dec 9, 2017
695
696
697
698
699
700
701
702
// !!! FIXME: this can be MTLLoadActionDontCare for textures (not the backbuffer) if SDL doesn't guarantee the texture contents should survive.
data.mtlpassdesc.colorAttachments[0].loadAction = MTLLoadActionLoad;
data.mtlcmdbuffer = [data.mtlcmdqueue commandBuffer];
data.mtlcmdencoder = [data.mtlcmdbuffer renderCommandEncoderWithDescriptor:data.mtlpassdesc];
data.mtlcmdencoder.label = texture ? @"SDL metal renderer render texture" : @"SDL metal renderer backbuffer";
// The higher level will reset the viewport and scissor after this call returns.
Dec 8, 2017
Dec 8, 2017
704
}}
Dec 31, 2017
Dec 31, 2017
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
static int
METAL_SetOrthographicProjection(SDL_Renderer *renderer, int w, int h)
{ @autoreleasepool {
METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
float projection[4][4];
if (!w || !h) {
return 0;
}
/* Prepare an orthographic projection */
projection[0][0] = 2.0f / w;
projection[0][1] = 0.0f;
projection[0][2] = 0.0f;
projection[0][3] = 0.0f;
projection[1][0] = 0.0f;
projection[1][1] = -2.0f / h;
projection[1][2] = 0.0f;
projection[1][3] = 0.0f;
projection[2][0] = 0.0f;
projection[2][1] = 0.0f;
projection[2][2] = 0.0f;
projection[2][3] = 0.0f;
projection[3][0] = -1.0f;
projection[3][1] = 1.0f;
projection[3][2] = 0.0f;
projection[3][3] = 1.0f;
// !!! FIXME: This should be in a buffer...
[data.mtlcmdencoder setVertexBytes:projection length:sizeof(float)*16 atIndex:2];
return 0;
}}
739
740
static int
METAL_UpdateViewport(SDL_Renderer * renderer)
Dec 8, 2017
Dec 8, 2017
741
{ @autoreleasepool {
Dec 8, 2017
Dec 8, 2017
742
METAL_ActivateRenderer(renderer);
Dec 8, 2017
Dec 8, 2017
743
METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
Dec 8, 2017
Dec 8, 2017
744
745
746
747
748
749
750
751
MTLViewport viewport;
viewport.originX = renderer->viewport.x;
viewport.originY = renderer->viewport.y;
viewport.width = renderer->viewport.w;
viewport.height = renderer->viewport.h;
viewport.znear = 0.0;
viewport.zfar = 1.0;
[data.mtlcmdencoder setViewport:viewport];
Dec 31, 2017
Dec 31, 2017
752
METAL_SetOrthographicProjection(renderer, renderer->viewport.w, renderer->viewport.h);
Dec 8, 2017
Dec 8, 2017
754
}}
755
756
757
static int
METAL_UpdateClipRect(SDL_Renderer * renderer)
Dec 8, 2017
Dec 8, 2017
758
{ @autoreleasepool {
Dec 8, 2017
Dec 8, 2017
759
METAL_ActivateRenderer(renderer);
Dec 8, 2017
Dec 8, 2017
760
METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
Dec 8, 2017
Dec 8, 2017
761
MTLScissorRect mtlrect;
Dec 8, 2017
Dec 8, 2017
762
// !!! FIXME: should this care about the viewport?
Dec 8, 2017
Dec 8, 2017
763
764
765
766
767
768
769
770
771
772
773
774
775
776
if (renderer->clipping_enabled) {
const SDL_Rect *rect = &renderer->clip_rect;
mtlrect.x = renderer->viewport.x + rect->x;
mtlrect.y = renderer->viewport.x + rect->y;
mtlrect.width = rect->w;
mtlrect.height = rect->h;
} else {
mtlrect.x = renderer->viewport.x;
mtlrect.y = renderer->viewport.y;
mtlrect.width = renderer->viewport.w;
mtlrect.height = renderer->viewport.h;
}
if (mtlrect.width > 0 && mtlrect.height > 0) {
[data.mtlcmdencoder setScissorRect:mtlrect];
777
778
}
return 0;
Dec 8, 2017
Dec 8, 2017
779
}}
780
781
782
static int
METAL_RenderClear(SDL_Renderer * renderer)
Dec 8, 2017
Dec 8, 2017
783
{ @autoreleasepool {
784
// We could dump the command buffer and force a clear on a new one, but this will respect the scissor state.
Dec 8, 2017
Dec 8, 2017
785
METAL_ActivateRenderer(renderer);
Dec 8, 2017
Dec 8, 2017
786
METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
787
788
789
790
791
792
// !!! FIXME: render color should live in a dedicated uniform buffer.
const float color[4] = { ((float)renderer->r) / 255.0f, ((float)renderer->g) / 255.0f, ((float)renderer->b) / 255.0f, ((float)renderer->a) / 255.0f };
MTLViewport viewport; // RenderClear ignores the viewport state, though, so reset that.
viewport.originX = viewport.originY = 0.0;
Dec 9, 2017
Dec 9, 2017
793
794
viewport.width = data.mtlpassdesc.colorAttachments[0].texture.width;
viewport.height = data.mtlpassdesc.colorAttachments[0].texture.height;
795
796
797
viewport.znear = 0.0;
viewport.zfar = 1.0;
Dec 30, 2017
Dec 30, 2017
798
// Draw a simple filled fullscreen triangle now.
Dec 31, 2017
Dec 31, 2017
799
METAL_SetOrthographicProjection(renderer, 1, 1);
Dec 8, 2017
Dec 8, 2017
800
[data.mtlcmdencoder setViewport:viewport];
Jan 1, 2018
Jan 1, 2018
801
[data.mtlcmdencoder setRenderPipelineState:ChoosePipelineState(data, data.mtlpipelineprims, SDL_BLENDMODE_NONE)];
Dec 8, 2017
Dec 8, 2017
802
803
[data.mtlcmdencoder setVertexBuffer:data.mtlbufclearverts offset:0 atIndex:0];
[data.mtlcmdencoder setFragmentBytes:color length:sizeof(color) atIndex:0];
Dec 30, 2017
Dec 30, 2017
804
[data.mtlcmdencoder drawPrimitives:MTLPrimitiveTypeTriangle vertexStart:0 vertexCount:3];
805
806
807
808
809
810
811
812
// reset the viewport for the rest of our usual drawing work...
viewport.originX = renderer->viewport.x;
viewport.originY = renderer->viewport.y;
viewport.width = renderer->viewport.w;
viewport.height = renderer->viewport.h;
viewport.znear = 0.0;
viewport.zfar = 1.0;
Dec 8, 2017
Dec 8, 2017
813
[data.mtlcmdencoder setViewport:viewport];
Dec 31, 2017
Dec 31, 2017
814
METAL_SetOrthographicProjection(renderer, renderer->viewport.w, renderer->viewport.h);
815
816
return 0;
Dec 8, 2017
Dec 8, 2017
817
}}
Jan 2, 2018
Jan 2, 2018
819
820
821
822
823
824
825
826
827
828
829
830
// adjust pixel center for x and y coordinates
static inline float
adjustx(const float val)
{
return (val + 0.5f);
}
static inline float
adjusty(const float val)
{
return (val - 0.5f);
}
Dec 10, 2017
Dec 10, 2017
831
832
833
834
835
836
837
838
// normalize a value from 0.0f to len into 0.0f to 1.0f.
static inline float
normtex(const float _val, const float len)
{
const float val = (_val < 0.0f) ? 0.0f : (_val > len) ? len : _val;
return ((val + 0.5f) / len);
}
839
840
841
static int
DrawVerts(SDL_Renderer * renderer, const SDL_FPoint * points, int count,
const MTLPrimitiveType primtype)
Dec 8, 2017
Dec 8, 2017
842
{ @autoreleasepool {
Dec 8, 2017
Dec 8, 2017
843
844
METAL_ActivateRenderer(renderer);
Jan 2, 2018
Jan 2, 2018
845
846
847
848
849
850
const size_t vertlen = (sizeof (float) * 2) * count;
float *verts = SDL_malloc(vertlen);
if (!verts) {
return SDL_OutOfMemory();
}
Dec 8, 2017
Dec 8, 2017
851
METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
852
853
854
855
// !!! FIXME: render color should live in a dedicated uniform buffer.
const float color[4] = { ((float)renderer->r) / 255.0f, ((float)renderer->g) / 255.0f, ((float)renderer->b) / 255.0f, ((float)renderer->a) / 255.0f };
Jan 1, 2018
Jan 1, 2018
856
[data.mtlcmdencoder setRenderPipelineState:ChoosePipelineState(data, data.mtlpipelineprims, renderer->blendMode)];
Dec 8, 2017
Dec 8, 2017
857
[data.mtlcmdencoder setFragmentBytes:color length:sizeof(color) atIndex:0];
Jan 2, 2018
Jan 2, 2018
858
859
860
861
862
863
864
865
float *ptr = verts;
for (int i = 0; i < count; i++, points++) {
*ptr = adjustx(points->x); ptr++;
*ptr = adjusty(points->y); ptr++;
}
[data.mtlcmdencoder setVertexBytes:verts length:vertlen atIndex:0];
Dec 8, 2017
Dec 8, 2017
866
[data.mtlcmdencoder drawPrimitives:primtype vertexStart:0 vertexCount:count];
Jan 2, 2018
Jan 2, 2018
867
868
SDL_free(verts);
Dec 8, 2017
Dec 8, 2017
870
}}
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
static int
METAL_RenderDrawPoints(SDL_Renderer * renderer, const SDL_FPoint * points, int count)
{
return DrawVerts(renderer, points, count, MTLPrimitiveTypePoint);
}
static int
METAL_RenderDrawLines(SDL_Renderer * renderer, const SDL_FPoint * points, int count)
{
return DrawVerts(renderer, points, count, MTLPrimitiveTypeLineStrip);
}
static int
METAL_RenderFillRects(SDL_Renderer * renderer, const SDL_FRect * rects, int count)
Dec 8, 2017
Dec 8, 2017
886
{ @autoreleasepool {
Dec 8, 2017
Dec 8, 2017
887
METAL_ActivateRenderer(renderer);
Dec 8, 2017
Dec 8, 2017
888
METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
889
890
891
892
// !!! FIXME: render color should live in a dedicated uniform buffer.
const float color[4] = { ((float)renderer->r) / 255.0f, ((float)renderer->g) / 255.0f, ((float)renderer->b) / 255.0f, ((float)renderer->a) / 255.0f };
Jan 1, 2018
Jan 1, 2018
893
[data.mtlcmdencoder setRenderPipelineState:ChoosePipelineState(data, data.mtlpipelineprims, renderer->blendMode)];
Dec 8, 2017
Dec 8, 2017
894
[data.mtlcmdencoder setFragmentBytes:color length:sizeof(color) atIndex:0];
895
896
897
898
899
for (int i = 0; i < count; i++, rects++) {
if ((rects->w <= 0.0f) || (rects->h <= 0.0f)) continue;
const float verts[] = {
Jan 2, 2018
Jan 2, 2018
900
901
902
903
adjustx(rects->x), adjusty(rects->y + rects->h),
adjustx(rects->x), adjusty(rects->y),
adjustx(rects->x + rects->w), adjusty(rects->y + rects->h),
adjustx(rects->x + rects->w), adjusty(rects->y)
Dec 8, 2017
Dec 8, 2017
906
[data.mtlcmdencoder setVertexBytes:verts length:sizeof(verts) atIndex:0];
Dec 30, 2017
Dec 30, 2017
907
[data.mtlcmdencoder drawPrimitives:MTLPrimitiveTypeTriangleStrip vertexStart:0 vertexCount:4];
908
909
910
}
return 0;
Dec 8, 2017
Dec 8, 2017
911
}}
912
913
914
915
static int
METAL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * srcrect, const SDL_FRect * dstrect)
Dec 8, 2017
Dec 8, 2017
916
{ @autoreleasepool {
Dec 8, 2017
Dec 8, 2017
917
METAL_ActivateRenderer(renderer);
Dec 8, 2017
Dec 8, 2017
918
METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
Dec 9, 2017
Dec 9, 2017
919
920
921
METAL_TextureData *texturedata = (__bridge METAL_TextureData *)texture->driverdata;
const float texw = (float) texturedata.mtltexture.width;
const float texh = (float) texturedata.mtltexture.height;
922
923
const float xy[] = {
Jan 2, 2018
Jan 2, 2018
924
925
926
927
adjustx(dstrect->x), adjusty(dstrect->y + dstrect->h),
adjustx(dstrect->x), adjusty(dstrect->y),
adjustx(dstrect->x + dstrect->w), adjusty(dstrect->y + dstrect->h),
adjustx(dstrect->x + dstrect->w), adjusty(dstrect->y)
928
929
930
};
const float uv[] = {
Dec 10, 2017
Dec 10, 2017
931
932
normtex(srcrect->x, texw), normtex(srcrect->y + srcrect->h, texh),
normtex(srcrect->x, texw), normtex(srcrect->y, texh),
Dec 30, 2017
Dec 30, 2017
933
934
normtex(srcrect->x + srcrect->w, texw), normtex(srcrect->y + srcrect->h, texh),
normtex(srcrect->x + srcrect->w, texw), normtex(srcrect->y, texh)
935
936
937
938
939
940
941
942
943
944
};
float color[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
if (texture->modMode) {
color[0] = ((float)texture->r) / 255.0f;
color[1] = ((float)texture->g) / 255.0f;
color[2] = ((float)texture->b) / 255.0f;
color[3] = ((float)texture->a) / 255.0f;
}
Jan 1, 2018
Jan 1, 2018
945
[data.mtlcmdencoder setRenderPipelineState:ChoosePipelineState(data, data.mtlpipelinecopy, texture->blendMode)];
Dec 8, 2017
Dec 8, 2017
946
947
[data.mtlcmdencoder setVertexBytes:xy length:sizeof(xy) atIndex:0];
[data.mtlcmdencoder setVertexBytes:uv length:sizeof(uv) atIndex:1];
Jan 1, 2018
Jan 1, 2018
948
949
950
[data.mtlcmdencoder setVertexBuffer:data.mtlbufidentitytransform offset:0 atIndex:3];
[data.mtlcmdencoder setFragmentBytes:color length:sizeof(color) atIndex:0];
[data.mtlcmdencoder setFragmentTexture:texturedata.mtltexture atIndex:0];
Jan 1, 2018
Jan 1, 2018
951
[data.mtlcmdencoder setFragmentSamplerState:texturedata.mtlsampler atIndex:0];
Dec 30, 2017
Dec 30, 2017
952
[data.mtlcmdencoder drawPrimitives:MTLPrimitiveTypeTriangleStrip vertexStart:0 vertexCount:4];
953
954
return 0;
Dec 8, 2017
Dec 8, 2017
955
}}
956
957
958
959
960
static int
METAL_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * srcrect, const SDL_FRect * dstrect,
const double angle, const SDL_FPoint *center, const SDL_RendererFlip flip)
Jan 1, 2018
Jan 1, 2018
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
{ @autoreleasepool {
METAL_ActivateRenderer(renderer);
METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
METAL_TextureData *texturedata = (__bridge METAL_TextureData *)texture->driverdata;
const float texw = (float) texturedata.mtltexture.width;
const float texh = (float) texturedata.mtltexture.height;
float transform[16];
float minu, maxu, minv, maxv;
minu = normtex(srcrect->x, texw);
maxu = normtex(srcrect->x + srcrect->w, texw);
minv = normtex(srcrect->y, texh);
maxv = normtex(srcrect->y + srcrect->h, texh);
if (flip & SDL_FLIP_HORIZONTAL) {
float tmp = maxu;
maxu = minu;
minu = tmp;
}
if (flip & SDL_FLIP_VERTICAL) {
float tmp = maxv;
maxv = minv;
minv = tmp;
}
const float uv[] = {
minu, maxv,
minu, minv,
maxu, maxv,
maxu, minv
};
const float xy[] = {
Jan 2, 2018
Jan 2, 2018
994
995
996
997
adjustx(-center->x), adjusty(dstrect->h - center->y),
adjustx(-center->x), adjusty(-center->y),
adjustx(dstrect->w - center->x), adjusty(dstrect->h - center->y),
adjustx(dstrect->w - center->x), adjusty(-center->y)
Jan 1, 2018
Jan 1, 2018
998
999
1000
};
{