1 /* 2 * Copyright © 2012 Intel Corporation 3 * 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: 10 * 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 13 * Software. 14 * 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 DEALINGS 21 * IN THE SOFTWARE. 22 */ 23 24 #ifndef IR_BUILDER_H 25 #define IR_BUILDER_H 26 27 #include "ir.h" 28 29 namespace ir_builder { 30 31 /** 32 * This little class exists to let the helper expression generators 33 * take either an ir_rvalue * or an ir_variable * to be automatically 34 * dereferenced, while still providing compile-time type checking. 35 * 36 * You don't have to explicitly call the constructor -- C++ will see 37 * that you passed an ir_variable, and silently call the 38 * operand(ir_variable *var) constructor behind your back. 39 */ 40 class operand { 41 public: operand(ir_rvalue * val)42 operand(ir_rvalue *val) 43 : val(val) 44 { 45 } 46 operand(ir_variable * var)47 operand(ir_variable *var) 48 { 49 void *mem_ctx = ralloc_parent(var); 50 val = new(mem_ctx) ir_dereference_variable(var); 51 } 52 53 ir_rvalue *val; 54 }; 55 56 /** Automatic generator for ir_dereference_variable on assignment LHS. 57 * 58 * \sa operand 59 */ 60 class deref { 61 public: deref(ir_dereference * val)62 deref(ir_dereference *val) 63 : val(val) 64 { 65 } 66 deref(ir_variable * var)67 deref(ir_variable *var) 68 { 69 void *mem_ctx = ralloc_parent(var); 70 val = new(mem_ctx) ir_dereference_variable(var); 71 } 72 73 74 ir_dereference *val; 75 }; 76 77 class ir_factory { 78 public: 79 ir_factory(exec_list *instructions = NULL, void *mem_ctx = NULL) instructions(instructions)80 : instructions(instructions), 81 mem_ctx(mem_ctx) 82 { 83 return; 84 } 85 86 void emit(ir_instruction *ir); 87 ir_variable *make_temp(const glsl_type *type, const char *name); 88 89 ir_constant* constant(float f)90 constant(float f) 91 { 92 return new(mem_ctx) ir_constant(f); 93 } 94 95 ir_constant* constant(int i)96 constant(int i) 97 { 98 return new(mem_ctx) ir_constant(i); 99 } 100 101 ir_constant* constant(unsigned u)102 constant(unsigned u) 103 { 104 return new(mem_ctx) ir_constant(u); 105 } 106 107 ir_constant* constant(bool b)108 constant(bool b) 109 { 110 return new(mem_ctx) ir_constant(b); 111 } 112 113 exec_list *instructions; 114 void *mem_ctx; 115 }; 116 117 ir_assignment *assign(deref lhs, operand rhs); 118 ir_assignment *assign(deref lhs, operand rhs, int writemask); 119 120 ir_return *ret(operand retval); 121 122 ir_expression *expr(ir_expression_operation op, operand a); 123 ir_expression *expr(ir_expression_operation op, operand a, operand b); 124 ir_expression *expr(ir_expression_operation op, operand a, operand b, operand c); 125 ir_expression *add(operand a, operand b); 126 ir_expression *sub(operand a, operand b); 127 ir_expression *mul(operand a, operand b); 128 ir_expression *imul_high(operand a, operand b); 129 ir_expression *div(operand a, operand b); 130 ir_expression *carry(operand a, operand b); 131 ir_expression *borrow(operand a, operand b); 132 ir_expression *trunc(operand a); 133 ir_expression *round_even(operand a); 134 ir_expression *fract(operand a); 135 ir_expression *dot(operand a, operand b); 136 ir_expression *clamp(operand a, operand b, operand c); 137 ir_expression *saturate(operand a); 138 ir_expression *abs(operand a); 139 ir_expression *neg(operand a); 140 ir_expression *sin(operand a); 141 ir_expression *cos(operand a); 142 ir_expression *exp(operand a); 143 ir_expression *rcp(operand a); 144 ir_expression *rsq(operand a); 145 ir_expression *sqrt(operand a); 146 ir_expression *log(operand a); 147 ir_expression *sign(operand a); 148 149 ir_expression *subr_to_int(operand a); 150 ir_expression *equal(operand a, operand b); 151 ir_expression *nequal(operand a, operand b); 152 ir_expression *less(operand a, operand b); 153 ir_expression *greater(operand a, operand b); 154 ir_expression *lequal(operand a, operand b); 155 ir_expression *gequal(operand a, operand b); 156 157 ir_expression *logic_not(operand a); 158 ir_expression *logic_and(operand a, operand b); 159 ir_expression *logic_or(operand a, operand b); 160 161 ir_expression *bit_not(operand a); 162 ir_expression *bit_or(operand a, operand b); 163 ir_expression *bit_and(operand a, operand b); 164 ir_expression *bit_xor(operand a, operand b); 165 ir_expression *lshift(operand a, operand b); 166 ir_expression *rshift(operand a, operand b); 167 168 ir_expression *f2i(operand a); 169 ir_expression *bitcast_f2i(operand a); 170 ir_expression *i2f(operand a); 171 ir_expression *bitcast_i2f(operand a); 172 ir_expression *f2u(operand a); 173 ir_expression *bitcast_f2u(operand a); 174 ir_expression *u2f(operand a); 175 ir_expression *bitcast_u2f(operand a); 176 ir_expression *i2u(operand a); 177 ir_expression *u2i(operand a); 178 ir_expression *b2i(operand a); 179 ir_expression *i2b(operand a); 180 ir_expression *f2b(operand a); 181 ir_expression *b2f(operand a); 182 183 ir_expression *f2f16(operand a); 184 185 ir_expression *f2d(operand a); 186 ir_expression *i2d(operand a); 187 ir_expression *u2d(operand a); 188 189 ir_expression *bitcast_d2i64(operand a); 190 ir_expression *bitcast_d2u64(operand a); 191 192 ir_expression *bitcast_i642d(operand a); 193 ir_expression *bitcast_u642d(operand a); 194 195 ir_expression *min2(operand a, operand b); 196 ir_expression *max2(operand a, operand b); 197 198 ir_expression *interpolate_at_centroid(operand a); 199 ir_expression *interpolate_at_offset(operand a, operand b); 200 ir_expression *interpolate_at_sample(operand a, operand b); 201 202 ir_expression *fma(operand a, operand b, operand c); 203 ir_expression *lrp(operand x, operand y, operand a); 204 ir_expression *csel(operand a, operand b, operand c); 205 ir_expression *bitfield_extract(operand a, operand b, operand c); 206 ir_expression *bitfield_insert(operand a, operand b, operand c, operand d); 207 208 ir_swizzle *swizzle(operand a, int swizzle, int components); 209 /** 210 * Swizzle away later components, but preserve the ordering. 211 */ 212 ir_swizzle *swizzle_for_size(operand a, unsigned components); 213 214 ir_swizzle *swizzle_xxxx(operand a); 215 ir_swizzle *swizzle_yyyy(operand a); 216 ir_swizzle *swizzle_zzzz(operand a); 217 ir_swizzle *swizzle_wwww(operand a); 218 ir_swizzle *swizzle_x(operand a); 219 ir_swizzle *swizzle_y(operand a); 220 ir_swizzle *swizzle_z(operand a); 221 ir_swizzle *swizzle_w(operand a); 222 ir_swizzle *swizzle_xy(operand a); 223 ir_swizzle *swizzle_xyz(operand a); 224 ir_swizzle *swizzle_xyzw(operand a); 225 226 ir_if *if_tree(operand condition, 227 ir_instruction *then_branch); 228 ir_if *if_tree(operand condition, 229 ir_instruction *then_branch, 230 ir_instruction *else_branch); 231 232 } /* namespace ir_builder */ 233 234 #endif 235