Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I have a DX10 program, where I create an array of 3 16x16 textures, then map, read, and unmap each subresource in turn. I use a single mip level, set resource usage to staging and CPU access to read. Now, here is the problem:

  • Subresource 0 contains 1024 bytes, pitch 64, as expected.
  • Subresource 1 contains 512 bytes, pitch 64.
  • Subresource 2 contains 256 bytes, pitch 64.

I expect all three to be the same size. Debugging output is enabled, but not reporting any warnings or errors. Am I missing something, or might this be some sort of driver issue?

Here is the code. The language is Nemerle, but C# and C++ would look almost the same. I have looked through the generated code, and am fairly confident the problem is not language-related.

def cpuTexture = Texture2D
    ( device
    , Texture2DDescription() <-
    {
        Width     = 16;
        Height    = 16;
        MipLevels = 1;
        ArraySize = 3;
        Format    = Format.R32_Float;
        Usage     = ResourceUsage.Staging;
        CpuAccessFlags    = CpuAccessFlags.Read;
        SampleDescription = SampleDescription(count = 1, quality = 0);
    }
    );
foreach (subresource in [0 .. 2])
{
    def data = cpuTexture.Map(subresource, MapMode.Read, MapFlags.None);
    Console.WriteLine($"subresource $subresource");
    Console.WriteLine($"length = $(data.Data.Length)");
    Console.WriteLine($"pitch  = $(data.Pitch)");
    cpuTexture.Unmap(subresource);
}
share|improve this question
    
Can you post your code for initializing and mapping the texture array? –  Nathan Reed Apr 20 '12 at 0:11
    
@NathanReed, sure, added the code. –  Don Reba Apr 20 '12 at 4:05
    
Thanks. Your code looks right as far as I can see, though, and I checked against the D3D10 docs as well. I'm afraid I have no idea what's going on. :( –  Nathan Reed Apr 20 '12 at 4:24
add comment

1 Answer

You're forgetting to consider mipLevels. When creating a texture, usually miplevels will be generated so you will not find texture element 2 at subresource 2 (there is a mipLevel in it, which is half of original texture and so on).

Use this function to get the right subresource index.

    ID3D11Texture2D *cpuTexture = NULL;

    D3D11_TEXTURE2D_DESC cputextdesc;
    memset(&cputextdesc,0,sizeof(cputextdesc));

    cputextdesc.Width = cputextdesc.Height = 16;
    cputextdesc.ArraySize = 3;
    cputextdesc.MipLevels = 1;
    cputextdesc.Format = DXGI_FORMAT_R32_FLOAT;
    cputextdesc.Usage = D3D11_USAGE_STAGING;
    cputextdesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
    cputextdesc.SampleDesc.Count = 1;
    cputextdesc.SampleDesc.Quality = 0;

    device->CreateTexture2D(&cputextdesc,NULL,&cpuTexture);

    int text1 = D3D11CalcSubresource(0,0,1);
    int text2 = D3D11CalcSubresource(0,1,1);
    int text3 = D3D11CalcSubresource(0,2,1);

    D3D11_MAPPED_SUBRESOURCE mres1,mres2,mres3;

    icontext->Map(cpuTexture,text1,D3D11_MAP_READ,0,&mres1);
    icontext->Map(cpuTexture,text2,D3D11_MAP_READ,0,&mres2);
    icontext->Map(cpuTexture,text3,D3D11_MAP_READ,0,&mres3);

    // (assert row and depth pitch are equals)

    icontext->Unmap(cpuTexture,text1);
    icontext->Unmap(cpuTexture,text2);
    icontext->Unmap(cpuTexture,text3);
share|improve this answer
    
There is only a single mip level. –  Don Reba Jun 13 '12 at 21:04
    
I modified my answer with a code example. 3 subresource have the same row and depth pitch. –  Vincenzo Jun 14 '12 at 6:48
1  
Still incorrect because "MipLevels = 1" in the OP, and besides - if it were mipmapping, the lengths would be quartering, not halving, for each subresource (the pitch would be expected to - but may not always - halve). I suspect it's more likely a problem in the calculation of "data.Data.Length" in the OP's environment. –  Jimmy Shelter Sep 12 '12 at 15:37
add comment

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.