diff --git a/dlls/d3d11/d3d11_private.h b/dlls/d3d11/d3d11_private.h index 55cd579dc4d..8fde5783ac1 100644 --- a/dlls/d3d11/d3d11_private.h +++ b/dlls/d3d11/d3d11_private.h @@ -220,6 +220,22 @@ struct d3d_shader_resource_view *unsafe_impl_from_ID3D11ShaderResourceView( struct d3d_shader_resource_view *unsafe_impl_from_ID3D10ShaderResourceView( ID3D10ShaderResourceView *iface) DECLSPEC_HIDDEN; +/* ID3D11UnorderedAccessView */ +struct d3d11_unordered_access_view +{ + ID3D11UnorderedAccessView ID3D11UnorderedAccessView_iface; + LONG refcount; + + struct wined3d_private_store private_store; + struct wined3d_unordered_access_view *wined3d_view; + D3D11_UNORDERED_ACCESS_VIEW_DESC desc; + ID3D11Resource *resource; + ID3D11Device *device; +}; + +HRESULT d3d11_unordered_access_view_create(struct d3d_device *device, ID3D11Resource *resource, + const D3D11_UNORDERED_ACCESS_VIEW_DESC *desc, struct d3d11_unordered_access_view **view) DECLSPEC_HIDDEN; + /* ID3D11InputLayout, ID3D10InputLayout */ struct d3d_input_layout { diff --git a/dlls/d3d11/device.c b/dlls/d3d11/device.c index 74a6055aa53..2fa0f9d3733 100644 --- a/dlls/d3d11/device.c +++ b/dlls/d3d11/device.c @@ -2114,9 +2114,18 @@ static HRESULT STDMETHODCALLTYPE d3d11_device_CreateShaderResourceView(ID3D11Dev static HRESULT STDMETHODCALLTYPE d3d11_device_CreateUnorderedAccessView(ID3D11Device *iface, ID3D11Resource *resource, const D3D11_UNORDERED_ACCESS_VIEW_DESC *desc, ID3D11UnorderedAccessView **view) { - FIXME("iface %p, resource %p, desc %p, view %p stub!\n", iface, resource, desc, view); + struct d3d_device *device = impl_from_ID3D11Device(iface); + struct d3d11_unordered_access_view *object; + HRESULT hr; - return E_NOTIMPL; + TRACE("iface %p, resource %p, desc %p, view %p.\n", iface, resource, desc, view); + + if (FAILED(hr = d3d11_unordered_access_view_create(device, resource, desc, &object))) + return hr; + + *view = &object->ID3D11UnorderedAccessView_iface; + + return S_OK; } static HRESULT STDMETHODCALLTYPE d3d11_device_CreateRenderTargetView(ID3D11Device *iface, diff --git a/dlls/d3d11/view.c b/dlls/d3d11/view.c index 122253c4244..d0d1b243ccb 100644 --- a/dlls/d3d11/view.c +++ b/dlls/d3d11/view.c @@ -702,6 +702,188 @@ static HRESULT normalize_srv_desc(D3D11_SHADER_RESOURCE_VIEW_DESC *desc, ID3D11R return S_OK; } +static HRESULT set_uav_desc_from_resource(D3D11_UNORDERED_ACCESS_VIEW_DESC *desc, ID3D11Resource *resource) +{ + D3D11_RESOURCE_DIMENSION dimension; + + ID3D11Resource_GetType(resource, &dimension); + + switch (dimension) + { + case D3D11_RESOURCE_DIMENSION_TEXTURE2D: + { + D3D11_TEXTURE2D_DESC texture_desc; + ID3D11Texture2D *texture; + + if (FAILED(ID3D11Resource_QueryInterface(resource, &IID_ID3D11Texture2D, (void **)&texture))) + { + ERR("Resource of type TEXTURE2D doesn't implement ID3D11Texture2D.\n"); + return E_INVALIDARG; + } + + ID3D11Texture2D_GetDesc(texture, &texture_desc); + ID3D11Texture2D_Release(texture); + + if (texture_desc.SampleDesc.Count != 1) + { + WARN("Trying to create view for multisample texture.\n"); + return E_INVALIDARG; + } + + desc->Format = texture_desc.Format; + if (texture_desc.ArraySize == 1) + { + desc->ViewDimension = D3D11_UAV_DIMENSION_TEXTURE2D; + desc->u.Texture2D.MipSlice = 0; + } + else + { + desc->ViewDimension = D3D11_UAV_DIMENSION_TEXTURE2DARRAY; + desc->u.Texture2DArray.MipSlice = 0; + desc->u.Texture2DArray.FirstArraySlice = 0; + desc->u.Texture2DArray.ArraySize = texture_desc.ArraySize; + } + + return S_OK; + } + + case D3D11_RESOURCE_DIMENSION_TEXTURE3D: + { + D3D11_TEXTURE3D_DESC texture_desc; + ID3D11Texture3D *texture; + + if (FAILED(ID3D11Resource_QueryInterface(resource, &IID_ID3D11Texture3D, (void **)&texture))) + { + ERR("Resource of type TEXTURE3D doesn't implement ID3D11Texture3D.\n"); + return E_INVALIDARG; + } + + ID3D11Texture3D_GetDesc(texture, &texture_desc); + ID3D11Texture3D_Release(texture); + + desc->Format = texture_desc.Format; + desc->ViewDimension = D3D11_UAV_DIMENSION_TEXTURE3D; + desc->u.Texture3D.MipSlice = 0; + desc->u.Texture3D.FirstWSlice = 0; + desc->u.Texture3D.WSize = texture_desc.Depth; + + return S_OK; + } + + default: + FIXME("Unhandled resource dimension %#x.\n", dimension); + return E_INVALIDARG; + } +} + +static HRESULT normalize_uav_desc(D3D11_UNORDERED_ACCESS_VIEW_DESC *desc, ID3D11Resource *resource) +{ + D3D11_RESOURCE_DIMENSION dimension; + unsigned int layer_count; + DXGI_FORMAT format; + + ID3D11Resource_GetType(resource, &dimension); + switch (dimension) + { + case D3D11_RESOURCE_DIMENSION_BUFFER: + { + if (desc->ViewDimension != D3D11_UAV_DIMENSION_BUFFER) + { + WARN("Incompatible dimensions %#x, %#x.\n", dimension, desc->ViewDimension); + return E_INVALIDARG; + } + return S_OK; + } + + case D3D11_RESOURCE_DIMENSION_TEXTURE1D: + { + if (desc->ViewDimension != D3D11_UAV_DIMENSION_TEXTURE1D + && desc->ViewDimension != D3D11_UAV_DIMENSION_TEXTURE1DARRAY) + { + WARN("Incompatible dimensions %#x, %#x.\n", dimension, desc->ViewDimension); + return E_INVALIDARG; + } + + FIXME("Unhandled 1D texture resource.\n"); + return S_OK; + } + + case D3D11_RESOURCE_DIMENSION_TEXTURE2D: + { + const struct d3d_texture2d *texture; + + if (desc->ViewDimension != D3D11_UAV_DIMENSION_TEXTURE2D + && desc->ViewDimension != D3D11_UAV_DIMENSION_TEXTURE2DARRAY) + { + WARN("Incompatible dimensions %#x, %#x.\n", dimension, desc->ViewDimension); + return E_INVALIDARG; + } + + if (!(texture = unsafe_impl_from_ID3D11Texture2D((ID3D11Texture2D *)resource))) + { + ERR("Cannot get implementation from ID3D11Texture2D.\n"); + return E_FAIL; + } + + format = texture->desc.Format; + layer_count = texture->desc.ArraySize; + break; + } + + case D3D11_RESOURCE_DIMENSION_TEXTURE3D: + { + const struct d3d_texture3d *texture; + + if (desc->ViewDimension != D3D11_UAV_DIMENSION_TEXTURE3D) + { + WARN("Incompatible dimensions %#x, %#x.\n", dimension, desc->ViewDimension); + return E_INVALIDARG; + } + + if (!(texture = unsafe_impl_from_ID3D11Texture3D((ID3D11Texture3D *)resource))) + { + ERR("Cannot get implementation from ID3D11Texture3D.\n"); + return E_FAIL; + } + + format = texture->desc.Format; + layer_count = texture->desc.Depth; + break; + } + + default: + ERR("Unhandled resource dimension %#x.\n", dimension); + return E_FAIL; + } + + if (desc->Format == DXGI_FORMAT_UNKNOWN) + desc->Format = format; + + switch (desc->ViewDimension) + { + case D3D11_UAV_DIMENSION_TEXTURE1DARRAY: + if (desc->u.Texture1DArray.ArraySize == ~0u && desc->u.Texture1DArray.FirstArraySlice < layer_count) + desc->u.Texture1DArray.ArraySize = layer_count - desc->u.Texture1DArray.ArraySize; + break; + + case D3D11_UAV_DIMENSION_TEXTURE2DARRAY: + if (desc->u.Texture2DArray.ArraySize == ~0u && desc->u.Texture2DArray.FirstArraySlice < layer_count) + desc->u.Texture2DArray.ArraySize = layer_count - desc->u.Texture2DArray.FirstArraySlice; + break; + + case D3D11_UAV_DIMENSION_TEXTURE3D: + layer_count = max(1, layer_count >> desc->u.Texture3D.MipSlice); + if (desc->u.Texture3D.WSize == ~0u && desc->u.Texture3D.FirstWSlice < layer_count) + desc->u.Texture3D.WSize = layer_count - desc->u.Texture3D.FirstWSlice; + break; + + default: + break; + } + + return S_OK; +} + /* ID3D11DepthStencilView methods */ static inline struct d3d_depthstencil_view *impl_from_ID3D11DepthStencilView(ID3D11DepthStencilView *iface) @@ -2011,3 +2193,263 @@ struct d3d_shader_resource_view *unsafe_impl_from_ID3D10ShaderResourceView(ID3D1 assert(iface->lpVtbl == (ID3D10ShaderResourceViewVtbl *)&d3d10_shader_resource_view_vtbl); return CONTAINING_RECORD(iface, struct d3d_shader_resource_view, ID3D10ShaderResourceView1_iface); } + +/* ID3D11UnorderedAccessView methods */ + +static inline struct d3d11_unordered_access_view *impl_from_ID3D11UnorderedAccessView(ID3D11UnorderedAccessView *iface) +{ + return CONTAINING_RECORD(iface, struct d3d11_unordered_access_view, ID3D11UnorderedAccessView_iface); +} + +static HRESULT STDMETHODCALLTYPE d3d11_unordered_access_view_QueryInterface(ID3D11UnorderedAccessView *iface, + REFIID riid, void **object) +{ + TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object); + + if (IsEqualGUID(riid, &IID_ID3D11UnorderedAccessView) + || IsEqualGUID(riid, &IID_ID3D11View) + || IsEqualGUID(riid, &IID_ID3D11DeviceChild) + || IsEqualGUID(riid, &IID_IUnknown)) + { + ID3D11UnorderedAccessView_AddRef(*object = iface); + return S_OK; + } + + WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid)); + + *object = NULL; + return E_NOINTERFACE; +} + +static ULONG STDMETHODCALLTYPE d3d11_unordered_access_view_AddRef(ID3D11UnorderedAccessView *iface) +{ + struct d3d11_unordered_access_view *view = impl_from_ID3D11UnorderedAccessView(iface); + ULONG refcount = InterlockedIncrement(&view->refcount); + + TRACE("%p increasing refcount to %u.\n", view, refcount); + + return refcount; +} + +static ULONG STDMETHODCALLTYPE d3d11_unordered_access_view_Release(ID3D11UnorderedAccessView *iface) +{ + struct d3d11_unordered_access_view *view = impl_from_ID3D11UnorderedAccessView(iface); + ULONG refcount = InterlockedDecrement(&view->refcount); + + TRACE("%p decreasing refcount to %u.\n", view, refcount); + + if (!refcount) + { + wined3d_mutex_lock(); + wined3d_unordered_access_view_decref(view->wined3d_view); + ID3D11Resource_Release(view->resource); + ID3D11Device_Release(view->device); + wined3d_private_store_cleanup(&view->private_store); + wined3d_mutex_unlock(); + HeapFree(GetProcessHeap(), 0, view); + } + + return refcount; +} + +static void STDMETHODCALLTYPE d3d11_unordered_access_view_GetDevice(ID3D11UnorderedAccessView *iface, + ID3D11Device **device) +{ + struct d3d11_unordered_access_view *view = impl_from_ID3D11UnorderedAccessView(iface); + + TRACE("iface %p, device %p.\n", iface, device); + + ID3D11Device_AddRef(*device = view->device); +} + +static HRESULT STDMETHODCALLTYPE d3d11_unordered_access_view_GetPrivateData(ID3D11UnorderedAccessView *iface, + REFGUID guid, UINT *data_size, void *data) +{ + struct d3d11_unordered_access_view *view = impl_from_ID3D11UnorderedAccessView(iface); + + TRACE("iface %p, guid %s, data_size %p, data %p.\n", iface, debugstr_guid(guid), data_size, data); + + return d3d_get_private_data(&view->private_store, guid, data_size, data); +} + +static HRESULT STDMETHODCALLTYPE d3d11_unordered_access_view_SetPrivateData(ID3D11UnorderedAccessView *iface, + REFGUID guid, UINT data_size, const void *data) +{ + struct d3d11_unordered_access_view *view = impl_from_ID3D11UnorderedAccessView(iface); + + TRACE("iface %p, guid %s, data_size %u, data %p.\n", iface, debugstr_guid(guid), data_size, data); + + return d3d_set_private_data(&view->private_store, guid, data_size, data); +} + +static HRESULT STDMETHODCALLTYPE d3d11_unordered_access_view_SetPrivateDataInterface(ID3D11UnorderedAccessView *iface, + REFGUID guid, const IUnknown *data) +{ + struct d3d11_unordered_access_view *view = impl_from_ID3D11UnorderedAccessView(iface); + + TRACE("iface %p, guid %s, data %p.\n", iface, debugstr_guid(guid), data); + + return d3d_set_private_data_interface(&view->private_store, guid, data); +} + +static void STDMETHODCALLTYPE d3d11_unordered_access_view_GetResource(ID3D11UnorderedAccessView *iface, + ID3D11Resource **resource) +{ + struct d3d11_unordered_access_view *view = impl_from_ID3D11UnorderedAccessView(iface); + + TRACE("iface %p, resource %p.\n", iface, resource); + + ID3D11Resource_AddRef(*resource = view->resource); +} + +static void STDMETHODCALLTYPE d3d11_unordered_access_view_GetDesc(ID3D11UnorderedAccessView *iface, + D3D11_UNORDERED_ACCESS_VIEW_DESC *desc) +{ + struct d3d11_unordered_access_view *view = impl_from_ID3D11UnorderedAccessView(iface); + + TRACE("iface %p, desc %p.\n", iface, desc); + + *desc = view->desc; +} + +static const struct ID3D11UnorderedAccessViewVtbl d3d11_unordered_access_view_vtbl = +{ + /* IUnknown methods */ + d3d11_unordered_access_view_QueryInterface, + d3d11_unordered_access_view_AddRef, + d3d11_unordered_access_view_Release, + /* ID3D11DeviceChild methods */ + d3d11_unordered_access_view_GetDevice, + d3d11_unordered_access_view_GetPrivateData, + d3d11_unordered_access_view_SetPrivateData, + d3d11_unordered_access_view_SetPrivateDataInterface, + /* ID3D11View methods */ + d3d11_unordered_access_view_GetResource, + /* ID3D11UnorderedAccessView methods */ + d3d11_unordered_access_view_GetDesc, +}; + +static HRESULT wined3d_unordered_access_view_desc_from_d3d11(struct wined3d_unordered_access_view_desc *wined3d_desc, + const D3D11_UNORDERED_ACCESS_VIEW_DESC *desc) +{ + wined3d_desc->format_id = wined3dformat_from_dxgi_format(desc->Format); + wined3d_desc->flags = 0; + + switch (desc->ViewDimension) + { + case D3D11_UAV_DIMENSION_BUFFER: + wined3d_desc->flags = desc->u.Buffer.Flags; + wined3d_desc->u.buffer.start_idx = desc->u.Buffer.FirstElement; + wined3d_desc->u.buffer.count = desc->u.Buffer.NumElements; + break; + + case D3D11_UAV_DIMENSION_TEXTURE1D: + wined3d_desc->u.texture.level_idx = desc->u.Texture1D.MipSlice; + wined3d_desc->u.texture.layer_idx = 0; + wined3d_desc->u.texture.layer_count = 1; + break; + + case D3D11_UAV_DIMENSION_TEXTURE1DARRAY: + wined3d_desc->flags = WINED3D_VIEW_TEXTURE_ARRAY; + wined3d_desc->u.texture.level_idx = desc->u.Texture1DArray.MipSlice; + wined3d_desc->u.texture.layer_idx = desc->u.Texture1DArray.FirstArraySlice; + wined3d_desc->u.texture.layer_count = desc->u.Texture1DArray.ArraySize; + break; + + case D3D11_UAV_DIMENSION_TEXTURE2D: + wined3d_desc->u.texture.level_idx = desc->u.Texture2D.MipSlice; + wined3d_desc->u.texture.layer_idx = 0; + wined3d_desc->u.texture.layer_count = 1; + break; + + case D3D11_UAV_DIMENSION_TEXTURE2DARRAY: + wined3d_desc->flags = WINED3D_VIEW_TEXTURE_ARRAY; + wined3d_desc->u.texture.level_idx = desc->u.Texture2DArray.MipSlice; + wined3d_desc->u.texture.layer_idx = desc->u.Texture2DArray.FirstArraySlice; + wined3d_desc->u.texture.layer_count = desc->u.Texture2DArray.ArraySize; + break; + + case D3D11_UAV_DIMENSION_TEXTURE3D: + wined3d_desc->u.texture.level_idx = desc->u.Texture3D.MipSlice; + wined3d_desc->u.texture.layer_idx = desc->u.Texture3D.FirstWSlice; + wined3d_desc->u.texture.layer_count = desc->u.Texture3D.WSize; + break; + + default: + WARN("Unrecognized view dimension %#x.\n", desc->ViewDimension); + return E_FAIL; + } + + return S_OK; +} + +static HRESULT d3d11_unordered_access_view_init(struct d3d11_unordered_access_view *view, + struct d3d_device *device, ID3D11Resource *resource, const D3D11_UNORDERED_ACCESS_VIEW_DESC *desc) +{ + struct wined3d_unordered_access_view_desc wined3d_desc; + struct wined3d_resource *wined3d_resource; + HRESULT hr; + + view->ID3D11UnorderedAccessView_iface.lpVtbl = &d3d11_unordered_access_view_vtbl; + view->refcount = 1; + + if (!desc) + { + hr = set_uav_desc_from_resource(&view->desc, resource); + } + else + { + view->desc = *desc; + hr = normalize_uav_desc(&view->desc, resource); + } + if (FAILED(hr)) + return hr; + + if (FAILED(hr = wined3d_unordered_access_view_desc_from_d3d11(&wined3d_desc, &view->desc))) + return hr; + + wined3d_mutex_lock(); + if (!(wined3d_resource = wined3d_resource_from_d3d11_resource(resource))) + { + wined3d_mutex_unlock(); + ERR("Failed to get wined3d resource for d3d11 resource %p.\n", resource); + return E_FAIL; + } + + if (FAILED(hr = wined3d_unordered_access_view_create(&wined3d_desc, wined3d_resource, + view, &d3d_null_wined3d_parent_ops, &view->wined3d_view))) + { + wined3d_mutex_unlock(); + WARN("Failed to create wined3d unordered access view, hr %#x.\n", hr); + return hr; + } + + wined3d_private_store_init(&view->private_store); + wined3d_mutex_unlock(); + ID3D11Resource_AddRef(view->resource = resource); + ID3D11Device_AddRef(view->device = &device->ID3D11Device_iface); + + return S_OK; +} + +HRESULT d3d11_unordered_access_view_create(struct d3d_device *device, ID3D11Resource *resource, + const D3D11_UNORDERED_ACCESS_VIEW_DESC *desc, struct d3d11_unordered_access_view **view) +{ + struct d3d11_unordered_access_view *object; + HRESULT hr; + + if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object)))) + return E_OUTOFMEMORY; + + if (FAILED(hr = d3d11_unordered_access_view_init(object, device, resource, desc))) + { + WARN("Failed to initialize unordered access view, hr %#x.\n", hr); + HeapFree(GetProcessHeap(), 0, object); + return hr; + } + + TRACE("Created unordered access view %p.\n", object); + *view = object; + + return S_OK; +}