天天看点

D3D11修改纹理数据

记录下来自己下次又愚蠢了的时候看看

D3D11修改纹理数据

目的:创建纹理,修改问题数据--渲染输出

问题:修改纹理数据不成功!

D3D11修改纹理数据

原因:创建纹理时使用的格式不对DXGI_FORMAT_R8G8B8A8_UNORM----DXGI_FORMAT_R32_UINT;

创建纹理描述代码:

D3D11_TEXTURE2D_DESC textureDesc;

// Initialize the  texture description.

ZeroMemory(&textureDesc, sizeof(textureDesc));

// Setup the texture description.

// We will have our map be a square

// We will need to have this texture bound as a render target AND a shader resource

textureDesc.Width = m_nWidth;

textureDesc.Height = m_nHeight;

textureDesc.MipLevels = 1;

textureDesc.ArraySize = 1;

textureDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;//原本使用DXGI_FORMAT_R32_UINT格式,一直不成功

D3D11修改纹理数据

textureDesc.SampleDesc.Count = 1;

textureDesc.Usage = D3D11_USAGE_DYNAMIC;//D3D11_USAGE_DEFAULT;

textureDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;///< D3D11_BIND_RENDER_TARGET | 

textureDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;

textureDesc.MiscFlags = 0;

修改纹理数据代码:

HRESULT hr = S_OK;

D3D11_MAPPED_SUBRESOURCE MappedResource;

hr = m_pImmediateContext->Map(m_pBlendTexture,0, D3D11_MAP_WRITE_DISCARD, 0, &MappedResource);

if(FAILED(hr)) return false;

BYTE* pRow = (BYTE*)MappedResource.pData;

for (int j=0; j<m_nHeight; j++)

{

int nOffset=0;

for (int i=0; i<m_nWidth; i++)

{

const BYTE pbyte = (BYTE)255;

memcpy(pRow+nOffset+1, &pbyte, sizeof(BYTE));

nOffset+=4;

}

pRow += MappedResource.RowPitch;

}

m_pImmediateContext->Unmap(m_pBlendTexture, 0);

return hr;

MSDN描述:

DXGI_FORMAT_R8G8B8A8_UNORM

A four-component, 32-bit unsigned-integer format.

DXGI_FORMAT_R8G8B8A8_UINT

A four-component, 32-bit unsigned-integer format.

咋一看,两个都一样,不过还有下文当初我没看到

D3D11修改纹理数据
,懒人啊。。。。。
_UINT Unsigned integer. For instance, a 3-bit UINT represents the values 0, 1, 2, 3, 4, 5, 6, 7.
_UNORM Unsigned normalized integer; which is interpreted in a resource as an unsigned integer, and is interpreted in a shader as an unsigned normalized floating-point value in the range [0, 1]. All 0's maps to 0.0f, and all 1's maps to 1.0f. A sequence of evenly spaced floating-point values from 0.0f to 1.0f are represented. For instance, a 2-bit UNORM represents 0.0f, 1/3, 2/3, and 1.0f.

带_UINT后缀的为无符号整形,如3位无符号可以表示的值有:000-->0, 001-->1,  ......111-->7;

而带_UNORM后缀的无符号整形,代表的是归一化的,如2位无符号可以表示的数:00-->0.0f, 01-->1/3.0f, 10-->2/3.0f, 11-->3/3.0f==1.0f;

下面的来自:http://www.gamedev.net/topic/546150-dxgi_format_r8g8b8a8_unorm-vs-uint/

modus   Members   -  Reputation: 148

Like 0Likes Like

Posted 01 September 2009 - 10:56 AM

I think the only formats currently supported for display are 

- R8G8B8A8_UNORM

- R8G8B8A8_UNORM_SRGB

- R10G10B10A2_UNORM

- R16G16B16A16_FLOAT

You can call CheckFormatSupport with D3D10_FORMAT_SUPPORT_DISPLAY on your device for a given format.