1
\$\begingroup\$

I'm trying to create immutable texture in d3d11 so I want to use subresourceData, every tutorial on textures creates them with 2nd argument being null and after that updates subresource but I want to create it using the 2nd argument. This code works:

hResult = m_device->CreateTexture2D(&textureDesc, NULL, &m_cubeTexture); if (FAILED(hResult)) { return false; } int rowPitch = textureWidth * 4; m_deviceContext->UpdateSubresource(m_cubeTexture, 0, NULL, &(decodedTexture[0]), rowPitch, rowPitch * textureHeight); 

And this does not:

int rowPitch = textureWidth * 4; D3D11_SUBRESOURCE_DATA tSData; tSData.pSysMem = &(decodedTexture[0]); tSData.SysMemPitch = rowPitch; tSData.SysMemSlicePitch = rowPitch * textureHeight; hResult = m_device->CreateTexture2D(&textureDesc, &tSData, &m_cubeTexture); if (FAILED(hResult)) { return false; } 

DirectX11 debug layer outputs this:

D3D11 ERROR: ID3D11Device::CreateTexture2D: pInitialData[3].SysMemPitch cannot be 0 [ STATE_CREATION ERROR #100: CREATETEXTURE2D_INVALIDINITIALDATA] D3D11 ERROR: ID3D11Device::CreateTexture2D: pInitialData[5].pSysMem cannot be NULL. [ STATE_CREATION ERROR #100: CREATETEXTURE2D_INVALIDINITIALDATA] 

Which I don't understand since it's not even an array and texture array size is set to 1

\$\endgroup\$
3
  • \$\begingroup\$ If it's not an array texture then you must have miplevels specified (each miplevel is also a subresource). What is the value of textureDesc.MipLevels? Anything other than 1 will cause this. \$\endgroup\$ Commented Sep 10, 2017 at 16:50
  • \$\begingroup\$ For DDSTextureLoader and WICTextureLoader in the DirectX Tool Kit I use initData initialization. The source code for these should help. \$\endgroup\$ Commented Sep 11, 2017 at 21:55
  • \$\begingroup\$ This thread has the answer: gamedev.net/forums/topic/… \$\endgroup\$ Commented Nov 25, 2017 at 1:44

1 Answer 1

1
\$\begingroup\$

When you create texture as D3D11_USAGE_IMMUTABLE set MipLevels to 1.

D3D11_TEXTURE2D_DESC textureDesc= {}; textureDesc.Width= textureWidth; textureDesc.Height= textureHeight; textureDesc.MipLevels= 1; /// !!! textureDesc.ArraySize= 1; textureDesc.Format= DXGI_FORMAT_R8G8B8A8_UNORM; textureDesc.SampleDesc.Count= 1; textureDesc.Usage= D3D11_USAGE_IMMUTABLE; textureDesc.BindFlags= D3D11_BIND_SHADER_RESOURCE; textureDesc.CPUAccessFlags= 0; textureDesc.MiscFlags= 0; D3D11_SUBRESOURCE_DATA tSData= {}; tSData.pSysMem = &(decodedTexture[0]); tSData.SysMemPitch = rowPitch; tSData.SysMemSlicePitch = 0; hResult= m_device->CreateTexture2D( &textureDesc, &tSData, &m_cubeTexture); 

Have in mind that if you create D3D11_USAGE_IMMUTABLE texture like this, you won't be able to call GenerateMips on it.

\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.