KhronosGroup / SPIRV-Cross

SPIRV-Cross is a practical tool and library for performing reflection on SPIR-V and disassembling SPIR-V back to high level languages.
Apache License 2.0
2.07k stars 564 forks source link

invalid MSL when using BDA #2368

Open 9291Sam opened 2 months ago

9291Sam commented 2 months ago

MoltenVK SDK version is 1.3.290

Full Shader here:

#version 460
#extension GL_EXT_buffer_reference : require

layout(std430, buffer_reference, buffer_reference_align = 4) readonly buffer ChunkLocalFaceBuffer
{
    uint face_data[];
};

layout(push_constant) readonly uniform PushConstants {
  ChunkLocalFaceBuffer ptr_quads_data;
} in_push_constants;

void main() {
    const uint face_data = in_push_constants.ptr_quads_data.face_data[46645];
}

produces the following error:

[Aug 08/23/2024 13:25:47:578:212] [crates/gfx/src/vulkan/inst.rs:148] [ERROR] VK_ERROR_INITIALIZATION_FAILED: Shader library compile failed (Error code 3):
program_source:132:97: error: reinterpret_cast from 'uint' (aka 'unsigned int') to 'ulong' (aka 'unsigned long') is not allowed
    bool _43 = inst_buffer_device_address(52u, uint4(0u, gl_VertexIndex, gl_InstanceIndex, 0u), reinterpret_cast<ulong>(in_push_constants.ptr_quads_data->face_data[46645]), 4u, 61u, _53, _56, _63, _66, _72, _72BufferSize, _83);
9291Sam commented 2 months ago

Also, changing the array index into pointer arithmetic also still causes the same error

HansKristian-Work commented 2 months ago

Seems fine to me:

SPIR-V:

; SPIR-V
; Version: 1.0
; Generator: Khronos Glslang Reference Front End; 11
; Bound: 24
; Schema: 0
               OpCapability Shader
               OpCapability PhysicalStorageBufferAddresses
               OpExtension "SPV_KHR_physical_storage_buffer"
          %1 = OpExtInstImport "GLSL.std.450"
               OpMemoryModel PhysicalStorageBuffer64 GLSL450
               OpEntryPoint GLCompute %main "main"
               OpExecutionMode %main LocalSize 1 1 1
               OpSource GLSL 460
               OpSourceExtension "GL_EXT_buffer_reference"
               OpName %main "main"
               OpName %face_data "face_data"
               OpName %PushConstants "PushConstants"
               OpMemberName %PushConstants 0 "ptr_quads_data"
               OpName %ChunkLocalFaceBuffer "ChunkLocalFaceBuffer"
               OpMemberName %ChunkLocalFaceBuffer 0 "face_data"
               OpName %in_push_constants "in_push_constants"
               OpMemberDecorate %PushConstants 0 Offset 0
               OpDecorate %PushConstants Block
               OpDecorate %_runtimearr_uint ArrayStride 4
               OpMemberDecorate %ChunkLocalFaceBuffer 0 NonWritable
               OpMemberDecorate %ChunkLocalFaceBuffer 0 Offset 0
               OpDecorate %ChunkLocalFaceBuffer Block
       %void = OpTypeVoid
          %3 = OpTypeFunction %void
       %uint = OpTypeInt 32 0
%_ptr_Function_uint = OpTypePointer Function %uint
               OpTypeForwardPointer %_ptr_PhysicalStorageBuffer_ChunkLocalFaceBuffer PhysicalStorageBuffer
%PushConstants = OpTypeStruct %_ptr_PhysicalStorageBuffer_ChunkLocalFaceBuffer
%_runtimearr_uint = OpTypeRuntimeArray %uint
%ChunkLocalFaceBuffer = OpTypeStruct %_runtimearr_uint
%_ptr_PhysicalStorageBuffer_ChunkLocalFaceBuffer = OpTypePointer PhysicalStorageBuffer %ChunkLocalFaceBuffer
%_ptr_PushConstant_PushConstants = OpTypePointer PushConstant %PushConstants
%in_push_constants = OpVariable %_ptr_PushConstant_PushConstants PushConstant
        %int = OpTypeInt 32 1
      %int_0 = OpConstant %int 0
%_ptr_PushConstant__ptr_PhysicalStorageBuffer_ChunkLocalFaceBuffer = OpTypePointer PushConstant %_ptr_PhysicalStorageBuffer_ChunkLocalFaceBuffer
  %int_46645 = OpConstant %int 46645
%_ptr_PhysicalStorageBuffer_uint = OpTypePointer PhysicalStorageBuffer %uint
       %main = OpFunction %void None %3
          %5 = OpLabel
  %face_data = OpVariable %_ptr_Function_uint Function
         %18 = OpAccessChain %_ptr_PushConstant__ptr_PhysicalStorageBuffer_ChunkLocalFaceBuffer %in_push_constants %int_0
         %19 = OpLoad %_ptr_PhysicalStorageBuffer_ChunkLocalFaceBuffer %18
         %22 = OpAccessChain %_ptr_PhysicalStorageBuffer_uint %19 %int_0 %int_46645
         %23 = OpLoad %uint %22 Aligned 4
               OpStore %face_data %23
               OpReturn
               OpFunctionEnd

MSL:

#include <metal_stdlib>
#include <simd/simd.h>

using namespace metal;

struct ChunkLocalFaceBuffer;

struct PushConstants
{
    device ChunkLocalFaceBuffer* ptr_quads_data;
};

struct ChunkLocalFaceBuffer
{
    uint face_data[1];
};

kernel void main0(constant PushConstants& in_push_constants [[buffer(0)]])
{
    uint face_data = in_push_constants.ptr_quads_data->face_data[46645];
}