Skip to content

Commit 72e3569

Browse files
committed
change batch render to use indexed draw
1 parent 33692fd commit 72e3569

File tree

2 files changed

+117
-46
lines changed

2 files changed

+117
-46
lines changed

include/nbl/ext/DebugDraw/CDrawAABB.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ namespace nbl::ext::debug_draw
1515
class DrawAABB final : public core::IReferenceCounted
1616
{
1717
public:
18+
static constexpr inline uint32_t IndicesCount = 24u;
19+
1820
struct SCachedCreationParameters
1921
{
2022
using streaming_buffer_t = video::StreamingTransientDataBufferST<core::allocator<uint8_t>>;
@@ -30,6 +32,7 @@ class DrawAABB final : public core::IReferenceCounted
3032

3133
struct SCreationParameters : SCachedCreationParameters
3234
{
35+
video::IQueue* transfer = nullptr;
3336
core::smart_refctd_ptr<asset::IAssetManager> assetManager = nullptr;
3437

3538
core::smart_refctd_ptr<video::IGPUPipelineLayout> pipelineLayout;
@@ -57,22 +60,24 @@ class DrawAABB final : public core::IReferenceCounted
5760

5861
bool render(video::IGPUCommandBuffer* commandBuffer, video::ISemaphore::SWaitInfo waitInfo, const hlsl::float32_t4x4& cameraMat);
5962

60-
static std::array<hlsl::float32_t3, 24> getVerticesFromAABB(const core::aabbox3d<float>& aabb);
63+
//static std::array<hlsl::float32_t3, 24> getVerticesFromAABB(const core::aabbox3d<float>& aabb);
6164

6265
void addAABB(const hlsl::shapes::AABB<3,float>& aabb, const hlsl::float32_t4& color = { 1,0,0,1 });
6366
void addOBB(const hlsl::shapes::AABB<3, float>& aabb, const hlsl::float32_t4x4& transform, const hlsl::float32_t4& color = { 1,0,0,1 });
6467
void clearAABBs();
6568

6669
protected:
67-
DrawAABB(SCreationParameters&& _params, core::smart_refctd_ptr<video::IGPUGraphicsPipeline> pipeline);
70+
DrawAABB(SCreationParameters&& _params, core::smart_refctd_ptr<video::IGPUGraphicsPipeline> pipeline, core::smart_refctd_ptr<video::IGPUBuffer> indicesBuffer);
6871
~DrawAABB() override;
6972

7073
private:
7174
static core::smart_refctd_ptr<video::IGPUGraphicsPipeline> createPipeline(SCreationParameters& params);
7275
static bool createStreamingBuffer(SCreationParameters& params);
76+
static core::smart_refctd_ptr<video::IGPUBuffer> createIndicesBuffer(SCreationParameters& params);
7377

7478
std::vector<debug_draw::InstanceData> m_instances;
75-
std::array<hlsl::float32_t3, 24> m_unitAABBVertices;
79+
std::array<hlsl::float32_t3, 8> m_unitAABBVertices;
80+
core::smart_refctd_ptr<video::IGPUBuffer> m_indicesBuffer;
7681

7782
SCachedCreationParameters m_cachedCreationParams;
7883

src/nbl/ext/DebugDraw/CDrawAABB.cpp

Lines changed: 109 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,31 @@ core::smart_refctd_ptr<DrawAABB> DrawAABB::create(SCreationParameters&& params)
3232
return nullptr;
3333
}
3434

35-
return core::smart_refctd_ptr<DrawAABB>(new DrawAABB(std::move(params), pipeline));
35+
auto indicesBuffer = createIndicesBuffer(params);
36+
if (!indicesBuffer)
37+
{
38+
logger->log("Failed to create indices buffer!", ILogger::ELL_ERROR);
39+
return nullptr;
40+
}
41+
42+
return core::smart_refctd_ptr<DrawAABB>(new DrawAABB(std::move(params), pipeline, indicesBuffer));
3643
}
3744

38-
DrawAABB::DrawAABB(SCreationParameters&& params, smart_refctd_ptr<IGPUGraphicsPipeline> pipeline)
39-
: m_cachedCreationParams(std::move(params)), m_pipeline(pipeline)
45+
DrawAABB::DrawAABB(SCreationParameters&& params, smart_refctd_ptr<IGPUGraphicsPipeline> pipeline, smart_refctd_ptr<IGPUBuffer> indicesBuffer)
46+
: m_cachedCreationParams(std::move(params)), m_pipeline(std::move(pipeline)), m_indicesBuffer(std::move(indicesBuffer))
4047
{
4148
const auto unitAABB = core::aabbox3d<float>({ 0, 0, 0 }, { 1, 1, 1 });
42-
m_unitAABBVertices = getVerticesFromAABB(unitAABB);
49+
float32_t3 pMin = { 0, 0, 0 };
50+
float32_t3 pMax = { 1, 1, 1 };
51+
52+
m_unitAABBVertices[0] = float32_t3(pMin.x, pMin.y, pMin.z);
53+
m_unitAABBVertices[1] = float32_t3(pMax.x, pMin.y, pMin.z);
54+
m_unitAABBVertices[2] = float32_t3(pMin.x, pMin.y, pMax.z);
55+
m_unitAABBVertices[3] = float32_t3(pMax.x, pMin.y, pMax.z);
56+
m_unitAABBVertices[4] = float32_t3(pMin.x, pMax.y, pMin.z);
57+
m_unitAABBVertices[5] = float32_t3(pMax.x, pMax.y, pMin.z);
58+
m_unitAABBVertices[6] = float32_t3(pMin.x, pMax.y, pMax.z);
59+
m_unitAABBVertices[7] = float32_t3(pMax.x, pMax.y, pMax.z);
4360
}
4461

4562
DrawAABB::~DrawAABB()
@@ -205,6 +222,53 @@ bool DrawAABB::createStreamingBuffer(SCreationParameters& params)
205222
return true;
206223
}
207224

225+
smart_refctd_ptr<IGPUBuffer> DrawAABB::createIndicesBuffer(SCreationParameters& params)
226+
{
227+
std::array<uint32_t, IndicesCount> unitAABBIndices;
228+
unitAABBIndices[0] = 0;
229+
unitAABBIndices[1] = 1;
230+
unitAABBIndices[2] = 0;
231+
unitAABBIndices[3] = 2;
232+
233+
unitAABBIndices[4] = 3;
234+
unitAABBIndices[5] = 1;
235+
unitAABBIndices[6] = 3;
236+
unitAABBIndices[7] = 2;
237+
238+
unitAABBIndices[8] = 4;
239+
unitAABBIndices[9] = 5;
240+
unitAABBIndices[10] = 4;
241+
unitAABBIndices[11] = 6;
242+
243+
unitAABBIndices[12] = 7;
244+
unitAABBIndices[13] = 5;
245+
unitAABBIndices[14] = 7;
246+
unitAABBIndices[15] = 6;
247+
248+
unitAABBIndices[16] = 0;
249+
unitAABBIndices[17] = 4;
250+
unitAABBIndices[18] = 1;
251+
unitAABBIndices[19] = 5;
252+
253+
unitAABBIndices[20] = 2;
254+
unitAABBIndices[21] = 6;
255+
unitAABBIndices[22] = 3;
256+
unitAABBIndices[23] = 7;
257+
258+
IGPUBuffer::SCreationParams bufparams;
259+
bufparams.size = sizeof(uint32_t) * unitAABBIndices.size();
260+
bufparams.usage = IGPUBuffer::EUF_INDEX_BUFFER_BIT | IGPUBuffer::EUF_TRANSFER_DST_BIT;
261+
262+
smart_refctd_ptr<IGPUBuffer> indicesBuffer;
263+
params.utilities->createFilledDeviceLocalBufferOnDedMem(
264+
SIntendedSubmitInfo{ .queue = params.transfer },
265+
std::move(bufparams),
266+
unitAABBIndices.data()
267+
).move_into(indicesBuffer);
268+
269+
return indicesBuffer;
270+
}
271+
208272
core::smart_refctd_ptr<video::IGPUPipelineLayout> DrawAABB::createDefaultPipelineLayout(video::ILogicalDevice* device, const asset::SPushConstantRange& pcRange)
209273
{
210274
return device->createPipelineLayout({ &pcRange , 1 }, nullptr, nullptr, nullptr, nullptr);
@@ -264,6 +328,8 @@ bool DrawAABB::render(IGPUCommandBuffer* commandBuffer, ISemaphore::SWaitInfo wa
264328

265329
commandBuffer->bindGraphicsPipeline(m_pipeline.get());
266330
commandBuffer->setLineWidth(1.f);
331+
asset::SBufferBinding<video::IGPUBuffer> indexBinding = { .offset = 0, .buffer = m_indicesBuffer };
332+
commandBuffer->bindIndexBuffer(indexBinding, asset::EIT_32BIT);
267333

268334
auto instances = m_instances;
269335
for (auto& inst : instances)
@@ -301,52 +367,52 @@ bool DrawAABB::render(IGPUCommandBuffer* commandBuffer, ISemaphore::SWaitInfo wa
301367
pc.pInstanceBuffer = m_cachedCreationParams.streamingBuffer->getBuffer()->getDeviceAddress() + instancesByteOffset;
302368

303369
commandBuffer->pushConstants(m_pipeline->getLayout(), ESS_VERTEX, 0, sizeof(SPushConstants), &pc);
304-
commandBuffer->draw(m_unitAABBVertices.size(), instanceCount, 0, 0);
370+
commandBuffer->drawIndexed(IndicesCount, instanceCount, 0, 0, 0);
305371

306372
streaming->multi_deallocate(1, &inputOffset, &totalSize, waitInfo);
307373
}
308374

309375
return true;
310376
}
311377

312-
std::array<float32_t3, 24> DrawAABB::getVerticesFromAABB(const core::aabbox3d<float>& aabb)
313-
{
314-
const auto& pMin = aabb.MinEdge;
315-
const auto& pMax = aabb.MaxEdge;
316-
317-
std::array<float32_t3, 24> vertices;
318-
vertices[0] = float32_t3(pMin.X, pMin.Y, pMin.Z);
319-
vertices[1] = float32_t3(pMax.X, pMin.Y, pMin.Z);
320-
vertices[2] = float32_t3(pMin.X, pMin.Y, pMin.Z);
321-
vertices[3] = float32_t3(pMin.X, pMin.Y, pMax.Z);
322-
323-
vertices[4] = float32_t3(pMax.X, pMin.Y, pMax.Z);
324-
vertices[5] = float32_t3(pMax.X, pMin.Y, pMin.Z);
325-
vertices[6] = float32_t3(pMax.X, pMin.Y, pMax.Z);
326-
vertices[7] = float32_t3(pMin.X, pMin.Y, pMax.Z);
327-
328-
vertices[8] = float32_t3(pMin.X, pMax.Y, pMin.Z);
329-
vertices[9] = float32_t3(pMax.X, pMax.Y, pMin.Z);
330-
vertices[10] = float32_t3(pMin.X, pMax.Y, pMin.Z);
331-
vertices[11] = float32_t3(pMin.X, pMax.Y, pMax.Z);
332-
333-
vertices[12] = float32_t3(pMax.X, pMax.Y, pMax.Z);
334-
vertices[13] = float32_t3(pMax.X, pMax.Y, pMin.Z);
335-
vertices[14] = float32_t3(pMax.X, pMax.Y, pMax.Z);
336-
vertices[15] = float32_t3(pMin.X, pMax.Y, pMax.Z);
337-
338-
vertices[16] = float32_t3(pMin.X, pMin.Y, pMin.Z);
339-
vertices[17] = float32_t3(pMin.X, pMax.Y, pMin.Z);
340-
vertices[18] = float32_t3(pMax.X, pMin.Y, pMin.Z);
341-
vertices[19] = float32_t3(pMax.X, pMax.Y, pMin.Z);
342-
343-
vertices[20] = float32_t3(pMin.X, pMin.Y, pMax.Z);
344-
vertices[21] = float32_t3(pMin.X, pMax.Y, pMax.Z);
345-
vertices[22] = float32_t3(pMax.X, pMin.Y, pMax.Z);
346-
vertices[23] = float32_t3(pMax.X, pMax.Y, pMax.Z);
347-
348-
return vertices;
349-
}
378+
//std::array<float32_t3, 24> DrawAABB::getVerticesFromAABB(const core::aabbox3d<float>& aabb)
379+
//{
380+
// const auto& pMin = aabb.MinEdge;
381+
// const auto& pMax = aabb.MaxEdge;
382+
//
383+
// std::array<float32_t3, 24> vertices;
384+
// vertices[0] = float32_t3(pMin.X, pMin.Y, pMin.Z); // 0
385+
// vertices[1] = float32_t3(pMax.X, pMin.Y, pMin.Z); // 1
386+
// vertices[2] = float32_t3(pMin.X, pMin.Y, pMin.Z); // 0
387+
// vertices[3] = float32_t3(pMin.X, pMin.Y, pMax.Z); // 2
388+
//
389+
// vertices[4] = float32_t3(pMax.X, pMin.Y, pMax.Z); // 3
390+
// vertices[5] = float32_t3(pMax.X, pMin.Y, pMin.Z); // 1
391+
// vertices[6] = float32_t3(pMax.X, pMin.Y, pMax.Z); // 3
392+
// vertices[7] = float32_t3(pMin.X, pMin.Y, pMax.Z); // 2
393+
//
394+
// vertices[8] = float32_t3(pMin.X, pMax.Y, pMin.Z); // 4
395+
// vertices[9] = float32_t3(pMax.X, pMax.Y, pMin.Z); // 5
396+
// vertices[10] = float32_t3(pMin.X, pMax.Y, pMin.Z); // 4
397+
// vertices[11] = float32_t3(pMin.X, pMax.Y, pMax.Z); // 6
398+
//
399+
// vertices[12] = float32_t3(pMax.X, pMax.Y, pMax.Z); // 7
400+
// vertices[13] = float32_t3(pMax.X, pMax.Y, pMin.Z); // 5
401+
// vertices[14] = float32_t3(pMax.X, pMax.Y, pMax.Z); // 7
402+
// vertices[15] = float32_t3(pMin.X, pMax.Y, pMax.Z); // 6
403+
//
404+
// vertices[16] = float32_t3(pMin.X, pMin.Y, pMin.Z); // 0
405+
// vertices[17] = float32_t3(pMin.X, pMax.Y, pMin.Z); // 4
406+
// vertices[18] = float32_t3(pMax.X, pMin.Y, pMin.Z); // 1
407+
// vertices[19] = float32_t3(pMax.X, pMax.Y, pMin.Z); // 5
408+
//
409+
// vertices[20] = float32_t3(pMin.X, pMin.Y, pMax.Z); // 2
410+
// vertices[21] = float32_t3(pMin.X, pMax.Y, pMax.Z); // 6
411+
// vertices[22] = float32_t3(pMax.X, pMin.Y, pMax.Z); // 3
412+
// vertices[23] = float32_t3(pMax.X, pMax.Y, pMax.Z); // 7
413+
//
414+
// return vertices;
415+
//}
350416

351417
void DrawAABB::addAABB(const hlsl::shapes::AABB<3,float>& aabb, const hlsl::float32_t4& color)
352418
{

0 commit comments

Comments
 (0)