Filament引擎分析--command抽象设备API

2023-12-07 04:15

本文主要是介绍Filament引擎分析--command抽象设备API,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1. 前言

Filament中使用了FrameGraph来管理渲染管线,需要准备两点:

  • 设备接口抽象:设备API抽象为Command
  • 资源抽象:使用虚拟资源,在实际用到时再创建,方便剔除无用资源

下面就围绕Filament中设备API抽象为Command代码部分做一个解读:

2. 代码分析

先贴一段创建顶点缓冲的接口调用堆栈:

[Inlined] filament::backend::CommandBase::CommandBase(void (*)(filament::backend::Driver &, filament::backend::CommandBase *, int *)) CommandStream.h:63
[Inlined] filament::backend::CommandType<void (filament::backend::Driver::*)(filament::backend::Handle<filament::backend::HwVertexBuffer>, unsigned char, unsigned char, unsigned int, std::__ndk1::array<filament::backend::Attribute, 16u>)>::Command<&filament::backend::Driver::createVertexBufferR(filament::backend::Handle<filament::backend::HwVertexBuffer>, unsigned char, unsigned char, unsigned int, std::__ndk1::array<filament::backend::Attribute, 16u>)>::Command<filament::backend::Handle<filament::backend::HwVertexBuffer>, unsigned char, unsigned char, unsigned int, std::__ndk1::array<filament::backend::Attribute, 16u>>(void (*)(filament::backend::Driver&, filament::backend::CommandBase*, int*), filament::backend::Handle<filament::backend::HwVertexBuffer>&&, unsigned char&&, unsigned char&&, unsigned int&&, std::__ndk1::array<filament::backend::Attribute, 16u>&&) CommandStream.h:154
[Inlined] filament::backend::CommandStream::createVertexBuffer(unsigned char, unsigned char, unsigned int, std::__ndk1::array<>) DriverAPI.inc:169
filament::FVertexBuffer::FVertexBuffer(filament::FEngine &, const filament::VertexBuffer::Builder &) VertexBuffer.cpp:185
[Inlined] utils::Arena::make<>(filament::FEngine &, const filament::VertexBuffer::Builder &) Allocator.h:647
[Inlined] filament::FEngine::create<>(filament::ResourceList<> &, const filament::FVertexBuffer::Builder &) Engine.cpp:680
filament::FEngine::createVertexBuffer(const filament::VertexBuffer::Builder &) Engine.cpp:690
filament::FEngine::init() Engine.cpp:277
filament::FEngine::create(filament::backend::Backend, filament::backend::Platform *, void *, const filament::Engine::Config *) Engine.cpp:110
[Inlined] FilamentTest::setupFilament() FilamentTest.cpp:98
FilamentTest::init() FilamentTest.cpp:68
boxing::xr::composer::StartBase::instance(ANativeWindow *, int, int) StartBase.h:263
[Inlined] native_OnDrawFrame::$_0::operator()() const JniImpl.cpp:100
[Inlined] std::__ndk1::__invoke<>(native_OnDrawFrame::$_0 &) type_traits:3874
[Inlined] std::__ndk1::__apply_functor<>(native_OnDrawFrame::$_0 &, std::__ndk1::tuple<> &, std::__ndk1::__tuple_indices<>, std::__ndk1::tuple<> &&) functional:2853
[Inlined] std::__ndk1::__bind::operator()<>() functional:2886
[Inlined] std::__ndk1::__invoke<>(std::__ndk1::__bind<> &) type_traits:3874
std::__ndk1::__packaged_task_func::operator()() future:1817
[Inlined] std::__ndk1::__packaged_task_function::operator()() const future:1994
std::__ndk1::packaged_task::operator()() future:2214
[Inlined] std::__ndk1::__function::__value_func::operator()() const functional:1884
[Inlined] std::__ndk1::function::operator()() const functional:2556
<lambda>::operator()() const ThreadPool.h:71
[Inlined] decltype(std::__ndk1::forward<boxing::core::ThreadPool::ThreadPool(unsigned int)::'lambda'()>(fp)()) std::__ndk1::__invoke<boxing::core::ThreadPool::ThreadPool(unsigned int)::'lambda'()>(boxing::core::ThreadPool::ThreadPool(unsigned int)::'lambda'()&&) type_traits:3874
[Inlined] std::__ndk1::__thread_execute<>(std::__ndk1::tuple<> &, std::__ndk1::__tuple_indices<>) thread:273
std::__ndk1::__thread_proxy<>(void *) thread:284
__pthread_start(void*) 0x00000000eab36828
__start_thread 0x00000000eaaed5ce

渲染设备API定义:

filament\filament\backend\include\private\backend\DriverAPI.inc

DriverAPI.inc中使用大量的宏替换操作,将设备接口进行封装,或打包,这部分代码可读性极差,不过可从其调用逻辑来进行拆解和理解:
先来分析其中一个接口: createVertexBuffer 创建一个顶点缓冲

DECL_DRIVER_API_R_N(backend::VertexBufferHandle, createVertexBuffer,uint8_t, bufferCount,uint8_t, attributeCount,uint32_t, vertexCount,backend::AttributeArray, attributes)

这里不是真的创建,而要看这个宏接口在哪里使用,我们主要看看这两个地方:

  CommandStream.h  //命令流Driver.h   //设备接口

这两个文件中都对DriverAPI.inc进行了include,但是意义完全不一样,先看DECL_DRIVER_API_R_N:

#define DECL_DRIVER_API_R_N(R, N, ...) \DECL_DRIVER_API_RETURN(R, N, PAIR_ARGS_N(ARG, ##__VA_ARGS__), PAIR_ARGS_N(PARAM, ##__VA_ARGS__))

关键在DECL_DRIVER_API_RETURN这个宏,在CommandStream.h和Driver.h头文件中include文件DriverAPI.inc 之前分别定义了自己的DECL_DRIVER_API_RETURN宏,看看CommandStream.h中:

#define DECL_DRIVER_API(methodName, paramsDecl, params)                                         \inline void methodName(paramsDecl) {                                                        \DEBUG_COMMAND_BEGIN(methodName, false, params);                                         \using Cmd = COMMAND_TYPE(methodName);                                                   \void* const p = allocateCommand(CommandBase::align(sizeof(Cmd)));                       \new(p) Cmd(mDispatcher.methodName##_, APPLY(std::move, params));                        \DEBUG_COMMAND_END(methodName, false);                                                   \}#define DECL_DRIVER_API_SYNCHRONOUS(RetType, methodName, paramsDecl, params)                    \inline RetType methodName(paramsDecl) {                                                     \DEBUG_COMMAND_BEGIN(methodName, true, params);                                          \AutoExecute callOnExit([=](){                                                           \DEBUG_COMMAND_END(methodName, true);                                                \});                                                                                     \return apply(&Driver::methodName, mDriver, std::forward_as_tuple(params));              \}#define DECL_DRIVER_API_RETURN(RetType, methodName, paramsDecl, params)                         \inline RetType methodName(paramsDecl) {                                                     \DEBUG_COMMAND_BEGIN(methodName, false, params);                                         \RetType result = mDriver.methodName##S();                                               \using Cmd = COMMAND_TYPE(methodName##R);                                                \void* const p = allocateCommand(CommandBase::align(sizeof(Cmd)));                       \new(p) Cmd(mDispatcher.methodName##_, RetType(result), APPLY(std::move, params));       \DEBUG_COMMAND_END(methodName, false);                                                   \return result;                                                                          \}

上面三个宏的作用基本是一样的,都将要调用的函数和参数封装为了Command,不同之处在于DECL_DRIVER_API是command无返回值的,DECL_DRIVER_API_SYNCHRONOUS是封装为command后同步执行的,DECL_DRIVER_API_RETURN是需要返回值的
主要看看DECL_DRIVER_API_RETURN:

RetType result = mDriver.methodName##S();    

将方法名后面拼接了S,调用拿到返回类型
看看拼接S后的实现:

Handle<HwVertexBuffer> OpenGLDriver::createVertexBufferS() noexcept {return initHandle<GLVertexBuffer>();
}

initHandle()这句在filament内存池HandleArena上创建了一个GLVertexBuffer对象,然后根据内存地址创建了对象的唯一handeID
再看下面这句:
using Cmd = COMMAND_TYPE(methodName##R);
方法名后面拼接了R,然后获取了command的类型,没有执行方法,看看拼接R后的实现:

void OpenGLDriver::createVertexBufferR(Handle<HwVertexBuffer> vbh,uint8_t bufferCount,uint8_t attributeCount,uint32_t elementCount,AttributeArray attributes) {DEBUG_MARKER()construct<GLVertexBuffer>(vbh, bufferCount, attributeCount, elementCount, attributes);
}

内存池HandleArena上创建了一个GLVertexBuffer对象
再看下面一句

void* const p = allocateCommand(CommandBase::align(sizeof(Cmd)));   
new(p) Cmd(mDispatcher.methodName##_, RetType(result), APPLY(std::move, params));   

在CommandStream内部的环形缓冲上申请了一块Command对象的内存p,然后在内存p上new了对象Command
看看CommandBase* execute执行函数的实现:

inline CommandBase* execute(Driver& driver) {// returning the next command by output parameter allows the compiler to perform the// tail-call optimization in the function called by mExecute, however that comes at// a cost here (writing and reading the stack at each iteration), in the end it's// probably better to pay the cost at just one location.intptr_t next;mExecute(driver, this, &next);return reinterpret_cast<CommandBase*>(reinterpret_cast<intptr_t>(this) + next);
}

mExecute就是上面new(p) Cmd(mDispatcher.methodName##_, RetType(result), APPLY(std::move, params)); 后的函数和参数的封装体,然后拿到了下一个圆形缓冲中下一个command的地址偏移量next,返回下一个command地址
CommandStream中执行command,执行完然后获取下一个执行。。。

mDriver.execute([this, buffer]() {Driver& UTILS_RESTRICT driver = mDriver;CommandBase* UTILS_RESTRICT base = static_cast<CommandBase*>(buffer);while (UTILS_LIKELY(base)) {base = base->execute(driver);}
});

这篇关于Filament引擎分析--command抽象设备API的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/464456

相关文章

LiteFlow轻量级工作流引擎使用示例详解

《LiteFlow轻量级工作流引擎使用示例详解》:本文主要介绍LiteFlow是一个灵活、简洁且轻量的工作流引擎,适合用于中小型项目和微服务架构中的流程编排,本文给大家介绍LiteFlow轻量级工... 目录1. LiteFlow 主要特点2. 工作流定义方式3. LiteFlow 流程示例4. LiteF

SpringBoot集成LiteFlow实现轻量级工作流引擎的详细过程

《SpringBoot集成LiteFlow实现轻量级工作流引擎的详细过程》LiteFlow是一款专注于逻辑驱动流程编排的轻量级框架,它以组件化方式快速构建和执行业务流程,有效解耦复杂业务逻辑,下面给大... 目录一、基础概念1.1 组件(Component)1.2 规则(Rule)1.3 上下文(Conte

MyBatis Plus 中 update_time 字段自动填充失效的原因分析及解决方案(最新整理)

《MyBatisPlus中update_time字段自动填充失效的原因分析及解决方案(最新整理)》在使用MyBatisPlus时,通常我们会在数据库表中设置create_time和update... 目录前言一、问题现象二、原因分析三、总结:常见原因与解决方法对照表四、推荐写法前言在使用 MyBATis

Python主动抛出异常的各种用法和场景分析

《Python主动抛出异常的各种用法和场景分析》在Python中,我们不仅可以捕获和处理异常,还可以主动抛出异常,也就是以类的方式自定义错误的类型和提示信息,这在编程中非常有用,下面我将详细解释主动抛... 目录一、为什么要主动抛出异常?二、基本语法:raise关键字基本示例三、raise的多种用法1. 抛

Python基于微信OCR引擎实现高效图片文字识别

《Python基于微信OCR引擎实现高效图片文字识别》这篇文章主要为大家详细介绍了一款基于微信OCR引擎的图片文字识别桌面应用开发全过程,可以实现从图片拖拽识别到文字提取,感兴趣的小伙伴可以跟随小编一... 目录一、项目概述1.1 开发背景1.2 技术选型1.3 核心优势二、功能详解2.1 核心功能模块2.

github打不开的问题分析及解决

《github打不开的问题分析及解决》:本文主要介绍github打不开的问题分析及解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、找到github.com域名解析的ip地址二、找到github.global.ssl.fastly.net网址解析的ip地址三

Mysql的主从同步/复制的原理分析

《Mysql的主从同步/复制的原理分析》:本文主要介绍Mysql的主从同步/复制的原理分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录为什么要主从同步?mysql主从同步架构有哪些?Mysql主从复制的原理/整体流程级联复制架构为什么好?Mysql主从复制注意

java -jar命令运行 jar包时运行外部依赖jar包的场景分析

《java-jar命令运行jar包时运行外部依赖jar包的场景分析》:本文主要介绍java-jar命令运行jar包时运行外部依赖jar包的场景分析,本文给大家介绍的非常详细,对大家的学习或工作... 目录Java -jar命令运行 jar包时如何运行外部依赖jar包场景:解决:方法一、启动参数添加: -Xb

Apache 高级配置实战之从连接保持到日志分析的完整指南

《Apache高级配置实战之从连接保持到日志分析的完整指南》本文带你从连接保持优化开始,一路走到访问控制和日志管理,最后用AWStats来分析网站数据,对Apache配置日志分析相关知识感兴趣的朋友... 目录Apache 高级配置实战:从连接保持到日志分析的完整指南前言 一、Apache 连接保持 - 性

MySQL 存储引擎 MyISAM详解(最新推荐)

《MySQL存储引擎MyISAM详解(最新推荐)》使用MyISAM存储引擎的表占用空间很小,但是由于使用表级锁定,所以限制了读/写操作的性能,通常用于中小型的Web应用和数据仓库配置中的只读或主要... 目录mysql 5.5 之前默认的存储引擎️‍一、MyISAM 存储引擎的特性️‍二、MyISAM 的主