2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
27 #include "main/glheader.h"
28 #include "main/context.h"
29 #include "main/colormac.h"
30 #include "main/imports.h"
31 #include "main/pixeltransfer.h"
32 #include "main/samplerobj.h"
33 #include "program/prog_instruction.h"
35 #include "s_context.h"
36 #include "s_texcombine.h"
40 * Pointer to array of float[4]
41 * This type makes the code below more concise and avoids a lot of casting.
43 typedef float (*float4_array
)[4];
47 * Return array of texels for given unit.
49 static inline float4_array
50 get_texel_array(SWcontext
*swrast
, GLuint unit
)
53 return (float4_array
) (swrast
->TexelBuffer
+ unit
* SWRAST_MAX_WIDTH
* 4 * omp_get_num_threads() + (SWRAST_MAX_WIDTH
* 4 * omp_get_thread_num()));
55 return (float4_array
) (swrast
->TexelBuffer
+ unit
* SWRAST_MAX_WIDTH
* 4);
62 * Do texture application for:
63 * GL_EXT_texture_env_combine
64 * GL_ARB_texture_env_combine
65 * GL_EXT_texture_env_dot3
66 * GL_ARB_texture_env_dot3
67 * GL_ATI_texture_env_combine3
68 * GL_NV_texture_env_combine4
69 * conventional GL texture env modes
71 * \param ctx rendering context
72 * \param unit the texture combiner unit
73 * \param primary_rgba incoming fragment color array
74 * \param texelBuffer pointer to texel colors for all texture units
76 * \param span two fields are used in this function:
77 * span->end: number of fragments to process
78 * span->array->rgba: incoming/result fragment colors
81 texture_combine( struct gl_context
*ctx
, GLuint unit
,
82 const float4_array primary_rgba
,
83 const GLfloat
*texelBuffer
,
86 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
87 const struct gl_texture_unit
*textureUnit
= &(ctx
->Texture
.Unit
[unit
]);
88 const struct gl_tex_env_combine_state
*combine
= textureUnit
->_CurrentCombine
;
89 float4_array argRGB
[MAX_COMBINER_TERMS
];
90 float4_array argA
[MAX_COMBINER_TERMS
];
91 const GLfloat scaleRGB
= (GLfloat
) (1 << combine
->ScaleShiftRGB
);
92 const GLfloat scaleA
= (GLfloat
) (1 << combine
->ScaleShiftA
);
93 const GLuint numArgsRGB
= combine
->_NumArgsRGB
;
94 const GLuint numArgsA
= combine
->_NumArgsA
;
95 float4_array ccolor
[4], rgba
;
98 GLchan (*rgbaChan
)[4] = span
->array
->rgba
;
100 /* alloc temp pixel buffers */
101 rgba
= malloc(4 * n
* sizeof(GLfloat
));
103 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "texture_combine");
107 for (i
= 0; i
< numArgsRGB
|| i
< numArgsA
; i
++) {
108 ccolor
[i
] = malloc(4 * n
* sizeof(GLfloat
));
114 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "texture_combine");
120 for (i
= 0; i
< n
; i
++) {
121 rgba
[i
][RCOMP
] = CHAN_TO_FLOAT(rgbaChan
[i
][RCOMP
]);
122 rgba
[i
][GCOMP
] = CHAN_TO_FLOAT(rgbaChan
[i
][GCOMP
]);
123 rgba
[i
][BCOMP
] = CHAN_TO_FLOAT(rgbaChan
[i
][BCOMP
]);
124 rgba
[i
][ACOMP
] = CHAN_TO_FLOAT(rgbaChan
[i
][ACOMP
]);
128 printf("modeRGB 0x%x modeA 0x%x srcRGB1 0x%x srcA1 0x%x srcRGB2 0x%x srcA2 0x%x\n",
131 combine->SourceRGB[0],
133 combine->SourceRGB[1],
134 combine->SourceA[1]);
138 * Do operand setup for up to 4 operands. Loop over the terms.
140 for (term
= 0; term
< numArgsRGB
; term
++) {
141 const GLenum srcRGB
= combine
->SourceRGB
[term
];
142 const GLenum operandRGB
= combine
->OperandRGB
[term
];
146 argRGB
[term
] = get_texel_array(swrast
, unit
);
148 case GL_PRIMARY_COLOR
:
149 argRGB
[term
] = primary_rgba
;
156 float4_array c
= ccolor
[term
];
157 GLfloat red
= textureUnit
->EnvColor
[0];
158 GLfloat green
= textureUnit
->EnvColor
[1];
159 GLfloat blue
= textureUnit
->EnvColor
[2];
160 GLfloat alpha
= textureUnit
->EnvColor
[3];
161 for (i
= 0; i
< n
; i
++) {
162 ASSIGN_4V(c
[i
], red
, green
, blue
, alpha
);
164 argRGB
[term
] = ccolor
[term
];
167 /* GL_ATI_texture_env_combine3 allows GL_ZERO & GL_ONE as sources.
171 float4_array c
= ccolor
[term
];
172 for (i
= 0; i
< n
; i
++) {
173 ASSIGN_4V(c
[i
], 0.0F
, 0.0F
, 0.0F
, 0.0F
);
175 argRGB
[term
] = ccolor
[term
];
180 float4_array c
= ccolor
[term
];
181 for (i
= 0; i
< n
; i
++) {
182 ASSIGN_4V(c
[i
], 1.0F
, 1.0F
, 1.0F
, 1.0F
);
184 argRGB
[term
] = ccolor
[term
];
188 /* ARB_texture_env_crossbar source */
190 const GLuint srcUnit
= srcRGB
- GL_TEXTURE0
;
191 ASSERT(srcUnit
< ctx
->Const
.MaxTextureUnits
);
192 if (!ctx
->Texture
.Unit
[srcUnit
]._ReallyEnabled
)
194 argRGB
[term
] = get_texel_array(swrast
, srcUnit
);
198 if (operandRGB
!= GL_SRC_COLOR
) {
199 float4_array src
= argRGB
[term
];
200 float4_array dst
= ccolor
[term
];
202 /* point to new arg[term] storage */
203 argRGB
[term
] = ccolor
[term
];
205 switch (operandRGB
) {
206 case GL_ONE_MINUS_SRC_COLOR
:
207 for (i
= 0; i
< n
; i
++) {
208 dst
[i
][RCOMP
] = 1.0F
- src
[i
][RCOMP
];
209 dst
[i
][GCOMP
] = 1.0F
- src
[i
][GCOMP
];
210 dst
[i
][BCOMP
] = 1.0F
- src
[i
][BCOMP
];
214 for (i
= 0; i
< n
; i
++) {
217 dst
[i
][BCOMP
] = src
[i
][ACOMP
];
220 case GL_ONE_MINUS_SRC_ALPHA
:
221 for (i
= 0; i
< n
; i
++) {
224 dst
[i
][BCOMP
] = 1.0F
- src
[i
][ACOMP
];
228 _mesa_problem(ctx
, "Bad operandRGB");
234 * Set up the argA[term] pointers
236 for (term
= 0; term
< numArgsA
; term
++) {
237 const GLenum srcA
= combine
->SourceA
[term
];
238 const GLenum operandA
= combine
->OperandA
[term
];
242 argA
[term
] = get_texel_array(swrast
, unit
);
244 case GL_PRIMARY_COLOR
:
245 argA
[term
] = primary_rgba
;
252 float4_array c
= ccolor
[term
];
253 GLfloat alpha
= textureUnit
->EnvColor
[3];
254 for (i
= 0; i
< n
; i
++)
256 argA
[term
] = ccolor
[term
];
259 /* GL_ATI_texture_env_combine3 allows GL_ZERO & GL_ONE as sources.
263 float4_array c
= ccolor
[term
];
264 for (i
= 0; i
< n
; i
++)
266 argA
[term
] = ccolor
[term
];
271 float4_array c
= ccolor
[term
];
272 for (i
= 0; i
< n
; i
++)
274 argA
[term
] = ccolor
[term
];
278 /* ARB_texture_env_crossbar source */
280 const GLuint srcUnit
= srcA
- GL_TEXTURE0
;
281 ASSERT(srcUnit
< ctx
->Const
.MaxTextureUnits
);
282 if (!ctx
->Texture
.Unit
[srcUnit
]._ReallyEnabled
)
284 argA
[term
] = get_texel_array(swrast
, srcUnit
);
288 if (operandA
== GL_ONE_MINUS_SRC_ALPHA
) {
289 float4_array src
= argA
[term
];
290 float4_array dst
= ccolor
[term
];
291 argA
[term
] = ccolor
[term
];
292 for (i
= 0; i
< n
; i
++) {
293 dst
[i
][ACOMP
] = 1.0F
- src
[i
][ACOMP
];
298 /* RGB channel combine */
300 float4_array arg0
= argRGB
[0];
301 float4_array arg1
= argRGB
[1];
302 float4_array arg2
= argRGB
[2];
303 float4_array arg3
= argRGB
[3];
305 switch (combine
->ModeRGB
) {
307 for (i
= 0; i
< n
; i
++) {
308 rgba
[i
][RCOMP
] = arg0
[i
][RCOMP
] * scaleRGB
;
309 rgba
[i
][GCOMP
] = arg0
[i
][GCOMP
] * scaleRGB
;
310 rgba
[i
][BCOMP
] = arg0
[i
][BCOMP
] * scaleRGB
;
314 for (i
= 0; i
< n
; i
++) {
315 rgba
[i
][RCOMP
] = arg0
[i
][RCOMP
] * arg1
[i
][RCOMP
] * scaleRGB
;
316 rgba
[i
][GCOMP
] = arg0
[i
][GCOMP
] * arg1
[i
][GCOMP
] * scaleRGB
;
317 rgba
[i
][BCOMP
] = arg0
[i
][BCOMP
] * arg1
[i
][BCOMP
] * scaleRGB
;
321 if (textureUnit
->EnvMode
== GL_COMBINE4_NV
) {
322 /* (a * b) + (c * d) */
323 for (i
= 0; i
< n
; i
++) {
324 rgba
[i
][RCOMP
] = (arg0
[i
][RCOMP
] * arg1
[i
][RCOMP
] +
325 arg2
[i
][RCOMP
] * arg3
[i
][RCOMP
]) * scaleRGB
;
326 rgba
[i
][GCOMP
] = (arg0
[i
][GCOMP
] * arg1
[i
][GCOMP
] +
327 arg2
[i
][GCOMP
] * arg3
[i
][GCOMP
]) * scaleRGB
;
328 rgba
[i
][BCOMP
] = (arg0
[i
][BCOMP
] * arg1
[i
][BCOMP
] +
329 arg2
[i
][BCOMP
] * arg3
[i
][BCOMP
]) * scaleRGB
;
333 /* 2-term addition */
334 for (i
= 0; i
< n
; i
++) {
335 rgba
[i
][RCOMP
] = (arg0
[i
][RCOMP
] + arg1
[i
][RCOMP
]) * scaleRGB
;
336 rgba
[i
][GCOMP
] = (arg0
[i
][GCOMP
] + arg1
[i
][GCOMP
]) * scaleRGB
;
337 rgba
[i
][BCOMP
] = (arg0
[i
][BCOMP
] + arg1
[i
][BCOMP
]) * scaleRGB
;
342 if (textureUnit
->EnvMode
== GL_COMBINE4_NV
) {
343 /* (a * b) + (c * d) - 0.5 */
344 for (i
= 0; i
< n
; i
++) {
345 rgba
[i
][RCOMP
] = (arg0
[i
][RCOMP
] * arg1
[i
][RCOMP
] +
346 arg2
[i
][RCOMP
] * arg3
[i
][RCOMP
] - 0.5F
) * scaleRGB
;
347 rgba
[i
][GCOMP
] = (arg0
[i
][GCOMP
] * arg1
[i
][GCOMP
] +
348 arg2
[i
][GCOMP
] * arg3
[i
][GCOMP
] - 0.5F
) * scaleRGB
;
349 rgba
[i
][BCOMP
] = (arg0
[i
][BCOMP
] * arg1
[i
][BCOMP
] +
350 arg2
[i
][BCOMP
] * arg3
[i
][BCOMP
] - 0.5F
) * scaleRGB
;
354 for (i
= 0; i
< n
; i
++) {
355 rgba
[i
][RCOMP
] = (arg0
[i
][RCOMP
] + arg1
[i
][RCOMP
] - 0.5F
) * scaleRGB
;
356 rgba
[i
][GCOMP
] = (arg0
[i
][GCOMP
] + arg1
[i
][GCOMP
] - 0.5F
) * scaleRGB
;
357 rgba
[i
][BCOMP
] = (arg0
[i
][BCOMP
] + arg1
[i
][BCOMP
] - 0.5F
) * scaleRGB
;
362 for (i
= 0; i
< n
; i
++) {
363 rgba
[i
][RCOMP
] = (arg0
[i
][RCOMP
] * arg2
[i
][RCOMP
] +
364 arg1
[i
][RCOMP
] * (1.0F
- arg2
[i
][RCOMP
])) * scaleRGB
;
365 rgba
[i
][GCOMP
] = (arg0
[i
][GCOMP
] * arg2
[i
][GCOMP
] +
366 arg1
[i
][GCOMP
] * (1.0F
- arg2
[i
][GCOMP
])) * scaleRGB
;
367 rgba
[i
][BCOMP
] = (arg0
[i
][BCOMP
] * arg2
[i
][BCOMP
] +
368 arg1
[i
][BCOMP
] * (1.0F
- arg2
[i
][BCOMP
])) * scaleRGB
;
372 for (i
= 0; i
< n
; i
++) {
373 rgba
[i
][RCOMP
] = (arg0
[i
][RCOMP
] - arg1
[i
][RCOMP
]) * scaleRGB
;
374 rgba
[i
][GCOMP
] = (arg0
[i
][GCOMP
] - arg1
[i
][GCOMP
]) * scaleRGB
;
375 rgba
[i
][BCOMP
] = (arg0
[i
][BCOMP
] - arg1
[i
][BCOMP
]) * scaleRGB
;
378 case GL_DOT3_RGB_EXT
:
379 case GL_DOT3_RGBA_EXT
:
380 /* Do not scale the result by 1 2 or 4 */
381 for (i
= 0; i
< n
; i
++) {
382 GLfloat dot
= ((arg0
[i
][RCOMP
] - 0.5F
) * (arg1
[i
][RCOMP
] - 0.5F
) +
383 (arg0
[i
][GCOMP
] - 0.5F
) * (arg1
[i
][GCOMP
] - 0.5F
) +
384 (arg0
[i
][BCOMP
] - 0.5F
) * (arg1
[i
][BCOMP
] - 0.5F
))
386 dot
= CLAMP(dot
, 0.0F
, 1.0F
);
387 rgba
[i
][RCOMP
] = rgba
[i
][GCOMP
] = rgba
[i
][BCOMP
] = dot
;
392 /* DO scale the result by 1 2 or 4 */
393 for (i
= 0; i
< n
; i
++) {
394 GLfloat dot
= ((arg0
[i
][RCOMP
] - 0.5F
) * (arg1
[i
][RCOMP
] - 0.5F
) +
395 (arg0
[i
][GCOMP
] - 0.5F
) * (arg1
[i
][GCOMP
] - 0.5F
) +
396 (arg0
[i
][BCOMP
] - 0.5F
) * (arg1
[i
][BCOMP
] - 0.5F
))
398 dot
= CLAMP(dot
, 0.0F
, 1.0F
);
399 rgba
[i
][RCOMP
] = rgba
[i
][GCOMP
] = rgba
[i
][BCOMP
] = dot
;
402 case GL_MODULATE_ADD_ATI
:
403 for (i
= 0; i
< n
; i
++) {
404 rgba
[i
][RCOMP
] = ((arg0
[i
][RCOMP
] * arg2
[i
][RCOMP
]) +
405 arg1
[i
][RCOMP
]) * scaleRGB
;
406 rgba
[i
][GCOMP
] = ((arg0
[i
][GCOMP
] * arg2
[i
][GCOMP
]) +
407 arg1
[i
][GCOMP
]) * scaleRGB
;
408 rgba
[i
][BCOMP
] = ((arg0
[i
][BCOMP
] * arg2
[i
][BCOMP
]) +
409 arg1
[i
][BCOMP
]) * scaleRGB
;
412 case GL_MODULATE_SIGNED_ADD_ATI
:
413 for (i
= 0; i
< n
; i
++) {
414 rgba
[i
][RCOMP
] = ((arg0
[i
][RCOMP
] * arg2
[i
][RCOMP
]) +
415 arg1
[i
][RCOMP
] - 0.5F
) * scaleRGB
;
416 rgba
[i
][GCOMP
] = ((arg0
[i
][GCOMP
] * arg2
[i
][GCOMP
]) +
417 arg1
[i
][GCOMP
] - 0.5F
) * scaleRGB
;
418 rgba
[i
][BCOMP
] = ((arg0
[i
][BCOMP
] * arg2
[i
][BCOMP
]) +
419 arg1
[i
][BCOMP
] - 0.5F
) * scaleRGB
;
422 case GL_MODULATE_SUBTRACT_ATI
:
423 for (i
= 0; i
< n
; i
++) {
424 rgba
[i
][RCOMP
] = ((arg0
[i
][RCOMP
] * arg2
[i
][RCOMP
]) -
425 arg1
[i
][RCOMP
]) * scaleRGB
;
426 rgba
[i
][GCOMP
] = ((arg0
[i
][GCOMP
] * arg2
[i
][GCOMP
]) -
427 arg1
[i
][GCOMP
]) * scaleRGB
;
428 rgba
[i
][BCOMP
] = ((arg0
[i
][BCOMP
] * arg2
[i
][BCOMP
]) -
429 arg1
[i
][BCOMP
]) * scaleRGB
;
432 case GL_BUMP_ENVMAP_ATI
:
433 /* this produces a fixed rgba color, and the coord calc is done elsewhere */
434 for (i
= 0; i
< n
; i
++) {
435 /* rgba result is 0,0,0,1 */
436 rgba
[i
][RCOMP
] = 0.0;
437 rgba
[i
][GCOMP
] = 0.0;
438 rgba
[i
][BCOMP
] = 0.0;
439 rgba
[i
][ACOMP
] = 1.0;
441 goto end
; /* no alpha processing */
443 _mesa_problem(ctx
, "invalid combine mode");
447 /* Alpha channel combine */
449 float4_array arg0
= argA
[0];
450 float4_array arg1
= argA
[1];
451 float4_array arg2
= argA
[2];
452 float4_array arg3
= argA
[3];
454 switch (combine
->ModeA
) {
456 for (i
= 0; i
< n
; i
++) {
457 rgba
[i
][ACOMP
] = arg0
[i
][ACOMP
] * scaleA
;
461 for (i
= 0; i
< n
; i
++) {
462 rgba
[i
][ACOMP
] = arg0
[i
][ACOMP
] * arg1
[i
][ACOMP
] * scaleA
;
466 if (textureUnit
->EnvMode
== GL_COMBINE4_NV
) {
467 /* (a * b) + (c * d) */
468 for (i
= 0; i
< n
; i
++) {
469 rgba
[i
][ACOMP
] = (arg0
[i
][ACOMP
] * arg1
[i
][ACOMP
] +
470 arg2
[i
][ACOMP
] * arg3
[i
][ACOMP
]) * scaleA
;
475 for (i
= 0; i
< n
; i
++) {
476 rgba
[i
][ACOMP
] = (arg0
[i
][ACOMP
] + arg1
[i
][ACOMP
]) * scaleA
;
481 if (textureUnit
->EnvMode
== GL_COMBINE4_NV
) {
482 /* (a * b) + (c * d) - 0.5 */
483 for (i
= 0; i
< n
; i
++) {
484 rgba
[i
][ACOMP
] = (arg0
[i
][ACOMP
] * arg1
[i
][ACOMP
] +
485 arg2
[i
][ACOMP
] * arg3
[i
][ACOMP
] -
491 for (i
= 0; i
< n
; i
++) {
492 rgba
[i
][ACOMP
] = (arg0
[i
][ACOMP
] + arg1
[i
][ACOMP
] - 0.5F
) * scaleA
;
497 for (i
= 0; i
< n
; i
++) {
498 rgba
[i
][ACOMP
] = (arg0
[i
][ACOMP
] * arg2
[i
][ACOMP
] +
499 arg1
[i
][ACOMP
] * (1.0F
- arg2
[i
][ACOMP
]))
504 for (i
= 0; i
< n
; i
++) {
505 rgba
[i
][ACOMP
] = (arg0
[i
][ACOMP
] - arg1
[i
][ACOMP
]) * scaleA
;
508 case GL_MODULATE_ADD_ATI
:
509 for (i
= 0; i
< n
; i
++) {
510 rgba
[i
][ACOMP
] = ((arg0
[i
][ACOMP
] * arg2
[i
][ACOMP
])
511 + arg1
[i
][ACOMP
]) * scaleA
;
514 case GL_MODULATE_SIGNED_ADD_ATI
:
515 for (i
= 0; i
< n
; i
++) {
516 rgba
[i
][ACOMP
] = ((arg0
[i
][ACOMP
] * arg2
[i
][ACOMP
]) +
517 arg1
[i
][ACOMP
] - 0.5F
) * scaleA
;
520 case GL_MODULATE_SUBTRACT_ATI
:
521 for (i
= 0; i
< n
; i
++) {
522 rgba
[i
][ACOMP
] = ((arg0
[i
][ACOMP
] * arg2
[i
][ACOMP
])
523 - arg1
[i
][ACOMP
]) * scaleA
;
527 _mesa_problem(ctx
, "invalid combine mode");
531 /* Fix the alpha component for GL_DOT3_RGBA_EXT/ARB combining.
532 * This is kind of a kludge. It would have been better if the spec
533 * were written such that the GL_COMBINE_ALPHA value could be set to
536 if (combine
->ModeRGB
== GL_DOT3_RGBA_EXT
||
537 combine
->ModeRGB
== GL_DOT3_RGBA
) {
538 for (i
= 0; i
< n
; i
++) {
539 rgba
[i
][ACOMP
] = rgba
[i
][RCOMP
];
543 for (i
= 0; i
< n
; i
++) {
544 UNCLAMPED_FLOAT_TO_CHAN(rgbaChan
[i
][RCOMP
], rgba
[i
][RCOMP
]);
545 UNCLAMPED_FLOAT_TO_CHAN(rgbaChan
[i
][GCOMP
], rgba
[i
][GCOMP
]);
546 UNCLAMPED_FLOAT_TO_CHAN(rgbaChan
[i
][BCOMP
], rgba
[i
][BCOMP
]);
547 UNCLAMPED_FLOAT_TO_CHAN(rgbaChan
[i
][ACOMP
], rgba
[i
][ACOMP
]);
549 /* The span->array->rgba values are of CHAN type so set
550 * span->array->ChanType field accordingly.
552 span
->array
->ChanType
= CHAN_TYPE
;
555 for (i
= 0; i
< numArgsRGB
|| i
< numArgsA
; i
++) {
563 * Apply X/Y/Z/W/0/1 swizzle to an array of colors/texels.
564 * See GL_EXT_texture_swizzle.
567 swizzle_texels(GLuint swizzle
, GLuint count
, float4_array texels
)
569 const GLuint swzR
= GET_SWZ(swizzle
, 0);
570 const GLuint swzG
= GET_SWZ(swizzle
, 1);
571 const GLuint swzB
= GET_SWZ(swizzle
, 2);
572 const GLuint swzA
= GET_SWZ(swizzle
, 3);
576 vector
[SWIZZLE_ZERO
] = 0;
577 vector
[SWIZZLE_ONE
] = 1.0F
;
579 for (i
= 0; i
< count
; i
++) {
580 vector
[SWIZZLE_X
] = texels
[i
][0];
581 vector
[SWIZZLE_Y
] = texels
[i
][1];
582 vector
[SWIZZLE_Z
] = texels
[i
][2];
583 vector
[SWIZZLE_W
] = texels
[i
][3];
584 texels
[i
][RCOMP
] = vector
[swzR
];
585 texels
[i
][GCOMP
] = vector
[swzG
];
586 texels
[i
][BCOMP
] = vector
[swzB
];
587 texels
[i
][ACOMP
] = vector
[swzA
];
593 * Apply texture mapping to a span of fragments.
596 _swrast_texture_span( struct gl_context
*ctx
, SWspan
*span
)
598 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
599 float4_array primary_rgba
;
602 if (!swrast
->TexelBuffer
) {
604 const GLint maxThreads
= omp_get_max_threads();
606 const GLint maxThreads
= 1;
609 /* TexelBuffer is also global and normally shared by all SWspan
610 * instances; when running with multiple threads, create one per
613 swrast
->TexelBuffer
=
614 malloc(ctx
->Const
.FragmentProgram
.MaxTextureImageUnits
* maxThreads
*
615 SWRAST_MAX_WIDTH
* 4 * sizeof(GLfloat
));
616 if (!swrast
->TexelBuffer
) {
617 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "texture_combine");
622 primary_rgba
= malloc(span
->end
* 4 * sizeof(GLfloat
));
625 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "texture_span");
629 ASSERT(span
->end
<= SWRAST_MAX_WIDTH
);
632 * Save copy of the incoming fragment colors (the GL_PRIMARY_COLOR)
634 if (swrast
->_TextureCombinePrimary
) {
636 for (i
= 0; i
< span
->end
; i
++) {
637 primary_rgba
[i
][RCOMP
] = CHAN_TO_FLOAT(span
->array
->rgba
[i
][RCOMP
]);
638 primary_rgba
[i
][GCOMP
] = CHAN_TO_FLOAT(span
->array
->rgba
[i
][GCOMP
]);
639 primary_rgba
[i
][BCOMP
] = CHAN_TO_FLOAT(span
->array
->rgba
[i
][BCOMP
]);
640 primary_rgba
[i
][ACOMP
] = CHAN_TO_FLOAT(span
->array
->rgba
[i
][ACOMP
]);
644 /* First must sample all bump maps */
645 for (unit
= 0; unit
< ctx
->Const
.MaxTextureUnits
; unit
++) {
646 const struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[unit
];
648 if (texUnit
->_ReallyEnabled
&&
649 texUnit
->_CurrentCombine
->ModeRGB
== GL_BUMP_ENVMAP_ATI
) {
650 const GLfloat (*texcoords
)[4] = (const GLfloat (*)[4])
651 span
->array
->attribs
[VARYING_SLOT_TEX0
+ unit
];
652 float4_array targetcoords
=
653 span
->array
->attribs
[VARYING_SLOT_TEX0
+
654 ctx
->Texture
.Unit
[unit
].BumpTarget
- GL_TEXTURE0
];
656 const struct gl_sampler_object
*samp
= _mesa_get_samplerobj(ctx
, unit
);
657 GLfloat
*lambda
= span
->array
->lambda
[unit
];
658 float4_array texels
= get_texel_array(swrast
, unit
);
660 GLfloat rotMatrix00
= ctx
->Texture
.Unit
[unit
].RotMatrix
[0];
661 GLfloat rotMatrix01
= ctx
->Texture
.Unit
[unit
].RotMatrix
[1];
662 GLfloat rotMatrix10
= ctx
->Texture
.Unit
[unit
].RotMatrix
[2];
663 GLfloat rotMatrix11
= ctx
->Texture
.Unit
[unit
].RotMatrix
[3];
665 /* adjust texture lod (lambda) */
666 if (span
->arrayMask
& SPAN_LAMBDA
) {
667 if (texUnit
->LodBias
+ samp
->LodBias
!= 0.0F
) {
668 /* apply LOD bias, but don't clamp yet */
669 const GLfloat bias
= CLAMP(texUnit
->LodBias
+ samp
->LodBias
,
670 -ctx
->Const
.MaxTextureLodBias
,
671 ctx
->Const
.MaxTextureLodBias
);
673 for (i
= 0; i
< span
->end
; i
++) {
678 if (samp
->MinLod
!= -1000.0 ||
679 samp
->MaxLod
!= 1000.0) {
680 /* apply LOD clamping to lambda */
681 const GLfloat min
= samp
->MinLod
;
682 const GLfloat max
= samp
->MaxLod
;
684 for (i
= 0; i
< span
->end
; i
++) {
685 GLfloat l
= lambda
[i
];
686 lambda
[i
] = CLAMP(l
, min
, max
);
691 /* Sample the texture (span->end = number of fragments) */
692 swrast
->TextureSample
[unit
]( ctx
, samp
,
693 ctx
->Texture
.Unit
[unit
]._Current
,
694 span
->end
, texcoords
, lambda
, texels
);
696 /* manipulate the span values of the bump target
697 not sure this can work correctly even ignoring
698 the problem that channel is unsigned */
699 for (i
= 0; i
< span
->end
; i
++) {
700 targetcoords
[i
][0] += (texels
[i
][0] * rotMatrix00
+ texels
[i
][1] *
701 rotMatrix01
) / targetcoords
[i
][3];
702 targetcoords
[i
][1] += (texels
[i
][0] * rotMatrix10
+ texels
[i
][1] *
703 rotMatrix11
) / targetcoords
[i
][3];
709 * Must do all texture sampling before combining in order to
710 * accomodate GL_ARB_texture_env_crossbar.
712 for (unit
= 0; unit
< ctx
->Const
.MaxTextureUnits
; unit
++) {
713 const struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[unit
];
714 if (texUnit
->_ReallyEnabled
&&
715 texUnit
->_CurrentCombine
->ModeRGB
!= GL_BUMP_ENVMAP_ATI
) {
716 const GLfloat (*texcoords
)[4] = (const GLfloat (*)[4])
717 span
->array
->attribs
[VARYING_SLOT_TEX0
+ unit
];
718 const struct gl_texture_object
*curObj
= texUnit
->_Current
;
719 const struct gl_sampler_object
*samp
= _mesa_get_samplerobj(ctx
, unit
);
720 GLfloat
*lambda
= span
->array
->lambda
[unit
];
721 float4_array texels
= get_texel_array(swrast
, unit
);
723 /* adjust texture lod (lambda) */
724 if (span
->arrayMask
& SPAN_LAMBDA
) {
725 if (texUnit
->LodBias
+ samp
->LodBias
!= 0.0F
) {
726 /* apply LOD bias, but don't clamp yet */
727 const GLfloat bias
= CLAMP(texUnit
->LodBias
+ samp
->LodBias
,
728 -ctx
->Const
.MaxTextureLodBias
,
729 ctx
->Const
.MaxTextureLodBias
);
731 for (i
= 0; i
< span
->end
; i
++) {
736 if (samp
->MinLod
!= -1000.0 ||
737 samp
->MaxLod
!= 1000.0) {
738 /* apply LOD clamping to lambda */
739 const GLfloat min
= samp
->MinLod
;
740 const GLfloat max
= samp
->MaxLod
;
742 for (i
= 0; i
< span
->end
; i
++) {
743 GLfloat l
= lambda
[i
];
744 lambda
[i
] = CLAMP(l
, min
, max
);
748 else if (samp
->MaxAnisotropy
> 1.0 &&
749 samp
->MinFilter
== GL_LINEAR_MIPMAP_LINEAR
) {
750 /* sample_lambda_2d_aniso is beeing used as texture_sample_func,
751 * it requires the current SWspan *span as an additional parameter.
752 * In order to keep the same function signature, the unused lambda
753 * parameter will be modified to actually contain the SWspan pointer.
754 * This is a Hack. To make it right, the texture_sample_func
755 * signature and all implementing functions need to be modified.
757 /* "hide" SWspan struct; cast to (GLfloat *) to suppress warning */
758 lambda
= (GLfloat
*)span
;
761 /* Sample the texture (span->end = number of fragments) */
762 swrast
->TextureSample
[unit
]( ctx
, samp
,
763 ctx
->Texture
.Unit
[unit
]._Current
,
764 span
->end
, texcoords
, lambda
, texels
);
766 /* GL_EXT_texture_swizzle */
767 if (curObj
->_Swizzle
!= SWIZZLE_NOOP
) {
768 swizzle_texels(curObj
->_Swizzle
, span
->end
, texels
);
774 * OK, now apply the texture (aka texture combine/blend).
775 * We modify the span->color.rgba values.
777 for (unit
= 0; unit
< ctx
->Const
.MaxTextureUnits
; unit
++) {
778 if (ctx
->Texture
.Unit
[unit
]._ReallyEnabled
)
779 texture_combine(ctx
, unit
, primary_rgba
, swrast
->TexelBuffer
, span
);