DirectX11 Defines Shader Constants

Having an XNA background, I'm trying to create a simple DirectX11 application. Now I'm trying to figure out how to set shader constants like projection matrix etc. I read about constant buffers, but is there an easy way to set some constants before rendering without using buffers?

+4
source share
2 answers

I went the same way, and this is what I fought with. Because DirectX10 made a lot of changes to the interface, allowing more general control.

Sooner or later you will get used to the buffers and find that they are actually pretty neat.

Here are some benefits and tips:

  • Now there is a single way to link vertex, index, and persistent data to the pipeline, namely the Buffer class.

  • The buffer does not care about the data layout. You can write any combination of floats, not just matrices and vectors. I usually combine all the constants of one buffer into float[] and then use a loop to write it to the stream.

  • Constants are packed into registers, each of which supports up to four 32-bit components.

  • You can manually set constants using the packoffset() function of HLSL. There are some rules you should consider.

  • Since you can link multiple constant buffers in one step, it is recommended that you separate your constants by the frequency of their updates. For example, representation and projection matrices are usually updated after each frame, while the world matrix will change for each grid. To ensure optimal performance, you must create two buffers, one of which contains the data of each frame, and the other for the data in the cell.

  • Finally, the limitations for persistent buffers are :

Each persistent buffer can contain up to 4096 vectors; each vector contains up to four 32-bit values. You can bind up to 14 constant buffers to one stage of the pipeline (2 additional slots are reserved for internal use).

+7
source

In DirectX11, you set constant buffers for shaders. You cannot set only one constant at a time. You can set only the entire buffer at a time. Either use persistent buffers, or look at the Effects11 structure, which will simplify the use of the API for shader constants (similar to the D3D9 effects).

Here is an example of creating a constant buffer and its application for the vertex shader: http://msdn.microsoft.com/en-us/library/ff476896.aspx

+4
source

Source: https://habr.com/ru/post/1399505/


All Articles