Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 71 additions & 30 deletions Ecs/Graphics/include/NovelRT/Ecs/Graphics/SpriteRendererSystem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ 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;
};
Expand Down Expand Up @@ -111,6 +114,12 @@ 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;
Expand Down Expand Up @@ -153,6 +162,8 @@ namespace NovelRT::Ecs::Graphics

auto spriteView = catalogue.GetComponentView<Components::Sprite>();

std::vector<uuids::uuid> assetIds = {};

for (auto [entityId, sprite] : spriteView)
{
if (_textureCache.find(sprite.assetId) != _textureCache.end() ||
Expand All @@ -163,47 +174,85 @@ 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]()
{
std::vector<ResourceManagement::TextureMetadata> result(assetIds.size());
std::transform(assetIds.begin(), assetIds.end(), result.begin(),
[this](auto& assetId) { return _resourceLoader->LoadTexture(assetId); });
return result;
},
[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<TGraphicsBackend>>>
texture2DVector = {};
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};
Comment thread
FiniteReality marked this conversation as resolved.
Resolved

auto textureStagingBuffer = _memoryAllocator->CreateBuffer(bufferCreateInfo);
auto gfxContext = _graphicsDevice->CreateGraphicsContext();

auto uploadCmdList = gfxContext->CreateCmdList();
uploadCmdList->Begin();

auto texture2DRegion = texture2D->Allocate(texture2D->GetSize(), 4);
auto textureStagingBufferRegion = textureStagingBuffer->Allocate(texture2D->GetSize(), 4);
for (auto [texture, texture2D] : newTextures)
{

auto pTextureData = textureStagingBuffer->template Map<uint8_t>(textureStagingBufferRegion);
auto texture2DRegion = texture2D->Allocate(texture2D->GetSize(), 4);
auto textureStagingBufferRegion = textureStagingBuffer->Allocate(texture2D->GetSize(), 4);

NovelRT::Utilities::Memory::Copy(std::span<const uint8_t>(textureMetadata.data), pTextureData);
auto pTextureData = textureStagingBuffer->template Map<uint8_t>(textureStagingBufferRegion);

textureStagingBuffer->UnmapAndWrite(textureStagingBufferRegion);
NovelRT::Utilities::Memory::Copy(std::span<const uint8_t>(texture.data), pTextureData);

auto gfxContext = _graphicsDevice->CreateGraphicsContext();
auto uploadCmdList = gfxContext->CreateCmdList();
uploadCmdList->Begin();
uploadCmdList->CmdBeginTexturePipelineBarrierLegacyVersion(texture2D);
uploadCmdList->CmdCopy(texture2D, textureStagingBufferRegion);
uploadCmdList->CmdEndTexturePipelineBarrierLegacyVersion(texture2D);
uploadCmdList->End();
textureStagingBuffer->UnmapAndWrite(textureStagingBufferRegion);

std::vector<std::shared_ptr<NovelRT::Graphics::GraphicsCmdList<TGraphicsBackend>>> lists{
uploadCmdList};
uploadCmdList->CmdBeginTexturePipelineBarrierLegacyVersion(texture2D);
uploadCmdList->CmdCopy(texture2D, textureStagingBufferRegion);
uploadCmdList->CmdEndTexturePipelineBarrierLegacyVersion(texture2D);

_pendingStagingBuffers.push_back({_submittedUploads, textureStagingBuffer,
textureStagingBufferRegion, gfxContext, uploadCmdList});
_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)};

_graphicsDevice->QueueSubmit(lists, signalSemaphores);
uploadCmdList->End();

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>>(
Expand All @@ -214,14 +263,6 @@ namespace NovelRT::Ecs::Graphics

completionCatalogue.GetComponentView<Components::TrackedSemaphore<TGraphicsBackend>>()
.AddComponent(id.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);
});
}
}
Expand Down
6 changes: 6 additions & 0 deletions ResourceManagement/Desktop/DesktopResourceLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
1 change: 1 addition & 0 deletions Samples/Ecs/Graphics/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ NovelRTBuildSystem_DeclareModule(EXECUTABLE NovelRT::Samples::Ecs::GraphicsEcsTe
Resources/Shaders/SpritePixel.spv
Resources/Shaders/SpriteVertex.spv
Resources/Images/novel-chan.png
Resources/Images/milk-carton-hi.png
)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions Samples/Ecs/Graphics/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,24 @@ class SpriteSetupSystem : public IEcsSystem
sprite.assetId = assetId;
spriteView.AddComponent(spriteEntity, sprite);

auto spriteEntity2 = catalogue.CreateEntity();


auto assetId2 = _resourceLoader->TryGetAssetIdBasedOnFilePath("Images/milk-carton-hi.png").value();

NovelRT::Ecs::Graphics::Components::Sprite sprite2{};
sprite2.assetId = assetId2;
spriteView.AddComponent(spriteEntity2, sprite2);

NovelRT::Ecs::Components::TransformComponent transform2{};
transform2.position = NovelRT::Maths::GeoVector2F(0.5f, 0.5f);
transform2.scale = NovelRT::Maths::GeoVector2F(1.0f, 1.0f);
transform2.rotationInRadians = 0.0f;
transformView.AddComponent(spriteEntity2, transform2);




NovelRT::Ecs::Components::TransformComponent transform{};
transform.position = NovelRT::Maths::GeoVector2F(0.0f, 0.0f);
transform.scale = NovelRT::Maths::GeoVector2F(1.0f, 1.0f);
Expand All @@ -117,6 +135,7 @@ class SpriteSetupSystem : public IEcsSystem
graphComp.parent = std::numeric_limits<EntityId>::max();
graphComp.childrenStartNode = std::numeric_limits<EntityId>::max();
graphView.AddComponent(spriteEntity, graphComp);
graphView.AddComponent(spriteEntity2, graphComp);

auto cameraViewportEntity = catalogue.CreateEntity();
_cameraViewportEntity = cameraViewportEntity.Id;
Expand Down
Loading