XNA Creators Club Online
Page 1 of 1 (3 items)
Sort Posts: Previous Next

Vertex Buffer is amiss - Input Layout

Last post 08-29-2008 11:44 PM by n3Xus. 2 replies.
  • 08-29-2008 10:21 PM

    Vertex Buffer is amiss - Input Layout

    I orignally thought the problem was in my data conversion from a LH system to a RH system. However, I figured out how to examine my data that goes into the shader in PIX. It revealed that the POS part of my vertex struct is getting used for pos, normal, and uv. I believe my problem to be in the input layout creation. I suspect the 4th and 5th member of each element, as I don't understand them or what goes in them no matter how many times I read the documentation. Please explain these fields if you can!

     

    //--------------------------------------------------------------------------------  
    class Vertex_PosNorTex  
    {  
    public:  
     
       /** 
       * Gets the input layout associated with this vertex format
       *
       * The input layout is used to tell DirectX how to read the input buffers
       * provided to it for rendering. The input layout must be set on a device
       * before an object is rendered.
       *
       * Release of the input layout object is left to the caller.
       */ 
       static ID3D10InputLayout * GetInputLayout(ID3D10Device & d3dDevice,  
                                                 ID3D10EffectTechnique & technique);  
       D3DXVECTOR3 m_pos;    // Position  
       D3DXVECTOR3 m_normal; // Normal  
       D3DXVECTOR2 m_uv;     // Texture Coordinate  
    };  
     
     

     

     

    //-----------------------------------------------------------------  
    ID3D10InputLayout * Vertex_PosNorTex::GetInputLayout(ID3D10Device & d3dDevice,  
                                                         ID3D10EffectTechnique & technique)  
    {  
       // Define the vertex input layout in GPU memory  
       D3D10_INPUT_ELEMENT_DESC vertexDesc[ =   
       {  
          {  
             "POSITION",                    // The HLSL Semantic associated with this element in the shader code  
             0,                             // Semantic Index for the element when there is more than one  
             DXGI_FORMAT_R32G32B32_FLOAT,   // Data type of the element  
             0,                             // Input Assembler Slot. Between 0 and 15  
             D3D10_APPEND_ALIGNED_ELEMENT,  // Optional, offset in bytes between each element  
             D3D10_INPUT_PER_VERTEX_DATA,   // Input data class for a single input slot. See D3D10_INPUT_CLASSIFICATION  
             0                              // Used when data class is D3D10_INPUT_PER_INSTANCE_DATA  
          },  
     
          {  
             "NORMAL",  
             0,  
             DXGI_FORMAT_R32G32B32_FLOAT,  
             0,  
             0,  
             D3D10_INPUT_PER_VERTEX_DATA,  
             0  
          },  
     
          {  
             "TEXCOORD",  
             0,  
             DXGI_FORMAT_R32G32_FLOAT,  
             0,  
             0,  
             D3D10_INPUT_PER_VERTEX_DATA,  
             0  
          }  
       };  
     
     
  • 08-29-2008 11:44 PM In reply to

    Re: Vertex Buffer is amiss - Input Layout

    I think your problem is at the 5th parameter (AlignedByteOffset). This is the number of bytes that the data for that element is stored-> so for the POSITION element this number should be 0, since it is the 1st element, for NORMAL this number should be 12 (how did I get 12? Very easy: the POSITION element is of type DXGI_FORMAT_R32G32B32_FLOAT, this means it has 3 floats (R,G,B; aka XYZ) and since one float is 4 bytes you multiply that by the number of floats and you get 12), and for TEXCOORD it should be 24 (why 24? because it has POSITION and NORMAL before it and POSITION and NORMAL both take 12 bytes you just sum it up :P )

    I'll try to explain some parameters (what I think you should know for this):

    LPCSTR SemanticName: you will access the data in the shader by this name

     UINT SemanticIndex: I haven't used these so I can't tell you much, just put it to 0

    DXGI_FORMAT Format: the format

    UINT InputSlot: put it to 0

    UINT AlignedByteOffset: the number of bytes from the beginning of the vertex structure to this element

    It isn't much of an explanation...

    Here is how i create my input layout (note: "die" stands for "desc-input-element" :P )

     

    m_numElem=3; 
     
        die=new D3D10_INPUT_ELEMENT_DESC[m_numElem]; 
         
        die[0].AlignedByteOffset=0; 
        die[0].Format=DXGI_FORMAT_R32G32B32_FLOAT; 
        die[0].InputSlot=0; 
        die[0].InputSlotClass=D3D10_INPUT_PER_VERTEX_DATA; 
        die[0].InstanceDataStepRate=0; 
        die[0].SemanticIndex=0; 
        die[0].SemanticName="POSITION"
     
        die[1].AlignedByteOffset=12; 
        die[1].Format=DXGI_FORMAT_R32G32B32_FLOAT; 
        die[1].InputSlot=0; 
        die[1].InputSlotClass=D3D10_INPUT_PER_VERTEX_DATA; 
        die[1].InstanceDataStepRate=0; 
        die[1].SemanticIndex=0; 
        die[1].SemanticName="NORMAL"
     
        die[2].AlignedByteOffset=24; 
        die[2].Format=DXGI_FORMAT_R32G32_FLOAT; 
        die[2].InputSlot=0; 
        die[2].InputSlotClass=D3D10_INPUT_PER_VERTEX_DATA; 
        die[2].InstanceDataStepRate=0; 
        die[2].SemanticIndex=0; 
        die[2].SemanticName="TEXCOORD"
     
    device->CreateInputLayout(die,m_numElem,passDesc.pIAInputSignature,passDesc.IAInputSignatureSize,&IL); 

  • 08-30-2008 10:39 AM In reply to

    Re: Vertex Buffer is amiss - Input Layout

    The input slot defines the vertex buffer stream that contains the element. It works together with the StartSlot parameter from IASetVertexBuffers. If you have only one buffer and using 0 as StartSlot your 0 in the input elements is right.

    AlignedByteOffset defines how many bytes from the beginning of each vertex the data’s for this element are stored. In your example the position is stored at offset 0. As it requires 3*4 bytes the normal will be at 12. 12 bytes ahead you store the texture coordinates at offset 24.

    To safe yourself from calculating these values you can always use D3D10_APPEND_ALIGNED_ELEMENT . In this case you need to ensure that the elements are defned in the same order as in your struct.

Page 1 of 1 (3 items) Previous Next