Skip to content

Commit

Permalink
igl | vulkan | SPIR-V reflection takes in the size in bytes
Browse files Browse the repository at this point in the history
Summary: SPIR-V reflection takes in the size in bytes.

Reviewed By: ChristianK275

Differential Revision: D50410133

fbshipit-source-id: de07a93aeeef4b2117e25b1e7990f781f155f5ad
  • Loading branch information
corporateshark authored and facebook-github-bot committed Oct 19, 2023
1 parent 4a254b8 commit 63c2843
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
7 changes: 4 additions & 3 deletions src/igl/tests/vulkan/SpvReflectionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,8 @@ const std::vector<uint32_t> kTextureSpvWords = {

TEST(SpvReflectionTest, UniformBufferTest) {
using namespace vulkan::util;
SpvModuleInfo spvModuleInfo =
getReflectionData(kUniformBufferSpvWords.data(), kUniformBufferSpvWords.size());
SpvModuleInfo spvModuleInfo = getReflectionData(kUniformBufferSpvWords.data(),
kUniformBufferSpvWords.size() * sizeof(uint32_t));

ASSERT_EQ(spvModuleInfo.uniformBufferBindingLocations.size(), 2);
EXPECT_EQ(spvModuleInfo.uniformBufferBindingLocations[0], 0);
Expand All @@ -233,7 +233,8 @@ TEST(SpvReflectionTest, UniformBufferTest) {

TEST(SpvReflectionTest, TextureTest) {
using namespace vulkan::util;
SpvModuleInfo spvModuleInfo = getReflectionData(kTextureSpvWords.data(), kTextureSpvWords.size());
SpvModuleInfo spvModuleInfo =
getReflectionData(kTextureSpvWords.data(), kTextureSpvWords.size() * sizeof(uint32_t));

ASSERT_EQ(spvModuleInfo.uniformBufferBindingLocations.size(), 0);
EXPECT_EQ(spvModuleInfo.storageBufferBindingLocations.size(), 0);
Expand Down
17 changes: 12 additions & 5 deletions src/igl/vulkan/util/SpvReflection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,19 @@ TextureType getTextureType(uint32_t dim, bool isArrayed) {

} // namespace

SpvModuleInfo getReflectionData(const uint32_t* words, size_t size) {
SpvModuleInfo spvModuleInfo;
if (size < kSpvHeaderSize) {
return spvModuleInfo;
SpvModuleInfo getReflectionData(const void* spirv, size_t numBytes) {
// go from bytes to SPIR-V words
const size_t size = numBytes / sizeof(uint32_t);

if (size <= kSpvHeaderSize) {
return {};
}

const uint32_t* words = reinterpret_cast<const uint32_t*>(spirv);

if (words[0] != kSpvMagicWord) {
IGL_ASSERT_MSG(false, "Invalid SPIR-V magic word");
return spvModuleInfo;
return {};
}

std::unordered_set<ResultId> interfaceBlockTypeIds;
Expand All @@ -118,6 +123,8 @@ SpvModuleInfo getReflectionData(const uint32_t* words, size_t size) {
std::unordered_map<ResultId, uint32_t> bindingLocations;
std::unordered_map<ResultId, TextureType> imageTypes;

SpvModuleInfo spvModuleInfo;

for (size_t pos = kSpvHeaderSize; pos < size;) {
uint16_t instructionSize = getInstructionSize(words[pos]);
OpCode opCode = getOpCode(words[pos]);
Expand Down
2 changes: 1 addition & 1 deletion src/igl/vulkan/util/SpvReflection.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ struct SpvModuleInfo {
std::vector<TextureDescription> textures;
};

SpvModuleInfo getReflectionData(const uint32_t* words, size_t size);
SpvModuleInfo getReflectionData(const void* spirv, size_t numBytes);

} // namespace igl::vulkan::util

0 comments on commit 63c2843

Please sign in to comment.