Introduction to shaders: Learn the basics!
ฝัง
- เผยแพร่เมื่อ 27 เม.ย. 2024
- 0:00 Intro
1:24 What is a shader?
3:37 Setting up shaders in P5js
5:58 GLSL data types
7:00 Vectors
8:58 Attributes, Uniforms and Varying
10:19 Barebones fragment shader
11:00 Vertex shader
14:57 Fragment shader revisited
15:30 Gradients
16:45 FragCoord tangent
18:28 Mix function
21:22 Setting uniforms
22:43 Uniform images (sampler2D)
24:58 p5.filterShader
25:12 Uniform arrays
27:05 Circles and SDFs
31:47 Boolean logic
33:25 Debugging shaders
34:13 Conclusion
Resources:
Inigo Quilez TH-cam: / @inigoquilez
Inigo Quilez shader: www.shadertoy.com/view/ldScDh
Kishimisu TH-cam: / @kishimisu
Kishimisu shader: www.shadertoy.com/view/mtyGWy
Types of shaders: www.khronos.org/opengl/wiki/S...
GLSL Data Types: www.khronos.org/opengl/wiki/D...)
All available p5js shader attributes and uniforms: github.com/processing/p5.js/b...
gl_FragCoord: registry.khronos.org/OpenGL-R...
mix function: registry.khronos.org/OpenGL-R...
p5.filterShader Library: github.com/BarneyWhiteman/p5....
2D SDFs: iquilezles.org/articles/distf...
Examples:
All examples: editor.p5js.org/BarneyCodes/c...
Vertex shader example: editor.p5js.org/BarneyCodes/s...
Fragment shader gradient example: editor.p5js.org/BarneyCodes/s...
Fragment shader position example: editor.p5js.org/BarneyCodes/s...
Fragment shader image example: editor.p5js.org/BarneyCodes/s...
Fragment shader array example: editor.p5js.org/BarneyCodes/s...
Fragment shader SDF example: editor.p5js.org/BarneyCodes/s...
Fragment shader logic example: editor.p5js.org/BarneyCodes/s...
Shaders are an incredibly useful programming tool to have under your belt, especially for creative coding and generative art. They unlock a whole heap of potential, but they're a bit quirky to learn!
This video covers a lot of the fundamentals and will hopefully set you up well for using shaders in the future!
This is a bit of a long/dense video so be sure to take a break if you need, and you can always watch it more than once to help it all sink in!
This video and its examples use P5js in WEBGL mode, however the concepts are transferable to pretty much any environment that you can use shaders in (some syntax may vary, especially in the CPU code).
Follow me:
Support the channel: www.youtube.com/@BarneyCodes/...
Twitter: / barneycodes
Reddit: / barneycodes
#shaders #graphics #creativecoding
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.
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?
@@Seb001eanGlad 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
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!
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!
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!!
seems a little shady to me 😅
Never trust shaders
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!
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!
Love the pace and the insight that gave us on shaders! Great!!!
Thanks so much, glad you enjoyed it!
This is all new to me. I can see the potential for a lot of this stuff, and I can see you've barely scratching the surface of this powerful tool. Man... this is going to take a few watches just to even get started :|
It took me so many cracks at trying shaders before it started to stick! Not meant to discourage you, just letting you know you're not alone in feeling that way!
I think it's just you have to approach it in a pretty different way from most programming so it can just take some time to get used to, I'm sure you'll get it though - if you have any questions or anything, just let me know :)
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!
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!
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!
Great stuff. Looking forward for more.
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!
What a great video! Thanks for making it :D
Thanks so much for the support! Glad you enjoyed it!
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!
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!
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
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?
Nice intro to shaders, thank you!
Thanks, glad you liked it!
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?
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!
Thanks man.. this really helped a lot. Thank you for putting this much time and effort into this❤
This is a great resource. :)
Glad you found it useful!
it helps that p5js just recently made it much easier to use shaders with their library!
You're very good at explaining. Thank you for this video!
Thank you and no worries!
Thanks! this was exactly the sort of tutorial I’ve been looking for 🎉
No worries, glad it was helpful!
Um, thank you. Wow. This is exactly the type of rundown I've been seeking.
No worries, glad it was helpful!
You are amazing at explaining and visualizing things
Thank you, that's very kind!
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!
Really good introduction! Thank you
Thanks, glad you liked it!
this channel rocks - please keep going!
Thank you so much! Definitely more to come!
Awesome video. Very entertaining and educational
Thank you, glad you enjoyed it!
视频做的真棒, 每一步都介绍的很详细👍
Thank you!
Superb! Thank you 😊
No worries!
Excellent video, thank you so much!
No worries, glad you liked it!
Rating: 12/10 on all levels.
This is YT-hall-of-fame material 🏆
Hahaha wow, thanks so much!
excellent video! really appreciate this
Glad it was helpful!
Great video.
Oh man! Greatfull by your content!
Glad you find it helpful!
thank you so much this is an incredible tutorial!
No worries, glad you found it useful!
I really needed a video like this about 2 years ago, when I studied this at university
Better late than never I guess?
The benefit of a strongly-typed language like C++ is that the developer is in control and well-informed. The advantages of loosely-typed languages are as many and varied as the reasons to be loose with variables in a computer program.
(none, there are none)
Thank you so much!!!! I appreciate this as it will help tremendously!
No worries!
Better than my public education 11/10
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!
Very good. Thank you
No worries!
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!
Wow, I can't wait to learn how to draw a cube!
Hey nice video. Good stuff
Thanks, glad you liked it!
Mate! I love glsl, but your explanation is dope! Thank you!
Wow, thanks so much! 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 gives my brain a nosebleed, but in a good way like in anime ❤
Top-tier video!
Thanks so much!
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!
yessssssss thanky ou for this!
No worries!
Small clarification, HLSL is the language used by DirectX, meaning it is windows only. Where as GLSL is multiplatform.
Thanks for clarifying!
Thank you!
No worries!
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!
thank you very much
No worries!
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!
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 :)
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!
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
I forgot more in 44 years of computer graphics development than others learned over the course of their entire lives
Cool...
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!
thanks breh
No worries!
0:40 coffee time
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 :)
Draw a circle... then draw the rest of the fucking pantheon! :D
Great video, straight to the point :)
Hahahaha thanks, glad you liked it!
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!
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.
Instructions unclear about beverage choice grabbed vodka now I have to rewatch the video 11/10
Hahaha I'll be more specific next time
crazy that nowadays a 30 min video is referred to as "quite a long video"
As in 30 mins is a long video or it's not? 😂
hahaha, I meant it's not, specially if it is informative like this... : )
@@zaftnotameni Glad you found it informative! I think part of it being a "long video" in my mind is because it took a long time to make!
can you cover generating normals with the shaders? tangent space and world space?
We can definitely look at that in a future video, great idea!
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 🙏
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!
not really familiar with shader languages but recently i've been looking into webgpu. in particular the wgpu rust crate. i think webgpu has a lot of potential. only downside is that there aren't many resources to learn outside of the spec since it's so new.
I did have a look into webgpu a little while ago, and like you said there aren't many resources. I'm not sure how many browsers support it at this point either? Looks really cool though, excited to see what it brings!
atleast now I know I need a new monitor
Hello. First of all great video. I am just getting started with this topic and learning more about how games work in general so this has been a great introduction to many concepts.
I have a question: For the code shown at 31:49:
float colour = 1.;
for(int i = 0; i < num_circles; i ++) {
float d = length(pos - circles[i].xy) - circles[i].z;
d = step(0., d);
colour *= d;
}
gl_FragColor = vec4(colour, colour, colour, 1.);
I don't understand how the "colour" calculation works here, meaning, if it is 0 in the first iteration, it will already be 0 for all other circles in the loop since the initial value is outside the loop . Also the gl_FragColor is outside the loop, so we only paint once even though we calculate a value for all circles ? What am I missing?
Very good question! I think my variable name makes it not very clear, it probably should have been called something like "outsideCircles" or something similar.
Basically, colour will stay as 1 only if it's not inside any of the circles. If it's inside any single circle, it will become 0, which will turn that pixel on the screen black (which is why I called it colour even though I probably shouldn't have!)
You're right that if it turns to 0 in the first iteration, then it will always be 0 making the rest of the looping a bit redundant. However, because of the way GPUs parallelise things, you're only as fast as the slowest pixel, so if one pixel has to loop through all the circles, then there's no real speed to gain from breaking out of the loop early for other pixels.
I hope that makes sense, please let me know if you've got any more questions!
@@BarneyCodes Wow thanks so much for the fast response. I think i understand what you are saying. At first i was seeing it as something sequential but I remembered that the code is executed in the context of a single pixel, and then it is executed for all pixels in parallel, so if I understand correctly we are basically checking if at least one circle is present in the pixel we are checking against and if it is, we paint that pixel black, otherwise we paint it white.
Let me know if that is correct or if I am missing something (again 😅)
Thanks again. Greetings from Colombia!
No worries at all, and yep, that is exactly what's happening! Each pixel is checking if it's inside any one of the circles and turning black if it is!
It can definitely take some time to think about the code in this context and totally normal to find it confusing! But it gets easier the more you do it and it seems like you're picking it up really well!
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 :)
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!
❤
Damn! If I had the chance to hug you I would surely do!
Glad it was helpful!
Can you do a video about changing Minecraft's Vanilla shaders? (it's also GLSL)
To get started, you simply need to create a resource pack, and hit F3+T to reload resource packs in-game, from there, it would be like copy/pasting shader files into the resource pack, and modifying them there. (legally, you would need to create the modified file)
That sounds like a really fun idea! I've never made Minecraft shaders before so I'll have to play around with it a bit before making a video, but I really like that idea! Thanks for the comment!
For a videogame, is there any reason we would want more code in the pixel shader? (I already forgot what it's called)
Seems like keeping it minimal would be the goal for performance.
It really depends on the use case, but in games the most common use is for visual effects (think shaders in Minecraft, it doesn't have an affect on gameplay but completely changes how it looks).
It can have applications outside of just visuals as well though, for example I'm working on a game at the moment where I use shaders, and in particular the fragment/pixel shader, to calculate the damage to the terrain on a per-pixel level. If you want to learn more about that, I've got a playlist with my devlogs here: th-cam.com/play/PLlaA0JvEhcG_rFyz7oa7BR6GM02oQeo8b.html
As with anything performance related though, it's worth using a profiler and finding out where the actual bottlenecks are, because generally the GPU is very fast, but only if you use it "properly" if that makes sense. Adding more effects and shaders won't necessarily have a negative affect on performance, but the best way to find out is to try it and see what happens!
Hopefully that helps, let me know if you have any other questions!
@@BarneyCodes Thank you so much. I'm taking a swing at making my own engine after the Unity debacle, and finding out that many calculations were happening a frame shocked me. Glad it's not usually an issue.
It's pretty amazing how fast computers really are! Hope your engine project goes well!
0:36 I am an asshole!! 😂
How does one attach or combine vec to a slider bar that controls the intensities? i am close but don't know how to combine say 3 calls into 1 call in a slider format and not separated x, y, z, w, + thanks if anyone helps.
I think if you want to control each aspect of the vector individually you're going to have to use multiple sliders. You should be able to combine the values from each slider into a single vector though and set that in your shader.
Pseudo code might look something like:
shader.setUniform("mySliderValues", [slider1.value(), slider2.value(), slider3.value()]);
Hope that helps
i'll give it a shot when i can and probably return with any progress, appreciate it, i do not do this, it is more like a new thing to learn, thank you.@@BarneyCodes
@@ecruz2317 Sounds good! I think the best way of learning is doing what you're doing and trying things a bit outside your comfort zone! Hope it goes well!
🧠⚡️📈
i just wanted how these shaders pass data and vars between them and between the environment , where the returned output value goes exactly
Data can be passed into the shader using by setting uniform values and, without doing extra setup, data is outputted from the shader in the form of textures/image data.
If you want to be able to get other data out of the shader, then you should look into Shader Storage Buffer Objects (SSBOs) which can be manipulated inside the shader and then read out again by the CPU. They can be a little fiddly to set up, but they work very well!
@@BarneyCodes i appreciate your reply and i'll learn more abt it , thank you
No worries at all! I should also mention that there are "compute" shaders, which are seperate from the rendering pipeline and work only with SSBOs I think, so if you're only using the shader to run some simulation or something, they might be worth checking out too!
exactly i wanted to use gpu for training ai
I tried to follow along and wrote the code shown at 11:09 in p5.js web editor but could not get it to work. I went back to 5:20 to look at the previously shown code but somehow couldn't see how these two codes link together. Please help
In the code at 5:20 you can see on line 5 the call to the loadShader() function. This is how we load the code you're writing at 11:09 with the JavaScript code. You'll need to make sure the names you use in the loadShader() function exactly match the filenames you use for your vertex and fragment shaders!!
What error message do you get when you run your code?
There's also the example code linked in the description, so feel free to have a look at that, it might be easier to see what you're missing there!
Hope that helps!
This video is swizzling!
What even is the language that you paid with javascript? I've never heard of it, likely less common.
If you mean P5js, it's just a javascript library that makes it really easy to draw on a canvas, but not necessary at all, it just makes the demos easier. And GLSL is a very common language for writing shaders! Hope that clears things up a bit!
p5.js says: Yikes! An error occurred compiling the vertex shader:ERROR: 0:3: 'gl_FragColor' : undeclared identifier
ERROR: 0:3: 'assign' : l-value required (can't modify a const)
ERROR: 0:3: '=' : dimension mismatch
ERROR: 0:3: 'assign' : cannot convert from 'const 4-component vector of float' to 'const highp float'
From the error it looks like you're trying to use gl_FragColor inside the vertex shader, but I think it's only usable inside the fragment shader.
It could be that you've accidentally got your vertex and fragment shaders the wrong way around in the loadShader function?
Let me know if that helps!
Just out of interest. Would it be worth it to write the SDF for loop as a while loop instead to bail out early as soon as 0 is hit? How complex does the calculation need to be that this actually makes a difference? Or is it a form of branching and inherently slower than predefined number of loops.
Great question! It probably would be beneficial because in most cases a pixel won't only be inside the last circle, but it probably won't be a massive speed up, since in shaders you can only go as fast as the slowest case. The way GPUs work is all the fragments that are being calculated have to wait for the last fragment until they can move on to the next step. With GPU programming you're better off making the worst case scenario better, rather than making the most common scenario better, which is a bit strange!
Hope that helps!
@@BarneyCodes Yeah, with that restriction it won't make a great difference unless well over 50% of the time you're certain that the pixel will be inside a circle in this case so there's a good chance that every pixel in the batch would complete early.
I assume then that the while-loop in itself is not noticeable slower than a discreet number of loops from execution standpoint since for-loop in general is just a fancier while-loop?
@Draugo yea you would have to do some fairly major optimising I think. There's probably a way to do it though!
And I'm not actually sure about while loops vs for loops, but my guess would be the same as yours, that they'd be basically the same
@@BarneyCodes Thanks for the great answers. The video was also a really good intro to shaders.
personally I'd prefer removing the p5 specific sections and making the video based on shadertoy or something like that, it gets a bit confusing if someone is new to both p5 and shaders
Good suggestion, thanks for the feedback!
@@BarneyCodes
Whereas I'm very happy that you *did* include the p5 specific sections - there are plenty of more general introductions to shaders out there while this one shows you how they integrate with p5. Thank you.
That's good to hear, thanks for your feedback too! I had to use something, so no matter what language/framework there would have been people who aren't familiar with it. I think P5 is hopefully easy enough for people to pick up!
Thanks again for your comment!
You spelled color wrong. :). Excellent video, thank you so much!
Glad you liked it! Colour is how we spell it in England/Australia!
Thks & over my head but;
Pls take a look at FPGAs & Geometric Algebra and tell us what you think
I don't really have any experience with either of those topics but I can take a look!