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
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ struct GPUDirectionalLight {
glm::mat4 viewProjectionMatrix;
Id buffer{};
Id bindGroupData{};
wgpu::TextureView shadowTextureView;
Id shadowTextureView{};
uint32_t shadowTextureIndex;

void Update(const Object::Component::DirectionalLight &light, const Object::Component::Transform &transform)
Expand Down
2 changes: 1 addition & 1 deletion src/plugin/default-pipeline/src/resource/pass/Shadow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class Shadow : public Graphic::Resource::AMultipleExecutionRenderPass<Shadow> {
{
auto &lightGPUComponent = entity.GetComponents<Component::GPUDirectionalLight>();
auto &outputs = this->GetOutputs();
outputs.depthBuffer->depthTextureView = lightGPUComponent.shadowTextureView;
outputs.depthBuffer->depthTextureViewId = lightGPUComponent.shadowTextureView;
lightGPUComponent.shadowTextureIndex = passIndex;
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "resource/SamplerContainer.hpp"
#include "resource/Texture.hpp"
#include "resource/TextureContainer.hpp"
#include "resource/TextureViewContainer.hpp"
#include "resource/buffer/DirectionalLightBuffer.hpp"
#include "resource/pass/GBuffer.hpp"
#include "resource/pass/Shadow.hpp"
Expand All @@ -23,6 +24,7 @@ void DefaultPipeline::System::OnDirectionalLightCreation(Engine::Core &core, Eng
auto &gpuBufferContainer = core.GetResource<Graphic::Resource::GPUBufferContainer>();
auto &bindGroupManager = core.GetResource<Graphic::Resource::BindGroupManager>();
auto &textureContainer = core.GetResource<Graphic::Resource::TextureContainer>();
auto &textureViewContainer = core.GetResource<Graphic::Resource::TextureViewContainer>();
auto &samplerContainer = core.GetResource<Graphic::Resource::SamplerContainer>();
const auto &context = core.GetResource<Graphic::Resource::Context>();

Expand All @@ -47,7 +49,10 @@ void DefaultPipeline::System::OnDirectionalLightCreation(Engine::Core &core, Eng
shadowTextureViewDesc.baseArrayLayer = lightIndex;
shadowTextureViewDesc.arrayLayerCount = 1;
shadowTextureViewDesc.usage = wgpu::TextureUsage::TextureBinding | wgpu::TextureUsage::RenderAttachment;
GPUDirectionalLight.shadowTextureView = directionalShadowsTexture.CreateView(shadowTextureViewDesc);

entt::hashed_string shadowTextureViewId{textureViewName.data(), textureViewName.size()};
textureViewContainer.Add(shadowTextureViewId, directionalShadowsTexture.CreateView(shadowTextureViewDesc));
GPUDirectionalLight.shadowTextureView = shadowTextureViewId;

std::string bindGroupName = fmt::format("DIRECTIONAL_LIGHT_BIND_GROUP_{}", entity);
const uint64_t directionalLightBufferSize = Resource::DirectionalLightBuffer::DirectionalLightTransfer::GPUSize();
Expand Down
3 changes: 3 additions & 0 deletions src/plugin/graphic/src/Graphic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include "resource/Surface.hpp"
#include "resource/Texture.hpp"
#include "resource/TextureContainer.hpp"
#include "resource/TextureView.hpp"
#include "resource/TextureViewContainer.hpp"

// Utils
#include "utils/DefaultSampler.hpp"
Expand Down Expand Up @@ -93,3 +95,4 @@
#include "system/shutdown/ReleaseSampler.hpp"
#include "system/shutdown/ReleaseShader.hpp"
#include "system/shutdown/ReleaseTexture.hpp"
#include "system/shutdown/ReleaseTextureView.hpp"
7 changes: 4 additions & 3 deletions src/plugin/graphic/src/plugin/PluginGraphic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ void Graphic::Plugin::Bind()
RegisterResource(Graphic::Resource::GraphicSettings());
RegisterResource(Graphic::Resource::ShaderContainer());
RegisterResource(Graphic::Resource::TextureContainer());
RegisterResource(Graphic::Resource::TextureViewContainer());
RegisterResource(Graphic::Resource::GPUBufferContainer());
RegisterResource(Graphic::Resource::SamplerContainer());
RegisterResource(Graphic::Resource::BindGroupManager());
Expand All @@ -33,7 +34,7 @@ void Graphic::Plugin::Bind()

RegisterSystems<RenderingPipeline::Presentation>(System::Present);

RegisterSystems<Engine::Scheduler::Shutdown>(System::ReleaseGPUBuffer, System::ReleaseBindingGroup,
System::ReleaseShader, System::ReleaseTexture, System::ReleaseSampler,
System::ReleaseContext);
RegisterSystems<Engine::Scheduler::Shutdown>(
System::ReleaseGPUBuffer, System::ReleaseBindingGroup, System::ReleaseShader, System::ReleaseTextureView,
System::ReleaseTexture, System::ReleaseSampler, System::ReleaseContext);
}
16 changes: 11 additions & 5 deletions src/plugin/graphic/src/resource/AMultipleExecutionRenderPass.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "exception/FailToCreateCommandEncoderError.hpp"
#include "exception/MissingOutputRenderPassError.hpp"
#include "resource/ARenderPass.hpp"
#include "resource/TextureViewContainer.hpp"

namespace Graphic::Resource {

Expand Down Expand Up @@ -104,15 +105,17 @@ template <typename TDerived> class AMultipleExecutionRenderPass : public ARender
wgpu::RenderPassColorAttachment colorAttachment(wgpu::Default);

entt::hashed_string textureId = colorTexture.textureId;
auto textureView = core.GetResource<Resource::TextureContainer>().Get(textureId).GetDefaultView();
auto textureView =
core.GetResource<Resource::TextureContainer>().Get(textureId).GetDefaultView().GetWebGPUView();

colorAttachment.view = textureView;
if (colorTexture.textureResolveTargetName.has_value())
{
std::string resolveTargetName = colorTexture.textureResolveTargetName.value();
auto resolveTextureView = core.GetResource<Resource::TextureContainer>()
.Get(entt::hashed_string{resolveTargetName.c_str()})
.GetDefaultView();
.GetDefaultView()
.GetWebGPUView();
colorAttachment.resolveTarget = resolveTextureView;
}
colorAttachment.storeOp = colorTexture.storeOp;
Expand All @@ -138,14 +141,17 @@ template <typename TDerived> class AMultipleExecutionRenderPass : public ARender
const auto &depthTexture = this->GetOutputs().depthBuffer.value();

wgpu::TextureView depthView;
if (depthTexture.depthTextureView.has_value())
if (depthTexture.depthTextureViewId.has_value())
{
depthView = depthTexture.depthTextureView.value();
depthView = core.GetResource<Resource::TextureViewContainer>()
.Get(depthTexture.depthTextureViewId.value())
.GetWebGPUView();
}
else
{
entt::hashed_string textureId = depthTexture.textureId;
depthView = core.GetResource<Resource::TextureContainer>().Get(textureId).GetDefaultView();
depthView =
core.GetResource<Resource::TextureContainer>().Get(textureId).GetDefaultView().GetWebGPUView();
}

depthAttachment.view = depthView;
Expand Down
2 changes: 1 addition & 1 deletion src/plugin/graphic/src/resource/ARenderPass.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct ColorOutput {
struct DepthOutput {
// TODO: use textureView container
entt::hashed_string textureId{};
std::optional<wgpu::TextureView> depthTextureView;
std::optional<entt::hashed_string> depthTextureViewId = std::nullopt;
explicit DepthOutput(std::string_view textureId_ = {})
{
if (!textureId_.empty())
Expand Down
15 changes: 10 additions & 5 deletions src/plugin/graphic/src/resource/ASingleExecutionRenderPass.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "exception/FailToCreateCommandEncoderError.hpp"
#include "exception/MissingOutputRenderPassError.hpp"
#include "resource/ARenderPass.hpp"
#include "resource/TextureViewContainer.hpp"

namespace Graphic::Resource {

Expand Down Expand Up @@ -75,15 +76,17 @@ template <typename TDerived> class ASingleExecutionRenderPass : public ARenderPa
wgpu::RenderPassColorAttachment colorAttachment(wgpu::Default);

entt::hashed_string textureId = colorTexture.textureId;
auto textureView = core.GetResource<Resource::TextureContainer>().Get(textureId).GetDefaultView();
auto textureView =
core.GetResource<Resource::TextureContainer>().Get(textureId).GetDefaultView().GetWebGPUView();

colorAttachment.view = textureView;
if (colorTexture.textureResolveTargetName.has_value())
{
std::string resolveTargetName = colorTexture.textureResolveTargetName.value();
auto resolveTextureView = core.GetResource<Resource::TextureContainer>()
.Get(entt::hashed_string{resolveTargetName.c_str()})
.GetDefaultView();
.GetDefaultView()
.GetWebGPUView();
colorAttachment.resolveTarget = resolveTextureView;
}
colorAttachment.storeOp = colorTexture.storeOp;
Expand All @@ -109,14 +112,16 @@ template <typename TDerived> class ASingleExecutionRenderPass : public ARenderPa
const auto &depthTexture = this->GetOutputs().depthBuffer.value();

wgpu::TextureView depthView;
if (depthTexture.depthTextureView.has_value())
if (depthTexture.depthTextureViewId.has_value())
{
depthView = depthTexture.depthTextureView.value();
const auto &textureViewId = depthTexture.depthTextureViewId.value();
depthView = core.GetResource<Resource::TextureViewContainer>().Get(textureViewId).GetWebGPUView();
}
else
{
entt::hashed_string textureId = depthTexture.textureId;
depthView = core.GetResource<Resource::TextureContainer>().Get(textureId).GetDefaultView();
depthView =
core.GetResource<Resource::TextureContainer>().Get(textureId).GetDefaultView().GetWebGPUView();
}

depthAttachment.view = depthView;
Expand Down
2 changes: 1 addition & 1 deletion src/plugin/graphic/src/resource/BindGroup.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class BindGroup {
case Asset::Type::Texture: {
auto &textureContainer = core.GetResource<Graphic::Resource::TextureContainer>();
auto &texture = textureContainer.GetOrDefault(asset.name);
entry.textureView = texture.GetDefaultView();
entry.textureView = texture.GetDefaultView().GetWebGPUView();
break;
}
default: throw Exception::BindGroupCreationError("Unexpected Asset::Type value in _CreateBindGroupEntry");
Expand Down
19 changes: 7 additions & 12 deletions src/plugin/graphic/src/resource/Texture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "exception/UnsupportedTextureFormatError.hpp"
#include "resource/Context.hpp"
#include "resource/Image.hpp"
#include "resource/TextureView.hpp"
#include "utils/GetBytesPerPixel.hpp"
#include "utils/webgpu.hpp"
#include <array>
Expand Down Expand Up @@ -112,7 +113,7 @@ class Texture {
Texture(std::string_view name, wgpu::Texture texture, bool ownsResources = true)
: _webgpuTexture(texture), _defaultView(nullptr), _name(std::string(name)), _ownsResources(ownsResources)
{
_defaultView = _webgpuTexture.createView();
_defaultView = Resource::TextureView(_webgpuTexture.createView());
}

Texture(const Context &context, const wgpu::TextureDescriptor &descriptor)
Expand All @@ -135,8 +136,7 @@ class Texture {

~Texture()
{
if (_defaultView != nullptr)
_defaultView.release();
_defaultView.Delete();

if (_ownsResources && _webgpuTexture != nullptr)
_webgpuTexture.release();
Expand All @@ -150,7 +150,6 @@ class Texture {
_name(std::move(other._name)), _ownsResources(other._ownsResources)
{
other._webgpuTexture = nullptr;
other._defaultView = nullptr;
other._name.clear();
other._ownsResources = false;
}
Expand All @@ -159,9 +158,6 @@ class Texture {
{
if (this != &other)
{
if (_defaultView != nullptr)
_defaultView.release();

if (_ownsResources && _webgpuTexture != nullptr)
{
_webgpuTexture.release();
Expand All @@ -173,7 +169,6 @@ class Texture {
_ownsResources = other._ownsResources;

other._webgpuTexture = nullptr;
other._defaultView = nullptr;
other._name.clear();
other._ownsResources = false;
}
Expand Down Expand Up @@ -293,11 +288,11 @@ class Texture {
return cbData.data;
}

wgpu::TextureView GetDefaultView() const { return _defaultView; }
const Resource::TextureView &GetDefaultView() const { return _defaultView; }

inline wgpu::TextureView CreateView(const wgpu::TextureViewDescriptor &descriptor) const
inline Resource::TextureView CreateView(const wgpu::TextureViewDescriptor &descriptor) const
{
return _webgpuTexture.createView(descriptor);
return Resource::TextureView(_webgpuTexture.createView(descriptor));
}

void ReleaseOwnership() { _ownsResources = false; }
Expand Down Expand Up @@ -328,7 +323,7 @@ class Texture {
uint32_t _GetBytesPerPixel() const { return Utils::GetBytesPerPixel(_webgpuTexture.getFormat()); }

wgpu::Texture _webgpuTexture;
wgpu::TextureView _defaultView;
Resource::TextureView _defaultView;
std::string _name;
bool _ownsResources = true;
};
Expand Down
64 changes: 64 additions & 0 deletions src/plugin/graphic/src/resource/TextureView.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#pragma once

#include "exception/UnsupportedTextureFormatError.hpp"
#include "resource/Context.hpp"
#include "resource/Image.hpp"
#include "utils/GetBytesPerPixel.hpp"
#include "utils/webgpu.hpp"
#include <array>
#include <bit>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <functional>
#include <glm/gtc/packing.hpp>
#include <glm/vec2.hpp>
#include <glm/vec4.hpp>
#include <vector>

namespace Graphic::Resource {

class TextureView {
public:
TextureView(void) = default;
explicit TextureView(wgpu::TextureView &&textureView) : _webgpuView(std::move(textureView)) {}

~TextureView() { Delete(); }
TextureView(const TextureView &) = delete;
TextureView &operator=(const TextureView &) = delete;

TextureView(TextureView &&other) noexcept : _webgpuView(std::move(other._webgpuView))
{
other._webgpuView = nullptr;
}

TextureView &operator=(TextureView &&other) noexcept
{
if (this != &other)
{
if (_webgpuView != nullptr)
{
_webgpuView.release();
_webgpuView = nullptr;
}
_webgpuView = std::move(other._webgpuView);
other._webgpuView = nullptr;
}
return *this;
}

const wgpu::TextureView &GetWebGPUView() const { return _webgpuView; }

void Delete()
{
if (_webgpuView != nullptr)
{
_webgpuView.release();
_webgpuView = nullptr;
}
}

private:
wgpu::TextureView _webgpuView = nullptr;
};
} // namespace Graphic::Resource
11 changes: 11 additions & 0 deletions src/plugin/graphic/src/resource/TextureViewContainer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include "resource/ResourceManager.hpp"
#include "resource/TextureView.hpp"
#include "utils/webgpu.hpp"

namespace Graphic::Resource {

using TextureViewContainer = Object::Resource::ResourceManager<Resource::TextureView>;

}
4 changes: 4 additions & 0 deletions src/plugin/graphic/src/system/shutdown/ReleaseTextureView.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#include "system/shutdown/ReleaseTextureView.hpp"
#include "resource/TextureViewContainer.hpp"

void Graphic::System::ReleaseTextureView(Engine::Core &core) { core.DeleteResource<Resource::TextureViewContainer>(); }
7 changes: 7 additions & 0 deletions src/plugin/graphic/src/system/shutdown/ReleaseTextureView.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

#include "core/Core.hpp"

namespace Graphic::System {
void ReleaseTextureView(Engine::Core &core);
} // namespace Graphic::System
7 changes: 4 additions & 3 deletions src/plugin/graphic/tests/BindGroupTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,9 @@ TEST(BindGroupTest, CreatesEntriesForTextureAssets)
const auto &entries = bindGroup.GetEntries();
ASSERT_EQ(entries.size(), 3u);
EXPECT_EQ(entries.at(0).binding, 0u);
EXPECT_EQ(entries.at(0).textureView,
core.GetResource<Graphic::Resource::TextureContainer>().Get(textureId).GetDefaultView());
EXPECT_EQ(
entries.at(0).textureView,
core.GetResource<Graphic::Resource::TextureContainer>().Get(textureId).GetDefaultView().GetWebGPUView());
EXPECT_EQ(entries.at(1).binding, 1u);
EXPECT_EQ(entries.at(1).buffer, core.GetResource<Graphic::Resource::GPUBufferContainer>()
.Get(entt::hashed_string("bindgroup_buffer_asset"))
Expand Down Expand Up @@ -237,7 +238,7 @@ TEST(BindGroupTest, RefreshUpdatesTextureBindings)
textures.Remove(textureId);
textures.Add(textureId, std::move(newTexture));
}
auto updatedView = textures.Get(textureId).GetDefaultView();
auto updatedView = textures.Get(textureId).GetDefaultView().GetWebGPUView();
EXPECT_NE(bindGroup.GetEntries().at(0).textureView, updatedView);

bindGroup.Refresh(core);
Expand Down
2 changes: 1 addition & 1 deletion src/plugin/rmlui/src/utils/RenderInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ wgpu::BindGroup CreateTextureBindGroup(Engine::Core &core, const wgpu::BindGroup
{
std::array<wgpu::BindGroupEntry, 2> entries = {};
entries[0].binding = 0;
entries[0].textureView = texture.GetDefaultView();
entries[0].textureView = texture.GetDefaultView().GetWebGPUView();
entries[1].binding = 1;
entries[1].sampler = sampler;

Expand Down
Loading