Adaptive level of detail

Hi,
I’m implementing a parallax-occlusion shader. I want to achieve adaptive per-pixel LOD as in here:

The idea was described by Tatarchuk at GDC06: http://www.ati.com/developer/gdc/2006/GDC06-Tatarchuk-Parallax_Occlusion_Mapping.pdf
It involves the following steps:

  • Compute the current mip map level
  • For furthest LOD levels, render using normal mapping (threshold level)
  • As the surface approaches the viewer, increase the sampling rate as a function of the current mip map level
  • In transition region between the threshold LOD level, blend between the normal mapping and the full parallax occlusion mapping

Here’s a pseudocode of what I’m using in my CG frag shader:

...
fixed4 frag (v2f i) : COLOR {
    fixed lodLevel = GetLodLevel( _uv );
    if (lodLevel < 1.0) {
        //...compute POM...
    } else {
        //...use normal maps instead...
    }
}

However this terrifically slows down my shader instead of optimizing it. And also the main branch never executes. Only the “else” branch is executing. Am I not getting something here? Looks like both branches are executing at the same time.
Is it possible to achieve adaptive LOD withoug dynamic branching?
What implementation Tatarchuk speaks about in that paper? Is it possible with unity/current hardware?

Thanks!

Are you using “#pragma target 3.0” (or “#pragma glsl” when using OpenGL)? Unity - Manual: HLSL in Unity
Without one of these, Unity might actually translate the shader such that it always executes both branches (at least as far as I remember).

Thanks for your help, the problem was on my side - I was supplying wrong UVs to the LOD calculator.