glsl-optimizer icon indicating copy to clipboard operation
glsl-optimizer copied to clipboard

Uniform buffer object declarations not being emitted correctly.

Open plynkus opened this issue 9 years ago • 3 comments

(I am wandering through the print visitors at this moment to find root cause / possible action, but figured I would submit an issue in case I am missing something obvious.)

Observed: The output stage (post-optimization) does not appear to emit GLSL uniform buffer objects that appear to be properly tracked during optimization.

E.g.:, a vertex shader that starts with:

version 300 es

layout (std140) uniform ModelViewProjectionConstantBuffer { mat4 model; mat4 view; mat4 projection; } mvp;

. . . with requisite access to mvp.model, mvp.view, mvp.projection in the shader body . . . . . . results in a shader that begins with:

version 300 es

uniform ModelViewProjectionConstantBuffer mvp;

. . . losing the layout definition entirely, resulting in errors if the resulting shader is compiled. The declaration is no longer valid.

The ir appears to track the definition okay (base type GLSL_TYPE_INTERFACE, name "ModelViewProjectionConstantBuffer", length of 3, first entry in the "fields" union is a seemingly valid representation of a mat4 named "model".

I suspect that this is an issue with the print-visiting only, will dig into it further.

plynkus avatar Aug 09 '14 23:08 plynkus

Any update on this? Did you have any luck plynkus?

themikelester avatar Jan 17 '15 00:01 themikelester

With some local, dirty, probably use case specific hacks to _mesa_print_ir_glsl() within ir_print_glsl_visitor.cpp, as I recall. I haven't looked at it in a while, but this is a good reminder to pull anew / retest / consider something more clean.

plynkus avatar Jan 25 '15 16:01 plynkus

Same issue. Yes, UBO are parsed but no printed in the glsl output. Indeed, a quick modifications can be done in the function _mesa_print_ir_glsl (file ir_print_glsl_visitor.cpp), especially in the ir_print_glsl_visitor visitor for variable :

void ir_print_glsl_visitor::visit(ir_variable *ir)

After the line :

buffer.asprintf_append ("%s%s%s%s", cent, inv, interp[ir->data.interpolation], mode[decormode][ir->data.mode]);

we can add a ubo printer like :

buffer.asprintf_append ("%s%s%s%s", cent, inv, interp[ir->data.interpolation], mode[decormode][ir->data.mode]);
	print_precision (ir, ir->type);
	print_type(buffer, ir->type, false);

	if (ir->type->base_type == GLSL_TYPE_INTERFACE)
	{
		buffer.asprintf_append("\n{\n");

		for (auto pid = 0u; pid < ir->type->length; ++pid)
		{
			const auto &structField = ir->type->fields.structure[pid];
			
			buffer.asprintf_append("\t");
			print_precision(ir, structField.type);
			print_type(buffer, structField.type, false);
			buffer.asprintf_append(" ");
			buffer.asprintf_append(structField.name);

			if (structField.type->base_type == GLSL_TYPE_ARRAY)
			{
				buffer.asprintf_append("[%u]", structField.type->length);
			}

			buffer.asprintf_append(";\n");
		}

		buffer.asprintf_append("\n}");
	}
	
	buffer.asprintf_append (" ");
	print_var_name (ir);

Asubayo avatar Sep 08 '17 19:09 Asubayo