Skip to content
Merged
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
11 changes: 10 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,19 @@ if(WIN32)
CONTENT "#define X2W_NATIVE_SHADER_REVISION \"${native_shader_revision}\"\n")
target_include_directories(x2w_shader_compiler PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/generated")
target_include_directories(x2w_shader_compiler PUBLIC src)
target_compile_features(x2w_shader_compiler PUBLIC cxx_std_17)
target_compile_features(x2w_shader_compiler PUBLIC cxx_std_17 PRIVATE cxx_std_20)
target_link_libraries(x2w_shader_compiler PRIVATE bcrypt d3d12)
add_executable(compile_dxil tools/startup/compile_dxil.cpp)
target_compile_features(compile_dxil PRIVATE cxx_std_20)
target_link_libraries(compile_dxil PRIVATE x2w_shader_compiler)
if(MSVC)
target_compile_options(x2w_shader_compiler PRIVATE /W4 /permissive-)
target_compile_options(compile_dxil PRIVATE /W4 /permissive-)
# Add translation units once they build cleanly at /W4. The target check
# keeps the same sources' standalone fixture builds outside this ratchet.
set_property(SOURCE src/gfx1013_isa.cpp src/gfx1013_vertex_id.cpp
APPEND PROPERTY COMPILE_OPTIONS "$<$<STREQUAL:$<TARGET_PROPERTY:NAME>,x2w_shader_compiler>:/WX>")
endif()
set_target_properties(compile_dxil x2w_shader_compiler PROPERTIES
MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
endif()
Expand Down
16 changes: 8 additions & 8 deletions src/gfx1013_compute_ir.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

namespace x2w {
struct LocalDataAccess {
unsigned offset, address, data0, data1, destination, count;
unsigned pair_words; // Width of each independently offset element; zero for a single address.
bool read, stride64, return_old;
const char *atomic_ir, *atomic_hlsl;
bool subtract, compare_exchange;
unsigned store_bits;
unsigned read_bits, read_destination_shift;
bool read_signed, read_d16;
unsigned offset = 0, address = 0, data0 = 0, data1 = 0, destination = 0, count = 0;
unsigned pair_words = 0; // Width of each independently offset element; zero for a single address.
bool read = false, stride64 = false, return_old = false;
const char *atomic_ir = nullptr, *atomic_hlsl = nullptr;
bool subtract = false, compare_exchange = false;
unsigned store_bits = 0;
unsigned read_bits = 0, read_destination_shift = 0;
bool read_signed = false, read_d16 = false;
unsigned byte_offset(unsigned element) const {
if (!pair_words) return offset + 4 * element;
unsigned encoded = (offset >> (8 * (element / pair_words))) & 255;
Expand Down
1 change: 1 addition & 0 deletions src/gfx1013_compute_native.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <algorithm>
#include <array>
#include <charconv>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
Expand Down
27 changes: 14 additions & 13 deletions src/gfx1013_compute_native.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ enum class GpuStage;
struct GuestShaderStage;
class UnsupportedComputeShader : public std::runtime_error {
public:
explicit UnsupportedComputeShader(const std::string& message, const char* code = nullptr)
: std::runtime_error(message), code(code) {}
explicit UnsupportedComputeShader(const std::string& message, const char* reason_code = nullptr)
: std::runtime_error(message), code(reason_code) {}
const char* code;
};
enum class ComputeViewKind {
Expand Down Expand Up @@ -89,11 +89,11 @@ inline bool compute_uav(ComputeViewKind kind) {
exact_texture_uav_channels(kind) || compute_buffer_uav(kind);
}
struct ComputeViewBinding {
ComputeViewKind kind;
unsigned root_parameter;
unsigned byte_offset;
unsigned shader_register;
unsigned required_bytes;
ComputeViewKind kind = ComputeViewKind::buffer_srv;
unsigned root_parameter = 0;
unsigned byte_offset = 0;
unsigned shader_register = 0;
unsigned required_bytes = 0;
unsigned array_count = 1, array_position = 0;
unsigned image_dimension = 1; // ISA dimension field; 1 denotes Texture2D.
enum class ScalarTable { unknown, reflected_cbv, declared_cbv, srv };
Expand All @@ -104,8 +104,8 @@ bool apply_reflected_table_use(ComputeViewBinding &binding, const GuestShaderSta
std::optional<unsigned> packed_sgpr = {});
void apply_scalar_table_layout(ComputeViewBinding &binding, const D3D12_ROOT_DESCRIPTOR_TABLE1 &table);
struct ComputeSampler {
std::array<unsigned, 4> words;
bool explicit_lod;
std::array<unsigned, 4> words{};
bool explicit_lod = false;
bool comparison = false;
};
struct ComputeConstants {
Expand Down Expand Up @@ -133,8 +133,9 @@ struct GraphicsConstantLayout {
}
};
struct GraphicsLinkage {
std::string name;
unsigned index, row, first, width, component, used, kind, interpolation;
std::string name{};
unsigned index = 0, row = 0, first = 0, width = 0;
unsigned component = 0, used = 0, kind = 0, interpolation = 0;
};
struct MeshHeapRead {
unsigned root_parameter, dword, mask; // R32_UINT SRV at (root constant & mask).
Expand Down Expand Up @@ -242,8 +243,8 @@ struct MeshRootMapping {
};
MeshRootMapping derive_mesh_root_mapping(const D3D12_ROOT_SIGNATURE_DESC1 &root);
struct MeshStageLayout {
unsigned threads, max_vertices, max_primitives, topology, group_stride;
bool wave32;
unsigned threads = 0, max_vertices = 0, max_primitives = 0, topology = 0, group_stride = 0;
bool wave32 = false;
std::vector<MeshRootInput> root_inputs{};
std::optional<unsigned> resource_heap_sgpr{};
bool validate_uint_heap_reads{};
Expand Down
26 changes: 15 additions & 11 deletions src/gfx1013_direct_compute.inl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
// 32-bit word per wave. Ordinary vector writes retain their old value on inactive lanes.
inline bool mesh_partial_zero()
{
#if defined(_MSC_VER)
// C4996: keep the existing read-only CRT environment lookup without allocating a copy.
#pragma warning(suppress: 4996)
#endif
const char* value=std::getenv("X2W_MESH57_PARTIAL_ZERO");
return value && value[0]=='1';
}
Expand Down Expand Up @@ -1944,7 +1948,7 @@ struct DirectCompute {
}
if (fmac)
words[2] = read(i.dst);
auto lane = [&](unsigned selectors, unsigned negate) {
auto packed_lane = [&](unsigned selectors, unsigned negate) {
auto operand = [&](unsigned j) {
auto bits = (selectors & (1u << j)) ? ir.binary("lshr", words[j], "16")
: ir.binary("and", words[j], "65535");
Expand Down Expand Up @@ -1972,8 +1976,8 @@ struct DirectCompute {
};
// In VOP3P, NEG_HI occupies the ABS field and OP_SEL_HI[2] lives in word 0.
// VOP2 v_pk_fmac_f16 runs low halves (pair 0) and high halves (pair 1) with no modifiers.
auto low = lane(fmac ? 0u : ((i.a >> 11) & 7u), fmac ? 0u : i.neg);
auto high = lane(fmac ? 7u : (((i.b >> 27) & 3u) | ((i.a >> 12) & 4u)), fmac ? 0u : i.abs);
auto low = packed_lane(fmac ? 0u : ((i.a >> 11) & 7u), fmac ? 0u : i.neg);
auto high = packed_lane(fmac ? 7u : (((i.b >> 27) & 3u) | ((i.a >> 12) & 4u)), fmac ? 0u : i.abs);
vector_write(i.dst, ir.binary("or", low, ir.binary("shl", high, "16")));
}
bool float_alu(const Inst &i, const string *permuted = nullptr) {
Expand Down Expand Up @@ -3965,7 +3969,7 @@ struct DirectCompute {
}
return signed_int ? ir.binary("xor", bits, "32768") : bits;
};
auto lane = [&](unsigned selectors, unsigned negate) {
auto packed_lane = [&](unsigned selectors, unsigned negate) {
auto x = half(a, selectors & 1), y = half(b, selectors & 2);
if (floating && (negate & 1u))
x = ir.binary("xor", x, "32768");
Expand All @@ -3986,7 +3990,7 @@ struct DirectCompute {
}
return result;
};
auto low = lane((i.a >> 11) & 3, i.neg), high = lane((i.b >> 27) & 3, i.abs);
auto low = packed_lane((i.a >> 11) & 3, i.neg), high = packed_lane((i.b >> 27) & 3, i.abs);
vector_write(i.dst, ir.binary("or", low, ir.binary("shl", high, "16")));
return true;
}
Expand Down Expand Up @@ -4203,7 +4207,7 @@ struct DirectCompute {
: ir.binary("ashr", ir.binary("shl", word, "16"), "16");
return hi ? ir.binary("lshr", word, "16") : ir.binary("and", word, "65535");
};
auto lane = [&](bool hi_out) {
auto packed_lane = [&](bool hi_out) {
const bool sel_a = hi_out ? ((i.b >> 27) & 1) != 0 : ((i.a >> 11) & 1) != 0;
const bool sel_b = hi_out ? ((i.b >> 28) & 1) != 0 : ((i.a >> 12) & 1) != 0;
const bool sel_c = hi_out ? ((i.a >> 14) & 1) != 0 : ((i.a >> 13) & 1) != 0;
Expand All @@ -4222,7 +4226,7 @@ struct DirectCompute {
}
return ir.binary("and", res, "65535");
};
auto lo = lane(false), hi = lane(true);
auto lo = packed_lane(false), hi = packed_lane(true);
vector_write(i.dst, ir.binary("or", lo, ir.binary("shl", hi, "16")));
return;
}
Expand Down Expand Up @@ -4699,7 +4703,7 @@ struct DirectCompute {
: ir.binary("ashr", ir.binary("shl", word, "16"), "16");
return hi ? ir.binary("lshr", word, "16") : ir.binary("and", word, "65535");
};
auto lane = [&](bool hi_out) {
auto packed_lane = [&](bool hi_out) {
const bool sel_a = hi_out ? ((i.b >> 27) & 1) != 0 : ((i.a >> 11) & 1) != 0;
const bool sel_b = hi_out ? ((i.b >> 28) & 1) != 0 : ((i.a >> 12) & 1) != 0;
auto x = half(a, sel_a, i.clamp && sign);
Expand All @@ -4717,7 +4721,7 @@ struct DirectCompute {
}
return ir.binary("and", res, "65535");
};
auto lo = lane(false), hi = lane(true);
auto lo = packed_lane(false), hi = packed_lane(true);
vector_write(i.dst, ir.binary("or", lo, ir.binary("shl", hi, "16")));
return;
}
Expand Down Expand Up @@ -4762,7 +4766,7 @@ struct DirectCompute {
if (i.enc != Encoding::Mix || i.clamp || (i.a & 0x2700) || (i.b >> 29) || i.src[2])
refuse(i.pc, "unsupported packed 16-bit shift operand modifiers");
auto a = operand32(i, i.src[0]), b = operand32(i, i.src[1]);
auto lane = [&](bool hi_out) {
auto packed_lane = [&](bool hi_out) {
const bool sel_a = hi_out ? ((i.b >> 27) & 1) != 0 : ((i.a >> 11) & 1) != 0;
const bool sel_b = hi_out ? ((i.b >> 28) & 1) != 0 : ((i.a >> 12) & 1) != 0;
auto amt = ir.binary("and", sel_a ? ir.binary("lshr", a, "16") : a, "15");
Expand All @@ -4776,7 +4780,7 @@ struct DirectCompute {
return ir.binary("lshr", val, amt);
return ir.binary("and", ir.binary("shl", val, amt), "65535");
};
auto lo = lane(false), hi = lane(true);
auto lo = packed_lane(false), hi = packed_lane(true);
vector_write(i.dst, ir.binary("or", lo, ir.binary("shl", hi, "16")));
return;
}
Expand Down
4 changes: 2 additions & 2 deletions src/gfx1013_direct_graphics.inl
Original file line number Diff line number Diff line change
Expand Up @@ -340,8 +340,8 @@ struct GraphicsStage {
auto id = row(true, "SV_Position", 0, 3, 9, s.row, s.mask, conservative ? 5 : 4);
auto parity = [&](unsigned channel) {
auto pos = alu.ir.value("bitcast i32 " + load(id, channel) + " to float");
auto pixel = alu.ir.invoke("float", "unary.f32", 27, {{"float", pos}}, "nounwind readnone");
return alu.ir.binary("and", alu.ir.value("fptosi float " + pixel + " to i32"), "1");
auto pixel_coord = alu.ir.invoke("float", "unary.f32", 27, {{"float", pos}}, "nounwind readnone");
return alu.ir.binary("and", alu.ir.value("fptosi float " + pixel_coord + " to i32"), "1");
};
auto x = parity(0), y = parity(1);
auto lane = alu.ir.binary("or", x, alu.ir.binary("shl", y, "1"));
Expand Down
4 changes: 2 additions & 2 deletions src/gfx1013_vertex_id.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,10 @@ class VertexProgram {
const unsigned count = (op & 0xfffff000) == 0xe03c2000 ? 3 : 2;
const unsigned dest = (args >> 8) & 255;
if (dest + count > values.size()) fail("vertex load destination range");
const auto address =
const auto vertex_address =
temporary(operand(256 + (args & 255)) + " * ud3 + " + std::to_string(op & 4095) + "u");
for (unsigned i = 0; i < count; ++i)
assign(dest + i, resource + ".Load(" + address + "+" + std::to_string(4 * i) + "u)");
assign(dest + i, resource + ".Load(" + vertex_address + "+" + std::to_string(4 * i) + "u)");
}
bool buffer_instruction(Word op) {
const auto dest = (op >> 16) & 127, src = op & 255;
Expand Down
11 changes: 10 additions & 1 deletion tools/startup/compile_dxil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <charconv>
#include <chrono>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <future>
#include <map>
Expand Down Expand Up @@ -178,6 +179,10 @@ static x2w::NativeComputeShader translate_raw_mesh(const std::string& source,
layout.root_inputs = std::move(mapping.inputs);
layout.resource_heap_sgpr = mapping.resource_heap_sgpr;
}
#if defined(_MSC_VER)
// C4996: these diagnostic switches only read the CRT environment.
#pragma warning(suppress: 4996)
#endif
if (std::getenv("X2W_MESH_DUMP_LAYOUT")) {
const unsigned heap = layout.resource_heap_sgpr ? *layout.resource_heap_sgpr : 0;
std::fprintf(stderr, "mesh layout heap_sgpr=%u inputs=%zu\n", heap, layout.root_inputs.size());
Expand Down Expand Up @@ -394,6 +399,9 @@ static std::optional<std::string> compile_one(x2w::ShaderDxilCompiler& compiler,
}
// Test-only observation point: the DXIL text (or the HLSL source for the
// profiles that still emit HLSL) exactly as it reaches the compiler.
#if defined(_MSC_VER)
#pragma warning(suppress: 4996) // Read-only CRT environment lookup.
#endif
if (const char *dump=std::getenv("X2W_DUMP_SHADER_IR")) {
std::ofstream file(dump,std::ios::binary);
file << source;
Expand Down Expand Up @@ -796,7 +804,8 @@ static int run_refusals(int argc, wchar_t** argv) {
std::string mesh_layout_display;
if (mesh_layout) {
const auto utf8 = mesh_layout->u8string();
mesh_layout_display.assign(utf8.begin(), utf8.end());
// Preserve the UTF-8 bytes returned as char8_t by C++20.
mesh_layout_display.assign(reinterpret_cast<const char*>(utf8.data()), utf8.size());
}
if (json) {
auto write_json_sec = [](std::ostream& os, const Section& sec) {
Expand Down
Loading