include: Use ULONG instead of unsigned long in ipexport.h.
[wine] / dlls / d3d8 / vertexdeclaration.c
1 /*
2  * IDirect3DVertexDeclaration8 implementation
3  *
4  * Copyright 2007 Henri Verbeet
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 /* IDirect3DVertexDeclaration8 is internal to our implementation.
22  * It's not visible in the API. */
23
24 #include "config.h"
25 #include "d3d8_private.h"
26
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d8);
28
29 /* IUnknown */
30 static HRESULT WINAPI IDirect3DVertexDeclaration8Impl_QueryInterface(IDirect3DVertexDeclaration8 *iface, REFIID riid, void **obj_ptr)
31 {
32     TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), obj_ptr);
33
34     if (IsEqualGUID(riid, &IID_IUnknown)
35             || IsEqualGUID(riid, &IID_IDirect3DVertexDeclaration8))
36     {
37         IUnknown_AddRef(iface);
38         *obj_ptr = iface;
39         return S_OK;
40     }
41
42     *obj_ptr = NULL;
43     return E_NOINTERFACE;
44 }
45
46 static ULONG WINAPI IDirect3DVertexDeclaration8Impl_AddRef(IDirect3DVertexDeclaration8 *iface)
47 {
48     IDirect3DVertexDeclaration8Impl *This = (IDirect3DVertexDeclaration8Impl *)iface;
49
50     ULONG ref_count = InterlockedIncrement(&This->ref_count);
51     TRACE("(%p) : AddRef increasing to %d\n", This, ref_count);
52
53     return ref_count;
54 }
55
56 static ULONG WINAPI IDirect3DVertexDeclaration8Impl_Release(IDirect3DVertexDeclaration8 *iface)
57 {
58     IDirect3DVertexDeclaration8Impl *This = (IDirect3DVertexDeclaration8Impl *)iface;
59
60     ULONG ref_count = InterlockedDecrement(&This->ref_count);
61     TRACE("(%p) : Releasing to %d\n", This, ref_count);
62
63     if (!ref_count) {
64         IWineD3DVertexDeclaration_Release(This->wined3d_vertex_declaration);
65         HeapFree(GetProcessHeap(), 0, This->elements);
66         HeapFree(GetProcessHeap(), 0, This);
67     }
68
69     return ref_count;
70 }
71
72 static const char *debug_d3dvsdt_type(D3DVSDT_TYPE d3dvsdt_type)
73 {
74     switch (d3dvsdt_type)
75     {
76 #define D3DVSDT_TYPE_TO_STR(u) case u: return #u
77         D3DVSDT_TYPE_TO_STR(D3DVSDT_FLOAT1);
78         D3DVSDT_TYPE_TO_STR(D3DVSDT_FLOAT2);
79         D3DVSDT_TYPE_TO_STR(D3DVSDT_FLOAT3);
80         D3DVSDT_TYPE_TO_STR(D3DVSDT_FLOAT4);
81         D3DVSDT_TYPE_TO_STR(D3DVSDT_D3DCOLOR);
82         D3DVSDT_TYPE_TO_STR(D3DVSDT_UBYTE4);
83         D3DVSDT_TYPE_TO_STR(D3DVSDT_SHORT2);
84         D3DVSDT_TYPE_TO_STR(D3DVSDT_SHORT4);
85 #undef D3DVSDT_TYPE_TO_STR
86         default:
87             FIXME("Unrecognized D3DVSDT_TYPE %#x\n", d3dvsdt_type);
88             return "unrecognized";
89     }
90 }
91
92 static const char *debug_d3dvsde_register(D3DVSDE_REGISTER d3dvsde_register)
93 {
94     switch (d3dvsde_register)
95     {
96 #define D3DVSDE_REGISTER_TO_STR(u) case u: return #u
97         D3DVSDE_REGISTER_TO_STR(D3DVSDE_POSITION);
98         D3DVSDE_REGISTER_TO_STR(D3DVSDE_BLENDWEIGHT);
99         D3DVSDE_REGISTER_TO_STR(D3DVSDE_BLENDINDICES);
100         D3DVSDE_REGISTER_TO_STR(D3DVSDE_NORMAL);
101         D3DVSDE_REGISTER_TO_STR(D3DVSDE_PSIZE);
102         D3DVSDE_REGISTER_TO_STR(D3DVSDE_DIFFUSE);
103         D3DVSDE_REGISTER_TO_STR(D3DVSDE_SPECULAR);
104         D3DVSDE_REGISTER_TO_STR(D3DVSDE_TEXCOORD0);
105         D3DVSDE_REGISTER_TO_STR(D3DVSDE_TEXCOORD1);
106         D3DVSDE_REGISTER_TO_STR(D3DVSDE_TEXCOORD2);
107         D3DVSDE_REGISTER_TO_STR(D3DVSDE_TEXCOORD3);
108         D3DVSDE_REGISTER_TO_STR(D3DVSDE_TEXCOORD4);
109         D3DVSDE_REGISTER_TO_STR(D3DVSDE_TEXCOORD5);
110         D3DVSDE_REGISTER_TO_STR(D3DVSDE_TEXCOORD6);
111         D3DVSDE_REGISTER_TO_STR(D3DVSDE_TEXCOORD7);
112         D3DVSDE_REGISTER_TO_STR(D3DVSDE_POSITION2);
113         D3DVSDE_REGISTER_TO_STR(D3DVSDE_NORMAL2);
114 #undef D3DVSDE_REGISTER_TO_STR
115         default:
116             FIXME("Unrecognized D3DVSDE_REGISTER %#x\n", d3dvsde_register);
117             return "unrecognized";
118     }
119 }
120
121 size_t parse_token(const DWORD* pToken)
122 {
123     const DWORD token = *pToken;
124     size_t tokenlen = 1;
125
126     switch ((token & D3DVSD_TOKENTYPEMASK) >> D3DVSD_TOKENTYPESHIFT) { /* maybe a macro to inverse ... */
127         case D3DVSD_TOKEN_NOP:
128             TRACE(" 0x%08x NOP()\n", token);
129             break;
130
131         case D3DVSD_TOKEN_STREAM:
132             if (token & D3DVSD_STREAMTESSMASK)
133             {
134                 TRACE(" 0x%08x STREAM_TESS()\n", token);
135             } else {
136                 TRACE(" 0x%08x STREAM(%u)\n", token, ((token & D3DVSD_STREAMNUMBERMASK) >> D3DVSD_STREAMNUMBERSHIFT));
137             }
138             break;
139
140         case D3DVSD_TOKEN_STREAMDATA:
141             if (token & 0x10000000)
142             {
143                 TRACE(" 0x%08x SKIP(%u)\n", token, ((token & D3DVSD_SKIPCOUNTMASK) >> D3DVSD_SKIPCOUNTSHIFT));
144             } else {
145                 DWORD type = ((token & D3DVSD_DATATYPEMASK)  >> D3DVSD_DATATYPESHIFT);
146                 DWORD reg = ((token & D3DVSD_VERTEXREGMASK) >> D3DVSD_VERTEXREGSHIFT);
147                 TRACE(" 0x%08x REG(%s, %s)\n", token, debug_d3dvsde_register(reg), debug_d3dvsdt_type(type));
148             }
149             break;
150
151         case D3DVSD_TOKEN_TESSELLATOR:
152             if (token & 0x10000000)
153             {
154                 DWORD type = ((token & D3DVSD_DATATYPEMASK)  >> D3DVSD_DATATYPESHIFT);
155                 DWORD reg = ((token & D3DVSD_VERTEXREGMASK) >> D3DVSD_VERTEXREGSHIFT);
156                 TRACE(" 0x%08x TESSUV(%s) as %s\n", token, debug_d3dvsde_register(reg), debug_d3dvsdt_type(type));
157             } else {
158                 DWORD type = ((token & D3DVSD_DATATYPEMASK)    >> D3DVSD_DATATYPESHIFT);
159                 DWORD regout = ((token & D3DVSD_VERTEXREGMASK)   >> D3DVSD_VERTEXREGSHIFT);
160                 DWORD regin = ((token & D3DVSD_VERTEXREGINMASK) >> D3DVSD_VERTEXREGINSHIFT);
161                 TRACE(" 0x%08x TESSNORMAL(%s, %s) as %s\n", token, debug_d3dvsde_register(regin),
162                         debug_d3dvsde_register(regout), debug_d3dvsdt_type(type));
163             }
164             break;
165
166         case D3DVSD_TOKEN_CONSTMEM:
167             {
168                 DWORD count = ((token & D3DVSD_CONSTCOUNTMASK)   >> D3DVSD_CONSTCOUNTSHIFT);
169                 tokenlen = (4 * count) + 1;
170             }
171             break;
172
173         case D3DVSD_TOKEN_EXT:
174             {
175                 DWORD count = ((token & D3DVSD_CONSTCOUNTMASK) >> D3DVSD_CONSTCOUNTSHIFT);
176                 DWORD extinfo = ((token & D3DVSD_EXTINFOMASK)    >> D3DVSD_EXTINFOSHIFT);
177                 TRACE(" 0x%08x EXT(%u, %u)\n", token, count, extinfo);
178                 /* todo ... print extension */
179                 tokenlen = count + 1;
180             }
181             break;
182
183         case D3DVSD_TOKEN_END:
184             TRACE(" 0x%08x END()\n", token);
185             break;
186
187         default:
188             TRACE(" 0x%08x UNKNOWN\n", token);
189             /* argg error */
190     }
191
192     return tokenlen;
193 }
194
195 void load_local_constants(const DWORD *d3d8_elements, IWineD3DVertexShader *wined3d_vertex_shader)
196 {
197     const DWORD *token = d3d8_elements;
198
199     while (*token != D3DVSD_END())
200     {
201         if (((*token & D3DVSD_TOKENTYPEMASK) >> D3DVSD_TOKENTYPESHIFT) == D3DVSD_TOKEN_CONSTMEM)
202         {
203             DWORD count = ((*token & D3DVSD_CONSTCOUNTMASK) >> D3DVSD_CONSTCOUNTSHIFT);
204             DWORD constant_idx = ((*token & D3DVSD_CONSTADDRESSMASK) >> D3DVSD_CONSTADDRESSSHIFT);
205             HRESULT hr;
206
207             if (TRACE_ON(d3d8))
208             {
209                 DWORD i;
210                 for (i = 0; i < count; ++i)
211                 {
212                     TRACE("c[%u] = (%8f, %8f, %8f, %8f)\n",
213                             constant_idx,
214                             *(const float *)(token + i * 4 + 1),
215                             *(const float *)(token + i * 4 + 2),
216                             *(const float *)(token + i * 4 + 3),
217                             *(const float *)(token + i * 4 + 4));
218                 }
219             }
220             hr = IWineD3DVertexShader_SetLocalConstantsF(wined3d_vertex_shader, constant_idx, (const float *)token+1, count);
221             if (FAILED(hr)) ERR("Failed setting shader constants\n");
222         }
223
224         token += parse_token(token);
225     }
226 }
227
228 /* NOTE: Make sure these are in the correct numerical order. (see /include/wined3d_types.h) */
229 static const size_t wined3d_type_sizes[WINED3DDECLTYPE_UNUSED] = {
230     /*WINED3DDECLTYPE_FLOAT1*/    1 * sizeof(float),
231     /*WINED3DDECLTYPE_FLOAT2*/    2 * sizeof(float),
232     /*WINED3DDECLTYPE_FLOAT3*/    3 * sizeof(float),
233     /*WINED3DDECLTYPE_FLOAT4*/    4 * sizeof(float),
234     /*WINED3DDECLTYPE_D3DCOLOR*/  4 * sizeof(BYTE),
235     /*WINED3DDECLTYPE_UBYTE4*/    4 * sizeof(BYTE),
236     /*WINED3DDECLTYPE_SHORT2*/    2 * sizeof(short int),
237     /*WINED3DDECLTYPE_SHORT4*/    4 * sizeof(short int),
238     /*WINED3DDECLTYPE_UBYTE4N*/   4 * sizeof(BYTE),
239     /*WINED3DDECLTYPE_SHORT2N*/   2 * sizeof(short int),
240     /*WINED3DDECLTYPE_SHORT4N*/   4 * sizeof(short int),
241     /*WINED3DDECLTYPE_USHORT2N*/  2 * sizeof(short int),
242     /*WINED3DDECLTYPE_USHORT4N*/  4 * sizeof(short int),
243     /*WINED3DDECLTYPE_UDEC3*/     3 * sizeof(short int),
244     /*WINED3DDECLTYPE_DEC3N*/     3 * sizeof(short int),
245     /*WINED3DDECLTYPE_FLOAT16_2*/ 2 * sizeof(short int),
246     /*WINED3DDECLTYPE_FLOAT16_4*/ 4 * sizeof(short int)
247 };
248
249 typedef struct {
250     BYTE usage;
251     BYTE usage_idx;
252 } wined3d_usage_t;
253
254 static const wined3d_usage_t wined3d_usage_lookup[] = {
255     /*D3DVSDE_POSITION*/     {WINED3DDECLUSAGE_POSITION,     0},
256     /*D3DVSDE_BLENDWEIGHT*/  {WINED3DDECLUSAGE_BLENDWEIGHT,  0},
257     /*D3DVSDE_BLENDINDICES*/ {WINED3DDECLUSAGE_BLENDINDICES, 0},
258     /*D3DVSDE_NORMAL*/       {WINED3DDECLUSAGE_NORMAL,       0},
259     /*D3DVSDE_PSIZE*/        {WINED3DDECLUSAGE_PSIZE,        0},
260     /*D3DVSDE_DIFFUSE*/      {WINED3DDECLUSAGE_COLOR,        0},
261     /*D3DVSDE_SPECULAR*/     {WINED3DDECLUSAGE_COLOR,        1},
262     /*D3DVSDE_TEXCOORD0*/    {WINED3DDECLUSAGE_TEXCOORD,     0},
263     /*D3DVSDE_TEXCOORD1*/    {WINED3DDECLUSAGE_TEXCOORD,     1},
264     /*D3DVSDE_TEXCOORD2*/    {WINED3DDECLUSAGE_TEXCOORD,     2},
265     /*D3DVSDE_TEXCOORD3*/    {WINED3DDECLUSAGE_TEXCOORD,     3},
266     /*D3DVSDE_TEXCOORD4*/    {WINED3DDECLUSAGE_TEXCOORD,     4},
267     /*D3DVSDE_TEXCOORD5*/    {WINED3DDECLUSAGE_TEXCOORD,     5},
268     /*D3DVSDE_TEXCOORD6*/    {WINED3DDECLUSAGE_TEXCOORD,     6},
269     /*D3DVSDE_TEXCOORD7*/    {WINED3DDECLUSAGE_TEXCOORD,     7},
270     /*D3DVSDE_POSITION2*/    {WINED3DDECLUSAGE_POSITION,     1},
271     /*D3DVSDE_NORMAL2*/      {WINED3DDECLUSAGE_NORMAL,       1},
272 };
273
274 /* TODO: find out where rhw (or positionT) is for declaration8 */
275 UINT convert_to_wined3d_declaration(const DWORD *d3d8_elements, DWORD *d3d8_elements_size, WINED3DVERTEXELEMENT **wined3d_elements)
276 {
277     const DWORD *token = d3d8_elements;
278     WINED3DVERTEXELEMENT *element;
279     D3DVSD_TOKENTYPE token_type;
280     unsigned int element_count = 0;
281     DWORD stream = 0;
282     int offset = 0;
283
284     TRACE("d3d8_elements %p, wined3d_elements %p\n", d3d8_elements, wined3d_elements);
285
286     /* 128 should be enough for anyone... */
287     *wined3d_elements = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 128 * sizeof(WINED3DVERTEXELEMENT));
288     while (D3DVSD_END() != *token)
289     {
290         token_type = ((*token & D3DVSD_TOKENTYPEMASK) >> D3DVSD_TOKENTYPESHIFT);
291
292         if (token_type == D3DVSD_TOKEN_STREAM && !(*token & D3DVSD_STREAMTESSMASK))
293         {
294             stream = ((*token & D3DVSD_STREAMNUMBERMASK) >> D3DVSD_STREAMNUMBERSHIFT);
295             offset = 0;
296         } else if (token_type == D3DVSD_TOKEN_STREAMDATA && !(token_type & 0x10000000)) {
297             DWORD type = ((*token & D3DVSD_DATATYPEMASK) >> D3DVSD_DATATYPESHIFT);
298             DWORD reg  = ((*token & D3DVSD_VERTEXREGMASK) >> D3DVSD_VERTEXREGSHIFT);
299
300             TRACE("Adding element %d:\n", element_count);
301
302             element = *wined3d_elements + element_count++;
303             element->Stream = stream;
304             element->Method = WINED3DDECLMETHOD_DEFAULT;
305             element->Usage = wined3d_usage_lookup[reg].usage;
306             element->UsageIndex = wined3d_usage_lookup[reg].usage_idx;
307             element->Type = type;
308             element->Offset = offset;
309             element->Reg = reg;
310
311             offset += wined3d_type_sizes[type];
312         } else if (token_type == D3DVSD_TOKEN_STREAMDATA && (token_type & 0x10000000)) {
313             TRACE(" 0x%08x SKIP(%u)\n", token_type, ((token_type & D3DVSD_SKIPCOUNTMASK) >> D3DVSD_SKIPCOUNTSHIFT));
314             offset += sizeof(DWORD) * ((token_type & D3DVSD_SKIPCOUNTMASK) >> D3DVSD_SKIPCOUNTSHIFT);
315         }
316
317         if (element_count >= 127) {
318             ERR("More than 127 elements?\n");
319             break;
320         }
321
322         token += parse_token(token);
323     }
324
325     /* END */
326     element = *wined3d_elements + element_count++;
327     element->Stream = 0xFF;
328     element->Type = WINED3DDECLTYPE_UNUSED;
329
330     *d3d8_elements_size = (++token - d3d8_elements) * sizeof(DWORD);
331
332     return element_count;
333 }
334
335 const IDirect3DVertexDeclaration8Vtbl Direct3DVertexDeclaration8_Vtbl =
336 {
337     IDirect3DVertexDeclaration8Impl_QueryInterface,
338     IDirect3DVertexDeclaration8Impl_AddRef,
339     IDirect3DVertexDeclaration8Impl_Release
340 };