Release 1.5.29.
[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 "wine/port.h"
27 #include "wined3d_private.h"
28
29 WINE_DEFAULT_DEBUG_CHANNEL(d3d_decl);
30
31 static void dump_wined3d_vertex_element(const struct wined3d_vertex_element *element)
32 {
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);
40 }
41
42 ULONG CDECL wined3d_vertex_declaration_incref(struct wined3d_vertex_declaration *declaration)
43 {
44     ULONG refcount = InterlockedIncrement(&declaration->ref);
45
46     TRACE("%p increasing refcount to %u.\n", declaration, refcount);
47
48     return refcount;
49 }
50
51 ULONG CDECL wined3d_vertex_declaration_decref(struct wined3d_vertex_declaration *declaration)
52 {
53     ULONG refcount = InterlockedDecrement(&declaration->ref);
54
55     TRACE("%p decreasing refcount to %u.\n", declaration, refcount);
56
57     if (!refcount)
58     {
59         HeapFree(GetProcessHeap(), 0, declaration->elements);
60         declaration->parent_ops->wined3d_object_destroyed(declaration->parent);
61         HeapFree(GetProcessHeap(), 0, declaration);
62     }
63
64     return refcount;
65 }
66
67 void * CDECL wined3d_vertex_declaration_get_parent(const struct wined3d_vertex_declaration *declaration)
68 {
69     TRACE("declaration %p.\n", declaration);
70
71     return declaration->parent;
72 }
73
74 static BOOL declaration_element_valid_ffp(const struct wined3d_vertex_element *element)
75 {
76     switch(element->usage)
77     {
78         case WINED3D_DECL_USAGE_POSITION:
79         case WINED3D_DECL_USAGE_POSITIONT:
80             switch(element->format)
81             {
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;
92             }
93
94         case WINED3D_DECL_USAGE_BLEND_WEIGHT:
95             switch(element->format)
96             {
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;
110             }
111
112         case WINED3D_DECL_USAGE_NORMAL:
113             switch(element->format)
114             {
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;
122             }
123
124         case WINED3D_DECL_USAGE_TEXCOORD:
125             switch(element->format)
126             {
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;
138             }
139
140         case WINED3D_DECL_USAGE_COLOR:
141             switch(element->format)
142             {
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;
155             }
156
157         default:
158             return FALSE;
159     }
160 }
161
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)
165 {
166     const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
167     WORD preloaded = 0; /* MAX_STREAMS, 16 */
168     unsigned int i;
169
170     if (TRACE_ON(d3d_decl))
171     {
172         for (i = 0; i < element_count; ++i)
173         {
174             dump_wined3d_vertex_element(elements + i);
175         }
176     }
177
178     declaration->ref = 1;
179     declaration->parent = parent;
180     declaration->parent_ops = parent_ops;
181     declaration->device = device;
182     declaration->elements = HeapAlloc(GetProcessHeap(), 0, sizeof(*declaration->elements) * element_count);
183     if (!declaration->elements)
184     {
185         ERR("Failed to allocate elements memory.\n");
186         return E_OUTOFMEMORY;
187     }
188     declaration->element_count = element_count;
189
190     /* Do some static analysis on the elements to make reading the
191      * declaration more comfortable for the drawing code. */
192     for (i = 0; i < element_count; ++i)
193     {
194         struct wined3d_vertex_declaration_element *e = &declaration->elements[i];
195
196         e->format = wined3d_get_format(gl_info, elements[i].format);
197         e->ffp_valid = declaration_element_valid_ffp(&elements[i]);
198         e->input_slot = elements[i].input_slot;
199         e->offset = elements[i].offset;
200         e->output_slot = elements[i].output_slot;
201         e->method = elements[i].method;
202         e->usage = elements[i].usage;
203         e->usage_idx = elements[i].usage_idx;
204
205         if (e->usage == WINED3D_DECL_USAGE_POSITIONT)
206             declaration->position_transformed = TRUE;
207
208         /* Find the streams used in the declaration. The vertex buffers have
209          * to be loaded when drawing, but filter tesselation pseudo streams. */
210         if (e->input_slot >= MAX_STREAMS) continue;
211
212         if (!e->format->gl_vtx_format)
213         {
214             FIXME("The application tries to use an unsupported format (%s), returning E_FAIL.\n",
215                     debug_d3dformat(elements[i].format));
216             HeapFree(GetProcessHeap(), 0, declaration->elements);
217             return E_FAIL;
218         }
219
220         if (e->offset & 0x3)
221         {
222             WARN("Declaration element %u is not 4 byte aligned(%u), returning E_FAIL.\n", i, e->offset);
223             HeapFree(GetProcessHeap(), 0, declaration->elements);
224             return E_FAIL;
225         }
226
227         if (!(preloaded & (1 << e->input_slot)))
228         {
229             declaration->streams[declaration->num_streams] = e->input_slot;
230             ++declaration->num_streams;
231             preloaded |= 1 << e->input_slot;
232         }
233
234         if (elements[i].format == WINED3DFMT_R16G16_FLOAT || elements[i].format == WINED3DFMT_R16G16B16A16_FLOAT)
235         {
236             if (!gl_info->supported[ARB_HALF_FLOAT_VERTEX]) declaration->half_float_conv_needed = TRUE;
237         }
238     }
239
240     return WINED3D_OK;
241 }
242
243 HRESULT CDECL wined3d_vertex_declaration_create(struct wined3d_device *device,
244         const struct wined3d_vertex_element *elements, UINT element_count, void *parent,
245         const struct wined3d_parent_ops *parent_ops, struct wined3d_vertex_declaration **declaration)
246 {
247     struct wined3d_vertex_declaration *object;
248     HRESULT hr;
249
250     TRACE("device %p, elements %p, element_count %u, parent %p, parent_ops %p, declaration %p.\n",
251             device, elements, element_count, parent, parent_ops, declaration);
252
253     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
254     if(!object)
255         return E_OUTOFMEMORY;
256
257     hr = vertexdeclaration_init(object, device, elements, element_count, parent, parent_ops);
258     if (FAILED(hr))
259     {
260         WARN("Failed to initialize vertex declaration, hr %#x.\n", hr);
261         HeapFree(GetProcessHeap(), 0, object);
262         return hr;
263     }
264
265     TRACE("Created vertex declaration %p.\n", object);
266     *declaration = object;
267
268     return WINED3D_OK;
269 }
270
271 struct wined3d_fvf_convert_state
272 {
273     const struct wined3d_gl_info *gl_info;
274     struct wined3d_vertex_element *elements;
275     UINT offset;
276     UINT idx;
277 };
278
279 static void append_decl_element(struct wined3d_fvf_convert_state *state,
280         enum wined3d_format_id format_id, enum wined3d_decl_usage usage, UINT usage_idx)
281 {
282     struct wined3d_vertex_element *elements = state->elements;
283     const struct wined3d_format *format;
284     UINT offset = state->offset;
285     UINT idx = state->idx;
286
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 = WINED3D_DECL_METHOD_DEFAULT;
292     elements[idx].usage = usage;
293     elements[idx].usage_idx = usage_idx;
294
295     format = wined3d_get_format(state->gl_info, format_id);
296     state->offset += format->component_count * format->component_size;
297     ++state->idx;
298 }
299
300 static unsigned int convert_fvf_to_declaration(const struct wined3d_gl_info *gl_info,
301         DWORD fvf, struct wined3d_vertex_element **elements)
302 {
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);
313
314     DWORD num_textures = (fvf & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
315     DWORD texcoords = (fvf & 0xffff0000) >> 16;
316     struct wined3d_fvf_convert_state state;
317     unsigned int size;
318     unsigned int idx;
319     DWORD num_blends = 1 + (((fvf & WINED3DFVF_XYZB5) - WINED3DFVF_XYZB1) >> 1);
320     if (has_blend_idx) num_blends--;
321
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;
325
326     state.gl_info = gl_info;
327     state.elements = HeapAlloc(GetProcessHeap(), 0, size * sizeof(*state.elements));
328     if (!state.elements) return ~0U;
329     state.offset = 0;
330     state.idx = 0;
331
332     if (has_pos)
333     {
334         if (!has_blend && (fvf & WINED3DFVF_XYZRHW))
335             append_decl_element(&state, WINED3DFMT_R32G32B32A32_FLOAT, WINED3D_DECL_USAGE_POSITIONT, 0);
336         else if ((fvf & WINED3DFVF_XYZW) == WINED3DFVF_XYZW)
337             append_decl_element(&state, WINED3DFMT_R32G32B32A32_FLOAT, WINED3D_DECL_USAGE_POSITION, 0);
338         else
339             append_decl_element(&state, WINED3DFMT_R32G32B32_FLOAT, WINED3D_DECL_USAGE_POSITION, 0);
340     }
341
342     if (has_blend && (num_blends > 0))
343     {
344         if ((fvf & WINED3DFVF_XYZB5) == WINED3DFVF_XYZB2 && (fvf & WINED3DFVF_LASTBETA_D3DCOLOR))
345             append_decl_element(&state, WINED3DFMT_B8G8R8A8_UNORM, WINED3D_DECL_USAGE_BLEND_WEIGHT, 0);
346         else
347         {
348             switch (num_blends)
349             {
350                 case 1:
351                     append_decl_element(&state, WINED3DFMT_R32_FLOAT, WINED3D_DECL_USAGE_BLEND_WEIGHT, 0);
352                     break;
353                 case 2:
354                     append_decl_element(&state, WINED3DFMT_R32G32_FLOAT, WINED3D_DECL_USAGE_BLEND_WEIGHT, 0);
355                     break;
356                 case 3:
357                     append_decl_element(&state, WINED3DFMT_R32G32B32_FLOAT, WINED3D_DECL_USAGE_BLEND_WEIGHT, 0);
358                     break;
359                 case 4:
360                     append_decl_element(&state, WINED3DFMT_R32G32B32A32_FLOAT, WINED3D_DECL_USAGE_BLEND_WEIGHT, 0);
361                     break;
362                 default:
363                     ERR("Unexpected amount of blend values: %u\n", num_blends);
364             }
365         }
366     }
367
368     if (has_blend_idx)
369     {
370         if ((fvf & WINED3DFVF_LASTBETA_UBYTE4)
371                 || ((fvf & WINED3DFVF_XYZB5) == WINED3DFVF_XYZB2 && (fvf & WINED3DFVF_LASTBETA_D3DCOLOR)))
372             append_decl_element(&state, WINED3DFMT_R8G8B8A8_UINT, WINED3D_DECL_USAGE_BLEND_INDICES, 0);
373         else if (fvf & WINED3DFVF_LASTBETA_D3DCOLOR)
374             append_decl_element(&state, WINED3DFMT_B8G8R8A8_UNORM, WINED3D_DECL_USAGE_BLEND_INDICES, 0);
375         else
376             append_decl_element(&state, WINED3DFMT_R32_FLOAT, WINED3D_DECL_USAGE_BLEND_INDICES, 0);
377     }
378
379     if (has_normal)
380         append_decl_element(&state, WINED3DFMT_R32G32B32_FLOAT, WINED3D_DECL_USAGE_NORMAL, 0);
381     if (has_psize)
382         append_decl_element(&state, WINED3DFMT_R32_FLOAT, WINED3D_DECL_USAGE_PSIZE, 0);
383     if (has_diffuse)
384         append_decl_element(&state, WINED3DFMT_B8G8R8A8_UNORM, WINED3D_DECL_USAGE_COLOR, 0);
385     if (has_specular)
386         append_decl_element(&state, WINED3DFMT_B8G8R8A8_UNORM, WINED3D_DECL_USAGE_COLOR, 1);
387
388     for (idx = 0; idx < num_textures; ++idx)
389     {
390         switch ((texcoords >> (idx * 2)) & 0x03)
391         {
392             case WINED3DFVF_TEXTUREFORMAT1:
393                 append_decl_element(&state, WINED3DFMT_R32_FLOAT, WINED3D_DECL_USAGE_TEXCOORD, idx);
394                 break;
395             case WINED3DFVF_TEXTUREFORMAT2:
396                 append_decl_element(&state, WINED3DFMT_R32G32_FLOAT, WINED3D_DECL_USAGE_TEXCOORD, idx);
397                 break;
398             case WINED3DFVF_TEXTUREFORMAT3:
399                 append_decl_element(&state, WINED3DFMT_R32G32B32_FLOAT, WINED3D_DECL_USAGE_TEXCOORD, idx);
400                 break;
401             case WINED3DFVF_TEXTUREFORMAT4:
402                 append_decl_element(&state, WINED3DFMT_R32G32B32A32_FLOAT, WINED3D_DECL_USAGE_TEXCOORD, idx);
403                 break;
404         }
405     }
406
407     *elements = state.elements;
408     return size;
409 }
410
411 HRESULT CDECL wined3d_vertex_declaration_create_from_fvf(struct wined3d_device *device,
412         DWORD fvf, void *parent, const struct wined3d_parent_ops *parent_ops,
413         struct wined3d_vertex_declaration **declaration)
414 {
415     struct wined3d_vertex_element *elements;
416     unsigned int size;
417     DWORD hr;
418
419     TRACE("device %p, fvf %#x, parent %p, parent_ops %p, declaration %p.\n",
420             device, fvf, parent, parent_ops, declaration);
421
422     size = convert_fvf_to_declaration(&device->adapter->gl_info, fvf, &elements);
423     if (size == ~0U) return E_OUTOFMEMORY;
424
425     hr = wined3d_vertex_declaration_create(device, elements, size, parent, parent_ops, declaration);
426     HeapFree(GetProcessHeap(), 0, elements);
427     return hr;
428 }