OSDN Git Service

i965: Add texrect scale parameters before pointers to ParameterValues.
[android-x86/external-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 #include "brw_vs.h"
28 }
29 #include "brw_fs.h"
30 #include "glsl/ir_optimization.h"
31 #include "glsl/ir_print_visitor.h"
32
33 struct gl_shader *
34 brw_new_shader(struct gl_context *ctx, GLuint name, GLuint type)
35 {
36    struct brw_shader *shader;
37
38    shader = rzalloc(NULL, struct brw_shader);
39    if (shader) {
40       shader->base.Type = type;
41       shader->base.Name = name;
42       _mesa_init_shader(ctx, &shader->base);
43    }
44
45    return &shader->base;
46 }
47
48 struct gl_shader_program *
49 brw_new_shader_program(struct gl_context *ctx, GLuint name)
50 {
51    struct gl_shader_program *prog = rzalloc(NULL, struct gl_shader_program);
52    if (prog) {
53       prog->Name = name;
54       _mesa_init_shader_program(ctx, prog);
55    }
56    return prog;
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 static bool
65 brw_shader_precompile(struct gl_context *ctx, struct gl_shader_program *prog)
66 {
67    struct brw_context *brw = brw_context(ctx);
68
69    if (brw->precompile && !brw_fs_precompile(ctx, prog))
70       return false;
71
72    if (brw->precompile && !brw_vs_precompile(ctx, prog))
73       return false;
74
75    return true;
76 }
77
78 GLboolean
79 brw_link_shader(struct gl_context *ctx, struct gl_shader_program *shProg)
80 {
81    struct brw_context *brw = brw_context(ctx);
82    struct intel_context *intel = &brw->intel;
83    unsigned int stage;
84
85    for (stage = 0; stage < ARRAY_SIZE(shProg->_LinkedShaders); stage++) {
86       struct brw_shader *shader =
87          (struct brw_shader *)shProg->_LinkedShaders[stage];
88       static const GLenum targets[] = {
89          GL_VERTEX_PROGRAM_ARB,
90          GL_FRAGMENT_PROGRAM_ARB,
91          GL_GEOMETRY_PROGRAM_NV
92       };
93
94       if (!shader)
95          continue;
96
97       struct gl_program *prog =
98          ctx->Driver.NewProgram(ctx, targets[stage], shader->base.Name);
99       if (!prog)
100         return false;
101       prog->Parameters = _mesa_new_parameter_list();
102
103       _mesa_generate_parameters_list_for_uniforms(shProg, &shader->base,
104                                                   prog->Parameters);
105
106       if (stage == 0) {
107          struct gl_vertex_program *vp = (struct gl_vertex_program *) prog;
108          vp->UsesClipDistance = shProg->Vert.UsesClipDistance;
109       }
110
111       void *mem_ctx = ralloc_context(NULL);
112       bool progress;
113
114       if (shader->ir)
115          ralloc_free(shader->ir);
116       shader->ir = new(shader) exec_list;
117       clone_ir_list(mem_ctx, shader->ir, shader->base.ir);
118
119       do_mat_op_to_vec(shader->ir);
120       lower_instructions(shader->ir,
121                          MOD_TO_FRACT |
122                          DIV_TO_MUL_RCP |
123                          SUB_TO_ADD_NEG |
124                          EXP_TO_EXP2 |
125                          LOG_TO_LOG2);
126
127       /* Pre-gen6 HW can only nest if-statements 16 deep.  Beyond this,
128        * if-statements need to be flattened.
129        */
130       if (intel->gen < 6)
131          lower_if_to_cond_assign(shader->ir, 16);
132
133       do_lower_texture_projection(shader->ir);
134       brw_lower_texture_gradients(shader->ir);
135       do_vec_index_to_cond_assign(shader->ir);
136       brw_do_cubemap_normalize(shader->ir);
137       lower_noise(shader->ir);
138       lower_quadop_vector(shader->ir, false);
139
140       bool input = true;
141       bool output = stage == MESA_SHADER_FRAGMENT;
142       bool temp = stage == MESA_SHADER_FRAGMENT;
143       bool uniform = false;
144
145       bool lowered_variable_indexing =
146          lower_variable_index_to_cond_assign(shader->ir,
147                                              input, output, temp, uniform);
148
149       if (unlikely((INTEL_DEBUG & DEBUG_PERF) && lowered_variable_indexing)) {
150          perf_debug("Unsupported form of variable indexing in FS; falling "
151                     "back to very inefficient code generation\n");
152       }
153
154       /* FINISHME: Do this before the variable index lowering. */
155       lower_ubo_reference(&shader->base, shader->ir);
156
157       do {
158          progress = false;
159
160          if (stage == MESA_SHADER_FRAGMENT) {
161             brw_do_channel_expressions(shader->ir);
162             brw_do_vector_splitting(shader->ir);
163          }
164
165          progress = do_lower_jumps(shader->ir, true, true,
166                                    true, /* main return */
167                                    false, /* continue */
168                                    false /* loops */
169                                    ) || progress;
170
171          progress = do_common_optimization(shader->ir, true, true, 32)
172            || progress;
173       } while (progress);
174
175       /* Make a pass over the IR to add state references for any built-in
176        * uniforms that are used.  This has to be done now (during linking).
177        * Code generation doesn't happen until the first time this shader is
178        * used for rendering.  Waiting until then to generate the parameters is
179        * too late.  At that point, the values for the built-in uniforms won't
180        * get sent to the shader.
181        */
182       foreach_list(node, shader->ir) {
183          ir_variable *var = ((ir_instruction *) node)->as_variable();
184
185          if ((var == NULL) || (var->mode != ir_var_uniform)
186              || (strncmp(var->name, "gl_", 3) != 0))
187             continue;
188
189          const ir_state_slot *const slots = var->state_slots;
190          assert(var->state_slots != NULL);
191
192          for (unsigned int i = 0; i < var->num_state_slots; i++) {
193             _mesa_add_state_reference(prog->Parameters,
194                                       (gl_state_index *) slots[i].tokens);
195          }
196       }
197
198       validate_ir_tree(shader->ir);
199
200       reparent_ir(shader->ir, shader->ir);
201       ralloc_free(mem_ctx);
202
203       do_set_program_inouts(shader->ir, prog,
204                             shader->base.Type == GL_FRAGMENT_SHADER);
205
206       prog->SamplersUsed = shader->base.active_samplers;
207       _mesa_update_shader_textures_used(shProg, prog);
208
209       _mesa_reference_program(ctx, &shader->base.Program, prog);
210
211       brw_add_texrect_params(prog);
212
213       /* This has to be done last.  Any operation that can cause
214        * prog->ParameterValues to get reallocated (e.g., anything that adds a
215        * program constant) has to happen before creating this linkage.
216        */
217       _mesa_associate_uniform_storage(ctx, shProg, prog->Parameters);
218
219       _mesa_reference_program(ctx, &prog, NULL);
220
221       if (ctx->Shader.Flags & GLSL_DUMP) {
222          static const char *target_strings[]
223             = { "vertex", "fragment", "geometry" };
224          printf("\n");
225          printf("GLSL IR for linked %s program %d:\n", target_strings[stage],
226                 shProg->Name);
227          _mesa_print_ir(shader->base.ir, NULL);
228       }
229    }
230
231    if (!brw_shader_precompile(ctx, shProg))
232       return false;
233
234    return true;
235 }
236
237
238 int
239 brw_type_for_base_type(const struct glsl_type *type)
240 {
241    switch (type->base_type) {
242    case GLSL_TYPE_FLOAT:
243       return BRW_REGISTER_TYPE_F;
244    case GLSL_TYPE_INT:
245    case GLSL_TYPE_BOOL:
246       return BRW_REGISTER_TYPE_D;
247    case GLSL_TYPE_UINT:
248       return BRW_REGISTER_TYPE_UD;
249    case GLSL_TYPE_ARRAY:
250       return brw_type_for_base_type(type->fields.array);
251    case GLSL_TYPE_STRUCT:
252    case GLSL_TYPE_SAMPLER:
253       /* These should be overridden with the type of the member when
254        * dereferenced into.  BRW_REGISTER_TYPE_UD seems like a likely
255        * way to trip up if we don't.
256        */
257       return BRW_REGISTER_TYPE_UD;
258    default:
259       assert(!"not reached");
260       return BRW_REGISTER_TYPE_F;
261    }
262 }
263
264 uint32_t
265 brw_conditional_for_comparison(unsigned int op)
266 {
267    switch (op) {
268    case ir_binop_less:
269       return BRW_CONDITIONAL_L;
270    case ir_binop_greater:
271       return BRW_CONDITIONAL_G;
272    case ir_binop_lequal:
273       return BRW_CONDITIONAL_LE;
274    case ir_binop_gequal:
275       return BRW_CONDITIONAL_GE;
276    case ir_binop_equal:
277    case ir_binop_all_equal: /* same as equal for scalars */
278       return BRW_CONDITIONAL_Z;
279    case ir_binop_nequal:
280    case ir_binop_any_nequal: /* same as nequal for scalars */
281       return BRW_CONDITIONAL_NZ;
282    default:
283       assert(!"not reached: bad operation for comparison");
284       return BRW_CONDITIONAL_NZ;
285    }
286 }
287
288 uint32_t
289 brw_math_function(enum opcode op)
290 {
291    switch (op) {
292    case SHADER_OPCODE_RCP:
293       return BRW_MATH_FUNCTION_INV;
294    case SHADER_OPCODE_RSQ:
295       return BRW_MATH_FUNCTION_RSQ;
296    case SHADER_OPCODE_SQRT:
297       return BRW_MATH_FUNCTION_SQRT;
298    case SHADER_OPCODE_EXP2:
299       return BRW_MATH_FUNCTION_EXP;
300    case SHADER_OPCODE_LOG2:
301       return BRW_MATH_FUNCTION_LOG;
302    case SHADER_OPCODE_POW:
303       return BRW_MATH_FUNCTION_POW;
304    case SHADER_OPCODE_SIN:
305       return BRW_MATH_FUNCTION_SIN;
306    case SHADER_OPCODE_COS:
307       return BRW_MATH_FUNCTION_COS;
308    case SHADER_OPCODE_INT_QUOTIENT:
309       return BRW_MATH_FUNCTION_INT_DIV_QUOTIENT;
310    case SHADER_OPCODE_INT_REMAINDER:
311       return BRW_MATH_FUNCTION_INT_DIV_REMAINDER;
312    default:
313       assert(!"not reached: unknown math function");
314       return 0;
315    }
316 }
317
318 uint32_t
319 brw_texture_offset(ir_constant *offset)
320 {
321    assert(offset != NULL);
322
323    signed char offsets[3];
324    for (unsigned i = 0; i < offset->type->vector_elements; i++)
325       offsets[i] = (signed char) offset->value.i[i];
326
327    /* Combine all three offsets into a single unsigned dword:
328     *
329     *    bits 11:8 - U Offset (X component)
330     *    bits  7:4 - V Offset (Y component)
331     *    bits  3:0 - R Offset (Z component)
332     */
333    unsigned offset_bits = 0;
334    for (unsigned i = 0; i < offset->type->vector_elements; i++) {
335       const unsigned shift = 4 * (2 - i);
336       offset_bits |= (offsets[i] << shift) & (0xF << shift);
337    }
338    return offset_bits;
339 }