It's very important to note that in modern versions of OpenGL (3.3+) qualifiers "attribute" and "varying" are deprecated and replaced with "in" and "out". And instead of gl_FragColor we are encouraged to use user-defined "out" variables. The logic of writing shaders is still the same, but none of this code will work in modern versions if allowance for the differences is not made.
I was about to write exactly this. This is indeed pre glsl 1.3 syntax so very old. 1.3 and later uses above syntax and is incorporated into WebGL2 since 2017. This is coupled with OpenGL ES 3.0 which might be worth noting. For anyone interested in these facts, the above video of course still applies ❤
I didn't realise how long WebGL2 has been out! I think P5js only started supporting it recently. I've used the newer syntax in other projects, but everything web related I've been using the old syntax. Might be time to change that! Thanks for the comment!
I guess what I noted is quite obvious to an experienced user, but me being a noob with OpenGL, it was strange why compiler throws so many cryptic errors in my python+pyglet environment. I saw that built-in classes use default shaders with "in" and "out" qualifiers. Asked ChatGPT and it was like "Dude, you're using an ancient syntax!"😅 Of course, all the love for the author, video is still relevant and definitely a must watch for anyone starting to mess with shaders!❤ @@BarneyCodes
if anyone wants traction for their channels, coding for marmoset will get you some traction, just a heads up, i only need to code 1 shader for myself and that is the "engine", i am using to get the results i want, it combines hlsl and glsl.
Hahaha yea sorry about that! There's just so much that I wanted to include in the video! I think shaders are just one of those things that take a few goes to sink in. Thanks for the comment!
@@Seb00leanGlad to hear you're finding it helpful! I'm thinking I'll do smaller videos that focus on one topic at a time instead of another video like this one. That'll hopefully mean they're more frequent but will also let me go into a little more depth each time. A few weeks ago I put out a video on how to use a noise texture in a shader if you want to check that out! I've got a few ideas such as using normal maps and creating very basic lighting, using vertex shaders to manipulate objects and a few others. Is there anything you would like to see cover?
For the first time in my life, I understood what that Shader code was all about. Thank you for the quick explanation. I'll definitely need more couple of years to be able to write my first shader script to create a simple black circle on screen because is too hard, but this video is definitely one of the best to see about the topic.
Thank you so much! You've got this, it took a couple of tries before it started to sink in for me as well, I think that's very normal because it's definitely a different sort of programming from what most of us are exposed to, but I promise it does get easier! If you have any questions or need help, please feel free to reach out!
Finally finished! This video took ages to put together, so if you enjoyed it, it would be great if you could give it a like and share it with anyone you know who needs to learn about shaders! Also, I forgot to mention the amazing www.shadertoy.com which is another great resource and an easy way to get stuck into shader programming in your browser. If you'd like to see what else I'm working on, it would be great if you could wishlist my game Star Mining Co. on Steam! s.team/a/2584800/
oh yeah, don't forget graphtoy, it's a graphing calculator specifically with glsl in mind !! there's also a few websites in kishimisu's video on shader art coding but youtube tends to delete comments with links, they're still worth checking out though!!
This was just the video I needed to get started with shaders. I knew very little about them or what they even did or were capable of, but this video really showed me, on a beginner level, just how indepth this really goes. Thank you so much for all the effort you put into this video. You explained so much indepth, while also making it simplistic, while not watering anything down. The care and passion you put into making this really shows.
Thank you so much! I'm so glad you found it helpful, shaders can be an amazing tool so getting more people creating and playing around with them is exactly what my goal was! All the best on your shader journey ahead!
I'm going through a similar experience drawing primitives in MonoGame, and I'll say it really is handy to have this context because the documentation and tutorials for this stuff are really obscure! Thanks @BarneyCodes for the tutorial, seriously
Hi, normally I really don't comment on TH-cam videos. But man, this introduction was EXACTLY what I needed to get started with my university project. Thanks a lot man, keep up the good work :)
I'm not really working in game dev but watched your video out of curiosity and it was a pleasure and nice relax for me. You have a great talent to explain things :)
Only half way through but this is gold. I've dabbled a little with real basic shaders in opengl using c++ but all that VAO, VBO stuff gets pretty old very quickly. I've also dabbled in p5.js a fair bit but never used shaders (didn't even know until recently that shaders in p5.js were possible). Pretty excited to see what I can create with p5.js shaders. Many thanks for getting me started, much appreciated.
Thank you so much for putting this video together! Fantastic explanation of the SDF and I also liked the part about the logic a lot! You touched so many topics with such a great presentation, really amazing man!!
As a software engineer that uses the standard python and java / C# dabbles, this is like a wizard learning a different school of magic lol, so interesting
Fantastic introduction to shader programming! Thank you for this :)❤ I kept wanting to know more about everything you talked about, it really could have been an even more thorough introduction. Looking forward to a potential series or just more on this wondrous field of programming.
Thanks so much! It was pretty hard to decide what to keep in and what to leave out and it still ended up being a long video! I do plan on doing more videos with shaders in the future, is there any topic in particular you'd like to see in more depth?
Getting into graphics programming as a hobby thanks for the video, You're a great explainer. I wish I had gotten into this sooner instead of web development theres a lot of neat stuff to understand
This is absolute fucking gold. I love you man. Crisp, clear and straight to the point. Your humour was never intrusive like with other tutorials and you even gave links for every checkpoint in the video. God bless you man, this was a fantastic video and I wish I could like it 10 times lol ♥
This is amazing! I am going through SimonDevs course but there is alot of clutter with threejs to make stuff happen, but using p5js is way simpler and really makes the javascript code as a secondary element and really focus on the glsl side of things. I hope this series keeps going.
I love Simon's videos, they're really great! But yea I think P5js is pretty much as straightforward as it gets for setting up shaders which is why I use it so much! There are some limitations with it though, like the ModelView matrix being combined in the vertex shader so that you can't do world space calculations. What topics would you like to see in the next video?
Neat video! I went through the (current state of the) book of Shaders. I later saw people being critical on it in forums, saying it has some limitations, and your video helped me see one of it. The book of Shaders tackles pretty much only the contents of the .frag files, but thanks to the "circles array" example you made, I realize the importance of the "CPU" side of things, and passing custom variables.
The only bad thing about this video is that it didn’t exist yet back when I was learning this the first time. From now on this will be the introduction I recommend. 👏👏👏
Im a 3d artist who has been wanting to get into technical art seeing that it's tight competition to get a job doing 3D stuff, but everyone and their mother needs like 5-10 tech artists. This is my gateway into securing my future. Did 3D stuff or 7 years under my last employer, but it's still tight as ever to get in anywhere else.
26:50 you can also use the spread operator, which has the added bonus that it works with any number of arrays, i.e.: const colours = [...col1, ...col2, ...col3]
it's insane just how much stuff inigo has done for shader community. You can't read about them for five minutes without running into a reference to him
Brilliant video, really a great way to visually teach complex topics. Very informative and I can appreciate how much work this video must have taken all in all. Also the two Edges was a nice touch :).
Thanks so much, I'm glad you enjoyed it! And I think you're just the second person to comment on the Edge joke, good to know there are a few cultured people out there hahaha
This video is the best one I've found so far. The pro tip around 32:00 makes a ton of sense but I hadn't thought of it yet, glad you caught me writing my if statements lol. For context I've been working on my very first shader today! Coincidentally also in P5js like your examples. I came here because of confusing syntax errors I've been getting and although I'll have to troubleshoot those some more (or maybe explicitly define the webGL version the syntax I've learned is correct for? 😅) I think this is helpful. The project I'm writing a shader for is rendering the mandelbrot set, moving the load to the GPU as an upgrade to my current p5 sketch for it. The current version I already built has scrolling to zoom in and mouse-drag panning support, as well as using arrowkeys to change the canvas resolution on the fly... but especially at higher resolutions it's taking minutes to render, so it's far from responsive lmao. I failed to make the rendering function render asyncronously to a graphics object *and* have p5 move or zoom in on the last rendered frame while the next one loaded though, I guess because it's not waiting for 1 long operation or fetch and only for a bunch of small calculations, so doesn't ever hand control back while awaiting, so p5 just shows the preview change for a split second after the next image finally renders and then immediately slaps the new one it's been waiting for over it lol. So I'm skipping that idea since I don't feel like learning web workers for it and instead going straight to shaders for the real performance gains! Before watching this video I'd already ported over (from JS to GLSL) my functions for complex number multiplication, mandelbrot iteration, checking if points are inside the set (need to change to using step function now 😆😅), and for coloring points based on the number of iterations each complex number takes to escape... I was trying to pass some uniforms into it for things like the number of iterations and user input for zooming, but I haven't gotten it to load once without getting syntax errors 😒. I mean I also haven't finished some of the basic features like properly mapping between the fragcoords to mandelbrot space *while* taking the zoom input into account, but I made a map function for it that I was about to test when I got weird syntax errors while trying to load my shader for the first time. Previously in the non-shader cpu version I was mapping from pixelcoords to the complex plane (within a range depending on the zooming and panning) so it'll be a bit different and I'll need to make sure I don't stretch the image by the aspect ratio again like I initially did in the cpu bound pixel version, but it's a work in progress and the whole point of this project was for fun and learning!!! 😁
Rendering fractals with shaders is a lot of fun and a really great project to get familiar with shaders! If you get stuck, I've got a video on my channel about making a mandelbrot shader ;)
i would like you to know that this video is SO WELL MADE and beautifully explained, that me, a person with 0 prior experience to coding, no idea what the brackets even do, are able to understand the concepts perfectly well. Thank you so much, my journey of learning how to make games is looking a lot less dim now 😭
Nice video. Just one nitpick. The grayscale is incorrect, since human eyes don't treat colors equally. It depends on the use case and the color space but the general simple formula is: 0.2126 R + 0.7152 G + 0.0722 B. There are ones with square root and squaring of colors that aproximate it better, but are much more expensive in terms of performance.
I really wasn't expecting this video to be this difficult to adapt for Java Processing, but it's proving to be incredibly hard to make this example code work there.
What issues are you running into? There are definitely some differences in getting the environment set up, but the actual shader code should be usable between both (you might just need to check what uniforms/attributes are available by default in Processing vs P5js) If you're getting any error in particular I can try to give you a hand to get it sorted out!
@@BarneyCodes That's what I thought as well. When I first copied everything down the shaders just refused to compile, so I figured the names of the attributes must just be different. It took me a while to find any documentation about what attributes were available in the Processing environment (the official site has pretty much nothing that I could find about implementing shader functionality despite this being a feature for years at this point) vs P5, but eventually I found examples in an old backup of a blog that went pretty in depth about how this stuff worked for Processing. After replacing all of the attributes with their correct counterparts the shaders were finally able to compile and run, but specifically the vertex shader just doesn't have the expected behaviour. It refuses to shade anything in 2d that isn't a square that fills the whole screen. I've been tinkering with it off and on for a while trying to figure out what in the implementation is different enough to make this not work, but I don't have enough experience or documentation support to really know what's up here
I've only used shaders in Processing for full screen effects, so unfortunately can't be too much help there sorry! A few of the example Processing shaders I've seen have different defines at the top, eg: #define PROCESSING_TEXTURE_SHADER or #define PROCESSING_COLOR_SHADER maybe something like that helps? I know Processing also lets you either supply a vertex shader or just use their default one, not sure which route you've been going down (maybe both already!) but it could be good to try them both out to see if that helps at all. As a last resort, the Processing source code is all available on GitHub so you can also dive into that (if you're feeling brave enough!) to try and figure out what's going on and why it might be behaving strangely! Sorry I don't have a more concrete bit of advice for you, but hopefully there's something in there that's helpful? Good luck!
Great video. Wish I understood it. I get the general concepts but a lot of the technical stuff flies over my head. Having a tough time learning basics of coding because I don’t have anything to code, a lot of tutorials/learning resources just feel like I’ve been given a list of a languages syntax.
It can definitely be that way when you're first getting started! Thankfully you'll start to notice that there are a lot of similarities between languages so once you get a bit more familiar with one, it'll be easier to pick up more. Keep at it and you'll get there! I highly recommend finding a tutorial for something that genuinely excites you, it makes it a lot easier to stick with it! (For me it was a really really basic game, I think it was a bit like space invaders or something!)
@@BarneyCodes I guess a follow up question would be where would/should I do something like that? Would you do that in a game engine like unity? Or just barebones? I feel like using a game engine might be a detriment to some of the learning process because the bells and whistles are already there, but I also don’t want to give myself an additional level of frustration for no reason.
@jackmalcolm I think if your aim is to learn to code, I'd probably recommend not using an engine. A really great starting point would be to check out "The Coding Train" here on TH-cam. Dan, the bloke that runs the channel, has hundreds of videos for all skill levels and his enthusiasm is infectious! In fact I think he may have pretty recently released a course on getting started programming, which could be perfect! EDIT: It's called "Creative Coding for Beginners - Full Course!" if you want to look it up :)
Lost me at swizzling, but guess it helps to have 3d coding knowledge. Really hard to find a simple tutorial that goes deep on the basics. I really like the style of this vid and you had me for the first ten minutes. I will persevere.
Sorry to hear that! Honestly, you can get away with not using swizzling so I'd encourage you to just skip that part and keep going! All you need to know for swizzling is, if you see something like vec.xxy, it's the same as saying vec3(vec.x, vec.x, vec.y) Hope that helps!
Great Video! I saw another comment talking about Minecraft's Vanilla Shaders (Vanilla means that it's the default version, without any API like OptiFine), and I think it would be interesting to make a video about it. Two great examples are bradleyq's shader-toolkit and VanillaPuddingTart. (The default files can be obtained by extracting Minecraft's jar file)
The edge joke cracked me up!! Would be interested if Shaders are used for physics and engineering math calculations ? At the end of the day it’s just doing a load of parallel float (or other) ops.
I'm glad somebody got it, it's the first time someone mentioned it hahaha That's a really great question! There's a type of shader called a compute shader that is often used for exactly that! Compute shaders are basically a really flexible type of shader where the programmer gets to define most things about how it is run, and can read/write into "shader storage blocks" so you're not limited to just outputting to textures and you don't need to render geometry to trigger it to run. Before I learned about compute shaders, I did try to use textures to store particle positions but I couldn't get it to work too well. I'm sure that it's possible though!
This introduction explains al ot about shaders even tho its just an intro it is really worth watching oh also i wanted to ask is it possible to learn GLSL and opengl at the same time?
Glad you found it helpful! I definitely think it's possible to learn both at the same time, in fact it's probably necessary to learn a little bit of GLSL to do anything with OpenGL, but I think it's probably best to just do the basics of one, enough to let you explore the other, and then you can come back and learn the first one properly, just so you're not too overwhelmed!
Great video, thanks I had some shaders in uni, but I didn't really get it Hopefully I'll start using them more often and learn them, but this video is a great introduction
Great video! A couple questions I have. If shaders are meant for the GPU, what happens when running on a system with only a CPU? For the background image, is there a difference in handling png, jpeg, etc? Or are they passed as just the pixel data? For video games, are shaders drawing everything? Or do they receive the frame (like the background image) and then apply additional changes to that? This is the first shader video I've come across, so I'm sorry if these are commonly answered questions.
Really great questions! Most mordern CPUs will have some sort of on-board graphics which is essentially like having a mini GPU built into it. A dedicated graphics card will be a lot faster and usually perform better too. You can get CPUs without graphics in built, but they're the exception not the norm. The CPU is where the work of opening/reading the files happens, and it then will just pass the pixel data to the CPU, so there shouldn't be any difference between different file types. If you're doing it yourself (ie not using an engine/framework), you'll have to set what form that pixel data takes using the graphics API you're using (this will be things like whether its RGBA or ARGB etc) In games, shaders will be drawing everything, but if you're using an engine or framework they will have shaders that do pretty much everything for you, you'll just tell it what models/textures/sprites to use and it will handle the rest. You'll almost always be able to write you own shaders though, if you need to do something a little obscure or want a custom effect. I hope that helps!
@@BarneyCodes Yes that helps a bunch! I did look some other stuff up and it seems there are more than just vertex and fragment shaders. Are the others just variants of these two that focus on specific aspects which could be combined into the base 2? Also, an additional question. I know NVIDIA applies their DLSS stuff that uses AI to make games look better. Are these just game specific shaders that are created using machine learning? Or does the regular image render from shaders, and then have another round in the GPU where DLSS polishes up the finished frame?
That's great to hear! Yea there are a bunch of types of shaders, and they are all their own unique thing and are used at different steps. You hear of vertex and fragment shaders the most though because they are required to draw stuff on the screen, where as the others are optional. The most common non-fragment or vertex shaders are probably geometry and compute shaders. Geometry shaders basically lets you turn a vertex into a group of vertices (eg you could turn a single point into a cube). Compute shaders are essentially just a way for you to use the parallel power of the GPU outside of the rendering process. It uses Shader Storage Buffer Objects (SSBOs) to transfer data between the CPU and GPU, so once the compute shader is finished you can read the SSBOs for the output and then use that information however you want. I don't know too much about DLSS but I had a quick look and it sounds like it's all done AFTER the rendering has finished. I think the idea is that you render to a lower resolution so that it's really fast, then DLSS upscales that smaller frame to a larger resolution. It uses special hardware on modern GPUs that is purpose built for running AI!
"For video games, are shaders drawing everything? Or do they receive the frame (like the background image) and then apply additional changes to that?" Yes, shaders are drawing everything, but at the same time, for things like transparency, you can enable blending (glEnable(GL_BLEND)), and then it will combine your calculated color with that of what you have drawn previously. Unfortunately, transparency in 3D is a complicated topic, so I'd recommend avoiding it when starting.
More specifically HLSL is used by Direct3D, while GLSL is used by OpenGL and Vulkan. It is also worth noting that crosscompilation through for example spir-v is possible which means you really only need to write 1 language (typically hlsl) to support a wide variety of graphics apis.
@@BarneyCodes The way I do it in my game engine is write everything in HLSL. Then i use glslang to compile to Spir-V. From there i use spirv-cross to "decompile" to either GLSL that targets opengl or vulkan; at which point you can use the opengl commands to compile it or precompile back to target opengl spirv or vulkan spirv (yep.. they're not compatible with eachother; they have their own spirv flavour). You have to take this decompile step because the HLSL resulting spir-v is not compatible with opengl. Supposedly it works with Vulkan but i couldnt get it to work. But with this it works flawlessly across d3d11, 12, opengl 4.6 and vulkan 1.2. The decompiled GLSL isn't even half bad either.. it looks pretty clean.
@@ruadeil_zabelin Thanks for explaining! I'm guessing your engine allows people to choose which renderer they're using then? If so, that's impressive! The only other scenario I can think of where you might need to do something like this is if you're porting a game from one renderer to another. Are there other use cases for such a thing?
@@BarneyCodes You can pick a renderer yes, but I made it so it just suggests what I think is the "best" supported one for that platform. So on windows it always defaults to d3d11; on linux its Vulkan, on mac its opengl (though i might try metal someday). I basically studied how unreal engine handles their render api abstraction and found that its basically very similar to the d3d11 api. I wrote a similar interface and just implemented all the function calls for all the graphics api's. If you choose the abstraction well its not all that difficult to do. The only hurdle was the shaders so the possibility of crosscompiling was really nice.
This is amazingly helpful. I’ve been having hell of a frustrating time with OpenGL, and this can aid in my woes 😁 In following along, you’re passing pos in the shaders. Where is the value of it coming from? I do understand what’s going on, but having that bit of info known helps. Am I missing it somewhere?
Glad you've found it helpful so far! I think I glossed over that in the video a bit. Basically the position value is being set in the vertex shader and is then passed through to the fragment shader (this is why the variable name in the vertex and fragment shaders have to match, because they're being used to pass the value across!) When sending geometry to the GPU to render, you have to specify the position of the vertices, but you can also specify the "UV" coordinates, which are basically the coordinates you'd like that vertex to use when looking up a texture, and this is the value I'm using to set the position variable in the vertex shader. How you set the UV coordinates from your CPU code will depend on what environment you're using, for P5js, they're set automatically! Since the UV/texture coordinate is per vertex, they get set as an attribute, which is only available in the vertex shader, which is why we need to set up the varying variable to pass the value into the fragment shader. I hope that makes sense, it's a bit of a rambling answer! If you've got any follow up questions, please just ask :)
I haven't really explored the 3D side of things too much yet, but it's definitely something I plan on getting into! I'll be sure to make a video showing what I've learnt when I do! Thanks for the comment :)
It's very important to note that in modern versions of OpenGL (3.3+) qualifiers "attribute" and "varying" are deprecated and replaced with "in" and "out". And instead of gl_FragColor we are encouraged to use user-defined "out" variables.
The logic of writing shaders is still the same, but none of this code will work in modern versions if allowance for the differences is not made.
Good call! I think WEBGL is a little behind, but I think that's about to change with WebGPU and WEBGL 2. Thanks for the comment!!
I was about to write exactly this. This is indeed pre glsl 1.3 syntax so very old. 1.3 and later uses above syntax and is incorporated into WebGL2 since 2017. This is coupled with OpenGL ES 3.0 which might be worth noting. For anyone interested in these facts, the above video of course still applies ❤
I didn't realise how long WebGL2 has been out! I think P5js only started supporting it recently.
I've used the newer syntax in other projects, but everything web related I've been using the old syntax. Might be time to change that! Thanks for the comment!
I guess what I noted is quite obvious to an experienced user, but me being a noob with OpenGL, it was strange why compiler throws so many cryptic errors in my python+pyglet environment. I saw that built-in classes use default shaders with "in" and "out" qualifiers. Asked ChatGPT and it was like "Dude, you're using an ancient syntax!"😅
Of course, all the love for the author, video is still relevant and definitely a must watch for anyone starting to mess with shaders!❤ @@BarneyCodes
if anyone wants traction for their channels, coding for marmoset will get you some traction, just a heads up, i only need to code 1 shader for myself and that is the "engine", i am using to get the results i want, it combines hlsl and glsl.
Only 3 minutes into the video and you answered so many of my questions. Thanks a ton already!
Glad you've already found it useful!
Does anybody else feel like going for a second helping of this... only a little bit later 😅 The info is dense! Awesome, man!
Hahaha yea sorry about that! There's just so much that I wanted to include in the video! I think shaders are just one of those things that take a few goes to sink in.
Thanks for the comment!
Me!
I go back every week. Such a great starting place. Will there be a part 2 to this? Do you have ideas of what you’d want to include in it?
@@Seb00leanGlad to hear you're finding it helpful! I'm thinking I'll do smaller videos that focus on one topic at a time instead of another video like this one. That'll hopefully mean they're more frequent but will also let me go into a little more depth each time.
A few weeks ago I put out a video on how to use a noise texture in a shader if you want to check that out!
I've got a few ideas such as using normal maps and creating very basic lighting, using vertex shaders to manipulate objects and a few others. Is there anything you would like to see cover?
I haven’t even gotten past the ads and yet know I’ll have to come back haha
perfect no-nonsense introduction to shaders. The pacing was perfect from start to finish. well done.
Thanks, glad you liked it!
it helps that p5js just recently made it much easier to use shaders with their library!
For the first time in my life, I understood what that Shader code was all about. Thank you for the quick explanation. I'll definitely need more couple of years to be able to write my first shader script to create a simple black circle on screen because is too hard, but this video is definitely one of the best to see about the topic.
Thank you so much! You've got this, it took a couple of tries before it started to sink in for me as well, I think that's very normal because it's definitely a different sort of programming from what most of us are exposed to, but I promise it does get easier!
If you have any questions or need help, please feel free to reach out!
Same.
Finally finished! This video took ages to put together, so if you enjoyed it, it would be great if you could give it a like and share it with anyone you know who needs to learn about shaders!
Also, I forgot to mention the amazing www.shadertoy.com which is another great resource and an easy way to get stuck into shader programming in your browser.
If you'd like to see what else I'm working on, it would be great if you could wishlist my game Star Mining Co. on Steam! s.team/a/2584800/
oh yeah, don't forget graphtoy, it's a graphing calculator specifically with glsl in mind !!
there's also a few websites in kishimisu's video on shader art coding but youtube tends to delete comments with links, they're still worth checking out though!!
This is better than the official learn OpenGL tutorial (mix of c++ and glsl)❤Nice one! Exactly what I needed on my Vulkan adventures 😃
Wow, thank you so much! Hope your adventures go well!
watching this to hopefully understand how this stuff works for my graphics class
Hope it's helpful! Let me know if you've got any questions :)
This was just the video I needed to get started with shaders. I knew very little about them or what they even did or were capable of, but this video really showed me, on a beginner level, just how indepth this really goes.
Thank you so much for all the effort you put into this video. You explained so much indepth, while also making it simplistic, while not watering anything down. The care and passion you put into making this really shows.
Thank you so much! I'm so glad you found it helpful, shaders can be an amazing tool so getting more people creating and playing around with them is exactly what my goal was!
All the best on your shader journey ahead!
I'm going through a similar experience drawing primitives in MonoGame, and I'll say it really is handy to have this context because the documentation and tutorials for this stuff are really obscure! Thanks @BarneyCodes for the tutorial, seriously
Hi, normally I really don't comment on TH-cam videos. But man, this introduction was EXACTLY what I needed to get started with my university project. Thanks a lot man, keep up the good work :)
Thanks so much, glad it was helpful and I hope your project goes well!
This is better than the official learn OpenGL tutorial (mix of c++ and glsl)Nice one! Exactly what I needed on my Vulkan adventures
I'm not really working in game dev but watched your video out of curiosity and it was a pleasure and nice relax for me. You have a great talent to explain things :)
Thanks so much!
Only half way through but this is gold.
I've dabbled a little with real basic shaders in opengl using c++ but all that VAO, VBO stuff gets pretty old very quickly.
I've also dabbled in p5.js a fair bit but never used shaders (didn't even know until recently that shaders in p5.js were possible).
Pretty excited to see what I can create with p5.js shaders.
Many thanks for getting me started, much appreciated.
Haha yea setting all that up can be a bit of a chore, P5 is great for just messing about and getting something on the screen quickly! Have fun!
this is bar none the BEST video explaining shaders I have watched in YT. Very very very well done!
Thank you so much! Glad you found it helpful!
Love the pace and the insight that gave us on shaders! Great!!!
Thanks so much, glad you enjoyed it!
Thanks loads for this! The comprehensive approach filled in literally all the gaps in my understanding of shaders.
No problem, glad you found it so useful!
this video is pure gold. Thank you so much
No worries at all, glad you found it useful!
Thank you so much for putting this video together! Fantastic explanation of the SDF and I also liked the part about the logic a lot! You touched so many topics with such a great presentation, really amazing man!!
Thanks so much, I'm glad you found it useful!
Small clarification, HLSL is the language used by DirectX, meaning it is windows only. Where as GLSL is multiplatform.
Thanks for clarifying!
I absolutely adore this video and your presentation. Shaders seemed like black magic before this but you really helped me understand
Thanks so much for the kind words! I'm really glad to hear this video helped!
As a software engineer that uses the standard python and java / C# dabbles, this is like a wizard learning a different school of magic lol, so interesting
the best shader tutorial ive watched so far!!!! THANK YOU!!!!!!!!!
No worries, glad you found it helpful!
Rating: 12/10 on all levels.
This is YT-hall-of-fame material 🏆
Hahaha wow, thanks so much!
Better than my public education 11/10
Great job, Barney! Congrats on this video and your channel taking off! Well deserved.
Thank you so much Steve, I really appreciate all the support you've shown me! I still can't really believe how well this video has done!
Um, thank you. Wow. This is exactly the type of rundown I've been seeking.
No worries, glad it was helpful!
Fantastic introduction to shader programming! Thank you for this :)❤ I kept wanting to know more about everything you talked about, it really could have been an even more thorough introduction. Looking forward to a potential series or just more on this wondrous field of programming.
Thanks so much! It was pretty hard to decide what to keep in and what to leave out and it still ended up being a long video! I do plan on doing more videos with shaders in the future, is there any topic in particular you'd like to see in more depth?
@@BarneyCodes shaders in wgsl!
I love this. Lots of information and no wasted time. Very well done.
Thanks so much, glad you found it useful!
Getting into graphics programming as a hobby thanks for the video, You're a great explainer. I wish I had gotten into this sooner instead of web development theres a lot of neat stuff to understand
This is absolute fucking gold. I love you man. Crisp, clear and straight to the point. Your humour was never intrusive like with other tutorials and you even gave links for every checkpoint in the video. God bless you man, this was a fantastic video and I wish I could like it 10 times lol ♥
Thank you so much, that's so encouraging to hear and I'm glad you found it useful!
This is amazing! I am going through SimonDevs course but there is alot of clutter with threejs to make stuff happen, but using p5js is way simpler and really makes the javascript code as a secondary element and really focus on the glsl side of things. I hope this series keeps going.
I love Simon's videos, they're really great! But yea I think P5js is pretty much as straightforward as it gets for setting up shaders which is why I use it so much!
There are some limitations with it though, like the ModelView matrix being combined in the vertex shader so that you can't do world space calculations.
What topics would you like to see in the next video?
Neat video! I went through the (current state of the) book of Shaders. I later saw people being critical on it in forums, saying it has some limitations, and your video helped me see one of it.
The book of Shaders tackles pretty much only the contents of the .frag files, but thanks to the "circles array" example you made, I realize the importance of the "CPU" side of things, and passing custom variables.
Glad you found it useful! I had similar frustrations with a lot of shader tutorials, so happy to hear that my one has helped in that regard
The only bad thing about this video is that it didn’t exist yet back when I was learning this the first time. From now on this will be the introduction I recommend. 👏👏👏
Wow! Thanks so much for that, very encouraging to hear!
What a great video! Thanks for making it :D
Thanks so much for the support! Glad you enjoyed it!
Im a 3d artist who has been wanting to get into technical art seeing that it's tight competition to get a job doing 3D stuff, but everyone and their mother needs like 5-10 tech artists. This is my gateway into securing my future. Did 3D stuff or 7 years under my last employer, but it's still tight as ever to get in anywhere else.
Best of luck! Hopefully this can get you started on your shader journey!
26:50 you can also use the spread operator, which has the added bonus that it works with any number of arrays, i.e.:
const colours = [...col1, ...col2, ...col3]
Oh that's really good! Thanks for the suggestion!
I really needed a video like this about 2 years ago, when I studied this at university
Better late than never I guess?
it's insane just how much stuff inigo has done for shader community. You can't read about them for five minutes without running into a reference to him
He's truly a gem, so grateful that he's shared so much of his knowledge with us!
Really well structured video and easly explained subject.
Keep up the good work! 😉
Thanks so much!
Brilliant video, really a great way to visually teach complex topics. Very informative and I can appreciate how much work this video must have taken all in all. Also the two Edges was a nice touch :).
Thanks so much, I'm glad you enjoyed it! And I think you're just the second person to comment on the Edge joke, good to know there are a few cultured people out there hahaha
@@BarneyCodes Wait a minute ... is cultured code for old?
You are amazing at explaining and visualizing things
Thank you, that's very kind!
Nice intro to shaders, thank you!
Thanks, glad you liked it!
Todays the day bro!! :D been so long coming... finally got some bloom and reflective water up and running in unity... wow
That's great, nice work!
This video is the best one I've found so far. The pro tip around 32:00 makes a ton of sense but I hadn't thought of it yet, glad you caught me writing my if statements lol.
For context I've been working on my very first shader today! Coincidentally also in P5js like your examples. I came here because of confusing syntax errors I've been getting and although I'll have to troubleshoot those some more (or maybe explicitly define the webGL version the syntax I've learned is correct for? 😅) I think this is helpful.
The project I'm writing a shader for is rendering the mandelbrot set, moving the load to the GPU as an upgrade to my current p5 sketch for it. The current version I already built has scrolling to zoom in and mouse-drag panning support, as well as using arrowkeys to change the canvas resolution on the fly... but especially at higher resolutions it's taking minutes to render, so it's far from responsive lmao. I failed to make the rendering function render asyncronously to a graphics object *and* have p5 move or zoom in on the last rendered frame while the next one loaded though, I guess because it's not waiting for 1 long operation or fetch and only for a bunch of small calculations, so doesn't ever hand control back while awaiting, so p5 just shows the preview change for a split second after the next image finally renders and then immediately slaps the new one it's been waiting for over it lol. So I'm skipping that idea since I don't feel like learning web workers for it and instead going straight to shaders for the real performance gains! Before watching this video I'd already ported over (from JS to GLSL) my functions for complex number multiplication, mandelbrot iteration, checking if points are inside the set (need to change to using step function now 😆😅), and for coloring points based on the number of iterations each complex number takes to escape... I was trying to pass some uniforms into it for things like the number of iterations and user input for zooming, but I haven't gotten it to load once without getting syntax errors 😒. I mean I also haven't finished some of the basic features like properly mapping between the fragcoords to mandelbrot space *while* taking the zoom input into account, but I made a map function for it that I was about to test when I got weird syntax errors while trying to load my shader for the first time. Previously in the non-shader cpu version I was mapping from pixelcoords to the complex plane (within a range depending on the zooming and panning) so it'll be a bit different and I'll need to make sure I don't stretch the image by the aspect ratio again like I initially did in the cpu bound pixel version, but it's a work in progress and the whole point of this project was for fun and learning!!! 😁
Rendering fractals with shaders is a lot of fun and a really great project to get familiar with shaders! If you get stuck, I've got a video on my channel about making a mandelbrot shader ;)
You're very good at explaining. Thank you for this video!
Thank you and no worries!
视频做的真棒, 每一步都介绍的很详细👍
Thank you!
This is a great resource. :)
Glad you found it useful!
thank you ❤amazing work.... i hope you make this is a series..i would love to learn about vertex shaders..looking forward to ur next video 😁
Thanks! There will definitely be more shader videos! I'll have to think of something cool to do with the vertex shader!
This gives my brain a nosebleed, but in a good way like in anime ❤
this channel rocks - please keep going!
Thank you so much! Definitely more to come!
Top-tier video!
Thanks so much!
Very very cool video. I really appreciate the detailed explanations.
Glad it was helpful!
Great Guide! Amazing video!
Thanks so much, glad you found it useful! :)
Demystifies a lot of the syntax that kinda stumped me while giving a cursory glance :)
One of the best on youtube.
Thanks so much!
Great stuff. Looking forward for more.
yessssssss thanky ou for this!
No worries!
Really good introduction! Thank you
Thanks, glad you liked it!
i would like you to know that this video is SO WELL MADE and beautifully explained, that me, a person with 0 prior experience to coding, no idea what the brackets even do, are able to understand the concepts perfectly well. Thank you so much, my journey of learning how to make games is looking a lot less dim now 😭
Wow, that's amazing! Glad you found it so useful and good luck on your journey! Feel free to reach out if you have any questions!
Oh no! I have recivied the power to create an alternate reality in javascript, i shall use this power to conquest the world.
Wow, I can't wait to learn how to draw a cube!
Thanks man.. this really helped a lot. Thank you for putting this much time and effort into this❤
Mate! I love glsl, but your explanation is dope! Thank you!
Wow, thanks so much! Glad you liked it!
This is great, thank you! Ps. There is a low humming or noise in the background , I don’t mind it but some people could be sensitive to it ^^
Glad you found it useful, and thanks for the feedback, I'll look into eliminating it for future videos!
Nice video. Just one nitpick. The grayscale is incorrect, since human eyes don't treat colors equally. It depends on the use case and the color space but the general simple formula is: 0.2126 R + 0.7152 G + 0.0722 B. There are ones with square root and squaring of colors that aproximate it better, but are much more expensive in terms of performance.
Huh, that's really interesting! Thanks for the info!
I really wasn't expecting this video to be this difficult to adapt for Java Processing, but it's proving to be incredibly hard to make this example code work there.
What issues are you running into? There are definitely some differences in getting the environment set up, but the actual shader code should be usable between both (you might just need to check what uniforms/attributes are available by default in Processing vs P5js)
If you're getting any error in particular I can try to give you a hand to get it sorted out!
@@BarneyCodes That's what I thought as well. When I first copied everything down the shaders just refused to compile, so I figured the names of the attributes must just be different. It took me a while to find any documentation about what attributes were available in the Processing environment (the official site has pretty much nothing that I could find about implementing shader functionality despite this being a feature for years at this point) vs P5, but eventually I found examples in an old backup of a blog that went pretty in depth about how this stuff worked for Processing.
After replacing all of the attributes with their correct counterparts the shaders were finally able to compile and run, but specifically the vertex shader just doesn't have the expected behaviour. It refuses to shade anything in 2d that isn't a square that fills the whole screen. I've been tinkering with it off and on for a while trying to figure out what in the implementation is different enough to make this not work, but I don't have enough experience or documentation support to really know what's up here
I've only used shaders in Processing for full screen effects, so unfortunately can't be too much help there sorry!
A few of the example Processing shaders I've seen have different defines at the top, eg:
#define PROCESSING_TEXTURE_SHADER
or
#define PROCESSING_COLOR_SHADER
maybe something like that helps?
I know Processing also lets you either supply a vertex shader or just use their default one, not sure which route you've been going down (maybe both already!) but it could be good to try them both out to see if that helps at all.
As a last resort, the Processing source code is all available on GitHub so you can also dive into that (if you're feeling brave enough!) to try and figure out what's going on and why it might be behaving strangely!
Sorry I don't have a more concrete bit of advice for you, but hopefully there's something in there that's helpful?
Good luck!
Great video.
Thanks! this was exactly the sort of tutorial I’ve been looking for 🎉
No worries, glad it was helpful!
Great video. Wish I understood it. I get the general concepts but a lot of the technical stuff flies over my head. Having a tough time learning basics of coding because I don’t have anything to code, a lot of tutorials/learning resources just feel like I’ve been given a list of a languages syntax.
It can definitely be that way when you're first getting started! Thankfully you'll start to notice that there are a lot of similarities between languages so once you get a bit more familiar with one, it'll be easier to pick up more. Keep at it and you'll get there!
I highly recommend finding a tutorial for something that genuinely excites you, it makes it a lot easier to stick with it! (For me it was a really really basic game, I think it was a bit like space invaders or something!)
@@BarneyCodes I guess a follow up question would be where would/should I do something like that? Would you do that in a game engine like unity? Or just barebones? I feel like using a game engine might be a detriment to some of the learning process because the bells and whistles are already there, but I also don’t want to give myself an additional level of frustration for no reason.
@@BarneyCodes though, I guess it’s just personal preference and in finding a tutorial like that I’d get my (or an) answer.
@jackmalcolm I think if your aim is to learn to code, I'd probably recommend not using an engine. A really great starting point would be to check out "The Coding Train" here on TH-cam. Dan, the bloke that runs the channel, has hundreds of videos for all skill levels and his enthusiasm is infectious!
In fact I think he may have pretty recently released a course on getting started programming, which could be perfect! EDIT: It's called "Creative Coding for Beginners - Full Course!" if you want to look it up :)
Awesome video. Very entertaining and educational
Thank you, glad you enjoyed it!
Excellent video, thank you so much!
No worries, glad you liked it!
excellent video! really appreciate this
Glad it was helpful!
Lost me at swizzling, but guess it helps to have 3d coding knowledge. Really hard to find a simple tutorial that goes deep on the basics. I really like the style of this vid and you had me for the first ten minutes. I will persevere.
Sorry to hear that! Honestly, you can get away with not using swizzling so I'd encourage you to just skip that part and keep going!
All you need to know for swizzling is, if you see something like vec.xxy, it's the same as saying vec3(vec.x, vec.x, vec.y)
Hope that helps!
@@BarneyCodes thanks 🙏
Great Video!
I saw another comment talking about Minecraft's Vanilla Shaders (Vanilla means that it's the default version, without any API like OptiFine), and I think it would be interesting to make a video about it.
Two great examples are bradleyq's shader-toolkit and VanillaPuddingTart.
(The default files can be obtained by extracting Minecraft's jar file)
Thanks! I'm actually really interested in pursuing this idea so I'll definitely check out both of those!
The edge joke cracked me up!! Would be interested if Shaders are used for physics and engineering math calculations ? At the end of the day it’s just doing a load of parallel float (or other) ops.
I'm glad somebody got it, it's the first time someone mentioned it hahaha
That's a really great question! There's a type of shader called a compute shader that is often used for exactly that! Compute shaders are basically a really flexible type of shader where the programmer gets to define most things about how it is run, and can read/write into "shader storage blocks" so you're not limited to just outputting to textures and you don't need to render geometry to trigger it to run.
Before I learned about compute shaders, I did try to use textures to store particle positions but I couldn't get it to work too well. I'm sure that it's possible though!
Thank you so much!!!! I appreciate this as it will help tremendously!
No worries!
thank you so much this is an incredible tutorial!
No worries, glad you found it useful!
Very good. Thank you
No worries!
Oh man! Greatfull by your content!
Glad you find it helpful!
Superb! Thank you 😊
No worries!
This is awesome
If you delete "Chapters:" in the videodescription an start right with 00:00 ... you'll get the chapters also in the videotimebar as well
Thanks so much for that! I moved some things around in the description which must have screwed them up!
@@BarneyCodes no problem, I delete my comment tomorrow, to keep you tutorial clean :)
This introduction explains al ot about shaders even tho its just an intro
it is really worth watching
oh also i wanted to ask
is it possible to learn GLSL and opengl at the same time?
Glad you found it helpful! I definitely think it's possible to learn both at the same time, in fact it's probably necessary to learn a little bit of GLSL to do anything with OpenGL, but I think it's probably best to just do the basics of one, enough to let you explore the other, and then you can come back and learn the first one properly, just so you're not too overwhelmed!
Great video, thanks
I had some shaders in uni, but I didn't really get it
Hopefully I'll start using them more often and learn them, but this video is a great introduction
Thanks! Hope the video is helpful, let me know if you have any questions!
Thank you!
No worries!
that was great!
Instructions unclear about beverage choice grabbed vodka now I have to rewatch the video 11/10
Hahaha I'll be more specific next time
thank you very much
No worries!
Great video! A couple questions I have.
If shaders are meant for the GPU, what happens when running on a system with only a CPU?
For the background image, is there a difference in handling png, jpeg, etc? Or are they passed as just the pixel data?
For video games, are shaders drawing everything? Or do they receive the frame (like the background image) and then apply additional changes to that?
This is the first shader video I've come across, so I'm sorry if these are commonly answered questions.
Really great questions!
Most mordern CPUs will have some sort of on-board graphics which is essentially like having a mini GPU built into it. A dedicated graphics card will be a lot faster and usually perform better too. You can get CPUs without graphics in built, but they're the exception not the norm.
The CPU is where the work of opening/reading the files happens, and it then will just pass the pixel data to the CPU, so there shouldn't be any difference between different file types. If you're doing it yourself (ie not using an engine/framework), you'll have to set what form that pixel data takes using the graphics API you're using (this will be things like whether its RGBA or ARGB etc)
In games, shaders will be drawing everything, but if you're using an engine or framework they will have shaders that do pretty much everything for you, you'll just tell it what models/textures/sprites to use and it will handle the rest. You'll almost always be able to write you own shaders though, if you need to do something a little obscure or want a custom effect.
I hope that helps!
@@BarneyCodes Yes that helps a bunch! I did look some other stuff up and it seems there are more than just vertex and fragment shaders. Are the others just variants of these two that focus on specific aspects which could be combined into the base 2?
Also, an additional question. I know NVIDIA applies their DLSS stuff that uses AI to make games look better. Are these just game specific shaders that are created using machine learning? Or does the regular image render from shaders, and then have another round in the GPU where DLSS polishes up the finished frame?
That's great to hear!
Yea there are a bunch of types of shaders, and they are all their own unique thing and are used at different steps. You hear of vertex and fragment shaders the most though because they are required to draw stuff on the screen, where as the others are optional. The most common non-fragment or vertex shaders are probably geometry and compute shaders. Geometry shaders basically lets you turn a vertex into a group of vertices (eg you could turn a single point into a cube). Compute shaders are essentially just a way for you to use the parallel power of the GPU outside of the rendering process. It uses Shader Storage Buffer Objects (SSBOs) to transfer data between the CPU and GPU, so once the compute shader is finished you can read the SSBOs for the output and then use that information however you want.
I don't know too much about DLSS but I had a quick look and it sounds like it's all done AFTER the rendering has finished. I think the idea is that you render to a lower resolution so that it's really fast, then DLSS upscales that smaller frame to a larger resolution. It uses special hardware on modern GPUs that is purpose built for running AI!
"For video games, are shaders drawing everything? Or do they receive the frame (like the background image) and then apply additional changes to that?"
Yes, shaders are drawing everything, but at the same time, for things like transparency, you can enable blending (glEnable(GL_BLEND)), and then it will combine your calculated color with that of what you have drawn previously.
Unfortunately, transparency in 3D is a complicated topic, so I'd recommend avoiding it when starting.
Awesome video Ty!
More specifically HLSL is used by Direct3D, while GLSL is used by OpenGL and Vulkan. It is also worth noting that crosscompilation through for example spir-v is possible which means you really only need to write 1 language (typically hlsl) to support a wide variety of graphics apis.
I'll have to check the cross compilers out, didn't know that was a thing! Thanks!
@@BarneyCodes The way I do it in my game engine is write everything in HLSL. Then i use glslang to compile to Spir-V. From there i use spirv-cross to "decompile" to either GLSL that targets opengl or vulkan; at which point you can use the opengl commands to compile it or precompile back to target opengl spirv or vulkan spirv (yep.. they're not compatible with eachother; they have their own spirv flavour). You have to take this decompile step because the HLSL resulting spir-v is not compatible with opengl. Supposedly it works with Vulkan but i couldnt get it to work. But with this it works flawlessly across d3d11, 12, opengl 4.6 and vulkan 1.2. The decompiled GLSL isn't even half bad either.. it looks pretty clean.
@@ruadeil_zabelin Thanks for explaining! I'm guessing your engine allows people to choose which renderer they're using then? If so, that's impressive! The only other scenario I can think of where you might need to do something like this is if you're porting a game from one renderer to another. Are there other use cases for such a thing?
@@BarneyCodes You can pick a renderer yes, but I made it so it just suggests what I think is the "best" supported one for that platform. So on windows it always defaults to d3d11; on linux its Vulkan, on mac its opengl (though i might try metal someday). I basically studied how unreal engine handles their render api abstraction and found that its basically very similar to the d3d11 api. I wrote a similar interface and just implemented all the function calls for all the graphics api's. If you choose the abstraction well its not all that difficult to do. The only hurdle was the shaders so the possibility of crosscompiling was really nice.
@@ruadeil_zabelin Ahhh that makes a lot of sense! Sounds like a really cool project! Thanks for all the info :)
Really really well made video thanks
No worries, glad you enjoyed it!
Hey nice video. Good stuff
Thanks, glad you liked it!
Cool, thank you very much.
No worries at all!
Draw a circle... then draw the rest of the fucking pantheon! :D
Great video, straight to the point :)
Hahahaha thanks, glad you liked it!
This is amazingly helpful. I’ve been having hell of a frustrating time with OpenGL, and this can aid in my woes 😁
In following along, you’re passing pos in the shaders. Where is the value of it coming from? I do understand what’s going on, but having that bit of info known helps. Am I missing it somewhere?
Glad you've found it helpful so far!
I think I glossed over that in the video a bit. Basically the position value is being set in the vertex shader and is then passed through to the fragment shader (this is why the variable name in the vertex and fragment shaders have to match, because they're being used to pass the value across!)
When sending geometry to the GPU to render, you have to specify the position of the vertices, but you can also specify the "UV" coordinates, which are basically the coordinates you'd like that vertex to use when looking up a texture, and this is the value I'm using to set the position variable in the vertex shader.
How you set the UV coordinates from your CPU code will depend on what environment you're using, for P5js, they're set automatically!
Since the UV/texture coordinate is per vertex, they get set as an attribute, which is only available in the vertex shader, which is why we need to set up the varying variable to pass the value into the fragment shader.
I hope that makes sense, it's a bit of a rambling answer! If you've got any follow up questions, please just ask :)
thank you soo much!!!!
No worries!
This was f*ing awesome
THIS WAS SO GOOD PLS DO ANOTHER VIDEO ON 3D STUFF
I haven't really explored the 3D side of things too much yet, but it's definitely something I plan on getting into! I'll be sure to make a video showing what I've learnt when I do!
Thanks for the comment :)