server: Create the initial thread as a separate request.
[wine.git] / dlls / d3d11 / buffer.c
blob2221a8fdd5ae5d7278471b89e8d61403a5fd16c7
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 /* ID3D11Buffer methods */
29 static inline struct d3d_buffer *impl_from_ID3D11Buffer(ID3D11Buffer *iface)
31 return CONTAINING_RECORD(iface, struct d3d_buffer, ID3D11Buffer_iface);
34 static HRESULT STDMETHODCALLTYPE d3d11_buffer_QueryInterface(ID3D11Buffer *iface, REFIID riid, void **out)
36 struct d3d_buffer *buffer = impl_from_ID3D11Buffer(iface);
38 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
40 if (IsEqualGUID(riid, &IID_ID3D11Buffer)
41 || IsEqualGUID(riid, &IID_ID3D11Resource)
42 || IsEqualGUID(riid, &IID_ID3D11DeviceChild)
43 || IsEqualGUID(riid, &IID_IUnknown))
45 ID3D11Buffer_AddRef(iface);
46 *out = iface;
47 return S_OK;
50 if (IsEqualGUID(riid, &IID_ID3D10Buffer)
51 || IsEqualGUID(riid, &IID_ID3D10Resource)
52 || IsEqualGUID(riid, &IID_ID3D10DeviceChild))
54 ID3D10Buffer_AddRef(&buffer->ID3D10Buffer_iface);
55 *out = &buffer->ID3D10Buffer_iface;
56 return S_OK;
59 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
61 *out = NULL;
62 return E_NOINTERFACE;
65 static ULONG STDMETHODCALLTYPE d3d11_buffer_AddRef(ID3D11Buffer *iface)
67 struct d3d_buffer *buffer = impl_from_ID3D11Buffer(iface);
68 ULONG refcount = InterlockedIncrement(&buffer->refcount);
70 TRACE("%p increasing refcount to %u.\n", buffer, refcount);
72 if (refcount == 1)
74 ID3D11Device2_AddRef(buffer->device);
75 wined3d_mutex_lock();
76 wined3d_buffer_incref(buffer->wined3d_buffer);
77 wined3d_mutex_unlock();
80 return refcount;
83 static ULONG STDMETHODCALLTYPE d3d11_buffer_Release(ID3D11Buffer *iface)
85 struct d3d_buffer *buffer = impl_from_ID3D11Buffer(iface);
86 ULONG refcount = InterlockedDecrement(&buffer->refcount);
88 TRACE("%p decreasing refcount to %u.\n", buffer, refcount);
90 if (!refcount)
92 ID3D11Device2 *device = buffer->device;
94 wined3d_mutex_lock();
95 wined3d_buffer_decref(buffer->wined3d_buffer);
96 wined3d_mutex_unlock();
97 /* Release the device last, it may cause the wined3d device to be
98 * destroyed. */
99 ID3D11Device2_Release(device);
102 return refcount;
105 static void STDMETHODCALLTYPE d3d11_buffer_GetDevice(ID3D11Buffer *iface, ID3D11Device **device)
107 struct d3d_buffer *buffer = impl_from_ID3D11Buffer(iface);
109 TRACE("iface %p, device %p.\n", iface, device);
111 *device = (ID3D11Device *)buffer->device;
112 ID3D11Device_AddRef(*device);
115 static HRESULT STDMETHODCALLTYPE d3d11_buffer_GetPrivateData(ID3D11Buffer *iface,
116 REFGUID guid, UINT *data_size, void *data)
118 struct d3d_buffer *buffer = impl_from_ID3D11Buffer(iface);
120 TRACE("iface %p, guid %s, data_size %p, data %p.\n", iface, debugstr_guid(guid), data_size, data);
122 return d3d_get_private_data(&buffer->private_store, guid, data_size, data);
125 static HRESULT STDMETHODCALLTYPE d3d11_buffer_SetPrivateData(ID3D11Buffer *iface,
126 REFGUID guid, UINT data_size, const void *data)
128 struct d3d_buffer *buffer = impl_from_ID3D11Buffer(iface);
130 TRACE("iface %p, guid %s, data_size %u, data %p.\n", iface, debugstr_guid(guid), data_size, data);
132 return d3d_set_private_data(&buffer->private_store, guid, data_size, data);
135 static HRESULT STDMETHODCALLTYPE d3d11_buffer_SetPrivateDataInterface(ID3D11Buffer *iface,
136 REFGUID guid, const IUnknown *data)
138 struct d3d_buffer *buffer = impl_from_ID3D11Buffer(iface);
140 TRACE("iface %p, guid %s, data %p.\n", iface, debugstr_guid(guid), data);
142 return d3d_set_private_data_interface(&buffer->private_store, guid, data);
145 static void STDMETHODCALLTYPE d3d11_buffer_GetType(ID3D11Buffer *iface,
146 D3D11_RESOURCE_DIMENSION *resource_dimension)
148 TRACE("iface %p, resource_dimension %p.\n", iface, resource_dimension);
150 *resource_dimension = D3D11_RESOURCE_DIMENSION_BUFFER;
153 static void STDMETHODCALLTYPE d3d11_buffer_SetEvictionPriority(ID3D11Buffer *iface, UINT eviction_priority)
155 FIXME("iface %p, eviction_priority %#x stub!\n", iface, eviction_priority);
158 static UINT STDMETHODCALLTYPE d3d11_buffer_GetEvictionPriority(ID3D11Buffer *iface)
160 FIXME("iface %p stub!\n", iface);
162 return 0;
165 static void STDMETHODCALLTYPE d3d11_buffer_GetDesc(ID3D11Buffer *iface, D3D11_BUFFER_DESC *desc)
167 struct d3d_buffer *buffer = impl_from_ID3D11Buffer(iface);
169 TRACE("iface %p, desc %p.\n", iface, desc);
171 *desc = buffer->desc;
174 static const struct ID3D11BufferVtbl d3d11_buffer_vtbl =
176 /* IUnknown methods */
177 d3d11_buffer_QueryInterface,
178 d3d11_buffer_AddRef,
179 d3d11_buffer_Release,
180 /* ID3D11DeviceChild methods */
181 d3d11_buffer_GetDevice,
182 d3d11_buffer_GetPrivateData,
183 d3d11_buffer_SetPrivateData,
184 d3d11_buffer_SetPrivateDataInterface,
185 /* ID3D11Resource methods */
186 d3d11_buffer_GetType,
187 d3d11_buffer_SetEvictionPriority,
188 d3d11_buffer_GetEvictionPriority,
189 /* ID3D11Buffer methods */
190 d3d11_buffer_GetDesc,
193 struct d3d_buffer *unsafe_impl_from_ID3D11Buffer(ID3D11Buffer *iface)
195 if (!iface)
196 return NULL;
197 assert(iface->lpVtbl == &d3d11_buffer_vtbl);
198 return CONTAINING_RECORD(iface, struct d3d_buffer, ID3D11Buffer_iface);
201 /* ID3D10Buffer methods */
203 static inline struct d3d_buffer *impl_from_ID3D10Buffer(ID3D10Buffer *iface)
205 return CONTAINING_RECORD(iface, struct d3d_buffer, ID3D10Buffer_iface);
208 /* IUnknown methods */
210 static HRESULT STDMETHODCALLTYPE d3d10_buffer_QueryInterface(ID3D10Buffer *iface, REFIID riid, void **out)
212 struct d3d_buffer *buffer = impl_from_ID3D10Buffer(iface);
214 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
216 return d3d11_buffer_QueryInterface(&buffer->ID3D11Buffer_iface, riid, out);
219 static ULONG STDMETHODCALLTYPE d3d10_buffer_AddRef(ID3D10Buffer *iface)
221 struct d3d_buffer *buffer = impl_from_ID3D10Buffer(iface);
223 TRACE("iface %p.\n", iface);
225 return d3d11_buffer_AddRef(&buffer->ID3D11Buffer_iface);
228 static ULONG STDMETHODCALLTYPE d3d10_buffer_Release(ID3D10Buffer *iface)
230 struct d3d_buffer *buffer = impl_from_ID3D10Buffer(iface);
232 TRACE("iface %p.\n", iface);
234 return d3d11_buffer_Release(&buffer->ID3D11Buffer_iface);
237 /* ID3D10DeviceChild methods */
239 static void STDMETHODCALLTYPE d3d10_buffer_GetDevice(ID3D10Buffer *iface, ID3D10Device **device)
241 struct d3d_buffer *buffer = impl_from_ID3D10Buffer(iface);
243 TRACE("iface %p, device %p.\n", iface, device);
245 ID3D11Device2_QueryInterface(buffer->device, &IID_ID3D10Device, (void **)device);
248 static HRESULT STDMETHODCALLTYPE d3d10_buffer_GetPrivateData(ID3D10Buffer *iface,
249 REFGUID guid, UINT *data_size, void *data)
251 struct d3d_buffer *buffer = impl_from_ID3D10Buffer(iface);
253 TRACE("iface %p, guid %s, data_size %p, data %p.\n",
254 iface, debugstr_guid(guid), data_size, data);
256 return d3d_get_private_data(&buffer->private_store, guid, data_size, data);
259 static HRESULT STDMETHODCALLTYPE d3d10_buffer_SetPrivateData(ID3D10Buffer *iface,
260 REFGUID guid, UINT data_size, const void *data)
262 struct d3d_buffer *buffer = impl_from_ID3D10Buffer(iface);
264 TRACE("iface %p, guid %s, data_size %u, data %p.\n",
265 iface, debugstr_guid(guid), data_size, data);
267 return d3d_set_private_data(&buffer->private_store, guid, data_size, data);
270 static HRESULT STDMETHODCALLTYPE d3d10_buffer_SetPrivateDataInterface(ID3D10Buffer *iface,
271 REFGUID guid, const IUnknown *data)
273 struct d3d_buffer *buffer = impl_from_ID3D10Buffer(iface);
275 TRACE("iface %p, guid %s, data %p.\n", iface, debugstr_guid(guid), data);
277 return d3d_set_private_data_interface(&buffer->private_store, guid, data);
280 /* ID3D10Resource methods */
282 static void STDMETHODCALLTYPE d3d10_buffer_GetType(ID3D10Buffer *iface, D3D10_RESOURCE_DIMENSION *resource_dimension)
284 TRACE("iface %p, resource_dimension %p\n", iface, resource_dimension);
286 *resource_dimension = D3D10_RESOURCE_DIMENSION_BUFFER;
289 static void STDMETHODCALLTYPE d3d10_buffer_SetEvictionPriority(ID3D10Buffer *iface, UINT eviction_priority)
291 FIXME("iface %p, eviction_priority %u stub!\n", iface, eviction_priority);
294 static UINT STDMETHODCALLTYPE d3d10_buffer_GetEvictionPriority(ID3D10Buffer *iface)
296 FIXME("iface %p stub!\n", iface);
298 return 0;
301 /* ID3D10Buffer methods */
303 static HRESULT STDMETHODCALLTYPE d3d10_buffer_Map(ID3D10Buffer *iface, D3D10_MAP map_type, UINT map_flags, void **data)
305 struct d3d_buffer *buffer = impl_from_ID3D10Buffer(iface);
306 struct wined3d_map_desc wined3d_map_desc;
307 HRESULT hr;
309 TRACE("iface %p, map_type %u, map_flags %#x, data %p.\n", iface, map_type, map_flags, data);
311 if (map_flags)
312 FIXME("Ignoring map_flags %#x.\n", map_flags);
314 wined3d_mutex_lock();
315 hr = wined3d_resource_map(wined3d_buffer_get_resource(buffer->wined3d_buffer), 0,
316 &wined3d_map_desc, NULL, wined3d_map_flags_from_d3d11_map_type(map_type));
317 *data = wined3d_map_desc.data;
318 wined3d_mutex_unlock();
320 return hr;
323 static void STDMETHODCALLTYPE d3d10_buffer_Unmap(ID3D10Buffer *iface)
325 struct d3d_buffer *buffer = impl_from_ID3D10Buffer(iface);
327 TRACE("iface %p.\n", iface);
329 wined3d_mutex_lock();
330 wined3d_resource_unmap(wined3d_buffer_get_resource(buffer->wined3d_buffer), 0);
331 wined3d_mutex_unlock();
334 static void STDMETHODCALLTYPE d3d10_buffer_GetDesc(ID3D10Buffer *iface, D3D10_BUFFER_DESC *desc)
336 struct d3d_buffer *buffer = impl_from_ID3D10Buffer(iface);
337 const D3D11_BUFFER_DESC *d3d11_desc = &buffer->desc;
339 TRACE("iface %p, desc %p.\n", iface, desc);
341 desc->ByteWidth = d3d11_desc->ByteWidth;
342 desc->Usage = d3d10_usage_from_d3d11_usage(d3d11_desc->Usage);
343 desc->BindFlags = d3d10_bind_flags_from_d3d11_bind_flags(d3d11_desc->BindFlags);
344 desc->CPUAccessFlags = d3d10_cpu_access_flags_from_d3d11_cpu_access_flags(d3d11_desc->CPUAccessFlags);
345 desc->MiscFlags = d3d10_resource_misc_flags_from_d3d11_resource_misc_flags(d3d11_desc->MiscFlags);
348 static const struct ID3D10BufferVtbl d3d10_buffer_vtbl =
350 /* IUnknown methods */
351 d3d10_buffer_QueryInterface,
352 d3d10_buffer_AddRef,
353 d3d10_buffer_Release,
354 /* ID3D10DeviceChild methods */
355 d3d10_buffer_GetDevice,
356 d3d10_buffer_GetPrivateData,
357 d3d10_buffer_SetPrivateData,
358 d3d10_buffer_SetPrivateDataInterface,
359 /* ID3D10Resource methods */
360 d3d10_buffer_GetType,
361 d3d10_buffer_SetEvictionPriority,
362 d3d10_buffer_GetEvictionPriority,
363 /* ID3D10Buffer methods */
364 d3d10_buffer_Map,
365 d3d10_buffer_Unmap,
366 d3d10_buffer_GetDesc,
369 struct d3d_buffer *unsafe_impl_from_ID3D10Buffer(ID3D10Buffer *iface)
371 if (!iface)
372 return NULL;
373 assert(iface->lpVtbl == &d3d10_buffer_vtbl);
374 return CONTAINING_RECORD(iface, struct d3d_buffer, ID3D10Buffer_iface);
377 static void STDMETHODCALLTYPE d3d_buffer_wined3d_object_released(void *parent)
379 struct d3d_buffer *buffer = parent;
381 wined3d_private_store_cleanup(&buffer->private_store);
382 heap_free(parent);
385 static const struct wined3d_parent_ops d3d_buffer_wined3d_parent_ops =
387 d3d_buffer_wined3d_object_released,
390 static BOOL validate_buffer_desc(D3D11_BUFFER_DESC *desc, D3D_FEATURE_LEVEL feature_level)
392 if (!validate_d3d11_resource_access_flags(D3D11_RESOURCE_DIMENSION_BUFFER,
393 desc->Usage, desc->BindFlags, desc->CPUAccessFlags, feature_level))
394 return FALSE;
396 if (desc->MiscFlags & D3D11_RESOURCE_MISC_BUFFER_ALLOW_RAW_VIEWS)
398 if (desc->MiscFlags & D3D11_RESOURCE_MISC_BUFFER_STRUCTURED)
400 WARN("Raw and structure buffers are mutually exclusive.\n");
401 return FALSE;
403 if (!(desc->BindFlags & (D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_UNORDERED_ACCESS)))
405 WARN("Invalid bind flags %#x for raw buffer.\n", desc->BindFlags);
406 return FALSE;
410 if (desc->MiscFlags & D3D11_RESOURCE_MISC_BUFFER_STRUCTURED)
412 if (!desc->StructureByteStride || desc->StructureByteStride % 4)
414 WARN("Invalid structure byte stride %u.\n", desc->StructureByteStride);
415 return FALSE;
417 if (desc->ByteWidth % desc->StructureByteStride)
419 WARN("Byte width %u is not divisible by structure byte stride %u.\n",
420 desc->ByteWidth, desc->StructureByteStride);
421 return FALSE;
424 else
426 desc->StructureByteStride = 0;
429 if (desc->MiscFlags & D3D11_RESOURCE_MISC_GENERATE_MIPS)
431 WARN("Buffer with the D3D11_RESOURCE_MISC_GENERATE_MIPS flag.\n");
432 return FALSE;
435 return TRUE;
438 static HRESULT d3d_buffer_init(struct d3d_buffer *buffer, struct d3d_device *device,
439 const D3D11_BUFFER_DESC *desc, const D3D11_SUBRESOURCE_DATA *data)
441 struct wined3d_buffer_desc wined3d_desc;
442 HRESULT hr;
444 buffer->ID3D11Buffer_iface.lpVtbl = &d3d11_buffer_vtbl;
445 buffer->ID3D10Buffer_iface.lpVtbl = &d3d10_buffer_vtbl;
446 buffer->refcount = 1;
447 buffer->desc = *desc;
449 if (!validate_buffer_desc(&buffer->desc, device->feature_level))
450 return E_INVALIDARG;
452 wined3d_desc.byte_width = buffer->desc.ByteWidth;
453 wined3d_desc.usage = wined3d_usage_from_d3d11(0, buffer->desc.Usage);
454 wined3d_desc.bind_flags = buffer->desc.BindFlags;
455 wined3d_desc.access = wined3d_access_from_d3d11(buffer->desc.Usage, buffer->desc.CPUAccessFlags);
456 wined3d_desc.misc_flags = buffer->desc.MiscFlags;
457 wined3d_desc.structure_byte_stride = buffer->desc.StructureByteStride;
459 wined3d_mutex_lock();
460 wined3d_private_store_init(&buffer->private_store);
462 if (FAILED(hr = wined3d_buffer_create(device->wined3d_device, &wined3d_desc,
463 (const struct wined3d_sub_resource_data *)data, buffer,
464 &d3d_buffer_wined3d_parent_ops, &buffer->wined3d_buffer)))
466 WARN("Failed to create wined3d buffer, hr %#x.\n", hr);
467 wined3d_private_store_cleanup(&buffer->private_store);
468 wined3d_mutex_unlock();
469 return hr;
471 wined3d_mutex_unlock();
473 ID3D11Device2_AddRef(buffer->device = &device->ID3D11Device2_iface);
475 return S_OK;
478 HRESULT d3d_buffer_create(struct d3d_device *device, const D3D11_BUFFER_DESC *desc,
479 const D3D11_SUBRESOURCE_DATA *data, struct d3d_buffer **buffer)
481 struct d3d_buffer *object;
482 HRESULT hr;
484 if (!(object = heap_alloc_zero(sizeof(*object))))
485 return E_OUTOFMEMORY;
487 if (FAILED(hr = d3d_buffer_init(object, device, desc, data)))
489 WARN("Failed to initialize buffer, hr %#x.\n", hr);
490 heap_free(object);
491 return hr;
494 TRACE("Created buffer %p.\n", object);
495 *buffer = object;
497 return S_OK;