Merge branch 'master' into r300-compiler
[mesa.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_program_alu.h"
28 #include "r300_fragprog.h"
29 #include "r300_fragprog_swizzle.h"
30 #include "r500_fragprog.h"
31
32
33 static void dataflow_outputs_mark_use(void * userdata, void * data,
34 void (*callback)(void *, unsigned int, unsigned int))
35 {
36 struct r300_fragment_program_compiler * c = userdata;
37 callback(data, c->OutputColor, RC_MASK_XYZW);
38 callback(data, c->OutputDepth, RC_MASK_W);
39 }
40
41 static void rewrite_depth_out(struct r300_fragment_program_compiler * c)
42 {
43 struct rc_instruction *rci;
44
45 for (rci = c->Base.Program.Instructions.Next; rci != &c->Base.Program.Instructions; rci = rci->Next) {
46 struct rc_sub_instruction * inst = &rci->I;
47
48 if (inst->DstReg.File != RC_FILE_OUTPUT || inst->DstReg.Index != c->OutputDepth)
49 continue;
50
51 if (inst->DstReg.WriteMask & RC_MASK_Z) {
52 inst->DstReg.WriteMask = RC_MASK_W;
53 } else {
54 inst->DstReg.WriteMask = 0;
55 continue;
56 }
57
58 switch (inst->Opcode) {
59 case RC_OPCODE_FRC:
60 case RC_OPCODE_MOV:
61 inst->SrcReg[0] = lmul_swizzle(RC_SWIZZLE_ZZZZ, inst->SrcReg[0]);
62 break;
63 case RC_OPCODE_ADD:
64 case RC_OPCODE_MAX:
65 case RC_OPCODE_MIN:
66 case RC_OPCODE_MUL:
67 inst->SrcReg[0] = lmul_swizzle(RC_SWIZZLE_ZZZZ, inst->SrcReg[0]);
68 inst->SrcReg[1] = lmul_swizzle(RC_SWIZZLE_ZZZZ, inst->SrcReg[1]);
69 break;
70 case RC_OPCODE_CMP:
71 case RC_OPCODE_MAD:
72 inst->SrcReg[0] = lmul_swizzle(RC_SWIZZLE_ZZZZ, inst->SrcReg[0]);
73 inst->SrcReg[1] = lmul_swizzle(RC_SWIZZLE_ZZZZ, inst->SrcReg[1]);
74 inst->SrcReg[2] = lmul_swizzle(RC_SWIZZLE_ZZZZ, inst->SrcReg[2]);
75 break;
76 default:
77 // Scalar instructions needn't be reswizzled
78 break;
79 }
80 }
81 }
82
83 void r3xx_compile_fragment_program(struct r300_fragment_program_compiler* c)
84 {
85 rewrite_depth_out(c);
86
87 if (c->is_r500) {
88 struct radeon_program_transformation transformations[] = {
89 { &r500_transform_TEX, c },
90 { &radeonTransformALU, 0 },
91 { &radeonTransformDeriv, 0 },
92 { &radeonTransformTrigScale, 0 }
93 };
94 radeonLocalTransform(&c->Base, 4, transformations);
95
96 c->Base.SwizzleCaps = &r500_swizzle_caps;
97 } else {
98 struct radeon_program_transformation transformations[] = {
99 { &r300_transform_TEX, c },
100 { &radeonTransformALU, 0 },
101 { &radeonTransformTrigSimple, 0 }
102 };
103 radeonLocalTransform(&c->Base, 3, transformations);
104
105 c->Base.SwizzleCaps = &r300_swizzle_caps;
106 }
107
108 if (c->Base.Debug) {
109 fprintf(stderr, "Fragment Program: After native rewrite:\n");
110 rc_print_program(&c->Base.Program, 0);
111 fflush(stderr);
112 }
113
114 rc_dataflow_annotate(&c->Base, &dataflow_outputs_mark_use, c);
115 rc_dataflow_dealias(&c->Base);
116 rc_dataflow_swizzles(&c->Base);
117
118 if (c->Base.Debug) {
119 fprintf(stderr, "Compiler: after dataflow passes:\n");
120 rc_print_program(&c->Base.Program, 0);
121 fflush(stderr);
122 }
123
124 if (c->is_r500) {
125 r500BuildFragmentProgramHwCode(c);
126 } else {
127 r300BuildFragmentProgramHwCode(c);
128 }
129
130 rc_constants_copy(&c->code->constants, &c->Base.Program.Constants);
131
132 if (c->Base.Debug) {
133 if (c->is_r500) {
134 r500FragmentProgramDump(c->code);
135 } else {
136 r300FragmentProgramDump(c->code);
137 }
138 }
139 }