lets assume we invoke a compute shader with 1 (x 1 x 1) workgroups, and the compute shader's task is to compute the sum of all numbers from 1 to n = 999. theres a formula how to calculate the result:

result = n * (n + 1) / 2 =

https://en.wikipedia.org/wiki/1_%2B_..._%2B_%E2%8B%AF

using uint variables:

Code glsl:

#version 450 core
layout (local_size_x = 1000, local_size_y = 1, local_size_z = 1) in;
layout (binding = 1, std430) buffer OutputBlock { uint Result; };
shared uint Total;
void main()
{
atomicAdd(Total, gl_LocalInvocationID.x);
barrier();
if (gl_LocalInvocationID.x == 50)
Result = Total;
}

here the actual math (reading variable, processing the temporary result, writing to variable) is done in the "atomicAdd(Total, gl_LocalInvocationID.x)" instruction. we then only need to wait for all other invocations to reach the "barrier()" point in the code, finally 1 certain (here: the 51st) invocation is allowed to write the sum into the shader storage buffer.

that works.

i currently have this:

Code glsl:

#version 450 core
layout (local_size_x = 1000, local_size_y = 1, local_size_z = 1) in;
layout (binding = 1, std430) buffer OutputBlock { float Result; };
shared float Total;
void main()
{
memoryBarrierShared();
Total += float(gl_LocalInvocationID.x);
memoryBarrierShared();
barrier();
if (gl_LocalInvocationID.x == 50)
Result = Total;
}

but it doesnt deliver the correct result. this is how i query the result:

Code cpp:

glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, buffer);
glBufferData(GL_SHADER_STORAGE_BUFFER, 4, NULL, GL_STATIC_READ);
/* execute shader */
glUseProgram(program);
glDispatchCompute(1, 1, 1);
glUseProgram(0);
glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
float result = 0.0f;
glGetBufferSubData(GL_SHADER_STORAGE_BUFFER, 0, 4, &result);
cout << "result = " << result << endl;

i'm trying to create random values in shader, the first thing i found was the "float noise1(float)" function. i tried it, it didnt work, i tried all of those 4 noise functions, none worked, so i read about it in the registry:

https://www.khronos.org/registry/Ope...ml/noise.xhtml

which appears do be wrong or deprecated (or i'm doing something wrong), so i read about it in the GLSL 4.5 specs, and it says these functions are all

on the web i found some pseudo-random stuff:

http://stackoverflow.com/questions/4...tions-for-glsl

but isnt there another way to generate random (or pseudo-random) numbers ??

currently i'm using:

Code glsl:

float rand(vec2 co)
{
return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
}

int current_level = 0;

uniform int lod_level = 1;

while(current_level < lod_level)

{

current_level++;

gl_Position = pos;

EmitVertex();

}

uniform int lod_level = 1;

while(current_level < lod_level)

{

current_level++;

gl_Position = pos;

EmitVertex();

}

This isn't all the code, but commenting out gl_positon or changing the while loop to;

while(current_level < 2)

will then work

Hope someone can shed some light on this. Thanks. ]]>

i have a Problem to understand this minnimal shader:

"#version 400\n"

"in vec3 vp;"

"void main() {"

" gl_Position = vec4(vp, 1.0);"

"}";

How does OpenGL know to put the vertex into the vp. variable.

I could write womething linke:

"#version 400\n"

"in vec3 vp;"

"in vec3 vp1;"

"in vec3 vp2;"

"void main() {"

" gl_Position = vec4(vp2, 1.0);"

"}";

so for me it is not celar how OpenGL passes a vertex to the shader...

Best Regards,

TinTin ]]>