Skip to content

Commit eaa1320

Browse files
author
devsh
committed
prep the conversion
1 parent e790c84 commit eaa1320

File tree

1 file changed

+102
-21
lines changed

1 file changed

+102
-21
lines changed

12_MeshLoaders/main.cpp

Lines changed: 102 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -40,29 +40,14 @@ class MeshLoadersApp final : public MonoWindowApplication, public BuiltinResourc
4040
m_qnc = make_smart_refctd_ptr<CQuantNormalCache>();
4141
m_qnc->loadCacheFromFile<EF_R8G8B8_SNORM>(m_system.get(),sharedOutputCWD/"../../tmp/normalCache888.sse");
4242

43+
auto scRes = static_cast<CDefaultSwapchainFramebuffers*>(m_surface->getSwapchainResources());
44+
m_renderer = CSimpleDebugRenderer::create(m_assetMgr.get(),scRes->getRenderpass(),0,{});
45+
if (!m_renderer)
46+
return logFail("Failed to create renderer!");
47+
4348
//
4449
if (!reloadModel())
4550
return false;
46-
#if 0
47-
const uint32_t addtionalBufferOwnershipFamilies[] = {getGraphicsQueue()->getFamilyIndex()};
48-
// we want to use the vertex data through UTBs
49-
using usage_f = IGPUBuffer::E_USAGE_FLAGS;
50-
CAssetConverter::patch_t<asset::ICPUPolygonGeometry> patch = {};
51-
patch.positionBufferUsages = usage_f::EUF_UNIFORM_TEXEL_BUFFER_BIT;
52-
patch.indexBufferUsages = usage_f::EUF_INDEX_BUFFER_BIT;
53-
patch.otherBufferUsages = usage_f::EUF_UNIFORM_TEXEL_BUFFER_BIT;
54-
m_scene = CGeometryCreatorScene::create(
55-
{
56-
.transferQueue = getTransferUpQueue(),
57-
.utilities = m_utils.get(),
58-
.logger = m_logger.get(),
59-
.addtionalBufferOwnershipFamilies = addtionalBufferOwnershipFamilies
60-
},patch
61-
);
62-
#endif
63-
64-
auto scRes = static_cast<CDefaultSwapchainFramebuffers*>(m_surface->getSwapchainResources());
65-
m_renderer = CSimpleDebugRenderer::create(m_assetMgr.get(),scRes->getRenderpass(),0,nullptr);
6651

6752
camera.mapKeysToArrows();
6853

@@ -250,6 +235,8 @@ class MeshLoadersApp final : public MonoWindowApplication, public BuiltinResourc
250235
}
251236

252237
// free up
238+
m_renderer->m_instances.clear();
239+
m_renderer->clearGeometries({.semaphore=m_semaphore.get(),.value=m_realFrameIx});
253240
m_assetMgr->clearAllAssetCache();
254241

255242
//! load the geometry
@@ -258,10 +245,104 @@ class MeshLoadersApp final : public MonoWindowApplication, public BuiltinResourc
258245
auto bundle = m_assetMgr->getAsset(m_modelPath,params);
259246
if (bundle.getContents().empty())
260247
return false;
248+
249+
//
250+
core::vector<smart_refctd_ptr<const ICPUPolygonGeometry>> geometries;
251+
switch (bundle.getAssetType())
252+
{
253+
case IAsset::E_TYPE::ET_GEOMETRY:
254+
for (const auto& item : bundle.getContents())
255+
if (auto polyGeo=IAsset::castDown<ICPUPolygonGeometry>(item); polyGeo)
256+
geometries.push_back(polyGeo);
257+
break;
258+
default:
259+
m_logger->log("Asset loaded but not a supported type (ET_GEOMETRY,ET_GEOMETRY_COLLECTION)",ILogger::ELL_ERROR);
260+
break;
261+
}
262+
if (geometries.empty())
263+
return false;
264+
261265
//! cache results -- speeds up mesh generation on second run
262266
m_qnc->saveCacheToFile<EF_R8G8B8_SNORM>(m_system.get(),sharedOutputCWD/"../../tmp/normalCache888.sse");
267+
268+
// convert the geometries
269+
{
270+
smart_refctd_ptr<CAssetConverter> converter = CAssetConverter::create({.device=m_device.get()});
263271

264-
return true;
272+
const auto transferFamily = getTransferUpQueue()->getFamilyIndex();
273+
274+
struct SInputs : CAssetConverter::SInputs
275+
{
276+
virtual inline std::span<const uint32_t> getSharedOwnershipQueueFamilies(const size_t groupCopyID, const asset::ICPUBuffer* buffer, const CAssetConverter::patch_t<asset::ICPUBuffer>& patch) const
277+
{
278+
return sharedBufferOwnership;
279+
}
280+
281+
core::vector<uint32_t> sharedBufferOwnership;
282+
} inputs = {};
283+
core::vector<CAssetConverter::patch_t<ICPUPolygonGeometry>> patches(geometries.size(),CSimpleDebugRenderer::DefaultPolygonGeometryPatch);
284+
{
285+
inputs.logger = m_logger.get();
286+
std::get<CAssetConverter::SInputs::asset_span_t<ICPUPolygonGeometry>>(inputs.assets) = {&geometries.front().get(),geometries.size()};
287+
std::get<CAssetConverter::SInputs::patch_span_t<ICPUPolygonGeometry>>(inputs.patches) = patches;
288+
// set up shared ownership so we don't have to
289+
core::unordered_set<uint32_t> families;
290+
families.insert(transferFamily);
291+
families.insert(getGraphicsQueue()->getFamilyIndex());
292+
if (families.size()>1)
293+
for (const auto fam : families)
294+
inputs.sharedBufferOwnership.push_back(fam);
295+
}
296+
297+
// reserve
298+
auto reservation = converter->reserve(inputs);
299+
if (!reservation)
300+
{
301+
m_logger->log("Failed to reserve GPU objects for CPU->GPU conversion!",ILogger::ELL_ERROR);
302+
return false;
303+
}
304+
305+
// convert
306+
{
307+
auto semaphore = m_device->createSemaphore(0u);
308+
309+
constexpr auto MultiBuffering = 2;
310+
std::array<smart_refctd_ptr<IGPUCommandBuffer>,MultiBuffering> commandBuffers = {};
311+
{
312+
auto pool = m_device->createCommandPool(transferFamily,IGPUCommandPool::CREATE_FLAGS::RESET_COMMAND_BUFFER_BIT|IGPUCommandPool::CREATE_FLAGS::TRANSIENT_BIT);
313+
pool->createCommandBuffers(IGPUCommandPool::BUFFER_LEVEL::PRIMARY,commandBuffers,smart_refctd_ptr(m_logger));
314+
}
315+
commandBuffers.front()->begin(IGPUCommandBuffer::USAGE::ONE_TIME_SUBMIT_BIT);
316+
317+
std::array<IQueue::SSubmitInfo::SCommandBufferInfo,MultiBuffering> commandBufferSubmits;
318+
for (auto i=0; i<MultiBuffering; i++)
319+
commandBufferSubmits[i].cmdbuf = commandBuffers[i].get();
320+
321+
SIntendedSubmitInfo transfer = {};
322+
transfer.queue = getTransferUpQueue();
323+
transfer.scratchCommandBuffers = commandBufferSubmits;
324+
transfer.scratchSemaphore = {
325+
.semaphore = semaphore.get(),
326+
.value = 0u,
327+
.stageMask = PIPELINE_STAGE_FLAGS::ALL_TRANSFER_BITS
328+
};
329+
330+
CAssetConverter::SConvertParams cpar = {};
331+
cpar.utilities = m_utils.get();
332+
cpar.transfer = &transfer;
333+
334+
// basically it records all data uploads and submits them right away
335+
auto future = reservation.convert(cpar);
336+
if (future.copy()!=IQueue::RESULT::SUCCESS)
337+
{
338+
m_logger->log("Failed to await submission feature!", ILogger::ELL_ERROR);
339+
return false;
340+
}
341+
}
342+
343+
const auto& converted = reservation.getGPUObjects<ICPUPolygonGeometry>();
344+
return m_renderer->addGeometries({&converted.front().get(),converted.size()});
345+
}
265346
}
266347

267348
// Maximum frames which can be simultaneously submitted, used to cycle through our per-frame resources like command buffers

0 commit comments

Comments
 (0)