Tizen 2.0 Release
[profile/ivi/osmesa.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
79    struct brw_shader *shader =
80       (struct brw_shader *)prog->_LinkedShaders[MESA_SHADER_FRAGMENT];
81    if (shader != NULL) {
82       void *mem_ctx = ralloc_context(NULL);
83       bool progress;
84
85       if (shader->ir)
86          ralloc_free(shader->ir);
87       shader->ir = new(shader) exec_list;
88       clone_ir_list(mem_ctx, shader->ir, shader->base.ir);
89
90       do_mat_op_to_vec(shader->ir);
91       lower_instructions(shader->ir,
92                          MOD_TO_FRACT |
93                          DIV_TO_MUL_RCP |
94                          SUB_TO_ADD_NEG |
95                          EXP_TO_EXP2 |
96                          LOG_TO_LOG2);
97
98       /* Pre-gen6 HW can only nest if-statements 16 deep.  Beyond this,
99        * if-statements need to be flattened.
100        */
101       if (intel->gen < 6)
102          lower_if_to_cond_assign(shader->ir, 16);
103
104       do_lower_texture_projection(shader->ir);
105       do_vec_index_to_cond_assign(shader->ir);
106       brw_do_cubemap_normalize(shader->ir);
107       lower_noise(shader->ir);
108       lower_quadop_vector(shader->ir, false);
109       lower_variable_index_to_cond_assign(shader->ir,
110                                           GL_TRUE, /* input */
111                                           GL_TRUE, /* output */
112                                           GL_TRUE, /* temp */
113                                           GL_TRUE /* uniform */
114                                           );
115
116       do {
117          progress = false;
118
119          brw_do_channel_expressions(shader->ir);
120          brw_do_vector_splitting(shader->ir);
121
122          progress = do_lower_jumps(shader->ir, true, true,
123                                    true, /* main return */
124                                    false, /* continue */
125                                    false /* loops */
126                                    ) || progress;
127
128          progress = do_common_optimization(shader->ir, true, 32) || progress;
129       } while (progress);
130
131       validate_ir_tree(shader->ir);
132
133       reparent_ir(shader->ir, shader->ir);
134       ralloc_free(mem_ctx);
135    }
136
137    if (!_mesa_ir_link_shader(ctx, prog))
138       return GL_FALSE;
139
140    if (!brw_shader_precompile(ctx, prog))
141       return GL_FALSE;
142
143    return GL_TRUE;
144 }
145
146
147 int
148 brw_type_for_base_type(const struct glsl_type *type)
149 {
150    switch (type->base_type) {
151    case GLSL_TYPE_FLOAT:
152       return BRW_REGISTER_TYPE_F;
153    case GLSL_TYPE_INT:
154    case GLSL_TYPE_BOOL:
155       return BRW_REGISTER_TYPE_D;
156    case GLSL_TYPE_UINT:
157       return BRW_REGISTER_TYPE_UD;
158    case GLSL_TYPE_ARRAY:
159    case GLSL_TYPE_STRUCT:
160    case GLSL_TYPE_SAMPLER:
161       /* These should be overridden with the type of the member when
162        * dereferenced into.  BRW_REGISTER_TYPE_UD seems like a likely
163        * way to trip up if we don't.
164        */
165       return BRW_REGISTER_TYPE_UD;
166    default:
167       assert(!"not reached");
168       return BRW_REGISTER_TYPE_F;
169    }
170 }
171
172 uint32_t
173 brw_conditional_for_comparison(unsigned int op)
174 {
175    switch (op) {
176    case ir_binop_less:
177       return BRW_CONDITIONAL_L;
178    case ir_binop_greater:
179       return BRW_CONDITIONAL_G;
180    case ir_binop_lequal:
181       return BRW_CONDITIONAL_LE;
182    case ir_binop_gequal:
183       return BRW_CONDITIONAL_GE;
184    case ir_binop_equal:
185    case ir_binop_all_equal: /* same as equal for scalars */
186       return BRW_CONDITIONAL_Z;
187    case ir_binop_nequal:
188    case ir_binop_any_nequal: /* same as nequal for scalars */
189       return BRW_CONDITIONAL_NZ;
190    default:
191       assert(!"not reached: bad operation for comparison");
192       return BRW_CONDITIONAL_NZ;
193    }
194 }