Where to start to get familiar with compute shaders?

Started by
5 comments, last by Infinisearch 6 years, 9 months ago

Hi Everyone! 

 

I am at a point where I know what a compute shader is, but still can't get my head completely around it. I feel like I would need to get my hand dirty, but if I look at stuff like the Mini Engine my brain hurt a bit... do you have a path of useful things to implement in cs that will gradually give me a better understanding and a more solid grasp of CS? 

 

Thank you!! 

Jacques 

Advertisement
40 minutes ago, parroteye said:

but if I look at stuff like the Mini Engine my brain hurt a bit...

Are you talking about the DX12 mini engine?

Anyway as far as compute shaders go how about starting off with a blur filter?

-potential energy is easily made kinetic-

I would just create a simple program that initializes a device and creates and calls a compute shader from a file.

Then I would do some "procedural" image generation, nothing fancy starting from : "casting rays from each pixel and intersecting them with s hardcoded sphere in the shader".

Otherwise if you're not specifically interested in Graphics APIs. I would start with CUDA or OpenCL.

1 minute ago, ongamex92 said:

I would just create a simple program that initializes a device and creates and calls a compute shader from a file.

Then I would do some "procedural" image generation, nothing fancy starting from : "casting rays from each pixel and intersecting them with s hardcoded sphere in the shader".

Otherwise if you're not specifically interested in Graphics APIs. I would start with CUDA or OpenCL.

I already have baseline renderer. I just wanted to learn the "CS way of thinking" while implementing something. I mean I guess I know how to map an existing pixel shader to a compute shader... I think... but I would really like to get into the right mindset to leverage compute

 

12 minutes ago, Infinisearch said:

Are you talking about the DX12 mini engine?

Anyway as far as compute shaders go how about starting off with a blur filter?

Yes, I mean the Microsoft one, I was googling for samples and ended up on their github page. Looks great, but a bit too advanced for me at the moment :(

As I wrote above I think I know how to translate a pixel shader into compute, I feel I don't know how to take advantage of compute or to start thinking properly about them and not 1 thread == 1 pixel. I keep reading about how compute allow for new and interesting things and that's what I would like to start looking at..

 

I guess with a blur I could try sharing samples? Do you have any resources that explain in a noob friendly way (this is my first real approach to graphics programming) how compute helps in that? 

I have read post around mentioning access patterns and stuff like that, is there something you suggest to read around this ? Any basic guidelines?

 

 

Thanks! 

Jacques

37 minutes ago, parroteye said:

As I wrote above I think I know how to translate a pixel shader into compute, I feel I don't know how to take advantage of compute or to start thinking properly about them and not 1 thread == 1 pixel.

The difference is that you have access to LDS memory. This gives you 2 advantages:

* Share data between threads in the same workgroup.

* Use LDS to cache data from main memory.

 

Blur is agood example to utilize both, (code in GLSL):


	layout(local_size_x = 64,local_size_y = 1,local_size_z = 1) in;
 
[...]
 
shared float lds[64+2];
 
void main(void)
{
    uint threadID = gl_LocalInvocationID.x;
    uint leftmostPixelIndex = gl_WorkGroupID.x * 64;
 
    float leftNeighbour = pixelBuffer[leftmostPixelIndex + threadID - 1]; // we store this value in a register
 
    lds[threadID] = leftNeighbour; // we put the value also to LDS so other threads have access
 
    if (threadID < 2) lds[threadID + 64] = pixelBuffer[leftmostPixelIndex + threadID + 64 - 1]; // read two more values
 
    memoryBarrierShared(); barrier(); // now we have loaded 64 pixels plus left and right neighbour

 
    float blurredResult =
    leftNeighbour * 0.25 + // we could load this one from LDS, but it's a lot faster to use the register we have anyways.
    lds[threadID + 1] * 0.5 + // our pixel
    lds[threadID + 2] * 0.25; // right neighbour
 
    blurredPixelBuffer[leftmostPixelIndex + threadID] = blurredResult;
}
	

 

 

But blur is too simple to get excited. I found the Chapter in OpenGL Super Bible really good and recommend it even if you're using DX. (Talks about building blocks like prefix sums, shows how to solve N-Body problem, ...)

 

This thread will be of use to you, the three links in my post in particular.  They describe how GPU's work, that might get you more comfortable thinking about compute shaders.

www.gamedev.net/forums/topic/688688-how-to-learn-about-how-graphic-cards-work/

One of the links will eventually lead you to this page: https://fgiesen.wordpress.com/2011/10/09/a-trip-through-the-graphics-pipeline-2011-part-13/

Also if you're new to graphics programming it's common to start with DX11.  DX11 will also have alot more online tutorials than DX12.  I don't have the time right now but I'll see if I know of any compute shader tutorials.

 

-potential energy is easily made kinetic-

This topic is closed to new replies.

Advertisement