C++ : DirectX Texture is Misaligned/Shifted

标签 c++ directx directx-11

我正在尝试渲染一个带有纹理的正方形。有谁知道为什么这个纹理看起来偏移或未对齐?

它应该是这样的:http://imgur.com/siCQXXT

imgur

问题如下所示:http://imgur.com/rj6tHcX

imgur

auto createVSTask = loadVSTask.then([this](const std::vector<byte>& fileData) {
    DX::ThrowIfFailed(
        m_deviceResources->GetD3DDevice()->CreateVertexShader(
            &fileData[0],
            fileData.size(),
            nullptr,
            &m_vertexShader
        )
    );

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

    DX::ThrowIfFailed(
        m_deviceResources->GetD3DDevice()->CreateInputLayout(
            vertexDesc,
            ARRAYSIZE(vertexDesc),
            &fileData[0],
            fileData.size(),
            &m_inputLayout
        )
    );
});

auto createPSTask = loadPSTask.then([this](const std::vector<byte>& fileData) {
    DX::ThrowIfFailed(
        m_deviceResources->GetD3DDevice()->CreatePixelShader(
            &fileData[0],
            fileData.size(),
            nullptr,
            &m_pixelShader
        )
    );

    CD3D11_BUFFER_DESC constantBufferDesc(sizeof(ModelViewProjectionConstantBuffer), D3D11_BIND_CONSTANT_BUFFER);
    DX::ThrowIfFailed(
        m_deviceResources->GetD3DDevice()->CreateBuffer(
            &constantBufferDesc,
            nullptr,
            &m_constantBuffer
        )
    );
});

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

    const VertexPositionColor cubeVertices[] =
    {
        { DirectX::XMFLOAT3(-1.0f, -1.0f, 0.0f),  DirectX::XMFLOAT3(0.0f, 1.0f, 0.0f), DirectX::XMFLOAT2(0.0f, 0.0f) },
        { DirectX::XMFLOAT3(1.0f, -1.0f, 0.0f), DirectX::XMFLOAT3(0.0f, 1.0f, 0.0f), DirectX::XMFLOAT2(1.0f, 0.0f) },
        { DirectX::XMFLOAT3(1.0f, 1.0f, 0.0f), DirectX::XMFLOAT3(0.0f, 1.0f, 0.0f), DirectX::XMFLOAT2(1.0f, 1.0f) },
        { DirectX::XMFLOAT3(-1.0f, 1.0f, 0.0f), DirectX::XMFLOAT3(0.0f, 1.0f, 0.0f), DirectX::XMFLOAT2(0.0f, 1.0f) },
    };

    static const unsigned short cubeIndices[] =
    {
        0, 1, 2,
        0, 2, 3
    };

    m_indexCount = ARRAYSIZE(cubeIndices);

    D3D11_SUBRESOURCE_DATA vertexBufferData = { 0 };
    vertexBufferData.pSysMem = cubeVertices;
    vertexBufferData.SysMemPitch = 0;
    vertexBufferData.SysMemSlicePitch = 0;
    CD3D11_BUFFER_DESC vertexBufferDesc(sizeof(cubeVertices), D3D11_BIND_VERTEX_BUFFER);
    DX::ThrowIfFailed(
        m_deviceResources->GetD3DDevice()->CreateBuffer(
            &vertexBufferDesc,
            &vertexBufferData,
            &m_vertexBuffer
        )
    );

    D3D11_SUBRESOURCE_DATA indexBufferData = { 0 };
    indexBufferData.pSysMem = cubeIndices;
    indexBufferData.SysMemPitch = 0;
    indexBufferData.SysMemSlicePitch = 0;
    CD3D11_BUFFER_DESC indexBufferDesc(sizeof(cubeIndices), D3D11_BIND_INDEX_BUFFER);
    DX::ThrowIfFailed(
        m_deviceResources->GetD3DDevice()->CreateBuffer(
            &indexBufferDesc,
            &indexBufferData,
            &m_indexBuffer
        )
    );
});

auto loadTDTask = DX::ReadDataAsync(m_textureFile);

auto createSubresourceTask = loadTDTask.then([=](std::vector<byte>& textureData) {

    D3D11_SUBRESOURCE_DATA textureSubresourceData = { 0 };
    textureSubresourceData.pSysMem = &textureData[0];
    textureSubresourceData.SysMemPitch = 1024;
    textureSubresourceData.SysMemSlicePitch = 0;

    D3D11_TEXTURE2D_DESC textureDesc = { 0 };
    textureDesc.Width = 256;
    textureDesc.Height = 256;
    textureDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
    textureDesc.Usage = D3D11_USAGE_DEFAULT;
    textureDesc.CPUAccessFlags = 0;
    textureDesc.ArraySize = 1;
    textureDesc.SampleDesc.Count = 1;
    textureDesc.SampleDesc.Quality = 0;
    textureDesc.MiscFlags = D3D11_RESOURCE_MISC_GENERATE_MIPS;
    textureDesc.MipLevels = 0;
    textureDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET;

    DX::ThrowIfFailed(m_d3dDevice->CreateTexture2D(
        &textureDesc,
        nullptr,
        &m_texture
    );

    if (m_texture != NULL)
    {
        D3D11_SHADER_RESOURCE_VIEW_DESC textureViewDesc;
        ZeroMemory(&textureViewDesc, sizeof(textureViewDesc));
        textureViewDesc.Format = textureDesc.Format;
        textureViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
        textureViewDesc.Texture2D.MipLevels = -1;
        textureViewDesc.Texture2D.MostDetailedMip = 0;

        DX::ThrowIfFailed(
            m_d3dDevice->CreateShaderResourceView(
            m_texture.Get(),
            &textureViewDesc,
            &m_textureView
            )
        );

        context->UpdateSubresource(m_texture.Get(), 0, nullptr, &textureData[0], textureSubresourceData.SysMemPitch, textureDesc.Width);
        context->GenerateMips(m_textureView.Get());
    }
}

D3D11_SAMPLER_DESC samplerDesc;
ZeroMemory(&samplerDesc, sizeof(samplerDesc));
samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
samplerDesc.MaxAnisotropy = 0;
samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
samplerDesc.MipLODBias = 0.0f;
samplerDesc.MinLOD = 0;
samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
samplerDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
samplerDesc.BorderColor[0] = 0.0f;
samplerDesc.BorderColor[1] = 0.0f;
samplerDesc.BorderColor[2] = 0.0f;
samplerDesc.BorderColor[3] = 0.0f;

DX::ThrowIfFailed(
    m_d3dDevice->CreateSamplerState(
    &samplerDesc,
    &m_sampler
    )
);

最佳答案

首先,您确认钳位实验没有状态或几何问题,其次,您说您使用的是 DDS 图像,这是关键。

根据你的代码,图像是 256 宽 RGBA8,因为石头是它的 1/8,这意味着它们覆盖 32*4 = 128 字节。足够接近了,DDS header当你没有 dx10 block 时是 124 字节,它解释了为什么图像是这样偏移的。

您所要做的就是跳过 header ,仅将图像数据传递给 UpdateSubResource。我请你看DDS reference了解文件的布局,以便您可以读取大小、格式,正确地跳到数据的开头,还可以利用 DDS 存储压缩格式,并预先包含 mip 映射以不使用 GenerateMips 这是不好的做法。

关于C++ : DirectX Texture is Misaligned/Shifted,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39922374/

相关文章:

c++ - Qt线程和循环问题

c++ - 为什么 getline 不移动到下一行?

c# - 如何在透明窗口中绘制透明的 DirectX 内容?

directx-11 - 在 SharpDX 中调整 DXGI 资源或 Texture2D 的大小

c++ - 嵌套循环导致复杂性效率降低?

c++ - Qt 使用哪种图形技术来呈现其自定义 UI?

c++ - 计算具有旋转前位置和旋转角度的点的绝对位置

visual-c++ - Directx11 SDK 6月(2010) VC++ 2010 上的初始化

c# - 检查安装了哪个版本的 DirectX

c++ - 如何通过数字获得长长的面具?