How to index array in compute shader?

自古美人都是妖i 提交于 2020-01-20 09:35:10

问题


Hello I am using compute shader for updating heights of terrain. I store data in 1D array. I would like to access individual elements of array in compute shader. Is it possible?

#type compute
#version 430
//Size of compute shader local work group - x=32, y=32, z=1(default)
layout(local_size_x = 32, local_size_y = 32, local_size_z = 1) in;

layout(std430, binding = 0) buffer
buffer_InPos
{
    vec4 InPos[];
};
layout(std430, binding = 1) buffer
buffer_InNormal
{
    vec4 InNormal[];
};


uniform vec2 u_BrushPosition;
uniform float u_Radius;
uniform float u_Strength;

bool IsInRadius(vec4 pos)
{
    if ((pos.x - u_BrushPosition.x) * (pos.x - u_BrushPosition.x) +
        (pos.z - u_BrushPosition.y) * (pos.z - u_BrushPosition.y) <= u_Radius * u_Radius)
        return true;
    else return false;
}

void main(void)
{
    uint index = gl_GlobalInvocationID.x + gl_GlobalInvocationID.y * gl_NumWorkGroups.x * gl_WorkGroupSize.y;

    vec4 pos = InPos[index];
    vec4 normal = InNormal[index];
    if (IsInRadius(pos))
    {
        pos.y += u_Strength;
    }
    InPos[index] = pos;
}

I am just learning about compute shaders.

来源:https://stackoverflow.com/questions/59793047/what-is-proper-way-of-handling-data-in-compute-shader

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!