wined3d: Properly release the converted vertex declaration.
[wine] / dlls / d3d9 / vertexdeclaration.c
1 /*
2  * IDirect3DVertexDeclaration9 implementation
3  *
4  * Copyright 2002-2003 Raphael Junqueira
5  *                     Jason Edmeades
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include "config.h"
23 #include "d3d9_private.h"
24
25 WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
26
27 typedef struct _D3DDECLTYPE_INFO {
28     D3DDECLTYPE d3dType;
29     int         size;
30     int         typesize;
31 } D3DDECLTYPE_INFO;
32
33 static D3DDECLTYPE_INFO const d3d_dtype_lookup[D3DDECLTYPE_UNUSED] = {
34    {D3DDECLTYPE_FLOAT1,    1, sizeof(float)},
35    {D3DDECLTYPE_FLOAT2,    2, sizeof(float)},
36    {D3DDECLTYPE_FLOAT3,    3, sizeof(float)},
37    {D3DDECLTYPE_FLOAT4,    4, sizeof(float)},
38    {D3DDECLTYPE_D3DCOLOR,  4, sizeof(BYTE)},
39    {D3DDECLTYPE_UBYTE4,    4, sizeof(BYTE)},
40    {D3DDECLTYPE_SHORT2,    2, sizeof(short int)},
41    {D3DDECLTYPE_SHORT4,    4, sizeof(short int)},
42    {D3DDECLTYPE_UBYTE4N,   4, sizeof(BYTE)},
43    {D3DDECLTYPE_SHORT2N,   2, sizeof(short int)},
44    {D3DDECLTYPE_SHORT4N,   4, sizeof(short int)},
45    {D3DDECLTYPE_USHORT2N,  2, sizeof(short int)},
46    {D3DDECLTYPE_USHORT4N,  4, sizeof(short int)},
47    {D3DDECLTYPE_UDEC3,     3, sizeof(short int)},
48    {D3DDECLTYPE_DEC3N,     3, sizeof(short int)},
49    {D3DDECLTYPE_FLOAT16_2, 2, sizeof(short int)},
50    {D3DDECLTYPE_FLOAT16_4, 4, sizeof(short int)}};
51
52 #define D3D_DECL_SIZE(type)          d3d_dtype_lookup[type].size
53 #define D3D_DECL_TYPESIZE(type)      d3d_dtype_lookup[type].typesize
54
55 HRESULT vdecl_convert_fvf(
56     DWORD fvf,
57     D3DVERTEXELEMENT9** ppVertexElements) {
58
59     unsigned int idx, idx2;
60     unsigned int offset;
61     BOOL has_pos = (fvf & D3DFVF_POSITION_MASK) != 0;
62     BOOL has_blend = (fvf & D3DFVF_XYZB5) > D3DFVF_XYZRHW;
63     BOOL has_blend_idx = has_blend &&
64        (((fvf & D3DFVF_XYZB5) == D3DFVF_XYZB5) ||
65         (fvf & D3DFVF_LASTBETA_D3DCOLOR) ||
66         (fvf & D3DFVF_LASTBETA_UBYTE4));
67     BOOL has_normal = (fvf & D3DFVF_NORMAL) != 0;
68     BOOL has_psize = (fvf & D3DFVF_PSIZE) != 0;
69     BOOL has_diffuse = (fvf & D3DFVF_DIFFUSE) != 0;
70     BOOL has_specular = (fvf & D3DFVF_SPECULAR) !=0;
71
72     DWORD num_textures = (fvf & D3DFVF_TEXCOUNT_MASK) >> D3DFVF_TEXCOUNT_SHIFT;
73     DWORD texcoords = (fvf & 0x00FF0000) >> 16;
74
75     D3DVERTEXELEMENT9 end_element = D3DDECL_END();
76     D3DVERTEXELEMENT9 *elements = NULL;
77
78     unsigned int size;
79     DWORD num_blends = 1 + (((fvf & D3DFVF_XYZB5) - D3DFVF_XYZB1) >> 1);
80     if (has_blend_idx) num_blends--;
81
82     /* Compute declaration size */
83     size = has_pos + (has_blend && num_blends > 0) + has_blend_idx + has_normal +
84            has_psize + has_diffuse + has_specular + num_textures + 1;
85
86     /* convert the declaration */
87     elements = HeapAlloc(GetProcessHeap(), 0, size * sizeof(D3DVERTEXELEMENT9));
88     if (!elements) 
89         return D3DERR_OUTOFVIDEOMEMORY;
90
91     memcpy(&elements[size-1], &end_element, sizeof(D3DVERTEXELEMENT9));
92     idx = 0;
93     if (has_pos) {
94         if (!has_blend && (fvf & D3DFVF_XYZRHW)) {
95             elements[idx].Type = D3DDECLTYPE_FLOAT4;
96             elements[idx].Usage = D3DDECLUSAGE_POSITIONT;
97         }
98         else {
99             elements[idx].Type = D3DDECLTYPE_FLOAT3;
100             elements[idx].Usage = D3DDECLUSAGE_POSITION;
101         }
102         elements[idx].UsageIndex = 0;
103         idx++;
104     }
105     if (has_blend && (num_blends > 0)) {
106         if (((fvf & D3DFVF_XYZB5) == D3DFVF_XYZB2) && (fvf & D3DFVF_LASTBETA_D3DCOLOR))
107             elements[idx].Type = D3DDECLTYPE_D3DCOLOR;
108         else
109             elements[idx].Type = D3DDECLTYPE_FLOAT1 + num_blends - 1;
110         elements[idx].Usage = D3DDECLUSAGE_BLENDWEIGHT;
111         elements[idx].UsageIndex = 0;
112         idx++;
113     }
114     if (has_blend_idx) {
115         if (fvf & D3DFVF_LASTBETA_UBYTE4 ||
116             (((fvf & D3DFVF_XYZB5) == D3DFVF_XYZB2) && (fvf & D3DFVF_LASTBETA_D3DCOLOR)))
117             elements[idx].Type = D3DDECLTYPE_UBYTE4;
118         else if (fvf & D3DFVF_LASTBETA_D3DCOLOR)
119             elements[idx].Type = D3DDECLTYPE_D3DCOLOR;
120         else
121             elements[idx].Type = D3DDECLTYPE_FLOAT1;
122         elements[idx].Usage = D3DDECLUSAGE_BLENDINDICES;
123         elements[idx].UsageIndex = 0;
124         idx++;
125     }
126     if (has_normal) {
127         elements[idx].Type = D3DDECLTYPE_FLOAT3;
128         elements[idx].Usage = D3DDECLUSAGE_NORMAL;
129         elements[idx].UsageIndex = 0;
130         idx++;
131     }
132     if (has_psize) {
133         elements[idx].Type = D3DDECLTYPE_FLOAT1;
134         elements[idx].Usage = D3DDECLUSAGE_PSIZE;
135         elements[idx].UsageIndex = 0;
136         idx++;
137     }
138     if (has_diffuse) {
139         elements[idx].Type = D3DDECLTYPE_D3DCOLOR;
140         elements[idx].Usage = D3DDECLUSAGE_COLOR;
141         elements[idx].UsageIndex = 0;
142         idx++;
143     }
144     if (has_specular) {
145         elements[idx].Type = D3DDECLTYPE_D3DCOLOR;
146         elements[idx].Usage = D3DDECLUSAGE_COLOR;
147         elements[idx].UsageIndex = 1;
148         idx++;
149     }
150     for (idx2 = 0; idx2 < num_textures; idx2++) {
151         unsigned int numcoords = (texcoords >> (idx2*2)) & 0x03;
152         switch (numcoords) {
153             case D3DFVF_TEXTUREFORMAT1:
154                 elements[idx].Type = D3DDECLTYPE_FLOAT1;
155                 break;
156             case D3DFVF_TEXTUREFORMAT2:
157                 elements[idx].Type = D3DDECLTYPE_FLOAT2;
158                 break;
159             case D3DFVF_TEXTUREFORMAT3:
160                 elements[idx].Type = D3DDECLTYPE_FLOAT3;
161                 break;
162             case D3DFVF_TEXTUREFORMAT4:
163                 elements[idx].Type = D3DDECLTYPE_FLOAT4;
164                 break;
165         }
166         elements[idx].Usage = D3DDECLUSAGE_TEXCOORD;
167         elements[idx].UsageIndex = idx2;
168         idx++;
169     }
170
171     /* Now compute offsets, and initialize the rest of the fields */
172     for (idx = 0, offset = 0; idx < size-1; idx++) {
173         elements[idx].Stream = 0;
174         elements[idx].Method = D3DDECLMETHOD_DEFAULT;
175         elements[idx].Offset = offset;
176         offset += D3D_DECL_SIZE(elements[idx].Type) * D3D_DECL_TYPESIZE(elements[idx].Type);
177     }
178
179     *ppVertexElements = elements;
180     return D3D_OK;
181 }
182
183 /* IDirect3DVertexDeclaration9 IUnknown parts follow: */
184 static HRESULT WINAPI IDirect3DVertexDeclaration9Impl_QueryInterface(LPDIRECT3DVERTEXDECLARATION9 iface, REFIID riid, LPVOID* ppobj) {
185     IDirect3DVertexDeclaration9Impl *This = (IDirect3DVertexDeclaration9Impl *)iface;
186
187     if (IsEqualGUID(riid, &IID_IUnknown)
188         || IsEqualGUID(riid, &IID_IDirect3DVertexDeclaration9)) {
189         IUnknown_AddRef(iface);
190         *ppobj = This;
191         return S_OK;
192     }
193
194     WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
195     *ppobj = NULL;
196     return E_NOINTERFACE;
197 }
198
199 static ULONG WINAPI IDirect3DVertexDeclaration9Impl_AddRef(LPDIRECT3DVERTEXDECLARATION9 iface) {
200     IDirect3DVertexDeclaration9Impl *This = (IDirect3DVertexDeclaration9Impl *)iface;
201     ULONG ref = InterlockedIncrement(&This->ref);
202
203     TRACE("(%p) : AddRef from %d\n", This, ref - 1);
204
205     return ref;
206 }
207
208 static ULONG WINAPI IDirect3DVertexDeclaration9Impl_Release(LPDIRECT3DVERTEXDECLARATION9 iface) {
209     IDirect3DVertexDeclaration9Impl *This = (IDirect3DVertexDeclaration9Impl *)iface;
210     ULONG ref = InterlockedDecrement(&This->ref);
211
212     TRACE("(%p) : ReleaseRef to %d\n", This, ref);
213
214     if (ref == 0) {
215         IWineD3DVertexDeclaration_Release(This->wineD3DVertexDeclaration);
216         IUnknown_Release(This->parentDevice);
217         HeapFree(GetProcessHeap(), 0, This);
218     }
219     return ref;
220 }
221
222 /* IDirect3DVertexDeclaration9 Interface follow: */
223 static HRESULT WINAPI IDirect3DVertexDeclaration9Impl_GetDevice(LPDIRECT3DVERTEXDECLARATION9 iface, IDirect3DDevice9** ppDevice) {
224     IDirect3DVertexDeclaration9Impl *This = (IDirect3DVertexDeclaration9Impl *)iface;
225     IWineD3DDevice *myDevice = NULL;
226     HRESULT hr = D3D_OK;
227
228     TRACE("(%p) : Relay\n", iface);
229
230     hr = IWineD3DVertexDeclaration_GetDevice(This->wineD3DVertexDeclaration, &myDevice);
231     if (hr == D3D_OK && myDevice != NULL) {
232         hr = IWineD3DDevice_GetParent(myDevice, (IUnknown **)ppDevice);
233         IWineD3DDevice_Release(myDevice);
234     }
235     return hr;
236 }
237
238 static HRESULT WINAPI IDirect3DVertexDeclaration9Impl_GetDeclaration(LPDIRECT3DVERTEXDECLARATION9 iface, D3DVERTEXELEMENT9* pDecl, UINT* pNumElements) {
239     IDirect3DVertexDeclaration9Impl *This = (IDirect3DVertexDeclaration9Impl *)iface;
240
241     TRACE("(%p) : pDecl %p, pNumElements %p)\n", This, pDecl, pNumElements);
242
243     *pNumElements = This->element_count;
244
245     /* Passing a NULL pDecl is used to just retrieve the number of elements */
246     if (!pDecl) {
247         TRACE("NULL pDecl passed. Returning D3D_OK.\n");
248         return D3D_OK;
249     }
250
251     TRACE("Copying %p to %p\n", This->elements, pDecl);
252     CopyMemory(pDecl, This->elements, This->element_count * sizeof(D3DVERTEXELEMENT9));
253
254     return D3D_OK;
255 }
256
257 static const IDirect3DVertexDeclaration9Vtbl Direct3DVertexDeclaration9_Vtbl =
258 {
259     /* IUnknown */
260     IDirect3DVertexDeclaration9Impl_QueryInterface,
261     IDirect3DVertexDeclaration9Impl_AddRef,
262     IDirect3DVertexDeclaration9Impl_Release,
263     /* IDirect3DVertexDeclaration9 */
264     IDirect3DVertexDeclaration9Impl_GetDevice,
265     IDirect3DVertexDeclaration9Impl_GetDeclaration
266 };
267
268 static size_t convert_to_wined3d_declaration(const D3DVERTEXELEMENT9* d3d9_elements, WINED3DVERTEXELEMENT **wined3d_elements) {
269     const D3DVERTEXELEMENT9* element;
270     size_t element_count = 1;
271     size_t i;
272
273     TRACE("d3d9_elements %p, wined3d_elements %p\n", d3d9_elements, wined3d_elements);
274
275     element = d3d9_elements;
276     while (element++->Stream != 0xff && element_count++ < 128);
277
278     if (element_count == 128) {
279         return 0;
280     }
281
282     *wined3d_elements = HeapAlloc(GetProcessHeap(), 0, element_count * sizeof(WINED3DVERTEXELEMENT));
283     if (!*wined3d_elements) {
284         FIXME("Memory allocation failed\n");
285         return 0;
286     }
287
288     for (i = 0; i < element_count; ++i) {
289         CopyMemory(*wined3d_elements + i, d3d9_elements + i, sizeof(D3DVERTEXELEMENT9));
290         (*wined3d_elements)[i].Reg = -1;
291     }
292
293     return element_count;
294 }
295
296 /* IDirect3DDevice9 IDirect3DVertexDeclaration9 Methods follow: */
297 HRESULT  WINAPI  IDirect3DDevice9Impl_CreateVertexDeclaration(LPDIRECT3DDEVICE9 iface, CONST D3DVERTEXELEMENT9* pVertexElements, IDirect3DVertexDeclaration9** ppDecl) {
298     
299     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
300     IDirect3DVertexDeclaration9Impl *object = NULL;
301     WINED3DVERTEXELEMENT* wined3d_elements;
302     size_t element_count;
303     HRESULT hr = D3D_OK;
304
305     TRACE("(%p) : Relay\n", iface);
306     if (NULL == ppDecl) {
307         WARN("(%p) : Caller passed NULL As ppDecl, returning D3DERR_INVALIDCALL\n",This);
308         return D3DERR_INVALIDCALL;
309     }
310
311     element_count = convert_to_wined3d_declaration(pVertexElements, &wined3d_elements);
312     if (!element_count) {
313         FIXME("(%p) : Error parsing vertex declaration\n", This);
314         return D3DERR_INVALIDCALL;
315     }
316
317     /* Allocate the storage for the device */
318     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DVertexDeclaration9Impl));
319     if (NULL == object) {
320         HeapFree(GetProcessHeap(), 0, wined3d_elements);
321         FIXME("Allocation of memory failed, returning D3DERR_OUTOFVIDEOMEMORY\n");
322         return D3DERR_OUTOFVIDEOMEMORY;
323     }
324
325     object->lpVtbl = &Direct3DVertexDeclaration9_Vtbl;
326     object->ref = 1;
327
328     object->elements = HeapAlloc(GetProcessHeap(), 0, element_count * sizeof(D3DVERTEXELEMENT9));
329     if (!object->elements) {
330         HeapFree(GetProcessHeap(), 0, wined3d_elements);
331         HeapFree(GetProcessHeap(), 0, object);
332         ERR("Memory allocation failed\n");
333         return D3DERR_OUTOFVIDEOMEMORY;
334     }
335     CopyMemory(object->elements, pVertexElements, element_count * sizeof(D3DVERTEXELEMENT9));
336     object->element_count = element_count;
337
338     hr = IWineD3DDevice_CreateVertexDeclaration(This->WineD3DDevice, &object->wineD3DVertexDeclaration, (IUnknown *)object, wined3d_elements, element_count);
339
340     HeapFree(GetProcessHeap(), 0, wined3d_elements);
341
342     if (FAILED(hr)) {
343
344         /* free up object */
345         FIXME("(%p) call to IWineD3DDevice_CreateVertexDeclaration failed\n", This);
346         HeapFree(GetProcessHeap(), 0, object);
347     } else {
348         IUnknown_AddRef(iface);
349         object->parentDevice = iface;
350         *ppDecl = (LPDIRECT3DVERTEXDECLARATION9) object;
351          TRACE("(%p) : Created vertex declaration %p\n", This, object);
352     }
353     return hr;
354 }
355
356 HRESULT  WINAPI  IDirect3DDevice9Impl_SetVertexDeclaration(LPDIRECT3DDEVICE9 iface, IDirect3DVertexDeclaration9* pDecl) {
357     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
358     IDirect3DVertexDeclaration9Impl *pDeclImpl = (IDirect3DVertexDeclaration9Impl *)pDecl;
359     HRESULT hr = D3D_OK;
360
361     TRACE("(%p) : Relay\n", iface);
362
363     if (This->convertedDecl && This->convertedDecl != pDecl) {
364         IUnknown_Release(This->convertedDecl);
365         This->convertedDecl = NULL;
366     }
367
368     hr = IWineD3DDevice_SetVertexDeclaration(This->WineD3DDevice, pDeclImpl == NULL ? NULL : pDeclImpl->wineD3DVertexDeclaration);
369
370     return hr;
371 }
372
373 HRESULT  WINAPI  IDirect3DDevice9Impl_GetVertexDeclaration(LPDIRECT3DDEVICE9 iface, IDirect3DVertexDeclaration9** ppDecl) {
374     IDirect3DDevice9Impl* This = (IDirect3DDevice9Impl*) iface;
375     IWineD3DVertexDeclaration* pTest = NULL;
376     HRESULT hr = D3D_OK;
377
378     TRACE("(%p) : Relay+\n", iface);
379
380     if (NULL == ppDecl) {
381       return D3DERR_INVALIDCALL;
382     }
383
384     *ppDecl = NULL;
385     hr = IWineD3DDevice_GetVertexDeclaration(This->WineD3DDevice, &pTest);
386     if (hr == D3D_OK && NULL != pTest) {
387         IWineD3DVertexDeclaration_GetParent(pTest, (IUnknown **)ppDecl);
388         IWineD3DVertexDeclaration_Release(pTest);
389     } else {
390         *ppDecl = NULL;
391     }
392     TRACE("(%p) : returning %p\n", This, *ppDecl);
393     return hr;
394 }