ddraw: Move clipper handling to ddraw.
[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 WINED3DDECLUSAGE_POSITION:
78         case WINED3DDECLUSAGE_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 WINED3DDECLUSAGE_BLENDWEIGHT:
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 WINED3DDECLUSAGE_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 WINED3DDECLUSAGE_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 WINED3DDECLUSAGE_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 == WINED3DDECLUSAGE_POSITIONT) declaration->position_transformed = TRUE;
205
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;
209
210         if (!e->format->gl_vtx_format)
211         {
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);
215             return E_FAIL;
216         }
217
218         if (e->offset & 0x3)
219         {
220             WARN("Declaration element %u is not 4 byte aligned(%u), returning E_FAIL.\n", i, e->offset);
221             HeapFree(GetProcessHeap(), 0, declaration->elements);
222             return E_FAIL;
223         }
224
225         if (!(preloaded & (1 << e->input_slot)))
226         {
227             declaration->streams[declaration->num_streams] = e->input_slot;
228             ++declaration->num_streams;
229             preloaded |= 1 << e->input_slot;
230         }
231
232         if (elements[i].format == WINED3DFMT_R16G16_FLOAT || elements[i].format == WINED3DFMT_R16G16B16A16_FLOAT)
233         {
234             if (!gl_info->supported[ARB_HALF_FLOAT_VERTEX]) declaration->half_float_conv_needed = TRUE;
235         }
236     }
237
238     return WINED3D_OK;
239 }
240
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)
244 {
245     struct wined3d_vertex_declaration *object;
246     HRESULT hr;
247
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);
250
251     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
252     if(!object)
253     {
254         ERR("Failed to allocate vertex declaration memory.\n");
255         return E_OUTOFMEMORY;
256     }
257
258     hr = vertexdeclaration_init(object, device, elements, element_count, parent, parent_ops);
259     if (FAILED(hr))
260     {
261         WARN("Failed to initialize vertex declaration, hr %#x.\n", hr);
262         HeapFree(GetProcessHeap(), 0, object);
263         return hr;
264     }
265
266     TRACE("Created vertex declaration %p.\n", object);
267     *declaration = object;
268
269     return WINED3D_OK;
270 }
271
272 struct wined3d_fvf_convert_state
273 {
274     const struct wined3d_gl_info *gl_info;
275     struct wined3d_vertex_element *elements;
276     UINT offset;
277     UINT idx;
278 };
279
280 static void append_decl_element(struct wined3d_fvf_convert_state *state,
281         enum wined3d_format_id format_id, WINED3DDECLUSAGE usage, UINT usage_idx)
282 {
283     struct wined3d_vertex_element *elements = state->elements;
284     const struct wined3d_format *format;
285     UINT offset = state->offset;
286     UINT idx = state->idx;
287
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;
295
296     format = wined3d_get_format(state->gl_info, format_id);
297     state->offset += format->component_count * format->component_size;
298     ++state->idx;
299 }
300
301 static unsigned int convert_fvf_to_declaration(const struct wined3d_gl_info *gl_info,
302         DWORD fvf, struct wined3d_vertex_element **elements)
303 {
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);
314
315     DWORD num_textures = (fvf & WINED3DFVF_TEXCOUNT_MASK) >> WINED3DFVF_TEXCOUNT_SHIFT;
316     DWORD texcoords = (fvf & 0xFFFF0000) >> 16;
317     struct wined3d_fvf_convert_state state;
318     unsigned int size;
319     unsigned int idx;
320     DWORD num_blends = 1 + (((fvf & WINED3DFVF_XYZB5) - WINED3DFVF_XYZB1) >> 1);
321     if (has_blend_idx) num_blends--;
322
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;
326
327     state.gl_info = gl_info;
328     state.elements = HeapAlloc(GetProcessHeap(), 0, size * sizeof(*state.elements));
329     if (!state.elements) return ~0U;
330     state.offset = 0;
331     state.idx = 0;
332
333     if (has_pos)
334     {
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);
339         else
340             append_decl_element(&state, WINED3DFMT_R32G32B32_FLOAT, WINED3DDECLUSAGE_POSITION, 0);
341     }
342
343     if (has_blend && (num_blends > 0))
344     {
345         if ((fvf & WINED3DFVF_XYZB5) == WINED3DFVF_XYZB2 && (fvf & WINED3DFVF_LASTBETA_D3DCOLOR))
346             append_decl_element(&state, WINED3DFMT_B8G8R8A8_UNORM, WINED3DDECLUSAGE_BLENDWEIGHT, 0);
347         else
348         {
349             switch (num_blends)
350             {
351                 case 1:
352                     append_decl_element(&state, WINED3DFMT_R32_FLOAT, WINED3DDECLUSAGE_BLENDWEIGHT, 0);
353                     break;
354                 case 2:
355                     append_decl_element(&state, WINED3DFMT_R32G32_FLOAT, WINED3DDECLUSAGE_BLENDWEIGHT, 0);
356                     break;
357                 case 3:
358                     append_decl_element(&state, WINED3DFMT_R32G32B32_FLOAT, WINED3DDECLUSAGE_BLENDWEIGHT, 0);
359                     break;
360                 case 4:
361                     append_decl_element(&state, WINED3DFMT_R32G32B32A32_FLOAT, WINED3DDECLUSAGE_BLENDWEIGHT, 0);
362                     break;
363                 default:
364                     ERR("Unexpected amount of blend values: %u\n", num_blends);
365             }
366         }
367     }
368
369     if (has_blend_idx)
370     {
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);
376         else
377             append_decl_element(&state, WINED3DFMT_R32_FLOAT, WINED3DDECLUSAGE_BLENDINDICES, 0);
378     }
379
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);
384
385     for (idx = 0; idx < num_textures; ++idx)
386     {
387         switch ((texcoords >> (idx * 2)) & 0x03)
388         {
389             case WINED3DFVF_TEXTUREFORMAT1:
390                 append_decl_element(&state, WINED3DFMT_R32_FLOAT, WINED3DDECLUSAGE_TEXCOORD, idx);
391                 break;
392             case WINED3DFVF_TEXTUREFORMAT2:
393                 append_decl_element(&state, WINED3DFMT_R32G32_FLOAT, WINED3DDECLUSAGE_TEXCOORD, idx);
394                 break;
395             case WINED3DFVF_TEXTUREFORMAT3:
396                 append_decl_element(&state, WINED3DFMT_R32G32B32_FLOAT, WINED3DDECLUSAGE_TEXCOORD, idx);
397                 break;
398             case WINED3DFVF_TEXTUREFORMAT4:
399                 append_decl_element(&state, WINED3DFMT_R32G32B32A32_FLOAT, WINED3DDECLUSAGE_TEXCOORD, idx);
400                 break;
401         }
402     }
403
404     *elements = state.elements;
405     return size;
406 }
407
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)
411 {
412     struct wined3d_vertex_element *elements;
413     unsigned int size;
414     DWORD hr;
415
416     TRACE("device %p, fvf %#x, parent %p, parent_ops %p, declaration %p.\n",
417             device, fvf, parent, parent_ops, declaration);
418
419     size = convert_fvf_to_declaration(&device->adapter->gl_info, fvf, &elements);
420     if (size == ~0U) return E_OUTOFMEMORY;
421
422     hr = wined3d_vertex_declaration_create(device, elements, size, parent, parent_ops, declaration);
423     HeapFree(GetProcessHeap(), 0, elements);
424     return hr;
425 }