2013-12-13 79 views
0

我想寫一個延遲的平鋪渲染器符合用於BF3的一個DICE,我要麼不理解我在做什麼,要麼GLSL在我身上拉快一個。OpenGL計算着色器原子操作

內核的第一部分是計算每個瓦片的最大和最小深度,我正在用這個代碼進行處理。

#version 430 

#define MAX_LIGHTS_PER_TILE 1024 
#define WORK_GROUP_SIZE 16 

struct PointLight 
{ 
    vec3 position; 
    float radius; 
    vec3 color; 
    float intensity; 
}; 

layout (binding = 0, rgba32f) uniform writeonly image2D outTexture; 
layout (binding = 1, rgba32f) uniform readonly image2D normalDepth; 
layout (binding = 2, rgba32f) uniform readonly image2D diffuse; 
layout (binding = 3, rgba32f) uniform readonly image2D specular; 
layout (binding = 4, rgba32f) uniform readonly image2D glowMatID; 

layout (std430, binding = 5) buffer BufferObject 
{ 
    PointLight pointLights[]; 
}; 

uniform mat4 inv_proj_view_mat; 

layout (local_size_x = WORK_GROUP_SIZE, local_size_y = WORK_GROUP_SIZE) in; 

shared uint minDepth = 0xFFFFFFFF; 
shared uint maxDepth = 0; 


void main() 
{ 
     ivec2 pixel = ivec2(gl_GlobalInvocationID.xy); 

     //SAMPLE ALL SHIT 
     vec4 normalColor = imageLoad(normalDepth, pixel); 

     float d = (normalColor.w + 1)/2.0f; 

     uint depth = uint(d * 0xFFFFFFFF); 

     atomicMin(minDepth, depth); 
     atomicMax(maxDepth, depth); 

     groupMemoryBarrier(); 

     imageStore(outTexture, pixel, vec4(float(float(minDepth)/float(0xFFFFFFFF)))); 
} 

場景是這樣的,如果我畫的每個片段的深度。

enter image description here

試圖繪製在純粹的白色屏幕minDepth結果和繪圖MAXDEPTH產生黑屏。我的內存管理/原子功能是否錯誤,或者是我的驅動程序/ GPU /獨角獸?

作爲一個說明,我已經試過

atomicMin(minDepth, 0) 

這也產生全白圖像,這讓我很懷疑究竟怎麼回事。

回答

1

原來用barrier()代替groupMemoryBarrier()解決了這個問題。爲什麼,我不知道如果有人能夠啓發我,那將是非常感激。

+1

IIRC groupMemoryBarrier()只能確保所有先前的寫入都已完成,但這並不能確保它們確實發生過。調用barrier()是在這裏做的正確的事情。 – Gigo