Atomic Counter

来源:互联网 发布:张大奕淘宝店有几家 编辑:程序博客网 时间:2024/05/11 22:06

Atomic Counter是一个缓存对象,存储着一个或者多个unsigned int的变量值。在GLSL着色器有着特定的原子操作(单一的,不可再分的操作)。

  1. atomicCounterIncrement 原子计数+1
  2. atomicCounterDecrement 原子计数-1

Atomic Counter可以用来控制片元的执行,避免对同一块内存区域同时操作。原子计数器表明当前片元在执行任务时,不能被其他任务打断,保证内存中数据修改的正确性。

简单示例,片元执行顺序的可视化。

初始化

    glGenBuffers(1, &m_AtomicBuffer);    glBindBuffer(GL_ATOMIC_COUNTER_BUFFER, m_AtomicBuffer);    glBufferData(GL_ATOMIC_COUNTER_BUFFER, sizeof(GLuint)*3, NULL, GL_DYNAMIC_DRAW);    glBindBuffer(GL_ATOMIC_COUNTER_BUFFER, 0);

更新

每一帧重绘时,需要重置GL_ATOMIC_COUNTER_BUFFER的值,两种方式可以修改其值。

    glBindBuffer(GL_ATOMIC_COUNTER_BUFFER, m_AtomicBuffer);    GLuint* pAtomicCounter = (GLuint*)glMapBufferRange(GL_ATOMIC_COUNTER_BUFFER, 0, sizeof(GLuint)*3, GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT | GL_MAP_UNSYNCHRONIZED_BIT);    _ASSERT(pAtomicCounter);    memset(pAtomicCounter, 0, sizeof(GLuint)*3);    glUnmapBuffer(GL_ATOMIC_COUNTER_BUFFER);    //第二种方式,glSubBufferData    //glBindBuffer(GL_ATOMIC_COUNTER_BUFFER, m_AtomicBuffer);    //GLuint Value[3] = {0, 0, 0};    //glBufferSubData(GL_ATOMIC_COUNTER_BUFFER, 0, sizeof(GLuint)*3, Value);    //glBindBuffer(GL_ATOMIC_COUNTER_BUFFER, 0);

Fragment Shader

#version 450 corelayout (binding = 0, offset = 0) uniform atomic_uint aRed;layout (binding = 0, offset = 4) uniform atomic_uint aGreen;layout (binding = 0, offset = 8) uniform atomic_uint aBlue;uniform float fNumPixels = 1.0/(1024*768);out vec4 FragColor;void main(){    float R = atomicCounterIncrement(aRed) * fNumPixels;    float G = atomicCounterIncrement(aGreen) * fNumPixels;    float B = atomicCounterIncrement(aBlue) * fNumPixels;    FragColor = vec4(R, G, B, 1.0);}

绑定到GL_ATOMIC_COUNTER_BUFFER 0号绑定点位置,glBindBufferBase(GL_ATOMIC_COUNTER_BUFFER, 0, m_AtomicBuffer);

通过建立一个GL_ATOMIC_COUNTER_BUFFER,里面储存3个uint变量值,随着源自计数器的增加,颜色输出逐渐变白的过程。

这里写图片描述

参考资料

【1】https://www.opengl.org/wiki/Atomic_Counter
【2】http://blog.csdn.net/csxiaoshui/article/details/33740065
【3】http://www.geeks3d.com/20120309/opengl-4-2-atomic-counter-demo-rendering-order-of-fragments/
【4】http://www.lighthouse3d.com/tutorials/opengl-atomic-counters/

0 0