Optimize shader constant update in xNa 4.0.

Source: Internet
Author: User

 Optimize shader constant update in xNa 4.0.

Author: Clayman

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

 

AlthoughX na 4.0DeletedSetshaderconstantAnd so on, so that we cannot update in the most efficient wayShaderParameters, but through excellent design, there is still a lot of room for optimization. We have two goals:

1.Reduce status changes---This is anyShader constant managementPrimary goals of the system;

2.DecreaseEffectparameter. setvalue.---UseReflectorWe can see that this is a very slow function;

 

How to implement it?DirectX 10It indicates the direction for us, that isConstant buffer (CB). AlthoughDX 9NotCBBut this does not prevent us from simulating a similar mechanism. All we need to do is to slightly change the writing.ShaderMethod:

Float4 perframeconstants [N]: Register (C0 );
Float4 materialconstant [m]: Register (N );
Float4 instanceconstant [v]: Register (n + M );

 

 

No more independent declarationsUniformVariables, but think of them as elements in different types of variable groups, which is almost the sameDX 10MediumCB. For ease of writing, you canShader, Re-organize these variables, such:

Static Float4 * 4 Viewproj = Float4 * 4 (Perframeconstants [ 0 ], Perframeconstants [ 1 ], Perframeconstants [ 2 ], Perframeconstants [ 3 ]);
Static Float4 * 4 Worldmatrix = Float4 * 4 (Instanceconstant [ 0 ], Instanceconstant [ 1 ], Instanceconstant [ 2 ], Instanceconstant [ 3 ]);

....................................

 

 

ApplyProgramFor example, the original number of parameters of different types is reduced to a few arrays. AllShader constatChanges are first cached in the array, and then submitted at one time:

Vector4 [] instanceconstant;
Instanceconstant. setvalue (startindex, elementcount, value );
// ............... Set other constant
Effectparameter. setvalue (instanceconstant );

 

 

although the preceding pseudo Code instanceconstant. setvalue and effectparameter. setvalu it looks very similar, but its performance varies many times: instanceconstant. setvalue only find and assign values to specific array elements; effectparameter. setvalue includes a series of parameter type verification, from Manage Code to native code , And the underlying DX function call and so on.

of course, performance improvement is not free, rewrite shader later, we need additional information to know which elements of the data where a parameter is located. In addition, it corresponds to dx10 same, CB divided It has a great impact on performance.

Finally, in addition to performance, this method also has an additional benefit, that is, unifiedDX9/10Parameter update methodXNaAnd supports multipleDXIt is also suitable for traditional engines,Just cause 2That's what we do.J

 

 

PS: I plan to do this for a long time, but I am afraid that the interface design is too complicated. Today I read just cause2's practice and finally strengthened my confidence. I will change the code tomorrow -,-

 

 

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.