crypt32: Make a global copy of crypt32's HINSTANCE.
[wine/multimedia.git] / dlls / d3d9 / vertexdeclaration.c
blob4631248a0f185b5bf3f1c8be34bfd8d6e90c1e0f
1 /*
2 * IDirect3DVertexDeclaration9 implementation
4 * Copyright 2002-2003 Raphael Junqueira
5 * Jason Edmeades
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.
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.
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
22 #include "config.h"
23 #include "d3d9_private.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
27 typedef struct _D3DDECLTYPE_INFO {
28 D3DDECLTYPE d3dType;
29 int size;
30 int typesize;
31 } D3DDECLTYPE_INFO;
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)}};
52 #define D3D_DECL_SIZE(type) d3d_dtype_lookup[type].size
53 #define D3D_DECL_TYPESIZE(type) d3d_dtype_lookup[type].typesize
55 HRESULT vdecl_convert_fvf(
56 DWORD fvf,
57 D3DVERTEXELEMENT9** ppVertexElements) {
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;
70 BOOL has_diffuse = (fvf & D3DFVF_DIFFUSE) != 0;
71 BOOL has_specular = (fvf & D3DFVF_SPECULAR) !=0;
73 DWORD num_textures = (fvf & D3DFVF_TEXCOUNT_MASK) >> D3DFVF_TEXCOUNT_SHIFT;
74 DWORD texcoords = (fvf & 0xFFFF0000) >> 16;
76 D3DVERTEXELEMENT9 end_element = D3DDECL_END();
77 D3DVERTEXELEMENT9 *elements = NULL;
79 unsigned int size;
80 DWORD num_blends = 1 + (((fvf & D3DFVF_XYZB5) - D3DFVF_XYZB1) >> 1);
81 if (has_blend_idx) num_blends--;
83 /* Compute declaration size */
84 size = has_pos + (has_blend && num_blends > 0) + has_blend_idx + has_normal +
85 has_psize + has_diffuse + has_specular + num_textures + 1;
87 /* convert the declaration */
88 elements = HeapAlloc(GetProcessHeap(), 0, size * sizeof(D3DVERTEXELEMENT9));
89 if (!elements)
90 return D3DERR_OUTOFVIDEOMEMORY;
92 elements[size-1] = end_element;
93 idx = 0;
94 if (has_pos) {
95 if (!has_blend && (fvf & D3DFVF_XYZRHW)) {
96 elements[idx].Type = D3DDECLTYPE_FLOAT4;
97 elements[idx].Usage = D3DDECLUSAGE_POSITIONT;
99 else {
100 elements[idx].Type = D3DDECLTYPE_FLOAT3;
101 elements[idx].Usage = D3DDECLUSAGE_POSITION;
103 elements[idx].UsageIndex = 0;
104 idx++;
106 if (has_blend && (num_blends > 0)) {
107 if (((fvf & D3DFVF_XYZB5) == D3DFVF_XYZB2) && (fvf & D3DFVF_LASTBETA_D3DCOLOR))
108 elements[idx].Type = D3DDECLTYPE_D3DCOLOR;
109 else {
110 switch(num_blends) {
111 case 1: elements[idx].Type = D3DDECLTYPE_FLOAT1; break;
112 case 2: elements[idx].Type = D3DDECLTYPE_FLOAT2; break;
113 case 3: elements[idx].Type = D3DDECLTYPE_FLOAT3; break;
114 case 4: elements[idx].Type = D3DDECLTYPE_FLOAT4; break;
115 default:
116 ERR("Unexpected amount of blend values: %u\n", num_blends);
119 elements[idx].Usage = D3DDECLUSAGE_BLENDWEIGHT;
120 elements[idx].UsageIndex = 0;
121 idx++;
123 if (has_blend_idx) {
124 if (fvf & D3DFVF_LASTBETA_UBYTE4 ||
125 (((fvf & D3DFVF_XYZB5) == D3DFVF_XYZB2) && (fvf & D3DFVF_LASTBETA_D3DCOLOR)))
126 elements[idx].Type = D3DDECLTYPE_UBYTE4;
127 else if (fvf & D3DFVF_LASTBETA_D3DCOLOR)
128 elements[idx].Type = D3DDECLTYPE_D3DCOLOR;
129 else
130 elements[idx].Type = D3DDECLTYPE_FLOAT1;
131 elements[idx].Usage = D3DDECLUSAGE_BLENDINDICES;
132 elements[idx].UsageIndex = 0;
133 idx++;
135 if (has_normal) {
136 elements[idx].Type = D3DDECLTYPE_FLOAT3;
137 elements[idx].Usage = D3DDECLUSAGE_NORMAL;
138 elements[idx].UsageIndex = 0;
139 idx++;
141 if (has_psize) {
142 elements[idx].Type = D3DDECLTYPE_FLOAT1;
143 elements[idx].Usage = D3DDECLUSAGE_PSIZE;
144 elements[idx].UsageIndex = 0;
145 idx++;
147 if (has_diffuse) {
148 elements[idx].Type = D3DDECLTYPE_D3DCOLOR;
149 elements[idx].Usage = D3DDECLUSAGE_COLOR;
150 elements[idx].UsageIndex = 0;
151 idx++;
153 if (has_specular) {
154 elements[idx].Type = D3DDECLTYPE_D3DCOLOR;
155 elements[idx].Usage = D3DDECLUSAGE_COLOR;
156 elements[idx].UsageIndex = 1;
157 idx++;
159 for (idx2 = 0; idx2 < num_textures; idx2++) {
160 unsigned int numcoords = (texcoords >> (idx2*2)) & 0x03;
161 switch (numcoords) {
162 case D3DFVF_TEXTUREFORMAT1:
163 elements[idx].Type = D3DDECLTYPE_FLOAT1;
164 break;
165 case D3DFVF_TEXTUREFORMAT2:
166 elements[idx].Type = D3DDECLTYPE_FLOAT2;
167 break;
168 case D3DFVF_TEXTUREFORMAT3:
169 elements[idx].Type = D3DDECLTYPE_FLOAT3;
170 break;
171 case D3DFVF_TEXTUREFORMAT4:
172 elements[idx].Type = D3DDECLTYPE_FLOAT4;
173 break;
175 elements[idx].Usage = D3DDECLUSAGE_TEXCOORD;
176 elements[idx].UsageIndex = idx2;
177 idx++;
180 /* Now compute offsets, and initialize the rest of the fields */
181 for (idx = 0, offset = 0; idx < size-1; idx++) {
182 elements[idx].Stream = 0;
183 elements[idx].Method = D3DDECLMETHOD_DEFAULT;
184 elements[idx].Offset = offset;
185 offset += D3D_DECL_SIZE(elements[idx].Type) * D3D_DECL_TYPESIZE(elements[idx].Type);
188 *ppVertexElements = elements;
189 return D3D_OK;
192 /* IDirect3DVertexDeclaration9 IUnknown parts follow: */
193 static HRESULT WINAPI IDirect3DVertexDeclaration9Impl_QueryInterface(LPDIRECT3DVERTEXDECLARATION9 iface, REFIID riid, LPVOID* ppobj) {
194 IDirect3DVertexDeclaration9Impl *This = (IDirect3DVertexDeclaration9Impl *)iface;
196 if (IsEqualGUID(riid, &IID_IUnknown)
197 || IsEqualGUID(riid, &IID_IDirect3DVertexDeclaration9)) {
198 IDirect3DVertexDeclaration9_AddRef(iface);
199 *ppobj = This;
200 return S_OK;
203 WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
204 *ppobj = NULL;
205 return E_NOINTERFACE;
208 static ULONG WINAPI IDirect3DVertexDeclaration9Impl_AddRef(LPDIRECT3DVERTEXDECLARATION9 iface) {
209 IDirect3DVertexDeclaration9Impl *This = (IDirect3DVertexDeclaration9Impl *)iface;
210 ULONG ref = InterlockedIncrement(&This->ref);
212 TRACE("(%p) : AddRef from %d\n", This, ref - 1);
214 if(ref == 1) {
215 IDirect3DDevice9Ex_AddRef(This->parentDevice);
218 return ref;
221 void IDirect3DVertexDeclaration9Impl_Destroy(LPDIRECT3DVERTEXDECLARATION9 iface) {
222 IDirect3DVertexDeclaration9Impl *This = (IDirect3DVertexDeclaration9Impl *)iface;
224 if(This->ref != 0) {
225 /* Should not happen unless wine has a bug or the application releases references it does not own */
226 ERR("Destroying vdecl with ref != 0\n");
228 EnterCriticalSection(&d3d9_cs);
229 IWineD3DVertexDeclaration_Release(This->wineD3DVertexDeclaration);
230 LeaveCriticalSection(&d3d9_cs);
231 HeapFree(GetProcessHeap(), 0, This->elements);
232 HeapFree(GetProcessHeap(), 0, This);
235 static ULONG WINAPI IDirect3DVertexDeclaration9Impl_Release(LPDIRECT3DVERTEXDECLARATION9 iface) {
236 IDirect3DVertexDeclaration9Impl *This = (IDirect3DVertexDeclaration9Impl *)iface;
237 ULONG ref = InterlockedDecrement(&This->ref);
239 TRACE("(%p) : ReleaseRef to %d\n", This, ref);
241 if (ref == 0) {
242 IDirect3DDevice9Ex *parentDevice = This->parentDevice;
244 if(!This->convFVF) {
245 IDirect3DVertexDeclaration9Impl_Release(iface);
247 IDirect3DDevice9Ex_Release(parentDevice);
249 return ref;
252 /* IDirect3DVertexDeclaration9 Interface follow: */
253 static HRESULT WINAPI IDirect3DVertexDeclaration9Impl_GetDevice(LPDIRECT3DVERTEXDECLARATION9 iface, IDirect3DDevice9** ppDevice) {
254 IDirect3DVertexDeclaration9Impl *This = (IDirect3DVertexDeclaration9Impl *)iface;
255 IWineD3DDevice *myDevice = NULL;
256 HRESULT hr = D3D_OK;
258 TRACE("(%p) : Relay\n", iface);
260 EnterCriticalSection(&d3d9_cs);
261 hr = IWineD3DVertexDeclaration_GetDevice(This->wineD3DVertexDeclaration, &myDevice);
262 if (hr == D3D_OK && myDevice != NULL) {
263 hr = IWineD3DDevice_GetParent(myDevice, (IUnknown **)ppDevice);
264 IWineD3DDevice_Release(myDevice);
266 LeaveCriticalSection(&d3d9_cs);
267 return hr;
270 static HRESULT WINAPI IDirect3DVertexDeclaration9Impl_GetDeclaration(LPDIRECT3DVERTEXDECLARATION9 iface, D3DVERTEXELEMENT9* pDecl, UINT* pNumElements) {
271 IDirect3DVertexDeclaration9Impl *This = (IDirect3DVertexDeclaration9Impl *)iface;
273 TRACE("(%p) : pDecl %p, pNumElements %p)\n", This, pDecl, pNumElements);
275 *pNumElements = This->element_count;
277 /* Passing a NULL pDecl is used to just retrieve the number of elements */
278 if (!pDecl) {
279 TRACE("NULL pDecl passed. Returning D3D_OK.\n");
280 return D3D_OK;
283 TRACE("Copying %p to %p\n", This->elements, pDecl);
284 CopyMemory(pDecl, This->elements, This->element_count * sizeof(D3DVERTEXELEMENT9));
286 return D3D_OK;
289 static const IDirect3DVertexDeclaration9Vtbl Direct3DVertexDeclaration9_Vtbl =
291 /* IUnknown */
292 IDirect3DVertexDeclaration9Impl_QueryInterface,
293 IDirect3DVertexDeclaration9Impl_AddRef,
294 IDirect3DVertexDeclaration9Impl_Release,
295 /* IDirect3DVertexDeclaration9 */
296 IDirect3DVertexDeclaration9Impl_GetDevice,
297 IDirect3DVertexDeclaration9Impl_GetDeclaration
300 static UINT convert_to_wined3d_declaration(const D3DVERTEXELEMENT9* d3d9_elements, WINED3DVERTEXELEMENT **wined3d_elements) {
301 const D3DVERTEXELEMENT9* element;
302 UINT element_count = 1;
303 UINT i;
305 TRACE("d3d9_elements %p, wined3d_elements %p\n", d3d9_elements, wined3d_elements);
307 element = d3d9_elements;
308 while (element++->Stream != 0xff && element_count++ < 128);
310 if (element_count == 128) {
311 return 0;
314 *wined3d_elements = HeapAlloc(GetProcessHeap(), 0, element_count * sizeof(WINED3DVERTEXELEMENT));
315 if (!*wined3d_elements) {
316 FIXME("Memory allocation failed\n");
317 return 0;
320 for (i = 0; i < element_count; ++i) {
321 CopyMemory(*wined3d_elements + i, d3d9_elements + i, sizeof(D3DVERTEXELEMENT9));
322 (*wined3d_elements)[i].Reg = -1;
325 return element_count;
328 /* IDirect3DDevice9 IDirect3DVertexDeclaration9 Methods follow: */
329 HRESULT WINAPI IDirect3DDevice9Impl_CreateVertexDeclaration(LPDIRECT3DDEVICE9EX iface, CONST D3DVERTEXELEMENT9* pVertexElements, IDirect3DVertexDeclaration9** ppDecl) {
331 IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
332 IDirect3DVertexDeclaration9Impl *object = NULL;
333 WINED3DVERTEXELEMENT* wined3d_elements;
334 UINT element_count;
335 HRESULT hr = D3D_OK;
337 TRACE("(%p) : Relay\n", iface);
338 if (NULL == ppDecl) {
339 WARN("(%p) : Caller passed NULL As ppDecl, returning D3DERR_INVALIDCALL\n",This);
340 return D3DERR_INVALIDCALL;
343 element_count = convert_to_wined3d_declaration(pVertexElements, &wined3d_elements);
344 if (!element_count) {
345 FIXME("(%p) : Error parsing vertex declaration\n", This);
346 return D3DERR_INVALIDCALL;
349 /* Allocate the storage for the device */
350 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DVertexDeclaration9Impl));
351 if (NULL == object) {
352 HeapFree(GetProcessHeap(), 0, wined3d_elements);
353 FIXME("Allocation of memory failed, returning D3DERR_OUTOFVIDEOMEMORY\n");
354 return D3DERR_OUTOFVIDEOMEMORY;
357 object->lpVtbl = &Direct3DVertexDeclaration9_Vtbl;
358 object->ref = 0;
360 object->elements = HeapAlloc(GetProcessHeap(), 0, element_count * sizeof(D3DVERTEXELEMENT9));
361 if (!object->elements) {
362 HeapFree(GetProcessHeap(), 0, wined3d_elements);
363 HeapFree(GetProcessHeap(), 0, object);
364 ERR("Memory allocation failed\n");
365 return D3DERR_OUTOFVIDEOMEMORY;
367 CopyMemory(object->elements, pVertexElements, element_count * sizeof(D3DVERTEXELEMENT9));
368 object->element_count = element_count;
370 EnterCriticalSection(&d3d9_cs);
371 hr = IWineD3DDevice_CreateVertexDeclaration(This->WineD3DDevice, &object->wineD3DVertexDeclaration, (IUnknown *)object, wined3d_elements, element_count);
372 LeaveCriticalSection(&d3d9_cs);
374 HeapFree(GetProcessHeap(), 0, wined3d_elements);
376 if (FAILED(hr)) {
378 /* free up object */
379 WARN("(%p) call to IWineD3DDevice_CreateVertexDeclaration failed\n", This);
380 HeapFree(GetProcessHeap(), 0, object->elements);
381 HeapFree(GetProcessHeap(), 0, object);
382 } else {
383 object->parentDevice = iface;
384 *ppDecl = (LPDIRECT3DVERTEXDECLARATION9) object;
385 IDirect3DVertexDeclaration9_AddRef(*ppDecl);
386 TRACE("(%p) : Created vertex declaration %p\n", This, object);
388 return hr;
391 HRESULT WINAPI IDirect3DDevice9Impl_SetVertexDeclaration(LPDIRECT3DDEVICE9EX iface, IDirect3DVertexDeclaration9* pDecl) {
392 IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
393 IDirect3DVertexDeclaration9Impl *pDeclImpl = (IDirect3DVertexDeclaration9Impl *)pDecl;
394 HRESULT hr = D3D_OK;
396 TRACE("(%p) : Relay\n", iface);
398 EnterCriticalSection(&d3d9_cs);
399 hr = IWineD3DDevice_SetVertexDeclaration(This->WineD3DDevice, pDeclImpl == NULL ? NULL : pDeclImpl->wineD3DVertexDeclaration);
400 LeaveCriticalSection(&d3d9_cs);
401 return hr;
404 HRESULT WINAPI IDirect3DDevice9Impl_GetVertexDeclaration(LPDIRECT3DDEVICE9EX iface, IDirect3DVertexDeclaration9** ppDecl) {
405 IDirect3DDevice9Impl* This = (IDirect3DDevice9Impl*) iface;
406 IWineD3DVertexDeclaration* pTest = NULL;
407 HRESULT hr = D3D_OK;
409 TRACE("(%p) : Relay+\n", iface);
411 if (NULL == ppDecl) {
412 return D3DERR_INVALIDCALL;
415 *ppDecl = NULL;
416 EnterCriticalSection(&d3d9_cs);
417 hr = IWineD3DDevice_GetVertexDeclaration(This->WineD3DDevice, &pTest);
418 if (hr == D3D_OK && NULL != pTest) {
419 IWineD3DVertexDeclaration_GetParent(pTest, (IUnknown **)ppDecl);
420 IWineD3DVertexDeclaration_Release(pTest);
421 } else {
422 *ppDecl = NULL;
424 LeaveCriticalSection(&d3d9_cs);
425 TRACE("(%p) : returning %p\n", This, *ppDecl);
426 return hr;