GLSL シェーダーをデバッグするにはどうすればよいですか?
質問
GLSL プログラムをデバッグする必要がありますが、中間結果を出力する方法がわかりません。GLSL を使用して (printf のような) デバッグ トレースを作成することは可能ですか?
解決
あなたは簡単にGLSL内から戻ってCPUに通信することはできません。 glslDevilまたは他のツールを使用すると、あなたの最善の策です。
Aのprintfは、GLSLのコードを実行しているGPUからCPUに戻って取得しようとして必要となります。代わりに、ディスプレイに推進してみることができます。代わりに、出力テキスト、画面に視覚的に独特な何かを出力しようとします。たとえば、あなたはあなたがprintf関数を追加したい、あなたのコードのポイントに達した場合にのみ何か特定の色を塗ることができます。あなたが値をprintfのために必要がある場合は、その値に応じて色を設定することができます。
他のヒント
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;
}
私はは頂点シェーダをデバッグするための便利なツールであることをフィードバックを変換発見しました。あなたは、VS出力の値をキャプチャし、ラスタライザを経由せず、バックCPU側でそれらを読むためにこれを使用することができます。
ここのフィードバックを変革についてのチュートリアルへの別のリンクがあります。
あなたが画面上価値の変動を視覚化したい場合は、これに類似したヒートマップ機能を使用することができます(私は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サンドボックスのシェーダのために私にはかなり重宝しています。
未デバッグ自体(できないと回答されている)が、すぐに出力の変化を見ることが便利。
テクスチャに対してオフライン レンダリングを実行し、テクスチャのデータを評価します。「テクスチャへのレンダリング」をグーグルでグーグルで検索して、GlreadPixelsを使用して出力を配列に読み取り、それについてアサーションを実行することで関連するコードを見つけることができます(デバッガーでこのような巨大な配列を調べることは通常、実際には有用ではありません)。
また、0 ~ 1 の範囲外の出力値へのクランプを無効にすることもできます。これは、次の場合にのみサポートされます。 浮動小数点テクスチャ.
私は個人的に、シェーダーを適切にデバッグするという問題にしばらく悩まされていました。良い方法はないようです。誰かが良い (そして古い/非推奨ではない) デバッガを見つけたら、私に知らせてください。
私はフラグメントシェーダの例を共有しています、どのようにi実際にデバッグます。
#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;
}
これを試すことができます: https://github.com/msqrt/shader-printf これは、適切に「GLSL 用の単純な printf 機能」と呼ばれる実装です。
ShaderToy を試してみたり、次のようなビデオを視聴したりすることもできます (https://youtu.be/EBrAdahFtuo) YouTube チャンネル「The Art of Code」から、デバッグと視覚化に効果的なテクニックのいくつかをご覧いただけます。彼は本当に良いものを書いているし、複雑なアイデアを斬新で魅力的で理解しやすい形式で提示する才能も持っているので、私は彼のチャンネルを強くお勧めできます (彼のマンデルブロビデオはまさにその素晴らしい例です: https://youtu.be/6IWXkV82oyY)
返信が遅れたことを気にしていない人がいないことを願っていますが、この質問は GLSL デバッグに関する Google 検索で上位にランクされており、もちろん 9 年間で多くのことが変わりました :-)
追伸:他の代替手段としては、シェーダー用のフル ステッピング デバッガーを提供する NVIDIA nSight や AMD ShaderAnalyzer も考えられます。
既存の答えは、すべての良いものですが、私はGLSLシェーダでトリッキーな精度の問題をデバッグするには、貴重なされているもう一つの小さな宝石を共有したいと思いました。非常に大きな整数の番号と正確intに使用床(n)と床(N + 0.5)適切ラウンド実装する()への世話をするために、浮動小数点のような1つの必要性を示しました。 R、G、Bの出力値にバイト・コンポーネントを梱包するには、以下の論理によって正確な整数であるfloat値をレンダリングすることが可能である。
// 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;
この回答の下部には、IEEE 754 float
をコードする、色として出力するフルbinary32
値を可能GLSLコードの一例です。以下のように私はそれを使用する(このスニペットは、モデルビュー行列のyy
成分を与える):
vec4 xAsColor=toColor(gl_ModelViewMatrix[1][1]);
if(bool(1)) // put 0 here to get lowest byte instead of three highest
gl_FrontColor=vec4(xAsColor.rgb,1);
else
gl_FrontColor=vec4(xAsColor.a,0,0,1);
あなたは、画面上でこれを取得した後、あなただけの任意のカラーピッカーを取ることができ、形式HTMLとしてカラー(00
値にrgb
を追加あなたはより高い精度を必要とした場合、下位バイトを取得するために、第二のパスをしていない場合は、あなたは)やる、とあなたはIEEE 754 float
としてbinary32
の進表現を取得ます。
ここでtoColor()
の実際の実装があります:
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=float(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 float 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(float(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 vec4(binary32)/255.;
}