如何调试GLSL着色器?
您不能轻松地从GLSL内部传回CPU。使用glslDevil或其他工具是你最好的选择。
printf将需要尝试从运行GLSL代码的GPU返回CPU。相反,您可以尝试推进显示。不要尝试输出文本,而是输出视觉上与屏幕不同的内容。例如,只有在达到要添加printf的代码点时才可以绘制特定颜色。如果您需要打印一个值,您可以根据该值设置颜色。
对纹理进行离线渲染并评估纹理的数据。 您可以通过搜索“渲染到纹理”来查找相关的代码。然后使用glReadPixels将输出读入数组并在其上执行断言(因为在调试器中查看如此巨大的数组通常不是很有用)。
此外,您可能希望禁用钳位,以输出不在0和1之间的值,该值仅在floating point textures受支持。
我个人一直在纠正着色器的问题。似乎没有什么好方法 - 如果任何人发现一个好的(并且不是过时的/不赞成的)调试器,请告诉我。
任何回答或评论说“谷歌xyz”应该被禁止或从*投票。 – gregoiregentil 2016-08-27 00:23:57
void main(){
float bug=0.0;
vec3 tile=texture2D(colMap, coords.st).xyz;
vec4 col=vec4(tile, 1.0);
if(something) bug=1.0;
col.x+=bug;
gl_FragColor=col;
}
我在分享一个片段着色器的例子,我是如何调试的。
#version 410 core
uniform sampler2D samp;
in VS_OUT
{
vec4 color;
vec2 texcoord;
} fs_in;
out vec4 color;
void main(void)
{
vec4 sampColor;
if(texture2D(samp, fs_in.texcoord).x > 0.8f) //Check if Color contains red
sampColor = vec4(1.0f, 1.0f, 1.0f, 1.0f); //If yes, set it to white
else
sampColor = texture2D(samp, fs_in.texcoord); //else sample from original
color = sampColor;
}
如果你希望显示在屏幕上的值的变化,可以使用类似这样的热图功能(我写的HLSL,但很容易适应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);
}
在像素着色器
然后你只需输出是这样的:
return HeatMapColor(myValue, 0.00, 50.00);
并能得到它在你的像素是如何变化的一个想法:
当然你可以使用任何你喜欢的颜色集合。
现有的答案都是很好的东西,但我想分享一个在调试GLSL着色器中棘手的精度问题方面有价值的小宝石。将非常大的int数表示为浮点数时,需要注意正确地使用floor(n)和floor(n + 0.5)来实现round()。然后可以通过以下逻辑呈现一个精确int的浮点值,以将字节分量打包为R,G和B输出值。
// Break components out of 24 bit float with rounded int value
// scaledWOB = (offset >> 8) & 0xFFFF
float scaledWOB = floor(offset/256.0);
// c2 = (scaledWOB >> 8) & 0xFF
float c2 = floor(scaledWOB/256.0);
// c0 = offset - (scaledWOB << 8)
float c0 = offset - floor(scaledWOB * 256.0);
// c1 = scaledWOB - (c2 << 8)
float c1 = scaledWOB - floor(c2 * 256.0);
// Normalize to byte range
vec4 pix;
pix.r = c0/255.0;
pix.g = c1/255.0;
pix.b = c2/255.0;
pix.a = 1.0;
gl_FragColor = pix;
在此答案的底部是GLSL代码的例子,其允许输出完整float
值作为颜色编码IEEE 754 binary32
。我用它喜欢如下(本段给出了模型视图矩阵的yy
分量):
你在屏幕上之后,你可以采取任何颜色选择器,格式化颜色为HTML(附加00
到rgb
值,如果你不需要更高的精度,并且如果你这样做了第二遍以得到低字节),并且你得到作为IEEE 754 binary32
的float
的十六进制表示。
下面是实际执行的toColor()
:
#version 120
const int emax=127;
// Input: x>=0
// Output: base 2 exponent of x if (x!=0 && !isnan(x) && !isinf(x))
// -emax if x==0
// emax+1 otherwise
int floorLog2(float x)
{
if(x==0) return -emax;
// NOTE: there exist values of x, for which floor(log2(x)) will give wrong
// (off by one) result as compared to the one calculated with infinite precision.
// Thus we do it in a brute-force way.
for(int e=emax;e>=1-emax;--e)
if(x>=exp2(float(e))) return e;
// If we are here, x must be infinity or NaN
return emax+1;
}
// Input: any x
// Output: IEEE 754 biased exponent with bias=emax
int biasedExp(float x) { return emax+floorLog2(abs(x)); }
// Input: any x such that (!isnan(x) && !isinf(x))
// Output: significand AKA mantissa of x if !isnan(x) && !isinf(x)
// undefined otherwise
float significand(float x)
{
// converting int to float so that exp2(genType) gets correctly-typed value
float expo=floorLog2(abs(x));
return abs(x)/exp2(expo);
}
// Input: x\in[0,1)
// N>=0
// Output: Nth byte as counted from the highest byte in the fraction
int part(float x,int N)
{
// All comments about exactness here assume that underflow and overflow don't occur
const int byteShift=256;
// Multiplication is exact since it's just an increase of exponent by 8
for(int n=0;n<N;++n)
x*=byteShift;
// Cut higher bits away.
// $q \in [0,1) \cap \mathbb Q'.$
float q=fract(x);
// Shift and cut lower bits away. Cutting lower bits prevents potentially unexpected
// results of rounding by the GPU later in the pipeline when transforming to TrueColor
// the resulting subpixel value.
// $c \in [0,255] \cap \mathbb Z.$
// Multiplication is exact since it's just and increase of exponent by 8
float c=floor(byteShift*q);
return int(c);
}
// Input: any x acceptable to significand()
// Output: significand of x split to (8,8,8)-bit data vector
ivec3 significandAsIVec3(float x)
{
ivec3 result;
float sig=significand(x)/2; // shift all bits to fractional part
result.x=part(sig,0);
result.y=part(sig,1);
result.z=part(sig,2);
return result;
}
// Input: any x such that !isnan(x)
// Output: IEEE 754 defined binary32 number, packed as ivec4(byte3,byte2,byte1,byte0)
ivec4 packIEEE754binary32(float x)
{
int e = biasedExp(x);
// sign to bit 7
int s = x<0 ? 128 : 0;
ivec4 binary32;
binary32.yzw=significandAsIVec3(x);
// clear the implicit integer bit of significand
if(binary32.y>=128) binary32.y-=128;
// put lowest bit of exponent into its position, replacing just cleared integer bit
binary32.y+=128*int(mod(e,2));
// prepare high bits of exponent for fitting into their positions
e/=2;
// pack highest byte
binary32.x=e+s;
return binary32;
}
vec4 toColor(float x)
{
ivec4 binary32=packIEEE754binary32(x);
// Transform color components to [0,1] range.
// Division is inexact, but works reliably for all integers from 0 to 255 if
// the transformation to TrueColor by GPU uses rounding to nearest or upwards.
// The result will be multiplied by 255 back when transformed
// to TrueColor subpixel value by OpenGL.
return binary32/255.;
}
...无需使用外部软件,如glslDevil。 – 2010-03-24 15:13:06
看看这个[从GLSL碎片着色器调试浮点变量和文本的打印](https://*.com/a/44797902/2521214)你只需要单个备用纹理单元用于输出值的字体和常量状态打印区域 – Spektre 2017-06-29 07:55:30