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 "main/samplerobj.h"
27 #include "swrast/s_atifragshader.h"
28 #include "swrast/s_context.h"
32 * State for executing ATI fragment shader.
36 GLfloat Registers
[6][4]; /** six temporary registers */
37 GLfloat PrevPassRegisters
[6][4];
38 GLfloat Inputs
[2][4]; /** Primary, secondary input colors */
47 fetch_texel(struct gl_context
* ctx
, const GLfloat texcoord
[4], GLfloat lambda
,
48 GLuint unit
, GLfloat color
[4])
50 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
52 /* XXX use a float-valued TextureSample routine here!!! */
53 swrast
->TextureSample
[unit
](ctx
, _mesa_get_samplerobj(ctx
, unit
),
54 ctx
->Texture
.Unit
[unit
]._Current
,
55 1, (const GLfloat(*)[4]) texcoord
,
56 &lambda
, (GLfloat (*)[4]) color
);
60 apply_swizzle(GLfloat values
[4], GLuint swizzle
)
70 case GL_SWIZZLE_STR_ATI
:
75 case GL_SWIZZLE_STQ_ATI
:
80 case GL_SWIZZLE_STR_DR_ATI
:
85 case GL_SWIZZLE_STQ_DQ_ATI
:
86 /* make sure q is not 0 to avoid problems later with infinite values (texture lookup)? */
98 apply_src_rep(GLint optype
, GLuint rep
, GLfloat
* val
)
105 start
= optype
? 3 : 0;
108 for (i
= start
; i
< end
; i
++) {
127 apply_src_mod(GLint optype
, GLuint mod
, GLfloat
* val
)
135 start
= optype
? 3 : 0;
138 for (i
= start
; i
< end
; i
++) {
139 if (mod
& GL_COMP_BIT_ATI
)
142 if (mod
& GL_BIAS_BIT_ATI
)
143 val
[i
] = val
[i
] - 0.5F
;
145 if (mod
& GL_2X_BIT_ATI
)
148 if (mod
& GL_NEGATE_BIT_ATI
)
154 apply_dst_mod(GLuint optype
, GLuint mod
, GLfloat
* val
)
157 GLint has_sat
= mod
& GL_SATURATE_BIT_ATI
;
160 mod
&= ~GL_SATURATE_BIT_ATI
;
162 start
= optype
? 3 : 0;
163 end
= optype
? 4 : 3;
165 for (i
= start
; i
< end
; i
++) {
176 case GL_HALF_BIT_ATI
:
177 val
[i
] = val
[i
] * 0.5F
;
179 case GL_QUARTER_BIT_ATI
:
180 val
[i
] = val
[i
] * 0.25F
;
182 case GL_EIGHTH_BIT_ATI
:
183 val
[i
] = val
[i
] * 0.125F
;
190 else if (val
[i
] > 1.0F
)
196 else if (val
[i
] > 8.0F
)
204 write_dst_addr(GLuint optype
, GLuint mod
, GLuint mask
, GLfloat
* src
,
208 apply_dst_mod(optype
, mod
, src
);
210 if (optype
== ATI_FRAGMENT_SHADER_COLOR_OP
) {
212 if (mask
& GL_RED_BIT_ATI
)
215 if (mask
& GL_GREEN_BIT_ATI
)
218 if (mask
& GL_BLUE_BIT_ATI
)
222 for (i
= 0; i
< 3; i
++)
231 finish_pass(struct atifs_machine
*machine
)
235 for (i
= 0; i
< 6; i
++) {
236 COPY_4V(machine
->PrevPassRegisters
[i
], machine
->Registers
[i
]);
242 handle_pass_op(struct atifs_machine
*machine
, struct atifs_setupinst
*texinst
,
243 const SWspan
*span
, GLuint column
, GLuint idx
)
245 GLuint swizzle
= texinst
->swizzle
;
246 GLuint pass_tex
= texinst
->src
;
248 if (pass_tex
>= GL_TEXTURE0_ARB
&& pass_tex
<= GL_TEXTURE7_ARB
) {
249 pass_tex
-= GL_TEXTURE0_ARB
;
250 COPY_4V(machine
->Registers
[idx
],
251 span
->array
->attribs
[FRAG_ATTRIB_TEX0
+ pass_tex
][column
]);
253 else if (pass_tex
>= GL_REG_0_ATI
&& pass_tex
<= GL_REG_5_ATI
) {
254 pass_tex
-= GL_REG_0_ATI
;
255 COPY_4V(machine
->Registers
[idx
], machine
->PrevPassRegisters
[pass_tex
]);
257 apply_swizzle(machine
->Registers
[idx
], swizzle
);
262 handle_sample_op(struct gl_context
* ctx
, struct atifs_machine
*machine
,
263 struct atifs_setupinst
*texinst
, const SWspan
*span
,
264 GLuint column
, GLuint idx
)
266 /* sample from unit idx using texinst->src as coords */
267 GLuint swizzle
= texinst
->swizzle
;
268 GLuint coord_source
= texinst
->src
;
269 GLfloat tex_coords
[4] = { 0 };
271 if (coord_source
>= GL_TEXTURE0_ARB
&& coord_source
<= GL_TEXTURE7_ARB
) {
272 coord_source
-= GL_TEXTURE0_ARB
;
274 span
->array
->attribs
[FRAG_ATTRIB_TEX0
+ coord_source
][column
]);
276 else if (coord_source
>= GL_REG_0_ATI
&& coord_source
<= GL_REG_5_ATI
) {
277 coord_source
-= GL_REG_0_ATI
;
278 COPY_4V(tex_coords
, machine
->PrevPassRegisters
[coord_source
]);
280 apply_swizzle(tex_coords
, swizzle
);
281 fetch_texel(ctx
, tex_coords
, 0.0F
, idx
, machine
->Registers
[idx
]);
284 #define SETUP_SRC_REG(optype, i, x) \
286 COPY_4V(src[optype][i], x); \
292 * Execute the given fragment shader.
293 * NOTE: we do everything in single-precision floating point
294 * \param ctx - rendering context
295 * \param shader - the shader to execute
296 * \param machine - virtual machine state
297 * \param span - the SWspan we're operating on
298 * \param column - which pixel [i] we're operating on in the span
301 execute_shader(struct gl_context
*ctx
, const struct ati_fragment_shader
*shader
,
302 struct atifs_machine
*machine
, const SWspan
*span
,
306 struct atifs_instruction
*inst
;
307 struct atifs_setupinst
*texinst
;
312 GLfloat src
[2][3][4];
313 GLfloat zeros
[4] = { 0.0, 0.0, 0.0, 0.0 };
314 GLfloat ones
[4] = { 1.0, 1.0, 1.0, 1.0 };
315 GLfloat dst
[2][4], *dstp
;
317 for (pass
= 0; pass
< shader
->NumPasses
; pass
++) {
319 finish_pass(machine
);
320 for (j
= 0; j
< MAX_NUM_FRAGMENT_REGISTERS_ATI
; j
++) {
321 texinst
= &shader
->SetupInst
[pass
][j
];
322 if (texinst
->Opcode
== ATI_FRAGMENT_SHADER_PASS_OP
)
323 handle_pass_op(machine
, texinst
, span
, column
, j
);
324 else if (texinst
->Opcode
== ATI_FRAGMENT_SHADER_SAMPLE_OP
)
325 handle_sample_op(ctx
, machine
, texinst
, span
, column
, j
);
328 for (pc
= 0; pc
< shader
->numArithInstr
[pass
]; pc
++) {
329 inst
= &shader
->Instructions
[pass
][pc
];
331 /* setup the source registers for color and alpha ops */
332 for (optype
= 0; optype
< 2; optype
++) {
333 for (i
= 0; i
< inst
->ArgCount
[optype
]; i
++) {
334 GLint index
= inst
->SrcReg
[optype
][i
].Index
;
336 if (index
>= GL_REG_0_ATI
&& index
<= GL_REG_5_ATI
)
337 SETUP_SRC_REG(optype
, i
,
338 machine
->Registers
[index
- GL_REG_0_ATI
]);
339 else if (index
>= GL_CON_0_ATI
&& index
<= GL_CON_7_ATI
) {
340 if (shader
->LocalConstDef
& (1 << (index
- GL_CON_0_ATI
))) {
341 SETUP_SRC_REG(optype
, i
,
342 shader
->Constants
[index
- GL_CON_0_ATI
]);
344 SETUP_SRC_REG(optype
, i
,
345 ctx
->ATIFragmentShader
.GlobalConstants
[index
- GL_CON_0_ATI
]);
348 else if (index
== GL_ONE
)
349 SETUP_SRC_REG(optype
, i
, ones
);
350 else if (index
== GL_ZERO
)
351 SETUP_SRC_REG(optype
, i
, zeros
);
352 else if (index
== GL_PRIMARY_COLOR_EXT
)
353 SETUP_SRC_REG(optype
, i
,
354 machine
->Inputs
[ATI_FS_INPUT_PRIMARY
]);
355 else if (index
== GL_SECONDARY_INTERPOLATOR_ATI
)
356 SETUP_SRC_REG(optype
, i
,
357 machine
->Inputs
[ATI_FS_INPUT_SECONDARY
]);
359 apply_src_rep(optype
, inst
->SrcReg
[optype
][i
].argRep
,
361 apply_src_mod(optype
, inst
->SrcReg
[optype
][i
].argMod
,
366 /* Execute the operations - color then alpha */
367 for (optype
= 0; optype
< 2; optype
++) {
368 if (inst
->Opcode
[optype
]) {
369 switch (inst
->Opcode
[optype
]) {
372 for (i
= 0; i
< 3; i
++) {
374 src
[optype
][0][i
] + src
[optype
][1][i
];
377 dst
[optype
][3] = src
[optype
][0][3] + src
[optype
][1][3];
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
] +
406 src
[optype
][0][3] * src
[optype
][1][3] +
411 for (i
= 0; i
< 3; i
++) {
413 src
[optype
][0][i
] * src
[optype
][1][i
] + (1 -
421 src
[optype
][0][3] * src
[optype
][1][3] + (1 -
429 for (i
= 0; i
< 3; i
++) {
430 dst
[optype
][i
] = src
[optype
][0][i
];
433 dst
[optype
][3] = src
[optype
][0][3];
437 for (i
= 0; i
< 3; i
++) {
440 0.5) ? src
[optype
][0][i
] : src
[optype
][1][i
];
446 0.5) ? src
[optype
][0][3] : src
[optype
][1][3];
452 for (i
= 0; i
< 3; i
++) {
454 (src
[optype
][2][i
] >=
455 0) ? src
[optype
][0][i
] : src
[optype
][1][i
];
459 (src
[optype
][2][3] >=
460 0) ? src
[optype
][0][3] : src
[optype
][1][3];
463 case GL_DOT2_ADD_ATI
:
467 /* DOT 2 always uses the source from the color op */
468 /* could save recalculation of dot products for alpha inst */
469 result
= src
[0][0][0] * src
[0][1][0] +
470 src
[0][0][1] * src
[0][1][1] + src
[0][2][2];
472 for (i
= 0; i
< 3; i
++) {
473 dst
[optype
][i
] = result
;
477 dst
[optype
][3] = result
;
484 /* DOT 3 always uses the source from the color op */
485 result
= src
[0][0][0] * src
[0][1][0] +
486 src
[0][0][1] * src
[0][1][1] +
487 src
[0][0][2] * src
[0][1][2];
490 for (i
= 0; i
< 3; i
++) {
491 dst
[optype
][i
] = result
;
495 dst
[optype
][3] = result
;
502 /* DOT 4 always uses the source from the color op */
503 result
= src
[0][0][0] * src
[0][1][0] +
504 src
[0][0][1] * src
[0][1][1] +
505 src
[0][0][2] * src
[0][1][2] +
506 src
[0][0][3] * src
[0][1][3];
508 for (i
= 0; i
< 3; i
++) {
509 dst
[optype
][i
] = result
;
513 dst
[optype
][3] = result
;
521 /* write out the destination registers */
522 for (optype
= 0; optype
< 2; optype
++) {
523 if (inst
->Opcode
[optype
]) {
524 dstreg
= inst
->DstReg
[optype
].Index
;
525 dstp
= machine
->Registers
[dstreg
- GL_REG_0_ATI
];
527 if ((optype
== 0) || ((inst
->Opcode
[1] != GL_DOT2_ADD_ATI
) &&
528 (inst
->Opcode
[1] != GL_DOT3_ATI
) && (inst
->Opcode
[1] != GL_DOT4_ATI
)))
529 write_dst_addr(optype
, inst
->DstReg
[optype
].dstMod
,
530 inst
->DstReg
[optype
].dstMask
, dst
[optype
],
533 write_dst_addr(1, inst
->DstReg
[0].dstMod
, 0, dst
[1], dstp
);
542 * Init fragment shader virtual machine state.
545 init_machine(struct gl_context
* ctx
, struct atifs_machine
*machine
,
546 const struct ati_fragment_shader
*shader
,
547 const SWspan
*span
, GLuint col
)
549 GLfloat (*inputs
)[4] = machine
->Inputs
;
552 for (i
= 0; i
< 6; i
++) {
553 for (j
= 0; j
< 4; j
++)
554 machine
->Registers
[i
][j
] = 0.0;
557 COPY_4V(inputs
[ATI_FS_INPUT_PRIMARY
], span
->array
->attribs
[FRAG_ATTRIB_COL0
][col
]);
558 COPY_4V(inputs
[ATI_FS_INPUT_SECONDARY
], span
->array
->attribs
[FRAG_ATTRIB_COL1
][col
]);
564 * Execute the current ATI shader program, operating on the given span.
567 _swrast_exec_fragment_shader(struct gl_context
* ctx
, SWspan
*span
)
569 const struct ati_fragment_shader
*shader
= ctx
->ATIFragmentShader
.Current
;
570 struct atifs_machine machine
;
573 /* incoming colors should be floats */
574 ASSERT(span
->array
->ChanType
== GL_FLOAT
);
576 for (i
= 0; i
< span
->end
; i
++) {
577 if (span
->array
->mask
[i
]) {
578 init_machine(ctx
, &machine
, shader
, span
, i
);
580 execute_shader(ctx
, shader
, &machine
, span
, i
);
582 /* store result color */
584 const GLfloat
*colOut
= machine
.Registers
[0];
585 /*fprintf(stderr,"outputs %f %f %f %f\n",
586 colOut[0], colOut[1], colOut[2], colOut[3]); */
587 COPY_4V(span
->array
->attribs
[FRAG_ATTRIB_COL0
][i
], colOut
);