c++ - 无法创建顶点缓冲区

标签 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 },
};

顶点着色器.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->CreateBuffer(&vertexBufferDesc, &vertexBufferData, &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);
}

最佳答案

关于采样器,您需要将它分配给您的像素着色器。

m_d3dContext.Get()->PSSetSamplers(0,1,&m_sampleState);

关于c++ - 无法创建顶点缓冲区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13604319/

相关文章:

c++ - 如何将 std::string 转换为不同类别的字符串?

json - 在 API 中发布 Json 数据

c++ - 为什么常量缓冲区在 Directx 11 中部分更新

C++ ShellExecute特殊字符

c++ - 可以将花括号数字列表用作传递给可变参数函数(或构造函数)的参数吗?

c# - 如何修复错误 : Cannot call the requested method (GetBasicPropertiesAsync). 之前对此方法的调用正在挂起

c# - 在c#中声明一个全局变量

windows-7 - VS2012、Windows 8、DirectX 及其 SDK

c++ - IDXGISwapChain::CheckInterfaceSupport 返回的结果不能

C++读取大量文件的一小部分