src/render/metal/SDL_render_metal.m
changeset 11730 ac6c607e065c
parent 11729 d1ce8396c356
child 11732 ad13456d6e7f
     1.1 --- a/src/render/metal/SDL_render_metal.m	Thu Apr 21 03:16:44 2016 -0400
     1.2 +++ b/src/render/metal/SDL_render_metal.m	Thu Dec 07 16:08:09 2017 -0800
     1.3 @@ -1,6 +1,6 @@
     1.4  /*
     1.5    Simple DirectMedia Layer
     1.6 -  Copyright (C) 1997-2015 Sam Lantinga <slouken@libsdl.org>
     1.7 +  Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
     1.8  
     1.9    This software is provided 'as-is', without any express or implied
    1.10    warranty.  In no event will the authors be held liable for any damages
    1.11 @@ -32,10 +32,12 @@
    1.12  #include <Metal/Metal.h>
    1.13  #include <QuartzCore/CAMetalLayer.h>
    1.14  
    1.15 -// these are in SDL_shaders_metal.c, regenerate it with build-metal-shaders.sh
    1.16 -extern const unsigned char sdl_metallib[];
    1.17 -extern const unsigned int sdl_metallib_len;
    1.18 -
    1.19 +/* Regenerate these with build-metal-shaders.sh */
    1.20 +#ifdef __MACOSX__
    1.21 +#include "SDL_shaders_metal_osx.h"
    1.22 +#else
    1.23 +#include "SDL_shaders_metal_ios.h"
    1.24 +#endif
    1.25  
    1.26  /* Apple Metal renderer implementation */
    1.27  
    1.28 @@ -141,10 +143,6 @@
    1.29      mtlpipedesc.colorAttachments[0].pixelFormat = data->mtlbackbuffer.texture.pixelFormat;
    1.30  
    1.31      switch (blendmode) {
    1.32 -        case SDL_BLENDMODE_NONE:
    1.33 -            mtlpipedesc.colorAttachments[0].blendingEnabled = NO;
    1.34 -            break;
    1.35 -
    1.36          case SDL_BLENDMODE_BLEND:
    1.37              mtlpipedesc.colorAttachments[0].blendingEnabled = YES;
    1.38              mtlpipedesc.colorAttachments[0].rgbBlendOperation = MTLBlendOperationAdd;
    1.39 @@ -174,6 +172,10 @@
    1.40              mtlpipedesc.colorAttachments[0].sourceAlphaBlendFactor = MTLBlendFactorZero;
    1.41              mtlpipedesc.colorAttachments[0].destinationAlphaBlendFactor = MTLBlendFactorOne;
    1.42              break;
    1.43 +
    1.44 +        default:
    1.45 +            mtlpipedesc.colorAttachments[0].blendingEnabled = NO;
    1.46 +            break;
    1.47      }
    1.48  
    1.49      mtlpipedesc.label = label;
    1.50 @@ -204,10 +206,10 @@
    1.51  ChoosePipelineState(id<MTLRenderPipelineState> *states, const SDL_BlendMode blendmode)
    1.52  {
    1.53      switch (blendmode) {
    1.54 -        case SDL_BLENDMODE_NONE: return states[0];
    1.55          case SDL_BLENDMODE_BLEND: return states[1];
    1.56          case SDL_BLENDMODE_ADD: return states[2];
    1.57          case SDL_BLENDMODE_MOD: return states[3];
    1.58 +        default: return states[0];
    1.59      }
    1.60      return nil;
    1.61  }
    1.62 @@ -244,8 +246,9 @@
    1.63      renderer->driverdata = data;
    1.64      renderer->window = window;
    1.65  
    1.66 -    data->mtldevice = MTLCreateSystemDefaultDevice();  // !!! FIXME: MTLCopyAllDevices() can find other GPUs...
    1.67 -    if (data->mtldevice == nil) {
    1.68 +#ifdef __MACOSX__
    1.69 +    id<MTLDevice> mtldevice = MTLCreateSystemDefaultDevice();  // !!! FIXME: MTLCopyAllDevices() can find other GPUs...
    1.70 +    if (mtldevice == nil) {
    1.71          SDL_free(renderer);
    1.72          SDL_free(data);
    1.73          SDL_SetError("Failed to obtain Metal device");
    1.74 @@ -256,10 +259,10 @@
    1.75  
    1.76      NSView *nsview = [syswm.info.cocoa.window contentView];
    1.77  
    1.78 -    // !!! FIXME: on iOS, we need to override +[UIView layerClass] to return [CAMetalLayer class] right from the start, and that's more complicated.
    1.79 -    CAMetalLayer *layer = [CAMetalLayer layer];
    1.80 +    // CAMetalLayer is available in QuartzCore starting at OSX 10.11
    1.81 +    CAMetalLayer *layer = [NSClassFromString( @"CAMetalLayer" ) layer];
    1.82  
    1.83 -    layer.device = data->mtldevice;
    1.84 +    layer.device = mtldevice;
    1.85      //layer.pixelFormat = MTLPixelFormatBGRA8Unorm;  // !!! FIXME: MTLPixelFormatBGRA8Unorm_sRGB ?
    1.86      layer.framebufferOnly = YES;
    1.87      //layer.drawableSize = (CGSize) [nsview convertRectToBacking:[nsview bounds]].size;
    1.88 @@ -269,6 +272,11 @@
    1.89      [nsview setLayer:layer];
    1.90  
    1.91      [layer retain];
    1.92 +#else
    1.93 +
    1.94 +#endif
    1.95 +
    1.96 +    data->mtldevice = layer.device;
    1.97      data->mtllayer = layer;
    1.98      data->mtlcmdqueue = [data->mtldevice newCommandQueue];
    1.99      data->mtlcmdqueue.label = @"SDL Metal Renderer";
   1.100 @@ -318,8 +326,8 @@
   1.101  
   1.102      NSError *err = nil;
   1.103  
   1.104 -    // The compiled .metallib is embedded in a static array in SDL_shaders_metal.c,
   1.105 -    //  but the original shader source code is in SDL_shaders_metal.metal.
   1.106 +    // The compiled .metallib is embedded in a static array in a header file
   1.107 +    // but the original shader source code is in SDL_shaders_metal.metal.
   1.108      dispatch_data_t mtllibdata = dispatch_data_create(sdl_metallib, sdl_metallib_len, dispatch_get_global_queue(0, 0), ^{});
   1.109      data->mtllibrary = [data->mtldevice newLibraryWithData:mtllibdata error:&err];
   1.110      SDL_assert(err == nil);