Added a BLENDMODE_MASK pixel shader so render tests succeed
authorSam Lantinga <slouken@libsdl.org>
Mon, 14 Dec 2009 04:19:00 +0000
changeset 35569c2e92de786a
parent 3555 7549b986ccb1
child 3557 7184580ab756
Added a BLENDMODE_MASK pixel shader so render tests succeed
src/video/win32/SDL_d3drender.c
     1.1 --- a/src/video/win32/SDL_d3drender.c	Sun Dec 13 08:00:25 2009 +0000
     1.2 +++ b/src/video/win32/SDL_d3drender.c	Mon Dec 14 04:19:00 2009 +0000
     1.3 @@ -26,6 +26,58 @@
     1.4  #include "SDL_win32video.h"
     1.5  #include "../SDL_yuv_sw_c.h"
     1.6  
     1.7 +#ifdef ASSEMBLE_SHADER
     1.8 +///////////////////////////////////////////////////////////////////////////
     1.9 +// ID3DXBuffer:
    1.10 +// ------------
    1.11 +// The buffer object is used by D3DX to return arbitrary size data.
    1.12 +//
    1.13 +// GetBufferPointer -
    1.14 +//    Returns a pointer to the beginning of the buffer.
    1.15 +//
    1.16 +// GetBufferSize -
    1.17 +//    Returns the size of the buffer, in bytes.
    1.18 +///////////////////////////////////////////////////////////////////////////
    1.19 +
    1.20 +typedef interface ID3DXBuffer ID3DXBuffer;
    1.21 +typedef interface ID3DXBuffer *LPD3DXBUFFER;
    1.22 +
    1.23 +// {8BA5FB08-5195-40e2-AC58-0D989C3A0102}
    1.24 +DEFINE_GUID(IID_ID3DXBuffer, 
    1.25 +0x8ba5fb08, 0x5195, 0x40e2, 0xac, 0x58, 0xd, 0x98, 0x9c, 0x3a, 0x1, 0x2);
    1.26 +
    1.27 +#undef INTERFACE
    1.28 +#define INTERFACE ID3DXBuffer
    1.29 +
    1.30 +typedef interface ID3DXBuffer {
    1.31 +    const struct ID3DXBufferVtbl FAR* lpVtbl;
    1.32 +} ID3DXBuffer;
    1.33 +typedef const struct ID3DXBufferVtbl ID3DXBufferVtbl;
    1.34 +const struct ID3DXBufferVtbl
    1.35 +{
    1.36 +    // IUnknown
    1.37 +    STDMETHOD(QueryInterface)(THIS_ REFIID iid, LPVOID *ppv) PURE;
    1.38 +    STDMETHOD_(ULONG, AddRef)(THIS) PURE;
    1.39 +    STDMETHOD_(ULONG, Release)(THIS) PURE;
    1.40 +
    1.41 +    // ID3DXBuffer
    1.42 +    STDMETHOD_(LPVOID, GetBufferPointer)(THIS) PURE;
    1.43 +    STDMETHOD_(DWORD, GetBufferSize)(THIS) PURE;
    1.44 +};
    1.45 +
    1.46 +HRESULT WINAPI
    1.47 +    D3DXAssembleShader(
    1.48 +        LPCSTR                          pSrcData,
    1.49 +        UINT                            SrcDataLen,
    1.50 +        CONST LPVOID*                   pDefines,
    1.51 +        LPVOID                          pInclude,
    1.52 +        DWORD                           Flags,
    1.53 +        LPD3DXBUFFER*                   ppShader,
    1.54 +        LPD3DXBUFFER*                   ppErrorMsgs);
    1.55 +
    1.56 +#endif /* ASSEMBLE_SHADER */
    1.57 +
    1.58 +
    1.59  /* Direct3D renderer implementation */
    1.60  
    1.61  #if 1                           /* This takes more memory but you won't lose your texture data */
    1.62 @@ -110,6 +162,7 @@
    1.63      IDirect3DDevice9 *device;
    1.64      UINT adapter;
    1.65      D3DPRESENT_PARAMETERS pparams;
    1.66 +    LPDIRECT3DPIXELSHADER9 ps_mask;
    1.67      SDL_bool beginScene;
    1.68  } D3D_RenderData;
    1.69  
    1.70 @@ -550,6 +603,45 @@
    1.71      IDirect3DDevice9_SetTextureStageState(data->device, 1, D3DTSS_ALPHAOP,
    1.72                                            D3DTOP_DISABLE);
    1.73  
    1.74 +    {
    1.75 +#ifdef ASSEMBLE_SHADER
    1.76 +        const char *shader_text =
    1.77 +"ps_1_1\n"
    1.78 +"def c0, 0, 0, 0, 0.496\n"
    1.79 +"def c1, 0, 0, 0, 1\n"
    1.80 +"def c2, 0, 0, 0, -1\n"
    1.81 +"tex t0\n"
    1.82 +"mul r1, t0, v0\n"
    1.83 +"add r0, r1, c0\n"
    1.84 +"cnd r0, r0.a, c1, c2\n"
    1.85 +"add r0, r0, r1\n";
    1.86 +        LPD3DXBUFFER pCode;         // buffer with the assembled shader code
    1.87 +        LPD3DXBUFFER pErrorMsgs;    // buffer with error messages
    1.88 +        LPDWORD shader_data;
    1.89 +        DWORD   shader_size;
    1.90 +        result = D3DXAssembleShader( shader_text, SDL_strlen(shader_text), NULL, NULL, 0, &pCode, &pErrorMsgs );
    1.91 +        if (FAILED(result)) {
    1.92 +            D3D_SetError("D3DXAssembleShader()", result);
    1.93 +        }
    1.94 +        shader_data = (DWORD*)pCode->lpVtbl->GetBufferPointer(pCode);
    1.95 +        shader_size = pCode->lpVtbl->GetBufferSize(pCode);
    1.96 +#else
    1.97 +        const DWORD shader_data[] = {
    1.98 +            0xffff0101,0x00000051,0xa00f0000,0x00000000,0x00000000,0x00000000,
    1.99 +            0x3efdf3b6,0x00000051,0xa00f0001,0x00000000,0x00000000,0x00000000,
   1.100 +            0x3f800000,0x00000051,0xa00f0002,0x00000000,0x00000000,0x00000000,
   1.101 +            0xbf800000,0x00000042,0xb00f0000,0x00000005,0x800f0001,0xb0e40000,
   1.102 +            0x90e40000,0x00000002,0x800f0000,0x80e40001,0xa0e40000,0x00000050,
   1.103 +            0x800f0000,0x80ff0000,0xa0e40001,0xa0e40002,0x00000002,0x800f0000,
   1.104 +            0x80e40000,0x80e40001,0x0000ffff
   1.105 +        };
   1.106 +#endif
   1.107 +        result = IDirect3DDevice9_CreatePixelShader(data->device, shader_data, &data->ps_mask);
   1.108 +        if (FAILED(result)) {
   1.109 +            D3D_SetError("CreatePixelShader()", result);
   1.110 +        }
   1.111 +    }
   1.112 +
   1.113      return renderer;
   1.114  }
   1.115  
   1.116 @@ -1115,6 +1207,7 @@
   1.117  {
   1.118      D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
   1.119      D3D_TextureData *texturedata = (D3D_TextureData *) texture->driverdata;
   1.120 +    LPDIRECT3DPIXELSHADER9 shader = NULL;
   1.121      float minx, miny, maxx, maxy;
   1.122      float minu, maxu, minv, maxv;
   1.123      DWORD color;
   1.124 @@ -1172,6 +1265,10 @@
   1.125  
   1.126      D3D_SetBlendMode(data, texture->blendMode);
   1.127  
   1.128 +    if (texture->blendMode == SDL_BLENDMODE_MASK) {
   1.129 +        shader = data->ps_mask;
   1.130 +    }
   1.131 +
   1.132      switch (texture->scaleMode) {
   1.133      case SDL_TEXTURESCALEMODE_NONE:
   1.134      case SDL_TEXTURESCALEMODE_FAST:
   1.135 @@ -1201,6 +1298,13 @@
   1.136          D3D_SetError("SetTexture()", result);
   1.137          return -1;
   1.138      }
   1.139 +    if (shader) {
   1.140 +        result = IDirect3DDevice9_SetPixelShader(data->device, shader);
   1.141 +        if (FAILED(result)) {
   1.142 +            D3D_SetError("SetShader()", result);
   1.143 +            return -1;
   1.144 +        }
   1.145 +    }
   1.146      result =
   1.147          IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_TRIANGLEFAN, 2,
   1.148                                           vertices, sizeof(*vertices));
   1.149 @@ -1208,6 +1312,13 @@
   1.150          D3D_SetError("DrawPrimitiveUP()", result);
   1.151          return -1;
   1.152      }
   1.153 +    if (shader) {
   1.154 +        result = IDirect3DDevice9_SetPixelShader(data->device, NULL);
   1.155 +        if (FAILED(result)) {
   1.156 +            D3D_SetError("SetShader()", result);
   1.157 +            return -1;
   1.158 +        }
   1.159 +    }
   1.160      return 0;
   1.161  }
   1.162