Changes in crossover-wine-src-6.1.0 except for configure
[wine/hacks.git] / dlls / d3d9 / vertexdeclaration.c
blobab7d484a78eb98fead26346ba075fbb785d9bc68
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;
69 BOOL has_diffuse = (fvf & D3DFVF_DIFFUSE) != 0;
70 BOOL has_specular = (fvf & D3DFVF_SPECULAR) !=0;
72 DWORD num_textures = (fvf & D3DFVF_TEXCOUNT_MASK) >> D3DFVF_TEXCOUNT_SHIFT;
73 DWORD texcoords = (fvf & 0x00FF0000) >> 16;
75 D3DVERTEXELEMENT9 end_element = D3DDECL_END();
76 D3DVERTEXELEMENT9 *elements = NULL;
78 unsigned int size;
79 DWORD num_blends = 1 + (((fvf & D3DFVF_XYZB5) - D3DFVF_XYZB1) >> 1);
80 if (has_blend_idx) num_blends--;
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;
86 /* convert the declaration */
87 elements = HeapAlloc(GetProcessHeap(), 0, size * sizeof(D3DVERTEXELEMENT9));
88 if (!elements)
89 return D3DERR_OUTOFVIDEOMEMORY;
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;
98 else {
99 elements[idx].Type = D3DDECLTYPE_FLOAT3;
100 elements[idx].Usage = D3DDECLUSAGE_POSITION;
102 elements[idx].UsageIndex = 0;
103 idx++;
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++;
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++;
126 if (has_normal) {
127 elements[idx].Type = D3DDECLTYPE_FLOAT3;
128 elements[idx].Usage = D3DDECLUSAGE_NORMAL;
129 elements[idx].UsageIndex = 0;
130 idx++;
132 if (has_psize) {
133 elements[idx].Type = D3DDECLTYPE_FLOAT1;
134 elements[idx].Usage = D3DDECLUSAGE_PSIZE;
135 elements[idx].UsageIndex = 0;
136 idx++;
138 if (has_diffuse) {
139 elements[idx].Type = D3DDECLTYPE_D3DCOLOR;
140 elements[idx].Usage = D3DDECLUSAGE_COLOR;
141 elements[idx].UsageIndex = 0;
142 idx++;
144 if (has_specular) {
145 elements[idx].Type = D3DDECLTYPE_D3DCOLOR;
146 elements[idx].Usage = D3DDECLUSAGE_COLOR;
147 elements[idx].UsageIndex = 1;
148 idx++;
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;
166 elements[idx].Usage = D3DDECLUSAGE_TEXCOORD;
167 elements[idx].UsageIndex = idx2;
168 idx++;
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);
179 *ppVertexElements = elements;
180 return D3D_OK;
183 /* IDirect3DVertexDeclaration9 IUnknown parts follow: */
184 static HRESULT WINAPI IDirect3DVertexDeclaration9Impl_QueryInterface(LPDIRECT3DVERTEXDECLARATION9 iface, REFIID riid, LPVOID* ppobj) {
185 IDirect3DVertexDeclaration9Impl *This = (IDirect3DVertexDeclaration9Impl *)iface;
187 if (IsEqualGUID(riid, &IID_IUnknown)
188 || IsEqualGUID(riid, &IID_IDirect3DVertexDeclaration9)) {
189 IUnknown_AddRef(iface);
190 *ppobj = This;
191 return S_OK;
194 WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
195 *ppobj = NULL;
196 return E_NOINTERFACE;
199 static ULONG WINAPI IDirect3DVertexDeclaration9Impl_AddRef(LPDIRECT3DVERTEXDECLARATION9 iface) {
200 IDirect3DVertexDeclaration9Impl *This = (IDirect3DVertexDeclaration9Impl *)iface;
201 ULONG ref = InterlockedIncrement(&This->ref);
203 TRACE("(%p) : AddRef from %d\n", This, ref - 1);
205 if(ref == 1) {
206 IUnknown_AddRef(This->parentDevice);
209 return ref;
212 void IDirect3DVertexDeclaration9Impl_Destroy(LPDIRECT3DVERTEXDECLARATION9 iface) {
213 IDirect3DVertexDeclaration9Impl *This = (IDirect3DVertexDeclaration9Impl *)iface;
215 if(This->ref != 0) {
216 /* Should not happen unless wine has a bug or the application releases references it does not own */
217 ERR("Destroying vdecl with ref != 0\n");
219 IWineD3DVertexDeclaration_Release(This->wineD3DVertexDeclaration);
220 HeapFree(GetProcessHeap(), 0, This->elements);
221 HeapFree(GetProcessHeap(), 0, This);
224 static ULONG WINAPI IDirect3DVertexDeclaration9Impl_Release(LPDIRECT3DVERTEXDECLARATION9 iface) {
225 IDirect3DVertexDeclaration9Impl *This = (IDirect3DVertexDeclaration9Impl *)iface;
226 ULONG ref = InterlockedDecrement(&This->ref);
228 TRACE("(%p) : ReleaseRef to %d\n", This, ref);
230 if (ref == 0) {
231 IDirect3DDevice9 *parentDevice = This->parentDevice;
233 if(!This->convFVF) {
234 IDirect3DVertexDeclaration9Impl_Release(iface);
236 IUnknown_Release(parentDevice);
238 return ref;
241 /* IDirect3DVertexDeclaration9 Interface follow: */
242 static HRESULT WINAPI IDirect3DVertexDeclaration9Impl_GetDevice(LPDIRECT3DVERTEXDECLARATION9 iface, IDirect3DDevice9** ppDevice) {
243 IDirect3DVertexDeclaration9Impl *This = (IDirect3DVertexDeclaration9Impl *)iface;
244 IWineD3DDevice *myDevice = NULL;
245 HRESULT hr = D3D_OK;
247 TRACE("(%p) : Relay\n", iface);
249 hr = IWineD3DVertexDeclaration_GetDevice(This->wineD3DVertexDeclaration, &myDevice);
250 if (hr == D3D_OK && myDevice != NULL) {
251 hr = IWineD3DDevice_GetParent(myDevice, (IUnknown **)ppDevice);
252 IWineD3DDevice_Release(myDevice);
254 return hr;
257 static HRESULT WINAPI IDirect3DVertexDeclaration9Impl_GetDeclaration(LPDIRECT3DVERTEXDECLARATION9 iface, D3DVERTEXELEMENT9* pDecl, UINT* pNumElements) {
258 IDirect3DVertexDeclaration9Impl *This = (IDirect3DVertexDeclaration9Impl *)iface;
260 TRACE("(%p) : pDecl %p, pNumElements %p)\n", This, pDecl, pNumElements);
262 *pNumElements = This->element_count;
264 /* Passing a NULL pDecl is used to just retrieve the number of elements */
265 if (!pDecl) {
266 TRACE("NULL pDecl passed. Returning D3D_OK.\n");
267 return D3D_OK;
270 TRACE("Copying %p to %p\n", This->elements, pDecl);
271 CopyMemory(pDecl, This->elements, This->element_count * sizeof(D3DVERTEXELEMENT9));
273 return D3D_OK;
276 static const IDirect3DVertexDeclaration9Vtbl Direct3DVertexDeclaration9_Vtbl =
278 /* IUnknown */
279 IDirect3DVertexDeclaration9Impl_QueryInterface,
280 IDirect3DVertexDeclaration9Impl_AddRef,
281 IDirect3DVertexDeclaration9Impl_Release,
282 /* IDirect3DVertexDeclaration9 */
283 IDirect3DVertexDeclaration9Impl_GetDevice,
284 IDirect3DVertexDeclaration9Impl_GetDeclaration
287 static size_t convert_to_wined3d_declaration(const D3DVERTEXELEMENT9* d3d9_elements, WINED3DVERTEXELEMENT **wined3d_elements) {
288 const D3DVERTEXELEMENT9* element;
289 size_t element_count = 1;
290 size_t i;
292 TRACE("d3d9_elements %p, wined3d_elements %p\n", d3d9_elements, wined3d_elements);
294 element = d3d9_elements;
295 while (element++->Stream != 0xff && element_count++ < 128);
297 if (element_count == 128) {
298 return 0;
301 *wined3d_elements = HeapAlloc(GetProcessHeap(), 0, element_count * sizeof(WINED3DVERTEXELEMENT));
302 if (!*wined3d_elements) {
303 FIXME("Memory allocation failed\n");
304 return 0;
307 for (i = 0; i < element_count; ++i) {
308 CopyMemory(*wined3d_elements + i, d3d9_elements + i, sizeof(D3DVERTEXELEMENT9));
309 (*wined3d_elements)[i].Reg = -1;
312 return element_count;
315 /* IDirect3DDevice9 IDirect3DVertexDeclaration9 Methods follow: */
316 HRESULT WINAPI IDirect3DDevice9Impl_CreateVertexDeclaration(LPDIRECT3DDEVICE9 iface, CONST D3DVERTEXELEMENT9* pVertexElements, IDirect3DVertexDeclaration9** ppDecl) {
318 IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
319 IDirect3DVertexDeclaration9Impl *object = NULL;
320 WINED3DVERTEXELEMENT* wined3d_elements;
321 size_t element_count;
322 HRESULT hr = D3D_OK;
324 TRACE("(%p) : Relay\n", iface);
325 if (NULL == ppDecl) {
326 WARN("(%p) : Caller passed NULL As ppDecl, returning D3DERR_INVALIDCALL\n",This);
327 return D3DERR_INVALIDCALL;
330 element_count = convert_to_wined3d_declaration(pVertexElements, &wined3d_elements);
331 if (!element_count) {
332 FIXME("(%p) : Error parsing vertex declaration\n", This);
333 return D3DERR_INVALIDCALL;
336 /* Allocate the storage for the device */
337 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DVertexDeclaration9Impl));
338 if (NULL == object) {
339 HeapFree(GetProcessHeap(), 0, wined3d_elements);
340 FIXME("Allocation of memory failed, returning D3DERR_OUTOFVIDEOMEMORY\n");
341 return D3DERR_OUTOFVIDEOMEMORY;
344 object->lpVtbl = &Direct3DVertexDeclaration9_Vtbl;
345 object->ref = 0;
347 object->elements = HeapAlloc(GetProcessHeap(), 0, element_count * sizeof(D3DVERTEXELEMENT9));
348 if (!object->elements) {
349 HeapFree(GetProcessHeap(), 0, wined3d_elements);
350 HeapFree(GetProcessHeap(), 0, object);
351 ERR("Memory allocation failed\n");
352 return D3DERR_OUTOFVIDEOMEMORY;
354 CopyMemory(object->elements, pVertexElements, element_count * sizeof(D3DVERTEXELEMENT9));
355 object->element_count = element_count;
357 hr = IWineD3DDevice_CreateVertexDeclaration(This->WineD3DDevice, &object->wineD3DVertexDeclaration, (IUnknown *)object, wined3d_elements, element_count);
359 HeapFree(GetProcessHeap(), 0, wined3d_elements);
361 if (FAILED(hr)) {
363 /* free up object */
364 FIXME("(%p) call to IWineD3DDevice_CreateVertexDeclaration failed\n", This);
365 HeapFree(GetProcessHeap(), 0, object->elements);
366 HeapFree(GetProcessHeap(), 0, object);
367 } else {
368 object->parentDevice = iface;
369 *ppDecl = (LPDIRECT3DVERTEXDECLARATION9) object;
370 IUnknown_AddRef(*ppDecl);
371 TRACE("(%p) : Created vertex declaration %p\n", This, object);
373 return hr;
376 HRESULT WINAPI IDirect3DDevice9Impl_SetVertexDeclaration(LPDIRECT3DDEVICE9 iface, IDirect3DVertexDeclaration9* pDecl) {
377 IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
378 IDirect3DVertexDeclaration9Impl *pDeclImpl = (IDirect3DVertexDeclaration9Impl *)pDecl;
379 HRESULT hr = D3D_OK;
381 TRACE("(%p) : Relay\n", iface);
383 hr = IWineD3DDevice_SetVertexDeclaration(This->WineD3DDevice, pDeclImpl == NULL ? NULL : pDeclImpl->wineD3DVertexDeclaration);
384 return hr;
387 HRESULT WINAPI IDirect3DDevice9Impl_GetVertexDeclaration(LPDIRECT3DDEVICE9 iface, IDirect3DVertexDeclaration9** ppDecl) {
388 IDirect3DDevice9Impl* This = (IDirect3DDevice9Impl*) iface;
389 IWineD3DVertexDeclaration* pTest = NULL;
390 HRESULT hr = D3D_OK;
392 TRACE("(%p) : Relay+\n", iface);
394 if (NULL == ppDecl) {
395 return D3DERR_INVALIDCALL;
398 *ppDecl = NULL;
399 hr = IWineD3DDevice_GetVertexDeclaration(This->WineD3DDevice, &pTest);
400 if (hr == D3D_OK && NULL != pTest) {
401 IWineD3DVertexDeclaration_GetParent(pTest, (IUnknown **)ppDecl);
402 IWineD3DVertexDeclaration_Release(pTest);
403 } else {
404 *ppDecl = NULL;
406 TRACE("(%p) : returning %p\n", This, *ppDecl);
407 return hr;