函数 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相与,是为了在驱动已经支持的前提下,找到额外的目的属性的内存类型。

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

相关推荐

  1. 函数 GetMemoryType 理解

    2024-03-28 11:40:03       42 阅读
  2. hasattr() 函数理解

    2024-03-28 11:40:03       63 阅读
  3. setattr()函数理解

    2024-03-28 11:40:03       64 阅读
  4. 深入理解OnCalculate函数运行机制

    2024-03-28 11:40:03       48 阅读
  5. 深入理解C++中inline函数

    2024-03-28 11:40:03       32 阅读
  6. 解析PythonLambda函数:【理解】与【运用】

    2024-03-28 11:40:03       53 阅读
  7. 理解并实现C语言中strcpy函数

    2024-03-28 11:40:03       59 阅读
  8. C语言中关于函数和数组理解

    2024-03-28 11:40:03       65 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-28 11:40:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-28 11:40:03       106 阅读
  3. 在Django里面运行非项目文件

    2024-03-28 11:40:03       87 阅读
  4. Python语言-面向对象

    2024-03-28 11:40:03       96 阅读

热门阅读

  1. linux进程切换

    2024-03-28 11:40:03       45 阅读
  2. 【C语言】RC4 测试代码

    2024-03-28 11:40:03       46 阅读
  3. el-upload上传文件前端自己读取excel

    2024-03-28 11:40:03       38 阅读
  4. uniapp H5 开发,公众号时请求跨域了,要用proxy

    2024-03-28 11:40:03       44 阅读
  5. Nginx服务

    2024-03-28 11:40:03       43 阅读
  6. Docker Compose 中的网络配置和优先级管理

    2024-03-28 11:40:03       45 阅读
  7. 无感刷新token

    2024-03-28 11:40:03       46 阅读
  8. CSS选择器 个人练习笔记

    2024-03-28 11:40:03       43 阅读