How to debug a GLSL shader?

后端 未结 11 1352
青春惊慌失措
青春惊慌失措 2020-11-28 17:38

I need to debug a GLSL program but I don\'t know how to output intermediate result. Is it possible to make some debug traces (like with printf) with GLSL ?

11条回答
  •  [愿得一人]
    2020-11-28 18:34

    If you want to visualize the variations of a value across the screen, you can use a heatmap function similar to this (I wrote it in hlsl, but it is easy to adapt to glsl):

    float4 HeatMapColor(float value, float minValue, float maxValue)
    {
        #define HEATMAP_COLORS_COUNT 6
        float4 colors[HEATMAP_COLORS_COUNT] =
        {
            float4(0.32, 0.00, 0.32, 1.00),
            float4(0.00, 0.00, 1.00, 1.00),
            float4(0.00, 1.00, 0.00, 1.00),
            float4(1.00, 1.00, 0.00, 1.00),
            float4(1.00, 0.60, 0.00, 1.00),
            float4(1.00, 0.00, 0.00, 1.00),
        };
        float ratio=(HEATMAP_COLORS_COUNT-1.0)*saturate((value-minValue)/(maxValue-minValue));
        float indexMin=floor(ratio);
        float indexMax=min(indexMin+1,HEATMAP_COLORS_COUNT-1);
        return lerp(colors[indexMin], colors[indexMax], ratio-indexMin);
    }
    

    Then in your pixel shader you just output something like:

    return HeatMapColor(myValue, 0.00, 50.00);
    

    And can get an idea of how it varies across your pixels:

    enter image description here

    Of course you can use any set of colors you like.

提交回复
热议问题