Added MIME type checking to ResProtocol::Start.
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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* 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* 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 DWORD IWineD3DVertexDeclarationImpl_ParseToken8(const DWORD* pToken) {
174   const DWORD token = *pToken;
175   DWORD tokenlen = 1;
176
177   switch ((token & D3DVSD_TOKENTYPEMASK) >> D3DVSD_TOKENTYPESHIFT) { /* maybe a macro to inverse ... */
178   case D3DVSD_TOKEN_NOP:
179     TRACE(" 0x%08lx NOP()\n", token);
180     break;
181   case D3DVSD_TOKEN_STREAM:
182     if (token & D3DVSD_STREAMTESSMASK) {
183       TRACE(" 0x%08lx STREAM_TESS()\n", token);
184     } else {
185       TRACE(" 0x%08lx STREAM(%lu)\n", token, ((token & D3DVSD_STREAMNUMBERMASK) >> D3DVSD_STREAMNUMBERSHIFT));
186     }
187     break;
188   case D3DVSD_TOKEN_STREAMDATA:
189     if (token & 0x10000000) {
190       TRACE(" 0x%08lx SKIP(%lu)\n", token, ((token & D3DVSD_SKIPCOUNTMASK) >> D3DVSD_SKIPCOUNTSHIFT));
191     } else {
192       DWORD type = ((token & D3DVSD_DATATYPEMASK)  >> D3DVSD_DATATYPESHIFT);
193       DWORD reg  = ((token & D3DVSD_VERTEXREGMASK) >> D3DVSD_VERTEXREGSHIFT);
194       TRACE(" 0x%08lx REG(%s, %s)\n", token, VertexDecl8_Registers[reg], VertexDecl8_DataTypes[type]);
195     }
196     break;
197   case D3DVSD_TOKEN_TESSELLATOR:
198     if (token & 0x10000000) {
199       DWORD type = ((token & D3DVSD_DATATYPEMASK)  >> D3DVSD_DATATYPESHIFT);
200       DWORD reg  = ((token & D3DVSD_VERTEXREGMASK) >> D3DVSD_VERTEXREGSHIFT);
201       TRACE(" 0x%08lx TESSUV(%s) as %s\n", token, VertexDecl8_Registers[reg], VertexDecl8_DataTypes[type]);
202     } else {
203       DWORD type   = ((token & D3DVSD_DATATYPEMASK)    >> D3DVSD_DATATYPESHIFT);
204       DWORD regout = ((token & D3DVSD_VERTEXREGMASK)   >> D3DVSD_VERTEXREGSHIFT);
205       DWORD regin  = ((token & D3DVSD_VERTEXREGINMASK) >> D3DVSD_VERTEXREGINSHIFT);
206       TRACE(" 0x%08lx TESSNORMAL(%s, %s) as %s\n", token, VertexDecl8_Registers[regin], VertexDecl8_Registers[regout], VertexDecl8_DataTypes[type]);
207     }
208     break;
209   case D3DVSD_TOKEN_CONSTMEM:
210     {
211       DWORD i;
212       DWORD count        = ((token & D3DVSD_CONSTCOUNTMASK)   >> D3DVSD_CONSTCOUNTSHIFT);
213       DWORD constaddress = ((token & D3DVSD_CONSTADDRESSMASK) >> D3DVSD_CONSTADDRESSSHIFT);
214       TRACE(" 0x%08lx CONST(%lu, %lu)\n", token, constaddress, count);
215       ++pToken;
216       for (i = 0; i < count; ++i) {
217 #if 0
218         TRACE("        c[%lu] = (0x%08lx, 0x%08lx, 0x%08lx, 0x%08lx)\n",
219             constaddress,
220             *pToken,
221             *(pToken + 1),
222             *(pToken + 2),
223             *(pToken + 3));
224 #endif
225         TRACE("        c[%lu] = (%8f, %8f, %8f, %8f)\n",
226             constaddress,
227             *(const float*) pToken,
228             *(const float*) (pToken + 1),
229             *(const float*) (pToken + 2),
230             *(const float*) (pToken + 3));
231         pToken += 4;
232         ++constaddress;
233       }
234       tokenlen = (4 * count) + 1;
235     }
236     break;
237   case D3DVSD_TOKEN_EXT:
238     {
239       DWORD count   = ((token & D3DVSD_CONSTCOUNTMASK) >> D3DVSD_CONSTCOUNTSHIFT);
240       DWORD extinfo = ((token & D3DVSD_EXTINFOMASK)    >> D3DVSD_EXTINFOSHIFT);
241       TRACE(" 0x%08lx EXT(%lu, %lu)\n", token, count, extinfo);
242       /* todo ... print extension */
243       tokenlen = count + 1;
244     }
245     break;
246   case D3DVSD_TOKEN_END:
247     TRACE(" 0x%08lx END()\n", token);
248     break;
249   default:
250     TRACE(" 0x%08lx UNKNOWN\n", token);
251     /* argg error */
252   }
253   return tokenlen;
254 }
255
256 /* structure used by the d3d8 to d3d9 conversion lookup table */
257 typedef struct _Decl8to9Lookup {
258     int usage;
259     int usageIndex;
260 } Decl8to9Lookup;
261
262 static HRESULT IWineD3DVertexDeclarationImpl_ParseDeclaration8(IWineD3DVertexDeclaration *iface, const DWORD *pDecl) {
263 IWineD3DVertexDeclarationImpl *This = (IWineD3DVertexDeclarationImpl *)iface;
264 #define MAKE_LOOKUP(_reg,_usage,_usageindex) decl8to9Lookup[_reg].usage = _usage; \
265                                                  decl8to9Lookup[_reg].usageIndex = _usageindex;
266     const DWORD* pToken = pDecl;
267     DWORD len = 0;
268     DWORD stream = 0;
269     DWORD token;
270     DWORD tokenlen;
271     DWORD tokentype;
272     DWORD nTokens = 0;
273     D3DVERTEXELEMENT9 convTo9[128];
274 /* TODO: find out where rhw (or positionT) is for declaration8 */
275     Decl8to9Lookup decl8to9Lookup[MAX_D3DVSDE];
276     MAKE_LOOKUP(D3DVSDE_POSITION,     D3DDECLUSAGE_POSITION, 0);
277     MAKE_LOOKUP(D3DVSDE_POSITION2,    D3DDECLUSAGE_POSITION, 1);
278     MAKE_LOOKUP(D3DVSDE_BLENDWEIGHT,  D3DDECLUSAGE_BLENDWEIGHT, 0);
279     MAKE_LOOKUP(D3DVSDE_BLENDINDICES, D3DDECLUSAGE_BLENDWEIGHT, 0);
280     MAKE_LOOKUP(D3DVSDE_NORMAL,       D3DDECLUSAGE_NORMAL, 0);
281     MAKE_LOOKUP(D3DVSDE_NORMAL2,      D3DDECLUSAGE_NORMAL, 1);
282     MAKE_LOOKUP(D3DVSDE_DIFFUSE,      D3DDECLUSAGE_COLOR, 0);
283     MAKE_LOOKUP(D3DVSDE_SPECULAR,     D3DDECLUSAGE_COLOR, 1);
284     MAKE_LOOKUP(D3DVSDE_TEXCOORD0,    D3DDECLUSAGE_BLENDWEIGHT, 0);
285     MAKE_LOOKUP(D3DVSDE_TEXCOORD1,    D3DDECLUSAGE_BLENDWEIGHT, 1);
286     MAKE_LOOKUP(D3DVSDE_TEXCOORD2,    D3DDECLUSAGE_BLENDWEIGHT, 2);
287     MAKE_LOOKUP(D3DVSDE_TEXCOORD3,    D3DDECLUSAGE_BLENDWEIGHT, 3);
288     MAKE_LOOKUP(D3DVSDE_TEXCOORD4,    D3DDECLUSAGE_BLENDWEIGHT, 4);
289     MAKE_LOOKUP(D3DVSDE_TEXCOORD5,    D3DDECLUSAGE_BLENDWEIGHT, 5);
290     MAKE_LOOKUP(D3DVSDE_TEXCOORD6,    D3DDECLUSAGE_BLENDWEIGHT, 6);
291     MAKE_LOOKUP(D3DVSDE_TEXCOORD7,    D3DDECLUSAGE_BLENDWEIGHT, 7);
292
293     #undef MAKE_LOOKUP
294     TRACE("(%p) :  pDecl(%p)\n", This, pDecl);
295     /* Convert from a directx* declaration into a directx9 one, so we only have to deal with one type of declaration everywhere else */
296     while (D3DVSD_END() != *pToken) {
297         token = *pToken;
298         tokenlen = IWineD3DVertexDeclarationImpl_ParseToken8(pToken);
299         tokentype = ((token & D3DVSD_TOKENTYPEMASK) >> D3DVSD_TOKENTYPESHIFT);
300
301         if (D3DVSD_TOKEN_STREAM == tokentype && 0 == (D3DVSD_STREAMTESSMASK & token)) {
302         /**
303         * how really works streams,
304         *  in DolphinVS dx8 dsk sample they seems to decal reg numbers !!!
305         */
306         stream = ((token & D3DVSD_STREAMNUMBERMASK) >> D3DVSD_STREAMNUMBERSHIFT);
307
308         } else if (D3DVSD_TOKEN_STREAMDATA == tokentype && 0 == (0x10000000 & tokentype)) {
309             DWORD type = ((token & D3DVSD_DATATYPEMASK)  >> D3DVSD_DATATYPESHIFT);
310             DWORD reg  = ((token & D3DVSD_VERTEXREGMASK) >> D3DVSD_VERTEXREGSHIFT);
311
312             convTo9[nTokens].Stream     = stream;
313             convTo9[nTokens].Method     = D3DDECLMETHOD_DEFAULT;
314             convTo9[nTokens].Usage      = decl8to9Lookup[reg].usage;
315             convTo9[nTokens].UsageIndex = decl8to9Lookup[reg].usageIndex;
316             convTo9[nTokens].Type       = type;
317             ++nTokens;
318         }/* TODO: Constants. */
319         len += tokenlen;
320         pToken += tokenlen;
321     }
322
323   /* here D3DVSD_END() */
324   len +=  IWineD3DVertexDeclarationImpl_ParseToken8(pToken);
325
326   convTo9[nTokens].Stream = 0xFF;
327   convTo9[nTokens].Type = D3DDECLTYPE_UNUSED;
328
329   /* compute size */
330   This->declaration8Length = len * sizeof(DWORD);
331   /* copy the declaration */
332   This->pDeclaration8 = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->declaration8Length);
333   memcpy(This->pDeclaration8, pDecl, This->declaration8Length);
334
335   /* compute convTo9 size */
336   This->declaration9NumElements = nTokens;
337   /* copy the convTo9 declaration */
338   This->pDeclaration9 = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, nTokens * sizeof(D3DVERTEXELEMENT9));
339   memcpy(This->pDeclaration9, convTo9, nTokens * sizeof(D3DVERTEXELEMENT9));
340 #if 0 /* TODO: This looks like overkill so I've removed it. */
341   {
342     D3DVERTEXELEMENT9* pIt = This->pDeclaration9;
343     TRACE("dumping of D3D9 Conversion:\n");
344     while (0xFF != pIt->Stream) {
345       IWineD3DVertexDeclarationImpl_ParseToken9(pIt);
346       ++pIt;
347     }
348     IWineD3DVertexDeclarationImpl_ParseToken9(pIt);
349   }
350 #endif
351   /* returns */
352   return D3D_OK;
353 }
354
355 static HRESULT IWineD3DVertexDeclarationImpl_ParseDeclaration9(IWineD3DVertexDeclaration* iface, const D3DVERTEXELEMENT9* pDecl) {
356     IWineD3DVertexDeclarationImpl *This = (IWineD3DVertexDeclarationImpl *)iface;
357     const D3DVERTEXELEMENT9* pToken = pDecl;
358
359     TRACE("(%p) :  pDecl(%p)\n", This, pDecl);
360
361     This->declaration9NumElements = 1;
362     for(pToken = pDecl;0xFF != pToken->Stream && This->declaration9NumElements < 128 ; pToken++) This->declaration9NumElements++;
363
364     if (This->declaration9NumElements == 128) {
365         FIXME("?(%p) Error parsing vertex declaration\n", This);
366         return D3DERR_INVALIDCALL;
367     }
368
369
370     /* copy the declaration */
371     This->pDeclaration9 = HeapAlloc(GetProcessHeap(), 0, This->declaration9NumElements * sizeof(D3DVERTEXELEMENT9));
372     memcpy(This->pDeclaration9, pDecl, This->declaration9NumElements * sizeof(D3DVERTEXELEMENT9));
373
374
375   return D3D_OK;
376 }
377
378 /* *******************************************
379    IWineD3DVertexDeclaration IUnknown parts follow
380    ******************************************* */
381 HRESULT WINAPI IWineD3DVertexDeclarationImpl_QueryInterface(IWineD3DVertexDeclaration *iface, REFIID riid, LPVOID *ppobj)
382 {
383     IWineD3DVertexDeclarationImpl *This = (IWineD3DVertexDeclarationImpl *)iface;
384     TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
385     if (IsEqualGUID(riid, &IID_IUnknown)
386         || IsEqualGUID(riid, &IID_IWineD3DVertexDeclaration)){
387         IUnknown_AddRef(iface);
388         *ppobj = This;
389         return D3D_OK;
390     }
391     return E_NOINTERFACE;
392 }
393
394 ULONG WINAPI IWineD3DVertexDeclarationImpl_AddRef(IWineD3DVertexDeclaration *iface) {
395     IWineD3DVertexDeclarationImpl *This = (IWineD3DVertexDeclarationImpl *)iface;
396     TRACE("(%p) : AddRef increasing from %ld\n", This, This->ref);
397     return InterlockedIncrement(&This->ref);
398 }
399
400 ULONG WINAPI IWineD3DVertexDeclarationImpl_Release(IWineD3DVertexDeclaration *iface) {
401     IWineD3DVertexDeclarationImpl *This = (IWineD3DVertexDeclarationImpl *)iface;
402     ULONG ref;
403     TRACE("(%p) : Releasing from %ld\n", This, This->ref);
404     ref = InterlockedDecrement(&This->ref);
405     if (ref == 0) {
406       HeapFree(GetProcessHeap(), 0, This->pDeclaration8);
407       HeapFree(GetProcessHeap(), 0, This->pDeclaration9);
408       HeapFree(GetProcessHeap(), 0, This);
409     }
410     return ref;
411 }
412
413 /* *******************************************
414    IWineD3DVertexDeclaration parts follow
415    ******************************************* */
416
417 HRESULT WINAPI IWineD3DVertexDeclarationImpl_GetParent(IWineD3DVertexDeclaration *iface, IUnknown** parent){
418     IWineD3DVertexDeclarationImpl *This = (IWineD3DVertexDeclarationImpl *)iface;
419
420     *parent= (IUnknown*) parent;
421     IUnknown_AddRef(*parent);
422     TRACE("(%p) : returning %p\n", This, *parent);
423     return D3D_OK;
424 }
425
426 HRESULT WINAPI IWineD3DVertexDeclarationImpl_GetDevice(IWineD3DVertexDeclaration *iface, IWineD3DDevice** ppDevice) {
427     IWineD3DVertexDeclarationImpl *This = (IWineD3DVertexDeclarationImpl *)iface;
428     TRACE("(%p) : returning %p\n", This, This->wineD3DDevice);
429
430     *ppDevice = (IWineD3DDevice *) This->wineD3DDevice;
431     IWineD3DDevice_AddRef(*ppDevice);
432
433     return D3D_OK;
434 }
435
436 static HRESULT WINAPI IWineD3DVertexDeclarationImpl_GetDeclaration8(IWineD3DVertexDeclaration* iface, DWORD* pData, DWORD* pSizeOfData) {
437     IWineD3DVertexDeclarationImpl *This = (IWineD3DVertexDeclarationImpl *)iface;
438     if (NULL == pData) {
439         *pSizeOfData = This->declaration8Length;
440         return D3D_OK;
441     }
442
443     /* The Incredibles and Teenage Mutant Ninja Turtles require this in d3d9 for NumElements == 0,
444     TODO: this needs to be tested against windows */
445     if(*pSizeOfData == 0) {
446         TRACE("(%p) : Requested the vertex declaration without specefying the size of the return buffer\n", This);
447         *pSizeOfData = This->declaration8Length;
448         memcpy(pData, This->pDeclaration8, This->declaration8Length);
449         return D3D_OK;
450     }
451
452     if (*pSizeOfData < This->declaration8Length) {
453         FIXME("(%p) : Returning D3DERR_MOREDATA numElements %ld expected %ld\n", iface, *pSizeOfData, This->declaration8Length);
454         *pSizeOfData = This->declaration8Length;
455         return D3DERR_MOREDATA;
456     }
457     TRACE("(%p) : GetVertexDeclaration8 copying to %p\n", This, pData);
458     memcpy(pData, This->pDeclaration8, This->declaration8Length);
459     return D3D_OK;
460 }
461
462 HRESULT WINAPI IWineD3DVertexDeclarationImpl_GetDeclaration9(IWineD3DVertexDeclaration* iface,  D3DVERTEXELEMENT9* pData, DWORD* pNumElements) {
463     IWineD3DVertexDeclarationImpl *This = (IWineD3DVertexDeclarationImpl *)iface;
464     if (NULL == pData) {
465         *pNumElements = This->declaration9NumElements;
466         TRACE("(%p) : Returning numElements %ld\n", iface, *pNumElements);
467         return D3D_OK;
468     }
469
470     /* The Incredibles and Teenage Mutant Ninja Turtles require this for NumElements == 0,
471     TODO: this needs to be tested against windows */
472     if(*pNumElements == 0) {
473         TRACE("(%p) : Requested the vertex declaration without specefying the size of the return buffer\n", This);
474         *pNumElements = This->declaration9NumElements;
475         memcpy(pData, This->pDeclaration9, *pNumElements * sizeof(*pData));
476         return D3D_OK;
477     }
478
479     /* just in case there's a similar problem to The Incredibles and Teenage Mutant Ninja Turtles without pNumElements = 0 */
480     if (*pNumElements < This->declaration9NumElements) {
481         *pNumElements = This->declaration9NumElements;
482         FIXME("(%p) : Returning D3DERR_MOREDATA numElements %ld expected %u\n", iface, *pNumElements, This->declaration9NumElements);
483         memcpy(pData, This->pDeclaration9, *pNumElements * sizeof(*pData));
484         return D3DERR_MOREDATA;
485     }
486     TRACE("(%p) : GetVertexDeclaration9 copying to %p\n", This, pData);
487     memcpy(pData, This->pDeclaration9, This->declaration9NumElements * sizeof(*pData));
488     return D3D_OK;
489 }
490
491 HRESULT WINAPI IWineD3DVertexDeclarationImpl_GetDeclaration(IWineD3DVertexDeclaration *iface, VOID *pData, DWORD *pSize) {
492     IWineD3DVertexDeclarationImpl *This = (IWineD3DVertexDeclarationImpl *)iface;
493     HRESULT hr = D3D_OK;
494
495     TRACE("(%p) : d3d version %d r\n", This, ((IWineD3DImpl *)This->wineD3DDevice->wineD3D)->dxVersion);
496     switch (((IWineD3DImpl *)This->wineD3DDevice->wineD3D)->dxVersion) {
497     case 8:
498         hr = IWineD3DVertexDeclarationImpl_GetDeclaration8(iface, (DWORD *)pData, pSize);
499     break;
500     case 9:
501         hr = IWineD3DVertexDeclarationImpl_GetDeclaration9(iface, (D3DVERTEXELEMENT9 *)pData, pSize);
502     break;
503     default:
504         FIXME("(%p)  : Unsupport DirectX version %u\n", This, ((IWineD3DImpl *)This->wineD3DDevice->wineD3D)->dxVersion);
505     break;
506     }
507     return hr;
508 }
509
510 HRESULT WINAPI IWineD3DVertexDeclarationImpl_SetDeclaration(IWineD3DVertexDeclaration *iface, VOID *pDecl) {
511     IWineD3DVertexDeclarationImpl *This = (IWineD3DVertexDeclarationImpl *)iface;
512     HRESULT hr = D3D_OK;
513
514     TRACE("(%p) : d3d version %d\n", This, ((IWineD3DImpl *)This->wineD3DDevice->wineD3D)->dxVersion);
515     switch (((IWineD3DImpl *)This->wineD3DDevice->wineD3D)->dxVersion) {
516     case 8:
517         TRACE("Parsing declatation 8\n");
518         hr = IWineD3DVertexDeclarationImpl_ParseDeclaration8(iface, (CONST DWORD *)pDecl);
519     break;
520     case 9:
521         FIXME("Parsing declatation 9\n");
522         hr = IWineD3DVertexDeclarationImpl_ParseDeclaration9(iface, (CONST D3DVERTEXELEMENT9 *)pDecl);
523     break;
524     default:
525         FIXME("(%p)  : Unsupport DirectX version %u\n", This, ((IWineD3DImpl *)This->wineD3DDevice->wineD3D)->dxVersion);
526     break;
527     }
528     TRACE("Returning\n");
529     return hr;
530 }
531
532 const IWineD3DVertexDeclarationVtbl IWineD3DVertexDeclaration_Vtbl =
533 {
534     /* IUnknown */
535     IWineD3DVertexDeclarationImpl_QueryInterface,
536     IWineD3DVertexDeclarationImpl_AddRef,
537     IWineD3DVertexDeclarationImpl_Release,
538     /* IWineD3DVertexDeclaration */
539     IWineD3DVertexDeclarationImpl_GetParent,
540     IWineD3DVertexDeclarationImpl_GetDevice,
541     IWineD3DVertexDeclarationImpl_GetDeclaration,
542     IWineD3DVertexDeclarationImpl_SetDeclaration
543 };