Skip to content

Commit

Permalink
Merge pull request #128 from crud89/std-format
Browse files Browse the repository at this point in the history
Use `std::format` for formatting.
  • Loading branch information
crud89 authored May 1, 2024
2 parents f2444d6 + b199641 commit fe627e6
Show file tree
Hide file tree
Showing 52 changed files with 299 additions and 243 deletions.
1 change: 0 additions & 1 deletion NOTICE
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ contact us by opening an issue:
The following third party material may be included and re-distributed within
LiteFX releases:

- {fmt}: Copyright (c) 2012 - present, Victor Zverovich (1)
- spdlog: Copyright (c) 2016 Gabi Melman (1)
- Vulkan Memory Allocator: (c) 2017-2019 Advanced Micro Devices, Inc. All
rights reserved. (1)
Expand Down
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,11 @@ If you are having problems building the project, you may find answers [in the wi

### Dependencies

All dependencies are automatically installed using *vcpkg*, when performing a manual build. The engine only has two hard dependencies:
All dependencies are automatically installed using *vcpkg*, when performing a manual build. The engine core by itself only has one hard dependency:

- [spdlog](https://github.com/gabime/spdlog): Lightweight logging library.
- [{fmt}](https://github.com/fmtlib/fmt): String formatting library and implicit dependency of *spdlog*.

Depending on which rendering backends are build, the following dependencies are required:
Depending on which rendering backends are build, the following dependencies are additionally linked against:

- [Vulkan SDK](https://www.lunarg.com/vulkan-sdk/): Required by the Vulkan backend.
- [Vulkan Memory Allocator](https://gpuopen.com/vulkan-memory-allocator/): Required by the Vulkan backend. Handles memory allocations.
Expand Down
3 changes: 2 additions & 1 deletion docs/release-logs/0.4.1.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# LiteFX 0.4.1 - Alpha 04

- Adapt C++23 where applicable. ([See PR #98](https://github.com/crud89/LiteFX/pull/98), [PR #102](https://github.com/crud89/LiteFX/pull/102) and [PC #113](https://github.com/crud89/LiteFX/pull/113)) This includes:
- Adapt C++23 where applicable. ([See PR #98](https://github.com/crud89/LiteFX/pull/98), [PR #102](https://github.com/crud89/LiteFX/pull/102) and [PR #113](https://github.com/crud89/LiteFX/pull/113)) This includes:
- Many of the range adaptors could be simplified.
- The adaptor `ranges::to` has been replaced with the STL counterpart.
- A novel `Enumerable` container is introduced to pass around immutable collections.
- Some places that previously used `std::ranges::generate` or `std::generate` now use `std::generator`.
- Builders are now `constexpr` where possible and are implemented using `deducing this` in place of CRTP, which makes them more lightweight.
- New exceptions with support for `stacktrace` and `source_location`.
- Replace `fmt` with `std::format`. ([See PR #128](https://github.com/crud89/LiteFX/pull/128))
- Updated vcpkg to version 2023.11.20. ([See PR #104](https://github.com/crud89/LiteFX/pull/104))
- The namespace `rtti` has been renamed to `meta`. ([See PR #121](https://github.com/crud89/LiteFX/pull/121))
- Improvements to C++ core guideline conformance. ([See PR #103](https://github.com/crud89/LiteFX/pull/103))
Expand Down
19 changes: 8 additions & 11 deletions src/AppModel/include/litefx/app_formatters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
#include <litefx/app_api.hpp>

template <>
struct LITEFX_APPMODEL_API fmt::formatter<LiteFX::Platform> : formatter<string_view> {
template <typename FormatContext>
auto format(LiteFX::Platform t, FormatContext& ctx) const {
struct LITEFX_APPMODEL_API std::formatter<LiteFX::Platform> : std::formatter<std::string_view> {
auto format(LiteFX::Platform t, std::format_context& ctx) const {
string_view name = "Invalid";
switch (t) {
case LiteFX::Platform::Win32: name = "Win32"; break;
Expand All @@ -17,9 +16,8 @@ struct LITEFX_APPMODEL_API fmt::formatter<LiteFX::Platform> : formatter<string_v
};

template <>
struct LITEFX_APPMODEL_API fmt::formatter<LiteFX::BackendType> : formatter<string_view> {
template <typename FormatContext>
auto format(LiteFX::BackendType t, FormatContext& ctx) const {
struct LITEFX_APPMODEL_API std::formatter<LiteFX::BackendType> : std::formatter<std::string_view> {
auto format(LiteFX::BackendType t, std::format_context& ctx) const {
string_view name = "Invalid";
switch (t) {
case LiteFX::BackendType::Rendering: name = "Rendering"; break;
Expand All @@ -31,7 +29,7 @@ struct LITEFX_APPMODEL_API fmt::formatter<LiteFX::BackendType> : formatter<strin
};

template <>
struct fmt::formatter<LiteFX::AppVersion> {
struct std::formatter<LiteFX::AppVersion> {
bool engineVersion = false;

constexpr auto parse(format_parse_context& ctx) {
Expand All @@ -49,10 +47,9 @@ struct fmt::formatter<LiteFX::AppVersion> {
return it;
}

template <typename FormatContext>
auto format(const LiteFX::AppVersion& app, FormatContext& ctx) {
auto format(const LiteFX::AppVersion& app, std::format_context& ctx) const {
return engineVersion ?
fmt::format_to(ctx.out(), "{} Version {}", app.engineIdentifier(), app.engineVersion()) :
fmt::format_to(ctx.out(), "{}.{}.{}.{}", app.major(), app.minor(), app.patch(), app.revision());
std::format_to(ctx.out(), "{} Version {}", app.engineIdentifier(), app.engineVersion()) :
std::format_to(ctx.out(), "{}.{}.{}.{}", app.major(), app.minor(), app.patch(), app.revision());
}
};
4 changes: 3 additions & 1 deletion src/Backends/DirectX12/include/litefx/backends/dx12_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ using namespace Microsoft::WRL;
#include <litefx/config.h>
#include <litefx/rendering.hpp>

#include "dx12_formatters.hpp"

namespace LiteFX::Rendering::Backends {
using namespace LiteFX::Math;
using namespace LiteFX::Rendering;
Expand Down Expand Up @@ -311,7 +313,7 @@ namespace LiteFX::Rendering::Backends {
/// <param name="args">The arguments passed to the error message format string.</param>
template <typename ...TArgs>
explicit DX12PlatformException(HRESULT result, StringView format, TArgs&&... args) noexcept :
DX12PlatformException(result, fmt::vformat(format, fmt::make_format_args(args...))) { }
DX12PlatformException(result, std::vformat(format, std::make_format_args(args...))) { }

DX12PlatformException(const DX12PlatformException&) = default;
DX12PlatformException(DX12PlatformException&&) = default;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
#include "dx12_api.hpp"

template <>
struct LITEFX_DIRECTX12_API fmt::formatter<D3D12_MESSAGE_ID> : formatter<string_view> {
template <typename FormatContext>
auto format(D3D12_MESSAGE_ID t, FormatContext& ctx) {
struct LITEFX_DIRECTX12_API std::formatter<D3D12_MESSAGE_ID> : std::formatter<std::string_view> {
auto format(D3D12_MESSAGE_ID t, std::format_context& ctx) const {
string_view name;

switch (t)
Expand Down Expand Up @@ -859,9 +858,8 @@ struct LITEFX_DIRECTX12_API fmt::formatter<D3D12_MESSAGE_ID> : formatter<string_
};

template <>
struct LITEFX_DIRECTX12_API fmt::formatter<D3D12_ROOT_PARAMETER_TYPE> : formatter<string_view> {
template <typename FormatContext>
auto format(D3D12_ROOT_PARAMETER_TYPE t, FormatContext& ctx) {
struct LITEFX_DIRECTX12_API std::formatter<D3D12_ROOT_PARAMETER_TYPE> : std::formatter<std::string_view> {
auto format(D3D12_ROOT_PARAMETER_TYPE t, std::format_context& ctx) const {
std::string_view name;

switch (t)
Expand All @@ -879,9 +877,8 @@ struct LITEFX_DIRECTX12_API fmt::formatter<D3D12_ROOT_PARAMETER_TYPE> : formatte
};

template <>
struct LITEFX_DIRECTX12_API fmt::formatter<D3D_SHADER_INPUT_TYPE> : formatter<string_view> {
template <typename FormatContext>
auto format(D3D_SHADER_INPUT_TYPE t, FormatContext& ctx) {
struct LITEFX_DIRECTX12_API std::formatter<D3D_SHADER_INPUT_TYPE> : std::formatter<std::string_view> {
auto format(D3D_SHADER_INPUT_TYPE t, std::format_context& ctx) const {
std::string_view name;

switch (t)
Expand Down
6 changes: 3 additions & 3 deletions src/Backends/DirectX12/src/buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ UniquePtr<IDirectX12Buffer> DirectX12Buffer::allocate(const String& name, Alloca
ComPtr<ID3D12Resource> resource;
D3D12MA::Allocation* allocation;
raiseIfFailed(allocator->CreateResource3(&allocationDesc, &resourceDesc, D3D12_BARRIER_LAYOUT_UNDEFINED, nullptr, 0, nullptr, &allocation, IID_PPV_ARGS(&resource)), "Unable to allocate buffer.");
LITEFX_DEBUG(DIRECTX12_LOG, "Allocated buffer {0} with {4} bytes {{ Type: {1}, Elements: {2}, Element Size: {3}, Usage: {5} }}", name.empty() ? fmt::to_string(fmt::ptr(resource.Get())) : name, type, elements, elementSize, elements * elementSize, usage);
LITEFX_DEBUG(DIRECTX12_LOG, "Allocated buffer {0} with {4} bytes {{ Type: {1}, Elements: {2}, Element Size: {3}, Usage: {5} }}", name.empty() ? std::format("{0}", reinterpret_cast<void*>(resource.Get())) : name, type, elements, elementSize, elements * elementSize, usage);

return makeUnique<DirectX12Buffer>(std::move(resource), type, elements, elementSize, alignment, usage, allocator, AllocationPtr(allocation), name);
}
Expand Down Expand Up @@ -234,7 +234,7 @@ UniquePtr<IDirectX12VertexBuffer> DirectX12VertexBuffer::allocate(const String&
ComPtr<ID3D12Resource> resource;
D3D12MA::Allocation* allocation;
raiseIfFailed(allocator->CreateResource3(&allocationDesc, &resourceDesc, D3D12_BARRIER_LAYOUT_UNDEFINED, nullptr, 0, nullptr, &allocation, IID_PPV_ARGS(&resource)), "Unable to allocate vertex buffer.");
LITEFX_DEBUG(DIRECTX12_LOG, "Allocated buffer {0} with {4} bytes {{ Type: {1}, Elements: {2}, Element Size: {3}, Usage: {5} }}", name.empty() ? fmt::to_string(fmt::ptr(resource.Get())) : name, BufferType::Vertex, elements, layout.elementSize(), layout.elementSize() * elements, usage);
LITEFX_DEBUG(DIRECTX12_LOG, "Allocated buffer {0} with {4} bytes {{ Type: {1}, Elements: {2}, Element Size: {3}, Usage: {5} }}", name.empty() ? std::format("{0}", reinterpret_cast<void*>(resource.Get())) : name, BufferType::Vertex, elements, layout.elementSize(), layout.elementSize() * elements, usage);

return makeUnique<DirectX12VertexBuffer>(std::move(resource), layout, elements, usage, allocator, AllocationPtr(allocation), name);
}
Expand Down Expand Up @@ -304,7 +304,7 @@ UniquePtr<IDirectX12IndexBuffer> DirectX12IndexBuffer::allocate(const String& na
ComPtr<ID3D12Resource> resource;
D3D12MA::Allocation* allocation;
raiseIfFailed(allocator->CreateResource3(&allocationDesc, &resourceDesc, D3D12_BARRIER_LAYOUT_UNDEFINED, nullptr, 0, nullptr, &allocation, IID_PPV_ARGS(&resource)), "Unable to allocate vertex buffer.");
LITEFX_DEBUG(DIRECTX12_LOG, "Allocated buffer {0} with {4} bytes {{ Type: {1}, Elements: {2}, Element Size: {3}, Usage: {5} }}", name.empty() ? fmt::to_string(fmt::ptr(resource.Get())) : name, BufferType::Index, elements, layout.elementSize(), layout.elementSize() * elements, usage);
LITEFX_DEBUG(DIRECTX12_LOG, "Allocated buffer {0} with {4} bytes {{ Type: {1}, Elements: {2}, Element Size: {3}, Usage: {5} }}", name.empty() ? std::format("{0}", reinterpret_cast<void*>(resource.Get())) : name, BufferType::Index, elements, layout.elementSize(), layout.elementSize() * elements, usage);

return makeUnique<DirectX12IndexBuffer>(std::move(resource), layout, elements, usage, allocator, AllocationPtr(allocation), name);
}
2 changes: 1 addition & 1 deletion src/Backends/DirectX12/src/compute_pipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class DirectX12ComputePipeline::DirectX12ComputePipelineImpl : public Implement<

// Setup shader stages.
auto modules = m_program->modules();
LITEFX_TRACE(DIRECTX12_LOG, "Using shader program {0} with {1} modules...", fmt::ptr(m_program.get()), modules.size());
LITEFX_TRACE(DIRECTX12_LOG, "Using shader program {0} with {1} modules...", reinterpret_cast<void*>(m_program.get()), modules.size());

std::ranges::for_each(modules, [&, i = 0](const DirectX12ShaderModule* shaderModule) mutable {
LITEFX_TRACE(DIRECTX12_LOG, "\tModule {0}/{1} (\"{2}\") state: {{ Type: {3}, EntryPoint: {4} }}", ++i, modules.size(), shaderModule->fileName(), shaderModule->type(), shaderModule->entryPoint());
Expand Down
2 changes: 1 addition & 1 deletion src/Backends/DirectX12/src/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ DirectX12Device::DirectX12Device(const DirectX12Backend& backend, const DirectX1
DirectX12Device::DirectX12Device(const DirectX12Backend& backend, const DirectX12GraphicsAdapter& adapter, UniquePtr<DirectX12Surface>&& surface, Format format, const Size2d& renderArea, UInt32 backBuffers, bool enableVsync, GraphicsDeviceFeatures features, UInt32 globalBufferHeapSize, UInt32 globalSamplerHeapSize) :
ComResource<ID3D12Device10>(nullptr), m_impl(makePimpl<DirectX12DeviceImpl>(this, adapter, std::move(surface), backend, globalBufferHeapSize, globalSamplerHeapSize))
{
LITEFX_DEBUG(DIRECTX12_LOG, "Creating DirectX 12 device {{ Surface: {0}, Adapter: {1} }}...", fmt::ptr(&surface), adapter.deviceId());
LITEFX_DEBUG(DIRECTX12_LOG, "Creating DirectX 12 device {{ Surface: {0}, Adapter: {1} }}...", reinterpret_cast<void*>(&surface), adapter.deviceId());
LITEFX_DEBUG(DIRECTX12_LOG, "--------------------------------------------------------------------------");
LITEFX_DEBUG(DIRECTX12_LOG, "Vendor: {0:#0x} (\"{1}\")", adapter.vendorId(), DX12::getVendorName(adapter.vendorId()).c_str());
LITEFX_DEBUG(DIRECTX12_LOG, "Driver Version: {0:#0x}", adapter.driverVersion());
Expand Down
2 changes: 1 addition & 1 deletion src/Backends/DirectX12/src/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ UniquePtr<DirectX12Image> DirectX12Image::allocate(const String& name, const Dir
ComPtr<ID3D12Resource> resource;
D3D12MA::Allocation* allocation;
raiseIfFailed(allocator->CreateResource3(&allocationDesc, &resourceDesc, isDepthStencil ? D3D12_BARRIER_LAYOUT_DEPTH_STENCIL_READ : D3D12_BARRIER_LAYOUT_COMMON, nullptr, 0, nullptr, &allocation, IID_PPV_ARGS(&resource)), "Unable to create image resource.");
LITEFX_DEBUG(DIRECTX12_LOG, "Allocated image {0} with {1} bytes {{ Extent: {2}x{3} Px, Format: {4}, Levels: {5}, Layers: {6}, Samples: {8}, Usage: {7} }}", name.empty() ? fmt::to_string(fmt::ptr(resource.Get())) : name, ::getSize(format) * extent.width() * extent.height(), extent.width(), extent.height(), format, levels, layers, usage, samples);
LITEFX_DEBUG(DIRECTX12_LOG, "Allocated image {0} with {1} bytes {{ Extent: {2}x{3} Px, Format: {4}, Levels: {5}, Layers: {6}, Samples: {8}, Usage: {7} }}", name.empty() ? std::format("{0}", reinterpret_cast<void*>(resource.Get())) : name, ::getSize(format) * extent.width() * extent.height(), extent.width(), extent.height(), format, levels, layers, usage, samples);

return makeUnique<DirectX12Image>(device, std::move(resource), extent, format, dimension, levels, layers, samples, usage, allocator, AllocationPtr(allocation), name);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Backends/DirectX12/src/pipeline_layout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class DirectX12PipelineLayout::DirectX12PipelineLayoutImpl : public Implement<Di
bool hasInputAttachmentSampler = false;
UInt32 rootParameterIndex{ 0 };

LITEFX_TRACE(DIRECTX12_LOG, "Creating render pipeline layout {0} {{ Descriptor Sets: {1}, Push Constant Ranges: {2} }}...", fmt::ptr(m_parent), m_descriptorSetLayouts.size(), m_pushConstantsLayout == nullptr ? 0 : m_pushConstantsLayout->ranges().size());
LITEFX_TRACE(DIRECTX12_LOG, "Creating render pipeline layout {0} {{ Descriptor Sets: {1}, Push Constant Ranges: {2} }}...", reinterpret_cast<void*>(m_parent), m_descriptorSetLayouts.size(), m_pushConstantsLayout == nullptr ? 0 : m_pushConstantsLayout->ranges().size());

if (m_pushConstantsLayout != nullptr)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Backends/DirectX12/src/ray_tracing_pipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class DirectX12RayTracingPipeline::DirectX12RayTracingPipelineImpl : public Impl
if (m_program != m_shaderRecordCollection.program()) [[unlikely]]
throw InvalidArgumentException("shaderRecords", "The ray tracing pipeline shader program must be the same as used to build the shader record collection.");

LITEFX_TRACE(DIRECTX12_LOG, "Creating ray-tracing pipeline (\"{1}\") for layout {0} (records: {2})...", fmt::ptr(reinterpret_cast<void*>(m_layout.get())), m_parent->name(), m_shaderRecordCollection.shaderRecords().size());
LITEFX_TRACE(DIRECTX12_LOG, "Creating ray-tracing pipeline (\"{1}\") for layout {0} (records: {2})...", reinterpret_cast<void*>(m_layout.get()), m_parent->name(), m_shaderRecordCollection.shaderRecords().size());

// Validate shader stage usage.
auto modules = m_program->modules();
Expand All @@ -87,7 +87,7 @@ class DirectX12RayTracingPipeline::DirectX12RayTracingPipelineImpl : public Impl
else if (hasMeshShaders) [[unlikely]]
throw InvalidArgumentException("shaderProgram", "The shader program contains a mesh shader, which is not supported in a ray-tracing pipeline");

LITEFX_TRACE(DIRECTX12_LOG, "Using shader program {0} with {1} modules...", fmt::ptr(m_program.get()), modules.size());
LITEFX_TRACE(DIRECTX12_LOG, "Using shader program {0} with {1} modules...", reinterpret_cast<const void*>(m_program.get()), modules.size());

// Start by describing the shader modules individually.
struct ShaderModuleSubobjectData {
Expand Down
8 changes: 4 additions & 4 deletions src/Backends/DirectX12/src/render_pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class DirectX12RenderPass::DirectX12RenderPassImpl : public Implement<DirectX12R
{
auto commandBuffer = m_queue->createCommandBuffer(false);
#ifndef NDEBUG
std::as_const(*commandBuffer).handle()->SetName(Widen(fmt::format("{0} Begin Commands {1}", m_parent->name(), m_beginCommandBuffers.size())).c_str());
std::as_const(*commandBuffer).handle()->SetName(Widen(std::format("{0} Begin Commands {1}", m_parent->name(), m_beginCommandBuffers.size())).c_str());
#endif
m_beginCommandBuffers[interfacePointer] = commandBuffer;
}
Expand All @@ -103,7 +103,7 @@ class DirectX12RenderPass::DirectX12RenderPassImpl : public Implement<DirectX12R
{
auto commandBuffer = m_queue->createCommandBuffer(false);
#ifndef NDEBUG
std::as_const(*commandBuffer).handle()->SetName(Widen(fmt::format("{0} End Commands {1}", m_parent->name(), m_endCommandBuffers.size())).c_str());
std::as_const(*commandBuffer).handle()->SetName(Widen(std::format("{0} End Commands {1}", m_parent->name(), m_endCommandBuffers.size())).c_str());
#endif
m_endCommandBuffers[interfacePointer] = commandBuffer;
}
Expand All @@ -113,7 +113,7 @@ class DirectX12RenderPass::DirectX12RenderPassImpl : public Implement<DirectX12R
std::views::transform([this](UInt32 i) {
auto commandBuffer = m_queue->createCommandBuffer(false);
#ifndef NDEBUG
std::as_const(*commandBuffer).handle()->SetName(Widen(fmt::format("{0} Secondary Commands {1}", m_parent->name(), i)).c_str());
std::as_const(*commandBuffer).handle()->SetName(Widen(std::format("{0} Secondary Commands {1}", m_parent->name(), i)).c_str());
#endif
return commandBuffer;
}) | std::ranges::to<Array<SharedPtr<DirectX12CommandBuffer>>>();
Expand Down Expand Up @@ -364,7 +364,7 @@ void DirectX12RenderPass::begin(const DirectX12FrameBuffer& frameBuffer) const
beginCommandBuffer->barrier(inputAttachmentBarrier);

if (!this->name().empty())
m_impl->m_queue->beginDebugRegion(fmt::format("{0} Render Pass", this->name()));
m_impl->m_queue->beginDebugRegion(std::format("{0} Render Pass", this->name()));

// Begin a suspending render pass for the transition and a suspend-the-resume render pass on each command buffer of the frame buffer.
std::as_const(*beginCommandBuffer).handle()->BeginRenderPass(std::get<0>(context).size(), std::get<0>(context).data(), std::get<1>(context).has_value() ? &std::get<1>(context).value() : nullptr, D3D12_RENDER_PASS_FLAG_SUSPENDING_PASS);
Expand Down
Loading

0 comments on commit fe627e6

Please sign in to comment.