-
Notifications
You must be signed in to change notification settings - Fork 67
New debug draw extension for AABBs #900
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
keptsecret
wants to merge
31
commits into
master
Choose a base branch
from
new_debug_draw
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
865e606
Merge branch 'mesh_loaders' into new_debug_draw
keptsecret ca86128
latest example
keptsecret 0ae3da2
merge master, fix conflicts
keptsecret cd2ef95
latest example
keptsecret fe55bd7
merge master
keptsecret 98ccfb2
added debug draw aabb extension, moved from ex
keptsecret a755514
removed todos
keptsecret 473592b
support hlsl AABBs, also OBBs with transform
keptsecret f68f9c5
merge master, fix conflicts
keptsecret daf34e0
minor syntax changes
keptsecret 33692fd
use hlsl cpp compat matrices, aabb
keptsecret 72e3569
change batch render to use indexed draw
keptsecret 5285e78
simplified single AABB draw
keptsecret 328aa34
change batch render to take span of InstanceData
keptsecret a14c9dc
latest example
keptsecret 9a35c9f
removed vertex buffer, use const vertex array in shader instead
keptsecret c6bd10b
validate creation params, added draw modes at create time
keptsecret 1cb4c14
merge master, fix conflicts
keptsecret 31e93f0
merge master, fix conflicts
keptsecret e5ceb1b
enable debug draw by default
keptsecret fe0a438
merge master, fix conflicts
keptsecret bfa233f
fix embed builtin resource build
keptsecret 3b67580
resolve https://github.com/Devsh-Graphics-Programming/Nabla/pull/900#…
AnastaZIuk aae42fa
merge master, fix conflicts
keptsecret 0879ce7
fix + optimize aabb vertex calc, includes
keptsecret 1f73ca9
changed debug_draw library target usage
keptsecret a40f540
some fixes to draw aabb
keptsecret f0f9957
removed commented out bit
keptsecret fdd675b
create pipelineLayout util can takes mode, also create layout if miss…
keptsecret 37cc551
aabb local transform is 3x4, common draw param struct between single …
keptsecret ba2860f
write instances data directly to streaming buffer mem
keptsecret File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule examples_tests
updated
16 files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,190 @@ | ||
| // Copyright (C) 2018-2025 - DevSH Graphics Programming Sp. z O.O. | ||
| // This file is part of the "Nabla Engine". | ||
| // For conditions of distribution and use, see copyright notice in nabla.h | ||
|
|
||
| #ifndef _NBL_EXT_DEBUG_DRAW_DRAW_AABB_H_ | ||
| #define _NBL_EXT_DEBUG_DRAW_DRAW_AABB_H_ | ||
|
|
||
| #include "nbl/video/declarations.h" | ||
| #include "nbl/builtin/hlsl/cpp_compat.hlsl" | ||
| #include "nbl/builtin/hlsl/shapes/aabb.hlsl" | ||
| #include "nbl/builtin/hlsl/math/linalg/fast_affine.hlsl" | ||
| #include "nbl/ext/DebugDraw/builtin/hlsl/common.hlsl" | ||
|
|
||
| namespace nbl::ext::debug_draw | ||
| { | ||
| class DrawAABB final : public core::IReferenceCounted | ||
| { | ||
| public: | ||
| static constexpr inline uint32_t IndicesCount = 24u; | ||
|
|
||
| enum DrawMode : uint16_t | ||
| { | ||
| ADM_DRAW_SINGLE = 0b01, | ||
| ADM_DRAW_BATCH = 0b10, | ||
| ADM_DRAW_BOTH = 0b11 | ||
| }; | ||
|
|
||
| struct SCachedCreationParameters | ||
| { | ||
| using streaming_buffer_t = video::StreamingTransientDataBufferST<core::allocator<uint8_t>>; | ||
|
|
||
| static constexpr inline auto RequiredAllocateFlags = core::bitflag<video::IDeviceMemoryAllocation::E_MEMORY_ALLOCATE_FLAGS>(video::IDeviceMemoryAllocation::EMAF_DEVICE_ADDRESS_BIT); | ||
| static constexpr inline auto RequiredUsageFlags = core::bitflag(asset::IBuffer::EUF_STORAGE_BUFFER_BIT) | asset::IBuffer::EUF_SHADER_DEVICE_ADDRESS_BIT; | ||
|
|
||
| DrawMode drawMode = ADM_DRAW_BOTH; | ||
|
|
||
| core::smart_refctd_ptr<video::IUtilities> utilities; | ||
|
|
||
| //! optional, default MDI buffer allocated if not provided | ||
| core::smart_refctd_ptr<streaming_buffer_t> streamingBuffer = nullptr; | ||
| }; | ||
|
|
||
| // only used to make the 24 element index buffer and instanced pipeline on create | ||
| struct SCreationParameters : SCachedCreationParameters | ||
| { | ||
| video::IQueue* transfer = nullptr; | ||
| core::smart_refctd_ptr<asset::IAssetManager> assetManager = nullptr; | ||
|
|
||
| core::smart_refctd_ptr<video::IGPUPipelineLayout> singlePipelineLayout; | ||
| core::smart_refctd_ptr<video::IGPUPipelineLayout> batchPipelineLayout; | ||
| core::smart_refctd_ptr<video::IGPURenderpass> renderpass = nullptr; | ||
|
|
||
| inline bool validate() const | ||
| { | ||
| assert(bool(assetManager)); | ||
| assert(bool(assetManager->getSystem())); | ||
| assert(bool(utilities)); | ||
| assert(bool(transfer)); | ||
| assert(bool(renderpass)); | ||
|
|
||
| system::logger_opt_ptr logger = utilities->getLogger(); | ||
| if (!bool(utilities->getLogicalDevice()->getPhysicalDevice()->getQueueFamilyProperties()[transfer->getFamilyIndex()].queueFlags.hasFlags(video::IQueue::FAMILY_FLAGS::TRANSFER_BIT))) | ||
| { | ||
| logger.log("Invalid `creationParams.transfer` is not capable of transfer operations!", system::ILogger::ELL_ERROR); | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
| }; | ||
|
|
||
| struct DrawParameters | ||
| { | ||
| video::IGPUCommandBuffer* commandBuffer = nullptr; | ||
| hlsl::float32_t4x4 cameraMat = hlsl::float32_t4x4(1); | ||
| float lineWidth = 1.f; | ||
| }; | ||
|
|
||
| // creates an instance that can draw one AABB via push constant or multiple using streaming buffer | ||
| static core::smart_refctd_ptr<DrawAABB> create(SCreationParameters&& params); | ||
|
|
||
| // creates pipeline layout from push constant range | ||
| static core::smart_refctd_ptr<video::IGPUPipelineLayout> createPipelineLayoutFromPCRange(video::ILogicalDevice* device, const asset::SPushConstantRange& pcRange); | ||
|
|
||
| // creates default pipeline layout for pipeline specified by draw mode (note: if mode==BOTH, returns layout for BATCH mode) | ||
| static core::smart_refctd_ptr<video::IGPUPipelineLayout> createDefaultPipelineLayout(video::ILogicalDevice* device, DrawMode mode = ADM_DRAW_BATCH); | ||
|
|
||
| //! mounts the extension's archive to given system - useful if you want to create your own shaders with common header included | ||
| static const core::smart_refctd_ptr<system::IFileArchive> mount(core::smart_refctd_ptr<system::ILogger> logger, system::ISystem* system, const std::string_view archiveAlias = ""); | ||
devshgraphicsprogramming marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| inline const SCachedCreationParameters& getCreationParameters() const { return m_cachedCreationParams; } | ||
|
|
||
| // records draw command for single AABB, user has to set pipeline outside | ||
| bool renderSingle(const DrawParameters& params, const hlsl::shapes::AABB<3, float>& aabb, const hlsl::float32_t4& color); | ||
|
|
||
| // records draw command for rendering batch of AABB instances as InstanceData | ||
| // user has to set span of filled-in InstanceData; camera matrix used in push constant | ||
| inline bool render(const DrawParameters& params, video::ISemaphore::SWaitInfo waitInfo, std::span<const InstanceData> aabbInstances) | ||
| { | ||
| if (!(m_cachedCreationParams.drawMode & ADM_DRAW_BATCH)) | ||
| { | ||
| m_cachedCreationParams.utilities->getLogger()->log("DrawAABB has not been enabled for draw batches!", system::ILogger::ELL_ERROR); | ||
| return false; | ||
| } | ||
|
|
||
| using offset_t = SCachedCreationParameters::streaming_buffer_t::size_type; | ||
| constexpr auto MdiSizes = std::to_array<offset_t>({ sizeof(hlsl::float32_t3), sizeof(InstanceData) }); | ||
| // shared nPoT alignment needs to be divisible by all smaller ones to satisfy an allocation from all | ||
| constexpr offset_t MaxAlignment = std::reduce(MdiSizes.begin(), MdiSizes.end(), 1, [](const offset_t a, const offset_t b)->offset_t {return std::lcm(a, b); }); | ||
| // allocator initialization needs us to round up to PoT | ||
| const auto MaxPOTAlignment = hlsl::roundUpToPoT(MaxAlignment); | ||
|
|
||
| auto* streaming = m_cachedCreationParams.streamingBuffer.get(); | ||
|
|
||
| auto* const streamingPtr = reinterpret_cast<uint8_t*>(streaming->getBufferPointer()); | ||
| assert(streamingPtr); | ||
|
|
||
| auto& commandBuffer = params.commandBuffer; | ||
| commandBuffer->bindGraphicsPipeline(m_batchPipeline.get()); | ||
| commandBuffer->setLineWidth(params.lineWidth); | ||
| asset::SBufferBinding<video::IGPUBuffer> indexBinding = { .offset = 0, .buffer = m_indicesBuffer }; | ||
| commandBuffer->bindIndexBuffer(indexBinding, asset::EIT_32BIT); | ||
|
|
||
| auto setInstancesRange = [&](InstanceData* data, uint32_t count) -> void { | ||
| for (uint32_t i = 0; i < count; i++) | ||
| { | ||
| auto inst = data + i; | ||
| *inst = aabbInstances[i]; | ||
| inst->transform = hlsl::mul(params.cameraMat, inst->transform); | ||
| } | ||
| }; | ||
|
|
||
| const uint32_t numInstances = aabbInstances.size(); | ||
| const uint32_t instancesPerIter = streaming->getBuffer()->getSize() / sizeof(InstanceData); | ||
| using suballocator_t = core::LinearAddressAllocatorST<offset_t>; | ||
| uint32_t beginOffset = 0; | ||
| while (beginOffset < numInstances) | ||
| { | ||
| const uint32_t instanceCount = hlsl::min<uint32_t>(instancesPerIter, numInstances); | ||
| offset_t inputOffset = 0u; | ||
| offset_t ImaginarySizeUpperBound = 0x1 << 30; | ||
| suballocator_t imaginaryChunk(nullptr, inputOffset, 0, hlsl::roundUpToPoT(MaxAlignment), ImaginarySizeUpperBound); | ||
| uint32_t instancesByteOffset = imaginaryChunk.alloc_addr(sizeof(InstanceData) * instanceCount, sizeof(InstanceData)); | ||
| const uint32_t totalSize = imaginaryChunk.get_allocated_size(); | ||
|
|
||
| inputOffset = SCachedCreationParameters::streaming_buffer_t::invalid_value; | ||
| std::chrono::steady_clock::time_point waitTill = std::chrono::steady_clock::now() + std::chrono::milliseconds(1u); | ||
| streaming->multi_allocate(waitTill, 1, &inputOffset, &totalSize, &MaxAlignment); | ||
|
|
||
| auto* const streamingInstancesPtr = reinterpret_cast<InstanceData*>(streamingPtr + instancesByteOffset); | ||
| setInstancesRange(streamingInstancesPtr, instanceCount); | ||
| beginOffset += instanceCount; | ||
|
|
||
| assert(!streaming->needsManualFlushOrInvalidate()); | ||
|
|
||
| SPushConstants pc; | ||
| pc.pInstanceBuffer = m_cachedCreationParams.streamingBuffer->getBuffer()->getDeviceAddress() + instancesByteOffset; | ||
|
|
||
| commandBuffer->pushConstants(m_batchPipeline->getLayout(), asset::IShader::E_SHADER_STAGE::ESS_VERTEX, 0, sizeof(SPushConstants), &pc); | ||
| commandBuffer->drawIndexed(IndicesCount, instanceCount, 0, 0, 0); | ||
|
|
||
| streaming->multi_deallocate(1, &inputOffset, &totalSize, waitInfo); | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| static hlsl::float32_t3x4 getTransformFromAABB(const hlsl::shapes::AABB<3, float>& aabb); | ||
|
|
||
| protected: | ||
| DrawAABB(SCreationParameters&& _params, core::smart_refctd_ptr<video::IGPUGraphicsPipeline> singlePipeline, core::smart_refctd_ptr<video::IGPUGraphicsPipeline> batchPipeline, | ||
| core::smart_refctd_ptr<video::IGPUBuffer> indicesBuffer); | ||
| ~DrawAABB() override; | ||
|
|
||
| private: | ||
| //static bool validateCreationParameters(SCreationParameters& params); | ||
| static core::smart_refctd_ptr<video::IGPUGraphicsPipeline> createPipeline(SCreationParameters& params, const video::IGPUPipelineLayout* pipelineLayout, const std::string& vsPath, const std::string& fsPath); | ||
| static bool createStreamingBuffer(SCreationParameters& params); | ||
| static core::smart_refctd_ptr<video::IGPUBuffer> createIndicesBuffer(SCreationParameters& params); | ||
|
|
||
| core::smart_refctd_ptr<video::IGPUBuffer> m_indicesBuffer; | ||
|
|
||
| SCachedCreationParameters m_cachedCreationParams; | ||
|
|
||
| core::smart_refctd_ptr<video::IGPUGraphicsPipeline> m_singlePipeline; | ||
| core::smart_refctd_ptr<video::IGPUGraphicsPipeline> m_batchPipeline; | ||
| }; | ||
| } | ||
|
|
||
| #endif | ||
13 changes: 13 additions & 0 deletions
13
include/nbl/ext/DebugDraw/builtin/hlsl/aabb_instances.fragment.hlsl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| #pragma shader_stage(fragment) | ||
|
|
||
| #include "nbl/ext/DebugDraw/builtin/hlsl/common.hlsl" | ||
|
|
||
| using namespace nbl::ext::debug_draw; | ||
|
|
||
| [shader("pixel")] | ||
| float32_t4 main(PSInput input) : SV_TARGET | ||
| { | ||
| float32_t4 outColor = input.color; | ||
|
|
||
| return outColor; | ||
| } |
21 changes: 21 additions & 0 deletions
21
include/nbl/ext/DebugDraw/builtin/hlsl/aabb_instances.vertex.hlsl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| #pragma shader_stage(vertex) | ||
|
|
||
| #include "nbl/ext/DebugDraw/builtin/hlsl/common.hlsl" | ||
|
|
||
| using namespace nbl::hlsl; | ||
| using namespace nbl::ext::debug_draw; | ||
|
|
||
| [[vk::push_constant]] SPushConstants pc; | ||
|
|
||
| [shader("vertex")] | ||
| PSInput main() | ||
| { | ||
| PSInput output; | ||
| const float32_t3 vertex = getUnitAABBVertex(); | ||
| InstanceData instance = vk::BufferPointer<InstanceData>(pc.pInstanceBuffer + sizeof(InstanceData) * glsl::gl_InstanceIndex()).Get(); | ||
|
|
||
| output.position = math::linalg::promoted_mul(instance.transform, vertex); | ||
| output.color = instance.color; | ||
|
|
||
| return output; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| #ifndef _NBL_DEBUG_DRAW_EXT_COMMON_HLSL | ||
| #define _NBL_DEBUG_DRAW_EXT_COMMON_HLSL | ||
|
|
||
| #include "nbl/builtin/hlsl/cpp_compat.hlsl" | ||
| #ifdef __HLSL_VERSION | ||
| #include "nbl/builtin/hlsl/math/linalg/fast_affine.hlsl" | ||
| #include "nbl/builtin/hlsl/glsl_compat/core.hlsl" | ||
| #include "nbl/builtin/hlsl/bda/__ptr.hlsl" | ||
| #endif | ||
|
|
||
| namespace nbl | ||
| { | ||
| namespace ext | ||
| { | ||
| namespace debug_draw | ||
| { | ||
|
|
||
| struct InstanceData | ||
| { | ||
| hlsl::float32_t4x4 transform; | ||
| hlsl::float32_t4 color; | ||
| }; | ||
|
|
||
| struct SSinglePushConstants | ||
| { | ||
| InstanceData instance; | ||
| }; | ||
|
|
||
| struct SPushConstants | ||
| { | ||
| uint64_t pInstanceBuffer; | ||
| }; | ||
|
|
||
| #ifdef __HLSL_VERSION | ||
| struct PSInput | ||
| { | ||
| float32_t4 position : SV_Position; | ||
| nointerpolation float32_t4 color : TEXCOORD0; | ||
| }; | ||
|
|
||
| float32_t3 getUnitAABBVertex() | ||
| { | ||
| return (hlsl::promote<uint32_t3>(hlsl::glsl::gl_VertexIndex()) >> uint32_t3(0,2,1)) & 0x1u; | ||
| } | ||
| #endif | ||
|
|
||
| } | ||
| } | ||
| } | ||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| #pragma shader_stage(vertex) | ||
|
|
||
| #include "nbl/ext/DebugDraw/builtin/hlsl/common.hlsl" | ||
|
|
||
| using namespace nbl::hlsl; | ||
| using namespace nbl::ext::debug_draw; | ||
|
|
||
| [[vk::push_constant]] SSinglePushConstants pc; | ||
|
|
||
| [shader("vertex")] | ||
| PSInput main() | ||
| { | ||
| PSInput output; | ||
| float32_t3 vertex = getUnitAABBVertex(); | ||
|
|
||
| output.position = math::linalg::promoted_mul(pc.instance.transform, vertex); | ||
| output.color = pc.instance.color; | ||
|
|
||
| return output; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -70,6 +70,7 @@ class NBL_API2 ISystem : public core::IReferenceCounted | |
| // | ||
| virtual inline bool isDirectory(const system::path& p) const | ||
| { | ||
| // TODO: fix bug, input "nbl/ext/DebugDraw/builtin/hlsl" -> returs true when no such dir present in mounted stuff due to how it uses parent paths in loop (goes up up till matches "nbl" builtin archive and thinks it resolved the requested dir) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @AnastaZIuk open an issue about it |
||
| if (isPathReadOnly(p)) | ||
| return p.extension()==""; // TODO: this is a temporary decision until we figure out how to check if a file is directory in android APK | ||
| else | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
comment that its only needed to make the 24 element index buffer and not used for anything later