title: How to obtain texture coordinate from arbitrary position in HLSL
body: <p><img src="https://i.stack.imgur.com/tibOH.png" alt="enter image description here"></p>
  
  <p>I'm working on a C++ project using DirectX 11 with HLSL shaders.</p>
  
  <p>I have a texture which is mapped onto some geometry.
  Each vertex of the geometry has a position and a texture coordinate.</p>
  
  <p>In the pixel shader, I now can easily obtain the texture coordinate for exactly this one pixel. But how can I sample the color from the neighboring pixels?</p>
  
  <p>For example for the pixel at position 0.2 / 0, I get the texture coordinate 0.5 / 0, which is blue.
  But how do I get the texture coordinate from let's say 0.8 / 0?</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>What I'm actually implementing is a Volume Renderer using raycasting.
  The volume to-be-rendered is a set of 2D slices which are parallel and aligned, but not necessarily equidistant.
  For the volume I use DirectX's Texture3D class in order to easily get interpolation in z direction.</p>
  
  <p>Now I cast rays through the volume and sample the 3D texture value at equidistant steps on that ray.
  Now my problem comes into play. I cannot simply sample the Texture3D at my current ray position, as the slices are not necessarily equidistant.
  So I have to somehow "lookup" the texture coordinate of that position in 3D space and then sample the texture using this texture coordinate.</p>
  
  <p>I already have an idea how to implement this, which would be an additional Texture3D of the same size where the color of the texel at position xyz can be interpreted as the texture coordinate at position xyz.</p>
  
  <p>This would solve my problem but I think it is maybe overkill and there might be a simpler way to accomplish the same thing.</p>
  
  <p><strong>Edit 2:</strong></p>
  
  <p>Here is another illustration of the sampling problem I am trying to fix.</p>
  
  <p><img src="https://i.stack.imgur.com/a2pwn.png" alt="enter image description here"></p>
  
  <p>The root of the problem is that my Texture3D is distorted in z direction.</p>
  
  <p>From within one single pixelshader instance I want to obtain the texture coordinate for <strong>any</strong> given position xyz in the volume, not only for the current fragment being rendered.</p>
  
  <p><strong>Edit 3:</strong></p>
  
  <p>Thanks for all the good comments and suggestions.</p>
  
  <p>The distances between the slices in z-order can be completely random, so they cannot be described mathematically by a function.
  So what I basically have is a very simple class, e.g.</p>
  
  <pre><code>struct Vertex
  {
    float4 Position; // position in space
    float4 TexCoord; // position in dataset
  }
  </code></pre>
  
  <p>I pass those objects to the buffer of my vertex shader.
  There, the values are simply passed through to the pixel shader.
  My interpolation is set to <code>D3D11_FILTER_MIN_MAG_MIP_LINEAR</code> so I get a nice interpolation for my data and the respective texture coordinates.
  The signature of my pixel shader looks like this:</p>
  
  <pre><code>float4 PShader( float4 position : SV_POSITION
                , float4 texCoord : TEXCOORD
                ) : SV_TARGET
  {
    ...
  }
  </code></pre>
  
  <p>So for each fragment to-be-rendered on the screen, I get the position in space ( <code>position</code> ) and the corresponding position ( <code>texCoord</code> ) in the ( interpolated ) dataset. So far so good.</p>
  
  <p>Now, from this <code>PShader</code> instance, I want to access not only <code>texCoord</code> at <code>position</code>, but also the <code>texCoord</code>s of other <code>position</code>s.</p>
  
  <p>I want to do raycasting, so for each screen-space fragment, I want to cast a ray and sample the volume dataset at discrete steps.</p>
  
  <p><img src="https://i.stack.imgur.com/LGf0K.png" alt="enter image description here"></p>
  
  <p>The black plane symbolizes the screen. The other planes are my dataset where the slices are aligned and parallel, but not equidistant.
  The green line is the ray that I cast from the screen to the dataset.
  The red spheres are the locations where I want to sample the dataset.</p>
  
  <p>DirectX knows how to interpolate the stuff correctly, as it does so for every screen-space fragment.</p>
  
  <p>I thought I could easily access this interpolation function and query the interpolated <code>texCoord</code> for position xyz. But as it seems DirectX has not a mechanism to do this.</p>
  
  <p>So the only solution really might be to use a 1D-Texture for z-lookup and interpolate between the values manually in the shader.
  Then use this information to lookup the pixel value at this position.</p>