floooh / sokol

minimal cross-platform standalone C headers
https://floooh.github.io/sokol-html5
zlib License
7.12k stars 501 forks source link

Default value _sg_gl_uniform_t::gl_loc when uniform_desc's name isn't given in sokol_gfx #1070

Closed gameoflord012 closed 4 months ago

gameoflord012 commented 5 months ago

In _sg_gl_create_shader() function line 8523 in file sokol_gfx.h

if (u_desc->name) {
     u->gl_loc = glGetUniformLocation(gl_prog, u_desc->name);
} else {
     u->gl_loc = u_index; // Here
}

Isn't there should be an assertion(false) instead of assign to u_index which is meaningless?

floooh commented 5 months ago

The idea is that shaders can define an explicit uniform location via layout(location = N), and in that case no uniform name needs to be provided.:

https://www.khronos.org/opengl/wiki/Layout_Qualifier_(GLSL)#Explicit_uniform_location

It's a very obscure feature though that's not actually used in any sample code, not used by the sokol-shdc shader compiler and also requires an extension or recent GL version.

It also only works on desktop GL, not GLES3 (in GLES3 uniform names are required: https://github.com/floooh/sokol/blob/b1221d1f2bfc3cbb22f566cfa8f31b4073c287ae/sokol_gfx.h#L16300-L16302).

TL;DR: it's not exactly a bug, but also not exactly a core feature, don't rely on it.