wined3d: Swizzle D3DCOLOR attributes in the GLSL FFP replacement when necessary.
[wine.git] / dlls / d3d11 / inputlayout.c
blob69ef985201eb4874e0cf9f6ef9f9048b0d417ea3
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 "config.h"
21 #include "wine/port.h"
23 #include "d3d11_private.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(d3d11);
27 static HRESULT isgn_handler(const char *data, DWORD data_size, DWORD tag, void *ctx)
29 struct wined3d_shader_signature *is = ctx;
31 switch(tag)
33 case TAG_ISGN:
34 return shader_parse_signature(data, data_size, is);
36 default:
37 FIXME("Unhandled chunk %s.\n", debugstr_an((const char *)&tag, 4));
38 return S_OK;
42 static HRESULT d3d11_input_layout_to_wined3d_declaration(const D3D11_INPUT_ELEMENT_DESC *element_descs,
43 UINT element_count, const void *shader_byte_code, SIZE_T shader_byte_code_length,
44 struct wined3d_vertex_element **wined3d_elements)
46 struct wined3d_shader_signature is;
47 HRESULT hr;
48 UINT i;
50 hr = parse_dxbc(shader_byte_code, shader_byte_code_length, isgn_handler, &is);
51 if (FAILED(hr))
53 ERR("Failed to parse input signature.\n");
54 return E_FAIL;
57 if (!(*wined3d_elements = d3d11_calloc(element_count, sizeof(**wined3d_elements))))
59 ERR("Failed to allocate wined3d vertex element array memory.\n");
60 HeapFree(GetProcessHeap(), 0, is.elements);
61 return E_OUTOFMEMORY;
64 for (i = 0; i < element_count; ++i)
66 struct wined3d_vertex_element *e = &(*wined3d_elements)[i];
67 const D3D11_INPUT_ELEMENT_DESC *f = &element_descs[i];
68 UINT j;
70 e->format = wined3dformat_from_dxgi_format(f->Format);
71 e->input_slot = f->InputSlot;
72 e->offset = f->AlignedByteOffset;
73 e->output_slot = WINED3D_OUTPUT_SLOT_UNUSED;
74 e->input_slot_class = f->InputSlotClass;
75 e->instance_data_step_rate = f->InstanceDataStepRate;
76 e->method = WINED3D_DECL_METHOD_DEFAULT;
77 e->usage = 0;
78 e->usage_idx = 0;
80 for (j = 0; j < is.element_count; ++j)
82 if (!strcmp(element_descs[i].SemanticName, is.elements[j].semantic_name)
83 && element_descs[i].SemanticIndex == is.elements[j].semantic_idx)
85 e->output_slot = is.elements[j].register_idx;
86 break;
91 shader_free_signature(&is);
93 return S_OK;
96 /* ID3D11InputLayout methods */
98 static inline struct d3d_input_layout *impl_from_ID3D11InputLayout(ID3D11InputLayout *iface)
100 return CONTAINING_RECORD(iface, struct d3d_input_layout, ID3D11InputLayout_iface);
103 static HRESULT STDMETHODCALLTYPE d3d11_input_layout_QueryInterface(ID3D11InputLayout *iface,
104 REFIID riid, void **object)
106 struct d3d_input_layout *layout = impl_from_ID3D11InputLayout(iface);
108 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
110 if (IsEqualGUID(riid, &IID_ID3D11InputLayout)
111 || IsEqualGUID(riid, &IID_ID3D11DeviceChild)
112 || IsEqualGUID(riid, &IID_IUnknown))
114 ID3D11InputLayout_AddRef(iface);
115 *object = iface;
116 return S_OK;
119 if (IsEqualGUID(riid, &IID_ID3D10InputLayout)
120 || IsEqualGUID(riid, &IID_ID3D10DeviceChild))
122 ID3D10InputLayout_AddRef(&layout->ID3D10InputLayout_iface);
123 *object = &layout->ID3D10InputLayout_iface;
124 return S_OK;
127 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
129 *object = NULL;
130 return E_NOINTERFACE;
133 static ULONG STDMETHODCALLTYPE d3d11_input_layout_AddRef(ID3D11InputLayout *iface)
135 struct d3d_input_layout *layout = impl_from_ID3D11InputLayout(iface);
136 ULONG refcount = InterlockedIncrement(&layout->refcount);
138 TRACE("%p increasing refcount to %u.\n", layout, refcount);
140 if (refcount == 1)
142 wined3d_mutex_lock();
143 wined3d_vertex_declaration_incref(layout->wined3d_decl);
144 wined3d_mutex_unlock();
147 return refcount;
150 static ULONG STDMETHODCALLTYPE d3d11_input_layout_Release(ID3D11InputLayout *iface)
152 struct d3d_input_layout *layout = impl_from_ID3D11InputLayout(iface);
153 ULONG refcount = InterlockedDecrement(&layout->refcount);
155 TRACE("%p decreasing refcount to %u.\n", layout, refcount);
157 if (!refcount)
159 wined3d_mutex_lock();
160 wined3d_vertex_declaration_decref(layout->wined3d_decl);
161 wined3d_mutex_unlock();
164 return refcount;
167 static void STDMETHODCALLTYPE d3d11_input_layout_GetDevice(ID3D11InputLayout *iface,
168 ID3D11Device **device)
170 FIXME("iface %p, device %p stub!\n", iface, device);
173 static HRESULT STDMETHODCALLTYPE d3d11_input_layout_GetPrivateData(ID3D11InputLayout *iface,
174 REFGUID guid, UINT *data_size, void *data)
176 struct d3d_input_layout *layout = impl_from_ID3D11InputLayout(iface);
178 TRACE("iface %p, guid %s, data_size %p, data %p.\n", iface, debugstr_guid(guid), data_size, data);
180 return d3d_get_private_data(&layout->private_store, guid, data_size, data);
183 static HRESULT STDMETHODCALLTYPE d3d11_input_layout_SetPrivateData(ID3D11InputLayout *iface,
184 REFGUID guid, UINT data_size, const void *data)
186 struct d3d_input_layout *layout = impl_from_ID3D11InputLayout(iface);
188 TRACE("iface %p, guid %s, data_size %u, data %p.\n", iface, debugstr_guid(guid), data_size, data);
190 return d3d_set_private_data(&layout->private_store, guid, data_size, data);
193 static HRESULT STDMETHODCALLTYPE d3d11_input_layout_SetPrivateDataInterface(ID3D11InputLayout *iface,
194 REFGUID guid, const IUnknown *data)
196 struct d3d_input_layout *layout = impl_from_ID3D11InputLayout(iface);
198 TRACE("iface %p, guid %s, data %p.\n", iface, debugstr_guid(guid), data);
200 return d3d_set_private_data_interface(&layout->private_store, guid, data);
203 static const struct ID3D11InputLayoutVtbl d3d11_input_layout_vtbl =
205 /* IUnknown methods */
206 d3d11_input_layout_QueryInterface,
207 d3d11_input_layout_AddRef,
208 d3d11_input_layout_Release,
209 /* ID3D11DeviceChild methods */
210 d3d11_input_layout_GetDevice,
211 d3d11_input_layout_GetPrivateData,
212 d3d11_input_layout_SetPrivateData,
213 d3d11_input_layout_SetPrivateDataInterface,
216 /* ID3D10InputLayout methods */
218 static inline struct d3d_input_layout *impl_from_ID3D10InputLayout(ID3D10InputLayout *iface)
220 return CONTAINING_RECORD(iface, struct d3d_input_layout, ID3D10InputLayout_iface);
223 /* IUnknown methods */
225 static HRESULT STDMETHODCALLTYPE d3d10_input_layout_QueryInterface(ID3D10InputLayout *iface,
226 REFIID riid, void **object)
228 struct d3d_input_layout *layout = impl_from_ID3D10InputLayout(iface);
230 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
232 return d3d11_input_layout_QueryInterface(&layout->ID3D11InputLayout_iface, riid, object);
235 static ULONG STDMETHODCALLTYPE d3d10_input_layout_AddRef(ID3D10InputLayout *iface)
237 struct d3d_input_layout *layout = impl_from_ID3D10InputLayout(iface);
239 TRACE("iface %p.\n", iface);
241 return d3d11_input_layout_AddRef(&layout->ID3D11InputLayout_iface);
244 static ULONG STDMETHODCALLTYPE d3d10_input_layout_Release(ID3D10InputLayout *iface)
246 struct d3d_input_layout *layout = impl_from_ID3D10InputLayout(iface);
248 TRACE("iface %p.\n", iface);
250 return d3d11_input_layout_Release(&layout->ID3D11InputLayout_iface);
253 /* ID3D10DeviceChild methods */
255 static void STDMETHODCALLTYPE d3d10_input_layout_GetDevice(ID3D10InputLayout *iface, ID3D10Device **device)
257 FIXME("iface %p, device %p stub!\n", iface, device);
260 static HRESULT STDMETHODCALLTYPE d3d10_input_layout_GetPrivateData(ID3D10InputLayout *iface,
261 REFGUID guid, UINT *data_size, void *data)
263 struct d3d_input_layout *layout = impl_from_ID3D10InputLayout(iface);
265 TRACE("iface %p, guid %s, data_size %p, data %p.\n",
266 iface, debugstr_guid(guid), data_size, data);
268 return d3d_get_private_data(&layout->private_store, guid, data_size, data);
271 static HRESULT STDMETHODCALLTYPE d3d10_input_layout_SetPrivateData(ID3D10InputLayout *iface,
272 REFGUID guid, UINT data_size, const void *data)
274 struct d3d_input_layout *layout = impl_from_ID3D10InputLayout(iface);
276 TRACE("iface %p, guid %s, data_size %u, data %p.\n",
277 iface, debugstr_guid(guid), data_size, data);
279 return d3d_set_private_data(&layout->private_store, guid, data_size, data);
282 static HRESULT STDMETHODCALLTYPE d3d10_input_layout_SetPrivateDataInterface(ID3D10InputLayout *iface,
283 REFGUID guid, const IUnknown *data)
285 struct d3d_input_layout *layout = impl_from_ID3D10InputLayout(iface);
287 TRACE("iface %p, guid %s, data %p.\n", iface, debugstr_guid(guid), data);
289 return d3d_set_private_data_interface(&layout->private_store, guid, data);
292 static const struct ID3D10InputLayoutVtbl d3d10_input_layout_vtbl =
294 /* IUnknown methods */
295 d3d10_input_layout_QueryInterface,
296 d3d10_input_layout_AddRef,
297 d3d10_input_layout_Release,
298 /* ID3D10DeviceChild methods */
299 d3d10_input_layout_GetDevice,
300 d3d10_input_layout_GetPrivateData,
301 d3d10_input_layout_SetPrivateData,
302 d3d10_input_layout_SetPrivateDataInterface,
305 static void STDMETHODCALLTYPE d3d_input_layout_wined3d_object_destroyed(void *parent)
307 struct d3d_input_layout *layout = parent;
309 wined3d_private_store_cleanup(&layout->private_store);
310 HeapFree(GetProcessHeap(), 0, parent);
313 static const struct wined3d_parent_ops d3d_input_layout_wined3d_parent_ops =
315 d3d_input_layout_wined3d_object_destroyed,
318 static HRESULT d3d_input_layout_init(struct d3d_input_layout *layout, struct d3d_device *device,
319 const D3D11_INPUT_ELEMENT_DESC *element_descs, UINT element_count,
320 const void *shader_byte_code, SIZE_T shader_byte_code_length)
322 struct wined3d_vertex_element *wined3d_elements;
323 HRESULT hr;
325 layout->ID3D11InputLayout_iface.lpVtbl = &d3d11_input_layout_vtbl;
326 layout->ID3D10InputLayout_iface.lpVtbl = &d3d10_input_layout_vtbl;
327 layout->refcount = 1;
328 wined3d_mutex_lock();
329 wined3d_private_store_init(&layout->private_store);
331 if (FAILED(hr = d3d11_input_layout_to_wined3d_declaration(element_descs, element_count,
332 shader_byte_code, shader_byte_code_length, &wined3d_elements)))
334 WARN("Failed to create wined3d vertex declaration elements, hr %#x.\n", hr);
335 wined3d_private_store_cleanup(&layout->private_store);
336 wined3d_mutex_unlock();
337 return hr;
340 hr = wined3d_vertex_declaration_create(device->wined3d_device, wined3d_elements, element_count,
341 layout, &d3d_input_layout_wined3d_parent_ops, &layout->wined3d_decl);
342 HeapFree(GetProcessHeap(), 0, wined3d_elements);
343 if (FAILED(hr))
345 WARN("Failed to create wined3d vertex declaration, hr %#x.\n", hr);
346 wined3d_private_store_cleanup(&layout->private_store);
347 wined3d_mutex_unlock();
348 return hr;
350 wined3d_mutex_unlock();
352 return S_OK;
355 HRESULT d3d_input_layout_create(struct d3d_device *device,
356 const D3D11_INPUT_ELEMENT_DESC *element_descs, UINT element_count,
357 const void *shader_byte_code, SIZE_T shader_byte_code_length,
358 struct d3d_input_layout **layout)
360 struct d3d_input_layout *object;
361 HRESULT hr;
363 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
364 if (!object)
365 return E_OUTOFMEMORY;
367 if (FAILED(hr = d3d_input_layout_init(object, device, element_descs, element_count,
368 shader_byte_code, shader_byte_code_length)))
370 WARN("Failed to initialize input layout, hr %#x.\n", hr);
371 HeapFree(GetProcessHeap(), 0, object);
372 return hr;
375 TRACE("Created input layout %p.\n", object);
376 *layout = object;
378 return S_OK;
381 struct d3d_input_layout *unsafe_impl_from_ID3D11InputLayout(ID3D11InputLayout *iface)
383 if (!iface)
384 return NULL;
385 assert(iface->lpVtbl == &d3d11_input_layout_vtbl);
387 return impl_from_ID3D11InputLayout(iface);
390 struct d3d_input_layout *unsafe_impl_from_ID3D10InputLayout(ID3D10InputLayout *iface)
392 if (!iface)
393 return NULL;
394 assert(iface->lpVtbl == &d3d10_input_layout_vtbl);
396 return impl_from_ID3D10InputLayout(iface);