简体   繁体   English

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

[英]DX11 Programmatically filling a 3D Texture

I'm trying to fill a 3D texture with arbitrary data (either -1 or 1) using the map, write and unmap method mentioned on the MSDN, but I haven't been able to find any actual code examples of how to do this, using the row and depth pitches etc. 我正在尝试使用MSDN上提到的map,write和unmap方法用任意数据(-1或1)填充3D纹理,但是我还没有找到任何实际的代码示例来完成此操作,使用行距和深度间距等。

The texture description is: 纹理描述为:

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

The code I'm using to fill the texture is: 我用来填充纹理的代码是:

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

Using this method, some of the data in the texture is fine but the rest is drastically wrong. 使用此方法,纹理中的某些数据很好,但其余数据则完全错误。 Can someone explain the correct way to use these pointers and pitches to step through the texture and initialise it? 有人可以解释使用这些指针和间距来逐步遍历并初始化纹理的正确方法吗?

For reference DepthPitch = 8448 and RowPitch = 256. 供参考,DepthPitch = 8448和RowPitch = 256。

Okay I finally worked out what I was doing wrong. 好吧,我终于弄清楚我做错了什么。 I had to divide rowStart and depStart by 4 to convert from byte to float and it fixed everything. 我必须将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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM