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

Support global const arrays in the shader language #3314

Closed
AndreSacilotto opened this issue Sep 18, 2021 · 1 comment
Closed

Support global const arrays in the shader language #3314

AndreSacilotto opened this issue Sep 18, 2021 · 1 comment
Milestone

Comments

@AndreSacilotto
Copy link

AndreSacilotto commented Sep 18, 2021

Describe the project you are working on

Trying to make shader effects on sprites.

Describe the problem or limitation you are having in your project

I'm trying to create a const array to loop troght in the fragment function.

I don't know if this proposal is related to #931. It is basically the same thing but for consts.

When I read the documentation I thinked that would work for both closure and global. But nope they only work inside a function.
https://docs.godotengine.org/en/3.3/tutorials/shading/shading_reference/shading_language.html#constants

Describe the feature / enhancement and how it helps to overcome the problem or limitation

Add the option to use const with array

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

As a example:
This dont compile "error(4): - Expected initialization of constant":

const float arr[] = { 0.1, 0.2, 0.3 };

void fragment(){
	for(int i = 0; i < 3; i++)
		COLOR.a = arr[i];
	COLOR.rgb = texture(TEXTURE, UV).rgb;
}

This compile:

void fragment(){
	const float arr[] = { 0.1, 0.2, 0.3 };
	for(int i = 0; i < 3; i++)
		COLOR.a = arr[i];
	COLOR.rgb = texture(TEXTURE, UV).rgb;
}

I don't know if the second code create a array per fragment execution or not. Is there a way to debug this type of things?

If this enhancement will not be used often, can it be worked around with a few lines of script?

No, there isn't a performance workaround. You could simple put the array inside the fragment or vertex, but the array will be create once per iteration (I belive). This is why global const are usefull, they are only created once.

Is there a reason why this should be core and not an add-on in the asset library?

I don't think you can have shaders addons

@Calinou
Copy link
Member

Calinou commented Sep 18, 2021

Support for shader global const arrays was already merged in 3.x and is available in 3.4beta4: godotengine/godot#50889

This is also implemented in the master branch (Godot 4.0): godotengine/godot#32751

@Calinou Calinou closed this as completed Sep 18, 2021
@Calinou Calinou added this to the 3.x milestone Sep 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants