dsound/tests: Win64 printf format warning fixes.
[wine] / dlls / d3d9 / vertexdeclaration.c
1 /*
2  * IDirect3DVertexDeclaration9 implementation
3  *
4  * Copyright 2002-2003 Raphael Junqueira
5  *                     Jason Edmeades
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include "config.h"
23 #include "d3d9_private.h"
24
25 WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
26
27 typedef struct _D3DDECLTYPE_INFO {
28     D3DDECLTYPE d3dType;
29     int         size;
30     int         typesize;
31 } D3DDECLTYPE_INFO;
32
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)}};
51
52 #define D3D_DECL_SIZE(type)          d3d_dtype_lookup[type].size
53 #define D3D_DECL_TYPESIZE(type)      d3d_dtype_lookup[type].typesize
54
55 HRESULT vdecl_convert_fvf(
56     DWORD fvf,
57     D3DVERTEXELEMENT9** ppVertexElements) {
58
59     unsigned int idx, idx2;
60     unsigned int offset;
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;
71
72     DWORD num_textures = (fvf & D3DFVF_TEXCOUNT_MASK) >> D3DFVF_TEXCOUNT_SHIFT;
73     DWORD texcoords = (fvf & 0x00FF0000) >> 16;
74
75     D3DVERTEXELEMENT9 end_element = D3DDECL_END();
76     D3DVERTEXELEMENT9 *elements = NULL;
77
78     unsigned int size;
79     DWORD num_blends = 1 + (((fvf & D3DFVF_XYZB5) - D3DFVF_XYZB1) >> 1);
80     if (has_blend_idx) num_blends--;
81
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;
85
86     /* convert the declaration */
87     elements = HeapAlloc(GetProcessHeap(), 0, size * sizeof(D3DVERTEXELEMENT9));
88     if (!elements) 
89         return D3DERR_OUTOFVIDEOMEMORY;
90
91     memcpy(&elements[size-1], &end_element, sizeof(D3DVERTEXELEMENT9));
92     idx = 0;
93     if (has_pos) {
94         if (!has_blend && (fvf & D3DFVF_XYZRHW)) {
95             elements[idx].Type = D3DDECLTYPE_FLOAT4;
96             elements[idx].Usage = D3DDECLUSAGE_POSITIONT;
97         }
98         else {
99             elements[idx].Type = D3DDECLTYPE_FLOAT3;
100             elements[idx].Usage = D3DDECLUSAGE_POSITION;
101         }
102         elements[idx].UsageIndex = 0;
103         idx++;
104     }
105     if (has_blend && (num_blends > 0)) {
106         if (((fvf & D3DFVF_XYZB5) == D3DFVF_XYZB2) && (fvf & D3DFVF_LASTBETA_D3DCOLOR))
107             elements[idx].Type = D3DDECLTYPE_D3DCOLOR;
108         else
109             elements[idx].Type = D3DDECLTYPE_FLOAT1 + num_blends - 1;
110         elements[idx].Usage = D3DDECLUSAGE_BLENDWEIGHT;
111         elements[idx].UsageIndex = 0;
112         idx++;
113     }
114     if (has_blend_idx) {
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;
120         else
121             elements[idx].Type = D3DDECLTYPE_FLOAT1;
122         elements[idx].Usage = D3DDECLUSAGE_BLENDINDICES;
123         elements[idx].UsageIndex = 0;
124         idx++;
125     }
126     if (has_normal) {
127         elements[idx].Type = D3DDECLTYPE_FLOAT3;
128         elements[idx].Usage = D3DDECLUSAGE_NORMAL;
129         elements[idx].UsageIndex = 0;
130         idx++;
131     }
132     if (has_psize) {
133         elements[idx].Type = D3DDECLTYPE_FLOAT1;
134         elements[idx].Usage = D3DDECLUSAGE_PSIZE;
135         elements[idx].UsageIndex = 0;
136         idx++;
137     }
138     if (has_diffuse) {
139         elements[idx].Type = D3DDECLTYPE_D3DCOLOR;
140         elements[idx].Usage = D3DDECLUSAGE_COLOR;
141         elements[idx].UsageIndex = 0;
142         idx++;
143     }
144     if (has_specular) {
145         elements[idx].Type = D3DDECLTYPE_D3DCOLOR;
146         elements[idx].Usage = D3DDECLUSAGE_COLOR;
147         elements[idx].UsageIndex = 1;
148         idx++;
149     }
150     for (idx2 = 0; idx2 < num_textures; idx2++) {
151         unsigned int numcoords = (texcoords >> (idx2*2)) & 0x03;
152         switch (numcoords) {
153             case D3DFVF_TEXTUREFORMAT1:
154                 elements[idx].Type = D3DDECLTYPE_FLOAT1;
155                 break;
156             case D3DFVF_TEXTUREFORMAT2:
157                 elements[idx].Type = D3DDECLTYPE_FLOAT2;
158                 break;
159             case D3DFVF_TEXTUREFORMAT3:
160                 elements[idx].Type = D3DDECLTYPE_FLOAT3;
161                 break;
162             case D3DFVF_TEXTUREFORMAT4:
163                 elements[idx].Type = D3DDECLTYPE_FLOAT4;
164                 break;
165         }
166         elements[idx].Usage = D3DDECLUSAGE_TEXCOORD;
167         elements[idx].UsageIndex = idx2;
168         idx++;
169     }
170
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);
177     }
178
179     *ppVertexElements = elements;
180     return D3D_OK;
181 }
182
183 /* IDirect3DVertexDeclaration9 IUnknown parts follow: */
184 static HRESULT WINAPI IDirect3DVertexDeclaration9Impl_QueryInterface(LPDIRECT3DVERTEXDECLARATION9 iface, REFIID riid, LPVOID* ppobj) {
185     IDirect3DVertexDeclaration9Impl *This = (IDirect3DVertexDeclaration9Impl *)iface;
186
187     if (IsEqualGUID(riid, &IID_IUnknown)
188         || IsEqualGUID(riid, &IID_IDirect3DVertexDeclaration9)) {
189         IUnknown_AddRef(iface);
190         *ppobj = This;
191         return S_OK;
192     }
193
194     WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
195     *ppobj = NULL;
196     return E_NOINTERFACE;
197 }
198
199 static ULONG WINAPI IDirect3DVertexDeclaration9Impl_AddRef(LPDIRECT3DVERTEXDECLARATION9 iface) {
200     IDirect3DVertexDeclaration9Impl *This = (IDirect3DVertexDeclaration9Impl *)iface;
201     ULONG ref = InterlockedIncrement(&This->ref);
202
203     TRACE("(%p) : AddRef from %ld\n", This, ref - 1);
204
205     return ref;
206 }
207
208 static ULONG WINAPI IDirect3DVertexDeclaration9Impl_Release(LPDIRECT3DVERTEXDECLARATION9 iface) {
209     IDirect3DVertexDeclaration9Impl *This = (IDirect3DVertexDeclaration9Impl *)iface;
210     ULONG ref = InterlockedDecrement(&This->ref);
211
212     TRACE("(%p) : ReleaseRef to %ld\n", This, ref);
213
214     if (ref == 0) {
215         IWineD3DVertexDeclaration_Release(This->wineD3DVertexDeclaration);
216         IUnknown_Release(This->parentDevice);
217         HeapFree(GetProcessHeap(), 0, This);
218     }
219     return ref;
220 }
221
222 /* IDirect3DVertexDeclaration9 Interface follow: */
223 static HRESULT WINAPI IDirect3DVertexDeclaration9Impl_GetDevice(LPDIRECT3DVERTEXDECLARATION9 iface, IDirect3DDevice9** ppDevice) {
224     IDirect3DVertexDeclaration9Impl *This = (IDirect3DVertexDeclaration9Impl *)iface;
225     IWineD3DDevice *myDevice = NULL;
226     HRESULT hr = D3D_OK;
227
228     TRACE("(%p) : Relay\n", iface);
229
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);
234     }
235     return hr;
236 }
237
238 static HRESULT WINAPI IDirect3DVertexDeclaration9Impl_GetDeclaration(LPDIRECT3DVERTEXDECLARATION9 iface, D3DVERTEXELEMENT9* pDecl, UINT* pNumElements) {
239     IDirect3DVertexDeclaration9Impl *This = (IDirect3DVertexDeclaration9Impl *)iface;
240     DWORD NumElements;
241     HRESULT hr;
242     TRACE("(%p) : Relay\n", iface);
243     hr = IWineD3DVertexDeclaration_GetDeclaration(This->wineD3DVertexDeclaration, pDecl, &NumElements);
244
245     *pNumElements = NumElements;
246     return hr;
247 }
248
249 static const IDirect3DVertexDeclaration9Vtbl Direct3DVertexDeclaration9_Vtbl =
250 {
251     /* IUnknown */
252     IDirect3DVertexDeclaration9Impl_QueryInterface,
253     IDirect3DVertexDeclaration9Impl_AddRef,
254     IDirect3DVertexDeclaration9Impl_Release,
255     /* IDirect3DVertexDeclaration9 */
256     IDirect3DVertexDeclaration9Impl_GetDevice,
257     IDirect3DVertexDeclaration9Impl_GetDeclaration
258 };
259
260
261 /* IDirect3DDevice9 IDirect3DVertexDeclaration9 Methods follow: */
262 HRESULT  WINAPI  IDirect3DDevice9Impl_CreateVertexDeclaration(LPDIRECT3DDEVICE9 iface, CONST D3DVERTEXELEMENT9* pVertexElements, IDirect3DVertexDeclaration9** ppDecl) {
263     
264     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
265     IDirect3DVertexDeclaration9Impl *object = NULL;
266     HRESULT hr = D3D_OK;
267
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;
272     }
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;
278     }
279
280     object->lpVtbl = &Direct3DVertexDeclaration9_Vtbl;
281     object->ref = 1;
282     hr = IWineD3DDevice_CreateVertexDeclaration(This->WineD3DDevice, pVertexElements, &object->wineD3DVertexDeclaration, (IUnknown *)object);
283
284     if (FAILED(hr)) {
285
286         /* free up object */
287         FIXME("(%p) call to IWineD3DDevice_CreateVertexDeclaration failed\n", This);
288         HeapFree(GetProcessHeap(), 0, object);
289     } else {
290         IUnknown_AddRef(iface);
291         object->parentDevice = iface;
292         *ppDecl = (LPDIRECT3DVERTEXDECLARATION9) object;
293          TRACE("(%p) : Created vertex declaration %p\n", This, object);
294     }
295     return hr;
296 }
297
298 HRESULT  WINAPI  IDirect3DDevice9Impl_SetVertexDeclaration(LPDIRECT3DDEVICE9 iface, IDirect3DVertexDeclaration9* pDecl) {
299     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
300     IDirect3DVertexDeclaration9Impl *pDeclImpl = (IDirect3DVertexDeclaration9Impl *)pDecl;
301     HRESULT hr = D3D_OK;
302
303     TRACE("(%p) : Relay\n", iface);
304
305     hr = IWineD3DDevice_SetVertexDeclaration(This->WineD3DDevice, pDeclImpl == NULL ? NULL : pDeclImpl->wineD3DVertexDeclaration);
306
307     return hr;
308 }
309
310 HRESULT  WINAPI  IDirect3DDevice9Impl_GetVertexDeclaration(LPDIRECT3DDEVICE9 iface, IDirect3DVertexDeclaration9** ppDecl) {
311     IDirect3DDevice9Impl* This = (IDirect3DDevice9Impl*) iface;
312     IWineD3DVertexDeclaration* pTest = NULL;
313     HRESULT hr = D3D_OK;
314
315     TRACE("(%p) : Relay+\n", iface);
316
317     if (NULL == ppDecl) {
318       return D3DERR_INVALIDCALL;
319     }
320
321     *ppDecl = NULL;
322     hr = IWineD3DDevice_GetVertexDeclaration(This->WineD3DDevice, &pTest);
323     if (hr == D3D_OK && NULL != pTest) {
324         IWineD3DResource_GetParent(pTest, (IUnknown **)ppDecl);
325         IWineD3DResource_Release(pTest);
326     } else {
327         *ppDecl = NULL;
328     }
329     TRACE("(%p) : returning %p\n", This, *ppDecl);
330     return hr;
331 }