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"
31 #include "s_nvfragprog.h"
39 fetch_texel(GLcontext
* ctx
, const GLfloat texcoord
[4], GLfloat lambda
,
40 GLuint unit
, GLfloat color
[4])
43 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
45 /* XXX use a float-valued TextureSample routine here!!! */
46 swrast
->TextureSample
[unit
] (ctx
, unit
, ctx
->Texture
.Unit
[unit
]._Current
,
47 1, (const GLfloat(*)[4]) texcoord
,
49 color
[0] = CHAN_TO_FLOAT(rgba
[0]);
50 color
[1] = CHAN_TO_FLOAT(rgba
[1]);
51 color
[2] = CHAN_TO_FLOAT(rgba
[2]);
52 color
[3] = CHAN_TO_FLOAT(rgba
[3]);
56 apply_swizzle(GLfloat values
[4], GLuint swizzle
)
66 case GL_SWIZZLE_STR_ATI
:
71 case GL_SWIZZLE_STQ_ATI
:
76 case GL_SWIZZLE_STR_DR_ATI
:
81 case GL_SWIZZLE_STQ_DQ_ATI
:
82 /* make sure q is not 0 to avoid problems later with infinite values (texture lookup)? */
83 if (q
== 0.0F
) q
= 0.000000001;
93 apply_src_rep(GLint optype
, GLuint rep
, GLfloat
* val
)
100 start
= optype
? 3 : 0;
103 for (i
= start
; i
< end
; i
++) {
122 apply_src_mod(GLint optype
, GLuint mod
, GLfloat
* val
)
130 start
= optype
? 3 : 0;
133 for (i
= start
; i
< end
; i
++) {
134 if (mod
& GL_COMP_BIT_ATI
)
137 if (mod
& GL_BIAS_BIT_ATI
)
138 val
[i
] = val
[i
] - 0.5;
140 if (mod
& GL_2X_BIT_ATI
)
143 if (mod
& GL_NEGATE_BIT_ATI
)
149 apply_dst_mod(GLuint optype
, GLuint mod
, GLfloat
* val
)
152 GLint has_sat
= mod
& GL_SATURATE_BIT_ATI
;
155 mod
&= ~GL_SATURATE_BIT_ATI
;
157 start
= optype
? 3 : 0;
158 end
= optype
? 4 : 3;
160 for (i
= start
; i
< end
; i
++) {
171 case GL_HALF_BIT_ATI
:
172 val
[i
] = val
[i
] * 0.5;
174 case GL_QUARTER_BIT_ATI
:
175 val
[i
] = val
[i
] * 0.25;
177 case GL_EIGHTH_BIT_ATI
:
178 val
[i
] = val
[i
] * 0.125;
185 else if (val
[i
] > 1.0)
191 else if (val
[i
] > 8.0)
199 write_dst_addr(GLuint optype
, GLuint mod
, GLuint mask
, GLfloat
* src
,
203 apply_dst_mod(optype
, mod
, src
);
205 if (optype
== ATI_FRAGMENT_SHADER_COLOR_OP
) {
207 if (mask
& GL_RED_BIT_ATI
)
210 if (mask
& GL_GREEN_BIT_ATI
)
213 if (mask
& GL_BLUE_BIT_ATI
)
217 for (i
= 0; i
< 3; i
++)
226 finish_pass(struct atifs_machine
*machine
)
230 for (i
= 0; i
< 6; i
++) {
231 COPY_4V(machine
->PrevPassRegisters
[i
], machine
->Registers
[i
]);
236 * Execute the given fragment shader
237 * NOTE: we do everything in single-precision floating point; we don't
238 * currently observe the single/half/fixed-precision qualifiers.
239 * \param ctx - rendering context
240 * \param program - the fragment program to execute
241 * \param machine - machine state (register file)
242 * \param maxInst - max number of instructions to execute
243 * \return GL_TRUE if program completed or GL_FALSE if program executed KIL.
246 struct ati_fs_opcode_st ati_fs_opcodes
[] = {
255 {GL_DOT2_ADD_ATI
, 3},
263 handle_pass_op(struct atifs_machine
*machine
, struct atifs_setupinst
*texinst
,
264 const struct sw_span
*span
, GLuint column
, GLuint idx
)
266 GLuint swizzle
= texinst
->swizzle
;
267 GLuint pass_tex
= texinst
->src
;
269 if (pass_tex
>= GL_TEXTURE0_ARB
&& pass_tex
<= GL_TEXTURE7_ARB
) {
270 pass_tex
-= GL_TEXTURE0_ARB
;
271 COPY_4V(machine
->Registers
[idx
],
272 span
->array
->texcoords
[pass_tex
][column
]);
274 else if (pass_tex
>= GL_REG_0_ATI
&& pass_tex
<= GL_REG_5_ATI
) {
275 pass_tex
-= GL_REG_0_ATI
;
276 COPY_4V(machine
->Registers
[idx
], machine
->PrevPassRegisters
[pass_tex
]);
278 apply_swizzle(machine
->Registers
[idx
], swizzle
);
283 handle_sample_op(GLcontext
* ctx
, struct atifs_machine
*machine
,
284 struct atifs_setupinst
*texinst
, const struct sw_span
*span
,
285 GLuint column
, GLuint idx
)
287 /* sample from unit idx using texinst->src as coords */
288 GLuint swizzle
= texinst
->swizzle
;
289 GLuint coord_source
= texinst
->src
;
290 GLfloat tex_coords
[4];
292 if (coord_source
>= GL_TEXTURE0_ARB
&& coord_source
<= GL_TEXTURE7_ARB
) {
293 coord_source
-= GL_TEXTURE0_ARB
;
294 COPY_4V(tex_coords
, span
->array
->texcoords
[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); \
310 execute_shader(GLcontext
* ctx
,
311 const struct ati_fragment_shader
*shader
, GLuint maxInst
,
312 struct atifs_machine
*machine
, const struct sw_span
*span
,
316 struct atifs_instruction
*inst
;
317 struct atifs_setupinst
*texinst
;
321 GLfloat src
[2][3][4];
322 GLfloat zeros
[4] = { 0.0, 0.0, 0.0, 0.0 };
323 GLfloat ones
[4] = { 1.0, 1.0, 1.0, 1.0 };
324 GLfloat dst
[2][4], *dstp
;
326 for (pass
= 0; pass
< shader
->NumPasses
; pass
++) {
328 finish_pass(machine
);
329 for (j
= 0; j
< MAX_NUM_FRAGMENT_REGISTERS_ATI
; j
++) {
330 texinst
= &shader
->SetupInst
[pass
][j
];
331 if (texinst
->Opcode
== ATI_FRAGMENT_SHADER_PASS_OP
)
332 handle_pass_op(machine
, texinst
, span
, column
, j
);
333 else if (texinst
->Opcode
== ATI_FRAGMENT_SHADER_SAMPLE_OP
)
334 handle_sample_op(ctx
, machine
, texinst
, span
, column
, j
);
337 for (pc
= 0; pc
< shader
->numArithInstr
[pass
]; pc
++) {
338 inst
= &shader
->Instructions
[pass
][pc
];
340 /* setup the source registers for color and alpha ops */
341 for (optype
= 0; optype
< 2; optype
++) {
342 for (i
= 0; i
< inst
->ArgCount
[optype
]; i
++) {
343 GLint index
= inst
->SrcReg
[optype
][i
].Index
;
345 if (index
>= GL_REG_0_ATI
&& index
<= GL_REG_5_ATI
)
346 SETUP_SRC_REG(optype
, i
,
347 machine
->Registers
[index
- GL_REG_0_ATI
]);
348 else if (index
>= GL_CON_0_ATI
&& index
<= GL_CON_7_ATI
) {
349 if (shader
->localConstDef
& (1 << (index
- GL_CON_0_ATI
))) {
350 SETUP_SRC_REG(optype
, i
,
351 shader
->Constants
[index
- GL_CON_0_ATI
]);
353 SETUP_SRC_REG(optype
, i
,
354 ctx
->ATIFragmentShader
.globalConstants
[index
- GL_CON_0_ATI
]);
357 else if (index
== GL_ONE
)
358 SETUP_SRC_REG(optype
, i
, ones
);
359 else if (index
== GL_ZERO
)
360 SETUP_SRC_REG(optype
, i
, zeros
);
361 else if (index
== GL_PRIMARY_COLOR_EXT
)
362 SETUP_SRC_REG(optype
, i
,
363 machine
->Inputs
[ATI_FS_INPUT_PRIMARY
]);
364 else if (index
== GL_SECONDARY_INTERPOLATOR_ATI
)
365 SETUP_SRC_REG(optype
, i
,
366 machine
->Inputs
[ATI_FS_INPUT_SECONDARY
]);
368 apply_src_rep(optype
, inst
->SrcReg
[optype
][i
].argRep
,
370 apply_src_mod(optype
, inst
->SrcReg
[optype
][i
].argMod
,
375 /* Execute the operations - color then alpha */
376 for (optype
= 0; optype
< 2; optype
++) {
377 if (inst
->Opcode
[optype
]) {
378 switch (inst
->Opcode
[optype
]) {
381 for (i
= 0; i
< 3; i
++) {
383 src
[optype
][0][i
] + src
[optype
][1][i
];
386 dst
[optype
][3] = src
[optype
][0][3] + src
[optype
][1][3];
390 for (i
= 0; i
< 3; i
++) {
392 src
[optype
][0][i
] - src
[optype
][1][i
];
395 dst
[optype
][3] = src
[optype
][0][3] - src
[optype
][1][3];
399 for (i
= 0; i
< 3; i
++) {
401 src
[optype
][0][i
] * src
[optype
][1][i
];
404 dst
[optype
][3] = src
[optype
][0][3] * src
[optype
][1][3];
408 for (i
= 0; i
< 3; i
++) {
410 src
[optype
][0][i
] * src
[optype
][1][i
] +
415 src
[optype
][0][3] * src
[optype
][1][3] +
420 for (i
= 0; i
< 3; i
++) {
422 src
[optype
][0][i
] * src
[optype
][1][i
] + (1 -
430 src
[optype
][0][3] * src
[optype
][1][3] + (1 -
438 for (i
= 0; i
< 3; i
++) {
439 dst
[optype
][i
] = src
[optype
][0][i
];
442 dst
[optype
][3] = src
[optype
][0][3];
446 for (i
= 0; i
< 3; i
++) {
449 0.5) ? src
[optype
][0][i
] : src
[optype
][1][i
];
455 0.5) ? src
[optype
][0][3] : src
[optype
][1][3];
461 for (i
= 0; i
< 3; i
++) {
463 (src
[optype
][2][i
] >=
464 0) ? src
[optype
][0][i
] : src
[optype
][1][i
];
468 (src
[optype
][2][3] >=
469 0) ? src
[optype
][0][3] : src
[optype
][1][3];
472 case GL_DOT2_ADD_ATI
:
476 /* DOT 2 always uses the source from the color op */
477 /* could save recalculation of dot products for alpha inst */
478 result
= src
[0][0][0] * src
[0][1][0] +
479 src
[0][0][1] * src
[0][1][1] + src
[0][2][2];
481 for (i
= 0; i
< 3; i
++) {
482 dst
[optype
][i
] = result
;
486 dst
[optype
][3] = result
;
493 /* DOT 3 always uses the source from the color op */
494 result
= src
[0][0][0] * src
[0][1][0] +
495 src
[0][0][1] * src
[0][1][1] +
496 src
[0][0][2] * src
[0][1][2];
499 for (i
= 0; i
< 3; i
++) {
500 dst
[optype
][i
] = result
;
504 dst
[optype
][3] = result
;
511 /* DOT 4 always uses the source from the color op */
512 result
= src
[0][0][0] * src
[0][1][0] +
513 src
[0][0][1] * src
[0][1][1] +
514 src
[0][0][2] * src
[0][1][2] +
515 src
[0][0][3] * src
[0][1][3];
517 for (i
= 0; i
< 3; i
++) {
518 dst
[optype
][i
] = result
;
522 dst
[optype
][3] = result
;
530 /* write out the destination registers */
531 for (optype
= 0; optype
< 2; optype
++) {
532 if (inst
->Opcode
[optype
]) {
533 dstreg
= inst
->DstReg
[optype
].Index
;
534 dstp
= machine
->Registers
[dstreg
- GL_REG_0_ATI
];
536 if ((optype
== 0) || ((inst
->Opcode
[1] != GL_DOT2_ADD_ATI
) &&
537 (inst
->Opcode
[1] != GL_DOT3_ATI
) && (inst
->Opcode
[1] != GL_DOT4_ATI
)))
538 write_dst_addr(optype
, inst
->DstReg
[optype
].dstMod
,
539 inst
->DstReg
[optype
].dstMask
, dst
[optype
],
542 write_dst_addr(1, inst
->DstReg
[0].dstMod
, 0, dst
[1], dstp
);
551 init_machine(GLcontext
* ctx
, struct atifs_machine
*machine
,
552 const struct ati_fragment_shader
*shader
,
553 const struct sw_span
*span
, GLuint col
)
557 for (i
= 0; i
< 6; i
++) {
558 for (j
= 0; j
< 4; j
++)
559 ctx
->ATIFragmentShader
.Machine
.Registers
[i
][j
] = 0.0;
563 ctx
->ATIFragmentShader
.Machine
.Inputs
[ATI_FS_INPUT_PRIMARY
][0] =
564 CHAN_TO_FLOAT(span
->array
->rgba
[col
][0]);
565 ctx
->ATIFragmentShader
.Machine
.Inputs
[ATI_FS_INPUT_PRIMARY
][1] =
566 CHAN_TO_FLOAT(span
->array
->rgba
[col
][1]);
567 ctx
->ATIFragmentShader
.Machine
.Inputs
[ATI_FS_INPUT_PRIMARY
][2] =
568 CHAN_TO_FLOAT(span
->array
->rgba
[col
][2]);
569 ctx
->ATIFragmentShader
.Machine
.Inputs
[ATI_FS_INPUT_PRIMARY
][3] =
570 CHAN_TO_FLOAT(span
->array
->rgba
[col
][3]);
572 ctx
->ATIFragmentShader
.Machine
.Inputs
[ATI_FS_INPUT_SECONDARY
][0] =
573 CHAN_TO_FLOAT(span
->array
->spec
[col
][0]);
574 ctx
->ATIFragmentShader
.Machine
.Inputs
[ATI_FS_INPUT_SECONDARY
][1] =
575 CHAN_TO_FLOAT(span
->array
->spec
[col
][1]);
576 ctx
->ATIFragmentShader
.Machine
.Inputs
[ATI_FS_INPUT_SECONDARY
][2] =
577 CHAN_TO_FLOAT(span
->array
->spec
[col
][2]);
578 ctx
->ATIFragmentShader
.Machine
.Inputs
[ATI_FS_INPUT_SECONDARY
][3] =
579 CHAN_TO_FLOAT(span
->array
->spec
[col
][3]);
581 ctx
->ATIFragmentShader
.Machine
.pass
= 0;
587 * Execute the current fragment program, operating on the given span.
590 _swrast_exec_fragment_shader(GLcontext
* ctx
, struct sw_span
*span
)
592 const struct ati_fragment_shader
*shader
= ctx
->ATIFragmentShader
.Current
;
595 ctx
->_CurrentProgram
= GL_FRAGMENT_SHADER_ATI
;
597 for (i
= 0; i
< span
->end
; i
++) {
598 if (span
->array
->mask
[i
]) {
599 init_machine(ctx
, &ctx
->ATIFragmentShader
.Machine
,
600 ctx
->ATIFragmentShader
.Current
, span
, i
);
602 if (execute_shader(ctx
, shader
, ~0,
603 &ctx
->ATIFragmentShader
.Machine
, span
, i
)) {
604 span
->array
->mask
[i
] = GL_FALSE
;
608 const GLfloat
*colOut
=
609 ctx
->ATIFragmentShader
.Machine
.Registers
[0];
611 /*fprintf(stderr,"outputs %f %f %f %f\n", colOut[0], colOut[1], colOut[2], colOut[3]); */
612 UNCLAMPED_FLOAT_TO_CHAN(span
->array
->rgba
[i
][RCOMP
], colOut
[0]);
613 UNCLAMPED_FLOAT_TO_CHAN(span
->array
->rgba
[i
][GCOMP
], colOut
[1]);
614 UNCLAMPED_FLOAT_TO_CHAN(span
->array
->rgba
[i
][BCOMP
], colOut
[2]);
615 UNCLAMPED_FLOAT_TO_CHAN(span
->array
->rgba
[i
][ACOMP
], colOut
[3]);
622 ctx
->_CurrentProgram
= 0;