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
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
;
36 FIXME("Multiple input signatures.\n");
37 shader_free_signature(is
);
39 return shader_parse_signature(tag
, data
, data_size
, is
);
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
;
50 memset(&is
, 0, sizeof(is
));
51 if (FAILED(hr
= parse_dxbc(shader_byte_code
, shader_byte_code_length
, isgn_handler
, &is
)))
53 ERR("Failed to parse input signature.\n");
57 if (!(*wined3d_elements
= heap_calloc(element_count
, sizeof(**wined3d_elements
))))
59 ERR("Failed to allocate wined3d vertex element array memory.\n");
60 shader_free_signature(&is
);
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 struct wined3d_shader_signature_element
*element
;
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
;
80 if ((element
= shader_find_signature_element(&is
, f
->SemanticName
, f
->SemanticIndex
, 0)))
81 e
->output_slot
= element
->register_idx
;
83 WARN("Unused input element %u.\n", i
);
86 shader_free_signature(&is
);
91 /* ID3D11InputLayout methods */
93 static inline struct d3d_input_layout
*impl_from_ID3D11InputLayout(ID3D11InputLayout
*iface
)
95 return CONTAINING_RECORD(iface
, struct d3d_input_layout
, ID3D11InputLayout_iface
);
98 static HRESULT STDMETHODCALLTYPE
d3d11_input_layout_QueryInterface(ID3D11InputLayout
*iface
,
99 REFIID riid
, void **object
)
101 struct d3d_input_layout
*layout
= impl_from_ID3D11InputLayout(iface
);
103 TRACE("iface %p, riid %s, object %p.\n", iface
, debugstr_guid(riid
), object
);
105 if (IsEqualGUID(riid
, &IID_ID3D11InputLayout
)
106 || IsEqualGUID(riid
, &IID_ID3D11DeviceChild
)
107 || IsEqualGUID(riid
, &IID_IUnknown
))
109 ID3D11InputLayout_AddRef(iface
);
114 if (IsEqualGUID(riid
, &IID_ID3D10InputLayout
)
115 || IsEqualGUID(riid
, &IID_ID3D10DeviceChild
))
117 ID3D10InputLayout_AddRef(&layout
->ID3D10InputLayout_iface
);
118 *object
= &layout
->ID3D10InputLayout_iface
;
122 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid
));
125 return E_NOINTERFACE
;
128 static ULONG STDMETHODCALLTYPE
d3d11_input_layout_AddRef(ID3D11InputLayout
*iface
)
130 struct d3d_input_layout
*layout
= impl_from_ID3D11InputLayout(iface
);
131 ULONG refcount
= InterlockedIncrement(&layout
->refcount
);
133 TRACE("%p increasing refcount to %u.\n", layout
, refcount
);
137 ID3D11Device2_AddRef(layout
->device
);
138 wined3d_mutex_lock();
139 wined3d_vertex_declaration_incref(layout
->wined3d_decl
);
140 wined3d_mutex_unlock();
146 static ULONG STDMETHODCALLTYPE
d3d11_input_layout_Release(ID3D11InputLayout
*iface
)
148 struct d3d_input_layout
*layout
= impl_from_ID3D11InputLayout(iface
);
149 ULONG refcount
= InterlockedDecrement(&layout
->refcount
);
151 TRACE("%p decreasing refcount to %u.\n", layout
, refcount
);
155 ID3D11Device2
*device
= layout
->device
;
157 wined3d_mutex_lock();
158 wined3d_vertex_declaration_decref(layout
->wined3d_decl
);
159 wined3d_mutex_unlock();
161 ID3D11Device2_Release(device
);
167 static void STDMETHODCALLTYPE
d3d11_input_layout_GetDevice(ID3D11InputLayout
*iface
,
168 ID3D11Device
**device
)
170 struct d3d_input_layout
*layout
= impl_from_ID3D11InputLayout(iface
);
172 TRACE("iface %p, device %p.\n", iface
, device
);
174 ID3D11Device_AddRef(*device
= (ID3D11Device
*)layout
->device
);
177 static HRESULT STDMETHODCALLTYPE
d3d11_input_layout_GetPrivateData(ID3D11InputLayout
*iface
,
178 REFGUID guid
, UINT
*data_size
, void *data
)
180 struct d3d_input_layout
*layout
= impl_from_ID3D11InputLayout(iface
);
182 TRACE("iface %p, guid %s, data_size %p, data %p.\n", iface
, debugstr_guid(guid
), data_size
, data
);
184 return d3d_get_private_data(&layout
->private_store
, guid
, data_size
, data
);
187 static HRESULT STDMETHODCALLTYPE
d3d11_input_layout_SetPrivateData(ID3D11InputLayout
*iface
,
188 REFGUID guid
, UINT data_size
, const void *data
)
190 struct d3d_input_layout
*layout
= impl_from_ID3D11InputLayout(iface
);
192 TRACE("iface %p, guid %s, data_size %u, data %p.\n", iface
, debugstr_guid(guid
), data_size
, data
);
194 return d3d_set_private_data(&layout
->private_store
, guid
, data_size
, data
);
197 static HRESULT STDMETHODCALLTYPE
d3d11_input_layout_SetPrivateDataInterface(ID3D11InputLayout
*iface
,
198 REFGUID guid
, const IUnknown
*data
)
200 struct d3d_input_layout
*layout
= impl_from_ID3D11InputLayout(iface
);
202 TRACE("iface %p, guid %s, data %p.\n", iface
, debugstr_guid(guid
), data
);
204 return d3d_set_private_data_interface(&layout
->private_store
, guid
, data
);
207 static const struct ID3D11InputLayoutVtbl d3d11_input_layout_vtbl
=
209 /* IUnknown methods */
210 d3d11_input_layout_QueryInterface
,
211 d3d11_input_layout_AddRef
,
212 d3d11_input_layout_Release
,
213 /* ID3D11DeviceChild methods */
214 d3d11_input_layout_GetDevice
,
215 d3d11_input_layout_GetPrivateData
,
216 d3d11_input_layout_SetPrivateData
,
217 d3d11_input_layout_SetPrivateDataInterface
,
220 /* ID3D10InputLayout methods */
222 static inline struct d3d_input_layout
*impl_from_ID3D10InputLayout(ID3D10InputLayout
*iface
)
224 return CONTAINING_RECORD(iface
, struct d3d_input_layout
, ID3D10InputLayout_iface
);
227 /* IUnknown methods */
229 static HRESULT STDMETHODCALLTYPE
d3d10_input_layout_QueryInterface(ID3D10InputLayout
*iface
,
230 REFIID riid
, void **object
)
232 struct d3d_input_layout
*layout
= impl_from_ID3D10InputLayout(iface
);
234 TRACE("iface %p, riid %s, object %p.\n", iface
, debugstr_guid(riid
), object
);
236 return d3d11_input_layout_QueryInterface(&layout
->ID3D11InputLayout_iface
, riid
, object
);
239 static ULONG STDMETHODCALLTYPE
d3d10_input_layout_AddRef(ID3D10InputLayout
*iface
)
241 struct d3d_input_layout
*layout
= impl_from_ID3D10InputLayout(iface
);
243 TRACE("iface %p.\n", iface
);
245 return d3d11_input_layout_AddRef(&layout
->ID3D11InputLayout_iface
);
248 static ULONG STDMETHODCALLTYPE
d3d10_input_layout_Release(ID3D10InputLayout
*iface
)
250 struct d3d_input_layout
*layout
= impl_from_ID3D10InputLayout(iface
);
252 TRACE("iface %p.\n", iface
);
254 return d3d11_input_layout_Release(&layout
->ID3D11InputLayout_iface
);
257 /* ID3D10DeviceChild methods */
259 static void STDMETHODCALLTYPE
d3d10_input_layout_GetDevice(ID3D10InputLayout
*iface
, ID3D10Device
**device
)
261 struct d3d_input_layout
*layout
= impl_from_ID3D10InputLayout(iface
);
263 TRACE("iface %p, device %p.\n", iface
, device
);
265 ID3D11Device2_QueryInterface(layout
->device
, &IID_ID3D10Device
, (void **)device
);
268 static HRESULT STDMETHODCALLTYPE
d3d10_input_layout_GetPrivateData(ID3D10InputLayout
*iface
,
269 REFGUID guid
, UINT
*data_size
, void *data
)
271 struct d3d_input_layout
*layout
= impl_from_ID3D10InputLayout(iface
);
273 TRACE("iface %p, guid %s, data_size %p, data %p.\n",
274 iface
, debugstr_guid(guid
), data_size
, data
);
276 return d3d_get_private_data(&layout
->private_store
, guid
, data_size
, data
);
279 static HRESULT STDMETHODCALLTYPE
d3d10_input_layout_SetPrivateData(ID3D10InputLayout
*iface
,
280 REFGUID guid
, UINT data_size
, const void *data
)
282 struct d3d_input_layout
*layout
= impl_from_ID3D10InputLayout(iface
);
284 TRACE("iface %p, guid %s, data_size %u, data %p.\n",
285 iface
, debugstr_guid(guid
), data_size
, data
);
287 return d3d_set_private_data(&layout
->private_store
, guid
, data_size
, data
);
290 static HRESULT STDMETHODCALLTYPE
d3d10_input_layout_SetPrivateDataInterface(ID3D10InputLayout
*iface
,
291 REFGUID guid
, const IUnknown
*data
)
293 struct d3d_input_layout
*layout
= impl_from_ID3D10InputLayout(iface
);
295 TRACE("iface %p, guid %s, data %p.\n", iface
, debugstr_guid(guid
), data
);
297 return d3d_set_private_data_interface(&layout
->private_store
, guid
, data
);
300 static const struct ID3D10InputLayoutVtbl d3d10_input_layout_vtbl
=
302 /* IUnknown methods */
303 d3d10_input_layout_QueryInterface
,
304 d3d10_input_layout_AddRef
,
305 d3d10_input_layout_Release
,
306 /* ID3D10DeviceChild methods */
307 d3d10_input_layout_GetDevice
,
308 d3d10_input_layout_GetPrivateData
,
309 d3d10_input_layout_SetPrivateData
,
310 d3d10_input_layout_SetPrivateDataInterface
,
313 static void STDMETHODCALLTYPE
d3d_input_layout_wined3d_object_destroyed(void *parent
)
315 struct d3d_input_layout
*layout
= parent
;
317 wined3d_private_store_cleanup(&layout
->private_store
);
321 static const struct wined3d_parent_ops d3d_input_layout_wined3d_parent_ops
=
323 d3d_input_layout_wined3d_object_destroyed
,
326 static HRESULT
d3d_input_layout_init(struct d3d_input_layout
*layout
, struct d3d_device
*device
,
327 const D3D11_INPUT_ELEMENT_DESC
*element_descs
, UINT element_count
,
328 const void *shader_byte_code
, SIZE_T shader_byte_code_length
)
330 struct wined3d_vertex_element
*wined3d_elements
;
333 layout
->ID3D11InputLayout_iface
.lpVtbl
= &d3d11_input_layout_vtbl
;
334 layout
->ID3D10InputLayout_iface
.lpVtbl
= &d3d10_input_layout_vtbl
;
335 layout
->refcount
= 1;
336 wined3d_mutex_lock();
337 wined3d_private_store_init(&layout
->private_store
);
339 if (FAILED(hr
= d3d11_input_layout_to_wined3d_declaration(element_descs
, element_count
,
340 shader_byte_code
, shader_byte_code_length
, &wined3d_elements
)))
342 WARN("Failed to create wined3d vertex declaration elements, hr %#x.\n", hr
);
343 wined3d_private_store_cleanup(&layout
->private_store
);
344 wined3d_mutex_unlock();
348 hr
= wined3d_vertex_declaration_create(device
->wined3d_device
, wined3d_elements
, element_count
,
349 layout
, &d3d_input_layout_wined3d_parent_ops
, &layout
->wined3d_decl
);
350 heap_free(wined3d_elements
);
353 WARN("Failed to create wined3d vertex declaration, hr %#x.\n", hr
);
354 wined3d_private_store_cleanup(&layout
->private_store
);
355 wined3d_mutex_unlock();
358 wined3d_mutex_unlock();
360 ID3D11Device2_AddRef(layout
->device
= &device
->ID3D11Device2_iface
);
365 HRESULT
d3d_input_layout_create(struct d3d_device
*device
,
366 const D3D11_INPUT_ELEMENT_DESC
*element_descs
, UINT element_count
,
367 const void *shader_byte_code
, SIZE_T shader_byte_code_length
,
368 struct d3d_input_layout
**layout
)
370 struct d3d_input_layout
*object
;
373 if (!(object
= heap_alloc_zero(sizeof(*object
))))
374 return E_OUTOFMEMORY
;
376 if (FAILED(hr
= d3d_input_layout_init(object
, device
, element_descs
, element_count
,
377 shader_byte_code
, shader_byte_code_length
)))
379 WARN("Failed to initialize input layout, hr %#x.\n", hr
);
384 TRACE("Created input layout %p.\n", object
);
390 struct d3d_input_layout
*unsafe_impl_from_ID3D11InputLayout(ID3D11InputLayout
*iface
)
394 assert(iface
->lpVtbl
== &d3d11_input_layout_vtbl
);
396 return impl_from_ID3D11InputLayout(iface
);
399 struct d3d_input_layout
*unsafe_impl_from_ID3D10InputLayout(ID3D10InputLayout
*iface
)
403 assert(iface
->lpVtbl
== &d3d10_input_layout_vtbl
);
405 return impl_from_ID3D10InputLayout(iface
);