msxml3: Add stub support of ISAXDeclHandler for MXWriter.
[wine] / dlls / wined3d / vertexdeclaration.c
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 #include "config.h"
26 #include "wined3d_private.h"
27
28 WINE_DEFAULT_DEBUG_CHANNEL(d3d_decl);
29
30 static void dump_wined3d_vertex_element(const struct wined3d_vertex_element *element)
31 {
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);
39 }
40
41 ULONG CDECL wined3d_vertex_declaration_incref(struct wined3d_vertex_declaration *declaration)
42 {
43     ULONG refcount = InterlockedIncrement(&declaration->ref);
44
45     TRACE("%p increasing refcount to %u.\n", declaration, refcount);
46
47     return refcount;
48 }
49
50 ULONG CDECL wined3d_vertex_declaration_decref(struct wined3d_vertex_declaration *declaration)
51 {
52     ULONG refcount = InterlockedDecrement(&declaration->ref);
53
54     TRACE("%p decreasing refcount to %u.\n", declaration, refcount);
55
56     if (!refcount)
57     {
58         HeapFree(GetProcessHeap(), 0, declaration->elements);
59         declaration->parent_ops->wined3d_object_destroyed(declaration->parent);
60         HeapFree(GetProcessHeap(), 0, declaration);
61     }
62
63     return refcount;
64 }
65
66 void * CDECL wined3d_vertex_declaration_get_parent(const struct wined3d_vertex_declaration *declaration)
67 {
68     TRACE("declaration %p.\n", declaration);
69
70     return declaration->parent;
71 }
72
73 static BOOL declaration_element_valid_ffp(const struct wined3d_vertex_element *element)
74 {
75     switch(element->usage)
76     {
77         case WINED3D_DECL_USAGE_POSITION:
78         case WINED3D_DECL_USAGE_POSITIONT:
79             switch(element->format)
80             {
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:
88                     return TRUE;
89                 default:
90                     return FALSE;
91             }
92
93         case WINED3D_DECL_USAGE_BLEND_WEIGHT:
94             switch(element->format)
95             {
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:
106                     return TRUE;
107                 default:
108                     return FALSE;
109             }
110
111         case WINED3D_DECL_USAGE_NORMAL:
112             switch(element->format)
113             {
114                 case WINED3DFMT_R32G32B32_FLOAT:
115                 case WINED3DFMT_R32G32B32A32_FLOAT:
116                 case WINED3DFMT_R16G16B16A16_SINT:
117                 case WINED3DFMT_R16G16B16A16_FLOAT:
118                     return TRUE;
119                 default:
120                     return FALSE;
121             }
122
123         case WINED3D_DECL_USAGE_TEXCOORD:
124             switch(element->format)
125             {
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:
134                     return TRUE;
135                 default:
136                     return FALSE;
137             }
138
139         case WINED3D_DECL_USAGE_COLOR:
140             switch(element->format)
141             {
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:
151                     return TRUE;
152                 default:
153                     return FALSE;
154             }
155
156         default:
157             return FALSE;
158     }
159 }
160
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)
164 {
165     const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
166     WORD preloaded = 0; /* MAX_STREAMS, 16 */
167     unsigned int i;
168
169     if (TRACE_ON(d3d_decl))
170     {
171         for (i = 0; i < element_count; ++i)
172         {
173             dump_wined3d_vertex_element(elements + i);
174         }
175     }
176
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)
183     {
184         ERR("Failed to allocate elements memory.\n");
185         return E_OUTOFMEMORY;
186     }
187     declaration->element_count = element_count;
188
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)
192     {
193         struct wined3d_vertex_declaration_element *e = &declaration->elements[i];
194
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;
203
204         if (e->usage == WINED3D_DECL_USAGE_POSITIONT)
205             declaration->position_transformed = TRUE;
206
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;
210
211         if (!e->format->gl_vtx_format)
212         {
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;
217         }
218
219         if (e->offset & 0x3)
220         {
221             WARN("Declaration element %u is not 4 byte aligned(%u), returning E_FAIL.\n", i, e->offset);
222             HeapFree(GetProcessHeap(), 0, declaration->elements);
223             return E_FAIL;
224         }
225
226         if (!(preloaded & (1 << e->input_slot)))
227         {
228             declaration->streams[declaration->num_streams] = e->input_slot;
229             ++declaration->num_streams;
230             preloaded |= 1 << e->input_slot;
231         }
232
233         if (elements[i].format == WINED3DFMT_R16G16_FLOAT || elements[i].format == WINED3DFMT_R16G16B16A16_FLOAT)
234         {
235             if (!gl_info->supported[ARB_HALF_FLOAT_VERTEX]) declaration->half_float_conv_needed = TRUE;
236         }
237     }
238
239     return WINED3D_OK;
240 }
241
242 HRESULT CDECL wined3d_vertex_declaration_create(struct wined3d_device *device,
243         const struct wined3d_vertex_element *elements, UINT element_count, void *parent,
244         const struct wined3d_parent_ops *parent_ops, struct wined3d_vertex_declaration **declaration)
245 {
246     struct wined3d_vertex_declaration *object;
247     HRESULT hr;
248
249     TRACE("device %p, elements %p, element_count %u, parent %p, parent_ops %p, declaration %p.\n",
250             device, elements, element_count, parent, parent_ops, declaration);
251
252     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
253     if(!object)
254     {
255         ERR("Failed to allocate vertex declaration memory.\n");
256         return E_OUTOFMEMORY;
257     }
258
259     hr = vertexdeclaration_init(object, device, elements, element_count, parent, parent_ops);
260     if (FAILED(hr))
261     {
262         WARN("Failed to initialize vertex declaration, hr %#x.\n", hr);
263         HeapFree(GetProcessHeap(), 0, object);
264         return hr;
265     }
266
267     TRACE("Created vertex declaration %p.\n", object);
268     *declaration = object;
269
270     return WINED3D_OK;
271 }
272
273 struct wined3d_fvf_convert_state
274 {
275     const struct wined3d_gl_info *gl_info;
276     struct wined3d_vertex_element *elements;
277     UINT offset;
278     UINT idx;
279 };
280
281 static void append_decl_element(struct wined3d_fvf_convert_state *state,
282         enum wined3d_format_id format_id, enum wined3d_decl_usage usage, UINT usage_idx)
283 {
284     struct wined3d_vertex_element *elements = state->elements;
285     const struct wined3d_format *format;
286     UINT offset = state->offset;
287     UINT idx = state->idx;
288
289     elements[idx].format = format_id;
290     elements[idx].input_slot = 0;
291     elements[idx].offset = offset;
292     elements[idx].output_slot = 0;
293     elements[idx].method = WINED3D_DECL_METHOD_DEFAULT;
294     elements[idx].usage = usage;
295     elements[idx].usage_idx = usage_idx;
296
297     format = wined3d_get_format(state->gl_info, format_id);
298     state->offset += format->component_count * format->component_size;
299     ++state->idx;
300 }
301
302 static unsigned int convert_fvf_to_declaration(const struct wined3d_gl_info *gl_info,
303         DWORD fvf, struct wined3d_vertex_element **elements)
304 {
305     BOOL has_pos = !!(fvf & WINED3DFVF_POSITION_MASK);
306     BOOL has_blend = (fvf & WINED3DFVF_XYZB5) > WINED3DFVF_XYZRHW;
307     BOOL has_blend_idx = has_blend &&
308        (((fvf & WINED3DFVF_XYZB5) == WINED3DFVF_XYZB5) ||
309         (fvf & WINED3DFVF_LASTBETA_D3DCOLOR) ||
310         (fvf & WINED3DFVF_LASTBETA_UBYTE4));
311     BOOL has_normal = !!(fvf & WINED3DFVF_NORMAL);
312     BOOL has_psize = !!(fvf & WINED3DFVF_PSIZE);
313     BOOL has_diffuse = !!(fvf & WINED3DFVF_DIFFUSE);
314     BOOL has_specular = !!(fvf & WINED3DFVF_SPECULAR);
315
316     DWORD num_textures = (fvf & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
317     DWORD texcoords = (fvf & 0xFFFF0000) >> 16;
318     struct wined3d_fvf_convert_state state;
319     unsigned int size;
320     unsigned int idx;
321     DWORD num_blends = 1 + (((fvf & WINED3DFVF_XYZB5) - WINED3DFVF_XYZB1) >> 1);
322     if (has_blend_idx) num_blends--;
323
324     /* Compute declaration size */
325     size = has_pos + (has_blend && num_blends > 0) + has_blend_idx + has_normal +
326            has_psize + has_diffuse + has_specular + num_textures;
327
328     state.gl_info = gl_info;
329     state.elements = HeapAlloc(GetProcessHeap(), 0, size * sizeof(*state.elements));
330     if (!state.elements) return ~0U;
331     state.offset = 0;
332     state.idx = 0;
333
334     if (has_pos)
335     {
336         if (!has_blend && (fvf & WINED3DFVF_XYZRHW))
337             append_decl_element(&state, WINED3DFMT_R32G32B32A32_FLOAT, WINED3D_DECL_USAGE_POSITIONT, 0);
338         else if ((fvf & WINED3DFVF_XYZW) == WINED3DFVF_XYZW)
339             append_decl_element(&state, WINED3DFMT_R32G32B32A32_FLOAT, WINED3D_DECL_USAGE_POSITION, 0);
340         else
341             append_decl_element(&state, WINED3DFMT_R32G32B32_FLOAT, WINED3D_DECL_USAGE_POSITION, 0);
342     }
343
344     if (has_blend && (num_blends > 0))
345     {
346         if ((fvf & WINED3DFVF_XYZB5) == WINED3DFVF_XYZB2 && (fvf & WINED3DFVF_LASTBETA_D3DCOLOR))
347             append_decl_element(&state, WINED3DFMT_B8G8R8A8_UNORM, WINED3D_DECL_USAGE_BLEND_WEIGHT, 0);
348         else
349         {
350             switch (num_blends)
351             {
352                 case 1:
353                     append_decl_element(&state, WINED3DFMT_R32_FLOAT, WINED3D_DECL_USAGE_BLEND_WEIGHT, 0);
354                     break;
355                 case 2:
356                     append_decl_element(&state, WINED3DFMT_R32G32_FLOAT, WINED3D_DECL_USAGE_BLEND_WEIGHT, 0);
357                     break;
358                 case 3:
359                     append_decl_element(&state, WINED3DFMT_R32G32B32_FLOAT, WINED3D_DECL_USAGE_BLEND_WEIGHT, 0);
360                     break;
361                 case 4:
362                     append_decl_element(&state, WINED3DFMT_R32G32B32A32_FLOAT, WINED3D_DECL_USAGE_BLEND_WEIGHT, 0);
363                     break;
364                 default:
365                     ERR("Unexpected amount of blend values: %u\n", num_blends);
366             }
367         }
368     }
369
370     if (has_blend_idx)
371     {
372         if ((fvf & WINED3DFVF_LASTBETA_UBYTE4)
373                 || ((fvf & WINED3DFVF_XYZB5) == WINED3DFVF_XYZB2 && (fvf & WINED3DFVF_LASTBETA_D3DCOLOR)))
374             append_decl_element(&state, WINED3DFMT_R8G8B8A8_UINT, WINED3D_DECL_USAGE_BLEND_INDICES, 0);
375         else if (fvf & WINED3DFVF_LASTBETA_D3DCOLOR)
376             append_decl_element(&state, WINED3DFMT_B8G8R8A8_UNORM, WINED3D_DECL_USAGE_BLEND_INDICES, 0);
377         else
378             append_decl_element(&state, WINED3DFMT_R32_FLOAT, WINED3D_DECL_USAGE_BLEND_INDICES, 0);
379     }
380
381     if (has_normal)
382         append_decl_element(&state, WINED3DFMT_R32G32B32_FLOAT, WINED3D_DECL_USAGE_NORMAL, 0);
383     if (has_psize)
384         append_decl_element(&state, WINED3DFMT_R32_FLOAT, WINED3D_DECL_USAGE_PSIZE, 0);
385     if (has_diffuse)
386         append_decl_element(&state, WINED3DFMT_B8G8R8A8_UNORM, WINED3D_DECL_USAGE_COLOR, 0);
387     if (has_specular)
388         append_decl_element(&state, WINED3DFMT_B8G8R8A8_UNORM, WINED3D_DECL_USAGE_COLOR, 1);
389
390     for (idx = 0; idx < num_textures; ++idx)
391     {
392         switch ((texcoords >> (idx * 2)) & 0x03)
393         {
394             case WINED3DFVF_TEXTUREFORMAT1:
395                 append_decl_element(&state, WINED3DFMT_R32_FLOAT, WINED3D_DECL_USAGE_TEXCOORD, idx);
396                 break;
397             case WINED3DFVF_TEXTUREFORMAT2:
398                 append_decl_element(&state, WINED3DFMT_R32G32_FLOAT, WINED3D_DECL_USAGE_TEXCOORD, idx);
399                 break;
400             case WINED3DFVF_TEXTUREFORMAT3:
401                 append_decl_element(&state, WINED3DFMT_R32G32B32_FLOAT, WINED3D_DECL_USAGE_TEXCOORD, idx);
402                 break;
403             case WINED3DFVF_TEXTUREFORMAT4:
404                 append_decl_element(&state, WINED3DFMT_R32G32B32A32_FLOAT, WINED3D_DECL_USAGE_TEXCOORD, idx);
405                 break;
406         }
407     }
408
409     *elements = state.elements;
410     return size;
411 }
412
413 HRESULT CDECL wined3d_vertex_declaration_create_from_fvf(struct wined3d_device *device,
414         DWORD fvf, void *parent, const struct wined3d_parent_ops *parent_ops,
415         struct wined3d_vertex_declaration **declaration)
416 {
417     struct wined3d_vertex_element *elements;
418     unsigned int size;
419     DWORD hr;
420
421     TRACE("device %p, fvf %#x, parent %p, parent_ops %p, declaration %p.\n",
422             device, fvf, parent, parent_ops, declaration);
423
424     size = convert_fvf_to_declaration(&device->adapter->gl_info, fvf, &elements);
425     if (size == ~0U) return E_OUTOFMEMORY;
426
427     hr = wined3d_vertex_declaration_create(device, elements, size, parent, parent_ops, declaration);
428     HeapFree(GetProcessHeap(), 0, elements);
429     return hr;
430 }