1

i am making a compute shader to use with bevy 0.12 so im using wgsl. i want to index into an array like this:

fn is_boxed_in(x: u32, y: u32, z: u32, voxels: array<ShaderVoxel, 4096>) -> bool {
    if check_for_voxel(voxels[voxel_to_1d(min(CHUNK_SIZE - 1u, x + 1u), y, z)])
        && check_for_voxel(voxels[voxel_to_1d(max(0u, x - 1u), y, z)])
        && check_for_voxel(voxels[voxel_to_1d(x, min(CHUNK_SIZE - 1u, y + 1u), z)])
        && check_for_voxel(voxels[voxel_to_1d(x, max(0u, y - 1u), z)])
        && check_for_voxel(voxels[voxel_to_1d(x, y, min(CHUNK_SIZE - 1u, z + 1u))])
        && check_for_voxel(voxels[voxel_to_1d(x, y, max(0u, z - 1u))])
    {
        return true;
    }

    return false;
}

but im getting this error:

Function [6] 'is_boxed_in' is invalid.

WithSpan {
    inner: Function {
        handle: [6],
        name: "is_boxed_in",
        source: Expression {
            handle: [10],
            source: IndexMustBeConstant(
                [4],
            ),
        },
    },
    spans: [
        (
            Span {
                start: 2094,
                end: 2700,
            },
            "naga::Function [6]",
        ),
        (
            Span {
                start: 2201,
                end: 2256,
            },
            "naga::Expression [10]",
        ),
    ],
}

is it possible and if so how do i index into this array? this is where i am calling the function:

@compute @workgroup_size(16, 16, 1)
fn init(@builtin(global_invocation_id) global_id: vec3<u32>){
    let index = voxel_to_1d(global_id.x, global_id.y, global_id.z);

    var iter = 0u;
    while iter < arrayLength(&chunks.chunks) {
        var chunk = chunks.chunks[iter];
        var voxels = chunk.voxels;
        for (var i = 0; i < 4096; i += 1){
            var voxel = voxels[i];
            if voxel.id != 0u && !is_boxed_in(voxel_to_3d(i).x, voxel_to_3d(i).y, voxel_to_3d(i).z, voxels){
                //gen normal
            }
        }
        iter += 1u;
    }
}

any help would be super appreciated :)

1 Answer 1

0

Sidenote: With this much data for the voxels you're better off using module_scope declaration for it. You can reach it as well, and still set the data like you normally would.

I believe what you were looking for was resolved last 25Oct; Refer to this issue.

I'd quote a relevant part but if my understanding is correct your code should work as is in latest versions!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.