OpenGL compute shader problem

Started by
46 comments, last by taby 4 years, 7 months ago

I'm using OpenGL 4.3 to use Compute Shaders. I'm running into trouble. Can anyone spot the problem?


#version 430
layout(local_size_x = 1, local_size_y = 1) in;
layout(rgba32f, binding = 0) uniform image2D img_output;

// see: http://antongerdelan.net/opengl/compute.html
void main()
{
    vec4 pixel = vec4(0.0, 0.5, 0.0, 1.0);
    
    ivec2 dims = imageSize(img_output); // fetch image dimensions
    ivec2 pixel_coords = ivec2(gl_GlobalInvocationID.xy);
    
    float x = float(pixel_coords.x)/float(dims.x);
    float y = float(pixel_coords.y)/float(dims.y);

    pixel.r = x;
    pixel.g = y;
    pixel.b = 0;
    pixel.a = 1;

    imageStore(img_output, pixel_coords, pixel);
}
	

out.tga

Advertisement

Better yet, I posted the whole code on GitHub:

https://github.com/sjhalayka/basic_opengl_compute_shader

I also included a sample output as out.tga

try 

if ((fptr = fopen(file_name, "wb")) == NULL)
4 hours ago, Krypt0n said:

try



if ((fptr = fopen(file_name, "wb")) == NULL)

This was the problem. Thank you! I forgot to set it to binary output.

Wouldn't OpenCL be a simpler solution? It's designed for computing, eg on GPUs.

It would be simpler to run it on cpu, with just c++.

But not knowing his goal, suggesting alternative solutions is not helpful.

I used to use vertex/fragment shaders to do GPGPU, but now I have an OpenGL 4.6-compliant machine (Ryzen 3). The simplest solution is now compute shaders. Getting data from uniform textures is super easy, no more messing around with texture coordinates.


#version 430 core
layout(local_size_x = 1, local_size_y = 1) in;
layout(binding = 0, rgba32f) writeonly uniform image2D output_image;
layout(binding = 1, rgba32f) readonly uniform image2D input_image;
// see: http://antongerdelan.net/opengl/compute.html
void main()
{
    ivec2 dims = imageSize(output_image);
    ivec2 pixel_coords = ivec2(gl_GlobalInvocationID.xy);
	float x = float(pixel_coords.x + 1)/float(dims.x);
    float y = float(pixel_coords.y + 1)/float(dims.y);
	vec4 pixel;
    pixel.r = x;
    pixel.g = y;
    pixel.b = 0;
    pixel.a = 1;
	// pixel = imageLoad(input_image, pixel_coords);
	imageStore(output_image, pixel_coords, pixel);
}
	
On 9/8/2019 at 8:20 AM, Krypt0n said:

It would be simpler to run it on cpu, with just c++.

But not knowing his goal, suggesting alternative solutions is not helpful.

I have a CPU-only Marching Cubes implementation, which is used to make an isosurface out of a 3D fractal set:

https://github.com/sjhalayka/marching_cubes

I then accelerated this using a fragment shader. It only works on AMD graphics systems:

https://github.com/sjhalayka/qjs-isosurface

Now, I am wanting to accelerate this using a compute shader, so that it runs on all OpenGL 4.3+ compliant systems. not just AMD.

On 9/7/2019 at 7:40 PM, taby said:

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

This means only one thread of a wavefront / warp of 64 / 32 threads will do any work, the majority of your GPU will do nothing.

(Maybe you do it just for testing, but worth to mention - unlike PS you are responsible to saturate threads yourself with CS.)

EDIT: oops, deleted - wrong Thread

This topic is closed to new replies.

Advertisement