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"
34 * State for executing ATI fragment shader.
38 GLfloat Registers
[6][4]; /** six temporary registers */
39 GLfloat PrevPassRegisters
[6][4];
40 GLfloat Inputs
[2][4]; /** Primary, secondary input colors */
49 fetch_texel(GLcontext
* ctx
, const GLfloat texcoord
[4], GLfloat lambda
,
50 GLuint unit
, GLfloat color
[4])
53 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
55 /* XXX use a float-valued TextureSample routine here!!! */
56 swrast
->TextureSample
[unit
](ctx
, ctx
->Texture
.Unit
[unit
]._Current
,
57 1, (const GLfloat(*)[4]) texcoord
,
59 color
[0] = CHAN_TO_FLOAT(rgba
[0]);
60 color
[1] = CHAN_TO_FLOAT(rgba
[1]);
61 color
[2] = CHAN_TO_FLOAT(rgba
[2]);
62 color
[3] = CHAN_TO_FLOAT(rgba
[3]);
66 apply_swizzle(GLfloat values
[4], GLuint swizzle
)
76 case GL_SWIZZLE_STR_ATI
:
81 case GL_SWIZZLE_STQ_ATI
:
86 case GL_SWIZZLE_STR_DR_ATI
:
91 case GL_SWIZZLE_STQ_DQ_ATI
:
92 /* make sure q is not 0 to avoid problems later with infinite values (texture lookup)? */
93 if (q
== 0.0F
) q
= 0.000000001;
103 apply_src_rep(GLint optype
, GLuint rep
, GLfloat
* val
)
110 start
= optype
? 3 : 0;
113 for (i
= start
; i
< end
; i
++) {
132 apply_src_mod(GLint optype
, GLuint mod
, GLfloat
* val
)
140 start
= optype
? 3 : 0;
143 for (i
= start
; i
< end
; i
++) {
144 if (mod
& GL_COMP_BIT_ATI
)
147 if (mod
& GL_BIAS_BIT_ATI
)
148 val
[i
] = val
[i
] - 0.5;
150 if (mod
& GL_2X_BIT_ATI
)
153 if (mod
& GL_NEGATE_BIT_ATI
)
159 apply_dst_mod(GLuint optype
, GLuint mod
, GLfloat
* val
)
162 GLint has_sat
= mod
& GL_SATURATE_BIT_ATI
;
165 mod
&= ~GL_SATURATE_BIT_ATI
;
167 start
= optype
? 3 : 0;
168 end
= optype
? 4 : 3;
170 for (i
= start
; i
< end
; i
++) {
181 case GL_HALF_BIT_ATI
:
182 val
[i
] = val
[i
] * 0.5;
184 case GL_QUARTER_BIT_ATI
:
185 val
[i
] = val
[i
] * 0.25;
187 case GL_EIGHTH_BIT_ATI
:
188 val
[i
] = val
[i
] * 0.125;
195 else if (val
[i
] > 1.0)
201 else if (val
[i
] > 8.0)
209 write_dst_addr(GLuint optype
, GLuint mod
, GLuint mask
, GLfloat
* src
,
213 apply_dst_mod(optype
, mod
, src
);
215 if (optype
== ATI_FRAGMENT_SHADER_COLOR_OP
) {
217 if (mask
& GL_RED_BIT_ATI
)
220 if (mask
& GL_GREEN_BIT_ATI
)
223 if (mask
& GL_BLUE_BIT_ATI
)
227 for (i
= 0; i
< 3; i
++)
236 finish_pass(struct atifs_machine
*machine
)
240 for (i
= 0; i
< 6; i
++) {
241 COPY_4V(machine
->PrevPassRegisters
[i
], machine
->Registers
[i
]);
245 struct ati_fs_opcode_st ati_fs_opcodes
[] = {
254 {GL_DOT2_ADD_ATI
, 3},
262 handle_pass_op(struct atifs_machine
*machine
, struct atifs_setupinst
*texinst
,
263 const SWspan
*span
, GLuint column
, GLuint idx
)
265 GLuint swizzle
= texinst
->swizzle
;
266 GLuint pass_tex
= texinst
->src
;
268 if (pass_tex
>= GL_TEXTURE0_ARB
&& pass_tex
<= GL_TEXTURE7_ARB
) {
269 pass_tex
-= GL_TEXTURE0_ARB
;
270 COPY_4V(machine
->Registers
[idx
],
271 span
->array
->attribs
[FRAG_ATTRIB_TEX0
+ pass_tex
][column
]);
273 else if (pass_tex
>= GL_REG_0_ATI
&& pass_tex
<= GL_REG_5_ATI
) {
274 pass_tex
-= GL_REG_0_ATI
;
275 COPY_4V(machine
->Registers
[idx
], machine
->PrevPassRegisters
[pass_tex
]);
277 apply_swizzle(machine
->Registers
[idx
], swizzle
);
282 handle_sample_op(GLcontext
* ctx
, struct atifs_machine
*machine
,
283 struct atifs_setupinst
*texinst
, const SWspan
*span
,
284 GLuint column
, GLuint idx
)
286 /* sample from unit idx using texinst->src as coords */
287 GLuint swizzle
= texinst
->swizzle
;
288 GLuint coord_source
= texinst
->src
;
289 GLfloat tex_coords
[4];
291 if (coord_source
>= GL_TEXTURE0_ARB
&& coord_source
<= GL_TEXTURE7_ARB
) {
292 coord_source
-= GL_TEXTURE0_ARB
;
294 span
->array
->attribs
[FRAG_ATTRIB_TEX0
+ coord_source
][column
]);
296 else if (coord_source
>= GL_REG_0_ATI
&& coord_source
<= GL_REG_5_ATI
) {
297 coord_source
-= GL_REG_0_ATI
;
298 COPY_4V(tex_coords
, machine
->PrevPassRegisters
[coord_source
]);
300 apply_swizzle(tex_coords
, swizzle
);
301 fetch_texel(ctx
, tex_coords
, 0.0F
, idx
, machine
->Registers
[idx
]);
304 #define SETUP_SRC_REG(optype, i, x) \
306 COPY_4V(src[optype][i], x); \
312 * Execute the given fragment shader.
313 * NOTE: we do everything in single-precision floating point
314 * \param ctx - rendering context
315 * \param shader - the shader to execute
316 * \param machine - virtual machine state
317 * \param span - the SWspan we're operating on
318 * \param column - which pixel [i] we're operating on in the span
321 execute_shader(GLcontext
*ctx
, const struct ati_fragment_shader
*shader
,
322 struct atifs_machine
*machine
, const SWspan
*span
,
326 struct atifs_instruction
*inst
;
327 struct atifs_setupinst
*texinst
;
332 GLfloat src
[2][3][4];
333 GLfloat zeros
[4] = { 0.0, 0.0, 0.0, 0.0 };
334 GLfloat ones
[4] = { 1.0, 1.0, 1.0, 1.0 };
335 GLfloat dst
[2][4], *dstp
;
337 for (pass
= 0; pass
< shader
->NumPasses
; pass
++) {
339 finish_pass(machine
);
340 for (j
= 0; j
< MAX_NUM_FRAGMENT_REGISTERS_ATI
; j
++) {
341 texinst
= &shader
->SetupInst
[pass
][j
];
342 if (texinst
->Opcode
== ATI_FRAGMENT_SHADER_PASS_OP
)
343 handle_pass_op(machine
, texinst
, span
, column
, j
);
344 else if (texinst
->Opcode
== ATI_FRAGMENT_SHADER_SAMPLE_OP
)
345 handle_sample_op(ctx
, machine
, texinst
, span
, column
, j
);
348 for (pc
= 0; pc
< shader
->numArithInstr
[pass
]; pc
++) {
349 inst
= &shader
->Instructions
[pass
][pc
];
351 /* setup the source registers for color and alpha ops */
352 for (optype
= 0; optype
< 2; optype
++) {
353 for (i
= 0; i
< inst
->ArgCount
[optype
]; i
++) {
354 GLint index
= inst
->SrcReg
[optype
][i
].Index
;
356 if (index
>= GL_REG_0_ATI
&& index
<= GL_REG_5_ATI
)
357 SETUP_SRC_REG(optype
, i
,
358 machine
->Registers
[index
- GL_REG_0_ATI
]);
359 else if (index
>= GL_CON_0_ATI
&& index
<= GL_CON_7_ATI
) {
360 if (shader
->LocalConstDef
& (1 << (index
- GL_CON_0_ATI
))) {
361 SETUP_SRC_REG(optype
, i
,
362 shader
->Constants
[index
- GL_CON_0_ATI
]);
364 SETUP_SRC_REG(optype
, i
,
365 ctx
->ATIFragmentShader
.GlobalConstants
[index
- GL_CON_0_ATI
]);
368 else if (index
== GL_ONE
)
369 SETUP_SRC_REG(optype
, i
, ones
);
370 else if (index
== GL_ZERO
)
371 SETUP_SRC_REG(optype
, i
, zeros
);
372 else if (index
== GL_PRIMARY_COLOR_EXT
)
373 SETUP_SRC_REG(optype
, i
,
374 machine
->Inputs
[ATI_FS_INPUT_PRIMARY
]);
375 else if (index
== GL_SECONDARY_INTERPOLATOR_ATI
)
376 SETUP_SRC_REG(optype
, i
,
377 machine
->Inputs
[ATI_FS_INPUT_SECONDARY
]);
379 apply_src_rep(optype
, inst
->SrcReg
[optype
][i
].argRep
,
381 apply_src_mod(optype
, inst
->SrcReg
[optype
][i
].argMod
,
386 /* Execute the operations - color then alpha */
387 for (optype
= 0; optype
< 2; optype
++) {
388 if (inst
->Opcode
[optype
]) {
389 switch (inst
->Opcode
[optype
]) {
392 for (i
= 0; i
< 3; i
++) {
394 src
[optype
][0][i
] + src
[optype
][1][i
];
397 dst
[optype
][3] = src
[optype
][0][3] + src
[optype
][1][3];
401 for (i
= 0; i
< 3; i
++) {
403 src
[optype
][0][i
] - src
[optype
][1][i
];
406 dst
[optype
][3] = src
[optype
][0][3] - src
[optype
][1][3];
410 for (i
= 0; i
< 3; i
++) {
412 src
[optype
][0][i
] * src
[optype
][1][i
];
415 dst
[optype
][3] = src
[optype
][0][3] * src
[optype
][1][3];
419 for (i
= 0; i
< 3; i
++) {
421 src
[optype
][0][i
] * src
[optype
][1][i
] +
426 src
[optype
][0][3] * src
[optype
][1][3] +
431 for (i
= 0; i
< 3; i
++) {
433 src
[optype
][0][i
] * src
[optype
][1][i
] + (1 -
441 src
[optype
][0][3] * src
[optype
][1][3] + (1 -
449 for (i
= 0; i
< 3; i
++) {
450 dst
[optype
][i
] = src
[optype
][0][i
];
453 dst
[optype
][3] = src
[optype
][0][3];
457 for (i
= 0; i
< 3; i
++) {
460 0.5) ? src
[optype
][0][i
] : src
[optype
][1][i
];
466 0.5) ? src
[optype
][0][3] : src
[optype
][1][3];
472 for (i
= 0; i
< 3; i
++) {
474 (src
[optype
][2][i
] >=
475 0) ? src
[optype
][0][i
] : src
[optype
][1][i
];
479 (src
[optype
][2][3] >=
480 0) ? src
[optype
][0][3] : src
[optype
][1][3];
483 case GL_DOT2_ADD_ATI
:
487 /* DOT 2 always uses the source from the color op */
488 /* could save recalculation of dot products for alpha inst */
489 result
= src
[0][0][0] * src
[0][1][0] +
490 src
[0][0][1] * src
[0][1][1] + src
[0][2][2];
492 for (i
= 0; i
< 3; i
++) {
493 dst
[optype
][i
] = result
;
497 dst
[optype
][3] = result
;
504 /* DOT 3 always uses the source from the color op */
505 result
= src
[0][0][0] * src
[0][1][0] +
506 src
[0][0][1] * src
[0][1][1] +
507 src
[0][0][2] * src
[0][1][2];
510 for (i
= 0; i
< 3; i
++) {
511 dst
[optype
][i
] = result
;
515 dst
[optype
][3] = result
;
522 /* DOT 4 always uses the source from the color op */
523 result
= src
[0][0][0] * src
[0][1][0] +
524 src
[0][0][1] * src
[0][1][1] +
525 src
[0][0][2] * src
[0][1][2] +
526 src
[0][0][3] * src
[0][1][3];
528 for (i
= 0; i
< 3; i
++) {
529 dst
[optype
][i
] = result
;
533 dst
[optype
][3] = result
;
541 /* write out the destination registers */
542 for (optype
= 0; optype
< 2; optype
++) {
543 if (inst
->Opcode
[optype
]) {
544 dstreg
= inst
->DstReg
[optype
].Index
;
545 dstp
= machine
->Registers
[dstreg
- GL_REG_0_ATI
];
547 if ((optype
== 0) || ((inst
->Opcode
[1] != GL_DOT2_ADD_ATI
) &&
548 (inst
->Opcode
[1] != GL_DOT3_ATI
) && (inst
->Opcode
[1] != GL_DOT4_ATI
)))
549 write_dst_addr(optype
, inst
->DstReg
[optype
].dstMod
,
550 inst
->DstReg
[optype
].dstMask
, dst
[optype
],
553 write_dst_addr(1, inst
->DstReg
[0].dstMod
, 0, dst
[1], dstp
);
562 * Init fragment shader virtual machine state.
565 init_machine(GLcontext
* ctx
, struct atifs_machine
*machine
,
566 const struct ati_fragment_shader
*shader
,
567 const SWspan
*span
, GLuint col
)
569 GLfloat (*inputs
)[4] = machine
->Inputs
;
572 for (i
= 0; i
< 6; i
++) {
573 for (j
= 0; j
< 4; j
++)
574 machine
->Registers
[i
][j
] = 0.0;
577 COPY_4V(inputs
[ATI_FS_INPUT_PRIMARY
], span
->array
->attribs
[FRAG_ATTRIB_COL0
][col
]);
578 COPY_4V(inputs
[ATI_FS_INPUT_SECONDARY
], span
->array
->attribs
[FRAG_ATTRIB_COL1
][col
]);
584 * Execute the current ATI shader program, operating on the given span.
587 _swrast_exec_fragment_shader(GLcontext
* ctx
, SWspan
*span
)
589 const struct ati_fragment_shader
*shader
= ctx
->ATIFragmentShader
.Current
;
590 struct atifs_machine machine
;
593 /* incoming colors should be floats */
594 ASSERT(span
->array
->ChanType
== GL_FLOAT
);
596 ctx
->_CurrentProgram
= GL_FRAGMENT_SHADER_ATI
;
598 for (i
= 0; i
< span
->end
; i
++) {
599 if (span
->array
->mask
[i
]) {
600 init_machine(ctx
, &machine
, shader
, span
, i
);
602 execute_shader(ctx
, shader
, &machine
, span
, i
);
604 /* store result color */
606 const GLfloat
*colOut
= machine
.Registers
[0];
607 /*fprintf(stderr,"outputs %f %f %f %f\n",
608 colOut[0], colOut[1], colOut[2], colOut[3]); */
609 COPY_4V(span
->array
->attribs
[FRAG_ATTRIB_COL0
][i
], colOut
);
614 ctx
->_CurrentProgram
= 0;