Jump to content
  • Advertisement
Sign in to follow this  
taby

OpenGL OpenGL compute shader problem

Recommended Posts

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

Edited by taby

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites

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

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

Share this post


Link to post
Share on other sites

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);
}
	
Edited by taby

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!