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"
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
, _mesa_get_samplerobj(ctx
, unit
),
53 ctx
->Texture
.Unit
[unit
]._Current
,
54 1, (const GLfloat(*)[4]) texcoord
,
55 &lambda
, (GLfloat (*)[4]) color
);
59 apply_swizzle(GLfloat values
[4], GLuint swizzle
)
69 case GL_SWIZZLE_STR_ATI
:
74 case GL_SWIZZLE_STQ_ATI
:
79 case GL_SWIZZLE_STR_DR_ATI
:
84 case GL_SWIZZLE_STQ_DQ_ATI
:
85 /* make sure q is not 0 to avoid problems later with infinite values (texture lookup)? */
97 apply_src_rep(GLint optype
, GLuint rep
, GLfloat
* val
)
104 start
= optype
? 3 : 0;
107 for (i
= start
; i
< end
; i
++) {
126 apply_src_mod(GLint optype
, GLuint mod
, GLfloat
* val
)
134 start
= optype
? 3 : 0;
137 for (i
= start
; i
< end
; i
++) {
138 if (mod
& GL_COMP_BIT_ATI
)
141 if (mod
& GL_BIAS_BIT_ATI
)
142 val
[i
] = val
[i
] - 0.5F
;
144 if (mod
& GL_2X_BIT_ATI
)
147 if (mod
& GL_NEGATE_BIT_ATI
)
153 apply_dst_mod(GLuint optype
, GLuint mod
, GLfloat
* val
)
156 GLint has_sat
= mod
& GL_SATURATE_BIT_ATI
;
159 mod
&= ~GL_SATURATE_BIT_ATI
;
161 start
= optype
? 3 : 0;
162 end
= optype
? 4 : 3;
164 for (i
= start
; i
< end
; i
++) {
175 case GL_HALF_BIT_ATI
:
176 val
[i
] = val
[i
] * 0.5F
;
178 case GL_QUARTER_BIT_ATI
:
179 val
[i
] = val
[i
] * 0.25F
;
181 case GL_EIGHTH_BIT_ATI
:
182 val
[i
] = val
[i
] * 0.125F
;
189 else if (val
[i
] > 1.0F
)
195 else if (val
[i
] > 8.0F
)
203 write_dst_addr(GLuint optype
, GLuint mod
, GLuint mask
, GLfloat
* src
,
207 apply_dst_mod(optype
, mod
, src
);
209 if (optype
== ATI_FRAGMENT_SHADER_COLOR_OP
) {
211 if (mask
& GL_RED_BIT_ATI
)
214 if (mask
& GL_GREEN_BIT_ATI
)
217 if (mask
& GL_BLUE_BIT_ATI
)
221 for (i
= 0; i
< 3; i
++)
230 finish_pass(struct atifs_machine
*machine
)
234 for (i
= 0; i
< 6; i
++) {
235 COPY_4V(machine
->PrevPassRegisters
[i
], machine
->Registers
[i
]);
241 handle_pass_op(struct atifs_machine
*machine
, struct atifs_setupinst
*texinst
,
242 const SWspan
*span
, GLuint column
, GLuint idx
)
244 GLuint swizzle
= texinst
->swizzle
;
245 GLuint pass_tex
= texinst
->src
;
247 if (pass_tex
>= GL_TEXTURE0_ARB
&& pass_tex
<= GL_TEXTURE7_ARB
) {
248 pass_tex
-= GL_TEXTURE0_ARB
;
249 COPY_4V(machine
->Registers
[idx
],
250 span
->array
->attribs
[VARYING_SLOT_TEX0
+ pass_tex
][column
]);
252 else if (pass_tex
>= GL_REG_0_ATI
&& pass_tex
<= GL_REG_5_ATI
) {
253 pass_tex
-= GL_REG_0_ATI
;
254 COPY_4V(machine
->Registers
[idx
], machine
->PrevPassRegisters
[pass_tex
]);
256 apply_swizzle(machine
->Registers
[idx
], swizzle
);
261 handle_sample_op(struct gl_context
* ctx
, struct atifs_machine
*machine
,
262 struct atifs_setupinst
*texinst
, const SWspan
*span
,
263 GLuint column
, GLuint idx
)
265 /* sample from unit idx using texinst->src as coords */
266 GLuint swizzle
= texinst
->swizzle
;
267 GLuint coord_source
= texinst
->src
;
268 GLfloat tex_coords
[4] = { 0 };
270 if (coord_source
>= GL_TEXTURE0_ARB
&& coord_source
<= GL_TEXTURE7_ARB
) {
271 coord_source
-= GL_TEXTURE0_ARB
;
273 span
->array
->attribs
[VARYING_SLOT_TEX0
+ coord_source
][column
]);
275 else if (coord_source
>= GL_REG_0_ATI
&& coord_source
<= GL_REG_5_ATI
) {
276 coord_source
-= GL_REG_0_ATI
;
277 COPY_4V(tex_coords
, machine
->PrevPassRegisters
[coord_source
]);
279 apply_swizzle(tex_coords
, swizzle
);
280 fetch_texel(ctx
, tex_coords
, 0.0F
, idx
, machine
->Registers
[idx
]);
283 #define SETUP_SRC_REG(optype, i, x) \
285 COPY_4V(src[optype][i], x); \
291 * Execute the given fragment shader.
292 * NOTE: we do everything in single-precision floating point
293 * \param ctx - rendering context
294 * \param shader - the shader to execute
295 * \param machine - virtual machine state
296 * \param span - the SWspan we're operating on
297 * \param column - which pixel [i] we're operating on in the span
300 execute_shader(struct gl_context
*ctx
, const struct ati_fragment_shader
*shader
,
301 struct atifs_machine
*machine
, const SWspan
*span
,
305 struct atifs_instruction
*inst
;
306 struct atifs_setupinst
*texinst
;
311 GLfloat src
[2][3][4];
312 GLfloat zeros
[4] = { 0.0, 0.0, 0.0, 0.0 };
313 GLfloat ones
[4] = { 1.0, 1.0, 1.0, 1.0 };
314 GLfloat dst
[2][4], *dstp
;
316 for (pass
= 0; pass
< shader
->NumPasses
; pass
++) {
318 finish_pass(machine
);
319 for (j
= 0; j
< MAX_NUM_FRAGMENT_REGISTERS_ATI
; j
++) {
320 texinst
= &shader
->SetupInst
[pass
][j
];
321 if (texinst
->Opcode
== ATI_FRAGMENT_SHADER_PASS_OP
)
322 handle_pass_op(machine
, texinst
, span
, column
, j
);
323 else if (texinst
->Opcode
== ATI_FRAGMENT_SHADER_SAMPLE_OP
)
324 handle_sample_op(ctx
, machine
, texinst
, span
, column
, j
);
327 for (pc
= 0; pc
< shader
->numArithInstr
[pass
]; pc
++) {
328 inst
= &shader
->Instructions
[pass
][pc
];
330 /* setup the source registers for color and alpha ops */
331 for (optype
= 0; optype
< 2; optype
++) {
332 for (i
= 0; i
< inst
->ArgCount
[optype
]; i
++) {
333 GLint index
= inst
->SrcReg
[optype
][i
].Index
;
335 if (index
>= GL_REG_0_ATI
&& index
<= GL_REG_5_ATI
)
336 SETUP_SRC_REG(optype
, i
,
337 machine
->Registers
[index
- GL_REG_0_ATI
]);
338 else if (index
>= GL_CON_0_ATI
&& index
<= GL_CON_7_ATI
) {
339 if (shader
->LocalConstDef
& (1 << (index
- GL_CON_0_ATI
))) {
340 SETUP_SRC_REG(optype
, i
,
341 shader
->Constants
[index
- GL_CON_0_ATI
]);
343 SETUP_SRC_REG(optype
, i
,
344 ctx
->ATIFragmentShader
.GlobalConstants
[index
- GL_CON_0_ATI
]);
347 else if (index
== GL_ONE
)
348 SETUP_SRC_REG(optype
, i
, ones
);
349 else if (index
== GL_ZERO
)
350 SETUP_SRC_REG(optype
, i
, zeros
);
351 else if (index
== GL_PRIMARY_COLOR_EXT
)
352 SETUP_SRC_REG(optype
, i
,
353 machine
->Inputs
[ATI_FS_INPUT_PRIMARY
]);
354 else if (index
== GL_SECONDARY_INTERPOLATOR_ATI
)
355 SETUP_SRC_REG(optype
, i
,
356 machine
->Inputs
[ATI_FS_INPUT_SECONDARY
]);
358 apply_src_rep(optype
, inst
->SrcReg
[optype
][i
].argRep
,
360 apply_src_mod(optype
, inst
->SrcReg
[optype
][i
].argMod
,
365 /* Execute the operations - color then alpha */
366 for (optype
= 0; optype
< 2; optype
++) {
367 if (inst
->Opcode
[optype
]) {
368 switch (inst
->Opcode
[optype
]) {
371 for (i
= 0; i
< 3; i
++) {
373 src
[optype
][0][i
] + src
[optype
][1][i
];
376 dst
[optype
][3] = src
[optype
][0][3] + src
[optype
][1][3];
380 for (i
= 0; i
< 3; i
++) {
382 src
[optype
][0][i
] - src
[optype
][1][i
];
385 dst
[optype
][3] = src
[optype
][0][3] - src
[optype
][1][3];
389 for (i
= 0; i
< 3; i
++) {
391 src
[optype
][0][i
] * src
[optype
][1][i
];
394 dst
[optype
][3] = src
[optype
][0][3] * src
[optype
][1][3];
398 for (i
= 0; i
< 3; i
++) {
400 src
[optype
][0][i
] * src
[optype
][1][i
] +
405 src
[optype
][0][3] * src
[optype
][1][3] +
410 for (i
= 0; i
< 3; i
++) {
412 src
[optype
][0][i
] * src
[optype
][1][i
] + (1 -
420 src
[optype
][0][3] * src
[optype
][1][3] + (1 -
428 for (i
= 0; i
< 3; i
++) {
429 dst
[optype
][i
] = src
[optype
][0][i
];
432 dst
[optype
][3] = src
[optype
][0][3];
436 for (i
= 0; i
< 3; i
++) {
439 0.5F
) ? src
[optype
][0][i
] : src
[optype
][1][i
];
445 0.5F
) ? src
[optype
][0][3] : src
[optype
][1][3];
451 for (i
= 0; i
< 3; i
++) {
453 (src
[optype
][2][i
] >=
454 0) ? src
[optype
][0][i
] : src
[optype
][1][i
];
458 (src
[optype
][2][3] >=
459 0) ? src
[optype
][0][3] : src
[optype
][1][3];
462 case GL_DOT2_ADD_ATI
:
466 /* DOT 2 always uses the source from the color op */
467 /* could save recalculation of dot products for alpha inst */
468 result
= src
[0][0][0] * src
[0][1][0] +
469 src
[0][0][1] * src
[0][1][1] + src
[0][2][2];
471 for (i
= 0; i
< 3; i
++) {
472 dst
[optype
][i
] = result
;
476 dst
[optype
][3] = result
;
483 /* DOT 3 always uses the source from the color op */
484 result
= src
[0][0][0] * src
[0][1][0] +
485 src
[0][0][1] * src
[0][1][1] +
486 src
[0][0][2] * src
[0][1][2];
489 for (i
= 0; i
< 3; i
++) {
490 dst
[optype
][i
] = result
;
494 dst
[optype
][3] = result
;
501 /* DOT 4 always uses the source from the color op */
502 result
= src
[0][0][0] * src
[0][1][0] +
503 src
[0][0][1] * src
[0][1][1] +
504 src
[0][0][2] * src
[0][1][2] +
505 src
[0][0][3] * src
[0][1][3];
507 for (i
= 0; i
< 3; i
++) {
508 dst
[optype
][i
] = result
;
512 dst
[optype
][3] = result
;
520 /* write out the destination registers */
521 for (optype
= 0; optype
< 2; optype
++) {
522 if (inst
->Opcode
[optype
]) {
523 dstreg
= inst
->DstReg
[optype
].Index
;
524 dstp
= machine
->Registers
[dstreg
- GL_REG_0_ATI
];
526 if ((optype
== 0) || ((inst
->Opcode
[1] != GL_DOT2_ADD_ATI
) &&
527 (inst
->Opcode
[1] != GL_DOT3_ATI
) && (inst
->Opcode
[1] != GL_DOT4_ATI
)))
528 write_dst_addr(optype
, inst
->DstReg
[optype
].dstMod
,
529 inst
->DstReg
[optype
].dstMask
, dst
[optype
],
532 write_dst_addr(1, inst
->DstReg
[0].dstMod
, 0, dst
[1], dstp
);
541 * Init fragment shader virtual machine state.
544 init_machine(struct gl_context
* ctx
, struct atifs_machine
*machine
,
545 const struct ati_fragment_shader
*shader
,
546 const SWspan
*span
, GLuint col
)
548 GLfloat (*inputs
)[4] = machine
->Inputs
;
551 for (i
= 0; i
< 6; i
++) {
552 for (j
= 0; j
< 4; j
++)
553 machine
->Registers
[i
][j
] = 0.0;
556 COPY_4V(inputs
[ATI_FS_INPUT_PRIMARY
], span
->array
->attribs
[VARYING_SLOT_COL0
][col
]);
557 COPY_4V(inputs
[ATI_FS_INPUT_SECONDARY
], span
->array
->attribs
[VARYING_SLOT_COL1
][col
]);
563 * Execute the current ATI shader program, operating on the given span.
566 _swrast_exec_fragment_shader(struct gl_context
* ctx
, SWspan
*span
)
568 const struct ati_fragment_shader
*shader
= ctx
->ATIFragmentShader
.Current
;
569 struct atifs_machine machine
;
572 /* incoming colors should be floats */
573 assert(span
->array
->ChanType
== GL_FLOAT
);
575 for (i
= 0; i
< span
->end
; i
++) {
576 if (span
->array
->mask
[i
]) {
577 init_machine(ctx
, &machine
, shader
, span
, i
);
579 execute_shader(ctx
, shader
, &machine
, span
, i
);
581 /* store result color */
583 const GLfloat
*colOut
= machine
.Registers
[0];
584 /*fprintf(stderr,"outputs %f %f %f %f\n",
585 colOut[0], colOut[1], colOut[2], colOut[3]); */
586 COPY_4V(span
->array
->attribs
[VARYING_SLOT_COL0
][i
], colOut
);