msvcr120: Add fegetexceptflag.
[wine/zf.git] / dlls / d3d11 / inputlayout.c
blob6b59dd994b3e7752d440f0afcda5ab82c05d7627
1 /*
2 * Copyright 2009 Henri Verbeet for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "d3d11_private.h"
21 #include "winternl.h"
23 WINE_DEFAULT_DEBUG_CHANNEL(d3d11);
25 static struct wined3d_shader_signature_element *shader_find_signature_element(const struct wined3d_shader_signature *s,
26 const char *semantic_name, unsigned int semantic_idx, unsigned int stream_idx)
28 struct wined3d_shader_signature_element *e = s->elements;
29 unsigned int i;
31 for (i = 0; i < s->element_count; ++i)
33 if (!stricmp(e[i].semantic_name, semantic_name) && e[i].semantic_idx == semantic_idx
34 && e[i].stream_idx == stream_idx)
35 return &e[i];
38 return NULL;
41 static enum wined3d_input_classification wined3d_input_classification_from_d3d11(D3D11_INPUT_CLASSIFICATION slot_class)
43 return (enum wined3d_input_classification)slot_class;
46 static HRESULT d3d11_input_layout_to_wined3d_declaration(const D3D11_INPUT_ELEMENT_DESC *element_descs,
47 UINT element_count, const void *shader_byte_code, SIZE_T shader_byte_code_length,
48 struct wined3d_vertex_element **wined3d_elements)
50 struct wined3d_shader_signature is;
51 unsigned int i;
52 HRESULT hr;
54 if (FAILED(hr = wined3d_extract_shader_input_signature_from_dxbc(&is, shader_byte_code, shader_byte_code_length)))
56 ERR("Failed to extract input signature.\n");
57 return E_FAIL;
60 if (!(*wined3d_elements = heap_calloc(element_count, sizeof(**wined3d_elements))))
62 ERR("Failed to allocate wined3d vertex element array memory.\n");
63 heap_free(is.elements);
64 return E_OUTOFMEMORY;
67 for (i = 0; i < element_count; ++i)
69 struct wined3d_vertex_element *e = &(*wined3d_elements)[i];
70 const D3D11_INPUT_ELEMENT_DESC *f = &element_descs[i];
71 struct wined3d_shader_signature_element *element;
73 e->format = wined3dformat_from_dxgi_format(f->Format);
74 e->input_slot = f->InputSlot;
75 e->offset = f->AlignedByteOffset;
76 e->output_slot = WINED3D_OUTPUT_SLOT_UNUSED;
77 e->input_slot_class = wined3d_input_classification_from_d3d11(f->InputSlotClass);
78 e->instance_data_step_rate = f->InstanceDataStepRate;
79 e->method = WINED3D_DECL_METHOD_DEFAULT;
80 e->usage = 0;
81 e->usage_idx = 0;
83 if ((element = shader_find_signature_element(&is, f->SemanticName, f->SemanticIndex, 0)))
84 e->output_slot = element->register_idx;
85 else
86 WARN("Unused input element %u.\n", i);
89 heap_free(is.elements);
91 return S_OK;
94 /* ID3D11InputLayout methods */
96 static inline struct d3d_input_layout *impl_from_ID3D11InputLayout(ID3D11InputLayout *iface)
98 return CONTAINING_RECORD(iface, struct d3d_input_layout, ID3D11InputLayout_iface);
101 static HRESULT STDMETHODCALLTYPE d3d11_input_layout_QueryInterface(ID3D11InputLayout *iface,
102 REFIID riid, void **object)
104 struct d3d_input_layout *layout = impl_from_ID3D11InputLayout(iface);
106 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
108 if (IsEqualGUID(riid, &IID_ID3D11InputLayout)
109 || IsEqualGUID(riid, &IID_ID3D11DeviceChild)
110 || IsEqualGUID(riid, &IID_IUnknown))
112 ID3D11InputLayout_AddRef(iface);
113 *object = iface;
114 return S_OK;
117 if (IsEqualGUID(riid, &IID_ID3D10InputLayout)
118 || IsEqualGUID(riid, &IID_ID3D10DeviceChild))
120 ID3D10InputLayout_AddRef(&layout->ID3D10InputLayout_iface);
121 *object = &layout->ID3D10InputLayout_iface;
122 return S_OK;
125 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
127 *object = NULL;
128 return E_NOINTERFACE;
131 static ULONG STDMETHODCALLTYPE d3d11_input_layout_AddRef(ID3D11InputLayout *iface)
133 struct d3d_input_layout *layout = impl_from_ID3D11InputLayout(iface);
134 ULONG refcount = InterlockedIncrement(&layout->refcount);
136 TRACE("%p increasing refcount to %u.\n", layout, refcount);
138 if (refcount == 1)
140 ID3D11Device2_AddRef(layout->device);
141 wined3d_mutex_lock();
142 wined3d_vertex_declaration_incref(layout->wined3d_decl);
143 wined3d_mutex_unlock();
146 return refcount;
149 static ULONG STDMETHODCALLTYPE d3d11_input_layout_Release(ID3D11InputLayout *iface)
151 struct d3d_input_layout *layout = impl_from_ID3D11InputLayout(iface);
152 ULONG refcount = InterlockedDecrement(&layout->refcount);
154 TRACE("%p decreasing refcount to %u.\n", layout, refcount);
156 if (!refcount)
158 ID3D11Device2 *device = layout->device;
160 wined3d_mutex_lock();
161 wined3d_vertex_declaration_decref(layout->wined3d_decl);
162 wined3d_mutex_unlock();
164 ID3D11Device2_Release(device);
167 return refcount;
170 static void STDMETHODCALLTYPE d3d11_input_layout_GetDevice(ID3D11InputLayout *iface,
171 ID3D11Device **device)
173 struct d3d_input_layout *layout = impl_from_ID3D11InputLayout(iface);
175 TRACE("iface %p, device %p.\n", iface, device);
177 ID3D11Device_AddRef(*device = (ID3D11Device *)layout->device);
180 static HRESULT STDMETHODCALLTYPE d3d11_input_layout_GetPrivateData(ID3D11InputLayout *iface,
181 REFGUID guid, UINT *data_size, void *data)
183 struct d3d_input_layout *layout = impl_from_ID3D11InputLayout(iface);
185 TRACE("iface %p, guid %s, data_size %p, data %p.\n", iface, debugstr_guid(guid), data_size, data);
187 return d3d_get_private_data(&layout->private_store, guid, data_size, data);
190 static HRESULT STDMETHODCALLTYPE d3d11_input_layout_SetPrivateData(ID3D11InputLayout *iface,
191 REFGUID guid, UINT data_size, const void *data)
193 struct d3d_input_layout *layout = impl_from_ID3D11InputLayout(iface);
195 TRACE("iface %p, guid %s, data_size %u, data %p.\n", iface, debugstr_guid(guid), data_size, data);
197 return d3d_set_private_data(&layout->private_store, guid, data_size, data);
200 static HRESULT STDMETHODCALLTYPE d3d11_input_layout_SetPrivateDataInterface(ID3D11InputLayout *iface,
201 REFGUID guid, const IUnknown *data)
203 struct d3d_input_layout *layout = impl_from_ID3D11InputLayout(iface);
205 TRACE("iface %p, guid %s, data %p.\n", iface, debugstr_guid(guid), data);
207 return d3d_set_private_data_interface(&layout->private_store, guid, data);
210 static const struct ID3D11InputLayoutVtbl d3d11_input_layout_vtbl =
212 /* IUnknown methods */
213 d3d11_input_layout_QueryInterface,
214 d3d11_input_layout_AddRef,
215 d3d11_input_layout_Release,
216 /* ID3D11DeviceChild methods */
217 d3d11_input_layout_GetDevice,
218 d3d11_input_layout_GetPrivateData,
219 d3d11_input_layout_SetPrivateData,
220 d3d11_input_layout_SetPrivateDataInterface,
223 /* ID3D10InputLayout methods */
225 static inline struct d3d_input_layout *impl_from_ID3D10InputLayout(ID3D10InputLayout *iface)
227 return CONTAINING_RECORD(iface, struct d3d_input_layout, ID3D10InputLayout_iface);
230 /* IUnknown methods */
232 static HRESULT STDMETHODCALLTYPE d3d10_input_layout_QueryInterface(ID3D10InputLayout *iface,
233 REFIID riid, void **object)
235 struct d3d_input_layout *layout = impl_from_ID3D10InputLayout(iface);
237 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
239 return d3d11_input_layout_QueryInterface(&layout->ID3D11InputLayout_iface, riid, object);
242 static ULONG STDMETHODCALLTYPE d3d10_input_layout_AddRef(ID3D10InputLayout *iface)
244 struct d3d_input_layout *layout = impl_from_ID3D10InputLayout(iface);
246 TRACE("iface %p.\n", iface);
248 return d3d11_input_layout_AddRef(&layout->ID3D11InputLayout_iface);
251 static ULONG STDMETHODCALLTYPE d3d10_input_layout_Release(ID3D10InputLayout *iface)
253 struct d3d_input_layout *layout = impl_from_ID3D10InputLayout(iface);
255 TRACE("iface %p.\n", iface);
257 return d3d11_input_layout_Release(&layout->ID3D11InputLayout_iface);
260 /* ID3D10DeviceChild methods */
262 static void STDMETHODCALLTYPE d3d10_input_layout_GetDevice(ID3D10InputLayout *iface, ID3D10Device **device)
264 struct d3d_input_layout *layout = impl_from_ID3D10InputLayout(iface);
266 TRACE("iface %p, device %p.\n", iface, device);
268 ID3D11Device2_QueryInterface(layout->device, &IID_ID3D10Device, (void **)device);
271 static HRESULT STDMETHODCALLTYPE d3d10_input_layout_GetPrivateData(ID3D10InputLayout *iface,
272 REFGUID guid, UINT *data_size, void *data)
274 struct d3d_input_layout *layout = impl_from_ID3D10InputLayout(iface);
276 TRACE("iface %p, guid %s, data_size %p, data %p.\n",
277 iface, debugstr_guid(guid), data_size, data);
279 return d3d_get_private_data(&layout->private_store, guid, data_size, data);
282 static HRESULT STDMETHODCALLTYPE d3d10_input_layout_SetPrivateData(ID3D10InputLayout *iface,
283 REFGUID guid, UINT data_size, const void *data)
285 struct d3d_input_layout *layout = impl_from_ID3D10InputLayout(iface);
287 TRACE("iface %p, guid %s, data_size %u, data %p.\n",
288 iface, debugstr_guid(guid), data_size, data);
290 return d3d_set_private_data(&layout->private_store, guid, data_size, data);
293 static HRESULT STDMETHODCALLTYPE d3d10_input_layout_SetPrivateDataInterface(ID3D10InputLayout *iface,
294 REFGUID guid, const IUnknown *data)
296 struct d3d_input_layout *layout = impl_from_ID3D10InputLayout(iface);
298 TRACE("iface %p, guid %s, data %p.\n", iface, debugstr_guid(guid), data);
300 return d3d_set_private_data_interface(&layout->private_store, guid, data);
303 static const struct ID3D10InputLayoutVtbl d3d10_input_layout_vtbl =
305 /* IUnknown methods */
306 d3d10_input_layout_QueryInterface,
307 d3d10_input_layout_AddRef,
308 d3d10_input_layout_Release,
309 /* ID3D10DeviceChild methods */
310 d3d10_input_layout_GetDevice,
311 d3d10_input_layout_GetPrivateData,
312 d3d10_input_layout_SetPrivateData,
313 d3d10_input_layout_SetPrivateDataInterface,
316 static void STDMETHODCALLTYPE d3d_input_layout_wined3d_object_destroyed(void *parent)
318 struct d3d_input_layout *layout = parent;
320 wined3d_private_store_cleanup(&layout->private_store);
321 heap_free(parent);
324 static const struct wined3d_parent_ops d3d_input_layout_wined3d_parent_ops =
326 d3d_input_layout_wined3d_object_destroyed,
329 static HRESULT d3d_input_layout_init(struct d3d_input_layout *layout, struct d3d_device *device,
330 const D3D11_INPUT_ELEMENT_DESC *element_descs, UINT element_count,
331 const void *shader_byte_code, SIZE_T shader_byte_code_length)
333 struct wined3d_vertex_element *wined3d_elements;
334 HRESULT hr;
336 layout->ID3D11InputLayout_iface.lpVtbl = &d3d11_input_layout_vtbl;
337 layout->ID3D10InputLayout_iface.lpVtbl = &d3d10_input_layout_vtbl;
338 layout->refcount = 1;
339 wined3d_mutex_lock();
340 wined3d_private_store_init(&layout->private_store);
342 if (FAILED(hr = d3d11_input_layout_to_wined3d_declaration(element_descs, element_count,
343 shader_byte_code, shader_byte_code_length, &wined3d_elements)))
345 WARN("Failed to create wined3d vertex declaration elements, hr %#x.\n", hr);
346 wined3d_private_store_cleanup(&layout->private_store);
347 wined3d_mutex_unlock();
348 return hr;
351 hr = wined3d_vertex_declaration_create(device->wined3d_device, wined3d_elements, element_count,
352 layout, &d3d_input_layout_wined3d_parent_ops, &layout->wined3d_decl);
353 heap_free(wined3d_elements);
354 if (FAILED(hr))
356 WARN("Failed to create wined3d vertex declaration, hr %#x.\n", hr);
357 wined3d_private_store_cleanup(&layout->private_store);
358 wined3d_mutex_unlock();
359 return hr;
361 wined3d_mutex_unlock();
363 ID3D11Device2_AddRef(layout->device = &device->ID3D11Device2_iface);
365 return S_OK;
368 HRESULT d3d_input_layout_create(struct d3d_device *device,
369 const D3D11_INPUT_ELEMENT_DESC *element_descs, UINT element_count,
370 const void *shader_byte_code, SIZE_T shader_byte_code_length,
371 struct d3d_input_layout **layout)
373 struct d3d_input_layout *object;
374 HRESULT hr;
376 if (!(object = heap_alloc_zero(sizeof(*object))))
377 return E_OUTOFMEMORY;
379 if (FAILED(hr = d3d_input_layout_init(object, device, element_descs, element_count,
380 shader_byte_code, shader_byte_code_length)))
382 WARN("Failed to initialize input layout, hr %#x.\n", hr);
383 heap_free(object);
384 return hr;
387 TRACE("Created input layout %p.\n", object);
388 *layout = object;
390 return S_OK;
393 struct d3d_input_layout *unsafe_impl_from_ID3D11InputLayout(ID3D11InputLayout *iface)
395 if (!iface)
396 return NULL;
397 assert(iface->lpVtbl == &d3d11_input_layout_vtbl);
399 return impl_from_ID3D11InputLayout(iface);
402 struct d3d_input_layout *unsafe_impl_from_ID3D10InputLayout(ID3D10InputLayout *iface)
404 if (!iface)
405 return NULL;
406 assert(iface->lpVtbl == &d3d10_input_layout_vtbl);
408 return impl_from_ID3D10InputLayout(iface);