2 * Copyright (C) 2008 Tony Wasserka
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.
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.
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
20 #include "wine/debug.h"
21 #include "d3dx9_36_private.h"
23 WINE_DEFAULT_DEBUG_CHANNEL(d3dx);
25 /* the combination of all possible D3DXSPRITE flags */
26 #define D3DXSPRITE_FLAGLIMIT 511
28 typedef struct _SPRITEVERTEX {
34 static HRESULT WINAPI ID3DXSpriteImpl_QueryInterface(LPD3DXSPRITE iface, REFIID riid, LPVOID *object)
36 ID3DXSpriteImpl *This=(ID3DXSpriteImpl*)iface;
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);
44 WARN("(%p)->(%s, %p): not found\n", iface, debugstr_guid(riid), *object);
48 static ULONG WINAPI ID3DXSpriteImpl_AddRef(LPD3DXSPRITE iface)
50 ID3DXSpriteImpl *This=(ID3DXSpriteImpl*)iface;
51 ULONG ref=InterlockedIncrement(&This->ref);
52 TRACE("(%p): AddRef from %d\n", This, ref-1);
56 static ULONG WINAPI ID3DXSpriteImpl_Release(LPD3DXSPRITE iface)
58 ID3DXSpriteImpl *This=(ID3DXSpriteImpl*)iface;
59 ULONG ref=InterlockedDecrement(&This->ref);
60 TRACE("(%p): ReleaseRef to %d\n", This, ref);
65 for(i=0;i<This->sprite_count;i++)
66 if(This->sprites[i].texture)
67 IDirect3DTexture9_Release(This->sprites[i].texture);
69 HeapFree(GetProcessHeap(), 0, This->sprites);
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);
79 static HRESULT WINAPI ID3DXSpriteImpl_GetDevice(LPD3DXSPRITE iface, LPDIRECT3DDEVICE9 *device)
81 ID3DXSpriteImpl *This=(ID3DXSpriteImpl*)iface;
82 FIXME("(%p): stub\n", This);
86 static HRESULT WINAPI ID3DXSpriteImpl_GetTransform(LPD3DXSPRITE iface, D3DXMATRIX *transform)
88 ID3DXSpriteImpl *This=(ID3DXSpriteImpl*)iface;
89 FIXME("(%p): stub\n", This);
93 static HRESULT WINAPI ID3DXSpriteImpl_SetTransform(LPD3DXSPRITE iface, CONST D3DXMATRIX *transform)
95 ID3DXSpriteImpl *This=(ID3DXSpriteImpl*)iface;
96 FIXME("(%p): stub\n", This);
100 static HRESULT WINAPI ID3DXSpriteImpl_SetWorldViewRH(LPD3DXSPRITE iface, CONST D3DXMATRIX *world, CONST D3DXMATRIX *view)
102 ID3DXSpriteImpl *This=(ID3DXSpriteImpl*)iface;
103 FIXME("(%p): stub\n", This);
107 static HRESULT WINAPI ID3DXSpriteImpl_SetWorldViewLH(LPD3DXSPRITE iface, CONST D3DXMATRIX *world, CONST D3DXMATRIX *view)
109 ID3DXSpriteImpl *This=(ID3DXSpriteImpl*)iface;
110 FIXME("(%p): stub\n", This);
114 /* Helper function */
115 void set_states(ID3DXSpriteImpl *object)
120 /* Miscelaneous stuff */
121 IDirect3DDevice9_SetVertexShader(object->device, NULL);
122 IDirect3DDevice9_SetPixelShader(object->device, NULL);
123 IDirect3DDevice9_SetNPatchMode(object->device, 0.0f);
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);
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);
166 IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP);
167 IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP);
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);
173 IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MAXMIPLEVEL, 0);
174 IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MAXANISOTROPY, object->maxanisotropy);
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);
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);
184 IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_MIPMAPLODBIAS, 0);
185 IDirect3DDevice9_SetSamplerState(object->device, 0, D3DSAMP_SRGBTEXTURE, 0);
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);
196 static HRESULT WINAPI ID3DXSpriteImpl_Begin(LPD3DXSPRITE iface, DWORD flags)
198 ID3DXSpriteImpl *This=(ID3DXSpriteImpl*)iface;
200 TRACE("(%p): relay\n", This);
202 if(flags>D3DXSPRITE_FLAGLIMIT || This->ready) return D3DERR_INVALIDCALL;
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
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;
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);
227 IDirect3DDevice9_EndStateBlock(This->device, &This->stateblock);
229 /* Save current state */
230 IDirect3DStateBlock9_Capture(This->stateblock);
232 /* Apply device state */
235 D3DXMatrixIdentity(&This->transform);
236 D3DXMatrixIdentity(&This->view);
244 static HRESULT WINAPI ID3DXSpriteImpl_Draw(LPD3DXSPRITE iface, LPDIRECT3DTEXTURE9 texture, CONST RECT *rect, CONST D3DXVECTOR3 *center,
245 CONST D3DXVECTOR3 *position, D3DCOLOR color)
247 ID3DXSpriteImpl *This=(ID3DXSpriteImpl*)iface;
248 D3DSURFACE_DESC texdesc;
249 TRACE("(%p): relay\n", This);
251 if(texture==NULL) return D3DERR_INVALIDCALL;
252 if(!This->ready) return D3DERR_INVALIDCALL;
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));
261 This->sprites[This->sprite_count].texture=texture;
262 IUnknown_AddRef(texture);
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);
269 texdesc.Width=This->sprites[This->sprite_count-1].texw;
270 texdesc.Height=This->sprites[This->sprite_count-1].texh;
272 } else IDirect3DTexture9_GetLevelDesc(texture, 0, &texdesc);
274 This->sprites[This->sprite_count].texw=texdesc.Width;
275 This->sprites[This->sprite_count].texh=texdesc.Height;
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;
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;
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;
296 This->sprites[This->sprite_count].color=color;
297 This->sprite_count++;
302 static HRESULT WINAPI ID3DXSpriteImpl_Flush(LPD3DXSPRITE iface)
304 ID3DXSpriteImpl *This=(ID3DXSpriteImpl*)iface;
306 FIXME("(%p): stub\n", This);
308 if(!This->ready) return D3DERR_INVALIDCALL;
310 for(i=0;i<This->sprite_count;i++)
311 if(This->sprites[i].texture)
312 IDirect3DTexture9_Release(This->sprites[i].texture);
314 This->sprite_count=0;
316 /* Flush may be called more than once, so we don't reset This->ready here */
321 static HRESULT WINAPI ID3DXSpriteImpl_End(LPD3DXSPRITE iface)
323 ID3DXSpriteImpl *This=(ID3DXSpriteImpl*)iface;
324 TRACE("(%p): relay\n", This);
326 if(!This->ready) return D3DERR_INVALIDCALL;
328 ID3DXSprite_Flush(iface);
329 if(This->stateblock) IDirect3DStateBlock9_Apply(This->stateblock); /* Restore old state */
336 static HRESULT WINAPI ID3DXSpriteImpl_OnLostDevice(LPD3DXSPRITE iface)
338 ID3DXSpriteImpl *This=(ID3DXSpriteImpl*)iface;
339 FIXME("(%p): stub\n", This);
343 static HRESULT WINAPI ID3DXSpriteImpl_OnResetDevice(LPD3DXSPRITE iface)
345 ID3DXSpriteImpl *This=(ID3DXSpriteImpl*)iface;
346 FIXME("(%p): stub\n", This);
350 static const ID3DXSpriteVtbl D3DXSprite_Vtbl =
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,
366 ID3DXSpriteImpl_OnLostDevice,
367 ID3DXSpriteImpl_OnResetDevice
370 HRESULT WINAPI D3DXCreateSprite(LPDIRECT3DDEVICE9 device, LPD3DXSPRITE *sprite)
372 ID3DXSpriteImpl *object;
374 static const D3DVERTEXELEMENT9 elements[] =
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 },
382 TRACE("(void): relay\n");
384 if(device==NULL || sprite==NULL) return D3DERR_INVALIDCALL;
386 object=HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ID3DXSpriteImpl));
389 return E_OUTOFMEMORY;
391 object->lpVtbl=&D3DXSprite_Vtbl;
393 object->device=device;
394 IUnknown_AddRef(device);
396 IDirect3DDevice9_CreateVertexDeclaration(object->device, elements, &object->vdecl);
397 object->stateblock=NULL;
399 D3DXMatrixIdentity(&object->transform);
400 D3DXMatrixIdentity(&object->view);
405 IDirect3DDevice9_GetDeviceCaps(device, &caps);
406 object->texfilter_caps=caps.TextureFilterCaps;
407 object->maxanisotropy=caps.MaxAnisotropy;
408 object->alphacmp_caps=caps.AlphaCmpCaps;
410 object->sprites=NULL;
411 object->sprite_count=0;
412 object->allocated_sprites=0;
414 *sprite=(ID3DXSprite*)object;