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;
242 TRACE("(%p) : Relay\n", iface);
243 hr = IWineD3DVertexDeclaration_GetDeclaration(This->wineD3DVertexDeclaration, pDecl, &NumElements);
245 *pNumElements = NumElements;
249 static const IDirect3DVertexDeclaration9Vtbl Direct3DVertexDeclaration9_Vtbl =
252 IDirect3DVertexDeclaration9Impl_QueryInterface,
253 IDirect3DVertexDeclaration9Impl_AddRef,
254 IDirect3DVertexDeclaration9Impl_Release,
255 /* IDirect3DVertexDeclaration9 */
256 IDirect3DVertexDeclaration9Impl_GetDevice,
257 IDirect3DVertexDeclaration9Impl_GetDeclaration
261 /* IDirect3DDevice9 IDirect3DVertexDeclaration9 Methods follow: */
262 HRESULT WINAPI IDirect3DDevice9Impl_CreateVertexDeclaration(LPDIRECT3DDEVICE9 iface, CONST D3DVERTEXELEMENT9* pVertexElements, IDirect3DVertexDeclaration9** ppDecl) {
264 IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
265 IDirect3DVertexDeclaration9Impl *object = NULL;
268 TRACE("(%p) : Relay\n", iface);
269 if (NULL == ppDecl) {
270 WARN("(%p) : Caller passed NULL As ppDecl, returning D3DERR_INVALIDCALL\n",This);
271 return D3DERR_INVALIDCALL;
273 /* Allocate the storage for the device */
274 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DVertexDeclaration9Impl));
275 if (NULL == object) {
276 FIXME("Allocation of memory failed, returning D3DERR_OUTOFVIDEOMEMORY\n");
277 return D3DERR_OUTOFVIDEOMEMORY;
280 object->lpVtbl = &Direct3DVertexDeclaration9_Vtbl;
282 hr = IWineD3DDevice_CreateVertexDeclaration(This->WineD3DDevice, pVertexElements, &object->wineD3DVertexDeclaration, (IUnknown *)object);
287 FIXME("(%p) call to IWineD3DDevice_CreateVertexDeclaration failed\n", This);
288 HeapFree(GetProcessHeap(), 0, object);
290 IUnknown_AddRef(iface);
291 object->parentDevice = iface;
292 *ppDecl = (LPDIRECT3DVERTEXDECLARATION9) object;
293 TRACE("(%p) : Created vertex declaration %p\n", This, object);
298 HRESULT WINAPI IDirect3DDevice9Impl_SetVertexDeclaration(LPDIRECT3DDEVICE9 iface, IDirect3DVertexDeclaration9* pDecl) {
299 IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
300 IDirect3DVertexDeclaration9Impl *pDeclImpl = (IDirect3DVertexDeclaration9Impl *)pDecl;
303 TRACE("(%p) : Relay\n", iface);
305 hr = IWineD3DDevice_SetVertexDeclaration(This->WineD3DDevice, pDeclImpl == NULL ? NULL : pDeclImpl->wineD3DVertexDeclaration);
310 HRESULT WINAPI IDirect3DDevice9Impl_GetVertexDeclaration(LPDIRECT3DDEVICE9 iface, IDirect3DVertexDeclaration9** ppDecl) {
311 IDirect3DDevice9Impl* This = (IDirect3DDevice9Impl*) iface;
312 IWineD3DVertexDeclaration* pTest = NULL;
315 TRACE("(%p) : Relay+\n", iface);
317 if (NULL == ppDecl) {
318 return D3DERR_INVALIDCALL;
322 hr = IWineD3DDevice_GetVertexDeclaration(This->WineD3DDevice, &pTest);
323 if (hr == D3D_OK && NULL != pTest) {
324 IWineD3DResource_GetParent(pTest, (IUnknown **)ppDecl);
325 IWineD3DResource_Release(pTest);
329 TRACE("(%p) : returning %p\n", This, *ppDecl);