wined3d: Pass a wined3d_device_context to wined3d_cs_emit_blt_sub_resource().
[wine/zf.git] / dlls / d3d9 / vertexdeclaration.c
blob60701a9a9105a3ed740f853dff68d941118e8e01
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 "d3d9_private.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
26 static const struct
28 enum wined3d_format_id format;
29 unsigned int component_count;
30 unsigned int component_size;
32 d3d_dtype_lookup[] =
34 /* D3DDECLTYPE_FLOAT1 */ {WINED3DFMT_R32_FLOAT, 1, sizeof(float)},
35 /* D3DDECLTYPE_FLOAT2 */ {WINED3DFMT_R32G32_FLOAT, 2, sizeof(float)},
36 /* D3DDECLTYPE_FLOAT3 */ {WINED3DFMT_R32G32B32_FLOAT, 3, sizeof(float)},
37 /* D3DDECLTYPE_FLOAT4 */ {WINED3DFMT_R32G32B32A32_FLOAT, 4, sizeof(float)},
38 /* D3DDECLTYPE_D3DCOLOR */ {WINED3DFMT_B8G8R8A8_UNORM, 4, sizeof(BYTE)},
39 /* D3DDECLTYPE_UBYTE4 */ {WINED3DFMT_R8G8B8A8_UINT, 4, sizeof(BYTE)},
40 /* D3DDECLTYPE_SHORT2 */ {WINED3DFMT_R16G16_SINT, 2, sizeof(short int)},
41 /* D3DDECLTYPE_SHORT4 */ {WINED3DFMT_R16G16B16A16_SINT, 4, sizeof(short int)},
42 /* D3DDECLTYPE_UBYTE4N */ {WINED3DFMT_R8G8B8A8_UNORM, 4, sizeof(BYTE)},
43 /* D3DDECLTYPE_SHORT2N */ {WINED3DFMT_R16G16_SNORM, 2, sizeof(short int)},
44 /* D3DDECLTYPE_SHORT4N */ {WINED3DFMT_R16G16B16A16_SNORM, 4, sizeof(short int)},
45 /* D3DDECLTYPE_USHORT2N */ {WINED3DFMT_R16G16_UNORM, 2, sizeof(short int)},
46 /* D3DDECLTYPE_USHORT4N */ {WINED3DFMT_R16G16B16A16_UNORM, 4, sizeof(short int)},
47 /* D3DDECLTYPE_UDEC3 */ {WINED3DFMT_R10G10B10X2_UINT, 3, sizeof(short int)},
48 /* D3DDECLTYPE_DEC3N */ {WINED3DFMT_R10G10B10X2_SNORM, 3, sizeof(short int)},
49 /* D3DDECLTYPE_FLOAT16_2 */ {WINED3DFMT_R16G16_FLOAT, 2, sizeof(short int)},
50 /* D3DDECLTYPE_FLOAT16_4 */ {WINED3DFMT_R16G16B16A16_FLOAT, 4, sizeof(short int)}
53 static inline struct d3d9_vertex_declaration *impl_from_IDirect3DVertexDeclaration9(IDirect3DVertexDeclaration9 *iface)
55 return CONTAINING_RECORD(iface, struct d3d9_vertex_declaration, IDirect3DVertexDeclaration9_iface);
58 HRESULT vdecl_convert_fvf(
59 DWORD fvf,
60 D3DVERTEXELEMENT9** ppVertexElements) {
62 unsigned int idx, idx2;
63 unsigned int offset;
64 BOOL has_pos = (fvf & D3DFVF_POSITION_MASK) != 0;
65 BOOL has_blend = (fvf & D3DFVF_XYZB5) > D3DFVF_XYZRHW;
66 BOOL has_blend_idx = has_blend &&
67 (((fvf & D3DFVF_XYZB5) == D3DFVF_XYZB5) ||
68 (fvf & D3DFVF_LASTBETA_D3DCOLOR) ||
69 (fvf & D3DFVF_LASTBETA_UBYTE4));
70 BOOL has_normal = (fvf & D3DFVF_NORMAL) != 0;
71 BOOL has_psize = (fvf & D3DFVF_PSIZE) != 0;
73 BOOL has_diffuse = (fvf & D3DFVF_DIFFUSE) != 0;
74 BOOL has_specular = (fvf & D3DFVF_SPECULAR) !=0;
76 DWORD num_textures = (fvf & D3DFVF_TEXCOUNT_MASK) >> D3DFVF_TEXCOUNT_SHIFT;
77 DWORD texcoords = (fvf & 0xFFFF0000) >> 16;
79 D3DVERTEXELEMENT9 end_element = D3DDECL_END();
80 D3DVERTEXELEMENT9 *elements = NULL;
82 unsigned int size;
83 DWORD num_blends = 1 + (((fvf & D3DFVF_XYZB5) - D3DFVF_XYZB1) >> 1);
84 if (has_blend_idx) num_blends--;
86 /* Compute declaration size */
87 size = has_pos + (has_blend && num_blends > 0) + has_blend_idx + has_normal +
88 has_psize + has_diffuse + has_specular + num_textures + 1;
90 /* convert the declaration */
91 if (!(elements = heap_alloc(size * sizeof(*elements))))
92 return D3DERR_OUTOFVIDEOMEMORY;
94 elements[size-1] = end_element;
95 idx = 0;
96 if (has_pos) {
97 if (!has_blend && (fvf & D3DFVF_XYZRHW)) {
98 elements[idx].Type = D3DDECLTYPE_FLOAT4;
99 elements[idx].Usage = D3DDECLUSAGE_POSITIONT;
101 else if (!has_blend && (fvf & D3DFVF_XYZW) == D3DFVF_XYZW) {
102 elements[idx].Type = D3DDECLTYPE_FLOAT4;
103 elements[idx].Usage = D3DDECLUSAGE_POSITION;
105 else {
106 elements[idx].Type = D3DDECLTYPE_FLOAT3;
107 elements[idx].Usage = D3DDECLUSAGE_POSITION;
109 elements[idx].UsageIndex = 0;
110 idx++;
112 if (has_blend && (num_blends > 0)) {
113 if (((fvf & D3DFVF_XYZB5) == D3DFVF_XYZB2) && (fvf & D3DFVF_LASTBETA_D3DCOLOR))
114 elements[idx].Type = D3DDECLTYPE_D3DCOLOR;
115 else {
116 switch(num_blends) {
117 case 1: elements[idx].Type = D3DDECLTYPE_FLOAT1; break;
118 case 2: elements[idx].Type = D3DDECLTYPE_FLOAT2; break;
119 case 3: elements[idx].Type = D3DDECLTYPE_FLOAT3; break;
120 case 4: elements[idx].Type = D3DDECLTYPE_FLOAT4; break;
121 default:
122 ERR("Unexpected amount of blend values: %u\n", num_blends);
125 elements[idx].Usage = D3DDECLUSAGE_BLENDWEIGHT;
126 elements[idx].UsageIndex = 0;
127 idx++;
129 if (has_blend_idx) {
130 if (fvf & D3DFVF_LASTBETA_UBYTE4 ||
131 (((fvf & D3DFVF_XYZB5) == D3DFVF_XYZB2) && (fvf & D3DFVF_LASTBETA_D3DCOLOR)))
132 elements[idx].Type = D3DDECLTYPE_UBYTE4;
133 else if (fvf & D3DFVF_LASTBETA_D3DCOLOR)
134 elements[idx].Type = D3DDECLTYPE_D3DCOLOR;
135 else
136 elements[idx].Type = D3DDECLTYPE_FLOAT1;
137 elements[idx].Usage = D3DDECLUSAGE_BLENDINDICES;
138 elements[idx].UsageIndex = 0;
139 idx++;
141 if (has_normal) {
142 elements[idx].Type = D3DDECLTYPE_FLOAT3;
143 elements[idx].Usage = D3DDECLUSAGE_NORMAL;
144 elements[idx].UsageIndex = 0;
145 idx++;
147 if (has_psize) {
148 elements[idx].Type = D3DDECLTYPE_FLOAT1;
149 elements[idx].Usage = D3DDECLUSAGE_PSIZE;
150 elements[idx].UsageIndex = 0;
151 idx++;
153 if (has_diffuse) {
154 elements[idx].Type = D3DDECLTYPE_D3DCOLOR;
155 elements[idx].Usage = D3DDECLUSAGE_COLOR;
156 elements[idx].UsageIndex = 0;
157 idx++;
159 if (has_specular) {
160 elements[idx].Type = D3DDECLTYPE_D3DCOLOR;
161 elements[idx].Usage = D3DDECLUSAGE_COLOR;
162 elements[idx].UsageIndex = 1;
163 idx++;
165 for (idx2 = 0; idx2 < num_textures; idx2++) {
166 unsigned int numcoords = (texcoords >> (idx2*2)) & 0x03;
167 switch (numcoords) {
168 case D3DFVF_TEXTUREFORMAT1:
169 elements[idx].Type = D3DDECLTYPE_FLOAT1;
170 break;
171 case D3DFVF_TEXTUREFORMAT2:
172 elements[idx].Type = D3DDECLTYPE_FLOAT2;
173 break;
174 case D3DFVF_TEXTUREFORMAT3:
175 elements[idx].Type = D3DDECLTYPE_FLOAT3;
176 break;
177 case D3DFVF_TEXTUREFORMAT4:
178 elements[idx].Type = D3DDECLTYPE_FLOAT4;
179 break;
181 elements[idx].Usage = D3DDECLUSAGE_TEXCOORD;
182 elements[idx].UsageIndex = idx2;
183 idx++;
186 /* Now compute offsets, and initialize the rest of the fields */
187 for (idx = 0, offset = 0; idx < size-1; idx++) {
188 elements[idx].Stream = 0;
189 elements[idx].Method = D3DDECLMETHOD_DEFAULT;
190 elements[idx].Offset = offset;
191 offset += d3d_dtype_lookup[elements[idx].Type].component_count
192 * d3d_dtype_lookup[elements[idx].Type].component_size;
195 *ppVertexElements = elements;
196 return D3D_OK;
199 static HRESULT WINAPI d3d9_vertex_declaration_QueryInterface(IDirect3DVertexDeclaration9 *iface,
200 REFIID riid, void **out)
202 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
204 if (IsEqualGUID(riid, &IID_IDirect3DVertexDeclaration9)
205 || IsEqualGUID(riid, &IID_IUnknown))
207 IDirect3DVertexDeclaration9_AddRef(iface);
208 *out = iface;
209 return S_OK;
212 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
214 *out = NULL;
215 return E_NOINTERFACE;
218 static ULONG WINAPI d3d9_vertex_declaration_AddRef(IDirect3DVertexDeclaration9 *iface)
220 struct d3d9_vertex_declaration *declaration = impl_from_IDirect3DVertexDeclaration9(iface);
221 ULONG refcount = InterlockedIncrement(&declaration->refcount);
223 TRACE("%p increasing refcount to %u.\n", iface, refcount);
225 if (refcount == 1)
227 IDirect3DDevice9Ex_AddRef(declaration->parent_device);
228 wined3d_mutex_lock();
229 wined3d_vertex_declaration_incref(declaration->wined3d_declaration);
230 wined3d_mutex_unlock();
233 return refcount;
236 static ULONG WINAPI d3d9_vertex_declaration_Release(IDirect3DVertexDeclaration9 *iface)
238 struct d3d9_vertex_declaration *declaration = impl_from_IDirect3DVertexDeclaration9(iface);
239 ULONG refcount = InterlockedDecrement(&declaration->refcount);
241 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
243 if (!refcount)
245 IDirect3DDevice9Ex *parent_device = declaration->parent_device;
246 wined3d_mutex_lock();
247 wined3d_vertex_declaration_decref(declaration->wined3d_declaration);
248 wined3d_mutex_unlock();
250 /* Release the device last, as it may cause the device to be destroyed. */
251 IDirect3DDevice9Ex_Release(parent_device);
254 return refcount;
257 static HRESULT WINAPI d3d9_vertex_declaration_GetDevice(IDirect3DVertexDeclaration9 *iface, IDirect3DDevice9 **device)
259 struct d3d9_vertex_declaration *declaration = impl_from_IDirect3DVertexDeclaration9(iface);
261 TRACE("iface %p, device %p.\n", iface, device);
263 *device = (IDirect3DDevice9 *)declaration->parent_device;
264 IDirect3DDevice9_AddRef(*device);
266 TRACE("Returning device %p.\n", *device);
268 return D3D_OK;
271 static HRESULT WINAPI d3d9_vertex_declaration_GetDeclaration(IDirect3DVertexDeclaration9 *iface,
272 D3DVERTEXELEMENT9 *elements, UINT *element_count)
274 struct d3d9_vertex_declaration *declaration = impl_from_IDirect3DVertexDeclaration9(iface);
276 TRACE("iface %p, elements %p, element_count %p.\n", iface, elements, element_count);
278 *element_count = declaration->element_count;
280 /* Passing a NULL elements is used to just retrieve the number of elements */
281 if (!elements)
282 return D3D_OK;
284 TRACE("Copying %p to %p.\n", declaration->elements, elements);
285 memcpy(elements, declaration->elements, sizeof(*declaration->elements) * declaration->element_count);
287 return D3D_OK;
290 static const struct IDirect3DVertexDeclaration9Vtbl d3d9_vertex_declaration_vtbl =
292 /* IUnknown */
293 d3d9_vertex_declaration_QueryInterface,
294 d3d9_vertex_declaration_AddRef,
295 d3d9_vertex_declaration_Release,
296 /* IDirect3DVertexDeclaration9 */
297 d3d9_vertex_declaration_GetDevice,
298 d3d9_vertex_declaration_GetDeclaration,
301 struct d3d9_vertex_declaration *unsafe_impl_from_IDirect3DVertexDeclaration9(IDirect3DVertexDeclaration9 *iface)
303 if (!iface)
304 return NULL;
305 assert(iface->lpVtbl == &d3d9_vertex_declaration_vtbl);
306 return CONTAINING_RECORD(iface, struct d3d9_vertex_declaration, IDirect3DVertexDeclaration9_iface);
309 static void STDMETHODCALLTYPE d3d9_vertexdeclaration_wined3d_object_destroyed(void *parent)
311 struct d3d9_vertex_declaration *declaration = parent;
312 heap_free(declaration->elements);
313 heap_free(declaration);
316 static const struct wined3d_parent_ops d3d9_vertexdeclaration_wined3d_parent_ops =
318 d3d9_vertexdeclaration_wined3d_object_destroyed,
321 static HRESULT convert_to_wined3d_declaration(const D3DVERTEXELEMENT9 *d3d9_elements,
322 struct wined3d_vertex_element **wined3d_elements, UINT *element_count, DWORD *stream_map)
324 const D3DVERTEXELEMENT9* element;
325 UINT count = 1;
326 UINT i;
328 TRACE("d3d9_elements %p, wined3d_elements %p, element_count %p\n", d3d9_elements, wined3d_elements, element_count);
330 *stream_map = 0;
332 element = d3d9_elements;
333 while (element++->Stream != 0xff && count++ < 128);
335 if (count == 128) return E_FAIL;
337 /* Skip the END element */
338 --count;
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);
352 return E_FAIL;
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;
363 *stream_map |= 1u << d3d9_elements[i].Stream;
366 *element_count = count;
368 return D3D_OK;
371 static HRESULT vertexdeclaration_init(struct d3d9_vertex_declaration *declaration,
372 struct d3d9_device *device, const D3DVERTEXELEMENT9 *elements)
374 struct wined3d_vertex_element *wined3d_elements;
375 UINT wined3d_element_count;
376 UINT element_count;
377 HRESULT hr;
379 hr = convert_to_wined3d_declaration(elements, &wined3d_elements, &wined3d_element_count,
380 &declaration->stream_map);
381 if (FAILED(hr))
383 WARN("Failed to create wined3d vertex declaration elements, hr %#x.\n", hr);
384 return hr;
387 declaration->IDirect3DVertexDeclaration9_iface.lpVtbl = &d3d9_vertex_declaration_vtbl;
388 declaration->refcount = 1;
390 element_count = wined3d_element_count + 1;
391 if (!(declaration->elements = heap_alloc(element_count * sizeof(*declaration->elements))))
393 heap_free(wined3d_elements);
394 ERR("Failed to allocate vertex declaration elements memory.\n");
395 return D3DERR_OUTOFVIDEOMEMORY;
397 memcpy(declaration->elements, elements, element_count * sizeof(*elements));
398 declaration->element_count = element_count;
400 wined3d_mutex_lock();
401 hr = wined3d_vertex_declaration_create(device->wined3d_device, wined3d_elements, wined3d_element_count,
402 declaration, &d3d9_vertexdeclaration_wined3d_parent_ops, &declaration->wined3d_declaration);
403 wined3d_mutex_unlock();
404 heap_free(wined3d_elements);
405 if (FAILED(hr))
407 heap_free(declaration->elements);
408 WARN("Failed to create wined3d vertex declaration, hr %#x.\n", hr);
409 if (hr == E_INVALIDARG)
410 hr = E_FAIL;
411 return hr;
414 declaration->parent_device = &device->IDirect3DDevice9Ex_iface;
415 IDirect3DDevice9Ex_AddRef(declaration->parent_device);
417 return D3D_OK;
420 HRESULT d3d9_vertex_declaration_create(struct d3d9_device *device,
421 const D3DVERTEXELEMENT9 *elements, struct d3d9_vertex_declaration **declaration)
423 struct d3d9_vertex_declaration *object;
424 HRESULT hr;
426 if (!(object = heap_alloc_zero(sizeof(*object))))
427 return E_OUTOFMEMORY;
429 hr = vertexdeclaration_init(object, device, elements);
430 if (FAILED(hr))
432 WARN("Failed to initialize vertex declaration, hr %#x.\n", hr);
433 heap_free(object);
434 return hr;
437 TRACE("Created vertex declaration %p.\n", object);
438 *declaration = object;
440 return D3D_OK;