2 * IDirect3DVertexDeclaration9 implementation
4 * Copyright 2002-2003 Raphael Junqueira
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wine/port.h"
24 #include "d3d9_private.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d9
);
30 enum wined3d_format_id format
;
31 unsigned int component_count
;
32 unsigned int component_size
;
36 /* D3DDECLTYPE_FLOAT1 */ {WINED3DFMT_R32_FLOAT
, 1, sizeof(float)},
37 /* D3DDECLTYPE_FLOAT2 */ {WINED3DFMT_R32G32_FLOAT
, 2, sizeof(float)},
38 /* D3DDECLTYPE_FLOAT3 */ {WINED3DFMT_R32G32B32_FLOAT
, 3, sizeof(float)},
39 /* D3DDECLTYPE_FLOAT4 */ {WINED3DFMT_R32G32B32A32_FLOAT
, 4, sizeof(float)},
40 /* D3DDECLTYPE_D3DCOLOR */ {WINED3DFMT_B8G8R8A8_UNORM
, 4, sizeof(BYTE
)},
41 /* D3DDECLTYPE_UBYTE4 */ {WINED3DFMT_R8G8B8A8_UINT
, 4, sizeof(BYTE
)},
42 /* D3DDECLTYPE_SHORT2 */ {WINED3DFMT_R16G16_SINT
, 2, sizeof(short int)},
43 /* D3DDECLTYPE_SHORT4 */ {WINED3DFMT_R16G16B16A16_SINT
, 4, sizeof(short int)},
44 /* D3DDECLTYPE_UBYTE4N */ {WINED3DFMT_R8G8B8A8_UNORM
, 4, sizeof(BYTE
)},
45 /* D3DDECLTYPE_SHORT2N */ {WINED3DFMT_R16G16_SNORM
, 2, sizeof(short int)},
46 /* D3DDECLTYPE_SHORT4N */ {WINED3DFMT_R16G16B16A16_SNORM
, 4, sizeof(short int)},
47 /* D3DDECLTYPE_USHORT2N */ {WINED3DFMT_R16G16_UNORM
, 2, sizeof(short int)},
48 /* D3DDECLTYPE_USHORT4N */ {WINED3DFMT_R16G16B16A16_UNORM
, 4, sizeof(short int)},
49 /* D3DDECLTYPE_UDEC3 */ {WINED3DFMT_R10G10B10X2_UINT
, 3, sizeof(short int)},
50 /* D3DDECLTYPE_DEC3N */ {WINED3DFMT_R10G10B10X2_SNORM
, 3, sizeof(short int)},
51 /* D3DDECLTYPE_FLOAT16_2 */ {WINED3DFMT_R16G16_FLOAT
, 2, sizeof(short int)},
52 /* D3DDECLTYPE_FLOAT16_4 */ {WINED3DFMT_R16G16B16A16_FLOAT
, 4, sizeof(short int)}
55 static inline struct d3d9_vertex_declaration
*impl_from_IDirect3DVertexDeclaration9(IDirect3DVertexDeclaration9
*iface
)
57 return CONTAINING_RECORD(iface
, struct d3d9_vertex_declaration
, IDirect3DVertexDeclaration9_iface
);
60 HRESULT
vdecl_convert_fvf(
62 D3DVERTEXELEMENT9
** ppVertexElements
) {
64 unsigned int idx
, idx2
;
66 BOOL has_pos
= (fvf
& D3DFVF_POSITION_MASK
) != 0;
67 BOOL has_blend
= (fvf
& D3DFVF_XYZB5
) > D3DFVF_XYZRHW
;
68 BOOL has_blend_idx
= has_blend
&&
69 (((fvf
& D3DFVF_XYZB5
) == D3DFVF_XYZB5
) ||
70 (fvf
& D3DFVF_LASTBETA_D3DCOLOR
) ||
71 (fvf
& D3DFVF_LASTBETA_UBYTE4
));
72 BOOL has_normal
= (fvf
& D3DFVF_NORMAL
) != 0;
73 BOOL has_psize
= (fvf
& D3DFVF_PSIZE
) != 0;
75 BOOL has_diffuse
= (fvf
& D3DFVF_DIFFUSE
) != 0;
76 BOOL has_specular
= (fvf
& D3DFVF_SPECULAR
) !=0;
78 DWORD num_textures
= (fvf
& D3DFVF_TEXCOUNT_MASK
) >> D3DFVF_TEXCOUNT_SHIFT
;
79 DWORD texcoords
= (fvf
& 0xFFFF0000) >> 16;
81 D3DVERTEXELEMENT9 end_element
= D3DDECL_END();
82 D3DVERTEXELEMENT9
*elements
= NULL
;
85 DWORD num_blends
= 1 + (((fvf
& D3DFVF_XYZB5
) - D3DFVF_XYZB1
) >> 1);
86 if (has_blend_idx
) num_blends
--;
88 /* Compute declaration size */
89 size
= has_pos
+ (has_blend
&& num_blends
> 0) + has_blend_idx
+ has_normal
+
90 has_psize
+ has_diffuse
+ has_specular
+ num_textures
+ 1;
92 /* convert the declaration */
93 if (!(elements
= heap_alloc(size
* sizeof(*elements
))))
94 return D3DERR_OUTOFVIDEOMEMORY
;
96 elements
[size
-1] = end_element
;
99 if (!has_blend
&& (fvf
& D3DFVF_XYZRHW
)) {
100 elements
[idx
].Type
= D3DDECLTYPE_FLOAT4
;
101 elements
[idx
].Usage
= D3DDECLUSAGE_POSITIONT
;
103 else if (!has_blend
&& (fvf
& D3DFVF_XYZW
) == D3DFVF_XYZW
) {
104 elements
[idx
].Type
= D3DDECLTYPE_FLOAT4
;
105 elements
[idx
].Usage
= D3DDECLUSAGE_POSITION
;
108 elements
[idx
].Type
= D3DDECLTYPE_FLOAT3
;
109 elements
[idx
].Usage
= D3DDECLUSAGE_POSITION
;
111 elements
[idx
].UsageIndex
= 0;
114 if (has_blend
&& (num_blends
> 0)) {
115 if (((fvf
& D3DFVF_XYZB5
) == D3DFVF_XYZB2
) && (fvf
& D3DFVF_LASTBETA_D3DCOLOR
))
116 elements
[idx
].Type
= D3DDECLTYPE_D3DCOLOR
;
119 case 1: elements
[idx
].Type
= D3DDECLTYPE_FLOAT1
; break;
120 case 2: elements
[idx
].Type
= D3DDECLTYPE_FLOAT2
; break;
121 case 3: elements
[idx
].Type
= D3DDECLTYPE_FLOAT3
; break;
122 case 4: elements
[idx
].Type
= D3DDECLTYPE_FLOAT4
; break;
124 ERR("Unexpected amount of blend values: %u\n", num_blends
);
127 elements
[idx
].Usage
= D3DDECLUSAGE_BLENDWEIGHT
;
128 elements
[idx
].UsageIndex
= 0;
132 if (fvf
& D3DFVF_LASTBETA_UBYTE4
||
133 (((fvf
& D3DFVF_XYZB5
) == D3DFVF_XYZB2
) && (fvf
& D3DFVF_LASTBETA_D3DCOLOR
)))
134 elements
[idx
].Type
= D3DDECLTYPE_UBYTE4
;
135 else if (fvf
& D3DFVF_LASTBETA_D3DCOLOR
)
136 elements
[idx
].Type
= D3DDECLTYPE_D3DCOLOR
;
138 elements
[idx
].Type
= D3DDECLTYPE_FLOAT1
;
139 elements
[idx
].Usage
= D3DDECLUSAGE_BLENDINDICES
;
140 elements
[idx
].UsageIndex
= 0;
144 elements
[idx
].Type
= D3DDECLTYPE_FLOAT3
;
145 elements
[idx
].Usage
= D3DDECLUSAGE_NORMAL
;
146 elements
[idx
].UsageIndex
= 0;
150 elements
[idx
].Type
= D3DDECLTYPE_FLOAT1
;
151 elements
[idx
].Usage
= D3DDECLUSAGE_PSIZE
;
152 elements
[idx
].UsageIndex
= 0;
156 elements
[idx
].Type
= D3DDECLTYPE_D3DCOLOR
;
157 elements
[idx
].Usage
= D3DDECLUSAGE_COLOR
;
158 elements
[idx
].UsageIndex
= 0;
162 elements
[idx
].Type
= D3DDECLTYPE_D3DCOLOR
;
163 elements
[idx
].Usage
= D3DDECLUSAGE_COLOR
;
164 elements
[idx
].UsageIndex
= 1;
167 for (idx2
= 0; idx2
< num_textures
; idx2
++) {
168 unsigned int numcoords
= (texcoords
>> (idx2
*2)) & 0x03;
170 case D3DFVF_TEXTUREFORMAT1
:
171 elements
[idx
].Type
= D3DDECLTYPE_FLOAT1
;
173 case D3DFVF_TEXTUREFORMAT2
:
174 elements
[idx
].Type
= D3DDECLTYPE_FLOAT2
;
176 case D3DFVF_TEXTUREFORMAT3
:
177 elements
[idx
].Type
= D3DDECLTYPE_FLOAT3
;
179 case D3DFVF_TEXTUREFORMAT4
:
180 elements
[idx
].Type
= D3DDECLTYPE_FLOAT4
;
183 elements
[idx
].Usage
= D3DDECLUSAGE_TEXCOORD
;
184 elements
[idx
].UsageIndex
= idx2
;
188 /* Now compute offsets, and initialize the rest of the fields */
189 for (idx
= 0, offset
= 0; idx
< size
-1; idx
++) {
190 elements
[idx
].Stream
= 0;
191 elements
[idx
].Method
= D3DDECLMETHOD_DEFAULT
;
192 elements
[idx
].Offset
= offset
;
193 offset
+= d3d_dtype_lookup
[elements
[idx
].Type
].component_count
194 * d3d_dtype_lookup
[elements
[idx
].Type
].component_size
;
197 *ppVertexElements
= elements
;
201 static HRESULT WINAPI
d3d9_vertex_declaration_QueryInterface(IDirect3DVertexDeclaration9
*iface
,
202 REFIID riid
, void **out
)
204 TRACE("iface %p, riid %s, out %p.\n", iface
, debugstr_guid(riid
), out
);
206 if (IsEqualGUID(riid
, &IID_IDirect3DVertexDeclaration9
)
207 || IsEqualGUID(riid
, &IID_IUnknown
))
209 IDirect3DVertexDeclaration9_AddRef(iface
);
214 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid
));
217 return E_NOINTERFACE
;
220 static ULONG WINAPI
d3d9_vertex_declaration_AddRef(IDirect3DVertexDeclaration9
*iface
)
222 struct d3d9_vertex_declaration
*declaration
= impl_from_IDirect3DVertexDeclaration9(iface
);
223 ULONG refcount
= InterlockedIncrement(&declaration
->refcount
);
225 TRACE("%p increasing refcount to %u.\n", iface
, refcount
);
229 IDirect3DDevice9Ex_AddRef(declaration
->parent_device
);
230 wined3d_mutex_lock();
231 wined3d_vertex_declaration_incref(declaration
->wined3d_declaration
);
232 wined3d_mutex_unlock();
238 static ULONG WINAPI
d3d9_vertex_declaration_Release(IDirect3DVertexDeclaration9
*iface
)
240 struct d3d9_vertex_declaration
*declaration
= impl_from_IDirect3DVertexDeclaration9(iface
);
241 ULONG refcount
= InterlockedDecrement(&declaration
->refcount
);
243 TRACE("%p decreasing refcount to %u.\n", iface
, refcount
);
247 IDirect3DDevice9Ex
*parent_device
= declaration
->parent_device
;
248 wined3d_mutex_lock();
249 wined3d_vertex_declaration_decref(declaration
->wined3d_declaration
);
250 wined3d_mutex_unlock();
252 /* Release the device last, as it may cause the device to be destroyed. */
253 IDirect3DDevice9Ex_Release(parent_device
);
259 static HRESULT WINAPI
d3d9_vertex_declaration_GetDevice(IDirect3DVertexDeclaration9
*iface
, IDirect3DDevice9
**device
)
261 struct d3d9_vertex_declaration
*declaration
= impl_from_IDirect3DVertexDeclaration9(iface
);
263 TRACE("iface %p, device %p.\n", iface
, device
);
265 *device
= (IDirect3DDevice9
*)declaration
->parent_device
;
266 IDirect3DDevice9_AddRef(*device
);
268 TRACE("Returning device %p.\n", *device
);
273 static HRESULT WINAPI
d3d9_vertex_declaration_GetDeclaration(IDirect3DVertexDeclaration9
*iface
,
274 D3DVERTEXELEMENT9
*elements
, UINT
*element_count
)
276 struct d3d9_vertex_declaration
*declaration
= impl_from_IDirect3DVertexDeclaration9(iface
);
278 TRACE("iface %p, elements %p, element_count %p.\n", iface
, elements
, element_count
);
280 *element_count
= declaration
->element_count
;
282 /* Passing a NULL elements is used to just retrieve the number of elements */
286 TRACE("Copying %p to %p.\n", declaration
->elements
, elements
);
287 memcpy(elements
, declaration
->elements
, sizeof(*declaration
->elements
) * declaration
->element_count
);
292 static const struct IDirect3DVertexDeclaration9Vtbl d3d9_vertex_declaration_vtbl
=
295 d3d9_vertex_declaration_QueryInterface
,
296 d3d9_vertex_declaration_AddRef
,
297 d3d9_vertex_declaration_Release
,
298 /* IDirect3DVertexDeclaration9 */
299 d3d9_vertex_declaration_GetDevice
,
300 d3d9_vertex_declaration_GetDeclaration
,
303 struct d3d9_vertex_declaration
*unsafe_impl_from_IDirect3DVertexDeclaration9(IDirect3DVertexDeclaration9
*iface
)
307 assert(iface
->lpVtbl
== &d3d9_vertex_declaration_vtbl
);
308 return CONTAINING_RECORD(iface
, struct d3d9_vertex_declaration
, IDirect3DVertexDeclaration9_iface
);
311 static void STDMETHODCALLTYPE
d3d9_vertexdeclaration_wined3d_object_destroyed(void *parent
)
313 struct d3d9_vertex_declaration
*declaration
= parent
;
314 heap_free(declaration
->elements
);
315 heap_free(declaration
);
318 static const struct wined3d_parent_ops d3d9_vertexdeclaration_wined3d_parent_ops
=
320 d3d9_vertexdeclaration_wined3d_object_destroyed
,
323 static HRESULT
convert_to_wined3d_declaration(const D3DVERTEXELEMENT9
*d3d9_elements
,
324 struct wined3d_vertex_element
**wined3d_elements
, UINT
*element_count
)
326 const D3DVERTEXELEMENT9
* element
;
330 TRACE("d3d9_elements %p, wined3d_elements %p, element_count %p\n", d3d9_elements
, wined3d_elements
, element_count
);
332 element
= d3d9_elements
;
333 while (element
++->Stream
!= 0xff && count
++ < 128);
335 if (count
== 128) return E_FAIL
;
337 /* Skip the END element */
340 if (!(*wined3d_elements
= heap_alloc(count
* sizeof(**wined3d_elements
))))
342 FIXME("Memory allocation failed\n");
343 return D3DERR_OUTOFVIDEOMEMORY
;
346 for (i
= 0; i
< count
; ++i
)
348 if (d3d9_elements
[i
].Type
>= ARRAY_SIZE(d3d_dtype_lookup
))
350 WARN("Invalid element type %#x.\n", d3d9_elements
[i
].Type
);
351 heap_free(*wined3d_elements
);
354 (*wined3d_elements
)[i
].format
= d3d_dtype_lookup
[d3d9_elements
[i
].Type
].format
;
355 (*wined3d_elements
)[i
].input_slot
= d3d9_elements
[i
].Stream
;
356 (*wined3d_elements
)[i
].offset
= d3d9_elements
[i
].Offset
;
357 (*wined3d_elements
)[i
].output_slot
= WINED3D_OUTPUT_SLOT_SEMANTIC
;
358 (*wined3d_elements
)[i
].input_slot_class
= WINED3D_INPUT_PER_VERTEX_DATA
;
359 (*wined3d_elements
)[i
].instance_data_step_rate
= 0;
360 (*wined3d_elements
)[i
].method
= d3d9_elements
[i
].Method
;
361 (*wined3d_elements
)[i
].usage
= d3d9_elements
[i
].Usage
;
362 (*wined3d_elements
)[i
].usage_idx
= d3d9_elements
[i
].UsageIndex
;
365 *element_count
= count
;
370 static HRESULT
vertexdeclaration_init(struct d3d9_vertex_declaration
*declaration
,
371 struct d3d9_device
*device
, const D3DVERTEXELEMENT9
*elements
)
373 struct wined3d_vertex_element
*wined3d_elements
;
374 UINT wined3d_element_count
;
378 hr
= convert_to_wined3d_declaration(elements
, &wined3d_elements
, &wined3d_element_count
);
381 WARN("Failed to create wined3d vertex declaration elements, hr %#x.\n", hr
);
385 declaration
->IDirect3DVertexDeclaration9_iface
.lpVtbl
= &d3d9_vertex_declaration_vtbl
;
386 declaration
->refcount
= 1;
388 element_count
= wined3d_element_count
+ 1;
389 if (!(declaration
->elements
= heap_alloc(element_count
* sizeof(*declaration
->elements
))))
391 heap_free(wined3d_elements
);
392 ERR("Failed to allocate vertex declaration elements memory.\n");
393 return D3DERR_OUTOFVIDEOMEMORY
;
395 memcpy(declaration
->elements
, elements
, element_count
* sizeof(*elements
));
396 declaration
->element_count
= element_count
;
398 wined3d_mutex_lock();
399 hr
= wined3d_vertex_declaration_create(device
->wined3d_device
, wined3d_elements
, wined3d_element_count
,
400 declaration
, &d3d9_vertexdeclaration_wined3d_parent_ops
, &declaration
->wined3d_declaration
);
401 wined3d_mutex_unlock();
402 heap_free(wined3d_elements
);
405 heap_free(declaration
->elements
);
406 WARN("Failed to create wined3d vertex declaration, hr %#x.\n", hr
);
410 declaration
->parent_device
= &device
->IDirect3DDevice9Ex_iface
;
411 IDirect3DDevice9Ex_AddRef(declaration
->parent_device
);
416 HRESULT
d3d9_vertex_declaration_create(struct d3d9_device
*device
,
417 const D3DVERTEXELEMENT9
*elements
, struct d3d9_vertex_declaration
**declaration
)
419 struct d3d9_vertex_declaration
*object
;
422 if (!(object
= heap_alloc_zero(sizeof(*object
))))
423 return E_OUTOFMEMORY
;
425 hr
= vertexdeclaration_init(object
, device
, elements
);
428 WARN("Failed to initialize vertex declaration, hr %#x.\n", hr
);
433 TRACE("Created vertex declaration %p.\n", object
);
434 *declaration
= object
;