DX11 中的隐形几何体

Invisible geometry in DX11

在努力理解和补偿 Microsoft 在 Windows 8 中对 D3DX 的弃用后,我 运行 遇到了一个我无法摆脱的问题。

我第一次启动我的程序时,它因为访问冲突而变得不朽。重新启动我的计算机并重新编译和 运行 相同的代码后,它没有崩溃,但我应该看到的立方体不存在。

我还收到 "object declared on the heap may not be aligned 16" 的警告。根据我的研究,这通常是因为 XNA 数学而发生的。因此,在找到 this discussion 之后,我随便尝试了所有建议的方法。除了 XMFLOAT4X4 解决方案之外的所有内容都有效,但我的立方体仍然不可见。这是我认为相关的代码部分:

class IEGame : public DX11InfernalEngineBase
{
public:
    //...
bool LoadContent()
{
    //...
    Vertex vertices[] =
    {
        { XMFLOAT3(-1.0f, 1.0f, -1.0f), XMFLOAT2(0.0f, 0.0f) },
        { XMFLOAT3(1.0f, 1.0f, -1.0f), XMFLOAT2(1.0f, 0.0f) },
        { XMFLOAT3(1.0f, 1.0f, 1.0f), XMFLOAT2(1.0f, 1.0f) },
        { XMFLOAT3(-1.0f, 1.0f, 1.0f), XMFLOAT2(0.0f, 1.0f) },

        { XMFLOAT3(-1.0f, -1.0f, -1.0f), XMFLOAT2(0.0f, 0.0f) },
        { XMFLOAT3(1.0f, -1.0f, -1.0f), XMFLOAT2(1.0f, 0.0f) },
        { XMFLOAT3(1.0f, -1.0f, 1.0f), XMFLOAT2(1.0f, 1.0f) },
        { XMFLOAT3(-1.0f, -1.0f, 1.0f), XMFLOAT2(0.0f, 1.0f) },

        { XMFLOAT3(-1.0f, -1.0f, 1.0f), XMFLOAT2(0.0f, 0.0f) },
        { XMFLOAT3(-1.0f, -1.0f, -1.0f), XMFLOAT2(1.0f, 0.0f) },
        { XMFLOAT3(-1.0f, 1.0f, -1.0f), XMFLOAT2(1.0f, 1.0f) },
        { XMFLOAT3(-1.0f, 1.0f, 1.0f), XMFLOAT2(0.0f, 1.0f) },

        { XMFLOAT3(1.0f, -1.0f, 1.0f), XMFLOAT2(0.0f, 0.0f) },
        { XMFLOAT3(1.0f, -1.0f, -1.0f), XMFLOAT2(1.0f, 0.0f) },
        { XMFLOAT3(1.0f, 1.0f, -1.0f), XMFLOAT2(1.0f, 1.0f) },
        { XMFLOAT3(1.0f, 1.0f, 1.0f), XMFLOAT2(0.0f, 1.0f) },

        { XMFLOAT3(-1.0f, -1.0f, -1.0f), XMFLOAT2(0.0f, 0.0f) },
        { XMFLOAT3(1.0f, -1.0f, -1.0f), XMFLOAT2(1.0f, 0.0f) },
        { XMFLOAT3(1.0f, 1.0f, -1.0f), XMFLOAT2(1.0f, 1.0f) },
        { XMFLOAT3(-1.0f, 1.0f, -1.0f), XMFLOAT2(0.0f, 1.0f) },

        { XMFLOAT3(-1.0f, -1.0f, 1.0f), XMFLOAT2(0.0f, 0.0f) },
        { XMFLOAT3(1.0f, -1.0f, 1.0f), XMFLOAT2(1.0f, 0.0f) },
        { XMFLOAT3(1.0f, 1.0f, 1.0f), XMFLOAT2(1.0f, 1.0f) },
        { XMFLOAT3(-1.0f, 1.0f, 1.0f), XMFLOAT2(0.0f, 1.0f) }
    };

    D3D11_BUFFER_DESC vertexDesc;
    ZeroMemory(&vertexDesc, sizeof(vertexDesc));
    vertexDesc.Usage = D3D11_USAGE_DEFAULT;
    vertexDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
    vertexDesc.ByteWidth = sizeof(VertexPos)*24;

    D3D11_SUBRESOURCE_DATA resourceData;
    ZeroMemory(&resourceData, sizeof(resourceData));
    resourceData.pSysMem = vertices;

    try{
        d3dresult = d3dDevice_->CreateBuffer(&vertexDesc, &resourceData, &vertexBuffer_);
        if (FAILED(d3dresult))
        {
            throw _com_error(d3dresult);
        }
    }
    catch (_com_error & comEx){
        MessageBox(0, comEx.ErrorMessage(), "Error Creating Vertex Buffer", MB_OK);
        return false;
    }

    WORD indices[] = {
        3, 1, 0, 2, 1, 3,
        6, 4, 5, 7, 4, 6,
        11, 9, 8, 10, 9, 11,
        14, 12, 13, 15, 12, 14,
        19, 17, 16, 18, 17, 19,
        22, 20, 21, 23, 20, 22
    };

    D3D11_BUFFER_DESC indexDesc;
    ZeroMemory(&indexDesc, sizeof(indexDesc));
    indexDesc.Usage = D3D11_USAGE_DEFAULT;
    indexDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
    indexDesc.ByteWidth = sizeof(WORD)* 36;
    indexDesc.CPUAccessFlags = 0;
    resourceData.pSysMem = indices;

    try{
        d3dresult = d3dDevice_->CreateBuffer(&indexDesc, &resourceData, &indexBuffer_);
        if (FAILED(d3dresult))
        {
            throw _com_error(d3dresult);
        }
    }
    catch (_com_error & comEx){
        MessageBox(0, comEx.ErrorMessage(), "Error Creating Index Buffer", MB_OK);
        return false;
    }

    try{
        std::vector<byte> textureFile = LoadFile("C:\Users\Marcus\documents\visual studio 2013\Projects\infernalEngine\Debug\guide.png");
        d3dresult = CreateWICTextureFromMemory(d3dDevice_, d3dContext_, textureFile.data(), textureFile.size(), nullptr, &colorMapView_, 0);
        if (FAILED(d3dresult))
        {
            throw _com_error(d3dresult);
        }
    }
    catch (_com_error & comEx){
        MessageBox(0, comEx.ErrorMessage(), "Error Loading Texture", MB_OK);
        return false;
    }

    D3D11_SAMPLER_DESC colorMapDesc;
    ZeroMemory(&colorMapDesc, sizeof(colorMapDesc));
    colorMapDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
    colorMapDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
    colorMapDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
    colorMapDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
    colorMapDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
    colorMapDesc.MaxLOD = D3D11_FLOAT32_MAX;

    try{
        d3dresult = d3dDevice_->CreateSamplerState(&colorMapDesc, &colorMapSampler_);
        if (FAILED(d3dresult))
        {
            throw _com_error(d3dresult);
        }
    }
    catch (_com_error & comEx){
        MessageBox(0, comEx.ErrorMessage(), "Error Creating Sampler State", MB_OK);
        return false;
    }

    D3D11_BUFFER_DESC constDesc;
    ZeroMemory(&constDesc, sizeof(constDesc));
    constDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
    constDesc.ByteWidth = sizeof(XMMATRIX);
    constDesc.Usage = D3D11_USAGE_DEFAULT;

    try{
        d3dresult = d3dDevice_->CreateBuffer(&constDesc, nullptr, &viewCB_);
        if (FAILED(d3dresult))
        {
            throw _com_error(d3dresult);
        }
    }
    catch (_com_error & comEx){
        MessageBox(0, comEx.ErrorMessage(), "Error Creating View Matrix", MB_OK);
        return false;
    }
    try{
        d3dresult = d3dDevice_->CreateBuffer(&constDesc, nullptr, &projCB_);
        if (FAILED(d3dresult))
        {
            throw _com_error(d3dresult);
        }
    }
    catch (_com_error & comEx){
        MessageBox(0, comEx.ErrorMessage(), "Error Creating Projection Matrix", MB_OK);
        return false;
    }
    try{
        d3dresult = d3dDevice_->CreateBuffer(&constDesc, nullptr, &worldCB_);
        if (FAILED(d3dresult))
        {
            throw _com_error(d3dresult);
        }
    }
    catch (_com_error & comEx){
        MessageBox(0, comEx.ErrorMessage(), "Error Creating World Matrix", MB_OK);
        return false;
    }

    viewMatrix_ = XMMatrixIdentity();
    projMatrix_ = XMMatrixPerspectiveFovLH(XM_PIDIV4, 800.0f / 600.0f, 0.01f, 100.0f);

    viewMatrix_ = XMMatrixTranspose(viewMatrix_);
    projMatrix_ = XMMatrixTranspose(projMatrix_);

    return true;
}

void UnloadContent()
{
    if (colorMapSampler_) colorMapSampler_->Release();
    if (colorMapView_) colorMapView_->Release();
    if (solidColorVS_) solidColorVS_->Release();
    if (solidColorPS_) solidColorPS_->Release();
    if (inputLayout_) inputLayout_->Release();
    if (vertexBuffer_) vertexBuffer_->Release();
    if (viewCB_) viewCB_->Release();
    if (projCB_) projCB_->Release();
    if (worldCB_) worldCB_->Release();

    colorMapSampler_ = 0;
    colorMapView_ = 0;
    solidColorVS_ = 0;
    solidColorPS_ = 0;
    inputLayout_ = 0;
    vertexBuffer_ = 0;
    viewCB_ = 0;
    projCB_ = 0;
    worldCB_ = 0;
}

void Render()
{
    if (d3dContext_ == 0)
        return;

    float clearColor[4] = { 0.0f, 0.0f, 0.25f, 1.0f };
    d3dContext_->ClearRenderTargetView(backBufferTarget_, clearColor);
    d3dContext_->ClearDepthStencilView(depthStencilView_, D3D11_CLEAR_DEPTH, 1.0f, 0);

    unsigned int nStride = sizeof(VertexPos);
    unsigned int nOffset = 0;

    d3dContext_->IASetInputLayout(inputLayout_);
    d3dContext_->IASetVertexBuffers(0, 1, &vertexBuffer_, &nStride, &nOffset);
    d3dContext_->IASetIndexBuffer(indexBuffer_, DXGI_FORMAT_R16_UINT, 0);
    d3dContext_->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);

    d3dContext_->VSSetShader(solidColorVS_, 0, 0);
    d3dContext_->PSSetShader(solidColorPS_, 0, 0);
    d3dContext_->PSSetShaderResources(0, 1, &colorMapView_);
    d3dContext_->PSSetSamplers(0, 1, &colorMapSampler_);

    XMMATRIX rotationMatrix;
    rotationMatrix = XMMatrixRotationRollPitchYaw(0.0f, 0.7f, 0.7f);
    XMMATRIX translationMatrix;
    translationMatrix = XMMatrixTranslation(0.0f, 0.0f, 0.6f);

    XMMATRIX worldMatrix;
    worldMatrix = rotationMatrix * translationMatrix;
    worldMatrix = XMMatrixTranspose(worldMatrix);

    d3dContext_->UpdateSubresource(worldCB_, 0, nullptr, &worldMatrix, 0, 0);
    d3dContext_->UpdateSubresource(viewCB_, 0, nullptr, &viewMatrix_, 0, 0);
    d3dContext_->UpdateSubresource(projCB_, 0, nullptr, &projMatrix_, 0, 0);

    d3dContext_->VSSetConstantBuffers(0, 1, &worldCB_);
    d3dContext_->VSSetConstantBuffers(1, 1, &viewCB_);
    d3dContext_->VSSetConstantBuffers(2, 1, &projCB_);

    d3dContext_->DrawIndexed(36, 0, 0);

    swapChain_->Present(0, 0);
}

private:
ID3D11VertexShader * solidColorVS_;
ID3D11PixelShader * solidColorPS_;

ID3D11InputLayout * inputLayout_;
ID3D11Buffer * vertexBuffer_;
ID3D11Buffer * indexBuffer_;

ID3D11ShaderResourceView * colorMapView_;
ID3D11SamplerState * colorMapSampler_;

ID3D11Buffer * viewCB_;
ID3D11Buffer * projCB_;
ID3D11Buffer * worldCB_;
XMMATRIX viewMatrix_;
XMMATRIX projMatrix_;
};

更新:所以,在启用 D3D 调试设备后,我在输出中得到了这个。实在想不通

D3D11 WARNING: Process is terminating. Using simple reporting. Please call ReportLiveObjects() at runtime for standard reporting.     [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING: Live Producer at 0x00F7F3F4, Refcount: 3. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING:  Live Object at 0x00F80218, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING:  Live Object at 0x0403A110, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING:  Live Object at 0x0403801C, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING:  Live Object at 0x04040304, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING:  Live Object at 0x0404054C, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING:  Live Object at 0x0404089C, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING:  Live Object at 0x04040AAC, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING:  Live Object at 0x04040CE0, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING:  Live Object at 0x04041394, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING:  Live Object at 0x040428E4, Refcount: 1. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING:  Live Object at 0x04043534, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING:  Live Object at 0x04043894, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING:  Live Object at 0x0404490C, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING:  Live Object at 0x0404F5CC, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING:  Live Object at 0x0405C7EC, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING:  Live Object at 0x0405B65C, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING:  Live Object at 0x04069D0C, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING:  Live Object at 0x0406701C, Refcount: 1. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING:  Live Object at 0x040653AC, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING:  Live Object at 0x0408243C, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING:  Live Object at 0x04036FFC, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING:  Live Object at 0x0403719C, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING:  Live Object at 0x04085C04, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING:  Live Object at 0x0408656C, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING:  Live Object at 0x040618CC, Refcount: 0. [ STATE_CREATION WARNING #0: UNKNOWN]
D3D11 WARNING: Live                         Object :     25 [ STATE_CREATION WARNING #0: UNKNOWN]
DXGI WARNING: Live Producer at 0x00F4AE50, Refcount: 4. [ STATE_CREATION WARNING #0: ]
DXGI WARNING:   Live Object at 0x00F4D480, Refcount: 2. [ STATE_CREATION WARNING #0: ]
DXGI WARNING: Live                         Object :      1 [ STATE_CREATION WARNING #0: ]

更新:我的 XMMATRIX 现在已正确对齐。立方体仍然是不可见的。我也找不到 Visual Studio 图形调试器。根据我的研究,我知道它应该在哪里,但它不在那里。

这里的关键问题是您使用的 XMMATRIX 需要 16 字节对齐作为堆分配的 class 成员,但是 new 在 x86(32 位)上) 默认只提供8字节对齐。

这正是 XMFLOAT4X4XMLoadFloat4x4XMStoreFloat4x4 要解决的问题。

XMMATRIX vm = XMMatrixIdentity();
XMMATRIX pm = XMMatrixPerspectiveFovLH(XM_PIDIV4, 800.0f / 600.0f, 0.01f, 100.0f);

XMStoreFloat4x4( &viewMatrix_, XMMatrixTranspose(vm) );
XMStoreFlaot4x4( &projMatrix_, XMMatrixTranspose(pm) );

XMFLOAT4X4 viewMatrix_;
XMFLOAT4X4 projMatrix_;

或者您可以切换到默认为 16 字节对齐的 x64(64 位)本机编码,您可以使用 XMMATRIXXMVECTOR 作为 class 成员在大多数情况下无需担心对齐问题。

MSDN 在 DirectXMath Programming Guide. See Getting Started, Type Usage Guidelines

中解决了这个问题

您还应该看看 SimpleMath wrapper in the DirectX Tool Kit。这些类型使用 C++ "magic":

处理这种 load/store 行为
#include <SimpleMath.h>

XMMATRIX vm = XMMatrixIdentity();
XMMATRIX pm = XMMatrixPerspectiveFovLH(XM_PIDIV4, 800.0f / 600.0f, 0.01f, 100.0f);

viewMatrix_ = XMMatrixTranspose(vm);
projMatrix_ = XMMatrixTranspose(pm); 

DirectX::SimpleMath::Matrix viewMatrix_;
DirectX::SimpleMath::Matrix projMatrix_;

如果您还没有看过这些,请务必阅读:

编辑 对于 Windows 桌面 DirectX 开发,如果您尚未使用 VS 2013 Professional+,则应使用 Visual Studio 2013 Community Edition。它包括 VS 图形调试器,而 VS 2013 Express for Windows Desktop 没有。

好吧,我终于找到了解决办法。由于 VS 图形调试器没有给出任何错误,我不得不做一些实际有效的随机操作。

我不得不在任何东西变得可见之前创建一个光栅器状态。原来我的立方体也是里里外外,所以我将 D3D11_RASTERIZER_DESC 的 FrontCounterClockWise 字段设置为 false。我还通过在 Render() 中设置以下行来缩小相机:

 translationMatrix = XMMatrixTranslation(0.0f, 0.0f, 4.0f);

我还创建了一个剪刀矩形;不知道和解决方案有没有关系

这是我找到所有相关资料的地方: This MSDN page.

感谢大家的帮助,try-catch的废话也用内联函数解决了