Chapter 1 of "Introduction to Unity Shader Essentials"

Table of contents

1. Classification of books and key points of the whole book (simplified as much as possible)

  1. Book classification

  2. Key points of the book

2. Chapter 2 Rendering Pipeline

  1. The focus of this chapter (simplify as much as possible)

  2. Chapter Contact

    2.1 one of two

    2.2 Two of three

    2.3 Two out of four

3. Detailed explanation of this chapter

  1 review

    1.1 Application stage

    1.2 Geometry stage

    1.3 Rasterization stage

  2 Communication between CPU and GPU

  3 GPU pipelines

Four, the problem

  1. What are OpenGL and DirectX?

  2. What are HLSL, GLSL, CG?

  3. What is a Draw Call, and why does too many Draw Calls affect the frame rate?

  4. How to reduce Draw Call?

5. Mind map


1. Classification of books and key points of the whole book (simplified as much as possible)

  1. Book classification

        Practical text, learn the basics of Unity Shader.

  2. Key points of the book

        Starting from the basics, gradually understand and master how to write Unity Shader. And establish a basic understanding of the rendering process, learn some rendering mechanisms in Unity and how to use Unity Shader to achieve various custom rendering effects.

2. Chapter 2 Rendering Pipeline

  1. The focus of this chapter (simplify as much as possible)

        The second chapter teaches how the GPU renders the pipeline.

  2. Chapter Contact

    2.1 one of two

        The first chapter is to chat and introduce the structure of the book.

    2.2 Two of three

        The third chapter talks about the realization principle and basic grammar of Unity Shader.

    2.3 Two out of four

        The fourth chapter talks about the mathematical foundation required for student status Shader.

3. Detailed explanation of this chapter

  1 review

        Shader is a shader, which is very closely related to the rendering pipeline .

        The assembly line is to divide the requirements into steps, and then operate at intervals of one or more steps, so that each step is spaced apart, and multiple steps are performed at the same time at each time, which will greatly increase the speed.

The purpose         of the rendering pipeline is to start from a 3D scene and generate (render) a 2D image to the screen .

        The rendering process is divided into three stages: application stage - geometry stage - rasterization stage .

    1.1 Application stage

        The CPU is responsible for the main task: prepare the scene data , such as the camera position, which models the scene contains, etc.; coarse-grained culling (culling) work, remove invisible objects; set the rendering state of each model, such as the texture used , material, etc.; the output is the rendering primitive, the geometric information required for rendering.

    1.2 Geometry stage

        Running on the GPU , the main task: perform vertex-by-vertex and polygon-by-polygon operations on each rendering primitive; transform the vertex coordinates to the screen space ; the output is the vertex coordinates and depth values ​​in the screen space.

    1.3 Rasterization stage

        Running on the GPU , the main task: generate the image on the screen from the data passed in the previous stage, and render the final image .

  2 Communication between CPU and GPU

        The starting point of the rendering pipeline is the CPU, which is the application stage. It is divided into three stages: loading data into video memory; setting rendering state; calling Draw Call.

        Load data into video memory : All data required for rendering needs to be loaded from the hard disk into system memory, and then data such as grids and textures are loaded into the storage space on the graphics card - video memory (feels like the Cache of the graphics card).

        Set the rendering state : the rendering state is to define how the mesh in the scene is rendered. For example, vertex shader (Vertex Shader)/fragment shader (Fragment Shader), light source attributes, materials, etc.

        Call Draw Call : The CPU initiates to the GPU to receive, the command points to the list of primitives that need to be rendered, and then lets the GPU render these primitives.

  3 GPU pipelines

rendering pipeline

        Vertex shader: per-vertex lighting and coordinate transformation (conversion to homogeneous clipping coordinate system). Output the subsequent required data.

        Cropping: What is not in the camera's field of view does not need to be processed.

        Screen mapping: Convert the x and y coordinates of each primitive to the screen coordinate system. It is to transform the point of the 1*1 coordinate system into the point of 1920*1080 on your screen.

        Triangle settings: calculate which pixels are covered by each primitive, and calculate its color.

        Triangle traversal: Check whether each pixel is covered by a triangle (covered is a fragment). Find these fragments. And the coverage area is interpolated (depth) using these three vertex information. Output a sequence of fragments with data.

triangle traversal

        Fragment shader: Output the color of the fragment according to the interpolation result of the previous stage.

Fragment shader 

        Fragment-by-fragment operation: determine the visibility of each fragment (depth, stencil test, etc.), and merge it with the color already stored in the color buffer after passing the test (the screen does not suddenly change color, there will be a buffer, It is color merging; the buffer is that the computer calculates the next frame of the display in the buffer in advance, and can directly replace the screen image).

Four, the problem

  1. What are OpenGL and DirectX?

        OpenGL and DirectX are image programming interfaces , which are a layer of abstraction for hardware such as various registers and video memory . It is a bridge that allows us to program and allow computers to understand and execute.

The relationship between CPU, OpenGL/DirectX, graphics driver and GPU

  2. What are HLSL, GLSL, CG?

        shader language . HLSL is DirectX, GLSL is OpenGL, NVIDIA is CG. These languages ​​​​will be compiled into an intermediate language IL , and then handed over to the graphics card driver for translation into machine language (somewhat similar to CLR).

  3. What is a Draw Call, and why does too many Draw Calls affect the frame rate?

        Draw Call means that the CPU calls the graphics programming interface and calls the graphics card to work. The command buffer is built from the CPU to the GPU. In order for the GPU to do the next job after rendering (you ask him to render other jobs before finishing the job, and there will be some screens that cannot be rendered), the CPU adds commands, and the GPU reads Take, the two do not conflict.

command buffer 

        The rendering speed of the GPU is generally greater than the speed of the CPU submitting commands. If you submit DrawCall one by one, the GPU will render it, and then sleep and wait for you to continue submitting. In order not to let the GPU sleep, let the CPU submit the batch-processed Draw Call once (similar to copying a hundred 1kb files is slower than copying a 100kb file).

  4. How to reduce Draw Call?

        Batch processing of Draw Call. Merge many small Draw Calls into one big Draw Call. But merging also consumes performance, so try to batch process static objects to reduce the number of merging, and dynamic batch processing requires re-merging each time. (Similar backpack system will use batch draw call)

5. Mind map

Guess you like

Origin blog.csdn.net/weixin_51374560/article/details/128792397