Skip to content

OpenGL: Better support for struct uniforms #938

Description

@DragonMoffon

I was developing a shader that takes in a Mercator map and projects it to a sphere, and I found 2 Issues with uniforms within my gl fragment shader. One is an enhancement that would make life a lot easier, and the other is a bug.

I realized that these are two separate issues and I've split them into to separate issues.

Enhancement request:

Currently if you use a struct as a uniform only the fields that are used within the script can be found in the program._uniforms dictionary, and arrays of structs do not work well.

for example:
Vertex code: [basic_vertex.glsl]

#version 430

in vec2 vert_pos;

void main() {
	gl_Position = vec4(vert_pos, 0, 1);
}

Fragment code: [example_frag.glsl]

#version 430

struct planet
{
	vec2 pos;
	float radius;
	float mass;
};

uniform planet earth;

in vec4 gl_FragCoord;

out vec4 fragColor;


void main() {
    float pixel_distance = distance(earth.pos, gl_FragCoord.xy);
    fragColor = vec4(vec3(1 - (pixel_distance/earth.radius)), 1);
}

Python code:

import arcade

if __name__ == '__main__':
    window = arcade.Window()
    program = window.ctx.load_program(
        vertex_shader="basic_vertex.glsl",
        fragment_shader="example_frag.glsl"
    )
    print(program._uniforms)

If you run the above code you get the following print statement:
image

Now this is understandable. Only uniforms that are used within the code are actually detected by the program loader, and any uniforms that were declared but never used don't appear in the _uniforms dictionary. My issue is that the two variables are separate, and I have to set each item individually. That by itself is annoying, but I can work around it.

The real problem comes when I want to make an array of structs. (For example, an array of lights, or in my case an array of planets)

If I modify the above fragment code to be

#version 430

struct planet
{
	vec2 pos;
	float radius;
	float mass;
};

uniform planet planets[9];

in vec4 gl_FragCoord;

out vec4 fragColor;


void main() {
	float avg_disance;
	float avg_radius;

	planet current_planet;

	for (int i = 0; i < planets.length(); i++)
	{
		current_planet = planets[i];
		avg_disance = avg_disance + distance(current_planet.pos, gl_FragCoord.xy);
		avg_radius = avg_radius + current_planet.radius;
	}
	avg_radius = avg_radius/planets.length();
    float pixel_distance = avg_disance/planets.length();
    fragColor = vec4(vec3(1 - (pixel_distance/avg_radius)), 1);
}

and run the code again I get the following print statement
(I have text wrapping on so the whole print can be seen, it is all one line):
image

Notice how it shows each and every attribute of each struct as it's own unique item in the dictionary. This is so unhelpful. It makes using structs, and arrays of structs so much slower than it has to be.

What should be added/changed?

The best way would be to make a Struct class that inherited from the Uniforms class, and have a unique loading method for structs and struct arrays that works with a dictionary or a dataclass.

It would be fine if they worked like vectors and normal arrays (having a 1 dimensional list of the input data that the program splits into each item of the vector/array)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions