pygfx/wgpu-py

The expression may only be indexed by a constant

Closed this issue · 3 comments

Describe the bug

people talking about the bug
note: naga moved into main wgpu repo so might be the problem?

To Reproduce

edit the triangle example such that positions is constant.

Observed behavior

Device::create_shader_module error: 
Shader validation error: 
   ┌─ :23:25
   │
23 │     out.pos = vec4<f32>(positions[in.vertex_index], 0.0, 1.0);
   │                         ^^^^^^^^^^^^^^^^^^^^^^^^^^ naga::Expression [29]


Traceback (most recent call last):
  File "/home/lizelive/Documents/doom/./headless.py", line 163, in <module>
    main(canvas)
  File "/home/lizelive/Documents/doom/./headless.py", line 68, in main
    return _main(canvas, device)
  File "/home/lizelive/Documents/doom/./headless.py", line 79, in _main
    shader = device.create_shader_module(code=shader_source)
  File "/usr/local/lib/python3.10/dist-packages/wgpu/backends/wgpu_native/_api.py", line 1325, in create_shader_module
    id = libf.wgpuDeviceCreateShaderModule(self._internal, struct)
  File "/usr/local/lib/python3.10/dist-packages/wgpu/backends/wgpu_native/_helpers.py", line 322, in proxy_func
    raise wgpu_error  # the frame above is more interesting ↑↑
wgpu._classes.GPUValidationError: Validation Error

Caused by:
    In wgpuDeviceCreateShaderModule

Shader validation error:
   ┌─ :23:25
   │
23 │     out.pos = vec4<f32>(positions[in.vertex_index], 0.0, 1.0);
   │                         ^^^^^^^^^^^^^^^^^^^^^^^^^^ naga::Expression [29]


    Entry point vs_main at Vertex is invalid
    Expression [29] is invalid
    The expression [11] may only be indexed by a constant

Can you share the exact changes you made to the triangle example?

Otherwise it will be pretty difficult for us to help you.

If I do some searching in the naga repo I can see that this issue was raised there in 2021 and addressed as well. See the test cases here for example: https://github.com/JCapucho/naga/blob/master/tests/in/glsl/global-constant-array.vert

So I believe it should work already for a long time. We can't help you any further until you share reproduction code.

I recognize this. Indeed, if you change var positions = array<vec2<f32>, 3>(...) to let positions = array<vec2<f32>, 3>(...), i.e. replace var with let, then this error is raised. This has been the case since I can remember, and applies to all arrays in wgsl. I've never viewed this a being a bug, TBH. And if it is, it's a Naga issue; we can't do anything about it here.

As Almar said, please open an issue at the naga repo.