shader-slang / slang

Making it easier to work with shaders
http://shader-slang.com
MIT License
2.16k stars 186 forks source link

Indexing unbounded struct array from pointer results in SPIRV validation error #5061

Closed dubiousconst282 closed 3 weeks ago

dubiousconst282 commented 1 month ago

Another silly one :)

Simplified repro:

struct MeshVertex {
    float3 Pos;
    float2 TexCoord;
};
struct MeshData { 
    float4x4 ModelMat;
    MeshVertex Vertices[];
};
struct DispatchParams {
    MeshData* Mesh;
    float3* Dest;
};

[vk::push_constant] DispatchParams pc;

[numthreads(64)]
void ComputeMain(uint tid: SV_DispatchThreadID) {
    pc.Dest[tid] = pc.Mesh->Vertices[tid].Pos;
}

spirv-val output:

error: line 85: OpAccessChain result type (OpTypeStruct) does not match the type that results from indexing into the base <id> (OpTypeStruct).
  %36 = OpAccessChain %_ptr_PhysicalStorageBuffer_MeshVertex_natural %33 %10
dubiousconst282 commented 1 month ago

Not sure if it helps, but here's a similar GLSL repro:

#version 460
#extension GL_EXT_buffer_reference2 : require

struct MeshVertex {
    vec3 Pos;
    vec2 TexCoord;
};
layout(std430, buffer_reference) buffer float3_ptr {
  vec3 value;
};
layout(std430, buffer_reference) buffer MeshData_ptr { 
    mat4 ModelMat;
    MeshVertex Vertices[];
};

layout(push_constant, std430) uniform pc_t {
    MeshData_ptr Mesh;
    float3_ptr Dest;
} pc;

layout(local_size_x = 64) in;
void main() {
    uint tid = gl_GlobalInvocationID.x;
    pc.Dest[tid].value = pc.Mesh.Vertices[tid].Pos;
}

Relevant SPIRV:

// Slang
         %27 = OpAccessChain %_ptr_PushConstant_13 %pc %int_0
         %28 = OpLoad %_ptr_PhysicalStorageBuffer_MeshData_natural %27
         %33 = OpAccessChain %_ptr_PhysicalStorageBuffer__runtimearr_MeshVertex %28 %int_1
         %36 = OpAccessChain %_ptr_PhysicalStorageBuffer_MeshVertex_natural %33 %10
         %37 = OpAccessChain %_ptr_PhysicalStorageBuffer_v3float %36 %int_0
         %38 = OpLoad %v3float %37 Aligned 4

// glslang
         %48 = OpAccessChain %_ptr_PushConstant__ptr_PhysicalStorageBuffer_MeshData_ptr %pc %int_0
         %49 = OpLoad %_ptr_PhysicalStorageBuffer_MeshData_ptr %48
         %50 = OpLoad %uint %tid
         %52 = OpAccessChain %_ptr_PhysicalStorageBuffer_v3float %49 %int_1 %50 %int_0
         %53 = OpLoad %v3float %52 Aligned 16
bmillsNV commented 1 month ago

Thanks for raising this issue. @kaizhangNV can you help to take a look?