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"
22 #include <vkd3d_shader.h>
24 WINE_DEFAULT_DEBUG_CHANNEL(d3d11
);
26 static BOOL
is_vs_sysval_semantic(const struct vkd3d_shader_signature_element
*e
)
28 return !stricmp(e
->semantic_name
, "sv_instanceid") || !stricmp(e
->semantic_name
, "sv_vertexid");
31 static unsigned int find_input_element(const D3D11_INPUT_ELEMENT_DESC
*element_descs
, unsigned int element_count
,
32 const struct vkd3d_shader_signature_element
*ise
)
34 const D3D11_INPUT_ELEMENT_DESC
*f
;
37 if (ise
->stream_index
)
40 for (i
= 0; i
< element_count
; ++i
)
42 f
= &element_descs
[i
];
43 if (!stricmp(ise
->semantic_name
, f
->SemanticName
) && ise
->semantic_index
== f
->SemanticIndex
)
49 static enum wined3d_input_classification
wined3d_input_classification_from_d3d11(D3D11_INPUT_CLASSIFICATION slot_class
)
51 return (enum wined3d_input_classification
)slot_class
;
54 static HRESULT
d3d11_input_layout_to_wined3d_declaration(const D3D11_INPUT_ELEMENT_DESC
*element_descs
,
55 UINT element_count
, const void *shader_byte_code
, SIZE_T shader_byte_code_length
,
56 struct wined3d_vertex_element
**wined3d_elements
)
58 const struct vkd3d_shader_code dxbc
= {shader_byte_code
, shader_byte_code_length
};
59 struct vkd3d_shader_signature_element
*ise
;
60 const D3D11_INPUT_ELEMENT_DESC
*f
;
61 struct vkd3d_shader_signature is
;
62 unsigned int i
, index
;
65 if ((ret
= vkd3d_shader_parse_input_signature(&dxbc
, &is
, NULL
)) < 0)
67 ERR("Failed to extract input signature, ret %d.\n", ret
);
71 if (!(*wined3d_elements
= calloc(element_count
, sizeof(**wined3d_elements
))))
73 ERR("Failed to allocate wined3d vertex element array memory.\n");
74 vkd3d_shader_free_shader_signature(&is
);
78 for (i
= 0; i
< element_count
; ++i
)
80 struct wined3d_vertex_element
*e
= &(*wined3d_elements
)[i
];
82 f
= &element_descs
[i
];
84 e
->format
= wined3dformat_from_dxgi_format(f
->Format
);
85 e
->input_slot
= f
->InputSlot
;
86 e
->offset
= f
->AlignedByteOffset
;
87 e
->output_slot
= WINED3D_OUTPUT_SLOT_UNUSED
;
88 e
->input_slot_class
= wined3d_input_classification_from_d3d11(f
->InputSlotClass
);
89 e
->instance_data_step_rate
= f
->InstanceDataStepRate
;
90 e
->method
= WINED3D_DECL_METHOD_DEFAULT
;
97 for (i
= 0; i
< is
.element_count
; ++i
)
99 if ((index
= find_input_element(element_descs
, element_count
, &ise
[i
])) < element_count
)
101 (*wined3d_elements
)[index
].output_slot
= ise
[i
].register_index
;
103 else if (!is_vs_sysval_semantic(&ise
[i
]))
105 WARN("Input element %s%u not found in shader signature.\n", ise
[i
].semantic_name
, ise
[i
].semantic_index
);
106 free(*wined3d_elements
);
107 vkd3d_shader_free_shader_signature(&is
);
112 vkd3d_shader_free_shader_signature(&is
);
117 /* ID3D11InputLayout methods */
119 static inline struct d3d_input_layout
*impl_from_ID3D11InputLayout(ID3D11InputLayout
*iface
)
121 return CONTAINING_RECORD(iface
, struct d3d_input_layout
, ID3D11InputLayout_iface
);
124 static HRESULT STDMETHODCALLTYPE
d3d11_input_layout_QueryInterface(ID3D11InputLayout
*iface
,
125 REFIID riid
, void **object
)
127 struct d3d_input_layout
*layout
= impl_from_ID3D11InputLayout(iface
);
129 TRACE("iface %p, riid %s, object %p.\n", iface
, debugstr_guid(riid
), object
);
131 if (IsEqualGUID(riid
, &IID_ID3D11InputLayout
)
132 || IsEqualGUID(riid
, &IID_ID3D11DeviceChild
)
133 || IsEqualGUID(riid
, &IID_IUnknown
))
135 ID3D11InputLayout_AddRef(iface
);
140 if (IsEqualGUID(riid
, &IID_ID3D10InputLayout
)
141 || IsEqualGUID(riid
, &IID_ID3D10DeviceChild
))
143 ID3D10InputLayout_AddRef(&layout
->ID3D10InputLayout_iface
);
144 *object
= &layout
->ID3D10InputLayout_iface
;
148 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid
));
151 return E_NOINTERFACE
;
154 static ULONG STDMETHODCALLTYPE
d3d11_input_layout_AddRef(ID3D11InputLayout
*iface
)
156 struct d3d_input_layout
*layout
= impl_from_ID3D11InputLayout(iface
);
157 ULONG refcount
= InterlockedIncrement(&layout
->refcount
);
159 TRACE("%p increasing refcount to %lu.\n", layout
, refcount
);
163 ID3D11Device2_AddRef(layout
->device
);
164 wined3d_vertex_declaration_incref(layout
->wined3d_decl
);
170 static ULONG STDMETHODCALLTYPE
d3d11_input_layout_Release(ID3D11InputLayout
*iface
)
172 struct d3d_input_layout
*layout
= impl_from_ID3D11InputLayout(iface
);
173 ULONG refcount
= InterlockedDecrement(&layout
->refcount
);
175 TRACE("%p decreasing refcount to %lu.\n", layout
, refcount
);
179 ID3D11Device2
*device
= layout
->device
;
180 wined3d_vertex_declaration_decref(layout
->wined3d_decl
);
181 ID3D11Device2_Release(device
);
187 static void STDMETHODCALLTYPE
d3d11_input_layout_GetDevice(ID3D11InputLayout
*iface
,
188 ID3D11Device
**device
)
190 struct d3d_input_layout
*layout
= impl_from_ID3D11InputLayout(iface
);
192 TRACE("iface %p, device %p.\n", iface
, device
);
194 ID3D11Device_AddRef(*device
= (ID3D11Device
*)layout
->device
);
197 static HRESULT STDMETHODCALLTYPE
d3d11_input_layout_GetPrivateData(ID3D11InputLayout
*iface
,
198 REFGUID guid
, UINT
*data_size
, void *data
)
200 struct d3d_input_layout
*layout
= impl_from_ID3D11InputLayout(iface
);
202 TRACE("iface %p, guid %s, data_size %p, data %p.\n", iface
, debugstr_guid(guid
), data_size
, data
);
204 return d3d_get_private_data(&layout
->private_store
, guid
, data_size
, data
);
207 static HRESULT STDMETHODCALLTYPE
d3d11_input_layout_SetPrivateData(ID3D11InputLayout
*iface
,
208 REFGUID guid
, UINT data_size
, const void *data
)
210 struct d3d_input_layout
*layout
= impl_from_ID3D11InputLayout(iface
);
212 TRACE("iface %p, guid %s, data_size %u, data %p.\n", iface
, debugstr_guid(guid
), data_size
, data
);
214 return d3d_set_private_data(&layout
->private_store
, guid
, data_size
, data
);
217 static HRESULT STDMETHODCALLTYPE
d3d11_input_layout_SetPrivateDataInterface(ID3D11InputLayout
*iface
,
218 REFGUID guid
, const IUnknown
*data
)
220 struct d3d_input_layout
*layout
= impl_from_ID3D11InputLayout(iface
);
222 TRACE("iface %p, guid %s, data %p.\n", iface
, debugstr_guid(guid
), data
);
224 return d3d_set_private_data_interface(&layout
->private_store
, guid
, data
);
227 static const struct ID3D11InputLayoutVtbl d3d11_input_layout_vtbl
=
229 /* IUnknown methods */
230 d3d11_input_layout_QueryInterface
,
231 d3d11_input_layout_AddRef
,
232 d3d11_input_layout_Release
,
233 /* ID3D11DeviceChild methods */
234 d3d11_input_layout_GetDevice
,
235 d3d11_input_layout_GetPrivateData
,
236 d3d11_input_layout_SetPrivateData
,
237 d3d11_input_layout_SetPrivateDataInterface
,
240 /* ID3D10InputLayout methods */
242 static inline struct d3d_input_layout
*impl_from_ID3D10InputLayout(ID3D10InputLayout
*iface
)
244 return CONTAINING_RECORD(iface
, struct d3d_input_layout
, ID3D10InputLayout_iface
);
247 /* IUnknown methods */
249 static HRESULT STDMETHODCALLTYPE
d3d10_input_layout_QueryInterface(ID3D10InputLayout
*iface
,
250 REFIID riid
, void **object
)
252 struct d3d_input_layout
*layout
= impl_from_ID3D10InputLayout(iface
);
254 TRACE("iface %p, riid %s, object %p.\n", iface
, debugstr_guid(riid
), object
);
256 return d3d11_input_layout_QueryInterface(&layout
->ID3D11InputLayout_iface
, riid
, object
);
259 static ULONG STDMETHODCALLTYPE
d3d10_input_layout_AddRef(ID3D10InputLayout
*iface
)
261 struct d3d_input_layout
*layout
= impl_from_ID3D10InputLayout(iface
);
263 TRACE("iface %p.\n", iface
);
265 return d3d11_input_layout_AddRef(&layout
->ID3D11InputLayout_iface
);
268 static ULONG STDMETHODCALLTYPE
d3d10_input_layout_Release(ID3D10InputLayout
*iface
)
270 struct d3d_input_layout
*layout
= impl_from_ID3D10InputLayout(iface
);
272 TRACE("iface %p.\n", iface
);
274 return d3d11_input_layout_Release(&layout
->ID3D11InputLayout_iface
);
277 /* ID3D10DeviceChild methods */
279 static void STDMETHODCALLTYPE
d3d10_input_layout_GetDevice(ID3D10InputLayout
*iface
, ID3D10Device
**device
)
281 struct d3d_input_layout
*layout
= impl_from_ID3D10InputLayout(iface
);
283 TRACE("iface %p, device %p.\n", iface
, device
);
285 ID3D11Device2_QueryInterface(layout
->device
, &IID_ID3D10Device
, (void **)device
);
288 static HRESULT STDMETHODCALLTYPE
d3d10_input_layout_GetPrivateData(ID3D10InputLayout
*iface
,
289 REFGUID guid
, UINT
*data_size
, void *data
)
291 struct d3d_input_layout
*layout
= impl_from_ID3D10InputLayout(iface
);
293 TRACE("iface %p, guid %s, data_size %p, data %p.\n",
294 iface
, debugstr_guid(guid
), data_size
, data
);
296 return d3d_get_private_data(&layout
->private_store
, guid
, data_size
, data
);
299 static HRESULT STDMETHODCALLTYPE
d3d10_input_layout_SetPrivateData(ID3D10InputLayout
*iface
,
300 REFGUID guid
, UINT data_size
, const void *data
)
302 struct d3d_input_layout
*layout
= impl_from_ID3D10InputLayout(iface
);
304 TRACE("iface %p, guid %s, data_size %u, data %p.\n",
305 iface
, debugstr_guid(guid
), data_size
, data
);
307 return d3d_set_private_data(&layout
->private_store
, guid
, data_size
, data
);
310 static HRESULT STDMETHODCALLTYPE
d3d10_input_layout_SetPrivateDataInterface(ID3D10InputLayout
*iface
,
311 REFGUID guid
, const IUnknown
*data
)
313 struct d3d_input_layout
*layout
= impl_from_ID3D10InputLayout(iface
);
315 TRACE("iface %p, guid %s, data %p.\n", iface
, debugstr_guid(guid
), data
);
317 return d3d_set_private_data_interface(&layout
->private_store
, guid
, data
);
320 static const struct ID3D10InputLayoutVtbl d3d10_input_layout_vtbl
=
322 /* IUnknown methods */
323 d3d10_input_layout_QueryInterface
,
324 d3d10_input_layout_AddRef
,
325 d3d10_input_layout_Release
,
326 /* ID3D10DeviceChild methods */
327 d3d10_input_layout_GetDevice
,
328 d3d10_input_layout_GetPrivateData
,
329 d3d10_input_layout_SetPrivateData
,
330 d3d10_input_layout_SetPrivateDataInterface
,
333 static void STDMETHODCALLTYPE
d3d_input_layout_wined3d_object_destroyed(void *parent
)
335 struct d3d_input_layout
*layout
= parent
;
337 wined3d_private_store_cleanup(&layout
->private_store
);
341 static const struct wined3d_parent_ops d3d_input_layout_wined3d_parent_ops
=
343 d3d_input_layout_wined3d_object_destroyed
,
346 static HRESULT
d3d_input_layout_init(struct d3d_input_layout
*layout
, struct d3d_device
*device
,
347 const D3D11_INPUT_ELEMENT_DESC
*element_descs
, UINT element_count
,
348 const void *shader_byte_code
, SIZE_T shader_byte_code_length
)
350 struct wined3d_vertex_element
*wined3d_elements
;
353 layout
->ID3D11InputLayout_iface
.lpVtbl
= &d3d11_input_layout_vtbl
;
354 layout
->ID3D10InputLayout_iface
.lpVtbl
= &d3d10_input_layout_vtbl
;
355 layout
->refcount
= 1;
356 wined3d_mutex_lock();
357 wined3d_private_store_init(&layout
->private_store
);
359 if (FAILED(hr
= d3d11_input_layout_to_wined3d_declaration(element_descs
, element_count
,
360 shader_byte_code
, shader_byte_code_length
, &wined3d_elements
)))
362 WARN("Failed to create wined3d vertex declaration elements, hr %#lx.\n", hr
);
363 wined3d_private_store_cleanup(&layout
->private_store
);
364 wined3d_mutex_unlock();
368 hr
= wined3d_vertex_declaration_create(device
->wined3d_device
, wined3d_elements
, element_count
,
369 layout
, &d3d_input_layout_wined3d_parent_ops
, &layout
->wined3d_decl
);
370 free(wined3d_elements
);
373 WARN("Failed to create wined3d vertex declaration, hr %#lx.\n", hr
);
374 wined3d_private_store_cleanup(&layout
->private_store
);
375 wined3d_mutex_unlock();
378 wined3d_mutex_unlock();
380 ID3D11Device2_AddRef(layout
->device
= &device
->ID3D11Device2_iface
);
385 HRESULT
d3d_input_layout_create(struct d3d_device
*device
,
386 const D3D11_INPUT_ELEMENT_DESC
*element_descs
, UINT element_count
,
387 const void *shader_byte_code
, SIZE_T shader_byte_code_length
,
388 struct d3d_input_layout
**layout
)
390 struct d3d_input_layout
*object
;
393 if (!element_descs
) return E_INVALIDARG
;
395 if (!(object
= calloc(1, sizeof(*object
))))
396 return E_OUTOFMEMORY
;
398 if (FAILED(hr
= d3d_input_layout_init(object
, device
, element_descs
, element_count
,
399 shader_byte_code
, shader_byte_code_length
)))
401 WARN("Failed to initialise input layout, hr %#lx.\n", hr
);
406 TRACE("Created input layout %p.\n", object
);
412 struct d3d_input_layout
*unsafe_impl_from_ID3D11InputLayout(ID3D11InputLayout
*iface
)
416 assert(iface
->lpVtbl
== &d3d11_input_layout_vtbl
);
418 return impl_from_ID3D11InputLayout(iface
);
421 struct d3d_input_layout
*unsafe_impl_from_ID3D10InputLayout(ID3D10InputLayout
*iface
)
425 assert(iface
->lpVtbl
== &d3d10_input_layout_vtbl
);
427 return impl_from_ID3D10InputLayout(iface
);