@@ -347,70 +347,6 @@ class VulkanDescriptorSetManager::DescriptorSetLayoutManager {
347
347
mVkLayouts ;
348
348
};
349
349
350
- class VulkanDescriptorSetManager ::DescriptorSetHistory {
351
- private:
352
- using TextureBundle = std::pair<VulkanTexture*, VkImageSubresourceRange>;
353
-
354
- public:
355
- DescriptorSetHistory ()
356
- : dynamicUboCount(0 ),
357
- mResources (nullptr ) {}
358
-
359
- DescriptorSetHistory (UniformBufferBitmask const & dynamicUbo, uint8_t uniqueDynamicUboCount,
360
- VulkanResourceAllocator* allocator, VulkanDescriptorSet* set)
361
- : dynamicUboMask(dynamicUbo),
362
- dynamicUboCount(uniqueDynamicUboCount),
363
- mResources(allocator),
364
- mSet(set),
365
- mBound(false ) {
366
- assert_invariant (set);
367
- // initial state is unbound.
368
- unbind ();
369
- }
370
-
371
- ~DescriptorSetHistory () {
372
- if (mSet ) {
373
- mResources .clear ();
374
- }
375
- }
376
-
377
- void setOffsets (backend::DescriptorSetOffsetArray&& offsets) noexcept {
378
- mOffsets = std::move (offsets);
379
- mBound = false ;
380
- }
381
-
382
- void write (uint8_t binding) noexcept { mBound = false ; }
383
-
384
- // Ownership will be transfered to the commandbuffer.
385
- void bind (VulkanCommandBuffer* commands, VkPipelineLayout pipelineLayout,
386
- uint8_t index) noexcept {
387
- VkCommandBuffer const cmdbuffer = commands->buffer ();
388
- vkCmdBindDescriptorSets (cmdbuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, index ,
389
- 1 , &mSet ->vkSet , dynamicUboCount, mOffsets .data ());
390
-
391
- commands->acquire (mSet );
392
- mResources .clear ();
393
- mBound = true ;
394
- }
395
-
396
- void unbind () noexcept {
397
- mResources .acquire (mSet );
398
- mBound = false ;
399
- }
400
-
401
- bool bound () const noexcept { return mBound ; }
402
-
403
- UniformBufferBitmask const dynamicUboMask;
404
- uint8_t const dynamicUboCount;
405
-
406
- private:
407
- FixedSizeVulkanResourceManager<1 > mResources ;
408
- VulkanDescriptorSet* mSet = nullptr ;
409
-
410
- backend::DescriptorSetOffsetArray mOffsets ;
411
- bool mBound = false ;
412
- };
413
-
414
350
VulkanDescriptorSetManager::VulkanDescriptorSetManager (VkDevice device,
415
351
VulkanResourceAllocator* resourceAllocator)
416
352
: mDevice (device),
@@ -423,50 +359,53 @@ VulkanDescriptorSetManager::~VulkanDescriptorSetManager() = default;
423
359
void VulkanDescriptorSetManager::terminate () noexcept {
424
360
mLayoutManager .reset ();
425
361
mDescriptorPool .reset ();
426
- mHistory .clear ();
427
362
}
428
363
429
364
// bind() is not really binding the set but just stashing until we have all the info
430
365
// (pipelinelayout).
431
366
void VulkanDescriptorSetManager::bind (uint8_t setIndex, VulkanDescriptorSet* set,
432
367
backend::DescriptorSetOffsetArray&& offsets) {
433
- auto history = mHistory [set].get ();
434
- history->setOffsets (std::move (offsets));
435
-
436
- auto lastHistory = mStashedSets [setIndex];
437
- if (lastHistory) {
438
- lastHistory->unbind ();
439
- }
440
- mStashedSets [setIndex] = history;
368
+ set->setOffsets (std::move (offsets));
369
+ mStashedSets [setIndex] = set;
441
370
}
442
371
443
372
void VulkanDescriptorSetManager::commit (VulkanCommandBuffer* commands,
444
373
VkPipelineLayout pipelineLayout, DescriptorSetMask const & setMask) {
445
- DescriptorSetHistoryArray& updateSets = mStashedSets ;
446
-
447
374
// setMask indicates the set of descriptor sets the driver wants to bind, curMask is the
448
375
// actual set of sets that *needs* to be bound.
449
376
DescriptorSetMask curMask = setMask;
450
377
378
+ auto & updateSets = mStashedSets ;
379
+ auto & lastBoundSets = mLastBoundInfo .boundSets ;
380
+
451
381
setMask.forEachSetBit ([&](size_t index ) {
452
- if (!updateSets[index ] || updateSets[index ]-> bound () ) {
382
+ if (!updateSets[index ] || updateSets[index ] == lastBoundSets[ index ] ) {
453
383
curMask.unset (index );
454
384
}
455
385
});
456
386
457
- BoundInfo nextInfo = {
458
- pipelineLayout,
459
- setMask,
460
- updateSets,
461
- };
462
- if (curMask.none () && mLastBoundInfo == nextInfo) {
387
+ if (curMask.none () &&
388
+ (mLastBoundInfo .pipelineLayout == pipelineLayout && mLastBoundInfo .setMask == setMask &&
389
+ mLastBoundInfo .boundSets == updateSets)) {
463
390
return ;
464
391
}
465
392
466
393
curMask.forEachSetBit ([&updateSets, commands, pipelineLayout](size_t index ) {
467
- updateSets[index ]->bind (commands, pipelineLayout, index );
394
+ // This code actually binds the descriptor sets.
395
+ auto set = updateSets[index ];
396
+ VkCommandBuffer const cmdbuffer = commands->buffer ();
397
+ vkCmdBindDescriptorSets (cmdbuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, index ,
398
+ 1 , &set->vkSet , set->uniqueDynamicUboCount , set->getOffsets ()->data ());
399
+ commands->acquire (set);
468
400
});
469
- mLastBoundInfo = nextInfo;
401
+
402
+ mStashedSets = {};
403
+
404
+ mLastBoundInfo = {
405
+ pipelineLayout,
406
+ setMask,
407
+ updateSets,
408
+ };
470
409
}
471
410
472
411
void VulkanDescriptorSetManager::updateBuffer (VulkanDescriptorSet* set, uint8_t binding,
@@ -478,9 +417,8 @@ void VulkanDescriptorSetManager::updateBuffer(VulkanDescriptorSet* set, uint8_t
478
417
};
479
418
480
419
VkDescriptorType type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
481
- auto & history = mHistory [set];
482
420
483
- if (history ->dynamicUboMask .test (binding)) {
421
+ if (set ->dynamicUboMask .test (binding)) {
484
422
type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC;
485
423
}
486
424
VkWriteDescriptorSet const descriptorWrite = {
@@ -494,7 +432,6 @@ void VulkanDescriptorSetManager::updateBuffer(VulkanDescriptorSet* set, uint8_t
494
432
};
495
433
vkUpdateDescriptorSets (mDevice , 1 , &descriptorWrite, 0 , nullptr );
496
434
set->acquire (bufferObject);
497
- history->write (binding);
498
435
}
499
436
500
437
void VulkanDescriptorSetManager::updateSampler (VulkanDescriptorSet* set, uint8_t binding,
@@ -526,7 +463,6 @@ void VulkanDescriptorSetManager::updateSampler(VulkanDescriptorSet* set, uint8_t
526
463
};
527
464
vkUpdateDescriptorSets (mDevice , 1 , &descriptorWrite, 0 , nullptr );
528
465
set->acquire (texture);
529
- mHistory [set]->write (binding);
530
466
}
531
467
532
468
void VulkanDescriptorSetManager::updateInputAttachment (VulkanDescriptorSet* set,
@@ -539,30 +475,24 @@ void VulkanDescriptorSetManager::createSet(Handle<HwDescriptorSet> handle,
539
475
auto const vkSet = mDescriptorPool ->obtainSet (layout);
540
476
auto const & count = layout->count ;
541
477
auto const vklayout = layout->getVkLayout ();
542
- VulkanDescriptorSet* set = mResourceAllocator ->construct <VulkanDescriptorSet>(handle,
543
- mResourceAllocator , vkSet, [vkSet, count, vklayout, this ](VulkanDescriptorSet* set) {
478
+ mResourceAllocator ->construct <VulkanDescriptorSet>(handle, mResourceAllocator , vkSet,
479
+ layout->bitmask .dynamicUbo , layout->count .dynamicUbo ,
480
+ [vkSet, count, vklayout, this ](VulkanDescriptorSet* set) {
544
481
eraseSetFromHistory (set);
545
482
mDescriptorPool ->recycle (count, vklayout, vkSet);
546
483
});
547
- mHistory [set] = std::make_unique<DescriptorSetHistory>(layout->bitmask .dynamicUbo ,
548
- layout->count .dynamicUbo , mResourceAllocator , set);
549
484
}
550
485
551
486
void VulkanDescriptorSetManager::destroySet (Handle <HwDescriptorSet> handle) {
552
- VulkanDescriptorSet* set = mResourceAllocator ->handle_cast <VulkanDescriptorSet*>(handle);
553
- eraseSetFromHistory (set);
554
487
}
555
488
556
489
void VulkanDescriptorSetManager::initVkLayout (VulkanDescriptorSetLayout* layout) {
557
490
layout->setVkLayout (mLayoutManager ->getVkLayout (layout));
558
491
}
559
492
560
493
void VulkanDescriptorSetManager::eraseSetFromHistory (VulkanDescriptorSet* set) {
561
- DescriptorSetHistory* history = mHistory [set].get ();
562
- mHistory .erase (set);
563
-
564
494
for (uint8_t i = 0 ; i < mStashedSets .size (); ++i) {
565
- if (mStashedSets [i] == history ) {
495
+ if (mStashedSets [i] == set ) {
566
496
mStashedSets [i] = nullptr ;
567
497
}
568
498
}
0 commit comments