gfx-rs / wgpu-native

Native WebGPU implementation based on wgpu-core
Apache License 2.0
875 stars 105 forks source link

NulError in logger when getting a DX12 compile error #416

Open Vipitis opened 2 months ago

Vipitis commented 2 months ago

so I have been running known bad shaders (infinite loops) and came across a really unexpected error dump when using D3D12 as backend.

It took me 3 days to go through the naga translation etc, but I have no recreated this issue in wgpu-native. It will happen with backend forced to Dx12 and not on Vulkan (using the snippet from #337). ref branch

here is the loop that naga translated into wgsl (I removed some of the placeholder vars). I think this is also a bad translation - but I am not too sure. it's an issue to file elsewhere too.

var i: i32 = 0i;
    loop {
        if !((i < 100i)) {
            break;
        }
        {
            a = (a - 0.005f);
        }
        continuing {
            b = (b + 0.1f);
        }
    }

If run with DX12 you get a panic emitted from https://github.com/gfx-rs/wgpu-native/blob/3e18e3d3e5fd433053875464447c36d980625f0b/src/logging.rs#L43

the full output and stacktrace: ```bash [wgpu] [warn] Naga generated shader for "fs_main" at Fragment: struct NagaConstants { int first_vertex; int first_instance; uint other; }; ConstantBuffer _NagaConstants: register(b0); float4 vs_main(uint in_vertex_index : SV_VertexID) : SV_Position { float x = float((int((_NagaConstants.first_vertex + in_vertex_index)) - 1)); float y = float(((int(((_NagaConstants.first_vertex + in_vertex_index) & 1u)) * 2) - 1)); return float4(x, y, 0.0, 1.0); } float4 fs_main() : SV_Target0 { float a = 1.0; float b = 0.0; int i = 0; bool loop_init = true; while(true) { if (!loop_init) { float _e13 = b; b = (_e13 + 0.1); } loop_init = false; int _e6 = i; if (!((_e6 < 100))) { break; } { float _e10 = a; a = (_e10 - 0.005); } } float _e16 = b; float _e17 = a; return float4(1.0, _e16, _e17, 1.0); } thread '' panicked at src\logging.rs:43:43: called `Result::unwrap()` on an `Err` value: NulError(264, [68, 101, 118, 105, 99, 101, 58, 58, 99, 114, 101, 97, 116, 101, 95, 114, 101, 110, 100, 101, 114, 95, 112, 105, 112, 101, 108, 105, 110, 101, 32, 101, 114, 114, 111, 114, 58, 32, 73, 110, 116, 101, 114, 110, 97, 108, 32, 101, 114, 114, 111, 114, 32, 105, 110, 32, 83, 104, 97, 100, 101, 114, 83, 116, 97, 103, 101, 115, 40, 70, 82, 65, 71, 77, 69, 78, 84, 41, 32, 115, 104, 97, 100, 101, 114, 58, 32, 70, 88, 67, 32, 68, 51, 68, 67, 111, 109, 112, 105, 108, 101, 32, 101, 114, 114, 111, 114, 32, 40, 48, 120, 56, 48, 48, 48, 52, 48, 48, 53, 41, 58, 32, 68, 58, 92, 68, 111, 107, 117, 109, 101, 110, 116, 101, 92, 85, 110, 105, 95, 79, 70, 70, 76, 73, 78, 69, 92, 83, 111, 83, 101, 50, 48, 50, 52, 92, 119, 103, 112, 117, 45, 110, 97, 116, 105, 118, 101, 92, 101, 120, 97, 109, 112, 108, 101, 115, 92, 116, 114, 105, 97, 110, 103, 108, 101, 92, 115, 104, 97, 100, 101, 114, 46, 119, 103, 115, 108, 40, 50, 50, 44, 49, 49, 45, 49, 52, 41, 58, 32, 101, 114, 114, 111, 114, 32, 88, 51, 54, 57, 54, 58, 32, 105, 110, 102, 105, 110, 105, 116, 101, 32, 108, 111, 111, 112, 32, 100, 101, 116, 101, 99, 116, 101, 100, 32, 45, 32, 108, 111, 111, 112, 32, 110, 101, 118, 101, 114, 32, 101, 120, 105, 116, 115, 10, 0]) stack backtrace: 0: 0x7ff7652fecad - std::backtrace_rs::backtrace::dbghelp64::trace at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library\std\src\..\..\backtrace\src\backtrace\dbghelp64.rs:91 1: 0x7ff7652fecad - std::backtrace_rs::backtrace::trace_unsynchronized at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library\std\src\..\..\backtrace\src\backtrace\mod.rs:66 2: 0x7ff7652fecad - std::sys_common::backtrace::_print_fmt at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library\std\src\sys_common\backtrace.rs:68 3: 0x7ff7652fecad - std::sys_common::backtrace::_print::impl$0::fmt at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library\std\src\sys_common\backtrace.rs:44 4: 0x7ff764f1f369 - core::fmt::rt::Argument::fmt at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library\core\src\fmt\rt.rs:165 5: 0x7ff764f1f369 - core::fmt::write at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library\core\src\fmt\mod.rs:1168 6: 0x7ff7652f50c1 - std::io::Write::write_fmt at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library\std\src\io\mod.rs:1835 7: 0x7ff7652fea86 - std::sys_common::backtrace::print at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library\std\src\sys_common\backtrace.rs:34 8: 0x7ff765301c68 - std::panicking::default_hook::closure$1 at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library\std\src\panicking.rs:271 9: 0x7ff765301916 - std::panicking::default_hook at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library\std\src\panicking.rs:298 10: 0x7ff765302268 - std::panicking::rust_panic_with_hook at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library\std\src\panicking.rs:795 11: 0x7ff765302127 - std::panicking::begin_panic_handler::closure$0 at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library\std\src\panicking.rs:664 12: 0x7ff7652ff61f - std::sys_common::backtrace::__rust_end_short_backtrace at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library\std\src\sys_common\backtrace.rs:171 13: 0x7ff765301dd8 - std::panicking::begin_panic_handler at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library\std\src\panicking.rs:652 14: 0x7ff766015e94 - core::panicking::panic_fmt at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library\core\src\panicking.rs:72 15: 0x7ff766016460 - core::result::unwrap_failed at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9/library\core\src\result.rs:1679 16: 0x7ff764f06754 - enum2$ >::unwrap at /rustc/051478957371ee0084a7c0913941d2a8c4757bb9\library\core\src\result.rs:1102 17: 0x7ff764f06754 - wgpu_native::logging::impl$0::log at D:\Dokumente\Uni_OFFLINE\SoSe2024\wgpu-native\src\logging.rs:43 18: 0x7ff764f3e662 - log::__private_api::log_impl at C:\Users\Jan\.cargo\registry\src\index.crates.io-6f17d22bba15001f\log-0.4.22\src\__private_api.rs:61 19: 0x7ff764f3f42a - log::__private_api::log > at C:\Users\Jan\.cargo\registry\src\index.crates.io-6f17d22bba15001f\log-0.4.22\src\__private_api.rs:72 20: 0x7ff76509253d - wgpu_core::global::Global::device_create_render_pipeline at C:\Users\Jan\.cargo\git\checkouts\wgpu-53e70f8674b08dd4\5c5c8b1\wgpu-core\src\device\global.rs:1567 21: 0x7ff764ef5c32 - wgpu_native::wgpuDeviceCreateRenderPipeline at D:\Dokumente\Uni_OFFLINE\SoSe2024\wgpu-native\src\lib.rs:2287 22: 0x7ff764ed1644 - main at D:\Dokumente\Uni_OFFLINE\SoSe2024\wgpu-native\examples\triangle\main.c:220 23: 0x7ff766013b69 - invoke_main at D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:78 24: 0x7ff766013ca2 - __scrt_common_main_seh at D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288 25: 0x7ff766013d2e - __scrt_common_main at D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:330 26: 0x7ff766013d4e - mainCRTStartup at D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_main.cpp:16 27: 0x7ffe3a787374 - BaseThreadInitThunk 28: 0x7ffe3bc3cc91 - RtlUserThreadStart make: *** [Makefile:144: run-example-triangle] Error 3 ```

if you decode the error message you get

Device::create_render_pipeline error: Internal error in ShaderStages(FRAGMENT) shader: FXC D3DCompile error (0x80004005): D:\Dokumente\Uni_OFFLINE\SoSe2024\wgpu-native\examples\triangle\shader.wgsl(22,11-14): error X3696: infinite loop detected - loop never exits

Which I guess is an FXC issue (since it works in vulkan) - but that is an issue to file elsewhere. The problem here is how there is seemingly a nullbyte at the end but the bound is wrong? I am not quite sure but it might be an easy fix.