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 /* 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
);
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
;
59 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid
));
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
);
74 ID3D11Device_AddRef(buffer
->device
);
76 wined3d_buffer_incref(buffer
->wined3d_buffer
);
77 wined3d_mutex_unlock();
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
);
92 ID3D11Device
*device
= buffer
->device
;
95 wined3d_buffer_decref(buffer
->wined3d_buffer
);
96 wined3d_mutex_unlock();
97 /* Release the device last, it may cause the wined3d device to be
99 ID3D11Device_Release(device
);
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
);
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
,
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
)
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
);
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
;
309 TRACE("iface %p, map_type %u, map_flags %#x, data %p.\n", iface
, map_type
, map_flags
, data
);
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();
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
,
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 */
366 d3d10_buffer_GetDesc
,
369 struct d3d_buffer
*unsafe_impl_from_ID3D10Buffer(ID3D10Buffer
*iface
)
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 HeapFree(GetProcessHeap(), 0, 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
)
392 if (desc
->MiscFlags
& D3D11_RESOURCE_MISC_BUFFER_ALLOW_RAW_VIEWS
)
394 if (desc
->MiscFlags
& D3D11_RESOURCE_MISC_BUFFER_STRUCTURED
)
396 WARN("Raw and structure buffers are mutually exclusive.\n");
399 if (!(desc
->BindFlags
& (D3D11_BIND_SHADER_RESOURCE
| D3D11_BIND_UNORDERED_ACCESS
)))
401 WARN("Invalid bind flags %#x for raw buffer.\n", desc
->BindFlags
);
406 if (desc
->MiscFlags
& D3D11_RESOURCE_MISC_BUFFER_STRUCTURED
)
408 if (!desc
->StructureByteStride
|| desc
->StructureByteStride
% 4)
410 WARN("Invalid structure byte stride %u.\n", desc
->StructureByteStride
);
413 if (desc
->ByteWidth
% desc
->StructureByteStride
)
415 WARN("Byte width %u is not divisible by structure byte stride %u.\n",
416 desc
->ByteWidth
, desc
->StructureByteStride
);
422 desc
->StructureByteStride
= 0;
425 if (desc
->MiscFlags
& D3D11_RESOURCE_MISC_GENERATE_MIPS
)
427 WARN("Buffer with the D3D11_RESOURCE_MISC_GENERATE_MIPS flag.\n");
434 static HRESULT
d3d_buffer_init(struct d3d_buffer
*buffer
, struct d3d_device
*device
,
435 const D3D11_BUFFER_DESC
*desc
, const D3D11_SUBRESOURCE_DATA
*data
)
437 struct wined3d_buffer_desc wined3d_desc
;
440 buffer
->ID3D11Buffer_iface
.lpVtbl
= &d3d11_buffer_vtbl
;
441 buffer
->ID3D10Buffer_iface
.lpVtbl
= &d3d10_buffer_vtbl
;
442 buffer
->refcount
= 1;
443 buffer
->desc
= *desc
;
445 if (!validate_buffer_desc(&buffer
->desc
))
448 wined3d_desc
.byte_width
= buffer
->desc
.ByteWidth
;
449 wined3d_desc
.usage
= wined3d_usage_from_d3d11(0, buffer
->desc
.Usage
);
450 wined3d_desc
.bind_flags
= buffer
->desc
.BindFlags
;
451 wined3d_desc
.cpu_access_flags
= buffer
->desc
.CPUAccessFlags
;
452 wined3d_desc
.misc_flags
= buffer
->desc
.MiscFlags
;
453 wined3d_desc
.structure_byte_stride
= buffer
->desc
.StructureByteStride
;
455 wined3d_mutex_lock();
456 wined3d_private_store_init(&buffer
->private_store
);
458 if (FAILED(hr
= wined3d_buffer_create(device
->wined3d_device
, &wined3d_desc
,
459 (const struct wined3d_sub_resource_data
*)data
, buffer
,
460 &d3d_buffer_wined3d_parent_ops
, &buffer
->wined3d_buffer
)))
462 WARN("Failed to create wined3d buffer, hr %#x.\n", hr
);
463 wined3d_private_store_cleanup(&buffer
->private_store
);
464 wined3d_mutex_unlock();
467 wined3d_mutex_unlock();
469 buffer
->device
= &device
->ID3D11Device_iface
;
470 ID3D11Device_AddRef(buffer
->device
);
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
;
481 if (!(object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, 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 HeapFree(GetProcessHeap(), 0, object
);
491 TRACE("Created buffer %p.\n", object
);