Skip to content

Commit

Permalink
Vulkan dump resources: Fix in vertex buffer size calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
panos-lunarg committed Dec 20, 2024
1 parent ce13272 commit 9445601
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 27 deletions.
43 changes: 30 additions & 13 deletions framework/decode/vulkan_replay_dump_resources_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,10 +317,11 @@ uint32_t VkIndexTypeToBytes(VkIndexType type)
}
}

uint32_t FindGreatestVertexIndex(const std::vector<uint8_t>& index_data,
uint32_t index_count,
uint32_t first_index,
VkIndexType type)
std::pair<uint32_t, uint32_t> FindMinMaxVertexIndices(const std::vector<uint8_t>& index_data,
uint32_t index_count,
uint32_t first_index,
int32_t vertex_offset,
VkIndexType type)
{
switch (type)
{
Expand All @@ -336,9 +337,10 @@ uint32_t FindGreatestVertexIndex(const std::vector<uint8_t>& index_data,

if (i == index_count)
{
return 0;
return std::make_pair(0, 0);
}

uint8_t min = indices[first_index + i];
uint8_t max = indices[first_index + i];

for (; i < index_count; ++i)
Expand All @@ -352,9 +354,15 @@ uint32_t FindGreatestVertexIndex(const std::vector<uint8_t>& index_data,
{
max = indices[first_index + i];
}

if (indices[first_index + i] < min)
{
min = indices[first_index + i];
}
}

return max;
return std::make_pair(static_cast<uint32_t>(min) + vertex_offset,
static_cast<uint32_t>(max) + vertex_offset);
}
break;

Expand All @@ -370,9 +378,10 @@ uint32_t FindGreatestVertexIndex(const std::vector<uint8_t>& index_data,

if (i == index_count)
{
return 0;
return std::make_pair(0, 0);
}

uint16_t min = indices[first_index + i];
uint16_t max = indices[first_index + i];

for (; i < index_count; ++i)
Expand All @@ -386,9 +395,15 @@ uint32_t FindGreatestVertexIndex(const std::vector<uint8_t>& index_data,
{
max = indices[first_index + i];
}

if (indices[first_index + i] < min)
{
min = indices[first_index + i];
}
}

return max;
return std::make_pair(static_cast<uint32_t>(min) + vertex_offset,
static_cast<uint32_t>(max) + vertex_offset);
}
break;

Expand All @@ -404,9 +419,10 @@ uint32_t FindGreatestVertexIndex(const std::vector<uint8_t>& index_data,

if (i == index_count)
{
return 0;
return std::make_pair(0, 0);
}

uint32_t min = indices[first_index + i];
uint32_t max = indices[first_index + i];

for (; i < index_count; ++i)
Expand All @@ -416,21 +432,22 @@ uint32_t FindGreatestVertexIndex(const std::vector<uint8_t>& index_data,
continue;
}

if (indices[first_index + i] > max)
if (indices[first_index + i] < min)
{
max = indices[first_index + i];
min = indices[first_index + i];
}
}

return max;
return std::make_pair(min + vertex_offset, max + vertex_offset);
}
break;

case VK_INDEX_TYPE_NONE_KHR:
default:
GFXRECON_LOG_ERROR("%s() Unrecognized/unhandled index type (%u)", __func__, static_cast<uint32_t>(type));
assert(0);
return 0;

return std::make_pair(0, 0);
break;
}
}
Expand Down
9 changes: 5 additions & 4 deletions framework/decode/vulkan_replay_dump_resources_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,11 @@ VkResult CloneBuffer(CommonObjectInfoTable& object_info_table,

uint32_t VkIndexTypeToBytes(VkIndexType type);

uint32_t FindGreatestVertexIndex(const std::vector<uint8_t>& index_data,
uint32_t index_count,
uint32_t first_index,
VkIndexType type);
std::pair<uint32_t, uint32_t> FindMinMaxVertexIndices(const std::vector<uint8_t>& index_data,
uint32_t index_count,
uint32_t first_index,
int32_t vertex_offset,
VkIndexType type);

VkResult DumpImageToFile(const VulkanImageInfo* image_info,
const VulkanDeviceInfo* device_info,
Expand Down
40 changes: 30 additions & 10 deletions framework/decode/vulkan_replay_dump_resources_draw_calls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2360,17 +2360,18 @@ VkResult DrawCallsDumpingContext::DumpVertexIndexBuffers(uint64_t qs_index, uint
assert(dc_params_entry != draw_call_params.end());
DrawCallParameters& dc_params = dc_params_entry->second;

uint32_t greatest_vertex_index = 0;
std::pair<uint32_t, uint32_t> min_max_vertex_indices(0, 0);

// Dump index buffer
if (IsDrawCallIndexed(dc_params.type) && dc_params.referenced_index_buffer.buffer_info != nullptr)
{
// Store all (indexCount, firstIndex) pairs used by all associated with this index buffer.
// Latter we will parse the index buffer using all these pairs in order to detect the
// greatest index.
// Store all (indexCount, firstIndex) pairs used by all draw calls (in case of indirect)
// associated with this index buffer. Then we will parse the index buffer using all these pairs in order to
// detect the greatest index.
std::vector<std::pair<uint32_t, uint32_t>> index_count_first_index_pairs;

uint32_t abs_index_count = 0;
uint32_t abs_index_count = 0;
int32_t greatest_vertex_offset = 0;

if (IsDrawCallIndirect(dc_params.type))
{
Expand All @@ -2394,6 +2395,11 @@ VkResult DrawCallsDumpingContext::DumpVertexIndexBuffers(uint64_t qs_index, uint
{
abs_index_count = indirect_index_count + indirect_first_index;
}

if (greatest_vertex_offset < ic_params.draw_indexed_params[d].vertexOffset)
{
greatest_vertex_offset = ic_params.draw_indexed_params[d].vertexOffset;
}
}
}
}
Expand All @@ -2417,6 +2423,11 @@ VkResult DrawCallsDumpingContext::DumpVertexIndexBuffers(uint64_t qs_index, uint
{
abs_index_count = indirect_index_count + indirect_first_index;
}

if (greatest_vertex_offset < i_params.draw_indexed_params[d].vertexOffset)
{
greatest_vertex_offset = i_params.draw_indexed_params[d].vertexOffset;
}
}
}
}
Expand All @@ -2427,7 +2438,8 @@ VkResult DrawCallsDumpingContext::DumpVertexIndexBuffers(uint64_t qs_index, uint
const uint32_t first_index = dc_params.dc_params_union.draw_indexed.firstIndex;

index_count_first_index_pairs.emplace_back(std::make_pair(index_count, first_index));
abs_index_count = index_count + first_index;
abs_index_count = index_count + first_index;
greatest_vertex_offset = dc_params.dc_params_union.draw_indexed.vertexOffset;
}

if (abs_index_count)
Expand Down Expand Up @@ -2468,12 +2480,19 @@ VkResult DrawCallsDumpingContext::DumpVertexIndexBuffers(uint64_t qs_index, uint
std::string filename = GenerateIndexBufferFilename(qs_index, bcb_index, dc_index, index_type);
util::bufferwriter::WriteBuffer(filename, index_data.data(), index_data.size());

// Parse all indices in order to find the smallest and greatest index
for (const auto& pairs : index_count_first_index_pairs)
{
const uint32_t gvi = FindGreatestVertexIndex(index_data, pairs.first, pairs.second, index_type);
if (greatest_vertex_index < gvi)
const std::pair<uint32_t, uint32_t> min_max_indices =
FindMinMaxVertexIndices(index_data, pairs.first, pairs.second, greatest_vertex_offset, index_type);
if (min_max_indices.first < min_max_vertex_indices.first)
{
min_max_vertex_indices.first = min_max_indices.first;
}

if (min_max_indices.second > min_max_vertex_indices.second)
{
greatest_vertex_index = gvi;
min_max_vertex_indices.second = min_max_indices.second;
}
}
}
Expand All @@ -2488,7 +2507,8 @@ VkResult DrawCallsDumpingContext::DumpVertexIndexBuffers(uint64_t qs_index, uint
if (IsDrawCallIndexed(dc_params.type))
{
// For indexed draw calls the greatest vertex index will be used as the max vertex count
vertex_count = greatest_vertex_index + 1;
GFXRECON_ASSERT(min_max_vertex_indices.second >= min_max_vertex_indices.first);
vertex_count = (min_max_vertex_indices.second - min_max_vertex_indices.first) + 1;

if (IsDrawCallIndirect(dc_params.type))
{
Expand Down

0 comments on commit 9445601

Please sign in to comment.