我目前正在用Vulkan用C++编写一个游戏引擎,现在我正在把我的意大利面条代码简化成更直观的东西。作为这个过程的一部分,我推广了一个描述符
struct
创建引擎所需的几个组件,以及
结构
由具有不同类型的多个对象继承。
这不是我之间的问题
SSBO
和
UBO
对象,因为它们都有一个成员变量
VkDescriptorBufferInfo
类型然而,我
Texture
对象的不同之处在于成员变量属于
VkDescriptorImageInfo
类型
这两种类型由
VkWriteDescriptorSet
对象,其在“vulkan_core.h”标头中的定义为:
typedef struct VkWriteDescriptorSet {
VkStructureType sType;
const void* pNext;
VkDescriptorSet dstSet;
uint32_t dstBinding;
uint32_t dstArrayElement;
uint32_t descriptorCount;
VkDescriptorType descriptorType;
const VkDescriptorImageInfo* pImageInfo; //to be provided by my Texture objects
const VkDescriptorBufferInfo* pBufferInfo; //to be provided by my UBO/SSBO objects
const VkBufferView* pTexelBufferView;
} VkWriteDescriptorSet;
我试图概括
VkWriteDescriptorSet
对象如下:
template <typename T>
inline VkWriteDescriptorSet writeSet(const std::vector<T>& bufferInfo, std::array<size_t,2> dst) {
VkWriteDescriptorSet writeInfo { VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET };
/* Here I try to differentiate how the VkWriteDescriptorSet object is created */
if (std::is_same<T, VkDescriptorImageInfo>::value) {
writeInfo.pImageInfo = &bufferInfo[dst[1]]; //requires T == VkDescriptorImageInfo
}
else {
writeInfo.pBufferInfo = &bufferInfo[dst[1]]; //requires T == VkDescriptorBufferInfo
}
/* Then the function fills out the rest of the object */
return writeInfo;
}
以上是在以下函数中调用的:
void writeSets(uint32_t bindingCount) {
// The type of the vector below depends on the calling object (UBO/SSBO or Texture)
std::vector</* VkDescriptorBufferInfo or VkDescriptorImageInfo */> bufferInfo(bindingCount);
std::vector<VkWriteDescriptorSet> descriptorWrites(bindingCount);
for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
/* loop does stuff */
for (size_t j = 0; j < bindingCount; j++) {
/* loop does more stuff */
descriptorWrites[j] = writeSet(bufferInfo, { i, j }); // the generalized function is called
}
vkUpdateDescriptorSets(VkGPU::device, bindingCount, descriptorWrites.data(), 0, nullptr);
}
}
不幸的是,我的程序将无法编译,并返回以下错误:
Severity: Error
Line: 27
Code: C2440
Description: '=': cannot convert from 'const _Ty *' to 'const VkDescriptorImageInfo *'
这最后引出了我的问题,我如何在“if”和“switch”语句中使用模板参数?
编辑:
我试图推广该函数的另一种方法是使用switch语句
VkDescriptorType
enum
:
template <typename T>
inline VkWriteDescriptorSet writeSet(const std::vector<T>& bufferInfo, std::array<size_t,2> dst) {
VkWriteDescriptorSet writeInfo { VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET };
/* Here I try to differentiate how the VkWriteDescriptorSet object is created */
switch (type) {
case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
writeInfo.pImageInfo = &bufferInfo[dst[1]]; //requires T == VkDescriptorImageInfo
default:
writeInfo.pBufferInfo = &bufferInfo[dst[1]]; //requires T == VkDescriptorBufferInfo
}
/* Then the function fills out the rest of the object */
return writeInfo;
}