VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/Graphics/Wine_new/d3d9/vertexdeclaration.c@ 63024

Last change on this file since 63024 was 63024, checked in by vboxsync, 8 years ago

GA/NT/Graphics: warnings

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.2 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 * Oracle 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, Oracle 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
36static const struct
37{
38 enum wined3d_format_id format;
39 unsigned int component_count;
40 unsigned int component_size;
41}
42d3d_dtype_lookup[] =
43{
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
63static inline struct d3d9_vertex_declaration *impl_from_IDirect3DVertexDeclaration9(IDirect3DVertexDeclaration9 *iface)
64{
65 return CONTAINING_RECORD(iface, struct d3d9_vertex_declaration, IDirect3DVertexDeclaration9_iface);
66}
67
68HRESULT vdecl_convert_fvf(
69 DWORD fvf,
70 D3DVERTEXELEMENT9** ppVertexElements) {
71
72 unsigned int idx, idx2;
73 unsigned int offset;
74 BOOL has_pos = (fvf & D3DFVF_POSITION_MASK) != 0;
75 BOOL has_blend = (fvf & D3DFVF_XYZB5) > D3DFVF_XYZRHW;
76 BOOL has_blend_idx = has_blend &&
77 (((fvf & D3DFVF_XYZB5) == D3DFVF_XYZB5) ||
78 (fvf & D3DFVF_LASTBETA_D3DCOLOR) ||
79 (fvf & D3DFVF_LASTBETA_UBYTE4));
80 BOOL has_normal = (fvf & D3DFVF_NORMAL) != 0;
81 BOOL has_psize = (fvf & D3DFVF_PSIZE) != 0;
82
83 BOOL has_diffuse = (fvf & D3DFVF_DIFFUSE) != 0;
84 BOOL has_specular = (fvf & D3DFVF_SPECULAR) !=0;
85
86 DWORD num_textures = (fvf & D3DFVF_TEXCOUNT_MASK) >> D3DFVF_TEXCOUNT_SHIFT;
87 DWORD texcoords = (fvf & 0xFFFF0000) >> 16;
88
89 D3DVERTEXELEMENT9 end_element = D3DDECL_END();
90 D3DVERTEXELEMENT9 *elements = NULL;
91
92 unsigned int size;
93 DWORD num_blends = 1 + (((fvf & D3DFVF_XYZB5) - D3DFVF_XYZB1) >> 1);
94 if (has_blend_idx) num_blends--;
95
96 /* Compute declaration size */
97 size = has_pos + (has_blend && num_blends > 0) + has_blend_idx + has_normal +
98 has_psize + has_diffuse + has_specular + num_textures + 1;
99
100 /* convert the declaration */
101 elements = HeapAlloc(GetProcessHeap(), 0, size * sizeof(D3DVERTEXELEMENT9));
102 if (!elements) return D3DERR_OUTOFVIDEOMEMORY;
103
104 elements[size-1] = end_element;
105 idx = 0;
106 if (has_pos) {
107 if (!has_blend && (fvf & D3DFVF_XYZRHW)) {
108 elements[idx].Type = D3DDECLTYPE_FLOAT4;
109 elements[idx].Usage = D3DDECLUSAGE_POSITIONT;
110 }
111 else if (!has_blend && (fvf & D3DFVF_XYZW) == D3DFVF_XYZW) {
112 elements[idx].Type = D3DDECLTYPE_FLOAT4;
113 elements[idx].Usage = D3DDECLUSAGE_POSITION;
114 }
115 else {
116 elements[idx].Type = D3DDECLTYPE_FLOAT3;
117 elements[idx].Usage = D3DDECLUSAGE_POSITION;
118 }
119 elements[idx].UsageIndex = 0;
120 idx++;
121 }
122 if (has_blend && (num_blends > 0)) {
123 if (((fvf & D3DFVF_XYZB5) == D3DFVF_XYZB2) && (fvf & D3DFVF_LASTBETA_D3DCOLOR))
124 elements[idx].Type = D3DDECLTYPE_D3DCOLOR;
125 else {
126 switch(num_blends) {
127 case 1: elements[idx].Type = D3DDECLTYPE_FLOAT1; break;
128 case 2: elements[idx].Type = D3DDECLTYPE_FLOAT2; break;
129 case 3: elements[idx].Type = D3DDECLTYPE_FLOAT3; break;
130 case 4: elements[idx].Type = D3DDECLTYPE_FLOAT4; break;
131 default:
132 ERR("Unexpected amount of blend values: %u\n", num_blends);
133 }
134 }
135 elements[idx].Usage = D3DDECLUSAGE_BLENDWEIGHT;
136 elements[idx].UsageIndex = 0;
137 idx++;
138 }
139 if (has_blend_idx) {
140 if (fvf & D3DFVF_LASTBETA_UBYTE4 ||
141 (((fvf & D3DFVF_XYZB5) == D3DFVF_XYZB2) && (fvf & D3DFVF_LASTBETA_D3DCOLOR)))
142 elements[idx].Type = D3DDECLTYPE_UBYTE4;
143 else if (fvf & D3DFVF_LASTBETA_D3DCOLOR)
144 elements[idx].Type = D3DDECLTYPE_D3DCOLOR;
145 else
146 elements[idx].Type = D3DDECLTYPE_FLOAT1;
147 elements[idx].Usage = D3DDECLUSAGE_BLENDINDICES;
148 elements[idx].UsageIndex = 0;
149 idx++;
150 }
151 if (has_normal) {
152 elements[idx].Type = D3DDECLTYPE_FLOAT3;
153 elements[idx].Usage = D3DDECLUSAGE_NORMAL;
154 elements[idx].UsageIndex = 0;
155 idx++;
156 }
157 if (has_psize) {
158 elements[idx].Type = D3DDECLTYPE_FLOAT1;
159 elements[idx].Usage = D3DDECLUSAGE_PSIZE;
160 elements[idx].UsageIndex = 0;
161 idx++;
162 }
163 if (has_diffuse) {
164 elements[idx].Type = D3DDECLTYPE_D3DCOLOR;
165 elements[idx].Usage = D3DDECLUSAGE_COLOR;
166 elements[idx].UsageIndex = 0;
167 idx++;
168 }
169 if (has_specular) {
170 elements[idx].Type = D3DDECLTYPE_D3DCOLOR;
171 elements[idx].Usage = D3DDECLUSAGE_COLOR;
172 elements[idx].UsageIndex = 1;
173 idx++;
174 }
175 for (idx2 = 0; idx2 < num_textures; idx2++) {
176 unsigned int numcoords = (texcoords >> (idx2*2)) & 0x03;
177 switch (numcoords) {
178 case D3DFVF_TEXTUREFORMAT1:
179 elements[idx].Type = D3DDECLTYPE_FLOAT1;
180 break;
181 case D3DFVF_TEXTUREFORMAT2:
182 elements[idx].Type = D3DDECLTYPE_FLOAT2;
183 break;
184 case D3DFVF_TEXTUREFORMAT3:
185 elements[idx].Type = D3DDECLTYPE_FLOAT3;
186 break;
187 case D3DFVF_TEXTUREFORMAT4:
188 elements[idx].Type = D3DDECLTYPE_FLOAT4;
189 break;
190 }
191 elements[idx].Usage = D3DDECLUSAGE_TEXCOORD;
192 elements[idx].UsageIndex = idx2;
193 idx++;
194 }
195
196 /* Now compute offsets, and initialize the rest of the fields */
197 for (idx = 0, offset = 0; idx < size-1; idx++) {
198 elements[idx].Stream = 0;
199 elements[idx].Method = D3DDECLMETHOD_DEFAULT;
200 elements[idx].Offset = offset;
201 offset += d3d_dtype_lookup[elements[idx].Type].component_count
202 * d3d_dtype_lookup[elements[idx].Type].component_size;
203 }
204
205 *ppVertexElements = elements;
206 return D3D_OK;
207}
208
209static HRESULT WINAPI d3d9_vertex_declaration_QueryInterface(IDirect3DVertexDeclaration9 *iface,
210 REFIID riid, void **out)
211{
212 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
213
214 if (IsEqualGUID(riid, &IID_IDirect3DVertexDeclaration9)
215 || IsEqualGUID(riid, &IID_IUnknown))
216 {
217 IDirect3DVertexDeclaration9_AddRef(iface);
218 *out = iface;
219 return S_OK;
220 }
221
222 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
223
224 *out = NULL;
225 return E_NOINTERFACE;
226}
227
228static ULONG WINAPI d3d9_vertex_declaration_AddRef(IDirect3DVertexDeclaration9 *iface)
229{
230 struct d3d9_vertex_declaration *declaration = impl_from_IDirect3DVertexDeclaration9(iface);
231 ULONG refcount = InterlockedIncrement(&declaration->refcount);
232
233 TRACE("%p increasing refcount to %u.\n", iface, refcount);
234
235 if (refcount == 1)
236 {
237 IDirect3DDevice9Ex_AddRef(declaration->parent_device);
238 wined3d_mutex_lock();
239 wined3d_vertex_declaration_incref(declaration->wined3d_declaration);
240 wined3d_mutex_unlock();
241 }
242
243 return refcount;
244}
245
246static ULONG WINAPI d3d9_vertex_declaration_Release(IDirect3DVertexDeclaration9 *iface)
247{
248 struct d3d9_vertex_declaration *declaration = impl_from_IDirect3DVertexDeclaration9(iface);
249 ULONG refcount = InterlockedDecrement(&declaration->refcount);
250
251 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
252
253 if (!refcount)
254 {
255 IDirect3DDevice9Ex *parent_device = declaration->parent_device;
256 wined3d_mutex_lock();
257 wined3d_vertex_declaration_decref(declaration->wined3d_declaration);
258 wined3d_mutex_unlock();
259
260 /* Release the device last, as it may cause the device to be destroyed. */
261 IDirect3DDevice9Ex_Release(parent_device);
262 }
263
264 return refcount;
265}
266
267static HRESULT WINAPI d3d9_vertex_declaration_GetDevice(IDirect3DVertexDeclaration9 *iface, IDirect3DDevice9 **device)
268{
269 struct d3d9_vertex_declaration *declaration = impl_from_IDirect3DVertexDeclaration9(iface);
270
271 TRACE("iface %p, device %p.\n", iface, device);
272
273 *device = (IDirect3DDevice9 *)declaration->parent_device;
274 IDirect3DDevice9_AddRef(*device);
275
276 TRACE("Returning device %p.\n", *device);
277
278 return D3D_OK;
279}
280
281static HRESULT WINAPI d3d9_vertex_declaration_GetDeclaration(IDirect3DVertexDeclaration9 *iface,
282 D3DVERTEXELEMENT9 *elements, UINT *element_count)
283{
284 struct d3d9_vertex_declaration *declaration = impl_from_IDirect3DVertexDeclaration9(iface);
285
286 TRACE("iface %p, elements %p, element_count %p.\n", iface, elements, element_count);
287
288 *element_count = declaration->element_count;
289
290 /* Passing a NULL elements is used to just retrieve the number of elements */
291 if (!elements)
292 return D3D_OK;
293
294 TRACE("Copying %p to %p.\n", declaration->elements, elements);
295 memcpy(elements, declaration->elements, sizeof(*declaration->elements) * declaration->element_count);
296
297 return D3D_OK;
298}
299
300static const struct IDirect3DVertexDeclaration9Vtbl d3d9_vertex_declaration_vtbl =
301{
302 /* IUnknown */
303 d3d9_vertex_declaration_QueryInterface,
304 d3d9_vertex_declaration_AddRef,
305 d3d9_vertex_declaration_Release,
306 /* IDirect3DVertexDeclaration9 */
307 d3d9_vertex_declaration_GetDevice,
308 d3d9_vertex_declaration_GetDeclaration,
309};
310
311struct d3d9_vertex_declaration *unsafe_impl_from_IDirect3DVertexDeclaration9(IDirect3DVertexDeclaration9 *iface)
312{
313 if (!iface)
314 return NULL;
315 assert(iface->lpVtbl == &d3d9_vertex_declaration_vtbl);
316 return CONTAINING_RECORD(iface, struct d3d9_vertex_declaration, IDirect3DVertexDeclaration9_iface);
317}
318
319static void STDMETHODCALLTYPE d3d9_vertexdeclaration_wined3d_object_destroyed(void *parent)
320{
321 struct d3d9_vertex_declaration *declaration = parent;
322 HeapFree(GetProcessHeap(), 0, declaration->elements);
323 HeapFree(GetProcessHeap(), 0, declaration);
324}
325
326static const struct wined3d_parent_ops d3d9_vertexdeclaration_wined3d_parent_ops =
327{
328 d3d9_vertexdeclaration_wined3d_object_destroyed,
329};
330
331static HRESULT convert_to_wined3d_declaration(const D3DVERTEXELEMENT9 *d3d9_elements,
332 struct wined3d_vertex_element **wined3d_elements, UINT *element_count)
333{
334 const D3DVERTEXELEMENT9* element;
335 UINT count = 1;
336 UINT i;
337
338 TRACE("d3d9_elements %p, wined3d_elements %p\n", d3d9_elements, wined3d_elements);
339
340 element = d3d9_elements;
341 while (element++->Stream != 0xff && count++ < 128);
342
343 if (count == 128) return E_FAIL;
344
345 /* Skip the END element */
346 --count;
347
348 *wined3d_elements = HeapAlloc(GetProcessHeap(), 0, count * sizeof(**wined3d_elements));
349 if (!*wined3d_elements) {
350 FIXME("Memory allocation failed\n");
351 return D3DERR_OUTOFVIDEOMEMORY;
352 }
353
354 for (i = 0; i < count; ++i)
355 {
356 if (d3d9_elements[i].Type >= (sizeof(d3d_dtype_lookup) / sizeof(*d3d_dtype_lookup)))
357 {
358 WARN("Invalid element type %#x.\n", d3d9_elements[i].Type);
359 HeapFree(GetProcessHeap(), 0, *wined3d_elements);
360 return E_FAIL;
361 }
362 (*wined3d_elements)[i].format = d3d_dtype_lookup[d3d9_elements[i].Type].format;
363 (*wined3d_elements)[i].input_slot = d3d9_elements[i].Stream;
364 (*wined3d_elements)[i].offset = d3d9_elements[i].Offset;
365 (*wined3d_elements)[i].output_slot = ~0U;
366 (*wined3d_elements)[i].method = d3d9_elements[i].Method;
367 (*wined3d_elements)[i].usage = d3d9_elements[i].Usage;
368 (*wined3d_elements)[i].usage_idx = d3d9_elements[i].UsageIndex;
369 }
370
371 *element_count = count;
372
373 return D3D_OK;
374}
375
376static HRESULT vertexdeclaration_init(struct d3d9_vertex_declaration *declaration,
377 struct d3d9_device *device, const D3DVERTEXELEMENT9 *elements)
378{
379 struct wined3d_vertex_element *wined3d_elements;
380 UINT wined3d_element_count;
381 UINT element_count;
382 HRESULT hr;
383
384 hr = convert_to_wined3d_declaration(elements, &wined3d_elements, &wined3d_element_count);
385 if (FAILED(hr))
386 {
387 WARN("Failed to create wined3d vertex declaration elements, hr %#x.\n", hr);
388 return hr;
389 }
390
391 declaration->IDirect3DVertexDeclaration9_iface.lpVtbl = (struct IDirect3DVertexDeclaration9Vtbl *)&d3d9_vertex_declaration_vtbl;
392 declaration->refcount = 1;
393
394 element_count = wined3d_element_count + 1;
395 declaration->elements = HeapAlloc(GetProcessHeap(), 0, element_count * sizeof(*declaration->elements));
396 if (!declaration->elements)
397 {
398 HeapFree(GetProcessHeap(), 0, wined3d_elements);
399 ERR("Failed to allocate vertex declaration elements memory.\n");
400 return D3DERR_OUTOFVIDEOMEMORY;
401 }
402 memcpy(declaration->elements, elements, element_count * sizeof(*elements));
403 declaration->element_count = element_count;
404
405 wined3d_mutex_lock();
406 hr = wined3d_vertex_declaration_create(device->wined3d_device, wined3d_elements, wined3d_element_count,
407 declaration, &d3d9_vertexdeclaration_wined3d_parent_ops, &declaration->wined3d_declaration);
408 wined3d_mutex_unlock();
409 HeapFree(GetProcessHeap(), 0, wined3d_elements);
410 if (FAILED(hr))
411 {
412 HeapFree(GetProcessHeap(), 0, declaration->elements);
413 WARN("Failed to create wined3d vertex declaration, hr %#x.\n", hr);
414 return hr;
415 }
416
417 declaration->parent_device = &device->IDirect3DDevice9Ex_iface;
418 IDirect3DDevice9Ex_AddRef(declaration->parent_device);
419
420 return D3D_OK;
421}
422
423HRESULT d3d9_vertex_declaration_create(struct d3d9_device *device,
424 const D3DVERTEXELEMENT9 *elements, struct d3d9_vertex_declaration **declaration)
425{
426 struct d3d9_vertex_declaration *object;
427 HRESULT hr;
428
429 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
430 if (!object)
431 return E_OUTOFMEMORY;
432
433 hr = vertexdeclaration_init(object, device, elements);
434 if (FAILED(hr))
435 {
436 WARN("Failed to initialize vertex declaration, hr %#x.\n", hr);
437 HeapFree(GetProcessHeap(), 0, object);
438 return hr;
439 }
440
441 TRACE("Created vertex declaration %p.\n", object);
442 *declaration = object;
443
444 return D3D_OK;
445}
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