Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
106 changes: 68 additions & 38 deletions Ecs/Graphics/include/NovelRT/Ecs/Graphics/SpriteRendererSystem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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() ||
Expand All @@ -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); },

Copy link
Copy Markdown
Contributor

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

[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 = {};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use TGraphicsBackend here instead of the Vulkan backend explicitly

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 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});
Comment thread
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>>(
Expand All @@ -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);
});
});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ namespace NovelRT::ResourceManagement

[[nodiscard]] virtual TextureMetadata LoadTexture(uuids::uuid assetId) = 0;


[[nodiscard]] virtual std::vector<TextureMetadata> LoadTextures(const std::span< const uuids::uuid> assetIds) = 0;


/**
* @brief Loads shader from a file on a given path.
*
Expand Down
18 changes: 18 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 Expand Up @@ -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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace NovelRT::ResourceManagement::Desktop
void InitAssetDatabase() final;
[[nodiscard]] TextureMetadata LoadTexture(std::filesystem::path filePath) final;
[[nodiscard]] TextureMetadata LoadTexture(uuids::uuid assetId) final;
[[nodiscard]] std::vector<TextureMetadata> LoadTextures(const std::span<const uuids::uuid> assetIds) final;
[[nodiscard]] ShaderMetadata LoadShaderSource(std::filesystem::path filePath) final;
[[nodiscard]] ShaderMetadata LoadShaderSource(uuids::uuid assetId) final;
[[nodiscard]] ScriptMetadata LoadScript(std::filesystem::path filePath) final;
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 @@ -108,6 +108,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 @@ -118,6 +136,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;
Expand Down
Loading