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 "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(" input slot class: %s\n", debug_d3dinput_classification(element
->input_slot_class
));
38 TRACE("instance data step rate: %u\n", element
->instance_data_step_rate
);
39 TRACE(" method: %s (%#x)\n", debug_d3ddeclmethod(element
->method
), element
->method
);
40 TRACE(" usage: %s (%#x)\n", debug_d3ddeclusage(element
->usage
), element
->usage
);
41 TRACE(" usage_idx: %u\n", element
->usage_idx
);
44 ULONG CDECL
wined3d_vertex_declaration_incref(struct wined3d_vertex_declaration
*declaration
)
46 ULONG refcount
= InterlockedIncrement(&declaration
->ref
);
48 TRACE("%p increasing refcount to %u.\n", declaration
, refcount
);
53 ULONG CDECL
wined3d_vertex_declaration_decref(struct wined3d_vertex_declaration
*declaration
)
55 ULONG refcount
= InterlockedDecrement(&declaration
->ref
);
57 TRACE("%p decreasing refcount to %u.\n", declaration
, refcount
);
61 HeapFree(GetProcessHeap(), 0, declaration
->elements
);
62 declaration
->parent_ops
->wined3d_object_destroyed(declaration
->parent
);
63 HeapFree(GetProcessHeap(), 0, declaration
);
69 void * CDECL
wined3d_vertex_declaration_get_parent(const struct wined3d_vertex_declaration
*declaration
)
71 TRACE("declaration %p.\n", declaration
);
73 return declaration
->parent
;
76 static BOOL
declaration_element_valid_ffp(const struct wined3d_vertex_element
*element
)
78 switch(element
->usage
)
80 case WINED3D_DECL_USAGE_POSITION
:
81 case WINED3D_DECL_USAGE_POSITIONT
:
82 switch(element
->format
)
84 case WINED3DFMT_R32G32_FLOAT
:
85 case WINED3DFMT_R32G32B32_FLOAT
:
86 case WINED3DFMT_R32G32B32A32_FLOAT
:
87 case WINED3DFMT_R16G16_SINT
:
88 case WINED3DFMT_R16G16B16A16_SINT
:
89 case WINED3DFMT_R16G16_FLOAT
:
90 case WINED3DFMT_R16G16B16A16_FLOAT
:
96 case WINED3D_DECL_USAGE_BLEND_WEIGHT
:
97 switch(element
->format
)
99 case WINED3DFMT_R32_FLOAT
:
100 case WINED3DFMT_R32G32_FLOAT
:
101 case WINED3DFMT_R32G32B32_FLOAT
:
102 case WINED3DFMT_R32G32B32A32_FLOAT
:
103 case WINED3DFMT_B8G8R8A8_UNORM
:
104 case WINED3DFMT_R8G8B8A8_UINT
:
105 case WINED3DFMT_R16G16_SINT
:
106 case WINED3DFMT_R16G16B16A16_SINT
:
107 case WINED3DFMT_R16G16_FLOAT
:
108 case WINED3DFMT_R16G16B16A16_FLOAT
:
114 case WINED3D_DECL_USAGE_NORMAL
:
115 switch(element
->format
)
117 case WINED3DFMT_R32G32B32_FLOAT
:
118 case WINED3DFMT_R32G32B32A32_FLOAT
:
119 case WINED3DFMT_R16G16B16A16_SINT
:
120 case WINED3DFMT_R16G16B16A16_FLOAT
:
126 case WINED3D_DECL_USAGE_TEXCOORD
:
127 switch(element
->format
)
129 case WINED3DFMT_R32_FLOAT
:
130 case WINED3DFMT_R32G32_FLOAT
:
131 case WINED3DFMT_R32G32B32_FLOAT
:
132 case WINED3DFMT_R32G32B32A32_FLOAT
:
133 case WINED3DFMT_R16G16_SINT
:
134 case WINED3DFMT_R16G16B16A16_SINT
:
135 case WINED3DFMT_R16G16_FLOAT
:
136 case WINED3DFMT_R16G16B16A16_FLOAT
:
142 case WINED3D_DECL_USAGE_COLOR
:
143 switch(element
->format
)
145 case WINED3DFMT_R32G32B32_FLOAT
:
146 case WINED3DFMT_R32G32B32A32_FLOAT
:
147 case WINED3DFMT_B8G8R8A8_UNORM
:
148 case WINED3DFMT_R8G8B8A8_UINT
:
149 case WINED3DFMT_R16G16B16A16_SINT
:
150 case WINED3DFMT_R8G8B8A8_UNORM
:
151 case WINED3DFMT_R16G16B16A16_SNORM
:
152 case WINED3DFMT_R16G16B16A16_UNORM
:
153 case WINED3DFMT_R16G16B16A16_FLOAT
:
164 static HRESULT
vertexdeclaration_init(struct wined3d_vertex_declaration
*declaration
,
165 struct wined3d_device
*device
, const struct wined3d_vertex_element
*elements
, UINT element_count
,
166 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
168 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
171 if (TRACE_ON(d3d_decl
))
173 for (i
= 0; i
< element_count
; ++i
)
175 dump_wined3d_vertex_element(elements
+ i
);
179 declaration
->ref
= 1;
180 declaration
->parent
= parent
;
181 declaration
->parent_ops
= parent_ops
;
182 declaration
->device
= device
;
183 declaration
->elements
= HeapAlloc(GetProcessHeap(), 0, sizeof(*declaration
->elements
) * element_count
);
184 if (!declaration
->elements
)
186 ERR("Failed to allocate elements memory.\n");
187 return E_OUTOFMEMORY
;
189 declaration
->element_count
= element_count
;
191 /* Do some static analysis on the elements to make reading the
192 * declaration more comfortable for the drawing code. */
193 for (i
= 0; i
< element_count
; ++i
)
195 struct wined3d_vertex_declaration_element
*e
= &declaration
->elements
[i
];
197 e
->format
= wined3d_get_format(gl_info
, elements
[i
].format
);
198 e
->ffp_valid
= declaration_element_valid_ffp(&elements
[i
]);
199 e
->input_slot
= elements
[i
].input_slot
;
200 e
->offset
= elements
[i
].offset
;
201 e
->output_slot
= elements
[i
].output_slot
;
202 e
->input_slot_class
= elements
[i
].input_slot_class
;
203 e
->instance_data_step_rate
= elements
[i
].instance_data_step_rate
;
204 e
->method
= elements
[i
].method
;
205 e
->usage
= elements
[i
].usage
;
206 e
->usage_idx
= elements
[i
].usage_idx
;
208 if (e
->usage
== WINED3D_DECL_USAGE_POSITIONT
)
209 declaration
->position_transformed
= TRUE
;
211 /* Find the streams used in the declaration. The vertex buffers have
212 * to be loaded when drawing, but filter tesselation pseudo streams. */
213 if (e
->input_slot
>= MAX_STREAMS
) continue;
215 if (!e
->format
->gl_vtx_format
)
217 FIXME("The application tries to use an unsupported format (%s), returning E_FAIL.\n",
218 debug_d3dformat(elements
[i
].format
));
219 HeapFree(GetProcessHeap(), 0, declaration
->elements
);
223 if (e
->offset
== WINED3D_APPEND_ALIGNED_ELEMENT
)
225 const struct wined3d_vertex_declaration_element
*prev
;
229 for (j
= 1; j
<= i
; ++j
)
231 prev
= &declaration
->elements
[i
- j
];
232 if (prev
->input_slot
== e
->input_slot
)
234 e
->offset
= (prev
->offset
+ prev
->format
->byte_count
+ 3) & ~3;
242 WARN("Declaration element %u is not 4 byte aligned(%u), returning E_FAIL.\n", i
, e
->offset
);
243 HeapFree(GetProcessHeap(), 0, declaration
->elements
);
247 if (elements
[i
].format
== WINED3DFMT_R16G16_FLOAT
|| elements
[i
].format
== WINED3DFMT_R16G16B16A16_FLOAT
)
249 if (!gl_info
->supported
[ARB_HALF_FLOAT_VERTEX
]) declaration
->half_float_conv_needed
= TRUE
;
256 HRESULT CDECL
wined3d_vertex_declaration_create(struct wined3d_device
*device
,
257 const struct wined3d_vertex_element
*elements
, UINT element_count
, void *parent
,
258 const struct wined3d_parent_ops
*parent_ops
, struct wined3d_vertex_declaration
**declaration
)
260 struct wined3d_vertex_declaration
*object
;
263 TRACE("device %p, elements %p, element_count %u, parent %p, parent_ops %p, declaration %p.\n",
264 device
, elements
, element_count
, parent
, parent_ops
, declaration
);
266 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
268 return E_OUTOFMEMORY
;
270 hr
= vertexdeclaration_init(object
, device
, elements
, element_count
, parent
, parent_ops
);
273 WARN("Failed to initialize vertex declaration, hr %#x.\n", hr
);
274 HeapFree(GetProcessHeap(), 0, object
);
278 TRACE("Created vertex declaration %p.\n", object
);
279 *declaration
= object
;
284 struct wined3d_fvf_convert_state
286 const struct wined3d_gl_info
*gl_info
;
287 struct wined3d_vertex_element
*elements
;
292 static void append_decl_element(struct wined3d_fvf_convert_state
*state
,
293 enum wined3d_format_id format_id
, enum wined3d_decl_usage usage
, UINT usage_idx
)
295 struct wined3d_vertex_element
*elements
= state
->elements
;
296 const struct wined3d_format
*format
;
297 UINT offset
= state
->offset
;
298 UINT idx
= state
->idx
;
300 elements
[idx
].format
= format_id
;
301 elements
[idx
].input_slot
= 0;
302 elements
[idx
].offset
= offset
;
303 elements
[idx
].output_slot
= WINED3D_OUTPUT_SLOT_SEMANTIC
;
304 elements
[idx
].input_slot_class
= WINED3D_INPUT_PER_VERTEX_DATA
;
305 elements
[idx
].instance_data_step_rate
= 0;
306 elements
[idx
].method
= WINED3D_DECL_METHOD_DEFAULT
;
307 elements
[idx
].usage
= usage
;
308 elements
[idx
].usage_idx
= usage_idx
;
310 format
= wined3d_get_format(state
->gl_info
, format_id
);
311 state
->offset
+= format
->component_count
* format
->component_size
;
315 static unsigned int convert_fvf_to_declaration(const struct wined3d_gl_info
*gl_info
,
316 DWORD fvf
, struct wined3d_vertex_element
**elements
)
318 BOOL has_pos
= !!(fvf
& WINED3DFVF_POSITION_MASK
);
319 BOOL has_blend
= (fvf
& WINED3DFVF_XYZB5
) > WINED3DFVF_XYZRHW
;
320 BOOL has_blend_idx
= has_blend
&&
321 (((fvf
& WINED3DFVF_XYZB5
) == WINED3DFVF_XYZB5
) ||
322 (fvf
& WINED3DFVF_LASTBETA_D3DCOLOR
) ||
323 (fvf
& WINED3DFVF_LASTBETA_UBYTE4
));
324 BOOL has_normal
= !!(fvf
& WINED3DFVF_NORMAL
);
325 BOOL has_psize
= !!(fvf
& WINED3DFVF_PSIZE
);
326 BOOL has_diffuse
= !!(fvf
& WINED3DFVF_DIFFUSE
);
327 BOOL has_specular
= !!(fvf
& WINED3DFVF_SPECULAR
);
329 DWORD num_textures
= (fvf
& WINED3DFVF_TEXCOUNT_MASK
) >> WINED3DFVF_TEXCOUNT_SHIFT
;
330 DWORD texcoords
= (fvf
& 0xffff0000) >> 16;
331 struct wined3d_fvf_convert_state state
;
334 DWORD num_blends
= 1 + (((fvf
& WINED3DFVF_XYZB5
) - WINED3DFVF_XYZB1
) >> 1);
335 if (has_blend_idx
) num_blends
--;
337 /* Compute declaration size */
338 size
= has_pos
+ (has_blend
&& num_blends
> 0) + has_blend_idx
+ has_normal
+
339 has_psize
+ has_diffuse
+ has_specular
+ num_textures
;
341 state
.gl_info
= gl_info
;
342 state
.elements
= HeapAlloc(GetProcessHeap(), 0, size
* sizeof(*state
.elements
));
343 if (!state
.elements
) return ~0U;
349 if (!has_blend
&& (fvf
& WINED3DFVF_XYZRHW
))
350 append_decl_element(&state
, WINED3DFMT_R32G32B32A32_FLOAT
, WINED3D_DECL_USAGE_POSITIONT
, 0);
351 else if ((fvf
& WINED3DFVF_XYZW
) == WINED3DFVF_XYZW
)
352 append_decl_element(&state
, WINED3DFMT_R32G32B32A32_FLOAT
, WINED3D_DECL_USAGE_POSITION
, 0);
354 append_decl_element(&state
, WINED3DFMT_R32G32B32_FLOAT
, WINED3D_DECL_USAGE_POSITION
, 0);
357 if (has_blend
&& (num_blends
> 0))
359 if ((fvf
& WINED3DFVF_XYZB5
) == WINED3DFVF_XYZB2
&& (fvf
& WINED3DFVF_LASTBETA_D3DCOLOR
))
360 append_decl_element(&state
, WINED3DFMT_B8G8R8A8_UNORM
, WINED3D_DECL_USAGE_BLEND_WEIGHT
, 0);
366 append_decl_element(&state
, WINED3DFMT_R32_FLOAT
, WINED3D_DECL_USAGE_BLEND_WEIGHT
, 0);
369 append_decl_element(&state
, WINED3DFMT_R32G32_FLOAT
, WINED3D_DECL_USAGE_BLEND_WEIGHT
, 0);
372 append_decl_element(&state
, WINED3DFMT_R32G32B32_FLOAT
, WINED3D_DECL_USAGE_BLEND_WEIGHT
, 0);
375 append_decl_element(&state
, WINED3DFMT_R32G32B32A32_FLOAT
, WINED3D_DECL_USAGE_BLEND_WEIGHT
, 0);
378 ERR("Unexpected amount of blend values: %u\n", num_blends
);
385 if ((fvf
& WINED3DFVF_LASTBETA_UBYTE4
)
386 || ((fvf
& WINED3DFVF_XYZB5
) == WINED3DFVF_XYZB2
&& (fvf
& WINED3DFVF_LASTBETA_D3DCOLOR
)))
387 append_decl_element(&state
, WINED3DFMT_R8G8B8A8_UINT
, WINED3D_DECL_USAGE_BLEND_INDICES
, 0);
388 else if (fvf
& WINED3DFVF_LASTBETA_D3DCOLOR
)
389 append_decl_element(&state
, WINED3DFMT_B8G8R8A8_UNORM
, WINED3D_DECL_USAGE_BLEND_INDICES
, 0);
391 append_decl_element(&state
, WINED3DFMT_R32_FLOAT
, WINED3D_DECL_USAGE_BLEND_INDICES
, 0);
395 append_decl_element(&state
, WINED3DFMT_R32G32B32_FLOAT
, WINED3D_DECL_USAGE_NORMAL
, 0);
397 append_decl_element(&state
, WINED3DFMT_R32_FLOAT
, WINED3D_DECL_USAGE_PSIZE
, 0);
399 append_decl_element(&state
, WINED3DFMT_B8G8R8A8_UNORM
, WINED3D_DECL_USAGE_COLOR
, 0);
401 append_decl_element(&state
, WINED3DFMT_B8G8R8A8_UNORM
, WINED3D_DECL_USAGE_COLOR
, 1);
403 for (idx
= 0; idx
< num_textures
; ++idx
)
405 switch ((texcoords
>> (idx
* 2)) & 0x03)
407 case WINED3DFVF_TEXTUREFORMAT1
:
408 append_decl_element(&state
, WINED3DFMT_R32_FLOAT
, WINED3D_DECL_USAGE_TEXCOORD
, idx
);
410 case WINED3DFVF_TEXTUREFORMAT2
:
411 append_decl_element(&state
, WINED3DFMT_R32G32_FLOAT
, WINED3D_DECL_USAGE_TEXCOORD
, idx
);
413 case WINED3DFVF_TEXTUREFORMAT3
:
414 append_decl_element(&state
, WINED3DFMT_R32G32B32_FLOAT
, WINED3D_DECL_USAGE_TEXCOORD
, idx
);
416 case WINED3DFVF_TEXTUREFORMAT4
:
417 append_decl_element(&state
, WINED3DFMT_R32G32B32A32_FLOAT
, WINED3D_DECL_USAGE_TEXCOORD
, idx
);
422 *elements
= state
.elements
;
426 HRESULT CDECL
wined3d_vertex_declaration_create_from_fvf(struct wined3d_device
*device
,
427 DWORD fvf
, void *parent
, const struct wined3d_parent_ops
*parent_ops
,
428 struct wined3d_vertex_declaration
**declaration
)
430 struct wined3d_vertex_element
*elements
;
434 TRACE("device %p, fvf %#x, parent %p, parent_ops %p, declaration %p.\n",
435 device
, fvf
, parent
, parent_ops
, declaration
);
437 size
= convert_fvf_to_declaration(&device
->adapter
->gl_info
, fvf
, &elements
);
438 if (size
== ~0U) return E_OUTOFMEMORY
;
440 hr
= wined3d_vertex_declaration_create(device
, elements
, size
, parent
, parent_ops
, declaration
);
441 HeapFree(GetProcessHeap(), 0, elements
);