Commit b83f4b9f authored by Neil Roberts's avatar Neil Roberts Committed by Marge Bot

glsl: Add an IR lowering pass to convert mediump operations to 16-bit

This works by finding the first rvalue that it can lower using an
ir_rvalue_visitor. In that case it adds a conversion to float16
after each rvalue and a conversion back to float before storing
the assignment.

Also it uses a set to keep track of rvalues that have been
lowred already. The handle_rvalue method of the rvalue visitor doesn’t
provide any way to stop iteration. If we handle a value in
find_precision_visitor we want to be able to stop it from descending into
the lowered rvalue again.

Additionally this pass disallows converting nodes containing non-float.
The can_lower_rvalue function explicitly excludes any branches
that have non-float types except bools. This avoids the need to have
special handling for functions that convert to int or double.
Co-authored-by: Hyunjun Ko's avatarHyunjun Ko <>

v2. Adds lowering for texture samples

v3. Instead of checking whether each node can be lowered while walking the
tree, a separate tree walk is now done to check all of the nodes in a
single pass. The lowerable nodes are added to a set which is checked
during find_precision_visitor instead of calling can_lower_rvalue.

v4. Move the special case for temporaries to find_lowerable_rvalues. This
needs to be handled while checking for lowerable rvalues so that any
later dereferences of the variable will see the right precision.

v5. Add an override to visit ir_call instructions and apply the same
technique to override the precision of the temporary variable in the
same way as done for builtin temporaries and ir_assignment calls.

v6. Changes the pass so that it doesn’t need to lower an entire subtree in
order do perform a lowering. Instead, certain instructions can be
marked as being indepedent of their child instructions. For example,
this is the case with array dereferences. The precision of the array
index doesn’t have any bearing on whether things using the result of
the array deref can be lowered.

Now, only toplevel lowerable nodes are added to the lowerable_rvalues
instead instead of additionally adding all of the subnodes.

It now also only needs one hash table instead of two.

v7. Don’t try to lower sampler types. Instead, the sample instruction is
now treated as an independent point where the result of the sample can
be used in a lowered section. The precision of the sampler type
determines the precision of the sample instruction. This also means
the coordinates to the sampler can be lowered.

v8. Use f2fmp instead of f2f16.

v9.  Disable lowering derivatives calcualtions, which might not work
properly on some hw backends.
Reviewed-by: Kristian H. Kristensen's avatarKristian H. Kristensen <>
Part-of: <!3885>
parent c525785e
......@@ -112,6 +112,7 @@ LIBGLSL_FILES = \
glsl/lower_packed_varyings.cpp \
glsl/lower_named_interface_blocks.cpp \
glsl/lower_packing_builtins.cpp \
glsl/lower_precision.cpp \
glsl/lower_subroutine.cpp \
glsl/lower_tess_level.cpp \
glsl/lower_texture_projection.cpp \
......@@ -2232,7 +2232,12 @@ _mesa_glsl_compile_shader(struct gl_context *ctx, struct gl_shader *shader,
shader->Version = state->language_version;
shader->IsES = state->es_shader;
struct gl_shader_compiler_options *options =
if (!state->error && !shader->ir->is_empty()) {
if (options->LowerPrecision && shader->Stage == MESA_SHADER_FRAGMENT)
lower_subroutine(shader->ir, state);
opt_shader_and_create_symbol_table(ctx, state->symbols, shader);
......@@ -2559,6 +2559,9 @@ nir_visitor::visit(ir_texture *ir)
instr->dest_type = nir_type_float;
instr->dest_type = nir_type_float16;
instr->dest_type = nir_type_int;
......@@ -188,4 +188,6 @@ ir_variable *compare_index_block(ir_builder::ir_factory &body,
bool lower_64bit_integer_instructions(exec_list *instructions,
unsigned what_to_lower);
bool lower_precision(exec_list *instructions);
This diff is collapsed.
......@@ -162,6 +162,7 @@ files_libglsl = files(
......@@ -3179,6 +3179,12 @@ struct gl_shader_compiler_options
* be lowered for transform feedback
* If we can lower the precision of variables based on precision
* qualifiers
GLboolean LowerPrecision;
* \name Forms of indirect addressing the driver cannot do.
......@@ -338,6 +338,12 @@ void st_init_limits(struct pipe_screen *screen,
if (screen->get_param(screen, PIPE_CAP_PSIZ_CLAMPED))
options->LowerBuiltinVariablesXfb |= VARYING_BIT_PSIZ;
/* Initialize lower precision shader compiler option based on
* the value of PIPE_SHADER_CAP_FP16.
options->LowerPrecision =
screen->get_shader_param(screen, sh, PIPE_SHADER_CAP_FP16);
c->MaxUserAssignableUniformLocations =
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment