# Constant Buffers

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. 这句话，说得很滑稽。说unity没给提供MVP矩阵。所以也就是没有M和矩阵VP的相乘。

#define UNITY_MATRIX_MVP    unity_MatrixMVP
#define UNITY_MATRIX_MV     unity_MatrixMV
#define UNITY_MATRIX_T_MV   unity_MatrixTMV
#define UNITY_MATRIX_IT_MV  unity_MatrixITMV


besides that, the VP matric can be reused for everything that gets drawn with the same camera during a frame.

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-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.

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.

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.

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.


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


©️2019 CSDN 皮肤主题: 酷酷鲨 设计师: CSDN官方博客