Shader chunks: Compilation order

I’ve been wondering for some time now:
“Which order is the shader chunks concatenated when they are compiled?”

If say, you declare a uniform in the normalMapPS chunk that variable is gonna be available in the diffusePS chunk, but not the other way around (which I assume means normalMapPS is put before diffusePS when compiled).

I tried looking a bit at the engine repository, but didn’t manage to get any wiser.

Anyone know the shader chunk compilation order (or where to look for it)?
Also, if you want to declare a uniform variable so that is available to all shader chunks, any tips on which chunk this should be done in? (startPS is NOT the answer unfortunately)

This file concatenates chunks for the backend shader … but it’s not something you can easily just read I’m afraid.

Typically I inspect the generated shader, and that gives me the best idea of how things are combined together.

1 Like

Thanks! Actually I just did that and this is what I found:

By enabling pc.Tracing.set(pc.TRACEID_SHADER_ALLOC, true) at start-up I was able to inspect the concatenated shader code for PlayCanvas standard materials.

From what I could surmise this is the order of the shader chunks for the fragment shader (as of date):

  • basePS
  • opacityPS
  • normalDetailMapPS
  • normalMapPS
  • diffuseDetailMapPS
  • diffusePS
  • specularPS
  • glossPS
  • emissivePS
  • endPS
  • startPS

I didn’t go through all the possible chunks, so feel to expand on this. It’s also possible to inspect the vertex shader this way :slight_smile:

Oh, yeah, so to answer my own question: It seems the best place to define variables that should be available to the whole shader is in the basePS chunk. I am pretty sure this is the the first chunk of all possible chunks in the fragment shader.