【问题标题】:DX11 Programmatically filling a 3D TextureDX11 以编程方式填充 3D 纹理
【发布时间】:2013-05-03 00:41:31
【问题描述】:

我正在尝试使用 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。

【问题讨论】:

    标签: c++ textures memcpy directx-11


    【解决方案1】:

    好吧,我终于弄清楚我做错了什么。我必须将 rowStart 和 depStart 除以 4 才能从字节转换为浮点数,它修复了所有问题。

    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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-05-17
      • 1970-01-01
      • 2015-12-13
      • 1970-01-01
      • 2019-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-15
      相关资源
      最近更新 更多