r300: Remove all Mesa dependencies from the shader 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_nqssadce.h"
28 #include "radeon_program_alu.h"
29 #include "r300_fragprog.h"
30 #include "r300_fragprog_swizzle.h"
31 #include "r500_fragprog.h"
32
33
34 static void nqssadce_init(struct nqssadce_state* s)
35 {
36 struct r300_fragment_program_compiler * c = s->UserData;
37 s->Outputs[c->OutputColor].Sourced = RC_MASK_XYZW;
38 s->Outputs[c->OutputDepth].Sourced = 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 } else {
96 struct radeon_program_transformation transformations[] = {
97 { &r300_transform_TEX, c },
98 { &radeonTransformALU, 0 },
99 { &radeonTransformTrigSimple, 0 }
100 };
101 radeonLocalTransform(&c->Base, 3, transformations);
102 }
103
104 if (c->Base.Debug) {
105 fprintf(stderr, "Fragment Program: After native rewrite:\n");
106 rc_print_program(&c->Base.Program);
107 fflush(stderr);
108 }
109
110 if (c->is_r500) {
111 struct radeon_nqssadce_descr nqssadce = {
112 .Init = &nqssadce_init,
113 .IsNativeSwizzle = &r500FPIsNativeSwizzle,
114 .BuildSwizzle = &r500FPBuildSwizzle
115 };
116 radeonNqssaDce(&c->Base, &nqssadce, c);
117 } else {
118 struct radeon_nqssadce_descr nqssadce = {
119 .Init = &nqssadce_init,
120 .IsNativeSwizzle = &r300FPIsNativeSwizzle,
121 .BuildSwizzle = &r300FPBuildSwizzle
122 };
123 radeonNqssaDce(&c->Base, &nqssadce, c);
124 }
125
126 if (c->Base.Debug) {
127 fprintf(stderr, "Compiler: after NqSSA-DCE:\n");
128 rc_print_program(&c->Base.Program);
129 fflush(stderr);
130 }
131
132 if (c->is_r500) {
133 r500BuildFragmentProgramHwCode(c);
134 } else {
135 r300BuildFragmentProgramHwCode(c);
136 }
137
138 rc_constants_copy(&c->code->constants, &c->Base.Program.Constants);
139
140 if (c->Base.Debug) {
141 if (c->is_r500) {
142 r500FragmentProgramDump(c->code);
143 } else {
144 r300FragmentProgramDump(c->code);
145 }
146 }
147 }