Skip to content

Commit 37a50d0

Browse files
committed
VK: More refactor of the external sampler flow
Query the texture directly to know if it uses an external sampler or not. This is already known when the texture is created. Remove dead code related to the external sampler flow. Simplify the code of the VulkanExternalImageManager class.
1 parent 1ddd10f commit 37a50d0

File tree

8 files changed

+10
-92
lines changed

8 files changed

+10
-92
lines changed

filament/backend/src/vulkan/VulkanDescriptorSetCache.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,8 +355,8 @@ void VulkanDescriptorSetCache::updateBuffer(fvkmemory::resource_ptr<VulkanDescri
355355
}
356356

357357
void VulkanDescriptorSetCache::updateSampler(fvkmemory::resource_ptr<VulkanDescriptorSet> set,
358-
uint8_t binding, fvkmemory::resource_ptr<VulkanTexture> texture, VkSampler sampler,
359-
VkDescriptorSetLayout externalSamplerLayout) noexcept {
358+
uint8_t binding, fvkmemory::resource_ptr<VulkanTexture> texture,
359+
VkSampler sampler) noexcept {
360360

361361
// We have to update a bound set for two use cases
362362
// - streaming API (a changing feed of AHardwareBuffer)

filament/backend/src/vulkan/VulkanDescriptorSetCache.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,7 @@ class VulkanDescriptorSetCache {
5555
VkDeviceSize size) noexcept;
5656

5757
void updateSampler(fvkmemory::resource_ptr<VulkanDescriptorSet> set, uint8_t binding,
58-
fvkmemory::resource_ptr<VulkanTexture> texture, VkSampler sampler,
59-
VkDescriptorSetLayout externalSamplerLayout = VK_NULL_HANDLE) noexcept;
60-
61-
void updateSamplerForExternalSamplerSet(fvkmemory::resource_ptr<VulkanDescriptorSet> set, uint8_t binding,
62-
fvkmemory::resource_ptr<VulkanTexture> texture) noexcept;
63-
58+
fvkmemory::resource_ptr<VulkanTexture> texture, VkSampler sampler) noexcept;
6459

6560
void updateInputAttachment(fvkmemory::resource_ptr<VulkanDescriptorSet> set,
6661
VulkanAttachment const& attachment) noexcept;

filament/backend/src/vulkan/VulkanDriver.cpp

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ void VulkanDriver::updateDescriptorSetTexture(
507507
auto set = resource_ptr<VulkanDescriptorSet>::cast(&mResourceManager, dsh);
508508
auto texture = resource_ptr<VulkanTexture>::cast(&mResourceManager, th);
509509

510-
if (UTILS_UNLIKELY(mExternalImageManager.isExternallySampledTexture(texture))) {
510+
if (UTILS_UNLIKELY(texture->isExternallySampled())) {
511511
mExternalImageManager.bindExternallySampledTexture(set, binding, texture, params);
512512
mAppState.hasBoundExternalImages = true;
513513
set->isAnExternalSamplerBound = true;
@@ -767,10 +767,6 @@ void VulkanDriver::createTextureExternalImage2R(Handle<HwTexture> th, backend::S
767767
// texture into the read layout.
768768
texture->transitionLayout(&commands, texture->getPrimaryViewRange(), VulkanLayout::FRAG_READ);
769769

770-
if (imgData.external.valid()) {
771-
mExternalImageManager.addExternallySampledTexture(texture, conversion);
772-
}
773-
774770
texture.inc();
775771
mResourceManager.associateHandle(th.getId(), std::move(tag));
776772
}
@@ -814,8 +810,6 @@ void VulkanDriver::destroyTexture(Handle<HwTexture> th) {
814810
}
815811
auto texture = resource_ptr<VulkanTexture>::cast(&mResourceManager, th);
816812
texture.dec();
817-
818-
mExternalImageManager.removeExternallySampledTexture(texture);
819813
}
820814

821815
void VulkanDriver::createProgramR(Handle<HwProgram> ph, Program&& program, utils::ImmutableCString&& tag) {
@@ -1429,8 +1423,6 @@ void VulkanDriver::updateStreams(CommandStream* driver) {
14291423
VulkanLayout::FRAG_READ);
14301424

14311425
if (imgData.external.valid()) {
1432-
mExternalImageManager.addExternallySampledTexture(newTexture,
1433-
conversion);
14341426
// Cache the AHB backed image. Acquires the image here.
14351427
s->pushImage(image, newTexture);
14361428
}
@@ -2409,7 +2401,6 @@ void VulkanDriver::bindPipeline(PipelineState const& pipelineState) {
24092401
if (std::any_of(layoutHandles.begin(), layoutHandles.end(), haveExternalSamplers)) {
24102402
BindInDrawBundle bundle = {
24112403
.pipelineState = pipelineState,
2412-
.dsLayoutHandles = layoutHandles,
24132404
.descriptorSetMask = descriptorSetMask,
24142405
};
24152406
mPipelineState.bindInDraw = { true, bundle };
@@ -2555,7 +2546,6 @@ void VulkanDriver::draw2(uint32_t indexOffset, uint32_t indexCount, uint32_t ins
25552546
VkCommandBuffer cmdbuffer = mCurrentRenderPass.commandBuffer->buffer();
25562547
auto const& [doBindInDraw, bundle] = mPipelineState.bindInDraw;
25572548

2558-
fvkutils::DescriptorSetMask setsWithExternalSamplers = {};
25592549
if (doBindInDraw) {
25602550
// Create the new pipeline layout from the current bounded descriptor sets.
25612551
// The layout of the descriptor sets at this point should have the final one taking into account

filament/backend/src/vulkan/VulkanDriver.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,6 @@ class VulkanDriver final : public DriverBase {
177177

178178
struct BindInDrawBundle {
179179
PipelineState pipelineState = {};
180-
DescriptorSetLayoutHandleList dsLayoutHandles = {};
181180
fvkutils::DescriptorSetMask descriptorSetMask = {};
182181
resource_ptr<VulkanProgram> program = {};
183182
};

filament/backend/src/vulkan/VulkanExternalImageManager.cpp

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,6 @@ void erasep(std::vector<T>& v, std::function<bool(T const&)> f) {
3737
v.erase(newEnd, v.end());
3838
}
3939

40-
using ImageData = VulkanExternalImageManager::VulkanExternalImageManager::ImageData;
41-
ImageData& findImage(std::vector<ImageData>& images,
42-
fvkmemory::resource_ptr<VulkanTexture> texture) {
43-
auto itr = std::find_if(images.begin(), images.end(), [&](ImageData const& data) {
44-
return data.image == texture;
45-
});
46-
assert_invariant(itr != images.end());
47-
return *itr;
48-
}
49-
5040
}// namespace
5141

5242
VulkanExternalImageManager::VulkanExternalImageManager(VulkanSamplerCache* samplerCache,
@@ -61,7 +51,6 @@ VulkanExternalImageManager::~VulkanExternalImageManager() = default;
6151

6252
void VulkanExternalImageManager::terminate() {
6353
mSetBindings.clear();
64-
mImages.clear();
6554
}
6655

6756
void VulkanExternalImageManager::updateSetAndLayout(
@@ -96,9 +85,7 @@ void VulkanExternalImageManager::updateSetAndLayout(
9685
actualExternalSamplers, outSamplers);
9786
// Update the external samplers in the set
9887
for (auto& [binding, sampler, image]: samplerAndBindings) {
99-
// We cannot call updateSamplerForExternalSamplerSet because some samplers are non NULL
100-
// (RGB) and we cannot do a combined update with a NULL sampler.
101-
mDescriptorSetCache->updateSampler(set, binding, image, sampler, set->boundLayout);
88+
mDescriptorSetCache->updateSampler(set, binding, image, sampler);
10289
}
10390
}
10491

@@ -137,8 +124,6 @@ void VulkanExternalImageManager::removeDescriptorSet(
137124
void VulkanExternalImageManager::bindExternallySampledTexture(
138125
fvkmemory::resource_ptr<VulkanDescriptorSet> set, uint8_t bindingPoint,
139126
fvkmemory::resource_ptr<VulkanTexture> image, SamplerParams samplerParams) {
140-
// Should we do duplicate validation here?
141-
auto& imageData = findImage(mImages, image);
142127
// according to spec, these must match chromaFilter
143128
// https://registry.khronos.org/vulkan/specs/latest/man/html/VkSamplerCreateInfo.html#VUID-VkSamplerCreateInfo-minFilter-01645
144129
samplerParams.filterMag = SamplerMagFilter::NEAREST;
@@ -153,34 +138,10 @@ void VulkanExternalImageManager::bindExternallySampledTexture(
153138

154139
VkSampler const sampler = mSamplerCache->getSampler({
155140
.sampler = samplerParams,
156-
.conversion = imageData.conversion,
157-
});
158-
159-
mSetBindings.push_back({ bindingPoint, imageData.image, set, sampler });
160-
}
161-
162-
void VulkanExternalImageManager::addExternallySampledTexture(
163-
fvkmemory::resource_ptr<VulkanTexture> image, VkSamplerYcbcrConversion const conversion) {
164-
mImages.push_back({
165-
.image = image,
166-
.conversion = conversion,
167-
});
168-
}
169-
170-
void VulkanExternalImageManager::removeExternallySampledTexture(
171-
fvkmemory::resource_ptr<VulkanTexture> image) {
172-
erasep<SetBindingInfo>(mSetBindings,
173-
[&](auto const& bindingInfo) { return (bindingInfo.image == image); });
174-
erasep<ImageData>(mImages, [&](auto const& imageData) {
175-
return imageData.image == image;
141+
.conversion = image->getYcbcrConversion(),
176142
});
177-
}
178143

179-
bool VulkanExternalImageManager::isExternallySampledTexture(
180-
fvkmemory::resource_ptr<VulkanTexture> image) const {
181-
return std::find_if(mImages.begin(), mImages.end(), [&](auto const& imageData) {
182-
return imageData.image == image;
183-
}) != mImages.end();
144+
mSetBindings.push_back({ bindingPoint, image, set, sampler });
184145
}
185146

186147
void VulkanExternalImageManager::clearTextureBinding(

filament/backend/src/vulkan/VulkanExternalImageManager.h

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,21 +62,9 @@ class VulkanExternalImageManager {
6262
void clearTextureBinding(fvkmemory::resource_ptr<VulkanDescriptorSet> set,
6363
uint8_t bindingPoint);
6464

65-
void addExternallySampledTexture(fvkmemory::resource_ptr<VulkanTexture> external,
66-
VkSamplerYcbcrConversion const conversion);
67-
68-
void removeExternallySampledTexture(fvkmemory::resource_ptr<VulkanTexture> image);
69-
70-
bool isExternallySampledTexture(fvkmemory::resource_ptr<VulkanTexture> image) const;
71-
7265
VkSamplerYcbcrConversion getVkSamplerYcbcrConversion(
7366
VulkanPlatform::ExternalImageMetadata const& metadata);
7467

75-
struct ImageData {
76-
fvkmemory::resource_ptr<VulkanTexture> image;
77-
VkSamplerYcbcrConversion conversion = VK_NULL_HANDLE;
78-
};
79-
8068
// - Update the descriptor set layout with the external samplers. This layout will be assign the
8169
// respective immutable samplers to the layout.
8270
// - Switch from binding the base layout to the new one.
@@ -104,7 +92,6 @@ class VulkanExternalImageManager {
10492

10593
// Use vectors instead of hash maps because we only expect small number of entries.
10694
std::vector<SetBindingInfo> mSetBindings;
107-
std::vector<ImageData> mImages;
10895
};
10996

11097
} // filament::backend

filament/backend/src/vulkan/VulkanTexture.cpp

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -840,19 +840,6 @@ void VulkanTexture::setLayout(VkImageSubresourceRange const& range, VulkanLayout
840840
}
841841
}
842842

843-
void VulkanTexture::setYcbcrConversion(VkSamplerYcbcrConversion conversion) {
844-
// Note that this comparison is valid because we only ever create VkSamplerYcbcrConversion from
845-
// a cache. So for each set of parameters, there is exactly one conversion (similar to
846-
// samplers).
847-
VulkanTextureState::Ycbcr ycbcr = {
848-
.conversion = conversion,
849-
};
850-
if (mState->mYcbcr != ycbcr) {
851-
mState->mYcbcr = ycbcr;
852-
mState->clearCachedImageViews();
853-
}
854-
}
855-
856843
VulkanLayout VulkanTexture::getLayout(uint32_t layer, uint32_t level) const {
857844
assert_invariant(level <= 0xffff && layer <= 0xffff);
858845
const uint32_t key = (layer << 16) | level;

filament/backend/src/vulkan/VulkanTexture.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,8 @@ struct VulkanTexture : public HwTexture, fvkmemory::Resource {
242242
return mState->mIsProtected;
243243
}
244244

245+
bool isExternallySampled() const { return mState->mYcbcr.conversion != VK_NULL_HANDLE; }
246+
245247
bool transitionLayout(VulkanCommandBuffer* commands, VkImageSubresourceRange const& range,
246248
VulkanLayout newLayout);
247249

@@ -262,10 +264,7 @@ struct VulkanTexture : public HwTexture, fvkmemory::Resource {
262264
// manually (outside of calls to transitionLayout).
263265
void setLayout(VkImageSubresourceRange const& range, VulkanLayout newLayout);
264266

265-
// This is used in the case of external images and external samplers. AHB might update the
266-
// conversion per-frame. This implies that we need to invalidate the view cache when that
267-
// happens.
268-
void setYcbcrConversion(VkSamplerYcbcrConversion conversion);
267+
VkSamplerYcbcrConversion getYcbcrConversion() const { return mState->mYcbcr.conversion; }
269268

270269
#if FVK_ENABLED(FVK_DEBUG_TEXTURE)
271270
void print() const;

0 commit comments

Comments
 (0)