Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[12.0] Proposal: Shader:getUniforms #2137

Open
AllMeatball opened this issue Jan 13, 2025 · 7 comments
Open

[12.0] Proposal: Shader:getUniforms #2137

AllMeatball opened this issue Jan 13, 2025 · 7 comments
Labels
feature New feature or request

Comments

@AllMeatball
Copy link

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'}
}
@slime73 slime73 added the feature New feature or request label Jan 14, 2025
@AllMeatball
Copy link
Author

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.

@slime73
Copy link
Member

slime73 commented Jan 14, 2025

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.

@AllMeatball
Copy link
Author

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.

@slime73
Copy link
Member

slime73 commented Jan 14, 2025

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).

@AllMeatball
Copy link
Author

AllMeatball commented Jan 14, 2025

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.

@slime73
Copy link
Member

slime73 commented Jan 14, 2025

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.

@AllMeatball
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants