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 HRESULT vertexdeclaration_init(struct wined3d_vertex_declaration *declaration, IWineD3DDeviceImpl *device,
161 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;