DX11以编程方式填充3D纹理

时间:2013-04-30 13:52:02

标签: c++ textures memcpy directx-11

我正在尝试使用MSDN上提到的map,write和unmap方法用任意数据(-1或1)填充3D纹理,但是我还没有找到任何实际的代码示例要做到这一点,使用行和深度节距等。

纹理描述是:

D3D11_TEXTURE3D_DESC m_3DTexDesc;
m_3DTexDesc.Height = 33;
m_3DTexDesc.Width = 33;
m_3DTexDesc.Depth = 33;
m_3DTexDesc.MipLevels = 1;
m_3DTexDesc.Format = DXGI_FORMAT_R32_FLOAT;
m_3DTexDesc.Usage = D3D11_USAGE_DYNAMIC;
m_3DTexDesc.BindFlags = D3D10_BIND_SHADER_RESOURCE;
m_3DTexDesc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE;
m_3DTexDesc.MiscFlags = 0;

result = p_device->CreateTexture3D(&m_3DTexDesc, NULL, &m_voxels);

我用来填充纹理的代码是:

D3D11_MAPPED_SUBRESOURCE mappedTex;

result = p_context->Map(m_voxels, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedTex);

float* pVoxels = (float*)mappedTex.pData;
// DepthPitch * 33 + RowPitch * 33 + 33;
float* pTexels = (float*)malloc(287265*4);

for( UINT dep = 0; dep < m_3DTexDesc.Depth; dep++ )
{
    UINT depStart = mappedTex.DepthPitch*dep;
    for( UINT row = 0; row < m_3DTexDesc.Width; row++ )
    {
        UINT rowStart = mappedTex.RowPitch*row;
        for( UINT col = 0; col < m_3DTexDesc.Height; col++ )
        {
            if(pos.y + col < 0.0f) pTexels[depStart + rowStart + col] = -1.0f;
            else pTexels[depStart + rowStart + col] = 1.0f;
        }
    }
}
memcpy(pVoxels, (void*)pTexels, 287265);

p_context->Unmap(m_voxels, 0 );

free((void*)pTexels);

使用这种方法,纹理中的一些数据很好,但其余的都是错误的。有人可以解释使用这些指针和音高来逐步完成纹理并初始化它的正确方法吗?

参考DepthPitch = 8448和RowPitch = 256。

1 个答案:

答案 0 :(得分:1)

好的,我终于弄明白我做错了什么。我不得不将rowStart和depStart除以4,从byte转换为float,并修复了所有内容。

UINT depStart = mappedTex.DepthPitch/4*dep;
for( UINT row = 0; row < m_3DTexDesc.Width; row++ )
{
    UINT rowStart = mappedTex.RowPitch/4*row;
    for( UINT col = 0; col < m_3DTexDesc.Height; col++ )
    {
        if(pos.y + col < 0.0f) pTexels[depStart + rowStart + col] = -1.0f;
        else pTexels[depStart + rowStart + col] = 1.0f;
    }
}