title: DirectX 11 ID3DDevice::CreateTexture2D with initial data fail
body: <p>so long I follow a tutorial that <code>CreateTexture2D()</code> with NULL initial data,  then 
   using <code>UpdateSubresource()</code>  to load data,every thing works fine. But <code>UpdateSubResource()</code> is a device context function, so I try to use <code>CreateTexture2D()</code> with initial data in one shot,but it fail.</p>
  
  <p>below code show the original that works fine.</p>
  
  <pre><code>tex_desc.Height = h;
  tex_desc.Width = w;
  tex_desc.MipLevels = 0;
  tex_desc.ArraySize = 1;
  tex_desc.Format= DXGI_FORMAT_R8G8B8A8_UNORM;
  tex_desc.SampleDesc.Count = 1;
  tex_desc.SampleDesc.Quality = 0;
  tex_desc.Usage= D3D11_USAGE_DEFAULT;
  tex_desc.BindFlags= D3D11_BIND_SHADER_RESOURCE|D3D11_BIND_RENDER_TARGET;
  tex_desc.CPUAccessFlags= 0;
  tex_desc.MiscFlags = D3D11_RESOURCE_MISC_GENERATE_MIPS;
  
  row_pitch = (w * 4) * sizeof(unsigned char);
  hres = _dv-&gt;CreateTexture2D(&amp;tex_desc, NULL, &amp;m_tex);
  HR_FAIL(hres,  "create 2D texture");
  
  _dc-&gt;UpdateSubresource(m_tex, 0, NULL, data, row_pitch, 0);
  </code></pre>
  
  <p>then I change it to</p>
  
  <pre><code>tex_desc.Height = h;
  tex_desc.Width = w;
  tex_desc.MipLevels = 0;
  tex_desc.ArraySize = 1;
  tex_desc.Format= DXGI_FORMAT_R8G8B8A8_UNORM;
  tex_desc.SampleDesc.Count = 1;
  tex_desc.SampleDesc.Quality = 0;
  tex_desc.Usage= D3D11_USAGE_DEFAULT;
  tex_desc.BindFlags= D3D11_BIND_SHADER_RESOURCE|D3D11_BIND_RENDER_TARGET;
  tex_desc.CPUAccessFlags= 0;
  tex_desc.MiscFlags = D3D11_RESOURCE_MISC_GENERATE_MIPS;
  
  row_pitch = (w * 4) * sizeof(unsigned char);
  
  D3D11_SUBRESOURCE_DATA sdata;            // &lt;&lt;===changed
  sdata.pSysMem = data;                  // &lt;&lt;===changed
  sdata.SysMemPitch = row_pitch;                 // &lt;&lt;===changed
  sdata.SysMemSlicePitch = (w*h*4)*sizeof(unsigned char);       // &lt;&lt;===changed
  hres = _dv-&gt;CreateTexture2D(&amp;tex_desc, &amp;sdata, &amp;m_tex);       // &lt;&lt;===changed
  HR_FAIL(hres, "testing create texture 2d");
  </code></pre>
  
  <p>then it generate the error</p>
  
  <pre><code>testing create texture 2d fail
  E_INVALIDARG : An invalid parameter was passed to the returning function.
  </code></pre>
  
  <p>it said some parameter is invalid but I don't see why the argument i passed is invalid</p>
  
  <hr>
  
  <p><strong>UPDATE:</strong><br>
  so I try to follow the links to setup debug device for DirectX . Here's what I got:</p>
  
  <pre><code>D3D11 ERROR: ID3D11Device::CreateTexture2D: pInitialData[4].SysMemPitch cannot be 0 [ STATE_CREATION ERROR #100: CREATETEXTURE2D_INVALIDINITIALDATA]
  D3D11 ERROR: ID3D11Device::CreateTexture2D: pInitialData[6].pSysMem cannot be NULL. [ STATE_CREATION ERROR #100: CREATETEXTURE2D_INVALIDINITIALDATA]
  D3D11 ERROR: ID3D11Device::CreateTexture2D: pInitialData[7].pSysMem cannot be NULL. [ STATE_CREATION ERROR #100: CREATETEXTURE2D_INVALIDINITIALDATA]
  D3D11 ERROR: ID3D11Device::CreateTexture2D: Returning E_INVALIDARG, meaning invalid parameters were passed. [ STATE_CREATION ERROR #104: CREATETEXTURE2D_INVALIDARG_RETURN]
  Exception thrown at 0x750BC54F in directx11_test.exe: Microsoft C++ exception: _com_error at memory location 0x002BF3D0.
  D3D11 ERROR: ID3D11Device::CreateTexture2D: pInitialData[2].pSysMem cannot be NULL. [ STATE_CREATION ERROR #100: CREATETEXTURE2D_INVALIDINITIALDATA]
  D3D11 ERROR: ID3D11Device::CreateTexture2D: pInitialData[4].SysMemPitch cannot be 0 [ STATE_CREATION ERROR #100: CREATETEXTURE2D_INVALIDINITIALDATA]
  D3D11 ERROR: ID3D11Device::CreateTexture2D: pInitialData[6].pSysMem cannot be NULL. [ STATE_CREATION ERROR #100: CREATETEXTURE2D_INVALIDINITIALDATA]
  D3D11 ERROR: ID3D11Device::CreateTexture2D: pInitialData[7].pSysMem cannot be NULL. [ STATE_CREATION ERROR #100: CREATETEXTURE2D_INVALIDINITIALDATA]
  D3D11 ERROR: ID3D11Device::CreateTexture2D: Returning E_INVALIDARG, meaning invalid parameters were passed. [ STATE_CREATION ERROR #104: CREATETEXTURE2D_INVALIDARG_RETURN]
  </code></pre>
  
  <p>so it said that the Create function try to access the InitialData as an array while I only has one element as pass as argument. 
  One thing to notice is that I only get the debug output in release config(I manually define _DEBUG in the code). In Debug config, I get access violation exception trigger so I don't get the D3D Debug Message. </p>
  
  <p>so I get a way to work around it is  get an array of subresource big enough to to store all kind of mipmap level to prevent access violation.</p>
  
  <pre><code>D3D11_SUBRESOURCE_DATA sdata[20];
      int idx = 0;
      while (row_pitch &gt; 0)
      {
  
          sdata[idx].pSysMem = data;
          sdata[idx].SysMemPitch = row_pitch;
          row_pitch &gt;&gt;= 1;
          idx++;
      }
  
      hres = _dv-&gt;CreateTexture2D(&amp;tex_desc, sdata, &amp;m_tex);
      HR_FAIL(hres, "testing create texture 2d");
  </code></pre>
  
  <p>but I think it's unwise since I still need generate mipmap and this function is in device context</p>