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

Allow shader arrays to be passed as parameters and return value in functions #48933

Merged
merged 1 commit into from
May 25, 2021

Conversation

Chaosus
Copy link
Member

@Chaosus Chaosus commented May 21, 2021

This PR significantly improves shader arrays by allowing them to be really passed to and from functions. Also, I refactored few methods for better functionality with them.

For example, such code will now behave correctly:

shader_type spatial;

struct Cool {
	vec3 v[2];
};

vec3[2] test(vec3 a[2]) {
	a[0].g = 0.0; // modify array inside function
	return a;
}

Cool[2] cool_test(Cool a[2]) {
	return {Cool({vec3(0.0, 0.0, 1.0), vec3(1.0, 0.0, 0.0)}), Cool({vec3(0.0, 0.0, 1.0), vec3(1.0, 0.0, 0.0)})};
}

void fragment() {
	// passing array to function "in-place"
	vec3 v[2] = test(vec3[2](vec3(0, 1, 0), vec3(1, 0, 0)));
	
	// assignment to existed array
	v = test({vec3(1, 1, 0), vec3(1, 0, 0)});
	
	// ternary operator
	bool t = true; 
	vec3 v3[2] = t ? v : test(v);
	
	// a complex assignment
	ALBEDO = v3[0] + cool_test({Cool(v), Cool({vec3(0.0, 0.0, 1.0), vec3(1.0, 0.0, 0.0)})})[0].v[0];
}

@Chaosus Chaosus requested a review from a team as a code owner May 21, 2021 16:24
@Chaosus Chaosus added this to the 4.0 milestone May 21, 2021
@Chaosus Chaosus force-pushed the shader_array_params branch from f78b377 to e44ef18 Compare May 21, 2021 16:33
@Chaosus Chaosus requested a review from clayjohn May 21, 2021 16:37
@Chaosus Chaosus changed the title Allow shader arrays to be passed as parameters and return value Allow shader arrays to be passed as parameters and return value in functions May 21, 2021
@Chaosus Chaosus force-pushed the shader_array_params branch from e44ef18 to a89487b Compare May 21, 2021 19:02
@Chaosus Chaosus force-pushed the shader_array_params branch from a89487b to 5874b7a Compare May 22, 2021 17:15
@akien-mga akien-mga merged commit f050719 into godotengine:master May 25, 2021
@akien-mga
Copy link
Member

Thanks!

@Chaosus Chaosus deleted the shader_array_params branch May 25, 2021 10:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants