diff --git a/runtime/shims/d3d12_com.h b/runtime/shims/d3d12_com.h index 0d76ae44ac..c5dd67fa80 100644 --- a/runtime/shims/d3d12_com.h +++ b/runtime/shims/d3d12_com.h @@ -136,6 +136,10 @@ struct VulkanDeviceInfo { // VK_EXT_conservative_rasterization is enabled on the logical device. Tier 1/2 depend on the physical // precision/overestimation properties; Tier 2 also requires post-snap degenerate triangles. bool conservative_rasterization = false; + // VK_EXT_line_rasterization features enabled on the logical device. + bool line_bresenham = false; + bool line_smooth = false; + bool line_rectangular = false; // Opt in only for shader libraries whose InnerCoverage input contract uses FullyCoveredEXT. The device // still checks physical Tier 2 and fullyCoveredFragmentShaderInputVariable properties. The standalone // device leaves this false until the Xbox PS input mapping is confirmed (synthetic GLSL is not that ABI). diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 12494d201d..9e7876c1ba 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -6640,7 +6640,9 @@ leonida::cached_pso::Key pipeline_key(const D3D12_COMPUTE_PIPELINE_STATE_DESC& d return leonida::sha256_hex(key.data()); } -leonida::cached_pso::Key pipeline_key(const D3D12_GRAPHICS_PIPELINE_STATE_DESC& desc) { +leonida::cached_pso::Key pipeline_key(const D3D12_GRAPHICS_PIPELINE_STATE_DESC& desc, int line_mode = -1, + const D3D12_SHADER_BYTECODE* ms = nullptr, + const D3D12_SHADER_BYTECODE* as = nullptr) { leonida::cached_pso::KeyBuilder key; key.tag("graphics"); key_root_signature(key, desc.pRootSignature); @@ -6649,6 +6651,8 @@ leonida::cached_pso::Key pipeline_key(const D3D12_GRAPHICS_PIPELINE_STATE_DESC& key_shader(key, "ds", desc.DS); key_shader(key, "hs", desc.HS); key_shader(key, "gs", desc.GS); + if (ms && ms->BytecodeLength) key_shader(key, "ms", *ms); + if (as && as->BytecodeLength) key_shader(key, "as", *as); const D3D12_STREAM_OUTPUT_DESC& so = desc.StreamOutput; key.u32(so.pSODeclaration ? so.NumEntries : 0); for (UINT i = 0; so.pSODeclaration && i < so.NumEntries; ++i) { @@ -6672,6 +6676,7 @@ leonida::cached_pso::Key pipeline_key(const D3D12_GRAPHICS_PIPELINE_STATE_DESC& key.u32(r.FillMode).u32(r.CullMode).u32(r.FrontCounterClockwise).u32(std::uint32_t(r.DepthBias)).u32(bias_clamp); key.u32(slope).u32(r.DepthClipEnable).u32(r.MultisampleEnable).u32(r.AntialiasedLineEnable); key.u32(r.ForcedSampleCount).u32(r.ConservativeRaster); + if (line_mode >= 0) key.tag("line_rasterization_mode").u32(std::uint32_t(line_mode)); const D3D12_DEPTH_STENCIL_DESC& d = desc.DepthStencilState; key.u32(d.DepthEnable).u32(d.DepthWriteMask).u32(d.DepthFunc).u32(d.StencilEnable).u32(d.StencilReadMask); key.u32(d.StencilWriteMask); @@ -6740,6 +6745,7 @@ struct PipelineStream { D3D12_COMPUTE_PIPELINE_STATE_DESC compute{}; D3D12_SHADER_BYTECODE ms{}; D3D12_SHADER_BYTECODE as{}; + int line_mode = -1; // Only RASTERIZER2 has an explicit line mode. bool has_cs = false; bool has_graphics_stage = false; // VS, PS, DS, HS, GS, AS or MS }; @@ -6924,6 +6930,7 @@ HRESULT parse_pipeline_stream(const D3D12_PIPELINE_STATE_STREAM_DESC& desc, Pipe r = {d.FillMode, d.CullMode, d.FrontCounterClockwise, 0, d.DepthBiasClamp, d.SlopeScaledDepthBias, d.DepthClipEnable, FALSE, FALSE, d.ForcedSampleCount, d.ConservativeRaster}; bias = d.DepthBias; + out.line_mode = int(d.LineRasterizationMode); } r.DepthBias = INT(bias); if (ok && FLOAT(r.DepthBias) != bias) log_once("a fractional depth bias is truncated; bias x1000", int(bias * 1000)); @@ -7116,7 +7123,8 @@ class PipelineState final if (seen.insert(line).second) std::fprintf(stderr, "%s\n", line); } HRESULT create_graphics(const D3D12_GRAPHICS_PIPELINE_STATE_DESC& desc, const D3D12_SHADER_BYTECODE* ms = nullptr, - const D3D12_SHADER_BYTECODE* as = nullptr, const StencilFaceMasks* back_masks = nullptr) { + const D3D12_SHADER_BYTECODE* as = nullptr, const StencilFaceMasks* back_masks = nullptr, + int line_mode = -1) { auto& vk = device_.context->vk; const VkDevice dev = device_.info.device; const bool mesh = ms && ms->pShaderBytecode && ms->BytecodeLength; @@ -7772,6 +7780,7 @@ class PipelineState final recipe_.assembly = assembly; recipe_.raster = raster; recipe_.conservative = r.ConservativeRaster == D3D12_CONSERVATIVE_RASTERIZATION_MODE_ON; + recipe_.line_mode = line_mode; recipe_.multisample = multisample; graphics_.samples = samples; recipe_.sample_mask = sample_mask; @@ -8136,6 +8145,7 @@ class PipelineState final VkPipelineInputAssemblyStateCreateInfo assembly{}; VkPipelineRasterizationStateCreateInfo raster{}; bool conservative = false; + int line_mode = -1; VkPipelineMultisampleStateCreateInfo multisample{}; VkSampleMask sample_mask = ~0u; VkPipelineDepthStencilStateCreateInfo depth{}; @@ -8187,6 +8197,29 @@ class PipelineState final conservative.pNext = raster.pNext; raster.pNext = &conservative; } + VkPipelineRasterizationLineStateCreateInfoEXT line{VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_LINE_STATE_CREATE_INFO_EXT}; + // Vulkan has no narrow quadrilateral mode; rectangular is the closest available approximation. Both + // QUADRILATERAL_WIDE and QUADRILATERAL_NARROW therefore become a rectangular line of the default width 1, + // so WIDE is not wider than NARROW here. + bool use_line = false; + switch (r.line_mode) { + case D3D12_LINE_RASTERIZATION_MODE_ALIASED: + line.lineRasterizationMode = VK_LINE_RASTERIZATION_MODE_BRESENHAM_EXT; + use_line = device_.info.line_bresenham && !multisample.alphaToCoverageEnable; break; + case D3D12_LINE_RASTERIZATION_MODE_ALPHA_ANTIALIASED: + line.lineRasterizationMode = VK_LINE_RASTERIZATION_MODE_RECTANGULAR_SMOOTH_EXT; + use_line = device_.info.line_smooth && !multisample.alphaToCoverageEnable; break; + case D3D12_LINE_RASTERIZATION_MODE_QUADRILATERAL_WIDE: + case D3D12_LINE_RASTERIZATION_MODE_QUADRILATERAL_NARROW: + line.lineRasterizationMode = VK_LINE_RASTERIZATION_MODE_RECTANGULAR_EXT; + use_line = device_.info.line_rectangular; break; + default: break; + } + if (use_line) { + line.stippledLineEnable = VK_FALSE; + line.pNext = raster.pNext; + raster.pNext = &line; + } multisample.pSampleMask = &r.sample_mask; VkPipelineColorBlendStateCreateInfo blend = r.blend; blend.attachmentCount = std::uint32_t(r.blends.size()); @@ -12094,11 +12127,14 @@ class Device final pipeline->attach_device(identity()); const HRESULT hr = stream.has_cs ? pipeline->create_compute(stream.compute) : pipeline->create_graphics(stream.graphics, &stream.ms, &stream.as, - stream.back_masks ? &stream.back_stencil : nullptr); + stream.back_masks ? &stream.back_stencil : nullptr, + stream.line_mode); if (FAILED(hr)) { pipeline->Release(); return finish(hr); } + if (!stream.has_cs && stream.line_mode >= 0) + pipeline->set_cache_key(pipeline_key(stream.graphics, stream.line_mode, &stream.ms, &stream.as)); const HRESULT result = hand_out(pipeline, riid, out); if (SUCCEEDED(result) && stream.ms.pShaderBytecode) d3d12_trace::record("PipelineStream::Mesh", "accepted"); return finish(result); @@ -16186,6 +16222,24 @@ HRESULT create_vulkan_device_standalone(REFIID riid, void** out, QueueTestSettin info.conservative_rasterization = std::any_of(extensions.begin(), extensions.end(), [](const auto& e) { return std::strcmp(e.extensionName, VK_EXT_CONSERVATIVE_RASTERIZATION_EXTENSION_NAME) == 0; }); + const bool has_line_rasterization = std::any_of(extensions.begin(), extensions.end(), [](const auto& e) { + return std::strcmp(e.extensionName, VK_EXT_LINE_RASTERIZATION_EXTENSION_NAME) == 0; + }); + VkPhysicalDeviceLineRasterizationFeaturesEXT line_features{ + VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_LINE_RASTERIZATION_FEATURES_EXT}; + if (has_line_rasterization) { + VkPhysicalDeviceFeatures2 query{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2, &line_features}; + vkGetPhysicalDeviceFeatures2(info.physical, &query); + info.line_bresenham = line_features.bresenhamLines == VK_TRUE; + info.line_smooth = line_features.smoothLines == VK_TRUE; + info.line_rectangular = line_features.rectangularLines == VK_TRUE; + // Stippled lines are not used by this layer. + line_features.stippledRectangularLines = VK_FALSE; + line_features.stippledBresenhamLines = VK_FALSE; + line_features.stippledSmoothLines = VK_FALSE; + line_features.pNext = *v13.next(); + *v13.next() = &line_features; + } const bool has_host_image_copy = std::any_of(extensions.begin(), extensions.end(), [](const auto& e) { return std::strcmp(e.extensionName, VK_EXT_HOST_IMAGE_COPY_EXTENSION_NAME) == 0; }); @@ -16483,6 +16537,7 @@ HRESULT create_vulkan_device_standalone(REFIID riid, void** out, QueueTestSettin if (trace_pipeline_compile() && !v13.core && info.pipeline_creation_feedback) device_extensions.push_back(VK_EXT_PIPELINE_CREATION_FEEDBACK_EXTENSION_NAME); if (info.conservative_rasterization) device_extensions.push_back(VK_EXT_CONSERVATIVE_RASTERIZATION_EXTENSION_NAME); + if (has_line_rasterization) device_extensions.push_back(VK_EXT_LINE_RASTERIZATION_EXTENSION_NAME); if (info.image_view_min_lod) device_extensions.push_back(VK_EXT_IMAGE_VIEW_MIN_LOD_EXTENSION_NAME); if (info.vertex_attribute_divisor) device_extensions.push_back(divisor_extension); if (info.conditional_rendering) device_extensions.push_back(conditional_extension); diff --git a/runtime/test/d3d12_draw_test.cpp b/runtime/test/d3d12_draw_test.cpp index d107161584..3d7bf607c9 100644 --- a/runtime/test/d3d12_draw_test.cpp +++ b/runtime/test/d3d12_draw_test.cpp @@ -75,6 +75,239 @@ static_assert(offsetof(Vertex, marker) == 12); static_assert(offsetof(Vertex, narrow_color) == 14); static_assert(offsetof(Vertex, wrong_color) == 16); +template +struct alignas(void*) StreamObject { + D3D12_PIPELINE_STATE_SUBOBJECT_TYPE type = Type; + T value{}; +}; + +// Two diagonal LINE_LIST segments per D3D12_LINE_RASTERIZATION_MODE, drawn through a RASTERIZER2 pipeline state +// stream into a 64x64 RGBA8 target cleared blue and read back. Only what real hardware and the spec guarantee is +// checked, not exact pixels (WARP rasterizes the anti-aliased modes differently): every mode creates a pipeline and +// draws; ALIASED leaves each pixel fully covered or untouched; WIDE and NARROW both cover pixels; nothing lands far +// from the segments. With line_features false the layer is told the device has no VK_EXT_line_rasterization +// feature, so the same draws must still work through the ordinary raster state. WIDE and NARROW are both a +// rectangular line of width 1 in the layer, so their coverage is not compared with each other. +int run_line_modes(const vktest::Device& d, const std::string& shader_dir, const std::vector& vs_container, + const std::vector& ps_container, bool line_features) { + constexpr UINT size = 64; + rt::d3d12::VulkanDeviceInfo info{}; + info.get_instance_proc = d.instance_proc; + info.instance = d.instance; + info.physical = d.physical; + info.device = d.device; + info.queue = d.queue; + info.queue_family = d.family; + info.buffer_device_address = true; + info.subgroup = d.subgroup; + info.shader_dir = shader_dir; + info.vertex_attribute_divisor = d.vertex_attribute_divisor; + info.line_bresenham = line_features && d.line_bresenham; + info.line_smooth = line_features && d.line_smooth; + info.line_rectangular = line_features && d.line_rectangular; + ID3D12Device* inner = nullptr; + EXPECT(SUCCEEDED(rt::d3d12::create_vulkan_device(info, IID_PPV_ARGS(&inner)))); + Ref device; + EXPECT(SUCCEEDED(rt::d3d12_xs::create_device_proxy(inner, __uuidof(ID3D12Device), + reinterpret_cast(&device.p)))); + rt::d3d12::set_device_outer(inner, device.p); + Ref device2; + EXPECT(SUCCEEDED(device->QueryInterface(IID_PPV_ARGS(&device2.p)))); + + D3D12_ROOT_PARAMETER params[2] = {}; + params[0].ParameterType = D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS; + params[0].Constants = {0, 0, 2}; + params[1].ParameterType = D3D12_ROOT_PARAMETER_TYPE_CBV; + params[1].Descriptor = {1, 0}; + D3D12_VERSIONED_ROOT_SIGNATURE_DESC root_desc{}; + root_desc.Version = D3D_ROOT_SIGNATURE_VERSION_1_0; + root_desc.Desc_1_0 = {2, params, 0, nullptr, D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT}; + const auto root_blob = serialize_root_signature(root_desc); + EXPECT(!root_blob.empty()); + Ref root; + EXPECT(SUCCEEDED(device->CreateRootSignature(0, root_blob.data(), root_blob.size(), IID_PPV_ARGS(&root.p)))); + + const D3D12_INPUT_ELEMENT_DESC elements[2] = { + {"POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0}, + {"COLOR", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, D3D12_APPEND_ALIGNED_ELEMENT, + D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0}}; + struct Stream { + StreamObject root; + StreamObject vs; + StreamObject ps; + StreamObject blend; + StreamObject sample_mask; + StreamObject raster; + StreamObject layout; + StreamObject topology; + StreamObject targets; + StreamObject samples; + } stream{}; + stream.root.value = root.p; + stream.vs.value = {vs_container.data(), vs_container.size()}; + stream.ps.value = {ps_container.data(), ps_container.size()}; + stream.blend.value.RenderTarget[0].RenderTargetWriteMask = D3D12_COLOR_WRITE_ENABLE_ALL; + stream.sample_mask.value = 0xffffffffu; + stream.raster.value.FillMode = D3D12_FILL_MODE_SOLID; + stream.raster.value.CullMode = D3D12_CULL_MODE_NONE; + stream.raster.value.DepthClipEnable = TRUE; + stream.layout.value = {elements, 2}; + stream.topology.value = D3D12_PRIMITIVE_TOPOLOGY_TYPE_LINE; + stream.targets.value.NumRenderTargets = 1; + stream.targets.value.RTFormats[0] = DXGI_FORMAT_R8G8B8A8_UNORM; + stream.samples.value.Count = 1; + + // Two segments along the pixel diagonal row == col: (3.2, 3.2) to (28.8, 28.8) and (35.2, 35.2) to (60.8, 60.8). + const Vertex vertices[4] = {{-0.9f, 0.9f, 255, 0, 0, 255}, {-0.1f, 0.1f, 255, 0, 0, 255}, + {0.1f, -0.1f, 255, 0, 0, 255}, {0.9f, -0.9f, 255, 0, 0, 255}}; + const float tint[3] = {1, 0, 1}; // red stays red, alpha 1 + const D3D12_HEAP_PROPERTIES default_heap{D3D12_HEAP_TYPE_DEFAULT}, upload_heap{D3D12_HEAP_TYPE_UPLOAD}, + readback_heap{D3D12_HEAP_TYPE_READBACK}; + Ref upload, readback, target; + const auto upload_desc = buffer_desc(4096); + EXPECT(SUCCEEDED(device->CreateCommittedResource(&upload_heap, D3D12_HEAP_FLAG_NONE, &upload_desc, + D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, + IID_PPV_ARGS(&upload.p)))); + const auto readback_desc = buffer_desc(size * 256); + EXPECT(SUCCEEDED(device->CreateCommittedResource(&readback_heap, D3D12_HEAP_FLAG_NONE, &readback_desc, + D3D12_RESOURCE_STATE_COPY_DEST, nullptr, + IID_PPV_ARGS(&readback.p)))); + D3D12_RESOURCE_DESC rt_desc{}; + rt_desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D; + rt_desc.Width = rt_desc.Height = size; + rt_desc.DepthOrArraySize = 1; + rt_desc.MipLevels = 1; + rt_desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; + rt_desc.SampleDesc.Count = 1; + rt_desc.Flags = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET; + EXPECT(SUCCEEDED(device->CreateCommittedResource(&default_heap, D3D12_HEAP_FLAG_NONE, &rt_desc, + D3D12_RESOURCE_STATE_COMMON, nullptr, IID_PPV_ARGS(&target.p)))); + std::uint8_t* up = nullptr; + EXPECT(SUCCEEDED(upload->Map(0, nullptr, reinterpret_cast(&up)))); + std::memcpy(up, vertices, sizeof(vertices)); + std::memcpy(up + 256, tint, sizeof(tint)); + upload->Unmap(0, nullptr); + const D3D12_GPU_VIRTUAL_ADDRESS upload_va = upload->GetGPUVirtualAddress(); + + const D3D12_DESCRIPTOR_HEAP_DESC heap_desc{D3D12_DESCRIPTOR_HEAP_TYPE_RTV, 1, D3D12_DESCRIPTOR_HEAP_FLAG_NONE, 0}; + Ref rtv_heap; + EXPECT(SUCCEEDED(device->CreateDescriptorHeap(&heap_desc, IID_PPV_ARGS(&rtv_heap.p)))); + const D3D12_CPU_DESCRIPTOR_HANDLE rtv{SIZE_T(reinterpret_cast(slot(rtv_heap.p, 9))(rtv_heap.p))}; + device->CreateRenderTargetView(target.p, nullptr, rtv); + D3D12_COMMAND_QUEUE_DESC queue_desc{}; + Ref queue; + Ref allocator; + Ref list; + Ref fence; + EXPECT(SUCCEEDED(device->CreateCommandQueue(&queue_desc, IID_PPV_ARGS(&queue.p)))); + EXPECT(SUCCEEDED(device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&allocator.p)))); + EXPECT(SUCCEEDED(device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, allocator.p, nullptr, + IID_PPV_ARGS(&list.p)))); + EXPECT(SUCCEEDED(device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence.p)))); + EXPECT(SUCCEEDED(list->Close())); + + const D3D12_VIEWPORT viewport{0, 0, float(size), float(size), 0, 1}; + const D3D12_RECT scissor{0, 0, LONG(size), LONG(size)}; + const FLOAT blue[4] = {0, 0, 1, 1}; + const float origin[2] = {0, 0}; + const char* const names[4] = {"aliased", "alpha_antialiased", "quadrilateral_wide", "quadrilateral_narrow"}; + const D3D12_LINE_RASTERIZATION_MODE modes[4] = { + D3D12_LINE_RASTERIZATION_MODE_ALIASED, D3D12_LINE_RASTERIZATION_MODE_ALPHA_ANTIALIASED, + D3D12_LINE_RASTERIZATION_MODE_QUADRILATERAL_WIDE, D3D12_LINE_RASTERIZATION_MODE_QUADRILATERAL_NARROW}; + void* mapped = nullptr; + EXPECT(SUCCEEDED(readback->Map(0, nullptr, &mapped))); + UINT64 fence_value = 0; + std::string summary; + unsigned counts[4] = {}; + bool first = true; + for (int m = 0; m != 4; ++m) { + stream.raster.value.LineRasterizationMode = modes[m]; + const D3D12_PIPELINE_STATE_STREAM_DESC stream_desc{sizeof(stream), &stream}; + Ref pso; + EXPECT(device2->CreatePipelineState(&stream_desc, IID_PPV_ARGS(&pso.p)) == S_OK); + + EXPECT(SUCCEEDED(allocator->Reset()) && SUCCEEDED(list->Reset(allocator.p, pso.p))); + D3D12_RESOURCE_BARRIER to_target = transition(target.p, first ? D3D12_RESOURCE_STATE_COMMON + : D3D12_RESOURCE_STATE_COPY_SOURCE, + D3D12_RESOURCE_STATE_RENDER_TARGET); + list->ResourceBarrier(1, &to_target); + first = false; + list->ClearRenderTargetView(rtv, blue, 0, nullptr); + list->SetGraphicsRootSignature(root.p); + list->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_LINELIST); + const D3D12_VERTEX_BUFFER_VIEW vbv{upload_va, sizeof(vertices), sizeof(Vertex)}; + list->IASetVertexBuffers(0, 1, &vbv); + list->RSSetViewports(1, &viewport); + list->RSSetScissorRects(1, &scissor); + list->OMSetRenderTargets(1, &rtv, FALSE, nullptr); + list->SetGraphicsRoot32BitConstants(0, 2, origin, 0); + list->SetGraphicsRootConstantBufferView(1, upload_va + 256); + list->DrawInstanced(4, 1, 0, 0); + D3D12_RESOURCE_BARRIER to_copy = transition(target.p, D3D12_RESOURCE_STATE_RENDER_TARGET, + D3D12_RESOURCE_STATE_COPY_SOURCE); + list->ResourceBarrier(1, &to_copy); + D3D12_TEXTURE_COPY_LOCATION dst{}; + dst.pResource = readback.p; + dst.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT; + dst.PlacedFootprint = {0, {DXGI_FORMAT_R8G8B8A8_UNORM, size, size, 1, 256}}; + D3D12_TEXTURE_COPY_LOCATION src{}; + src.pResource = target.p; + src.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX; + list->CopyTextureRegion(&dst, 0, 0, 0, &src, nullptr); + EXPECT(list->Close() == S_OK); + ID3D12CommandList* lists[] = {list.p}; + queue->ExecuteCommandLists(1, lists); + EXPECT(rt::d3d12::last_execute_result(queue.p) == leonida::ReplayResult::success); + ++fence_value; + EXPECT(SUCCEEDED(queue->Signal(fence.p, fence_value)) && + SUCCEEDED(fence->SetEventOnCompletion(fence_value, nullptr))); + + unsigned covered = 0, partial = 0, far_touched = 0; + for (UINT row = 0; row < size; ++row) { + for (UINT col = 0; col < size; ++col) { + const std::uint8_t* px = static_cast(mapped) + row * 256 + col * 4; + const bool clear = px[0] == 0 && px[1] == 0 && px[2] == 255 && px[3] == 255; + const bool full = px[0] == 255 && px[1] == 0 && px[2] == 0 && px[3] == 255; + if (clear) continue; + ++covered; + if (!full) ++partial; + const int off = int(col) - int(row); + // Far from both segments, or in the gap between them (rows 30 to 33 on the diagonal). + if (off > 3 || off < -3 || (col >= 30 && col <= 33 && off == 0)) ++far_touched; + } + } + char line[160]; + counts[m] = covered; + std::snprintf(line, sizeof(line), " %s=%u(partial %u)", names[m], covered, partial); + summary += line; + if (covered < 40 || covered > 400 || far_touched != 0 || (modes[m] == D3D12_LINE_RASTERIZATION_MODE_ALIASED && partial != 0)) { + std::fprintf(stderr, "line mode %s: covered %u partial %u far %u (line features %d)\n", names[m], covered, + partial, far_touched, int(line_features)); + return 1; + } + } + readback->Unmap(0, nullptr); + // Smooth lines cover every pixel the one-pixel-wide rectangle touches, more than Bresenham's one per step along + // the major axis. Seen only where the line state really reaches the driver. + if (info.line_smooth && counts[1] <= counts[0]) { + std::fprintf(stderr, "smooth lines covered %u pixels, aliased %u\n", counts[1], counts[0]); + return 1; + } + std::printf("d3d12_draw_test: line modes, line features %s (device bresenham %d smooth %d rectangular %d):%s\n", + line_features ? "on" : "off", int(info.line_bresenham), int(info.line_smooth), + int(info.line_rectangular), summary.c_str()); + for (auto* r : {&upload, &readback, &target}) r->reset(); + rtv_heap.reset(); + list.reset(); + allocator.reset(); + queue.reset(); + fence.reset(); + root.reset(); + device2.reset(); + EXPECT(rt::d3d12_xs::device_proxy_ref_count(device.p) == 1); + return 0; +} + } // namespace int main(int argc, char** argv) { @@ -82,6 +315,7 @@ int main(int argc, char** argv) { const bool title_vertex = argc == 2 && std::strcmp(argv[1], "--title-vertex-window") == 0; const bool title_window = title_index || title_vertex; vktest::Device d; + d.request_line_rasterization = true; // for the line-mode draws at the end const auto device_status = d.initialize(); if (device_status == vktest::DeviceStatus::unavailable) { std::fprintf(stderr, "SKIP: no usable Vulkan 1.3 device\n"); @@ -1389,6 +1623,9 @@ int main(int argc, char** argv) { "pixels from red to magenta)\n", step1_flipped, (size / 2) * (size / 2)); } + // Line modes: once with the device's VK_EXT_line_rasterization features, once told it has none (the fallback). + for (const bool line_features : {true, false}) + if (run_line_modes(d, library.string(), vs_container, ps_container, line_features)) return 1; if (d.validation_errors()) { std::fprintf(stderr, "%u validation errors\n", d.validation_errors()); return 1; diff --git a/runtime/test/d3d12_pso_validation_test.cpp b/runtime/test/d3d12_pso_validation_test.cpp index 89bee7bd26..54085008af 100644 --- a/runtime/test/d3d12_pso_validation_test.cpp +++ b/runtime/test/d3d12_pso_validation_test.cpp @@ -32,6 +32,12 @@ using namespace d3d12test; namespace { +template +struct alignas(void*) StreamObject { + D3D12_PIPELINE_STATE_SUBOBJECT_TYPE type = Type; + T value{}; +}; + struct HostContext { std::string name; Ref device; @@ -202,6 +208,45 @@ int main() { const D3D12_SHADER_BYTECODE layer_ps_uint{ps_uint_container.data(), ps_uint_container.size()}; const D3D12_SHADER_BYTECODE layer_cs{cs_container.data(), cs_container.size()}; + // The cached PSO identity must distinguish each explicit line mode, even when the device + // cannot honour it. In that case creation still succeeds with the ordinary raster state. + { + struct Stream { + StreamObject root; + StreamObject vs; + StreamObject ps; + StreamObject raster; + StreamObject depth; + StreamObject topology; + StreamObject targets; + } stream{}; + stream.root.value = layer_root.p; + stream.vs.value = layer_vs_fs; + stream.ps.value = layer_ps_unorm; + stream.raster.value.FillMode = D3D12_FILL_MODE_SOLID; + stream.raster.value.CullMode = D3D12_CULL_MODE_NONE; + stream.raster.value.DepthClipEnable = TRUE; + stream.topology.value = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE; + stream.targets.value.NumRenderTargets = 1; + stream.targets.value.RTFormats[0] = DXGI_FORMAT_R8G8B8A8_UNORM; + Ref device2; + EXPECT(SUCCEEDED(layer_dev->QueryInterface(IID_PPV_ARGS(&device2.p)))); + std::vector> blobs; + for (int mode = 0; mode != 5; ++mode) { + // 99 cannot be honoured and checks the existing permissive fallback. + stream.raster.value.LineRasterizationMode = D3D12_LINE_RASTERIZATION_MODE(mode == 4 ? 99 : mode); + D3D12_PIPELINE_STATE_STREAM_DESC desc{sizeof(stream), &stream}; + Ref pso; + EXPECT(device2->CreatePipelineState(&desc, IID_PPV_ARGS(&pso.p)) == S_OK); + Ref blob; + EXPECT(pso->GetCachedBlob(&blob.p) == S_OK); + const auto* data = static_cast(blob->GetBufferPointer()); + std::vector bytes(data, data + blob->GetBufferSize()); + for (const auto& previous : blobs) EXPECT(previous != bytes); + blobs.push_back(std::move(bytes)); + } + } + unsigned checked = 0, host_compared = 0; auto check_gfx_parity = [&](const char* label, HRESULT expected, bool use_pos_vs, bool use_uint_ps, diff --git a/runtime/test/vk_test_device.h b/runtime/test/vk_test_device.h index 079707aa90..6686bc0d6b 100644 --- a/runtime/test/vk_test_device.h +++ b/runtime/test/vk_test_device.h @@ -127,6 +127,8 @@ class Device { bool request_buffer_device_address = false; // set before create(): enable bufferDeviceAddress where supported bool buffer_device_address = false; // enabled: buffer() memory and buffers can be addressed (device_address()) bool conservative_rasterization = false; // VK_EXT_conservative_rasterization enabled where available + bool request_line_rasterization = false; // set before create(): enable VK_EXT_line_rasterization where available + bool line_bresenham = false, line_smooth = false, line_rectangular = false; // the line features enabled bool request_mesh_shader = false; // set before create(): enable VK_EXT_mesh_shader (meshShader) where available bool mesh_shader = false; // VK_EXT_mesh_shader and meshShader are enabled bool mesh_shader_queries = false; // the meshShaderQueries feature is enabled on the device @@ -645,6 +647,25 @@ class Device { *v13_.next() = &custom_border; } + // VK_EXT_line_rasterization (Bresenham, smooth and rectangular lines only; no stippling), for the tests that ask. + VkPhysicalDeviceLineRasterizationFeaturesEXT line_features{ + VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_LINE_RASTERIZATION_FEATURES_EXT}; + const bool has_line_rasterization = + request_line_rasterization && std::any_of(extensions.begin(), extensions.end(), [](const auto& e) { + return std::strcmp(e.extensionName, VK_EXT_LINE_RASTERIZATION_EXTENSION_NAME) == 0; + }); + if (has_line_rasterization) { + VkPhysicalDeviceFeatures2 query{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2, &line_features}; + instance_fn("vkGetPhysicalDeviceFeatures2")(physical, &query); + line_bresenham = line_features.bresenhamLines == VK_TRUE; + line_smooth = line_features.smoothLines == VK_TRUE; + line_rectangular = line_features.rectangularLines == VK_TRUE; + line_features.stippledRectangularLines = line_features.stippledBresenhamLines = + line_features.stippledSmoothLines = VK_FALSE; + line_features.pNext = *v13_.next(); + *v13_.next() = &line_features; + } + VkDeviceCreateInfo device_info{VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, &enable12}; device_info.queueCreateInfoCount = std::uint32_t(queue_infos.size()); device_info.pQueueCreateInfos = queue_infos.data(); @@ -714,6 +735,7 @@ class Device { if (shader_fma) device_extensions.push_back(VK_KHR_SHADER_FMA_EXTENSION_NAME); #endif if (custom_border_color) device_extensions.push_back(custom_border_color_name); + if (has_line_rasterization) device_extensions.push_back(VK_EXT_LINE_RASTERIZATION_EXTENSION_NAME); if (mesh_shader) device_extensions.push_back(VK_EXT_MESH_SHADER_EXTENSION_NAME); device_info.enabledExtensionCount = std::uint32_t(device_extensions.size()); device_info.ppEnabledExtensionNames = device_extensions.data();