What can and can't be done with shaders.

I’ve been playing with openGL for about 4 years now. I am no specialist at it but I can make a fairly optimized Minecraft clone or a model renderer.
I am right now working in a 2d game with libgdx, and wanted to add some fancy light effects, like the ones seen here:
https://youtu.be/GPbLJ_QDOgo?t=214 (Revenge of the titans)
(the particles, the laser aim, etc)
or the effects that come out of their weapons here:
https://youtu.be/x66GUr-Lg8Y?t=215 (Soul Calibur)

For the first video I could think of some ways of doing it without shaders, using only textures and stuff, although it probably wouldn’t look that good. For the second one I actually don’t have an idea. So I thought probably shaders, although with my very limited knowledge of shaders I still have no idea how to implement it.

So, is it possible to do those kind of effects without shaders? Is it worth learning shaders to achieve such effects ? Or everything can be worked around with smart use of textures?

The answer to “should I learn XYZ?” is always… why not?

What do you gain by not learning something?

When I learned shaders, I found two things

  • I need to do stuff my self now, that OpenGL did for me previously
  • I can control every bit of the calculation, which I could not, previously

So it’s a medal with two sides - you got to do stuff yourself, but you can do stuff in the way you want to.

I don’t thinkt here is anything you could do before and can’t do with shaders. You have to write the code, but it allows you to do a lot more than you could, previously.

Free time to do stuff.

Because I have a job and family and my supply of time is limited.
I could learn mandarin to read originals of Sun Tzu and fully appreciate its beauty, but I prefer to under appreciate it and read it in English.

I could try doing it without shaders, but I’m trying to find out is the cost X benefit of learning it. In other words, is worth my limited supply of time or should I keep doing it without it ?

Although I do not have a lot of shader experience I can only encourage the usage of them :wink:

It takes quite some time to learn, but as far as I can see the principle behind shaders is the future of game development.

I guess my point is: if you’re curious about it, then taking a day or so to learn more about it is only in your interests. Or if you’re the opposite and don’t feel like learning more about it, then that’s fine too.

Are shaders useful? Of course. Are they more useful than another option? It depends entirely on what you’re doing.

For smaller 2D stuff, you might want to check out Box2DLights.

This. Since I learned shaders, I think so, too.

Regarding comparison of time and effort between two possible activities:
First, there is no one here who can tell you that you need exactly 17 days and five hours to comprehend shaders, and that you would be better off learning Mandarin in just 15 days.
Likewise there is no one here who can tell you exactly a measure of the “gains” that you get by using shaders.
Do what you like to do in your spare time and what you are most curious about and have fun doing.
Don’t compare activities by properties such as time/effort to “finish” it. That thinking should be reserved for your day-to-day job where efficiency and money/time counts.
Your spare time should be completely free of any such thoughts, at least that’s my opinion. :slight_smile:
In my opinion, if people ask whether they should learn something or not then their curiosity is just not big enough; because if it was, they would just go ahead and learn/do it, and simply not care about how long it will take them.
Even if they had just 10 minutes a day to invest time into it. As long as they have fun doing it and remain curious of what there is to come, then nothing else matters.
It’s also not the case that there will be some point in time where you say: “Okay, now I’ve successfully learnt shaders.”
There is just so incredibly much information and techniques out there and ways in which shaders play together with the rest of OpenGL that there will be learnable stuff for many many years. :slight_smile:

Yes, I totally agree. Innate curiosity, interest and of course sociological factors should be the only things determining what people do in their spare time.

Ok guys, I edited the part of my initial post where I asked " Is it worth learning shaders to achieve such effects ? " to see if I can get other type of responses.

Can we get some example of things that can be achieved with shaders? What is it used for in a 2d game environment?

Shader hardware nowadays is so incredibly flexible and general-purpose that asking “what can and can’t be done with shaders” is like asking “what can and can’t be done with a programming language?”

There are only a few restrictions that seemingly get worked out with any new graphics card generation.
One is: Shaders still can’t use recursive calls (however, I might be wrong on this, but I think this restriction is still up).
Another is: Shaders cannot alter the primitive type/topology of the vertex data fed to them. If you render GL_TRIANGLES then this is what the primitive assembly and rasterization stage will process. And if you have a geometry shader then also that can only output a static primitive type.

Other than that, shaders can nowadays read and write all kinds of memory.
Nowadays, just the question remains: “what is a shader stage ought to do, typically?”

Vertex shaders are ought to transform vertices and compute some interpolated stuff for the fragment shader to consume.
While fragment shaders “usually” output one or many color channels on some framebuffer backed by a texture or a renderbuffer or by the backbuffer of the GL context/window.
This kind of programming model is well suited to implement one thing that shaders are particularly good and invented for: an own lighting and shading model.
Usually, with the fixed-function pipeline in OpenGL you are limited to Gouraud shading.
With shaders you can do Phong or Blinn-Phong or whatever funky local shading model you can think of.

But any of those shader stages and any of the other unmentioned shader stages, can read from and write to anything nowadays. This makes shaders really like a general purpose language running on a platform, which is the graphics card, with limited I/O support (i.e. no sockets, no file system) and a certain programming model (inherently multithreaded).

All this flexibility then lead to “OpenGL compute shaders,” which nowadays also are not that much different from other shader stages, except they do not get fed any data with a draw call, like the other shader stages, but the client has to provide data for it herself by specifying buffer objects, textures and images to read from (and/or write to).

So, shaders now can do anything. But not everything you can do with shaders is also the most performant way to do it. But this entirely depends on what it is in the end that you want to achieve. First, have a clear vision of the graphics design that you are after and then try to find the most performant way to realize that, either with or without shaders.
But that however necessitates that you know what a shader can do.
And that only comes with alot of practice.

So: Yes, do learn shaders. :slight_smile:

Yes, you can do those types of effects without shaders. Now a follow up question you might ask is how to do it, and you would with clever use of textures, transparency, geometry(primitives/meshes), and math.

Now I will say that if you’re planning on building an engine for your game that you should definitely take the time to learn shaders. The reason is that your game will perform better* with less* work. *usually

Note: if you’re making the move to shaders I also recommend that you abandon legacy openGL at the same time. I didn’t and have ran into tons of quirks as a result.

Thank you all for your responses, it helped me to opened my mind about shaders .

You are right. I will make a follow up question on how to achieve the effects I wanted.

I just started out with shaders not that long ago. When I started out it was a hell. For this reason I postponed it but sooner or later you got to move on anyway. So I gave it a few more tries and once you succeed with it, you’ll see it actually isn’t that hard. It is every piece of documentation on the web that make it look like impossible. Tutorials are often written for c++ and therefore have far more code and lots of different code compared to Java.

That being said, if I take a look at what I actually had to do to get a basic VBO with a fragment and vertex shader up and running it is like 1/10 of the examples on the web. Something you really gotta watch out for when you follow tutorials. Besides that I have to admit the quality of my graphics has improved big time. Not to mention lights. They took a while for me to implent but with shaders you are not limited to 7 lights. You can choice the amount you desire. One very big advantage.

But there is one another advice I like to give that really helped me out a lot. A lot of people suggested me to start with modern OpenGL straight away and not look at legacy OpenGL at all. I disagreed back than, and I do now. My experience with the fixed function pipeline helped me out soooo much that I became familiar with shaders way faster then expected.

Go to www.shadertoy.com there are tons of cool example of what you can do with shaders there (albeit very complicated examples)

But much of the complexity of brute-force shaders is because they’re brute force.