From demo to engine (5)-design a material system Part2

Source: Internet
Author: User

From demo to engine (5) -- Design a Material SystemPart2

For personal use only, do not reprint, do not use for any commercial purposes

Author: Clayman

 

PreviousArticleThe basic material system interface is introduced. This article will continue to discuss some details.

 

Redundancy Filtering

Reducing state changes is one of the most important ways to improve the performance of any real-time rendering system. Some people think that if each parameter update performs a redundancy check, it will sacrifice performance. Others think that the driver will perform a redundancy check, so you don't have to do this. Is there any need for redundant filtering? The answer is yes. First, the redundancy check performed by the driver is limited. Second, if we can find the redundancy settings at the system's top level, why should we always pass them to the bottom layer for filtering? Early checks can reduceDXFunction call to reduceDXFunction-to-driver calls, and the price is often only one condition judgment statement. Why not?

 

Redundancy filtering can be performed at different levels, Render queue sort Is the highest level of redundancy filtering, as shown at the end of the previous ArticleCode, We saw Per Material Level filter, further, you can also do Per parameter group Or Per Parameter Level filter. Parameter Group What is the concept? It is actually a series of parameters as a set. DX 10 , Render State object Yes Per group The best example of filtering. DX9 Although no State object But write a similar Wrapper It is not difficult, X na 4.0 That's it! As Per Parameter Whether redundant checks are required based on the parameter type. For example Float It is difficult to judge redundancy for type variables:

If (value! = Cachedfloatvale)

Updateparam ();

 

As we all know, due to the inaccuracy of floating point numbers, such judgments usually fail to achieve the expected results. EspeciallyMatrixThis type of value that contains multiple elements and is easy to change is obviously not worth the candle for redundant filtering. However, similar filtering is suitable for textures. The purpose of redundancy detection is not100%Filter all repeated settings to avoid most repeated operations.

 

To implement redundant filtering, you need to save the current status of each variable in the internal system, including Material, shader program, State Block , Texture And so on. To ensure State persistence, you must allow the High-Level System to modify these parameters only from one place. Imagine if both of them are exposed Device And Effect , Both of which change the device status, so the status tracing will become very troublesome. Let's talk about it here. DX9 Lower Effect The third disadvantage: unable to obtain Effect All stored data, such Sampler state , Render state . In . FX Directly set in the file Render state It looks very convenient, but it is very unfriendly to the engine and we cannot know Effect The status and values changed are equivalent to the loss of control over the rendering system. . FX An incorrect state variable in the file may damage the rendering effect of the entire scenario, and the error cannot be found at the engine level! DX9 The only solution to this problem is Effect State Manager . However Effect It's slow enough. add another one. Manager Layer Not even slower. Besides, since all implementations Effectstatemanager The courage, why not abandon it further Effect , Direct management Shader After all, write 2 The workload is almost the same.

 

Re-invent the wheel

To avoid Effect The fundamental solution is to write custom Effect/meta-Material . This step is not as complicated as most people think. All you have to do is load / Create Shader And manage parameters. Create Shader Set the original Compileeffect Changed Compileshader Make a slight modification. Note, Meta-Material Still need to have a similar Technique , Pass . DX SDK Li Hlslwithoutfx Introduction passed Constanttable Management parameters are the easiest way to implement them, and there is almost no need to write too much additional code. If you are crazy enough, you can connect Constanttable No, parse Constanttable The Register number and type corresponding to each variable in, directly Setshaderconstant . I will not discuss how to resolve it here. Constant table But you must note that Constanttable Or Setshaderconstant , VS/PS The corresponding variables are independent: for example, in . FX The file declares global variables. VaR And VS And PS If this variable is used VS/PS Put them in 2 In different registers.

 

Possible Optimization Solutions

delayed update: all parameters, status update delay to dp before calling, similar to effect commitchange mechanism, material. apply other operations do not set the update status immediately, but Mark and record the operations to be performed, dp before, the rendering system checks whether operations need to be performed and then executes them.

Parameter packaging: Package the parameters to be updated into one or moreFloat [], UseSetshaderconstate (float [])Update multiple parameters at a time. This updateShaderInStrutType variables are particularly useful, suchLight, You can write similarSetshaderconstant (lightsemantic, light. convernttofloatarray).

Parameter register binding: Material / Rendering System Optimization relies heavily on specific projects and programming. Shader . For engines, it is difficult to require users to write certain rules Shader But it is feasible for a specific project. Parameter register binding refers to all Shader Bind the same parameters to the same register, for example Color map-s0, normap map-register (S1), World matrix-register (C0) And so on. This not only reduces redundancy settings, but also simplifies Setshaderparam The Code logic. In addition, try not to use Effectpool , Effectblock Such things, MS Design the entire Effect Although a lot of good concepts are introduced in the framework, it is a pity that the specific implementation is not very good, often outweighs the loss.

 

At a higher level, the material system is part of the entire rendering system, which is incomplete due to the discussion of the rendering system. Rely only Material The data in is not enough to render objects, World , View , Projection Matrix , Atomization, lighting parameters are also essential. The data is composed Irenderable Or other part of the rendering system, but all must pass Material Interface. In addition, the rendering system can save some " Global Material" . An object is not limited to one Material , In Renderer To achieve some global effects, you can use Renderer Replace the material defined in Material For example Z-pre pass :

Renderer. enablezprepass =   True ;
................
If (Enablezprepass)
Render all objects with Z - Pre pass Material
Else
Render all objects with normal material

 

 

The advantage of this design is that when you modify the global effect, you only need to make the minimum changes, instead of modifying allMaterial. In the comment in the previous article, someone mentionedDeferred RenderingIs also a similar idea. RegardlessForwardOrDeferred ShadingThe material system does not need to be changed.ShaderThe Code supports the corresponding rendering method and can be adjusted in the Renderer, for example:

Deferredshading

Meta - Material {technique01: Normal, technique02, deferred, other technique .......};
Meta - Material. settechnique (currentglobalshadingprofile );
.....................
Renderer:
If (Deferredshading)
{
Render all objects with per Object 'S normal material (DS technique) // Geometry phase
Perform Screen - Space lighting with Renderer specific material
Perform composition with Renderer specific material
Post - Process with Renderer specific material
}
Else
........

 

 

That's all!The most important design concept for the material system is to abstract a unified mechanism that can manage arbitraryShaderTo process any similar parameters and ensure performance. These two articles are incomplete. They only introduce the basic ideas and rough implementation methods,Multi-Material,Multi-passAnd other concepts are not discussed. I hope it will be helpful to those who are designing the engine. You are also welcome to correct your shortcomings.


PS: No surprise, even <game engine architecture> this week should have arrived (bless the uncle of the postman and do not read the wrong e-address). I will refer to the content here, continue this series of articles :)

 

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.