2

I am working on a procedurally generated terrain simulator using the marching cubes algorithm and simplex noise. The problem with my current program is that it assigns a value to each vertex and generates triangles one at a time. After doing some research, I decided that using Compute Shaders(One for assigning values to each vertex using simplex noise, and one for creating the triangle mesh) will significantly increase the performance. I have a very basic idea as to how I want the shader to interact with my program but I have no clue how to make it.

UseNoiseComputeShader();//get output of ComputeShader.
output = noiseOutput;

UseMeshComputeShader(output);//get output of ComputeShader.
float[] vertices = outputMesh;

//output vertices to VAO and draw

I have seen some people use Compute Shaders for similar projects online but they were using Unity and HLSL so I was wondering if it was at all possible using OpenGL and GLSL.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Ethan Ma
  • 69
  • 3
  • 7
  • Please note, while [Unity](https://unity.com/de) is a game engine, OpenGL is a [specificationy](https://www.khronos.org/registry/OpenGL/index_gl.php) The OpenGL API is provided by the graphics driver. On some platforms Unity may use OpenGL (ES) under the hood. – Rabbid76 Jul 21 '20 at 17:29

1 Answers1

1

In general a compute shader writes to an Image or Shader Storage Buffer Object.

An image can be read and written by image load and store:

layout(rgba32f, binding = 1) readonly uniform image2D img_input;
layout(rgba32f, binding = 2) writeonly uniform image2D img_output;

void main()
{
    ivec2 coord = ivec2(gl_GlobalInvocationID.xy);
    vec4 color = imageLoad(img_input, coord);
    imageStore(img_output, coord, color);
}

To a Shader Storage Buffer Object can be written by assignment or Atomic operations

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • @EthanMa [How to get texture data using textureID's in openGL](https://stackoverflow.com/questions/5117653/how-to-get-texture-data-using-textureids-in-opengl/62965713#62965713) – Rabbid76 Jul 21 '20 at 17:25
  • Would using the cs to write to a image or SSBO be better in my case? – Ethan Ma Jul 21 '20 at 17:51
  • @EthanMa It's up to you a may depend on the use case. But I don't have a rule of thumb about when to use one or the other. – Rabbid76 Jul 21 '20 at 18:12
  • Is there a certain way read my SSBO into a float array? (edit: This question I found on the LWJGL forum is similar to mine [link](http://forum.lwjgl.org/index.php?topic=6777.0)) – Ethan Ma Jul 21 '20 at 18:18
  • 1
    As a very generic rule of thumb, if your data is organized in a regular 2D or 3D grid, and is representable int the data formats provided by images (without weird and expensive conversion or encoding tricks) and your access pattern also profits from 2- or 3-dimensional locality (and this will also depend a lot how you organize your work into workgroups and invocations), you could try to go for an image. – derhass Jul 21 '20 at 18:22
  • @derhass so essentially you're saying that since I am working in a 3D data format I can just read the data from the image and convert it into the format I need? – Ethan Ma Jul 21 '20 at 18:32