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/macros.h"
24 #include "main/atifragshader.h"
25 #include "main/samplerobj.h"
26 #include "swrast/s_atifragshader.h"
27 #include "swrast/s_context.h"
29 #define ATI_FS_INPUT_PRIMARY 0
30 #define ATI_FS_INPUT_SECONDARY 1
33 * State for executing ATI fragment shader.
37 GLfloat Registers
[6][4]; /** six temporary registers */
38 GLfloat PrevPassRegisters
[6][4];
39 GLfloat Inputs
[2][4]; /** Primary, secondary input colors */
48 fetch_texel(struct gl_context
* ctx
, const GLfloat texcoord
[4], GLfloat lambda
,
49 GLuint unit
, GLfloat color
[4])
51 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
53 /* XXX use a float-valued TextureSample routine here!!! */
54 swrast
->TextureSample
[unit
](ctx
, _mesa_get_samplerobj(ctx
, unit
),
55 ctx
->Texture
.Unit
[unit
]._Current
,
56 1, (const GLfloat(*)[4]) texcoord
,
57 &lambda
, (GLfloat (*)[4]) color
);
61 apply_swizzle(GLfloat values
[4], GLuint swizzle
)
71 case GL_SWIZZLE_STR_ATI
:
76 case GL_SWIZZLE_STQ_ATI
:
81 case GL_SWIZZLE_STR_DR_ATI
:
86 case GL_SWIZZLE_STQ_DQ_ATI
:
87 /* make sure q is not 0 to avoid problems later with infinite values (texture lookup)? */
99 apply_src_rep(GLint optype
, GLuint rep
, GLfloat
* val
)
106 start
= optype
? 3 : 0;
109 for (i
= start
; i
< end
; i
++) {
128 apply_src_mod(GLint optype
, GLuint mod
, GLfloat
* val
)
136 start
= optype
? 3 : 0;
139 for (i
= start
; i
< end
; i
++) {
140 if (mod
& GL_COMP_BIT_ATI
)
143 if (mod
& GL_BIAS_BIT_ATI
)
144 val
[i
] = val
[i
] - 0.5F
;
146 if (mod
& GL_2X_BIT_ATI
)
149 if (mod
& GL_NEGATE_BIT_ATI
)
155 apply_dst_mod(GLuint optype
, GLuint mod
, GLfloat
* val
)
158 GLint has_sat
= mod
& GL_SATURATE_BIT_ATI
;
161 mod
&= ~GL_SATURATE_BIT_ATI
;
163 start
= optype
? 3 : 0;
164 end
= optype
? 4 : 3;
166 for (i
= start
; i
< end
; i
++) {
177 case GL_HALF_BIT_ATI
:
178 val
[i
] = val
[i
] * 0.5F
;
180 case GL_QUARTER_BIT_ATI
:
181 val
[i
] = val
[i
] * 0.25F
;
183 case GL_EIGHTH_BIT_ATI
:
184 val
[i
] = val
[i
] * 0.125F
;
191 else if (val
[i
] > 1.0F
)
197 else if (val
[i
] > 8.0F
)
205 write_dst_addr(GLuint optype
, GLuint mod
, GLuint mask
, GLfloat
* src
,
209 apply_dst_mod(optype
, mod
, src
);
211 if (optype
== ATI_FRAGMENT_SHADER_COLOR_OP
) {
213 if (mask
& GL_RED_BIT_ATI
)
216 if (mask
& GL_GREEN_BIT_ATI
)
219 if (mask
& GL_BLUE_BIT_ATI
)
223 for (i
= 0; i
< 3; i
++)
232 finish_pass(struct atifs_machine
*machine
)
236 for (i
= 0; i
< 6; i
++) {
237 COPY_4V(machine
->PrevPassRegisters
[i
], machine
->Registers
[i
]);
243 handle_pass_op(struct atifs_machine
*machine
, struct atifs_setupinst
*texinst
,
244 const SWspan
*span
, GLuint column
, GLuint idx
)
246 GLuint swizzle
= texinst
->swizzle
;
247 GLuint pass_tex
= texinst
->src
;
249 if (pass_tex
>= GL_TEXTURE0_ARB
&& pass_tex
<= GL_TEXTURE7_ARB
) {
250 pass_tex
-= GL_TEXTURE0_ARB
;
251 COPY_4V(machine
->Registers
[idx
],
252 span
->array
->attribs
[VARYING_SLOT_TEX0
+ pass_tex
][column
]);
254 else if (pass_tex
>= GL_REG_0_ATI
&& pass_tex
<= GL_REG_5_ATI
) {
255 pass_tex
-= GL_REG_0_ATI
;
256 COPY_4V(machine
->Registers
[idx
], machine
->PrevPassRegisters
[pass_tex
]);
258 apply_swizzle(machine
->Registers
[idx
], swizzle
);
263 handle_sample_op(struct gl_context
* ctx
, struct atifs_machine
*machine
,
264 struct atifs_setupinst
*texinst
, const SWspan
*span
,
265 GLuint column
, GLuint idx
)
267 /* sample from unit idx using texinst->src as coords */
268 GLuint swizzle
= texinst
->swizzle
;
269 GLuint coord_source
= texinst
->src
;
270 GLfloat tex_coords
[4] = { 0 };
272 if (coord_source
>= GL_TEXTURE0_ARB
&& coord_source
<= GL_TEXTURE7_ARB
) {
273 coord_source
-= GL_TEXTURE0_ARB
;
275 span
->array
->attribs
[VARYING_SLOT_TEX0
+ coord_source
][column
]);
277 else if (coord_source
>= GL_REG_0_ATI
&& coord_source
<= GL_REG_5_ATI
) {
278 coord_source
-= GL_REG_0_ATI
;
279 COPY_4V(tex_coords
, machine
->PrevPassRegisters
[coord_source
]);
281 apply_swizzle(tex_coords
, swizzle
);
282 fetch_texel(ctx
, tex_coords
, 0.0F
, idx
, machine
->Registers
[idx
]);
285 #define SETUP_SRC_REG(optype, i, x) \
287 COPY_4V(src[optype][i], x); \
293 * Execute the given fragment shader.
294 * NOTE: we do everything in single-precision floating point
295 * \param ctx - rendering context
296 * \param shader - the shader to execute
297 * \param machine - virtual machine state
298 * \param span - the SWspan we're operating on
299 * \param column - which pixel [i] we're operating on in the span
302 execute_shader(struct gl_context
*ctx
, const struct ati_fragment_shader
*shader
,
303 struct atifs_machine
*machine
, const SWspan
*span
,
307 struct atifs_instruction
*inst
;
308 struct atifs_setupinst
*texinst
;
313 GLfloat src
[2][3][4];
314 GLfloat zeros
[4] = { 0.0, 0.0, 0.0, 0.0 };
315 GLfloat ones
[4] = { 1.0, 1.0, 1.0, 1.0 };
316 GLfloat dst
[2][4], *dstp
;
318 for (pass
= 0; pass
< shader
->NumPasses
; pass
++) {
320 finish_pass(machine
);
321 for (j
= 0; j
< MAX_NUM_FRAGMENT_REGISTERS_ATI
; j
++) {
322 texinst
= &shader
->SetupInst
[pass
][j
];
323 if (texinst
->Opcode
== ATI_FRAGMENT_SHADER_PASS_OP
)
324 handle_pass_op(machine
, texinst
, span
, column
, j
);
325 else if (texinst
->Opcode
== ATI_FRAGMENT_SHADER_SAMPLE_OP
)
326 handle_sample_op(ctx
, machine
, texinst
, span
, column
, j
);
329 for (pc
= 0; pc
< shader
->numArithInstr
[pass
]; pc
++) {
330 inst
= &shader
->Instructions
[pass
][pc
];
332 /* setup the source registers for color and alpha ops */
333 for (optype
= 0; optype
< 2; optype
++) {
334 for (i
= 0; i
< inst
->ArgCount
[optype
]; i
++) {
335 GLint index
= inst
->SrcReg
[optype
][i
].Index
;
337 if (index
>= GL_REG_0_ATI
&& index
<= GL_REG_5_ATI
)
338 SETUP_SRC_REG(optype
, i
,
339 machine
->Registers
[index
- GL_REG_0_ATI
]);
340 else if (index
>= GL_CON_0_ATI
&& index
<= GL_CON_7_ATI
) {
341 if (shader
->LocalConstDef
& (1 << (index
- GL_CON_0_ATI
))) {
342 SETUP_SRC_REG(optype
, i
,
343 shader
->Constants
[index
- GL_CON_0_ATI
]);
345 SETUP_SRC_REG(optype
, i
,
346 ctx
->ATIFragmentShader
.GlobalConstants
[index
- GL_CON_0_ATI
]);
349 else if (index
== GL_ONE
)
350 SETUP_SRC_REG(optype
, i
, ones
);
351 else if (index
== GL_ZERO
)
352 SETUP_SRC_REG(optype
, i
, zeros
);
353 else if (index
== GL_PRIMARY_COLOR_EXT
)
354 SETUP_SRC_REG(optype
, i
,
355 machine
->Inputs
[ATI_FS_INPUT_PRIMARY
]);
356 else if (index
== GL_SECONDARY_INTERPOLATOR_ATI
)
357 SETUP_SRC_REG(optype
, i
,
358 machine
->Inputs
[ATI_FS_INPUT_SECONDARY
]);
360 apply_src_rep(optype
, inst
->SrcReg
[optype
][i
].argRep
,
362 apply_src_mod(optype
, inst
->SrcReg
[optype
][i
].argMod
,
367 /* Execute the operations - color then alpha */
368 for (optype
= 0; optype
< 2; optype
++) {
369 if (inst
->Opcode
[optype
]) {
370 switch (inst
->Opcode
[optype
]) {
373 for (i
= 0; i
< 3; i
++) {
375 src
[optype
][0][i
] + src
[optype
][1][i
];
378 dst
[optype
][3] = src
[optype
][0][3] + src
[optype
][1][3];
382 for (i
= 0; i
< 3; i
++) {
384 src
[optype
][0][i
] - src
[optype
][1][i
];
387 dst
[optype
][3] = src
[optype
][0][3] - src
[optype
][1][3];
391 for (i
= 0; i
< 3; i
++) {
393 src
[optype
][0][i
] * src
[optype
][1][i
];
396 dst
[optype
][3] = src
[optype
][0][3] * src
[optype
][1][3];
400 for (i
= 0; i
< 3; i
++) {
402 src
[optype
][0][i
] * src
[optype
][1][i
] +
407 src
[optype
][0][3] * src
[optype
][1][3] +
412 for (i
= 0; i
< 3; i
++) {
414 src
[optype
][0][i
] * src
[optype
][1][i
] + (1 -
422 src
[optype
][0][3] * src
[optype
][1][3] + (1 -
430 for (i
= 0; i
< 3; i
++) {
431 dst
[optype
][i
] = src
[optype
][0][i
];
434 dst
[optype
][3] = src
[optype
][0][3];
438 for (i
= 0; i
< 3; i
++) {
441 0.5F
) ? src
[optype
][0][i
] : src
[optype
][1][i
];
447 0.5F
) ? src
[optype
][0][3] : src
[optype
][1][3];
453 for (i
= 0; i
< 3; i
++) {
455 (src
[optype
][2][i
] >=
456 0) ? src
[optype
][0][i
] : src
[optype
][1][i
];
460 (src
[optype
][2][3] >=
461 0) ? src
[optype
][0][3] : src
[optype
][1][3];
464 case GL_DOT2_ADD_ATI
:
468 /* DOT 2 always uses the source from the color op */
469 /* could save recalculation of dot products for alpha inst */
470 result
= src
[0][0][0] * src
[0][1][0] +
471 src
[0][0][1] * src
[0][1][1] + src
[0][2][2];
473 for (i
= 0; i
< 3; i
++) {
474 dst
[optype
][i
] = result
;
478 dst
[optype
][3] = result
;
485 /* DOT 3 always uses the source from the color op */
486 result
= src
[0][0][0] * src
[0][1][0] +
487 src
[0][0][1] * src
[0][1][1] +
488 src
[0][0][2] * src
[0][1][2];
491 for (i
= 0; i
< 3; i
++) {
492 dst
[optype
][i
] = result
;
496 dst
[optype
][3] = result
;
503 /* DOT 4 always uses the source from the color op */
504 result
= src
[0][0][0] * src
[0][1][0] +
505 src
[0][0][1] * src
[0][1][1] +
506 src
[0][0][2] * src
[0][1][2] +
507 src
[0][0][3] * src
[0][1][3];
509 for (i
= 0; i
< 3; i
++) {
510 dst
[optype
][i
] = result
;
514 dst
[optype
][3] = result
;
522 /* write out the destination registers */
523 for (optype
= 0; optype
< 2; optype
++) {
524 if (inst
->Opcode
[optype
]) {
525 dstreg
= inst
->DstReg
[optype
].Index
;
526 dstp
= machine
->Registers
[dstreg
- GL_REG_0_ATI
];
528 if ((optype
== 0) || ((inst
->Opcode
[1] != GL_DOT2_ADD_ATI
) &&
529 (inst
->Opcode
[1] != GL_DOT3_ATI
) && (inst
->Opcode
[1] != GL_DOT4_ATI
)))
530 write_dst_addr(optype
, inst
->DstReg
[optype
].dstMod
,
531 inst
->DstReg
[optype
].dstMask
, dst
[optype
],
534 write_dst_addr(1, inst
->DstReg
[0].dstMod
, 0, dst
[1], dstp
);
543 * Init fragment shader virtual machine state.
546 init_machine(struct gl_context
* ctx
, struct atifs_machine
*machine
,
547 const struct ati_fragment_shader
*shader
,
548 const SWspan
*span
, GLuint col
)
550 GLfloat (*inputs
)[4] = machine
->Inputs
;
553 for (i
= 0; i
< 6; i
++) {
554 for (j
= 0; j
< 4; j
++)
555 machine
->Registers
[i
][j
] = 0.0;
558 COPY_4V(inputs
[ATI_FS_INPUT_PRIMARY
], span
->array
->attribs
[VARYING_SLOT_COL0
][col
]);
559 COPY_4V(inputs
[ATI_FS_INPUT_SECONDARY
], span
->array
->attribs
[VARYING_SLOT_COL1
][col
]);
565 * Execute the current ATI shader program, operating on the given span.
568 _swrast_exec_fragment_shader(struct gl_context
* ctx
, SWspan
*span
)
570 const struct ati_fragment_shader
*shader
= ctx
->ATIFragmentShader
.Current
;
571 struct atifs_machine machine
;
574 /* incoming colors should be floats */
575 assert(span
->array
->ChanType
== GL_FLOAT
);
577 for (i
= 0; i
< span
->end
; i
++) {
578 if (span
->array
->mask
[i
]) {
579 init_machine(ctx
, &machine
, shader
, span
, i
);
581 execute_shader(ctx
, shader
, &machine
, span
, i
);
583 /* store result color */
585 const GLfloat
*colOut
= machine
.Registers
[0];
586 /*fprintf(stderr,"outputs %f %f %f %f\n",
587 colOut[0], colOut[1], colOut[2], colOut[3]); */
588 COPY_4V(span
->array
->attribs
[VARYING_SLOT_COL0
][i
], colOut
);