src/render/direct3d11/SDL_render_d3d11.c
changeset 8607 ef23c9d08eed
parent 8599 532cd234e923
child 8608 c5d603f7094e
     1.1 --- a/src/render/direct3d11/SDL_render_d3d11.c	Tue Mar 11 10:33:51 2014 -0400
     1.2 +++ b/src/render/direct3d11/SDL_render_d3d11.c	Mon Mar 10 22:53:03 2014 -0400
     1.3 @@ -929,6 +929,36 @@
     1.4      ID3D11DeviceContext *d3dContext = NULL;
     1.5      IDXGIDevice1 *dxgiDevice = NULL;
     1.6      HRESULT result = S_OK;
     1.7 +    UINT creationFlags;
     1.8 +    const char *hint;
     1.9 +
    1.10 +    /* This array defines the set of DirectX hardware feature levels this app will support.
    1.11 +     * Note the ordering should be preserved.
    1.12 +     * Don't forget to declare your application's minimum required feature level in its
    1.13 +     * description.  All applications are assumed to support 9.1 unless otherwise stated.
    1.14 +     */
    1.15 +    D3D_FEATURE_LEVEL featureLevels[] = 
    1.16 +    {
    1.17 +        D3D_FEATURE_LEVEL_11_1,
    1.18 +        D3D_FEATURE_LEVEL_11_0,
    1.19 +        D3D_FEATURE_LEVEL_10_1,
    1.20 +        D3D_FEATURE_LEVEL_10_0,
    1.21 +        D3D_FEATURE_LEVEL_9_3,
    1.22 +        D3D_FEATURE_LEVEL_9_2,
    1.23 +        D3D_FEATURE_LEVEL_9_1
    1.24 +    };
    1.25 +
    1.26 +    /* Declare how the input layout for SDL's vertex shader will be setup: */
    1.27 +    const D3D11_INPUT_ELEMENT_DESC vertexDesc[] = 
    1.28 +    {
    1.29 +        { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
    1.30 +        { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
    1.31 +        { "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 20, D3D11_INPUT_PER_VERTEX_DATA, 0 },
    1.32 +    };
    1.33 +
    1.34 +    D3D11_BUFFER_DESC constantBufferDesc;
    1.35 +    D3D11_SAMPLER_DESC samplerDesc;
    1.36 +    D3D11_RASTERIZER_DESC rasterDesc;
    1.37  
    1.38  #ifdef __WINRT__
    1.39      CreateDXGIFactoryFunc = CreateDXGIFactory;
    1.40 @@ -975,30 +1005,14 @@
    1.41      /* This flag adds support for surfaces with a different color channel ordering
    1.42       * than the API default. It is required for compatibility with Direct2D.
    1.43       */
    1.44 -    UINT creationFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
    1.45 +    creationFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
    1.46  
    1.47      /* Make sure Direct3D's debugging feature gets used, if the app requests it. */
    1.48 -    const char *hint = SDL_GetHint(SDL_HINT_RENDER_DIRECT3D11_DEBUG);
    1.49 +    hint = SDL_GetHint(SDL_HINT_RENDER_DIRECT3D11_DEBUG);
    1.50      if (hint && SDL_atoi(hint) > 0) {
    1.51          creationFlags |= D3D11_CREATE_DEVICE_DEBUG;
    1.52      }
    1.53  
    1.54 -    /* This array defines the set of DirectX hardware feature levels this app will support.
    1.55 -     * Note the ordering should be preserved.
    1.56 -     * Don't forget to declare your application's minimum required feature level in its
    1.57 -     * description.  All applications are assumed to support 9.1 unless otherwise stated.
    1.58 -     */
    1.59 -    D3D_FEATURE_LEVEL featureLevels[] = 
    1.60 -    {
    1.61 -        D3D_FEATURE_LEVEL_11_1,
    1.62 -        D3D_FEATURE_LEVEL_11_0,
    1.63 -        D3D_FEATURE_LEVEL_10_1,
    1.64 -        D3D_FEATURE_LEVEL_10_0,
    1.65 -        D3D_FEATURE_LEVEL_9_3,
    1.66 -        D3D_FEATURE_LEVEL_9_2,
    1.67 -        D3D_FEATURE_LEVEL_9_1
    1.68 -    };
    1.69 -
    1.70      /* Create the Direct3D 11 API device object and a corresponding context. */
    1.71      result = D3D11CreateDeviceFunc(
    1.72          data->dxgiAdapter,
    1.73 @@ -1087,13 +1101,6 @@
    1.74      }
    1.75  
    1.76      /* Create an input layout for SDL's vertex shader: */
    1.77 -    const D3D11_INPUT_ELEMENT_DESC vertexDesc[] = 
    1.78 -    {
    1.79 -        { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
    1.80 -        { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
    1.81 -        { "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 20, D3D11_INPUT_PER_VERTEX_DATA, 0 },
    1.82 -    };
    1.83 -
    1.84      result = ID3D11Device_CreateInputLayout(data->d3dDevice,
    1.85          vertexDesc,
    1.86          ARRAYSIZE(vertexDesc),
    1.87 @@ -1141,7 +1148,6 @@
    1.88      }
    1.89  
    1.90      /* Setup space to hold vertex shader constants: */
    1.91 -    D3D11_BUFFER_DESC constantBufferDesc;
    1.92      SDL_zero(constantBufferDesc);
    1.93      constantBufferDesc.ByteWidth = sizeof(VertexShaderConstants);
    1.94      constantBufferDesc.Usage = D3D11_USAGE_DEFAULT;
    1.95 @@ -1157,7 +1163,6 @@
    1.96      }
    1.97  
    1.98      /* Create samplers to use when drawing textures: */
    1.99 -    D3D11_SAMPLER_DESC samplerDesc;
   1.100      SDL_zero(samplerDesc);
   1.101      samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT;
   1.102      samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
   1.103 @@ -1188,7 +1193,6 @@
   1.104      }
   1.105  
   1.106      /* Setup Direct3D rasterizer states */
   1.107 -    D3D11_RASTERIZER_DESC rasterDesc;
   1.108      SDL_zero(rasterDesc);
   1.109      rasterDesc.AntialiasedLineEnable = FALSE;
   1.110      rasterDesc.CullMode = D3D11_CULL_NONE;
   1.111 @@ -1500,6 +1504,7 @@
   1.112      ID3D11Texture2D *backBuffer = NULL;
   1.113      HRESULT result = S_OK;
   1.114      int w, h;
   1.115 +    BOOL swapDimensions;
   1.116  
   1.117      /* Release the previous render target view */
   1.118      D3D11_ReleaseMainRenderTargetView(renderer);
   1.119 @@ -1512,9 +1517,9 @@
   1.120      data->rotation = D3D11_GetCurrentRotation();
   1.121  
   1.122  #if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
   1.123 -    const BOOL swapDimensions = FALSE;
   1.124 +    swapDimensions = FALSE;
   1.125  #else
   1.126 -    const BOOL swapDimensions = D3D11_IsDisplayRotated90Degrees(data->rotation);
   1.127 +    swapDimensions = D3D11_IsDisplayRotated90Degrees(data->rotation);
   1.128  #endif
   1.129      if (swapDimensions) {
   1.130          int tmp = w;
   1.131 @@ -1646,6 +1651,9 @@
   1.132      D3D11_TextureData *textureData;
   1.133      HRESULT result;
   1.134      DXGI_FORMAT textureFormat = SDLPixelFormatToDXGIFormat(texture->format);
   1.135 +    D3D11_TEXTURE2D_DESC textureDesc;
   1.136 +    D3D11_SHADER_RESOURCE_VIEW_DESC resourceViewDesc;
   1.137 +
   1.138      if (textureFormat == SDL_PIXELFORMAT_UNKNOWN) {
   1.139          return SDL_SetError("%s, An unsupported SDL pixel format (0x%x) was specified",
   1.140              __FUNCTION__, texture->format);
   1.141 @@ -1660,7 +1668,6 @@
   1.142  
   1.143      texture->driverdata = textureData;
   1.144  
   1.145 -    D3D11_TEXTURE2D_DESC textureDesc;
   1.146      SDL_zero(textureDesc);
   1.147      textureDesc.Width = texture->w;
   1.148      textureDesc.Height = texture->h;
   1.149 @@ -1726,7 +1733,6 @@
   1.150          }
   1.151      }
   1.152  
   1.153 -    D3D11_SHADER_RESOURCE_VIEW_DESC resourceViewDesc;
   1.154      resourceViewDesc.Format = textureDesc.Format;
   1.155      resourceViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
   1.156      resourceViewDesc.Texture2D.MostDetailedMip = 0;
   1.157 @@ -1817,9 +1823,10 @@
   1.158      int row;
   1.159      UINT length;
   1.160      HRESULT result;
   1.161 +    D3D11_TEXTURE2D_DESC stagingTextureDesc;
   1.162 +    D3D11_MAPPED_SUBRESOURCE textureMemory;
   1.163  
   1.164      /* Create a 'staging' texture, which will be used to write to a portion of the main texture. */
   1.165 -    D3D11_TEXTURE2D_DESC stagingTextureDesc;
   1.166      ID3D11Texture2D_GetDesc(texture, &stagingTextureDesc);
   1.167      stagingTextureDesc.Width = w;
   1.168      stagingTextureDesc.Height = h;
   1.169 @@ -1837,7 +1844,6 @@
   1.170      }
   1.171  
   1.172      /* Get a write-only pointer to data in the staging texture: */
   1.173 -    D3D11_MAPPED_SUBRESOURCE textureMemory;
   1.174      result = ID3D11DeviceContext_Map(rendererData->d3dContext,
   1.175          (ID3D11Resource *)stagingTexture,
   1.176          0,
   1.177 @@ -1959,6 +1965,8 @@
   1.178      D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
   1.179      D3D11_TextureData *textureData = (D3D11_TextureData *) texture->driverdata;
   1.180      HRESULT result = S_OK;
   1.181 +    D3D11_TEXTURE2D_DESC stagingTextureDesc;
   1.182 +    D3D11_MAPPED_SUBRESOURCE textureMemory;
   1.183  
   1.184      if (!textureData) {
   1.185          SDL_SetError("Texture is not currently available");
   1.186 @@ -1994,7 +2002,6 @@
   1.187       *
   1.188       * TODO, WinRT: consider avoiding the use of a staging texture in D3D11_LockTexture if/when the entire texture is being updated
   1.189       */
   1.190 -    D3D11_TEXTURE2D_DESC stagingTextureDesc;
   1.191      ID3D11Texture2D_GetDesc(textureData->mainTexture, &stagingTextureDesc);
   1.192      stagingTextureDesc.Width = rect->w;
   1.193      stagingTextureDesc.Height = rect->h;
   1.194 @@ -2012,7 +2019,6 @@
   1.195      }
   1.196  
   1.197      /* Get a write-only pointer to data in the staging texture: */
   1.198 -    D3D11_MAPPED_SUBRESOURCE textureMemory;
   1.199      result = ID3D11DeviceContext_Map(rendererData->d3dContext,
   1.200          (ID3D11Resource *)textureData->stagingTexture,
   1.201          0,
   1.202 @@ -2082,13 +2088,14 @@
   1.203  D3D11_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture)
   1.204  {
   1.205      D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
   1.206 +    D3D11_TextureData *textureData = NULL;
   1.207  
   1.208      if (texture == NULL) {
   1.209          rendererData->currentOffscreenRenderTargetView = NULL;
   1.210          return 0;
   1.211      }
   1.212  
   1.213 -    D3D11_TextureData *textureData = (D3D11_TextureData *) texture->driverdata;
   1.214 +    textureData = (D3D11_TextureData *) texture->driverdata;
   1.215  
   1.216      if (!textureData->mainTextureRenderTargetView) {
   1.217          return SDL_SetError("specified texture is not a render target");
   1.218 @@ -2124,6 +2131,11 @@
   1.219  D3D11_UpdateViewport(SDL_Renderer * renderer)
   1.220  {
   1.221      D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
   1.222 +    Float4X4 projection;
   1.223 +    Float4X4 view;
   1.224 +    SDL_FRect orientationAlignedViewport;
   1.225 +    BOOL swapDimensions;
   1.226 +    D3D11_VIEWPORT viewport;
   1.227  
   1.228      if (renderer->viewport.w == 0 || renderer->viewport.h == 0) {
   1.229          /* If the viewport is empty, assume that it is because
   1.230 @@ -2138,7 +2150,6 @@
   1.231       * default coordinate system) so rotations will be done in the opposite
   1.232       * direction of the DXGI_MODE_ROTATION enumeration.
   1.233       */
   1.234 -    Float4X4 projection;
   1.235      switch (data->rotation) {
   1.236          case DXGI_MODE_ROTATION_IDENTITY:
   1.237              projection = MatrixIdentity();
   1.238 @@ -2157,7 +2168,6 @@
   1.239      }
   1.240  
   1.241      /* Update the view matrix */
   1.242 -    Float4X4 view;
   1.243      view.m[0][0] = 2.0f / renderer->viewport.w;
   1.244      view.m[0][1] = 0.0f;
   1.245      view.m[0][2] = 0.0f;
   1.246 @@ -2191,8 +2201,7 @@
   1.247       * a landscape mode, for all Windows 8/RT devices, or a portrait mode,
   1.248       * for Windows Phone devices.
   1.249       */
   1.250 -    SDL_FRect orientationAlignedViewport;
   1.251 -    const BOOL swapDimensions = D3D11_IsDisplayRotated90Degrees(data->rotation);
   1.252 +    swapDimensions = D3D11_IsDisplayRotated90Degrees(data->rotation);
   1.253      if (swapDimensions) {
   1.254          orientationAlignedViewport.x = (float) renderer->viewport.y;
   1.255          orientationAlignedViewport.y = (float) renderer->viewport.x;
   1.256 @@ -2206,7 +2215,6 @@
   1.257      }
   1.258      /* TODO, WinRT: get custom viewports working with non-Landscape modes (Portrait, PortraitFlipped, and LandscapeFlipped) */
   1.259  
   1.260 -    D3D11_VIEWPORT viewport;
   1.261      viewport.TopLeftX = orientationAlignedViewport.x;
   1.262      viewport.TopLeftY = orientationAlignedViewport.y;
   1.263      viewport.Width = orientationAlignedViewport.w;
   1.264 @@ -2281,6 +2289,9 @@
   1.265      D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
   1.266      D3D11_BUFFER_DESC vertexBufferDesc;
   1.267      HRESULT result = S_OK;
   1.268 +    D3D11_SUBRESOURCE_DATA vertexBufferData;
   1.269 +    const UINT stride = sizeof(VertexPositionColor);
   1.270 +    const UINT offset = 0;
   1.271  
   1.272      if (rendererData->vertexBuffer) {
   1.273          ID3D11Buffer_GetDesc(rendererData->vertexBuffer, &vertexBufferDesc);
   1.274 @@ -2311,7 +2322,6 @@
   1.275          vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
   1.276          vertexBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
   1.277  
   1.278 -        D3D11_SUBRESOURCE_DATA vertexBufferData;
   1.279          SDL_zero(vertexBufferData);
   1.280          vertexBufferData.pSysMem = vertexData;
   1.281          vertexBufferData.SysMemPitch = 0;
   1.282 @@ -2327,8 +2337,6 @@
   1.283              return -1;
   1.284          }
   1.285  
   1.286 -        UINT stride = sizeof(VertexPositionColor);
   1.287 -        UINT offset = 0;
   1.288          ID3D11DeviceContext_IASetVertexBuffers(rendererData->d3dContext,
   1.289              0,
   1.290              1,
   1.291 @@ -2345,6 +2353,7 @@
   1.292  D3D11_RenderStartDrawOp(SDL_Renderer * renderer)
   1.293  {
   1.294      D3D11_RenderData *rendererData = (D3D11_RenderData *)renderer->driverdata;
   1.295 +    ID3D11RasterizerState *rasterizerState;
   1.296      ID3D11RenderTargetView *renderTargetView = D3D11_GetCurrentRenderTargetView(renderer);
   1.297      if (renderTargetView != rendererData->currentRenderTargetView) {
   1.298          ID3D11DeviceContext_OMSetRenderTargets(rendererData->d3dContext,
   1.299 @@ -2355,7 +2364,6 @@
   1.300          rendererData->currentRenderTargetView = renderTargetView;
   1.301      }
   1.302  
   1.303 -    ID3D11RasterizerState *rasterizerState;
   1.304      if (SDL_RectEmpty(&renderer->clip_rect)) {
   1.305          rasterizerState = rendererData->mainRasterizer;
   1.306      } else {
   1.307 @@ -2437,14 +2445,16 @@
   1.308  {
   1.309      D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
   1.310      float r, g, b, a;
   1.311 +    VertexPositionColor *vertices;
   1.312 +    int i;
   1.313  
   1.314      r = (float)(renderer->r / 255.0f);
   1.315      g = (float)(renderer->g / 255.0f);
   1.316      b = (float)(renderer->b / 255.0f);
   1.317      a = (float)(renderer->a / 255.0f);
   1.318  
   1.319 -    VertexPositionColor * vertices = SDL_stack_alloc(VertexPositionColor, count);
   1.320 -    for (int i = 0; i < min(count, 128); ++i) {
   1.321 +    vertices = SDL_stack_alloc(VertexPositionColor, count);
   1.322 +    for (i = 0; i < min(count, 128); ++i) {
   1.323          const VertexPositionColor v = { { points[i].x, points[i].y, 0.0f }, { 0.0f, 0.0f }, { r, g, b, a } };
   1.324          vertices[i] = v;
   1.325      }
   1.326 @@ -2474,14 +2484,16 @@
   1.327  {
   1.328      D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
   1.329      float r, g, b, a;
   1.330 +    VertexPositionColor *vertices;
   1.331 +    int i;
   1.332  
   1.333      r = (float)(renderer->r / 255.0f);
   1.334      g = (float)(renderer->g / 255.0f);
   1.335      b = (float)(renderer->b / 255.0f);
   1.336      a = (float)(renderer->a / 255.0f);
   1.337  
   1.338 -    VertexPositionColor * vertices = SDL_stack_alloc(VertexPositionColor, count);
   1.339 -    for (int i = 0; i < count; ++i) {
   1.340 +    vertices = SDL_stack_alloc(VertexPositionColor, count);
   1.341 +    for (i = 0; i < count; ++i) {
   1.342          const VertexPositionColor v = { { points[i].x, points[i].y, 0.0f }, { 0.0f, 0.0f }, { r, g, b, a } };
   1.343          vertices[i] = v;
   1.344      }
   1.345 @@ -2511,22 +2523,23 @@
   1.346  {
   1.347      D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
   1.348      float r, g, b, a;
   1.349 +    int i;
   1.350  
   1.351      r = (float)(renderer->r / 255.0f);
   1.352      g = (float)(renderer->g / 255.0f);
   1.353      b = (float)(renderer->b / 255.0f);
   1.354      a = (float)(renderer->a / 255.0f);
   1.355  
   1.356 -    for (int i = 0; i < count; ++i) {
   1.357 -        D3D11_RenderStartDrawOp(renderer);
   1.358 -        D3D11_RenderSetBlendMode(renderer, renderer->blendMode);
   1.359 -
   1.360 +    for (i = 0; i < count; ++i) {
   1.361          VertexPositionColor vertices[] = {
   1.362              { { rects[i].x, rects[i].y, 0.0f },                             { 0.0f, 0.0f}, {r, g, b, a} },
   1.363              { { rects[i].x, rects[i].y + rects[i].h, 0.0f },                { 0.0f, 0.0f }, { r, g, b, a } },
   1.364              { { rects[i].x + rects[i].w, rects[i].y, 0.0f },                { 0.0f, 0.0f }, { r, g, b, a } },
   1.365              { { rects[i].x + rects[i].w, rects[i].y + rects[i].h, 0.0f },   { 0.0f, 0.0f }, { r, g, b, a } },
   1.366          };
   1.367 +
   1.368 +        D3D11_RenderStartDrawOp(renderer);
   1.369 +        D3D11_RenderSetBlendMode(renderer, renderer->blendMode);
   1.370          if (D3D11_UpdateVertexBuffer(renderer, vertices, sizeof(vertices)) != 0) {
   1.371              return -1;
   1.372          }
   1.373 @@ -2566,39 +2579,65 @@
   1.374  {
   1.375      D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
   1.376      D3D11_TextureData *textureData = (D3D11_TextureData *) texture->driverdata;
   1.377 +    float minu, maxu, minv, maxv;
   1.378 +    Float4 color;
   1.379 +    VertexPositionColor vertices[4];
   1.380 +    ID3D11SamplerState *textureSampler;
   1.381  
   1.382      D3D11_RenderStartDrawOp(renderer);
   1.383      D3D11_RenderSetBlendMode(renderer, texture->blendMode);
   1.384  
   1.385 -    float minu = (float) srcrect->x / texture->w;
   1.386 -    float maxu = (float) (srcrect->x + srcrect->w) / texture->w;
   1.387 -    float minv = (float) srcrect->y / texture->h;
   1.388 -    float maxv = (float) (srcrect->y + srcrect->h) / texture->h;
   1.389 +    minu = (float) srcrect->x / texture->w;
   1.390 +    maxu = (float) (srcrect->x + srcrect->w) / texture->w;
   1.391 +    minv = (float) srcrect->y / texture->h;
   1.392 +    maxv = (float) (srcrect->y + srcrect->h) / texture->h;
   1.393  
   1.394 -    float r = 1.0f;
   1.395 -    float g = 1.0f;
   1.396 -    float b = 1.0f;
   1.397 -    float a = 1.0f;
   1.398 +    color.x = 1.0f;     /* red */
   1.399 +    color.y = 1.0f;     /* green */
   1.400 +    color.z = 1.0f;     /* blue */
   1.401 +    color.w = 1.0f;     /* alpha */
   1.402      if (texture->modMode & SDL_TEXTUREMODULATE_COLOR) {
   1.403 -        r = (float)(texture->r / 255.0f);
   1.404 -        g = (float)(texture->g / 255.0f);
   1.405 -        b = (float)(texture->b / 255.0f);
   1.406 +        color.x = (float)(texture->r / 255.0f);     /* red */
   1.407 +        color.y = (float)(texture->g / 255.0f);     /* green */
   1.408 +        color.z = (float)(texture->b / 255.0f);     /* blue */
   1.409      }
   1.410      if (texture->modMode & SDL_TEXTUREMODULATE_ALPHA) {
   1.411 -        a = (float)(texture->a / 255.0f);
   1.412 +        color.w = (float)(texture->a / 255.0f);     /* alpha */
   1.413      }
   1.414  
   1.415 -    VertexPositionColor vertices[] = {
   1.416 -        { { dstrect->x, dstrect->y, 0.0f },                             { minu, minv }, { r, g, b, a } },
   1.417 -        { { dstrect->x, dstrect->y + dstrect->h, 0.0f },                { minu, maxv }, { r, g, b, a } },
   1.418 -        { { dstrect->x + dstrect->w, dstrect->y, 0.0f },                { maxu, minv }, { r, g, b, a } },
   1.419 -        { { dstrect->x + dstrect->w, dstrect->y + dstrect->h, 0.0f },   { maxu, maxv }, { r, g, b, a } },
   1.420 -    };
   1.421 +    vertices[0].pos.x = dstrect->x;
   1.422 +    vertices[0].pos.y = dstrect->y;
   1.423 +    vertices[0].pos.z = 0.0f;
   1.424 +    vertices[0].tex.x = minu;
   1.425 +    vertices[0].tex.y = minv;
   1.426 +    vertices[0].color = color;
   1.427 +
   1.428 +    vertices[1].pos.x = dstrect->x;
   1.429 +    vertices[1].pos.y = dstrect->y + dstrect->h;
   1.430 +    vertices[1].pos.z = 0.0f;
   1.431 +    vertices[1].tex.x = minu;
   1.432 +    vertices[1].tex.y = maxv;
   1.433 +    vertices[1].color = color;
   1.434 +
   1.435 +    vertices[2].pos.x = dstrect->x + dstrect->w;
   1.436 +    vertices[2].pos.y = dstrect->y;
   1.437 +    vertices[2].pos.z = 0.0f;
   1.438 +    vertices[2].tex.x = maxu;
   1.439 +    vertices[2].tex.y = minv;
   1.440 +    vertices[2].color = color;
   1.441 +
   1.442 +    vertices[3].pos.x = dstrect->x + dstrect->w;
   1.443 +    vertices[3].pos.y = dstrect->y + dstrect->h;
   1.444 +    vertices[3].pos.z = 0.0f;
   1.445 +    vertices[3].tex.x = maxu;
   1.446 +    vertices[3].tex.y = maxv;
   1.447 +    vertices[3].color = color;
   1.448 +
   1.449      if (D3D11_UpdateVertexBuffer(renderer, vertices, sizeof(vertices)) != 0) {
   1.450          return -1;
   1.451      }
   1.452  
   1.453 -    ID3D11SamplerState *textureSampler = D3D11_RenderGetSampler(renderer, texture);
   1.454 +    textureSampler = D3D11_RenderGetSampler(renderer, texture);
   1.455      if (textureData->yuv) {
   1.456          ID3D11ShaderResourceView *shaderResources[] = {
   1.457              textureData->mainTextureResourceView,
   1.458 @@ -2632,26 +2671,32 @@
   1.459  {
   1.460      D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
   1.461      D3D11_TextureData *textureData = (D3D11_TextureData *) texture->driverdata;
   1.462 +    float minu, maxu, minv, maxv;
   1.463 +    Float4 color;
   1.464 +    Float4X4 modelMatrix;
   1.465 +    float minx, maxx, miny, maxy;
   1.466 +    VertexPositionColor vertices[4];
   1.467 +    ID3D11SamplerState *textureSampler;
   1.468  
   1.469      D3D11_RenderStartDrawOp(renderer);
   1.470      D3D11_RenderSetBlendMode(renderer, texture->blendMode);
   1.471  
   1.472 -    float minu = (float) srcrect->x / texture->w;
   1.473 -    float maxu = (float) (srcrect->x + srcrect->w) / texture->w;
   1.474 -    float minv = (float) srcrect->y / texture->h;
   1.475 -    float maxv = (float) (srcrect->y + srcrect->h) / texture->h;
   1.476 +    minu = (float) srcrect->x / texture->w;
   1.477 +    maxu = (float) (srcrect->x + srcrect->w) / texture->w;
   1.478 +    minv = (float) srcrect->y / texture->h;
   1.479 +    maxv = (float) (srcrect->y + srcrect->h) / texture->h;
   1.480  
   1.481 -    float r = 1.0f;
   1.482 -    float g = 1.0f;
   1.483 -    float b = 1.0f;
   1.484 -    float a = 1.0f;
   1.485 +    color.x = 1.0f;     /* red */
   1.486 +    color.y = 1.0f;     /* green */
   1.487 +    color.z = 1.0f;     /* blue */
   1.488 +    color.w = 1.0f;     /* alpha */
   1.489      if (texture->modMode & SDL_TEXTUREMODULATE_COLOR) {
   1.490 -        r = (float)(texture->r / 255.0f);
   1.491 -        g = (float)(texture->g / 255.0f);
   1.492 -        b = (float)(texture->b / 255.0f);
   1.493 +        color.x = (float)(texture->r / 255.0f);     /* red */
   1.494 +        color.y = (float)(texture->g / 255.0f);     /* green */
   1.495 +        color.z = (float)(texture->b / 255.0f);     /* blue */
   1.496      }
   1.497      if (texture->modMode & SDL_TEXTUREMODULATE_ALPHA) {
   1.498 -        a = (float)(texture->a / 255.0f);
   1.499 +        color.w = (float)(texture->a / 255.0f);     /* alpha */
   1.500      }
   1.501  
   1.502      if (flip & SDL_FLIP_HORIZONTAL) {
   1.503 @@ -2665,28 +2710,50 @@
   1.504          minv = tmp;
   1.505      }
   1.506  
   1.507 -    Float4X4 modelMatrix = MatrixMultiply(
   1.508 +    modelMatrix = MatrixMultiply(
   1.509              MatrixRotationZ((float)(M_PI * (float) angle / 180.0f)),
   1.510              MatrixTranslation(dstrect->x + center->x, dstrect->y + center->y, 0)
   1.511              );
   1.512      D3D11_SetModelMatrix(renderer, &modelMatrix);
   1.513  
   1.514 -    const float minx = -center->x;
   1.515 -    const float maxx = dstrect->w - center->x;
   1.516 -    const float miny = -center->y;
   1.517 -    const float maxy = dstrect->h - center->y;
   1.518 +    minx = -center->x;
   1.519 +    maxx = dstrect->w - center->x;
   1.520 +    miny = -center->y;
   1.521 +    maxy = dstrect->h - center->y;
   1.522  
   1.523 -    VertexPositionColor vertices[] = {
   1.524 -        {{minx, miny, 0.0f}, {minu, minv}, {r, g, b, a}},
   1.525 -        {{minx, maxy, 0.0f}, {minu, maxv}, {r, g, b, a}},
   1.526 -        {{maxx, miny, 0.0f}, {maxu, minv}, {r, g, b, a}},
   1.527 -        {{maxx, maxy, 0.0f}, {maxu, maxv}, {r, g, b, a}},
   1.528 -    };
   1.529 +    vertices[0].pos.x = minx;
   1.530 +    vertices[0].pos.y = miny;
   1.531 +    vertices[0].pos.z = 0.0f;
   1.532 +    vertices[0].tex.x = minu;
   1.533 +    vertices[0].tex.y = minv;
   1.534 +    vertices[0].color = color;
   1.535 +    
   1.536 +    vertices[1].pos.x = minx;
   1.537 +    vertices[1].pos.y = maxy;
   1.538 +    vertices[1].pos.z = 0.0f;
   1.539 +    vertices[1].tex.x = minu;
   1.540 +    vertices[1].tex.y = maxv;
   1.541 +    vertices[1].color = color;
   1.542 +    
   1.543 +    vertices[2].pos.x = maxx;
   1.544 +    vertices[2].pos.y = miny;
   1.545 +    vertices[2].pos.z = 0.0f;
   1.546 +    vertices[2].tex.x = maxu;
   1.547 +    vertices[2].tex.y = minv;
   1.548 +    vertices[2].color = color;
   1.549 +    
   1.550 +    vertices[3].pos.x = maxx;
   1.551 +    vertices[3].pos.y = maxy;
   1.552 +    vertices[3].pos.z = 0.0f;
   1.553 +    vertices[3].tex.x = maxu;
   1.554 +    vertices[3].tex.y = maxv;
   1.555 +    vertices[3].color = color;
   1.556 +
   1.557      if (D3D11_UpdateVertexBuffer(renderer, vertices, sizeof(vertices)) != 0) {
   1.558          return -1;
   1.559      }
   1.560  
   1.561 -    ID3D11SamplerState *textureSampler = D3D11_RenderGetSampler(renderer, texture);
   1.562 +    textureSampler = D3D11_RenderGetSampler(renderer, texture);
   1.563      if (textureData->yuv) {
   1.564          ID3D11ShaderResourceView *shaderResources[] = {
   1.565              textureData->mainTextureResourceView,
   1.566 @@ -2724,6 +2791,10 @@
   1.567      ID3D11Texture2D *stagingTexture = NULL;
   1.568      HRESULT result;
   1.569      int status = -1;
   1.570 +    D3D11_TEXTURE2D_DESC stagingTextureDesc;
   1.571 +    D3D11_RECT srcRect;
   1.572 +    D3D11_BOX srcBox;
   1.573 +    D3D11_MAPPED_SUBRESOURCE textureMemory;
   1.574  
   1.575      /* Retrieve a pointer to the back buffer: */
   1.576      result = IDXGISwapChain_GetBuffer(data->swapChain,
   1.577 @@ -2737,7 +2808,6 @@
   1.578      }
   1.579  
   1.580      /* Create a staging texture to copy the screen's data to: */
   1.581 -    D3D11_TEXTURE2D_DESC stagingTextureDesc;
   1.582      ID3D11Texture2D_GetDesc(backBuffer, &stagingTextureDesc);
   1.583      stagingTextureDesc.Width = rect->w;
   1.584      stagingTextureDesc.Height = rect->h;
   1.585 @@ -2755,13 +2825,11 @@
   1.586      }
   1.587  
   1.588      /* Copy the desired portion of the back buffer to the staging texture: */
   1.589 -    D3D11_RECT srcRect;
   1.590      if (D3D11_GetViewportAlignedD3DRect(renderer, rect, &srcRect) != 0) {
   1.591          /* D3D11_GetViewportAlignedD3DRect will have set the SDL error */
   1.592          goto done;
   1.593      }
   1.594  
   1.595 -    D3D11_BOX srcBox;
   1.596      srcBox.left = srcRect.left;
   1.597      srcBox.right = srcRect.right;
   1.598      srcBox.top = srcRect.top;
   1.599 @@ -2777,7 +2845,6 @@
   1.600          &srcBox);
   1.601  
   1.602      /* Map the staging texture's data to CPU-accessible memory: */
   1.603 -    D3D11_MAPPED_SUBRESOURCE textureMemory;
   1.604      result = ID3D11DeviceContext_Map(data->d3dContext,
   1.605          (ID3D11Resource *)stagingTexture,
   1.606          0,
   1.607 @@ -2828,6 +2895,8 @@
   1.608      D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
   1.609      UINT syncInterval;
   1.610      UINT presentFlags;
   1.611 +    HRESULT result;
   1.612 +    DXGI_PRESENT_PARAMETERS parameters;
   1.613  
   1.614      if (renderer->info.flags & SDL_RENDERER_PRESENTVSYNC) {
   1.615          syncInterval = 1;
   1.616 @@ -2838,15 +2907,14 @@
   1.617      }
   1.618  
   1.619  #if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
   1.620 -    HRESULT result = IDXGISwapChain_Present(data->swapChain, syncInterval, presentFlags);
   1.621 +    result = IDXGISwapChain_Present(data->swapChain, syncInterval, presentFlags);
   1.622  #else
   1.623      /* The application may optionally specify "dirty" or "scroll"
   1.624       * rects to improve efficiency in certain scenarios.
   1.625       * This option is not available on Windows Phone 8, to note.
   1.626       */
   1.627 -    DXGI_PRESENT_PARAMETERS parameters;
   1.628      SDL_zero(parameters);
   1.629 -    HRESULT result = IDXGISwapChain1_Present1(data->swapChain, syncInterval, presentFlags, &parameters);
   1.630 +    result = IDXGISwapChain1_Present1(data->swapChain, syncInterval, presentFlags, &parameters);
   1.631  #endif
   1.632  
   1.633      /* Discard the contents of the render target.