advapi32: Return a fake handle from EventRegister.
[wine/multimedia.git] / dlls / d3d9 / vertexdeclaration.c
blob65d8d189de82a36408ca63d58a7932a6e6236f80
1 /*
2 * IDirect3DVertexDeclaration9 implementation
4 * Copyright 2002-2003 Raphael Junqueira
5 * Jason Edmeades
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
22 #include "config.h"
23 #include "d3d9_private.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
27 typedef struct _D3DDECLTYPE_INFO {
28 D3DDECLTYPE d3dType;
29 enum wined3d_format_id format;
30 int size;
31 int typesize;
32 } D3DDECLTYPE_INFO;
34 static D3DDECLTYPE_INFO const d3d_dtype_lookup[D3DDECLTYPE_UNUSED] = {
35 {D3DDECLTYPE_FLOAT1, WINED3DFMT_R32_FLOAT, 1, sizeof(float)},
36 {D3DDECLTYPE_FLOAT2, WINED3DFMT_R32G32_FLOAT, 2, sizeof(float)},
37 {D3DDECLTYPE_FLOAT3, WINED3DFMT_R32G32B32_FLOAT, 3, sizeof(float)},
38 {D3DDECLTYPE_FLOAT4, WINED3DFMT_R32G32B32A32_FLOAT, 4, sizeof(float)},
39 {D3DDECLTYPE_D3DCOLOR, WINED3DFMT_B8G8R8A8_UNORM, 4, sizeof(BYTE)},
40 {D3DDECLTYPE_UBYTE4, WINED3DFMT_R8G8B8A8_UINT, 4, sizeof(BYTE)},
41 {D3DDECLTYPE_SHORT2, WINED3DFMT_R16G16_SINT, 2, sizeof(short int)},
42 {D3DDECLTYPE_SHORT4, WINED3DFMT_R16G16B16A16_SINT, 4, sizeof(short int)},
43 {D3DDECLTYPE_UBYTE4N, WINED3DFMT_R8G8B8A8_UNORM, 4, sizeof(BYTE)},
44 {D3DDECLTYPE_SHORT2N, WINED3DFMT_R16G16_SNORM, 2, sizeof(short int)},
45 {D3DDECLTYPE_SHORT4N, WINED3DFMT_R16G16B16A16_SNORM, 4, sizeof(short int)},
46 {D3DDECLTYPE_USHORT2N, WINED3DFMT_R16G16_UNORM, 2, sizeof(short int)},
47 {D3DDECLTYPE_USHORT4N, WINED3DFMT_R16G16B16A16_UNORM, 4, sizeof(short int)},
48 {D3DDECLTYPE_UDEC3, WINED3DFMT_R10G10B10A2_UINT, 3, sizeof(short int)},
49 {D3DDECLTYPE_DEC3N, WINED3DFMT_R10G10B10A2_SNORM, 3, sizeof(short int)},
50 {D3DDECLTYPE_FLOAT16_2, WINED3DFMT_R16G16_FLOAT, 2, sizeof(short int)},
51 {D3DDECLTYPE_FLOAT16_4, WINED3DFMT_R16G16B16A16_FLOAT, 4, sizeof(short int)}};
53 #define D3D_DECL_SIZE(type) d3d_dtype_lookup[type].size
54 #define D3D_DECL_TYPESIZE(type) d3d_dtype_lookup[type].typesize
56 static inline IDirect3DVertexDeclaration9Impl *impl_from_IDirect3DVertexDeclaration9(IDirect3DVertexDeclaration9 *iface)
58 return CONTAINING_RECORD(iface, IDirect3DVertexDeclaration9Impl, IDirect3DVertexDeclaration9_iface);
61 HRESULT vdecl_convert_fvf(
62 DWORD fvf,
63 D3DVERTEXELEMENT9** ppVertexElements) {
65 unsigned int idx, idx2;
66 unsigned int offset;
67 BOOL has_pos = (fvf & D3DFVF_POSITION_MASK) != 0;
68 BOOL has_blend = (fvf & D3DFVF_XYZB5) > D3DFVF_XYZRHW;
69 BOOL has_blend_idx = has_blend &&
70 (((fvf & D3DFVF_XYZB5) == D3DFVF_XYZB5) ||
71 (fvf & D3DFVF_LASTBETA_D3DCOLOR) ||
72 (fvf & D3DFVF_LASTBETA_UBYTE4));
73 BOOL has_normal = (fvf & D3DFVF_NORMAL) != 0;
74 BOOL has_psize = (fvf & D3DFVF_PSIZE) != 0;
76 BOOL has_diffuse = (fvf & D3DFVF_DIFFUSE) != 0;
77 BOOL has_specular = (fvf & D3DFVF_SPECULAR) !=0;
79 DWORD num_textures = (fvf & D3DFVF_TEXCOUNT_MASK) >> D3DFVF_TEXCOUNT_SHIFT;
80 DWORD texcoords = (fvf & 0xFFFF0000) >> 16;
82 D3DVERTEXELEMENT9 end_element = D3DDECL_END();
83 D3DVERTEXELEMENT9 *elements = NULL;
85 unsigned int size;
86 DWORD num_blends = 1 + (((fvf & D3DFVF_XYZB5) - D3DFVF_XYZB1) >> 1);
87 if (has_blend_idx) num_blends--;
89 /* Compute declaration size */
90 size = has_pos + (has_blend && num_blends > 0) + has_blend_idx + has_normal +
91 has_psize + has_diffuse + has_specular + num_textures + 1;
93 /* convert the declaration */
94 elements = HeapAlloc(GetProcessHeap(), 0, size * sizeof(D3DVERTEXELEMENT9));
95 if (!elements) return D3DERR_OUTOFVIDEOMEMORY;
97 elements[size-1] = end_element;
98 idx = 0;
99 if (has_pos) {
100 if (!has_blend && (fvf & D3DFVF_XYZRHW)) {
101 elements[idx].Type = D3DDECLTYPE_FLOAT4;
102 elements[idx].Usage = D3DDECLUSAGE_POSITIONT;
104 else if (!has_blend && (fvf & D3DFVF_XYZW) == D3DFVF_XYZW) {
105 elements[idx].Type = D3DDECLTYPE_FLOAT4;
106 elements[idx].Usage = D3DDECLUSAGE_POSITION;
108 else {
109 elements[idx].Type = D3DDECLTYPE_FLOAT3;
110 elements[idx].Usage = D3DDECLUSAGE_POSITION;
112 elements[idx].UsageIndex = 0;
113 idx++;
115 if (has_blend && (num_blends > 0)) {
116 if (((fvf & D3DFVF_XYZB5) == D3DFVF_XYZB2) && (fvf & D3DFVF_LASTBETA_D3DCOLOR))
117 elements[idx].Type = D3DDECLTYPE_D3DCOLOR;
118 else {
119 switch(num_blends) {
120 case 1: elements[idx].Type = D3DDECLTYPE_FLOAT1; break;
121 case 2: elements[idx].Type = D3DDECLTYPE_FLOAT2; break;
122 case 3: elements[idx].Type = D3DDECLTYPE_FLOAT3; break;
123 case 4: elements[idx].Type = D3DDECLTYPE_FLOAT4; break;
124 default:
125 ERR("Unexpected amount of blend values: %u\n", num_blends);
128 elements[idx].Usage = D3DDECLUSAGE_BLENDWEIGHT;
129 elements[idx].UsageIndex = 0;
130 idx++;
132 if (has_blend_idx) {
133 if (fvf & D3DFVF_LASTBETA_UBYTE4 ||
134 (((fvf & D3DFVF_XYZB5) == D3DFVF_XYZB2) && (fvf & D3DFVF_LASTBETA_D3DCOLOR)))
135 elements[idx].Type = D3DDECLTYPE_UBYTE4;
136 else if (fvf & D3DFVF_LASTBETA_D3DCOLOR)
137 elements[idx].Type = D3DDECLTYPE_D3DCOLOR;
138 else
139 elements[idx].Type = D3DDECLTYPE_FLOAT1;
140 elements[idx].Usage = D3DDECLUSAGE_BLENDINDICES;
141 elements[idx].UsageIndex = 0;
142 idx++;
144 if (has_normal) {
145 elements[idx].Type = D3DDECLTYPE_FLOAT3;
146 elements[idx].Usage = D3DDECLUSAGE_NORMAL;
147 elements[idx].UsageIndex = 0;
148 idx++;
150 if (has_psize) {
151 elements[idx].Type = D3DDECLTYPE_FLOAT1;
152 elements[idx].Usage = D3DDECLUSAGE_PSIZE;
153 elements[idx].UsageIndex = 0;
154 idx++;
156 if (has_diffuse) {
157 elements[idx].Type = D3DDECLTYPE_D3DCOLOR;
158 elements[idx].Usage = D3DDECLUSAGE_COLOR;
159 elements[idx].UsageIndex = 0;
160 idx++;
162 if (has_specular) {
163 elements[idx].Type = D3DDECLTYPE_D3DCOLOR;
164 elements[idx].Usage = D3DDECLUSAGE_COLOR;
165 elements[idx].UsageIndex = 1;
166 idx++;
168 for (idx2 = 0; idx2 < num_textures; idx2++) {
169 unsigned int numcoords = (texcoords >> (idx2*2)) & 0x03;
170 switch (numcoords) {
171 case D3DFVF_TEXTUREFORMAT1:
172 elements[idx].Type = D3DDECLTYPE_FLOAT1;
173 break;
174 case D3DFVF_TEXTUREFORMAT2:
175 elements[idx].Type = D3DDECLTYPE_FLOAT2;
176 break;
177 case D3DFVF_TEXTUREFORMAT3:
178 elements[idx].Type = D3DDECLTYPE_FLOAT3;
179 break;
180 case D3DFVF_TEXTUREFORMAT4:
181 elements[idx].Type = D3DDECLTYPE_FLOAT4;
182 break;
184 elements[idx].Usage = D3DDECLUSAGE_TEXCOORD;
185 elements[idx].UsageIndex = idx2;
186 idx++;
189 /* Now compute offsets, and initialize the rest of the fields */
190 for (idx = 0, offset = 0; idx < size-1; idx++) {
191 elements[idx].Stream = 0;
192 elements[idx].Method = D3DDECLMETHOD_DEFAULT;
193 elements[idx].Offset = offset;
194 offset += D3D_DECL_SIZE(elements[idx].Type) * D3D_DECL_TYPESIZE(elements[idx].Type);
197 *ppVertexElements = elements;
198 return D3D_OK;
201 /* IDirect3DVertexDeclaration9 IUnknown parts follow: */
202 static HRESULT WINAPI IDirect3DVertexDeclaration9Impl_QueryInterface(IDirect3DVertexDeclaration9 *iface,
203 REFIID riid, void **out)
205 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
207 if (IsEqualGUID(riid, &IID_IDirect3DVertexDeclaration9)
208 || IsEqualGUID(riid, &IID_IUnknown))
210 IDirect3DVertexDeclaration9_AddRef(iface);
211 *out = iface;
212 return S_OK;
215 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
217 *out = NULL;
218 return E_NOINTERFACE;
221 static ULONG WINAPI IDirect3DVertexDeclaration9Impl_AddRef(IDirect3DVertexDeclaration9 *iface)
223 IDirect3DVertexDeclaration9Impl *declaration = impl_from_IDirect3DVertexDeclaration9(iface);
224 ULONG ref = InterlockedIncrement(&declaration->ref);
226 TRACE("%p increasing refcount to %u.\n", iface, ref);
228 if (ref == 1)
230 IDirect3DDevice9Ex_AddRef(declaration->parentDevice);
231 wined3d_mutex_lock();
232 wined3d_vertex_declaration_incref(declaration->wineD3DVertexDeclaration);
233 wined3d_mutex_unlock();
236 return ref;
239 static ULONG WINAPI IDirect3DVertexDeclaration9Impl_Release(IDirect3DVertexDeclaration9 *iface)
241 IDirect3DVertexDeclaration9Impl *declaration = impl_from_IDirect3DVertexDeclaration9(iface);
242 ULONG ref = InterlockedDecrement(&declaration->ref);
244 TRACE("%p decreasing refcount to %u.\n", iface, ref);
246 if (!ref)
248 IDirect3DDevice9Ex *parentDevice = declaration->parentDevice;
249 wined3d_mutex_lock();
250 wined3d_vertex_declaration_decref(declaration->wineD3DVertexDeclaration);
251 wined3d_mutex_unlock();
253 /* Release the device last, as it may cause the device to be destroyed. */
254 IDirect3DDevice9Ex_Release(parentDevice);
256 return ref;
259 /* IDirect3DVertexDeclaration9 Interface follow: */
260 static HRESULT WINAPI IDirect3DVertexDeclaration9Impl_GetDevice(IDirect3DVertexDeclaration9 *iface,
261 IDirect3DDevice9 **device)
263 IDirect3DVertexDeclaration9Impl *declaration = impl_from_IDirect3DVertexDeclaration9(iface);
265 TRACE("iface %p, device %p.\n", iface, device);
267 *device = (IDirect3DDevice9 *)declaration->parentDevice;
268 IDirect3DDevice9_AddRef(*device);
270 TRACE("Returning device %p.\n", *device);
272 return D3D_OK;
275 static HRESULT WINAPI IDirect3DVertexDeclaration9Impl_GetDeclaration(IDirect3DVertexDeclaration9 *iface,
276 D3DVERTEXELEMENT9 *elements, UINT *element_count)
278 IDirect3DVertexDeclaration9Impl *declaration = impl_from_IDirect3DVertexDeclaration9(iface);
280 TRACE("iface %p, elements %p, element_count %p.\n", iface, elements, element_count);
282 *element_count = declaration->element_count;
284 /* Passing a NULL elements is used to just retrieve the number of elements */
285 if (!elements)
286 return D3D_OK;
288 TRACE("Copying %p to %p.\n", declaration->elements, elements);
289 memcpy(elements, declaration->elements, sizeof(*declaration->elements) * declaration->element_count);
291 return D3D_OK;
294 static const IDirect3DVertexDeclaration9Vtbl Direct3DVertexDeclaration9_Vtbl =
296 /* IUnknown */
297 IDirect3DVertexDeclaration9Impl_QueryInterface,
298 IDirect3DVertexDeclaration9Impl_AddRef,
299 IDirect3DVertexDeclaration9Impl_Release,
300 /* IDirect3DVertexDeclaration9 */
301 IDirect3DVertexDeclaration9Impl_GetDevice,
302 IDirect3DVertexDeclaration9Impl_GetDeclaration
305 IDirect3DVertexDeclaration9Impl *unsafe_impl_from_IDirect3DVertexDeclaration9(IDirect3DVertexDeclaration9 *iface)
307 if (!iface)
308 return NULL;
309 assert(iface->lpVtbl == &Direct3DVertexDeclaration9_Vtbl);
310 return CONTAINING_RECORD(iface, IDirect3DVertexDeclaration9Impl, IDirect3DVertexDeclaration9_iface);
313 static void STDMETHODCALLTYPE d3d9_vertexdeclaration_wined3d_object_destroyed(void *parent)
315 IDirect3DVertexDeclaration9Impl *declaration = parent;
316 HeapFree(GetProcessHeap(), 0, declaration->elements);
317 HeapFree(GetProcessHeap(), 0, declaration);
320 static const struct wined3d_parent_ops d3d9_vertexdeclaration_wined3d_parent_ops =
322 d3d9_vertexdeclaration_wined3d_object_destroyed,
325 static HRESULT convert_to_wined3d_declaration(const D3DVERTEXELEMENT9 *d3d9_elements,
326 struct wined3d_vertex_element **wined3d_elements, UINT *element_count)
328 const D3DVERTEXELEMENT9* element;
329 UINT count = 1;
330 UINT i;
332 TRACE("d3d9_elements %p, wined3d_elements %p\n", d3d9_elements, wined3d_elements);
334 element = d3d9_elements;
335 while (element++->Stream != 0xff && count++ < 128);
337 if (count == 128) return E_FAIL;
339 /* Skip the END element */
340 --count;
342 *wined3d_elements = HeapAlloc(GetProcessHeap(), 0, count * sizeof(**wined3d_elements));
343 if (!*wined3d_elements) {
344 FIXME("Memory allocation failed\n");
345 return D3DERR_OUTOFVIDEOMEMORY;
348 for (i = 0; i < count; ++i)
350 if (d3d9_elements[i].Type >= (sizeof(d3d_dtype_lookup) / sizeof(*d3d_dtype_lookup)))
352 WARN("Invalid element type %#x.\n", d3d9_elements[i].Type);
353 HeapFree(GetProcessHeap(), 0, *wined3d_elements);
354 return E_FAIL;
356 (*wined3d_elements)[i].format = d3d_dtype_lookup[d3d9_elements[i].Type].format;
357 (*wined3d_elements)[i].input_slot = d3d9_elements[i].Stream;
358 (*wined3d_elements)[i].offset = d3d9_elements[i].Offset;
359 (*wined3d_elements)[i].output_slot = ~0U;
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;
367 return D3D_OK;
370 static HRESULT vertexdeclaration_init(IDirect3DVertexDeclaration9Impl *declaration,
371 IDirect3DDevice9Impl *device, const D3DVERTEXELEMENT9 *elements)
373 struct wined3d_vertex_element *wined3d_elements;
374 UINT wined3d_element_count;
375 UINT element_count;
376 HRESULT hr;
378 hr = convert_to_wined3d_declaration(elements, &wined3d_elements, &wined3d_element_count);
379 if (FAILED(hr))
381 WARN("Failed to create wined3d vertex declaration elements, hr %#x.\n", hr);
382 return hr;
385 declaration->IDirect3DVertexDeclaration9_iface.lpVtbl = &Direct3DVertexDeclaration9_Vtbl;
386 declaration->ref = 1;
388 element_count = wined3d_element_count + 1;
389 declaration->elements = HeapAlloc(GetProcessHeap(), 0, element_count * sizeof(*declaration->elements));
390 if (!declaration->elements)
392 HeapFree(GetProcessHeap(), 0, wined3d_elements);
393 ERR("Failed to allocate vertex declaration elements memory.\n");
394 return D3DERR_OUTOFVIDEOMEMORY;
396 memcpy(declaration->elements, elements, element_count * sizeof(*elements));
397 declaration->element_count = element_count;
399 wined3d_mutex_lock();
400 hr = wined3d_vertex_declaration_create(device->wined3d_device, wined3d_elements, wined3d_element_count,
401 declaration, &d3d9_vertexdeclaration_wined3d_parent_ops, &declaration->wineD3DVertexDeclaration);
402 wined3d_mutex_unlock();
403 HeapFree(GetProcessHeap(), 0, wined3d_elements);
404 if (FAILED(hr))
406 HeapFree(GetProcessHeap(), 0, declaration->elements);
407 WARN("Failed to create wined3d vertex declaration, hr %#x.\n", hr);
408 return hr;
411 declaration->parentDevice = &device->IDirect3DDevice9Ex_iface;
412 IDirect3DDevice9Ex_AddRef(declaration->parentDevice);
414 return D3D_OK;
417 HRESULT d3d9_vertex_declaration_create(IDirect3DDevice9Impl *device,
418 const D3DVERTEXELEMENT9 *elements, IDirect3DVertexDeclaration9Impl **declaration)
420 IDirect3DVertexDeclaration9Impl *object;
421 HRESULT hr;
423 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
424 if (!object)
426 ERR("Failed to allocate vertex declaration memory.\n");
427 return E_OUTOFMEMORY;
430 hr = vertexdeclaration_init(object, device, elements);
431 if (FAILED(hr))
433 WARN("Failed to initialize vertex declaration, hr %#x.\n", hr);
434 HeapFree(GetProcessHeap(), 0, object);
435 return hr;
438 TRACE("Created vertex declaration %p.\n", object);
439 *declaration = object;
441 return D3D_OK;