Unity's Shader

Source: Internet
Author: User

1. Rendering pipeline

Task: Create (or Render) a two-dimensional image from a three-dimensional scene. That is, the computer needs to start from a series of fixed-point data, textures and other information, the final conversion of this information to a human eye can see the image. This work is usually done by both the CPU and the GPU.

Three stages:

(1) Application phase (CPU): 1. Prepare the scene data. 2. Coarse-grained culling of the work. 3 sets the rendering state of the model, outputting the geometry information required for rendering.

The application phase can be broadly divided into three stages:

1. Load the data into the video memory.

2. Set the render state. Render state: Defines how the mesh in the scene is rendered. For example, use that vertex shader/slice shader.

3. Call Drawcall. Drawcall is a command that initiates the CPU and the receiver is the GPU.

(2) Geometric phase: the fixed-point coordinates are transformed into the screen space, and then the grating is processed. After multi-processing of the input render drawing source, the two-dimensional vertex coordinates of the screen space, the depth value corresponding to each point, and the coloring and other related information will be output.

(3) Rasterization phase: produces the pixel on the screen and renders the final image. The rasterization of any of the pixels in the source of each rendered graph should be drawn again on the screen. Interpolation of the point-in-time data obtained at the previous stage, and then pixel-wise processing.

To avoid the elements we see that are being rasterized, the GPU uses a double buffering strategy (front buffer, back buffer). When the scene is rendered into a post buffer, the GPU swaps the contents of the front and back buffers, and the pre-buffered events appear on the screen before the image.

(1) Vertex shader: The processing unit is the vertex, and the input point shader is called once for each point that comes in. The vertex shader itself cannot create or destroy any vertices, and can not get the relationship between vertices and vertex. The work done mainly consists of: coordinate transformation (converting vertex coordinates from model space to homogeneous cropping space such as Mul (unity_mvp,v.postion)) and vertex-by-pixel illumination.

(2) Cutting: An entity and the camera business relationship has three kinds: completely in the field of vision, part in the field of vision, completely outside the field of view.

(3) Screen map: Converts the x, Y coordinates of each entity to the screen coordinate system.

(4) Triangle setting: Calculates the information needed to rasterize a triangular mesh. That is: to get each vertex of the triangle, we need to get the entire triangle mesh coverage of the pixels.

(5) Triangle traversal: Check if each pixel is covered by a triangular network. Based on the calculation of the previous phase, a triangular mesh is used to override those pixels, and the pixel of the entire coverage area is interpolated using the vertex information of the 3 vertices of the triangular mesh.

(6) Chip shader: The main technique used is texture sampling. In the vertex shader stage, the texture coordinates of each vertex are output, then the texture coordinates of the 3 vertices of the triangular mesh are interpolated by the rasterization stage, and the texture coordinates of the slices are obtained.

(7) per-slice operation: Several main tasks:

1. Determine the visibility of each slice. Involves a lot of testing work, such as: deep testing, template testing.

2. When an element passes all tests, it is necessary to merge (blend) The color value of the slice with the color already stored in the color buffer.

Turn on blending: The GPU takes out the resource and target colors and blends the two colors.

Turn off blending: The color in the color buffer is overwritten directly with the color of the slice.

For opaque objects, even if the blending operation is turned off, the color values computed by the slice shader can directly override the pixel values in the color buffer. But for translucent objects, you need to use a blending operation to make the object appear wet and transparent.

2.Draw Call

(1) How do CPUs and GPUs work in parallel?

Use a command buffer. The command buffer contains a command queue from which the CPU adds commands, and the GPU reads the commands, and the read and add commands are independent of each other.

(2) Why does the draw call affect the frame rate?

Each time a draw call is called, the CPU needs to send a lot of content to the GPU, including data, status, and commands.

(3) How to reduce draw call?

Batch processing method. Combine many small draw call into a large draw call.

In the game development process, in order to reduce the cost of draw call, you need to pay attention to:

(1) Avoid the use of a large number of small grids, when it is unavoidable to use a very small grid, you can consider whether you can merge.

(2) Avoid using too many materials. Try to share the same material between different networks. 、

3.Shader

1. Use the process:

(1) Create a material.

(2) Create a shader and assign to (1) the created material.

(3) Assign the material to the object to be rendered

(4) Adjust the properties of the shader in the material panel.

2. Infrastructure

Shader"Custom/myshader"{Properties {_int ("Int", Int) =2_float ("Float", Float) =1.5_range ("Range", Range (0.0,5.0)) =3.0_color ("Color", Color) = (1,1,1,1) _vector ("Vector", Vector) = (2,3,5,6) _2d ("2D", 2D) =""{} _cube ("Cube", Cube) =" White"{} _3d ("3D", 3D) ="Black"{}} subshader {//The real shader code will appear here.//Surface shader/Vertex/element/fixed function shader        ///options available        //labelTags {"Rendertype"="Opaque" }        //Status[Rendersetup] pass{[Name] [Tags] [Rendersetup]}//Other pass} Fallback"name"}

Tag Tag Properties

53

Unity's Shader

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.