2 * IDirect3DVertexDeclaration9 implementation
4 * Copyright 2002-2003 Raphael Junqueira
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
23 #include "d3d9_private.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
27 typedef struct _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(
57 D3DVERTEXELEMENT9** ppVertexElements) {
59 unsigned int idx, idx2;
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;
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));
89 return D3DERR_OUTOFVIDEOMEMORY;
91 memcpy(&elements[size-1], &end_element, sizeof(D3DVERTEXELEMENT9));
94 if (!has_blend && (fvf & D3DFVF_XYZRHW)) {
95 elements[idx].Type = D3DDECLTYPE_FLOAT4;
96 elements[idx].Usage = D3DDECLUSAGE_POSITIONT;
99 elements[idx].Type = D3DDECLTYPE_FLOAT3;
100 elements[idx].Usage = D3DDECLUSAGE_POSITION;
102 elements[idx].UsageIndex = 0;
105 if (has_blend && (num_blends > 0)) {
106 if (((fvf & D3DFVF_XYZB5) == D3DFVF_XYZB2) && (fvf & D3DFVF_LASTBETA_D3DCOLOR))
107 elements[idx].Type = D3DDECLTYPE_D3DCOLOR;
109 elements[idx].Type = D3DDECLTYPE_FLOAT1 + num_blends - 1;
110 elements[idx].Usage = D3DDECLUSAGE_BLENDWEIGHT;
111 elements[idx].UsageIndex = 0;
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;
121 elements[idx].Type = D3DDECLTYPE_FLOAT1;
122 elements[idx].Usage = D3DDECLUSAGE_BLENDINDICES;
123 elements[idx].UsageIndex = 0;
127 elements[idx].Type = D3DDECLTYPE_FLOAT3;
128 elements[idx].Usage = D3DDECLUSAGE_NORMAL;
129 elements[idx].UsageIndex = 0;
133 elements[idx].Type = D3DDECLTYPE_FLOAT1;
134 elements[idx].Usage = D3DDECLUSAGE_PSIZE;
135 elements[idx].UsageIndex = 0;
139 elements[idx].Type = D3DDECLTYPE_D3DCOLOR;
140 elements[idx].Usage = D3DDECLUSAGE_COLOR;
141 elements[idx].UsageIndex = 0;
145 elements[idx].Type = D3DDECLTYPE_D3DCOLOR;
146 elements[idx].Usage = D3DDECLUSAGE_COLOR;
147 elements[idx].UsageIndex = 1;
150 for (idx2 = 0; idx2 < num_textures; idx2++) {
151 unsigned int numcoords = (texcoords >> (idx2*2)) & 0x03;
153 case D3DFVF_TEXTUREFORMAT1:
154 elements[idx].Type = D3DDECLTYPE_FLOAT1;
156 case D3DFVF_TEXTUREFORMAT2:
157 elements[idx].Type = D3DDECLTYPE_FLOAT2;
159 case D3DFVF_TEXTUREFORMAT3:
160 elements[idx].Type = D3DDECLTYPE_FLOAT3;
162 case D3DFVF_TEXTUREFORMAT4:
163 elements[idx].Type = D3DDECLTYPE_FLOAT4;
166 elements[idx].Usage = D3DDECLUSAGE_TEXCOORD;
167 elements[idx].UsageIndex = idx2;
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;
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);
194 WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
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);
208 static ULONG WINAPI IDirect3DVertexDeclaration9Impl_Release(LPDIRECT3DVERTEXDECLARATION9 iface) {
209 IDirect3DVertexDeclaration9Impl *This = (IDirect3DVertexDeclaration9Impl *)iface;
210 ULONG ref = InterlockedDecrement(&This->ref);
212 TRACE("(%p) : ReleaseRef to %d\n", This, ref);
215 IWineD3DVertexDeclaration_Release(This->wineD3DVertexDeclaration);
216 IUnknown_Release(This->parentDevice);
217 HeapFree(GetProcessHeap(), 0, This);
222 /* IDirect3DVertexDeclaration9 Interface follow: */
223 static HRESULT WINAPI IDirect3DVertexDeclaration9Impl_GetDevice(LPDIRECT3DVERTEXDECLARATION9 iface, IDirect3DDevice9** ppDevice) {
224 IDirect3DVertexDeclaration9Impl *This = (IDirect3DVertexDeclaration9Impl *)iface;
225 IWineD3DDevice *myDevice = NULL;
228 TRACE("(%p) : Relay\n", iface);
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);
238 static HRESULT WINAPI IDirect3DVertexDeclaration9Impl_GetDeclaration(LPDIRECT3DVERTEXDECLARATION9 iface, D3DVERTEXELEMENT9* pDecl, UINT* pNumElements) {
239 IDirect3DVertexDeclaration9Impl *This = (IDirect3DVertexDeclaration9Impl *)iface;
241 TRACE("(%p) : pDecl %p, pNumElements %p)\n", This, pDecl, pNumElements);
243 *pNumElements = This->element_count;
245 /* Passing a NULL pDecl is used to just retrieve the number of elements */
247 TRACE("NULL pDecl passed. Returning D3D_OK.\n");
251 TRACE("Copying %p to %p\n", This->elements, pDecl);
252 CopyMemory(pDecl, This->elements, This->element_count * sizeof(D3DVERTEXELEMENT9));
257 static const IDirect3DVertexDeclaration9Vtbl Direct3DVertexDeclaration9_Vtbl =
260 IDirect3DVertexDeclaration9Impl_QueryInterface,
261 IDirect3DVertexDeclaration9Impl_AddRef,
262 IDirect3DVertexDeclaration9Impl_Release,
263 /* IDirect3DVertexDeclaration9 */
264 IDirect3DVertexDeclaration9Impl_GetDevice,
265 IDirect3DVertexDeclaration9Impl_GetDeclaration
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;
273 TRACE("d3d9_elements %p, wined3d_elements %p\n", d3d9_elements, wined3d_elements);
275 element = d3d9_elements;
276 while (element++->Stream != 0xff && element_count++ < 128);
278 if (element_count == 128) {
282 *wined3d_elements = HeapAlloc(GetProcessHeap(), 0, element_count * sizeof(WINED3DVERTEXELEMENT));
283 if (!*wined3d_elements) {
284 FIXME("Memory allocation failed\n");
288 for (i = 0; i < element_count; ++i) {
289 CopyMemory(*wined3d_elements + i, d3d9_elements + i, sizeof(D3DVERTEXELEMENT9));
290 (*wined3d_elements)[i].Reg = -1;
293 return element_count;
296 /* IDirect3DDevice9 IDirect3DVertexDeclaration9 Methods follow: */
297 HRESULT WINAPI IDirect3DDevice9Impl_CreateVertexDeclaration(LPDIRECT3DDEVICE9 iface, CONST D3DVERTEXELEMENT9* pVertexElements, IDirect3DVertexDeclaration9** ppDecl) {
299 IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
300 IDirect3DVertexDeclaration9Impl *object = NULL;
301 WINED3DVERTEXELEMENT* wined3d_elements;
302 size_t element_count;
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;
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;
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;
325 object->lpVtbl = &Direct3DVertexDeclaration9_Vtbl;
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;
335 CopyMemory(object->elements, pVertexElements, element_count * sizeof(D3DVERTEXELEMENT9));
336 object->element_count = element_count;
338 hr = IWineD3DDevice_CreateVertexDeclaration(This->WineD3DDevice, &object->wineD3DVertexDeclaration, (IUnknown *)object, wined3d_elements, element_count);
340 HeapFree(GetProcessHeap(), 0, wined3d_elements);
345 FIXME("(%p) call to IWineD3DDevice_CreateVertexDeclaration failed\n", This);
346 HeapFree(GetProcessHeap(), 0, object);
348 IUnknown_AddRef(iface);
349 object->parentDevice = iface;
350 *ppDecl = (LPDIRECT3DVERTEXDECLARATION9) object;
351 TRACE("(%p) : Created vertex declaration %p\n", This, object);
356 HRESULT WINAPI IDirect3DDevice9Impl_SetVertexDeclaration(LPDIRECT3DDEVICE9 iface, IDirect3DVertexDeclaration9* pDecl) {
357 IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
358 IDirect3DVertexDeclaration9Impl *pDeclImpl = (IDirect3DVertexDeclaration9Impl *)pDecl;
361 TRACE("(%p) : Relay\n", iface);
363 if (This->convertedDecl && This->convertedDecl != pDecl) {
364 IUnknown_Release(This->convertedDecl);
365 This->convertedDecl = NULL;
368 hr = IWineD3DDevice_SetVertexDeclaration(This->WineD3DDevice, pDeclImpl == NULL ? NULL : pDeclImpl->wineD3DVertexDeclaration);
373 HRESULT WINAPI IDirect3DDevice9Impl_GetVertexDeclaration(LPDIRECT3DDEVICE9 iface, IDirect3DVertexDeclaration9** ppDecl) {
374 IDirect3DDevice9Impl* This = (IDirect3DDevice9Impl*) iface;
375 IWineD3DVertexDeclaration* pTest = NULL;
378 TRACE("(%p) : Relay+\n", iface);
380 if (NULL == ppDecl) {
381 return D3DERR_INVALIDCALL;
385 hr = IWineD3DDevice_GetVertexDeclaration(This->WineD3DDevice, &pTest);
386 if (hr == D3D_OK && NULL != pTest) {
387 IWineD3DVertexDeclaration_GetParent(pTest, (IUnknown **)ppDecl);
388 IWineD3DVertexDeclaration_Release(pTest);
392 TRACE("(%p) : returning %p\n", This, *ppDecl);