Skip to content

Commit

Permalink
Port and optimize blitter implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
crud89 committed Dec 15, 2024
1 parent a4657c7 commit b699b14
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
16 changes: 9 additions & 7 deletions src/Graphics/src/blitter_d3d12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ class Blitter<DirectX12Backend>::BlitImpl {
void initialize(const DirectX12Device& device)
{
// Allocate shader module.
Array<UniquePtr<DirectX12ShaderModule>> modules;
auto blitShader = LiteFX::Graphics::Shaders::blit_dxi::open();
modules.push_back(std::move(makeUnique<DirectX12ShaderModule>(device, ShaderStage::Compute, blitShader, LiteFX::Graphics::Shaders::blit_dxi::name(), "main")));
auto shaderProgram = DirectX12ShaderProgram::create(device, std::move(modules | std::views::as_rvalue));
Array<UniquePtr<DirectX12ShaderModule>> modules;
modules.push_back(makeUnique<DirectX12ShaderModule>(device, ShaderStage::Compute, blitShader, LiteFX::Graphics::Shaders::blit_dxi::name(), "main"));
auto shaderProgram = DirectX12ShaderProgram::create(device, modules | std::views::as_rvalue);

// Allocate descriptor set layouts.
// NOLINTBEGIN(cppcoreguidelines-avoid-magic-numbers)
Expand All @@ -58,12 +58,14 @@ class Blitter<DirectX12Backend>::BlitImpl {
// Shared interface.
// ------------------------------------------------------------------------------------------------

template <>
Blitter<DirectX12Backend>::Blitter(const DirectX12Device& device) :
m_impl(device)
{
m_impl->initialize(device);
}

template <>
void Blitter<DirectX12Backend>::generateMipMaps(IDirectX12Image& image, DirectX12CommandBuffer& commandBuffer)
{
auto device = m_impl->m_device.lock();
Expand All @@ -81,7 +83,7 @@ void Blitter<DirectX12Backend>::generateMipMaps(IDirectX12Image& image, DirectX1
// Create the array of parameter data.
Array<Parameters> parametersData(image.levels());

std::ranges::generate(parametersData, [this, &image, i = 0]() mutable {
std::ranges::generate(parametersData, [&image, i = 0]() mutable {
auto level = i++;

return Parameters{
Expand All @@ -92,7 +94,7 @@ void Blitter<DirectX12Backend>::generateMipMaps(IDirectX12Image& image, DirectX1
});

auto parametersBlock = parametersData |
std::views::transform([](const Parameters& parameters) { return reinterpret_cast<const void*>(&parameters); }) |
std::views::transform([](const Parameters& parameters) { return static_cast<const void*>(&parameters); }) |
std::ranges::to<Array<const void*>>();

// Set the active pipeline state.
Expand Down Expand Up @@ -135,7 +137,7 @@ void Blitter<DirectX12Backend>::generateMipMaps(IDirectX12Image& image, DirectX1

// Dispatch the pipeline.
commandBuffer.bind(*(*resource), pipeline);
commandBuffer.dispatch({ std::max<UInt32>(static_cast<UInt32>(size.width()) / 8, 1), std::max<UInt32>(static_cast<UInt32>(size.height()) / 8, 1), 1 });
commandBuffer.dispatch({ std::max<UInt32>(static_cast<UInt32>(size.width()) / 8, 1), std::max<UInt32>(static_cast<UInt32>(size.height()) / 8, 1), 1 }); // NOLINT(cppcoreguidelines-avoid-magic-numbers)

// Wait for all writes.
DirectX12Barrier subBarrier(PipelineStage::Compute, PipelineStage::Compute);
Expand All @@ -155,6 +157,6 @@ void Blitter<DirectX12Backend>::generateMipMaps(IDirectX12Image& image, DirectX1
// Export definition.
// ------------------------------------------------------------------------------------------------

template class LITEFX_GRAPHICS_API Blitter<Backends::DirectX12Backend>;
template class LITEFX_GRAPHICS_API LiteFX::Graphics::Blitter<Backends::DirectX12Backend>;

#endif // LITEFX_BUILD_DIRECTX_12_BACKEND
4 changes: 3 additions & 1 deletion src/Graphics/src/blitter_vk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ class Blitter<VulkanBackend>::BlitImpl {
// Shared interface.
// ------------------------------------------------------------------------------------------------

template <>
Blitter<VulkanBackend>::Blitter(const VulkanDevice& /*device*/) :
m_impl()
{
}

template <>
void Blitter<VulkanBackend>::generateMipMaps(IVulkanImage& image, VulkanCommandBuffer& commandBuffer)
{
VulkanBarrier startBarrier(PipelineStage::None, PipelineStage::Transfer);
Expand Down Expand Up @@ -89,6 +91,6 @@ void Blitter<VulkanBackend>::generateMipMaps(IVulkanImage& image, VulkanCommandB
// Export definition.
// ------------------------------------------------------------------------------------------------

template class LITEFX_GRAPHICS_API Blitter<Backends::VulkanBackend>;
template class LITEFX_GRAPHICS_API LiteFX::Graphics::Blitter<Backends::VulkanBackend>;

#endif // LITEFX_BUILD_VULKAN_BACKEND
2 changes: 1 addition & 1 deletion src/Samples/Textures/src/sample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ void loadTexture(TDevice& device, SharedPtr<typename TDevice::image_type>& textu
}

template<render_backend TBackend, typename TDevice = TBackend::device_type>
UInt64 initBuffers(SampleApp& app, TDevice& device, SharedPtr<IInputAssembler> inputAssembler)
UInt64 initBuffers(SampleApp& app, TDevice& device, const SharedPtr<IInputAssembler>& inputAssembler)
{
using image_type = TDevice::image_type;
using sampler_type = TDevice::sampler_type;
Expand Down

0 comments on commit b699b14

Please sign in to comment.