Jump to content

  • Log In with Google      Sign In   
  • Create Account

#ActualNightCreature83

Posted 01 August 2013 - 12:21 PM

Well I did do some restructuring of that particular piece of code and have gotten ride of the memory allocation in vertex buffer and also gotten ride of having to actually store the elements in a member variable anyway. This has moved the problem into the destructor on the passed in vector, which is also a nightmare to debug because of the STL syntax and unlikely it is due to the use of STL.

 

Also build a win32 executable and can say it is nothing to do with the bit fields as the win32 executable crashes at the same location sad.png.

 

If I knew what was going on I could actually build a minimal case, as it is crashing in code that shouldn't it gets weirder than it should be as I have no starting point for this crash. The odd bit is that we are only every accessing members of the vector and never actually writing to it, at least not from the C++ code as it looks sad.png.

 

BTW here is the reworked code for vertex buffer.

//-----------------------------------------------------------------------------
//! @brief   TODO enter a description
//! @remark
//-----------------------------------------------------------------------------
bool VertexBuffer::createBufferAndLayoutElements( const DeviceManager& deviceManager, unsigned int bufferSize, void* data, bool dynamic, bool position, bool normal, const std::vector<unsigned int>& textureCoordinateDimensions, ID3DBlob* vertexShaderCodeBlob )
{
    D3D11_BUFFER_DESC bufferDescriptor;
    ZeroMemory(&bufferDescriptor, sizeof(D3D11_BUFFER_DESC));
    bufferDescriptor.Usage = D3D11_USAGE_IMMUTABLE;
    bufferDescriptor.ByteWidth = bufferSize;
    bufferDescriptor.BindFlags = D3D11_BIND_VERTEX_BUFFER;
    bufferDescriptor.CPUAccessFlags = 0;
    bufferDescriptor.MiscFlags = 0;

    D3D11_SUBRESOURCE_DATA initData;
    ZeroMemory(&initData, sizeof(D3D11_SUBRESOURCE_DATA));
    initData.pSysMem = data;
    initData.SysMemPitch = 0;
    initData.SysMemSlicePitch = 0;

    HRESULT hr = deviceManager.getDevice()->CreateBuffer( &bufferDescriptor, &initData, &m_buffer );
    if (FAILED(hr))
    {
        MSG_TRACE_CHANNEL("VERTEXBUFFER", "Failed to create a D3D11 Buffer object with code: 0x%x", hr );
        return false;
    }

    createVertexInputLayout(deviceManager, vertexShaderCodeBlob, createInputElementLayout(position, normal, textureCoordinateDimensions));
    
    m_vertexCount = bufferSize / m_vertexStride;
    dynamic = false;

    return true;
}
//-----------------------------------------------------------------------------
//! @brief   TODO enter a description
//! @remark
//-----------------------------------------------------------------------------
bool VertexBuffer::createVertexInputLayout( const DeviceManager& deviceManager, ID3DBlob* vertexShaderCodeBlob, const std::vector<D3D11_INPUT_ELEMENT_DESC>& inputElements )
{
    HRESULT hr = deviceManager.getDevice()->CreateInputLayout(&inputElements[0], (unsigned int)inputElements.size(), vertexShaderCodeBlob->GetBufferPointer(), vertexShaderCodeBlob->GetBufferSize(), &m_inputLayout );

    if (FAILED( hr ) )
    {        
        MSG_TRACE_CHANNEL("VERTEXBUFFER", "Failed to create the input layout: 0x%x", hr )
            return false;
    }

    return true;
}
//-----------------------------------------------------------------------------
//! @brief   TODO enter a description
//! @remark
//-----------------------------------------------------------------------------
std::vector<D3D11_INPUT_ELEMENT_DESC> VertexBuffer::createInputElementLayout( bool position, bool normal, const std::vector<unsigned int> &textureCoordinateDimensions )
{
    //Create the buffer layout elements
    unsigned int numberOfElements = 0;
    if (position)
    {
        ++numberOfElements;
    }
    if (normal)
    {
        ++numberOfElements;
    }
    numberOfElements += (unsigned int)textureCoordinateDimensions.size();
    std::vector<D3D11_INPUT_ELEMENT_DESC> vertexDataLayoutElements;
    vertexDataLayoutElements.reserve(numberOfElements);//New overwrites data on the stack
    m_vertexStride = 0;
    if (position)
    {
        D3D11_INPUT_ELEMENT_DESC layout;
        layout.SemanticName = "POSITION";
        layout.SemanticIndex = 0; 
        layout.Format = DXGI_FORMAT_R32G32B32_FLOAT;
        layout.InputSlot = 0;
        layout.AlignedByteOffset = m_vertexStride;
        layout.InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
        layout.InstanceDataStepRate = 0;
        vertexDataLayoutElements.push_back(layout);
        m_vertexStride += 12;
    }

    if (normal)
    {
        D3D11_INPUT_ELEMENT_DESC layout;
        layout.SemanticName = "NORMAL";
        layout.SemanticIndex = 0; 
        layout.Format = DXGI_FORMAT_R32G32B32_FLOAT;
        layout.InputSlot = 0;
        layout.AlignedByteOffset = m_vertexStride;
        layout.InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
        layout.InstanceDataStepRate = 0;
        vertexDataLayoutElements.push_back(layout);
        m_vertexStride += 12;
    }

    for (unsigned int counter = 0; counter < textureCoordinateDimensions.size(); ++counter)
    {
        D3D11_INPUT_ELEMENT_DESC layout;
        layout.SemanticName = "TEXCOORD";
        layout.SemanticIndex = counter; 
        layout.InputSlot = 0;
        layout.InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
        layout.InstanceDataStepRate = 0;
        if (textureCoordinateDimensions[counter] == 2)
        {
            layout.AlignedByteOffset = m_vertexStride;
            layout.Format = DXGI_FORMAT_R32G32_FLOAT;
            m_vertexStride += 8;
        }
        else if (textureCoordinateDimensions[counter] == 3)
        {
            layout.AlignedByteOffset = m_vertexStride;
            layout.Format = DXGI_FORMAT_R32G32B32_FLOAT;
            m_vertexStride += 12;
        }
        else if (textureCoordinateDimensions[counter] == 4)
        {
            layout.AlignedByteOffset = m_vertexStride;
            layout.Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
            m_vertexStride += 16;
        }
        else
        {
            //Assume 2D texcoords
            layout.AlignedByteOffset = m_vertexStride;
            layout.Format = DXGI_FORMAT_R32G32_FLOAT;
            m_vertexStride += 8;
        }
        vertexDataLayoutElements.push_back(layout);
    }

    return vertexDataLayoutElements;
}

#1NightCreature83

Posted 01 August 2013 - 12:19 PM

Well I did do some restructuring of that particular piece of code and have gotten ride of the memory allocation in vertex buffer and also gotten ride of having to actually store the elements in a member variable anyway. This has moved the problem into the destructor on the passed in vector, which is also a nightmare to debug because of the STL syntax and unlikely it is due to the use of STL.

 

Also build a win32 executable and can say it is nothing to do with the bit fields as the win32 executable crashes at the same location :(.

 

If I knew what was going on I could actually build a minimal case, as it is crashing in code that shouldn't it gets weirder than it should be as I have no starting point for this crash. The odd bit is that we are only every accessing members of the vector and never actually writing to it, at least not from the C++ code as it looks :(.


PARTNERS