4
votes

In shader model 3.0, I'm pretty sure this was a no but I want to ask this anyway,

In shader model 5.0, can you sample a texture in a vertex shader?

If I want to make large amounts of supplementary information available per-vertex, what are my options?

Edit: Apparently it is possible to do a Vertex Texture Fetch, as done here, but when I try it in my hlsl shader model 5 program, I get the error

error X4532: cannot map expression to vs_5_0 instruction set
2

2 Answers

4
votes

Yes, sampling a texture from a vertex shader is very easily done in Shader Model 5.0, using operator[ unint2 ] on any Texture2D object.

So for example, tex0 is a Texture2D object in a shader model 5 hlsl program:

Texture2D tex0 : register( t0 );

// in a vertex shader program
uint2 pos_xy = { 0, 1 } ;
texelColor = tex0[ pos_xy ] ;
0
votes

YES, but you must use SampleLevel to sample. You can also load values directly from the texture using mytexture[xy_coord] or texture.Load(...) which provides values without 'sampling' (e.g. bilinear interpolation).

You can sample a texture in a vertex shader using the SampleLevel function, where you must also specify the mip-mapping level that you want to sample at. You can imagine that the Sample function uses the SampleLevel function internally, with the added benefit that the mip-mapping level is selected for you automatically by taking local derivatives of the texture coordinate lookup in screen space (this allows the GPU to select which mip level to use).

https://msdn.microsoft.com/en-us/library/windows/desktop/bb509699(v=vs.85).aspx