Constant Buffers

https://catlikecoding.com/unity/tutorials/scriptable-render-pipeline/custom-shaders/
taken from the above URL.

Constant Buffer
Unity does not the Provide US with A Projection Model-View-the Matrix, the Matrix A Way Because that multiplication of matrices at The M and VP CAN BE avoided. This sentence, it very funny. MVP says unity is not to provide a matrix. It is not multiplied by the matrix M and the VP.
But I checked the standard built-in library:

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

With a camera, VP matrix is constant, can be used repeatedly.
unity's shaders takes advantage of that fact adn put the matrices in different constant buffers. unity these same matrices, placed in a constant buffer.
although but WE DEFINE Them AS variable, Their Data Remains Constant During The Drawing of A SINGLE Shape, and Often field longer Within last that.
The VP Matric the gets PUT in A per-Frame Buffer, the while The M Matrix the gets PUT in A per-Draw Buffer.
here's per-frame buffer-- each frame buffer
per-draw buffer-- each drawing buffer
which size is smaller, each frame can be painted many times, you can understand.

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.

Saying we should not force the shader variables constant buffer in place, but lately all the data in the same buffer, so that efficiency will be higher.

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.
For higher efficiency, we will take advantage of the constant buffer, the matrix VP UnityPerFrame buffer placed in the matrix M on the 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;
}

Compatibility:
Because Constant buffers do not Benefit All Platforms, unity's shaders RELY ON Macros Them to only use the when needed.
Not all platforms support constant buffer, use a macro to do so unity compatible.

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

Of course, this is a macro to perform file contains:

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

How to introduce this document:
Reference this passage:

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.

This in: \ Program Files \ Unity2018.3.0f2 \ Unity \ Editor \ Data \ Resources \ PackageManager \ Editor \ package \ ShaderLibrary \ API \ D3D11.hlsl: D
is similarly defined in this way are:

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

This corresponds exactly to them myself. . .
Right here! ! !

Published 646 original articles · won praise 107 · views 360 000 +

Guess you like

Origin blog.csdn.net/wodownload2/article/details/105112782