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
26 #include "wined3d_private.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(d3d_decl);
30 static void dump_wined3d_vertex_element(const struct wined3d_vertex_element *element)
32 TRACE(" format: %s (%#x)\n", debug_d3dformat(element->format), element->format);
33 TRACE(" input_slot: %u\n", element->input_slot);
34 TRACE(" offset: %u\n", element->offset);
35 TRACE("output_slot: %u\n", element->output_slot);
36 TRACE(" method: %s (%#x)\n", debug_d3ddeclmethod(element->method), element->method);
37 TRACE(" usage: %s (%#x)\n", debug_d3ddeclusage(element->usage), element->usage);
38 TRACE(" usage_idx: %u\n", element->usage_idx);
41 ULONG CDECL wined3d_vertex_declaration_incref(struct wined3d_vertex_declaration *declaration)
43 ULONG refcount = InterlockedIncrement(&declaration->ref);
45 TRACE("%p increasing refcount to %u.\n", declaration, refcount);
50 ULONG CDECL wined3d_vertex_declaration_decref(struct wined3d_vertex_declaration *declaration)
52 ULONG refcount = InterlockedDecrement(&declaration->ref);
54 TRACE("%p decreasing refcount to %u.\n", declaration, refcount);
58 HeapFree(GetProcessHeap(), 0, declaration->elements);
59 declaration->parent_ops->wined3d_object_destroyed(declaration->parent);
60 HeapFree(GetProcessHeap(), 0, declaration);
66 void * CDECL wined3d_vertex_declaration_get_parent(const struct wined3d_vertex_declaration *declaration)
68 TRACE("declaration %p.\n", declaration);
70 return declaration->parent;
73 static BOOL declaration_element_valid_ffp(const struct wined3d_vertex_element *element)
75 switch(element->usage)
77 case WINED3DDECLUSAGE_POSITION:
78 case WINED3DDECLUSAGE_POSITIONT:
79 switch(element->format)
81 case WINED3DFMT_R32G32_FLOAT:
82 case WINED3DFMT_R32G32B32_FLOAT:
83 case WINED3DFMT_R32G32B32A32_FLOAT:
84 case WINED3DFMT_R16G16_SINT:
85 case WINED3DFMT_R16G16B16A16_SINT:
86 case WINED3DFMT_R16G16_FLOAT:
87 case WINED3DFMT_R16G16B16A16_FLOAT:
93 case WINED3DDECLUSAGE_BLENDWEIGHT:
94 switch(element->format)
96 case WINED3DFMT_R32_FLOAT:
97 case WINED3DFMT_R32G32_FLOAT:
98 case WINED3DFMT_R32G32B32_FLOAT:
99 case WINED3DFMT_R32G32B32A32_FLOAT:
100 case WINED3DFMT_B8G8R8A8_UNORM:
101 case WINED3DFMT_R8G8B8A8_UINT:
102 case WINED3DFMT_R16G16_SINT:
103 case WINED3DFMT_R16G16B16A16_SINT:
104 case WINED3DFMT_R16G16_FLOAT:
105 case WINED3DFMT_R16G16B16A16_FLOAT:
111 case WINED3DDECLUSAGE_NORMAL:
112 switch(element->format)
114 case WINED3DFMT_R32G32B32_FLOAT:
115 case WINED3DFMT_R32G32B32A32_FLOAT:
116 case WINED3DFMT_R16G16B16A16_SINT:
117 case WINED3DFMT_R16G16B16A16_FLOAT:
123 case WINED3DDECLUSAGE_TEXCOORD:
124 switch(element->format)
126 case WINED3DFMT_R32_FLOAT:
127 case WINED3DFMT_R32G32_FLOAT:
128 case WINED3DFMT_R32G32B32_FLOAT:
129 case WINED3DFMT_R32G32B32A32_FLOAT:
130 case WINED3DFMT_R16G16_SINT:
131 case WINED3DFMT_R16G16B16A16_SINT:
132 case WINED3DFMT_R16G16_FLOAT:
133 case WINED3DFMT_R16G16B16A16_FLOAT:
139 case WINED3DDECLUSAGE_COLOR:
140 switch(element->format)
142 case WINED3DFMT_R32G32B32_FLOAT:
143 case WINED3DFMT_R32G32B32A32_FLOAT:
144 case WINED3DFMT_B8G8R8A8_UNORM:
145 case WINED3DFMT_R8G8B8A8_UINT:
146 case WINED3DFMT_R16G16B16A16_SINT:
147 case WINED3DFMT_R8G8B8A8_UNORM:
148 case WINED3DFMT_R16G16B16A16_SNORM:
149 case WINED3DFMT_R16G16B16A16_UNORM:
150 case WINED3DFMT_R16G16B16A16_FLOAT:
161 static HRESULT vertexdeclaration_init(struct wined3d_vertex_declaration *declaration,
162 struct wined3d_device *device, const struct wined3d_vertex_element *elements, UINT element_count,
163 void *parent, const struct wined3d_parent_ops *parent_ops)
165 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
166 WORD preloaded = 0; /* MAX_STREAMS, 16 */
169 if (TRACE_ON(d3d_decl))
171 for (i = 0; i < element_count; ++i)
173 dump_wined3d_vertex_element(elements + i);
177 declaration->ref = 1;
178 declaration->parent = parent;
179 declaration->parent_ops = parent_ops;
180 declaration->device = device;
181 declaration->elements = HeapAlloc(GetProcessHeap(), 0, sizeof(*declaration->elements) * element_count);
182 if (!declaration->elements)
184 ERR("Failed to allocate elements memory.\n");
185 return E_OUTOFMEMORY;
187 declaration->element_count = element_count;
189 /* Do some static analysis on the elements to make reading the
190 * declaration more comfortable for the drawing code. */
191 for (i = 0; i < element_count; ++i)
193 struct wined3d_vertex_declaration_element *e = &declaration->elements[i];
195 e->format = wined3d_get_format(gl_info, elements[i].format);
196 e->ffp_valid = declaration_element_valid_ffp(&elements[i]);
197 e->input_slot = elements[i].input_slot;
198 e->offset = elements[i].offset;
199 e->output_slot = elements[i].output_slot;
200 e->method = elements[i].method;
201 e->usage = elements[i].usage;
202 e->usage_idx = elements[i].usage_idx;
204 if (e->usage == WINED3DDECLUSAGE_POSITIONT) declaration->position_transformed = TRUE;
206 /* Find the streams used in the declaration. The vertex buffers have
207 * to be loaded when drawing, but filter tesselation pseudo streams. */
208 if (e->input_slot >= MAX_STREAMS) continue;
210 if (!e->format->gl_vtx_format)
212 FIXME("The application tries to use an unsupported format (%s), returning E_FAIL.\n",
213 debug_d3dformat(elements[i].format));
214 HeapFree(GetProcessHeap(), 0, declaration->elements);
220 WARN("Declaration element %u is not 4 byte aligned(%u), returning E_FAIL.\n", i, e->offset);
221 HeapFree(GetProcessHeap(), 0, declaration->elements);
225 if (!(preloaded & (1 << e->input_slot)))
227 declaration->streams[declaration->num_streams] = e->input_slot;
228 ++declaration->num_streams;
229 preloaded |= 1 << e->input_slot;
232 if (elements[i].format == WINED3DFMT_R16G16_FLOAT || elements[i].format == WINED3DFMT_R16G16B16A16_FLOAT)
234 if (!gl_info->supported[ARB_HALF_FLOAT_VERTEX]) declaration->half_float_conv_needed = TRUE;
241 HRESULT CDECL wined3d_vertex_declaration_create(struct wined3d_device *device,
242 const struct wined3d_vertex_element *elements, UINT element_count, void *parent,
243 const struct wined3d_parent_ops *parent_ops, struct wined3d_vertex_declaration **declaration)
245 struct wined3d_vertex_declaration *object;
248 TRACE("device %p, elements %p, element_count %u, parent %p, parent_ops %p, declaration %p.\n",
249 device, elements, element_count, parent, parent_ops, declaration);
251 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
254 ERR("Failed to allocate vertex declaration memory.\n");
255 return E_OUTOFMEMORY;
258 hr = vertexdeclaration_init(object, device, elements, element_count, parent, parent_ops);
261 WARN("Failed to initialize vertex declaration, hr %#x.\n", hr);
262 HeapFree(GetProcessHeap(), 0, object);
266 TRACE("Created vertex declaration %p.\n", object);
267 *declaration = object;
272 struct wined3d_fvf_convert_state
274 const struct wined3d_gl_info *gl_info;
275 struct wined3d_vertex_element *elements;
280 static void append_decl_element(struct wined3d_fvf_convert_state *state,
281 enum wined3d_format_id format_id, WINED3DDECLUSAGE usage, UINT usage_idx)
283 struct wined3d_vertex_element *elements = state->elements;
284 const struct wined3d_format *format;
285 UINT offset = state->offset;
286 UINT idx = state->idx;
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 = WINED3DDECLMETHOD_DEFAULT;
293 elements[idx].usage = usage;
294 elements[idx].usage_idx = usage_idx;
296 format = wined3d_get_format(state->gl_info, format_id);
297 state->offset += format->component_count * format->component_size;
301 static unsigned int convert_fvf_to_declaration(const struct wined3d_gl_info *gl_info,
302 DWORD fvf, struct wined3d_vertex_element **elements)
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);
315 DWORD num_textures = (fvf & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
316 DWORD texcoords = (fvf & 0xFFFF0000) >> 16;
317 struct wined3d_fvf_convert_state state;
320 DWORD num_blends = 1 + (((fvf & WINED3DFVF_XYZB5) - WINED3DFVF_XYZB1) >> 1);
321 if (has_blend_idx) num_blends--;
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;
327 state.gl_info = gl_info;
328 state.elements = HeapAlloc(GetProcessHeap(), 0, size * sizeof(*state.elements));
329 if (!state.elements) return ~0U;
335 if (!has_blend && (fvf & WINED3DFVF_XYZRHW))
336 append_decl_element(&state, WINED3DFMT_R32G32B32A32_FLOAT, WINED3DDECLUSAGE_POSITIONT, 0);
337 else if ((fvf & WINED3DFVF_XYZW) == WINED3DFVF_XYZW)
338 append_decl_element(&state, WINED3DFMT_R32G32B32A32_FLOAT, WINED3DDECLUSAGE_POSITION, 0);
340 append_decl_element(&state, WINED3DFMT_R32G32B32_FLOAT, WINED3DDECLUSAGE_POSITION, 0);
343 if (has_blend && (num_blends > 0))
345 if ((fvf & WINED3DFVF_XYZB5) == WINED3DFVF_XYZB2 && (fvf & WINED3DFVF_LASTBETA_D3DCOLOR))
346 append_decl_element(&state, WINED3DFMT_B8G8R8A8_UNORM, WINED3DDECLUSAGE_BLENDWEIGHT, 0);
352 append_decl_element(&state, WINED3DFMT_R32_FLOAT, WINED3DDECLUSAGE_BLENDWEIGHT, 0);
355 append_decl_element(&state, WINED3DFMT_R32G32_FLOAT, WINED3DDECLUSAGE_BLENDWEIGHT, 0);
358 append_decl_element(&state, WINED3DFMT_R32G32B32_FLOAT, WINED3DDECLUSAGE_BLENDWEIGHT, 0);
361 append_decl_element(&state, WINED3DFMT_R32G32B32A32_FLOAT, WINED3DDECLUSAGE_BLENDWEIGHT, 0);
364 ERR("Unexpected amount of blend values: %u\n", num_blends);
371 if ((fvf & WINED3DFVF_LASTBETA_UBYTE4)
372 || ((fvf & WINED3DFVF_XYZB5) == WINED3DFVF_XYZB2 && (fvf & WINED3DFVF_LASTBETA_D3DCOLOR)))
373 append_decl_element(&state, WINED3DFMT_R8G8B8A8_UINT, WINED3DDECLUSAGE_BLENDINDICES, 0);
374 else if (fvf & WINED3DFVF_LASTBETA_D3DCOLOR)
375 append_decl_element(&state, WINED3DFMT_B8G8R8A8_UNORM, WINED3DDECLUSAGE_BLENDINDICES, 0);
377 append_decl_element(&state, WINED3DFMT_R32_FLOAT, WINED3DDECLUSAGE_BLENDINDICES, 0);
380 if (has_normal) append_decl_element(&state, WINED3DFMT_R32G32B32_FLOAT, WINED3DDECLUSAGE_NORMAL, 0);
381 if (has_psize) append_decl_element(&state, WINED3DFMT_R32_FLOAT, WINED3DDECLUSAGE_PSIZE, 0);
382 if (has_diffuse) append_decl_element(&state, WINED3DFMT_B8G8R8A8_UNORM, WINED3DDECLUSAGE_COLOR, 0);
383 if (has_specular) append_decl_element(&state, WINED3DFMT_B8G8R8A8_UNORM, WINED3DDECLUSAGE_COLOR, 1);
385 for (idx = 0; idx < num_textures; ++idx)
387 switch ((texcoords >> (idx * 2)) & 0x03)
389 case WINED3DFVF_TEXTUREFORMAT1:
390 append_decl_element(&state, WINED3DFMT_R32_FLOAT, WINED3DDECLUSAGE_TEXCOORD, idx);
392 case WINED3DFVF_TEXTUREFORMAT2:
393 append_decl_element(&state, WINED3DFMT_R32G32_FLOAT, WINED3DDECLUSAGE_TEXCOORD, idx);
395 case WINED3DFVF_TEXTUREFORMAT3:
396 append_decl_element(&state, WINED3DFMT_R32G32B32_FLOAT, WINED3DDECLUSAGE_TEXCOORD, idx);
398 case WINED3DFVF_TEXTUREFORMAT4:
399 append_decl_element(&state, WINED3DFMT_R32G32B32A32_FLOAT, WINED3DDECLUSAGE_TEXCOORD, idx);
404 *elements = state.elements;
408 HRESULT CDECL wined3d_vertex_declaration_create_from_fvf(struct wined3d_device *device,
409 DWORD fvf, void *parent, const struct wined3d_parent_ops *parent_ops,
410 struct wined3d_vertex_declaration **declaration)
412 struct wined3d_vertex_element *elements;
416 TRACE("device %p, fvf %#x, parent %p, parent_ops %p, declaration %p.\n",
417 device, fvf, parent, parent_ops, declaration);
419 size = convert_fvf_to_declaration(&device->adapter->gl_info, fvf, &elements);
420 if (size == ~0U) return E_OUTOFMEMORY;
422 hr = wined3d_vertex_declaration_create(device, elements, size, parent, parent_ops, declaration);
423 HeapFree(GetProcessHeap(), 0, elements);