3 * Copyright (C) 2004 David Airlie All Rights Reserved.
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:
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
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.
26 #include "atifragshader.h"
30 #include "s_atifragshader.h"
37 fetch_texel(GLcontext
* ctx
, const GLfloat texcoord
[4], GLfloat lambda
,
38 GLuint unit
, GLfloat color
[4])
41 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
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
,
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]);
54 apply_swizzle(GLfloat values
[4], GLuint swizzle
)
64 case GL_SWIZZLE_STR_ATI
:
69 case GL_SWIZZLE_STQ_ATI
:
74 case GL_SWIZZLE_STR_DR_ATI
:
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;
91 apply_src_rep(GLint optype
, GLuint rep
, GLfloat
* val
)
98 start
= optype
? 3 : 0;
101 for (i
= start
; i
< end
; i
++) {
120 apply_src_mod(GLint optype
, GLuint mod
, GLfloat
* val
)
128 start
= optype
? 3 : 0;
131 for (i
= start
; i
< end
; i
++) {
132 if (mod
& GL_COMP_BIT_ATI
)
135 if (mod
& GL_BIAS_BIT_ATI
)
136 val
[i
] = val
[i
] - 0.5;
138 if (mod
& GL_2X_BIT_ATI
)
141 if (mod
& GL_NEGATE_BIT_ATI
)
147 apply_dst_mod(GLuint optype
, GLuint mod
, GLfloat
* val
)
150 GLint has_sat
= mod
& GL_SATURATE_BIT_ATI
;
153 mod
&= ~GL_SATURATE_BIT_ATI
;
155 start
= optype
? 3 : 0;
156 end
= optype
? 4 : 3;
158 for (i
= start
; i
< end
; i
++) {
169 case GL_HALF_BIT_ATI
:
170 val
[i
] = val
[i
] * 0.5;
172 case GL_QUARTER_BIT_ATI
:
173 val
[i
] = val
[i
] * 0.25;
175 case GL_EIGHTH_BIT_ATI
:
176 val
[i
] = val
[i
] * 0.125;
183 else if (val
[i
] > 1.0)
189 else if (val
[i
] > 8.0)
197 write_dst_addr(GLuint optype
, GLuint mod
, GLuint mask
, GLfloat
* src
,
201 apply_dst_mod(optype
, mod
, src
);
203 if (optype
== ATI_FRAGMENT_SHADER_COLOR_OP
) {
205 if (mask
& GL_RED_BIT_ATI
)
208 if (mask
& GL_GREEN_BIT_ATI
)
211 if (mask
& GL_BLUE_BIT_ATI
)
215 for (i
= 0; i
< 3; i
++)
224 finish_pass(struct atifs_machine
*machine
)
228 for (i
= 0; i
< 6; i
++) {
229 COPY_4V(machine
->PrevPassRegisters
[i
], machine
->Registers
[i
]);
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.
244 struct ati_fs_opcode_st ati_fs_opcodes
[] = {
253 {GL_DOT2_ADD_ATI
, 3},
261 handle_pass_op(struct atifs_machine
*machine
, struct atifs_setupinst
*texinst
,
262 const struct sw_span
*span
, GLuint column
, GLuint idx
)
264 GLuint swizzle
= texinst
->swizzle
;
265 GLuint pass_tex
= texinst
->src
;
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
]);
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
]);
276 apply_swizzle(machine
->Registers
[idx
], swizzle
);
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
)
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];
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
]);
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
]);
298 apply_swizzle(tex_coords
, swizzle
);
299 fetch_texel(ctx
, tex_coords
, 0.0F
, idx
, machine
->Registers
[idx
]);
302 #define SETUP_SRC_REG(optype, i, x) \
304 COPY_4V(src[optype][i], x); \
308 execute_shader(GLcontext
* ctx
,
309 const struct ati_fragment_shader
*shader
, GLuint maxInst
,
310 struct atifs_machine
*machine
, const struct sw_span
*span
,
314 struct atifs_instruction
*inst
;
315 struct atifs_setupinst
*texinst
;
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
;
324 for (pass
= 0; pass
< shader
->NumPasses
; pass
++) {
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
);
335 for (pc
= 0; pc
< shader
->numArithInstr
[pass
]; pc
++) {
336 inst
= &shader
->Instructions
[pass
][pc
];
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
;
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
]);
351 SETUP_SRC_REG(optype
, i
,
352 ctx
->ATIFragmentShader
.GlobalConstants
[index
- GL_CON_0_ATI
]);
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
]);
366 apply_src_rep(optype
, inst
->SrcReg
[optype
][i
].argRep
,
368 apply_src_mod(optype
, inst
->SrcReg
[optype
][i
].argMod
,
373 /* Execute the operations - color then alpha */
374 for (optype
= 0; optype
< 2; optype
++) {
375 if (inst
->Opcode
[optype
]) {
376 switch (inst
->Opcode
[optype
]) {
379 for (i
= 0; i
< 3; i
++) {
381 src
[optype
][0][i
] + src
[optype
][1][i
];
384 dst
[optype
][3] = src
[optype
][0][3] + src
[optype
][1][3];
388 for (i
= 0; i
< 3; i
++) {
390 src
[optype
][0][i
] - src
[optype
][1][i
];
393 dst
[optype
][3] = src
[optype
][0][3] - src
[optype
][1][3];
397 for (i
= 0; i
< 3; i
++) {
399 src
[optype
][0][i
] * src
[optype
][1][i
];
402 dst
[optype
][3] = src
[optype
][0][3] * src
[optype
][1][3];
406 for (i
= 0; i
< 3; i
++) {
408 src
[optype
][0][i
] * src
[optype
][1][i
] +
413 src
[optype
][0][3] * src
[optype
][1][3] +
418 for (i
= 0; i
< 3; i
++) {
420 src
[optype
][0][i
] * src
[optype
][1][i
] + (1 -
428 src
[optype
][0][3] * src
[optype
][1][3] + (1 -
436 for (i
= 0; i
< 3; i
++) {
437 dst
[optype
][i
] = src
[optype
][0][i
];
440 dst
[optype
][3] = src
[optype
][0][3];
444 for (i
= 0; i
< 3; i
++) {
447 0.5) ? src
[optype
][0][i
] : src
[optype
][1][i
];
453 0.5) ? src
[optype
][0][3] : src
[optype
][1][3];
459 for (i
= 0; i
< 3; i
++) {
461 (src
[optype
][2][i
] >=
462 0) ? src
[optype
][0][i
] : src
[optype
][1][i
];
466 (src
[optype
][2][3] >=
467 0) ? src
[optype
][0][3] : src
[optype
][1][3];
470 case GL_DOT2_ADD_ATI
:
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];
479 for (i
= 0; i
< 3; i
++) {
480 dst
[optype
][i
] = result
;
484 dst
[optype
][3] = result
;
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];
497 for (i
= 0; i
< 3; i
++) {
498 dst
[optype
][i
] = result
;
502 dst
[optype
][3] = result
;
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];
515 for (i
= 0; i
< 3; i
++) {
516 dst
[optype
][i
] = result
;
520 dst
[optype
][3] = result
;
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
];
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
],
540 write_dst_addr(1, inst
->DstReg
[0].dstMod
, 0, dst
[1], dstp
);
549 init_machine(GLcontext
* ctx
, struct atifs_machine
*machine
,
550 const struct ati_fragment_shader
*shader
,
551 const struct sw_span
*span
, GLuint col
)
555 for (i
= 0; i
< 6; i
++) {
556 for (j
= 0; j
< 4; j
++)
557 ctx
->ATIFragmentShader
.Machine
.Registers
[i
][j
] = 0.0;
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]);
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]);
582 * Execute the current fragment program, operating on the given span.
585 _swrast_exec_fragment_shader(GLcontext
* ctx
, struct sw_span
*span
)
587 const struct ati_fragment_shader
*shader
= ctx
->ATIFragmentShader
.Current
;
590 ctx
->_CurrentProgram
= GL_FRAGMENT_SHADER_ATI
;
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 /* can't really happen... */
597 if (!execute_shader(ctx
, shader
, ~0,
598 &ctx
->ATIFragmentShader
.Machine
, span
, i
)) {
599 span
->array
->mask
[i
] = GL_FALSE
;
600 span
->writeAll
= GL_FALSE
;
604 const GLfloat
*colOut
=
605 ctx
->ATIFragmentShader
.Machine
.Registers
[0];
607 /*fprintf(stderr,"outputs %f %f %f %f\n", colOut[0], colOut[1], colOut[2], colOut[3]); */
608 UNCLAMPED_FLOAT_TO_CHAN(span
->array
->rgba
[i
][RCOMP
], colOut
[0]);
609 UNCLAMPED_FLOAT_TO_CHAN(span
->array
->rgba
[i
][GCOMP
], colOut
[1]);
610 UNCLAMPED_FLOAT_TO_CHAN(span
->array
->rgba
[i
][BCOMP
], colOut
[2]);
611 UNCLAMPED_FLOAT_TO_CHAN(span
->array
->rgba
[i
][ACOMP
], colOut
[3]);
616 ctx
->_CurrentProgram
= 0;