How to do it...

Perform the following steps:

  1. In the compute shader, we start by defining the number of shader invocations per work group:
layout( local_size_x = 32, local_size_y = 32 ) in; 
  1. Next, we declare the output image as well as some other uniform variables:
layout( binding = 0, rgba8) uniform image2D ColorImg; 
#define MAX_ITERATIONS 100 
uniform vec4 CompWindow; 
uniform uint Width = 256; 
uniform uint Height = 256; 
  1. We define a function to compute the number of iterations for a given position on the complex plane:
uint mandelbrot( vec2 c ) { 
  vec2 z = vec2(0.0,0.0); 
  uint i = 0; 
  while(i < MAX_ITERATIONS && (z.x*z.x + z.y*z.y) < 4.0) { 
    z = vec2( z.x*z.x-z.y*z.y+c.x, 2 * z.x*z.y + c.y );  
    i++; 
  } 
  return i; 
} 
  1. In the main function, we ...

Get OpenGL 4 Shading Language Cookbook - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.