wined3d: Get resource info from the texture in surface_load_renderbuffer().
[wine.git] / dlls / d3d11 / buffer.c
blob5af121f50657e977a68c4db89c2d7970dd76174c
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 ID3D11Device_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 ID3D11Device *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 ID3D11Device_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 = 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 ID3D11Device_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 HRESULT hr;
308 TRACE("iface %p, map_type %u, map_flags %#x, data %p.\n", iface, map_type, map_flags, data);
310 if (map_flags)
311 FIXME("Ignoring map_flags %#x.\n", map_flags);
313 wined3d_mutex_lock();
314 hr = wined3d_buffer_map(buffer->wined3d_buffer, 0, 0, (BYTE **)data,
315 wined3d_map_flags_from_d3d11_map_type(map_type));
316 wined3d_mutex_unlock();
318 return hr;
321 static void STDMETHODCALLTYPE d3d10_buffer_Unmap(ID3D10Buffer *iface)
323 struct d3d_buffer *buffer = impl_from_ID3D10Buffer(iface);
325 TRACE("iface %p.\n", iface);
327 wined3d_mutex_lock();
328 wined3d_buffer_unmap(buffer->wined3d_buffer);
329 wined3d_mutex_unlock();
332 static void STDMETHODCALLTYPE d3d10_buffer_GetDesc(ID3D10Buffer *iface, D3D10_BUFFER_DESC *desc)
334 struct d3d_buffer *buffer = impl_from_ID3D10Buffer(iface);
335 const D3D11_BUFFER_DESC *d3d11_desc = &buffer->desc;
337 TRACE("iface %p, desc %p.\n", iface, desc);
339 desc->ByteWidth = d3d11_desc->ByteWidth;
340 desc->Usage = d3d10_usage_from_d3d11_usage(d3d11_desc->Usage);
341 desc->BindFlags = d3d10_bind_flags_from_d3d11_bind_flags(d3d11_desc->BindFlags);
342 desc->CPUAccessFlags = d3d10_cpu_access_flags_from_d3d11_cpu_access_flags(d3d11_desc->CPUAccessFlags);
343 desc->MiscFlags = d3d10_resource_misc_flags_from_d3d11_resource_misc_flags(d3d11_desc->MiscFlags);
346 static const struct ID3D10BufferVtbl d3d10_buffer_vtbl =
348 /* IUnknown methods */
349 d3d10_buffer_QueryInterface,
350 d3d10_buffer_AddRef,
351 d3d10_buffer_Release,
352 /* ID3D10DeviceChild methods */
353 d3d10_buffer_GetDevice,
354 d3d10_buffer_GetPrivateData,
355 d3d10_buffer_SetPrivateData,
356 d3d10_buffer_SetPrivateDataInterface,
357 /* ID3D10Resource methods */
358 d3d10_buffer_GetType,
359 d3d10_buffer_SetEvictionPriority,
360 d3d10_buffer_GetEvictionPriority,
361 /* ID3D10Buffer methods */
362 d3d10_buffer_Map,
363 d3d10_buffer_Unmap,
364 d3d10_buffer_GetDesc,
367 struct d3d_buffer *unsafe_impl_from_ID3D10Buffer(ID3D10Buffer *iface)
369 if (!iface)
370 return NULL;
371 assert(iface->lpVtbl == &d3d10_buffer_vtbl);
372 return CONTAINING_RECORD(iface, struct d3d_buffer, ID3D10Buffer_iface);
375 static void STDMETHODCALLTYPE d3d10_buffer_wined3d_object_released(void *parent)
377 struct d3d_buffer *buffer = parent;
379 wined3d_private_store_cleanup(&buffer->private_store);
380 HeapFree(GetProcessHeap(), 0, parent);
383 static const struct wined3d_parent_ops d3d10_buffer_wined3d_parent_ops =
385 d3d10_buffer_wined3d_object_released,
388 static HRESULT d3d_buffer_init(struct d3d_buffer *buffer, struct d3d_device *device,
389 const D3D11_BUFFER_DESC *desc, const D3D11_SUBRESOURCE_DATA *data)
391 struct wined3d_buffer_desc wined3d_desc;
392 HRESULT hr;
394 buffer->ID3D11Buffer_iface.lpVtbl = &d3d11_buffer_vtbl;
395 buffer->ID3D10Buffer_iface.lpVtbl = &d3d10_buffer_vtbl;
396 buffer->refcount = 1;
397 wined3d_mutex_lock();
398 wined3d_private_store_init(&buffer->private_store);
399 buffer->desc = *desc;
401 wined3d_desc.byte_width = desc->ByteWidth;
402 wined3d_desc.usage = wined3d_usage_from_d3d11(0, desc->Usage);
403 wined3d_desc.bind_flags = desc->BindFlags;
404 wined3d_desc.cpu_access_flags = desc->CPUAccessFlags;
405 wined3d_desc.misc_flags = desc->MiscFlags;
407 if (desc->StructureByteStride)
408 FIXME("Ignoring structure byte stride %u.\n", desc->StructureByteStride);
410 if (FAILED(hr = wined3d_buffer_create(device->wined3d_device, &wined3d_desc,
411 (const struct wined3d_sub_resource_data *)data, buffer,
412 &d3d10_buffer_wined3d_parent_ops, &buffer->wined3d_buffer)))
414 WARN("Failed to create wined3d buffer, hr %#x.\n", hr);
415 wined3d_private_store_cleanup(&buffer->private_store);
416 wined3d_mutex_unlock();
417 return hr;
419 wined3d_mutex_unlock();
421 buffer->device = &device->ID3D11Device_iface;
422 ID3D11Device_AddRef(buffer->device);
424 return S_OK;
427 HRESULT d3d_buffer_create(struct d3d_device *device, const D3D11_BUFFER_DESC *desc,
428 const D3D11_SUBRESOURCE_DATA *data, struct d3d_buffer **buffer)
430 struct d3d_buffer *object;
431 HRESULT hr;
433 if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object))))
434 return E_OUTOFMEMORY;
436 if (FAILED(hr = d3d_buffer_init(object, device, desc, data)))
438 WARN("Failed to initialize buffer, hr %#x.\n", hr);
439 HeapFree(GetProcessHeap(), 0, object);
440 return hr;
443 TRACE("Created buffer %p.\n", object);
444 *buffer = object;
446 return S_OK;