Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


Incorrect Vertex Shader float input values (NaN or 0.000000000).


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 collie   Members   -  Reputation: 169

Like
0Likes
Like

Posted 08 February 2013 - 11:38 AM

I have a problem where certain floats in a VertexShaderInput are either NaN or 0.000000000.
I have 4 streams being sent to the vertex shader.

float2 'textureCoordinates' of slot 0 always appear to be NaN.
float 'bitWidth' of slot 2 always appear to be 0.000000000.

struct VertexShaderInput
{
    // Vertex buffer slot 0.
    float4 vertexPosition      : POSITION0;
    float2 textureCoordinates  : TEXCOORD0;
    float vertexNumber         : TEXCOORD1;
    
    // Vertex buffer slot 1.
    float4 bitColour           : COLOR0;
    
    // Vertex buffer slot 2.
    float bitWidth             : TEXCOORD2;
    
    // Vertex buffer slot 4.
    float2 bitOffset           : TEXCOORD3;
};

The other floats appear to have the expected values when debugged.  Slot 2-3 are instanced streams.
I have tried changing float sizes and the matching DXGI_FORMAT_xxxx_FLOAT to no avail.

In the data I have given each float an incrementing value to make it easier to check the input.
 

  const D3D11_INPUT_ELEMENT_DESC vertexLayoutDesc[] =
  {    
    // Vertex buffer slot 0.
    { "POSITION", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 0,
      D3D11_INPUT_PER_VERTEX_DATA,   0 }, // vertexPosition
    { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT,       0, D3D11_APPEND_ALIGNED_ELEMENT, 
      D3D11_INPUT_PER_VERTEX_DATA,   0 }, // textureCoordinates
    { "TEXCOORD", 1, DXGI_FORMAT_R32_FLOAT,          0, D3D11_APPEND_ALIGNED_ELEMENT, 
      D3D11_INPUT_PER_VERTEX_DATA,   0 }, // vertexNumber    
    
    // Vertex buffer slot 1.
    { "COLOR",    0, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, D3D11_APPEND_ALIGNED_ELEMENT, 
      D3D11_INPUT_PER_INSTANCE_DATA, 1 }, // bitColour
    
    // Vertex buffer slot 2.
    { "TEXCOORD", 2, DXGI_FORMAT_R32_FLOAT,          2, D3D11_APPEND_ALIGNED_ELEMENT, 
      D3D11_INPUT_PER_INSTANCE_DATA, 1 }, // bitWidth
    
    // Vertex buffer slot 3.
    { "TEXCOORD", 3, DXGI_FORMAT_R32G32_FLOAT,       3, D3D11_APPEND_ALIGNED_ELEMENT, 
      D3D11_INPUT_PER_INSTANCE_DATA, 1 }, // bitOffset
  };
 
  hr = device->CreateInputLayout(
    vertexLayoutDesc,
    ARRAYSIZE(vertexLayoutDesc),
    vertexShaderBytecode,
    vertexShaderBytecodeLength,
    &_inputLayout);

deviceContext->IASetInputLayout(_inputLayout);

 struct VertexPositions
 {
     DirectX::XMFLOAT4 _vertexPosition;
     DirectX::XMFLOAT2 _textureCoordinates;
     FLOAT             _vertexNumber;
 }
 
 VertexPositions vpnts[] =
  {
    {
      DirectX::XMFLOAT4(1.0f, 2.0f, 3.0f, 4.0f),   // vertexPosition
      DirectX::XMFLOAT2(5.0f, 6.0f),               // textureCoordinates
      7.0f                                         // vertexNumber
    },
    {
      DirectX::XMFLOAT4(8.0f, 9.0f, 10.0f, 10.0f), // vertexPosition
      DirectX::XMFLOAT2(12.0f, 13.0f),             // textureCoordinates
      14.0f                                        // vertexNumber
    },
    {
      DirectX::XMFLOAT4(15.0f, 16.0f, 17.0f, 18.0f),  // vertexPosition
      DirectX::XMFLOAT2(19.0f, 20.0f),                // textureCoordinates
      21.0f                                           // vertexNumber
      },
    {
      DirectX::XMFLOAT4(22.0f, 24.0f, 24.0f, 25.0f),  // vertexPosition
      DirectX::XMFLOAT2(26.0f, 27.0f),                // textureCoordinates
      28.0f                                           // vertexNumber
    }
  };
 
#pragma region Vertex Buffer slot 0
  D3D11_BUFFER_DESC vertexBufferDesc;
  ::ZeroMemory(&vertexBufferDesc, sizeof(D3D11_BUFFER_DESC));
  vertexBufferDesc.ByteWidth = sizeof(VertexPositions) * 4;
  vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
  vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
  vertexBufferDesc.CPUAccessFlags = 0;
  vertexBufferDesc.MiscFlags = 0;

  D3D11_SUBRESOURCE_DATA vertexBufferData;
  ::ZeroMemory(&vertexBufferData, sizeof(D3D11_SUBRESOURCE_DATA));
  vertexBufferData.pSysMem = vpnts;
  vertexBufferData.SysMemPitch = 0;
  vertexBufferData.SysMemSlicePitch = 0;

  hr = device->CreateBuffer(&vertexBufferDesc, &vertexBufferData, &_vbBitBlock);
#pragma endregion

  INT instanceCount = 1;

  DirectX::XMFLOAT4* bitColors = new DirectX::XMFLOAT4[1];
  (*bitColors).x = 29.0f;
  (*bitColors).y = 30.0f;
  (*bitColors).z = 31.0f;
  (*bitColors).w = 32.0f;
 
  FLOAT* bitWidths = new FLOAT[1];
  (*bitWidths) = 33.0f;
 
  DirectX::XMFLOAT2* bitOffsets = new DirectX::XMFLOAT2[1];
  (*bitOffsets).x = 34.0f;
  (*bitOffsets).y = 35.0f;
 
#pragma region Vertex Buffer slot 1
  D3D11_BUFFER_DESC vertexBufferDesc_one;
  ::ZeroMemory(&vertexBufferDesc_one, sizeof(D3D11_BUFFER_DESC));
  vertexBufferDesc_one.ByteWidth = sizeof(DirectX::XMFLOAT4) * instanceCount;
  vertexBufferDesc_one.Usage = D3D11_USAGE_DEFAULT;
  vertexBufferDesc_one.BindFlags = D3D11_BIND_VERTEX_BUFFER;
  vertexBufferDesc_one.CPUAccessFlags = 0;
  vertexBufferDesc_one.MiscFlags = 0;
 
  D3D11_SUBRESOURCE_DATA vertexBufferData_one;
  ::ZeroMemory(&vertexBufferData_one, sizeof(D3D11_SUBRESOURCE_DATA));
  vertexBufferData_one.pSysMem = bitColors;
  vertexBufferData_one.SysMemPitch = 0;
  vertexBufferData_one.SysMemSlicePitch = 0;
    
  hr = device->CreateBuffer(&vertexBufferDesc_one, &vertexBufferData_one, &_vbBitColors);
  assert(SUCCEEDED(hr));
#pragma endregion

#pragma region Vertex Buffer slot 2
  D3D11_BUFFER_DESC vertexBufferDesc_two;
  ::ZeroMemory(&vertexBufferDesc_two, sizeof(D3D11_BUFFER_DESC));
  vertexBufferDesc_two.ByteWidth = sizeof(FLOAT) * instanceCount;
  vertexBufferDesc_two.Usage = D3D11_USAGE_DEFAULT;
  vertexBufferDesc_two.BindFlags = D3D11_BIND_VERTEX_BUFFER;
  vertexBufferDesc_two.CPUAccessFlags = 0;
  vertexBufferDesc_two.MiscFlags = 0;
 
  D3D11_SUBRESOURCE_DATA vertexBufferData_two;
  ::ZeroMemory(&vertexBufferData_two, sizeof(D3D11_SUBRESOURCE_DATA));
  vertexBufferData_two.pSysMem = bitWidths;
  vertexBufferData_two.SysMemPitch = 0;
  vertexBufferData_two.SysMemSlicePitch = 0;
    
  hr = device->CreateBuffer(&vertexBufferDesc_two, &vertexBufferData_two, &_vbBitWidths);
  assert(SUCCEEDED(hr));
#pragma endregion

#pragma region Vertex Buffer slot 3
  D3D11_BUFFER_DESC vertexBufferDesc_three;
  ::ZeroMemory(&vertexBufferDesc_three, sizeof(D3D11_BUFFER_DESC));
  vertexBufferDesc_three.ByteWidth = sizeof(DirectX::XMFLOAT2) * instanceCount;
  vertexBufferDesc_three.Usage = D3D11_USAGE_DEFAULT;
  vertexBufferDesc_three.BindFlags = D3D11_BIND_VERTEX_BUFFER;
  vertexBufferDesc_three.CPUAccessFlags = 0;
  vertexBufferDesc_three.MiscFlags = 0;
 
  D3D11_SUBRESOURCE_DATA vertexBufferData_three;
  ::ZeroMemory(&vertexBufferData_three, sizeof(D3D11_SUBRESOURCE_DATA));
  vertexBufferData_three.pSysMem = bitOffsets;
  vertexBufferData_three.SysMemPitch = 0;
  vertexBufferData_three.SysMemSlicePitch = 0;
    
  hr = device->CreateBuffer(&vertexBufferDesc_three, &vertexBufferData_three, &_vbBitOffsets);
  assert(SUCCEEDED(hr));
#pragma endregion
 
  const int NumVertexBuffers = 4;

  UINT strides[NumVertexBuffers];
  strides[0] = sizeof(VertexPositions);   // stride 28.
  strides[1] = sizeof(DirectX::XMFLOAT4); // stride 16.
  strides[2] = sizeof(FLOAT);             // stride 4.
  strides[3] = sizeof(DirectX::XMFLOAT2); // stride 8.

  // Set the buffer offsets.
  UINT offsets[NumVertexBuffers];
  offsets[0] = 0;
  offsets[1] = 0;
  offsets[2] = 0;
  offsets[3] = 0;

  // Set the array of pointers to the vertex and instance buffers.
  ID3D11Buffer* bufferPointers[NumVertexBuffers];
  bufferPointers[0] = _vbBitBlock;    
  bufferPointers[1] = _vbBitColors;
  bufferPointers[2] = _vbBitWidths;
  bufferPointers[3] = _vbBitOffsets;

  DeviceContext->IASetVertexBuffers(0, NumVertexBuffers, bufferPointers, strides, offsets);
 
  DeviceContext->DrawIndexedInstanced(6, instanceCount, 0, 0, 0);


The vertex buffers captured from PIX below appear to be correct.
 

 Currently used format:  float
---------------------------------------------------------------------------------------------------------------------------------
 
  0    [0x00000000-0x00000003]    |                  +1
  1    [0x00000004-0x00000007]    |                  +2
  2    [0x00000008-0x0000000b]    |                  +3
  3    [0x0000000c-0x0000000f]    |                  +4
  4    [0x00000010-0x00000013]    |                  +5
  5    [0x00000014-0x00000017]    |                  +6
  6    [0x00000018-0x0000001b]    |                  +7
  7    [0x0000001c-0x0000001f]    |                  +8
  8    [0x00000020-0x00000023]    |                  +9
  9    [0x00000024-0x00000027]    |                 +10
 10    [0x00000028-0x0000002b]    |                 +10
 11    [0x0000002c-0x0000002f]    |                 +12
 12    [0x00000030-0x00000033]    |                 +13
 13    [0x00000034-0x00000037]    |                 +14
 14    [0x00000038-0x0000003b]    |                 +15
 15    [0x0000003c-0x0000003f]    |                 +16
 16    [0x00000040-0x00000043]    |                 +17
 17    [0x00000044-0x00000047]    |                 +18
 18    [0x00000048-0x0000004b]    |                 +19
 19    [0x0000004c-0x0000004f]    |                 +20
 20    [0x00000050-0x00000053]    |                 +21
 21    [0x00000054-0x00000057]    |                 +22
 22    [0x00000058-0x0000005b]    |                 +24
 23    [0x0000005c-0x0000005f]    |                 +24
 24    [0x00000060-0x00000063]    |                 +25
 25    [0x00000064-0x00000067]    |                 +26
 26    [0x00000068-0x0000006b]    |                 +27
 27    [0x0000006c-0x0000006f]    |                 +28

  0    [0x00000000-0x00000003]    |                 +29
  1    [0x00000004-0x00000007]    |                 +30
  2    [0x00000008-0x0000000b]    |                 +31
  3    [0x0000000c-0x0000000f]    |                 +32

  0    [0x00000000-0x00000003]    |                 +33

  0    [0x00000000-0x00000003]    |                 +34
  1    [0x00000004-0x00000007]    |                 +35

Debugging the vertex shader input values within hlsl shows that most values do appear to be correct.
 

vertexPosition        x = 1.000000000, y = 2.000000000, z = 3.000000000, w = 4.000000000    float4
textureCoordinates    x = NaN, y = NaN    float2
vertexNumber          7.000000000    float
bitColour             x = 29.000000000, y = 30.000000000, z = 31.000000000, w = 32.000000000    float4
bitWidth              0.000000000    float
bitOffset             x = 34.000000000, y = 35.000000000    float2

So whats happened to 'textureCoordinates' and 'bitWidth'?

I've left out the IASetIndexBuffer() code as I don't think it's causing the issue.
I am using debug D3D11_CREATE_DEVICE_DEBUG and during execution there appears to be no DX errors or warnings.

 

Hopefully I've given enough information above to describe what I'm doing.

I've been trying to figure this out for the last week by commenting bits out and changing type sizes but nothing works.
As far as I can see it appears what I'm doing is correct so any pointers into what could be giving me the NaN and 0.000000000 would be *really* appreciated.
 
Many thanks.

Cole


Edited by collie, 08 February 2013 - 12:43 PM.


Sponsor:

#2 Polarist   Members   -  Reputation: 160

Like
0Likes
Like

Posted 08 February 2013 - 12:36 PM

Where are you setting vertexLayoutDesc?



#3 collie   Members   -  Reputation: 169

Like
0Likes
Like

Posted 08 February 2013 - 12:50 PM

Thanks for the reply.

I set it directly after the vertexLayoutDesc declaration.

 

const D3D11_INPUT_ELEMENT_DESC vertexLayoutDesc[] =
{
// Vertex buffer slot 0.
{ "POSITION", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 0,
D3D11_INPUT_PER_VERTEX_DATA, 0 }, // vertexPosition
{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT,
D3D11_INPUT_PER_VERTEX_DATA, 0 }, // textureCoordinates
{ "TEXCOORD", 1, DXGI_FORMAT_R32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT,
D3D11_INPUT_PER_VERTEX_DATA, 0 }, // vertexNumber

// Vertex buffer slot 1.
{ "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, D3D11_APPEND_ALIGNED_ELEMENT,
D3D11_INPUT_PER_INSTANCE_DATA, 1 }, // bitColour

// Vertex buffer slot 2.
{ "TEXCOORD", 2, DXGI_FORMAT_R32_FLOAT, 2, D3D11_APPEND_ALIGNED_ELEMENT,
D3D11_INPUT_PER_INSTANCE_DATA, 1 }, // bitWidth

// Vertex buffer slot 3.
{ "TEXCOORD", 3, DXGI_FORMAT_R32G32_FLOAT, 3, D3D11_APPEND_ALIGNED_ELEMENT,
D3D11_INPUT_PER_INSTANCE_DATA, 1 }, // bitOffset
};

  hr = device->CreateInputLayout(
    vertexLayoutDesc,
    ARRAYSIZE(vertexLayoutDesc),
    vertexShaderBytecode,
    vertexShaderBytecodeLength,
    &_inputLayout);

  deviceContext->IASetInputLayout(_inputLayout);

Edited by collie, 08 February 2013 - 12:51 PM.


#4 Polarist   Members   -  Reputation: 160

Like
0Likes
Like

Posted 08 February 2013 - 01:33 PM

I set it directly after the vertexLayoutDesc declaration.

 

Ahh sorry, somehow I missed it when doing a "find".

 

I stepped through your code (line-by-line), and the logic appears to be OK..

 

how are you outputting the VertexShaderInput struct?  What behavior do you see when you render?

 

Have you verified that D3D11_APPEND_ALIGNED_ELEMENT is producing the correct offsets?  It probably is, I haven't used that preprocessor define before.



#5 Polarist   Members   -  Reputation: 160

Like
1Likes
Like

Posted 08 February 2013 - 01:50 PM

Also, are you using those values (texcoord and bytewidth)?  If not, the shader compiler could optimize those variables away.



#6 collie   Members   -  Reputation: 169

Like
0Likes
Like

Posted 08 February 2013 - 03:51 PM

Well done; you were right I *wasn't* using...

float2 textureCoordinates  : TEXCOORD0;

...within the vertex shader.
I did a quick hack and added it to another existing value, recompiled and it magically appeared.
As I was only using the .x part of the float2 it appeared as...

textureCoordinates    x = 5.000000000, y = NaN    float2

So 'NaN's basically could mean a variable is unused. 

 

Also interestingly...

float bitWidth             : TEXCOORD2;

...turned out to have a value of...

bitWidth    00.000000000    float

...because though it *was* being used within the vertex shader it was within a 'if' that wouldn't be entered.
Another quick bodge to allow the entering of the 'if' showed the value as...

bitWidth    33.000000000    float

So a 0.000000000 value could/may mean that though a variable is being used within the vertex shader it may not be called (so you won't see the true value).

I wonder if this behaviour is written down anywhere as I can't remember reading this or seeing it before?

Many thanks for your help Polarist; have a good weekend.

 

Regards
Cole


Edited by collie, 08 February 2013 - 03:52 PM.


#7 Polarist   Members   -  Reputation: 160

Like
0Likes
Like

Posted 09 February 2013 - 12:36 AM

I wonder if this behaviour is written down anywhere as I can't remember reading this or seeing it before?

 

I don't recall where it's specified, but the compiler will optimize away unused parts of code.  E.g. in this case, free up registers that aren't being used/referenced in your shader's main() function.  The values being shown (NaN and 00.0000) are likely just values for uninitialized variables (uninitialized because those variables were optimized away).

 

Among other tricks, this behavior is nice to keep in mind if you prefer to unify many shaders with the same include file.  You can define a common "interface" for attaching data to your shaders, but leave them unused in the implementation to make the simpler ones lightweight.  For instance, you might have two pixel shaders that take nearly the same information, except one of the two takes an additional Normal vector.  With this optimization feature in mind, they could share the same VertexShaderInput definition through a header file, and you wouldn't have to worry about it being wasteful.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS