VirtualBox

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

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

crOpenGL: update to wine 1.1.36 and disable unnecessary fbo state poll

  • Property svn:eol-style set to native
File size: 17.0 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 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), ppobj);
210
211 if (IsEqualGUID(riid, &IID_IUnknown)
212 || IsEqualGUID(riid, &IID_IDirect3DVertexDeclaration9)) {
213 IDirect3DVertexDeclaration9_AddRef(iface);
214 *ppobj = This;
215 return S_OK;
216 }
217
218 WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
219 *ppobj = NULL;
220 return E_NOINTERFACE;
221}
222
223static ULONG WINAPI IDirect3DVertexDeclaration9Impl_AddRef(LPDIRECT3DVERTEXDECLARATION9 iface) {
224 IDirect3DVertexDeclaration9Impl *This = (IDirect3DVertexDeclaration9Impl *)iface;
225 ULONG ref = InterlockedIncrement(&This->ref);
226
227 TRACE("%p increasing refcount to %u.\n", iface, ref);
228
229 if(ref == 1) {
230 IDirect3DDevice9Ex_AddRef(This->parentDevice);
231 if (!This->convFVF)
232 {
233 wined3d_mutex_lock();
234 IWineD3DVertexDeclaration_AddRef(This->wineD3DVertexDeclaration);
235 wined3d_mutex_unlock();
236 }
237 }
238
239 return ref;
240}
241
242void IDirect3DVertexDeclaration9Impl_Destroy(LPDIRECT3DVERTEXDECLARATION9 iface) {
243 IDirect3DVertexDeclaration9Impl *This = (IDirect3DVertexDeclaration9Impl *)iface;
244
245 if(This->ref != 0) {
246 /* Should not happen unless wine has a bug or the application releases references it does not own */
247 ERR("Destroying vdecl with ref != 0\n");
248 }
249
250 wined3d_mutex_lock();
251 IWineD3DVertexDeclaration_Release(This->wineD3DVertexDeclaration);
252 wined3d_mutex_unlock();
253}
254
255static ULONG WINAPI IDirect3DVertexDeclaration9Impl_Release(LPDIRECT3DVERTEXDECLARATION9 iface) {
256 IDirect3DVertexDeclaration9Impl *This = (IDirect3DVertexDeclaration9Impl *)iface;
257 ULONG ref = InterlockedDecrement(&This->ref);
258
259 TRACE("%p decreasing refcount to %u.\n", iface, ref);
260
261 if (ref == 0) {
262 IDirect3DDevice9Ex *parentDevice = This->parentDevice;
263
264 if(!This->convFVF) {
265 IDirect3DVertexDeclaration9Impl_Destroy(iface);
266 }
267
268 /* Release the device last, as it may cause the device to be destroyed. */
269 IDirect3DDevice9Ex_Release(parentDevice);
270 }
271 return ref;
272}
273
274/* IDirect3DVertexDeclaration9 Interface follow: */
275static HRESULT WINAPI IDirect3DVertexDeclaration9Impl_GetDevice(IDirect3DVertexDeclaration9 *iface,
276 IDirect3DDevice9 **device)
277{
278 IDirect3DVertexDeclaration9Impl *This = (IDirect3DVertexDeclaration9Impl *)iface;
279
280 TRACE("iface %p, device %p.\n", iface, device);
281
282 *device = (IDirect3DDevice9 *)This->parentDevice;
283 IDirect3DDevice9_AddRef(*device);
284
285 TRACE("Returning device %p.\n", *device);
286
287 return D3D_OK;
288}
289
290static HRESULT WINAPI IDirect3DVertexDeclaration9Impl_GetDeclaration(LPDIRECT3DVERTEXDECLARATION9 iface, D3DVERTEXELEMENT9* pDecl, UINT* pNumElements) {
291 IDirect3DVertexDeclaration9Impl *This = (IDirect3DVertexDeclaration9Impl *)iface;
292
293 TRACE("iface %p, elements %p, element_count %p.\n", iface, pDecl, pNumElements);
294
295 *pNumElements = This->element_count;
296
297 /* Passing a NULL pDecl is used to just retrieve the number of elements */
298 if (!pDecl) {
299 TRACE("NULL pDecl passed. Returning D3D_OK.\n");
300 return D3D_OK;
301 }
302
303 TRACE("Copying %p to %p\n", This->elements, pDecl);
304 CopyMemory(pDecl, This->elements, This->element_count * sizeof(D3DVERTEXELEMENT9));
305
306 return D3D_OK;
307}
308
309static const IDirect3DVertexDeclaration9Vtbl Direct3DVertexDeclaration9_Vtbl =
310{
311 /* IUnknown */
312 IDirect3DVertexDeclaration9Impl_QueryInterface,
313 IDirect3DVertexDeclaration9Impl_AddRef,
314 IDirect3DVertexDeclaration9Impl_Release,
315 /* IDirect3DVertexDeclaration9 */
316 IDirect3DVertexDeclaration9Impl_GetDevice,
317 IDirect3DVertexDeclaration9Impl_GetDeclaration
318};
319
320static void STDMETHODCALLTYPE d3d9_vertexdeclaration_wined3d_object_destroyed(void *parent)
321{
322 IDirect3DVertexDeclaration9Impl *declaration = parent;
323 HeapFree(GetProcessHeap(), 0, declaration->elements);
324 HeapFree(GetProcessHeap(), 0, declaration);
325}
326
327static const struct wined3d_parent_ops d3d9_vertexdeclaration_wined3d_parent_ops =
328{
329 d3d9_vertexdeclaration_wined3d_object_destroyed,
330};
331
332static HRESULT convert_to_wined3d_declaration(const D3DVERTEXELEMENT9* d3d9_elements,
333 WINED3DVERTEXELEMENT **wined3d_elements, UINT *element_count)
334{
335 const D3DVERTEXELEMENT9* element;
336 UINT count = 1;
337 UINT i;
338
339 TRACE("d3d9_elements %p, wined3d_elements %p\n", d3d9_elements, wined3d_elements);
340
341 element = d3d9_elements;
342 while (element++->Stream != 0xff && count++ < 128);
343
344 if (count == 128) return E_FAIL;
345
346 /* Skip the END element */
347 --count;
348
349 *wined3d_elements = HeapAlloc(GetProcessHeap(), 0, count * sizeof(WINED3DVERTEXELEMENT));
350 if (!*wined3d_elements) {
351 FIXME("Memory allocation failed\n");
352 return D3DERR_OUTOFVIDEOMEMORY;
353 }
354
355 for (i = 0; i < count; ++i)
356 {
357 if (d3d9_elements[i].Type >= (sizeof(d3d_dtype_lookup) / sizeof(*d3d_dtype_lookup)))
358 {
359 WARN("Invalid element type %#x.\n", d3d9_elements[i].Type);
360 HeapFree(GetProcessHeap(), 0, *wined3d_elements);
361 return E_FAIL;
362 }
363 (*wined3d_elements)[i].format = d3d_dtype_lookup[d3d9_elements[i].Type].format;
364 (*wined3d_elements)[i].input_slot = d3d9_elements[i].Stream;
365 (*wined3d_elements)[i].offset = d3d9_elements[i].Offset;
366 (*wined3d_elements)[i].output_slot = ~0U;
367 (*wined3d_elements)[i].method = d3d9_elements[i].Method;
368 (*wined3d_elements)[i].usage = d3d9_elements[i].Usage;
369 (*wined3d_elements)[i].usage_idx = d3d9_elements[i].UsageIndex;
370 }
371
372 *element_count = count;
373
374 return D3D_OK;
375}
376
377HRESULT vertexdeclaration_init(IDirect3DVertexDeclaration9Impl *declaration,
378 IDirect3DDevice9Impl *device, const D3DVERTEXELEMENT9 *elements)
379{
380 WINED3DVERTEXELEMENT *wined3d_elements;
381 UINT wined3d_element_count;
382 UINT element_count;
383 HRESULT hr;
384
385 hr = convert_to_wined3d_declaration(elements, &wined3d_elements, &wined3d_element_count);
386 if (FAILED(hr))
387 {
388 WARN("Failed to create wined3d vertex declaration elements, hr %#x.\n", hr);
389 return hr;
390 }
391
392 declaration->lpVtbl = &Direct3DVertexDeclaration9_Vtbl;
393 declaration->ref = 1;
394
395 element_count = wined3d_element_count + 1;
396 declaration->elements = HeapAlloc(GetProcessHeap(), 0, element_count * sizeof(*declaration->elements));
397 if (!declaration->elements)
398 {
399 HeapFree(GetProcessHeap(), 0, wined3d_elements);
400 ERR("Failed to allocate vertex declaration elements memory.\n");
401 return D3DERR_OUTOFVIDEOMEMORY;
402 }
403 memcpy(declaration->elements, elements, element_count * sizeof(*elements));
404 declaration->element_count = element_count;
405
406 wined3d_mutex_lock();
407 hr = IWineD3DDevice_CreateVertexDeclaration(device->WineD3DDevice, &declaration->wineD3DVertexDeclaration,
408 (IUnknown *)declaration, &d3d9_vertexdeclaration_wined3d_parent_ops,
409 wined3d_elements, wined3d_element_count);
410 wined3d_mutex_unlock();
411 HeapFree(GetProcessHeap(), 0, wined3d_elements);
412 if (FAILED(hr))
413 {
414 HeapFree(GetProcessHeap(), 0, declaration->elements);
415 WARN("Failed to create wined3d vertex declaration, hr %#x.\n", hr);
416 return hr;
417 }
418
419 declaration->parentDevice = (IDirect3DDevice9Ex *)device;
420 IDirect3DDevice9Ex_AddRef(declaration->parentDevice);
421
422 return D3D_OK;
423}
424
425HRESULT WINAPI IDirect3DDevice9Impl_SetVertexDeclaration(LPDIRECT3DDEVICE9EX iface, IDirect3DVertexDeclaration9* pDecl) {
426 IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
427 IDirect3DVertexDeclaration9Impl *pDeclImpl = (IDirect3DVertexDeclaration9Impl *)pDecl;
428 HRESULT hr = D3D_OK;
429
430 TRACE("iface %p, vertex declaration %p.\n", iface, pDecl);
431
432 wined3d_mutex_lock();
433 hr = IWineD3DDevice_SetVertexDeclaration(This->WineD3DDevice, pDeclImpl == NULL ? NULL : pDeclImpl->wineD3DVertexDeclaration);
434 wined3d_mutex_unlock();
435
436 return hr;
437}
438
439HRESULT WINAPI IDirect3DDevice9Impl_GetVertexDeclaration(LPDIRECT3DDEVICE9EX iface, IDirect3DVertexDeclaration9** ppDecl) {
440 IDirect3DDevice9Impl* This = (IDirect3DDevice9Impl*) iface;
441 IWineD3DVertexDeclaration* pTest = NULL;
442 HRESULT hr = D3D_OK;
443
444 TRACE("iface %p, declaration %p.\n", iface, ppDecl);
445
446 if (NULL == ppDecl) {
447 return D3DERR_INVALIDCALL;
448 }
449
450 *ppDecl = NULL;
451
452 wined3d_mutex_lock();
453 hr = IWineD3DDevice_GetVertexDeclaration(This->WineD3DDevice, &pTest);
454 if (hr == D3D_OK && NULL != pTest) {
455 IWineD3DVertexDeclaration_GetParent(pTest, (IUnknown **)ppDecl);
456 IWineD3DVertexDeclaration_Release(pTest);
457 } else {
458 *ppDecl = NULL;
459 }
460 wined3d_mutex_unlock();
461
462 TRACE("(%p) : returning %p\n", This, *ppDecl);
463 return hr;
464}
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