Repository navigation
Feature/batch texture loading #723
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
5ee84aa
203e159
c0df649
b38487e
fa97344
2578830
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -80,6 +80,7 @@ namespace NovelRT::Ecs::Graphics | |
| { | ||
| uint64_t uploadIndex; | ||
| std::shared_ptr<NovelRT::Graphics::GraphicsBuffer<TGraphicsBackend>> stagingBuffer; | ||
| std::shared_ptr<NovelRT::Graphics::GraphicsResourceMemoryRegion<NovelRT::Graphics::GraphicsBuffer, TGraphicsBackend>> stagingBufferRegion; | ||
| std::shared_ptr<NovelRT::Graphics::GraphicsContext<TGraphicsBackend>> uploadContext; | ||
| std::shared_ptr<NovelRT::Graphics::GraphicsCmdList<TGraphicsBackend>> uploadCmdList; | ||
| }; | ||
|
|
@@ -110,6 +111,11 @@ namespace NovelRT::Ecs::Graphics | |
| Maths::GeoVector2F textureSize; | ||
| }; | ||
|
|
||
| struct TextureCache { | ||
| NovelRT::ResourceManagement::TextureMetadata textureMetadata; | ||
| std::shared_ptr<NovelRT::Graphics::GraphicsTexture<TGraphicsBackend>> textureRegion; | ||
| }; | ||
|
|
||
| std::shared_ptr<ResourceManagement::Desktop::DesktopResourceLoader> _resourceLoader; | ||
| std::shared_ptr<NovelRT::Graphics::GraphicsDevice<TGraphicsBackend>> _graphicsDevice; | ||
| std::shared_ptr<NovelRT::Graphics::GraphicsMemoryAllocator<TGraphicsBackend>> _memoryAllocator; | ||
|
|
@@ -134,6 +140,7 @@ namespace NovelRT::Ecs::Graphics | |
| uint64_t _frameCounter{0}; | ||
| std::shared_ptr<NovelRT::Graphics::GraphicsSemaphore<TGraphicsBackend>> _orchestratorSubmissionSemaphore; | ||
| std::optional<EntityId> _orchestratorSubmissionSemaphoreEntity; | ||
|
|
||
|
|
||
| std::unordered_map<EntityId, BufferRegion> _cameraConstantBuffers; | ||
|
|
||
|
|
@@ -144,14 +151,17 @@ namespace NovelRT::Ecs::Graphics | |
| { | ||
| lastCompletedUpload -= 1; | ||
| } | ||
|
|
||
| _pendingStagingBuffers.erase(std::remove_if(_pendingStagingBuffers.begin(), _pendingStagingBuffers.end(), | ||
| [lastCompletedUpload](const auto& pending) | ||
| { return pending.uploadIndex <= lastCompletedUpload; }), | ||
| _pendingStagingBuffers.end()); | ||
|
|
||
| auto spriteView = catalogue.GetComponentView<Components::Sprite>(); | ||
|
|
||
| std::vector<uuids::uuid> assetIds = {}; | ||
|
|
||
|
|
||
| for (auto [entityId, sprite] : spriteView) | ||
| { | ||
| if (_textureCache.find(sprite.assetId) != _textureCache.end() || | ||
|
|
@@ -162,47 +172,75 @@ namespace NovelRT::Ecs::Graphics | |
|
|
||
| _pendingLoads.insert(sprite.assetId); | ||
|
|
||
| assetIds.push_back(sprite.assetId); | ||
|
|
||
| } | ||
|
|
||
|
|
||
| if(assetIds.size() != 0) { | ||
| catalogue.ScheduleWithCompletion( | ||
| [this, assetId = sprite.assetId]() { return _resourceLoader->LoadTexture(assetId); }, | ||
| [this, assetId = sprite.assetId](Timing::Timestamp /* now */, Timing::Timestamp::duration /* delta */, Catalogue completionCatalogue, | ||
| ResourceManagement::TextureMetadata textureMetadata) | ||
| [this, assetIds]() { | ||
| const std::span<const uuids::uuid> assetSpan(assetIds); | ||
| return _resourceLoader->LoadTextures(assetSpan); }, | ||
| [this](Timing::Timestamp /* now */, Timing::Timestamp::duration /*delta*/, Catalogue completionCatalogue, | ||
| std::vector<ResourceManagement::TextureMetadata> textureMetadata) | ||
| { | ||
|
|
||
| auto texture2D = _memoryAllocator->CreateTexture2DRepeatGpuWriteOnly(textureMetadata.width, | ||
| textureMetadata.height); | ||
| std::vector<TextureCache> newTextures; | ||
| size_t totalSize = 0; | ||
| std::vector<std::shared_ptr<NovelRT::Graphics::GraphicsTexture<NovelRT::Graphics::Vulkan::VulkanGraphicsBackend>>> texture2DVector = {}; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
| for(auto texture : textureMetadata) { | ||
| auto texture2D = _memoryAllocator->CreateTexture2DRepeatGpuWriteOnly(texture.width, | ||
| texture.height); | ||
|
|
||
| totalSize += texture2D->GetSize(); | ||
|
|
||
| TextureCache textureCache = TextureCache{.textureMetadata = texture, .textureRegion = texture2D}; | ||
| newTextures.push_back(textureCache); | ||
| } | ||
|
|
||
| NovelRT::Graphics::GraphicsBufferCreateInfo bufferCreateInfo{ | ||
| .cpuAccessKind = NovelRT::Graphics::GraphicsResourceAccess::Write, | ||
| .gpuAccessKind = NovelRT::Graphics::GraphicsResourceAccess::Read, | ||
| .size = texture2D->GetSize()}; | ||
| .size = totalSize}; | ||
|
FiniteReality marked this conversation as resolved.
Resolved
|
||
|
|
||
| auto textureStagingBuffer = _memoryAllocator->CreateBuffer(bufferCreateInfo); | ||
|
|
||
| auto texture2DRegion = texture2D->Allocate(texture2D->GetSize(), 4); | ||
| auto textureStagingBufferRegion = textureStagingBuffer->Allocate(texture2D->GetSize(), 4); | ||
|
|
||
| auto pTextureData = textureStagingBuffer->template Map<uint8_t>(textureStagingBufferRegion); | ||
|
|
||
| NovelRT::Utilities::Memory::Copy(std::span<const uint8_t>(textureMetadata.data), pTextureData); | ||
|
|
||
| textureStagingBuffer->UnmapAndWrite(textureStagingBufferRegion); | ||
|
|
||
| auto gfxContext = _graphicsDevice->CreateGraphicsContext(); | ||
|
|
||
| auto uploadCmdList = gfxContext->CreateCmdList(); | ||
| uploadCmdList->Begin(); | ||
| uploadCmdList->CmdBeginTexturePipelineBarrierLegacyVersion(texture2D); | ||
| uploadCmdList->CmdCopy(texture2D, textureStagingBufferRegion); | ||
| uploadCmdList->CmdEndTexturePipelineBarrierLegacyVersion(texture2D); | ||
| uploadCmdList->End(); | ||
|
|
||
| std::vector<std::shared_ptr<NovelRT::Graphics::GraphicsCmdList<TGraphicsBackend>>> lists{ | ||
| uploadCmdList}; | ||
|
|
||
| std::vector<std::pair<std::shared_ptr<NovelRT::Graphics::GraphicsSemaphore<TGraphicsBackend>>, | ||
| uint64_t>> | ||
|
|
||
| for(auto [texture, texture2D] : newTextures) { | ||
|
|
||
| auto texture2DRegion = texture2D->Allocate(texture2D->GetSize(), 4); | ||
| auto textureStagingBufferRegion = textureStagingBuffer->Allocate(texture2D->GetSize(), 4); | ||
|
|
||
| auto pTextureData = textureStagingBuffer->template Map<uint8_t>(textureStagingBufferRegion); | ||
|
|
||
| NovelRT::Utilities::Memory::Copy(std::span<const uint8_t>(texture.data), pTextureData); | ||
|
|
||
| textureStagingBuffer->UnmapAndWrite(textureStagingBufferRegion); | ||
|
|
||
| uploadCmdList->CmdBeginTexturePipelineBarrierLegacyVersion(texture2D); | ||
| uploadCmdList->CmdCopy(texture2D, textureStagingBufferRegion); | ||
| uploadCmdList->CmdEndTexturePipelineBarrierLegacyVersion(texture2D); | ||
|
|
||
| _pendingStagingBuffers.push_back( | ||
| {_submittedUploads, textureStagingBuffer,textureStagingBufferRegion, gfxContext, uploadCmdList}); | ||
|
FiniteReality marked this conversation as resolved.
Resolved
|
||
| _textureCache.emplace( | ||
| texture.databaseHandle, TextureCacheItem{texture2DRegion, | ||
| Maths::GeoVector2F(static_cast<float>(texture.width), | ||
| static_cast<float>(texture.height))}); | ||
|
|
||
| _pendingLoads.erase(texture.databaseHandle); | ||
| } | ||
| std::vector<std::pair<std::shared_ptr<NovelRT::Graphics::GraphicsSemaphore<TGraphicsBackend>>, | ||
| uint64_t>> | ||
| signalSemaphores{std::make_pair(_uploadSemaphore, ++_submittedUploads)}; | ||
|
|
||
| uploadCmdList->End(); | ||
|
|
||
| _graphicsDevice->QueueSubmit(lists, signalSemaphores); | ||
| std::vector<std::shared_ptr<NovelRT::Graphics::GraphicsCmdList<TGraphicsBackend>>> list{uploadCmdList}; | ||
| _graphicsDevice->QueueSubmit(list, signalSemaphores); | ||
|
|
||
| Components::TrackedSemaphore<TGraphicsBackend> newUploadTracker{ | ||
| .semaphore = new std::shared_ptr<NovelRT::Graphics::GraphicsSemaphore<TGraphicsBackend>>( | ||
|
|
@@ -213,15 +251,7 @@ namespace NovelRT::Ecs::Graphics | |
|
|
||
| completionCatalogue.GetComponentView<Components::TrackedSemaphore<TGraphicsBackend>>() | ||
| .AddComponent(id, newUploadTracker); | ||
|
|
||
| _pendingStagingBuffers.push_back( | ||
| {_submittedUploads, textureStagingBuffer, gfxContext, uploadCmdList}); | ||
| _textureCache.emplace( | ||
| assetId, TextureCacheItem{texture2DRegion, | ||
| Maths::GeoVector2F(static_cast<float>(textureMetadata.width), | ||
| static_cast<float>(textureMetadata.height))}); | ||
| _pendingLoads.erase(assetId); | ||
| }); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,12 @@ | |
| #include <png.h> | ||
| #include <samplerate.h> | ||
| #include <sndfile.h> | ||
| #include <optional> | ||
| #include <span> | ||
| #include <unordered_map> | ||
| #include <unordered_set> | ||
| #include <vector> | ||
|
|
||
|
|
||
| namespace NovelRT::ResourceManagement::Desktop | ||
| { | ||
|
|
@@ -246,6 +252,18 @@ namespace NovelRT::ResourceManagement::Desktop | |
| return LoadTexture(GetGuidsToFilePathsMap().at(assetId)); | ||
| } | ||
|
|
||
| std::vector<TextureMetadata> DesktopResourceLoader::LoadTextures(const std::span<const uuids::uuid> assetIds) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure this API is necessary; we should implement it as part of the sprite renderer system.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you mean as a function or calling the function that loads one texture in a loop?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, in the SpriteRendererSystem we'd just do this instead: [this, assetIds]() {
std::vector<TextureMetadata> result(assetIds.size());
std::transform(assetIds.begin(), assetIds.end(), result.begin(), [_resourceLoader](auto& assetId){ return _resourceLoader->LoadTexture(assetId); });
return result;
} |
||
| { | ||
| std::vector<TextureMetadata> textures = {}; | ||
| for(auto assetId : assetIds) { | ||
| auto texture = LoadTexture(GetGuidsToFilePathsMap().at(assetId)); | ||
| textures.push_back(texture); | ||
| } | ||
|
|
||
| return textures; | ||
| } | ||
|
|
||
|
|
||
| ShaderMetadata DesktopResourceLoader::LoadShaderSource(std::filesystem::path filePath) | ||
| { | ||
| if (filePath.is_relative()) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: missing newline after the semicolon