VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/Graphics/Wine/d3d9/vertexdeclaration.c@ 23571

Last change on this file since 23571 was 23571, checked in by vboxsync, 15 years ago

crOpenGL: update to wine 1.1.30

  • Property svn:eol-style set to native
File size: 16.8 KB
Line 
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/*
23 * Sun LGPL Disclaimer: For the avoidance of doubt, except that if any license choice
24 * other than GPL or LGPL is available it will apply instead, Sun elects to use only
25 * the Lesser General Public License version 2.1 (LGPLv2) at this time for any software where
26 * a choice of LGPL license versions is made available with the language indicating
27 * that LGPLv2 or any later version may be used, or where a choice of which version
28 * of the LGPL is applied is otherwise unspecified.
29 */
30
31#include "config.h"
32#include "d3d9_private.h"
33
34WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
35
36typedef struct _D3DDECLTYPE_INFO {
37 D3DDECLTYPE d3dType;
38 WINED3DFORMAT format;
39 int size;
40 int typesize;
41} D3DDECLTYPE_INFO;
42
43static D3DDECLTYPE_INFO const d3d_dtype_lookup[D3DDECLTYPE_UNUSED] = {
44 {D3DDECLTYPE_FLOAT1, WINED3DFMT_R32_FLOAT, 1, sizeof(float)},
45 {D3DDECLTYPE_FLOAT2, WINED3DFMT_R32G32_FLOAT, 2, sizeof(float)},
46 {D3DDECLTYPE_FLOAT3, WINED3DFMT_R32G32B32_FLOAT, 3, sizeof(float)},
47 {D3DDECLTYPE_FLOAT4, WINED3DFMT_R32G32B32A32_FLOAT, 4, sizeof(float)},
48 {D3DDECLTYPE_D3DCOLOR, WINED3DFMT_B8G8R8A8_UNORM, 4, sizeof(BYTE)},
49 {D3DDECLTYPE_UBYTE4, WINED3DFMT_R8G8B8A8_UINT, 4, sizeof(BYTE)},
50 {D3DDECLTYPE_SHORT2, WINED3DFMT_R16G16_SINT, 2, sizeof(short int)},
51 {D3DDECLTYPE_SHORT4, WINED3DFMT_R16G16B16A16_SINT, 4, sizeof(short int)},
52 {D3DDECLTYPE_UBYTE4N, WINED3DFMT_R8G8B8A8_UNORM, 4, sizeof(BYTE)},
53 {D3DDECLTYPE_SHORT2N, WINED3DFMT_R16G16_SNORM, 2, sizeof(short int)},
54 {D3DDECLTYPE_SHORT4N, WINED3DFMT_R16G16B16A16_SNORM, 4, sizeof(short int)},
55 {D3DDECLTYPE_USHORT2N, WINED3DFMT_R16G16_UNORM, 2, sizeof(short int)},
56 {D3DDECLTYPE_USHORT4N, WINED3DFMT_R16G16B16A16_UNORM, 4, sizeof(short int)},
57 {D3DDECLTYPE_UDEC3, WINED3DFMT_R10G10B10A2_UINT, 3, sizeof(short int)},
58 {D3DDECLTYPE_DEC3N, WINED3DFMT_R10G10B10A2_SNORM, 3, sizeof(short int)},
59 {D3DDECLTYPE_FLOAT16_2, WINED3DFMT_R16G16_FLOAT, 2, sizeof(short int)},
60 {D3DDECLTYPE_FLOAT16_4, WINED3DFMT_R16G16B16A16_FLOAT, 4, sizeof(short int)}};
61
62#define D3D_DECL_SIZE(type) d3d_dtype_lookup[type].size
63#define D3D_DECL_TYPESIZE(type) d3d_dtype_lookup[type].typesize
64
65HRESULT vdecl_convert_fvf(
66 DWORD fvf,
67 D3DVERTEXELEMENT9** ppVertexElements) {
68
69 unsigned int idx, idx2;
70 unsigned int offset;
71 BOOL has_pos = (fvf & D3DFVF_POSITION_MASK) != 0;
72 BOOL has_blend = (fvf & D3DFVF_XYZB5) > D3DFVF_XYZRHW;
73 BOOL has_blend_idx = has_blend &&
74 (((fvf & D3DFVF_XYZB5) == D3DFVF_XYZB5) ||
75 (fvf & D3DFVF_LASTBETA_D3DCOLOR) ||
76 (fvf & D3DFVF_LASTBETA_UBYTE4));
77 BOOL has_normal = (fvf & D3DFVF_NORMAL) != 0;
78 BOOL has_psize = (fvf & D3DFVF_PSIZE) != 0;
79
80 BOOL has_diffuse = (fvf & D3DFVF_DIFFUSE) != 0;
81 BOOL has_specular = (fvf & D3DFVF_SPECULAR) !=0;
82
83 DWORD num_textures = (fvf & D3DFVF_TEXCOUNT_MASK) >> D3DFVF_TEXCOUNT_SHIFT;
84 DWORD texcoords = (fvf & 0xFFFF0000) >> 16;
85
86 D3DVERTEXELEMENT9 end_element = D3DDECL_END();
87 D3DVERTEXELEMENT9 *elements = NULL;
88
89 unsigned int size;
90 DWORD num_blends = 1 + (((fvf & D3DFVF_XYZB5) - D3DFVF_XYZB1) >> 1);
91 if (has_blend_idx) num_blends--;
92
93 /* Compute declaration size */
94 size = has_pos + (has_blend && num_blends > 0) + has_blend_idx + has_normal +
95 has_psize + has_diffuse + has_specular + num_textures + 1;
96
97 /* convert the declaration */
98 elements = HeapAlloc(GetProcessHeap(), 0, size * sizeof(D3DVERTEXELEMENT9));
99 if (!elements) return D3DERR_OUTOFVIDEOMEMORY;
100
101 elements[size-1] = end_element;
102 idx = 0;
103 if (has_pos) {
104 if (!has_blend && (fvf & D3DFVF_XYZRHW)) {
105 elements[idx].Type = D3DDECLTYPE_FLOAT4;
106 elements[idx].Usage = D3DDECLUSAGE_POSITIONT;
107 }
108 else if (!has_blend && (fvf & D3DFVF_XYZW) == D3DFVF_XYZW) {
109 elements[idx].Type = D3DDECLTYPE_FLOAT4;
110 elements[idx].Usage = D3DDECLUSAGE_POSITION;
111 }
112 else {
113 elements[idx].Type = D3DDECLTYPE_FLOAT3;
114 elements[idx].Usage = D3DDECLUSAGE_POSITION;
115 }
116 elements[idx].UsageIndex = 0;
117 idx++;
118 }
119 if (has_blend && (num_blends > 0)) {
120 if (((fvf & D3DFVF_XYZB5) == D3DFVF_XYZB2) && (fvf & D3DFVF_LASTBETA_D3DCOLOR))
121 elements[idx].Type = D3DDECLTYPE_D3DCOLOR;
122 else {
123 switch(num_blends) {
124 case 1: elements[idx].Type = D3DDECLTYPE_FLOAT1; break;
125 case 2: elements[idx].Type = D3DDECLTYPE_FLOAT2; break;
126 case 3: elements[idx].Type = D3DDECLTYPE_FLOAT3; break;
127 case 4: elements[idx].Type = D3DDECLTYPE_FLOAT4; break;
128 default:
129 ERR("Unexpected amount of blend values: %u\n", num_blends);
130 }
131 }
132 elements[idx].Usage = D3DDECLUSAGE_BLENDWEIGHT;
133 elements[idx].UsageIndex = 0;
134 idx++;
135 }
136 if (has_blend_idx) {
137 if (fvf & D3DFVF_LASTBETA_UBYTE4 ||
138 (((fvf & D3DFVF_XYZB5) == D3DFVF_XYZB2) && (fvf & D3DFVF_LASTBETA_D3DCOLOR)))
139 elements[idx].Type = D3DDECLTYPE_UBYTE4;
140 else if (fvf & D3DFVF_LASTBETA_D3DCOLOR)
141 elements[idx].Type = D3DDECLTYPE_D3DCOLOR;
142 else
143 elements[idx].Type = D3DDECLTYPE_FLOAT1;
144 elements[idx].Usage = D3DDECLUSAGE_BLENDINDICES;
145 elements[idx].UsageIndex = 0;
146 idx++;
147 }
148 if (has_normal) {
149 elements[idx].Type = D3DDECLTYPE_FLOAT3;
150 elements[idx].Usage = D3DDECLUSAGE_NORMAL;
151 elements[idx].UsageIndex = 0;
152 idx++;
153 }
154 if (has_psize) {
155 elements[idx].Type = D3DDECLTYPE_FLOAT1;
156 elements[idx].Usage = D3DDECLUSAGE_PSIZE;
157 elements[idx].UsageIndex = 0;
158 idx++;
159 }
160 if (has_diffuse) {
161 elements[idx].Type = D3DDECLTYPE_D3DCOLOR;
162 elements[idx].Usage = D3DDECLUSAGE_COLOR;
163 elements[idx].UsageIndex = 0;
164 idx++;
165 }
166 if (has_specular) {
167 elements[idx].Type = D3DDECLTYPE_D3DCOLOR;
168 elements[idx].Usage = D3DDECLUSAGE_COLOR;
169 elements[idx].UsageIndex = 1;
170 idx++;
171 }
172 for (idx2 = 0; idx2 < num_textures; idx2++) {
173 unsigned int numcoords = (texcoords >> (idx2*2)) & 0x03;
174 switch (numcoords) {
175 case D3DFVF_TEXTUREFORMAT1:
176 elements[idx].Type = D3DDECLTYPE_FLOAT1;
177 break;
178 case D3DFVF_TEXTUREFORMAT2:
179 elements[idx].Type = D3DDECLTYPE_FLOAT2;
180 break;
181 case D3DFVF_TEXTUREFORMAT3:
182 elements[idx].Type = D3DDECLTYPE_FLOAT3;
183 break;
184 case D3DFVF_TEXTUREFORMAT4:
185 elements[idx].Type = D3DDECLTYPE_FLOAT4;
186 break;
187 }
188 elements[idx].Usage = D3DDECLUSAGE_TEXCOORD;
189 elements[idx].UsageIndex = idx2;
190 idx++;
191 }
192
193 /* Now compute offsets, and initialize the rest of the fields */
194 for (idx = 0, offset = 0; idx < size-1; idx++) {
195 elements[idx].Stream = 0;
196 elements[idx].Method = D3DDECLMETHOD_DEFAULT;
197 elements[idx].Offset = offset;
198 offset += D3D_DECL_SIZE(elements[idx].Type) * D3D_DECL_TYPESIZE(elements[idx].Type);
199 }
200
201 *ppVertexElements = elements;
202 return D3D_OK;
203}
204
205/* IDirect3DVertexDeclaration9 IUnknown parts follow: */
206static HRESULT WINAPI IDirect3DVertexDeclaration9Impl_QueryInterface(LPDIRECT3DVERTEXDECLARATION9 iface, REFIID riid, LPVOID* ppobj) {
207 IDirect3DVertexDeclaration9Impl *This = (IDirect3DVertexDeclaration9Impl *)iface;
208
209 if (IsEqualGUID(riid, &IID_IUnknown)
210 || IsEqualGUID(riid, &IID_IDirect3DVertexDeclaration9)) {
211 IDirect3DVertexDeclaration9_AddRef(iface);
212 *ppobj = This;
213 return S_OK;
214 }
215
216 WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
217 *ppobj = NULL;
218 return E_NOINTERFACE;
219}
220
221static ULONG WINAPI IDirect3DVertexDeclaration9Impl_AddRef(LPDIRECT3DVERTEXDECLARATION9 iface) {
222 IDirect3DVertexDeclaration9Impl *This = (IDirect3DVertexDeclaration9Impl *)iface;
223 ULONG ref = InterlockedIncrement(&This->ref);
224
225 TRACE("(%p) : AddRef from %d\n", This, ref - 1);
226
227 if(ref == 1) {
228 IDirect3DDevice9Ex_AddRef(This->parentDevice);
229 if (!This->convFVF)
230 {
231 wined3d_mutex_lock();
232 IWineD3DVertexDeclaration_AddRef(This->wineD3DVertexDeclaration);
233 wined3d_mutex_unlock();
234 }
235 }
236
237 return ref;
238}
239
240void IDirect3DVertexDeclaration9Impl_Destroy(LPDIRECT3DVERTEXDECLARATION9 iface) {
241 IDirect3DVertexDeclaration9Impl *This = (IDirect3DVertexDeclaration9Impl *)iface;
242
243 if(This->ref != 0) {
244 /* Should not happen unless wine has a bug or the application releases references it does not own */
245 ERR("Destroying vdecl with ref != 0\n");
246 }
247
248 wined3d_mutex_lock();
249 IWineD3DVertexDeclaration_Release(This->wineD3DVertexDeclaration);
250 wined3d_mutex_unlock();
251}
252
253static ULONG WINAPI IDirect3DVertexDeclaration9Impl_Release(LPDIRECT3DVERTEXDECLARATION9 iface) {
254 IDirect3DVertexDeclaration9Impl *This = (IDirect3DVertexDeclaration9Impl *)iface;
255 ULONG ref = InterlockedDecrement(&This->ref);
256
257 TRACE("(%p) : ReleaseRef to %d\n", This, ref);
258
259 if (ref == 0) {
260 IDirect3DDevice9Ex_Release(This->parentDevice);
261 if(!This->convFVF) {
262 IDirect3DVertexDeclaration9Impl_Destroy(iface);
263 }
264 }
265 return ref;
266}
267
268/* IDirect3DVertexDeclaration9 Interface follow: */
269static HRESULT WINAPI IDirect3DVertexDeclaration9Impl_GetDevice(LPDIRECT3DVERTEXDECLARATION9 iface, IDirect3DDevice9** ppDevice) {
270 IDirect3DVertexDeclaration9Impl *This = (IDirect3DVertexDeclaration9Impl *)iface;
271 IWineD3DDevice *myDevice = NULL;
272 HRESULT hr = D3D_OK;
273
274 TRACE("(%p) : Relay\n", iface);
275
276 wined3d_mutex_lock();
277 hr = IWineD3DVertexDeclaration_GetDevice(This->wineD3DVertexDeclaration, &myDevice);
278 if (hr == D3D_OK && myDevice != NULL) {
279 hr = IWineD3DDevice_GetParent(myDevice, (IUnknown **)ppDevice);
280 IWineD3DDevice_Release(myDevice);
281 }
282 wined3d_mutex_unlock();
283
284 return hr;
285}
286
287static HRESULT WINAPI IDirect3DVertexDeclaration9Impl_GetDeclaration(LPDIRECT3DVERTEXDECLARATION9 iface, D3DVERTEXELEMENT9* pDecl, UINT* pNumElements) {
288 IDirect3DVertexDeclaration9Impl *This = (IDirect3DVertexDeclaration9Impl *)iface;
289
290 TRACE("(%p) : pDecl %p, pNumElements %p)\n", This, pDecl, pNumElements);
291
292 *pNumElements = This->element_count;
293
294 /* Passing a NULL pDecl is used to just retrieve the number of elements */
295 if (!pDecl) {
296 TRACE("NULL pDecl passed. Returning D3D_OK.\n");
297 return D3D_OK;
298 }
299
300 TRACE("Copying %p to %p\n", This->elements, pDecl);
301 CopyMemory(pDecl, This->elements, This->element_count * sizeof(D3DVERTEXELEMENT9));
302
303 return D3D_OK;
304}
305
306static const IDirect3DVertexDeclaration9Vtbl Direct3DVertexDeclaration9_Vtbl =
307{
308 /* IUnknown */
309 IDirect3DVertexDeclaration9Impl_QueryInterface,
310 IDirect3DVertexDeclaration9Impl_AddRef,
311 IDirect3DVertexDeclaration9Impl_Release,
312 /* IDirect3DVertexDeclaration9 */
313 IDirect3DVertexDeclaration9Impl_GetDevice,
314 IDirect3DVertexDeclaration9Impl_GetDeclaration
315};
316
317static void STDMETHODCALLTYPE d3d9_vertexdeclaration_wined3d_object_destroyed(void *parent)
318{
319 IDirect3DVertexDeclaration9Impl *declaration = parent;
320 HeapFree(GetProcessHeap(), 0, declaration->elements);
321 HeapFree(GetProcessHeap(), 0, declaration);
322}
323
324static const struct wined3d_parent_ops d3d9_vertexdeclaration_wined3d_parent_ops =
325{
326 d3d9_vertexdeclaration_wined3d_object_destroyed,
327};
328
329static HRESULT convert_to_wined3d_declaration(const D3DVERTEXELEMENT9* d3d9_elements,
330 WINED3DVERTEXELEMENT **wined3d_elements, UINT *element_count)
331{
332 const D3DVERTEXELEMENT9* element;
333 UINT count = 1;
334 UINT i;
335
336 TRACE("d3d9_elements %p, wined3d_elements %p\n", d3d9_elements, wined3d_elements);
337
338 element = d3d9_elements;
339 while (element++->Stream != 0xff && count++ < 128);
340
341 if (count == 128) return E_FAIL;
342
343 /* Skip the END element */
344 --count;
345
346 *wined3d_elements = HeapAlloc(GetProcessHeap(), 0, count * sizeof(WINED3DVERTEXELEMENT));
347 if (!*wined3d_elements) {
348 FIXME("Memory allocation failed\n");
349 return D3DERR_OUTOFVIDEOMEMORY;
350 }
351
352 for (i = 0; i < count; ++i)
353 {
354 if (d3d9_elements[i].Type >= (sizeof(d3d_dtype_lookup) / sizeof(*d3d_dtype_lookup)))
355 {
356 WARN("Invalid element type %#x.\n", d3d9_elements[i].Type);
357 HeapFree(GetProcessHeap(), 0, *wined3d_elements);
358 return E_FAIL;
359 }
360 (*wined3d_elements)[i].format = d3d_dtype_lookup[d3d9_elements[i].Type].format;
361 (*wined3d_elements)[i].input_slot = d3d9_elements[i].Stream;
362 (*wined3d_elements)[i].offset = d3d9_elements[i].Offset;
363 (*wined3d_elements)[i].output_slot = ~0U;
364 (*wined3d_elements)[i].method = d3d9_elements[i].Method;
365 (*wined3d_elements)[i].usage = d3d9_elements[i].Usage;
366 (*wined3d_elements)[i].usage_idx = d3d9_elements[i].UsageIndex;
367 }
368
369 *element_count = count;
370
371 return D3D_OK;
372}
373
374HRESULT vertexdeclaration_init(IDirect3DVertexDeclaration9Impl *declaration,
375 IDirect3DDevice9Impl *device, const D3DVERTEXELEMENT9 *elements)
376{
377 WINED3DVERTEXELEMENT *wined3d_elements;
378 UINT wined3d_element_count;
379 UINT element_count;
380 HRESULT hr;
381
382 hr = convert_to_wined3d_declaration(elements, &wined3d_elements, &wined3d_element_count);
383 if (FAILED(hr))
384 {
385 WARN("Failed to create wined3d vertex declaration elements, hr %#x.\n", hr);
386 return hr;
387 }
388
389 declaration->lpVtbl = &Direct3DVertexDeclaration9_Vtbl;
390 declaration->ref = 1;
391
392 element_count = wined3d_element_count + 1;
393 declaration->elements = HeapAlloc(GetProcessHeap(), 0, element_count * sizeof(*declaration->elements));
394 if (!declaration->elements)
395 {
396 HeapFree(GetProcessHeap(), 0, wined3d_elements);
397 ERR("Failed to allocate vertex declaration elements memory.\n");
398 return D3DERR_OUTOFVIDEOMEMORY;
399 }
400 memcpy(declaration->elements, elements, element_count * sizeof(*elements));
401 declaration->element_count = element_count;
402
403 wined3d_mutex_lock();
404 hr = IWineD3DDevice_CreateVertexDeclaration(device->WineD3DDevice, &declaration->wineD3DVertexDeclaration,
405 (IUnknown *)declaration, &d3d9_vertexdeclaration_wined3d_parent_ops,
406 wined3d_elements, wined3d_element_count);
407 wined3d_mutex_unlock();
408 HeapFree(GetProcessHeap(), 0, wined3d_elements);
409 if (FAILED(hr))
410 {
411 WARN("Failed to create wined3d vertex declaration, hr %#x.\n", hr);
412 return hr;
413 }
414
415 declaration->parentDevice = (IDirect3DDevice9Ex *)device;
416 IDirect3DDevice9Ex_AddRef(declaration->parentDevice);
417
418 return D3D_OK;
419}
420
421HRESULT WINAPI IDirect3DDevice9Impl_SetVertexDeclaration(LPDIRECT3DDEVICE9EX iface, IDirect3DVertexDeclaration9* pDecl) {
422 IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
423 IDirect3DVertexDeclaration9Impl *pDeclImpl = (IDirect3DVertexDeclaration9Impl *)pDecl;
424 HRESULT hr = D3D_OK;
425
426 TRACE("(%p) : Relay\n", iface);
427
428 wined3d_mutex_lock();
429 hr = IWineD3DDevice_SetVertexDeclaration(This->WineD3DDevice, pDeclImpl == NULL ? NULL : pDeclImpl->wineD3DVertexDeclaration);
430 wined3d_mutex_unlock();
431
432 return hr;
433}
434
435HRESULT WINAPI IDirect3DDevice9Impl_GetVertexDeclaration(LPDIRECT3DDEVICE9EX iface, IDirect3DVertexDeclaration9** ppDecl) {
436 IDirect3DDevice9Impl* This = (IDirect3DDevice9Impl*) iface;
437 IWineD3DVertexDeclaration* pTest = NULL;
438 HRESULT hr = D3D_OK;
439
440 TRACE("(%p) : Relay+\n", iface);
441
442 if (NULL == ppDecl) {
443 return D3DERR_INVALIDCALL;
444 }
445
446 *ppDecl = NULL;
447
448 wined3d_mutex_lock();
449 hr = IWineD3DDevice_GetVertexDeclaration(This->WineD3DDevice, &pTest);
450 if (hr == D3D_OK && NULL != pTest) {
451 IWineD3DVertexDeclaration_GetParent(pTest, (IUnknown **)ppDecl);
452 IWineD3DVertexDeclaration_Release(pTest);
453 } else {
454 *ppDecl = NULL;
455 }
456 wined3d_mutex_unlock();
457
458 TRACE("(%p) : returning %p\n", This, *ppDecl);
459 return hr;
460}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette