How to make your UNIFORMS FASTER in OpenGL

preview_player
Показать описание


Gear I use:
-----------------

Рекомендации по теме
Комментарии
Автор

Why doesn't anybody seem to notice he had already implemented this trick in an older OpenGL video?

fakestiv
Автор

Wow c++ series and opengl series 2 days in a row

koungmeng
Автор

I wrote an OpenGL renderer on a whim and did exactly this without even thinking about it. Now that I know it's actually a best practice, I feel kinda proud about myself.

RoyaltyInTraining.
Автор

We love the cherno, we do
We love the cherno, we do
ohhhh cherno we love you

ktggivubmmbas
Автор

Nice one! It is indeed a performance improvement. I did a similar implementation with DirectX overriding most of the device calls caching render-states, sampler-states, textures and stage-states, it reduced significantly a lot of API calls instead of having to query the GPU for every call. Thanks for the video! :)

giladreich
Автор

Oh god I love this video publish rates you're reaching, I just hope these videos make you enough to make up for the fact that you left your full-time job for them, so you know... you could keep making more of them, thanks Yan.

kamran_aghlami
Автор

WE LOVE U CHERNO KEEP MAKING TONS OF VIDEOS

ExistenceV
Автор

to avoid a double lookup by (abbreviating the code, since i am lazy):

auto x = cache.find(name);
if (x != cache.end()) return x;
/// code same after

phargobikcin
Автор

Nice video, but didn't you do this optimization in the shader abstraction video?

MarcusTL
Автор

1. Store the result of find() and return iterator->second instead, currently you are doing 2 lookups.
2. Seperate the GetUniformLocation and SetUniform to 2 calls and load them after loading the shader and do 0 lookups!

madeso
Автор

Wait what, so you left EA to make more videos on youtube? :D

bartoss
Автор

Yes!! Thank you for continuing this series!

This vídeo was great.

leoalvaleo
Автор

Nice video, but you do 2 searches in your code. First you search the map to see if the key is is there and then you return it using operator[] seach in turn does it's own search all over again. Instead you should call find() and save the returned iterator and return iterator->second in the case that the iterator is != end()

sghsghdk
Автор

OMG!! I've been waiting for the openGL lecture. i love Cherno

seopi
Автор

I bench marked almost this exact code not very long ago and got noticeably slower performance than just making the glGetLocation call every time with the only real different being if/else in the call for the setuniform instead of letting it drop through. I suppose I will run it again and give some definitive results with this exact code but it will take a small bit of effort since I switched to bind less uniform buffers since then.

seditt
Автор

That is still slow solution(using hashmaps + strings). If you only use uniforms programmatically it's better to use unique type for each uniform and store them in std::tuple:

template<typename UniformT, typename DataT>
struct Uniform // Specialize for specific glUniformi function.
{
Gluint location = -1;

void initialize_location(const std::string_view UNIFORM_NAME);
void update(); //<-- needs to be specialized
};

and then use it like this:

struct MyUniform1 : public Uniform<MyUniform1, int>{};
struct MyUniform2 : public Uniform<MyUniform2, vec2>{};
struct MyUniform3 : public Uniform<MyUniform3, float>{};

std::tuple<MyUniform1, MyUniform2, MyUniform3> uniforms;
// initialize each uniform location here

// call this in update loop:

std::get<MyUniform2>(uniforms).update(vec2(1, 1));

jarekmyszko
Автор

std::unordered_map timings to find an item can become problematic with shaders with more uniforms.
best case scenario std::unordered_map has O(1) and worst case scenario O(n), while std::map has O(logn) in both cases.
but of course all of these are dependent on the machine you're running your code on and how good your hashing functions is.
i haven't worked in EA so i don't know what they do in this case D:
hey you could get an iterator to your call in find method and dereference that in your branch instead of fetching the item using subscript operator again.

therealgunny
Автор

IN ARRAY. PUT THEM IN ARRAY. Use predefined set of constants. dont put them in unordered_map. Especially not in the slow MSVC version of unordered_map, that works slower than normal map

triihart
Автор

Thank you very much! I used this method before on java, but when I started moving my engine to c++ I've got a problem with uniforms, they're was broken, if I create 3 uniforms with different names, they're connects to the first one... Now I fixed it by this video!

oxygense
Автор

1:12 6:07 Is hitting the GPU from a C++ call similar to hitting other I/O devices?

3:30 GPU manufacturers hate him!

dkthehunter