-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
More SoA stuff. Ensures we get one global bindless array of images.
- Loading branch information
1 parent
cc51654
commit f6c37cd
Showing
9 changed files
with
282 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/* Copyright (c) 2017-2023 Hans-Kristian Arntzen | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining | ||
* a copy of this software and associated documentation files (the | ||
* "Software"), to deal in the Software without restriction, including | ||
* without limitation the rights to use, copy, modify, merge, publish, | ||
* distribute, sublicense, and/or sell copies of the Software, and to | ||
* permit persons to whom the Software is furnished to do so, subject to | ||
* the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be | ||
* included in all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | ||
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | ||
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
#include "material_manager.hpp" | ||
#include "device.hpp" | ||
#include "limits.hpp" | ||
#include "hashmap.hpp" | ||
|
||
namespace Granite | ||
{ | ||
MaterialManager::MaterialManager() | ||
{ | ||
EVENT_MANAGER_REGISTER_LATCH(MaterialManager, on_device_created, on_device_destroyed, Vulkan::DeviceCreatedEvent); | ||
material_payload.reserve(Vulkan::VULKAN_MAX_UBO_SIZE / MaterialPayloadSize); | ||
bindless_texture_assets.reserve(Vulkan::VULKAN_NUM_BINDINGS_BINDLESS_VARYING); | ||
allocator.reserve_max_resources_per_pool(256, 8 * Vulkan::VULKAN_NUM_BINDINGS_BINDLESS_VARYING); | ||
allocator.set_bindless_resource_type(Vulkan::BindlessResourceType::Image); | ||
} | ||
|
||
MaterialOffsets MaterialManager::register_material( | ||
const AssetID *assets, unsigned count, const void *payload_data, | ||
size_t payload_size, bool force_unique) | ||
{ | ||
Util::Hasher hasher; | ||
if (!force_unique) | ||
{ | ||
for (unsigned i = 0; i < count; i++) | ||
hasher.u32(assets[i].id); | ||
hasher.data(static_cast<const uint8_t *>(payload_data), payload_size); | ||
} | ||
auto *group = force_unique ? nullptr : material.find(hasher.get()); | ||
|
||
if (group) | ||
return group->offsets; | ||
|
||
if (bindless_texture_assets.size() + count > Vulkan::VULKAN_NUM_BINDINGS_BINDLESS_VARYING) | ||
LOGE("Exceeding number of bindless slots.\n"); | ||
if (payload_size && material_payload.size() >= Vulkan::VULKAN_MAX_UBO_SIZE / MaterialPayloadSize) | ||
LOGE("Exceeding number of material payload slots.\n"); | ||
|
||
MaterialOffsets offsets = {}; | ||
offsets.texture_offset = uint32_t(bindless_texture_assets.size()); | ||
offsets.uniform_offset = payload_size ? uint32_t(material_payload.size()) : UINT16_MAX; | ||
|
||
if (!force_unique) | ||
{ | ||
group = material.allocate(); | ||
group->offsets = offsets; | ||
material.insert_replace(hasher.get(), group); | ||
} | ||
|
||
bindless_texture_assets.insert(bindless_texture_assets.end(), assets, assets + count); | ||
|
||
if (payload_size) | ||
{ | ||
material_payload.emplace_back(); | ||
memcpy(material_payload.back().raw, payload_data, payload_size); | ||
} | ||
|
||
return offsets; | ||
} | ||
|
||
void MaterialManager::set_material_payloads(Vulkan::CommandBuffer &cmd, unsigned set_index, unsigned binding) | ||
{ | ||
if (material_payload.empty()) | ||
{ | ||
void *data = cmd.allocate_constant_data(set_index, binding, sizeof(MaterialRawPayload)); | ||
memset(data, 0, sizeof(MaterialRawPayload)); | ||
} | ||
else | ||
{ | ||
void *data = cmd.allocate_constant_data(set_index, binding, material_payload.size() * sizeof(MaterialRawPayload)); | ||
memcpy(data, material_payload.data(), material_payload.size() * sizeof(MaterialRawPayload)); | ||
} | ||
} | ||
|
||
void MaterialManager::set_bindless(Vulkan::CommandBuffer &cmd, unsigned set_index) | ||
{ | ||
if (vk_set == VK_NULL_HANDLE) | ||
{ | ||
allocator.begin(); | ||
vk_set = allocator.commit(*device); | ||
} | ||
|
||
cmd.set_bindless(set_index, vk_set); | ||
} | ||
|
||
void MaterialManager::iterate(AssetManagerInterface *) | ||
{ | ||
auto &res = device->get_resource_manager(); | ||
allocator.begin(); | ||
for (auto &id : bindless_texture_assets) | ||
allocator.push(*res.get_image_view(id)); | ||
vk_set = allocator.commit(*device); | ||
} | ||
|
||
void MaterialManager::on_device_created(const Vulkan::DeviceCreatedEvent &e) | ||
{ | ||
device = &e.get_device(); | ||
} | ||
|
||
void MaterialManager::on_device_destroyed(const Vulkan::DeviceCreatedEvent &) | ||
{ | ||
allocator.reset(); | ||
device = nullptr; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* Copyright (c) 2017-2023 Hans-Kristian Arntzen | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining | ||
* a copy of this software and associated documentation files (the | ||
* "Software"), to deal in the Software without restriction, including | ||
* without limitation the rights to use, copy, modify, merge, publish, | ||
* distribute, sublicense, and/or sell copies of the Software, and to | ||
* permit persons to whom the Software is furnished to do so, subject to | ||
* the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be | ||
* included in all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | ||
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | ||
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
#pragma once | ||
#include "global_managers.hpp" | ||
#include "event.hpp" | ||
#include "descriptor_set.hpp" | ||
#include "asset_manager.hpp" | ||
#include "application_wsi_events.hpp" | ||
#include "hashmap.hpp" | ||
#include <assert.h> | ||
#include <stddef.h> | ||
|
||
namespace Vulkan | ||
{ | ||
class CommandBuffer; | ||
}; | ||
|
||
namespace Granite | ||
{ | ||
static constexpr uint32_t MaterialPayloadSize = 32; | ||
|
||
struct MaterialOffsets | ||
{ | ||
uint16_t texture_offset; | ||
uint16_t uniform_offset; | ||
}; | ||
|
||
class MaterialManager final : public MaterialManagerInterface, public EventHandler | ||
{ | ||
public: | ||
MaterialManager(); | ||
void iterate(AssetManagerInterface *iface) override; | ||
|
||
MaterialOffsets register_material(const AssetID *assets, unsigned count, | ||
const void *payload_data, size_t payload_size, | ||
bool force_unique = false /* can be used for "animating" material properties */); | ||
|
||
template <typename T> | ||
inline T &get_material_payload(const MaterialOffsets &offsets) | ||
{ | ||
static_assert(sizeof(T) <= MaterialPayloadSize, "sizeof(T) is too large."); | ||
static_assert(alignof(T) <= alignof(max_align_t), "alignof(T) is too large."); | ||
assert(offsets.uniform_offset < material_payload.size()); | ||
return reinterpret_cast<T &>(material_payload[offsets.uniform_offset]); | ||
} | ||
|
||
const AssetID *get_asset_ids(const MaterialOffsets &offsets) | ||
{ | ||
assert(offsets.texture_offset < bindless_texture_assets.size()); | ||
return bindless_texture_assets.data() + offsets.texture_offset; | ||
} | ||
|
||
void set_bindless(Vulkan::CommandBuffer &cmd, unsigned set_index); | ||
void set_material_payloads(Vulkan::CommandBuffer &cmd, unsigned set_index, unsigned binding); | ||
|
||
private: | ||
Vulkan::BindlessAllocator allocator; | ||
Vulkan::Device *device = nullptr; | ||
|
||
void on_device_created(const Vulkan::DeviceCreatedEvent &e); | ||
void on_device_destroyed(const Vulkan::DeviceCreatedEvent &e); | ||
|
||
struct MaterialRawPayload | ||
{ | ||
MaterialRawPayload() {} | ||
uint32_t raw[MaterialPayloadSize / sizeof(uint32_t)]; | ||
}; | ||
|
||
std::vector<MaterialRawPayload> material_payload; | ||
std::vector<AssetID> bindless_texture_assets; | ||
struct MaterialGroup : Util::IntrusiveHashMapEnabled<MaterialGroup> | ||
{ | ||
MaterialOffsets offsets; | ||
}; | ||
Util::IntrusiveHashMap<MaterialGroup> material; | ||
VkDescriptorSet vk_set = VK_NULL_HANDLE; | ||
}; | ||
} |
Oops, something went wrong.