2

I am trying to load a basic texture (1024x1024) and fill it with a solid color:

unsigned char *texArray = (unsigned char *)malloc(4 * 1024 * 1024 * sizeof(unsigned char));

for (int i = 0; i < 1024 * 1024 * 4; i++) {
    texArray[i] = (unsigned char)125;
}

D3D11_TEXTURE2D_DESC desc;
desc.Width = 1024;
desc.Height = 1024;
desc.MipLevels = desc.ArraySize = 1;
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
desc.SampleDesc.Count = 1;
desc.Usage = D3D11_USAGE_DYNAMIC;
desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
desc.MiscFlags = 0;

ID3D11Texture2D *pTexture = NULL;

D3D11_SUBRESOURCE_DATA TexInitData;
ZeroMemory(&TexInitData, sizeof(D3D11_SUBRESOURCE_DATA));
TexInitData.pSysMem = texArray;
TexInitData.SysMemPitch = static_cast<UINT>( 1024*4 );
TexInitData.SysMemSlicePitch = static_cast<UINT>( 4 * 1024 * 1024 * sizeof(unsigned char) );

GraphicsBase.DXDevice->CreateTexture2D( &desc, &TexInitData, &pTexture );
GraphicsBase.DXDevice->CreateShaderResourceView(pTexture, NULL, &DXTextureContent);

When I use DXTextureContent, my textures appear all white. I am filling the texture with 125/125/125/125 for RGBA. Am I missing anything here?

Grapes
  • 2,473
  • 3
  • 26
  • 42

2 Answers2

3

You should always develop DX11 application with the D3D11_CREATE_DEVICE_DEBUG flag set at device creation.

With it, each D3D call are verbose as possible and your CreateTexture2D would had fail here with an invalid parameter error code.

D3D11 introduced some class helper to initialize resource description, with a CD3D11_TEXTURE2D_DESC, you are guarantee to not have uninitialized member.

galop1n
  • 8,573
  • 22
  • 36
1

You haven't set the SysMemPitch of TexInitData for a start. A pitch of 0 is probably not what you're after and could perhaps be affecting the way it fills the texture. You'll probably want to set this member to (1024 * 4).

Adam Miles
  • 3,504
  • 17
  • 15
  • Hello Adam, I've updated my code to include `Pitch` and `SlicePitch`, it seems that the texture is still white. – Grapes Jul 26 '13 at 20:06
  • Hello Adam, after further investigation, I found that `desc.SampleDesc.Quality` was not set, so i would get random values in billions. After settings `desc.SampleDesc.Quality` to 0 it worked! – Grapes Jul 26 '13 at 20:18
  • Ah yes, didn't spot that one, glad you figured it out. Although some values for SampleDesc.Quality other than 0 are valid, it would surprise me if the debug layer hadn't had something to say about it, make sure you always run with it turned on in Debug builds if you aren't already. – Adam Miles Jul 26 '13 at 21:25