0

Is it possible to separate the different types of data being passed into HLSL shader?

For example, most tutorials that I have seen setup the buffer as

 struct vertin{
    float4 position: POSITION;
    float2 text    : TEXCOORD;
    float4 norm    : NORMAL:
 }

What I want to do is pass in each data separately without create a struct to hold everything together, thus separating the position vertices from texture from normal, and pass each in separately. Also how would the shader be able to tell which normal correspond to which vertices and texture?

I think what I am thinking about is very similar to how OPENGL can bind each vertex buffer, texture buffer, and normal buffer separately when passing to the shader. Can HLSL do the same, and how so?

Also I am working with DirectX 11.

mma1480
  • 659
  • 1
  • 9
  • 18
  • 1
    That's pretty much what I'm looking for. Thanks. I couldn't find it when I was searching for it. I guess I didn't word it right or something. – mma1480 Apr 10 '14 at 04:55

1 Answers1

-1

Yes you can.

In DirectX app, define each variable as a struct

struct CBNeverChanges
{
    XMMATRIX mView;
};

struct CBChangeOnResize
{
    XMMATRIX mProjection;
};

in Shader file, define each variable as cbuffer

cbuffer cbNeverChanges : register( b0 )
{
    matrix View;
};

cbuffer cbChangeOnResize : register( b1 )
{
    matrix Projection;
};

Use CreateBuffer to create the contant buffer

D3D11_BUFFER_DESC bd;
ZeroMemory( &bd, sizeof(bd) );
bd.Usage = D3D11_USAGE_DEFAULT;
bd.ByteWidth = sizeof(CBNeverChanges);
bd.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
bd.CPUAccessFlags = 0;
hr = g_pd3dDevice->CreateBuffer( &bd, NULL, &g_pCBNeverChanges );

Use UpdateSubresource to update the shader variables.

CBNeverChanges cbNeverChanges;
cbNeverChanges.mView = XMMatrixTranspose( g_View );
g_pImmediateContext->UpdateSubresource( g_pCBNeverChanges, 0, NULL, &cbNeverChanges, 0, 0 );

UPDATE: If you even don't want to use a struct, try below code.

// Shader variables
XMMATRIX mView;

// pointer to constant buffer
ID3D11Buffer* g_pCBChangeOnResize = NULL;

// Create constant buffer
D3D11_BUFFER_DESC bd;
ZeroMemory( &bd, sizeof(bd) );
bd.Usage = D3D11_USAGE_DEFAULT;
bd.ByteWidth = sizeof(XMMATRIX);
bd.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
bd.CPUAccessFlags = 0;
hr = g_pd3dDevice->CreateBuffer( &bd, NULL, &g_pCBNeverChanges );

// Update shader variable
mView = XMMatrixTranspose( g_View );
g_pImmediateContext->UpdateSubresource( g_pCBNeverChanges, 0, NULL, &mView, 0, 0 );
zdd
  • 8,258
  • 8
  • 46
  • 75
  • Even if it's like an array of variables? For example, I want to pass in texture and normals separately, would it still work in this case? – mma1480 Apr 10 '14 at 02:29
  • Of course, you can define your texture and normals in different struct. – zdd Apr 10 '14 at 02:34
  • I see. I will look more into that and try it out. Thanks. – mma1480 Apr 10 '14 at 02:36
  • @mma1480 Hi, see my update for code without using a struct. – zdd Apr 10 '14 at 02:47
  • The struct in the original question is the input layout, which provides different data per vertex shader invocation. Your solution only provides a single buffer (or set of a few) that are constant for all invocations. – MooseBoys Apr 10 '14 at 03:58
  • @MooseBoys Not quite clear what you mean, He just want to separate the variables. – zdd Apr 10 '14 at 04:08
  • Please give a explanation for voting down. – zdd Apr 10 '14 at 04:29
  • 1
    I think you are talking about using multiple constant buffers but the OP is looking for multiple vertex buffer solution – gareththegeek Apr 10 '14 at 08:42
  • @gareththegeek I got you! thanks! – zdd Apr 10 '14 at 09:00