msado15: Implement Dispatch functions in Fields.
[wine.git] / dlls / d3d11 / buffer.c
blobb33be57d439e3479faeb3a5be8799e1d40d31721
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"
22 WINE_DEFAULT_DEBUG_CHANNEL(d3d11);
24 /* ID3D11Buffer methods */
26 static inline struct d3d_buffer *impl_from_ID3D11Buffer(ID3D11Buffer *iface)
28 return CONTAINING_RECORD(iface, struct d3d_buffer, ID3D11Buffer_iface);
31 static HRESULT STDMETHODCALLTYPE d3d11_buffer_QueryInterface(ID3D11Buffer *iface, REFIID riid, void **out)
33 struct d3d_buffer *buffer = impl_from_ID3D11Buffer(iface);
35 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
37 if (IsEqualGUID(riid, &IID_ID3D11Buffer)
38 || IsEqualGUID(riid, &IID_ID3D11Resource)
39 || IsEqualGUID(riid, &IID_ID3D11DeviceChild)
40 || IsEqualGUID(riid, &IID_IUnknown))
42 ID3D11Buffer_AddRef(iface);
43 *out = iface;
44 return S_OK;
47 if (IsEqualGUID(riid, &IID_ID3D10Buffer)
48 || IsEqualGUID(riid, &IID_ID3D10Resource)
49 || IsEqualGUID(riid, &IID_ID3D10DeviceChild))
51 ID3D10Buffer_AddRef(&buffer->ID3D10Buffer_iface);
52 *out = &buffer->ID3D10Buffer_iface;
53 return S_OK;
56 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
58 *out = NULL;
59 return E_NOINTERFACE;
62 static ULONG STDMETHODCALLTYPE d3d11_buffer_AddRef(ID3D11Buffer *iface)
64 struct d3d_buffer *buffer = impl_from_ID3D11Buffer(iface);
65 ULONG refcount = InterlockedIncrement(&buffer->refcount);
67 TRACE("%p increasing refcount to %u.\n", buffer, refcount);
69 if (refcount == 1)
71 ID3D11Device2_AddRef(buffer->device);
72 wined3d_mutex_lock();
73 wined3d_buffer_incref(buffer->wined3d_buffer);
74 wined3d_mutex_unlock();
77 return refcount;
80 static ULONG STDMETHODCALLTYPE d3d11_buffer_Release(ID3D11Buffer *iface)
82 struct d3d_buffer *buffer = impl_from_ID3D11Buffer(iface);
83 ULONG refcount = InterlockedDecrement(&buffer->refcount);
85 TRACE("%p decreasing refcount to %u.\n", buffer, refcount);
87 if (!refcount)
89 ID3D11Device2 *device = buffer->device;
91 wined3d_mutex_lock();
92 wined3d_buffer_decref(buffer->wined3d_buffer);
93 wined3d_mutex_unlock();
94 /* Release the device last, it may cause the wined3d device to be
95 * destroyed. */
96 ID3D11Device2_Release(device);
99 return refcount;
102 static void STDMETHODCALLTYPE d3d11_buffer_GetDevice(ID3D11Buffer *iface, ID3D11Device **device)
104 struct d3d_buffer *buffer = impl_from_ID3D11Buffer(iface);
106 TRACE("iface %p, device %p.\n", iface, device);
108 *device = (ID3D11Device *)buffer->device;
109 ID3D11Device_AddRef(*device);
112 static HRESULT STDMETHODCALLTYPE d3d11_buffer_GetPrivateData(ID3D11Buffer *iface,
113 REFGUID guid, UINT *data_size, void *data)
115 struct d3d_buffer *buffer = impl_from_ID3D11Buffer(iface);
117 TRACE("iface %p, guid %s, data_size %p, data %p.\n", iface, debugstr_guid(guid), data_size, data);
119 return d3d_get_private_data(&buffer->private_store, guid, data_size, data);
122 static HRESULT STDMETHODCALLTYPE d3d11_buffer_SetPrivateData(ID3D11Buffer *iface,
123 REFGUID guid, UINT data_size, const void *data)
125 struct d3d_buffer *buffer = impl_from_ID3D11Buffer(iface);
127 TRACE("iface %p, guid %s, data_size %u, data %p.\n", iface, debugstr_guid(guid), data_size, data);
129 return d3d_set_private_data(&buffer->private_store, guid, data_size, data);
132 static HRESULT STDMETHODCALLTYPE d3d11_buffer_SetPrivateDataInterface(ID3D11Buffer *iface,
133 REFGUID guid, const IUnknown *data)
135 struct d3d_buffer *buffer = impl_from_ID3D11Buffer(iface);
137 TRACE("iface %p, guid %s, data %p.\n", iface, debugstr_guid(guid), data);
139 return d3d_set_private_data_interface(&buffer->private_store, guid, data);
142 static void STDMETHODCALLTYPE d3d11_buffer_GetType(ID3D11Buffer *iface,
143 D3D11_RESOURCE_DIMENSION *resource_dimension)
145 TRACE("iface %p, resource_dimension %p.\n", iface, resource_dimension);
147 *resource_dimension = D3D11_RESOURCE_DIMENSION_BUFFER;
150 static void STDMETHODCALLTYPE d3d11_buffer_SetEvictionPriority(ID3D11Buffer *iface, UINT eviction_priority)
152 FIXME("iface %p, eviction_priority %#x stub!\n", iface, eviction_priority);
155 static UINT STDMETHODCALLTYPE d3d11_buffer_GetEvictionPriority(ID3D11Buffer *iface)
157 FIXME("iface %p stub!\n", iface);
159 return 0;
162 static void STDMETHODCALLTYPE d3d11_buffer_GetDesc(ID3D11Buffer *iface, D3D11_BUFFER_DESC *desc)
164 struct d3d_buffer *buffer = impl_from_ID3D11Buffer(iface);
166 TRACE("iface %p, desc %p.\n", iface, desc);
168 *desc = buffer->desc;
171 static const struct ID3D11BufferVtbl d3d11_buffer_vtbl =
173 /* IUnknown methods */
174 d3d11_buffer_QueryInterface,
175 d3d11_buffer_AddRef,
176 d3d11_buffer_Release,
177 /* ID3D11DeviceChild methods */
178 d3d11_buffer_GetDevice,
179 d3d11_buffer_GetPrivateData,
180 d3d11_buffer_SetPrivateData,
181 d3d11_buffer_SetPrivateDataInterface,
182 /* ID3D11Resource methods */
183 d3d11_buffer_GetType,
184 d3d11_buffer_SetEvictionPriority,
185 d3d11_buffer_GetEvictionPriority,
186 /* ID3D11Buffer methods */
187 d3d11_buffer_GetDesc,
190 struct d3d_buffer *unsafe_impl_from_ID3D11Buffer(ID3D11Buffer *iface)
192 if (!iface)
193 return NULL;
194 assert(iface->lpVtbl == &d3d11_buffer_vtbl);
195 return CONTAINING_RECORD(iface, struct d3d_buffer, ID3D11Buffer_iface);
198 /* ID3D10Buffer methods */
200 static inline struct d3d_buffer *impl_from_ID3D10Buffer(ID3D10Buffer *iface)
202 return CONTAINING_RECORD(iface, struct d3d_buffer, ID3D10Buffer_iface);
205 /* IUnknown methods */
207 static HRESULT STDMETHODCALLTYPE d3d10_buffer_QueryInterface(ID3D10Buffer *iface, REFIID riid, void **out)
209 struct d3d_buffer *buffer = impl_from_ID3D10Buffer(iface);
211 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
213 return d3d11_buffer_QueryInterface(&buffer->ID3D11Buffer_iface, riid, out);
216 static ULONG STDMETHODCALLTYPE d3d10_buffer_AddRef(ID3D10Buffer *iface)
218 struct d3d_buffer *buffer = impl_from_ID3D10Buffer(iface);
220 TRACE("iface %p.\n", iface);
222 return d3d11_buffer_AddRef(&buffer->ID3D11Buffer_iface);
225 static ULONG STDMETHODCALLTYPE d3d10_buffer_Release(ID3D10Buffer *iface)
227 struct d3d_buffer *buffer = impl_from_ID3D10Buffer(iface);
229 TRACE("iface %p.\n", iface);
231 return d3d11_buffer_Release(&buffer->ID3D11Buffer_iface);
234 /* ID3D10DeviceChild methods */
236 static void STDMETHODCALLTYPE d3d10_buffer_GetDevice(ID3D10Buffer *iface, ID3D10Device **device)
238 struct d3d_buffer *buffer = impl_from_ID3D10Buffer(iface);
240 TRACE("iface %p, device %p.\n", iface, device);
242 ID3D11Device2_QueryInterface(buffer->device, &IID_ID3D10Device, (void **)device);
245 static HRESULT STDMETHODCALLTYPE d3d10_buffer_GetPrivateData(ID3D10Buffer *iface,
246 REFGUID guid, UINT *data_size, void *data)
248 struct d3d_buffer *buffer = impl_from_ID3D10Buffer(iface);
250 TRACE("iface %p, guid %s, data_size %p, data %p.\n",
251 iface, debugstr_guid(guid), data_size, data);
253 return d3d_get_private_data(&buffer->private_store, guid, data_size, data);
256 static HRESULT STDMETHODCALLTYPE d3d10_buffer_SetPrivateData(ID3D10Buffer *iface,
257 REFGUID guid, UINT data_size, const void *data)
259 struct d3d_buffer *buffer = impl_from_ID3D10Buffer(iface);
261 TRACE("iface %p, guid %s, data_size %u, data %p.\n",
262 iface, debugstr_guid(guid), data_size, data);
264 return d3d_set_private_data(&buffer->private_store, guid, data_size, data);
267 static HRESULT STDMETHODCALLTYPE d3d10_buffer_SetPrivateDataInterface(ID3D10Buffer *iface,
268 REFGUID guid, const IUnknown *data)
270 struct d3d_buffer *buffer = impl_from_ID3D10Buffer(iface);
272 TRACE("iface %p, guid %s, data %p.\n", iface, debugstr_guid(guid), data);
274 return d3d_set_private_data_interface(&buffer->private_store, guid, data);
277 /* ID3D10Resource methods */
279 static void STDMETHODCALLTYPE d3d10_buffer_GetType(ID3D10Buffer *iface, D3D10_RESOURCE_DIMENSION *resource_dimension)
281 TRACE("iface %p, resource_dimension %p\n", iface, resource_dimension);
283 *resource_dimension = D3D10_RESOURCE_DIMENSION_BUFFER;
286 static void STDMETHODCALLTYPE d3d10_buffer_SetEvictionPriority(ID3D10Buffer *iface, UINT eviction_priority)
288 FIXME("iface %p, eviction_priority %u stub!\n", iface, eviction_priority);
291 static UINT STDMETHODCALLTYPE d3d10_buffer_GetEvictionPriority(ID3D10Buffer *iface)
293 FIXME("iface %p stub!\n", iface);
295 return 0;
298 /* ID3D10Buffer methods */
300 static HRESULT STDMETHODCALLTYPE d3d10_buffer_Map(ID3D10Buffer *iface, D3D10_MAP map_type, UINT map_flags, void **data)
302 struct d3d_buffer *buffer = impl_from_ID3D10Buffer(iface);
303 struct wined3d_map_desc wined3d_map_desc;
304 HRESULT hr;
306 TRACE("iface %p, map_type %u, map_flags %#x, data %p.\n", iface, map_type, map_flags, data);
308 if (map_flags)
309 FIXME("Ignoring map_flags %#x.\n", map_flags);
311 wined3d_mutex_lock();
312 hr = wined3d_resource_map(wined3d_buffer_get_resource(buffer->wined3d_buffer), 0,
313 &wined3d_map_desc, NULL, wined3d_map_flags_from_d3d10_map_type(map_type));
314 *data = wined3d_map_desc.data;
315 wined3d_mutex_unlock();
317 return hr;
320 static void STDMETHODCALLTYPE d3d10_buffer_Unmap(ID3D10Buffer *iface)
322 struct d3d_buffer *buffer = impl_from_ID3D10Buffer(iface);
324 TRACE("iface %p.\n", iface);
326 wined3d_mutex_lock();
327 wined3d_resource_unmap(wined3d_buffer_get_resource(buffer->wined3d_buffer), 0);
328 wined3d_mutex_unlock();
331 static void STDMETHODCALLTYPE d3d10_buffer_GetDesc(ID3D10Buffer *iface, D3D10_BUFFER_DESC *desc)
333 struct d3d_buffer *buffer = impl_from_ID3D10Buffer(iface);
334 const D3D11_BUFFER_DESC *d3d11_desc = &buffer->desc;
336 TRACE("iface %p, desc %p.\n", iface, desc);
338 desc->ByteWidth = d3d11_desc->ByteWidth;
339 desc->Usage = d3d10_usage_from_d3d11_usage(d3d11_desc->Usage);
340 desc->BindFlags = d3d10_bind_flags_from_d3d11_bind_flags(d3d11_desc->BindFlags);
341 desc->CPUAccessFlags = d3d10_cpu_access_flags_from_d3d11_cpu_access_flags(d3d11_desc->CPUAccessFlags);
342 desc->MiscFlags = d3d10_resource_misc_flags_from_d3d11_resource_misc_flags(d3d11_desc->MiscFlags);
345 static const struct ID3D10BufferVtbl d3d10_buffer_vtbl =
347 /* IUnknown methods */
348 d3d10_buffer_QueryInterface,
349 d3d10_buffer_AddRef,
350 d3d10_buffer_Release,
351 /* ID3D10DeviceChild methods */
352 d3d10_buffer_GetDevice,
353 d3d10_buffer_GetPrivateData,
354 d3d10_buffer_SetPrivateData,
355 d3d10_buffer_SetPrivateDataInterface,
356 /* ID3D10Resource methods */
357 d3d10_buffer_GetType,
358 d3d10_buffer_SetEvictionPriority,
359 d3d10_buffer_GetEvictionPriority,
360 /* ID3D10Buffer methods */
361 d3d10_buffer_Map,
362 d3d10_buffer_Unmap,
363 d3d10_buffer_GetDesc,
366 struct d3d_buffer *unsafe_impl_from_ID3D10Buffer(ID3D10Buffer *iface)
368 if (!iface)
369 return NULL;
370 assert(iface->lpVtbl == &d3d10_buffer_vtbl);
371 return CONTAINING_RECORD(iface, struct d3d_buffer, ID3D10Buffer_iface);
374 static void STDMETHODCALLTYPE d3d_buffer_wined3d_object_released(void *parent)
376 struct d3d_buffer *buffer = parent;
378 wined3d_private_store_cleanup(&buffer->private_store);
379 heap_free(parent);
382 static const struct wined3d_parent_ops d3d_buffer_wined3d_parent_ops =
384 d3d_buffer_wined3d_object_released,
387 static BOOL validate_buffer_desc(D3D11_BUFFER_DESC *desc, D3D_FEATURE_LEVEL feature_level)
389 if (!validate_d3d11_resource_access_flags(D3D11_RESOURCE_DIMENSION_BUFFER,
390 desc->Usage, desc->BindFlags, desc->CPUAccessFlags, feature_level))
391 return FALSE;
393 if (desc->MiscFlags & D3D11_RESOURCE_MISC_BUFFER_ALLOW_RAW_VIEWS)
395 if (desc->MiscFlags & D3D11_RESOURCE_MISC_BUFFER_STRUCTURED)
397 WARN("Raw and structure buffers are mutually exclusive.\n");
398 return FALSE;
400 if (!(desc->BindFlags & (D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_UNORDERED_ACCESS)))
402 WARN("Invalid bind flags %#x for raw buffer.\n", desc->BindFlags);
403 return FALSE;
407 if (desc->MiscFlags & D3D11_RESOURCE_MISC_BUFFER_STRUCTURED)
409 if (!desc->StructureByteStride || desc->StructureByteStride % 4)
411 WARN("Invalid structure byte stride %u.\n", desc->StructureByteStride);
412 return FALSE;
414 if (desc->ByteWidth % desc->StructureByteStride)
416 WARN("Byte width %u is not divisible by structure byte stride %u.\n",
417 desc->ByteWidth, desc->StructureByteStride);
418 return FALSE;
421 else
423 desc->StructureByteStride = 0;
426 if (desc->MiscFlags & D3D11_RESOURCE_MISC_GENERATE_MIPS)
428 WARN("Buffer with the D3D11_RESOURCE_MISC_GENERATE_MIPS flag.\n");
429 return FALSE;
432 return TRUE;
435 static HRESULT d3d_buffer_init(struct d3d_buffer *buffer, struct d3d_device *device,
436 const D3D11_BUFFER_DESC *desc, const D3D11_SUBRESOURCE_DATA *data)
438 struct wined3d_buffer_desc wined3d_desc;
439 HRESULT hr;
441 buffer->ID3D11Buffer_iface.lpVtbl = &d3d11_buffer_vtbl;
442 buffer->ID3D10Buffer_iface.lpVtbl = &d3d10_buffer_vtbl;
443 buffer->refcount = 1;
444 buffer->desc = *desc;
446 if (!validate_buffer_desc(&buffer->desc, device->feature_level))
447 return E_INVALIDARG;
449 wined3d_desc.byte_width = buffer->desc.ByteWidth;
450 wined3d_desc.usage = wined3d_usage_from_d3d11(buffer->desc.Usage);
451 wined3d_desc.bind_flags = wined3d_bind_flags_from_d3d11(buffer->desc.BindFlags, buffer->desc.MiscFlags);
452 wined3d_desc.access = wined3d_access_from_d3d11(buffer->desc.Usage, buffer->desc.CPUAccessFlags);
453 wined3d_desc.misc_flags = buffer->desc.MiscFlags;
454 wined3d_desc.structure_byte_stride = buffer->desc.StructureByteStride;
456 wined3d_mutex_lock();
457 wined3d_private_store_init(&buffer->private_store);
459 if (FAILED(hr = wined3d_buffer_create(device->wined3d_device, &wined3d_desc,
460 (const struct wined3d_sub_resource_data *)data, buffer,
461 &d3d_buffer_wined3d_parent_ops, &buffer->wined3d_buffer)))
463 WARN("Failed to create wined3d buffer, hr %#x.\n", hr);
464 wined3d_private_store_cleanup(&buffer->private_store);
465 wined3d_mutex_unlock();
466 return hr;
468 wined3d_mutex_unlock();
470 ID3D11Device2_AddRef(buffer->device = &device->ID3D11Device2_iface);
472 return S_OK;
475 HRESULT d3d_buffer_create(struct d3d_device *device, const D3D11_BUFFER_DESC *desc,
476 const D3D11_SUBRESOURCE_DATA *data, struct d3d_buffer **buffer)
478 struct d3d_buffer *object;
479 HRESULT hr;
481 if (!(object = heap_alloc_zero(sizeof(*object))))
482 return E_OUTOFMEMORY;
484 if (FAILED(hr = d3d_buffer_init(object, device, desc, data)))
486 WARN("Failed to initialize buffer, hr %#x.\n", hr);
487 heap_free(object);
488 return hr;
491 TRACE("Created buffer %p.\n", object);
492 *buffer = object;
494 return S_OK;