Tell me more ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I've been trying to load textures into a texture array, but I've hit a wall with my knowledge of subresources. Any time I try loading the data, I just get junk as output. The code below is only set up for one item, so I could focus on just getting one texture right using the array method as seen at this answer Loading A Texture2D Array. The texture I'm loading is a 16 by 16 bit map (and works with using the resource directly from the CreateTextureFromFile function, but that's only one item), any help on how I'm not using subresources correctly would be much appreciated.

void Cd3d11Texture2D::addTexture(wchar_t *  s_textureFileName, int i_slot)
setFile(s_textureFileName);

D3DX11CreateTextureFromFile( d3d11p_environment->d3d11p_dev, wcs_filenameTextureColorMap, NULL, NULL, &d3d11p_resourceTexture, NULL );

ID3D11Texture2D                     *d3d11p_texture;
    d3d11p_resourceTexture->QueryInterface( __uuidof( ID3D11Texture2D ), (LPVOID*)&d3d11p_texture );
    d3d11p_texture->GetDesc(&d3d11_textureDesc);

    if (d3d11_textureDesc.MipLevels > 4)  d3d11_textureDesc.MipLevels -= 4;

    d3d11_textureDesc.Width = 16;
    d3d11_textureDesc.Height = 16;
    d3d11_textureDesc.MipLevels = 1;
    d3d11_textureDesc.ArraySize = 1;
    d3d11_textureDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
    d3d11_textureDesc.SampleDesc.Count = 1;
    d3d11_textureDesc.SampleDesc.Quality = 0;
    d3d11_textureDesc.Usage = D3D11_USAGE_IMMUTABLE;
    d3d11_textureDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
    d3d11_textureDesc.CPUAccessFlags = 0;
    d3d11_textureDesc.MiscFlags = 0;

    d3d11p_subResourceData.pSysMem = d3d11p_resourceTexture;
    d3d11p_subResourceData.SysMemPitch = (UINT) (16 * 4);
    d3d11p_subResourceData.SysMemSlicePitch = (UINT) (16 * 16 * 4);

d3d11p_environment->d3d11p_dev->CreateTexture2D(&d3d11_textureDesc,&d3d11p_subResourceData,&d3d11p_textureBuffer);

D3D11_SHADER_RESOURCE_VIEW_DESC     d3d11_shaderResourceViewDesc;
    ZeroMemory( &d3d11_shaderResourceViewDesc, sizeof(d3d11_shaderResourceViewDesc));

    d3d11_shaderResourceViewDesc.ViewDimension          = D3D11_SRV_DIMENSION_TEXTURE2DARRAY;
    d3d11_shaderResourceViewDesc.Format     = d3d11_textureDesc.Format;
    d3d11_shaderResourceViewDesc.Texture2DArray.MostDetailedMip = d3d11_textureDesc.MipLevels - 1;
    d3d11_shaderResourceViewDesc.Texture2DArray.MipLevels       = d3d11_textureDesc.MipLevels;
    d3d11_shaderResourceViewDesc.Texture2DArray.FirstArraySlice = 0;
    d3d11_shaderResourceViewDesc.Texture2DArray.ArraySize       = i_countTexture;

d3d11p_environment->d3d11p_dev->CreateShaderResourceView( d3d11p_textureBuffer, &d3d11_shaderResourceViewDesc, &d3d11p_shaderResourceViewTexture);
share|improve this question
The first thing I'd suggest is to check the return code from every DirectX function. – CiscoIPPhone Jul 15 '11 at 7:28
Yeah, some information on success/failure of the function calls would be useful. Also, why the if (mipLevel > 4) mipLevels -=4? – njdw Jul 15 '11 at 17:43
Thanks for the comments, I did make a edit to the original post to mke thing a little clearer, that I'm not sure went in before. Everything is returning a successful result (CreateTextTureFromFile, CreateTexture2D, CreateShaderResourceView), which makes it all the more frustrating. As for the mipLevel>4, sometimes with directx when y9ou autoload a image, it'll give you 5 mip levels when you really only have 1, it's just a fix. – RandomEvents Jul 15 '11 at 18:13
You should instead request that the loader not autogenerate mip levels; that isn't a bug. – Josh Petrie Oct 5 '11 at 15:50

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.