Skip to content

Commit

Permalink
safestruct: Accessors for non trivial globals
Browse files Browse the repository at this point in the history
Adding Tracy in VVL showed that we are paying the price of initializing
global variables upon shared library entry, even if we do not
end up accessing those.
Adding accessors will make sure we pay this price
only when truly needed.
Also making the necessary changes in VVL.
  • Loading branch information
arno-lunarg committed Jul 17, 2024
1 parent 5f26cf6 commit 0d1d954
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 36 deletions.
4 changes: 4 additions & 0 deletions include/vulkan/utility/vk_safe_struct.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@

namespace vku {

// Mapping of unknown stype codes to structure lengths. This should be set up by the application
// before vkCreateInstance() and not modified afterwards.
std::vector<std::pair<uint32_t, uint32_t>>& GetCustomStypeInfo();

struct safe_VkBufferMemoryBarrier {
VkStructureType sType;
const void* pNext{};
Expand Down
4 changes: 0 additions & 4 deletions include/vulkan/utility/vk_safe_struct_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@

namespace vku {

// Mapping of unknown stype codes to structure lengths. This should be set up by the application
// before vkCreateInstance() and not modified afterwards.
extern std::vector<std::pair<uint32_t, uint32_t>> custom_stype_info;

// State that elements in a pNext chain may need to be aware of
struct PNextCopyState {
// Custom initialization function. Returns true if the structure passed to init was initialized, false otherwise
Expand Down
10 changes: 6 additions & 4 deletions scripts/generators/safe_struct_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ def generateHeader(self):
#include <vulkan/utility/vk_safe_struct_utils.hpp>
namespace vku {
// Mapping of unknown stype codes to structure lengths. This should be set up by the application
// before vkCreateInstance() and not modified afterwards.
std::vector<std::pair<uint32_t, uint32_t>>& GetCustomStypeInfo();
\n''')

guard_helper = PlatformGuardHelper()
Expand Down Expand Up @@ -251,8 +255,6 @@ def generateUtil(self):
#include <vector>
#include <cstring>
extern std::vector<std::pair<uint32_t, uint32_t>> custom_stype_info;
namespace vku {
char *SafeStringCopy(const char *in_string) {
if (nullptr == in_string) return nullptr;
Expand Down Expand Up @@ -305,7 +307,7 @@ def generateUtil(self):
out.append('''
default: // Encountered an unknown sType -- skip (do not copy) this entry in the chain
// If sType is in custom list, construct blind copy
for (auto item : custom_stype_info) {
for (auto item : GetCustomStypeInfo()) {
if (item.first == static_cast<uint32_t>(header->sType)) {
safe_pNext = malloc(item.second);
memcpy(safe_pNext, header, item.second);
Expand Down Expand Up @@ -361,7 +363,7 @@ def generateUtil(self):
out.append('''
default: // Encountered an unknown sType
// If sType is in custom list, free custom struct memory and clean up
for (auto item : custom_stype_info) {
for (auto item : GetCustomStypeInfo() ) {
if (item.first == static_cast<uint32_t>(header->sType)) {
free(current);
break;
Expand Down
56 changes: 32 additions & 24 deletions src/vulkan/vk_safe_struct_manual.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@

namespace vku {

std::vector<std::pair<uint32_t, uint32_t>> custom_stype_info{};
std::vector<std::pair<uint32_t, uint32_t>>& GetCustomStypeInfo() {
static std::vector<std::pair<uint32_t, uint32_t>> custom_stype_info{};
return custom_stype_info;
}

struct ASGeomKHRExtraData {
ASGeomKHRExtraData(uint8_t* alloc, uint32_t primOffset, uint32_t primCount)
Expand All @@ -31,7 +34,12 @@ struct ASGeomKHRExtraData {
uint32_t primitiveCount;
};

vku::concurrent::unordered_map<const safe_VkAccelerationStructureGeometryKHR*, ASGeomKHRExtraData*, 4> as_geom_khr_host_alloc;
vku::concurrent::unordered_map<const safe_VkAccelerationStructureGeometryKHR*, ASGeomKHRExtraData*, 4>&
GetAccelStructGeomHostAllocMap() {
static vku::concurrent::unordered_map<const safe_VkAccelerationStructureGeometryKHR*, ASGeomKHRExtraData*, 4>
as_geom_khr_host_alloc;
return as_geom_khr_host_alloc;
}

safe_VkAccelerationStructureGeometryKHR::safe_VkAccelerationStructureGeometryKHR(
const VkAccelerationStructureGeometryKHR* in_struct, const bool is_host,
Expand All @@ -57,7 +65,7 @@ safe_VkAccelerationStructureGeometryKHR::safe_VkAccelerationStructureGeometryKHR
ppInstances[i] = &pInstances[i];
}
geometry.instances.data.hostAddress = allocation;
as_geom_khr_host_alloc.insert(
GetAccelStructGeomHostAllocMap().insert(
this, new ASGeomKHRExtraData(allocation, build_range_info->primitiveOffset, build_range_info->primitiveCount));
} else {
const auto primitive_offset = build_range_info->primitiveOffset;
Expand All @@ -68,7 +76,7 @@ safe_VkAccelerationStructureGeometryKHR::safe_VkAccelerationStructureGeometryKHR
memcpy(allocation + primitive_offset, host_address + primitive_offset,
primitive_count * sizeof(VkAccelerationStructureInstanceKHR));
geometry.instances.data.hostAddress = allocation;
as_geom_khr_host_alloc.insert(
GetAccelStructGeomHostAllocMap().insert(
this, new ASGeomKHRExtraData(allocation, build_range_info->primitiveOffset, build_range_info->primitiveCount));
}
}
Expand All @@ -85,8 +93,8 @@ safe_VkAccelerationStructureGeometryKHR::safe_VkAccelerationStructureGeometryKHR
flags = copy_src.flags;

pNext = SafePnextCopy(copy_src.pNext);
auto src_iter = as_geom_khr_host_alloc.find(&copy_src);
if (src_iter != as_geom_khr_host_alloc.end()) {
auto src_iter = GetAccelStructGeomHostAllocMap().find(&copy_src);
if (src_iter != GetAccelStructGeomHostAllocMap().end()) {
auto& src_alloc = src_iter->second;
if (geometry.instances.arrayOfPointers) {
size_t pp_array_size = src_alloc->primitiveCount * sizeof(VkAccelerationStructureInstanceKHR*);
Expand All @@ -103,14 +111,14 @@ safe_VkAccelerationStructureGeometryKHR::safe_VkAccelerationStructureGeometryKHR
ppInstances[i] = &pInstances[i];
}
geometry.instances.data.hostAddress = allocation;
as_geom_khr_host_alloc.insert(
GetAccelStructGeomHostAllocMap().insert(
this, new ASGeomKHRExtraData(allocation, src_alloc->primitiveOffset, src_alloc->primitiveCount));
} else {
size_t array_size = src_alloc->primitiveOffset + src_alloc->primitiveCount * sizeof(VkAccelerationStructureInstanceKHR);
uint8_t* allocation = new uint8_t[array_size];
memcpy(allocation, src_alloc->ptr, array_size);
geometry.instances.data.hostAddress = allocation;
as_geom_khr_host_alloc.insert(
GetAccelStructGeomHostAllocMap().insert(
this, new ASGeomKHRExtraData(allocation, src_alloc->primitiveOffset, src_alloc->primitiveCount));
}
}
Expand All @@ -120,8 +128,8 @@ safe_VkAccelerationStructureGeometryKHR& safe_VkAccelerationStructureGeometryKHR
const safe_VkAccelerationStructureGeometryKHR& copy_src) {
if (&copy_src == this) return *this;

auto iter = as_geom_khr_host_alloc.pop(this);
if (iter != as_geom_khr_host_alloc.end()) {
auto iter = GetAccelStructGeomHostAllocMap().pop(this);
if (iter != GetAccelStructGeomHostAllocMap().end()) {
delete iter->second;
}
FreePnextChain(pNext);
Expand All @@ -132,8 +140,8 @@ safe_VkAccelerationStructureGeometryKHR& safe_VkAccelerationStructureGeometryKHR
flags = copy_src.flags;

pNext = SafePnextCopy(copy_src.pNext);
auto src_iter = as_geom_khr_host_alloc.find(&copy_src);
if (src_iter != as_geom_khr_host_alloc.end()) {
auto src_iter = GetAccelStructGeomHostAllocMap().find(&copy_src);
if (src_iter != GetAccelStructGeomHostAllocMap().end()) {
auto& src_alloc = src_iter->second;
if (geometry.instances.arrayOfPointers) {
size_t pp_array_size = src_alloc->primitiveCount * sizeof(VkAccelerationStructureInstanceKHR*);
Expand All @@ -150,14 +158,14 @@ safe_VkAccelerationStructureGeometryKHR& safe_VkAccelerationStructureGeometryKHR
ppInstances[i] = &pInstances[i];
}
geometry.instances.data.hostAddress = allocation;
as_geom_khr_host_alloc.insert(
GetAccelStructGeomHostAllocMap().insert(
this, new ASGeomKHRExtraData(allocation, src_alloc->primitiveOffset, src_alloc->primitiveCount));
} else {
size_t array_size = src_alloc->primitiveOffset + src_alloc->primitiveCount * sizeof(VkAccelerationStructureInstanceKHR);
uint8_t* allocation = new uint8_t[array_size];
memcpy(allocation, src_alloc->ptr, array_size);
geometry.instances.data.hostAddress = allocation;
as_geom_khr_host_alloc.insert(
GetAccelStructGeomHostAllocMap().insert(
this, new ASGeomKHRExtraData(allocation, src_alloc->primitiveOffset, src_alloc->primitiveCount));
}
}
Expand All @@ -166,8 +174,8 @@ safe_VkAccelerationStructureGeometryKHR& safe_VkAccelerationStructureGeometryKHR
}

safe_VkAccelerationStructureGeometryKHR::~safe_VkAccelerationStructureGeometryKHR() {
auto iter = as_geom_khr_host_alloc.pop(this);
if (iter != as_geom_khr_host_alloc.end()) {
auto iter = GetAccelStructGeomHostAllocMap().pop(this);
if (iter != GetAccelStructGeomHostAllocMap().end()) {
delete iter->second;
}
FreePnextChain(pNext);
Expand All @@ -176,8 +184,8 @@ safe_VkAccelerationStructureGeometryKHR::~safe_VkAccelerationStructureGeometryKH
void safe_VkAccelerationStructureGeometryKHR::initialize(const VkAccelerationStructureGeometryKHR* in_struct, const bool is_host,
const VkAccelerationStructureBuildRangeInfoKHR* build_range_info,
[[maybe_unused]] PNextCopyState* copy_state) {
auto iter = as_geom_khr_host_alloc.pop(this);
if (iter != as_geom_khr_host_alloc.end()) {
auto iter = GetAccelStructGeomHostAllocMap().pop(this);
if (iter != GetAccelStructGeomHostAllocMap().end()) {
delete iter->second;
}
FreePnextChain(pNext);
Expand All @@ -204,7 +212,7 @@ void safe_VkAccelerationStructureGeometryKHR::initialize(const VkAccelerationStr
ppInstances[i] = &pInstances[i];
}
geometry.instances.data.hostAddress = allocation;
as_geom_khr_host_alloc.insert(
GetAccelStructGeomHostAllocMap().insert(
this, new ASGeomKHRExtraData(allocation, build_range_info->primitiveOffset, build_range_info->primitiveCount));
} else {
const auto primitive_offset = build_range_info->primitiveOffset;
Expand All @@ -215,7 +223,7 @@ void safe_VkAccelerationStructureGeometryKHR::initialize(const VkAccelerationStr
memcpy(allocation + primitive_offset, host_address + primitive_offset,
primitive_count * sizeof(VkAccelerationStructureInstanceKHR));
geometry.instances.data.hostAddress = allocation;
as_geom_khr_host_alloc.insert(
GetAccelStructGeomHostAllocMap().insert(
this, new ASGeomKHRExtraData(allocation, build_range_info->primitiveOffset, build_range_info->primitiveCount));
}
}
Expand All @@ -229,8 +237,8 @@ void safe_VkAccelerationStructureGeometryKHR::initialize(const safe_VkAccelerati
flags = copy_src->flags;

pNext = SafePnextCopy(copy_src->pNext);
auto src_iter = as_geom_khr_host_alloc.find(copy_src);
if (src_iter != as_geom_khr_host_alloc.end()) {
auto src_iter = GetAccelStructGeomHostAllocMap().find(copy_src);
if (src_iter != GetAccelStructGeomHostAllocMap().end()) {
auto& src_alloc = src_iter->second;
if (geometry.instances.arrayOfPointers) {
size_t pp_array_size = src_alloc->primitiveCount * sizeof(VkAccelerationStructureInstanceKHR*);
Expand All @@ -247,14 +255,14 @@ void safe_VkAccelerationStructureGeometryKHR::initialize(const safe_VkAccelerati
ppInstances[i] = &pInstances[i];
}
geometry.instances.data.hostAddress = allocation;
as_geom_khr_host_alloc.insert(
GetAccelStructGeomHostAllocMap().insert(
this, new ASGeomKHRExtraData(allocation, src_alloc->primitiveOffset, src_alloc->primitiveCount));
} else {
size_t array_size = src_alloc->primitiveOffset + src_alloc->primitiveCount * sizeof(VkAccelerationStructureInstanceKHR);
uint8_t* allocation = new uint8_t[array_size];
memcpy(allocation, src_alloc->ptr, array_size);
geometry.instances.data.hostAddress = allocation;
as_geom_khr_host_alloc.insert(
GetAccelStructGeomHostAllocMap().insert(
this, new ASGeomKHRExtraData(allocation, src_alloc->primitiveOffset, src_alloc->primitiveCount));
}
}
Expand Down
6 changes: 2 additions & 4 deletions src/vulkan/vk_safe_struct_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
#include <vector>
#include <cstring>

extern std::vector<std::pair<uint32_t, uint32_t>> custom_stype_info;

namespace vku {
char *SafeStringCopy(const char *in_string) {
if (nullptr == in_string) return nullptr;
Expand Down Expand Up @@ -1848,7 +1846,7 @@ void *SafePnextCopy(const void *pNext, PNextCopyState* copy_state) {

default: // Encountered an unknown sType -- skip (do not copy) this entry in the chain
// If sType is in custom list, construct blind copy
for (auto item : custom_stype_info) {
for (auto item : GetCustomStypeInfo()) {
if (item.first == static_cast<uint32_t>(header->sType)) {
safe_pNext = malloc(item.second);
memcpy(safe_pNext, header, item.second);
Expand Down Expand Up @@ -3680,7 +3678,7 @@ void FreePnextChain(const void *pNext) {

default: // Encountered an unknown sType
// If sType is in custom list, free custom struct memory and clean up
for (auto item : custom_stype_info) {
for (auto item : GetCustomStypeInfo() ) {
if (item.first == static_cast<uint32_t>(header->sType)) {
free(current);
break;
Expand Down

0 comments on commit 0d1d954

Please sign in to comment.