2 * Copyright © 2019 Google, Inc
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
25 * \file lower_precision.cpp
28 #include "main/macros.h"
29 #include "main/mtypes.h"
30 #include "compiler/glsl_types.h"
32 #include "ir_builder.h"
33 #include "ir_optimization.h"
34 #include "ir_rvalue_visitor.h"
35 #include "util/half_float.h"
37 #include "util/hash_table.h"
42 class find_precision_visitor
: public ir_rvalue_enter_visitor
{
44 find_precision_visitor(const struct gl_shader_compiler_options
*options
);
45 ~find_precision_visitor();
47 virtual void handle_rvalue(ir_rvalue
**rvalue
);
48 virtual ir_visitor_status
visit_enter(ir_call
*ir
);
50 ir_function_signature
*map_builtin(ir_function_signature
*sig
);
54 /* Set of rvalues that can be lowered. This will be filled in by
55 * find_lowerable_rvalues_visitor. Only the root node of a lowerable section
56 * will be added to this set.
58 struct set
*lowerable_rvalues
;
61 * A mapping of builtin signature functions to lowered versions. This is
62 * filled in lazily when a lowered version is needed.
64 struct hash_table
*lowered_builtins
;
66 * A temporary hash table only used in order to clone functions.
68 struct hash_table
*clone_ht
;
70 void *lowered_builtin_mem_ctx
;
72 const struct gl_shader_compiler_options
*options
;
75 class find_lowerable_rvalues_visitor
: public ir_hierarchical_visitor
{
77 enum can_lower_state
{
83 enum parent_relation
{
84 /* The parent performs a further operation involving the result from the
85 * child and can be lowered along with it.
88 /* The parent instruction’s operation is independent of the child type so
89 * the child should be lowered separately.
91 INDEPENDENT_OPERATION
,
95 ir_instruction
*instr
;
96 enum can_lower_state state
;
97 /* List of child rvalues that can be lowered. When this stack entry is
98 * popped, if this node itself can’t be lowered than all of the children
99 * are root nodes to lower so we will add them to lowerable_rvalues.
100 * Otherwise if this node can also be lowered then we won’t add the
101 * children because we only want to add the topmost lowerable nodes to
102 * lowerable_rvalues and the children will be lowered as part of lowering
105 std::vector
<ir_instruction
*> lowerable_children
;
108 find_lowerable_rvalues_visitor(struct set
*result
,
109 const struct gl_shader_compiler_options
*options
);
110 bool can_lower_type(const glsl_type
*type
) const;
112 static void stack_enter(class ir_instruction
*ir
, void *data
);
113 static void stack_leave(class ir_instruction
*ir
, void *data
);
115 virtual ir_visitor_status
visit(ir_constant
*ir
);
116 virtual ir_visitor_status
visit(ir_dereference_variable
*ir
);
118 virtual ir_visitor_status
visit_enter(ir_dereference_record
*ir
);
119 virtual ir_visitor_status
visit_enter(ir_dereference_array
*ir
);
120 virtual ir_visitor_status
visit_enter(ir_texture
*ir
);
121 virtual ir_visitor_status
visit_enter(ir_expression
*ir
);
123 virtual ir_visitor_status
visit_leave(ir_assignment
*ir
);
124 virtual ir_visitor_status
visit_leave(ir_call
*ir
);
126 can_lower_state
handle_precision(const glsl_type
*type
,
127 int precision
) const;
129 static parent_relation
get_parent_relation(ir_instruction
*parent
,
130 ir_instruction
*child
);
132 std::vector
<stack_entry
> stack
;
133 struct set
*lowerable_rvalues
;
134 const struct gl_shader_compiler_options
*options
;
136 void pop_stack_entry();
137 void add_lowerable_children(const stack_entry
&entry
);
140 class lower_precision_visitor
: public ir_rvalue_visitor
{
142 virtual void handle_rvalue(ir_rvalue
**rvalue
);
143 virtual ir_visitor_status
visit_enter(ir_dereference_array
*);
144 virtual ir_visitor_status
visit_enter(ir_dereference_record
*);
145 virtual ir_visitor_status
visit_enter(ir_call
*ir
);
146 virtual ir_visitor_status
visit_enter(ir_texture
*ir
);
147 virtual ir_visitor_status
visit_leave(ir_expression
*);
151 find_lowerable_rvalues_visitor::can_lower_type(const glsl_type
*type
) const
153 /* Don’t lower any expressions involving non-float types except bool and
154 * texture samplers. This will rule out operations that change the type such
155 * as conversion to ints. Instead it will end up lowering the arguments
156 * instead and adding a final conversion to float32. We want to handle
157 * boolean types so that it will do comparisons as 16-bit.
160 switch (type
->base_type
) {
161 /* TODO: should we do anything for these two with regard to Int16 vs FP16
165 case GLSL_TYPE_SAMPLER
:
166 case GLSL_TYPE_IMAGE
:
169 case GLSL_TYPE_FLOAT
:
170 return options
->LowerPrecisionFloat16
;
174 return options
->LowerPrecisionInt16
;
181 find_lowerable_rvalues_visitor::find_lowerable_rvalues_visitor(struct set
*res
,
182 const struct gl_shader_compiler_options
*opts
)
184 lowerable_rvalues
= res
;
186 callback_enter
= stack_enter
;
187 callback_leave
= stack_leave
;
193 find_lowerable_rvalues_visitor::stack_enter(class ir_instruction
*ir
,
196 find_lowerable_rvalues_visitor
*state
=
197 (find_lowerable_rvalues_visitor
*) data
;
199 /* Add a new stack entry for this instruction */
203 entry
.state
= state
->in_assignee
? CANT_LOWER
: UNKNOWN
;
205 state
->stack
.push_back(entry
);
209 find_lowerable_rvalues_visitor::add_lowerable_children(const stack_entry
&entry
)
211 /* We can’t lower this node so if there were any pending children then they
212 * are all root lowerable nodes and we should add them to the set.
214 for (auto &it
: entry
.lowerable_children
)
215 _mesa_set_add(lowerable_rvalues
, it
);
219 find_lowerable_rvalues_visitor::pop_stack_entry()
221 const stack_entry
&entry
= stack
.back();
223 if (stack
.size() >= 2) {
224 /* Combine this state into the parent state, unless the parent operation
225 * doesn’t have any relation to the child operations
227 stack_entry
&parent
= stack
.end()[-2];
228 parent_relation rel
= get_parent_relation(parent
.instr
, entry
.instr
);
230 if (rel
== COMBINED_OPERATION
) {
231 switch (entry
.state
) {
233 parent
.state
= CANT_LOWER
;
236 if (parent
.state
== UNKNOWN
)
237 parent
.state
= SHOULD_LOWER
;
245 if (entry
.state
== SHOULD_LOWER
) {
246 ir_rvalue
*rv
= entry
.instr
->as_rvalue();
249 add_lowerable_children(entry
);
250 } else if (stack
.size() >= 2) {
251 stack_entry
&parent
= stack
.end()[-2];
253 switch (get_parent_relation(parent
.instr
, rv
)) {
254 case COMBINED_OPERATION
:
255 /* We only want to add the toplevel lowerable instructions to the
256 * lowerable set. Therefore if there is a parent then instead of
257 * adding this instruction to the set we will queue depending on
258 * the result of the parent instruction.
260 parent
.lowerable_children
.push_back(entry
.instr
);
262 case INDEPENDENT_OPERATION
:
263 _mesa_set_add(lowerable_rvalues
, rv
);
267 /* This is a toplevel node so add it directly to the lowerable
270 _mesa_set_add(lowerable_rvalues
, rv
);
272 } else if (entry
.state
== CANT_LOWER
) {
273 add_lowerable_children(entry
);
280 find_lowerable_rvalues_visitor::stack_leave(class ir_instruction
*ir
,
283 find_lowerable_rvalues_visitor
*state
=
284 (find_lowerable_rvalues_visitor
*) data
;
286 state
->pop_stack_entry();
289 enum find_lowerable_rvalues_visitor::can_lower_state
290 find_lowerable_rvalues_visitor::handle_precision(const glsl_type
*type
,
293 if (!can_lower_type(type
))
297 case GLSL_PRECISION_NONE
:
299 case GLSL_PRECISION_HIGH
:
301 case GLSL_PRECISION_MEDIUM
:
302 case GLSL_PRECISION_LOW
:
309 enum find_lowerable_rvalues_visitor::parent_relation
310 find_lowerable_rvalues_visitor::get_parent_relation(ir_instruction
*parent
,
311 ir_instruction
*child
)
313 /* If the parent is a dereference instruction then the only child could be
314 * for example an array dereference and that should be lowered independently
317 if (parent
->as_dereference())
318 return INDEPENDENT_OPERATION
;
320 /* The precision of texture sampling depend on the precision of the sampler.
321 * The rest of the arguments don’t matter so we can treat it as an
322 * independent operation.
324 if (parent
->as_texture())
325 return INDEPENDENT_OPERATION
;
327 return COMBINED_OPERATION
;
331 find_lowerable_rvalues_visitor::visit(ir_constant
*ir
)
333 stack_enter(ir
, this);
335 if (!can_lower_type(ir
->type
))
336 stack
.back().state
= CANT_LOWER
;
338 stack_leave(ir
, this);
340 return visit_continue
;
344 find_lowerable_rvalues_visitor::visit(ir_dereference_variable
*ir
)
346 stack_enter(ir
, this);
348 if (stack
.back().state
== UNKNOWN
)
349 stack
.back().state
= handle_precision(ir
->type
, ir
->precision());
351 stack_leave(ir
, this);
353 return visit_continue
;
357 find_lowerable_rvalues_visitor::visit_enter(ir_dereference_record
*ir
)
359 ir_hierarchical_visitor::visit_enter(ir
);
361 if (stack
.back().state
== UNKNOWN
)
362 stack
.back().state
= handle_precision(ir
->type
, ir
->precision());
364 return visit_continue
;
368 find_lowerable_rvalues_visitor::visit_enter(ir_dereference_array
*ir
)
370 ir_hierarchical_visitor::visit_enter(ir
);
372 if (stack
.back().state
== UNKNOWN
)
373 stack
.back().state
= handle_precision(ir
->type
, ir
->precision());
375 return visit_continue
;
379 find_lowerable_rvalues_visitor::visit_enter(ir_texture
*ir
)
381 ir_hierarchical_visitor::visit_enter(ir
);
383 if (stack
.back().state
== UNKNOWN
) {
384 /* The precision of the sample value depends on the precision of the
387 stack
.back().state
= handle_precision(ir
->type
,
388 ir
->sampler
->precision());
391 return visit_continue
;
395 find_lowerable_rvalues_visitor::visit_enter(ir_expression
*ir
)
397 ir_hierarchical_visitor::visit_enter(ir
);
399 if (!can_lower_type(ir
->type
))
400 stack
.back().state
= CANT_LOWER
;
402 /* Don't lower precision for derivative calculations */
403 if (!options
->LowerPrecisionDerivatives
&&
404 (ir
->operation
== ir_unop_dFdx
||
405 ir
->operation
== ir_unop_dFdx_coarse
||
406 ir
->operation
== ir_unop_dFdx_fine
||
407 ir
->operation
== ir_unop_dFdy
||
408 ir
->operation
== ir_unop_dFdy_coarse
||
409 ir
->operation
== ir_unop_dFdy_fine
)) {
410 stack
.back().state
= CANT_LOWER
;
413 return visit_continue
;
417 is_lowerable_builtin(ir_call
*ir
,
418 const struct set
*lowerable_rvalues
)
420 /* The intrinsic call is inside the wrapper imageLoad function that will
421 * be inlined. We have to handle both of them.
423 if (ir
->callee
->intrinsic_id
== ir_intrinsic_image_load
||
424 (ir
->callee
->is_builtin() &&
425 !strcmp(ir
->callee_name(), "imageLoad"))) {
426 ir_rvalue
*param
= (ir_rvalue
*)ir
->actual_parameters
.get_head();
427 ir_variable
*resource
= param
->variable_referenced();
429 assert(ir
->callee
->return_precision
== GLSL_PRECISION_NONE
);
430 assert(resource
->type
->without_array()->is_image());
432 /* GLSL ES 3.20 requires that images have a precision modifier, but if
433 * you set one, it doesn't do anything, because all intrinsics are
434 * defined with highp. This seems to be a spec bug.
436 * In theory we could set the return value to mediump if the image
437 * format has a lower precision. This appears to be the most sensible
440 const struct util_format_description
*desc
=
441 util_format_description(resource
->data
.image_format
);
443 util_format_get_first_non_void_channel(resource
->data
.image_format
);
445 if (desc
->channel
[i
].pure_integer
||
446 desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_FLOAT
)
447 return desc
->channel
[i
].size
<= 16;
449 return desc
->channel
[i
].size
<= 10; /* unorm/snorm */
452 /* Handle special calls. */
453 if (ir
->callee
->is_builtin() && ir
->actual_parameters
.length()) {
454 ir_rvalue
*param
= (ir_rvalue
*)ir
->actual_parameters
.get_head();
455 ir_variable
*var
= param
->variable_referenced();
457 /* Handle builtin wrappers around ir_texture opcodes. These wrappers will
458 * be inlined by lower_precision() if we return true here, so that we can
459 * get to ir_texture later and do proper lowering.
461 * We should lower the type of the return value if the sampler type
462 * uses lower precision. The function parameters don't matter.
464 if (var
&& var
->type
->without_array()->is_sampler()) {
465 return var
->data
.precision
== GLSL_PRECISION_MEDIUM
||
466 var
->data
.precision
== GLSL_PRECISION_LOW
;
470 if (!ir
->callee
->is_builtin())
473 assert(ir
->callee
->return_precision
== GLSL_PRECISION_NONE
);
475 foreach_in_list(ir_rvalue
, param
, &ir
->actual_parameters
) {
476 if (!param
->as_constant() &&
477 _mesa_set_search(lowerable_rvalues
, param
) == NULL
)
485 find_lowerable_rvalues_visitor::visit_leave(ir_call
*ir
)
487 ir_hierarchical_visitor::visit_leave(ir
);
489 /* Special case for handling temporary variables generated by the compiler
490 * for function calls. If we assign to one of these using a function call
491 * that has a lowerable return type then we can assume the temporary
492 * variable should have a medium precision too.
495 /* Do nothing if the return type is void. */
496 if (!ir
->return_deref
)
497 return visit_continue
;
499 ir_variable
*var
= ir
->return_deref
->variable_referenced();
501 assert(var
->data
.mode
== ir_var_temporary
);
503 unsigned return_precision
= ir
->callee
->return_precision
;
505 /* If the call is to a builtin, then the function won’t have a return
506 * precision and we should determine it from the precision of the arguments.
508 if (is_lowerable_builtin(ir
, lowerable_rvalues
))
509 return_precision
= GLSL_PRECISION_MEDIUM
;
511 can_lower_state lower_state
=
512 handle_precision(var
->type
, return_precision
);
514 if (lower_state
== SHOULD_LOWER
) {
515 /* There probably shouldn’t be any situations where multiple ir_call
516 * instructions write to the same temporary?
518 assert(var
->data
.precision
== GLSL_PRECISION_NONE
);
519 var
->data
.precision
= GLSL_PRECISION_MEDIUM
;
521 var
->data
.precision
= GLSL_PRECISION_HIGH
;
524 return visit_continue
;
528 find_lowerable_rvalues_visitor::visit_leave(ir_assignment
*ir
)
530 ir_hierarchical_visitor::visit_leave(ir
);
532 /* Special case for handling temporary variables generated by the compiler.
533 * If we assign to one of these using a lowered precision then we can assume
534 * the temporary variable should have a medium precision too.
536 ir_variable
*var
= ir
->lhs
->variable_referenced();
538 if (var
->data
.mode
== ir_var_temporary
) {
539 if (_mesa_set_search(lowerable_rvalues
, ir
->rhs
)) {
540 /* Only override the precision if this is the first assignment. For
541 * temporaries such as the ones generated for the ?: operator there
542 * can be multiple assignments with different precisions. This way we
543 * get the highest precision of all of the assignments.
545 if (var
->data
.precision
== GLSL_PRECISION_NONE
)
546 var
->data
.precision
= GLSL_PRECISION_MEDIUM
;
547 } else if (!ir
->rhs
->as_constant()) {
548 var
->data
.precision
= GLSL_PRECISION_HIGH
;
552 return visit_continue
;
556 find_lowerable_rvalues(const struct gl_shader_compiler_options
*options
,
557 exec_list
*instructions
,
560 find_lowerable_rvalues_visitor
v(result
, options
);
562 visit_list_elements(&v
, instructions
);
564 assert(v
.stack
.empty());
568 convert_precision(glsl_base_type type
, bool up
, ir_rvalue
*ir
)
570 unsigned new_type
, op
;
574 case GLSL_TYPE_FLOAT16
:
575 new_type
= GLSL_TYPE_FLOAT
;
578 case GLSL_TYPE_INT16
:
579 new_type
= GLSL_TYPE_INT
;
582 case GLSL_TYPE_UINT16
:
583 new_type
= GLSL_TYPE_UINT
;
587 unreachable("invalid type");
592 case GLSL_TYPE_FLOAT
:
593 new_type
= GLSL_TYPE_FLOAT16
;
597 new_type
= GLSL_TYPE_INT16
;
601 new_type
= GLSL_TYPE_UINT16
;
605 unreachable("invalid type");
610 const glsl_type
*desired_type
;
611 desired_type
= glsl_type::get_instance(new_type
,
612 ir
->type
->vector_elements
,
613 ir
->type
->matrix_columns
);
615 void *mem_ctx
= ralloc_parent(ir
);
616 return new(mem_ctx
) ir_expression(op
, desired_type
, ir
, NULL
);
619 static glsl_base_type
620 lower_type(glsl_base_type type
)
623 case GLSL_TYPE_FLOAT
:
624 return GLSL_TYPE_FLOAT16
;
626 return GLSL_TYPE_INT16
;
628 return GLSL_TYPE_UINT16
;
630 unreachable("invalid type");
631 return GLSL_TYPE_ERROR
;;
636 lower_precision_visitor::handle_rvalue(ir_rvalue
**rvalue
)
638 ir_rvalue
*ir
= *rvalue
;
643 if (ir
->as_dereference()) {
644 if (!ir
->type
->is_boolean())
645 *rvalue
= convert_precision(ir
->type
->base_type
, false, ir
);
646 } else if (ir
->type
->base_type
== GLSL_TYPE_FLOAT
||
647 ir
->type
->base_type
== GLSL_TYPE_INT
||
648 ir
->type
->base_type
== GLSL_TYPE_UINT
) {
649 ir
->type
= glsl_type::get_instance(lower_type(ir
->type
->base_type
),
650 ir
->type
->vector_elements
,
651 ir
->type
->matrix_columns
,
652 ir
->type
->explicit_stride
,
653 ir
->type
->interface_row_major
);
655 ir_constant
*const_ir
= ir
->as_constant();
658 ir_constant_data value
;
660 if (ir
->type
->base_type
== GLSL_TYPE_FLOAT16
) {
661 for (unsigned i
= 0; i
< ARRAY_SIZE(value
.f16
); i
++)
662 value
.f16
[i
] = _mesa_float_to_half(const_ir
->value
.f
[i
]);
663 } else if (ir
->type
->base_type
== GLSL_TYPE_INT16
) {
664 for (unsigned i
= 0; i
< ARRAY_SIZE(value
.i16
); i
++)
665 value
.i16
[i
] = const_ir
->value
.i
[i
];
666 } else if (ir
->type
->base_type
== GLSL_TYPE_UINT16
) {
667 for (unsigned i
= 0; i
< ARRAY_SIZE(value
.u16
); i
++)
668 value
.u16
[i
] = const_ir
->value
.u
[i
];
670 unreachable("invalid type");
673 const_ir
->value
= value
;
679 lower_precision_visitor::visit_enter(ir_dereference_record
*ir
)
681 /* We don’t want to lower the variable */
682 return visit_continue_with_parent
;
686 lower_precision_visitor::visit_enter(ir_dereference_array
*ir
)
688 /* We don’t want to convert the array index or the variable. If the array
689 * index itself is lowerable that will be handled separately.
691 return visit_continue_with_parent
;
695 lower_precision_visitor::visit_enter(ir_call
*ir
)
697 /* We don’t want to convert the arguments. These will be handled separately.
699 return visit_continue_with_parent
;
703 lower_precision_visitor::visit_enter(ir_texture
*ir
)
705 /* We don’t want to convert the arguments. These will be handled separately.
707 return visit_continue_with_parent
;
711 lower_precision_visitor::visit_leave(ir_expression
*ir
)
713 ir_rvalue_visitor::visit_leave(ir
);
715 /* If the expression is a conversion operation to or from bool then fix the
718 switch (ir
->operation
) {
720 ir
->operation
= ir_unop_b2f16
;
723 ir
->operation
= ir_unop_f162b
;
727 /* Nothing to do - they both support int16. */
733 return visit_continue
;
737 find_precision_visitor::handle_rvalue(ir_rvalue
**rvalue
)
739 /* Checking the precision of rvalue can be lowered first throughout
740 * find_lowerable_rvalues_visitor.
741 * Once it found the precision of rvalue can be lowered, then we can
742 * add conversion f2fmp, etc. through lower_precision_visitor.
747 struct set_entry
*entry
= _mesa_set_search(lowerable_rvalues
, *rvalue
);
752 _mesa_set_remove(lowerable_rvalues
, entry
);
754 /* If the entire expression is just a variable dereference then trying to
755 * lower it will just directly add pointless to and from conversions without
756 * any actual operation in-between. Although these will eventually get
757 * optimised out, avoiding generating them here also avoids breaking inout
758 * parameters to functions.
760 if ((*rvalue
)->as_dereference())
763 lower_precision_visitor v
;
765 (*rvalue
)->accept(&v
);
766 v
.handle_rvalue(rvalue
);
768 /* We don’t need to add the final conversion if the final type has been
771 if ((*rvalue
)->type
->base_type
!= GLSL_TYPE_BOOL
)
772 *rvalue
= convert_precision((*rvalue
)->type
->base_type
, true, *rvalue
);
778 find_precision_visitor::visit_enter(ir_call
*ir
)
780 ir_rvalue_enter_visitor::visit_enter(ir
);
782 ir_variable
*return_var
=
783 ir
->return_deref
? ir
->return_deref
->variable_referenced() : NULL
;
785 /* Don't do anything for image_load here. We have only changed the return
786 * value to mediump/lowp, so that following instructions can use reduced
789 * The return value type of the intrinsic itself isn't changed here, but
790 * can be changed in NIR if all users use the *2*mp opcode.
792 if (ir
->callee
->intrinsic_id
== ir_intrinsic_image_load
)
793 return visit_continue
;
795 /* If this is a call to a builtin and the find_lowerable_rvalues_visitor
796 * overrode the precision of the temporary return variable, then we can
797 * replace the builtin implementation with a lowered version.
800 if (!ir
->callee
->is_builtin() ||
801 return_var
== NULL
||
802 (return_var
->data
.precision
!= GLSL_PRECISION_MEDIUM
&&
803 return_var
->data
.precision
!= GLSL_PRECISION_LOW
))
804 return visit_continue
;
806 ir
->callee
= map_builtin(ir
->callee
);
807 ir
->generate_inline(ir
);
810 return visit_continue_with_parent
;
813 ir_function_signature
*
814 find_precision_visitor::map_builtin(ir_function_signature
*sig
)
816 if (lowered_builtins
== NULL
) {
817 lowered_builtins
= _mesa_pointer_hash_table_create(NULL
);
818 clone_ht
=_mesa_pointer_hash_table_create(NULL
);
819 lowered_builtin_mem_ctx
= ralloc_context(NULL
);
821 struct hash_entry
*entry
= _mesa_hash_table_search(lowered_builtins
, sig
);
823 return (ir_function_signature
*) entry
->data
;
826 ir_function_signature
*lowered_sig
=
827 sig
->clone(lowered_builtin_mem_ctx
, clone_ht
);
829 foreach_in_list(ir_variable
, param
, &lowered_sig
->parameters
) {
830 param
->data
.precision
= GLSL_PRECISION_MEDIUM
;
833 lower_precision(options
, &lowered_sig
->body
);
835 _mesa_hash_table_clear(clone_ht
, NULL
);
837 _mesa_hash_table_insert(lowered_builtins
, sig
, lowered_sig
);
842 find_precision_visitor::find_precision_visitor(const struct gl_shader_compiler_options
*options
)
844 lowerable_rvalues(_mesa_pointer_set_create(NULL
)),
845 lowered_builtins(NULL
),
847 lowered_builtin_mem_ctx(NULL
),
852 find_precision_visitor::~find_precision_visitor()
854 _mesa_set_destroy(lowerable_rvalues
, NULL
);
856 if (lowered_builtins
) {
857 _mesa_hash_table_destroy(lowered_builtins
, NULL
);
858 _mesa_hash_table_destroy(clone_ht
, NULL
);
859 ralloc_free(lowered_builtin_mem_ctx
);
866 lower_precision(const struct gl_shader_compiler_options
*options
,
867 exec_list
*instructions
)
869 find_precision_visitor
v(options
);
871 find_lowerable_rvalues(options
, instructions
, v
.lowerable_rvalues
);
873 visit_list_elements(&v
, instructions
);