wined3d: Move the projection matrix into its own state.
[wine] / dlls / wined3d / vertexdeclaration.c
1 /*
2  * vertex declaration implementation
3  *
4  * Copyright 2002-2005 Raphael Junqueira
5  * Copyright 2004 Jason Edmeades
6  * Copyright 2004 Christian Costa
7  * Copyright 2005 Oliver Stieber
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23
24 #include "config.h"
25 #include "wined3d_private.h"
26
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d_decl);
28
29 /**
30  * DirectX9 SDK download
31  *  http://msdn.microsoft.com/library/default.asp?url=/downloads/list/directx.asp
32  *
33  * Exploring D3DX
34  *  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndrive/html/directx07162002.asp
35  *
36  * Using Vertex Shaders
37  *  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndrive/html/directx02192001.asp
38  *
39  * Dx9 New
40  *  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/whatsnew.asp
41  *
42  * Dx9 Shaders
43  *  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/reference/Shaders/VertexShader2_0/VertexShader2_0.asp
44  *  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/reference/Shaders/VertexShader2_0/Instructions/Instructions.asp
45  *  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/programmingguide/GettingStarted/VertexDeclaration/VertexDeclaration.asp
46  *  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/reference/Shaders/VertexShader3_0/VertexShader3_0.asp
47  *
48  * Dx9 D3DX
49  *  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/programmingguide/advancedtopics/VertexPipe/matrixstack/matrixstack.asp
50  *
51  * FVF
52  *  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/programmingguide/GettingStarted/VertexFormats/vformats.asp
53  *
54  * NVIDIA: DX8 Vertex Shader to NV Vertex Program
55  *  http://developer.nvidia.com/view.asp?IO=vstovp
56  *
57  * NVIDIA: Memory Management with VAR
58  *  http://developer.nvidia.com/view.asp?IO=var_memory_management
59  */
60
61 /** Vertex Shader Declaration 8 data types tokens */
62 #define MAX_VSHADER_DECL_TYPES 8
63
64 static const char * const VertexDecl8_DataTypes[] = {
65   "D3DVSDT_FLOAT1",
66   "D3DVSDT_FLOAT2",
67   "D3DVSDT_FLOAT3",
68   "D3DVSDT_FLOAT4",
69   "D3DVSDT_D3DCOLOR",
70   "D3DVSDT_UBYTE4",
71   "D3DVSDT_SHORT2",
72   "D3DVSDT_SHORT4",
73   NULL
74 };
75
76 static const char * const VertexDecl8_Registers[] = {
77   "D3DVSDE_POSITION",
78   "D3DVSDE_BLENDWEIGHT",
79   "D3DVSDE_BLENDINDICES",
80   "D3DVSDE_NORMAL",
81   "D3DVSDE_PSIZE",
82   "D3DVSDE_DIFFUSE",
83   "D3DVSDE_SPECULAR",
84   "D3DVSDE_TEXCOORD0",
85   "D3DVSDE_TEXCOORD1",
86   "D3DVSDE_TEXCOORD2",
87   "D3DVSDE_TEXCOORD3",
88   "D3DVSDE_TEXCOORD4",
89   "D3DVSDE_TEXCOORD5",
90   "D3DVSDE_TEXCOORD6",
91   "D3DVSDE_TEXCOORD7",
92   "D3DVSDE_POSITION2",
93   "D3DVSDE_NORMAL2",
94   NULL
95 };
96
97 typedef enum _D3DVSD_TOKENTYPE {
98   D3DVSD_TOKEN_NOP         = 0,
99   D3DVSD_TOKEN_STREAM      = 1,
100   D3DVSD_TOKEN_STREAMDATA  = 2,
101   D3DVSD_TOKEN_TESSELLATOR = 3,
102   D3DVSD_TOKEN_CONSTMEM    = 4,
103   D3DVSD_TOKEN_EXT         = 5,
104   /* RESERVED              = 6 */
105   D3DVSD_TOKEN_END         = 7,
106   D3DVSD_FORCE_DWORD       = 0x7FFFFFFF
107 } D3DVSD_TOKENTYPE;
108
109 typedef enum _D3DVSDE_REGISTER {
110   D3DVSDE_POSITION     =  0,
111   D3DVSDE_BLENDWEIGHT  =  1,
112   D3DVSDE_BLENDINDICES =  2,
113   D3DVSDE_NORMAL       =  3,
114   D3DVSDE_PSIZE        =  4,
115   D3DVSDE_DIFFUSE      =  5,
116   D3DVSDE_SPECULAR     =  6,
117   D3DVSDE_TEXCOORD0    =  7,
118   D3DVSDE_TEXCOORD1    =  8,
119   D3DVSDE_TEXCOORD2    =  9,
120   D3DVSDE_TEXCOORD3    = 10,
121   D3DVSDE_TEXCOORD4    = 11,
122   D3DVSDE_TEXCOORD5    = 12,
123   D3DVSDE_TEXCOORD6    = 13,
124   D3DVSDE_TEXCOORD7    = 14,
125   D3DVSDE_POSITION2    = 15,
126   D3DVSDE_NORMAL2      = 16,
127   MAX_D3DVSDE          = 17
128 } D3DVSDE_REGISTER;
129
130 typedef enum _D3DVSDT_TYPE {
131   D3DVSDT_FLOAT1   = 0x00,
132   D3DVSDT_FLOAT2   = 0x01,
133   D3DVSDT_FLOAT3   = 0x02,
134   D3DVSDT_FLOAT4   = 0x03,
135   D3DVSDT_D3DCOLOR = 0x04,
136   D3DVSDT_UBYTE4   = 0x05,
137   D3DVSDT_SHORT2   = 0x06,
138   D3DVSDT_SHORT4   = 0x07
139 } D3DVSDT_TYPE;
140
141
142 #define D3DVSD_CONSTADDRESSSHIFT  0
143 #define D3DVSD_EXTINFOSHIFT       0
144 #define D3DVSD_STREAMNUMBERSHIFT  0
145 #define D3DVSD_VERTEXREGSHIFT     0
146 #define D3DVSD_CONSTRSSHIFT      16
147 #define D3DVSD_DATATYPESHIFT     16
148 #define D3DVSD_SKIPCOUNTSHIFT    16
149 #define D3DVSD_VERTEXREGINSHIFT  20
150 #define D3DVSD_EXTCOUNTSHIFT     24
151 #define D3DVSD_CONSTCOUNTSHIFT   25
152 #define D3DVSD_DATALOADTYPESHIFT 28
153 #define D3DVSD_STREAMTESSSHIFT   28
154 #define D3DVSD_TOKENTYPESHIFT    29
155
156 #define D3DVSD_CONSTADDRESSMASK  (0x7F     << D3DVSD_CONSTADDRESSSHIFT)
157 #define D3DVSD_EXTINFOMASK       (0xFFFFFF << D3DVSD_EXTINFOSHIFT)
158 #define D3DVSD_STREAMNUMBERMASK  (0xF      << D3DVSD_STREAMNUMBERSHIFT)
159 #define D3DVSD_VERTEXREGMASK     (0x1F     << D3DVSD_VERTEXREGSHIFT)
160 #define D3DVSD_CONSTRSMASK       (0x1FFF   << D3DVSD_CONSTRSSHIFT)
161 #define D3DVSD_DATATYPEMASK      (0xF      << D3DVSD_DATATYPESHIFT)
162 #define D3DVSD_SKIPCOUNTMASK     (0xF      << D3DVSD_SKIPCOUNTSHIFT)
163 #define D3DVSD_EXTCOUNTMASK      (0x1F     << D3DVSD_EXTCOUNTSHIFT)
164 #define D3DVSD_VERTEXREGINMASK   (0xF      << D3DVSD_VERTEXREGINSHIFT)
165 #define D3DVSD_CONSTCOUNTMASK    (0xF      << D3DVSD_CONSTCOUNTSHIFT)
166 #define D3DVSD_DATALOADTYPEMASK  (0x1      << D3DVSD_DATALOADTYPESHIFT)
167 #define D3DVSD_STREAMTESSMASK    (0x1      << D3DVSD_STREAMTESSSHIFT)
168 #define D3DVSD_TOKENTYPEMASK     (0x7      << D3DVSD_TOKENTYPESHIFT)
169
170 #define D3DVSD_END() 0xFFFFFFFF
171 #define D3DVSD_NOP() 0x00000000
172
173 static void dump_wined3dvertexelement(const WINED3DVERTEXELEMENT *element) {
174     TRACE("     Stream: %d\n", element->Stream);
175     TRACE("     Offset: %d\n", element->Offset);
176     TRACE("       Type: %s (%#x)\n", debug_d3ddecltype(element->Type), element->Type);
177     TRACE("     Method: %s (%#x)\n", debug_d3ddeclmethod(element->Method), element->Method);
178     TRACE("      Usage: %s (%#x)\n", debug_d3ddeclusage(element->Usage), element->Usage);
179     TRACE("Usage index: %d\n", element->UsageIndex);
180     TRACE("   Register: %d\n", element->Reg);
181 }
182
183 static DWORD IWineD3DVertexDeclarationImpl_ParseToken8(const DWORD* pToken) {
184   const DWORD token = *pToken;
185   DWORD tokenlen = 1;
186
187   switch ((token & D3DVSD_TOKENTYPEMASK) >> D3DVSD_TOKENTYPESHIFT) { /* maybe a macro to inverse ... */
188   case D3DVSD_TOKEN_NOP:
189     TRACE(" 0x%08x NOP()\n", token);
190     break;
191   case D3DVSD_TOKEN_STREAM:
192     if (token & D3DVSD_STREAMTESSMASK) {
193       TRACE(" 0x%08x STREAM_TESS()\n", token);
194     } else {
195       TRACE(" 0x%08x STREAM(%u)\n", token, ((token & D3DVSD_STREAMNUMBERMASK) >> D3DVSD_STREAMNUMBERSHIFT));
196     }
197     break;
198   case D3DVSD_TOKEN_STREAMDATA:
199     if (token & 0x10000000) {
200       TRACE(" 0x%08x SKIP(%u)\n", token, ((token & D3DVSD_SKIPCOUNTMASK) >> D3DVSD_SKIPCOUNTSHIFT));
201     } else {
202       DWORD type = ((token & D3DVSD_DATATYPEMASK)  >> D3DVSD_DATATYPESHIFT);
203       DWORD reg  = ((token & D3DVSD_VERTEXREGMASK) >> D3DVSD_VERTEXREGSHIFT);
204       TRACE(" 0x%08x REG(%s, %s)\n", token, VertexDecl8_Registers[reg], VertexDecl8_DataTypes[type]);
205     }
206     break;
207   case D3DVSD_TOKEN_TESSELLATOR:
208     if (token & 0x10000000) {
209       DWORD type = ((token & D3DVSD_DATATYPEMASK)  >> D3DVSD_DATATYPESHIFT);
210       DWORD reg  = ((token & D3DVSD_VERTEXREGMASK) >> D3DVSD_VERTEXREGSHIFT);
211       TRACE(" 0x%08x TESSUV(%s) as %s\n", token, VertexDecl8_Registers[reg], VertexDecl8_DataTypes[type]);
212     } else {
213       DWORD type   = ((token & D3DVSD_DATATYPEMASK)    >> D3DVSD_DATATYPESHIFT);
214       DWORD regout = ((token & D3DVSD_VERTEXREGMASK)   >> D3DVSD_VERTEXREGSHIFT);
215       DWORD regin  = ((token & D3DVSD_VERTEXREGINMASK) >> D3DVSD_VERTEXREGINSHIFT);
216       TRACE(" 0x%08x TESSNORMAL(%s, %s) as %s\n", token, VertexDecl8_Registers[regin], VertexDecl8_Registers[regout], VertexDecl8_DataTypes[type]);
217     }
218     break;
219   case D3DVSD_TOKEN_CONSTMEM:
220     {
221       DWORD count        = ((token & D3DVSD_CONSTCOUNTMASK)   >> D3DVSD_CONSTCOUNTSHIFT);
222       tokenlen = (4 * count) + 1;
223     }
224     break;
225   case D3DVSD_TOKEN_EXT:
226     {
227       DWORD count   = ((token & D3DVSD_CONSTCOUNTMASK) >> D3DVSD_CONSTCOUNTSHIFT);
228       DWORD extinfo = ((token & D3DVSD_EXTINFOMASK)    >> D3DVSD_EXTINFOSHIFT);
229       TRACE(" 0x%08x EXT(%u, %u)\n", token, count, extinfo);
230       /* todo ... print extension */
231       tokenlen = count + 1;
232     }
233     break;
234   case D3DVSD_TOKEN_END:
235     TRACE(" 0x%08x END()\n", token);
236     break;
237   default:
238     TRACE(" 0x%08x UNKNOWN\n", token);
239     /* argg error */
240   }
241   return tokenlen;
242 }
243
244 /* structure used by the d3d8 to d3d9 conversion lookup table */
245 typedef struct _Decl8to9Lookup {
246     int usage;
247     int usageIndex;
248 } Decl8to9Lookup;
249
250 static HRESULT IWineD3DVertexDeclarationImpl_ParseDeclaration8(IWineD3DVertexDeclaration *iface, const DWORD *pDecl) {
251 IWineD3DVertexDeclarationImpl *This = (IWineD3DVertexDeclarationImpl *)iface;
252 #define MAKE_LOOKUP(_reg,_usage,_usageindex) decl8to9Lookup[_reg].usage = _usage; \
253                                                  decl8to9Lookup[_reg].usageIndex = _usageindex;
254     const DWORD* pToken = pDecl;
255     DWORD len = 0;
256     DWORD stream = 0;
257     DWORD token;
258     DWORD tokenlen;
259     DWORD tokentype;
260     DWORD nTokens = 0;
261     int offset    = 0;
262
263     WINED3DVERTEXELEMENT convToW[128];
264 /* TODO: find out where rhw (or positionT) is for declaration8 */
265     Decl8to9Lookup decl8to9Lookup[MAX_D3DVSDE];
266     MAKE_LOOKUP(D3DVSDE_POSITION,     D3DDECLUSAGE_POSITION, 0);
267     MAKE_LOOKUP(D3DVSDE_POSITION2,    D3DDECLUSAGE_POSITION, 1);
268     MAKE_LOOKUP(D3DVSDE_BLENDWEIGHT,  D3DDECLUSAGE_BLENDWEIGHT, 0);
269     MAKE_LOOKUP(D3DVSDE_BLENDINDICES, D3DDECLUSAGE_BLENDINDICES, 0);
270     MAKE_LOOKUP(D3DVSDE_NORMAL,       D3DDECLUSAGE_NORMAL, 0);
271     MAKE_LOOKUP(D3DVSDE_NORMAL2,      D3DDECLUSAGE_NORMAL, 1);
272     MAKE_LOOKUP(D3DVSDE_DIFFUSE,      D3DDECLUSAGE_COLOR, 0);
273     MAKE_LOOKUP(D3DVSDE_SPECULAR,     D3DDECLUSAGE_COLOR, 1);
274     MAKE_LOOKUP(D3DVSDE_TEXCOORD0,    D3DDECLUSAGE_TEXCOORD, 0);
275     MAKE_LOOKUP(D3DVSDE_TEXCOORD1,    D3DDECLUSAGE_TEXCOORD, 1);
276     MAKE_LOOKUP(D3DVSDE_TEXCOORD2,    D3DDECLUSAGE_TEXCOORD, 2);
277     MAKE_LOOKUP(D3DVSDE_TEXCOORD3,    D3DDECLUSAGE_TEXCOORD, 3);
278     MAKE_LOOKUP(D3DVSDE_TEXCOORD4,    D3DDECLUSAGE_TEXCOORD, 4);
279     MAKE_LOOKUP(D3DVSDE_TEXCOORD5,    D3DDECLUSAGE_TEXCOORD, 5);
280     MAKE_LOOKUP(D3DVSDE_TEXCOORD6,    D3DDECLUSAGE_TEXCOORD, 6);
281     MAKE_LOOKUP(D3DVSDE_TEXCOORD7,    D3DDECLUSAGE_TEXCOORD, 7);
282     MAKE_LOOKUP(D3DVSDE_PSIZE,        D3DDECLUSAGE_PSIZE, 0); 
283
284     #undef MAKE_LOOKUP
285     TRACE("(%p) :  pDecl(%p)\n", This, pDecl);
286     /* Convert from a directx* declaration into a directx9 one, so we only have to deal with one type of declaration everywhere else */
287     while (D3DVSD_END() != *pToken) {
288         token = *pToken;
289         tokenlen = IWineD3DVertexDeclarationImpl_ParseToken8(pToken);
290         tokentype = ((token & D3DVSD_TOKENTYPEMASK) >> D3DVSD_TOKENTYPESHIFT);
291
292         if (D3DVSD_TOKEN_STREAM == tokentype && 0 == (D3DVSD_STREAMTESSMASK & token)) {
293             /**
294             * how really works streams,
295             *  in DolphinVS dx8 dsk sample they seems to decal reg numbers !!!
296             */
297             stream = ((token & D3DVSD_STREAMNUMBERMASK) >> D3DVSD_STREAMNUMBERSHIFT);
298             offset = 0;
299
300         } else if (D3DVSD_TOKEN_STREAMDATA == tokentype && 0 == (0x10000000 & tokentype)) {
301             DWORD type = ((token & D3DVSD_DATATYPEMASK)  >> D3DVSD_DATATYPESHIFT);
302             DWORD reg  = ((token & D3DVSD_VERTEXREGMASK) >> D3DVSD_VERTEXREGSHIFT);
303
304             convToW[nTokens].Stream     = stream;
305             convToW[nTokens].Method     = D3DDECLMETHOD_DEFAULT;
306             convToW[nTokens].Usage      = decl8to9Lookup[reg].usage;
307             convToW[nTokens].UsageIndex = decl8to9Lookup[reg].usageIndex;
308             convToW[nTokens].Type       = type;
309             convToW[nTokens].Offset     = offset;
310             convToW[nTokens].Reg        = reg;
311             TRACE("Adding element %d:\n", nTokens);
312             dump_wined3dvertexelement(&convToW[nTokens]);
313             offset += glTypeLookup[type].size * glTypeLookup[type].typesize;
314             ++nTokens;
315         } else if (D3DVSD_TOKEN_STREAMDATA == tokentype &&  0x10000000 & tokentype ) {
316              TRACE(" 0x%08x SKIP(%u)\n", tokentype, ((tokentype & D3DVSD_SKIPCOUNTMASK) >> D3DVSD_SKIPCOUNTSHIFT));
317              offset += sizeof(DWORD) * ((tokentype & D3DVSD_SKIPCOUNTMASK) >> D3DVSD_SKIPCOUNTSHIFT);
318         } else if (D3DVSD_TOKEN_CONSTMEM  == tokentype) {
319             DWORD i;
320             DWORD count        = ((token & D3DVSD_CONSTCOUNTMASK)   >> D3DVSD_CONSTCOUNTSHIFT);
321             DWORD constaddress = ((token & D3DVSD_CONSTADDRESSMASK) >> D3DVSD_CONSTADDRESSSHIFT);
322             if (This->constants == NULL ) {
323                 This->constants = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 
324                         ((IWineD3DImpl*)This->wineD3DDevice->wineD3D)->gl_info.max_vshader_constantsF * 4 * sizeof(float));
325             }
326             TRACE(" 0x%08x CONST(%u, %u)\n", token, constaddress, count);
327             for (i = 0; i < count; ++i) {
328                 TRACE("        c[%u] = (0x%08x, 0x%08x, 0x%08x, 0x%08x)\n",
329                     constaddress,
330                     *pToken,
331                     *(pToken + 1),
332                     *(pToken + 2),
333                     *(pToken + 3));
334
335                 This->constants[constaddress * 4] = *(const float*) (pToken+ i * 4 + 1);
336                 This->constants[constaddress * 4 + 1] = *(const float *)(pToken + i * 4 + 2);
337                 This->constants[constaddress * 4 + 2] = *(const float *)(pToken + i * 4 + 3);
338                 This->constants[constaddress * 4 + 3] = *(const float *)(pToken + i * 4 + 4);
339                 FIXME("        c[%u] = (%8f, %8f, %8f, %8f)\n",
340                     constaddress,
341                     *(const float*) (pToken+ i * 4 + 1),
342                     *(const float*) (pToken + i * 4 + 2),
343                     *(const float*) (pToken + i * 4 +3),
344                     *(const float*) (pToken + i * 4 + 4));
345                 ++constaddress;
346             }
347         }
348
349         len += tokenlen;
350         pToken += tokenlen;
351     }
352
353   /* here D3DVSD_END() */
354   len +=  IWineD3DVertexDeclarationImpl_ParseToken8(pToken);
355
356   convToW[nTokens].Stream = 0xFF;
357   convToW[nTokens].Type = D3DDECLTYPE_UNUSED;
358   ++nTokens;
359
360   /* compute size */
361   This->declaration8Length = len * sizeof(DWORD);
362   /* copy the declaration */
363   This->pDeclaration8 = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->declaration8Length);
364   memcpy(This->pDeclaration8, pDecl, This->declaration8Length);
365
366   /* compute convToW size */
367   This->declarationWNumElements = nTokens;
368   /* copy the convTo9 declaration */
369   This->pDeclarationWine = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, nTokens * sizeof(WINED3DVERTEXELEMENT));
370   memcpy(This->pDeclarationWine, convToW, nTokens * sizeof(WINED3DVERTEXELEMENT));
371
372   /* returns */
373   return WINED3D_OK;
374 }
375
376 static HRESULT IWineD3DVertexDeclarationImpl_ParseDeclaration9(IWineD3DVertexDeclaration* iface, const D3DVERTEXELEMENT9* pDecl) {
377     IWineD3DVertexDeclarationImpl *This = (IWineD3DVertexDeclarationImpl *)iface;
378     const D3DVERTEXELEMENT9* pToken = pDecl;
379     int i;
380
381     TRACE("(%p) :  pDecl(%p)\n", This, pDecl);
382
383     This->declaration9NumElements = 1;
384     for(pToken = pDecl;0xFF != pToken->Stream && This->declaration9NumElements < 128 ; pToken++) This->declaration9NumElements++;
385
386     if (This->declaration9NumElements == 128) {
387         FIXME("?(%p) Error parsing vertex declaration\n", This);
388         return WINED3DERR_INVALIDCALL;
389     }
390
391
392     /* copy the declaration */
393     This->pDeclaration9 = HeapAlloc(GetProcessHeap(), 0, This->declaration9NumElements * sizeof(D3DVERTEXELEMENT9));
394     memcpy(This->pDeclaration9, pDecl, This->declaration9NumElements * sizeof(D3DVERTEXELEMENT9));
395
396     /* copy to wine style declaration */
397     This->pDeclarationWine = HeapAlloc(GetProcessHeap(), 0, This->declaration9NumElements * sizeof(WINED3DVERTEXELEMENT));
398     for(i = 0; i < This->declaration9NumElements; ++i) {
399         memcpy(This->pDeclarationWine + i, This->pDeclaration9 + i, sizeof(D3DVERTEXELEMENT9));
400         This->pDeclarationWine[i].Reg = -1;
401         TRACE("Adding element %d:\n", i);
402         dump_wined3dvertexelement(&This->pDeclarationWine[i]);
403     }
404
405     This->declarationWNumElements = This->declaration9NumElements;
406
407   return WINED3D_OK;
408 }
409
410 /* *******************************************
411    IWineD3DVertexDeclaration IUnknown parts follow
412    ******************************************* */
413 static HRESULT WINAPI IWineD3DVertexDeclarationImpl_QueryInterface(IWineD3DVertexDeclaration *iface, REFIID riid, LPVOID *ppobj)
414 {
415     IWineD3DVertexDeclarationImpl *This = (IWineD3DVertexDeclarationImpl *)iface;
416     TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
417     if (IsEqualGUID(riid, &IID_IUnknown)
418         || IsEqualGUID(riid, &IID_IWineD3DBase)
419         || IsEqualGUID(riid, &IID_IWineD3DVertexDeclaration)){
420         IUnknown_AddRef(iface);
421         *ppobj = This;
422         return S_OK;
423     }
424     *ppobj = NULL;
425     return E_NOINTERFACE;
426 }
427
428 static ULONG WINAPI IWineD3DVertexDeclarationImpl_AddRef(IWineD3DVertexDeclaration *iface) {
429     IWineD3DVertexDeclarationImpl *This = (IWineD3DVertexDeclarationImpl *)iface;
430     TRACE("(%p) : AddRef increasing from %d\n", This, This->ref);
431     return InterlockedIncrement(&This->ref);
432 }
433
434 static ULONG WINAPI IWineD3DVertexDeclarationImpl_Release(IWineD3DVertexDeclaration *iface) {
435     IWineD3DVertexDeclarationImpl *This = (IWineD3DVertexDeclarationImpl *)iface;
436     ULONG ref;
437     TRACE("(%p) : Releasing from %d\n", This, This->ref);
438     ref = InterlockedDecrement(&This->ref);
439     if (ref == 0) {
440         HeapFree(GetProcessHeap(), 0, This->pDeclaration8);
441         HeapFree(GetProcessHeap(), 0, This->pDeclaration9);
442         HeapFree(GetProcessHeap(), 0, This->pDeclarationWine);
443         HeapFree(GetProcessHeap(), 0, This->constants);
444         HeapFree(GetProcessHeap(), 0, This);
445     }
446     return ref;
447 }
448
449 /* *******************************************
450    IWineD3DVertexDeclaration parts follow
451    ******************************************* */
452
453 static HRESULT WINAPI IWineD3DVertexDeclarationImpl_GetParent(IWineD3DVertexDeclaration *iface, IUnknown** parent){
454     IWineD3DVertexDeclarationImpl *This = (IWineD3DVertexDeclarationImpl *)iface;
455
456     *parent= This->parent;
457     IUnknown_AddRef(*parent);
458     TRACE("(%p) : returning %p\n", This, *parent);
459     return WINED3D_OK;
460 }
461
462 static HRESULT WINAPI IWineD3DVertexDeclarationImpl_GetDevice(IWineD3DVertexDeclaration *iface, IWineD3DDevice** ppDevice) {
463     IWineD3DVertexDeclarationImpl *This = (IWineD3DVertexDeclarationImpl *)iface;
464     TRACE("(%p) : returning %p\n", This, This->wineD3DDevice);
465
466     *ppDevice = (IWineD3DDevice *) This->wineD3DDevice;
467     IWineD3DDevice_AddRef(*ppDevice);
468
469     return WINED3D_OK;
470 }
471
472 static HRESULT WINAPI IWineD3DVertexDeclarationImpl_GetDeclaration8(IWineD3DVertexDeclaration* iface, DWORD* pData, DWORD* pSizeOfData) {
473     IWineD3DVertexDeclarationImpl *This = (IWineD3DVertexDeclarationImpl *)iface;
474     if (NULL == pData) {
475         *pSizeOfData = This->declaration8Length;
476         return WINED3D_OK;
477     }
478
479     /* The Incredibles and Teenage Mutant Ninja Turtles require this in d3d9 for NumElements == 0,
480     TODO: this needs to be tested against windows */
481     if(*pSizeOfData == 0) {
482         TRACE("(%p) : Requested the vertex declaration without specifying the size of the return buffer\n", This);
483         *pSizeOfData = This->declaration8Length;
484         memcpy(pData, This->pDeclaration8, This->declaration8Length);
485         return WINED3D_OK;
486     }
487
488     if (*pSizeOfData < This->declaration8Length) {
489         FIXME("(%p) : Returning WINED3DERR_MOREDATA numElements %d expected %d\n", iface, *pSizeOfData, This->declaration8Length);
490         *pSizeOfData = This->declaration8Length;
491         return WINED3DERR_MOREDATA;
492     }
493     TRACE("(%p) : GetVertexDeclaration8 copying to %p\n", This, pData);
494     memcpy(pData, This->pDeclaration8, This->declaration8Length);
495     return WINED3D_OK;
496 }
497
498 static HRESULT WINAPI IWineD3DVertexDeclarationImpl_GetDeclaration9(IWineD3DVertexDeclaration* iface,  D3DVERTEXELEMENT9* pData, DWORD* pNumElements) {
499     IWineD3DVertexDeclarationImpl *This = (IWineD3DVertexDeclarationImpl *)iface;
500
501     TRACE("(This %p, pData %p, pNumElements %p)\n", This, pData, pNumElements);
502
503     TRACE("Setting *pNumElements to %d\n", This->declaration9NumElements);
504     *pNumElements = This->declaration9NumElements;
505
506     /* Passing a NULL pData is used to just retrieve the number of elements */
507     if (!pData) {
508         TRACE("NULL pData passed. Returning WINED3D_OK.\n");
509         return WINED3D_OK;
510     }
511
512     TRACE("Copying %p to %p\n", This->pDeclaration9, pData);
513     memcpy(pData, This->pDeclaration9, This->declaration9NumElements * sizeof(*pData));
514     return WINED3D_OK;
515 }
516
517 static HRESULT WINAPI IWineD3DVertexDeclarationImpl_GetDeclaration(IWineD3DVertexDeclaration *iface, VOID *pData, DWORD *pSize) {
518     IWineD3DVertexDeclarationImpl *This = (IWineD3DVertexDeclarationImpl *)iface;
519     HRESULT hr = WINED3D_OK;
520
521     TRACE("(%p) : d3d version %d r\n", This, ((IWineD3DImpl *)This->wineD3DDevice->wineD3D)->dxVersion);
522     switch (((IWineD3DImpl *)This->wineD3DDevice->wineD3D)->dxVersion) {
523     case 8:
524         hr = IWineD3DVertexDeclarationImpl_GetDeclaration8(iface, (DWORD *)pData, pSize);
525     break;
526     case 9:
527         hr = IWineD3DVertexDeclarationImpl_GetDeclaration9(iface, (D3DVERTEXELEMENT9 *)pData, pSize);
528     break;
529     default:
530         FIXME("(%p)  : Unsupported DirectX version %u\n", This, ((IWineD3DImpl *)This->wineD3DDevice->wineD3D)->dxVersion);
531     break;
532     }
533     return hr;
534 }
535
536 static HRESULT WINAPI IWineD3DVertexDeclarationImpl_SetDeclaration(IWineD3DVertexDeclaration *iface, VOID *pDecl) {
537     IWineD3DVertexDeclarationImpl *This = (IWineD3DVertexDeclarationImpl *)iface;
538     HRESULT hr = WINED3D_OK;
539
540     TRACE("(%p) : d3d version %d\n", This, ((IWineD3DImpl *)This->wineD3DDevice->wineD3D)->dxVersion);
541     switch (((IWineD3DImpl *)This->wineD3DDevice->wineD3D)->dxVersion) {
542     case 8:
543         TRACE("Parsing declaration 8\n");
544         hr = IWineD3DVertexDeclarationImpl_ParseDeclaration8(iface, (CONST DWORD *)pDecl);
545     break;
546     case 9:
547         TRACE("Parsing declaration 9\n");
548         hr = IWineD3DVertexDeclarationImpl_ParseDeclaration9(iface, (CONST D3DVERTEXELEMENT9 *)pDecl);
549     break;
550     default:
551         FIXME("(%p)  : Unsupported DirectX version %u\n", This, ((IWineD3DImpl *)This->wineD3DDevice->wineD3D)->dxVersion);
552     break;
553     }
554     TRACE("Returning\n");
555     return hr;
556 }
557
558 const IWineD3DVertexDeclarationVtbl IWineD3DVertexDeclaration_Vtbl =
559 {
560     /* IUnknown */
561     IWineD3DVertexDeclarationImpl_QueryInterface,
562     IWineD3DVertexDeclarationImpl_AddRef,
563     IWineD3DVertexDeclarationImpl_Release,
564     /* IWineD3DVertexDeclaration */
565     IWineD3DVertexDeclarationImpl_GetParent,
566     IWineD3DVertexDeclarationImpl_GetDevice,
567     IWineD3DVertexDeclarationImpl_GetDeclaration,
568     IWineD3DVertexDeclarationImpl_SetDeclaration
569 };