Sampling Textures in Vertex Stage with SparkSL?

Hey all, wanted to know if anyone’s had any luck with sampling textures in the vertex stage in SparkSL? I’m trying to use a texture to control vertex displacement. The documentation seems to indicate that vertex stage sampling is possible with the sampleLod function.. But when I try, I get the runtime error “SparkSL:Vertex position can’t be set in fragment stage!” indicating that the sampling was done in fragment stage :slightly_frowning_face:.

I have the ‘Vertex Texture Fetch’ capability enabled as described in the documentation, so I don’t think that’s the issue. Anyone have any idea what I might be missing? :thinking: Code below:

// @param[default=#00FFFFFF] color
// @return color
vec4 main(std::Texture2d displaceMask, vec4 color, out vec4 Position) {
  
  //VERTEX SHADER
  vec2 uvVertStage = std::getVertexTexCoord();
  vec4 vertPos = std::getVertexPosition();
  vec3 vertNormal = std::getVertexNormal();

  //this sampling seems to happen in fragment stage (even though Vertex Texture Fetch capability is enabled in Spark and UVs were fetched in vertex stage)
  float displaceAlpha = displaceMask.sampleLod(uvVertStage, 0.0).x;
  vertPos.xyz += vertNormal*displaceAlpha;

  Position = std::getModelViewProjectionMatrix() * vertPos;

  //FRAGMENT SHADER
  return color;
}

Seems to work on my computer. I’m not sure what the problem is, but here’s my test project.


vert-displace.arprojpkg (405.0 KB)

1 Like

From looking at your example I was able to figure out what I was doing wrong!

The texture I’m using is a dynamic texture made in the patch editor. I needed to put the texture through a Shader Render Pass patch before passing it to the shader to get everything working properly.

Thanks Josh!

Aha! Yes! The shader outputs are always vec4 until you convert them back into textures with shader render passes. Glad to hear you figured it out!

1 Like