i965: Start adding the VS visitor and codegen.
[mesa.git] / src / mesa / drivers / dri / i965 / brw_shader.cpp
1 /*
2 * Copyright © 2010 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 extern "C" {
25 #include "main/macros.h"
26 #include "brw_context.h"
27 }
28 #include "brw_fs.h"
29 #include "../glsl/ir_optimization.h"
30 #include "../glsl/ir_print_visitor.h"
31
32 struct gl_shader *
33 brw_new_shader(struct gl_context *ctx, GLuint name, GLuint type)
34 {
35 struct brw_shader *shader;
36
37 shader = rzalloc(NULL, struct brw_shader);
38 if (shader) {
39 shader->base.Type = type;
40 shader->base.Name = name;
41 _mesa_init_shader(ctx, &shader->base);
42 }
43
44 return &shader->base;
45 }
46
47 struct gl_shader_program *
48 brw_new_shader_program(struct gl_context *ctx, GLuint name)
49 {
50 struct brw_shader_program *prog;
51 prog = rzalloc(NULL, struct brw_shader_program);
52 if (prog) {
53 prog->base.Name = name;
54 _mesa_init_shader_program(ctx, &prog->base);
55 }
56 return &prog->base;
57 }
58
59 /**
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.
63 */
64 bool
65 brw_shader_precompile(struct gl_context *ctx, struct gl_shader_program *prog)
66 {
67 if (!brw_fs_precompile(ctx, prog))
68 return false;
69
70 return true;
71 }
72
73 GLboolean
74 brw_link_shader(struct gl_context *ctx, struct gl_shader_program *prog)
75 {
76 struct brw_context *brw = brw_context(ctx);
77 struct intel_context *intel = &brw->intel;
78 unsigned int stage;
79
80 for (stage = 0; stage < ARRAY_SIZE(prog->_LinkedShaders); stage++) {
81 struct brw_shader *shader =
82 (struct brw_shader *)prog->_LinkedShaders[stage];
83
84 if (!shader)
85 continue;
86
87 void *mem_ctx = ralloc_context(NULL);
88 bool progress;
89
90 if (shader->ir)
91 ralloc_free(shader->ir);
92 shader->ir = new(shader) exec_list;
93 clone_ir_list(mem_ctx, shader->ir, shader->base.ir);
94
95 do_mat_op_to_vec(shader->ir);
96 lower_instructions(shader->ir,
97 MOD_TO_FRACT |
98 DIV_TO_MUL_RCP |
99 SUB_TO_ADD_NEG |
100 EXP_TO_EXP2 |
101 LOG_TO_LOG2);
102
103 /* Pre-gen6 HW can only nest if-statements 16 deep. Beyond this,
104 * if-statements need to be flattened.
105 */
106 if (intel->gen < 6)
107 lower_if_to_cond_assign(shader->ir, 16);
108
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,
115 GL_TRUE, /* input */
116 GL_TRUE, /* output */
117 GL_TRUE, /* temp */
118 GL_TRUE /* uniform */
119 );
120
121 do {
122 progress = false;
123
124 if (stage == MESA_SHADER_FRAGMENT) {
125 brw_do_channel_expressions(shader->ir);
126 brw_do_vector_splitting(shader->ir);
127 }
128
129 progress = do_lower_jumps(shader->ir, true, true,
130 true, /* main return */
131 false, /* continue */
132 false /* loops */
133 ) || progress;
134
135 progress = do_common_optimization(shader->ir, true, 32) || progress;
136 } while (progress);
137
138 validate_ir_tree(shader->ir);
139
140 reparent_ir(shader->ir, shader->ir);
141 ralloc_free(mem_ctx);
142 }
143
144 if (!_mesa_ir_link_shader(ctx, prog))
145 return GL_FALSE;
146
147 if (!brw_shader_precompile(ctx, prog))
148 return GL_FALSE;
149
150 return GL_TRUE;
151 }
152
153
154 int
155 brw_type_for_base_type(const struct glsl_type *type)
156 {
157 switch (type->base_type) {
158 case GLSL_TYPE_FLOAT:
159 return BRW_REGISTER_TYPE_F;
160 case GLSL_TYPE_INT:
161 case GLSL_TYPE_BOOL:
162 return BRW_REGISTER_TYPE_D;
163 case GLSL_TYPE_UINT:
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.
171 */
172 return BRW_REGISTER_TYPE_UD;
173 default:
174 assert(!"not reached");
175 return BRW_REGISTER_TYPE_F;
176 }
177 }
178
179 uint32_t
180 brw_conditional_for_comparison(unsigned int op)
181 {
182 switch (op) {
183 case ir_binop_less:
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;
191 case ir_binop_equal:
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;
197 default:
198 assert(!"not reached: bad operation for comparison");
199 return BRW_CONDITIONAL_NZ;
200 }
201 }
202
203 uint32_t
204 brw_math_function(enum opcode op)
205 {
206 switch (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;
223 default:
224 assert(!"not reached: unknown math function");
225 return 0;
226 }
227 }