love icon indicating copy to clipboard operation
love copied to clipboard

[12.0] Proposal: Shader:getUniforms

Open AllMeatball opened this issue 11 months ago • 7 comments

The idea with this proposal is that you could have an in-game shader editor, and you want to edit values without parsing the shader file or hardcoding values in scripts.

This proposed function would make be it quicker (and easier) to know what shader uniforms are available.

It would similarly to GL_ACTIVE_UNIFORMS in OpenGL 3.x.

You can look at the Program_Introspection Khronos wiki page for reference on how to get this data.

NOTE: I am aware that Love 12.0 has a few more rendering backends (than OpenGL) in version 11.x. Not quite sure how easy that would be implement in for all of them.

Example output:

{
  ['time'] = {type = 'float'},
  ['scroll_dir'] = {type = 'vec2'}
}

AllMeatball avatar Jan 13 '25 06:01 AllMeatball

I've see a potential flaw with my example output, tables with non-number keys aren't sorted.

Another way of implementing the function could be:

{
  {name = 'time', type = 'float'},
  {name = 'scroll_dir', type = 'vec2'}
}

When you print it out the result would always be read in the same order in from the graphics api, which shouldn't change.

for _, uniform in ipairs(example_shader:getUniforms()) do
  print("Name: ".. uniform.name .." , Type: ".. uniform.type)
end
--Result:
--[[
Name: time, Type: float,
Name: scroll_dir, Type: vec2
]]

In case the uniforms are in the wrong order, then you can use table.sort.

AllMeatball avatar Jan 14 '25 19:01 AllMeatball

What's the right order? In what situation would you need uniforms to be sorted? There isn't any inherent order internally or in your shader code.

slime73 avatar Jan 14 '25 19:01 slime73

What's the right order? In what situation would you need uniforms to be sorted? There isn't any inherent order internally or in your shader code.

You do make a good point. I believe the right order would be the location of the uniforms. OpenGL might already do this when you call GL_ACTIVE_UNIFORMS with glGetProgramiv, I don't know how you get the location on Vulkan and Metal but I think most game engines would be in that order (e.g. Godot). it would be annoying when looking for a specfic shader parameter in a live editor and they aren't sorted, it would especially be huge annoyance on shaders with many parameters.

AllMeatball avatar Jan 14 '25 21:01 AllMeatball

Location for individual numeric uniforms is actually not really a thing in Metal/Vulkan/Direct3D.

If you want to have a live editor, you should probably do your own sorting based on whatever parameters you think are good (for example separating textures from vectors from floats etc, and then sorting based on name within those separations, or something).

slime73 avatar Jan 14 '25 21:01 slime73

Location for individual numeric uniforms is actually not really a thing in Metal/Vulkan/Direct3D.

If you want to have a live editor, you should probably do your own sorting based on whatever parameters you think are good (for example separating textures from vectors from floats etc, and then sorting based on name within those separations, or something).

Probably the output of the latter format is the best for both cases, you can decide sort the table or not.

AllMeatball avatar Jan 14 '25 22:01 AllMeatball

For a key/value table, you can sort by putting the keys into an array an sorting that array, and then the values are accessed by going through the array to get the sorted key.

slime73 avatar Jan 14 '25 22:01 slime73

For a key/value table, you can sort by putting the keys into an array an sorting that array, and then the values are accessed by going through the array to get the sorted key.

That would make much more sense since the order isn't guaranteed. I didn't really think of doing that trick in hindsight.

AllMeatball avatar Jan 14 '25 22:01 AllMeatball