Optimize shader constant update in xNa 4.0 (2)

Source: Internet
Author: User

 Optimize shader constant update in xNa 4.0 (2)

Author: Clayman

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

 

PreviousArticleIntroduced inDX9Use arrays to simulateConstant BufferBut not completely. In terms of performance improvement, it completely loses ease of use:HLSLAfter declaring the variable as an array, applyProgramThe client loses information about each independent variable, for example:

HLSL

Float4 globalparam [ 8 ]: Register (C0 );
Static Float4x4 View = Float4x4 (globalparam [ 0 ], Globalparam [ 1 ], Globalparam [ 2 ], Globalparam [ 3 ]);
Static Float4x4 viewprojection = Float4x4 (globalparam [ 4 ], Globalparam [ 5 ], Globalparam [ 6 ], Globalparam [ 7 ]);

Viewproj, worldviewproj It is transparent to applications and we do not know Perframeconstants Which element corresponds to which variable. One solution is to make a Convention during encoding so that a variable is located at a specific position in a specific array. Obviously, unless it is a very simple application, this convention method is completely non-flexible and scalable. A better way is to use an extra external file. ( For example XML) Description Shader Variable definition in. Although this method is flexible, additional files mean additional work and need to be ensured Shader Synchronization with the description file also increases the possibility of errors. Is there Shader Add HLSL CodeWhat about Adding metadata? Of course. Annotation !

 

UnfamiliarAnnotationHere is a brief introduction to children's shoes,AnnotationAndSemanticSimilarHLSLVariable or function definition, which belongsMS effect frameworkSo it does not affect the generatedShaderCode.AnnotationInclude in a pair of angle brackets, for example:

Float4 hlslvariable: semantic <annotationdatatype annotationname = value>;

 

Anotation Always take Annotationdatatype annotationname = Value The syntax is the same as that defined by common variables, Anotationdatatype Yes Float, Int, bool, float4, string Wait, <> Can contain multiple Annotation Definition, separated by semicolons. In the application, you can Effect To access each Annotaion . Effectparameter Almost the same.

 

WithAnnotaionYou can define an array as follows:

Float4 globalparam [ 8 ]: Register (C0)
<  
String Param0 = "View 0   4 "
String Param1 = "Viewprojection 4   4 "
>  

 

Here,Param *IndicatesHLSLInStaticVariable. The first element in the string indicates the variable name, the second element indicates the position of the variable in the array, and the third element indicates the variable size, in the unit of the number of registers. This is just a simple example. You can expandAnntation.

 

return to the application end, we hope that you do not need to know HLSL material parameter binding can be implemented when a variable is defined. setshaderconstant (name, value) all tasks are completed. setshaderconstant jobs are divided into 2 steps :

1. use name Find the corresponding effectparameter (EP) . Note the EP NO Effect EP , but by annotaion parsed parameters.

2. AccordingEPToValueSaveBuffer.BufferCorrespondingHLSL.

 

After all material parameters are updatedEffectMedium OfEP, Set the entireBufferSubmitShader.

The above describes the basic material Binding design. The following are some possible optimizations:

 

Optimization1:

Sometimes we don't want all HLSL All variables start Constant Buffer Such Bonematrix In this way, a large number Matrix To Bonematrix Save Buffer , And then from Buffer To Shader , May cause a large number of data copies, which may be slower than direct submission. Therefore, I allow you to choose whether to use Constant Buffer . You only need to slightly modify the code:

Float4 bonematrix [ 50 ]
<  
Bool Isconstantbuffer =   False ;
>  

 

In addition, for our own definition EP Add the same variable. When Setshaderconstant Find EP If you find EP No Constantbuffer , Then directly submit Shader Let's EP Express Constant Buffer A variable in can also represent Effect In EP Variable, or directly Shader Register value.

 

Optimization2:

For some common parameters, suchWorld,ViewFind the correspondingEPIt is relatively slow and can be converted to these parameters for optimization:

Class Shaderconstattable
{
Setconstantvalue (name, value)
Setworldmatrix (matrix );
Setworldview (matrix );
}

 

SetworldmatrixCompleteEPIfShaderThe correspondingEP, Then do nothing. My engine defines common parameters. If you think that an undefined parameter is frequently used, you can inherit it.ShaderconstattableAdd your own optimizations.

 

Optimization3:

For applications, each Shaderconstattable All have their own Constant Buffer Array, Shaderconstattable Shared Constant Buffer , In Shaderconstattable. prerender () Medium, Shaderconstattable From Pool Find Constant Buffer To save considerable memory.

 

Optimization4:

Use oneFlagTo determine a certain speedConstant BufferWhether update is required. One I used32Bit value, each corresponding to oneBuffer, BecauseBufferThe number should not be too large, so32Bit valueDx10/11It is enough.

 

Finally, this article is based onXNa/DX9ButDx10/11The system also has reference value

 

 

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.