can multiple shaders with different vertex input types use the same vertex buffer?

StackOverflow https://stackoverflow.com/questions/11436146

  •  20-06-2021
  •  | 
  •  

سؤال

Let's say that i've got a model. This model has data for position, color, normals, and 2 texture coords. Now let's say i have a shader who's input type is position and color only. However, this model's vertex buffer is in the format of:

struct Vertex_PCNT2
{
    D3DXVECTOR3 position;
    D3DXVECTOR4 color;
    D3DXVECTOR3 normal;
    D3DXVECTOR2 tex1;
    D3DXVECTOR2 tex2;
};

Despite the fact that this model has the information in it for the shader that takes position and color, i can't use it because there is superfluous data. The only other solution i can think of is to have multiple vertex buffers with the same information minus a couple fields, but that's just plain redundant. There seriously has to be a better way. Any help here?

edit: Figured i'd elaborate my question a little with some time before work. When talking about the vertex buffer i mean the ID3D11Buffer * which you create with ID3D11Device::CreateBuffer . Before hand i fill an array of vertices wiht type Vertex_PCNT2 up there. From what i can tell at this point, that buffer is now permanently in this format so all shaders would have to work with this format. Setting up other buffers would just be a strain on ram when the data already exists.

هل كانت مفيدة؟

المحلول

Usually, this is done by creating different InputLayout for the same vertex buffer. Remember that the InputLayout can act as a filtered view between vertex input data and the expected input of vertex shader.

The InputLayout is exactly done for this kind of scenario. It is basically decoupling the layout of the original data and the layout of the binding to the vertex shader input.

For example, if you only need to map position and color, you just have to instantiate an input layout with an array of two D3D11_INPUT_ELEMENT_DESC elements with the correct AlignedByteOffset set.

In the case of selecting only "POSITION" (offset 0) and "COLOR" (offset in bytes: 12):

D3D11_INPUT_ELEMENT_DESC layout[] =
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT,    0, 0,  D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "COLOR"   , 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
}; 

but you could also select just "POSITION" (offset 0) and "NORMAL" (offset 28):

D3D11_INPUT_ELEMENT_DESC layout[] =
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT,    0, 0,  D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "NORMAL"  , 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 28, D3D11_INPUT_PER_VERTEX_DATA, 0 },
}; 
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top