Skip to content

Commit 3342a40

Browse files
committed
refactor gizmo example to use our geometry creator
1 parent 89fa6d0 commit 3342a40

File tree

6 files changed

+46
-195
lines changed

6 files changed

+46
-195
lines changed

35_GeometryCreator/CMakeLists.txt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1 @@
1-
2-
include(common RESULT_VARIABLE RES)
3-
if(NOT RES)
4-
message(FATAL_ERROR "common.cmake not found. Should be in {repo_root}/cmake directory")
5-
endif()
6-
71
nbl_create_executable_project("" "" "" "" "${NBL_EXECUTABLE_PROJECT_CREATION_PCH_TARGET}")

64_Gizmo/main.cpp

Lines changed: 30 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// For conditions of distribution and use, see copyright notice in nabla.h
44

55
#include <nabla.h>
6+
#include "nbl/asset/utils/CGeometryCreator.h"
67
#include "nbl/video/utilities/CSimpleResizeSurface.h"
78

89
#include "../common/SimpleWindowedApplication.hpp"
@@ -12,8 +13,7 @@
1213
#include "this_example/spirv/builtin/CArchive.h"
1314
#include "this_example/spirv/builtin/builtinResources.h"
1415

15-
#include "shaders/cube.common.hlsl"
16-
#include "shaders/grid.common.hlsl"
16+
#include "shaders/common.hlsl"
1717

1818
using namespace nbl;
1919
using namespace core;
@@ -268,6 +268,9 @@ class GizmoApp final : public examples::SimpleWindowedApplication
268268
m_winMgr->setWindowSize(m_window.get(), WIN_W, WIN_H);
269269
m_surface->recreateSwapchain();
270270

271+
auto assetManager = make_smart_refctd_ptr<nbl::asset::IAssetManager>(smart_refctd_ptr(system));
272+
auto* geometry = assetManager->getGeometryCreator();
273+
271274
SPushConstantRange pushConstantRanges[] = {
272275
{
273276
.stageFlags = IShader::ESS_VERTEX,
@@ -305,98 +308,23 @@ class GizmoApp final : public examples::SimpleWindowedApplication
305308
if (!pipelineLayout)
306309
return logFail("Could not create Pipeline Layout!");
307310

308-
SPrimitiveAssemblyParams primitiveAssemblyParams{};
309-
{
310-
primitiveAssemblyParams.primitiveType = EPT_TRIANGLE_LIST;
311-
}
312-
313-
SVertexInputParams vertexInputParams{};
314-
{
315-
vertexInputParams.enabledBindingFlags = 0b1u;
316-
vertexInputParams.enabledAttribFlags = 0b11u;
317-
318-
vertexInputParams.bindings[0].inputRate = asset::SVertexInputBindingParams::EVIR_PER_VERTEX;
319-
vertexInputParams.bindings[0].stride = sizeof(cube::VSInput);
311+
const auto cube = geometry->createCubeMesh(vector3df(1.f, 1.f, 1.f));
312+
const auto grid = geometry->createRectangleMesh(vector2df_SIMD(999.f, 999.f));
320313

321-
auto& position = vertexInputParams.attributes[0];
322-
323-
position.format = EF_R32G32B32A32_SFLOAT;
324-
position.relativeOffset = offsetof(cube::VSInput, position);
325-
position.binding = 0u;
326-
327-
auto& color = vertexInputParams.attributes[1];
328-
color.format = EF_R8G8B8A8_UNORM;
329-
color.relativeOffset = offsetof(cube::VSInput, color);
330-
color.binding = 0u;
331-
}
332-
333-
if(!createPipeline<NBL_CORE_UNIQUE_STRING_LITERAL_TYPE("gizmo/spirv/cube.vertex.spv"), NBL_CORE_UNIQUE_STRING_LITERAL_TYPE("gizmo/spirv/cube.fragment.spv")>(EP_CUBE, vertexInputParams, primitiveAssemblyParams, pipelineLayout.get(), renderpass))
314+
if(!createPipeline<NBL_CORE_UNIQUE_STRING_LITERAL_TYPE("gizmo/spirv/cube.vertex.spv"), NBL_CORE_UNIQUE_STRING_LITERAL_TYPE("gizmo/spirv/cube.fragment.spv")>(EP_CUBE, cube, pipelineLayout.get(), renderpass))
334315
return logFail("Could not create pipeline for cube pass!");
335316

336-
{
337-
vertexInputParams.bindings[0].stride = sizeof(grid::VSInput);
338-
339-
auto& position = vertexInputParams.attributes[0];
340-
position.format = EF_R32G32B32_SFLOAT;
341-
position.relativeOffset = offsetof(grid::VSInput, position);
342-
343-
auto& color = vertexInputParams.attributes[1];
344-
color.format = EF_R32G32_SFLOAT;
345-
color.relativeOffset = offsetof(grid::VSInput, uv);
346-
347-
if (!createPipeline<NBL_CORE_UNIQUE_STRING_LITERAL_TYPE("gizmo/spirv/grid.vertex.spv"), NBL_CORE_UNIQUE_STRING_LITERAL_TYPE("gizmo/spirv/grid.fragment.spv") > (EP_GRID, vertexInputParams, primitiveAssemblyParams, pipelineLayout.get(), renderpass))
348-
return logFail("Could not create pipeline for grid pass!");
349-
}
350-
351-
auto getRandomColor = []()
352-
{
353-
static std::random_device rd;
354-
static std::mt19937 gen(rd());
355-
static std::uniform_real_distribution<float> dis(0.0f, 1.0f);
356-
357-
return dis(gen);
358-
};
359-
360-
// cube
361-
const auto cubeVertices = std::to_array(
362-
{
363-
cube::VSInput{{-1.0f, -1.0f, -1.0f, 1.0f}, {getRandomColor(), getRandomColor(), getRandomColor(), 1.0f}}, // vertex 0
364-
cube::VSInput{{ 1.0f, -1.0f, -1.0f, 1.0f}, {getRandomColor(), getRandomColor(), getRandomColor(), 1.0f}}, // vertex 1
365-
cube::VSInput{{ 1.0f, 1.0f, -1.0f, 1.0f}, {getRandomColor(), getRandomColor(), getRandomColor(), 1.0f}}, // vertex 2
366-
cube::VSInput{{-1.0f, 1.0f, -1.0f, 1.0f}, {getRandomColor(), getRandomColor(), getRandomColor(), 1.0f}}, // vertex 3
367-
cube::VSInput{{-1.0f, -1.0f, 1.0f, 1.0f}, {getRandomColor(), getRandomColor(), getRandomColor(), 1.0f}}, // vertex 4
368-
cube::VSInput{{ 1.0f, -1.0f, 1.0f, 1.0f}, {getRandomColor(), getRandomColor(), getRandomColor(), 1.0f}}, // vertex 5
369-
cube::VSInput{{ 1.0f, 1.0f, 1.0f, 1.0f}, {getRandomColor(), getRandomColor(), getRandomColor(), 1.0f}}, // vertex 6
370-
cube::VSInput{{-1.0f, 1.0f, 1.0f, 1.0f}, {getRandomColor(), getRandomColor(), getRandomColor(), 1.0f}} // vertex 7
371-
});
372-
373-
const auto cubeIndices = std::to_array<uint16_t>(
374-
{
375-
// Front face (0, 1, 2, 3)
376-
0, 1, 2, 2, 3, 0,
377-
// Back face (4, 5, 6, 7)
378-
4, 5, 6, 6, 7, 4,
379-
// Left face (0, 3, 7, 4)
380-
0, 3, 7, 7, 4, 0,
381-
// Right face (1, 2, 6, 5)
382-
1, 5, 6, 6, 2, 1,
383-
// Top face (3, 2, 6, 7)
384-
3, 2, 6, 6, 7, 3,
385-
// Bottom face (0, 1, 5, 4)
386-
0, 1, 5, 5, 4, 0
387-
});
388-
389-
const auto gridVertices = generateGridVertices();
390-
const auto gridIndices = generateGridIndices();
317+
if (!createPipeline<NBL_CORE_UNIQUE_STRING_LITERAL_TYPE("gizmo/spirv/grid.vertex.spv"), NBL_CORE_UNIQUE_STRING_LITERAL_TYPE("gizmo/spirv/grid.fragment.spv")>(EP_GRID, grid, pipelineLayout.get(), renderpass))
318+
return logFail("Could not create pipeline for grid pass!");
391319

392320
// buffers
393321
{
394322
const auto mask = m_device->getPhysicalDevice()->getUpStreamingMemoryTypeBits();
395323

396-
if (!createVIBuffers(EP_CUBE, cubeVertices, cubeIndices, mask))
324+
if (!createVIBuffers(EP_CUBE, cube, mask))
397325
return false;
398326

399-
if (!createVIBuffers(EP_GRID, gridVertices, gridIndices, mask))
327+
if (!createVIBuffers(EP_GRID, grid, mask))
400328
return false;
401329

402330
m_ubo = m_device->createBuffer({{.size = sizeof(SBasicViewParameters), .usage = core::bitflag(asset::IBuffer::EUF_UNIFORM_BUFFER_BIT) | asset::IBuffer::EUF_TRANSFER_DST_BIT | asset::IBuffer::EUF_INLINE_UPDATE_VIA_CMDBUF} });
@@ -556,10 +484,10 @@ class GizmoApp final : public examples::SimpleWindowedApplication
556484
cb->pushConstants(rawPipeline->getLayout(), IShader::ESS_VERTEX, 0, sizeof(PushConstants), &m_pc);
557485

558486
const asset::SBufferBinding<const IGPUBuffer> bVertices[] = { {.offset = 0, .buffer = hook.m_vertexBuffer} };
559-
const asset::SBufferBinding<const IGPUBuffer> bIndex = { .offset = 0, .buffer = hook.m_indexBuffer };
487+
const asset::SBufferBinding<const IGPUBuffer> bIndices = { .offset = 0, .buffer = hook.m_indexBuffer };
560488

561489
cb->bindVertexBuffers(0, 1, bVertices);
562-
cb->bindIndexBuffer(bIndex, EIT_16BIT);
490+
cb->bindIndexBuffer(bIndices, EIT_16BIT); // geometry creator uses 16 bit for indicies
563491
cb->drawIndexed(hook.m_indexBuffer->getSize() / sizeof(uint16_t), 1, 0, 0, 0);
564492
};
565493

@@ -663,7 +591,7 @@ class GizmoApp final : public examples::SimpleWindowedApplication
663591
PushConstants m_pc = {.withGizmo = true};
664592

665593
template<nbl::core::StringLiteral vPath, nbl::core::StringLiteral fPath>
666-
bool createPipeline(E_PASS ep, const SVertexInputParams& vip, const SPrimitiveAssemblyParams& pas, const video::IGPUPipelineLayout* pl, const video::IGPURenderpass* rp)
594+
bool createPipeline(E_PASS ep, const CGeometryCreator::return_type& oData, const video::IGPUPipelineLayout* pl, const video::IGPURenderpass* rp)
667595
{
668596
struct
669597
{
@@ -678,12 +606,12 @@ class GizmoApp final : public examples::SimpleWindowedApplication
678606
} spirv;
679607

680608
auto createShader = [&](const system::SBuiltinFile& in, asset::IShader::E_SHADER_STAGE stage) -> core::smart_refctd_ptr<video::IGPUShader>
681-
{
682-
const auto buffer = core::make_smart_refctd_ptr<asset::CCustomAllocatorCPUBuffer<core::null_allocator<uint8_t>, true> >(in.size, (void*)in.contents, core::adopt_memory);
683-
const auto shader = make_smart_refctd_ptr<ICPUShader>(core::smart_refctd_ptr(buffer), stage, IShader::E_CONTENT_TYPE::ECT_SPIRV, "");
609+
{
610+
const auto buffer = core::make_smart_refctd_ptr<asset::CCustomAllocatorCPUBuffer<core::null_allocator<uint8_t>, true> >(in.size, (void*)in.contents, core::adopt_memory);
611+
const auto shader = make_smart_refctd_ptr<ICPUShader>(core::smart_refctd_ptr(buffer), stage, IShader::E_CONTENT_TYPE::ECT_SPIRV, "");
684612

685-
return m_device->createShader(shader.get());
686-
};
613+
return m_device->createShader(shader.get());
614+
};
687615

688616
shaders.vertex = createShader(spirv.vertex, IShader::ESS_VERTEX);
689617
shaders.fragment = createShader(spirv.fragment, IShader::ESS_FRAGMENT);
@@ -719,19 +647,21 @@ class GizmoApp final : public examples::SimpleWindowedApplication
719647
param.layout = pl;
720648
param.shaders = specs;
721649
param.renderpass = rp;
722-
param.cached = { .vertexInput = vip, .primitiveAssembly = pas, .rasterization = rasterizationParams, .blend = blendParams, .subpassIx = 0u };
650+
param.cached = { .vertexInput = oData.inputParams, .primitiveAssembly = oData.assemblyParams, .rasterization = rasterizationParams, .blend = blendParams, .subpassIx = 0u };
723651
};
724652

725653
return m_device->createGraphicsPipelines(nullptr, params, &pass[ep].pipeline);
726654
}
727655
}
728656

729-
bool createVIBuffers(E_PASS ep, const auto & vBuffer, const auto & iBuffer, auto mask)
657+
bool createVIBuffers(E_PASS ep, const CGeometryCreator::return_type& oData, auto mask)
730658
{
731659
auto& hook = pass[ep];
660+
auto vBuffer = core::smart_refctd_ptr(oData.bindings[0].buffer); // no offset
661+
auto iBuffer = core::smart_refctd_ptr(oData.indexBuffer.buffer); // no offset
732662

733-
hook.m_vertexBuffer = m_device->createBuffer({ {.size = sizeof(vBuffer.front()) * vBuffer.size(), .usage = core::bitflag(asset::IBuffer::EUF_VERTEX_BUFFER_BIT) | asset::IBuffer::EUF_TRANSFER_DST_BIT | asset::IBuffer::EUF_INLINE_UPDATE_VIA_CMDBUF} });
734-
hook.m_indexBuffer = m_device->createBuffer({ {.size = sizeof(iBuffer.front()) * iBuffer.size(), .usage = core::bitflag(asset::IBuffer::EUF_INDEX_BUFFER_BIT) | asset::IBuffer::EUF_VERTEX_BUFFER_BIT | asset::IBuffer::EUF_TRANSFER_DST_BIT | asset::IBuffer::EUF_INLINE_UPDATE_VIA_CMDBUF} });
663+
hook.m_vertexBuffer = m_device->createBuffer({ {.size = vBuffer->getSize(), .usage = core::bitflag(asset::IBuffer::EUF_VERTEX_BUFFER_BIT) | asset::IBuffer::EUF_TRANSFER_DST_BIT | asset::IBuffer::EUF_INLINE_UPDATE_VIA_CMDBUF}});
664+
hook.m_indexBuffer = m_device->createBuffer({ {.size = iBuffer->getSize(), .usage = core::bitflag(asset::IBuffer::EUF_INDEX_BUFFER_BIT) | asset::IBuffer::EUF_VERTEX_BUFFER_BIT | asset::IBuffer::EUF_TRANSFER_DST_BIT | asset::IBuffer::EUF_INLINE_UPDATE_VIA_CMDBUF}});
735665

736666
for (auto it : { hook.m_vertexBuffer , hook.m_indexBuffer })
737667
{
@@ -773,70 +703,18 @@ class GizmoApp final : public examples::SimpleWindowedApplication
773703
}
774704
}
775705

776-
auto* vPointer = static_cast<cube::VSInput*>(vBinding.memory->getMappedPointer());
777-
auto* iPointer = static_cast<uint16_t*>(iBinding.memory->getMappedPointer());
706+
auto* vPointer = vBinding.memory->getMappedPointer();
707+
auto* iPointer = iBinding.memory->getMappedPointer();
778708

779-
memcpy(vPointer, vBuffer.data(), hook.m_vertexBuffer->getSize());
780-
memcpy(iPointer, iBuffer.data(), hook.m_indexBuffer->getSize());
709+
memcpy(vPointer, vBuffer->getPointer(), hook.m_vertexBuffer->getSize());
710+
memcpy(iPointer, iBuffer->getPointer(), hook.m_indexBuffer->getSize());
781711

782712
vBinding.memory->unmap();
783713
iBinding.memory->unmap();
784714

785715
return true;
786716
}
787717
}
788-
789-
std::vector<grid::VSInput> generateGridVertices()
790-
{
791-
std::vector<grid::VSInput> vertices;
792-
793-
std::vector<float> corners =
794-
{
795-
-999.0f, -999.0f, -0.015f,
796-
-999.0f, 999.0f, -0.015f,
797-
999.0f, 999.0f, -0.015f,
798-
999.0f, -999.0f, -0.015f,
799-
};
800-
801-
std::vector<float> uvs =
802-
{
803-
1.0f, 0.0f,
804-
0.0f, 0.0f,
805-
0.0f, 1.0f,
806-
1.0f, 1.0f,
807-
};
808-
809-
for (size_t i = 0; i < corners.size(); i += 12)
810-
{
811-
for (size_t j = 0; j < 4; ++j) {
812-
grid::VSInput vertex;
813-
vertex.position[0] = corners[i + j * 3 + 0];
814-
vertex.position[1] = corners[i + j * 3 + 1];
815-
vertex.position[2] = corners[i + j * 3 + 2];
816-
vertex.uv[0] = uvs[j * 2 + 0];
817-
vertex.uv[1] = uvs[j * 2 + 1];
818-
vertices.push_back(vertex);
819-
}
820-
}
821-
822-
return vertices;
823-
}
824-
825-
std::vector<uint16_t> generateGridIndices()
826-
{
827-
std::vector<uint16_t> indices;
828-
829-
for (uint16_t i = 0; i < 4; i += 4) {
830-
indices.push_back(i + 0);
831-
indices.push_back(i + 1);
832-
indices.push_back(i + 2);
833-
indices.push_back(i + 2);
834-
indices.push_back(i + 3);
835-
indices.push_back(i + 0);
836-
}
837-
838-
return indices;
839-
}
840718
};
841719

842720
NBL_MAIN_FUNC(GizmoApp)

64_Gizmo/shaders/cube.common.hlsl

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,17 @@
44
#ifdef __HLSL_VERSION
55
struct VSInput
66
{
7-
[[vk::location(0)]] float4 position : POSITION;
8-
[[vk::location(1)]] float4 color : COLOR0;
7+
[[vk::location(0)]] float3 position : POSITION;
8+
[[vk::location(1)]] float4 color : COLOR;
9+
[[vk::location(2)]] float2 uv : TEXCOORD;
10+
[[vk::location(3)]] float3 normal : NORMAL;
911
};
1012

1113
struct PSInput
1214
{
1315
float4 position : SV_Position;
14-
float4 color : COLOR0;
16+
float4 color : COLOR0;
1517
};
16-
#else
17-
namespace cube
18-
{
19-
#include "nbl/nblpack.h"
20-
struct VSInput
21-
{
22-
float position[4];
23-
float color[4];
24-
} PACK_STRUCT;
25-
#include "nbl/nblunpack.h"
26-
}
27-
28-
static_assert(sizeof(cube::VSInput) == sizeof(float) * 4 * 2);
2918
#endif // __HLSL_VERSION
3019

3120
#include "common.hlsl"

64_Gizmo/shaders/cube.vertex.hlsl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ cbuffer CameraData
1212
PSInput VSMain(VSInput input)
1313
{
1414
PSInput output;
15-
output.color = input.color;
16-
output.position = mul(params.MVP, input.position);
15+
16+
output.position = mul(params.MVP, float4(input.position, 1.0));
17+
output.color = float4(input.normal * 0.5 + 0.5, 1.0);
1718

1819
return output;
1920
}

64_Gizmo/shaders/grid.common.hlsl

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@
33

44
#ifdef __HLSL_VERSION
55
struct VSInput
6-
{
7-
[[vk::location(0)]] float3 position : POSITION;
8-
[[vk::location(1)]] float2 uv : TEXCOORD0;
9-
};
6+
{
7+
[[vk::location(0)]] float3 position : POSITION;
8+
[[vk::location(1)]] float4 color : COLOR;
9+
[[vk::location(2)]] float2 uv : TEXCOORD;
10+
[[vk::location(3)]] float3 normal : NORMAL;
11+
};
1012

1113
struct PSInput
1214
{
1315
float4 position : SV_Position;
14-
float2 uv : TEXCOORD0;
16+
float2 uv : TEXCOORD0;
1517
};
1618

1719
float gridTextureGradBox(float2 p, float2 ddx, float2 ddy)
@@ -27,19 +29,6 @@
2729
// pattern
2830
return (1.0 - i.x) * (1.0 - i.y);
2931
}
30-
#else
31-
namespace grid
32-
{
33-
#include "nbl/nblpack.h"
34-
struct VSInput
35-
{
36-
float position[3];
37-
float uv[2];
38-
} PACK_STRUCT;
39-
#include "nbl/nblunpack.h"
40-
}
41-
42-
static_assert(sizeof(grid::VSInput) == sizeof(float) * (3 + 2));
4332
#endif // __HLSL_VERSION
4433

4534
#include "common.hlsl"

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ if(NBL_BUILD_EXAMPLES)
3535
#add_subdirectory(27_PLYSTLDemo EXCLUDE_FROM_ALL)
3636
#add_subdirectory(29_SpecializationConstants EXCLUDE_FROM_ALL)
3737
#add_subdirectory(33_Draw3DLine EXCLUDE_FROM_ALL)
38-
#add_subdirectory(35_GeometryCreator EXCLUDE_FROM_ALL)
38+
add_subdirectory(35_GeometryCreator EXCLUDE_FROM_ALL)
3939

4040
# Unit Test Examples
4141
add_subdirectory(20_AllocatorTest EXCLUDE_FROM_ALL)

0 commit comments

Comments
 (0)