diff --git a/dlls/d3d11/tests/d3d11.c b/dlls/d3d11/tests/d3d11.c index 453fb524bef..4ac2a70d302 100644 --- a/dlls/d3d11/tests/d3d11.c +++ b/dlls/d3d11/tests/d3d11.c @@ -33,12 +33,72 @@ static const D3D_FEATURE_LEVEL d3d11_feature_levels[] = D3D_FEATURE_LEVEL_9_1 }; +struct vec2 +{ + float x, y; +}; + +struct vec4 +{ + float x, y, z, w; +}; + static ULONG get_refcount(IUnknown *iface) { IUnknown_AddRef(iface); return IUnknown_Release(iface); } +static BOOL compare_color(DWORD c1, DWORD c2, BYTE max_diff) +{ + if (abs((c1 & 0xff) - (c2 & 0xff)) > max_diff) + return FALSE; + c1 >>= 8; c2 >>= 8; + if (abs((c1 & 0xff) - (c2 & 0xff)) > max_diff) + return FALSE; + c1 >>= 8; c2 >>= 8; + if (abs((c1 & 0xff) - (c2 & 0xff)) > max_diff) + return FALSE; + c1 >>= 8; c2 >>= 8; + if (abs((c1 & 0xff) - (c2 & 0xff)) > max_diff) + return FALSE; + return TRUE; +} + +static DWORD get_texture_color(ID3D11Texture2D *src_texture, unsigned int x, unsigned int y) +{ + D3D11_MAPPED_SUBRESOURCE mapped_texture; + D3D11_TEXTURE2D_DESC texture_desc; + ID3D11Texture2D *dst_texture; + ID3D11DeviceContext *context; + ID3D11Device *device; + DWORD color; + HRESULT hr; + + ID3D11Texture2D_GetDevice(src_texture, &device); + ID3D11Device_GetImmediateContext(device, &context); + + ID3D11Texture2D_GetDesc(src_texture, &texture_desc); + texture_desc.Usage = D3D11_USAGE_STAGING; + texture_desc.BindFlags = 0; + texture_desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ; + texture_desc.MiscFlags = 0; + hr = ID3D11Device_CreateTexture2D(device, &texture_desc, NULL, &dst_texture); + ok(SUCCEEDED(hr), "Failed to create texture, hr %#x.\n", hr); + + ID3D11DeviceContext_CopyResource(context, (ID3D11Resource *)dst_texture, (ID3D11Resource *)src_texture); + hr = ID3D11DeviceContext_Map(context, (ID3D11Resource *)dst_texture, 0, D3D11_MAP_READ, 0, &mapped_texture); + ok(SUCCEEDED(hr), "Failed to map texture, hr %#x.\n", hr); + color = *(DWORD *)(((BYTE *)mapped_texture.pData) + mapped_texture.RowPitch * y + x * 4); + ID3D11DeviceContext_Unmap(context, (ID3D11Resource *)dst_texture, 0); + + ID3D11Texture2D_Release(dst_texture); + ID3D11DeviceContext_Release(context); + ID3D11Device_Release(device); + + return color; +} + static ID3D11Device *create_device(const D3D_FEATURE_LEVEL *feature_level) { ID3D11Device *device; @@ -57,6 +117,47 @@ static ID3D11Device *create_device(const D3D_FEATURE_LEVEL *feature_level) return NULL; } +static IDXGISwapChain *create_swapchain(ID3D11Device *device, HWND window, BOOL windowed) +{ + IDXGISwapChain *swapchain; + DXGI_SWAP_CHAIN_DESC desc; + IDXGIDevice *dxgi_device; + IDXGIAdapter *adapter; + IDXGIFactory *factory; + HRESULT hr; + + hr = ID3D11Device_QueryInterface(device, &IID_IDXGIDevice, (void **)&dxgi_device); + ok(SUCCEEDED(hr), "Failed to get DXGI device, hr %#x.\n", hr); + hr = IDXGIDevice_GetAdapter(dxgi_device, &adapter); + ok(SUCCEEDED(hr), "Failed to get adapter, hr %#x.\n", hr); + IDXGIDevice_Release(dxgi_device); + hr = IDXGIAdapter_GetParent(adapter, &IID_IDXGIFactory, (void **)&factory); + ok(SUCCEEDED(hr), "Failed to get factory, hr %#x.\n", hr); + IDXGIAdapter_Release(adapter); + + desc.BufferDesc.Width = 640; + desc.BufferDesc.Height = 480; + desc.BufferDesc.RefreshRate.Numerator = 60; + desc.BufferDesc.RefreshRate.Denominator = 1; + desc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; + desc.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED; + desc.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED; + desc.SampleDesc.Count = 1; + desc.SampleDesc.Quality = 0; + desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; + desc.BufferCount = 1; + desc.OutputWindow = window; + desc.Windowed = windowed; + desc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD; + desc.Flags = 0; + + hr = IDXGIFactory_CreateSwapChain(factory, (IUnknown *)device, &desc, &swapchain); + ok(SUCCEEDED(hr), "Failed to create swapchain, hr %#x.\n", hr); + IDXGIFactory_Release(factory); + + return swapchain; +} + static void test_create_device(void) { D3D_FEATURE_LEVEL feature_level, supported_feature_level; @@ -2443,6 +2544,247 @@ static void test_private_data(void) ok(!refcount, "Test object has %u references left.\n", refcount); } +static void test_il_append_aligned(void) +{ + ID3D11RenderTargetView *backbuffer_rtv; + D3D11_SUBRESOURCE_DATA resource_data; + ID3D11InputLayout *input_layout; + D3D11_BUFFER_DESC buffer_desc; + ID3D11DeviceContext *context; + ID3D11Texture2D *backbuffer; + unsigned int stride, offset; + IDXGISwapChain *swapchain; + ID3D11VertexShader *vs; + ID3D11PixelShader *ps; + ID3D11Device *device; + ID3D11Buffer *vb[3]; + D3D11_VIEWPORT vp; + ULONG refcount; + DWORD color; + HWND window; + HRESULT hr; + + static const D3D11_INPUT_ELEMENT_DESC layout_desc[] = + { + {"COLOR", 2, DXGI_FORMAT_R32G32_FLOAT, 1, D3D11_APPEND_ALIGNED_ELEMENT, + D3D11_INPUT_PER_INSTANCE_DATA, 2}, + {"COLOR", 3, DXGI_FORMAT_R32G32_FLOAT, 2, D3D11_APPEND_ALIGNED_ELEMENT, + D3D11_INPUT_PER_INSTANCE_DATA, 1}, + {"POSITION", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, + D3D11_INPUT_PER_VERTEX_DATA, 0}, + {"COLOR", 0, DXGI_FORMAT_R32G32_FLOAT, 2, D3D11_APPEND_ALIGNED_ELEMENT, + D3D11_INPUT_PER_INSTANCE_DATA, 1}, + {"COLOR", 1, DXGI_FORMAT_R32G32_FLOAT, 1, D3D11_APPEND_ALIGNED_ELEMENT, + D3D11_INPUT_PER_INSTANCE_DATA, 2}, + }; + static const DWORD vs_code[] = + { +#if 0 + struct vs_in + { + float4 position : POSITION; + float2 color_xy : COLOR0; + float2 color_zw : COLOR1; + unsigned int instance_id : SV_INSTANCEID; + }; + + struct vs_out + { + float4 position : SV_POSITION; + float2 color_xy : COLOR0; + float2 color_zw : COLOR1; + }; + + struct vs_out main(struct vs_in i) + { + struct vs_out o; + + o.position = i.position; + o.position.x += i.instance_id * 0.5; + o.color_xy = i.color_xy; + o.color_zw = i.color_zw; + + return o; + } +#endif + 0x43425844, 0x52e3bf46, 0x6300403d, 0x624cffe4, 0xa4fc0013, 0x00000001, 0x00000214, 0x00000003, + 0x0000002c, 0x000000bc, 0x00000128, 0x4e475349, 0x00000088, 0x00000004, 0x00000008, 0x00000068, + 0x00000000, 0x00000000, 0x00000003, 0x00000000, 0x00000f0f, 0x00000071, 0x00000000, 0x00000000, + 0x00000003, 0x00000001, 0x00000303, 0x00000071, 0x00000001, 0x00000000, 0x00000003, 0x00000002, + 0x00000303, 0x00000077, 0x00000000, 0x00000008, 0x00000001, 0x00000003, 0x00000101, 0x49534f50, + 0x4e4f4954, 0x4c4f4300, 0x5300524f, 0x4e495f56, 0x4e415453, 0x44494543, 0xababab00, 0x4e47534f, + 0x00000064, 0x00000003, 0x00000008, 0x00000050, 0x00000000, 0x00000001, 0x00000003, 0x00000000, + 0x0000000f, 0x0000005c, 0x00000000, 0x00000000, 0x00000003, 0x00000001, 0x00000c03, 0x0000005c, + 0x00000001, 0x00000000, 0x00000003, 0x00000001, 0x0000030c, 0x505f5653, 0x5449534f, 0x004e4f49, + 0x4f4c4f43, 0xabab0052, 0x52444853, 0x000000e4, 0x00010040, 0x00000039, 0x0300005f, 0x001010f2, + 0x00000000, 0x0300005f, 0x00101032, 0x00000001, 0x0300005f, 0x00101032, 0x00000002, 0x04000060, + 0x00101012, 0x00000003, 0x00000008, 0x04000067, 0x001020f2, 0x00000000, 0x00000001, 0x03000065, + 0x00102032, 0x00000001, 0x03000065, 0x001020c2, 0x00000001, 0x02000068, 0x00000001, 0x05000056, + 0x00100012, 0x00000000, 0x0010100a, 0x00000003, 0x09000032, 0x00102012, 0x00000000, 0x0010000a, + 0x00000000, 0x00004001, 0x3f000000, 0x0010100a, 0x00000000, 0x05000036, 0x001020e2, 0x00000000, + 0x00101e56, 0x00000000, 0x05000036, 0x00102032, 0x00000001, 0x00101046, 0x00000001, 0x05000036, + 0x001020c2, 0x00000001, 0x00101406, 0x00000002, 0x0100003e, + }; + static const DWORD ps_code[] = + { +#if 0 + struct vs_out + { + float4 position : SV_POSITION; + float2 color_xy : COLOR0; + float2 color_zw : COLOR1; + }; + + float4 main(struct vs_out i) : SV_TARGET + { + return float4(i.color_xy.xy, i.color_zw.xy); + } +#endif + 0x43425844, 0x64e48a09, 0xaa484d46, 0xe40a6e78, 0x9885edf3, 0x00000001, 0x00000118, 0x00000003, + 0x0000002c, 0x00000098, 0x000000cc, 0x4e475349, 0x00000064, 0x00000003, 0x00000008, 0x00000050, + 0x00000000, 0x00000001, 0x00000003, 0x00000000, 0x0000000f, 0x0000005c, 0x00000000, 0x00000000, + 0x00000003, 0x00000001, 0x00000303, 0x0000005c, 0x00000001, 0x00000000, 0x00000003, 0x00000001, + 0x00000c0c, 0x505f5653, 0x5449534f, 0x004e4f49, 0x4f4c4f43, 0xabab0052, 0x4e47534f, 0x0000002c, + 0x00000001, 0x00000008, 0x00000020, 0x00000000, 0x00000000, 0x00000003, 0x00000000, 0x0000000f, + 0x545f5653, 0x45475241, 0xabab0054, 0x52444853, 0x00000044, 0x00000040, 0x00000011, 0x03001062, + 0x00101032, 0x00000001, 0x03001062, 0x001010c2, 0x00000001, 0x03000065, 0x001020f2, 0x00000000, + 0x05000036, 0x001020f2, 0x00000000, 0x00101e46, 0x00000001, 0x0100003e, + }; + static const struct + { + struct vec4 position; + } + stream0[] = + { + {{-1.0f, -1.0f, 0.0f, 1.0f}}, + {{-1.0f, 1.0f, 0.0f, 1.0f}}, + {{-0.5f, -1.0f, 0.0f, 1.0f}}, + {{-0.5f, 1.0f, 0.0f, 1.0f}}, + }; + static const struct + { + struct vec2 color2; + struct vec2 color1; + } + stream1[] = + { + {{0.5f, 0.5f}, {0.0f, 1.0f}}, + {{0.5f, 0.5f}, {1.0f, 1.0f}}, + }; + static const struct + { + struct vec2 color3; + struct vec2 color0; + } + stream2[] = + { + {{0.5f, 0.5f}, {1.0f, 0.0f}}, + {{0.5f, 0.5f}, {0.0f, 1.0f}}, + {{0.5f, 0.5f}, {0.0f, 0.0f}}, + {{0.5f, 0.5f}, {1.0f, 0.0f}}, + }; + static const float red[] = {1.0f, 0.0f, 0.0f, 0.5f}; + + if (!(device = create_device(NULL))) + { + skip("Failed to create device.\n"); + return; + } + window = CreateWindowA("static", "d3d11_test", WS_OVERLAPPEDWINDOW | WS_VISIBLE, + 0, 0, 640, 480, NULL, NULL, NULL, NULL); + swapchain = create_swapchain(device, window, TRUE); + hr = IDXGISwapChain_GetBuffer(swapchain, 0, &IID_ID3D11Texture2D, (void **)&backbuffer); + ok(SUCCEEDED(hr), "Failed to get buffer, hr %#x.\n", hr); + + hr = ID3D11Device_CreateInputLayout(device, layout_desc, sizeof(layout_desc) / sizeof(*layout_desc), + vs_code, sizeof(vs_code), &input_layout); + ok(SUCCEEDED(hr), "Failed to create input layout, hr %#x.\n", hr); + + buffer_desc.ByteWidth = sizeof(stream0); + buffer_desc.Usage = D3D11_USAGE_DEFAULT; + buffer_desc.BindFlags = D3D11_BIND_VERTEX_BUFFER; + buffer_desc.CPUAccessFlags = 0; + buffer_desc.MiscFlags = 0; + buffer_desc.StructureByteStride = 0; + + resource_data.pSysMem = stream0; + resource_data.SysMemPitch = 0; + resource_data.SysMemSlicePitch = 0; + + hr = ID3D11Device_CreateBuffer(device, &buffer_desc, &resource_data, &vb[0]); + ok(SUCCEEDED(hr), "Failed to create vertex buffer, hr %#x.\n", hr); + + buffer_desc.ByteWidth = sizeof(stream1); + resource_data.pSysMem = stream1; + + hr = ID3D11Device_CreateBuffer(device, &buffer_desc, &resource_data, &vb[1]); + ok(SUCCEEDED(hr), "Failed to create vertex buffer, hr %#x.\n", hr); + + buffer_desc.ByteWidth = sizeof(stream2); + resource_data.pSysMem = stream2; + + hr = ID3D11Device_CreateBuffer(device, &buffer_desc, &resource_data, &vb[2]); + ok(SUCCEEDED(hr), "Failed to create vertex buffer, hr %#x.\n", hr); + + hr = ID3D11Device_CreateVertexShader(device, vs_code, sizeof(vs_code), NULL, &vs); + ok(SUCCEEDED(hr), "Failed to create vertex shader, hr %#x.\n", hr); + hr = ID3D11Device_CreatePixelShader(device, ps_code, sizeof(ps_code), NULL, &ps); + ok(SUCCEEDED(hr), "Failed to create pixel shader, hr %#x.\n", hr); + + hr = ID3D11Device_CreateRenderTargetView(device, (ID3D11Resource *)backbuffer, NULL, &backbuffer_rtv); + ok(SUCCEEDED(hr), "Failed to create rendertarget view, hr %#x.\n", hr); + + ID3D11Device_GetImmediateContext(device, &context); + + ID3D11DeviceContext_OMSetRenderTargets(context, 1, &backbuffer_rtv, NULL); + ID3D11DeviceContext_IASetInputLayout(context, input_layout); + ID3D11DeviceContext_IASetPrimitiveTopology(context, D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); + offset = 0; + stride = sizeof(*stream0); + ID3D11DeviceContext_IASetVertexBuffers(context, 0, 1, &vb[0], &stride, &offset); + stride = sizeof(*stream1); + ID3D11DeviceContext_IASetVertexBuffers(context, 1, 1, &vb[1], &stride, &offset); + stride = sizeof(*stream2); + ID3D11DeviceContext_IASetVertexBuffers(context, 2, 1, &vb[2], &stride, &offset); + ID3D11DeviceContext_VSSetShader(context, vs, NULL, 0); + ID3D11DeviceContext_PSSetShader(context, ps, NULL, 0); + + vp.TopLeftX = 0.0f; + vp.TopLeftY = 0.0f; + vp.Width = 640.0f; + vp.Height = 480.0f; + vp.MinDepth = 0.0f; + vp.MaxDepth = 1.0f; + ID3D11DeviceContext_RSSetViewports(context, 1, &vp); + + ID3D11DeviceContext_ClearRenderTargetView(context, backbuffer_rtv, red); + + ID3D11DeviceContext_DrawInstanced(context, 4, 4, 0, 0); + + color = get_texture_color(backbuffer, 80, 240); + ok(compare_color(color, 0xff0000ff, 1), "Got unexpected color 0x%08x.\n", color); + color = get_texture_color(backbuffer, 240, 240); + ok(compare_color(color, 0xff00ff00, 1), "Got unexpected color 0x%08x.\n", color); + color = get_texture_color(backbuffer, 400, 240); + ok(compare_color(color, 0xffff0000, 1), "Got unexpected color 0x%08x.\n", color); + color = get_texture_color(backbuffer, 560, 240); + ok(compare_color(color, 0xffff00ff, 1), "Got unexpected color 0x%08x.\n", color); + + ID3D11PixelShader_Release(ps); + ID3D11VertexShader_Release(vs); + ID3D11Buffer_Release(vb[2]); + ID3D11Buffer_Release(vb[1]); + ID3D11Buffer_Release(vb[0]); + ID3D11InputLayout_Release(input_layout); + ID3D11RenderTargetView_Release(backbuffer_rtv); + ID3D11Texture2D_Release(backbuffer); + IDXGISwapChain_Release(swapchain); + ID3D11DeviceContext_Release(context); + refcount = ID3D11Device_Release(device); + ok(!refcount, "Device has %u references left.\n", refcount); + DestroyWindow(window); +} + static void test_resource_map(void) { D3D11_MAPPED_SUBRESOURCE mapped_subresource; @@ -2607,5 +2949,6 @@ START_TEST(d3d11) test_create_predicate(); test_device_removed_reason(); test_private_data(); + test_il_append_aligned(); test_resource_map(); }