2 * Copyright © 2010 Intel Corporation
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 DEALINGS
25 #include "main/macros.h"
26 #include "brw_context.h"
29 #include "../glsl/ir_optimization.h"
30 #include "../glsl/ir_print_visitor.h"
33 brw_new_shader(struct gl_context *ctx, GLuint name, GLuint type)
35 struct brw_shader *shader;
37 shader = rzalloc(NULL, struct brw_shader);
39 shader->base.Type = type;
40 shader->base.Name = name;
41 _mesa_init_shader(ctx, &shader->base);
47 struct gl_shader_program *
48 brw_new_shader_program(struct gl_context *ctx, GLuint name)
50 struct brw_shader_program *prog;
51 prog = rzalloc(NULL, struct brw_shader_program);
53 prog->base.Name = name;
54 _mesa_init_shader_program(ctx, &prog->base);
60 * Performs a compile of the shader stages even when we don't know
61 * what non-orthogonal state will be set, in the hope that it reflects
62 * the eventual NOS used, and thus allows us to produce link failures.
65 brw_shader_precompile(struct gl_context *ctx, struct gl_shader_program *prog)
67 if (!brw_fs_precompile(ctx, prog))
74 brw_link_shader(struct gl_context *ctx, struct gl_shader_program *prog)
76 struct brw_context *brw = brw_context(ctx);
77 struct intel_context *intel = &brw->intel;
80 for (stage = 0; stage < ARRAY_SIZE(prog->_LinkedShaders); stage++) {
81 struct brw_shader *shader =
82 (struct brw_shader *)prog->_LinkedShaders[stage];
87 void *mem_ctx = ralloc_context(NULL);
91 ralloc_free(shader->ir);
92 shader->ir = new(shader) exec_list;
93 clone_ir_list(mem_ctx, shader->ir, shader->base.ir);
95 do_mat_op_to_vec(shader->ir);
96 lower_instructions(shader->ir,
103 /* Pre-gen6 HW can only nest if-statements 16 deep. Beyond this,
104 * if-statements need to be flattened.
107 lower_if_to_cond_assign(shader->ir, 16);
109 do_lower_texture_projection(shader->ir);
110 do_vec_index_to_cond_assign(shader->ir);
111 brw_do_cubemap_normalize(shader->ir);
112 lower_noise(shader->ir);
113 lower_quadop_vector(shader->ir, false);
114 lower_variable_index_to_cond_assign(shader->ir,
116 GL_TRUE, /* output */
118 GL_TRUE /* uniform */
124 if (stage == MESA_SHADER_FRAGMENT) {
125 brw_do_channel_expressions(shader->ir);
126 brw_do_vector_splitting(shader->ir);
129 progress = do_lower_jumps(shader->ir, true, true,
130 true, /* main return */
131 false, /* continue */
135 progress = do_common_optimization(shader->ir, true, 32) || progress;
138 validate_ir_tree(shader->ir);
140 reparent_ir(shader->ir, shader->ir);
141 ralloc_free(mem_ctx);
144 if (!_mesa_ir_link_shader(ctx, prog))
147 if (!brw_shader_precompile(ctx, prog))
155 brw_type_for_base_type(const struct glsl_type *type)
157 switch (type->base_type) {
158 case GLSL_TYPE_FLOAT:
159 return BRW_REGISTER_TYPE_F;
162 return BRW_REGISTER_TYPE_D;
164 return BRW_REGISTER_TYPE_UD;
165 case GLSL_TYPE_ARRAY:
166 case GLSL_TYPE_STRUCT:
167 case GLSL_TYPE_SAMPLER:
168 /* These should be overridden with the type of the member when
169 * dereferenced into. BRW_REGISTER_TYPE_UD seems like a likely
170 * way to trip up if we don't.
172 return BRW_REGISTER_TYPE_UD;
174 assert(!"not reached");
175 return BRW_REGISTER_TYPE_F;
180 brw_conditional_for_comparison(unsigned int op)
184 return BRW_CONDITIONAL_L;
185 case ir_binop_greater:
186 return BRW_CONDITIONAL_G;
187 case ir_binop_lequal:
188 return BRW_CONDITIONAL_LE;
189 case ir_binop_gequal:
190 return BRW_CONDITIONAL_GE;
192 case ir_binop_all_equal: /* same as equal for scalars */
193 return BRW_CONDITIONAL_Z;
194 case ir_binop_nequal:
195 case ir_binop_any_nequal: /* same as nequal for scalars */
196 return BRW_CONDITIONAL_NZ;
198 assert(!"not reached: bad operation for comparison");
199 return BRW_CONDITIONAL_NZ;
204 brw_math_function(enum opcode op)
207 case SHADER_OPCODE_RCP:
208 return BRW_MATH_FUNCTION_INV;
209 case SHADER_OPCODE_RSQ:
210 return BRW_MATH_FUNCTION_RSQ;
211 case SHADER_OPCODE_SQRT:
212 return BRW_MATH_FUNCTION_SQRT;
213 case SHADER_OPCODE_EXP2:
214 return BRW_MATH_FUNCTION_EXP;
215 case SHADER_OPCODE_LOG2:
216 return BRW_MATH_FUNCTION_LOG;
217 case SHADER_OPCODE_POW:
218 return BRW_MATH_FUNCTION_POW;
219 case SHADER_OPCODE_SIN:
220 return BRW_MATH_FUNCTION_SIN;
221 case SHADER_OPCODE_COS:
222 return BRW_MATH_FUNCTION_COS;
224 assert(!"not reached: unknown math function");