d3dx9: Implement ID3DXSprite_Begin/End.
[wine] / dlls / d3dx9_36 / sprite.c
1 /*
2  * Copyright (C) 2008 Tony Wasserka
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  *
18  */
19
20 #include "wine/debug.h"
21 #include "d3dx9_36_private.h"
22
23 WINE_DEFAULT_DEBUG_CHANNEL(d3dx);
24
25 /* the combination of all possible D3DXSPRITE flags */
26 #define D3DXSPRITE_FLAGLIMIT 511
27
28 typedef struct _SPRITEVERTEX {
29     D3DXVECTOR3 pos;
30     DWORD col;
31     D3DXVECTOR2 tex;
32 } SPRITEVERTEX;
33
34 static HRESULT WINAPI ID3DXSpriteImpl_QueryInterface(LPD3DXSPRITE iface, REFIID riid, LPVOID *object)
35 {
36     ID3DXSpriteImpl *This=(ID3DXSpriteImpl*)iface;
37
38     TRACE("(%p): QueryInterface from %s\n", This, debugstr_guid(riid));
39     if(IsEqualGUID(riid, &IID_IUnknown) || IsEqualGUID(riid, &IID_ID3DXSprite)) {
40         IUnknown_AddRef(iface);
41         *object=This;
42         return S_OK;
43     }
44     WARN("(%p)->(%s, %p): not found\n", iface, debugstr_guid(riid), *object);
45     return E_NOINTERFACE;
46 }
47
48 static ULONG WINAPI ID3DXSpriteImpl_AddRef(LPD3DXSPRITE iface)
49 {
50     ID3DXSpriteImpl *This=(ID3DXSpriteImpl*)iface;
51     ULONG ref=InterlockedIncrement(&This->ref);
52     TRACE("(%p): AddRef from %d\n", This, ref-1);
53     return ref;
54 }
55
56 static ULONG WINAPI ID3DXSpriteImpl_Release(LPD3DXSPRITE iface)
57 {
58     ID3DXSpriteImpl *This=(ID3DXSpriteImpl*)iface;
59     ULONG ref=InterlockedDecrement(&This->ref);
60     TRACE("(%p): ReleaseRef to %d\n", This, ref);
61
62     if(ref==0) {
63         if(This->sprites) {
64             int i;
65             for(i=0;i<This->sprite_count;i++)
66                 if(This->sprites[i].texture)
67                     IDirect3DTexture9_Release(This->sprites[i].texture);
68
69             HeapFree(GetProcessHeap(), 0, This->sprites);
70         }
71         if(This->stateblock) IDirect3DStateBlock9_Release(This->stateblock);
72         if(This->vdecl) IDirect3DVertexDeclaration9_Release(This->vdecl);
73         if(This->device) IDirect3DDevice9_Release(This->device);
74         HeapFree(GetProcessHeap(), 0, This);
75     }
76     return ref;
77 }
78
79 static HRESULT WINAPI ID3DXSpriteImpl_GetDevice(LPD3DXSPRITE iface, LPDIRECT3DDEVICE9 *device)
80 {
81     ID3DXSpriteImpl *This=(ID3DXSpriteImpl*)iface;
82     FIXME("(%p): stub\n", This);
83     return E_NOTIMPL;
84 }
85
86 static HRESULT WINAPI ID3DXSpriteImpl_GetTransform(LPD3DXSPRITE iface, D3DXMATRIX *transform)
87 {
88     ID3DXSpriteImpl *This=(ID3DXSpriteImpl*)iface;
89     FIXME("(%p): stub\n", This);
90     return E_NOTIMPL;
91 }
92
93 static HRESULT WINAPI ID3DXSpriteImpl_SetTransform(LPD3DXSPRITE iface, CONST D3DXMATRIX *transform)
94 {
95     ID3DXSpriteImpl *This=(ID3DXSpriteImpl*)iface;
96     FIXME("(%p): stub\n", This);
97     return E_NOTIMPL;
98 }
99
100 static HRESULT WINAPI ID3DXSpriteImpl_SetWorldViewRH(LPD3DXSPRITE iface, CONST D3DXMATRIX *world, CONST D3DXMATRIX *view)
101 {
102     ID3DXSpriteImpl *This=(ID3DXSpriteImpl*)iface;
103     FIXME("(%p): stub\n", This);
104     return E_NOTIMPL;
105 }
106
107 static HRESULT WINAPI ID3DXSpriteImpl_SetWorldViewLH(LPD3DXSPRITE iface, CONST D3DXMATRIX *world, CONST D3DXMATRIX *view)
108 {
109     ID3DXSpriteImpl *This=(ID3DXSpriteImpl*)iface;
110     FIXME("(%p): stub\n", This);
111     return E_NOTIMPL;
112 }
113
114 /* Helper function */
115 void set_states(ID3DXSpriteImpl *object)
116 {
117     D3DXMATRIX mat;
118     D3DVIEWPORT9 vp;
119
120     /* Miscelaneous stuff */
121     IDirect3DDevice9_SetVertexShader(object->device, NULL);
122     IDirect3DDevice9_SetPixelShader(object->device, NULL);
123     IDirect3DDevice9_SetNPatchMode(object->device, 0.0f);
124
125     /* Render states */
126     IDirect3DDevice9_SetRenderState(object->device, D3DRS_ALPHABLENDENABLE, TRUE);
127     IDirect3DDevice9_SetRenderState(object->device, D3DRS_ALPHAFUNC, D3DCMP_GREATER);
128     IDirect3DDevice9_SetRenderState(object->device, D3DRS_ALPHAREF, 0x00);
129     IDirect3DDevice9_SetRenderState(object->device, D3DRS_ALPHATESTENABLE, object->alphacmp_caps);
130     IDirect3DDevice9_SetRenderState(object->device, D3DRS_BLENDOP, D3DBLENDOP_ADD);
131     IDirect3DDevice9_SetRenderState(object->device, D3DRS_CLIPPING, TRUE);
132     IDirect3DDevice9_SetRenderState(object->device, D3DRS_CLIPPLANEENABLE, FALSE);
133     IDirect3DDevice9_SetRenderState(object->device, D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_ALPHA | D3DCOLORWRITEENABLE_BLUE |
134                                     D3DCOLORWRITEENABLE_GREEN | D3DCOLORWRITEENABLE_RED);
135     IDirect3DDevice9_SetRenderState(object->device, D3DRS_CULLMODE, D3DCULL_NONE);
136     IDirect3DDevice9_SetRenderState(object->device, D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
137     IDirect3DDevice9_SetRenderState(object->device, D3DRS_DIFFUSEMATERIALSOURCE, D3DMCS_COLOR1);
138     IDirect3DDevice9_SetRenderState(object->device, D3DRS_ENABLEADAPTIVETESSELLATION, FALSE);
139     IDirect3DDevice9_SetRenderState(object->device, D3DRS_FILLMODE, D3DFILL_SOLID);
140     IDirect3DDevice9_SetRenderState(object->device, D3DRS_FOGENABLE, FALSE);
141     IDirect3DDevice9_SetRenderState(object->device, D3DRS_INDEXEDVERTEXBLENDENABLE, FALSE);
142     IDirect3DDevice9_SetRenderState(object->device, D3DRS_LIGHTING, FALSE);
143     IDirect3DDevice9_SetRenderState(object->device, D3DRS_RANGEFOGENABLE, FALSE);
144     IDirect3DDevice9_SetRenderState(object->device, D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
145     IDirect3DDevice9_SetRenderState(object->device, D3DRS_SHADEMODE, D3DSHADE_GOURAUD);
146     IDirect3DDevice9_SetRenderState(object->device, D3DRS_SPECULARENABLE, FALSE);
147     IDirect3DDevice9_SetRenderState(object->device, D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
148     IDirect3DDevice9_SetRenderState(object->device, D3DRS_SRGBWRITEENABLE, FALSE);
149     IDirect3DDevice9_SetRenderState(object->device, D3DRS_STENCILENABLE, FALSE);
150     IDirect3DDevice9_SetRenderState(object->device, D3DRS_VERTEXBLEND, FALSE);
151     IDirect3DDevice9_SetRenderState(object->device, D3DRS_WRAP0, 0);
152
153     /* Texture stage states */
154     IDirect3DDevice9_SetTextureStageState(object->device, 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
155     IDirect3DDevice9_SetTextureStageState(object->device, 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);
156     IDirect3DDevice9_SetTextureStageState(object->device, 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
157     IDirect3DDevice9_SetTextureStageState(object->device, 0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
158     IDirect3DDevice9_SetTextureStageState(object->device, 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
159     IDirect3DDevice9_SetTextureStageState(object->device, 0, D3DTSS_COLOROP, D3DTOP_MODULATE);
160     IDirect3DDevice9_SetTextureStageState(object->device, 0, D3DTSS_TEXCOORDINDEX, 0);
161     IDirect3DDevice9_SetTextureStageState(object->device, 0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE);
162     IDirect3DDevice9_SetTextureStageState(object->device, 1, D3DTSS_ALPHAOP, D3DTOP_DISABLE);
163     IDirect3DDevice9_SetTextureStageState(object->device, 1, D3DTSS_COLOROP, D3DTOP_DISABLE);
164
165     /* Sampler states */
166     IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP);
167     IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP);
168
169     if(object->texfilter_caps & D3DPTFILTERCAPS_MAGFANISOTROPIC)
170         IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MAGFILTER, D3DTEXF_ANISOTROPIC);
171     else IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
172
173     IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MAXMIPLEVEL, 0);
174     IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MAXANISOTROPY, object->maxanisotropy);
175
176     if(object->texfilter_caps & D3DPTFILTERCAPS_MINFANISOTROPIC)
177         IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MINFILTER, D3DTEXF_ANISOTROPIC);
178     else IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
179
180     if(object->texfilter_caps & D3DPTFILTERCAPS_MIPFLINEAR)
181         IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
182     else IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MIPFILTER, D3DTEXF_POINT);
183
184     IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MIPMAPLODBIAS, 0);
185     IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_SRGBTEXTURE, 0);
186
187     /* Matrices */
188     D3DXMatrixIdentity(&mat);
189     IDirect3DDevice9_SetTransform(object->device, D3DTS_WORLD, &mat);
190     IDirect3DDevice9_SetTransform(object->device, D3DTS_VIEW, &mat);
191     IDirect3DDevice9_GetViewport(object->device, &vp);
192     D3DXMatrixOrthoOffCenterLH(&mat, vp.X+0.5f, (float)vp.Width+vp.X+0.5f, (float)vp.Height+vp.Y+0.5f, vp.Y+0.5f, vp.MinZ, vp.MaxZ);
193     IDirect3DDevice9_SetTransform(object->device, D3DTS_PROJECTION, &mat);
194 }
195
196 static HRESULT WINAPI ID3DXSpriteImpl_Begin(LPD3DXSPRITE iface, DWORD flags)
197 {
198     ID3DXSpriteImpl *This=(ID3DXSpriteImpl*)iface;
199     HRESULT hr;
200     TRACE("(%p): relay\n", This);
201
202     if(flags>D3DXSPRITE_FLAGLIMIT || This->ready) return D3DERR_INVALIDCALL;
203
204 /* TODO: Implement flags:
205 D3DXSPRITE_ALPHABLEND: enables alpha blending
206 D3DXSPRITE_BILLBOARD: makes the sprite always face the camera
207 D3DXSPRITE_DONOTMODIFY_RENDERSTATE: name says it all
208 D3DXSPRITE_OBJECTSPACE: do not change device transforms
209 D3DXSPRITE_SORT_DEPTH_BACKTOFRONT: sort by position
210 D3DXSPRITE_SORT_DEPTH_FRONTTOBACK: sort by position
211 D3DXSPRITE_SORT_TEXTURE: sort by texture (so that it doesn't change too often)
212 D3DXSPRITE_DO_NOT_ADDREF_TEXTURE: don't call AddRef/Release on every Draw/Flush call
213 D3DXSPRITE_DONOTSAVESTATE: don't restore the current device state on ID3DXSprite_End
214 */
215     if(This->stateblock==NULL) {
216         /* Tell our state block what it must store */
217         hr=IDirect3DDevice9_BeginStateBlock(This->device);
218         if(hr!=D3D_OK) return hr;
219
220         set_states(This);
221
222         IDirect3DDevice9_SetVertexDeclaration(This->device, This->vdecl);
223         IDirect3DDevice9_SetStreamSource(This->device, 0, NULL, 0, sizeof(SPRITEVERTEX));
224         IDirect3DDevice9_SetIndices(This->device, NULL);
225         IDirect3DDevice9_SetTexture(This->device, 0, NULL);
226
227         IDirect3DDevice9_EndStateBlock(This->device, &This->stateblock);
228     }
229     /* Save current state */
230     IDirect3DStateBlock9_Capture(This->stateblock);
231
232     /* Apply device state */
233     set_states(This);
234
235     D3DXMatrixIdentity(&This->transform);
236     D3DXMatrixIdentity(&This->view);
237
238     This->flags=flags;
239     This->ready=TRUE;
240
241     return D3D_OK;
242 }
243
244 static HRESULT WINAPI ID3DXSpriteImpl_Draw(LPD3DXSPRITE iface, LPDIRECT3DTEXTURE9 texture, CONST RECT *rect, CONST D3DXVECTOR3 *center,
245                                            CONST D3DXVECTOR3 *position, D3DCOLOR color)
246 {
247     ID3DXSpriteImpl *This=(ID3DXSpriteImpl*)iface;
248     D3DSURFACE_DESC texdesc;
249     TRACE("(%p): relay\n", This);
250
251     if(texture==NULL) return D3DERR_INVALIDCALL;
252     if(!This->ready) return D3DERR_INVALIDCALL;
253
254     if(This->allocated_sprites==0) {
255         This->sprites=HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 32*sizeof(SPRITE));
256         This->allocated_sprites=32;
257     } else if(This->allocated_sprites<=This->sprite_count) {
258         This->allocated_sprites=This->allocated_sprites*3/2;
259         This->sprites=HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->sprites, This->allocated_sprites*sizeof(SPRITE));
260     }
261     This->sprites[This->sprite_count].texture=texture;
262     IUnknown_AddRef(texture);
263
264     /* Reuse the texture desc if possible */
265     if(This->sprite_count) {
266         if(This->sprites[This->sprite_count-1].texture!=texture) {
267             IDirect3DTexture9_GetLevelDesc(texture, 0, &texdesc);
268         } else {
269             texdesc.Width=This->sprites[This->sprite_count-1].texw;
270             texdesc.Height=This->sprites[This->sprite_count-1].texh;
271         }
272     } else IDirect3DTexture9_GetLevelDesc(texture, 0, &texdesc);
273
274     This->sprites[This->sprite_count].texw=texdesc.Width;
275     This->sprites[This->sprite_count].texh=texdesc.Height;
276
277     if(rect==NULL) {
278         This->sprites[This->sprite_count].rect.left=0;
279         This->sprites[This->sprite_count].rect.top=0;
280         This->sprites[This->sprite_count].rect.right=texdesc.Width;
281         This->sprites[This->sprite_count].rect.bottom=texdesc.Height;
282     } else This->sprites[This->sprite_count].rect=*rect;
283
284     if(center==NULL) {
285         This->sprites[This->sprite_count].center.x=0.0f;
286         This->sprites[This->sprite_count].center.y=0.0f;
287         This->sprites[This->sprite_count].center.z=0.0f;
288     } else This->sprites[This->sprite_count].center=*center;
289
290     if(position==NULL) {
291         This->sprites[This->sprite_count].pos.x=0.0f;
292         This->sprites[This->sprite_count].pos.y=0.0f;
293         This->sprites[This->sprite_count].pos.z=0.0f;
294     } else This->sprites[This->sprite_count].pos=*position;
295
296     This->sprites[This->sprite_count].color=color;
297     This->sprite_count++;
298
299     return D3D_OK;
300 }
301
302 static HRESULT WINAPI ID3DXSpriteImpl_Flush(LPD3DXSPRITE iface)
303 {
304     ID3DXSpriteImpl *This=(ID3DXSpriteImpl*)iface;
305     int i;
306     FIXME("(%p): stub\n", This);
307
308     if(!This->ready) return D3DERR_INVALIDCALL;
309
310     for(i=0;i<This->sprite_count;i++)
311         if(This->sprites[i].texture)
312             IDirect3DTexture9_Release(This->sprites[i].texture);
313
314     This->sprite_count=0;
315
316     /* Flush may be called more than once, so we don't reset This->ready here */
317
318     return E_NOTIMPL;
319 }
320
321 static HRESULT WINAPI ID3DXSpriteImpl_End(LPD3DXSPRITE iface)
322 {
323     ID3DXSpriteImpl *This=(ID3DXSpriteImpl*)iface;
324     TRACE("(%p): relay\n", This);
325
326     if(!This->ready) return D3DERR_INVALIDCALL;
327
328     ID3DXSprite_Flush(iface);
329     if(This->stateblock) IDirect3DStateBlock9_Apply(This->stateblock); /* Restore old state */
330
331     This->ready=FALSE;
332
333     return D3D_OK;
334 }
335
336 static HRESULT WINAPI ID3DXSpriteImpl_OnLostDevice(LPD3DXSPRITE iface)
337 {
338     ID3DXSpriteImpl *This=(ID3DXSpriteImpl*)iface;
339     FIXME("(%p): stub\n", This);
340     return E_NOTIMPL;
341 }
342
343 static HRESULT WINAPI ID3DXSpriteImpl_OnResetDevice(LPD3DXSPRITE iface)
344 {
345     ID3DXSpriteImpl *This=(ID3DXSpriteImpl*)iface;
346     FIXME("(%p): stub\n", This);
347     return E_NOTIMPL;
348 }
349
350 static const ID3DXSpriteVtbl D3DXSprite_Vtbl =
351 {
352     /*** IUnknown methods ***/
353     ID3DXSpriteImpl_QueryInterface,
354     ID3DXSpriteImpl_AddRef,
355     ID3DXSpriteImpl_Release,
356     /*** ID3DXSprite methods ***/
357     ID3DXSpriteImpl_GetDevice,
358     ID3DXSpriteImpl_GetTransform,
359     ID3DXSpriteImpl_SetTransform,
360     ID3DXSpriteImpl_SetWorldViewRH,
361     ID3DXSpriteImpl_SetWorldViewLH,
362     ID3DXSpriteImpl_Begin,
363     ID3DXSpriteImpl_Draw,
364     ID3DXSpriteImpl_Flush,
365     ID3DXSpriteImpl_End,
366     ID3DXSpriteImpl_OnLostDevice,
367     ID3DXSpriteImpl_OnResetDevice
368 };
369
370 HRESULT WINAPI D3DXCreateSprite(LPDIRECT3DDEVICE9 device, LPD3DXSPRITE *sprite)
371 {
372     ID3DXSpriteImpl *object;
373     D3DCAPS9 caps;
374     static const D3DVERTEXELEMENT9 elements[] =
375         {
376             { 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
377             { 0, 12, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0 },
378             { 0, 16, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
379             D3DDECL_END()
380         };
381
382     TRACE("(void): relay\n");
383
384     if(device==NULL || sprite==NULL) return D3DERR_INVALIDCALL;
385
386     object=HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ID3DXSpriteImpl));
387     if(object==NULL) {
388         *sprite=NULL;
389         return E_OUTOFMEMORY;
390     }
391     object->lpVtbl=&D3DXSprite_Vtbl;
392     object->ref=1;
393     object->device=device;
394     IUnknown_AddRef(device);
395
396     IDirect3DDevice9_CreateVertexDeclaration(object->device, elements, &object->vdecl);
397     object->stateblock=NULL;
398
399     D3DXMatrixIdentity(&object->transform);
400     D3DXMatrixIdentity(&object->view);
401
402     object->flags=0;
403     object->ready=FALSE;
404
405     IDirect3DDevice9_GetDeviceCaps(device, &caps);
406     object->texfilter_caps=caps.TextureFilterCaps;
407     object->maxanisotropy=caps.MaxAnisotropy;
408     object->alphacmp_caps=caps.AlphaCmpCaps;
409
410     object->sprites=NULL;
411     object->sprite_count=0;
412     object->allocated_sprites=0;
413
414     *sprite=(ID3DXSprite*)object;
415
416     return D3D_OK;
417 }