From d654ad800e0df63ffd9e7d9a17f49c4f88406c1c Mon Sep 17 00:00:00 2001 From: Leyukaka <45659323+Leyukaka@users.noreply.github.com> Date: Tue, 1 Dec 2026 21:10:05 +0100 Subject: [PATCH 1/3] build: raise MSVC warnings for the shader compiler --- CMakeLists.txt | 8 ++++++++ src/gfx1013_compute_native.cpp | 1 + src/gfx1013_compute_native.h | 4 ++-- src/gfx1013_direct_compute.inl | 26 +++++++++++++++----------- src/gfx1013_direct_graphics.inl | 4 ++-- src/gfx1013_vertex_id.cpp | 4 ++-- tools/startup/compile_dxil.cpp | 8 ++++++++ 7 files changed, 38 insertions(+), 17 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a5b4d5e5d5..16f85ee010 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,6 +30,14 @@ if(WIN32) target_link_libraries(x2w_shader_compiler PRIVATE bcrypt d3d12) add_executable(compile_dxil tools/startup/compile_dxil.cpp) 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 "$<$,x2w_shader_compiler>:/WX>") + endif() set_target_properties(compile_dxil x2w_shader_compiler PROPERTIES MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") endif() diff --git a/src/gfx1013_compute_native.cpp b/src/gfx1013_compute_native.cpp index b7f59fc75c..9b9b922ad0 100644 --- a/src/gfx1013_compute_native.cpp +++ b/src/gfx1013_compute_native.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include diff --git a/src/gfx1013_compute_native.h b/src/gfx1013_compute_native.h index b478740524..7cdd242b41 100644 --- a/src/gfx1013_compute_native.h +++ b/src/gfx1013_compute_native.h @@ -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 { diff --git a/src/gfx1013_direct_compute.inl b/src/gfx1013_direct_compute.inl index 1f3dd36d66..8a1ef45564 100644 --- a/src/gfx1013_direct_compute.inl +++ b/src/gfx1013_direct_compute.inl @@ -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'; } @@ -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"); @@ -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) { @@ -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"); @@ -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; } @@ -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; @@ -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; } @@ -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); @@ -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; } @@ -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"); @@ -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; } diff --git a/src/gfx1013_direct_graphics.inl b/src/gfx1013_direct_graphics.inl index ecb534da0e..c5b6935ff0 100644 --- a/src/gfx1013_direct_graphics.inl +++ b/src/gfx1013_direct_graphics.inl @@ -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")); diff --git a/src/gfx1013_vertex_id.cpp b/src/gfx1013_vertex_id.cpp index d8ed79ffb1..961b54d44e 100644 --- a/src/gfx1013_vertex_id.cpp +++ b/src/gfx1013_vertex_id.cpp @@ -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; diff --git a/tools/startup/compile_dxil.cpp b/tools/startup/compile_dxil.cpp index 93a4214344..4b10355df2 100644 --- a/tools/startup/compile_dxil.cpp +++ b/tools/startup/compile_dxil.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -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()); @@ -394,6 +399,9 @@ static std::optional 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; From eecfdf7cc02c02aaca73474251d53ac2c2a791fe Mon Sep 17 00:00:00 2001 From: Leyukaka <45659323+Leyukaka@users.noreply.github.com> Date: Tue, 1 Dec 2026 21:10:24 +0100 Subject: [PATCH 2/3] build: compile shader compiler targets as C++20 --- CMakeLists.txt | 3 ++- tools/startup/compile_dxil.cpp | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 16f85ee010..adbb4eda63 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -26,9 +26,10 @@ 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-) diff --git a/tools/startup/compile_dxil.cpp b/tools/startup/compile_dxil.cpp index 4b10355df2..89c73d1d4a 100644 --- a/tools/startup/compile_dxil.cpp +++ b/tools/startup/compile_dxil.cpp @@ -804,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(utf8.data()), utf8.size()); } if (json) { auto write_json_sec = [](std::ostream& os, const Section& sec) { From 11bf403e75ba44f8452d0015b8371d8171eebc01 Mon Sep 17 00:00:00 2001 From: Leyukaka <45659323+Leyukaka@users.noreply.github.com> Date: Tue, 1 Dec 2026 21:11:59 +0100 Subject: [PATCH 3/3] shader: default-initialize translator aggregate members --- src/gfx1013_compute_ir.h | 16 ++++++++-------- src/gfx1013_compute_native.h | 23 ++++++++++++----------- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/src/gfx1013_compute_ir.h b/src/gfx1013_compute_ir.h index 39267e4643..5a5a8f1375 100644 --- a/src/gfx1013_compute_ir.h +++ b/src/gfx1013_compute_ir.h @@ -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; diff --git a/src/gfx1013_compute_native.h b/src/gfx1013_compute_native.h index 7cdd242b41..567ed48d2e 100644 --- a/src/gfx1013_compute_native.h +++ b/src/gfx1013_compute_native.h @@ -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 }; @@ -104,8 +104,8 @@ bool apply_reflected_table_use(ComputeViewBinding &binding, const GuestShaderSta std::optional packed_sgpr = {}); void apply_scalar_table_layout(ComputeViewBinding &binding, const D3D12_ROOT_DESCRIPTOR_TABLE1 &table); struct ComputeSampler { - std::array words; - bool explicit_lod; + std::array words{}; + bool explicit_lod = false; bool comparison = false; }; struct ComputeConstants { @@ -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). @@ -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 root_inputs{}; std::optional resource_heap_sgpr{}; bool validate_uint_heap_reads{};