mesa/src/gfxstream/guest/vulkan_enc/DescriptorSetVirtualization.cpp
Gurchetan Singh 7b50e62179 gfxstream: mega-change to support guest Linux WSI with gfxstream
This is a mega-change to support Linux guest WSI with gfxstream.
We tried to do a branch where every commit was buildable and
runnable, but that quickly proved unworkable. So we squashed
the branch into a mega-change.

Zink provides the GL implementation for Linux guests, so we just
needed to implement the proper Vulkan Wayland/X11 WSI
entrypoints.

The overall strategy to support this is to use Mesa's WSI
functions.  The Vulkan WSI layer was also considered:

https://gitlab.freedesktop.org/mesa/vulkan-wsi-layer

But it was less maintained compared to Mesa.  The way Mesa common
layers communicate with drivers is the through base objects
embedded in driver and a common dispatch layer:

https://gitlab.freedesktop.org/mesa/mesa/-/blob/main/docs/vulkan/dispatch.rst
https://gitlab.freedesktop.org/mesa/mesa/-/blob/main/docs/vulkan/base-objs.rst

Our objects are defined in gfxstream_vk_private.h.  Currently,
Mesa-derived Vulkan objects just serve as shim to gfxstream
Vulkan’s internal handle mapping. Long-term, we can use
Mesa-derived objects inside gfxstream guest Vulkan exclusively.

The flow is typically inside a Vulkan entrypoint is:

- VK_FROM_HANDLE(vk-object) to convert to a gfxstream_vk_obj
  object
- Call ResourceTracker::func(gfxstream_vk_obj->internal) or
  VkEncoder::func(gfxstream_vk_obj>internal)
- Return result

A good follow-up cleanup would be to delete older gfxstream
objects.  For example, we now have struct gfxstream_vk_device
and info_VkDevice in ResourceTracker.

Most of this logic was auto-generated and included in
func_table.cpp. Some vulkan functions were too difficult to
auto-generate or required special logic, and these are included
in gfxstream_vk_device.cpp.  For example, anything that needs to
setup the HostConnection requires special handling.

Android Blueprint support is added to the parts of Mesa needed
to build the Vulkan runtime.  One thing to call out it's
required to build the guest/vulkan_enc and guest/vulkan files
in the same shared library now, when previously have
libvulkan_enc.so and libvulkan_ranchu.so was sufficient
[otherwise, some weak pointer logic wouldn't work].

A side effect of this is libOpenglSystem must also be a static
lib, and so should libandroid_aemu too.  That conceptually makes
sense and the Meson build had been doing this all a long.  We
can probably transition everything besides libGLESv1_emulation.so,
libGLESv2_emulation.so and libvulkan_ranchu.so to be static.

This requires changes in the end2end tests, because since each
HostConnection is separate and internal to it's constituent
library. Lifetimes need to be managed separately: for example
the HostConnection instance created by the end2end tests would
not be visible inside libvulkan_ranchu.so anymore. Probably the
best solution would be improve the testing facade so a
HostConnection represents one virtio-gpu context, while some
other entity represents a virtio-gpu device (client-server
would work).

vk.xml was modified, but change sent to Khronos:

https://gitlab.khronos.org/vulkan/vulkan/-/merge_requests/6325

Fuchsia builds still need to be migrated, but they already have
Fuchsia Mesa with all the build rules so that shouldn't be too
bad. Just need to copy them over the gfxstream/Mesa hybrid.

The new command for building Linux guests is:

meson amd64-build/ -Dvulkan-drivers="gfxstream" -Dgallium-drivers="" -Dvk-no-nir=true -Dopengl=false

Big shout-out to Aaron Ruby, who did most of the gnarly codegen
needed to get the function table logic to work.

     * Run Weston/vkcube on Linux and automotive platform
     * launch_cvd --gpu_mode=gfxstream vkcube
     * launch_cvd --gpu_mod=gfxstream_guest_angle
     * vkcube + 3D Mark Slingshot extreme work with guest ANGLE and
       GL-VK interop
     * GfxstreamEnd2EndTests
     * Some select dEQP tests

Aaron Ruby (46):
  gfxstream: function table: remove entry points that are hand-written.
  gfxstream: function table: more changes
  gfxstream: function table: scope internal_arrays to encoder
  gfxstream: function table: autogenerate compoundType params
  gfxstream: add handwritten EnumeratePhysicalDeviceGroup entrypoint.
  gfxstream: function table: handle nested handle arrays
  gfxstream: function table: adding some handwritten implementations
  gfxstream: revert some unnecessary changes
  gfxstream: use vk_object_zalloc/free instead of vk_zalloc/free.
  gfxstream: revert most gfxstream objects to use vk_object_base
  gfxstream: function table: handwritten commmand-buffers/pools
  gfxstream: codegen functionality to handle special param
  gfxstream: function table: random fixes
  gfxstream: add vk_command_buffer_ops handlers
  gfxstream: func_table.py: Codegen support for nested compound type
  gfxstream: remove handwritten/add autogen entry points
  gfxstream: add gfxstream_vk_device.cpp
  gfxstream: query device and instance extensions early
  gfxstream: func_table: explicit allocation for nested arrays/compound
    types
  gfxstream: goldfish_vulkan: fix commandBuffer allocation.
  gfxstream: meson: Raise api_version in ICD config to 1.1.
  gfxstream: function table: add more handwritten entries
  gfxstream: goldfish_vulkan: update VkDescriptorSetAllocateInfo logic
  gfxstream: function table: NULL check on internal_object dereference
  gfxstream: function table: Remove POSTPROCESSES handling from
    functable
  gfxstream: mesa: Add 'gfxstream' as a -Dvulkan-drivers
  gfxstream: ResourceTracker: add some allowedExtensions
  gfxstream: gfxstream_vk_device: add wsi_common_entrypoints
  gfxstream: Move instance handling into gfxstream_vk_device.cpp
  gfxstream: ResourceTracker: Enable Linux WSI-related extensions
  gfxstream: wsi: add wsi_device initialization
  gfxstream: gfxstream_vk_device: use Mesa common physical device
    management
  gfxstream: ResourceTracker: translate mesa objects in user buffer
  gfxstream: exclude VkSampler and VkDescriptorSet objects from
    translation
  gfxstream: Add guest-side external memory support with colorBuffers.
  gfxstream: function table: Modify semaphoreList inputs to no-op
    semaphores
  gfxstream: function table: Allow VK_NULL_HANDLE for free/destroy APIs.
  gfxstream: cereal: Add VK_EXT_depth_clip_enable as supported feature.
  gfxstream: vulkan_enc: un-namespace vk_util.h and vk_struct_id.h
  gfxstream: gfxstream_vk_device.cpp: Support VK_KHR_surface and
    VK_*_surface
  gfxstream: vulkan_enc: Add support for Mesa-only extensions.
  gfxstream: ResourceTracker: Use DEVICE_TYPE_VIRTUAL_GPU always
  gfxstream: platform: add dma-buf export support with dedicatedBuffer.
  gfxstream: ResourceTracker: add VK_EXT_depth_clip_enable allowed
    extension
  gfxstream: ResourceTracker: external memory via QNX_screen_buffer
    extension
  gfxstream: Add VK_QNX_external_memory_screen_buffer to VulkanDispatch

Gurchetan Singh (18):
  gfxstream: mesa: write Android.bp files
  gfxstream: generate gfxstream_vk_entrypoints.{c, h}
  gfxstream: vulkan_enc: add gfxstream_vk_private.h (objects)
  gfxstream: function table: modify function table to use gfxstream_vk_*
  gfxstream: compiles
  gfxstream: build system improvements
  gfxstream: ResourceTracker: don't crash without
    VkBindImageMemorySwapchainInfoKHR
  gfxstream: vk.xml: make some vkAcquireImageANDROID params optional
  gfxstream_vk_device: filter out swapchain maintenance guest side
  gfxstream: end2end: fixes for End2End tests
  gfxstream: func_table: custom vkEnumerateInstanceLayerProperties
  gfxstream: add VK_EXT_DEBUG_UTILS_EXTENSION_NAME into Mesa list
  gfxstream: clang-format guest code
  gfxstream: libandroid AEMU static
  gfxstream: vkEnumerateInstanceVersion
  gfxstream: vkCreateComputePipeLines
  gfxstream: make end2end tests happy
  gfxstream: delete prior vk.xml, vk_icd_gen.py

Reviewed-by: Aaron Ruby <aruby@blackberry.com>
Acked-by: Yonggang Luo <luoyonggang@gmail.com>
Acked-by: Adam Jackson <ajax@redhat.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/27246>
2024-09-19 20:05:59 +00:00

521 lines
20 KiB
C++

// Copyright (C) 2021 The Android Open Source Project
// Copyright (C) 2021 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "DescriptorSetVirtualization.h"
#include "Resources.h"
namespace gfxstream {
namespace vk {
void clearReifiedDescriptorSet(ReifiedDescriptorSet* set) {
set->pool = VK_NULL_HANDLE;
set->setLayout = VK_NULL_HANDLE;
set->poolId = -1;
set->allocationPending = false;
set->allWrites.clear();
set->pendingWriteArrayRanges.clear();
}
void initDescriptorWriteTable(const std::vector<VkDescriptorSetLayoutBinding>& layoutBindings,
DescriptorWriteTable& table) {
uint32_t highestBindingNumber = 0;
for (uint32_t i = 0; i < layoutBindings.size(); ++i) {
if (layoutBindings[i].binding > highestBindingNumber) {
highestBindingNumber = layoutBindings[i].binding;
}
}
std::vector<uint32_t> countsEachBinding(highestBindingNumber + 1, 0);
for (uint32_t i = 0; i < layoutBindings.size(); ++i) {
countsEachBinding[layoutBindings[i].binding] = layoutBindings[i].descriptorCount;
}
table.resize(countsEachBinding.size());
for (uint32_t i = 0; i < table.size(); ++i) {
table[i].resize(countsEachBinding[i]);
for (uint32_t j = 0; j < countsEachBinding[i]; ++j) {
table[i][j].type = DescriptorWriteType::Empty;
table[i][j].dstArrayElement = 0;
}
}
}
static void initializeReifiedDescriptorSet(VkDescriptorPool pool, VkDescriptorSetLayout setLayout,
ReifiedDescriptorSet* set) {
set->pendingWriteArrayRanges.clear();
const auto& layoutInfo = *(as_goldfish_VkDescriptorSetLayout(setLayout)->layoutInfo);
initDescriptorWriteTable(layoutInfo.bindings, set->allWrites);
for (size_t i = 0; i < layoutInfo.bindings.size(); ++i) {
// Bindings can be sparsely defined
const auto& binding = layoutInfo.bindings[i];
uint32_t bindingIndex = binding.binding;
if (set->bindingIsImmutableSampler.size() <= bindingIndex) {
set->bindingIsImmutableSampler.resize(bindingIndex + 1, false);
}
set->bindingIsImmutableSampler[bindingIndex] =
binding.descriptorCount > 0 &&
(binding.descriptorType == VK_DESCRIPTOR_TYPE_SAMPLER ||
binding.descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER) &&
binding.pImmutableSamplers;
}
set->pool = pool;
set->setLayout = setLayout;
set->allocationPending = true;
set->bindings = layoutInfo.bindings;
}
bool isDescriptorTypeImageInfo(VkDescriptorType descType) {
return (descType == VK_DESCRIPTOR_TYPE_SAMPLER) ||
(descType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER) ||
(descType == VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE) ||
(descType == VK_DESCRIPTOR_TYPE_STORAGE_IMAGE) ||
(descType == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT);
}
bool isDescriptorTypeBufferInfo(VkDescriptorType descType) {
return (descType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER) ||
(descType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) ||
(descType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER) ||
(descType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC);
}
bool isDescriptorTypeBufferView(VkDescriptorType descType) {
return (descType == VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER) ||
(descType == VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER);
}
bool isDescriptorTypeInlineUniformBlock(VkDescriptorType descType) {
return descType == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT;
}
bool isDescriptorTypeAccelerationStructure(VkDescriptorType descType) {
return descType == VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR;
}
void doEmulatedDescriptorWrite(const VkWriteDescriptorSet* write, ReifiedDescriptorSet* toWrite) {
VkDescriptorType descType = write->descriptorType;
uint32_t dstBinding = write->dstBinding;
uint32_t dstArrayElement = write->dstArrayElement;
uint32_t descriptorCount = write->descriptorCount;
DescriptorWriteTable& table = toWrite->allWrites;
uint32_t arrOffset = dstArrayElement;
if (isDescriptorTypeImageInfo(descType)) {
for (uint32_t i = 0; i < descriptorCount; ++i, ++arrOffset) {
if (arrOffset >= table[dstBinding].size()) {
++dstBinding;
arrOffset = 0;
}
auto& entry = table[dstBinding][arrOffset];
entry.imageInfo = write->pImageInfo[i];
entry.type = DescriptorWriteType::ImageInfo;
entry.descriptorType = descType;
}
} else if (isDescriptorTypeBufferInfo(descType)) {
for (uint32_t i = 0; i < descriptorCount; ++i, ++arrOffset) {
if (arrOffset >= table[dstBinding].size()) {
++dstBinding;
arrOffset = 0;
}
auto& entry = table[dstBinding][arrOffset];
entry.bufferInfo = write->pBufferInfo[i];
entry.type = DescriptorWriteType::BufferInfo;
entry.descriptorType = descType;
}
} else if (isDescriptorTypeBufferView(descType)) {
for (uint32_t i = 0; i < descriptorCount; ++i, ++arrOffset) {
if (arrOffset >= table[dstBinding].size()) {
++dstBinding;
arrOffset = 0;
}
auto& entry = table[dstBinding][arrOffset];
entry.bufferView = write->pTexelBufferView[i];
entry.type = DescriptorWriteType::BufferView;
entry.descriptorType = descType;
}
} else if (isDescriptorTypeInlineUniformBlock(descType)) {
const VkWriteDescriptorSetInlineUniformBlock* descInlineUniformBlock =
static_cast<const VkWriteDescriptorSetInlineUniformBlock*>(write->pNext);
while (descInlineUniformBlock &&
descInlineUniformBlock->sType !=
VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET_INLINE_UNIFORM_BLOCK) {
descInlineUniformBlock = static_cast<const VkWriteDescriptorSetInlineUniformBlock*>(
descInlineUniformBlock->pNext);
}
if (!descInlineUniformBlock) {
ALOGE("%s: did not find inline uniform block\n", __func__);
return;
}
auto& entry = table[dstBinding][0];
entry.inlineUniformBlock = *descInlineUniformBlock;
entry.inlineUniformBlockBuffer.assign(
static_cast<const uint8_t*>(descInlineUniformBlock->pData),
static_cast<const uint8_t*>(descInlineUniformBlock->pData) +
descInlineUniformBlock->dataSize);
entry.type = DescriptorWriteType::InlineUniformBlock;
entry.descriptorType = descType;
entry.dstArrayElement = dstArrayElement;
} else if (isDescriptorTypeAccelerationStructure(descType)) {
// TODO
// Look for pNext inline uniform block or acceleration structure.
// Append new DescriptorWrite entry that holds the buffer
ALOGW("%s: Ignoring emulated write for descriptor type 0x%x\n", __func__, descType);
}
}
void doEmulatedDescriptorCopy(const VkCopyDescriptorSet* copy, const ReifiedDescriptorSet* src,
ReifiedDescriptorSet* dst) {
const DescriptorWriteTable& srcTable = src->allWrites;
DescriptorWriteTable& dstTable = dst->allWrites;
// src/dst may be the same descriptor set, so we need to create a temporary array for that case.
// (TODO: Maybe just notice the pointers are the same? can aliasing in any other way happen?)
std::vector<DescriptorWrite> toCopy;
uint32_t currBinding = copy->srcBinding;
uint32_t arrOffset = copy->srcArrayElement;
for (uint32_t i = 0; i < copy->descriptorCount; ++i, ++arrOffset) {
if (arrOffset >= srcTable[currBinding].size()) {
++currBinding;
arrOffset = 0;
}
toCopy.push_back(srcTable[currBinding][arrOffset]);
}
currBinding = copy->dstBinding;
arrOffset = copy->dstArrayElement;
for (uint32_t i = 0; i < copy->descriptorCount; ++i, ++arrOffset) {
if (arrOffset >= dstTable[currBinding].size()) {
++currBinding;
arrOffset = 0;
}
dstTable[currBinding][arrOffset] = toCopy[i];
}
}
void doEmulatedDescriptorImageInfoWriteFromTemplate(VkDescriptorType descType, uint32_t binding,
uint32_t dstArrayElement, uint32_t count,
const VkDescriptorImageInfo* imageInfos,
ReifiedDescriptorSet* set) {
DescriptorWriteTable& table = set->allWrites;
uint32_t currBinding = binding;
uint32_t arrOffset = dstArrayElement;
for (uint32_t i = 0; i < count; ++i, ++arrOffset) {
if (arrOffset >= table[currBinding].size()) {
++currBinding;
arrOffset = 0;
}
auto& entry = table[currBinding][arrOffset];
entry.imageInfo = imageInfos[i];
entry.type = DescriptorWriteType::ImageInfo;
entry.descriptorType = descType;
}
}
void doEmulatedDescriptorBufferInfoWriteFromTemplate(VkDescriptorType descType, uint32_t binding,
uint32_t dstArrayElement, uint32_t count,
const VkDescriptorBufferInfo* bufferInfos,
ReifiedDescriptorSet* set) {
DescriptorWriteTable& table = set->allWrites;
uint32_t currBinding = binding;
uint32_t arrOffset = dstArrayElement;
for (uint32_t i = 0; i < count; ++i, ++arrOffset) {
if (arrOffset >= table[currBinding].size()) {
++currBinding;
arrOffset = 0;
}
auto& entry = table[currBinding][dstArrayElement + i];
entry.bufferInfo = bufferInfos[i];
entry.type = DescriptorWriteType::BufferInfo;
entry.descriptorType = descType;
}
}
void doEmulatedDescriptorBufferViewWriteFromTemplate(VkDescriptorType descType, uint32_t binding,
uint32_t dstArrayElement, uint32_t count,
const VkBufferView* bufferViews,
ReifiedDescriptorSet* set) {
DescriptorWriteTable& table = set->allWrites;
uint32_t currBinding = binding;
uint32_t arrOffset = dstArrayElement;
for (uint32_t i = 0; i < count; ++i, ++arrOffset) {
if (arrOffset >= table[currBinding].size()) {
++currBinding;
arrOffset = 0;
}
auto& entry = table[currBinding][dstArrayElement + i];
entry.bufferView = bufferViews[i];
entry.type = DescriptorWriteType::BufferView;
entry.descriptorType = descType;
}
}
void doEmulatedDescriptorInlineUniformBlockFromTemplate(VkDescriptorType descType, uint32_t binding,
uint32_t dstArrayElement, uint32_t count,
const void* pData,
ReifiedDescriptorSet* set) {
DescriptorWriteTable& table = set->allWrites;
auto& entry = table[binding][0];
entry.dstArrayElement = dstArrayElement;
entry.inlineUniformBlockBuffer.assign(static_cast<const uint8_t*>(pData),
static_cast<const uint8_t*>(pData) + count);
entry.type = DescriptorWriteType::InlineUniformBlock;
entry.descriptorType = descType;
}
static bool isBindingFeasibleForAlloc(
const DescriptorPoolAllocationInfo::DescriptorCountInfo& countInfo,
const VkDescriptorSetLayoutBinding& binding) {
if (binding.descriptorCount && (countInfo.type != binding.descriptorType)) {
return false;
}
uint32_t availDescriptorCount = countInfo.descriptorCount - countInfo.used;
if (availDescriptorCount < binding.descriptorCount) {
ALOGV(
"%s: Ran out of descriptors of type 0x%x. "
"Wanted %u from layout but "
"we only have %u free (total in pool: %u)\n",
__func__, binding.descriptorType, binding.descriptorCount,
countInfo.descriptorCount - countInfo.used, countInfo.descriptorCount);
return false;
}
return true;
}
static bool isBindingFeasibleForFree(
const DescriptorPoolAllocationInfo::DescriptorCountInfo& countInfo,
const VkDescriptorSetLayoutBinding& binding) {
if (countInfo.type != binding.descriptorType) return false;
if (countInfo.used < binding.descriptorCount) {
ALOGV(
"%s: Was a descriptor set double freed? "
"Ran out of descriptors of type 0x%x. "
"Wanted to free %u from layout but "
"we only have %u used (total in pool: %u)\n",
__func__, binding.descriptorType, binding.descriptorCount, countInfo.used,
countInfo.descriptorCount);
return false;
}
return true;
}
static void allocBindingFeasible(const VkDescriptorSetLayoutBinding& binding,
DescriptorPoolAllocationInfo::DescriptorCountInfo& poolState) {
poolState.used += binding.descriptorCount;
}
static void freeBindingFeasible(const VkDescriptorSetLayoutBinding& binding,
DescriptorPoolAllocationInfo::DescriptorCountInfo& poolState) {
poolState.used -= binding.descriptorCount;
}
static VkResult validateDescriptorSetAllocation(const VkDescriptorSetAllocateInfo* pAllocateInfo) {
VkDescriptorPool pool = pAllocateInfo->descriptorPool;
DescriptorPoolAllocationInfo* poolInfo = as_goldfish_VkDescriptorPool(pool)->allocInfo;
// Check the number of sets available.
auto setsAvailable = poolInfo->maxSets - poolInfo->usedSets;
if (setsAvailable < pAllocateInfo->descriptorSetCount) {
ALOGV(
"%s: Error: VkDescriptorSetAllocateInfo wants %u sets "
"but we only have %u available. "
"Bailing with VK_ERROR_OUT_OF_POOL_MEMORY.\n",
__func__, pAllocateInfo->descriptorSetCount, setsAvailable);
return VK_ERROR_OUT_OF_POOL_MEMORY;
}
// Perform simulated allocation and error out with
// VK_ERROR_OUT_OF_POOL_MEMORY if it fails.
std::vector<DescriptorPoolAllocationInfo::DescriptorCountInfo> descriptorCountCopy =
poolInfo->descriptorCountInfo;
for (uint32_t i = 0; i < pAllocateInfo->descriptorSetCount; ++i) {
if (!pAllocateInfo->pSetLayouts[i]) {
ALOGV("%s: Error: Tried to allocate a descriptor set with null set layout.\n",
__func__);
return VK_ERROR_INITIALIZATION_FAILED;
}
auto setLayoutInfo =
as_goldfish_VkDescriptorSetLayout(pAllocateInfo->pSetLayouts[i])->layoutInfo;
if (!setLayoutInfo) {
return VK_ERROR_INITIALIZATION_FAILED;
}
for (const auto& binding : setLayoutInfo->bindings) {
bool success = false;
for (auto& pool : descriptorCountCopy) {
if (!isBindingFeasibleForAlloc(pool, binding)) continue;
success = true;
allocBindingFeasible(binding, pool);
break;
}
if (!success) {
return VK_ERROR_OUT_OF_POOL_MEMORY;
}
}
}
return VK_SUCCESS;
}
void applyDescriptorSetAllocation(VkDescriptorPool pool, VkDescriptorSetLayout setLayout) {
auto allocInfo = as_goldfish_VkDescriptorPool(pool)->allocInfo;
auto setLayoutInfo = as_goldfish_VkDescriptorSetLayout(setLayout)->layoutInfo;
++allocInfo->usedSets;
for (const auto& binding : setLayoutInfo->bindings) {
for (auto& countForPool : allocInfo->descriptorCountInfo) {
if (!isBindingFeasibleForAlloc(countForPool, binding)) continue;
allocBindingFeasible(binding, countForPool);
break;
}
}
}
void removeDescriptorSetAllocation(VkDescriptorPool pool,
const std::vector<VkDescriptorSetLayoutBinding>& bindings) {
auto allocInfo = as_goldfish_VkDescriptorPool(pool)->allocInfo;
if (0 == allocInfo->usedSets) {
ALOGV("%s: Warning: a descriptor set was double freed.\n", __func__);
return;
}
--allocInfo->usedSets;
for (const auto& binding : bindings) {
for (auto& countForPool : allocInfo->descriptorCountInfo) {
if (!isBindingFeasibleForFree(countForPool, binding)) continue;
freeBindingFeasible(binding, countForPool);
break;
}
}
}
void fillDescriptorSetInfoForPool(VkDescriptorPool pool, VkDescriptorSetLayout setLayout,
VkDescriptorSet set) {
DescriptorPoolAllocationInfo* allocInfo = as_goldfish_VkDescriptorPool(pool)->allocInfo;
ReifiedDescriptorSet* newReified = new ReifiedDescriptorSet;
newReified->poolId = as_goldfish_VkDescriptorSet(set)->underlying;
newReified->allocationPending = true;
as_goldfish_VkDescriptorSet(set)->reified = newReified;
allocInfo->allocedPoolIds.insert(newReified->poolId);
allocInfo->allocedSets.insert(set);
initializeReifiedDescriptorSet(pool, setLayout, newReified);
}
VkResult validateAndApplyVirtualDescriptorSetAllocation(
const VkDescriptorSetAllocateInfo* pAllocateInfo, VkDescriptorSet* pSets) {
VkResult validateRes = validateDescriptorSetAllocation(pAllocateInfo);
if (validateRes != VK_SUCCESS) return validateRes;
for (uint32_t i = 0; i < pAllocateInfo->descriptorSetCount; ++i) {
applyDescriptorSetAllocation(pAllocateInfo->descriptorPool, pAllocateInfo->pSetLayouts[i]);
}
VkDescriptorPool pool = pAllocateInfo->descriptorPool;
DescriptorPoolAllocationInfo* allocInfo = as_goldfish_VkDescriptorPool(pool)->allocInfo;
if (allocInfo->freePoolIds.size() < pAllocateInfo->descriptorSetCount) {
ALOGE(
"%s: FATAL: Somehow out of descriptor pool IDs. Wanted %u IDs but only have %u free "
"IDs remaining. The count for maxSets was %u and used was %u\n",
__func__, pAllocateInfo->descriptorSetCount, (uint32_t)allocInfo->freePoolIds.size(),
allocInfo->maxSets, allocInfo->usedSets);
abort();
}
for (uint32_t i = 0; i < pAllocateInfo->descriptorSetCount; ++i) {
uint64_t id = allocInfo->freePoolIds.back();
allocInfo->freePoolIds.pop_back();
VkDescriptorSet newSet = new_from_host_VkDescriptorSet((VkDescriptorSet)id);
pSets[i] = newSet;
fillDescriptorSetInfoForPool(pool, pAllocateInfo->pSetLayouts[i], newSet);
}
return VK_SUCCESS;
}
bool removeDescriptorSetFromPool(VkDescriptorSet set, bool usePoolIds) {
ReifiedDescriptorSet* reified = as_goldfish_VkDescriptorSet(set)->reified;
VkDescriptorPool pool = reified->pool;
DescriptorPoolAllocationInfo* allocInfo = as_goldfish_VkDescriptorPool(pool)->allocInfo;
if (usePoolIds) {
// Look for the set's pool Id in the pool. If not found, then this wasn't really allocated,
// and bail.
if (allocInfo->allocedPoolIds.find(reified->poolId) == allocInfo->allocedPoolIds.end()) {
return false;
}
}
const std::vector<VkDescriptorSetLayoutBinding>& bindings = reified->bindings;
removeDescriptorSetAllocation(pool, bindings);
if (usePoolIds) {
allocInfo->freePoolIds.push_back(reified->poolId);
allocInfo->allocedPoolIds.erase(reified->poolId);
}
allocInfo->allocedSets.erase(set);
return true;
}
std::vector<VkDescriptorSet> clearDescriptorPool(VkDescriptorPool pool, bool usePoolIds) {
std::vector<VkDescriptorSet> toClear;
for (auto set : as_goldfish_VkDescriptorPool(pool)->allocInfo->allocedSets) {
toClear.push_back(set);
}
for (auto set : toClear) {
removeDescriptorSetFromPool(set, usePoolIds);
}
return toClear;
}
} // namespace vk
} // namespace gfxstream