ntdll: Implement NtCreateDebugObject().
[wine.git] / dlls / wined3d / vertexdeclaration.c
blobc5df45541e4d7a8dbf2cd5d62093de61a60215a6
1 /*
2 * vertex declaration implementation
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
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.
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.
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
25 #include "config.h"
26 #include "wine/port.h"
27 #include "wined3d_private.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(d3d_decl);
31 static void dump_wined3d_vertex_element(const struct wined3d_vertex_element *element)
33 TRACE(" format: %s (%#x)\n", debug_d3dformat(element->format), element->format);
34 TRACE(" input_slot: %u\n", element->input_slot);
35 TRACE(" offset: %u\n", element->offset);
36 TRACE(" output_slot: %u\n", element->output_slot);
37 TRACE(" input slot class: %s\n", debug_d3dinput_classification(element->input_slot_class));
38 TRACE("instance data step rate: %u\n", element->instance_data_step_rate);
39 TRACE(" method: %s (%#x)\n", debug_d3ddeclmethod(element->method), element->method);
40 TRACE(" usage: %s (%#x)\n", debug_d3ddeclusage(element->usage), element->usage);
41 TRACE(" usage_idx: %u\n", element->usage_idx);
44 ULONG CDECL wined3d_vertex_declaration_incref(struct wined3d_vertex_declaration *declaration)
46 ULONG refcount = InterlockedIncrement(&declaration->ref);
48 TRACE("%p increasing refcount to %u.\n", declaration, refcount);
50 return refcount;
53 static void wined3d_vertex_declaration_destroy_object(void *object)
55 struct wined3d_vertex_declaration *declaration = object;
57 heap_free(declaration->elements);
58 heap_free(declaration);
61 ULONG CDECL wined3d_vertex_declaration_decref(struct wined3d_vertex_declaration *declaration)
63 ULONG refcount = InterlockedDecrement(&declaration->ref);
65 TRACE("%p decreasing refcount to %u.\n", declaration, refcount);
67 if (!refcount)
69 declaration->parent_ops->wined3d_object_destroyed(declaration->parent);
70 wined3d_cs_destroy_object(declaration->device->cs,
71 wined3d_vertex_declaration_destroy_object, declaration);
74 return refcount;
77 void * CDECL wined3d_vertex_declaration_get_parent(const struct wined3d_vertex_declaration *declaration)
79 TRACE("declaration %p.\n", declaration);
81 return declaration->parent;
84 static BOOL declaration_element_valid_ffp(const struct wined3d_vertex_element *element)
86 switch(element->usage)
88 case WINED3D_DECL_USAGE_POSITION:
89 case WINED3D_DECL_USAGE_POSITIONT:
90 switch(element->format)
92 case WINED3DFMT_R32G32_FLOAT:
93 case WINED3DFMT_R32G32B32_FLOAT:
94 case WINED3DFMT_R32G32B32A32_FLOAT:
95 case WINED3DFMT_R16G16_SINT:
96 case WINED3DFMT_R16G16B16A16_SINT:
97 case WINED3DFMT_R16G16_FLOAT:
98 case WINED3DFMT_R16G16B16A16_FLOAT:
99 return TRUE;
100 default:
101 return FALSE;
104 case WINED3D_DECL_USAGE_BLEND_WEIGHT:
105 switch(element->format)
107 case WINED3DFMT_R32_FLOAT:
108 case WINED3DFMT_R32G32_FLOAT:
109 case WINED3DFMT_R32G32B32_FLOAT:
110 case WINED3DFMT_R32G32B32A32_FLOAT:
111 case WINED3DFMT_B8G8R8A8_UNORM:
112 case WINED3DFMT_R8G8B8A8_UINT:
113 case WINED3DFMT_R16G16_SINT:
114 case WINED3DFMT_R16G16B16A16_SINT:
115 case WINED3DFMT_R16G16_FLOAT:
116 case WINED3DFMT_R16G16B16A16_FLOAT:
117 return TRUE;
118 default:
119 return FALSE;
122 case WINED3D_DECL_USAGE_NORMAL:
123 switch(element->format)
125 case WINED3DFMT_R32G32B32_FLOAT:
126 case WINED3DFMT_R32G32B32A32_FLOAT:
127 case WINED3DFMT_R16G16B16A16_SINT:
128 case WINED3DFMT_R16G16B16A16_FLOAT:
129 return TRUE;
130 default:
131 return FALSE;
134 case WINED3D_DECL_USAGE_TEXCOORD:
135 switch(element->format)
137 case WINED3DFMT_R32_FLOAT:
138 case WINED3DFMT_R32G32_FLOAT:
139 case WINED3DFMT_R32G32B32_FLOAT:
140 case WINED3DFMT_R32G32B32A32_FLOAT:
141 case WINED3DFMT_R16G16_SINT:
142 case WINED3DFMT_R16G16B16A16_SINT:
143 case WINED3DFMT_R16G16_FLOAT:
144 case WINED3DFMT_R16G16B16A16_FLOAT:
145 return TRUE;
146 default:
147 return FALSE;
150 case WINED3D_DECL_USAGE_COLOR:
151 switch(element->format)
153 case WINED3DFMT_R32G32B32_FLOAT:
154 case WINED3DFMT_R32G32B32A32_FLOAT:
155 case WINED3DFMT_B8G8R8A8_UNORM:
156 case WINED3DFMT_R8G8B8A8_UINT:
157 case WINED3DFMT_R16G16B16A16_SINT:
158 case WINED3DFMT_R8G8B8A8_UNORM:
159 case WINED3DFMT_R16G16B16A16_SNORM:
160 case WINED3DFMT_R16G16B16A16_UNORM:
161 case WINED3DFMT_R16G16B16A16_FLOAT:
162 return TRUE;
163 default:
164 return FALSE;
167 default:
168 return FALSE;
172 static HRESULT vertexdeclaration_init(struct wined3d_vertex_declaration *declaration,
173 struct wined3d_device *device, const struct wined3d_vertex_element *elements, UINT element_count,
174 void *parent, const struct wined3d_parent_ops *parent_ops)
176 const struct wined3d_adapter *adapter = device->adapter;
177 unsigned int i;
179 if (TRACE_ON(d3d_decl))
181 for (i = 0; i < element_count; ++i)
183 dump_wined3d_vertex_element(elements + i);
187 declaration->ref = 1;
188 declaration->parent = parent;
189 declaration->parent_ops = parent_ops;
190 declaration->device = device;
191 if (!(declaration->elements = heap_calloc(element_count, sizeof(*declaration->elements))))
193 ERR("Failed to allocate elements memory.\n");
194 return E_OUTOFMEMORY;
196 declaration->element_count = element_count;
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)
202 struct wined3d_vertex_declaration_element *e = &declaration->elements[i];
204 e->format = wined3d_get_format(adapter, elements[i].format, 0);
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->input_slot_class = elements[i].input_slot_class;
210 e->instance_data_step_rate = elements[i].instance_data_step_rate;
211 e->method = elements[i].method;
212 e->usage = elements[i].usage;
213 e->usage_idx = elements[i].usage_idx;
215 if (e->usage == WINED3D_DECL_USAGE_POSITIONT)
216 declaration->position_transformed = TRUE;
218 /* Find the streams used in the declaration. The vertex buffers have
219 * to be loaded when drawing, but filter tessellation pseudo streams. */
220 if (e->input_slot >= WINED3D_MAX_STREAMS)
221 continue;
223 if (!(e->format->flags[WINED3D_GL_RES_TYPE_BUFFER] & WINED3DFMT_FLAG_VERTEX_ATTRIBUTE))
225 FIXME("The application tries to use an unsupported format (%s), returning E_FAIL.\n",
226 debug_d3dformat(elements[i].format));
227 heap_free(declaration->elements);
228 return E_FAIL;
231 if (e->offset == WINED3D_APPEND_ALIGNED_ELEMENT)
233 const struct wined3d_vertex_declaration_element *prev;
234 unsigned int j;
236 e->offset = 0;
237 for (j = 1; j <= i; ++j)
239 prev = &declaration->elements[i - j];
240 if (prev->input_slot == e->input_slot)
242 e->offset = (prev->offset + prev->format->byte_count + 3) & ~3;
243 break;
248 if (e->offset & 0x3)
250 WARN("Declaration element %u is not 4 byte aligned(%u), returning E_FAIL.\n", i, e->offset);
251 heap_free(declaration->elements);
252 return E_FAIL;
256 return WINED3D_OK;
259 HRESULT CDECL wined3d_vertex_declaration_create(struct wined3d_device *device,
260 const struct wined3d_vertex_element *elements, UINT element_count, void *parent,
261 const struct wined3d_parent_ops *parent_ops, struct wined3d_vertex_declaration **declaration)
263 struct wined3d_vertex_declaration *object;
264 HRESULT hr;
266 TRACE("device %p, elements %p, element_count %u, parent %p, parent_ops %p, declaration %p.\n",
267 device, elements, element_count, parent, parent_ops, declaration);
269 if (!(object = heap_alloc_zero(sizeof(*object))))
270 return E_OUTOFMEMORY;
272 hr = vertexdeclaration_init(object, device, elements, element_count, parent, parent_ops);
273 if (FAILED(hr))
275 WARN("Failed to initialize vertex declaration, hr %#x.\n", hr);
276 heap_free(object);
277 return hr;
280 TRACE("Created vertex declaration %p.\n", object);
281 *declaration = object;
283 return WINED3D_OK;
286 struct wined3d_fvf_convert_state
288 const struct wined3d_adapter *adapter;
289 struct wined3d_vertex_element *elements;
290 unsigned int offset;
291 unsigned int idx;
294 static void append_decl_element(struct wined3d_fvf_convert_state *state,
295 enum wined3d_format_id format_id, enum wined3d_decl_usage usage, UINT usage_idx)
297 struct wined3d_vertex_element *elements = state->elements;
298 const struct wined3d_format *format;
299 UINT offset = state->offset;
300 UINT idx = state->idx;
302 elements[idx].format = format_id;
303 elements[idx].input_slot = 0;
304 elements[idx].offset = offset;
305 elements[idx].output_slot = WINED3D_OUTPUT_SLOT_SEMANTIC;
306 elements[idx].input_slot_class = WINED3D_INPUT_PER_VERTEX_DATA;
307 elements[idx].instance_data_step_rate = 0;
308 elements[idx].method = WINED3D_DECL_METHOD_DEFAULT;
309 elements[idx].usage = usage;
310 elements[idx].usage_idx = usage_idx;
312 format = wined3d_get_format(state->adapter, format_id, 0);
313 state->offset += format->byte_count;
314 ++state->idx;
317 static unsigned int convert_fvf_to_declaration(const struct wined3d_adapter *adapter,
318 DWORD fvf, struct wined3d_vertex_element **elements)
320 BOOL has_pos = !!(fvf & WINED3DFVF_POSITION_MASK);
321 BOOL has_blend = (fvf & WINED3DFVF_XYZB5) > WINED3DFVF_XYZRHW;
322 BOOL has_blend_idx = has_blend &&
323 (((fvf & WINED3DFVF_XYZB5) == WINED3DFVF_XYZB5) ||
324 (fvf & WINED3DFVF_LASTBETA_D3DCOLOR) ||
325 (fvf & WINED3DFVF_LASTBETA_UBYTE4));
326 BOOL has_normal = !!(fvf & WINED3DFVF_NORMAL);
327 BOOL has_psize = !!(fvf & WINED3DFVF_PSIZE);
328 BOOL has_diffuse = !!(fvf & WINED3DFVF_DIFFUSE);
329 BOOL has_specular = !!(fvf & WINED3DFVF_SPECULAR);
331 DWORD num_textures = (fvf & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
332 DWORD texcoords = (fvf & 0xffff0000) >> 16;
333 struct wined3d_fvf_convert_state state;
334 unsigned int size;
335 unsigned int idx;
336 DWORD num_blends = 1 + (((fvf & WINED3DFVF_XYZB5) - WINED3DFVF_XYZB1) >> 1);
337 if (has_blend_idx) num_blends--;
339 /* Compute declaration size */
340 size = has_pos + (has_blend && num_blends > 0) + has_blend_idx + has_normal +
341 has_psize + has_diffuse + has_specular + num_textures;
343 state.adapter = adapter;
344 if (!(state.elements = heap_calloc(size, sizeof(*state.elements))))
345 return ~0u;
346 state.offset = 0;
347 state.idx = 0;
349 if (has_pos)
351 if (!has_blend && (fvf & WINED3DFVF_XYZRHW))
352 append_decl_element(&state, WINED3DFMT_R32G32B32A32_FLOAT, WINED3D_DECL_USAGE_POSITIONT, 0);
353 else if ((fvf & WINED3DFVF_XYZW) == WINED3DFVF_XYZW)
354 append_decl_element(&state, WINED3DFMT_R32G32B32A32_FLOAT, WINED3D_DECL_USAGE_POSITION, 0);
355 else
356 append_decl_element(&state, WINED3DFMT_R32G32B32_FLOAT, WINED3D_DECL_USAGE_POSITION, 0);
359 if (has_blend && (num_blends > 0))
361 if ((fvf & WINED3DFVF_XYZB5) == WINED3DFVF_XYZB2 && (fvf & WINED3DFVF_LASTBETA_D3DCOLOR))
362 append_decl_element(&state, WINED3DFMT_B8G8R8A8_UNORM, WINED3D_DECL_USAGE_BLEND_WEIGHT, 0);
363 else
365 switch (num_blends)
367 case 1:
368 append_decl_element(&state, WINED3DFMT_R32_FLOAT, WINED3D_DECL_USAGE_BLEND_WEIGHT, 0);
369 break;
370 case 2:
371 append_decl_element(&state, WINED3DFMT_R32G32_FLOAT, WINED3D_DECL_USAGE_BLEND_WEIGHT, 0);
372 break;
373 case 3:
374 append_decl_element(&state, WINED3DFMT_R32G32B32_FLOAT, WINED3D_DECL_USAGE_BLEND_WEIGHT, 0);
375 break;
376 case 4:
377 append_decl_element(&state, WINED3DFMT_R32G32B32A32_FLOAT, WINED3D_DECL_USAGE_BLEND_WEIGHT, 0);
378 break;
379 default:
380 ERR("Unexpected amount of blend values: %u\n", num_blends);
385 if (has_blend_idx)
387 if ((fvf & WINED3DFVF_LASTBETA_UBYTE4)
388 || ((fvf & WINED3DFVF_XYZB5) == WINED3DFVF_XYZB2 && (fvf & WINED3DFVF_LASTBETA_D3DCOLOR)))
389 append_decl_element(&state, WINED3DFMT_R8G8B8A8_UINT, WINED3D_DECL_USAGE_BLEND_INDICES, 0);
390 else if (fvf & WINED3DFVF_LASTBETA_D3DCOLOR)
391 append_decl_element(&state, WINED3DFMT_B8G8R8A8_UNORM, WINED3D_DECL_USAGE_BLEND_INDICES, 0);
392 else
393 append_decl_element(&state, WINED3DFMT_R32_FLOAT, WINED3D_DECL_USAGE_BLEND_INDICES, 0);
396 if (has_normal)
397 append_decl_element(&state, WINED3DFMT_R32G32B32_FLOAT, WINED3D_DECL_USAGE_NORMAL, 0);
398 if (has_psize)
399 append_decl_element(&state, WINED3DFMT_R32_FLOAT, WINED3D_DECL_USAGE_PSIZE, 0);
400 if (has_diffuse)
401 append_decl_element(&state, WINED3DFMT_B8G8R8A8_UNORM, WINED3D_DECL_USAGE_COLOR, 0);
402 if (has_specular)
403 append_decl_element(&state, WINED3DFMT_B8G8R8A8_UNORM, WINED3D_DECL_USAGE_COLOR, 1);
405 for (idx = 0; idx < num_textures; ++idx)
407 switch ((texcoords >> (idx * 2)) & 0x03)
409 case WINED3DFVF_TEXTUREFORMAT1:
410 append_decl_element(&state, WINED3DFMT_R32_FLOAT, WINED3D_DECL_USAGE_TEXCOORD, idx);
411 break;
412 case WINED3DFVF_TEXTUREFORMAT2:
413 append_decl_element(&state, WINED3DFMT_R32G32_FLOAT, WINED3D_DECL_USAGE_TEXCOORD, idx);
414 break;
415 case WINED3DFVF_TEXTUREFORMAT3:
416 append_decl_element(&state, WINED3DFMT_R32G32B32_FLOAT, WINED3D_DECL_USAGE_TEXCOORD, idx);
417 break;
418 case WINED3DFVF_TEXTUREFORMAT4:
419 append_decl_element(&state, WINED3DFMT_R32G32B32A32_FLOAT, WINED3D_DECL_USAGE_TEXCOORD, idx);
420 break;
424 *elements = state.elements;
425 return size;
428 HRESULT CDECL wined3d_vertex_declaration_create_from_fvf(struct wined3d_device *device,
429 DWORD fvf, void *parent, const struct wined3d_parent_ops *parent_ops,
430 struct wined3d_vertex_declaration **declaration)
432 struct wined3d_vertex_element *elements;
433 unsigned int size;
434 DWORD hr;
436 TRACE("device %p, fvf %#x, parent %p, parent_ops %p, declaration %p.\n",
437 device, fvf, parent, parent_ops, declaration);
439 size = convert_fvf_to_declaration(device->adapter, fvf, &elements);
440 if (size == ~0u)
441 return E_OUTOFMEMORY;
443 hr = wined3d_vertex_declaration_create(device, elements, size, parent, parent_ops, declaration);
444 heap_free(elements);
445 return hr;