From: Józef Kucia jkucia@codeweavers.com
Signed-off-by: Józef Kucia jkucia@codeweavers.com --- dlls/d3d11/buffer.c | 8 +++-- dlls/d3d11/d3d11_private.h | 4 +++ dlls/d3d11/texture.c | 8 +++-- dlls/d3d11/utils.c | 88 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 104 insertions(+), 4 deletions(-)
diff --git a/dlls/d3d11/buffer.c b/dlls/d3d11/buffer.c index 3ccccc8cc861..ffa95a19396c 100644 --- a/dlls/d3d11/buffer.c +++ b/dlls/d3d11/buffer.c @@ -387,8 +387,12 @@ static const struct wined3d_parent_ops d3d_buffer_wined3d_parent_ops = d3d_buffer_wined3d_object_released, };
-static BOOL validate_buffer_desc(D3D11_BUFFER_DESC *desc) +static BOOL validate_buffer_desc(D3D11_BUFFER_DESC *desc, D3D_FEATURE_LEVEL feature_level) { + if (!validate_d3d11_resource_access_flags(D3D11_RESOURCE_DIMENSION_BUFFER, + desc->Usage, desc->BindFlags, desc->CPUAccessFlags, feature_level)) + return FALSE; + if (desc->MiscFlags & D3D11_RESOURCE_MISC_BUFFER_ALLOW_RAW_VIEWS) { if (desc->MiscFlags & D3D11_RESOURCE_MISC_BUFFER_STRUCTURED) @@ -442,7 +446,7 @@ static HRESULT d3d_buffer_init(struct d3d_buffer *buffer, struct d3d_device *dev buffer->refcount = 1; buffer->desc = *desc;
- if (!validate_buffer_desc(&buffer->desc)) + if (!validate_buffer_desc(&buffer->desc, device->feature_level)) return E_INVALIDARG;
wined3d_desc.byte_width = buffer->desc.ByteWidth; diff --git a/dlls/d3d11/d3d11_private.h b/dlls/d3d11/d3d11_private.h index c286e768d9cb..6702248e6958 100644 --- a/dlls/d3d11/d3d11_private.h +++ b/dlls/d3d11/d3d11_private.h @@ -83,6 +83,10 @@ UINT d3d10_cpu_access_flags_from_d3d11_cpu_access_flags(UINT cpu_access_flags) D UINT d3d11_resource_misc_flags_from_d3d10_resource_misc_flags(UINT resource_misc_flags) DECLSPEC_HIDDEN; UINT d3d10_resource_misc_flags_from_d3d11_resource_misc_flags(UINT resource_misc_flags) DECLSPEC_HIDDEN;
+BOOL validate_d3d11_resource_access_flags(D3D11_RESOURCE_DIMENSION resource_dimension, + D3D11_USAGE usage, UINT bind_flags, UINT cpu_access_flags, + D3D_FEATURE_LEVEL feature_level) DECLSPEC_HIDDEN; + HRESULT d3d_get_private_data(struct wined3d_private_store *store, REFGUID guid, UINT *data_size, void *data) DECLSPEC_HIDDEN; HRESULT d3d_set_private_data(struct wined3d_private_store *store, diff --git a/dlls/d3d11/texture.c b/dlls/d3d11/texture.c index 2644b52dfa25..a35a9ac9a219 100644 --- a/dlls/d3d11/texture.c +++ b/dlls/d3d11/texture.c @@ -456,8 +456,12 @@ static BOOL is_gdi_compatible_texture(const D3D11_TEXTURE2D_DESC *desc) return TRUE; }
-static BOOL validate_texture2d_desc(const D3D11_TEXTURE2D_DESC *desc) +static BOOL validate_texture2d_desc(const D3D11_TEXTURE2D_DESC *desc, D3D_FEATURE_LEVEL feature_level) { + if (!validate_d3d11_resource_access_flags(D3D11_RESOURCE_DIMENSION_TEXTURE2D, + desc->Usage, desc->BindFlags, desc->CPUAccessFlags, feature_level)) + return FALSE; + if (desc->MiscFlags & D3D11_RESOURCE_MISC_TEXTURECUBE && desc->ArraySize < 6) { @@ -492,7 +496,7 @@ HRESULT d3d_texture2d_create(struct d3d_device *device, const D3D11_TEXTURE2D_DE DWORD flags = 0; HRESULT hr;
- if (!validate_texture2d_desc(desc)) + if (!validate_texture2d_desc(desc, device->feature_level)) { WARN("Failed to validate texture desc.\n"); return E_INVALIDARG; diff --git a/dlls/d3d11/utils.c b/dlls/d3d11/utils.c index ae761f448b74..b3894bbfa585 100644 --- a/dlls/d3d11/utils.c +++ b/dlls/d3d11/utils.c @@ -610,6 +610,94 @@ UINT d3d10_resource_misc_flags_from_d3d11_resource_misc_flags(UINT resource_misc return d3d10_resource_misc_flags; }
+static BOOL d3d11_bind_flags_are_gpu_read_only(UINT bind_flags) +{ + static const BOOL read_only_bind_flags = D3D11_BIND_VERTEX_BUFFER + | D3D11_BIND_INDEX_BUFFER | D3D11_BIND_CONSTANT_BUFFER + | D3D11_BIND_SHADER_RESOURCE; + + return !(bind_flags & ~read_only_bind_flags); +} + +BOOL validate_d3d11_resource_access_flags(D3D11_RESOURCE_DIMENSION resource_dimension, + D3D11_USAGE usage, UINT bind_flags, UINT cpu_access_flags, D3D_FEATURE_LEVEL feature_level) +{ + const BOOL is_texture = resource_dimension != D3D11_RESOURCE_DIMENSION_BUFFER; + + switch (usage) + { + case D3D11_USAGE_DEFAULT: + if ((bind_flags == D3D11_BIND_SHADER_RESOURCE && feature_level >= D3D_FEATURE_LEVEL_11_0) + || (is_texture && bind_flags == D3D11_BIND_RENDER_TARGET) + || bind_flags == D3D11_BIND_UNORDERED_ACCESS) + break; + if (cpu_access_flags) + { + WARN("Default resources are not CPU accessible.\n"); + return FALSE; + } + break; + + case D3D11_USAGE_IMMUTABLE: + if (!bind_flags) + { + WARN("Bind flags must be non-zero for immutable resources.\n"); + return FALSE; + } + if (!d3d11_bind_flags_are_gpu_read_only(bind_flags)) + { + WARN("Immutable resources cannot be writable by GPU.\n"); + return FALSE; + } + + if (cpu_access_flags) + { + WARN("Immutable resources are not CPU accessible.\n"); + return FALSE; + } + break; + + case D3D11_USAGE_DYNAMIC: + if (!bind_flags) + { + WARN("Bind flags must be non-zero for dynamic resources.\n"); + return FALSE; + } + if (!d3d11_bind_flags_are_gpu_read_only(bind_flags)) + { + WARN("Dynamic resources cannot be writable by GPU.\n"); + return FALSE; + } + + if (cpu_access_flags != D3D11_CPU_ACCESS_WRITE) + { + WARN("CPU access must be D3D11_CPU_ACCESS_WRITE for dynamic resources.\n"); + return FALSE; + } + break; + + case D3D11_USAGE_STAGING: + if (bind_flags) + { + WARN("Invalid bind flags %#x for staging resources.\n", bind_flags); + return FALSE; + } + + if (!cpu_access_flags) + { + WARN("CPU access must be non-zero for staging resources.\n"); + return FALSE; + } + break; + + default: + WARN("Invalid usage %#x.\n", usage); + return FALSE; + } + + return TRUE; +} + struct wined3d_resource *wined3d_resource_from_d3d11_resource(ID3D11Resource *resource) { D3D11_RESOURCE_DIMENSION dimension;
From: Józef Kucia jkucia@codeweavers.com
Signed-off-by: Józef Kucia jkucia@codeweavers.com ---
Version 3: Mark NO_OVERWRITE for constant buffers and shader access buffers as broken().
--- dlls/d3d11/tests/d3d11.c | 357 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 356 insertions(+), 1 deletion(-)
diff --git a/dlls/d3d11/tests/d3d11.c b/dlls/d3d11/tests/d3d11.c index 2625e08f834b..18b7f4855608 100644 --- a/dlls/d3d11/tests/d3d11.c +++ b/dlls/d3d11/tests/d3d11.c @@ -24,7 +24,7 @@ #include <stdlib.h> #define COBJMACROS #include "initguid.h" -#include "d3d11_1.h" +#include "d3d11_4.h" #include "wine/test.h"
#ifndef ARRAY_SIZE @@ -1092,6 +1092,21 @@ static BOOL is_nvidia_device(ID3D11Device *device) return is_vendor_device(device, 0x10de); }
+static BOOL is_d3d11_2_runtime(ID3D11Device *device) +{ + ID3D11Device2 *device2; + HRESULT hr; + + /* FIXME: Wine doesn't implement required interfaces yet, but we want to test new behavior. */ + if (!strcmp(winetest_platform, "wine")) + return TRUE; + + hr = ID3D11Device_QueryInterface(device, &IID_ID3D11Device2, (void **)&device2); + if (SUCCEEDED(hr)) + ID3D11Device2_Release(device2); + return hr == S_OK; +} + static BOOL check_compute_shaders_via_sm4_support(ID3D11Device *device) { D3D11_FEATURE_DATA_D3D10_X_HARDWARE_OPTIONS options; @@ -1102,6 +1117,13 @@ static BOOL check_compute_shaders_via_sm4_support(ID3D11Device *device) return options.ComputeShaders_Plus_RawAndStructuredBuffers_Via_Shader_4_x; }
+static BOOL is_buffer(ID3D11Resource *resource) +{ + D3D11_RESOURCE_DIMENSION dimension; + ID3D11Resource_GetType(resource, &dimension); + return dimension == D3D11_RESOURCE_DIMENSION_BUFFER; +} + static IDXGISwapChain *create_swapchain(ID3D11Device *device, HWND window, const struct swapchain_desc *swapchain_desc) { @@ -10928,6 +10950,338 @@ static void test_resource_map(void) ok(!refcount, "Device has %u references left.\n", refcount); }
+#define check_resource_cpu_access(a, b, c, d, e) check_resource_cpu_access_(__LINE__, a, b, c, d, e) +static void check_resource_cpu_access_(unsigned int line, ID3D11DeviceContext *context, + ID3D11Resource *resource, D3D11_USAGE usage, UINT bind_flags, UINT cpu_access) +{ + BOOL cpu_write = cpu_access & D3D11_CPU_ACCESS_WRITE; + BOOL cpu_read = cpu_access & D3D11_CPU_ACCESS_READ; + BOOL dynamic = usage == D3D11_USAGE_DYNAMIC; + D3D11_MAPPED_SUBRESOURCE map_desc; + HRESULT hr, expected_hr; + ID3D11Device *device; + + expected_hr = cpu_read ? S_OK : E_INVALIDARG; + hr = ID3D11DeviceContext_Map(context, resource, 0, D3D11_MAP_READ, 0, &map_desc); + todo_wine_if(expected_hr != S_OK) + ok_(__FILE__, line)(hr == expected_hr, "Got hr %#x for READ.\n", hr); + if (SUCCEEDED(hr)) + ID3D11DeviceContext_Unmap(context, resource, 0); + + /* WRITE_DISCARD and WRITE_NO_OVERWRITE are the only allowed options for dynamic resources. */ + expected_hr = !dynamic && cpu_write ? S_OK : E_INVALIDARG; + hr = ID3D11DeviceContext_Map(context, resource, 0, D3D11_MAP_WRITE, 0, &map_desc); + todo_wine_if(expected_hr != S_OK) + ok_(__FILE__, line)(hr == expected_hr, "Got hr %#x for WRITE.\n", hr); + if (SUCCEEDED(hr)) + ID3D11DeviceContext_Unmap(context, resource, 0); + + expected_hr = cpu_read && cpu_write ? S_OK : E_INVALIDARG; + hr = ID3D11DeviceContext_Map(context, resource, 0, D3D11_MAP_READ_WRITE, 0, &map_desc); + todo_wine_if(expected_hr != S_OK) + ok_(__FILE__, line)(hr == expected_hr, "Got hr %#x for READ_WRITE.\n", hr); + if (SUCCEEDED(hr)) + ID3D11DeviceContext_Unmap(context, resource, 0); + + expected_hr = dynamic ? S_OK : E_INVALIDARG; + hr = ID3D11DeviceContext_Map(context, resource, 0, D3D11_MAP_WRITE_DISCARD, 0, &map_desc); + todo_wine_if(expected_hr != S_OK) + ok_(__FILE__, line)(hr == expected_hr, "Got hr %#x for WRITE_DISCARD.\n", hr); + if (SUCCEEDED(hr)) + ID3D11DeviceContext_Unmap(context, resource, 0); + + if (!dynamic) + return; + + ID3D11DeviceContext_GetDevice(context, &device); + + /* WRITE_NO_OVERWRITE is supported only for buffers. */ + expected_hr = is_buffer(resource) ? S_OK : E_INVALIDARG; + hr = ID3D11DeviceContext_Map(context, resource, 0, D3D11_MAP_WRITE_NO_OVERWRITE, 0, &map_desc); + /* D3D11.1 is required for constant and shader buffers. */ + todo_wine_if(expected_hr != S_OK) + ok_(__FILE__, line)(hr == expected_hr + || broken(bind_flags & (D3D11_BIND_CONSTANT_BUFFER | D3D11_BIND_SHADER_RESOURCE)), + "Got hr %#x for WRITE_NO_OVERWRITE.\n", hr); + if (SUCCEEDED(hr)) + ID3D11DeviceContext_Unmap(context, resource, 0); + + ID3D11Device_Release(device); +} + +static void test_resource_access(const D3D_FEATURE_LEVEL feature_level) +{ + D3D11_TEXTURE2D_DESC texture_desc; + struct device_desc device_desc; + D3D11_BUFFER_DESC buffer_desc; + ID3D11DeviceContext *context; + D3D11_SUBRESOURCE_DATA data; + ID3D11Resource *resource; + BOOL required_cpu_access; + BOOL cpu_write, cpu_read; + HRESULT hr, expected_hr; + UINT allowed_cpu_access; + BOOL broken_validation; + ID3D11Device *device; + unsigned int i; + ULONG refcount; + + static const struct + { + D3D11_USAGE usage; + UINT bind_flags; + BOOL is_valid; + UINT allowed_cpu_access; + } + tests[] = + { + /* Default resources cannot be written by CPU. */ + {D3D11_USAGE_DEFAULT, D3D11_BIND_VERTEX_BUFFER, TRUE, 0}, + {D3D11_USAGE_DEFAULT, D3D11_BIND_INDEX_BUFFER, TRUE, 0}, + {D3D11_USAGE_DEFAULT, D3D11_BIND_CONSTANT_BUFFER, TRUE, 0}, + {D3D11_USAGE_DEFAULT, D3D11_BIND_SHADER_RESOURCE, TRUE, 0}, + {D3D11_USAGE_DEFAULT, D3D11_BIND_STREAM_OUTPUT, TRUE, 0}, + {D3D11_USAGE_DEFAULT, D3D11_BIND_RENDER_TARGET, TRUE, 0}, + {D3D11_USAGE_DEFAULT, D3D11_BIND_DEPTH_STENCIL, TRUE, 0}, + {D3D11_USAGE_DEFAULT, D3D11_BIND_UNORDERED_ACCESS, TRUE, 0}, + + /* Immutable resources cannot be written by CPU and GPU. */ + {D3D11_USAGE_IMMUTABLE, 0, FALSE, 0}, + {D3D11_USAGE_IMMUTABLE, D3D11_BIND_VERTEX_BUFFER, TRUE, 0}, + {D3D11_USAGE_IMMUTABLE, D3D11_BIND_INDEX_BUFFER, TRUE, 0}, + {D3D11_USAGE_IMMUTABLE, D3D11_BIND_CONSTANT_BUFFER, TRUE, 0}, + {D3D11_USAGE_IMMUTABLE, D3D11_BIND_SHADER_RESOURCE, TRUE, 0}, + {D3D11_USAGE_IMMUTABLE, D3D11_BIND_STREAM_OUTPUT, FALSE, 0}, + {D3D11_USAGE_IMMUTABLE, D3D11_BIND_RENDER_TARGET, FALSE, 0}, + {D3D11_USAGE_IMMUTABLE, D3D11_BIND_DEPTH_STENCIL, FALSE, 0}, + {D3D11_USAGE_IMMUTABLE, D3D11_BIND_UNORDERED_ACCESS, FALSE, 0}, + + /* Dynamic resources cannot be written by GPU. */ + {D3D11_USAGE_DYNAMIC, 0, FALSE, D3D11_CPU_ACCESS_WRITE}, + {D3D11_USAGE_DYNAMIC, D3D11_BIND_VERTEX_BUFFER, TRUE, D3D11_CPU_ACCESS_WRITE}, + {D3D11_USAGE_DYNAMIC, D3D11_BIND_INDEX_BUFFER, TRUE, D3D11_CPU_ACCESS_WRITE}, + {D3D11_USAGE_DYNAMIC, D3D11_BIND_CONSTANT_BUFFER, TRUE, D3D11_CPU_ACCESS_WRITE}, + {D3D11_USAGE_DYNAMIC, D3D11_BIND_SHADER_RESOURCE, TRUE, D3D11_CPU_ACCESS_WRITE}, + {D3D11_USAGE_DYNAMIC, D3D11_BIND_STREAM_OUTPUT, FALSE, D3D11_CPU_ACCESS_WRITE}, + {D3D11_USAGE_DYNAMIC, D3D11_BIND_RENDER_TARGET, FALSE, D3D11_CPU_ACCESS_WRITE}, + {D3D11_USAGE_DYNAMIC, D3D11_BIND_DEPTH_STENCIL, FALSE, D3D11_CPU_ACCESS_WRITE}, + {D3D11_USAGE_DYNAMIC, D3D11_BIND_UNORDERED_ACCESS, FALSE, D3D11_CPU_ACCESS_WRITE}, + + /* Staging resources support only data transfer. */ + {D3D11_USAGE_STAGING, 0, TRUE, D3D11_CPU_ACCESS_WRITE | D3D11_CPU_ACCESS_READ}, + {D3D11_USAGE_STAGING, D3D11_BIND_VERTEX_BUFFER, FALSE, 0}, + {D3D11_USAGE_STAGING, D3D11_BIND_INDEX_BUFFER, FALSE, 0}, + {D3D11_USAGE_STAGING, D3D11_BIND_CONSTANT_BUFFER, FALSE, 0}, + {D3D11_USAGE_STAGING, D3D11_BIND_SHADER_RESOURCE, FALSE, 0}, + {D3D11_USAGE_STAGING, D3D11_BIND_STREAM_OUTPUT, FALSE, 0}, + {D3D11_USAGE_STAGING, D3D11_BIND_RENDER_TARGET, FALSE, 0}, + {D3D11_USAGE_STAGING, D3D11_BIND_DEPTH_STENCIL, FALSE, 0}, + {D3D11_USAGE_STAGING, D3D11_BIND_UNORDERED_ACCESS, FALSE, 0}, + }; + + device_desc.feature_level = &feature_level; + device_desc.flags = 0; + if (!(device = create_device(&device_desc))) + { + skip("Failed to create device for feature level %#x.\n", feature_level); + return; + } + ID3D11Device_GetImmediateContext(device, &context); + + data.SysMemPitch = 0; + data.SysMemSlicePitch = 0; + data.pSysMem = HeapAlloc(GetProcessHeap(), 0, 10240); + ok(!!data.pSysMem, "Failed to allocate memory.\n"); + + for (i = 0; i < ARRAY_SIZE(tests); ++i) + { + switch (tests[i].bind_flags) + { + case D3D11_BIND_DEPTH_STENCIL: + continue; + + case D3D11_BIND_SHADER_RESOURCE: + case D3D11_BIND_STREAM_OUTPUT: + case D3D11_BIND_RENDER_TARGET: + if (feature_level < D3D_FEATURE_LEVEL_10_0) + continue; + break; + + case D3D11_BIND_UNORDERED_ACCESS: + if (feature_level < D3D_FEATURE_LEVEL_11_0) + continue; + break; + + default: + break; + } + + allowed_cpu_access = tests[i].allowed_cpu_access; + if (feature_level >= D3D_FEATURE_LEVEL_11_0 && is_d3d11_2_runtime(device) + && tests[i].usage == D3D11_USAGE_DEFAULT + && (tests[i].bind_flags == D3D11_BIND_SHADER_RESOURCE + || tests[i].bind_flags == D3D11_BIND_UNORDERED_ACCESS)) + allowed_cpu_access |= D3D11_CPU_ACCESS_WRITE | D3D11_CPU_ACCESS_READ; + + required_cpu_access = tests[i].usage == D3D11_USAGE_DYNAMIC || tests[i].usage == D3D11_USAGE_STAGING; + cpu_write = allowed_cpu_access & D3D11_CPU_ACCESS_WRITE; + cpu_read = allowed_cpu_access & D3D11_CPU_ACCESS_READ; + + buffer_desc.ByteWidth = 1024; + buffer_desc.Usage = tests[i].usage; + buffer_desc.BindFlags = tests[i].bind_flags; + buffer_desc.MiscFlags = 0; + buffer_desc.StructureByteStride = 0; + + buffer_desc.CPUAccessFlags = 0; + expected_hr = tests[i].is_valid && !required_cpu_access ? S_OK : E_INVALIDARG; + hr = ID3D11Device_CreateBuffer(device, &buffer_desc, &data, (ID3D11Buffer **)&resource); + ok(hr == expected_hr, "Got hr %#x, expected %#x, test %u.\n", hr, expected_hr, i); + if (SUCCEEDED(hr)) + { + check_resource_cpu_access(context, resource, + buffer_desc.Usage, buffer_desc.BindFlags, buffer_desc.CPUAccessFlags); + ID3D11Resource_Release(resource); + } + + buffer_desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; + expected_hr = tests[i].is_valid && cpu_write ? S_OK : E_INVALIDARG; + hr = ID3D11Device_CreateBuffer(device, &buffer_desc, &data, (ID3D11Buffer **)&resource); + ok(hr == expected_hr, "Got hr %#x, expected %#x, test %u.\n", hr, expected_hr, i); + if (SUCCEEDED(hr)) + { + check_resource_cpu_access(context, resource, + buffer_desc.Usage, buffer_desc.BindFlags, buffer_desc.CPUAccessFlags); + ID3D11Resource_Release(resource); + } + + buffer_desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ; + expected_hr = tests[i].is_valid && cpu_read ? S_OK : E_INVALIDARG; + hr = ID3D11Device_CreateBuffer(device, &buffer_desc, &data, (ID3D11Buffer **)&resource); + ok(hr == expected_hr, "Got hr %#x, expected %#x, test %u.\n", hr, expected_hr, i); + if (SUCCEEDED(hr)) + { + check_resource_cpu_access(context, resource, + buffer_desc.Usage, buffer_desc.BindFlags, buffer_desc.CPUAccessFlags); + ID3D11Resource_Release(resource); + } + + buffer_desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE | D3D11_CPU_ACCESS_READ; + expected_hr = tests[i].is_valid && cpu_write && cpu_read ? S_OK : E_INVALIDARG; + hr = ID3D11Device_CreateBuffer(device, &buffer_desc, &data, (ID3D11Buffer **)&resource); + ok(hr == expected_hr, "Got hr %#x, expected %#x, test %u.\n", hr, expected_hr, i); + if (SUCCEEDED(hr)) + { + check_resource_cpu_access(context, resource, + buffer_desc.Usage, buffer_desc.BindFlags, buffer_desc.CPUAccessFlags); + ID3D11Resource_Release(resource); + } + } + + data.SysMemPitch = 16; + + for (i = 0; i < ARRAY_SIZE(tests); ++i) + { + switch (tests[i].bind_flags) + { + case D3D11_BIND_VERTEX_BUFFER: + case D3D11_BIND_INDEX_BUFFER: + case D3D11_BIND_CONSTANT_BUFFER: + case D3D11_BIND_STREAM_OUTPUT: + continue; + + case D3D11_BIND_UNORDERED_ACCESS: + if (feature_level < D3D_FEATURE_LEVEL_11_0) + continue; + break; + + default: + break; + } + + broken_validation = tests[i].usage == D3D11_USAGE_DEFAULT + && (tests[i].bind_flags == D3D11_BIND_SHADER_RESOURCE + || tests[i].bind_flags == D3D11_BIND_RENDER_TARGET + || tests[i].bind_flags == D3D11_BIND_UNORDERED_ACCESS); + + required_cpu_access = tests[i].usage == D3D11_USAGE_DYNAMIC || tests[i].usage == D3D11_USAGE_STAGING; + cpu_write = tests[i].allowed_cpu_access & D3D11_CPU_ACCESS_WRITE; + cpu_read = tests[i].allowed_cpu_access & D3D11_CPU_ACCESS_READ; + + texture_desc.Width = 4; + texture_desc.Height = 4; + texture_desc.MipLevels = 1; + texture_desc.ArraySize = 1; + texture_desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; + texture_desc.SampleDesc.Count = 1; + texture_desc.SampleDesc.Quality = 0; + texture_desc.Usage = tests[i].usage; + texture_desc.BindFlags = tests[i].bind_flags; + texture_desc.MiscFlags = 0; + if (tests[i].bind_flags == D3D11_BIND_DEPTH_STENCIL) + texture_desc.Format = DXGI_FORMAT_D16_UNORM; + + texture_desc.CPUAccessFlags = 0; + expected_hr = tests[i].is_valid && !required_cpu_access ? S_OK : E_INVALIDARG; + hr = ID3D11Device_CreateTexture2D(device, &texture_desc, &data, (ID3D11Texture2D **)&resource); + ok(hr == expected_hr, "Got hr %#x, expected %#x, test %u.\n", hr, expected_hr, i); + if (SUCCEEDED(hr)) + { + check_resource_cpu_access(context, resource, + texture_desc.Usage, texture_desc.BindFlags, texture_desc.CPUAccessFlags); + ID3D11Resource_Release(resource); + } + + texture_desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; + expected_hr = tests[i].is_valid && cpu_write ? S_OK : E_INVALIDARG; + hr = ID3D11Device_CreateTexture2D(device, &texture_desc, &data, (ID3D11Texture2D **)&resource); + ok(hr == expected_hr || (hr == S_OK && broken_validation), + "Got hr %#x, expected %#x, test %u.\n", hr, expected_hr, i); + if (SUCCEEDED(hr)) + { + if (broken_validation) + texture_desc.CPUAccessFlags = 0; + check_resource_cpu_access(context, resource, + texture_desc.Usage, texture_desc.BindFlags, texture_desc.CPUAccessFlags); + ID3D11Resource_Release(resource); + } + + texture_desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ; + expected_hr = tests[i].is_valid && cpu_read ? S_OK : E_INVALIDARG; + hr = ID3D11Device_CreateTexture2D(device, &texture_desc, &data, (ID3D11Texture2D **)&resource); + ok(hr == expected_hr || (hr == S_OK && broken_validation), + "Got hr %#x, expected %#x, test %u.\n", hr, expected_hr, i); + if (SUCCEEDED(hr)) + { + if (broken_validation) + texture_desc.CPUAccessFlags = 0; + check_resource_cpu_access(context, resource, + texture_desc.Usage, texture_desc.BindFlags, texture_desc.CPUAccessFlags); + ID3D11Resource_Release(resource); + } + + texture_desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE | D3D11_CPU_ACCESS_READ; + expected_hr = tests[i].is_valid && cpu_write && cpu_read ? S_OK : E_INVALIDARG; + hr = ID3D11Device_CreateTexture2D(device, &texture_desc, &data, (ID3D11Texture2D **)&resource); + ok(hr == expected_hr || (hr == S_OK && broken_validation), + "Got hr %#x, expected %#x, test %u.\n", hr, expected_hr, i); + if (SUCCEEDED(hr)) + { + if (broken_validation) + texture_desc.CPUAccessFlags = 0; + check_resource_cpu_access(context, resource, + texture_desc.Usage, texture_desc.BindFlags, texture_desc.CPUAccessFlags); + ID3D11Resource_Release(resource); + } + } + + HeapFree(GetProcessHeap(), 0, (void *)data.pSysMem); + + ID3D11DeviceContext_Release(context); + refcount = ID3D11Device_Release(device); + ok(!refcount, "Device has %u references left.\n", refcount); +} + static void test_check_multisample_quality_levels(void) { ID3D11Device *device; @@ -24425,6 +24779,7 @@ START_TEST(d3d11) test_update_subresource(); test_copy_subresource_region(); test_resource_map(); + run_for_each_feature_level(test_resource_access); test_check_multisample_quality_levels(); run_for_each_feature_level(test_swapchain_formats); test_swapchain_views();
Signed-off-by: Henri Verbeet hverbeet@codeweavers.com
From: Józef Kucia jkucia@codeweavers.com
Signed-off-by: Józef Kucia jkucia@codeweavers.com ---
Version 3: Mark NO_OVERWRITE for constant buffers and shader access buffers as broken().
--- dlls/d3d10core/tests/device.c | 330 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 330 insertions(+)
diff --git a/dlls/d3d10core/tests/device.c b/dlls/d3d10core/tests/device.c index 86a8f43d4e93..a77f04f36805 100644 --- a/dlls/d3d10core/tests/device.c +++ b/dlls/d3d10core/tests/device.c @@ -8186,6 +8186,335 @@ static void test_copy_subresource_region(void) release_test_context(&test_context); }
+#define check_buffer_cpu_access(a, b, c, d) check_buffer_cpu_access_(__LINE__, a, b, c, d) +static void check_buffer_cpu_access_(unsigned int line, ID3D10Buffer *buffer, + D3D10_USAGE usage, UINT bind_flags, UINT cpu_access) +{ + BOOL cpu_write = cpu_access & D3D10_CPU_ACCESS_WRITE; + BOOL cpu_read = cpu_access & D3D10_CPU_ACCESS_READ; + BOOL dynamic = usage == D3D10_USAGE_DYNAMIC; + HRESULT hr, expected_hr; + ID3D10Device *device; + void *data; + + expected_hr = cpu_read ? S_OK : E_INVALIDARG; + hr = ID3D10Buffer_Map(buffer, D3D10_MAP_READ, 0, &data); + todo_wine_if(expected_hr != S_OK) + ok_(__FILE__, line)(hr == expected_hr, "Got hr %#x for READ.\n", hr); + if (SUCCEEDED(hr)) + ID3D10Buffer_Unmap(buffer); + + expected_hr = !dynamic && cpu_write ? S_OK : E_INVALIDARG; + hr = ID3D10Buffer_Map(buffer, D3D10_MAP_WRITE, 0, &data); + todo_wine_if(expected_hr != S_OK) + ok_(__FILE__, line)(hr == expected_hr, "Got hr %#x for WRITE.\n", hr); + if (SUCCEEDED(hr)) + ID3D10Buffer_Unmap(buffer); + + expected_hr = cpu_read && cpu_write ? S_OK : E_INVALIDARG; + hr = ID3D10Buffer_Map(buffer, D3D10_MAP_READ_WRITE, 0, &data); + todo_wine_if(expected_hr != S_OK) + ok_(__FILE__, line)(hr == expected_hr, "Got hr %#x for READ_WRITE.\n", hr); + if (SUCCEEDED(hr)) + ID3D10Buffer_Unmap(buffer); + + expected_hr = dynamic ? S_OK : E_INVALIDARG; + hr = ID3D10Buffer_Map(buffer, D3D10_MAP_WRITE_DISCARD, 0, &data); + todo_wine_if(expected_hr != S_OK) + ok_(__FILE__, line)(hr == expected_hr, "Got hr %#x for WRITE_DISCARD.\n", hr); + if (SUCCEEDED(hr)) + ID3D10Buffer_Unmap(buffer); + + if (!dynamic) + return; + + ID3D10Buffer_GetDevice(buffer, &device); + + expected_hr = S_OK; + hr = ID3D10Buffer_Map(buffer, D3D10_MAP_WRITE_NO_OVERWRITE, 0, &data); + todo_wine_if(expected_hr != S_OK) + ok_(__FILE__, line)(hr == expected_hr + || broken(bind_flags & (D3D10_BIND_CONSTANT_BUFFER | D3D10_BIND_SHADER_RESOURCE)), + "Got hr %#x for WRITE_NO_OVERWRITE.\n", hr); + if (SUCCEEDED(hr)) + ID3D10Buffer_Unmap(buffer); + + ID3D10Device_Release(device); +} + +#define check_texture_cpu_access(a, b, c, d) check_texture_cpu_access_(__LINE__, a, b, c, d) +static void check_texture_cpu_access_(unsigned int line, ID3D10Texture2D *texture, + D3D10_USAGE usage, UINT bind_flags, UINT cpu_access) +{ + BOOL cpu_write = cpu_access & D3D10_CPU_ACCESS_WRITE; + BOOL cpu_read = cpu_access & D3D10_CPU_ACCESS_READ; + BOOL dynamic = usage == D3D10_USAGE_DYNAMIC; + D3D10_MAPPED_TEXTURE2D map_desc; + HRESULT hr, expected_hr; + + expected_hr = cpu_read ? S_OK : E_INVALIDARG; + hr = ID3D10Texture2D_Map(texture, 0, D3D10_MAP_READ, 0, &map_desc); + todo_wine_if(expected_hr != S_OK) + ok_(__FILE__, line)(hr == expected_hr, "Got hr %#x for READ.\n", hr); + if (SUCCEEDED(hr)) + ID3D10Texture2D_Unmap(texture, 0); + + expected_hr = !dynamic && cpu_write ? S_OK : E_INVALIDARG; + hr = ID3D10Texture2D_Map(texture, 0, D3D10_MAP_WRITE, 0, &map_desc); + todo_wine_if(expected_hr != S_OK) + ok_(__FILE__, line)(hr == expected_hr, "Got hr %#x for WRITE.\n", hr); + if (SUCCEEDED(hr)) + ID3D10Texture2D_Unmap(texture, 0); + + expected_hr = cpu_read && cpu_write ? S_OK : E_INVALIDARG; + hr = ID3D10Texture2D_Map(texture, 0, D3D10_MAP_READ_WRITE, 0, &map_desc); + todo_wine_if(expected_hr != S_OK) + ok_(__FILE__, line)(hr == expected_hr, "Got hr %#x for READ_WRITE.\n", hr); + if (SUCCEEDED(hr)) + ID3D10Texture2D_Unmap(texture, 0); + + expected_hr = dynamic ? S_OK : E_INVALIDARG; + hr = ID3D10Texture2D_Map(texture, 0, D3D10_MAP_WRITE_DISCARD, 0, &map_desc); + todo_wine_if(expected_hr != S_OK) + ok_(__FILE__, line)(hr == expected_hr, "Got hr %#x for WRITE_DISCARD.\n", hr); + if (SUCCEEDED(hr)) + ID3D10Texture2D_Unmap(texture, 0); + + if (!dynamic) + return; + + hr = ID3D10Texture2D_Map(texture, 0, D3D10_MAP_WRITE_NO_OVERWRITE, 0, &map_desc); + todo_wine + ok_(__FILE__, line)(hr == E_INVALIDARG, "Got hr %#x for WRITE_NO_OVERWRITE.\n", hr); + if (SUCCEEDED(hr)) + ID3D10Texture2D_Unmap(texture, 0); +} + +static void test_resource_access(void) +{ + D3D10_TEXTURE2D_DESC texture_desc; + D3D10_BUFFER_DESC buffer_desc; + D3D10_SUBRESOURCE_DATA data; + BOOL cpu_write, cpu_read; + BOOL required_cpu_access; + ID3D10Texture2D *texture; + HRESULT hr, expected_hr; + BOOL broken_validation; + ID3D10Device *device; + ID3D10Buffer *buffer; + unsigned int i; + ULONG refcount; + + static const struct + { + D3D10_USAGE usage; + UINT bind_flags; + BOOL is_valid; + UINT allowed_cpu_access; + } + tests[] = + { + /* Default resources cannot be written by CPU. */ + {D3D10_USAGE_DEFAULT, D3D10_BIND_VERTEX_BUFFER, TRUE, 0}, + {D3D10_USAGE_DEFAULT, D3D10_BIND_INDEX_BUFFER, TRUE, 0}, + {D3D10_USAGE_DEFAULT, D3D10_BIND_CONSTANT_BUFFER, TRUE, 0}, + {D3D10_USAGE_DEFAULT, D3D10_BIND_SHADER_RESOURCE, TRUE, 0}, + {D3D10_USAGE_DEFAULT, D3D10_BIND_STREAM_OUTPUT, TRUE, 0}, + {D3D10_USAGE_DEFAULT, D3D10_BIND_RENDER_TARGET, TRUE, 0}, + {D3D10_USAGE_DEFAULT, D3D10_BIND_DEPTH_STENCIL, TRUE, 0}, + + /* Immutable resources cannot be written by CPU and GPU. */ + {D3D10_USAGE_IMMUTABLE, 0, FALSE, 0}, + {D3D10_USAGE_IMMUTABLE, D3D10_BIND_VERTEX_BUFFER, TRUE, 0}, + {D3D10_USAGE_IMMUTABLE, D3D10_BIND_INDEX_BUFFER, TRUE, 0}, + {D3D10_USAGE_IMMUTABLE, D3D10_BIND_CONSTANT_BUFFER, TRUE, 0}, + {D3D10_USAGE_IMMUTABLE, D3D10_BIND_SHADER_RESOURCE, TRUE, 0}, + {D3D10_USAGE_IMMUTABLE, D3D10_BIND_STREAM_OUTPUT, FALSE, 0}, + {D3D10_USAGE_IMMUTABLE, D3D10_BIND_RENDER_TARGET, FALSE, 0}, + {D3D10_USAGE_IMMUTABLE, D3D10_BIND_DEPTH_STENCIL, FALSE, 0}, + + /* Dynamic resources cannot be written by GPU. */ + {D3D10_USAGE_DYNAMIC, 0, FALSE, D3D10_CPU_ACCESS_WRITE}, + {D3D10_USAGE_DYNAMIC, D3D10_BIND_VERTEX_BUFFER, TRUE, D3D10_CPU_ACCESS_WRITE}, + {D3D10_USAGE_DYNAMIC, D3D10_BIND_INDEX_BUFFER, TRUE, D3D10_CPU_ACCESS_WRITE}, + {D3D10_USAGE_DYNAMIC, D3D10_BIND_CONSTANT_BUFFER, TRUE, D3D10_CPU_ACCESS_WRITE}, + {D3D10_USAGE_DYNAMIC, D3D10_BIND_SHADER_RESOURCE, TRUE, D3D10_CPU_ACCESS_WRITE}, + {D3D10_USAGE_DYNAMIC, D3D10_BIND_STREAM_OUTPUT, FALSE, D3D10_CPU_ACCESS_WRITE}, + {D3D10_USAGE_DYNAMIC, D3D10_BIND_RENDER_TARGET, FALSE, D3D10_CPU_ACCESS_WRITE}, + {D3D10_USAGE_DYNAMIC, D3D10_BIND_DEPTH_STENCIL, FALSE, D3D10_CPU_ACCESS_WRITE}, + + /* Staging resources support only data transfer. */ + {D3D10_USAGE_STAGING, 0, TRUE, D3D10_CPU_ACCESS_WRITE | D3D10_CPU_ACCESS_READ}, + {D3D10_USAGE_STAGING, D3D10_BIND_VERTEX_BUFFER, FALSE, 0}, + {D3D10_USAGE_STAGING, D3D10_BIND_INDEX_BUFFER, FALSE, 0}, + {D3D10_USAGE_STAGING, D3D10_BIND_CONSTANT_BUFFER, FALSE, 0}, + {D3D10_USAGE_STAGING, D3D10_BIND_SHADER_RESOURCE, FALSE, 0}, + {D3D10_USAGE_STAGING, D3D10_BIND_STREAM_OUTPUT, FALSE, 0}, + {D3D10_USAGE_STAGING, D3D10_BIND_RENDER_TARGET, FALSE, 0}, + {D3D10_USAGE_STAGING, D3D10_BIND_DEPTH_STENCIL, FALSE, 0}, + }; + + if (!(device = create_device())) + { + skip("Failed to create device.\n"); + return; + } + + data.SysMemPitch = 0; + data.SysMemSlicePitch = 0; + data.pSysMem = HeapAlloc(GetProcessHeap(), 0, 10240); + ok(!!data.pSysMem, "Failed to allocate memory.\n"); + + for (i = 0; i < ARRAY_SIZE(tests); ++i) + { + if (tests[i].bind_flags == D3D10_BIND_DEPTH_STENCIL) + continue; + + required_cpu_access = tests[i].usage == D3D10_USAGE_DYNAMIC || tests[i].usage == D3D10_USAGE_STAGING; + cpu_write = tests[i].allowed_cpu_access & D3D10_CPU_ACCESS_WRITE; + cpu_read = tests[i].allowed_cpu_access & D3D10_CPU_ACCESS_READ; + + buffer_desc.ByteWidth = 1024; + buffer_desc.Usage = tests[i].usage; + buffer_desc.BindFlags = tests[i].bind_flags; + buffer_desc.MiscFlags = 0; + + buffer_desc.CPUAccessFlags = 0; + expected_hr = tests[i].is_valid && !required_cpu_access ? S_OK : E_INVALIDARG; + hr = ID3D10Device_CreateBuffer(device, &buffer_desc, &data, &buffer); + ok(hr == expected_hr, "Got hr %#x, expected %#x, test %u.\n", hr, expected_hr, i); + if (SUCCEEDED(hr)) + { + check_buffer_cpu_access(buffer, buffer_desc.Usage, + buffer_desc.BindFlags, buffer_desc.CPUAccessFlags); + ID3D10Buffer_Release(buffer); + } + + buffer_desc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE; + expected_hr = tests[i].is_valid && cpu_write ? S_OK : E_INVALIDARG; + hr = ID3D10Device_CreateBuffer(device, &buffer_desc, &data, &buffer); + ok(hr == expected_hr, "Got hr %#x, expected %#x, test %u.\n", hr, expected_hr, i); + if (SUCCEEDED(hr)) + { + check_buffer_cpu_access(buffer, buffer_desc.Usage, + buffer_desc.BindFlags, buffer_desc.CPUAccessFlags); + ID3D10Buffer_Release(buffer); + } + + buffer_desc.CPUAccessFlags = D3D10_CPU_ACCESS_READ; + expected_hr = tests[i].is_valid && cpu_read ? S_OK : E_INVALIDARG; + hr = ID3D10Device_CreateBuffer(device, &buffer_desc, &data, &buffer); + ok(hr == expected_hr, "Got hr %#x, expected %#x, test %u.\n", hr, expected_hr, i); + if (SUCCEEDED(hr)) + { + check_buffer_cpu_access(buffer, buffer_desc.Usage, + buffer_desc.BindFlags, buffer_desc.CPUAccessFlags); + ID3D10Buffer_Release(buffer); + } + + buffer_desc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE | D3D10_CPU_ACCESS_READ; + expected_hr = tests[i].is_valid && cpu_write && cpu_read ? S_OK : E_INVALIDARG; + hr = ID3D10Device_CreateBuffer(device, &buffer_desc, &data, &buffer); + ok(hr == expected_hr, "Got hr %#x, expected %#x, test %u.\n", hr, expected_hr, i); + if (SUCCEEDED(hr)) + { + check_buffer_cpu_access(buffer, buffer_desc.Usage, + buffer_desc.BindFlags, buffer_desc.CPUAccessFlags); + ID3D10Buffer_Release(buffer); + } + } + + data.SysMemPitch = 16; + + for (i = 0; i < ARRAY_SIZE(tests); ++i) + { + if (tests[i].bind_flags == D3D10_BIND_VERTEX_BUFFER + || tests[i].bind_flags == D3D10_BIND_INDEX_BUFFER + || tests[i].bind_flags == D3D10_BIND_CONSTANT_BUFFER + || tests[i].bind_flags == D3D10_BIND_STREAM_OUTPUT) + continue; + + broken_validation = tests[i].usage == D3D10_USAGE_DEFAULT + && (tests[i].bind_flags == D3D10_BIND_SHADER_RESOURCE + || tests[i].bind_flags == D3D10_BIND_RENDER_TARGET); + + required_cpu_access = tests[i].usage == D3D10_USAGE_DYNAMIC || tests[i].usage == D3D10_USAGE_STAGING; + cpu_write = tests[i].allowed_cpu_access & D3D10_CPU_ACCESS_WRITE; + cpu_read = tests[i].allowed_cpu_access & D3D10_CPU_ACCESS_READ; + + texture_desc.Width = 4; + texture_desc.Height = 4; + texture_desc.MipLevels = 1; + texture_desc.ArraySize = 1; + texture_desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; + texture_desc.SampleDesc.Count = 1; + texture_desc.SampleDesc.Quality = 0; + texture_desc.Usage = tests[i].usage; + texture_desc.BindFlags = tests[i].bind_flags; + texture_desc.MiscFlags = 0; + if (tests[i].bind_flags == D3D10_BIND_DEPTH_STENCIL) + texture_desc.Format = DXGI_FORMAT_D16_UNORM; + + texture_desc.CPUAccessFlags = 0; + expected_hr = tests[i].is_valid && !required_cpu_access ? S_OK : E_INVALIDARG; + hr = ID3D10Device_CreateTexture2D(device, &texture_desc, &data, &texture); + ok(hr == expected_hr, "Got hr %#x, expected %#x, test %u.\n", hr, expected_hr, i); + if (SUCCEEDED(hr)) + { + check_texture_cpu_access(texture, texture_desc.Usage, + texture_desc.BindFlags, texture_desc.CPUAccessFlags); + ID3D10Texture2D_Release(texture); + } + + texture_desc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE; + expected_hr = tests[i].is_valid && cpu_write ? S_OK : E_INVALIDARG; + hr = ID3D10Device_CreateTexture2D(device, &texture_desc, &data, &texture); + ok(hr == expected_hr || (hr == S_OK && broken_validation), + "Got hr %#x, expected %#x, test %u.\n", hr, expected_hr, i); + if (SUCCEEDED(hr)) + { + if (broken_validation) + texture_desc.CPUAccessFlags = 0; + check_texture_cpu_access(texture, texture_desc.Usage, + texture_desc.BindFlags, texture_desc.CPUAccessFlags); + ID3D10Texture2D_Release(texture); + } + + texture_desc.CPUAccessFlags = D3D10_CPU_ACCESS_READ; + expected_hr = tests[i].is_valid && cpu_read ? S_OK : E_INVALIDARG; + hr = ID3D10Device_CreateTexture2D(device, &texture_desc, &data, &texture); + ok(hr == expected_hr || (hr == S_OK && broken_validation), + "Got hr %#x, expected %#x, test %u.\n", hr, expected_hr, i); + if (SUCCEEDED(hr)) + { + if (broken_validation) + texture_desc.CPUAccessFlags = 0; + check_texture_cpu_access(texture, texture_desc.Usage, + texture_desc.BindFlags, texture_desc.CPUAccessFlags); + ID3D10Texture2D_Release(texture); + } + + texture_desc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE | D3D10_CPU_ACCESS_READ; + expected_hr = tests[i].is_valid && cpu_write && cpu_read ? S_OK : E_INVALIDARG; + hr = ID3D10Device_CreateTexture2D(device, &texture_desc, &data, &texture); + ok(hr == expected_hr || (hr == S_OK && broken_validation), + "Got hr %#x, expected %#x, test %u.\n", hr, expected_hr, i); + if (SUCCEEDED(hr)) + { + if (broken_validation) + texture_desc.CPUAccessFlags = 0; + check_texture_cpu_access(texture, texture_desc.Usage, + texture_desc.BindFlags, texture_desc.CPUAccessFlags); + ID3D10Texture2D_Release(texture); + } + } + + HeapFree(GetProcessHeap(), 0, (void *)data.pSysMem); + + refcount = ID3D10Device_Release(device); + ok(!refcount, "Device has %u references left.\n", refcount); +} + static void test_check_multisample_quality_levels(void) { ID3D10Device *device; @@ -13882,6 +14211,7 @@ START_TEST(device) test_fragment_coords(); test_update_subresource(); test_copy_subresource_region(); + test_resource_access(); test_check_multisample_quality_levels(); test_cb_relative_addressing(); test_vs_input_relative_addressing();
Signed-off-by: Henri Verbeet hverbeet@codeweavers.com