Loading a Texture2D Array in DirectX11

matth
  • Loading a Texture2D Array in DirectX11 matth

    How does one create a Texture2D Array in DirectX11? The texture I am trying to create is not multisampled, and I want only one mip level. I can't find a working example of a Texture2D Array. I have a buffer of 2D images that I want to use as a Texture2DArray in a shader.

    The following code snippet works when sTexDesc.ArraySize=1 (as a Texture2D). With sTexDesc.ArraySize=2 the subsequent call to CreateTexture2D crashes with this Access violation inside DirectX:

    Unhandled exception at 0x000007fef5d6771a in test.exe: 0xC0000005: Access violation reading location 0xffffffffffffffff.

    With larger values of ArraySize CreateTexture2D returns E_INVALIDARG. ubImageStorage contains valid data. As a Texture2D I can add multiples of the slice offset to this buffer address when assigning pSysMem and read each slice from a shader. What am I missing? I have tried all combinations of bind and usage flags and various values for MipLevels with no success.

    D3D11_TEXTURE2D_DESC sTexDesc;
    sTexDesc.Width = 1024;
    sTexDesc.Height = 1024;
    sTexDesc.MipLevels = 1;
    sTexDesc.ArraySize = 2;
    sTexDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
    sTexDesc.SampleDesc.Count = 1;
    sTexDesc.SampleDesc.Quality = 0;
    sTexDesc.Usage = D3D11_USAGE_IMMUTABLE;
    sTexDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
    sTexDesc.CPUAccessFlags = 0;
    sTexDesc.MiscFlags = 0;
    
    D3D11_SUBRESOURCE_DATA sSubData;
    sSubData.pSysMem = ubImageStorage;
    sSubData.SysMemPitch = (UINT) (1024 * 4);
    sSubData.SysMemSlicePitch = (UINT) (1024 * 1024 * 4);
    
    ID3D11Texture2D* pTexture;
    
    hr = m_pd3dDevice->CreateTexture2D(&sTexDesc,&sSubData,&pTexture);
    

    Here are the relevant pages from MSDN:
    ID3D11Device::CreateTexture2D Method
    D3D11_TEXTURE2D_DESC Structure

  • (Note: This is the answer originally provided in the question body by the OP).

    Problem solved. I needed to create an array of D3D11_SUBRESOURCE_DATA elements, one for each element in the Texture2DArray. Like this:

    D3D11_TEXTURE2D_DESC sTexDesc;
    sTexDesc.Width = m_uTextureWidth;
    sTexDesc.Height = m_uTextureHeight;
    sTexDesc.MipLevels = 1;
    sTexDesc.ArraySize = 16;
    sTexDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
    sTexDesc.SampleDesc.Count = 1;
    sTexDesc.SampleDesc.Quality = 0;
    sTexDesc.Usage = D3D11_USAGE_IMMUTABLE;
    sTexDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
    sTexDesc.CPUAccessFlags = 0;
    sTexDesc.MiscFlags = 0;
    
    D3D11_SUBRESOURCE_DATA *sSubData = new D3D11_SUBRESOURCE_DATA[16];
    for(int i=0; i<16; i++) {
        sSubData[i].pSysMem = ubImageStorage;
        sSubData[i].SysMemPitch = (UINT) (1024 * 4);
        sSubData[i].SysMemSlicePitch = (UINT) (1024 * 1024 * 4);
    }
    
    ID3D11Texture2D* pTexture;
    
    hr = m_pd3dDevice->CreateTexture2D(&sTexDesc,sSubData,&pTexture);
    
    delete [] sSubData;
    

    Note that I've replaced the variables in the code above with constants, but allocated the D3D11_SUBRESOURCE_DATA array dynamically because its size is unknown at compile time.

    Why would you ever have different slices with different pitches? Is that even really supported? That seems really strange.

Tags
c++ directx
Related questions and answers
  • in the shader code) for( int n = 0; n < vShaderArgs.size(); n ++) glBindAttribLocation( m_nProgramId, n, vShaderArgs[n].sFieldName.c_str() ); // Create and bind to a vertex array object, which... into a shader program, for use with glDrawArrays. My vertex data already exists in a VertexBufferObject that uses the following data structure to create a vertex buffer: class CustomVertex { public... ); glDisableClientState( GL_COLOR_ARRAY ); glBindBuffer( GL_ARRAY_BUFFER, 0 ); glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 ); } Back to the Vertex Array Object though. My code for creating the Vertex

  • (); // create the index buffer out of DWORDs DWORD OurIndices[] = { 0, 1, 2, // side 1 2, 1, 3, 4, 0, 6, // side 2 6, 0, 2, 7, 5, 6, // side 3 6, 5, 4, 3, 1, 7, // side 4 7, 1, 5, 4, 5, 0, // side 5 0, 5, 1, 3, 7, 2, // side 6 2, 7, 6, }; // create the index buffer // D3D10_BUFFER_DESC bd; // redefinition bd.Usage = D3D10_USAGE_DYNAMIC... memcpy(pVoid, OurVertices, sizeof(OurVertices)); // copy the vertices to the buffer pBuffer->Unmap(); // create the index buffer out of DWORDs DWORD OurIndices[] = { 0, 1, 2, // side 1

  • , In); // Get The Current Line ( NEW ) shaderData[i][0] = shaderData[i][1] = shaderData[i][2] = float(atof (Line)); // Copy Over The Value ( NEW ) } fclose... (1, &amp;shaderTexture[0]); // Get A Free Texture ID ( NEW ) glBindTexture (GL_TEXTURE_1D, shaderTexture[0]); // Bind This Texture. From Now On It Will Be 1D...;normal * frac; if (normal[0] == 0 &amp;&amp; normal[1] == 0 &amp;&amp; normal[2] == 0) { normal = Vec3f(0, 0, 1); } glNormal3f(normal[0], normal[1

  • I'm writing a simple class to draw all the debugging lines I have in my scene at once. The code in the draw loop is this so far: (If I put for example, 2 instead of temporary2DVerts.size() where I have marked with //THIS LINE, the code works fine.) when I run the code below the line breaks //HERE. Access violation reading location 0x00000000. seems like the create buffer line is not working...(D3D10_PRIMITIVE_TOPOLOGY_LINELIST); UINT stride = sizeof(VERTEX); UINT offset = 0; device->IASetVertexBuffers(0, 1, &amp;pBuffer, &amp;stride, &amp;offset); screenPass->Apply(0

  • don't want to find out in the end that I have to rewrite everything again, as a lot of other objects will be working with these data. Sorry if it's a too subjective matter, but I need some insight. I'm... for my needs. Ah and I forgot to mention that nodes can have hierarchy, but this is not a problem if the frame data is reduced. The structure instances don't get duplicated unless any value changes. When... and to store runtime values. The thing is I don't want to create layers unless it's really worth it, to prevent excessive distinction for small values, such as a making a frame object to store only

  • ); glBindFramebuffer(GL_FRAMEBUFFER, 0); } Here is my drawing box code, which just takes a transformation matrix and calls the appropriate functions. The current values of P is a projection matrix... transformation has occurred somewhere. Which doesn't make sense as the object renders fine when not using my frame buffer (see bottom of post). The current result is such: Current result http://k.minus.com... wrong in my frame buffer set up code, or elsewhere. But I can't see what. The FBO is set up through the following function: unsigned int fbo_id; unsigned int depth_buffer; int m_FBOWidth, m_FBOHeight

  • I'm rendering to a depth map in order to use it as a shader resource view, but when I sample the depth map in my shader, the red component has a value of 1 while all other channels have a value of 0... errors from D3D, and no compile time warning or anything. I'm not sure what I'm missing here at all. I have the impression the depth value is always being set to 1. I have not set any depth/stencil...; td.Usage = D3D11_USAGE_DEFAULT; td.BindFlags = D3D11_BIND_DEPTH_STENCIL | D3D11_BIND_SHADER_RESOURCE; td.CPUAccessFlags = 0; td.MiscFlags = 0; _device->CreateTexture2D(&amp

  • UINT stride = sizeof(VERTEX); UINT offset = 0; device->IASetVertexBuffers(0, 1, mesh.PBuffer(), &amp;stride, &amp;offset); device->IASetIndexBuffer(mesh.IBuffer(), DXGI_FORMAT_R32_UINT, 0... UINT stride = sizeof(LINE); UINT offset = 0; device->IASetVertexBuffers(0, 1, &amp;pBuffer, &amp;stride, &amp;offset); device->IASetIndexBuffer(iBuffer, DXGI_FORMAT_R32_UINT, 0); allLines...); pRotation->SetMatrix(&amp;temporaryLines[i].rotation._11); // set the rotation matrix in the effect pPass->Apply(0); device->DrawIndexed(2, 0, 0); } temporaryLines.clear

  • Im writing a game for which, one of the models are loaded from an .obj file. It's a model of a plane, and I want to rotate the propeller. The object file is broken into groups, and the propeller... it, and then translate back. This makes the propeller spin as I want it to, but it also rotates along the origin >.< http://people.sc.fsu.edu/~jburkardt/data/obj/cessna.obj <- This is the object file. groups...(GL_LIGHT0); glEnable(GL_LIGHT1); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glColor3f(0.0f, 0.0f, 0.0f); int vertexIndex = 0, normalIndex; glRotatef(90, 0, 1, 0); glPushMatrix(); for(int

Data information