Skip to content

Commit 66e486c

Browse files
Merge pull request #201 from Devsh-Graphics-Programming/mitsuba_serialized
Use Mitsuba serialized Loader in Ex 12
2 parents 0c13db5 + 26302a3 commit 66e486c

File tree

5 files changed

+89
-63
lines changed

5 files changed

+89
-63
lines changed

12_MeshLoaders/CMakeLists.txt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
set(NBL_INCLUDE_SERACH_DIRECTORIES
22
"${CMAKE_CURRENT_SOURCE_DIR}/include"
33
)
4+
set(NBL_LIBRARIES)
5+
6+
if (NBL_BUILD_MITSUBA_LOADER)
7+
list(APPEND NBL_INCLUDE_SERACH_DIRECTORIES
8+
"${NBL_EXT_MITSUBA_LOADER_INCLUDE_DIRS}"
9+
)
10+
list(APPEND NBL_LIBRARIES
11+
"${NBL_EXT_MITSUBA_LOADER_LIB}"
12+
)
13+
endif()
414

515
# TODO; Arek I removed `NBL_EXECUTABLE_PROJECT_CREATION_PCH_TARGET` from the last parameter here, doesn't this macro have 4 arguments anyway !?
6-
nbl_create_executable_project("" "" "${NBL_INCLUDE_SERACH_DIRECTORIES}" "" "")
16+
nbl_create_executable_project("" "" "${NBL_INCLUDE_SERACH_DIRECTORIES}" "${NBL_LIBRARIES}")
717
# TODO: Arek temporarily disabled cause I haven't figured out how to make this target yet
818
# LINK_BUILTIN_RESOURCES_TO_TARGET(${EXECUTABLE_NAME} nblExamplesGeometrySpirvBRD)

12_MeshLoaders/main.cpp

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
#include "../3rdparty/portable-file-dialogs/portable-file-dialogs.h"
77

8-
#ifdef _NBL_COMPILE_WITH_MITSUBA_SERIALIZED_LOADER_
8+
#ifdef NBL_BUILD_MITSUBA_LOADER
99
#include "nbl/ext/MitsubaLoader/CSerializedLoader.h"
1010
#endif
1111

@@ -17,13 +17,13 @@ class MeshLoadersApp final : public MonoWindowApplication, public BuiltinResourc
1717
public:
1818
inline MeshLoadersApp(const path& _localInputCWD, const path& _localOutputCWD, const path& _sharedInputCWD, const path& _sharedOutputCWD)
1919
: IApplicationFramework(_localInputCWD, _localOutputCWD, _sharedInputCWD, _sharedOutputCWD),
20-
device_base_t({1280,720}, EF_UNKNOWN, _localInputCWD, _localOutputCWD, _sharedInputCWD, _sharedOutputCWD) {}
20+
device_base_t({1280,720}, EF_D32_SFLOAT, _localInputCWD, _localOutputCWD, _sharedInputCWD, _sharedOutputCWD) {}
2121

2222
inline bool onAppInitialized(smart_refctd_ptr<ISystem>&& system) override
2323
{
2424
if (!asset_base_t::onAppInitialized(smart_refctd_ptr(system)))
2525
return false;
26-
#ifdef _NBL_COMPILE_WITH_MITSUBA_SERIALIZED_LOADER_
26+
#ifdef NBL_BUILD_MITSUBA_LOADER
2727
m_assetMgr->addAssetLoader(make_smart_refctd_ptr<ext::MitsubaLoader::CSerializedLoader>());
2828
#endif
2929
if (!device_base_t::onAppInitialized(smart_refctd_ptr(system)))
@@ -218,9 +218,9 @@ class MeshLoadersApp final : public MonoWindowApplication, public BuiltinResourc
218218

219219
private:
220220
// TODO: standardise this across examples, and take from `argv`
221-
bool m_nonInteractiveTest = true;
221+
bool m_nonInteractiveTest = false;
222222

223-
inline bool reloadModel()
223+
bool reloadModel()
224224
{
225225
if (m_nonInteractiveTest) // TODO: maybe also take from argv and argc
226226
m_modelPath = (sharedInputCWD/"ply/Spanner-ply.ply").string();
@@ -268,8 +268,13 @@ class MeshLoadersApp final : public MonoWindowApplication, public BuiltinResourc
268268
}
269269
if (geometries.empty())
270270
return false;
271-
272-
auto bound = hlsl::shapes::AABB<3,double>::create();
271+
272+
using aabb_t = hlsl::shapes::AABB<3,double>;
273+
auto printAABB = [&](const aabb_t& aabb, const char* extraMsg="")->void
274+
{
275+
m_logger->log("%s AABB is (%f,%f,%f) -> (%f,%f,%f)",ILogger::ELL_INFO,extraMsg,aabb.minVx.x,aabb.minVx.y,aabb.minVx.z,aabb.maxVx.x,aabb.maxVx.y,aabb.maxVx.z);
276+
};
277+
auto bound = aabb_t::create();
273278
// convert the geometries
274279
{
275280
smart_refctd_ptr<CAssetConverter> converter = CAssetConverter::create({.device=m_device.get()});
@@ -344,36 +349,41 @@ class MeshLoadersApp final : public MonoWindowApplication, public BuiltinResourc
344349
return false;
345350
}
346351
}
347-
352+
353+
auto tmp = hlsl::float32_t4x3(
354+
hlsl::float32_t3(1,0,0),
355+
hlsl::float32_t3(0,1,0),
356+
hlsl::float32_t3(0,0,1),
357+
hlsl::float32_t3(0,0,0)
358+
);
359+
core::vector<hlsl::float32_t3x4> worldTforms;
348360
const auto& converted = reservation.getGPUObjects<ICPUPolygonGeometry>();
349361
for (const auto& geom : converted)
350362
{
351-
geom.value->visitAABB([&bound](const auto& aabb)->void
352-
{
353-
hlsl::shapes::AABB<3,double> promoted;
354-
promoted.minVx = aabb.minVx;
355-
promoted.maxVx = aabb.maxVx;
356-
bound = hlsl::shapes::util::union_(promoted,bound);
357-
}
358-
);
363+
const auto promoted = geom.value->getAABB<aabb_t>();
364+
printAABB(promoted,"Geometry");
365+
tmp[3].x += promoted.getExtent().x;
366+
const auto promotedWorld = hlsl::float64_t3x4(worldTforms.emplace_back(hlsl::transpose(tmp)));
367+
const auto transformed = hlsl::shapes::util::transform(promotedWorld,promoted);
368+
printAABB(transformed,"Transformed");
369+
bound = hlsl::shapes::util::union_(transformed,bound);
359370
}
371+
printAABB(bound,"Total");
360372
if (!m_renderer->addGeometries({ &converted.front().get(),converted.size() }))
361373
return false;
374+
375+
auto worlTformsIt = worldTforms.begin();
362376
for (const auto& geo : m_renderer->getGeometries())
363377
m_renderer->m_instances.push_back({
364-
.world = hlsl::float32_t3x4(
365-
hlsl::float32_t4(1,0,0,0),
366-
hlsl::float32_t4(0,1,0,0),
367-
hlsl::float32_t4(0,0,1,0)
368-
),
378+
.world = *(worlTformsIt++),
369379
.packedGeo = &geo
370380
});
371381
}
372382

373383
// get scene bounds and reset camera
374384
{
375385
const double distance = 0.05;
376-
const auto diagonal = bound.maxVx-bound.minVx;
386+
const auto diagonal = bound.getExtent();
377387
{
378388
const auto measure = hlsl::length(diagonal);
379389
const auto aspectRatio = float(m_window->getWidth())/float(m_window->getHeight());

common/include/nbl/examples/geometry/CSimpleDebugRenderer.hpp

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ class CSimpleDebugRenderer final : public core::IReferenceCounted
5757
asset::SBufferBinding<const video::IGPUBuffer> indexBuffer = {};
5858
uint32_t elementCount = 0;
5959
// indices into the descriptor set
60-
uint8_t positionView = 0;
61-
uint8_t normalView = 0;
62-
uint8_t uvView = 0;
60+
constexpr static inline auto MissingView = hlsl::examples::geometry_creator_scene::SPushConstants::DescriptorCount;
61+
uint16_t positionView = MissingView;
62+
uint16_t normalView = MissingView;
6363
asset::E_INDEX_TYPE indexType = asset::EIT_UNKNOWN;
6464
};
6565
//
@@ -72,8 +72,7 @@ class CSimpleDebugRenderer final : public core::IReferenceCounted
7272
return {
7373
.matrices = viewParams.computeForInstance(world),
7474
.positionView = packedGeo->positionView,
75-
.normalView = packedGeo->normalView,
76-
.uvView = packedGeo->uvView
75+
.normalView = packedGeo->normalView
7776
};
7877
}
7978

@@ -137,7 +136,7 @@ class CSimpleDebugRenderer final : public core::IReferenceCounted
137136
// need this trifecta of flags for `SubAllocatedDescriptorSet` to accept the binding as suballocatable
138137
.createFlags = binding_flags_t::ECF_UPDATE_AFTER_BIND_BIT|binding_flags_t::ECF_UPDATE_UNUSED_WHILE_PENDING_BIT |binding_flags_t::ECF_PARTIALLY_BOUND_BIT,
139138
.stageFlags = IShader::E_SHADER_STAGE::ESS_VERTEX|IShader::E_SHADER_STAGE::ESS_FRAGMENT,
140-
.count = SInstance::SPushConstants::DescriptorCount
139+
.count = SPackedGeometry::MissingView
141140
}
142141
};
143142
dsLayout = device->createDescriptorSetLayout(bindings);
@@ -161,7 +160,7 @@ class CSimpleDebugRenderer final : public core::IReferenceCounted
161160

162161
// create pipeline layout
163162
const SPushConstantRange ranges[] = {{
164-
.stageFlags = hlsl::ShaderStage::ESS_VERTEX,
163+
.stageFlags = hlsl::ShaderStage::ESS_VERTEX|hlsl::ShaderStage::ESS_FRAGMENT,
165164
.offset = 0,
166165
.size = sizeof(SInstance::SPushConstants),
167166
}};
@@ -246,24 +245,30 @@ class CSimpleDebugRenderer final : public core::IReferenceCounted
246245

247246
core::vector<IGPUDescriptorSet::SWriteDescriptorSet> writes;
248247
core::vector<IGPUDescriptorSet::SDescriptorInfo> infos;
249-
auto allocateUTB = [&](const IGeometry<const IGPUBuffer>::SDataView& view)->uint8_t
248+
bool anyFailed = false;
249+
auto allocateUTB = [&](const IGeometry<const IGPUBuffer>::SDataView& view)->decltype(SubAllocatedDescriptorSet::invalid_value)
250250
{
251251
if (!view)
252-
return SInstance::SPushConstants::DescriptorCount;
252+
return SPackedGeometry::MissingView;
253253
auto index = SubAllocatedDescriptorSet::invalid_value;
254254
if (m_params.subAllocDS->multi_allocate(VertexAttrubUTBDescBinding,1,&index)!=0)
255-
return SInstance::SPushConstants::DescriptorCount;
256-
const auto retval = infos.size();
255+
{
256+
anyFailed = true;
257+
return SPackedGeometry::MissingView;
258+
}
259+
const auto infosOffset = infos.size();
257260
infos.emplace_back().desc = device->createBufferView(view.src,view.composed.format);
258261
writes.emplace_back() = {
259262
.dstSet = m_params.subAllocDS->getDescriptorSet(),
260263
.binding = VertexAttrubUTBDescBinding,
261264
.arrayElement = index,
262265
.count = 1,
263-
.info = reinterpret_cast<const IGPUDescriptorSet::SDescriptorInfo*>(retval)
266+
.info = reinterpret_cast<const IGPUDescriptorSet::SDescriptorInfo*>(infosOffset)
264267
};
265-
return retval;
268+
return index;
266269
};
270+
if (anyFailed)
271+
device->getLogger()->log("Failed to allocate a UTB for some geometries, probably ran out of space in Descriptor Set!",system::ILogger::ELL_ERROR);
267272

268273
auto sizeToSet = m_geoms.size();
269274
auto resetGeoms = core::makeRAIIExiter([&]()->void
@@ -308,9 +313,6 @@ class CSimpleDebugRenderer final : public core::IReferenceCounted
308313
out.elementCount = geom->getVertexReferenceCount();
309314
out.positionView = allocateUTB(geom->getPositionView());
310315
out.normalView = allocateUTB(geom->getNormalView());
311-
// the first view is usually the UV
312-
if (const auto& auxViews = geom->getAuxAttributeViews(); !auxViews.empty())
313-
out.uvView = allocateUTB(auxViews.front());
314316
}
315317

316318
// no geometry
@@ -340,6 +342,8 @@ class CSimpleDebugRenderer final : public core::IReferenceCounted
340342
deferredFree.reserve(3);
341343
auto deallocate = [&](SubAllocatedDescriptorSet::value_type index)->void
342344
{
345+
if (index>=SPackedGeometry::MissingView)
346+
return;
343347
if (info.semaphore)
344348
deferredFree.push_back(index);
345349
else
@@ -348,14 +352,11 @@ class CSimpleDebugRenderer final : public core::IReferenceCounted
348352
auto geo = m_geoms.begin() + ix;
349353
deallocate(geo->positionView);
350354
deallocate(geo->normalView);
351-
deallocate(geo->uvView);
352355
m_geoms.erase(geo);
353356

354357
if (deferredFree.empty())
355358
return;
356-
357-
core::vector<IGPUDescriptorSet::SDropDescriptorSet> nullify(deferredFree.size());
358-
const_cast<ILogicalDevice*>(m_params.layout->getOriginDevice())->nullifyDescriptors(nullify);
359+
m_params.subAllocDS->multi_deallocate(VertexAttrubUTBDescBinding,deferredFree.size(),deferredFree.data(),info);
359360
}
360361

361362
//
@@ -386,7 +387,7 @@ class CSimpleDebugRenderer final : public core::IReferenceCounted
386387
const auto* geo = instance.packedGeo;
387388
cmdbuf->bindGraphicsPipeline(geo->pipeline.get());
388389
const auto pc = instance.computePushConstants(viewParams);
389-
cmdbuf->pushConstants(layout,hlsl::ShaderStage::ESS_VERTEX,0,sizeof(pc),&pc);
390+
cmdbuf->pushConstants(layout,hlsl::ShaderStage::ESS_VERTEX|hlsl::ShaderStage::ESS_FRAGMENT,0,sizeof(pc),&pc);
390391
if (geo->indexBuffer)
391392
{
392393
cmdbuf->bindIndexBuffer(geo->indexBuffer,geo->indexType);

common/include/nbl/examples/geometry/SPushConstants.hlsl

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,11 @@ struct SInstanceMatrices
2222

2323
struct SPushConstants
2424
{
25-
// no idea if DXC still has this bug with Push Constant static variables
26-
#ifndef __HLSL_VERSiON
27-
NBL_CONSTEXPR_STATIC_INLINE uint32_t DescriptorCount = 255;
28-
#endif
25+
NBL_CONSTEXPR_STATIC_INLINE uint32_t DescriptorCount = (0x1<<16)-1;
2926

3027
SInstanceMatrices matrices;
31-
uint32_t positionView : 11;
32-
uint32_t normalView : 10;
33-
uint32_t uvView : 11;
28+
uint32_t positionView : 16;
29+
uint32_t normalView : 16;
3430
};
3531

3632
}

common/src/nbl/examples/geometry/shaders/unified.hlsl

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,47 @@ using namespace nbl::hlsl;
44
using namespace nbl::hlsl::examples::geometry_creator_scene;
55

66
// for dat sweet programmable pulling
7-
[[vk::binding(0)]] Buffer<float32_t4> utbs[/*SPushConstants::DescriptorCount*/255];
7+
[[vk::binding(0)]] Buffer<float32_t4> utbs[SPushConstants::DescriptorCount];
88

99
//
1010
[[vk::push_constant]] SPushConstants pc;
1111

1212
//
1313
struct SInterpolants
1414
{
15-
float32_t4 position : SV_Position;
16-
float32_t3 meta : COLOR0;
15+
float32_t4 ndc : SV_Position;
16+
float32_t3 meta : COLOR1;
1717
};
1818
#include "nbl/builtin/hlsl/math/linalg/fast_affine.hlsl"
1919

20+
float32_t3 reconstructGeometricNormal(float32_t3 pos)
21+
{
22+
const float32_t2x3 dPos_dScreen = float32_t2x3(
23+
ddx(pos),
24+
ddy(pos)
25+
);
26+
return cross(dPos_dScreen[0],dPos_dScreen[1]);
27+
}
28+
2029
//
2130
[shader("vertex")]
2231
SInterpolants BasicVS(uint32_t VertexIndex : SV_VertexID)
2332
{
2433
const float32_t3 position = utbs[pc.positionView][VertexIndex].xyz;
2534

2635
SInterpolants output;
27-
output.position = math::linalg::promoted_mul(pc.matrices.worldViewProj,position);
28-
output.meta = mul(pc.matrices.normal,utbs[pc.normalView][VertexIndex].xyz);
36+
output.ndc = math::linalg::promoted_mul(pc.matrices.worldViewProj,position);
37+
if (pc.normalView<SPushConstants::DescriptorCount)
38+
output.meta = mul(pc.matrices.normal,utbs[pc.normalView][VertexIndex].xyz);
39+
else
40+
output.meta = mul(inverse(transpose(pc.matrices.normal)),position);
2941
return output;
3042
}
3143
[shader("pixel")]
3244
float32_t4 BasicFS(SInterpolants input) : SV_Target0
3345
{
34-
return float32_t4(normalize(input.meta)*0.5f+promote<float32_t3>(0.5f),1.f);
46+
const float32_t3 normal = pc.normalView<SPushConstants::DescriptorCount ? input.meta:reconstructGeometricNormal(input.meta);
47+
return float32_t4(normalize(normal)*0.5f+promote<float32_t3>(0.5f),1.f);
3548
}
3649

3750
// TODO: do smooth normals on the cone
@@ -41,17 +54,13 @@ SInterpolants ConeVS(uint32_t VertexIndex : SV_VertexID)
4154
const float32_t3 position = utbs[pc.positionView][VertexIndex].xyz;
4255

4356
SInterpolants output;
44-
output.position = math::linalg::promoted_mul(pc.matrices.worldViewProj,position);
57+
output.ndc = math::linalg::promoted_mul(pc.matrices.worldViewProj,position);
4558
output.meta = mul(inverse(transpose(pc.matrices.normal)),position);
4659
return output;
4760
}
4861
[shader("pixel")]
4962
float32_t4 ConeFS(SInterpolants input) : SV_Target0
5063
{
51-
const float32_t2x3 dViewPos_dScreen = float32_t2x3(
52-
ddx(input.meta),
53-
ddy(input.meta)
54-
);
55-
const float32_t3 normal = cross(dViewPos_dScreen[0],dViewPos_dScreen[1]);
64+
const float32_t3 normal = reconstructGeometricNormal(input.meta);
5665
return float32_t4(normalize(normal)*0.5f+promote<float32_t3>(0.5f),1.f);
5766
}

0 commit comments

Comments
 (0)