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

[naga] Calling a function accepting a ptr from within another function #4827

Open
gents83 opened this issue Dec 4, 2023 · 10 comments
Open

Comments

@gents83
Copy link
Contributor

gents83 commented Dec 4, 2023

I'm in a situation where I would like to call a function accepting a ptr from within another function.
But it fails to compile without much info
image
and inside another function I'm calling
image
But the error that I get is just: Function [44] 'my_function_name' is invalid

It seems that it should work, but the error doesn't give much info and it seems to not be able to validate the shader

@teoxoy
Copy link
Member

teoxoy commented Dec 4, 2023

let will load the value, you should use var.

@gents83
Copy link
Contributor Author

gents83 commented Dec 4, 2023

I tried also with var but with no luck 😥

@teoxoy
Copy link
Member

teoxoy commented Dec 4, 2023

Same error message?

@gents83
Copy link
Contributor Author

gents83 commented Dec 14, 2023

Adding maybe similar use case scenario - the situation is the following one:
I've following struct

struct RayData {
ray: Ray,
node_index: i32,
next_index: i32,
data_type: u32,
visibility_id: u32,
};

and a function

fn intersect_aabb(ray: ptr<function, Ray>, aabb_min: vec3, aabb_max: vec3) -> f32 { /* ... */ }

and it seems that naga today is complaining on:

let intersection = intersect_aabb(&data.ray, (*node).min, (*node).max);

while it seems to accept if I do something like:

var ray_copied = data.ray;
let intersection = intersect_aabb(&ray_copied, (*node).min, (*node).max);

data is also declared as a var data but it seems that ptr is not able to use to inner variables of a struct as ptr, but maybe is correct and it's me that I'm expecting something that should not work like that

@teoxoy
Copy link
Member

teoxoy commented Dec 15, 2023

struct RayData {
    ray: Ray,
};

struct Ray {
    data_type: u32,
};

fn main() {
    var data = RayData();
    let intersection = intersect_aabb(&data.ray);
}

fn intersect_aabb(ray: ptr<function, Ray>) -> f32 {
    return 0.0;
}

This compiles fine via naga cli on trunk. Could you provide a minimum reproducible example like the one above that doesn't?

@gents83
Copy link
Contributor Author

gents83 commented Dec 15, 2023

Oh sorry, maybe I didn't clarity it.
It compiles/validate fine on my side too, but once used inside a compute pipeline it actually fails to be used 😅

@teoxoy
Copy link
Member

teoxoy commented Dec 15, 2023

It compiles/validate fine on my side too, but once used inside a compute pipeline it actually fails to be used

In what way does it fail to be used?

The original error you were getting was "Function [44] 'my_function_name' is invalid" which is a naga validation error; I'm confused 😅.

@gents83
Copy link
Contributor Author

gents83 commented Dec 15, 2023

I'm usually validating my shaders at compile time with:
image
And it's successefully validated.
But at runtime - when calling the device.create_compute_pipeline() - it fails with that error :(

@gents83
Copy link
Contributor Author

gents83 commented Dec 16, 2023

This is an example that validate correctly but than fails at runtime:

struct InnerContainer {
    inner_id: u32,
}
struct Container {
    id: u32,
    inner: InnerContainer,
};

fn id(c: ptr<function, Container>) -> u32 {
    return (*c).id;
}
fn inner_id(c: ptr<function, InnerContainer>) -> u32 {
    return (*c).inner_id;
}
fn first(c: ptr<function, Container>) -> u32 {
    return id(c);
}
fn second(c: ptr<function, Container>) -> u32 {
    return inner_id(&(*c).inner);
}

@compute
@workgroup_size(16, 16, 1)
fn main() {
    var global_var = Container(1u, InnerContainer(2u));
    let u = first(&global_var);
    let v = second(&global_var);
}

But this time the error is:
internal error: entered unreachable code: Expression [2] is not cached!

@teoxoy
Copy link
Member

teoxoy commented Jan 2, 2024

Ah, this is probably the same issue as #4517.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants