• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
collie

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

6 posts in this topic

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
0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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

1

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0