Skip to content

Commit

Permalink
Update SPV_GOOGLE_user_type based on spec
Browse files Browse the repository at this point in the history
The spec for SPV_GOOGLE_user_type was recently updated – see
KhronosGroup/SPIRV-Registry#230.  This handles
all types listed in the spec, as well as allowing types with template
parameters after a colon.
  • Loading branch information
cassiebeckley committed Feb 7, 2024
1 parent f68d692 commit 7595e81
Show file tree
Hide file tree
Showing 9 changed files with 3,072 additions and 26 deletions.
26 changes: 26 additions & 0 deletions common/output_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -643,12 +643,32 @@ std::string ToStringUserType(SpvReflectUserType user_type) {
return "Buffer";
case SPV_REFLECT_USER_TYPE_BYTE_ADDRESS_BUFFER:
return "ByteAddressBuffer";
case SPV_REFLECT_USER_TYPE_CONSTANT_BUFFER:
return "ConstantBuffer";
case SPV_REFLECT_USER_TYPE_CONSUME_STRUCTURED_BUFFER:
return "ConsumeStructuredBuffer";
case SPV_REFLECT_USER_TYPE_INPUT_PATCH:
return "InputPatch";
case SPV_REFLECT_USER_TYPE_OUTPUT_PATCH:
return "OutputPatch";
case SPV_REFLECT_USER_TYPE_RASTERIZER_ORDERED_BUFFER:
return "RasterizerOrderedBuffer";
case SPV_REFLECT_USER_TYPE_RASTERIZER_ORDERED_BYTE_ADDRESS_BUFFER:
return "RasterizerOrderedByteAddressBuffer";
case SPV_REFLECT_USER_TYPE_RASTERIZER_ORDERED_STRUCTURED_BUFFER:
return "RasterizerOrderedStructuredBuffer";
case SPV_REFLECT_USER_TYPE_RASTERIZER_ORDERED_TEXTURE_1D:
return "RasterizerOrderedTexture1D";
case SPV_REFLECT_USER_TYPE_RASTERIZER_ORDERED_TEXTURE_1D_ARRAY:
return "RasterizerOrderedTexture1DArray";
case SPV_REFLECT_USER_TYPE_RASTERIZER_ORDERED_TEXTURE_2D:
return "RasterizerOrderedTexture2D";
case SPV_REFLECT_USER_TYPE_RASTERIZER_ORDERED_TEXTURE_2D_ARRAY:
return "RasterizerOrderedTexture2DArray";
case SPV_REFLECT_USER_TYPE_RASTERIZER_ORDERED_TEXTURE_3D:
return "RasterizerOrderedTexture3D";
case SPV_REFLECT_USER_TYPE_RAYTRACING_ACCELERATION_STRUCTURE:
return "RaytracingAccelerationStructure";
case SPV_REFLECT_USER_TYPE_RW_BUFFER:
return "RWBuffer";
case SPV_REFLECT_USER_TYPE_RW_BYTE_ADDRESS_BUFFER:
Expand All @@ -667,6 +687,10 @@ std::string ToStringUserType(SpvReflectUserType user_type) {
return "RWTexture3D";
case SPV_REFLECT_USER_TYPE_STRUCTURED_BUFFER:
return "StructuredBuffer";
case SPV_REFLECT_USER_TYPE_SUBPASS_INPUT:
return "SubpassInput";
case SPV_REFLECT_USER_TYPE_SUBPASS_INPUT_MS:
return "SubpassInputMS";
case SPV_REFLECT_USER_TYPE_TEXTURE_1D:
return "Texture1D";
case SPV_REFLECT_USER_TYPE_TEXTURE_1D_ARRAY:
Expand All @@ -681,6 +705,8 @@ std::string ToStringUserType(SpvReflectUserType user_type) {
return "Texture2DMSArray";
case SPV_REFLECT_USER_TYPE_TEXTURE_3D:
return "Texture3D";
case SPV_REFLECT_USER_TYPE_TEXTURE_BUFFER:
return "TextureBuffer";
case SPV_REFLECT_USER_TYPE_TEXTURE_CUBE:
return "TextureCube";
case SPV_REFLECT_USER_TYPE_TEXTURE_CUBE_ARRAY:
Expand Down
91 changes: 65 additions & 26 deletions spirv_reflect.c
Original file line number Diff line number Diff line change
Expand Up @@ -1365,6 +1365,19 @@ static SpvReflectResult ParseNames(SpvReflectPrvParser* p_parser) {
return SPV_REFLECT_RESULT_SUCCESS;
}

// Returns true if user_type matches pattern or if user_type begins with pattern and the next character is ':'
// For example, UserTypeMatches("rwbuffer", "rwbuffer") will be true, UserTypeMatches("rwbuffer", "rwbuffer:<S>") will be true, and
// UserTypeMatches("rwbuffer", "rwbufferfoo") will be false.
static bool UserTypeMatches(const char* user_type, const char* pattern) {
const size_t pattern_length = strlen(pattern);
if (strncmp(user_type, pattern, pattern_length) == 0) {
if (user_type[pattern_length] == ':' || user_type[pattern_length] == '\0') {
return true;
}
}
return false;
}

static SpvReflectResult ParseDecorations(SpvReflectPrvParser* p_parser, SpvReflectShaderModule* p_module) {
uint32_t spec_constant_count = 0;
for (uint32_t i = 0; i < p_parser->node_count; ++i) {
Expand Down Expand Up @@ -1583,57 +1596,83 @@ static SpvReflectResult ParseDecorations(SpvReflectPrvParser* p_parser, SpvRefle
return result;
}
const char* name = (const char*)(p_parser->spirv_code + p_node->word_offset + 3);
if (strcmp(name, "cbuffer") == 0) {
if (UserTypeMatches(name, "cbuffer")) {
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_CBUFFER;
} else if (strcmp(name, "tbuffer") == 0) {
} else if (UserTypeMatches(name, "tbuffer")) {
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_TBUFFER;
} else if (strcmp(name, "appendstructuredbuffer") == 0) {
} else if (UserTypeMatches(name, "appendstructuredbuffer")) {
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_APPEND_STRUCTURED_BUFFER;
} else if (strcmp(name, "buffer") == 0) {
} else if (UserTypeMatches(name, "buffer")) {
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_BUFFER;
} else if (strcmp(name, "byteaddressbuffer") == 0) {
} else if (UserTypeMatches(name, "byteaddressbuffer")) {
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_BYTE_ADDRESS_BUFFER;
} else if (strcmp(name, "consumestructuredbuffer") == 0) {
} else if (UserTypeMatches(name, "constantbuffer")) {
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_CONSTANT_BUFFER;
} else if (UserTypeMatches(name, "consumestructuredbuffer")) {
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_CONSUME_STRUCTURED_BUFFER;
} else if (strcmp(name, "inputpatch") == 0) {
} else if (UserTypeMatches(name, "inputpatch")) {
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_INPUT_PATCH;
} else if (strcmp(name, "outputpatch") == 0) {
} else if (UserTypeMatches(name, "outputpatch")) {
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_OUTPUT_PATCH;
} else if (strcmp(name, "rwbuffer") == 0) {
} else if (UserTypeMatches(name, "rasterizerorderedbuffer")) {
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_RASTERIZER_ORDERED_BUFFER;
} else if (UserTypeMatches(name, "rasterizerorderedbyteaddressbuffer")) {
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_RASTERIZER_ORDERED_BYTE_ADDRESS_BUFFER;
} else if (UserTypeMatches(name, "rasterizerorderedstructuredbuffer")) {
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_RASTERIZER_ORDERED_STRUCTURED_BUFFER;
} else if (UserTypeMatches(name, "rasterizerorderedtexture1d")) {
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_RASTERIZER_ORDERED_TEXTURE_1D;
} else if (UserTypeMatches(name, "rasterizerorderedtexture1darray")) {
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_RASTERIZER_ORDERED_TEXTURE_1D_ARRAY;
} else if (UserTypeMatches(name, "rasterizerorderedtexture2d")) {
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_RASTERIZER_ORDERED_TEXTURE_2D;
} else if (UserTypeMatches(name, "rasterizerorderedtexture2darray")) {
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_RASTERIZER_ORDERED_TEXTURE_2D_ARRAY;
} else if (UserTypeMatches(name, "rasterizerorderedtexture3d")) {
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_RASTERIZER_ORDERED_TEXTURE_3D;
} else if (UserTypeMatches(name, "raytracingaccelerationstructure")) {
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_RAYTRACING_ACCELERATION_STRUCTURE;
} else if (UserTypeMatches(name, "rwbuffer")) {
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_RW_BUFFER;
} else if (strcmp(name, "rwbyteaddressbuffer") == 0) {
} else if (UserTypeMatches(name, "rwbyteaddressbuffer")) {
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_RW_BYTE_ADDRESS_BUFFER;
} else if (strcmp(name, "rwstructuredbuffer") == 0) {
} else if (UserTypeMatches(name, "rwstructuredbuffer")) {
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_RW_STRUCTURED_BUFFER;
} else if (strcmp(name, "rwtexture1d") == 0) {
} else if (UserTypeMatches(name, "rwtexture1d")) {
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_RW_TEXTURE_1D;
} else if (strcmp(name, "rwtexture1darray") == 0) {
} else if (UserTypeMatches(name, "rwtexture1darray")) {
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_RW_TEXTURE_1D_ARRAY;
} else if (strcmp(name, "rwtexture2d") == 0) {
} else if (UserTypeMatches(name, "rwtexture2d")) {
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_RW_TEXTURE_2D;
} else if (strcmp(name, "rwtexture2darray") == 0) {
} else if (UserTypeMatches(name, "rwtexture2darray")) {
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_RW_TEXTURE_2D_ARRAY;
} else if (strcmp(name, "rwtexture3d") == 0) {
} else if (UserTypeMatches(name, "rwtexture3d")) {
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_RW_TEXTURE_3D;
} else if (strcmp(name, "structuredbuffer") == 0) {
} else if (UserTypeMatches(name, "structuredbuffer")) {
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_STRUCTURED_BUFFER;
} else if (strcmp(name, "texture1d") == 0) {
} else if (UserTypeMatches(name, "subpassinput")) {
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_SUBPASS_INPUT;
} else if (UserTypeMatches(name, "subpassinputms")) {
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_SUBPASS_INPUT_MS;
} else if (UserTypeMatches(name, "texture1d")) {
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_TEXTURE_1D;
} else if (strcmp(name, "texture1darray") == 0) {
} else if (UserTypeMatches(name, "texture1darray")) {
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_TEXTURE_1D_ARRAY;
} else if (strcmp(name, "texture2d") == 0) {
} else if (UserTypeMatches(name, "texture2d")) {
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_TEXTURE_2D;
} else if (strcmp(name, "texture2darray") == 0) {
} else if (UserTypeMatches(name, "texture2darray")) {
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_TEXTURE_2D_ARRAY;
} else if (strcmp(name, "texture2dms") == 0) {
} else if (UserTypeMatches(name, "texture2dms")) {
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_TEXTURE_2DMS;
} else if (strcmp(name, "texture2dmsarray") == 0) {
} else if (UserTypeMatches(name, "texture2dmsarray")) {
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_TEXTURE_2DMS_ARRAY;
} else if (strcmp(name, "texture3d") == 0) {
} else if (UserTypeMatches(name, "texture3d")) {
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_TEXTURE_3D;
} else if (strcmp(name, "texturecube") == 0) {
} else if (UserTypeMatches(name, "texturebuffer")) {
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_TEXTURE_BUFFER;
} else if (UserTypeMatches(name, "texturecube")) {
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_TEXTURE_CUBE;
} else if (strcmp(name, "texturecubearray") == 0) {
} else if (UserTypeMatches(name, "texturecubearray")) {
p_target_decorations->user_type = SPV_REFLECT_USER_TYPE_TEXTURE_CUBE_ARRAY;
}
}
Expand Down
13 changes: 13 additions & 0 deletions spirv_reflect.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,19 @@ typedef enum SpvReflectUserType {
SPV_REFLECT_USER_TYPE_APPEND_STRUCTURED_BUFFER,
SPV_REFLECT_USER_TYPE_BUFFER,
SPV_REFLECT_USER_TYPE_BYTE_ADDRESS_BUFFER,
SPV_REFLECT_USER_TYPE_CONSTANT_BUFFER,
SPV_REFLECT_USER_TYPE_CONSUME_STRUCTURED_BUFFER,
SPV_REFLECT_USER_TYPE_INPUT_PATCH,
SPV_REFLECT_USER_TYPE_OUTPUT_PATCH,
SPV_REFLECT_USER_TYPE_RASTERIZER_ORDERED_BUFFER,
SPV_REFLECT_USER_TYPE_RASTERIZER_ORDERED_BYTE_ADDRESS_BUFFER,
SPV_REFLECT_USER_TYPE_RASTERIZER_ORDERED_STRUCTURED_BUFFER,
SPV_REFLECT_USER_TYPE_RASTERIZER_ORDERED_TEXTURE_1D,
SPV_REFLECT_USER_TYPE_RASTERIZER_ORDERED_TEXTURE_1D_ARRAY,
SPV_REFLECT_USER_TYPE_RASTERIZER_ORDERED_TEXTURE_2D,
SPV_REFLECT_USER_TYPE_RASTERIZER_ORDERED_TEXTURE_2D_ARRAY,
SPV_REFLECT_USER_TYPE_RASTERIZER_ORDERED_TEXTURE_3D,
SPV_REFLECT_USER_TYPE_RAYTRACING_ACCELERATION_STRUCTURE,
SPV_REFLECT_USER_TYPE_RW_BUFFER,
SPV_REFLECT_USER_TYPE_RW_BYTE_ADDRESS_BUFFER,
SPV_REFLECT_USER_TYPE_RW_STRUCTURED_BUFFER,
Expand All @@ -180,13 +190,16 @@ typedef enum SpvReflectUserType {
SPV_REFLECT_USER_TYPE_RW_TEXTURE_2D_ARRAY,
SPV_REFLECT_USER_TYPE_RW_TEXTURE_3D,
SPV_REFLECT_USER_TYPE_STRUCTURED_BUFFER,
SPV_REFLECT_USER_TYPE_SUBPASS_INPUT,
SPV_REFLECT_USER_TYPE_SUBPASS_INPUT_MS,
SPV_REFLECT_USER_TYPE_TEXTURE_1D,
SPV_REFLECT_USER_TYPE_TEXTURE_1D_ARRAY,
SPV_REFLECT_USER_TYPE_TEXTURE_2D,
SPV_REFLECT_USER_TYPE_TEXTURE_2D_ARRAY,
SPV_REFLECT_USER_TYPE_TEXTURE_2DMS,
SPV_REFLECT_USER_TYPE_TEXTURE_2DMS_ARRAY,
SPV_REFLECT_USER_TYPE_TEXTURE_3D,
SPV_REFLECT_USER_TYPE_TEXTURE_BUFFER,
SPV_REFLECT_USER_TYPE_TEXTURE_CUBE,
SPV_REFLECT_USER_TYPE_TEXTURE_CUBE_ARRAY,
} SpvReflectUserType;
Expand Down
1 change: 1 addition & 0 deletions tests/build_all_shaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def my_which(cmd):
{'source':"hlsl/cbuffer.hlsl", 'entry':"main", 'profile':'vs_6_0', 'stage':'vert'},
{'source':"hlsl/counter_buffers.hlsl", 'entry':"main", 'profile':'ps_6_0', 'stage':'frag'},
{'source':"hlsl/semantics.hlsl", 'entry':"main", 'profile':'ps_6_0', 'stage':'frag'},
{'source':"hlsl/user_type.hlsl", 'entry':"main", 'profile':'ps_6_0', 'stage':'frag'},
]

if __name__ == "__main__":
Expand Down
63 changes: 63 additions & 0 deletions tests/hlsl/user_type.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
StructuredBuffer<float> a;
RWStructuredBuffer<float> b;
AppendStructuredBuffer<float> c;
ConsumeStructuredBuffer<float> d;
Texture1D<float> e;
Texture2D<float> f;
Texture3D<float> g;
TextureCube<float> h;
Texture1DArray<float> i;
Texture2DArray<float> j;
Texture2DMS<float> k;
Texture2DMSArray<float> l;
TextureCubeArray<float> m;
RWTexture1D<float> n;
RWTexture2D<float> o;
RWTexture3D<float> p;
RWTexture1DArray<float> q;
RWTexture2DArray<float> r;
Buffer<float> s;
RWBuffer<float> t;
Texture2DMSArray<float4, 64> u;
const Texture2DMSArray<float4, 64> v;
Texture1D t1;
Texture2D t2;

Texture1D<float> eArr[5];
Texture2D<float> fArr[5];
Texture3D<float> gArr[5];
TextureCube<float> hArr[5];
Texture1DArray<float> iArr[5];
Texture2DArray<float> jArr[5];
Texture2DMS<float> kArr[5];
Texture2DMSArray<float> lArr[5];
TextureCubeArray<float> mArr[5];
RWTexture1D<float> nArr[5];
RWTexture2D<float> oArr[5];
RWTexture3D<float> pArr[5];
RWTexture1DArray<float> qArr[5];
RWTexture2DArray<float> rArr[5];
Buffer<float> sArr[5];
RWBuffer<float> tArr[5];

cbuffer MyCBuffer { float x; };

tbuffer MyTBuffer { float y; };

ByteAddressBuffer bab;

RWByteAddressBuffer rwbab;

RaytracingAccelerationStructure rs;

struct S {
float f1;
float3 f2;
};

ConstantBuffer<S> cb;

TextureBuffer<S> tb;

void main(){
}
Binary file added tests/hlsl/user_type.spv
Binary file not shown.
Loading

0 comments on commit 7595e81

Please sign in to comment.