当前位置: 首页 > news >正文

Constant Buffers

https://catlikecoding.com/unity/tutorials/scriptable-render-pipeline/custom-shaders/

https://docs.microsoft.com/zh-cn/windows/win32/direct3dhlsl/dx-graphics-hlsl-constants?redirectedfrom=MSDN

https://zhuanlan.zhihu.com/p/35830868

摘自上面的网址。

常量buffer
unity does not provide us with a model-view-projection matrix, because that way a matrix multiplication of the M and VP matrices can be avoided.
besides that, the VP matric can be reused for everything that gets drawn with the same camera during a frame.

同一个相机的,VP矩阵是不变的,可反复使用的。
unity’s shaders takes advantage of that fact adn put the matrices in different constant buffers. unity把这些不变的矩阵,放在一个常量buffer中。
although we define them as variable, their data remains constant during the drawing of a single shape, and often longer than that.
the VP matric gets put in a per-frame buffer, while the M matrix gets put in a per-draw buffer.
这里的per-frame buffer——每帧buffer
per-draw buffer——每次绘制buffer
后者粒度更小,每帧可以画多次,你可以这么理解。

while it is not strictly required to put shader variables in constant buffers, doing so makes it possible for all data in the same buffer to be changed more efficiently. at least, that is the case when it is supported by the graphics API. opengl does not.

没有强制说要把shader的变量放在常量buffer中,但是近来将所有的数据放在同一个buffer中,这样效率会更高。

to be as efficient as possible, we will also make use of constant buffers. unity puts the VP matrix in a UnityPerFrame buffer and the M matrix in a UnityPerDraw buffer.
为了效率的更高,我们将充分利用常量buffer,将VP矩阵放在UnityPerFrame buffer中,而M矩阵放在UnityPerDraw buffer中。

a constant buffer is defined like a struct, except with the cbuffer keyword and the variables remain accessible as before.
写法如下:

cbuffer UnityPerFrame 
{
	float4x4 unity_MatrixVP;
};

cbuffer UnityPerDraw 
{
	float4x4 unity_ObjectToWorld;
}

兼容性:
because constant buffers do not benefit all platforms, unity’s shaders rely on macros to only use them when needed.
并不是所有的平台都支持常量buffer,所以unity使用宏来做兼容。

the CBUFFER_START macro with a name parameter is used instead of directly writing cbuffer and accompanying CBUFFER_END macro replaces the end of the buffer.
写法如下:

CBUFFER_START(UnityPerFrame)
	float4x4 unity_MatrixVP;
CBUFFER_END

CBUFFER_START(UnityPerDraw)
	float4x4 unity_ObjectToWorld;
CBUFFER_END

当然,是这个宏,要进行文件的包含:

#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"

如何引入这个文件:
参考这段话:

we'll make use of Unity's core library for render pipelines. It can be added to our project via the package manager window. Switch to the All Packages list and enable Show preview packages under Advanced, then select Render-pipelines.core, and install it. I'm using version 4.6.0-preview, the highest version that works in Unity 2018.3.

这个在:D:\Program Files\Unity2018.3.0f2\Unity\Editor\Data\Resources\PackageManager\Editor\package\ShaderLibrary\API\D3D11.hlsl
中有这样类似的定义:

#define CBUFFER_START(name) cbuffer name {
#define CBUFFER_END };

这样就完全对应起来喽。。。

在这里插入图片描述

cbuffer myObject
{       
    float4x4 matWorld;
    float3   vObjectPosition;
    int      arrayIndex;
}
 
cbuffer myScene
{
    float3   vSunPosition;
    float4x4 matView;
}

相关文章:

  • P4013 数字梯形问题 最小费用最大流
  • 分析GlobalIllumination函数的实现
  • UVa 10474 Where is the Marble?
  • 光照贴图的中的编码格式
  • macOS U盘制作启动系统
  • 再谈gamma校正——重要知识点
  • 微信小程序小结
  • RenderDoc截取unity帧,分析shader
  • pc和android平台下lightmap的质量选取导致的宏变化
  • mac 笔记本编辑文本命令
  • SHADOWS_SCREEN宏打开的时机
  • swagger 如何在UI界面加入Authentication token值
  • unity屏幕空间阴影
  • UNITY_NO_SCREENSPACE_SHADOWS打开时机
  • mixed模式下烘焙shadowmask记录的数据
  • 【347天】每日项目总结系列085(2018.01.18)
  • 【每日笔记】【Go学习笔记】2019-01-10 codis proxy处理流程
  • bearychat的java client
  • docker容器内的网络抓包
  • E-HPC支持多队列管理和自动伸缩
  • Just for fun——迅速写完快速排序
  • Laravel Mix运行时关于es2015报错解决方案
  • passportjs 源码分析
  • PAT A1050
  • React+TypeScript入门
  • Vue2.x学习三:事件处理生命周期钩子
  • 搞机器学习要哪些技能
  • 前端知识点整理(待续)
  • 王永庆:技术创新改变教育未来
  • 400多位云计算专家和开发者,加入了同一个组织 ...
  • ​无人机石油管道巡检方案新亮点:灵活准确又高效
  • $.type 怎么精确判断对象类型的 --(源码学习2)
  • (22)C#传智:复习,多态虚方法抽象类接口,静态类,String与StringBuilder,集合泛型List与Dictionary,文件类,结构与类的区别
  • (Mac上)使用Python进行matplotlib 画图时,中文显示不出来
  • (附源码)ssm基于jsp高校选课系统 毕业设计 291627
  • (附源码)小程序儿童艺术培训机构教育管理小程序 毕业设计 201740
  • (一)基于IDEA的JAVA基础12
  • .net core 6 集成和使用 mongodb
  • .net web项目 调用webService
  • .NET 将混合了多个不同平台(Windows Mac Linux)的文件 目录的路径格式化成同一个平台下的路径
  • .NET设计模式(2):单件模式(Singleton Pattern)
  • .NET实现之(自动更新)
  • :“Failed to access IIS metabase”解决方法
  • @ResponseBody
  • @staticmethod和@classmethod的作用与区别
  • @TableId注解详细介绍 mybaits 实体类主键注解
  • @Transactional 竟也能解决分布式事务?
  • [⑧ADRV902x]: Digital Pre-Distortion (DPD)学习笔记
  • [AHOI2009]中国象棋 DP,递推,组合数
  • [AIGC] 使用Curl进行网络请求的常见用法
  • [BUUCTF NewStarCTF 2023 公开赛道] week3 crypto/pwn
  • [Bzoj4722]由乃(线段树好题)(倍增处理模数小快速幂)
  • [C++]STL之map
  • [CentOs7]图形界面
  • [codevs 1296] 营业额统计