我目前正在使用OpenGl中的計算着色器,我的目標是從一個紋理渲染到另一個紋理上進行一些修改。但是,它似乎並不像我的計算着色器對紋理有任何影響。OpenGL Compute Shader - glDispatchCompue()不運行
創建計算着色器後,我做了以下
//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白色。但是,緩衝區A未變。所以,我一直無法弄清楚爲什麼我的計算着色器沒有效果。如果任何人有什麼我可以嘗試做的想法,將不勝感激。
結束語:這是我在這個網站上詢問的第一個問題。我試圖僅提供相關信息,但我仍然覺得它可能變得太多文字了。如果有關於如何改進這個問題的結構的反饋也是受歡迎的。
--------------------------------------------- ------------------------
編輯:
我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);
您可以添加如何定義/初始化'sourceBuffer'和'targetBuffer'嗎? – BDL