我試圖實現GLSL自旋鎖,以便能夠實現單程深度剝離。我遇到了麻煩,因爲鎖定紋理使用的例子很少。我不得不承認,我並不真正知道自己在做什麼,所以我只是爲了安全起見而描述可能更多的背景。GLSL Spinlock永不終止
我寫了一個片段程序,它應該做的有效虛無
#version 420 core
//The lock texture holds either 0 or 1.
//0 means that the texture is available.
//1 means that the texture is locked.
layout(r32ui) coherent uniform uimage2D img2D_0; //locking texture
layout(RGBA32F) coherent uniform image2D img2D_1; //data texture (currently unused)
void main() {
ivec2 coord = ivec2(gl_FragCoord.xy);
//The loop's exchange function swaps out the old value with 1.
//If the locking texture was 0, 0 will be returned, terminating the loop;
//the locking texture will now contain 1, indicating that the locking
//texture is now locked.
//Conversely, if the locking texture contains 1, then the exchange function
//will write a 1 (so the texture is still locked), and return 1, indicating
//that the texture is locked and unavailable.
while (imageAtomicExchange(img2D_0,coord,1u)==1u);
//The locking texture is locked. More code would go here
//This unlocks the texture.
imageAtomicExchange(img2D_0,coord,0);
}
鎖定紋理創建像這樣:
//data is an array initialized to all 0.
glTexImage2D(GL_TEXTURE_2D,0,GL_R32UI,size_x,size_y,0,GL_RED_INTEGER,GL_UNSIGNED_INT,data);
要執行的算法,我拿一個FBO,用色RGBA F32渲染附件並啓用它。我結合以上着色器,然後通過所述鎖定紋理img2D_0和彩色附着到img2D_1,使用此代碼:
glBindImageTextureEXT(
/* 0, 1, respectively */,
texture_id, 0,GL_FALSE,0, GL_READ_WRITE,
/* GL_R32UI, GL_RGBA32F, respectively */
);
該目的然後用VBO渲染,和一些第二通行證顯示數據的內容。
問題是給出的片段程序崩潰視頻驅動程序(因爲它永遠不會終止)。我的問題是爲什麼?紋理初始化爲0,我很確定我的交換函數的邏輯是有效的。我的設置和方法基本正確嗎?
通過對紋理進行採樣並將值存儲到另一個紋理,將它們轉儲到主機上,從而確信它已被正確初始化。 – 2012-08-04 15:20:54
這是一個很好的測試。如果鎖定爲0,則移除破碎的代碼並存儲vec4(0.25,0.0,0.0,0.0),如果鎖定爲1,則vec4(0.0,0.50,0.0,0.0),否則存儲vec4(0.0,0.0,0.75,0.0)並且將結果轉儲到數據紋理僅顯示(0.25,0.0,0.0,0.0) - 即(至少在開始時)所有鎖定值均爲0. – imallett 2012-08-04 15:30:23
轉儲着色器反彙編。 – 2012-08-04 15:45:17