函数 GetMemoryType 的理解

从开始接触Vulkan,有个函数一直不解,今日做一个记录:

/**
 * Get the index of a memory type that has all the requested property bits set
 *
 * @param typeBits Bit mask with bits set for each memory type supported by the resource to request for (from VkMemoryRequirements) 
 * @param properties Bit mask of properties for the memory type to request
 * 
 * @return Index of the requested memory type
 *
 * @throw Throws an exception if memTypeFound is null and no memory type could be found that supports the requested properties
 */
 uint32_t GetMemoryType(uint32_t typeBits, VkMemoryPropertyFlags properties) const {
    for (uint32_t i = 0; i < memoryProperties.memoryTypeCount; i++) {           
        if ((typeBits & 1) == 1) { // 问题一: 为什么要和1做与?
            if ((memoryProperties.memoryTypes[i].propertyFlags & properties) == properties) { // 问题二:为什么要和properties做与?
                return i;
            }
        }
        typeBits >>= 1;
    }
    throw std::runtime_error("Could not find a matching memory type");
 }

上述代码有两个地方需要理解,才能真正理解内存分配请求,如果不理解,直接抄写,也是没有问题的。
要回答上述两个问题,需要往前看:

// 省略部分代码
// Get memory requirements for the staging buffer (alignment, memory type bits)
vkGetBufferMemoryRequirements(logicalDevice, stagingBuffer, &reqs);
// Get memory type index for a host visible buffer
uint32_t index = GetMemoryType(reqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
// 省略部分代码

根据上述代码可以得出:
typeBits:是通过函数vkGetBufferMemoryRequirements获取,是驱动返回的,代表的含义是:针对stagingBuffer这个缓存资源来说,驱动内部内存能够用来存放的类型集合,按位或以后,返回的。
properties:是应用程序直接传入的,代表了应用程序针对stagingBuffer这个缓存资源,额外需要的目的,HOST可见,且COHERENT

多说一句,Vulkan编程习惯里面的stagingBuffer一般都是用于数据上传至GPU上的中间缓存,也就是说,CPU必须可见,CPU往里面写入数据后,GPU侧最好也能直接看到,无需flush最好,但是GPU访问数据高效类型是DEVICE类型,所以,最后还需要经过一次拷贝。

现在来回答上述两个问题:

  1. 和1相与,是为了直接筛选出,驱动支持的内存类型;
  2. properties相与,是为了在驱动已经支持的前提下,找到额外的目的属性的内存类型。

这就是为什么,在经过寻找以后,如果没有交集的话,直接抛异常。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值