[英]OpenGL Compute Shader - glDispatchCompue() does not run
我目前正在使用OpenGl中的計算着色器,我的目標是從一個紋理渲染到另一個紋理並進行一些修改。 但是,似乎我的計算着色器根本不會對紋理產生任何影響。
創建計算着色器后,我執行以下操作
//Use the compute shader program
(*shaderPtr).useProgram();
//Get the uniform location for a uniform called "sourceTex"
//Then connect it to texture-unit 0
GLuint location = glGetUniformLocation((*shaderPtr).program, "sourceTex");
glUniform1i(location, 0);
//Bind buffers and call compute shader
this->bindAndCompute(bufferA, bufferB);
bindAndCompute()函數看起來像這樣,其目的是准備計算着色器訪問兩個緩沖區,然后運行計算着色器。
bindAndCompute(GLuint sourceBuffer, GLuint targetBuffer){
glBindImageTexture(
0, //Always bind to slot 0
sourceBuffer,
0,
GL_FALSE,
0,
GL_READ_ONLY, //Only read from this texture
GL_RGB16F
);
glBindImageTexture(
1, //Always bind to slot 1
targetBuffer,
0,
GL_FALSE,
0,
GL_WRITE_ONLY, //Only write to this texture
GL_RGB16F
);
//this->height is currently 960
glDispatchCompute(1, this->height, 1); //Call upon shader
glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);
}
最后,這是計算着色器。 我目前只嘗試設置它,使它使第二個紋理完全變白。
#version 440
#extension GL_ARB_compute_shader : enable
#extension GL_ARB_shader_image_load_store : enable
layout (rgba16, binding=0) uniform image2D sourceTex; //Textures bound to 0 and 1 resp. that are used to
layout (rgba16, binding=1) uniform image2D targetTex; //acquire texture and save changes made to texture
layout (local_size_x=960 , local_size_y=1 , local_size_z=1) in; //Local work-group size
void main(){
vec4 result; //Vec4 to store the value to be written
pxlPos = ivec2(gl_GlobalInvocationID.xy); //Get pxl-pos
/*
result = imageLoad(sourceTex, pxlPos);
...
*/
imageStore(targetTex, pxlPos, vec4(1.0f)); //Write white to texture
}
現在,當我啟動bufferB為空時。 當我運行這個時,我希望bufferB變成完全白色。 但是,此代碼緩沖區B保持為空。 我的結論是
答:計算着色器不會寫入紋理
B: glDispatchCompute()根本沒有運行
但是,我沒有錯誤,着色器確實編譯它應該。 我已經檢查過我通過綁定bufferA正確綁定紋理,我已經知道它包含什么,然后運行bindAndCompute(bufferA,bufferA)來將bufferA變成白色。 但是, bufferA沒有改變。 所以,我無法弄清楚為什么我的計算着色器沒有效果。 如果有人對我可以嘗試做什么有任何想法,將不勝感激。
結束語:這是我在本網站上提出的第一個問題。 我試圖只提供相關信息,但我仍然覺得它可能無論如何都變得過多了。 如果有關於如何改進問題結構的反饋也是受歡迎的。
-------------------------------------------------- -------------------
編輯:
我使用sourceBuffer和targetBuffer發送的紋理定義如下:
glGenTextures(1, *buffer);
glBindTexture(GL_TEXTURE_2D, *buffer);
glTexImage2D(
GL_TEXTURE_2D,
0,
GL_RGBA16F, //Internal format
this->width,
this->height,
0,
GL_RGBA, //Format read
GL_FLOAT, //Type of values in read format
NULL //source
);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
綁定的圖像的圖像格式與着色器中的圖像格式不匹配。 綁定RGB16F
(每個紋素RGB16F
)紋理,但在着色器中聲明它是rgba16
格式(每個紋素64byte)。
格式必須根據此處給出的規則進行匹配。 假設您在OpenGL中分配了紋理,這意味着每個紋素的總大小必須匹配。 另請注意,圖像加載/存儲不支持3通道紋理(沒有一些相當奇怪的例外)。
作為旁注:如果紋理格式大小匹配,着色器將執行並寫入。 但是你寫的東西可能是垃圾,因為你的紋理是16位浮點格式( RGBA_16F
),而你告訴着色器它們是16位無符號標准化格式( rgba16
)。 雖然這不會直接影響計算着色器,但是如果您回讀紋理或通過采樣器訪問紋理或將數據> 1.0f或<0.0f寫入其中,則無關緊要。 如果需要16位浮點數, rgba16f
在計算着色器中使用rgba16f
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.