comparison src/video/win32/SDL_d3drender.c @ 3556:9c2e92de786a

Added a BLENDMODE_MASK pixel shader so render tests succeed
author Sam Lantinga <slouken@libsdl.org>
date Mon, 14 Dec 2009 04:19:00 +0000
parents a579694613fd
children 0f958e527e5e
comparison
equal deleted inserted replaced
3555:7549b986ccb1 3556:9c2e92de786a
23 23
24 #if SDL_VIDEO_RENDER_D3D 24 #if SDL_VIDEO_RENDER_D3D
25 25
26 #include "SDL_win32video.h" 26 #include "SDL_win32video.h"
27 #include "../SDL_yuv_sw_c.h" 27 #include "../SDL_yuv_sw_c.h"
28
29 #ifdef ASSEMBLE_SHADER
30 ///////////////////////////////////////////////////////////////////////////
31 // ID3DXBuffer:
32 // ------------
33 // The buffer object is used by D3DX to return arbitrary size data.
34 //
35 // GetBufferPointer -
36 // Returns a pointer to the beginning of the buffer.
37 //
38 // GetBufferSize -
39 // Returns the size of the buffer, in bytes.
40 ///////////////////////////////////////////////////////////////////////////
41
42 typedef interface ID3DXBuffer ID3DXBuffer;
43 typedef interface ID3DXBuffer *LPD3DXBUFFER;
44
45 // {8BA5FB08-5195-40e2-AC58-0D989C3A0102}
46 DEFINE_GUID(IID_ID3DXBuffer,
47 0x8ba5fb08, 0x5195, 0x40e2, 0xac, 0x58, 0xd, 0x98, 0x9c, 0x3a, 0x1, 0x2);
48
49 #undef INTERFACE
50 #define INTERFACE ID3DXBuffer
51
52 typedef interface ID3DXBuffer {
53 const struct ID3DXBufferVtbl FAR* lpVtbl;
54 } ID3DXBuffer;
55 typedef const struct ID3DXBufferVtbl ID3DXBufferVtbl;
56 const struct ID3DXBufferVtbl
57 {
58 // IUnknown
59 STDMETHOD(QueryInterface)(THIS_ REFIID iid, LPVOID *ppv) PURE;
60 STDMETHOD_(ULONG, AddRef)(THIS) PURE;
61 STDMETHOD_(ULONG, Release)(THIS) PURE;
62
63 // ID3DXBuffer
64 STDMETHOD_(LPVOID, GetBufferPointer)(THIS) PURE;
65 STDMETHOD_(DWORD, GetBufferSize)(THIS) PURE;
66 };
67
68 HRESULT WINAPI
69 D3DXAssembleShader(
70 LPCSTR pSrcData,
71 UINT SrcDataLen,
72 CONST LPVOID* pDefines,
73 LPVOID pInclude,
74 DWORD Flags,
75 LPD3DXBUFFER* ppShader,
76 LPD3DXBUFFER* ppErrorMsgs);
77
78 #endif /* ASSEMBLE_SHADER */
79
28 80
29 /* Direct3D renderer implementation */ 81 /* Direct3D renderer implementation */
30 82
31 #if 1 /* This takes more memory but you won't lose your texture data */ 83 #if 1 /* This takes more memory but you won't lose your texture data */
32 #define D3DPOOL_SDL D3DPOOL_MANAGED 84 #define D3DPOOL_SDL D3DPOOL_MANAGED
108 { 160 {
109 IDirect3D9 *d3d; 161 IDirect3D9 *d3d;
110 IDirect3DDevice9 *device; 162 IDirect3DDevice9 *device;
111 UINT adapter; 163 UINT adapter;
112 D3DPRESENT_PARAMETERS pparams; 164 D3DPRESENT_PARAMETERS pparams;
165 LPDIRECT3DPIXELSHADER9 ps_mask;
113 SDL_bool beginScene; 166 SDL_bool beginScene;
114 } D3D_RenderData; 167 } D3D_RenderData;
115 168
116 typedef struct 169 typedef struct
117 { 170 {
548 IDirect3DDevice9_SetTextureStageState(data->device, 1, D3DTSS_COLOROP, 601 IDirect3DDevice9_SetTextureStageState(data->device, 1, D3DTSS_COLOROP,
549 D3DTOP_DISABLE); 602 D3DTOP_DISABLE);
550 IDirect3DDevice9_SetTextureStageState(data->device, 1, D3DTSS_ALPHAOP, 603 IDirect3DDevice9_SetTextureStageState(data->device, 1, D3DTSS_ALPHAOP,
551 D3DTOP_DISABLE); 604 D3DTOP_DISABLE);
552 605
606 {
607 #ifdef ASSEMBLE_SHADER
608 const char *shader_text =
609 "ps_1_1\n"
610 "def c0, 0, 0, 0, 0.496\n"
611 "def c1, 0, 0, 0, 1\n"
612 "def c2, 0, 0, 0, -1\n"
613 "tex t0\n"
614 "mul r1, t0, v0\n"
615 "add r0, r1, c0\n"
616 "cnd r0, r0.a, c1, c2\n"
617 "add r0, r0, r1\n";
618 LPD3DXBUFFER pCode; // buffer with the assembled shader code
619 LPD3DXBUFFER pErrorMsgs; // buffer with error messages
620 LPDWORD shader_data;
621 DWORD shader_size;
622 result = D3DXAssembleShader( shader_text, SDL_strlen(shader_text), NULL, NULL, 0, &pCode, &pErrorMsgs );
623 if (FAILED(result)) {
624 D3D_SetError("D3DXAssembleShader()", result);
625 }
626 shader_data = (DWORD*)pCode->lpVtbl->GetBufferPointer(pCode);
627 shader_size = pCode->lpVtbl->GetBufferSize(pCode);
628 #else
629 const DWORD shader_data[] = {
630 0xffff0101,0x00000051,0xa00f0000,0x00000000,0x00000000,0x00000000,
631 0x3efdf3b6,0x00000051,0xa00f0001,0x00000000,0x00000000,0x00000000,
632 0x3f800000,0x00000051,0xa00f0002,0x00000000,0x00000000,0x00000000,
633 0xbf800000,0x00000042,0xb00f0000,0x00000005,0x800f0001,0xb0e40000,
634 0x90e40000,0x00000002,0x800f0000,0x80e40001,0xa0e40000,0x00000050,
635 0x800f0000,0x80ff0000,0xa0e40001,0xa0e40002,0x00000002,0x800f0000,
636 0x80e40000,0x80e40001,0x0000ffff
637 };
638 #endif
639 result = IDirect3DDevice9_CreatePixelShader(data->device, shader_data, &data->ps_mask);
640 if (FAILED(result)) {
641 D3D_SetError("CreatePixelShader()", result);
642 }
643 }
644
553 return renderer; 645 return renderer;
554 } 646 }
555 647
556 static int 648 static int
557 D3D_Reset(SDL_Renderer * renderer) 649 D3D_Reset(SDL_Renderer * renderer)
1113 D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture, 1205 D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
1114 const SDL_Rect * srcrect, const SDL_Rect * dstrect) 1206 const SDL_Rect * srcrect, const SDL_Rect * dstrect)
1115 { 1207 {
1116 D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata; 1208 D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
1117 D3D_TextureData *texturedata = (D3D_TextureData *) texture->driverdata; 1209 D3D_TextureData *texturedata = (D3D_TextureData *) texture->driverdata;
1210 LPDIRECT3DPIXELSHADER9 shader = NULL;
1118 float minx, miny, maxx, maxy; 1211 float minx, miny, maxx, maxy;
1119 float minu, maxu, minv, maxv; 1212 float minu, maxu, minv, maxv;
1120 DWORD color; 1213 DWORD color;
1121 Vertex vertices[4]; 1214 Vertex vertices[4];
1122 HRESULT result; 1215 HRESULT result;
1170 vertices[3].u = minu; 1263 vertices[3].u = minu;
1171 vertices[3].v = maxv; 1264 vertices[3].v = maxv;
1172 1265
1173 D3D_SetBlendMode(data, texture->blendMode); 1266 D3D_SetBlendMode(data, texture->blendMode);
1174 1267
1268 if (texture->blendMode == SDL_BLENDMODE_MASK) {
1269 shader = data->ps_mask;
1270 }
1271
1175 switch (texture->scaleMode) { 1272 switch (texture->scaleMode) {
1176 case SDL_TEXTURESCALEMODE_NONE: 1273 case SDL_TEXTURESCALEMODE_NONE:
1177 case SDL_TEXTURESCALEMODE_FAST: 1274 case SDL_TEXTURESCALEMODE_FAST:
1178 IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MINFILTER, 1275 IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MINFILTER,
1179 D3DTEXF_POINT); 1276 D3DTEXF_POINT);
1199 texturedata->texture); 1296 texturedata->texture);
1200 if (FAILED(result)) { 1297 if (FAILED(result)) {
1201 D3D_SetError("SetTexture()", result); 1298 D3D_SetError("SetTexture()", result);
1202 return -1; 1299 return -1;
1203 } 1300 }
1301 if (shader) {
1302 result = IDirect3DDevice9_SetPixelShader(data->device, shader);
1303 if (FAILED(result)) {
1304 D3D_SetError("SetShader()", result);
1305 return -1;
1306 }
1307 }
1204 result = 1308 result =
1205 IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_TRIANGLEFAN, 2, 1309 IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_TRIANGLEFAN, 2,
1206 vertices, sizeof(*vertices)); 1310 vertices, sizeof(*vertices));
1207 if (FAILED(result)) { 1311 if (FAILED(result)) {
1208 D3D_SetError("DrawPrimitiveUP()", result); 1312 D3D_SetError("DrawPrimitiveUP()", result);
1209 return -1; 1313 return -1;
1314 }
1315 if (shader) {
1316 result = IDirect3DDevice9_SetPixelShader(data->device, NULL);
1317 if (FAILED(result)) {
1318 D3D_SetError("SetShader()", result);
1319 return -1;
1320 }
1210 } 1321 }
1211 return 0; 1322 return 0;
1212 } 1323 }
1213 1324
1214 static int 1325 static int