2 * Copyright (C) 2004 David Airlie All Rights Reserved.
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 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * DAVID AIRLIE BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 #include "main/glheader.h"
23 #include "main/colormac.h"
24 #include "main/macros.h"
25 #include "main/atifragshader.h"
26 #include "swrast/s_atifragshader.h"
27 #include "swrast/s_context.h"
31 * State for executing ATI fragment shader.
35 GLfloat Registers
[6][4]; /** six temporary registers */
36 GLfloat PrevPassRegisters
[6][4];
37 GLfloat Inputs
[2][4]; /** Primary, secondary input colors */
46 fetch_texel(struct gl_context
* ctx
, const GLfloat texcoord
[4], GLfloat lambda
,
47 GLuint unit
, GLfloat color
[4])
49 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
51 /* XXX use a float-valued TextureSample routine here!!! */
52 swrast
->TextureSample
[unit
](ctx
, ctx
->Texture
.Unit
[unit
]._Current
,
53 1, (const GLfloat(*)[4]) texcoord
,
54 &lambda
, (GLfloat (*)[4]) color
);
58 apply_swizzle(GLfloat values
[4], GLuint swizzle
)
68 case GL_SWIZZLE_STR_ATI
:
73 case GL_SWIZZLE_STQ_ATI
:
78 case GL_SWIZZLE_STR_DR_ATI
:
83 case GL_SWIZZLE_STQ_DQ_ATI
:
84 /* make sure q is not 0 to avoid problems later with infinite values (texture lookup)? */
96 apply_src_rep(GLint optype
, GLuint rep
, GLfloat
* val
)
103 start
= optype
? 3 : 0;
106 for (i
= start
; i
< end
; i
++) {
125 apply_src_mod(GLint optype
, GLuint mod
, GLfloat
* val
)
133 start
= optype
? 3 : 0;
136 for (i
= start
; i
< end
; i
++) {
137 if (mod
& GL_COMP_BIT_ATI
)
140 if (mod
& GL_BIAS_BIT_ATI
)
141 val
[i
] = val
[i
] - 0.5F
;
143 if (mod
& GL_2X_BIT_ATI
)
146 if (mod
& GL_NEGATE_BIT_ATI
)
152 apply_dst_mod(GLuint optype
, GLuint mod
, GLfloat
* val
)
155 GLint has_sat
= mod
& GL_SATURATE_BIT_ATI
;
158 mod
&= ~GL_SATURATE_BIT_ATI
;
160 start
= optype
? 3 : 0;
161 end
= optype
? 4 : 3;
163 for (i
= start
; i
< end
; i
++) {
174 case GL_HALF_BIT_ATI
:
175 val
[i
] = val
[i
] * 0.5F
;
177 case GL_QUARTER_BIT_ATI
:
178 val
[i
] = val
[i
] * 0.25F
;
180 case GL_EIGHTH_BIT_ATI
:
181 val
[i
] = val
[i
] * 0.125F
;
188 else if (val
[i
] > 1.0F
)
194 else if (val
[i
] > 8.0F
)
202 write_dst_addr(GLuint optype
, GLuint mod
, GLuint mask
, GLfloat
* src
,
206 apply_dst_mod(optype
, mod
, src
);
208 if (optype
== ATI_FRAGMENT_SHADER_COLOR_OP
) {
210 if (mask
& GL_RED_BIT_ATI
)
213 if (mask
& GL_GREEN_BIT_ATI
)
216 if (mask
& GL_BLUE_BIT_ATI
)
220 for (i
= 0; i
< 3; i
++)
229 finish_pass(struct atifs_machine
*machine
)
233 for (i
= 0; i
< 6; i
++) {
234 COPY_4V(machine
->PrevPassRegisters
[i
], machine
->Registers
[i
]);
238 struct ati_fs_opcode_st ati_fs_opcodes
[] = {
247 {GL_DOT2_ADD_ATI
, 3},
255 handle_pass_op(struct atifs_machine
*machine
, struct atifs_setupinst
*texinst
,
256 const SWspan
*span
, GLuint column
, GLuint idx
)
258 GLuint swizzle
= texinst
->swizzle
;
259 GLuint pass_tex
= texinst
->src
;
261 if (pass_tex
>= GL_TEXTURE0_ARB
&& pass_tex
<= GL_TEXTURE7_ARB
) {
262 pass_tex
-= GL_TEXTURE0_ARB
;
263 COPY_4V(machine
->Registers
[idx
],
264 span
->array
->attribs
[FRAG_ATTRIB_TEX0
+ pass_tex
][column
]);
266 else if (pass_tex
>= GL_REG_0_ATI
&& pass_tex
<= GL_REG_5_ATI
) {
267 pass_tex
-= GL_REG_0_ATI
;
268 COPY_4V(machine
->Registers
[idx
], machine
->PrevPassRegisters
[pass_tex
]);
270 apply_swizzle(machine
->Registers
[idx
], swizzle
);
275 handle_sample_op(struct gl_context
* ctx
, struct atifs_machine
*machine
,
276 struct atifs_setupinst
*texinst
, const SWspan
*span
,
277 GLuint column
, GLuint idx
)
279 /* sample from unit idx using texinst->src as coords */
280 GLuint swizzle
= texinst
->swizzle
;
281 GLuint coord_source
= texinst
->src
;
282 GLfloat tex_coords
[4] = { 0 };
284 if (coord_source
>= GL_TEXTURE0_ARB
&& coord_source
<= GL_TEXTURE7_ARB
) {
285 coord_source
-= GL_TEXTURE0_ARB
;
287 span
->array
->attribs
[FRAG_ATTRIB_TEX0
+ coord_source
][column
]);
289 else if (coord_source
>= GL_REG_0_ATI
&& coord_source
<= GL_REG_5_ATI
) {
290 coord_source
-= GL_REG_0_ATI
;
291 COPY_4V(tex_coords
, machine
->PrevPassRegisters
[coord_source
]);
293 apply_swizzle(tex_coords
, swizzle
);
294 fetch_texel(ctx
, tex_coords
, 0.0F
, idx
, machine
->Registers
[idx
]);
297 #define SETUP_SRC_REG(optype, i, x) \
299 COPY_4V(src[optype][i], x); \
305 * Execute the given fragment shader.
306 * NOTE: we do everything in single-precision floating point
307 * \param ctx - rendering context
308 * \param shader - the shader to execute
309 * \param machine - virtual machine state
310 * \param span - the SWspan we're operating on
311 * \param column - which pixel [i] we're operating on in the span
314 execute_shader(struct gl_context
*ctx
, const struct ati_fragment_shader
*shader
,
315 struct atifs_machine
*machine
, const SWspan
*span
,
319 struct atifs_instruction
*inst
;
320 struct atifs_setupinst
*texinst
;
325 GLfloat src
[2][3][4];
326 GLfloat zeros
[4] = { 0.0, 0.0, 0.0, 0.0 };
327 GLfloat ones
[4] = { 1.0, 1.0, 1.0, 1.0 };
328 GLfloat dst
[2][4], *dstp
;
330 for (pass
= 0; pass
< shader
->NumPasses
; pass
++) {
332 finish_pass(machine
);
333 for (j
= 0; j
< MAX_NUM_FRAGMENT_REGISTERS_ATI
; j
++) {
334 texinst
= &shader
->SetupInst
[pass
][j
];
335 if (texinst
->Opcode
== ATI_FRAGMENT_SHADER_PASS_OP
)
336 handle_pass_op(machine
, texinst
, span
, column
, j
);
337 else if (texinst
->Opcode
== ATI_FRAGMENT_SHADER_SAMPLE_OP
)
338 handle_sample_op(ctx
, machine
, texinst
, span
, column
, j
);
341 for (pc
= 0; pc
< shader
->numArithInstr
[pass
]; pc
++) {
342 inst
= &shader
->Instructions
[pass
][pc
];
344 /* setup the source registers for color and alpha ops */
345 for (optype
= 0; optype
< 2; optype
++) {
346 for (i
= 0; i
< inst
->ArgCount
[optype
]; i
++) {
347 GLint index
= inst
->SrcReg
[optype
][i
].Index
;
349 if (index
>= GL_REG_0_ATI
&& index
<= GL_REG_5_ATI
)
350 SETUP_SRC_REG(optype
, i
,
351 machine
->Registers
[index
- GL_REG_0_ATI
]);
352 else if (index
>= GL_CON_0_ATI
&& index
<= GL_CON_7_ATI
) {
353 if (shader
->LocalConstDef
& (1 << (index
- GL_CON_0_ATI
))) {
354 SETUP_SRC_REG(optype
, i
,
355 shader
->Constants
[index
- GL_CON_0_ATI
]);
357 SETUP_SRC_REG(optype
, i
,
358 ctx
->ATIFragmentShader
.GlobalConstants
[index
- GL_CON_0_ATI
]);
361 else if (index
== GL_ONE
)
362 SETUP_SRC_REG(optype
, i
, ones
);
363 else if (index
== GL_ZERO
)
364 SETUP_SRC_REG(optype
, i
, zeros
);
365 else if (index
== GL_PRIMARY_COLOR_EXT
)
366 SETUP_SRC_REG(optype
, i
,
367 machine
->Inputs
[ATI_FS_INPUT_PRIMARY
]);
368 else if (index
== GL_SECONDARY_INTERPOLATOR_ATI
)
369 SETUP_SRC_REG(optype
, i
,
370 machine
->Inputs
[ATI_FS_INPUT_SECONDARY
]);
372 apply_src_rep(optype
, inst
->SrcReg
[optype
][i
].argRep
,
374 apply_src_mod(optype
, inst
->SrcReg
[optype
][i
].argMod
,
379 /* Execute the operations - color then alpha */
380 for (optype
= 0; optype
< 2; optype
++) {
381 if (inst
->Opcode
[optype
]) {
382 switch (inst
->Opcode
[optype
]) {
385 for (i
= 0; i
< 3; i
++) {
387 src
[optype
][0][i
] + src
[optype
][1][i
];
390 dst
[optype
][3] = src
[optype
][0][3] + src
[optype
][1][3];
394 for (i
= 0; i
< 3; i
++) {
396 src
[optype
][0][i
] - src
[optype
][1][i
];
399 dst
[optype
][3] = src
[optype
][0][3] - src
[optype
][1][3];
403 for (i
= 0; i
< 3; i
++) {
405 src
[optype
][0][i
] * src
[optype
][1][i
];
408 dst
[optype
][3] = src
[optype
][0][3] * src
[optype
][1][3];
412 for (i
= 0; i
< 3; i
++) {
414 src
[optype
][0][i
] * src
[optype
][1][i
] +
419 src
[optype
][0][3] * src
[optype
][1][3] +
424 for (i
= 0; i
< 3; i
++) {
426 src
[optype
][0][i
] * src
[optype
][1][i
] + (1 -
434 src
[optype
][0][3] * src
[optype
][1][3] + (1 -
442 for (i
= 0; i
< 3; i
++) {
443 dst
[optype
][i
] = src
[optype
][0][i
];
446 dst
[optype
][3] = src
[optype
][0][3];
450 for (i
= 0; i
< 3; i
++) {
453 0.5) ? src
[optype
][0][i
] : src
[optype
][1][i
];
459 0.5) ? src
[optype
][0][3] : src
[optype
][1][3];
465 for (i
= 0; i
< 3; i
++) {
467 (src
[optype
][2][i
] >=
468 0) ? src
[optype
][0][i
] : src
[optype
][1][i
];
472 (src
[optype
][2][3] >=
473 0) ? src
[optype
][0][3] : src
[optype
][1][3];
476 case GL_DOT2_ADD_ATI
:
480 /* DOT 2 always uses the source from the color op */
481 /* could save recalculation of dot products for alpha inst */
482 result
= src
[0][0][0] * src
[0][1][0] +
483 src
[0][0][1] * src
[0][1][1] + src
[0][2][2];
485 for (i
= 0; i
< 3; i
++) {
486 dst
[optype
][i
] = result
;
490 dst
[optype
][3] = result
;
497 /* DOT 3 always uses the source from the color op */
498 result
= src
[0][0][0] * src
[0][1][0] +
499 src
[0][0][1] * src
[0][1][1] +
500 src
[0][0][2] * src
[0][1][2];
503 for (i
= 0; i
< 3; i
++) {
504 dst
[optype
][i
] = result
;
508 dst
[optype
][3] = result
;
515 /* DOT 4 always uses the source from the color op */
516 result
= src
[0][0][0] * src
[0][1][0] +
517 src
[0][0][1] * src
[0][1][1] +
518 src
[0][0][2] * src
[0][1][2] +
519 src
[0][0][3] * src
[0][1][3];
521 for (i
= 0; i
< 3; i
++) {
522 dst
[optype
][i
] = result
;
526 dst
[optype
][3] = result
;
534 /* write out the destination registers */
535 for (optype
= 0; optype
< 2; optype
++) {
536 if (inst
->Opcode
[optype
]) {
537 dstreg
= inst
->DstReg
[optype
].Index
;
538 dstp
= machine
->Registers
[dstreg
- GL_REG_0_ATI
];
540 if ((optype
== 0) || ((inst
->Opcode
[1] != GL_DOT2_ADD_ATI
) &&
541 (inst
->Opcode
[1] != GL_DOT3_ATI
) && (inst
->Opcode
[1] != GL_DOT4_ATI
)))
542 write_dst_addr(optype
, inst
->DstReg
[optype
].dstMod
,
543 inst
->DstReg
[optype
].dstMask
, dst
[optype
],
546 write_dst_addr(1, inst
->DstReg
[0].dstMod
, 0, dst
[1], dstp
);
555 * Init fragment shader virtual machine state.
558 init_machine(struct gl_context
* ctx
, struct atifs_machine
*machine
,
559 const struct ati_fragment_shader
*shader
,
560 const SWspan
*span
, GLuint col
)
562 GLfloat (*inputs
)[4] = machine
->Inputs
;
565 for (i
= 0; i
< 6; i
++) {
566 for (j
= 0; j
< 4; j
++)
567 machine
->Registers
[i
][j
] = 0.0;
570 COPY_4V(inputs
[ATI_FS_INPUT_PRIMARY
], span
->array
->attribs
[FRAG_ATTRIB_COL0
][col
]);
571 COPY_4V(inputs
[ATI_FS_INPUT_SECONDARY
], span
->array
->attribs
[FRAG_ATTRIB_COL1
][col
]);
577 * Execute the current ATI shader program, operating on the given span.
580 _swrast_exec_fragment_shader(struct gl_context
* ctx
, SWspan
*span
)
582 const struct ati_fragment_shader
*shader
= ctx
->ATIFragmentShader
.Current
;
583 struct atifs_machine machine
;
586 /* incoming colors should be floats */
587 ASSERT(span
->array
->ChanType
== GL_FLOAT
);
589 for (i
= 0; i
< span
->end
; i
++) {
590 if (span
->array
->mask
[i
]) {
591 init_machine(ctx
, &machine
, shader
, span
, i
);
593 execute_shader(ctx
, shader
, &machine
, span
, i
);
595 /* store result color */
597 const GLfloat
*colOut
= machine
.Registers
[0];
598 /*fprintf(stderr,"outputs %f %f %f %f\n",
599 colOut[0], colOut[1], colOut[2], colOut[3]); */
600 COPY_4V(span
->array
->attribs
[FRAG_ATTRIB_COL0
][i
], colOut
);