函数 GetMemoryType 的理解

news/2024/7/24 3:11:33 标签: 图形渲染

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

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


http://www.niftyadmin.cn/n/5454187.html

相关文章

一、CentOS基础命令(2.系统与用户操作)

文章目录 2、用户管理&#xff08;1.&#xff09;useradd - 创建新用户&#xff08;2.&#xff09;userdel - 删除用户&#xff08;3.&#xff09;usermod - 修改用户属性&#xff08;4.&#xff09;passwd - 管理用户密码&#xff08;5.&#xff09;groupadd - 创建用户组&…

AABB包围盒高质量阴影

摄像机代码 using System.Collections; using System.Collections.Generic; using Unity.VisualScripting; using UnityEngine; using UnityEngine.Rendering;public class UpdateAABBScript : MonoBehaviour {public LayerMask layerMask = ~0;private Bounds bounds = new Bo…

【前端】-

相对路径和绝对路径是描述文件位置的两种方式。 1. 相对路径&#xff1a;相对于自己的目标文件的位置&#xff0c;以引用文件之间网页所在位置为参考基础&#xff0c;而建立出的目录路径。因此&#xff0c;当保存于不同目录的网页引用同一个文件时&#xff0c;所使用的路径将不…

设置mysql 数据库和表 的编码方式UTF-8

要设置 MySQL 数据库表和字段的编码方式为 UTF-8&#xff0c;可以使用下面的SQL语句&#xff1a; 1. 设置数据库默认编码为 UTF-8&#xff1a; ALTER DATABASE your_database_name CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;2. 创建表时指定编码为 UTF-8&#xff1a…

配置文件 application properties

配置文件 application properties 1 参数交由配置文件集中管理 Value(“${}”)用于外部配置的属性注入 在之前编写的程序中进行文件上传时&#xff0c;需要调用AliOSSUtils工具类&#xff0c;将文件上传到阿里云OSS对象存储服务当中。而在调用工具类进行文件上传时&#xff0c…

QGIS编译(跨平台编译)057:FastCGI111编译(Windows、Linux、MacOS环境下编译)

文章目录 1、FastCGI介绍2、FastCGI下载3、Windows下编译4、linux下编译5、MacOS下编译1、FastCGI介绍 2、FastCGI下载 PDAL官网:PDAL 获取 PDAL-2.6.3-src.tar.bz2 文件。 3、Windows下编译 解压缩 PDAL-2.6.3-src.tar.bz2 文件,进入PDAL-2.6.3-src目录。 4、linux下编译 …

爬虫基础训练题

1.抓取imooc网站实战课程部分的课程名称&#xff08;所有课程大概7页&#xff0c;抓取1到5页&#xff09;&#xff0c;并把所有课程名称存储为txt文件第一页地址 2.设置一个请求头&#xff08;headers&#xff09;&#xff0c;这是一个字典&#xff0c;用于在HTTP请求中设置请…

编译与链接

环境&#xff1a; 在ASCI C的任何一种实现中&#xff0c;存在两种不同的环境&#xff1a; 1.翻译环境&#xff0c;在这个环境中&#xff0c;源代码被转换为可执行的机器指令&#xff08;二进制指令&#xff09; 2.运行环境&#xff0c;用于实际执行代码。 流程:c文件->翻译…