Tizen 2.0 Release
[profile/ivi/osmesa.git] / src / mesa / drivers / dri / r300 / compiler / r3xx_fragprog.c
1 /*
2  * Copyright 2009 Nicolai Hähnle <nhaehnle@gmail.com>
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  * on the rights to use, copy, modify, merge, publish, distribute, sub
8  * license, and/or sell copies of the Software, and to permit persons to whom
9  * the 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 NON-INFRINGEMENT. IN NO EVENT SHALL
18  * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21  * USE OR OTHER DEALINGS IN THE SOFTWARE. */
22
23 #include "radeon_compiler.h"
24
25 #include <stdio.h>
26
27 #include "radeon_compiler_util.h"
28 #include "radeon_dataflow.h"
29 #include "radeon_emulate_branches.h"
30 #include "radeon_emulate_loops.h"
31 #include "radeon_program_alu.h"
32 #include "radeon_program_tex.h"
33 #include "radeon_rename_regs.h"
34 #include "radeon_remove_constants.h"
35 #include "r300_fragprog.h"
36 #include "r300_fragprog_swizzle.h"
37 #include "r500_fragprog.h"
38
39
40 static void dataflow_outputs_mark_use(void * userdata, void * data,
41                 void (*callback)(void *, unsigned int, unsigned int))
42 {
43         struct r300_fragment_program_compiler * c = userdata;
44         callback(data, c->OutputColor[0], RC_MASK_XYZW);
45         callback(data, c->OutputColor[1], RC_MASK_XYZW);
46         callback(data, c->OutputColor[2], RC_MASK_XYZW);
47         callback(data, c->OutputColor[3], RC_MASK_XYZW);
48         callback(data, c->OutputDepth, RC_MASK_W);
49 }
50
51 static void rc_rewrite_depth_out(struct radeon_compiler *cc, void *user)
52 {
53         struct r300_fragment_program_compiler *c = (struct r300_fragment_program_compiler*)cc;
54         struct rc_instruction *rci;
55
56         for (rci = c->Base.Program.Instructions.Next; rci != &c->Base.Program.Instructions; rci = rci->Next) {
57                 struct rc_sub_instruction * inst = &rci->U.I;
58                 unsigned i;
59                 const struct rc_opcode_info *info = rc_get_opcode_info(inst->Opcode);
60
61                 if (inst->DstReg.File != RC_FILE_OUTPUT || inst->DstReg.Index != c->OutputDepth)
62                         continue;
63
64                 if (inst->DstReg.WriteMask & RC_MASK_Z) {
65                         inst->DstReg.WriteMask = RC_MASK_W;
66                 } else {
67                         inst->DstReg.WriteMask = 0;
68                         continue;
69                 }
70
71                 if (!info->IsComponentwise) {
72                         continue;
73                 }
74
75                 for (i = 0; i < info->NumSrcRegs; i++) {
76                         inst->SrcReg[i] = lmul_swizzle(RC_SWIZZLE_ZZZZ, inst->SrcReg[i]);
77                 }
78         }
79 }
80
81 static int radeon_saturate_output(
82                 struct radeon_compiler * c,
83                 struct rc_instruction * inst,
84                 void* data)
85 {
86         const struct rc_opcode_info *info = rc_get_opcode_info(inst->U.I.Opcode);
87
88         if (!info->HasDstReg || inst->U.I.DstReg.File != RC_FILE_OUTPUT)
89                 return 0;
90
91         inst->U.I.SaturateMode = RC_SATURATE_ZERO_ONE;
92         return 1;
93 }
94
95 void r3xx_compile_fragment_program(struct r300_fragment_program_compiler* c)
96 {
97         int is_r500 = c->Base.is_r500;
98         int opt = !c->Base.disable_optimizations;
99         int sat_out = c->state.frag_clamp;
100
101         /* Lists of instruction transformations. */
102         struct radeon_program_transformation saturate_output[] = {
103                 { &radeon_saturate_output, c },
104                 { 0, 0 }
105         };
106
107         struct radeon_program_transformation rewrite_tex[] = {
108                 { &radeonTransformTEX, c },
109                 { 0, 0 }
110         };
111
112         struct radeon_program_transformation rewrite_if[] = {
113                 { &r500_transform_IF, 0 },
114                 {0, 0}
115         };
116
117         struct radeon_program_transformation native_rewrite_r500[] = {
118                 { &radeonTransformALU, 0 },
119                 { &radeonTransformDeriv, 0 },
120                 { &radeonTransformTrigScale, 0 },
121                 { 0, 0 }
122         };
123
124         struct radeon_program_transformation native_rewrite_r300[] = {
125                 { &radeonTransformALU, 0 },
126                 { &r300_transform_trig_simple, 0 },
127                 { 0, 0 }
128         };
129
130         /* List of compiler passes. */
131         struct radeon_compiler_pass fs_list[] = {
132                 /* NAME                         DUMP PREDICATE  FUNCTION                        PARAM */
133                 {"rewrite depth out",           1, 1,           rc_rewrite_depth_out,           NULL},
134                 /* This transformation needs to be done before any of the IF
135                  * instructions are modified. */
136                 {"transform KILP",              1, 1,           rc_transform_KILP,              NULL},
137                 {"unroll loops",                1, is_r500,     rc_unroll_loops,                NULL},
138                 {"transform loops",             1, !is_r500,    rc_transform_loops,             NULL},
139                 {"emulate branches",            1, !is_r500,    rc_emulate_branches,            NULL},
140                 {"saturate output writes",      1, sat_out,     rc_local_transform,             saturate_output},
141                 {"transform TEX",               1, 1,           rc_local_transform,             rewrite_tex},
142                 {"transform IF",                1, is_r500,     rc_local_transform,             rewrite_if},
143                 {"native rewrite",              1, is_r500,     rc_local_transform,             native_rewrite_r500},
144                 {"native rewrite",              1, !is_r500,    rc_local_transform,             native_rewrite_r300},
145                 {"deadcode",                    1, opt,         rc_dataflow_deadcode,           dataflow_outputs_mark_use},
146                 {"emulate loops",               1, !is_r500,    rc_emulate_loops,               NULL},
147                 {"dataflow optimize",           1, opt,         rc_optimize,                    NULL},
148                 {"dataflow swizzles",           1, 1,           rc_dataflow_swizzles,           NULL},
149                 {"dead constants",              1, 1,           rc_remove_unused_constants,     &c->code->constants_remap_table},
150                 /* This pass makes it easier for the scheduler to group TEX
151                  * instructions and reduces the chances of creating too
152                  * many texture indirections.*/
153                 {"register rename",             1, !is_r500,    rc_rename_regs,                 NULL},
154                 {"pair translate",              1, 1,           rc_pair_translate,              NULL},
155                 {"pair scheduling",             1, 1,           rc_pair_schedule,               NULL},
156                 {"dead sources",                1, 1,           rc_pair_remove_dead_sources, NULL},
157                 {"register allocation",         1, 1,           rc_pair_regalloc,               &opt},
158                 {"final code validation",       0, 1,           rc_validate_final_shader,       NULL},
159                 {"machine code generation",     0, is_r500,     r500BuildFragmentProgramHwCode, NULL},
160                 {"machine code generation",     0, !is_r500,    r300BuildFragmentProgramHwCode, NULL},
161                 {"dump machine code",           0, is_r500  && (c->Base.Debug & RC_DBG_LOG), r500FragmentProgramDump, NULL},
162                 {"dump machine code",           0, !is_r500 && (c->Base.Debug & RC_DBG_LOG), r300FragmentProgramDump, NULL},
163                 {NULL, 0, 0, NULL, NULL}
164         };
165
166         c->Base.type = RC_FRAGMENT_PROGRAM;
167         c->Base.SwizzleCaps = c->Base.is_r500 ? &r500_swizzle_caps : &r300_swizzle_caps;
168
169         rc_run_compiler(&c->Base, fs_list);
170
171         rc_constants_copy(&c->code->constants, &c->Base.Program.Constants);
172 }