Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1930,6 +1930,18 @@ if(TARGET compile_dxil AND EXISTS "${DXC_BIN_DIR}/dxcompiler.dll" AND EXISTS "${
"${CMAKE_SOURCE_DIR}/tests/native_direct_container_check.py" --graphics-sampler-pso
"${DXC_BIN_DIR}" "$<TARGET_FILE:native_recording_check>")
set_tests_properties(native_graphics_comparison_pso_warp PROPERTIES SKIP_RETURN_CODE 77)
# Cycle 1 of the global SRV work: ResourceDescriptorHeap[index] from a native DXIL module against a DXC reference.
add_executable(native_heap_handle_probe tests/native_heap_handle_probe.cpp)
target_include_directories(native_heap_handle_probe PRIVATE src)
target_compile_features(native_heap_handle_probe PRIVATE cxx_std_17)
target_link_libraries(native_heap_handle_probe PRIVATE x2w_shader_compiler dxgi d3d12)
set_property(TARGET native_heap_handle_probe PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
foreach(adapter IN ITEMS warp hardware)
add_test(NAME native_heap_handle_probe_${adapter} COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_SOURCE_DIR}/tests/native_heap_handle_probe_check.py" "$<TARGET_FILE:native_heap_handle_probe>"
"${DXC_BIN_DIR}" ${adapter})
set_tests_properties(native_heap_handle_probe_${adapter} PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 120)
endforeach()
add_test(NAME native_recording_pages_warp COMMAND native_recording_check)
add_test(NAME native_dispatch_states_warp COMMAND native_recording_check --subresources)
set_tests_properties(native_dispatch_states_warp PROPERTIES SKIP_RETURN_CODE 77)
Expand Down
2 changes: 1 addition & 1 deletion src/direct_compute_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ NativeComputeShader decode(std::string_view bytes) {
unsigned n = in.count(1000000);
shader.bindings.reserve(n);
for (unsigned j = 0; j < n; ++j) {
auto kind = static_cast<ComputeViewKind>(in.count(static_cast<unsigned>(ComputeViewKind::byte_window_srv)));
auto kind = static_cast<ComputeViewKind>(in.count(static_cast<unsigned>(ComputeViewKind::heap_buffer_srv)));
ComputeViewBinding b{kind, in.word(), in.word(), in.word(), in.word(), in.word(), in.word(), in.count(7)};
b.scalar_table = static_cast<ComputeViewBinding::ScalarTable>(in.count(unsigned(ComputeViewBinding::ScalarTable::srv)));
if (!b.array_count || b.array_position >= b.array_count ||
Expand Down
19 changes: 19 additions & 0 deletions src/dxil_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class DxilModule {
};
std::ostringstream body, setup, globals;
bool uses_wave = false;
bool heap_used = false;
unsigned features = 0;
struct Signature {
Text name;
Expand Down Expand Up @@ -141,6 +142,18 @@ class DxilModule {
<< " })\n";
return h;
}
// A handle taken from the descriptor heap at a run-time index (SM 6.6 ResourceDescriptorHeap[index]), annotated with
// the view class the shader reads: `kind` is the DXIL resource kind (11 raw buffer, 2 Texture2D) and `props` the
// second ResourceProperties word. No resource record is declared: the binding is the whole heap.
Text heap_handle(const Text &index, unsigned kind, unsigned props, bool uav = false) {
heap_used = true;
const Text h = "%heap" + std::to_string(sequence++);
body << " " << h << "b = call %dx.types.Handle @dx.op.createHandleFromHeap(i32 218, i32 " << index
<< ", i1 false, i1 true)\n"
<< " " << h << " = call %dx.types.Handle @dx.op.annotateHandle(i32 216, %dx.types.Handle " << h
<< "b, %dx.types.ResourceProperties { i32 " << (kind + (uav ? 4096 : 0)) << ", i32 " << props << " })\n";
return h;
}
Text finish_compute(unsigned x, unsigned y, unsigned z, const Text &stage = "cs") {
std::ostringstream out;
out << "target datalayout = "
Expand Down Expand Up @@ -256,6 +269,12 @@ class DxilModule {
out << globals.str() << "define void @main() {\nentry:\n" << setup.str() << body.str() << "}\n";
for (auto &[name, decl] : declarations)
out << decl << "\n";
if (heap_used) {
out << "declare %dx.types.Handle @dx.op.createHandleFromHeap(i32,i32,i1,i1) nounwind readnone\n";
if (resources.empty())
out << "declare %dx.types.Handle "
"@dx.op.annotateHandle(i32,%dx.types.Handle,%dx.types.ResourceProperties) nounwind readnone\n";
}
if (!resources.empty())
out << "declare %dx.types.Handle @dx.op.createHandleFromBinding(i32,%dx.types.ResBind,i32,i1) nounwind "
"readnone\n"
Expand Down
5 changes: 4 additions & 1 deletion src/gfx1013_compute_native.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ enum class ComputeViewKind {
cube_comparison_srv,
indexed_atomic_uav,
d16_sample_srv,
byte_window_srv
byte_window_srv,
heap_buffer_srv // A raw buffer read through ResourceDescriptorHeap[slot]; the slot is a GPU value.
};
inline constexpr unsigned compute_resource_heap = ~0u;
// A binding of the whole descriptor heap: it has no table register and no root parameter.
inline bool compute_heap_binding(ComputeViewKind kind) { return kind == ComputeViewKind::heap_buffer_srv; }
inline bool compute_buffer_uav(ComputeViewKind kind) {
return kind == ComputeViewKind::raw_buffer_uav || kind == ComputeViewKind::typed_buffer_uav ||
kind == ComputeViewKind::half_buffer_uav || kind == ComputeViewKind::indexed_atomic_uav;
Expand Down
28 changes: 28 additions & 0 deletions src/gfx1013_direct_compute.inl
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,38 @@ struct DirectCompute {
}
}
}
// A raw buffer SRV chosen by a GPU value in the whole descriptor heap: ResourceDescriptorHeap[slot]. The view class the
// shader reads is the one this handle is annotated with (raw buffer); a slot holding another class is refused by the
// dispatch before the shader runs. The binding names the heap (no table register); the pipeline indexes it directly.
string heap_resource(const Inst &i, const State &s, unsigned base, ComputeViewKind kind, unsigned bytes) {
const auto &symbol = s.at(base);
if (kind != ComputeViewKind::raw_vector_srv && kind != ComputeViewKind::buffer_srv)
refuse(i.pc, "a GPU-selected heap slot is read as a raw buffer SRV only");
if (symbol.offset % 32 || s[base].type != Symbol::Type::indexed_descriptor)
refuse(i.pc, "unaligned heap descriptor");
for (unsigned j = 1; j < 4; ++j) {
Symbol expected = symbol;
expected.offset += 4 * j;
if (!(s.at(base + j) == expected))
refuse(i.pc, "non-contiguous resource descriptor");
}
auto &bindings = analysis.result.bindings;
auto it = std::find_if(bindings.begin(), bindings.end(), [](const auto &b) {
return b.kind == ComputeViewKind::heap_buffer_srv;
});
if (it == bindings.end())
bindings.push_back({ComputeViewKind::heap_buffer_srv, compute_resource_heap, 0, 0, bytes});
else
it->required_bytes = (std::max)(it->required_bytes, bytes);
ir.features |= 16;
return ir.heap_handle(ir.binary("add", read(base), to_string(symbol.offset / 32)), 11, 0);
}
string resource(const Inst &i, const State &s, unsigned base, ComputeViewKind kind, unsigned bytes,
unsigned *bound_register = nullptr) {
const auto &symbol = s.at(base);
const bool indexed = symbol.type == Symbol::Type::indexed_descriptor;
if (indexed && symbol.value == compute_resource_heap)
return heap_resource(i, s, base, kind, bytes);
const unsigned count = indexed ? 1 + symbol.index_mask / 32 : 1;
analysis.binding(i, s, base, 4, kind, bytes);
auto it = std::find_if(analysis.result.bindings.begin(), analysis.result.bindings.end(), [&](const auto &b) {
Expand Down
10 changes: 8 additions & 2 deletions src/native_compute_pipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ NativeComputePipeline create_native_compute_pipeline(ID3D12Device *device, Shade

UINT counts[2]{};
for (const auto &binding : pipeline.bindings) {
if (compute_heap_binding(binding.kind)) {
pipeline.heap_indexed = true; // ResourceDescriptorHeap reads: no table register
continue;
}
auto &count = counts[is_uav(binding)];
if (binding.shader_register != count++)
throw std::runtime_error("noncontiguous compute register range");
Expand Down Expand Up @@ -82,7 +86,9 @@ NativeComputePipeline create_native_compute_pipeline(ID3D12Device *device, Shade
param.ParameterType = D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS;
param.Constants = {b.shader_register + 1, 0, 2};
}
D3D12_ROOT_SIGNATURE_DESC desc{param_count, params.data(), static_cast<UINT>(samplers.size()), samplers.data()};
D3D12_ROOT_SIGNATURE_DESC desc{param_count, params.data(), static_cast<UINT>(samplers.size()), samplers.data(),
pipeline.heap_indexed ? static_cast<D3D12_ROOT_SIGNATURE_FLAGS>(0x400) // CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED
: D3D12_ROOT_SIGNATURE_FLAG_NONE};
Microsoft::WRL::ComPtr<ID3DBlob> blob;
if (FAILED(D3D12SerializeRootSignature(&desc, D3D_ROOT_SIGNATURE_VERSION_1, &blob, nullptr)) ||
FAILED(device->CreateRootSignature(0, blob->GetBufferPointer(), blob->GetBufferSize(),
Expand All @@ -104,7 +110,7 @@ void NativeComputePipeline::bind(ID3D12GraphicsCommandList *list, ID3D12Descript
throw std::runtime_error("compute root constants missing");
if (atomic.size() != atomic_views)
throw std::runtime_error("indexed atomic view metadata missing");
if (srv_count || uav_count)
if (srv_count || uav_count || heap_indexed)
list->SetDescriptorHeaps(1, &heap);
list->SetComputeRootSignature(root.Get());
list->SetPipelineState(state.Get());
Expand Down
2 changes: 2 additions & 0 deletions src/native_compute_pipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ struct NativeComputePipeline {
unsigned constant_words = 0, atomic_views = 0;
unsigned instructions = 0;
bool cache_hit = false, frontend_cache_hit = false;
bool heap_indexed = false; // reads ResourceDescriptorHeap: the root signature indexes the heap directly, which must be set
// on the list (bind() does it) and holds the selectable slots from index 0
double frontend_ms = 0;
double backend_ms = 0;
double pipeline_ms = 0;
Expand Down
142 changes: 142 additions & 0 deletions src/startup_stubs/native_global_srv.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
#pragma once

#include "native_buffer_stride.h"
#include <cstddef>
#include <cstdint>
#include <sstream>
#include <string>
#include <vector>

// The view type of a global (descriptor heap) SRV slot that a dispatch can select from GPU data.
//
// A shader that picks its slot at run time can only be recorded and replayed if the type of every slot it may pick is
// known: the capture keeps {slot, type, stride} for each slot of the heap, and the dispatch is refused, before any
// shader runs, when the selected slot holds another type than the one the shader reads. No heap-indexed data read is
// translated here; this header only classifies, serializes and checks.
namespace x2w_startup {
enum class GlobalSrvType : unsigned { none, raw_buffer, structured_buffer, typed_buffer, texture };

inline const char *global_srv_type_name(GlobalSrvType type) {
switch (type) {
case GlobalSrvType::raw_buffer: return "raw_buffer";
case GlobalSrvType::structured_buffer: return "structured_buffer";
case GlobalSrvType::typed_buffer: return "typed_buffer";
case GlobalSrvType::texture: return "texture";
default: return "none";
}
}

inline GlobalSrvType global_srv_type_from_name(const std::string &name) {
for (auto type : {GlobalSrvType::raw_buffer, GlobalSrvType::structured_buffer, GlobalSrvType::typed_buffer,
GlobalSrvType::texture})
if (name == global_srv_type_name(type)) return type;
return GlobalSrvType::none;
}

struct GlobalSrvView {
unsigned slot = 0;
GlobalSrvType type = GlobalSrvType::none;
unsigned stride = 0; // Bytes per element for a buffer view (the value native_view_stride gives), 0 for a texture.
};

// Classifies the description a title passed for an SRV (slot 18 of the device interface). False: not a view this
// path can name (an unknown dimension, or a buffer view without a usable stride).
inline bool classify_global_srv_view(unsigned heap_slot, const void *bytes, std::size_t size, GlobalSrvView &view) {
D3D12_SHADER_RESOURCE_VIEW_DESC desc{};
if (!bytes || size < sizeof(desc)) return false;
std::memcpy(&desc, bytes, sizeof(desc));
view = {};
view.slot = heap_slot;
if (desc.ViewDimension == D3D12_SRV_DIMENSION_UNKNOWN) return false;
if (desc.ViewDimension != D3D12_SRV_DIMENSION_BUFFER) {
view.type = GlobalSrvType::texture;
return true;
}
if (!native_view_stride(18, bytes, size, view.stride)) return false;
view.type = desc.Buffer.StructureByteStride ? GlobalSrvType::structured_buffer
: (desc.Buffer.Flags & D3D12_BUFFER_SRV_FLAG_RAW) ? GlobalSrvType::raw_buffer
: GlobalSrvType::typed_buffer;
return true;
}

// One request's view metadata, the text that travels with a captured request (req_<key>.views).
struct GlobalSrvRequest {
unsigned selected = 0;
GlobalSrvType needed = GlobalSrvType::none;
std::vector<GlobalSrvView> views;
bool has_selected = false;
};

inline std::string encode_global_srv_request(const GlobalSrvRequest &request) {
std::ostringstream out;
out << "x2w-global-srv-views 1\nselect " << request.selected << "\nneed " << global_srv_type_name(request.needed) << '\n';
for (const auto &view : request.views)
out << "slot " << view.slot << ' ' << global_srv_type_name(view.type) << ' ' << view.stride << '\n';
return out.str();
}

// False with a diagnostic in `error` for a text that cannot reconstruct the binding.
inline bool decode_global_srv_request(const std::string &text, GlobalSrvRequest &request, std::string &error) {
request = {};
std::istringstream in(text);
std::string line;
if (!std::getline(in, line) || line != "x2w-global-srv-views 1") {
error = "global SRV view metadata has no 'x2w-global-srv-views 1' header";
return false;
}
bool has_need = false;
while (std::getline(in, line)) {
std::istringstream fields(line);
std::string key;
fields >> key;
if (key.empty()) continue;
if (key == "select") {
if (!(fields >> request.selected)) { error = "global SRV view metadata: unreadable select line"; return false; }
request.has_selected = true;
} else if (key == "need") {
std::string name;
fields >> name;
request.needed = global_srv_type_from_name(name);
if (request.needed == GlobalSrvType::none) {
error = "global SRV view metadata: unknown needed view type '" + name + "'";
return false;
}
has_need = true;
} else if (key == "slot") {
GlobalSrvView view;
std::string name;
if (!(fields >> view.slot >> name >> view.stride)) {
error = "global SRV view metadata: unreadable slot line";
return false;
}
view.type = global_srv_type_from_name(name);
if (view.type == GlobalSrvType::none) {
error = "global SRV slot " + std::to_string(view.slot) + " has unknown view type '" + name + "'";
return false;
}
request.views.push_back(view);
} else {
error = "global SRV view metadata: unknown line '" + key + "'";
return false;
}
}
if (!request.has_selected || !has_need) {
error = std::string("global SRV view metadata lacks the ") + (request.has_selected ? "needed view type" : "selected slot");
return false;
}
return true;
}

// Empty when the selected slot holds the type the shader reads. Otherwise the refusal text: it names the slot and the
// view types, and is produced before the shader runs. Used by the native recording and by the replay alike.
inline std::string refuse_global_srv_selection(const std::vector<GlobalSrvView> &views, unsigned selected,
GlobalSrvType needed) {
for (const auto &view : views) {
if (view.slot != selected) continue;
if (view.type == needed) return {};
return "global SRV slot " + std::to_string(selected) + " has view type " + global_srv_type_name(view.type) +
", the shader needs " + global_srv_type_name(needed);
}
return "global SRV slot " + std::to_string(selected) + " has no captured view type";
}
} // namespace x2w_startup
Loading