[英]OpenGL: Compute Shader - gl_GlobalInvocationID giving static output
所以我有一個計算着色器應該采用紋理並將其復制到另一個紋理稍作修改。 我已經確認紋理是綁定的,並且可以使用RenderDoc編寫數據,RenderDoc是圖形的調試工具。 我的問題是在着色器內部由OpenGL創建的變量gl_GlobalInvocationID似乎無法正常工作。
這是我對計算着色器的調用:( 紋理高度為480)
glDispatchCompute(1, this->m_texture_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 (rgba8, binding=0) uniform image2D texture_source0;
layout (rgba8, binding=1) uniform image2D texture_target0;
layout (local_size_x=640 , local_size_y=1 , local_size_z=1) in; //Local work-group size
void main() {
ivec2 txlPos; //A variable keeping track of where on the texture current texel is from
vec4 result; //A variable to store color
txlPos = ivec2(gl_GlobalInvocationID.xy);
//txlPos = ivec2( (gl_WorkGroupID * gl_WorkGroupSize + gl_LocalInvocationID).xy );
result = imageLoad(texture_source0, txlPos); //Get color value
barrier();
result = vec4(txlPos, 0.0, 1.0);
imageStore(texture_target0, txlPos, result); //Save color in target texture
}
當我運行它時,目標紋理變為完全黃色,除了左邊框的1pxl粗綠線和沿底邊的1pxl粗紅線。 我的期望是看到某種梯度,因為將txlPos保存為顏色值。
我是以某種方式定義我的工作組錯了嗎? 我已經嘗試將gl_GlobalInvokationID拆分為其組件,但沒有設法讓他們更聰明地擺弄它們。
8位浮點紋理只能存儲介於0和1之間的值。由於gl_GlobalInvocationID
在大多數情況下大於1,因此它被限制為最大值1,這使紋理變為黃色。
如果要在兩個方向上創建漸變,則必須確保存儲的值從0開始並以1結束。一種可能性除以最大值:
result = vec4(vec2(gl_GlobalInvocationID.xy) / vec2(640, 480), 0, 1);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.