Merge branch 'mesa_7_5_branch'
[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 "shader/prog_parameter.h"
26 #include "shader/prog_print.h"
27 #include "shader/prog_statevars.h"
28
29 #include "radeon_nqssadce.h"
30 #include "radeon_program_alu.h"
31 #include "r300_fragprog.h"
32 #include "r300_fragprog_swizzle.h"
33 #include "r500_fragprog.h"
34
35
36 static void nqssadce_init(struct nqssadce_state* s)
37 {
38 struct r300_fragment_program_compiler * c = s->UserData;
39 s->Outputs[c->OutputColor].Sourced = WRITEMASK_XYZW;
40 s->Outputs[c->OutputDepth].Sourced = WRITEMASK_W;
41 }
42
43 static void rewrite_depth_out(struct r300_fragment_program_compiler * c)
44 {
45 struct rc_instruction *rci;
46
47 for (rci = c->Base.Program.Instructions.Next; rci != &c->Base.Program.Instructions; rci = rci->Next) {
48 struct prog_instruction * inst = &rci->I;
49
50 if (inst->DstReg.File != PROGRAM_OUTPUT || inst->DstReg.Index != c->OutputDepth)
51 continue;
52
53 if (inst->DstReg.WriteMask & WRITEMASK_Z) {
54 inst->DstReg.WriteMask = WRITEMASK_W;
55 } else {
56 inst->DstReg.WriteMask = 0;
57 continue;
58 }
59
60 switch (inst->Opcode) {
61 case OPCODE_FRC:
62 case OPCODE_MOV:
63 inst->SrcReg[0] = lmul_swizzle(SWIZZLE_ZZZZ, inst->SrcReg[0]);
64 break;
65 case OPCODE_ADD:
66 case OPCODE_MAX:
67 case OPCODE_MIN:
68 case OPCODE_MUL:
69 inst->SrcReg[0] = lmul_swizzle(SWIZZLE_ZZZZ, inst->SrcReg[0]);
70 inst->SrcReg[1] = lmul_swizzle(SWIZZLE_ZZZZ, inst->SrcReg[1]);
71 break;
72 case OPCODE_CMP:
73 case OPCODE_MAD:
74 inst->SrcReg[0] = lmul_swizzle(SWIZZLE_ZZZZ, inst->SrcReg[0]);
75 inst->SrcReg[1] = lmul_swizzle(SWIZZLE_ZZZZ, inst->SrcReg[1]);
76 inst->SrcReg[2] = lmul_swizzle(SWIZZLE_ZZZZ, inst->SrcReg[2]);
77 break;
78 default:
79 // Scalar instructions needn't be reswizzled
80 break;
81 }
82 }
83 }
84
85 void r3xx_compile_fragment_program(struct r300_fragment_program_compiler* c)
86 {
87 rewrite_depth_out(c);
88
89 if (c->is_r500) {
90 struct radeon_program_transformation transformations[] = {
91 { &r500_transform_TEX, c },
92 { &radeonTransformALU, 0 },
93 { &radeonTransformDeriv, 0 },
94 { &radeonTransformTrigScale, 0 }
95 };
96 radeonLocalTransform(&c->Base, 4, transformations);
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
106 if (c->Base.Debug) {
107 _mesa_printf("Fragment Program: After native rewrite:\n");
108 rc_print_program(&c->Base.Program);
109 fflush(stderr);
110 }
111
112 if (c->is_r500) {
113 struct radeon_nqssadce_descr nqssadce = {
114 .Init = &nqssadce_init,
115 .IsNativeSwizzle = &r500FPIsNativeSwizzle,
116 .BuildSwizzle = &r500FPBuildSwizzle
117 };
118 radeonNqssaDce(&c->Base, &nqssadce, c);
119 } else {
120 struct radeon_nqssadce_descr nqssadce = {
121 .Init = &nqssadce_init,
122 .IsNativeSwizzle = &r300FPIsNativeSwizzle,
123 .BuildSwizzle = &r300FPBuildSwizzle
124 };
125 radeonNqssaDce(&c->Base, &nqssadce, c);
126 }
127
128 if (c->Base.Debug) {
129 _mesa_printf("Compiler: after NqSSA-DCE:\n");
130 rc_print_program(&c->Base.Program);
131 fflush(stderr);
132 }
133
134 if (c->is_r500) {
135 r500BuildFragmentProgramHwCode(c);
136 } else {
137 r300BuildFragmentProgramHwCode(c);
138 }
139
140 rc_constants_copy(&c->code->constants, &c->Base.Program.Constants);
141
142 if (c->Base.Debug) {
143 if (c->is_r500) {
144 r500FragmentProgramDump(c->code);
145 } else {
146 r300FragmentProgramDump(c->code);
147 }
148 }
149 }