Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compiler panics when using #[spirv(object_to_world)] #723

Closed
hatoo opened this issue Aug 17, 2021 · 3 comments · Fixed by #729
Closed

Compiler panics when using #[spirv(object_to_world)] #723

hatoo opened this issue Aug 17, 2021 · 3 comments · Fixed by #729
Labels
t: bug Something isn't working

Comments

@hatoo
Copy link
Contributor

hatoo commented Aug 17, 2021

Hi, I encountered compiler panic and I report here as output says.

Expected Behaviour

The compiler doesn't panic.
I also want to know how do we use #[spirv(object_to_world)] correctly.

Example & Steps To Reproduce

  1. Create shader

lib.rs

#![cfg_attr(
    target_arch = "spirv",
    no_std,
    feature(register_attr),
    register_attr(spirv)
)]

use spirv_std::glam::Vec4;
#[cfg(not(target_arch = "spirv"))]
use spirv_std::macros::spirv;

#[spirv(closest_hit)]
pub fn test_closest_hit(#[spirv(object_to_world)] _object_to_world: [Vec4; 3]) {}
  1. build it

build.rs

use std::error::Error;

use spirv_builder::{Capability, MetadataPrintout, SpirvBuilder};

fn main() -> Result<(), Box<dyn Error>> {
    SpirvBuilder::new("../shader", "spirv-unknown-vulkan1.2")
        .capability(Capability::RayTracingKHR)
        .extension("SPV_KHR_ray_tracing")
        .print_metadata(MetadataPrintout::Full)
        .build()?;

    Ok(())
}
  1. get panic
> cargo build
    Blocking waiting for file lock on build directory
   Compiling builder v0.1.0 (C:\Users\hato2\Desktop\rust-gpu-issue\builder)
error: failed to run custom build command for `builder v0.1.0 (C:\Users\hato2\Desktop\rust-gpu-issue\builder)`

Caused by:
  process didn't exit successfully: `C:\Users\hato2\Desktop\rust-gpu-issue\target\debug\build\builder-8dcc5829546998ea\build-script-build` (exit code: 1)
  --- stderr
     Compiling core v0.0.0 (C:\Users\hato2\.rustup\toolchains\nightly-2021-08-10-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core)
     Compiling rustc-std-workspace-core v1.99.0 (C:\Users\hato2\.rustup\toolchains\nightly-2021-08-10-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\rustc-std-workspace-core)
     Compiling compiler_builtins v0.1.49
     Compiling libm v0.2.1
     Compiling spirv-types v0.4.0-alpha.12
     Compiling bitflags v1.3.2
     Compiling num-traits v0.2.14
     Compiling glam v0.17.3
     Compiling spirv-std v0.4.0-alpha.12
     Compiling shader v0.1.0 (C:\Users\hato2\Desktop\rust-gpu-issue\shader)
  error: internal compiler error: unexpected panic

  note: the compiler unexpectedly panicked. this is a bug.

  note: we would appreciate a bug report: https://github.com/EmbarkStudios/rust-gpu/issues/new

  note: rustc 1.56.0-nightly (ae90dcf02 2021-08-09) running on x86_64-pc-windows-msvc

  note: compiler flags: -Z unstable-options -Z codegen-backend=C:\Users\hato2\Desktop\rust-gpu-issue\target\debug\rustc_codegen_spirv.dll -Z symbol-mangling-version=legacy -C opt-level=3 -C embed-bitcode=no -C target-feature=+RayTracingKHR,+ext:SPV_KHR_ray_tracing --crate-type lib --crate-type dylib

  note: some of the compiler flags provided by cargo are hidden

  query stack during panic:
  end of query stack
  note: `rust-gpu` version 0.4.0-alpha.12

  error: could not compile `shader`
  Error: BuildFailed

I created a repository for this issue https://github.com/hatoo/rust-gpu-issue.
You can reproduce by clone and build it.

System Info

  • Rust: 1.56.0-nightly (ae90dcf02 2021-08-09)
  • OS: Windows 11 Pro
  • GPU: RTX 2080ti
  • SPIR-V: v2021.2-dev v2021.1-42-g5dd2f769

Backtrace

Backtrace

<backtrace>

@hatoo hatoo added the t: bug Something isn't working label Aug 17, 2021
@khyperia
Copy link
Contributor

Thanks for the issue! Looks like the problem is due to some problematic interaction between internal compiler details due to unused parameters. In the meantime, this issue can be worked around via using the parameter in some way (I've not done raytracing stuff before, not sure what form the shader takes or if there's output variables, but something like this):

#[spirv(closest_hit)]
pub fn test_closest_hit(#[spirv(object_to_world)] object_to_world: [Vec4; 3], something: &mut f32) {
    *something = object_to_world[0].x;
}

@hatoo
Copy link
Contributor Author

hatoo commented Aug 18, 2021

Thanks! I've succeeded to work around compiler panic but I still fail to use #[spirv(object_to_world)].
I tried all types that I think it's fit to the 4x3 matrix e.g. [Vec3; 4], [Vec4; 3], [[f32; 3]; 4], Affine3A .. but I always get errors like

error: [VUID-ObjectToWorldKHR-ObjectToWorldKHR-04307] According to the Vulkan spec BuiltIn ObjectToWorldNV variable needs to be a matrix with 4 columns of 3-component vectors of 32-bit floats. ID <2> (OpVariable) has columns 0 and rows 0 not equal to expected 4x3.
    %object_to_world = OpVariable %_ptr_Input__arr__arr_float_uint_3_uint_4 Input

I checked valid SPIR-V emitted by glslc and I found that we need to use OpTypeMatrix to express matrix type.

test.rchit

#version 460

#extension GL_EXT_ray_tracing : enable

void main() {
	float x = gl_ObjectToWorldEXT[0][0];
}
glslc test.rchit --target-env=vulkan1.2

and got

...
%float = OpTypeFloat 32
%_ptr_Function_float = OpTypePointer Function %float
    %v3float = OpTypeVector %float 3
%mat4v3float = OpTypeMatrix %v3float 4
%_ptr_Input_mat4v3float = OpTypePointer Input %mat4v3float
%gl_ObjectToWorldEXT = OpVariable %_ptr_Input_mat4v3float Input
...

But I couldn't find any way to use OpTypeMatrix by rust-gpu.
Is there a way to use OpTypeMatrix or any workaround?
Should I create another issue about this?

@khyperia
Copy link
Contributor

Ah, shoot, yeah, we currently don't expose OpTypeMatrix. That feature is currently tracked by #82 :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
t: bug Something isn't working
Projects
None yet
2 participants