No longer derive 'ati_fragment_shader' from 'program' class. Only the
[mesa.git] / src / mesa / swrast / s_atifragshader.c
1 /*
2 *
3 * Copyright (C) 2004 David Airlie All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * DAVID AIRLIE BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
19 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 */
22
23 #include "glheader.h"
24 #include "colormac.h"
25 #include "context.h"
26 #include "atifragshader.h"
27 #include "macros.h"
28 #include "program.h"
29
30 #include "s_atifragshader.h"
31
32
33 /**
34 * Fetch a texel.
35 */
36 static void
37 fetch_texel(GLcontext * ctx, const GLfloat texcoord[4], GLfloat lambda,
38 GLuint unit, GLfloat color[4])
39 {
40 GLchan rgba[4];
41 SWcontext *swrast = SWRAST_CONTEXT(ctx);
42
43 /* XXX use a float-valued TextureSample routine here!!! */
44 swrast->TextureSample[unit](ctx, ctx->Texture.Unit[unit]._Current,
45 1, (const GLfloat(*)[4]) texcoord,
46 &lambda, &rgba);
47 color[0] = CHAN_TO_FLOAT(rgba[0]);
48 color[1] = CHAN_TO_FLOAT(rgba[1]);
49 color[2] = CHAN_TO_FLOAT(rgba[2]);
50 color[3] = CHAN_TO_FLOAT(rgba[3]);
51 }
52
53 static void
54 apply_swizzle(GLfloat values[4], GLuint swizzle)
55 {
56 GLfloat s, t, r, q;
57
58 s = values[0];
59 t = values[1];
60 r = values[2];
61 q = values[3];
62
63 switch (swizzle) {
64 case GL_SWIZZLE_STR_ATI:
65 values[0] = s;
66 values[1] = t;
67 values[2] = r;
68 break;
69 case GL_SWIZZLE_STQ_ATI:
70 values[0] = s;
71 values[1] = t;
72 values[2] = q;
73 break;
74 case GL_SWIZZLE_STR_DR_ATI:
75 values[0] = s / r;
76 values[1] = t / r;
77 values[2] = 1 / r;
78 break;
79 case GL_SWIZZLE_STQ_DQ_ATI:
80 /* make sure q is not 0 to avoid problems later with infinite values (texture lookup)? */
81 if (q == 0.0F) q = 0.000000001;
82 values[0] = s / q;
83 values[1] = t / q;
84 values[2] = 1 / q;
85 break;
86 }
87 values[3] = 0.0;
88 }
89
90 static void
91 apply_src_rep(GLint optype, GLuint rep, GLfloat * val)
92 {
93 GLint i;
94 GLint start, end;
95 if (!rep)
96 return;
97
98 start = optype ? 3 : 0;
99 end = 4;
100
101 for (i = start; i < end; i++) {
102 switch (rep) {
103 case GL_RED:
104 val[i] = val[0];
105 break;
106 case GL_GREEN:
107 val[i] = val[1];
108 break;
109 case GL_BLUE:
110 val[i] = val[2];
111 break;
112 case GL_ALPHA:
113 val[i] = val[3];
114 break;
115 }
116 }
117 }
118
119 static void
120 apply_src_mod(GLint optype, GLuint mod, GLfloat * val)
121 {
122 GLint i;
123 GLint start, end;
124
125 if (!mod)
126 return;
127
128 start = optype ? 3 : 0;
129 end = 4;
130
131 for (i = start; i < end; i++) {
132 if (mod & GL_COMP_BIT_ATI)
133 val[i] = 1 - val[i];
134
135 if (mod & GL_BIAS_BIT_ATI)
136 val[i] = val[i] - 0.5;
137
138 if (mod & GL_2X_BIT_ATI)
139 val[i] = 2 * val[i];
140
141 if (mod & GL_NEGATE_BIT_ATI)
142 val[i] = -val[i];
143 }
144 }
145
146 static void
147 apply_dst_mod(GLuint optype, GLuint mod, GLfloat * val)
148 {
149 GLint i;
150 GLint has_sat = mod & GL_SATURATE_BIT_ATI;
151 GLint start, end;
152
153 mod &= ~GL_SATURATE_BIT_ATI;
154
155 start = optype ? 3 : 0;
156 end = optype ? 4 : 3;
157
158 for (i = start; i < end; i++) {
159 switch (mod) {
160 case GL_2X_BIT_ATI:
161 val[i] = 2 * val[i];
162 break;
163 case GL_4X_BIT_ATI:
164 val[i] = 4 * val[i];
165 break;
166 case GL_8X_BIT_ATI:
167 val[i] = 8 * val[i];
168 break;
169 case GL_HALF_BIT_ATI:
170 val[i] = val[i] * 0.5;
171 break;
172 case GL_QUARTER_BIT_ATI:
173 val[i] = val[i] * 0.25;
174 break;
175 case GL_EIGHTH_BIT_ATI:
176 val[i] = val[i] * 0.125;
177 break;
178 }
179
180 if (has_sat) {
181 if (val[i] < 0.0)
182 val[i] = 0;
183 else if (val[i] > 1.0)
184 val[i] = 1.0;
185 }
186 else {
187 if (val[i] < -8.0)
188 val[i] = -8.0;
189 else if (val[i] > 8.0)
190 val[i] = 8.0;
191 }
192 }
193 }
194
195
196 static void
197 write_dst_addr(GLuint optype, GLuint mod, GLuint mask, GLfloat * src,
198 GLfloat * dst)
199 {
200 GLint i;
201 apply_dst_mod(optype, mod, src);
202
203 if (optype == ATI_FRAGMENT_SHADER_COLOR_OP) {
204 if (mask) {
205 if (mask & GL_RED_BIT_ATI)
206 dst[0] = src[0];
207
208 if (mask & GL_GREEN_BIT_ATI)
209 dst[1] = src[1];
210
211 if (mask & GL_BLUE_BIT_ATI)
212 dst[2] = src[2];
213 }
214 else {
215 for (i = 0; i < 3; i++)
216 dst[i] = src[i];
217 }
218 }
219 else
220 dst[3] = src[3];
221 }
222
223 static void
224 finish_pass(struct atifs_machine *machine)
225 {
226 GLint i;
227
228 for (i = 0; i < 6; i++) {
229 COPY_4V(machine->PrevPassRegisters[i], machine->Registers[i]);
230 }
231 }
232
233 /**
234 * Execute the given fragment shader
235 * NOTE: we do everything in single-precision floating point; we don't
236 * currently observe the single/half/fixed-precision qualifiers.
237 * \param ctx - rendering context
238 * \param program - the fragment program to execute
239 * \param machine - machine state (register file)
240 * \param maxInst - max number of instructions to execute
241 * \return GL_TRUE if program completed or GL_FALSE if program executed KIL.
242 */
243
244 struct ati_fs_opcode_st ati_fs_opcodes[] = {
245 {GL_ADD_ATI, 2},
246 {GL_SUB_ATI, 2},
247 {GL_MUL_ATI, 2},
248 {GL_MAD_ATI, 3},
249 {GL_LERP_ATI, 3},
250 {GL_MOV_ATI, 1},
251 {GL_CND_ATI, 3},
252 {GL_CND0_ATI, 3},
253 {GL_DOT2_ADD_ATI, 3},
254 {GL_DOT3_ATI, 2},
255 {GL_DOT4_ATI, 2}
256 };
257
258
259
260 static void
261 handle_pass_op(struct atifs_machine *machine, struct atifs_setupinst *texinst,
262 const struct sw_span *span, GLuint column, GLuint idx)
263 {
264 GLuint swizzle = texinst->swizzle;
265 GLuint pass_tex = texinst->src;
266
267 if (pass_tex >= GL_TEXTURE0_ARB && pass_tex <= GL_TEXTURE7_ARB) {
268 pass_tex -= GL_TEXTURE0_ARB;
269 COPY_4V(machine->Registers[idx],
270 span->array->texcoords[pass_tex][column]);
271 }
272 else if (pass_tex >= GL_REG_0_ATI && pass_tex <= GL_REG_5_ATI) {
273 pass_tex -= GL_REG_0_ATI;
274 COPY_4V(machine->Registers[idx], machine->PrevPassRegisters[pass_tex]);
275 }
276 apply_swizzle(machine->Registers[idx], swizzle);
277
278 }
279
280 static void
281 handle_sample_op(GLcontext * ctx, struct atifs_machine *machine,
282 struct atifs_setupinst *texinst, const struct sw_span *span,
283 GLuint column, GLuint idx)
284 {
285 /* sample from unit idx using texinst->src as coords */
286 GLuint swizzle = texinst->swizzle;
287 GLuint coord_source = texinst->src;
288 GLfloat tex_coords[4];
289
290 if (coord_source >= GL_TEXTURE0_ARB && coord_source <= GL_TEXTURE7_ARB) {
291 coord_source -= GL_TEXTURE0_ARB;
292 COPY_4V(tex_coords, span->array->texcoords[coord_source][column]);
293 }
294 else if (coord_source >= GL_REG_0_ATI && coord_source <= GL_REG_5_ATI) {
295 coord_source -= GL_REG_0_ATI;
296 COPY_4V(tex_coords, machine->PrevPassRegisters[coord_source]);
297 }
298 apply_swizzle(tex_coords, swizzle);
299 fetch_texel(ctx, tex_coords, 0.0F, idx, machine->Registers[idx]);
300 }
301
302 #define SETUP_SRC_REG(optype, i, x) \
303 do { \
304 COPY_4V(src[optype][i], x); \
305 } while (0)
306
307 static GLboolean
308 execute_shader(GLcontext * ctx,
309 const struct ati_fragment_shader *shader, GLuint maxInst,
310 struct atifs_machine *machine, const struct sw_span *span,
311 GLuint column)
312 {
313 GLuint pc;
314 struct atifs_instruction *inst;
315 struct atifs_setupinst *texinst;
316 GLint optype;
317 GLint i, j, pass;
318 GLint dstreg;
319 GLfloat src[2][3][4];
320 GLfloat zeros[4] = { 0.0, 0.0, 0.0, 0.0 };
321 GLfloat ones[4] = { 1.0, 1.0, 1.0, 1.0 };
322 GLfloat dst[2][4], *dstp;
323
324 for (pass = 0; pass < shader->NumPasses; pass++) {
325 if (pass > 0)
326 finish_pass(machine);
327 for (j = 0; j < MAX_NUM_FRAGMENT_REGISTERS_ATI; j++) {
328 texinst = &shader->SetupInst[pass][j];
329 if (texinst->Opcode == ATI_FRAGMENT_SHADER_PASS_OP)
330 handle_pass_op(machine, texinst, span, column, j);
331 else if (texinst->Opcode == ATI_FRAGMENT_SHADER_SAMPLE_OP)
332 handle_sample_op(ctx, machine, texinst, span, column, j);
333 }
334
335 for (pc = 0; pc < shader->numArithInstr[pass]; pc++) {
336 inst = &shader->Instructions[pass][pc];
337
338 /* setup the source registers for color and alpha ops */
339 for (optype = 0; optype < 2; optype++) {
340 for (i = 0; i < inst->ArgCount[optype]; i++) {
341 GLint index = inst->SrcReg[optype][i].Index;
342
343 if (index >= GL_REG_0_ATI && index <= GL_REG_5_ATI)
344 SETUP_SRC_REG(optype, i,
345 machine->Registers[index - GL_REG_0_ATI]);
346 else if (index >= GL_CON_0_ATI && index <= GL_CON_7_ATI) {
347 if (shader->LocalConstDef & (1 << (index - GL_CON_0_ATI))) {
348 SETUP_SRC_REG(optype, i,
349 shader->Constants[index - GL_CON_0_ATI]);
350 } else {
351 SETUP_SRC_REG(optype, i,
352 ctx->ATIFragmentShader.GlobalConstants[index - GL_CON_0_ATI]);
353 }
354 }
355 else if (index == GL_ONE)
356 SETUP_SRC_REG(optype, i, ones);
357 else if (index == GL_ZERO)
358 SETUP_SRC_REG(optype, i, zeros);
359 else if (index == GL_PRIMARY_COLOR_EXT)
360 SETUP_SRC_REG(optype, i,
361 machine->Inputs[ATI_FS_INPUT_PRIMARY]);
362 else if (index == GL_SECONDARY_INTERPOLATOR_ATI)
363 SETUP_SRC_REG(optype, i,
364 machine->Inputs[ATI_FS_INPUT_SECONDARY]);
365
366 apply_src_rep(optype, inst->SrcReg[optype][i].argRep,
367 src[optype][i]);
368 apply_src_mod(optype, inst->SrcReg[optype][i].argMod,
369 src[optype][i]);
370 }
371 }
372
373 /* Execute the operations - color then alpha */
374 for (optype = 0; optype < 2; optype++) {
375 if (inst->Opcode[optype]) {
376 switch (inst->Opcode[optype]) {
377 case GL_ADD_ATI:
378 if (!optype)
379 for (i = 0; i < 3; i++) {
380 dst[optype][i] =
381 src[optype][0][i] + src[optype][1][i];
382 }
383 else
384 dst[optype][3] = src[optype][0][3] + src[optype][1][3];
385 break;
386 case GL_SUB_ATI:
387 if (!optype)
388 for (i = 0; i < 3; i++) {
389 dst[optype][i] =
390 src[optype][0][i] - src[optype][1][i];
391 }
392 else
393 dst[optype][3] = src[optype][0][3] - src[optype][1][3];
394 break;
395 case GL_MUL_ATI:
396 if (!optype)
397 for (i = 0; i < 3; i++) {
398 dst[optype][i] =
399 src[optype][0][i] * src[optype][1][i];
400 }
401 else
402 dst[optype][3] = src[optype][0][3] * src[optype][1][3];
403 break;
404 case GL_MAD_ATI:
405 if (!optype)
406 for (i = 0; i < 3; i++) {
407 dst[optype][i] =
408 src[optype][0][i] * src[optype][1][i] +
409 src[optype][2][i];
410 }
411 else
412 dst[optype][3] =
413 src[optype][0][3] * src[optype][1][3] +
414 src[optype][2][3];
415 break;
416 case GL_LERP_ATI:
417 if (!optype)
418 for (i = 0; i < 3; i++) {
419 dst[optype][i] =
420 src[optype][0][i] * src[optype][1][i] + (1 -
421 src
422 [optype]
423 [0][i]) *
424 src[optype][2][i];
425 }
426 else
427 dst[optype][3] =
428 src[optype][0][3] * src[optype][1][3] + (1 -
429 src[optype]
430 [0][3]) *
431 src[optype][2][3];
432 break;
433
434 case GL_MOV_ATI:
435 if (!optype)
436 for (i = 0; i < 3; i++) {
437 dst[optype][i] = src[optype][0][i];
438 }
439 else
440 dst[optype][3] = src[optype][0][3];
441 break;
442 case GL_CND_ATI:
443 if (!optype) {
444 for (i = 0; i < 3; i++) {
445 dst[optype][i] =
446 (src[optype][2][i] >
447 0.5) ? src[optype][0][i] : src[optype][1][i];
448 }
449 }
450 else {
451 dst[optype][3] =
452 (src[optype][2][3] >
453 0.5) ? src[optype][0][3] : src[optype][1][3];
454 }
455 break;
456
457 case GL_CND0_ATI:
458 if (!optype)
459 for (i = 0; i < 3; i++) {
460 dst[optype][i] =
461 (src[optype][2][i] >=
462 0) ? src[optype][0][i] : src[optype][1][i];
463 }
464 else {
465 dst[optype][3] =
466 (src[optype][2][3] >=
467 0) ? src[optype][0][3] : src[optype][1][3];
468 }
469 break;
470 case GL_DOT2_ADD_ATI:
471 {
472 GLfloat result;
473
474 /* DOT 2 always uses the source from the color op */
475 /* could save recalculation of dot products for alpha inst */
476 result = src[0][0][0] * src[0][1][0] +
477 src[0][0][1] * src[0][1][1] + src[0][2][2];
478 if (!optype) {
479 for (i = 0; i < 3; i++) {
480 dst[optype][i] = result;
481 }
482 }
483 else
484 dst[optype][3] = result;
485 }
486 break;
487 case GL_DOT3_ATI:
488 {
489 GLfloat result;
490
491 /* DOT 3 always uses the source from the color op */
492 result = src[0][0][0] * src[0][1][0] +
493 src[0][0][1] * src[0][1][1] +
494 src[0][0][2] * src[0][1][2];
495
496 if (!optype) {
497 for (i = 0; i < 3; i++) {
498 dst[optype][i] = result;
499 }
500 }
501 else
502 dst[optype][3] = result;
503 }
504 break;
505 case GL_DOT4_ATI:
506 {
507 GLfloat result;
508
509 /* DOT 4 always uses the source from the color op */
510 result = src[0][0][0] * src[0][1][0] +
511 src[0][0][1] * src[0][1][1] +
512 src[0][0][2] * src[0][1][2] +
513 src[0][0][3] * src[0][1][3];
514 if (!optype) {
515 for (i = 0; i < 3; i++) {
516 dst[optype][i] = result;
517 }
518 }
519 else
520 dst[optype][3] = result;
521 }
522 break;
523
524 }
525 }
526 }
527
528 /* write out the destination registers */
529 for (optype = 0; optype < 2; optype++) {
530 if (inst->Opcode[optype]) {
531 dstreg = inst->DstReg[optype].Index;
532 dstp = machine->Registers[dstreg - GL_REG_0_ATI];
533
534 if ((optype == 0) || ((inst->Opcode[1] != GL_DOT2_ADD_ATI) &&
535 (inst->Opcode[1] != GL_DOT3_ATI) && (inst->Opcode[1] != GL_DOT4_ATI)))
536 write_dst_addr(optype, inst->DstReg[optype].dstMod,
537 inst->DstReg[optype].dstMask, dst[optype],
538 dstp);
539 else
540 write_dst_addr(1, inst->DstReg[0].dstMod, 0, dst[1], dstp);
541 }
542 }
543 }
544 }
545 return GL_TRUE;
546 }
547
548 static void
549 init_machine(GLcontext * ctx, struct atifs_machine *machine,
550 const struct ati_fragment_shader *shader,
551 const struct sw_span *span, GLuint col)
552 {
553 GLint i, j;
554
555 for (i = 0; i < 6; i++) {
556 for (j = 0; j < 4; j++)
557 ctx->ATIFragmentShader.Machine.Registers[i][j] = 0.0;
558 }
559
560 ctx->ATIFragmentShader.Machine.Inputs[ATI_FS_INPUT_PRIMARY][0] =
561 CHAN_TO_FLOAT(span->array->rgba[col][0]);
562 ctx->ATIFragmentShader.Machine.Inputs[ATI_FS_INPUT_PRIMARY][1] =
563 CHAN_TO_FLOAT(span->array->rgba[col][1]);
564 ctx->ATIFragmentShader.Machine.Inputs[ATI_FS_INPUT_PRIMARY][2] =
565 CHAN_TO_FLOAT(span->array->rgba[col][2]);
566 ctx->ATIFragmentShader.Machine.Inputs[ATI_FS_INPUT_PRIMARY][3] =
567 CHAN_TO_FLOAT(span->array->rgba[col][3]);
568
569 ctx->ATIFragmentShader.Machine.Inputs[ATI_FS_INPUT_SECONDARY][0] =
570 CHAN_TO_FLOAT(span->array->spec[col][0]);
571 ctx->ATIFragmentShader.Machine.Inputs[ATI_FS_INPUT_SECONDARY][1] =
572 CHAN_TO_FLOAT(span->array->spec[col][1]);
573 ctx->ATIFragmentShader.Machine.Inputs[ATI_FS_INPUT_SECONDARY][2] =
574 CHAN_TO_FLOAT(span->array->spec[col][2]);
575 ctx->ATIFragmentShader.Machine.Inputs[ATI_FS_INPUT_SECONDARY][3] =
576 CHAN_TO_FLOAT(span->array->spec[col][3]);
577 }
578
579
580
581 /**
582 * Execute the current fragment program, operating on the given span.
583 */
584 void
585 _swrast_exec_fragment_shader(GLcontext * ctx, struct sw_span *span)
586 {
587 const struct ati_fragment_shader *shader = ctx->ATIFragmentShader.Current;
588 GLuint i;
589
590 ctx->_CurrentProgram = GL_FRAGMENT_SHADER_ATI;
591
592 for (i = 0; i < span->end; i++) {
593 if (span->array->mask[i]) {
594 init_machine(ctx, &ctx->ATIFragmentShader.Machine,
595 ctx->ATIFragmentShader.Current, span, i);
596
597 if (execute_shader(ctx, shader, ~0,
598 &ctx->ATIFragmentShader.Machine, span, i)) {
599 span->array->mask[i] = GL_FALSE;
600 }
601
602 {
603 const GLfloat *colOut =
604 ctx->ATIFragmentShader.Machine.Registers[0];
605
606 /*fprintf(stderr,"outputs %f %f %f %f\n", colOut[0], colOut[1], colOut[2], colOut[3]); */
607 UNCLAMPED_FLOAT_TO_CHAN(span->array->rgba[i][RCOMP], colOut[0]);
608 UNCLAMPED_FLOAT_TO_CHAN(span->array->rgba[i][GCOMP], colOut[1]);
609 UNCLAMPED_FLOAT_TO_CHAN(span->array->rgba[i][BCOMP], colOut[2]);
610 UNCLAMPED_FLOAT_TO_CHAN(span->array->rgba[i][ACOMP], colOut[3]);
611 }
612 }
613 }
614
615 ctx->_CurrentProgram = 0;
616 }