title: DX11 convert pixel format BGRA to RGBA
body: <p>I have currently the problem that a library creates a DX11 texture with BGRA pixel format.
  But the displaying library can only display RGBA correctly. (This means the colors are swapped in the rendered image)</p>
  
  <p>After looking around I found a simple for-loop to solve the problem, but the performance is not very good and scales bad with higher resolutions. I'm new to DirectX and maybe I just missed a simple function to do the converting.</p>
  
  <pre><code>// Get the image data
  unsigned char* pDest = view-&gt;image-&gt;getPixels();
  
  // Prepare source texture
  ID3D11Texture2D* pTexture = static_cast&lt;ID3D11Texture2D*&gt;( tex );
  
  // Get context
  ID3D11DeviceContext* pContext = NULL;
  dxDevice11-&gt;GetImmediateContext(&amp;pContext);
  
  // Copy data, fast operation
  pContext-&gt;CopySubresourceRegion(texStaging, 0, 0, 0, 0, tex, 0, nullptr);
  
  // Create mapping 
  D3D11_MAPPED_SUBRESOURCE mapped;
  HRESULT hr = pContext-&gt;Map( texStaging, 0, D3D11_MAP_READ, 0, &amp;mapped );
  
  if ( FAILED( hr ) )
  {
      return;
  }
  
  // Calculate size
  const size_t size = _width * _height * 4;
  
  // Access pixel data
  unsigned char* pSrc = static_cast&lt;unsigned char*&gt;( mapped.pData );
  
  // Offsets
  int offsetSrc = 0;
  int offsetDst = 0;
  int rowOffset = mapped.RowPitch % _width;
  
  // Loop through it, BRGA to RGBA conversation
  for (int row = 0; row &lt; _height; ++row)
  {
      for (int col = 0; col &lt; _width; ++col)
      {
          pDest[offsetDst] = pSrc[offsetSrc+2];
          pDest[offsetDst+1] = pSrc[offsetSrc+1];
          pDest[offsetDst+2] = pSrc[offsetSrc];
          pDest[offsetDst+3] = pSrc[offsetSrc+3];
  
          offsetSrc += 4;
          offsetDst += 4;
      }
  
      // Adjuste offset
      offsetSrc += rowOffset;
  }
  
  // Unmap texture
  pContext-&gt;Unmap( texStaging, 0 );
  </code></pre>
  
  <p>Solution:</p>
  
  <pre><code>    Texture2D txDiffuse : register(t0);
  SamplerState texSampler : register(s0);
  
  struct VSScreenQuadOutput
  {
  float4 Position : SV_POSITION;
  float2 TexCoords0 : TEXCOORD0;
  };
  
  float4 PSMain(VSScreenQuadOutput input) : SV_Target
  {
  return txDiffuse.Sample(texSampler, input.TexCoords0).rgba;
  }
  </code></pre>