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
);
111 static void stack_enter(class ir_instruction
*ir
, void *data
);
112 static void stack_leave(class ir_instruction
*ir
, void *data
);
114 virtual ir_visitor_status
visit(ir_constant
*ir
);
115 virtual ir_visitor_status
visit(ir_dereference_variable
*ir
);
117 virtual ir_visitor_status
visit_enter(ir_dereference_record
*ir
);
118 virtual ir_visitor_status
visit_enter(ir_dereference_array
*ir
);
119 virtual ir_visitor_status
visit_enter(ir_texture
*ir
);
120 virtual ir_visitor_status
visit_enter(ir_expression
*ir
);
122 virtual ir_visitor_status
visit_leave(ir_assignment
*ir
);
123 virtual ir_visitor_status
visit_leave(ir_call
*ir
);
125 can_lower_state
handle_precision(const glsl_type
*type
,
126 int precision
) const;
128 static parent_relation
get_parent_relation(ir_instruction
*parent
,
129 ir_instruction
*child
);
131 std::vector
<stack_entry
> stack
;
132 struct set
*lowerable_rvalues
;
133 const struct gl_shader_compiler_options
*options
;
135 void pop_stack_entry();
136 void add_lowerable_children(const stack_entry
&entry
);
139 class lower_precision_visitor
: public ir_rvalue_visitor
{
141 virtual void handle_rvalue(ir_rvalue
**rvalue
);
142 virtual ir_visitor_status
visit_enter(ir_dereference_array
*);
143 virtual ir_visitor_status
visit_enter(ir_dereference_record
*);
144 virtual ir_visitor_status
visit_enter(ir_call
*ir
);
145 virtual ir_visitor_status
visit_enter(ir_texture
*ir
);
146 virtual ir_visitor_status
visit_leave(ir_expression
*);
150 can_lower_type(const struct gl_shader_compiler_options
*options
,
151 const glsl_type
*type
)
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(options
, 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(options
, 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(options
, 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());
567 static const glsl_type
*
568 lower_glsl_type(const glsl_type
*type
)
570 glsl_base_type new_base_type
;
572 switch (type
->base_type
) {
573 case GLSL_TYPE_FLOAT
:
574 new_base_type
= GLSL_TYPE_FLOAT16
;
577 new_base_type
= GLSL_TYPE_INT16
;
580 new_base_type
= GLSL_TYPE_UINT16
;
583 unreachable("invalid type");
587 return glsl_type::get_instance(new_base_type
,
588 type
->vector_elements
,
589 type
->matrix_columns
,
590 type
->explicit_stride
,
591 type
->interface_row_major
);
595 convert_precision(glsl_base_type type
, bool up
, ir_rvalue
*ir
)
597 unsigned new_type
, op
;
601 case GLSL_TYPE_FLOAT16
:
602 new_type
= GLSL_TYPE_FLOAT
;
605 case GLSL_TYPE_INT16
:
606 new_type
= GLSL_TYPE_INT
;
609 case GLSL_TYPE_UINT16
:
610 new_type
= GLSL_TYPE_UINT
;
614 unreachable("invalid type");
619 case GLSL_TYPE_FLOAT
:
620 new_type
= GLSL_TYPE_FLOAT16
;
624 new_type
= GLSL_TYPE_INT16
;
628 new_type
= GLSL_TYPE_UINT16
;
632 unreachable("invalid type");
637 const glsl_type
*desired_type
;
638 desired_type
= glsl_type::get_instance(new_type
,
639 ir
->type
->vector_elements
,
640 ir
->type
->matrix_columns
);
642 void *mem_ctx
= ralloc_parent(ir
);
643 return new(mem_ctx
) ir_expression(op
, desired_type
, ir
, NULL
);
647 lower_precision_visitor::handle_rvalue(ir_rvalue
**rvalue
)
649 ir_rvalue
*ir
= *rvalue
;
654 if (ir
->as_dereference()) {
655 if (!ir
->type
->is_boolean())
656 *rvalue
= convert_precision(ir
->type
->base_type
, false, ir
);
657 } else if (ir
->type
->base_type
== GLSL_TYPE_FLOAT
||
658 ir
->type
->base_type
== GLSL_TYPE_INT
||
659 ir
->type
->base_type
== GLSL_TYPE_UINT
) {
660 ir
->type
= lower_glsl_type(ir
->type
);
662 ir_constant
*const_ir
= ir
->as_constant();
665 ir_constant_data value
;
667 if (ir
->type
->base_type
== GLSL_TYPE_FLOAT16
) {
668 for (unsigned i
= 0; i
< ARRAY_SIZE(value
.f16
); i
++)
669 value
.f16
[i
] = _mesa_float_to_half(const_ir
->value
.f
[i
]);
670 } else if (ir
->type
->base_type
== GLSL_TYPE_INT16
) {
671 for (unsigned i
= 0; i
< ARRAY_SIZE(value
.i16
); i
++)
672 value
.i16
[i
] = const_ir
->value
.i
[i
];
673 } else if (ir
->type
->base_type
== GLSL_TYPE_UINT16
) {
674 for (unsigned i
= 0; i
< ARRAY_SIZE(value
.u16
); i
++)
675 value
.u16
[i
] = const_ir
->value
.u
[i
];
677 unreachable("invalid type");
680 const_ir
->value
= value
;
686 lower_precision_visitor::visit_enter(ir_dereference_record
*ir
)
688 /* We don’t want to lower the variable */
689 return visit_continue_with_parent
;
693 lower_precision_visitor::visit_enter(ir_dereference_array
*ir
)
695 /* We don’t want to convert the array index or the variable. If the array
696 * index itself is lowerable that will be handled separately.
698 return visit_continue_with_parent
;
702 lower_precision_visitor::visit_enter(ir_call
*ir
)
704 /* We don’t want to convert the arguments. These will be handled separately.
706 return visit_continue_with_parent
;
710 lower_precision_visitor::visit_enter(ir_texture
*ir
)
712 /* We don’t want to convert the arguments. These will be handled separately.
714 return visit_continue_with_parent
;
718 lower_precision_visitor::visit_leave(ir_expression
*ir
)
720 ir_rvalue_visitor::visit_leave(ir
);
722 /* If the expression is a conversion operation to or from bool then fix the
725 switch (ir
->operation
) {
727 ir
->operation
= ir_unop_b2f16
;
730 ir
->operation
= ir_unop_f162b
;
734 /* Nothing to do - they both support int16. */
740 return visit_continue
;
744 find_precision_visitor::handle_rvalue(ir_rvalue
**rvalue
)
746 /* Checking the precision of rvalue can be lowered first throughout
747 * find_lowerable_rvalues_visitor.
748 * Once it found the precision of rvalue can be lowered, then we can
749 * add conversion f2fmp, etc. through lower_precision_visitor.
754 struct set_entry
*entry
= _mesa_set_search(lowerable_rvalues
, *rvalue
);
759 _mesa_set_remove(lowerable_rvalues
, entry
);
761 /* If the entire expression is just a variable dereference then trying to
762 * lower it will just directly add pointless to and from conversions without
763 * any actual operation in-between. Although these will eventually get
764 * optimised out, avoiding generating them here also avoids breaking inout
765 * parameters to functions.
767 if ((*rvalue
)->as_dereference())
770 lower_precision_visitor v
;
772 (*rvalue
)->accept(&v
);
773 v
.handle_rvalue(rvalue
);
775 /* We don’t need to add the final conversion if the final type has been
778 if ((*rvalue
)->type
->base_type
!= GLSL_TYPE_BOOL
)
779 *rvalue
= convert_precision((*rvalue
)->type
->base_type
, true, *rvalue
);
785 find_precision_visitor::visit_enter(ir_call
*ir
)
787 ir_rvalue_enter_visitor::visit_enter(ir
);
789 ir_variable
*return_var
=
790 ir
->return_deref
? ir
->return_deref
->variable_referenced() : NULL
;
792 /* Don't do anything for image_load here. We have only changed the return
793 * value to mediump/lowp, so that following instructions can use reduced
796 * The return value type of the intrinsic itself isn't changed here, but
797 * can be changed in NIR if all users use the *2*mp opcode.
799 if (ir
->callee
->intrinsic_id
== ir_intrinsic_image_load
)
800 return visit_continue
;
802 /* If this is a call to a builtin and the find_lowerable_rvalues_visitor
803 * overrode the precision of the temporary return variable, then we can
804 * replace the builtin implementation with a lowered version.
807 if (!ir
->callee
->is_builtin() ||
808 return_var
== NULL
||
809 (return_var
->data
.precision
!= GLSL_PRECISION_MEDIUM
&&
810 return_var
->data
.precision
!= GLSL_PRECISION_LOW
))
811 return visit_continue
;
813 ir
->callee
= map_builtin(ir
->callee
);
814 ir
->generate_inline(ir
);
817 return visit_continue_with_parent
;
820 ir_function_signature
*
821 find_precision_visitor::map_builtin(ir_function_signature
*sig
)
823 if (lowered_builtins
== NULL
) {
824 lowered_builtins
= _mesa_pointer_hash_table_create(NULL
);
825 clone_ht
=_mesa_pointer_hash_table_create(NULL
);
826 lowered_builtin_mem_ctx
= ralloc_context(NULL
);
828 struct hash_entry
*entry
= _mesa_hash_table_search(lowered_builtins
, sig
);
830 return (ir_function_signature
*) entry
->data
;
833 ir_function_signature
*lowered_sig
=
834 sig
->clone(lowered_builtin_mem_ctx
, clone_ht
);
836 foreach_in_list(ir_variable
, param
, &lowered_sig
->parameters
) {
837 param
->data
.precision
= GLSL_PRECISION_MEDIUM
;
840 lower_precision(options
, &lowered_sig
->body
);
842 _mesa_hash_table_clear(clone_ht
, NULL
);
844 _mesa_hash_table_insert(lowered_builtins
, sig
, lowered_sig
);
849 find_precision_visitor::find_precision_visitor(const struct gl_shader_compiler_options
*options
)
851 lowerable_rvalues(_mesa_pointer_set_create(NULL
)),
852 lowered_builtins(NULL
),
854 lowered_builtin_mem_ctx(NULL
),
859 find_precision_visitor::~find_precision_visitor()
861 _mesa_set_destroy(lowerable_rvalues
, NULL
);
863 if (lowered_builtins
) {
864 _mesa_hash_table_destroy(lowered_builtins
, NULL
);
865 _mesa_hash_table_destroy(clone_ht
, NULL
);
866 ralloc_free(lowered_builtin_mem_ctx
);
873 lower_precision(const struct gl_shader_compiler_options
*options
,
874 exec_list
*instructions
)
876 find_precision_visitor
v(options
);
878 find_lowerable_rvalues(options
, instructions
, v
.lowerable_rvalues
);
880 visit_list_elements(&v
, instructions
);