OLD | NEW |
(Empty) | |
| 1 Port of the following to mesa 7.9: |
| 2 |
| 3 From 940df10100d740ef27fa39026fd51c3199ed3d62 Mon Sep 17 00:00:00 2001 |
| 4 From: Kenneth Graunke <kenneth@whitecape.org> |
| 5 Date: Thu, 25 Nov 2010 09:09:26 +0000 |
| 6 Subject: glsl: Add a lowering pass to move discards out of if-statements. |
| 7 |
| 8 This should allow lower_if_to_cond_assign to work in the presence of |
| 9 discards, fixing bug #31690 and likely #31983. |
| 10 |
| 11 NOTE: This is a candidate for the 7.9 branch. |
| 12 --- |
| 13 diff -Naurp Mesa-7.9.orig/src/glsl/Makefile Mesa-7.9.work/src/glsl/Makefile |
| 14 --- Mesa-7.9.orig/src/glsl/Makefile 2010-12-07 18:22:17.511752000 -0800 |
| 15 +++ Mesa-7.9.work/src/glsl/Makefile 2010-12-10 16:15:41.026229000 -0800 |
| 16 @@ -75,6 +75,7 @@ CXX_SOURCES = \ |
| 17 loop_analysis.cpp \ |
| 18 loop_controls.cpp \ |
| 19 loop_unroll.cpp \ |
| 20 + lower_discard.cpp \ |
| 21 lower_noise.cpp \ |
| 22 lower_variable_index_to_cond_assign.cpp \ |
| 23 opt_redundant_jumps.cpp \ |
| 24 diff -Naurp Mesa-7.9.orig/src/glsl/SConscript Mesa-7.9.work/src/glsl/SConscript |
| 25 --- Mesa-7.9.orig/src/glsl/SConscript 2010-12-07 18:22:17.518757000 -0800 |
| 26 +++ Mesa-7.9.work/src/glsl/SConscript 2010-12-10 16:16:28.878294000 -0800 |
| 27 @@ -72,6 +72,7 @@ sources = [ |
| 28 'loop_analysis.cpp', |
| 29 'loop_controls.cpp', |
| 30 'loop_unroll.cpp', |
| 31 + 'lower_discard.cpp', |
| 32 'lower_noise.cpp', |
| 33 'lower_variable_index_to_cond_assign.cpp', |
| 34 'opt_redundant_jumps.cpp', |
| 35 diff -Naurp Mesa-7.9.orig/src/glsl/ir_optimization.h Mesa-7.9.work/src/glsl/ir_o
ptimization.h |
| 36 --- Mesa-7.9.orig/src/glsl/ir_optimization.h 2010-12-07 18:22:17.556757000 -0
800 |
| 37 +++ Mesa-7.9.work/src/glsl/ir_optimization.h 2010-12-10 16:34:56.074610000 -0
800 |
| 38 @@ -55,6 +55,7 @@ bool do_swizzle_swizzle(exec_list *instr |
| 39 bool do_tree_grafting(exec_list *instructions); |
| 40 bool do_vec_index_to_cond_assign(exec_list *instructions); |
| 41 bool do_vec_index_to_swizzle(exec_list *instructions); |
| 42 +bool lower_discard(exec_list *instructions); |
| 43 bool lower_noise(exec_list *instructions); |
| 44 bool lower_variable_index_to_cond_assign(exec_list *instructions, |
| 45 bool lower_input, bool lower_output, bool lower_temp, bool lower_uniform); |
| 46 diff -Naurp Mesa-7.9.orig/src/glsl/lower_discard.cpp Mesa-7.9.work/src/glsl/lowe
r_discard.cpp |
| 47 --- Mesa-7.9.orig/src/glsl/lower_discard.cpp 1969-12-31 16:00:00.000000000 -0
800 |
| 48 +++ Mesa-7.9.work/src/glsl/lower_discard.cpp 2010-12-10 16:15:16.494214000 -0
800 |
| 49 @@ -0,0 +1,198 @@ |
| 50 +/* |
| 51 + * Copyright © 2010 Intel Corporation |
| 52 + * |
| 53 + * Permission is hereby granted, free of charge, to any person obtaining a |
| 54 + * copy of this software and associated documentation files (the "Software"), |
| 55 + * to deal in the Software without restriction, including without limitation |
| 56 + * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 57 + * and/or sell copies of the Software, and to permit persons to whom the |
| 58 + * Software is furnished to do so, subject to the following conditions: |
| 59 + * |
| 60 + * The above copyright notice and this permission notice (including the next |
| 61 + * paragraph) shall be included in all copies or substantial portions of the |
| 62 + * Software. |
| 63 + * |
| 64 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 65 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 66 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 67 + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 68 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 69 + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 70 + * DEALINGS IN THE SOFTWARE. |
| 71 + */ |
| 72 + |
| 73 +/** |
| 74 + * \file lower_discard.cpp |
| 75 + * |
| 76 + * This pass moves discards out of if-statements. |
| 77 + * |
| 78 + * Case 1: The "then" branch contains a conditional discard: |
| 79 + * --------------------------------------------------------- |
| 80 + * |
| 81 + * if (cond1) { |
| 82 + * s1; |
| 83 + * discard cond2; |
| 84 + * s2; |
| 85 + * } else { |
| 86 + * s3; |
| 87 + * } |
| 88 + * |
| 89 + * becomes: |
| 90 + * |
| 91 + * temp = false; |
| 92 + * if (cond1) { |
| 93 + * s1; |
| 94 + * temp = cond2; |
| 95 + * s2; |
| 96 + * } else { |
| 97 + * s3; |
| 98 + * } |
| 99 + * discard temp; |
| 100 + * |
| 101 + * Case 2: The "else" branch contains a conditional discard: |
| 102 + * --------------------------------------------------------- |
| 103 + * |
| 104 + * if (cond1) { |
| 105 + * s1; |
| 106 + * } else { |
| 107 + * s2; |
| 108 + * discard cond2; |
| 109 + * s3; |
| 110 + * } |
| 111 + * |
| 112 + * becomes: |
| 113 + * |
| 114 + * temp = false; |
| 115 + * if (cond1) { |
| 116 + * s1; |
| 117 + * } else { |
| 118 + * s2; |
| 119 + * temp = cond2; |
| 120 + * s3; |
| 121 + * } |
| 122 + * discard temp; |
| 123 + * |
| 124 + * Case 3: Both branches contain a conditional discard: |
| 125 + * ---------------------------------------------------- |
| 126 + * |
| 127 + * if (cond1) { |
| 128 + * s1; |
| 129 + * discard cond2; |
| 130 + * s2; |
| 131 + * } else { |
| 132 + * s3; |
| 133 + * discard cond3; |
| 134 + * s4; |
| 135 + * } |
| 136 + * |
| 137 + * becomes: |
| 138 + * |
| 139 + * temp = false; |
| 140 + * if (cond1) { |
| 141 + * s1; |
| 142 + * temp = cond2; |
| 143 + * s2; |
| 144 + * } else { |
| 145 + * s3; |
| 146 + * temp = cond3; |
| 147 + * s4; |
| 148 + * } |
| 149 + * discard temp; |
| 150 + * |
| 151 + * If there are multiple conditional discards, we need only deal with one of |
| 152 + * them. Repeatedly applying this pass will take care of the others. |
| 153 + * |
| 154 + * Unconditional discards are treated as having a condition of "true". |
| 155 + */ |
| 156 + |
| 157 +#include "glsl_types.h" |
| 158 +#include "ir.h" |
| 159 + |
| 160 +class lower_discard_visitor : public ir_hierarchical_visitor { |
| 161 +public: |
| 162 + lower_discard_visitor() |
| 163 + { |
| 164 + this->progress = false; |
| 165 + } |
| 166 + |
| 167 + ir_visitor_status visit_leave(ir_if *); |
| 168 + |
| 169 + bool progress; |
| 170 +}; |
| 171 + |
| 172 + |
| 173 +bool |
| 174 +lower_discard(exec_list *instructions) |
| 175 +{ |
| 176 + lower_discard_visitor v; |
| 177 + |
| 178 + visit_list_elements(&v, instructions); |
| 179 + |
| 180 + return v.progress; |
| 181 +} |
| 182 + |
| 183 + |
| 184 +static ir_discard * |
| 185 +find_discard(exec_list &instructions) |
| 186 +{ |
| 187 + foreach_list(n, &instructions) { |
| 188 + ir_discard *ir = ((ir_instruction *) n)->as_discard(); |
| 189 + if (ir != NULL) |
| 190 + return ir; |
| 191 + } |
| 192 + return NULL; |
| 193 +} |
| 194 + |
| 195 + |
| 196 +static void |
| 197 +replace_discard(void *mem_ctx, ir_variable *var, ir_discard *ir) |
| 198 +{ |
| 199 + ir_rvalue *condition = ir->condition; |
| 200 + |
| 201 + /* For unconditional discards, use "true" as the condition. */ |
| 202 + if (condition == NULL) |
| 203 + condition = new(mem_ctx) ir_constant(true); |
| 204 + |
| 205 + ir_assignment *assignment = |
| 206 + new(mem_ctx) ir_assignment(new(mem_ctx) ir_dereference_variable(var), |
| 207 + condition, NULL); |
| 208 + |
| 209 + ir->replace_with(assignment); |
| 210 +} |
| 211 + |
| 212 + |
| 213 +ir_visitor_status |
| 214 +lower_discard_visitor::visit_leave(ir_if *ir) |
| 215 +{ |
| 216 + ir_discard *then_discard = find_discard(ir->then_instructions); |
| 217 + ir_discard *else_discard = find_discard(ir->else_instructions); |
| 218 + |
| 219 + if (then_discard == NULL && else_discard == NULL) |
| 220 + return visit_continue; |
| 221 + |
| 222 + void *mem_ctx = talloc_parent(ir); |
| 223 + |
| 224 + ir_variable *temp = new(mem_ctx) ir_variable(glsl_type::bool_type, |
| 225 + "discard_cond_temp", |
| 226 + ir_var_temporary); |
| 227 + ir_assignment *temp_initializer = |
| 228 + new(mem_ctx) ir_assignment(new(mem_ctx) ir_dereference_variable(temp), |
| 229 + new(mem_ctx) ir_constant(false), NULL); |
| 230 + |
| 231 + ir->insert_before(temp); |
| 232 + ir->insert_before(temp_initializer); |
| 233 + |
| 234 + if (then_discard != NULL) |
| 235 + replace_discard(mem_ctx, temp, then_discard); |
| 236 + |
| 237 + if (else_discard != NULL) |
| 238 + replace_discard(mem_ctx, temp, else_discard); |
| 239 + |
| 240 + ir_discard *discard = then_discard != NULL ? then_discard : else_discard; |
| 241 + discard->condition = new(mem_ctx) ir_dereference_variable(temp); |
| 242 + ir->insert_after(discard); |
| 243 + |
| 244 + this->progress = true; |
| 245 + |
| 246 + return visit_continue; |
| 247 +} |
| 248 diff -Naurp Mesa-7.9.orig/src/mesa/program/ir_to_mesa.cpp Mesa-7.9.work/src/mesa
/program/ir_to_mesa.cpp |
| 249 --- Mesa-7.9.orig/src/mesa/program/ir_to_mesa.cpp 2010-12-07 18:22:19.9545
89000 -0800 |
| 250 +++ Mesa-7.9.work/src/mesa/program/ir_to_mesa.cpp 2010-12-10 16:15:16.5552
05000 -0800 |
| 251 @@ -2780,8 +2780,10 @@ _mesa_ir_link_shader(GLcontext *ctx, str |
| 252 |
| 253 progress = do_common_optimization(ir, true, options->MaxUnrollIteration
s) || progress; |
| 254 |
| 255 - if (options->EmitNoIfs) |
| 256 + if (options->EmitNoIfs) { |
| 257 + progress = lower_discard(ir) || progress; |
| 258 progress = do_if_to_cond_assign(ir) || progress; |
| 259 + } |
| 260 |
| 261 if (options->EmitNoNoise) |
| 262 progress = lower_noise(ir) || progress; |
OLD | NEW |