Skip to content

Commit

Permalink
Issue #34 #33: Expose API to update render target initially
Browse files Browse the repository at this point in the history
Changes:
fix: this allows us to update the light texture and upload it directly
to the GPU. This will have minimal bandwidth update between frame calls.

Modules:
D3D12RenderSystem
DeferredRenderer
  • Loading branch information
vasumahesh1 committed Oct 24, 2018
1 parent 2091792 commit 99741c1
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 13 deletions.
6 changes: 6 additions & 0 deletions Source/Azura/RenderSystem/Inc/D3D12/D3D12Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class D3D12Renderer : public Renderer {

void SnapshotFrame(const String& exportPath) const override;

void BindRenderTarget(U32 renderTargetId, const TextureDesc& desc, const U8* buffer) override;

private:
void AddShader(const ShaderCreateInfo& info) override;

Expand All @@ -69,6 +71,10 @@ class D3D12Renderer : public Renderer {

D3D12ScopedImage m_depthTexture{};

D3D12ScopedBuffer m_stagingBuffer;

Containers::Vector<TextureBufferInfo> m_renderTargetUpdates;

Containers::Vector<std::pair<U32, RenderPassType>> m_renderSequence;

Containers::Vector<D3D12ScopedImage> m_renderTargetImages;
Expand Down
5 changes: 5 additions & 0 deletions Source/Azura/RenderSystem/Inc/Generic/GenericTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,12 @@ constexpr U32 MAX_RENDER_PASS_OUTPUTS = 4;
constexpr U32 MAX_RENDER_PASS_SETS = 8;
constexpr U32 MAX_RENDER_PASS_SHADERS = 4;

struct MemoryRequirements {
U32 m_stagingBufferSize{0x4000000};
};

struct ApplicationRequirements {
MemoryRequirements m_renderer;
};

struct SwapChainRequirements {
Expand Down
2 changes: 2 additions & 0 deletions Source/Azura/RenderSystem/Inc/Generic/Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class Renderer {

virtual void SnapshotFrame(const String& exportPath) const = 0;

virtual void BindRenderTarget(U32 renderTargetId, const TextureDesc& desc, const U8* buffer) = 0;

protected:
const ApplicationInfo& GetApplicationInfo() const;
const DeviceRequirements& GetDeviceRequirements() const;
Expand Down
2 changes: 2 additions & 0 deletions Source/Azura/RenderSystem/Inc/Vulkan/VkRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ class VkRenderer : public Renderer {

void SnapshotFrame(const String& exportPath) const override;

void BindRenderTarget(U32 renderTargetId, const TextureDesc& desc, const U8* buffer) override;

private:
void AddShader(const ShaderCreateInfo& info) override;

Expand Down
51 changes: 50 additions & 1 deletion Source/Azura/RenderSystem/Src/D3D12/D3D12Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ D3D12Renderer::D3D12Renderer(const ApplicationInfo& appInfo,
m_initBuffer(0x400000),
m_initAllocator(m_initBuffer, 0x400000),
m_renderSequence(renderPassRequirements.m_passSequence.GetSize(), mainAllocator),
m_renderTargetUpdates(renderPassRequirements.m_targets.GetSize(), m_initAllocator),
m_renderTargetImages(renderPassRequirements.m_targets.GetSize(), mainAllocator),
m_renderPasses(renderPassRequirements.m_passSequence.GetSize(), mainAllocator),
m_computePasses(renderPassRequirements.m_passSequence.GetSize(), mainAllocator),
Expand Down Expand Up @@ -92,6 +93,12 @@ D3D12Renderer::D3D12Renderer(const ApplicationInfo& appInfo,

SetCurrentFrame(m_swapChain.RealComPtr()->GetCurrentBackBufferIndex());

// Create Buffers
m_stagingBuffer.Create(m_device, CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD), GetApplicationRequirements().m_renderer.m_stagingBufferSize,
D3D12_RESOURCE_STATE_GENERIC_READ, log_D3D12RenderSystem);
m_stagingBuffer.Map();


for (const auto& shaderCreateInfo : shaderRequirements.m_shaders) {
D3D12Renderer::AddShader(shaderCreateInfo);
}
Expand Down Expand Up @@ -229,7 +236,27 @@ ComputePool& D3D12Renderer::CreateComputePool(const ComputePoolCreateInfo& creat
void D3D12Renderer::Submit() {
LOG_DBG(log_D3D12RenderSystem, LOG_LEVEL, "Submitting Renderer");

STACK_ALLOCATOR(Temporary, Memory::MonotonicAllocator, 4096);
HEAP_ALLOCATOR(Temporary, Memory::MonotonicAllocator, 8192);

if (m_renderTargetUpdates.GetSize() > 0) {
auto oneTimeSubmitBuffer = D3D12ScopedCommandBuffer(m_device, D3D12_COMMAND_LIST_TYPE_DIRECT, log_D3D12RenderSystem);
oneTimeSubmitBuffer.CreateGraphicsCommandList(m_device, nullptr, log_D3D12RenderSystem);
auto oneTimeCommandList = oneTimeSubmitBuffer.GetGraphicsCommandList();

for (const auto& renderTargetUpdate : m_renderTargetUpdates)
{
// TODO(vasumahesh1):[STATES]: Get current state and don't hardcode
auto& targetImage = m_renderTargetImages[renderTargetUpdate.m_binding];
targetImage.Transition(oneTimeCommandList, D3D12_RESOURCE_STATE_COMMON, D3D12_RESOURCE_STATE_COPY_DEST);
targetImage.CopyFromBuffer(m_device, oneTimeCommandList, m_stagingBuffer, renderTargetUpdate.m_offset);
targetImage.Transition(oneTimeCommandList, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_COMMON);
}

oneTimeCommandList->Close();
oneTimeSubmitBuffer.Execute(m_device, m_mainGraphicsCommandQueue.Get(), log_D3D12RenderSystem);
oneTimeSubmitBuffer.WaitForComplete(m_mainGraphicsCommandQueue.Get(), log_D3D12RenderSystem);
}


for (auto& drawablePool : m_drawablePools) {
drawablePool.Submit();
Expand Down Expand Up @@ -396,5 +423,27 @@ void D3D12Renderer::AddShader(const ShaderCreateInfo& info) {

m_shaders.Last().SetStage(info.m_stage);
}

void D3D12Renderer::BindRenderTarget(U32 renderTargetId, const TextureDesc& desc, const U8* buffer) {
LOG_DBG(log_D3D12RenderSystem, LOG_LEVEL,
"D3D12 Render Target: Updating Render Target: %d of Size: %d bytes", renderTargetId, desc.m_size);

const U32 size = desc.m_size;

const U32 textureWidthBytes = desc.m_bounds.m_width * GetFormatSize(desc.m_format);

// TODO(vasumahesh1):[INPUT]: Could be an issue with sizeof(float)
const U32 offset = m_stagingBuffer.AppendTextureData(buffer, size, 512, textureWidthBytes, D3D12_TEXTURE_DATA_PITCH_ALIGNMENT, log_D3D12RenderSystem);

TextureBufferInfo info = TextureBufferInfo();
info.m_byteSize = size;
info.m_offset = offset;
info.m_desc = desc;
info.m_binding = renderTargetId;
info.m_set = 0;

m_renderTargetUpdates.PushBack(info);
}

} // namespace D3D12
} // namespace Azura
6 changes: 6 additions & 0 deletions Source/Azura/RenderSystem/Src/Vulkan/VkRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,12 @@ void VkRenderer::SnapshotFrame(const String& exportPath) const {
height);
}

void VkRenderer::BindRenderTarget(U32 renderTargetId, const TextureDesc& desc, const U8* buffer) {
UNUSED(renderTargetId);
UNUSED(desc);
UNUSED(buffer);
}

void VkRenderer::AddShader(const ShaderCreateInfo& info) {
// TODO(vasumahesh1):[ASSETS]: Manage assets
const String fullPath = "Shaders/" + VkRenderer::GetRenderingAPI() + "/" + info.m_shaderFileName;
Expand Down
6 changes: 6 additions & 0 deletions Source/Samples/2_DeferredRenderer/Inc/App/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,10 @@ struct MeshObject
U32 m_normalDataSize{0};
};

struct LightUBO
{
float timeDelta{ 0.0f };
float _pad[3];
};

} // namespace Azura
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class ForwardComputeScene : public Scene {
ForwardComputePass m_pass{};
DrawablePool* m_mainPool{nullptr};
ComputePool* m_computePool{nullptr};
LightTexture m_lightTexture;
};

} // namespace Azura
3 changes: 2 additions & 1 deletion Source/Samples/2_DeferredRenderer/Shaders/Common.slang
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ struct Light {
};

struct LightUBO {
Light lights[NUM_LIGHTS];
float timeDelta;
float3 _pad;
};

float cubicGaussian(float h) {
Expand Down
19 changes: 15 additions & 4 deletions Source/Samples/2_DeferredRenderer/Shaders/ForwardCompute.cs.slang
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ struct RWBlock {
RWTexture2D<float4> rwTexture;
};

ParameterBlock<LightUBO> lightBlock;
const static float LIGHT_DT = -2.5;
const static float3 LIGHT_MIN = {-5.0, 0.0, -5.0 };
const static float3 LIGHT_MAX = { 5.0, 15.0, 5.0 };


ParameterBlock<LightUBO> lightUBO;
ParameterBlock<RWBlock> rwBlock;

const static int lightsPerThread = NUM_LIGHTS / 32;
Expand All @@ -15,11 +20,17 @@ void main(uint3 DTid : SV_DispatchThreadID)
int offset = lightsPerThread * DTid.x;

for(int idx = offset; idx < offset + lightsPerThread; ++idx) {
Light currentLight = lightBlock.lights[idx];
float2 upper = float2(idx, 0);
float2 lower = float2(idx, 1);

rwBlock.rwTexture[upper] = float4(currentLight.position, currentLight.radius);
rwBlock.rwTexture[lower] = float4(currentLight.color, 1.0);
float4 currentLightPosRad = rwBlock.rwTexture[upper];

currentLightPosRad.y += (LIGHT_DT * lightUBO.timeDelta);

if (currentLightPosRad.y < LIGHT_MIN.y) {
currentLightPosRad.y = LIGHT_MAX.y;
}

rwBlock.rwTexture[upper] = float4(currentLightPosRad.xyz, currentLightPosRad.w);
}
}
19 changes: 12 additions & 7 deletions Source/Samples/2_DeferredRenderer/Src/ForwardComputeScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Azura {


ForwardComputeScene::ForwardComputeScene(Memory::Allocator& mainAllocator, Memory::Allocator& drawAllocator)
: Scene("ForwardComputeScene", mainAllocator, drawAllocator) {
: Scene("ForwardComputeScene", mainAllocator, drawAllocator), m_lightTexture(NUM_LIGHTS, 7, mainAllocator) {
}

void ForwardComputeScene::Initialize(Window& window,
Expand Down Expand Up @@ -124,6 +124,8 @@ void ForwardComputeScene::Initialize(Window& window,
m_textureManager = RenderSystem::CreateTextureManager(textureRequirements);

// Load Assets
m_lightTexture.Fill(lights);
m_renderer->BindRenderTarget(LIGHT_TARGET, m_lightTexture.GetTextureDesc(), m_lightTexture.GetBuffer());

ComputePoolCreateInfo computePoolInfo = { allocatorTemporary };
computePoolInfo.m_byteSize = 0xF00000;
Expand All @@ -133,9 +135,9 @@ void ForwardComputeScene::Initialize(Window& window,
ComputePool& computePool = m_renderer->CreateComputePool(computePoolInfo);
m_computePool = &computePool;

const auto lightUBOStart = reinterpret_cast<const U8*>(lights.Data()); // NOLINT

computePool.BindUniformData(m_pass.m_computeUBO, lightUBOStart, sizeof(PointLight) * lights.GetSize());
const LightUBO temp{};
const auto lightUBOStart = reinterpret_cast<const U8*>(&temp); // NOLINT
computePool.BindUniformData(m_pass.m_computeUBO, lightUBOStart, sizeof(LightUBO));

DrawablePoolCreateInfo poolInfo = {allocatorTemporary};
poolInfo.m_byteSize = 0xF00000;
Expand Down Expand Up @@ -201,13 +203,16 @@ void ForwardComputeScene::Update(float timeDelta,
const UniformBufferData& uboData,
const Containers::Vector<PointLight>& lights) {

UNUSED(timeDelta);
UNUSED(lights);

LightUBO frameUBO = {};
frameUBO.timeDelta = timeDelta;

const auto uboDataBuffer = reinterpret_cast<const U8*>(&uboData); // NOLINT
const auto lightUBOStart = reinterpret_cast<const U8*>(lights.Data()); // NOLINT
const auto lightUBOStart = reinterpret_cast<const U8*>(&frameUBO); // NOLINT

m_computePool->BeginUpdates();
m_computePool->UpdateUniformData(m_pass.m_computeUBO, lightUBOStart, sizeof(PointLight) * lights.GetSize());
m_computePool->UpdateUniformData(m_pass.m_computeUBO, lightUBOStart, sizeof(LightUBO));
m_computePool->SubmitUpdates();

m_mainPool->BeginUpdates();
Expand Down

0 comments on commit 99741c1

Please sign in to comment.