Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(rendering particles) Should I learn shader or OpenCL?

I am trying to run 100000 and more particles. I've been watching many tutorials and other examples that demonstrate the power of shaders and OpenCL.

In one example that I watched, particle's position was calculated based on the position of your mouse pointer(physical device that you hold with one hand and cursor on the screen). The position of each particle was stored as RGB. R being x, G y, and B, z. And passed to pixel shader.And then each color pixel was drawn as position of particle afterward.

However I felt absurd towards this approach.

  • Isn't this approach or coding style rather to be avoided?
  • Shoudn't I learn how to use OpenCL and use the power of GPU's multithreading to directly state and pass my intended code?
like image 770
BlueBug Avatar asked Aug 07 '12 01:08

BlueBug


2 Answers

Isn't this approach or coding style rather to be avoided?

Why?

The entire point of shaders is for you to be able to do what you want, to more effectively express what you want to do, and to allow yourself greater control over the hardware.

You should never, ever be afraid of re-purposing something for a different functionality. Textures do not store colors; they store data, which can be color, but it can also be other stuff. The sooner you stop thinking of textures as pictures, the better off you will be as a graphics programmer.

The GPU and API exist to be used. Use it as you see fit; do not allow how you think the API should be used to limit you.

Shoudn't I learn how to use OpenCL and use the power of GPU's multithreading to directly state and pass my intended code?

Yesterday, I would have said "yes". However, today this was released: OpenGL compute shaders.

The fact that the OpenGL ARB and Khronos created this shader type and so forth is a tacit admission that OpenCL/OpenGL interop is not the most efficient way to generate data for rendering purposes. After all, if it was, there would be no need for OpenGL to have generalized compute functionality. There were 3 versions of GL 4.x that didn't provide this. The fact that it's here now is basically the ARB saying, "Yeah, OK, we need this."

If the ARB, staffed by many people who make the hardware, think that CL/GL interop is not the fastest way to go, then it's pretty clear that you should use compute shaders.

Of course, if you're trying to do something right now, that won't help; only NVIDIA has compute shader support. And even that's only in beta drivers. It will take many months before AMD gets support for them, and many more before that support becomes solid and stable enough to use.

Even so, you don't need compute shaders to generate data. People have used transform feedback and geometry shaders to do LOD and frustum culling for instanced rendering. Do not be afraid to think outside of the "OpenGL draws stuff" box.

like image 93
Nicol Bolas Avatar answered Nov 14 '22 14:11

Nicol Bolas


To simulate particles in OpenCL, you should try out "Yet Another Shader Editor" / http://yase.chnk.us/ - it takes away all the tricky parts and lets you get down to the meat of coding the particle control algorithms. IN YOUR BROWSER. Nothing to download, no accounts to create, just alter whatever examples you find. It's a blast.

https://lotsacode.wordpress.com/2013/04/16/fun-with-particles-yet-another-shader-editor/

I'm not affiliated with yase in any way.

like image 31
user1141008 Avatar answered Nov 14 '22 13:11

user1141008