WinRT: D3D 11.1 blending mode support added; FillRects coloring bug-fix
authorDavid Ludwig <dludwig@pobox.com>
Sat, 16 Feb 2013 16:53:06 -0500
changeset 84312078dc4f4378
parent 8430 87a1dbf2d1d2
child 8432 929e53386278
WinRT: D3D 11.1 blending mode support added; FillRects coloring bug-fix
src/render/direct3d11/SDL_render_d3d11.cpp
src/render/direct3d11/SDL_render_d3d11_cpp.h
     1.1 --- a/src/render/direct3d11/SDL_render_d3d11.cpp	Sat Feb 16 16:13:48 2013 -0500
     1.2 +++ b/src/render/direct3d11/SDL_render_d3d11.cpp	Sat Feb 16 16:53:06 2013 -0500
     1.3 @@ -240,6 +240,37 @@
     1.4      return S_OK;
     1.5  }
     1.6  
     1.7 +static HRESULT
     1.8 +D3D11_CreateBlendMode(SDL_Renderer * renderer,
     1.9 +                      BOOL enableBlending,
    1.10 +                      D3D11_BLEND srcBlend,
    1.11 +                      D3D11_BLEND destBlend,
    1.12 +                      ID3D11BlendState ** blendStateOutput)
    1.13 +{
    1.14 +    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
    1.15 +    HRESULT result = S_OK;
    1.16 +
    1.17 +    D3D11_BLEND_DESC blendDesc;
    1.18 +    memset(&blendDesc, 0, sizeof(blendDesc));
    1.19 +    blendDesc.AlphaToCoverageEnable = FALSE;
    1.20 +    blendDesc.IndependentBlendEnable = FALSE;
    1.21 +    blendDesc.RenderTarget[0].BlendEnable = enableBlending;
    1.22 +    blendDesc.RenderTarget[0].SrcBlend = srcBlend;
    1.23 +    blendDesc.RenderTarget[0].DestBlend = destBlend;
    1.24 +    blendDesc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
    1.25 +    blendDesc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE;
    1.26 +    blendDesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO;
    1.27 +    blendDesc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
    1.28 +    blendDesc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
    1.29 +    result = data->d3dDevice->CreateBlendState(&blendDesc, blendStateOutput);
    1.30 +    if (FAILED(result)) {
    1.31 +        WIN_SetErrorFromHRESULT(__FUNCTION__, result);
    1.32 +        return result;
    1.33 +    }
    1.34 +
    1.35 +    return S_OK;
    1.36 +}
    1.37 +
    1.38  // Create resources that depend on the device.
    1.39  HRESULT
    1.40  D3D11_CreateDeviceResources(SDL_Renderer * renderer)
    1.41 @@ -429,6 +460,42 @@
    1.42      }
    1.43  
    1.44      //
    1.45 +    // Create blending states:
    1.46 +    //
    1.47 +    result = D3D11_CreateBlendMode(
    1.48 +        renderer,
    1.49 +        TRUE,
    1.50 +        D3D11_BLEND_SRC_ALPHA,
    1.51 +        D3D11_BLEND_INV_SRC_ALPHA,
    1.52 +        &data->blendModeBlend);
    1.53 +    if (FAILED(result)) {
    1.54 +        // D3D11_CreateBlendMode will set the SDL error, if it fails
    1.55 +        return result;
    1.56 +    }
    1.57 +
    1.58 +    result = D3D11_CreateBlendMode(
    1.59 +        renderer,
    1.60 +        TRUE,
    1.61 +        D3D11_BLEND_SRC_ALPHA,
    1.62 +        D3D11_BLEND_ONE,
    1.63 +        &data->blendModeAdd);
    1.64 +    if (FAILED(result)) {
    1.65 +        // D3D11_CreateBlendMode will set the SDL error, if it fails
    1.66 +        return result;
    1.67 +    }
    1.68 +
    1.69 +    result = D3D11_CreateBlendMode(
    1.70 +        renderer,
    1.71 +        TRUE,
    1.72 +        D3D11_BLEND_ZERO,
    1.73 +        D3D11_BLEND_SRC_COLOR,
    1.74 +        &data->blendModeMod);
    1.75 +    if (FAILED(result)) {
    1.76 +        // D3D11_CreateBlendMode will set the SDL error, if it fails
    1.77 +        return result;
    1.78 +    }
    1.79 +
    1.80 +    //
    1.81      // All done!
    1.82      //
    1.83      data->loadingComplete = true;       // This variable can probably be factored-out
    1.84 @@ -960,6 +1027,21 @@
    1.85  		0,
    1.86  		0
    1.87  		);
    1.88 +
    1.89 +    switch (renderer->blendMode) {
    1.90 +        case SDL_BLENDMODE_BLEND:
    1.91 +            rendererData->d3dContext->OMSetBlendState(rendererData->blendModeBlend.Get(), 0, 0xFFFFFFFF);
    1.92 +            break;
    1.93 +        case SDL_BLENDMODE_ADD:
    1.94 +            rendererData->d3dContext->OMSetBlendState(rendererData->blendModeAdd.Get(), 0, 0xFFFFFFFF);
    1.95 +            break;
    1.96 +        case SDL_BLENDMODE_MOD:
    1.97 +            rendererData->d3dContext->OMSetBlendState(rendererData->blendModeMod.Get(), 0, 0xFFFFFFFF);
    1.98 +            break;
    1.99 +        case SDL_BLENDMODE_NONE:
   1.100 +            rendererData->d3dContext->OMSetBlendState(NULL, 0, 0xFFFFFFFF);
   1.101 +            break;
   1.102 +    }
   1.103  }
   1.104  
   1.105  static void
   1.106 @@ -994,12 +1076,12 @@
   1.107      D3D11_RenderData *rendererData = (D3D11_RenderData *) renderer->driverdata;
   1.108      float r, g, b, a;
   1.109  
   1.110 -    r = (float)(renderer->r / 255);
   1.111 -    g = (float)(renderer->g / 255);
   1.112 -    b = (float)(renderer->b / 255);
   1.113 -    a = (float)(renderer->a / 255);
   1.114 +    r = (float)(renderer->r / 255.0f);
   1.115 +    g = (float)(renderer->g / 255.0f);
   1.116 +    b = (float)(renderer->b / 255.0f);
   1.117 +    a = (float)(renderer->a / 255.0f);
   1.118  
   1.119 -    D3D11_RenderStartDrawOp(renderer);
   1.120 +    D3D11_RenderStartDrawOp(renderer);
   1.121  
   1.122      for (int i = 0; i < count; ++i) {
   1.123          VertexPositionColor vertices[] = {
     2.1 --- a/src/render/direct3d11/SDL_render_d3d11_cpp.h	Sat Feb 16 16:13:48 2013 -0500
     2.2 +++ b/src/render/direct3d11/SDL_render_d3d11_cpp.h	Sat Feb 16 16:53:06 2013 -0500
     2.3 @@ -41,6 +41,9 @@
     2.4      Microsoft::WRL::ComPtr<ID3D11VertexShader> vertexShader;
     2.5      Microsoft::WRL::ComPtr<ID3D11PixelShader> texturePixelShader;
     2.6      Microsoft::WRL::ComPtr<ID3D11PixelShader> colorPixelShader;
     2.7 +    Microsoft::WRL::ComPtr<ID3D11BlendState> blendModeBlend;
     2.8 +    Microsoft::WRL::ComPtr<ID3D11BlendState> blendModeAdd;
     2.9 +    Microsoft::WRL::ComPtr<ID3D11BlendState> blendModeMod;
    2.10      Microsoft::WRL::ComPtr<ID3D11SamplerState> mainSampler;
    2.11      Microsoft::WRL::ComPtr<ID3D11RasterizerState> mainRasterizer;
    2.12      D3D_FEATURE_LEVEL featureLevel;