Skip to content

Commit c6bd10b

Browse files
committed
validate creation params, added draw modes at create time
1 parent 9a35c9f commit c6bd10b

File tree

3 files changed

+68
-8
lines changed

3 files changed

+68
-8
lines changed

include/nbl/ext/DebugDraw/CDrawAABB.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,22 @@ class DrawAABB final : public core::IReferenceCounted
1818
static constexpr inline uint32_t IndicesCount = 24u;
1919
static constexpr inline uint32_t VerticesCount = 8u;
2020

21+
enum DrawMode : uint16_t
22+
{
23+
ADM_DRAW_SINGLE = 0b01,
24+
ADM_DRAW_BATCH = 0b10,
25+
ADM_DRAW_BOTH = 0b11
26+
};
27+
2128
struct SCachedCreationParameters
2229
{
2330
using streaming_buffer_t = video::StreamingTransientDataBufferST<core::allocator<uint8_t>>;
2431

2532
static constexpr inline auto RequiredAllocateFlags = core::bitflag<video::IDeviceMemoryAllocation::E_MEMORY_ALLOCATE_FLAGS>(video::IDeviceMemoryAllocation::EMAF_DEVICE_ADDRESS_BIT);
2633
static constexpr inline auto RequiredUsageFlags = core::bitflag(asset::IBuffer::EUF_STORAGE_BUFFER_BIT) | asset::IBuffer::EUF_SHADER_DEVICE_ADDRESS_BIT;
2734

35+
DrawMode drawMode = ADM_DRAW_BOTH;
36+
2837
core::smart_refctd_ptr<video::IUtilities> utilities;
2938

3039
//! optional, default MDI buffer allocated if not provided
@@ -68,6 +77,7 @@ class DrawAABB final : public core::IReferenceCounted
6877
~DrawAABB() override;
6978

7079
private:
80+
static bool validateCreationParameters(SCreationParameters& params);
7181
static core::smart_refctd_ptr<video::IGPUGraphicsPipeline> createPipeline(SCreationParameters& params, const video::IGPUPipelineLayout* pipelineLayout, const std::string& vsPath, const std::string& fsPath);
7282
static bool createStreamingBuffer(SCreationParameters& params);
7383
static core::smart_refctd_ptr<video::IGPUBuffer> createIndicesBuffer(SCreationParameters& params);

src/nbl/ext/DebugDraw/CDrawAABB.cpp

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,32 @@ core::smart_refctd_ptr<DrawAABB> DrawAABB::create(SCreationParameters&& params)
1919
{
2020
auto* const logger = params.utilities->getLogger();
2121

22-
auto singlePipeline = createPipeline(params, params.singlePipelineLayout.get(), "single.vertex.hlsl", "aabb_instances.fragment.hlsl");
23-
if (!singlePipeline)
22+
if (!validateCreationParameters(params))
2423
{
25-
logger->log("Failed to create pipeline!", ILogger::ELL_ERROR);
24+
logger->log("Failed creation parameters validation!", ILogger::ELL_ERROR);
2625
return nullptr;
2726
}
28-
auto batchPipeline = createPipeline(params, params.batchPipelineLayout.get(), "aabb_instances.vertex.hlsl", "aabb_instances.fragment.hlsl");
29-
if (!batchPipeline)
27+
28+
smart_refctd_ptr<IGPUGraphicsPipeline> singlePipeline = nullptr;
29+
if (params.drawMode & ADM_DRAW_SINGLE)
3030
{
31-
logger->log("Failed to create pipeline!", ILogger::ELL_ERROR);
32-
return nullptr;
31+
singlePipeline = createPipeline(params, params.singlePipelineLayout.get(), "single.vertex.hlsl", "aabb_instances.fragment.hlsl");
32+
if (!singlePipeline)
33+
{
34+
logger->log("Failed to create pipeline!", ILogger::ELL_ERROR);
35+
return nullptr;
36+
}
37+
}
38+
39+
smart_refctd_ptr<IGPUGraphicsPipeline> batchPipeline = nullptr;
40+
if (params.drawMode & ADM_DRAW_BATCH)
41+
{
42+
batchPipeline = createPipeline(params, params.batchPipelineLayout.get(), "aabb_instances.vertex.hlsl", "aabb_instances.fragment.hlsl");
43+
if (!batchPipeline)
44+
{
45+
logger->log("Failed to create pipeline!", ILogger::ELL_ERROR);
46+
return nullptr;
47+
}
3348
}
3449

3550
if (!createStreamingBuffer(params))
@@ -83,6 +98,29 @@ const smart_refctd_ptr<IFileArchive> DrawAABB::mount(smart_refctd_ptr<ILogger> l
8398
return smart_refctd_ptr(archive);
8499
}
85100

101+
bool DrawAABB::validateCreationParameters(SCreationParameters& creationParams)
102+
{
103+
const auto validation = std::to_array
104+
({
105+
std::make_pair(bool(creationParams.assetManager), "Invalid `creationParams.assetManager` is nullptr!"),
106+
std::make_pair(bool(creationParams.assetManager->getSystem()), "Invalid `creationParams.assetManager->getSystem()` is nullptr!"),
107+
std::make_pair(bool(creationParams.utilities), "Invalid `creationParams.utilities` is nullptr!"),
108+
std::make_pair(bool(creationParams.transfer), "Invalid `creationParams.transfer` is nullptr!"),
109+
std::make_pair(bool(creationParams.renderpass), "Invalid `creationParams.renderpass` is nullptr!"),
110+
(creationParams.assetManager && creationParams.utilities && creationParams.transfer && creationParams.renderpass) ? std::make_pair(bool(creationParams.utilities->getLogicalDevice()->getPhysicalDevice()->getQueueFamilyProperties()[creationParams.transfer->getFamilyIndex()].queueFlags.hasFlags(IQueue::FAMILY_FLAGS::TRANSFER_BIT)), "Invalid `creationParams.transfer` is not capable of transfer operations!") : std::make_pair(false, "Pass valid required DrawAABB::S_CREATION_PARAMETERS!")
111+
});
112+
113+
system::logger_opt_ptr logger = creationParams.utilities->getLogger();
114+
for (const auto& [ok, error] : validation)
115+
if (!ok)
116+
{
117+
logger.log(error, ILogger::ELL_ERROR);
118+
return false;
119+
}
120+
121+
return true;
122+
}
123+
86124
smart_refctd_ptr<IGPUGraphicsPipeline> DrawAABB::createPipeline(SCreationParameters& params, const IGPUPipelineLayout* pipelineLayout, const std::string& vsPath, const std::string& fsPath)
87125
{
88126
auto system = smart_refctd_ptr<ISystem>(params.assetManager->getSystem());
@@ -281,6 +319,12 @@ core::smart_refctd_ptr<video::IGPUPipelineLayout> DrawAABB::createDefaultPipelin
281319

282320
bool DrawAABB::renderSingle(IGPUCommandBuffer* commandBuffer, const hlsl::shapes::AABB<3, float>& aabb, const hlsl::float32_t4& color, const hlsl::float32_t4x4& cameraMat)
283321
{
322+
if (!(m_cachedCreationParams.drawMode & ADM_DRAW_SINGLE))
323+
{
324+
m_cachedCreationParams.utilities->getLogger()->log("DrawAABB has not been enabled for draw single!", ILogger::ELL_ERROR);
325+
return false;
326+
}
327+
284328
commandBuffer->bindGraphicsPipeline(m_singlePipeline.get());
285329
commandBuffer->setLineWidth(1.f);
286330
asset::SBufferBinding<video::IGPUBuffer> indexBinding = { .offset = 0, .buffer = m_indicesBuffer };
@@ -300,6 +344,12 @@ bool DrawAABB::renderSingle(IGPUCommandBuffer* commandBuffer, const hlsl::shapes
300344

301345
bool DrawAABB::render(IGPUCommandBuffer* commandBuffer, ISemaphore::SWaitInfo waitInfo, std::span<const InstanceData> aabbInstances, const hlsl::float32_t4x4& cameraMat)
302346
{
347+
if (!(m_cachedCreationParams.drawMode & ADM_DRAW_BATCH))
348+
{
349+
m_cachedCreationParams.utilities->getLogger()->log("DrawAABB has not been enabled for draw batches!", ILogger::ELL_ERROR);
350+
return false;
351+
}
352+
303353
using offset_t = SCachedCreationParameters::streaming_buffer_t::size_type;
304354
constexpr auto MdiSizes = std::to_array<offset_t>({ sizeof(float32_t3), sizeof(InstanceData) });
305355
// shared nPoT alignment needs to be divisible by all smaller ones to satisfy an allocation from all

0 commit comments

Comments
 (0)