title: Using unsigned byte textures with DirectX 10 / 11
body: <p>I am attempting to do some processing in the pixel shader on a texture. The data for the texture is coming from a memory chunk of 8 bit data. The problem I am facing is how to read the data in the shader.</p>
  
  <p>Code to create the texture and ressource view:</p>
  
  <p>In OnD3D11CreateDevice:</p>
  
  <pre><code>D3D11_TEXTURE2D_DESC tDesc;
  tDesc.Height = 480;
  tDesc.Width = 640;
  tDesc.Usage = D3D11_USAGE_DYNAMIC;
  tDesc.MipLevels = 1;
  tDesc.ArraySize = 1;
  tDesc.SampleDesc.Count = 1;
  tDesc.SampleDesc.Quality = 0;
  tDesc.Format = DXGI_FORMAT_R8_UINT;
  tDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
  tDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
  tDesc.MiscFlags = 0;
  V_RETURN(pd3dDevice-&gt;CreateTexture2D(&amp;tDesc, NULL, &amp;g_pCurrentImage));
  D3D11_SHADER_RESOURCE_VIEW_DESC rvDesc;
  g_pCurrentImage-&gt;GetDesc(&amp;tDesc);
  rvDesc.Format = DXGI_FORMAT_R8_UINT;
  rvDesc.Texture2D.MipLevels = tDesc.MipLevels;
  rvDesc.Texture2D.MostDetailedMip = tDesc.MipLevels - 1;
  rvDesc.ViewDimension = D3D_SRV_DIMENSION_TEXTURE2D;
  V_RETURN(pd3dDevice-&gt;CreateShaderResourceView(g_pCurrentImage, &amp;rvDesc, &amp;g_pImageRV));    &lt;/code&gt;
  </code></pre>
  
  <p>in OnD3D11FrameRender:</p>
  
  <pre><code>HRESULT okay;
  
  if( !g_updateDone ) {
      D3D11_MAPPED_SUBRESOURCE resource;
      resource.pData = mImage.GetData();
      resource.RowPitch = 640;
      resource.DepthPitch = 1;
      okay = pd3dImmediateContext-&gt;Map(g_pCurrentImage, 0, D3D11_MAP_WRITE_DISCARD, 0, &amp;resource);
  
      g_updateDone = true;
  }
  
  pd3dImmediateContext-&gt;PSSetShaderResources(0, 1, &amp;g_pImageRV);
  </code></pre>
  
  <p>This returns no errors so far, everything seems to work.</p>
  
  <p>The HLSL Shader:</p>
  
  <pre><code>//-----  
  // Textures and Samplers  
  //-----  
  
  Texture2D &lt;int&gt; g_txDiffuse : register( t0 );  
  SamplerState g_samLinear : register( s0 );  
  
  //-----  
  // shader input/output structure  
  //-----  
  
  struct VS_INPUT  
  {  
      float4 Position     : POSITION; // vertex position   
      float2 TextureUV    : TEXCOORD0;// vertex texture coords   
  };  
  
  struct VS_OUTPUT  
  {  
      float4 Position     : SV_POSITION; // vertex position   
      float2 TextureUV    : TEXCOORD0;   // vertex texture coords   
  };  
  
  //-----  
  // Vertex shader  
  //-----  
  VS_OUTPUT RenderSceneVS( VS_INPUT input )  
  {  
      VS_OUTPUT Output;  
  
      Output.Position = input.Position;  
  
      Output.TextureUV = input.TextureUV;   
  
      return Output;      
  }  
  
  //-----  
  // Pixel Shader  
  //-----  
  
  float4 RenderScenePS( VS_OUTPUT In ) : SV_TARGET  
  {   
      int3 loc;  
      loc.x = 0;  
      loc.y = 0;  
      loc.z = 1;  
      int r = g_txDiffuse.Load(loc);  
      //float fTest = (float) r;  
  
      return float4( In.TextureUV.x, In.TextureUV.y, In.TextureUV.x + In.TextureUV.y, 1);  
  }
  </code></pre>
  
  <p>The thing is, I can't even debug it in PIX to see what r results in, because even with Shader optimization disabled, the line int r = ... is never reached</p>
  
  <p>I tested </p>
  
  <pre><code>float fTest = (float) r;
  return float4( In.TextureUV.x, In.TextureUV.y, In.TextureUV.x + In.TextureUV.y, fTest);
  </code></pre>
  
  <p>but this would result in "cannot map expression to pixel shader instruction set", even though it's a float.</p>
  
  <p>So how do I read and use 8bit integers from a texture, and if possible, with no sampling at all.</p>
  
  <p>Thanks for any feedback.</p>