VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/Graphics/Wine_new/wined3d/vertexdeclaration.c@ 48345

Last change on this file since 48345 was 48345, checked in by vboxsync, 11 years ago

header fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.0 KB
Line 
1/*
2 * vertex declaration implementation
3 *
4 * Copyright 2002-2005 Raphael Junqueira
5 * Copyright 2004 Jason Edmeades
6 * Copyright 2004 Christian Costa
7 * Copyright 2005 Oliver Stieber
8 * Copyright 2009 Henri Verbeet for CodeWeavers
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
25/*
26 * Oracle LGPL Disclaimer: For the avoidance of doubt, except that if any license choice
27 * other than GPL or LGPL is available it will apply instead, Oracle elects to use only
28 * the Lesser General Public License version 2.1 (LGPLv2) at this time for any software where
29 * a choice of LGPL license versions is made available with the language indicating
30 * that LGPLv2 or any later version may be used, or where a choice of which version
31 * of the LGPL is applied is otherwise unspecified.
32 */
33
34#include "config.h"
35#include "wine/port.h"
36#include "wined3d_private.h"
37
38WINE_DEFAULT_DEBUG_CHANNEL(d3d_decl);
39
40static void dump_wined3d_vertex_element(const struct wined3d_vertex_element *element)
41{
42 TRACE(" format: %s (%#x)\n", debug_d3dformat(element->format), element->format);
43 TRACE(" input_slot: %u\n", element->input_slot);
44 TRACE(" offset: %u\n", element->offset);
45 TRACE("output_slot: %u\n", element->output_slot);
46 TRACE(" method: %s (%#x)\n", debug_d3ddeclmethod(element->method), element->method);
47 TRACE(" usage: %s (%#x)\n", debug_d3ddeclusage(element->usage), element->usage);
48 TRACE(" usage_idx: %u\n", element->usage_idx);
49}
50
51ULONG CDECL wined3d_vertex_declaration_incref(struct wined3d_vertex_declaration *declaration)
52{
53 ULONG refcount = InterlockedIncrement(&declaration->ref);
54
55 TRACE("%p increasing refcount to %u.\n", declaration, refcount);
56
57 return refcount;
58}
59
60ULONG CDECL wined3d_vertex_declaration_decref(struct wined3d_vertex_declaration *declaration)
61{
62 ULONG refcount = InterlockedDecrement(&declaration->ref);
63
64 TRACE("%p decreasing refcount to %u.\n", declaration, refcount);
65
66 if (!refcount)
67 {
68 HeapFree(GetProcessHeap(), 0, declaration->elements);
69 declaration->parent_ops->wined3d_object_destroyed(declaration->parent);
70 HeapFree(GetProcessHeap(), 0, declaration);
71 }
72
73 return refcount;
74}
75
76void * CDECL wined3d_vertex_declaration_get_parent(const struct wined3d_vertex_declaration *declaration)
77{
78 TRACE("declaration %p.\n", declaration);
79
80 return declaration->parent;
81}
82
83static BOOL declaration_element_valid_ffp(const struct wined3d_vertex_element *element)
84{
85 switch(element->usage)
86 {
87 case WINED3D_DECL_USAGE_POSITION:
88 case WINED3D_DECL_USAGE_POSITIONT:
89 switch(element->format)
90 {
91 case WINED3DFMT_R32G32_FLOAT:
92 case WINED3DFMT_R32G32B32_FLOAT:
93 case WINED3DFMT_R32G32B32A32_FLOAT:
94 case WINED3DFMT_R16G16_SINT:
95 case WINED3DFMT_R16G16B16A16_SINT:
96 case WINED3DFMT_R16G16_FLOAT:
97 case WINED3DFMT_R16G16B16A16_FLOAT:
98 return TRUE;
99 default:
100 return FALSE;
101 }
102
103 case WINED3D_DECL_USAGE_BLEND_WEIGHT:
104 switch(element->format)
105 {
106 case WINED3DFMT_R32_FLOAT:
107 case WINED3DFMT_R32G32_FLOAT:
108 case WINED3DFMT_R32G32B32_FLOAT:
109 case WINED3DFMT_R32G32B32A32_FLOAT:
110 case WINED3DFMT_B8G8R8A8_UNORM:
111 case WINED3DFMT_R8G8B8A8_UINT:
112 case WINED3DFMT_R16G16_SINT:
113 case WINED3DFMT_R16G16B16A16_SINT:
114 case WINED3DFMT_R16G16_FLOAT:
115 case WINED3DFMT_R16G16B16A16_FLOAT:
116 return TRUE;
117 default:
118 return FALSE;
119 }
120
121 case WINED3D_DECL_USAGE_NORMAL:
122 switch(element->format)
123 {
124 case WINED3DFMT_R32G32B32_FLOAT:
125 case WINED3DFMT_R32G32B32A32_FLOAT:
126 case WINED3DFMT_R16G16B16A16_SINT:
127 case WINED3DFMT_R16G16B16A16_FLOAT:
128 return TRUE;
129 default:
130 return FALSE;
131 }
132
133 case WINED3D_DECL_USAGE_TEXCOORD:
134 switch(element->format)
135 {
136 case WINED3DFMT_R32_FLOAT:
137 case WINED3DFMT_R32G32_FLOAT:
138 case WINED3DFMT_R32G32B32_FLOAT:
139 case WINED3DFMT_R32G32B32A32_FLOAT:
140 case WINED3DFMT_R16G16_SINT:
141 case WINED3DFMT_R16G16B16A16_SINT:
142 case WINED3DFMT_R16G16_FLOAT:
143 case WINED3DFMT_R16G16B16A16_FLOAT:
144 return TRUE;
145 default:
146 return FALSE;
147 }
148
149 case WINED3D_DECL_USAGE_COLOR:
150 switch(element->format)
151 {
152 case WINED3DFMT_R32G32B32_FLOAT:
153 case WINED3DFMT_R32G32B32A32_FLOAT:
154 case WINED3DFMT_B8G8R8A8_UNORM:
155 case WINED3DFMT_R8G8B8A8_UINT:
156 case WINED3DFMT_R16G16B16A16_SINT:
157 case WINED3DFMT_R8G8B8A8_UNORM:
158 case WINED3DFMT_R16G16B16A16_SNORM:
159 case WINED3DFMT_R16G16B16A16_UNORM:
160 case WINED3DFMT_R16G16B16A16_FLOAT:
161 return TRUE;
162 default:
163 return FALSE;
164 }
165
166 default:
167 return FALSE;
168 }
169}
170
171static HRESULT vertexdeclaration_init(struct wined3d_vertex_declaration *declaration,
172 struct wined3d_device *device, const struct wined3d_vertex_element *elements, UINT element_count,
173 void *parent, const struct wined3d_parent_ops *parent_ops)
174{
175 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
176 unsigned int i;
177
178 if (TRACE_ON(d3d_decl))
179 {
180 for (i = 0; i < element_count; ++i)
181 {
182 dump_wined3d_vertex_element(elements + i);
183 }
184 }
185
186 declaration->ref = 1;
187 declaration->parent = parent;
188 declaration->parent_ops = parent_ops;
189 declaration->device = device;
190 declaration->elements = HeapAlloc(GetProcessHeap(), 0, sizeof(*declaration->elements) * element_count);
191 if (!declaration->elements)
192 {
193 ERR("Failed to allocate elements memory.\n");
194 return E_OUTOFMEMORY;
195 }
196 declaration->element_count = element_count;
197
198 /* Do some static analysis on the elements to make reading the
199 * declaration more comfortable for the drawing code. */
200 for (i = 0; i < element_count; ++i)
201 {
202 struct wined3d_vertex_declaration_element *e = &declaration->elements[i];
203
204 e->format = wined3d_get_format(gl_info, elements[i].format);
205 e->ffp_valid = declaration_element_valid_ffp(&elements[i]);
206 e->input_slot = elements[i].input_slot;
207 e->offset = elements[i].offset;
208 e->output_slot = elements[i].output_slot;
209 e->method = elements[i].method;
210 e->usage = elements[i].usage;
211 e->usage_idx = elements[i].usage_idx;
212
213 if (e->usage == WINED3D_DECL_USAGE_POSITIONT)
214 declaration->position_transformed = TRUE;
215
216 /* Find the streams used in the declaration. The vertex buffers have
217 * to be loaded when drawing, but filter tesselation pseudo streams. */
218 if (e->input_slot >= MAX_STREAMS) continue;
219
220 if (!e->format->gl_vtx_format)
221 {
222 FIXME("The application tries to use an unsupported format (%s), returning E_FAIL.\n",
223 debug_d3dformat(elements[i].format));
224 HeapFree(GetProcessHeap(), 0, declaration->elements);
225 return E_FAIL;
226 }
227
228 if (e->offset & 0x3)
229 {
230 WARN("Declaration element %u is not 4 byte aligned(%u), returning E_FAIL.\n", i, e->offset);
231 HeapFree(GetProcessHeap(), 0, declaration->elements);
232 return E_FAIL;
233 }
234
235 if (elements[i].format == WINED3DFMT_R16G16_FLOAT || elements[i].format == WINED3DFMT_R16G16B16A16_FLOAT)
236 {
237 if (!gl_info->supported[ARB_HALF_FLOAT_VERTEX]) declaration->half_float_conv_needed = TRUE;
238 }
239 }
240
241 return WINED3D_OK;
242}
243
244HRESULT CDECL wined3d_vertex_declaration_create(struct wined3d_device *device,
245 const struct wined3d_vertex_element *elements, UINT element_count, void *parent,
246 const struct wined3d_parent_ops *parent_ops, struct wined3d_vertex_declaration **declaration)
247{
248 struct wined3d_vertex_declaration *object;
249 HRESULT hr;
250
251 TRACE("device %p, elements %p, element_count %u, parent %p, parent_ops %p, declaration %p.\n",
252 device, elements, element_count, parent, parent_ops, declaration);
253
254 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
255 if(!object)
256 return E_OUTOFMEMORY;
257
258 hr = vertexdeclaration_init(object, device, elements, element_count, parent, parent_ops);
259 if (FAILED(hr))
260 {
261 WARN("Failed to initialize vertex declaration, hr %#x.\n", hr);
262 HeapFree(GetProcessHeap(), 0, object);
263 return hr;
264 }
265
266 TRACE("Created vertex declaration %p.\n", object);
267 *declaration = object;
268
269 return WINED3D_OK;
270}
271
272struct wined3d_fvf_convert_state
273{
274 const struct wined3d_gl_info *gl_info;
275 struct wined3d_vertex_element *elements;
276 UINT offset;
277 UINT idx;
278};
279
280static void append_decl_element(struct wined3d_fvf_convert_state *state,
281 enum wined3d_format_id format_id, enum wined3d_decl_usage usage, UINT usage_idx)
282{
283 struct wined3d_vertex_element *elements = state->elements;
284 const struct wined3d_format *format;
285 UINT offset = state->offset;
286 UINT idx = state->idx;
287
288 elements[idx].format = format_id;
289 elements[idx].input_slot = 0;
290 elements[idx].offset = offset;
291 elements[idx].output_slot = 0;
292 elements[idx].method = WINED3D_DECL_METHOD_DEFAULT;
293 elements[idx].usage = usage;
294 elements[idx].usage_idx = usage_idx;
295
296 format = wined3d_get_format(state->gl_info, format_id);
297 state->offset += format->component_count * format->component_size;
298 ++state->idx;
299}
300
301static unsigned int convert_fvf_to_declaration(const struct wined3d_gl_info *gl_info,
302 DWORD fvf, struct wined3d_vertex_element **elements)
303{
304 BOOL has_pos = !!(fvf & WINED3DFVF_POSITION_MASK);
305 BOOL has_blend = (fvf & WINED3DFVF_XYZB5) > WINED3DFVF_XYZRHW;
306 BOOL has_blend_idx = has_blend &&
307 (((fvf & WINED3DFVF_XYZB5) == WINED3DFVF_XYZB5) ||
308 (fvf & WINED3DFVF_LASTBETA_D3DCOLOR) ||
309 (fvf & WINED3DFVF_LASTBETA_UBYTE4));
310 BOOL has_normal = !!(fvf & WINED3DFVF_NORMAL);
311 BOOL has_psize = !!(fvf & WINED3DFVF_PSIZE);
312 BOOL has_diffuse = !!(fvf & WINED3DFVF_DIFFUSE);
313 BOOL has_specular = !!(fvf & WINED3DFVF_SPECULAR);
314
315 DWORD num_textures = (fvf & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
316 DWORD texcoords = (fvf & 0xffff0000) >> 16;
317 struct wined3d_fvf_convert_state state;
318 unsigned int size;
319 unsigned int idx;
320 DWORD num_blends = 1 + (((fvf & WINED3DFVF_XYZB5) - WINED3DFVF_XYZB1) >> 1);
321 if (has_blend_idx) num_blends--;
322
323 /* Compute declaration size */
324 size = has_pos + (has_blend && num_blends > 0) + has_blend_idx + has_normal +
325 has_psize + has_diffuse + has_specular + num_textures;
326
327 state.gl_info = gl_info;
328 state.elements = HeapAlloc(GetProcessHeap(), 0, size * sizeof(*state.elements));
329 if (!state.elements) return ~0U;
330 state.offset = 0;
331 state.idx = 0;
332
333 if (has_pos)
334 {
335 if (!has_blend && (fvf & WINED3DFVF_XYZRHW))
336 append_decl_element(&state, WINED3DFMT_R32G32B32A32_FLOAT, WINED3D_DECL_USAGE_POSITIONT, 0);
337 else if ((fvf & WINED3DFVF_XYZW) == WINED3DFVF_XYZW)
338 append_decl_element(&state, WINED3DFMT_R32G32B32A32_FLOAT, WINED3D_DECL_USAGE_POSITION, 0);
339 else
340 append_decl_element(&state, WINED3DFMT_R32G32B32_FLOAT, WINED3D_DECL_USAGE_POSITION, 0);
341 }
342
343 if (has_blend && (num_blends > 0))
344 {
345 if ((fvf & WINED3DFVF_XYZB5) == WINED3DFVF_XYZB2 && (fvf & WINED3DFVF_LASTBETA_D3DCOLOR))
346 append_decl_element(&state, WINED3DFMT_B8G8R8A8_UNORM, WINED3D_DECL_USAGE_BLEND_WEIGHT, 0);
347 else
348 {
349 switch (num_blends)
350 {
351 case 1:
352 append_decl_element(&state, WINED3DFMT_R32_FLOAT, WINED3D_DECL_USAGE_BLEND_WEIGHT, 0);
353 break;
354 case 2:
355 append_decl_element(&state, WINED3DFMT_R32G32_FLOAT, WINED3D_DECL_USAGE_BLEND_WEIGHT, 0);
356 break;
357 case 3:
358 append_decl_element(&state, WINED3DFMT_R32G32B32_FLOAT, WINED3D_DECL_USAGE_BLEND_WEIGHT, 0);
359 break;
360 case 4:
361 append_decl_element(&state, WINED3DFMT_R32G32B32A32_FLOAT, WINED3D_DECL_USAGE_BLEND_WEIGHT, 0);
362 break;
363 default:
364 ERR("Unexpected amount of blend values: %u\n", num_blends);
365 }
366 }
367 }
368
369 if (has_blend_idx)
370 {
371 if ((fvf & WINED3DFVF_LASTBETA_UBYTE4)
372 || ((fvf & WINED3DFVF_XYZB5) == WINED3DFVF_XYZB2 && (fvf & WINED3DFVF_LASTBETA_D3DCOLOR)))
373 append_decl_element(&state, WINED3DFMT_R8G8B8A8_UINT, WINED3D_DECL_USAGE_BLEND_INDICES, 0);
374 else if (fvf & WINED3DFVF_LASTBETA_D3DCOLOR)
375 append_decl_element(&state, WINED3DFMT_B8G8R8A8_UNORM, WINED3D_DECL_USAGE_BLEND_INDICES, 0);
376 else
377 append_decl_element(&state, WINED3DFMT_R32_FLOAT, WINED3D_DECL_USAGE_BLEND_INDICES, 0);
378 }
379
380 if (has_normal)
381 append_decl_element(&state, WINED3DFMT_R32G32B32_FLOAT, WINED3D_DECL_USAGE_NORMAL, 0);
382 if (has_psize)
383 append_decl_element(&state, WINED3DFMT_R32_FLOAT, WINED3D_DECL_USAGE_PSIZE, 0);
384 if (has_diffuse)
385 append_decl_element(&state, WINED3DFMT_B8G8R8A8_UNORM, WINED3D_DECL_USAGE_COLOR, 0);
386 if (has_specular)
387 append_decl_element(&state, WINED3DFMT_B8G8R8A8_UNORM, WINED3D_DECL_USAGE_COLOR, 1);
388
389 for (idx = 0; idx < num_textures; ++idx)
390 {
391 switch ((texcoords >> (idx * 2)) & 0x03)
392 {
393 case WINED3DFVF_TEXTUREFORMAT1:
394 append_decl_element(&state, WINED3DFMT_R32_FLOAT, WINED3D_DECL_USAGE_TEXCOORD, idx);
395 break;
396 case WINED3DFVF_TEXTUREFORMAT2:
397 append_decl_element(&state, WINED3DFMT_R32G32_FLOAT, WINED3D_DECL_USAGE_TEXCOORD, idx);
398 break;
399 case WINED3DFVF_TEXTUREFORMAT3:
400 append_decl_element(&state, WINED3DFMT_R32G32B32_FLOAT, WINED3D_DECL_USAGE_TEXCOORD, idx);
401 break;
402 case WINED3DFVF_TEXTUREFORMAT4:
403 append_decl_element(&state, WINED3DFMT_R32G32B32A32_FLOAT, WINED3D_DECL_USAGE_TEXCOORD, idx);
404 break;
405 }
406 }
407
408 *elements = state.elements;
409 return size;
410}
411
412HRESULT CDECL wined3d_vertex_declaration_create_from_fvf(struct wined3d_device *device,
413 DWORD fvf, void *parent, const struct wined3d_parent_ops *parent_ops,
414 struct wined3d_vertex_declaration **declaration)
415{
416 struct wined3d_vertex_element *elements;
417 unsigned int size;
418 DWORD hr;
419
420 TRACE("device %p, fvf %#x, parent %p, parent_ops %p, declaration %p.\n",
421 device, fvf, parent, parent_ops, declaration);
422
423 size = convert_fvf_to_declaration(&device->adapter->gl_info, fvf, &elements);
424 if (size == ~0U) return E_OUTOFMEMORY;
425
426 hr = wined3d_vertex_declaration_create(device, elements, size, parent, parent_ops, declaration);
427 HeapFree(GetProcessHeap(), 0, elements);
428 return hr;
429}
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