无法创建顶点缓冲区

时间:2012-11-28 11:45:41

标签: c++ windows-phone-8 directx-11 vertex-buffer

我有一个带有DirectX组件的Windows Phone 8 C#/ XAML项目。我正在尝试渲染一些粒子。我创建了一个顶点缓冲区,我看到它进入要创建的函数,但是当它到达更新顶点缓冲区时,缓冲区为NULL。我还没有发布缓冲区。你知道为什么会这样吗?任何输出消息有帮助吗?感谢。

我的输出窗口上的打印输出和错误:

'TaskHost.exe' (Win32): Loaded '\Device\HarddiskVolume4\Windows\System32\d3d11_1SDKLayers.dll'. Cannot find or open the PDB file.
D3D11 WARNING: ID3D11Texture2D::SetPrivateData: Existing private data of same name with different size found! [ STATE_SETTING WARNING #55: SETPRIVATEDATA_CHANGINGPARAMS]
Create vertex buffer
D3D11 WARNING: ID3D11DeviceContext::DrawIndexed: The Pixel Shader unit expects a Sampler to be set at Slot 0, but none is bound. This is perfectly valid, as a NULL Sampler maps to default Sampler state. However, the developer may not want to rely on the defaults.  [ EXECUTION WARNING #352: DEVICE_DRAW_SAMPLER_NOT_SET]
The thread 0xb64 has exited with code 0 (0x0).
m_vertexBuffer is null
D3D11 WARNING: ID3D11DeviceContext::DrawIndexed: The Pixel Shader unit expects a Sampler to be set at Slot 0, but none is bound. This is perfectly valid, as a NULL Sampler maps to default Sampler state. However, the developer may not want to rely on the defaults.  [ EXECUTION WARNING #352: DEVICE_DRAW_SAMPLER_NOT_SET]
m_vertexBuffer is null

在CreateDeviceResources中,我调用CreateVertexShader,CreateInputLayout,CreatePixelShader,CreateBuffer。然后我开始创建采样器和顶点缓冲区,代码如下:

    auto createCubeTask = (createPSTask && createVSTask).then([this] () {

        // Create a texture sampler state description.
        D3D11_SAMPLER_DESC samplerDesc;
        samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
        samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
        samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
        samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
        samplerDesc.MipLODBias = 0.0f;
        samplerDesc.MaxAnisotropy = 1;
        samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
        samplerDesc.BorderColor[0] = 0;
        samplerDesc.BorderColor[1] = 0;
        samplerDesc.BorderColor[2] = 0;
        samplerDesc.BorderColor[3] = 0;
        samplerDesc.MinLOD = 0;
        samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;

        // Create the texture sampler state.
        HRESULT result = m_d3dDevice->CreateSamplerState(&samplerDesc, &m_sampleState);
        if(FAILED(result))
        {
            OutputDebugString(L"Can't CreateSamplerState");
        }

        //InitParticleSystem();

        // Set the maximum number of vertices in the vertex array.
        m_vertexCount = m_maxParticles * 6;

        // Set the maximum number of indices in the index array.
        m_indexCount = m_vertexCount;

        // Create the vertex array for the particles that will be rendered.
        m_vertices = new VertexType[m_vertexCount];
        if(!m_vertices)
        {
            OutputDebugString(L"Can't create the vertex array for the particles that will be rendered.");
        }
        else
        {
            // Initialize vertex array to zeros at first.
            int sizeOfVertexType = sizeof(VertexType);
            int totalSizeVertex = sizeOfVertexType * m_vertexCount;
            memset(m_vertices, 0, totalSizeVertex);
            D3D11_SUBRESOURCE_DATA vertexBufferData = {0};
            vertexBufferData.pSysMem = m_vertices;
            vertexBufferData.SysMemPitch = 0;
            vertexBufferData.SysMemSlicePitch = 0;

            int sizeOfMVertices = sizeof(m_vertices);
            CD3D11_BUFFER_DESC vertexBufferDesc(
                totalSizeVertex,            // byteWidth
                D3D11_BIND_VERTEX_BUFFER,   // bindFlags
                D3D11_USAGE_DYNAMIC,        // D3D11_USAGE usage = D3D11_USAGE_DEFAULT
                D3D11_CPU_ACCESS_WRITE,     // cpuaccessFlags
                0,                          // miscFlags
                0                           // structureByteStride
                );

            OutputDebugString(L"Create vertex buffer\n");

            DX::ThrowIfFailed(
                m_d3dDevice->CreateBuffer(
                    &vertexBufferDesc,
                    &vertexBufferData,
                    &m_vertexBuffer
                    )
                );
        }


        unsigned long* indices = new unsigned long[m_indexCount];
        if(!indices)
        {
            OutputDebugString(L"Can't create the index array.");
        }
        else
        {
            // Initialize the index array.
            for(int i=0; i<m_indexCount; i++)
            {
                indices[i] = i;
            }

            // Set up the description of the static index buffer.
            // Create the index array.
            D3D11_BUFFER_DESC indexBufferDesc;
            indexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
            indexBufferDesc.ByteWidth = sizeof(unsigned long) * m_indexCount;
            indexBufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
            indexBufferDesc.CPUAccessFlags = 0;
            indexBufferDesc.MiscFlags = 0;
            indexBufferDesc.StructureByteStride = 0;

            // Give the subresource structure a pointer to the index data.
            D3D11_SUBRESOURCE_DATA indexData;       
            indexData.pSysMem = indices;
            indexData.SysMemPitch = 0;
            indexData.SysMemSlicePitch = 0;

            // Create the index buffer.
            DX::ThrowIfFailed(
                m_d3dDevice->CreateBuffer(
                    &indexBufferDesc,
                    &indexData,
                    &m_indexBuffer
                    )
                );

            // Release the index array since it is no longer needed.
            delete [] indices;
            indices = 0;

        }

    });

    createCubeTask.then([this] () {     
        m_loadingComplete = true;
    });
}

我的顶点是位置,特色和颜色:

struct VertexType
{
    DirectX::XMFLOAT3 position;
    DirectX::XMFLOAT2 texture;
    DirectX::XMFLOAT4 color;

};

Microsoft::WRL::ComPtr<ID3D11SamplerState> m_sampleState;
VertexType* m_vertices;
Microsoft::WRL::ComPtr<ID3D11Buffer> m_vertexBuffer;


const D3D11_INPUT_ELEMENT_DESC vertexDesc[] = 
{
    { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0,  D3D11_INPUT_PER_VERTEX_DATA, 0 },
    { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
    { "COLOR",    0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};

VertexShader.HLSL:

cbuffer ModelViewProjectionConstantBuffer : register(b0)
{
    matrix model;
    matrix view;
    matrix projection;
};

struct VertexInputType
{
    float4 position : POSITION;
    float2 tex : TEXCOORD0;
    float4 color : COLOR;
};

struct PixelInputType
{
    float4 position : SV_POSITION;
    float2 tex : TEXCOORD0;
    float4 color : COLOR;
};

PixelInputType main(VertexInputType input)
{
    PixelInputType output;    
    // Change the position vector to be 4 units for proper matrix calculations.
    input.position.w = 1.0f;
    // Calculate the position of the vertex against the world, view, and projection matrices.
    output.position = mul(input.position, model);
    output.position = mul(output.position, view);
    output.position = mul(output.position, projection);

    // Store the texture coordinates for the pixel shader.
    output.tex = input.tex;

    // Store the particle color for the pixel shader. 
    output.color = input.color;

    return output;
}

在调用:m_d3dDevice-&gt; CreateBuffer(&amp; vertexBufferDesc,&amp; vertexBufferData,&amp; m_vertexBuffer)之后,m_vertexBuffer不为空。

但是当我进入Update()函数时,m_vertexBuffer为NULL!

D3D11_MAPPED_SUBRESOURCE mappedResource;    
if (m_vertexBuffer == nullptr)
{
    OutputDebugString(L"m_vertexBuffer is null\n");
}
else
{

    // Lock the vertex buffer.
    DX::ThrowIfFailed(m_d3dContext->Map(m_vertexBuffer.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource));

    // Get a pointer to the data in the vertex buffer.
    VertexType * verticesPtr = (VertexType*)mappedResource.pData;

    //// Copy the data into the vertex buffer.
    int sizeOfVertices = sizeof(VertexType) * m_vertexCount;
    memcpy(verticesPtr, (void*)m_vertices, sizeOfVertices);

    //// Unlock the vertex buffer.
    m_d3dContext->Unmap(m_vertexBuffer.Get(), 0);
}

2 个答案:

答案 0 :(得分:1)

关于采样器,您需要将其分配给您的像素管。

m_d3dContext.Get() - &GT; PSSetSamplers(0,1,&安培; m_sampleState);

答案 1 :(得分:0)

当我去渲染它时,我在m_vertexBuffer上进行了错误的调用。 这修复了错误,m_vertexBuffer为null。

一个:

m_d3dContext.Get()->IASetVertexBuffers(0, 1, &m_vertexBuffer, &stride, &offset);

修正:

m_d3dContext.Get()->IASetVertexBuffers(0, 1, m_vertexBuffer.GetAddressOf(), &stride, &offset);

它不会修复采样器状态的错误:

  

DrawIndexed:像素着色器单元需要将采样器设置为Slot   0