gdi32: Change an ERR to a WARN for fonts with too-long names.
[wine/multimedia.git] / dlls / wined3d / vertexdeclaration.c
blobdf3d8ba8d9f51c602981ab0e5ae23b15d53fcba8
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(" method: %s (%#x)\n", debug_d3ddeclmethod(element->method), element->method);
38 TRACE(" usage: %s (%#x)\n", debug_d3ddeclusage(element->usage), element->usage);
39 TRACE(" usage_idx: %u\n", element->usage_idx);
42 ULONG CDECL wined3d_vertex_declaration_incref(struct wined3d_vertex_declaration *declaration)
44 ULONG refcount = InterlockedIncrement(&declaration->ref);
46 TRACE("%p increasing refcount to %u.\n", declaration, refcount);
48 return refcount;
51 ULONG CDECL wined3d_vertex_declaration_decref(struct wined3d_vertex_declaration *declaration)
53 ULONG refcount = InterlockedDecrement(&declaration->ref);
55 TRACE("%p decreasing refcount to %u.\n", declaration, refcount);
57 if (!refcount)
59 HeapFree(GetProcessHeap(), 0, declaration->elements);
60 declaration->parent_ops->wined3d_object_destroyed(declaration->parent);
61 HeapFree(GetProcessHeap(), 0, declaration);
64 return refcount;
67 void * CDECL wined3d_vertex_declaration_get_parent(const struct wined3d_vertex_declaration *declaration)
69 TRACE("declaration %p.\n", declaration);
71 return declaration->parent;
74 static BOOL declaration_element_valid_ffp(const struct wined3d_vertex_element *element)
76 switch(element->usage)
78 case WINED3D_DECL_USAGE_POSITION:
79 case WINED3D_DECL_USAGE_POSITIONT:
80 switch(element->format)
82 case WINED3DFMT_R32G32_FLOAT:
83 case WINED3DFMT_R32G32B32_FLOAT:
84 case WINED3DFMT_R32G32B32A32_FLOAT:
85 case WINED3DFMT_R16G16_SINT:
86 case WINED3DFMT_R16G16B16A16_SINT:
87 case WINED3DFMT_R16G16_FLOAT:
88 case WINED3DFMT_R16G16B16A16_FLOAT:
89 return TRUE;
90 default:
91 return FALSE;
94 case WINED3D_DECL_USAGE_BLEND_WEIGHT:
95 switch(element->format)
97 case WINED3DFMT_R32_FLOAT:
98 case WINED3DFMT_R32G32_FLOAT:
99 case WINED3DFMT_R32G32B32_FLOAT:
100 case WINED3DFMT_R32G32B32A32_FLOAT:
101 case WINED3DFMT_B8G8R8A8_UNORM:
102 case WINED3DFMT_R8G8B8A8_UINT:
103 case WINED3DFMT_R16G16_SINT:
104 case WINED3DFMT_R16G16B16A16_SINT:
105 case WINED3DFMT_R16G16_FLOAT:
106 case WINED3DFMT_R16G16B16A16_FLOAT:
107 return TRUE;
108 default:
109 return FALSE;
112 case WINED3D_DECL_USAGE_NORMAL:
113 switch(element->format)
115 case WINED3DFMT_R32G32B32_FLOAT:
116 case WINED3DFMT_R32G32B32A32_FLOAT:
117 case WINED3DFMT_R16G16B16A16_SINT:
118 case WINED3DFMT_R16G16B16A16_FLOAT:
119 return TRUE;
120 default:
121 return FALSE;
124 case WINED3D_DECL_USAGE_TEXCOORD:
125 switch(element->format)
127 case WINED3DFMT_R32_FLOAT:
128 case WINED3DFMT_R32G32_FLOAT:
129 case WINED3DFMT_R32G32B32_FLOAT:
130 case WINED3DFMT_R32G32B32A32_FLOAT:
131 case WINED3DFMT_R16G16_SINT:
132 case WINED3DFMT_R16G16B16A16_SINT:
133 case WINED3DFMT_R16G16_FLOAT:
134 case WINED3DFMT_R16G16B16A16_FLOAT:
135 return TRUE;
136 default:
137 return FALSE;
140 case WINED3D_DECL_USAGE_COLOR:
141 switch(element->format)
143 case WINED3DFMT_R32G32B32_FLOAT:
144 case WINED3DFMT_R32G32B32A32_FLOAT:
145 case WINED3DFMT_B8G8R8A8_UNORM:
146 case WINED3DFMT_R8G8B8A8_UINT:
147 case WINED3DFMT_R16G16B16A16_SINT:
148 case WINED3DFMT_R8G8B8A8_UNORM:
149 case WINED3DFMT_R16G16B16A16_SNORM:
150 case WINED3DFMT_R16G16B16A16_UNORM:
151 case WINED3DFMT_R16G16B16A16_FLOAT:
152 return TRUE;
153 default:
154 return FALSE;
157 default:
158 return FALSE;
162 static HRESULT vertexdeclaration_init(struct wined3d_vertex_declaration *declaration,
163 struct wined3d_device *device, const struct wined3d_vertex_element *elements, UINT element_count,
164 void *parent, const struct wined3d_parent_ops *parent_ops)
166 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
167 unsigned int i;
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 == WINED3D_DECL_USAGE_POSITIONT)
205 declaration->position_transformed = TRUE;
207 /* Find the streams used in the declaration. The vertex buffers have
208 * to be loaded when drawing, but filter tesselation pseudo streams. */
209 if (e->input_slot >= MAX_STREAMS) continue;
211 if (!e->format->gl_vtx_format)
213 FIXME("The application tries to use an unsupported format (%s), returning E_FAIL.\n",
214 debug_d3dformat(elements[i].format));
215 HeapFree(GetProcessHeap(), 0, declaration->elements);
216 return E_FAIL;
219 if (e->offset == WINED3D_APPEND_ALIGNED_ELEMENT)
221 if (!i)
223 e->offset = 0;
225 else
227 struct wined3d_vertex_declaration_element *prev = &declaration->elements[i - 1];
228 e->offset = (prev->offset + prev->format->byte_count + 3) & ~3;
232 if (e->offset & 0x3)
234 WARN("Declaration element %u is not 4 byte aligned(%u), returning E_FAIL.\n", i, e->offset);
235 HeapFree(GetProcessHeap(), 0, declaration->elements);
236 return E_FAIL;
239 if (elements[i].format == WINED3DFMT_R16G16_FLOAT || elements[i].format == WINED3DFMT_R16G16B16A16_FLOAT)
241 if (!gl_info->supported[ARB_HALF_FLOAT_VERTEX]) declaration->half_float_conv_needed = TRUE;
245 return WINED3D_OK;
248 HRESULT CDECL wined3d_vertex_declaration_create(struct wined3d_device *device,
249 const struct wined3d_vertex_element *elements, UINT element_count, void *parent,
250 const struct wined3d_parent_ops *parent_ops, struct wined3d_vertex_declaration **declaration)
252 struct wined3d_vertex_declaration *object;
253 HRESULT hr;
255 TRACE("device %p, elements %p, element_count %u, parent %p, parent_ops %p, declaration %p.\n",
256 device, elements, element_count, parent, parent_ops, declaration);
258 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
259 if(!object)
260 return E_OUTOFMEMORY;
262 hr = vertexdeclaration_init(object, device, elements, element_count, parent, parent_ops);
263 if (FAILED(hr))
265 WARN("Failed to initialize vertex declaration, hr %#x.\n", hr);
266 HeapFree(GetProcessHeap(), 0, object);
267 return hr;
270 TRACE("Created vertex declaration %p.\n", object);
271 *declaration = object;
273 return WINED3D_OK;
276 struct wined3d_fvf_convert_state
278 const struct wined3d_gl_info *gl_info;
279 struct wined3d_vertex_element *elements;
280 UINT offset;
281 UINT idx;
284 static void append_decl_element(struct wined3d_fvf_convert_state *state,
285 enum wined3d_format_id format_id, enum wined3d_decl_usage usage, UINT usage_idx)
287 struct wined3d_vertex_element *elements = state->elements;
288 const struct wined3d_format *format;
289 UINT offset = state->offset;
290 UINT idx = state->idx;
292 elements[idx].format = format_id;
293 elements[idx].input_slot = 0;
294 elements[idx].offset = offset;
295 elements[idx].output_slot = 0;
296 elements[idx].method = WINED3D_DECL_METHOD_DEFAULT;
297 elements[idx].usage = usage;
298 elements[idx].usage_idx = usage_idx;
300 format = wined3d_get_format(state->gl_info, format_id);
301 state->offset += format->component_count * format->component_size;
302 ++state->idx;
305 static unsigned int convert_fvf_to_declaration(const struct wined3d_gl_info *gl_info,
306 DWORD fvf, struct wined3d_vertex_element **elements)
308 BOOL has_pos = !!(fvf & WINED3DFVF_POSITION_MASK);
309 BOOL has_blend = (fvf & WINED3DFVF_XYZB5) > WINED3DFVF_XYZRHW;
310 BOOL has_blend_idx = has_blend &&
311 (((fvf & WINED3DFVF_XYZB5) == WINED3DFVF_XYZB5) ||
312 (fvf & WINED3DFVF_LASTBETA_D3DCOLOR) ||
313 (fvf & WINED3DFVF_LASTBETA_UBYTE4));
314 BOOL has_normal = !!(fvf & WINED3DFVF_NORMAL);
315 BOOL has_psize = !!(fvf & WINED3DFVF_PSIZE);
316 BOOL has_diffuse = !!(fvf & WINED3DFVF_DIFFUSE);
317 BOOL has_specular = !!(fvf & WINED3DFVF_SPECULAR);
319 DWORD num_textures = (fvf & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
320 DWORD texcoords = (fvf & 0xffff0000) >> 16;
321 struct wined3d_fvf_convert_state state;
322 unsigned int size;
323 unsigned int idx;
324 DWORD num_blends = 1 + (((fvf & WINED3DFVF_XYZB5) - WINED3DFVF_XYZB1) >> 1);
325 if (has_blend_idx) num_blends--;
327 /* Compute declaration size */
328 size = has_pos + (has_blend && num_blends > 0) + has_blend_idx + has_normal +
329 has_psize + has_diffuse + has_specular + num_textures;
331 state.gl_info = gl_info;
332 state.elements = HeapAlloc(GetProcessHeap(), 0, size * sizeof(*state.elements));
333 if (!state.elements) return ~0U;
334 state.offset = 0;
335 state.idx = 0;
337 if (has_pos)
339 if (!has_blend && (fvf & WINED3DFVF_XYZRHW))
340 append_decl_element(&state, WINED3DFMT_R32G32B32A32_FLOAT, WINED3D_DECL_USAGE_POSITIONT, 0);
341 else if ((fvf & WINED3DFVF_XYZW) == WINED3DFVF_XYZW)
342 append_decl_element(&state, WINED3DFMT_R32G32B32A32_FLOAT, WINED3D_DECL_USAGE_POSITION, 0);
343 else
344 append_decl_element(&state, WINED3DFMT_R32G32B32_FLOAT, WINED3D_DECL_USAGE_POSITION, 0);
347 if (has_blend && (num_blends > 0))
349 if ((fvf & WINED3DFVF_XYZB5) == WINED3DFVF_XYZB2 && (fvf & WINED3DFVF_LASTBETA_D3DCOLOR))
350 append_decl_element(&state, WINED3DFMT_B8G8R8A8_UNORM, WINED3D_DECL_USAGE_BLEND_WEIGHT, 0);
351 else
353 switch (num_blends)
355 case 1:
356 append_decl_element(&state, WINED3DFMT_R32_FLOAT, WINED3D_DECL_USAGE_BLEND_WEIGHT, 0);
357 break;
358 case 2:
359 append_decl_element(&state, WINED3DFMT_R32G32_FLOAT, WINED3D_DECL_USAGE_BLEND_WEIGHT, 0);
360 break;
361 case 3:
362 append_decl_element(&state, WINED3DFMT_R32G32B32_FLOAT, WINED3D_DECL_USAGE_BLEND_WEIGHT, 0);
363 break;
364 case 4:
365 append_decl_element(&state, WINED3DFMT_R32G32B32A32_FLOAT, WINED3D_DECL_USAGE_BLEND_WEIGHT, 0);
366 break;
367 default:
368 ERR("Unexpected amount of blend values: %u\n", num_blends);
373 if (has_blend_idx)
375 if ((fvf & WINED3DFVF_LASTBETA_UBYTE4)
376 || ((fvf & WINED3DFVF_XYZB5) == WINED3DFVF_XYZB2 && (fvf & WINED3DFVF_LASTBETA_D3DCOLOR)))
377 append_decl_element(&state, WINED3DFMT_R8G8B8A8_UINT, WINED3D_DECL_USAGE_BLEND_INDICES, 0);
378 else if (fvf & WINED3DFVF_LASTBETA_D3DCOLOR)
379 append_decl_element(&state, WINED3DFMT_B8G8R8A8_UNORM, WINED3D_DECL_USAGE_BLEND_INDICES, 0);
380 else
381 append_decl_element(&state, WINED3DFMT_R32_FLOAT, WINED3D_DECL_USAGE_BLEND_INDICES, 0);
384 if (has_normal)
385 append_decl_element(&state, WINED3DFMT_R32G32B32_FLOAT, WINED3D_DECL_USAGE_NORMAL, 0);
386 if (has_psize)
387 append_decl_element(&state, WINED3DFMT_R32_FLOAT, WINED3D_DECL_USAGE_PSIZE, 0);
388 if (has_diffuse)
389 append_decl_element(&state, WINED3DFMT_B8G8R8A8_UNORM, WINED3D_DECL_USAGE_COLOR, 0);
390 if (has_specular)
391 append_decl_element(&state, WINED3DFMT_B8G8R8A8_UNORM, WINED3D_DECL_USAGE_COLOR, 1);
393 for (idx = 0; idx < num_textures; ++idx)
395 switch ((texcoords >> (idx * 2)) & 0x03)
397 case WINED3DFVF_TEXTUREFORMAT1:
398 append_decl_element(&state, WINED3DFMT_R32_FLOAT, WINED3D_DECL_USAGE_TEXCOORD, idx);
399 break;
400 case WINED3DFVF_TEXTUREFORMAT2:
401 append_decl_element(&state, WINED3DFMT_R32G32_FLOAT, WINED3D_DECL_USAGE_TEXCOORD, idx);
402 break;
403 case WINED3DFVF_TEXTUREFORMAT3:
404 append_decl_element(&state, WINED3DFMT_R32G32B32_FLOAT, WINED3D_DECL_USAGE_TEXCOORD, idx);
405 break;
406 case WINED3DFVF_TEXTUREFORMAT4:
407 append_decl_element(&state, WINED3DFMT_R32G32B32A32_FLOAT, WINED3D_DECL_USAGE_TEXCOORD, idx);
408 break;
412 *elements = state.elements;
413 return size;
416 HRESULT CDECL wined3d_vertex_declaration_create_from_fvf(struct wined3d_device *device,
417 DWORD fvf, void *parent, const struct wined3d_parent_ops *parent_ops,
418 struct wined3d_vertex_declaration **declaration)
420 struct wined3d_vertex_element *elements;
421 unsigned int size;
422 DWORD hr;
424 TRACE("device %p, fvf %#x, parent %p, parent_ops %p, declaration %p.\n",
425 device, fvf, parent, parent_ops, declaration);
427 size = convert_fvf_to_declaration(&device->adapter->gl_info, fvf, &elements);
428 if (size == ~0U) return E_OUTOFMEMORY;
430 hr = wined3d_vertex_declaration_create(device, elements, size, parent, parent_ops, declaration);
431 HeapFree(GetProcessHeap(), 0, elements);
432 return hr;