Merge remote branch 'origin/master' into radeon-rewrite
[mesa.git] / src / mesa / swrast / s_texcombine.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.5
4 *
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26
27 #include "main/glheader.h"
28 #include "main/context.h"
29 #include "main/colormac.h"
30 #include "main/image.h"
31 #include "main/imports.h"
32 #include "main/pixel.h"
33 #include "shader/prog_instruction.h"
34
35 #include "s_context.h"
36 #include "s_texcombine.h"
37
38
39 /**
40 * Pointer to array of float[4]
41 * This type makes the code below more concise and avoids a lot of casting.
42 */
43 typedef float (*float4_array)[4];
44
45
46 /**
47 * Return array of texels for given unit.
48 */
49 static INLINE float4_array
50 get_texel_array(SWcontext *swrast, GLuint unit)
51 {
52 return (float4_array)
53 (swrast->TexelBuffer + unit * MAX_WIDTH * 4 * sizeof(GLfloat));
54 }
55
56
57
58 /**
59 * Do texture application for:
60 * GL_EXT_texture_env_combine
61 * GL_ARB_texture_env_combine
62 * GL_EXT_texture_env_dot3
63 * GL_ARB_texture_env_dot3
64 * GL_ATI_texture_env_combine3
65 * GL_NV_texture_env_combine4
66 * conventional GL texture env modes
67 *
68 * \param ctx rendering context
69 * \param unit the texture combiner unit
70 * \param n number of fragments to process (span width)
71 * \param primary_rgba incoming fragment color array
72 * \param texelBuffer pointer to texel colors for all texture units
73 *
74 * \param rgba incoming/result fragment colors
75 */
76 static void
77 texture_combine( GLcontext *ctx, GLuint unit, GLuint n,
78 const float4_array primary_rgba,
79 const GLfloat *texelBuffer,
80 GLchan (*rgbaChan)[4] )
81 {
82 SWcontext *swrast = SWRAST_CONTEXT(ctx);
83 const struct gl_texture_unit *textureUnit = &(ctx->Texture.Unit[unit]);
84 const struct gl_tex_env_combine_state *combine = textureUnit->_CurrentCombine;
85 float4_array argRGB[MAX_COMBINER_TERMS];
86 float4_array argA[MAX_COMBINER_TERMS];
87 const GLfloat scaleRGB = (GLfloat) (1 << combine->ScaleShiftRGB);
88 const GLfloat scaleA = (GLfloat) (1 << combine->ScaleShiftA);
89 const GLuint numArgsRGB = combine->_NumArgsRGB;
90 const GLuint numArgsA = combine->_NumArgsA;
91 GLfloat ccolor[MAX_COMBINER_TERMS][MAX_WIDTH][4]; /* temp color buffers */
92 GLfloat rgba[MAX_WIDTH][4];
93 GLuint i, term;
94
95 for (i = 0; i < n; i++) {
96 rgba[i][RCOMP] = CHAN_TO_FLOAT(rgbaChan[i][RCOMP]);
97 rgba[i][GCOMP] = CHAN_TO_FLOAT(rgbaChan[i][GCOMP]);
98 rgba[i][BCOMP] = CHAN_TO_FLOAT(rgbaChan[i][BCOMP]);
99 rgba[i][ACOMP] = CHAN_TO_FLOAT(rgbaChan[i][ACOMP]);
100 }
101
102 /*
103 printf("modeRGB 0x%x modeA 0x%x srcRGB1 0x%x srcA1 0x%x srcRGB2 0x%x srcA2 0x%x\n",
104 combine->ModeRGB,
105 combine->ModeA,
106 combine->SourceRGB[0],
107 combine->SourceA[0],
108 combine->SourceRGB[1],
109 combine->SourceA[1]);
110 */
111
112 /*
113 * Do operand setup for up to 4 operands. Loop over the terms.
114 */
115 for (term = 0; term < numArgsRGB; term++) {
116 const GLenum srcRGB = combine->SourceRGB[term];
117 const GLenum operandRGB = combine->OperandRGB[term];
118
119 switch (srcRGB) {
120 case GL_TEXTURE:
121 argRGB[term] = get_texel_array(swrast, unit);
122 break;
123 case GL_PRIMARY_COLOR:
124 argRGB[term] = primary_rgba;
125 break;
126 case GL_PREVIOUS:
127 argRGB[term] = rgba;
128 break;
129 case GL_CONSTANT:
130 {
131 float4_array c = ccolor[term];
132 GLfloat red = textureUnit->EnvColor[0];
133 GLfloat green = textureUnit->EnvColor[1];
134 GLfloat blue = textureUnit->EnvColor[2];
135 GLfloat alpha = textureUnit->EnvColor[3];
136 for (i = 0; i < n; i++) {
137 ASSIGN_4V(c[i], red, green, blue, alpha);
138 }
139 argRGB[term] = ccolor[term];
140 }
141 break;
142 /* GL_ATI_texture_env_combine3 allows GL_ZERO & GL_ONE as sources.
143 */
144 case GL_ZERO:
145 {
146 float4_array c = ccolor[term];
147 for (i = 0; i < n; i++) {
148 ASSIGN_4V(c[i], 0.0F, 0.0F, 0.0F, 0.0F);
149 }
150 argRGB[term] = ccolor[term];
151 }
152 break;
153 case GL_ONE:
154 {
155 float4_array c = ccolor[term];
156 for (i = 0; i < n; i++) {
157 ASSIGN_4V(c[i], 1.0F, 1.0F, 1.0F, 1.0F);
158 }
159 argRGB[term] = ccolor[term];
160 }
161 break;
162 default:
163 /* ARB_texture_env_crossbar source */
164 {
165 const GLuint srcUnit = srcRGB - GL_TEXTURE0;
166 ASSERT(srcUnit < ctx->Const.MaxTextureUnits);
167 if (!ctx->Texture.Unit[srcUnit]._ReallyEnabled)
168 return;
169 argRGB[term] = get_texel_array(swrast, srcUnit);
170 }
171 }
172
173 if (operandRGB != GL_SRC_COLOR) {
174 float4_array src = argRGB[term];
175 float4_array dst = ccolor[term];
176
177 /* point to new arg[term] storage */
178 argRGB[term] = ccolor[term];
179
180 switch (operandRGB) {
181 case GL_ONE_MINUS_SRC_COLOR:
182 for (i = 0; i < n; i++) {
183 dst[i][RCOMP] = 1.0F - src[i][RCOMP];
184 dst[i][GCOMP] = 1.0F - src[i][GCOMP];
185 dst[i][BCOMP] = 1.0F - src[i][BCOMP];
186 }
187 break;
188 case GL_SRC_ALPHA:
189 for (i = 0; i < n; i++) {
190 dst[i][RCOMP] =
191 dst[i][GCOMP] =
192 dst[i][BCOMP] = src[i][ACOMP];
193 }
194 break;
195 case GL_ONE_MINUS_SRC_ALPHA:
196 for (i = 0; i < n; i++) {
197 dst[i][RCOMP] =
198 dst[i][GCOMP] =
199 dst[i][BCOMP] = 1.0F - src[i][ACOMP];
200 }
201 break;
202 default:
203 _mesa_problem(ctx, "Bad operandRGB");
204 }
205 }
206 }
207
208 /*
209 * Set up the argA[term] pointers
210 */
211 for (term = 0; term < numArgsA; term++) {
212 const GLenum srcA = combine->SourceA[term];
213 const GLenum operandA = combine->OperandA[term];
214
215 switch (srcA) {
216 case GL_TEXTURE:
217 argA[term] = get_texel_array(swrast, unit);
218 break;
219 case GL_PRIMARY_COLOR:
220 argA[term] = primary_rgba;
221 break;
222 case GL_PREVIOUS:
223 argA[term] = rgba;
224 break;
225 case GL_CONSTANT:
226 {
227 float4_array c = ccolor[term];
228 GLfloat alpha = textureUnit->EnvColor[3];
229 for (i = 0; i < n; i++)
230 c[i][ACOMP] = alpha;
231 argA[term] = ccolor[term];
232 }
233 break;
234 /* GL_ATI_texture_env_combine3 allows GL_ZERO & GL_ONE as sources.
235 */
236 case GL_ZERO:
237 {
238 float4_array c = ccolor[term];
239 for (i = 0; i < n; i++)
240 c[i][ACOMP] = 0.0F;
241 argA[term] = ccolor[term];
242 }
243 break;
244 case GL_ONE:
245 {
246 float4_array c = ccolor[term];
247 for (i = 0; i < n; i++)
248 c[i][ACOMP] = 1.0F;
249 argA[term] = ccolor[term];
250 }
251 break;
252 default:
253 /* ARB_texture_env_crossbar source */
254 {
255 const GLuint srcUnit = srcA - GL_TEXTURE0;
256 ASSERT(srcUnit < ctx->Const.MaxTextureUnits);
257 if (!ctx->Texture.Unit[srcUnit]._ReallyEnabled)
258 return;
259 argA[term] = get_texel_array(swrast, srcUnit);
260 }
261 }
262
263 if (operandA == GL_ONE_MINUS_SRC_ALPHA) {
264 float4_array src = argA[term];
265 float4_array dst = ccolor[term];
266 argA[term] = ccolor[term];
267 for (i = 0; i < n; i++) {
268 dst[i][ACOMP] = 1.0F - src[i][ACOMP];
269 }
270 }
271 }
272
273 /* RGB channel combine */
274 {
275 float4_array arg0 = argRGB[0];
276 float4_array arg1 = argRGB[1];
277 float4_array arg2 = argRGB[2];
278 float4_array arg3 = argRGB[3];
279
280 switch (combine->ModeRGB) {
281 case GL_REPLACE:
282 for (i = 0; i < n; i++) {
283 rgba[i][RCOMP] = arg0[i][RCOMP] * scaleRGB;
284 rgba[i][GCOMP] = arg0[i][GCOMP] * scaleRGB;
285 rgba[i][BCOMP] = arg0[i][BCOMP] * scaleRGB;
286 }
287 break;
288 case GL_MODULATE:
289 for (i = 0; i < n; i++) {
290 rgba[i][RCOMP] = arg0[i][RCOMP] * arg1[i][RCOMP] * scaleRGB;
291 rgba[i][GCOMP] = arg0[i][GCOMP] * arg1[i][GCOMP] * scaleRGB;
292 rgba[i][BCOMP] = arg0[i][BCOMP] * arg1[i][BCOMP] * scaleRGB;
293 }
294 break;
295 case GL_ADD:
296 if (textureUnit->EnvMode == GL_COMBINE4_NV) {
297 /* (a * b) + (c * d) */
298 for (i = 0; i < n; i++) {
299 rgba[i][RCOMP] = (arg0[i][RCOMP] * arg1[i][RCOMP] +
300 arg2[i][RCOMP] * arg3[i][RCOMP]) * scaleRGB;
301 rgba[i][GCOMP] = (arg0[i][GCOMP] * arg1[i][GCOMP] +
302 arg2[i][GCOMP] * arg3[i][GCOMP]) * scaleRGB;
303 rgba[i][BCOMP] = (arg0[i][BCOMP] * arg1[i][BCOMP] +
304 arg2[i][BCOMP] * arg3[i][BCOMP]) * scaleRGB;
305 }
306 }
307 else {
308 /* 2-term addition */
309 for (i = 0; i < n; i++) {
310 rgba[i][RCOMP] = (arg0[i][RCOMP] + arg1[i][RCOMP]) * scaleRGB;
311 rgba[i][GCOMP] = (arg0[i][GCOMP] + arg1[i][GCOMP]) * scaleRGB;
312 rgba[i][BCOMP] = (arg0[i][BCOMP] + arg1[i][BCOMP]) * scaleRGB;
313 }
314 }
315 break;
316 case GL_ADD_SIGNED:
317 if (textureUnit->EnvMode == GL_COMBINE4_NV) {
318 /* (a * b) + (c * d) - 0.5 */
319 for (i = 0; i < n; i++) {
320 rgba[i][RCOMP] = (arg0[i][RCOMP] * arg1[i][RCOMP] +
321 arg2[i][RCOMP] * arg3[i][RCOMP] - 0.5) * scaleRGB;
322 rgba[i][GCOMP] = (arg0[i][GCOMP] * arg1[i][GCOMP] +
323 arg2[i][GCOMP] * arg3[i][GCOMP] - 0.5) * scaleRGB;
324 rgba[i][BCOMP] = (arg0[i][BCOMP] * arg1[i][BCOMP] +
325 arg2[i][BCOMP] * arg3[i][BCOMP] - 0.5) * scaleRGB;
326 }
327 }
328 else {
329 for (i = 0; i < n; i++) {
330 rgba[i][RCOMP] = (arg0[i][RCOMP] + arg1[i][RCOMP] - 0.5) * scaleRGB;
331 rgba[i][GCOMP] = (arg0[i][GCOMP] + arg1[i][GCOMP] - 0.5) * scaleRGB;
332 rgba[i][BCOMP] = (arg0[i][BCOMP] + arg1[i][BCOMP] - 0.5) * scaleRGB;
333 }
334 }
335 break;
336 case GL_INTERPOLATE:
337 for (i = 0; i < n; i++) {
338 rgba[i][RCOMP] = (arg0[i][RCOMP] * arg2[i][RCOMP] +
339 arg1[i][RCOMP] * (1.0F - arg2[i][RCOMP])) * scaleRGB;
340 rgba[i][GCOMP] = (arg0[i][GCOMP] * arg2[i][GCOMP] +
341 arg1[i][GCOMP] * (1.0F - arg2[i][GCOMP])) * scaleRGB;
342 rgba[i][BCOMP] = (arg0[i][BCOMP] * arg2[i][BCOMP] +
343 arg1[i][BCOMP] * (1.0F - arg2[i][BCOMP])) * scaleRGB;
344 }
345 break;
346 case GL_SUBTRACT:
347 for (i = 0; i < n; i++) {
348 rgba[i][RCOMP] = (arg0[i][RCOMP] - arg1[i][RCOMP]) * scaleRGB;
349 rgba[i][GCOMP] = (arg0[i][GCOMP] - arg1[i][GCOMP]) * scaleRGB;
350 rgba[i][BCOMP] = (arg0[i][BCOMP] - arg1[i][BCOMP]) * scaleRGB;
351 }
352 break;
353 case GL_DOT3_RGB_EXT:
354 case GL_DOT3_RGBA_EXT:
355 /* Do not scale the result by 1 2 or 4 */
356 for (i = 0; i < n; i++) {
357 GLfloat dot = ((arg0[i][RCOMP] - 0.5F) * (arg1[i][RCOMP] - 0.5F) +
358 (arg0[i][GCOMP] - 0.5F) * (arg1[i][GCOMP] - 0.5F) +
359 (arg0[i][BCOMP] - 0.5F) * (arg1[i][BCOMP] - 0.5F))
360 * 4.0F;
361 dot = CLAMP(dot, 0.0F, 1.0F);
362 rgba[i][RCOMP] = rgba[i][GCOMP] = rgba[i][BCOMP] = dot;
363 }
364 break;
365 case GL_DOT3_RGB:
366 case GL_DOT3_RGBA:
367 /* DO scale the result by 1 2 or 4 */
368 for (i = 0; i < n; i++) {
369 GLfloat dot = ((arg0[i][RCOMP] - 0.5F) * (arg1[i][RCOMP] - 0.5F) +
370 (arg0[i][GCOMP] - 0.5F) * (arg1[i][GCOMP] - 0.5F) +
371 (arg0[i][BCOMP] - 0.5F) * (arg1[i][BCOMP] - 0.5F))
372 * 4.0F * scaleRGB;
373 dot = CLAMP(dot, 0.0, 1.0F);
374 rgba[i][RCOMP] = rgba[i][GCOMP] = rgba[i][BCOMP] = dot;
375 }
376 break;
377 case GL_MODULATE_ADD_ATI:
378 for (i = 0; i < n; i++) {
379 rgba[i][RCOMP] = ((arg0[i][RCOMP] * arg2[i][RCOMP]) +
380 arg1[i][RCOMP]) * scaleRGB;
381 rgba[i][GCOMP] = ((arg0[i][GCOMP] * arg2[i][GCOMP]) +
382 arg1[i][GCOMP]) * scaleRGB;
383 rgba[i][BCOMP] = ((arg0[i][BCOMP] * arg2[i][BCOMP]) +
384 arg1[i][BCOMP]) * scaleRGB;
385 }
386 break;
387 case GL_MODULATE_SIGNED_ADD_ATI:
388 for (i = 0; i < n; i++) {
389 rgba[i][RCOMP] = ((arg0[i][RCOMP] * arg2[i][RCOMP]) +
390 arg1[i][RCOMP] - 0.5) * scaleRGB;
391 rgba[i][GCOMP] = ((arg0[i][GCOMP] * arg2[i][GCOMP]) +
392 arg1[i][GCOMP] - 0.5) * scaleRGB;
393 rgba[i][BCOMP] = ((arg0[i][BCOMP] * arg2[i][BCOMP]) +
394 arg1[i][BCOMP] - 0.5) * scaleRGB;
395 }
396 break;
397 case GL_MODULATE_SUBTRACT_ATI:
398 for (i = 0; i < n; i++) {
399 rgba[i][RCOMP] = ((arg0[i][RCOMP] * arg2[i][RCOMP]) -
400 arg1[i][RCOMP]) * scaleRGB;
401 rgba[i][GCOMP] = ((arg0[i][GCOMP] * arg2[i][GCOMP]) -
402 arg1[i][GCOMP]) * scaleRGB;
403 rgba[i][BCOMP] = ((arg0[i][BCOMP] * arg2[i][BCOMP]) -
404 arg1[i][BCOMP]) * scaleRGB;
405 }
406 break;
407 case GL_BUMP_ENVMAP_ATI:
408 /* this produces a fixed rgba color, and the coord calc is done elsewhere */
409 for (i = 0; i < n; i++) {
410 /* rgba result is 0,0,0,1 */
411 #if CHAN_TYPE == GL_FLOAT
412 rgba[i][RCOMP] = 0.0;
413 rgba[i][GCOMP] = 0.0;
414 rgba[i][BCOMP] = 0.0;
415 rgba[i][ACOMP] = 1.0;
416 #else
417 rgba[i][RCOMP] = 0;
418 rgba[i][GCOMP] = 0;
419 rgba[i][BCOMP] = 0;
420 rgba[i][ACOMP] = CHAN_MAX;
421 #endif
422 }
423 return; /* no alpha processing */
424 default:
425 _mesa_problem(ctx, "invalid combine mode");
426 }
427 }
428
429 /* Alpha channel combine */
430 {
431 float4_array arg0 = argA[0];
432 float4_array arg1 = argA[1];
433 float4_array arg2 = argA[2];
434 float4_array arg3 = argA[3];
435
436 switch (combine->ModeA) {
437 case GL_REPLACE:
438 for (i = 0; i < n; i++) {
439 rgba[i][ACOMP] = arg0[i][ACOMP] * scaleA;
440 }
441 break;
442 case GL_MODULATE:
443 for (i = 0; i < n; i++) {
444 rgba[i][ACOMP] = arg0[i][ACOMP] * arg1[i][ACOMP] * scaleA;
445 }
446 break;
447 case GL_ADD:
448 if (textureUnit->EnvMode == GL_COMBINE4_NV) {
449 /* (a * b) + (c * d) */
450 for (i = 0; i < n; i++) {
451 rgba[i][ACOMP] = (arg0[i][ACOMP] * arg1[i][ACOMP] +
452 arg2[i][ACOMP] * arg3[i][ACOMP]) * scaleA;
453 }
454 }
455 else {
456 /* two-term add */
457 for (i = 0; i < n; i++) {
458 rgba[i][ACOMP] = (arg0[i][ACOMP] + arg1[i][ACOMP]) * scaleA;
459 }
460 }
461 break;
462 case GL_ADD_SIGNED:
463 if (textureUnit->EnvMode == GL_COMBINE4_NV) {
464 /* (a * b) + (c * d) - 0.5 */
465 for (i = 0; i < n; i++) {
466 rgba[i][ACOMP] = (arg0[i][ACOMP] * arg1[i][ACOMP] +
467 arg2[i][ACOMP] * arg3[i][ACOMP] -
468 0.5) * scaleA;
469 }
470 }
471 else {
472 /* a + b - 0.5 */
473 for (i = 0; i < n; i++) {
474 rgba[i][ACOMP] = (arg0[i][ACOMP] + arg1[i][ACOMP] - 0.5F) * scaleA;
475 }
476 }
477 break;
478 case GL_INTERPOLATE:
479 for (i = 0; i < n; i++) {
480 rgba[i][ACOMP] = (arg0[i][ACOMP] * arg2[i][ACOMP] +
481 arg1[i][ACOMP] * (1.0F - arg2[i][ACOMP]))
482 * scaleA;
483 }
484 break;
485 case GL_SUBTRACT:
486 for (i = 0; i < n; i++) {
487 rgba[i][ACOMP] = (arg0[i][ACOMP] - arg1[i][ACOMP]) * scaleA;
488 }
489 break;
490 case GL_MODULATE_ADD_ATI:
491 for (i = 0; i < n; i++) {
492 rgba[i][ACOMP] = ((arg0[i][ACOMP] * arg2[i][ACOMP])
493 + arg1[i][ACOMP]) * scaleA;
494 }
495 break;
496 case GL_MODULATE_SIGNED_ADD_ATI:
497 for (i = 0; i < n; i++) {
498 rgba[i][ACOMP] = ((arg0[i][ACOMP] * arg2[i][ACOMP]) +
499 arg1[i][ACOMP] - 0.5F) * scaleA;
500 }
501 break;
502 case GL_MODULATE_SUBTRACT_ATI:
503 for (i = 0; i < n; i++) {
504 rgba[i][ACOMP] = ((arg0[i][ACOMP] * arg2[i][ACOMP])
505 - arg1[i][ACOMP]) * scaleA;
506 }
507 break;
508 default:
509 _mesa_problem(ctx, "invalid combine mode");
510 }
511 }
512
513 /* Fix the alpha component for GL_DOT3_RGBA_EXT/ARB combining.
514 * This is kind of a kludge. It would have been better if the spec
515 * were written such that the GL_COMBINE_ALPHA value could be set to
516 * GL_DOT3.
517 */
518 if (combine->ModeRGB == GL_DOT3_RGBA_EXT ||
519 combine->ModeRGB == GL_DOT3_RGBA) {
520 for (i = 0; i < n; i++) {
521 rgba[i][ACOMP] = rgba[i][RCOMP];
522 }
523 }
524
525 for (i = 0; i < n; i++) {
526 UNCLAMPED_FLOAT_TO_CHAN(rgbaChan[i][RCOMP], rgba[i][RCOMP]);
527 UNCLAMPED_FLOAT_TO_CHAN(rgbaChan[i][GCOMP], rgba[i][GCOMP]);
528 UNCLAMPED_FLOAT_TO_CHAN(rgbaChan[i][BCOMP], rgba[i][BCOMP]);
529 UNCLAMPED_FLOAT_TO_CHAN(rgbaChan[i][ACOMP], rgba[i][ACOMP]);
530 }
531 }
532
533
534 /**
535 * Apply X/Y/Z/W/0/1 swizzle to an array of colors/texels.
536 * See GL_EXT_texture_swizzle.
537 */
538 static void
539 swizzle_texels(GLuint swizzle, GLuint count, float4_array texels)
540 {
541 const GLuint swzR = GET_SWZ(swizzle, 0);
542 const GLuint swzG = GET_SWZ(swizzle, 1);
543 const GLuint swzB = GET_SWZ(swizzle, 2);
544 const GLuint swzA = GET_SWZ(swizzle, 3);
545 GLfloat vector[6];
546 GLuint i;
547
548 vector[SWIZZLE_ZERO] = 0;
549 vector[SWIZZLE_ONE] = 1.0F;
550
551 for (i = 0; i < count; i++) {
552 vector[SWIZZLE_X] = texels[i][0];
553 vector[SWIZZLE_Y] = texels[i][1];
554 vector[SWIZZLE_Z] = texels[i][2];
555 vector[SWIZZLE_W] = texels[i][3];
556 texels[i][RCOMP] = vector[swzR];
557 texels[i][GCOMP] = vector[swzG];
558 texels[i][BCOMP] = vector[swzB];
559 texels[i][ACOMP] = vector[swzA];
560 }
561 }
562
563
564 /**
565 * Apply texture mapping to a span of fragments.
566 */
567 void
568 _swrast_texture_span( GLcontext *ctx, SWspan *span )
569 {
570 SWcontext *swrast = SWRAST_CONTEXT(ctx);
571 GLfloat primary_rgba[MAX_WIDTH][4];
572 GLuint unit;
573
574 ASSERT(span->end <= MAX_WIDTH);
575
576 /*
577 * Save copy of the incoming fragment colors (the GL_PRIMARY_COLOR)
578 */
579 if (swrast->_TextureCombinePrimary) {
580 GLuint i;
581 for (i = 0; i < span->end; i++) {
582 primary_rgba[i][RCOMP] = CHAN_TO_FLOAT(span->array->rgba[i][RCOMP]);
583 primary_rgba[i][GCOMP] = CHAN_TO_FLOAT(span->array->rgba[i][GCOMP]);
584 primary_rgba[i][BCOMP] = CHAN_TO_FLOAT(span->array->rgba[i][BCOMP]);
585 primary_rgba[i][ACOMP] = CHAN_TO_FLOAT(span->array->rgba[i][ACOMP]);
586 }
587 }
588
589 /* First must sample all bump maps */
590 for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++) {
591 const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
592
593 if (texUnit->_ReallyEnabled &&
594 texUnit->_CurrentCombine->ModeRGB == GL_BUMP_ENVMAP_ATI) {
595 const GLfloat (*texcoords)[4] = (const GLfloat (*)[4])
596 span->array->attribs[FRAG_ATTRIB_TEX0 + unit];
597 float4_array targetcoords =
598 span->array->attribs[FRAG_ATTRIB_TEX0 +
599 ctx->Texture.Unit[unit].BumpTarget - GL_TEXTURE0];
600
601 const struct gl_texture_object *curObj = texUnit->_Current;
602 GLfloat *lambda = span->array->lambda[unit];
603 float4_array texels = get_texel_array(swrast, unit);
604 GLuint i;
605 GLfloat rotMatrix00 = ctx->Texture.Unit[unit].RotMatrix[0];
606 GLfloat rotMatrix01 = ctx->Texture.Unit[unit].RotMatrix[1];
607 GLfloat rotMatrix10 = ctx->Texture.Unit[unit].RotMatrix[2];
608 GLfloat rotMatrix11 = ctx->Texture.Unit[unit].RotMatrix[3];
609
610 /* adjust texture lod (lambda) */
611 if (span->arrayMask & SPAN_LAMBDA) {
612 if (texUnit->LodBias + curObj->LodBias != 0.0F) {
613 /* apply LOD bias, but don't clamp yet */
614 const GLfloat bias = CLAMP(texUnit->LodBias + curObj->LodBias,
615 -ctx->Const.MaxTextureLodBias,
616 ctx->Const.MaxTextureLodBias);
617 GLuint i;
618 for (i = 0; i < span->end; i++) {
619 lambda[i] += bias;
620 }
621 }
622
623 if (curObj->MinLod != -1000.0 || curObj->MaxLod != 1000.0) {
624 /* apply LOD clamping to lambda */
625 const GLfloat min = curObj->MinLod;
626 const GLfloat max = curObj->MaxLod;
627 GLuint i;
628 for (i = 0; i < span->end; i++) {
629 GLfloat l = lambda[i];
630 lambda[i] = CLAMP(l, min, max);
631 }
632 }
633 }
634
635 /* Sample the texture (span->end = number of fragments) */
636 swrast->TextureSample[unit]( ctx, texUnit->_Current, span->end,
637 texcoords, lambda, texels );
638
639 /* manipulate the span values of the bump target
640 not sure this can work correctly even ignoring
641 the problem that channel is unsigned */
642 for (i = 0; i < span->end; i++) {
643 targetcoords[i][0] += (texels[i][0] * rotMatrix00 + texels[i][1] *
644 rotMatrix01) / targetcoords[i][3];
645 targetcoords[i][1] += (texels[i][0] * rotMatrix10 + texels[i][1] *
646 rotMatrix11) / targetcoords[i][3];
647 }
648 }
649 }
650
651 /*
652 * Must do all texture sampling before combining in order to
653 * accomodate GL_ARB_texture_env_crossbar.
654 */
655 for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++) {
656 const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
657 if (texUnit->_ReallyEnabled &&
658 texUnit->_CurrentCombine->ModeRGB != GL_BUMP_ENVMAP_ATI) {
659 const GLfloat (*texcoords)[4] = (const GLfloat (*)[4])
660 span->array->attribs[FRAG_ATTRIB_TEX0 + unit];
661 const struct gl_texture_object *curObj = texUnit->_Current;
662 GLfloat *lambda = span->array->lambda[unit];
663 float4_array texels = get_texel_array(swrast, unit);
664
665 /* adjust texture lod (lambda) */
666 if (span->arrayMask & SPAN_LAMBDA) {
667 if (texUnit->LodBias + curObj->LodBias != 0.0F) {
668 /* apply LOD bias, but don't clamp yet */
669 const GLfloat bias = CLAMP(texUnit->LodBias + curObj->LodBias,
670 -ctx->Const.MaxTextureLodBias,
671 ctx->Const.MaxTextureLodBias);
672 GLuint i;
673 for (i = 0; i < span->end; i++) {
674 lambda[i] += bias;
675 }
676 }
677
678 if (curObj->MinLod != -1000.0 || curObj->MaxLod != 1000.0) {
679 /* apply LOD clamping to lambda */
680 const GLfloat min = curObj->MinLod;
681 const GLfloat max = curObj->MaxLod;
682 GLuint i;
683 for (i = 0; i < span->end; i++) {
684 GLfloat l = lambda[i];
685 lambda[i] = CLAMP(l, min, max);
686 }
687 }
688 }
689
690 /* Sample the texture (span->end = number of fragments) */
691 swrast->TextureSample[unit]( ctx, texUnit->_Current, span->end,
692 texcoords, lambda, texels );
693
694 /* GL_SGI_texture_color_table */
695 if (texUnit->ColorTableEnabled) {
696 _mesa_lookup_rgba_float(&texUnit->ColorTable, span->end, texels);
697 }
698
699 /* GL_EXT_texture_swizzle */
700 if (curObj->_Swizzle != SWIZZLE_NOOP) {
701 swizzle_texels(curObj->_Swizzle, span->end, texels);
702 }
703 }
704 }
705
706 /*
707 * OK, now apply the texture (aka texture combine/blend).
708 * We modify the span->color.rgba values.
709 */
710 for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++) {
711 if (ctx->Texture.Unit[unit]._ReallyEnabled) {
712 texture_combine( ctx, unit, span->end,
713 primary_rgba,
714 swrast->TexelBuffer,
715 span->array->rgba );
716 }
717 }
718 }