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_wined3dvertexelement(const WINED3DVERTEXELEMENT *element) {
31 TRACE(" format: %s (%#x)\n", debug_d3dformat(element->format), element->format);
32 TRACE(" input_slot: %u\n", element->input_slot);
33 TRACE(" offset: %u\n", element->offset);
34 TRACE("output_slot: %u\n", element->output_slot);
35 TRACE(" method: %s (%#x)\n", debug_d3ddeclmethod(element->method), element->method);
36 TRACE(" usage: %s (%#x)\n", debug_d3ddeclusage(element->usage), element->usage);
37 TRACE(" usage_idx: %u\n", element->usage_idx);
40 ULONG CDECL wined3d_vertex_declaration_incref(struct wined3d_vertex_declaration *declaration)
42 ULONG refcount = InterlockedIncrement(&declaration->ref);
44 TRACE("%p increasing refcount to %u.\n", declaration, refcount);
49 ULONG CDECL wined3d_vertex_declaration_decref(struct wined3d_vertex_declaration *declaration)
51 ULONG refcount = InterlockedDecrement(&declaration->ref);
53 TRACE("%p decreasing refcount to %u.\n", declaration, refcount);
57 HeapFree(GetProcessHeap(), 0, declaration->elements);
58 declaration->parent_ops->wined3d_object_destroyed(declaration->parent);
59 HeapFree(GetProcessHeap(), 0, declaration);
65 void * CDECL wined3d_vertex_declaration_get_parent(const struct wined3d_vertex_declaration *declaration)
67 TRACE("declaration %p.\n", declaration);
69 return declaration->parent;
72 static BOOL declaration_element_valid_ffp(const WINED3DVERTEXELEMENT *element)
74 switch(element->usage)
76 case WINED3DDECLUSAGE_POSITION:
77 case WINED3DDECLUSAGE_POSITIONT:
78 switch(element->format)
80 case WINED3DFMT_R32G32_FLOAT:
81 case WINED3DFMT_R32G32B32_FLOAT:
82 case WINED3DFMT_R32G32B32A32_FLOAT:
83 case WINED3DFMT_R16G16_SINT:
84 case WINED3DFMT_R16G16B16A16_SINT:
85 case WINED3DFMT_R16G16_FLOAT:
86 case WINED3DFMT_R16G16B16A16_FLOAT:
92 case WINED3DDECLUSAGE_BLENDWEIGHT:
93 switch(element->format)
95 case WINED3DFMT_R32_FLOAT:
96 case WINED3DFMT_R32G32_FLOAT:
97 case WINED3DFMT_R32G32B32_FLOAT:
98 case WINED3DFMT_R32G32B32A32_FLOAT:
99 case WINED3DFMT_B8G8R8A8_UNORM:
100 case WINED3DFMT_R8G8B8A8_UINT:
101 case WINED3DFMT_R16G16_SINT:
102 case WINED3DFMT_R16G16B16A16_SINT:
103 case WINED3DFMT_R16G16_FLOAT:
104 case WINED3DFMT_R16G16B16A16_FLOAT:
110 case WINED3DDECLUSAGE_NORMAL:
111 switch(element->format)
113 case WINED3DFMT_R32G32B32_FLOAT:
114 case WINED3DFMT_R32G32B32A32_FLOAT:
115 case WINED3DFMT_R16G16B16A16_SINT:
116 case WINED3DFMT_R16G16B16A16_FLOAT:
122 case WINED3DDECLUSAGE_TEXCOORD:
123 switch(element->format)
125 case WINED3DFMT_R32_FLOAT:
126 case WINED3DFMT_R32G32_FLOAT:
127 case WINED3DFMT_R32G32B32_FLOAT:
128 case WINED3DFMT_R32G32B32A32_FLOAT:
129 case WINED3DFMT_R16G16_SINT:
130 case WINED3DFMT_R16G16B16A16_SINT:
131 case WINED3DFMT_R16G16_FLOAT:
132 case WINED3DFMT_R16G16B16A16_FLOAT:
138 case WINED3DDECLUSAGE_COLOR:
139 switch(element->format)
141 case WINED3DFMT_R32G32B32_FLOAT:
142 case WINED3DFMT_R32G32B32A32_FLOAT:
143 case WINED3DFMT_B8G8R8A8_UNORM:
144 case WINED3DFMT_R8G8B8A8_UINT:
145 case WINED3DFMT_R16G16B16A16_SINT:
146 case WINED3DFMT_R8G8B8A8_UNORM:
147 case WINED3DFMT_R16G16B16A16_SNORM:
148 case WINED3DFMT_R16G16B16A16_UNORM:
149 case WINED3DFMT_R16G16B16A16_FLOAT:
160 static HRESULT vertexdeclaration_init(struct wined3d_vertex_declaration *declaration,
161 struct wined3d_device *device, const WINED3DVERTEXELEMENT *elements, UINT element_count,
162 void *parent, const struct wined3d_parent_ops *parent_ops)
164 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
165 WORD preloaded = 0; /* MAX_STREAMS, 16 */
168 if (TRACE_ON(d3d_decl))
170 for (i = 0; i < element_count; ++i)
172 dump_wined3dvertexelement(elements + i);
176 declaration->ref = 1;
177 declaration->parent = parent;
178 declaration->parent_ops = parent_ops;
179 declaration->device = device;
180 declaration->elements = HeapAlloc(GetProcessHeap(), 0, sizeof(*declaration->elements) * element_count);
181 if (!declaration->elements)
183 ERR("Failed to allocate elements memory.\n");
184 return E_OUTOFMEMORY;
186 declaration->element_count = element_count;
188 /* Do some static analysis on the elements to make reading the
189 * declaration more comfortable for the drawing code. */
190 for (i = 0; i < element_count; ++i)
192 struct wined3d_vertex_declaration_element *e = &declaration->elements[i];
194 e->format = wined3d_get_format(gl_info, elements[i].format);
195 e->ffp_valid = declaration_element_valid_ffp(&elements[i]);
196 e->input_slot = elements[i].input_slot;
197 e->offset = elements[i].offset;
198 e->output_slot = elements[i].output_slot;
199 e->method = elements[i].method;
200 e->usage = elements[i].usage;
201 e->usage_idx = elements[i].usage_idx;
203 if (e->usage == WINED3DDECLUSAGE_POSITIONT) declaration->position_transformed = TRUE;
205 /* Find the streams used in the declaration. The vertex buffers have
206 * to be loaded when drawing, but filter tesselation pseudo streams. */
207 if (e->input_slot >= MAX_STREAMS) continue;
209 if (!e->format->gl_vtx_format)
211 FIXME("The application tries to use an unsupported format (%s), returning E_FAIL.\n",
212 debug_d3dformat(elements[i].format));
213 HeapFree(GetProcessHeap(), 0, declaration->elements);
219 WARN("Declaration element %u is not 4 byte aligned(%u), returning E_FAIL.\n", i, e->offset);
220 HeapFree(GetProcessHeap(), 0, declaration->elements);
224 if (!(preloaded & (1 << e->input_slot)))
226 declaration->streams[declaration->num_streams] = e->input_slot;
227 ++declaration->num_streams;
228 preloaded |= 1 << e->input_slot;
231 if (elements[i].format == WINED3DFMT_R16G16_FLOAT || elements[i].format == WINED3DFMT_R16G16B16A16_FLOAT)
233 if (!gl_info->supported[ARB_HALF_FLOAT_VERTEX]) declaration->half_float_conv_needed = TRUE;
240 HRESULT CDECL wined3d_vertex_declaration_create(struct wined3d_device *device,
241 const WINED3DVERTEXELEMENT *elements, UINT element_count, void *parent,
242 const struct wined3d_parent_ops *parent_ops, struct wined3d_vertex_declaration **declaration)
244 struct wined3d_vertex_declaration *object;
247 TRACE("device %p, elements %p, element_count %u, parent %p, parent_ops %p, declaration %p.\n",
248 device, elements, element_count, parent, parent_ops, declaration);
250 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
253 ERR("Failed to allocate vertex declaration memory.\n");
254 return E_OUTOFMEMORY;
257 hr = vertexdeclaration_init(object, device, elements, element_count, parent, parent_ops);
260 WARN("Failed to initialize vertex declaration, hr %#x.\n", hr);
261 HeapFree(GetProcessHeap(), 0, object);
265 TRACE("Created vertex declaration %p.\n", object);
266 *declaration = object;
271 struct wined3d_fvf_convert_state
273 const struct wined3d_gl_info *gl_info;
274 WINED3DVERTEXELEMENT *elements;
279 static void append_decl_element(struct wined3d_fvf_convert_state *state,
280 enum wined3d_format_id format_id, WINED3DDECLUSAGE usage, UINT usage_idx)
282 WINED3DVERTEXELEMENT *elements = state->elements;
283 const struct wined3d_format *format;
284 UINT offset = state->offset;
285 UINT idx = state->idx;
287 elements[idx].format = format_id;
288 elements[idx].input_slot = 0;
289 elements[idx].offset = offset;
290 elements[idx].output_slot = 0;
291 elements[idx].method = WINED3DDECLMETHOD_DEFAULT;
292 elements[idx].usage = usage;
293 elements[idx].usage_idx = usage_idx;
295 format = wined3d_get_format(state->gl_info, format_id);
296 state->offset += format->component_count * format->component_size;
300 static unsigned int convert_fvf_to_declaration(const struct wined3d_gl_info *gl_info,
301 DWORD fvf, WINED3DVERTEXELEMENT **elements)
303 BOOL has_pos = !!(fvf & WINED3DFVF_POSITION_MASK);
304 BOOL has_blend = (fvf & WINED3DFVF_XYZB5) > WINED3DFVF_XYZRHW;
305 BOOL has_blend_idx = has_blend &&
306 (((fvf & WINED3DFVF_XYZB5) == WINED3DFVF_XYZB5) ||
307 (fvf & WINED3DFVF_LASTBETA_D3DCOLOR) ||
308 (fvf & WINED3DFVF_LASTBETA_UBYTE4));
309 BOOL has_normal = !!(fvf & WINED3DFVF_NORMAL);
310 BOOL has_psize = !!(fvf & WINED3DFVF_PSIZE);
311 BOOL has_diffuse = !!(fvf & WINED3DFVF_DIFFUSE);
312 BOOL has_specular = !!(fvf & WINED3DFVF_SPECULAR);
314 DWORD num_textures = (fvf & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
315 DWORD texcoords = (fvf & 0xFFFF0000) >> 16;
316 struct wined3d_fvf_convert_state state;
319 DWORD num_blends = 1 + (((fvf & WINED3DFVF_XYZB5) - WINED3DFVF_XYZB1) >> 1);
320 if (has_blend_idx) num_blends--;
322 /* Compute declaration size */
323 size = has_pos + (has_blend && num_blends > 0) + has_blend_idx + has_normal +
324 has_psize + has_diffuse + has_specular + num_textures;
326 state.gl_info = gl_info;
327 state.elements = HeapAlloc(GetProcessHeap(), 0, size * sizeof(*state.elements));
328 if (!state.elements) return ~0U;
334 if (!has_blend && (fvf & WINED3DFVF_XYZRHW))
335 append_decl_element(&state, WINED3DFMT_R32G32B32A32_FLOAT, WINED3DDECLUSAGE_POSITIONT, 0);
336 else if ((fvf & WINED3DFVF_XYZW) == WINED3DFVF_XYZW)
337 append_decl_element(&state, WINED3DFMT_R32G32B32A32_FLOAT, WINED3DDECLUSAGE_POSITION, 0);
339 append_decl_element(&state, WINED3DFMT_R32G32B32_FLOAT, WINED3DDECLUSAGE_POSITION, 0);
342 if (has_blend && (num_blends > 0))
344 if ((fvf & WINED3DFVF_XYZB5) == WINED3DFVF_XYZB2 && (fvf & WINED3DFVF_LASTBETA_D3DCOLOR))
345 append_decl_element(&state, WINED3DFMT_B8G8R8A8_UNORM, WINED3DDECLUSAGE_BLENDWEIGHT, 0);
351 append_decl_element(&state, WINED3DFMT_R32_FLOAT, WINED3DDECLUSAGE_BLENDWEIGHT, 0);
354 append_decl_element(&state, WINED3DFMT_R32G32_FLOAT, WINED3DDECLUSAGE_BLENDWEIGHT, 0);
357 append_decl_element(&state, WINED3DFMT_R32G32B32_FLOAT, WINED3DDECLUSAGE_BLENDWEIGHT, 0);
360 append_decl_element(&state, WINED3DFMT_R32G32B32A32_FLOAT, WINED3DDECLUSAGE_BLENDWEIGHT, 0);
363 ERR("Unexpected amount of blend values: %u\n", num_blends);
370 if ((fvf & WINED3DFVF_LASTBETA_UBYTE4)
371 || ((fvf & WINED3DFVF_XYZB5) == WINED3DFVF_XYZB2 && (fvf & WINED3DFVF_LASTBETA_D3DCOLOR)))
372 append_decl_element(&state, WINED3DFMT_R8G8B8A8_UINT, WINED3DDECLUSAGE_BLENDINDICES, 0);
373 else if (fvf & WINED3DFVF_LASTBETA_D3DCOLOR)
374 append_decl_element(&state, WINED3DFMT_B8G8R8A8_UNORM, WINED3DDECLUSAGE_BLENDINDICES, 0);
376 append_decl_element(&state, WINED3DFMT_R32_FLOAT, WINED3DDECLUSAGE_BLENDINDICES, 0);
379 if (has_normal) append_decl_element(&state, WINED3DFMT_R32G32B32_FLOAT, WINED3DDECLUSAGE_NORMAL, 0);
380 if (has_psize) append_decl_element(&state, WINED3DFMT_R32_FLOAT, WINED3DDECLUSAGE_PSIZE, 0);
381 if (has_diffuse) append_decl_element(&state, WINED3DFMT_B8G8R8A8_UNORM, WINED3DDECLUSAGE_COLOR, 0);
382 if (has_specular) append_decl_element(&state, WINED3DFMT_B8G8R8A8_UNORM, WINED3DDECLUSAGE_COLOR, 1);
384 for (idx = 0; idx < num_textures; ++idx)
386 switch ((texcoords >> (idx * 2)) & 0x03)
388 case WINED3DFVF_TEXTUREFORMAT1:
389 append_decl_element(&state, WINED3DFMT_R32_FLOAT, WINED3DDECLUSAGE_TEXCOORD, idx);
391 case WINED3DFVF_TEXTUREFORMAT2:
392 append_decl_element(&state, WINED3DFMT_R32G32_FLOAT, WINED3DDECLUSAGE_TEXCOORD, idx);
394 case WINED3DFVF_TEXTUREFORMAT3:
395 append_decl_element(&state, WINED3DFMT_R32G32B32_FLOAT, WINED3DDECLUSAGE_TEXCOORD, idx);
397 case WINED3DFVF_TEXTUREFORMAT4:
398 append_decl_element(&state, WINED3DFMT_R32G32B32A32_FLOAT, WINED3DDECLUSAGE_TEXCOORD, idx);
403 *elements = state.elements;
407 HRESULT CDECL wined3d_vertex_declaration_create_from_fvf(struct wined3d_device *device,
408 DWORD fvf, void *parent, const struct wined3d_parent_ops *parent_ops,
409 struct wined3d_vertex_declaration **declaration)
411 WINED3DVERTEXELEMENT *elements;
415 TRACE("device %p, fvf %#x, parent %p, parent_ops %p, declaration %p.\n",
416 device, fvf, parent, parent_ops, declaration);
418 size = convert_fvf_to_declaration(&device->adapter->gl_info, fvf, &elements);
419 if (size == ~0U) return E_OUTOFMEMORY;
421 hr = wined3d_vertex_declaration_create(device, elements, size, parent, parent_ops, declaration);
422 HeapFree(GetProcessHeap(), 0, elements);