Merge commit 'origin/7.8'
[mesa.git] / src / mesa / drivers / dri / r300 / r300_fragprog_common.c
1 /*
2 * Copyright (C) 2009 Maciej Cencora <m.cencora@gmail.com>
3 *
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial
16 * portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 */
27
28 /**
29 * \file
30 *
31 * Fragment program compiler. Perform transformations on the intermediate
32 * representation until the program is in a form where we can translate
33 * it more or less directly into machine-readable form.
34 *
35 * \author Ben Skeggs <darktama@iinet.net.au>
36 * \author Jerome Glisse <j.glisse@gmail.com>
37 */
38
39 #include "r300_fragprog_common.h"
40
41 #include "shader/prog_parameter.h"
42 #include "shader/prog_print.h"
43
44 #include "compiler/radeon_compiler.h"
45
46 #include "radeon_mesa_to_rc.h"
47
48
49 static GLuint build_dtm(GLuint depthmode)
50 {
51 switch(depthmode) {
52 default:
53 case GL_LUMINANCE: return 0;
54 case GL_INTENSITY: return 1;
55 case GL_ALPHA: return 2;
56 }
57 }
58
59 static GLuint build_func(GLuint comparefunc)
60 {
61 return comparefunc - GL_NEVER;
62 }
63
64 /**
65 * Collect all external state that is relevant for compiling the given
66 * fragment program.
67 */
68 static void build_state(
69 r300ContextPtr r300,
70 struct gl_fragment_program *fp,
71 struct r300_fragment_program_external_state *state)
72 {
73 int unit;
74
75 memset(state, 0, sizeof(*state));
76
77 for(unit = 0; unit < 16; ++unit) {
78 if (fp->Base.ShadowSamplers & (1 << unit)) {
79 struct gl_texture_object* tex = r300->radeon.glCtx->Texture.Unit[unit]._Current;
80
81 state->unit[unit].depth_texture_mode = build_dtm(tex->DepthMode);
82 state->unit[unit].texture_compare_func = build_func(tex->CompareFunc);
83 }
84 }
85 }
86
87
88 /**
89 * Transform the program to support fragment.position.
90 *
91 * Introduce a small fragment at the start of the program that will be
92 * the only code that directly reads the FRAG_ATTRIB_WPOS input.
93 * All other code pieces that reference that input will be rewritten
94 * to read from a newly allocated temporary.
95 *
96 */
97 static void insert_WPOS_trailer(struct r300_fragment_program_compiler *compiler, struct r300_fragment_program * fp)
98 {
99 int i;
100
101 fp->wpos_attr = FRAG_ATTRIB_MAX;
102 if (!(compiler->Base.Program.InputsRead & FRAG_BIT_WPOS)) {
103 return;
104 }
105
106 for (i = FRAG_ATTRIB_TEX0; i <= FRAG_ATTRIB_TEX7; ++i)
107 {
108 if (!(compiler->Base.Program.InputsRead & (1 << i))) {
109 fp->wpos_attr = i;
110 break;
111 }
112 }
113
114 /* No free texcoord found, fall-back to software rendering */
115 if (fp->wpos_attr == FRAG_ATTRIB_MAX)
116 {
117 compiler->Base.Error = 1;
118 return;
119 }
120
121 rc_transform_fragment_wpos(&compiler->Base, FRAG_ATTRIB_WPOS, fp->wpos_attr, GL_FALSE);
122 }
123
124 /**
125 * Rewrite fragment.fogcoord to use a texture coordinate slot.
126 * Note that fogcoord is forced into an X001 pattern, and this enforcement
127 * is done here.
128 *
129 * See also the counterpart rewriting for vertex programs.
130 */
131 static void rewriteFog(struct r300_fragment_program_compiler *compiler, struct r300_fragment_program * fp)
132 {
133 struct rc_src_register src;
134 int i;
135
136 fp->fog_attr = FRAG_ATTRIB_MAX;
137 if (!(compiler->Base.Program.InputsRead & FRAG_BIT_FOGC)) {
138 return;
139 }
140
141 for (i = FRAG_ATTRIB_TEX0; i <= FRAG_ATTRIB_TEX7; ++i)
142 {
143 if (!(compiler->Base.Program.InputsRead & (1 << i))) {
144 fp->fog_attr = i;
145 break;
146 }
147 }
148
149 /* No free texcoord found, fall-back to software rendering */
150 if (fp->fog_attr == FRAG_ATTRIB_MAX)
151 {
152 compiler->Base.Error = 1;
153 return;
154 }
155
156 memset(&src, 0, sizeof(src));
157 src.File = RC_FILE_INPUT;
158 src.Index = fp->fog_attr;
159 src.Swizzle = MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_ZERO, SWIZZLE_ZERO, SWIZZLE_ONE);
160 rc_move_input(&compiler->Base, FRAG_ATTRIB_FOGC, src);
161 }
162
163
164 /**
165 * Reserve hardware temporary registers for the program inputs.
166 *
167 * @note This allocation is performed explicitly, because the order of inputs
168 * is determined by the RS hardware.
169 */
170 static void allocate_hw_inputs(
171 struct r300_fragment_program_compiler * c,
172 void (*allocate)(void * data, unsigned input, unsigned hwreg),
173 void * mydata)
174 {
175 GLuint InputsRead = c->Base.Program.InputsRead;
176 int i;
177 GLuint hwindex = 0;
178
179 /* Primary colour */
180 if (InputsRead & FRAG_BIT_COL0)
181 allocate(mydata, FRAG_ATTRIB_COL0, hwindex++);
182 InputsRead &= ~FRAG_BIT_COL0;
183
184 /* Secondary color */
185 if (InputsRead & FRAG_BIT_COL1)
186 allocate(mydata, FRAG_ATTRIB_COL1, hwindex++);
187 InputsRead &= ~FRAG_BIT_COL1;
188
189 /* Texcoords */
190 for (i = 0; i < 8; i++) {
191 if (InputsRead & (FRAG_BIT_TEX0 << i))
192 allocate(mydata, FRAG_ATTRIB_TEX0+i, hwindex++);
193 }
194 InputsRead &= ~FRAG_BITS_TEX_ANY;
195
196 /* Fogcoords treated as a texcoord */
197 if (InputsRead & FRAG_BIT_FOGC)
198 allocate(mydata, FRAG_ATTRIB_FOGC, hwindex++);
199 InputsRead &= ~FRAG_BIT_FOGC;
200
201 /* fragment position treated as a texcoord */
202 if (InputsRead & FRAG_BIT_WPOS)
203 allocate(mydata, FRAG_ATTRIB_WPOS, hwindex++);
204 InputsRead &= ~FRAG_BIT_WPOS;
205
206 /* Anything else */
207 if (InputsRead)
208 rc_error(&c->Base, "Don't know how to handle inputs 0x%x\n", InputsRead);
209 }
210
211
212 static void translate_fragment_program(GLcontext *ctx, struct r300_fragment_program_cont *cont, struct r300_fragment_program *fp)
213 {
214 r300ContextPtr r300 = R300_CONTEXT(ctx);
215 struct r300_fragment_program_compiler compiler;
216
217 rc_init(&compiler.Base);
218 compiler.Base.Debug = (RADEON_DEBUG & RADEON_PIXEL) ? GL_TRUE : GL_FALSE;
219
220 compiler.code = &fp->code;
221 compiler.state = fp->state;
222 compiler.is_r500 = (r300->radeon.radeonScreen->chip_family >= CHIP_FAMILY_RV515) ? GL_TRUE : GL_FALSE;
223 compiler.max_temp_regs = (compiler.is_r500) ? 128 : 32;
224 compiler.OutputDepth = FRAG_RESULT_DEPTH;
225 memset(compiler.OutputColor, 0, 4 * sizeof(unsigned));
226 compiler.OutputColor[0] = FRAG_RESULT_COLOR;
227 compiler.AllocateHwInputs = &allocate_hw_inputs;
228
229 if (compiler.Base.Debug) {
230 fflush(stderr);
231 printf("Fragment Program: Initial program:\n");
232 _mesa_print_program(&cont->Base.Base);
233 fflush(stderr);
234 }
235
236 radeon_mesa_to_rc_program(&compiler.Base, &cont->Base.Base);
237
238 insert_WPOS_trailer(&compiler, fp);
239
240 rewriteFog(&compiler, fp);
241
242 r3xx_compile_fragment_program(&compiler);
243
244 if (compiler.is_r500) {
245 /* We need to support the non-KMS DRM interface, which
246 * artificially limits the number of instructions and
247 * constants which are available to us.
248 *
249 * See also the comment in r300_context.c where we
250 * set the MAX_NATIVE_xxx values.
251 */
252 if (fp->code.code.r500.inst_end >= 255 || fp->code.constants.Count > 255)
253 rc_error(&compiler.Base, "Program is too big (upgrade to r300g to avoid this limitation).\n");
254 }
255
256 fp->error = compiler.Base.Error;
257
258 fp->InputsRead = compiler.Base.Program.InputsRead;
259
260 /* Clear the fog/wpos_attr if code accessing these
261 * attributes has been removed during compilation
262 */
263 if (fp->fog_attr != FRAG_ATTRIB_MAX) {
264 if (!(fp->InputsRead & (1 << fp->fog_attr)))
265 fp->fog_attr = FRAG_ATTRIB_MAX;
266 }
267
268 if (fp->wpos_attr != FRAG_ATTRIB_MAX) {
269 if (!(fp->InputsRead & (1 << fp->wpos_attr)))
270 fp->wpos_attr = FRAG_ATTRIB_MAX;
271 }
272
273 rc_destroy(&compiler.Base);
274 }
275
276 struct r300_fragment_program *r300SelectAndTranslateFragmentShader(GLcontext *ctx)
277 {
278 r300ContextPtr r300 = R300_CONTEXT(ctx);
279 struct r300_fragment_program_cont *fp_list;
280 struct r300_fragment_program *fp;
281 struct r300_fragment_program_external_state state;
282
283 fp_list = (struct r300_fragment_program_cont *)ctx->FragmentProgram._Current;
284 build_state(r300, ctx->FragmentProgram._Current, &state);
285
286 fp = fp_list->progs;
287 while (fp) {
288 if (memcmp(&fp->state, &state, sizeof(state)) == 0) {
289 return r300->selected_fp = fp;
290 }
291 fp = fp->next;
292 }
293
294 fp = calloc(1, sizeof(struct r300_fragment_program));
295
296 fp->state = state;
297
298 fp->next = fp_list->progs;
299 fp_list->progs = fp;
300
301 translate_fragment_program(ctx, fp_list, fp);
302
303 return r300->selected_fp = fp;
304 }