Added few more stubs so that control reaches to DestroyDevice().
[mesa.git] / src / mesa / main / ff_fragment_shader.cpp
1 /**************************************************************************
2 *
3 * Copyright 2007 VMware, Inc.
4 * All Rights Reserved.
5 * Copyright 2009 VMware, Inc. All Rights Reserved.
6 * Copyright © 2010-2011 Intel Corporation
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sub license, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
15 *
16 * The above copyright notice and this permission notice (including the
17 * next paragraph) shall be included in all copies or substantial portions
18 * of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
23 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
24 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 *
28 **************************************************************************/
29
30 #include "main/glheader.h"
31 #include "main/context.h"
32
33 #include "main/macros.h"
34 #include "main/samplerobj.h"
35 #include "main/shaderobj.h"
36 #include "main/state.h"
37 #include "main/texenvprogram.h"
38 #include "main/texobj.h"
39 #include "main/uniforms.h"
40 #include "compiler/glsl/ir_builder.h"
41 #include "compiler/glsl/ir_optimization.h"
42 #include "compiler/glsl/glsl_parser_extras.h"
43 #include "compiler/glsl/glsl_symbol_table.h"
44 #include "compiler/glsl_types.h"
45 #include "program/ir_to_mesa.h"
46 #include "program/program.h"
47 #include "program/programopt.h"
48 #include "program/prog_cache.h"
49 #include "program/prog_instruction.h"
50 #include "program/prog_parameter.h"
51 #include "program/prog_print.h"
52 #include "program/prog_statevars.h"
53 #include "util/bitscan.h"
54
55 using namespace ir_builder;
56
57 /*
58 * Note on texture units:
59 *
60 * The number of texture units supported by fixed-function fragment
61 * processing is MAX_TEXTURE_COORD_UNITS, not MAX_TEXTURE_IMAGE_UNITS.
62 * That's because there's a one-to-one correspondence between texture
63 * coordinates and samplers in fixed-function processing.
64 *
65 * Since fixed-function vertex processing is limited to MAX_TEXTURE_COORD_UNITS
66 * sets of texcoords, so is fixed-function fragment processing.
67 *
68 * We can safely use ctx->Const.MaxTextureUnits for loop bounds.
69 */
70
71
72 static GLboolean
73 texenv_doing_secondary_color(struct gl_context *ctx)
74 {
75 if (ctx->Light.Enabled &&
76 (ctx->Light.Model.ColorControl == GL_SEPARATE_SPECULAR_COLOR))
77 return GL_TRUE;
78
79 if (ctx->Fog.ColorSumEnabled)
80 return GL_TRUE;
81
82 return GL_FALSE;
83 }
84
85 struct state_key {
86 GLuint nr_enabled_units:4;
87 GLuint separate_specular:1;
88 GLuint fog_mode:2; /**< FOG_x */
89 GLuint inputs_available:12;
90 GLuint num_draw_buffers:4;
91
92 /* NOTE: This array of structs must be last! (see "keySize" below) */
93 struct {
94 GLuint enabled:1;
95 GLuint source_index:4; /**< TEXTURE_x_INDEX */
96 GLuint shadow:1;
97
98 /***
99 * These are taken from struct gl_tex_env_combine_packed
100 * @{
101 */
102 GLuint ModeRGB:4;
103 GLuint ModeA:4;
104 GLuint ScaleShiftRGB:2;
105 GLuint ScaleShiftA:2;
106 GLuint NumArgsRGB:3;
107 GLuint NumArgsA:3;
108 struct gl_tex_env_argument ArgsRGB[MAX_COMBINER_TERMS];
109 struct gl_tex_env_argument ArgsA[MAX_COMBINER_TERMS];
110 /** @} */
111 } unit[MAX_TEXTURE_COORD_UNITS];
112 };
113
114
115 /**
116 * Do we need to clamp the results of the given texture env/combine mode?
117 * If the inputs to the mode are in [0,1] we don't always have to clamp
118 * the results.
119 */
120 static GLboolean
121 need_saturate( GLuint mode )
122 {
123 switch (mode) {
124 case TEXENV_MODE_REPLACE:
125 case TEXENV_MODE_MODULATE:
126 case TEXENV_MODE_INTERPOLATE:
127 return GL_FALSE;
128 case TEXENV_MODE_ADD:
129 case TEXENV_MODE_ADD_SIGNED:
130 case TEXENV_MODE_SUBTRACT:
131 case TEXENV_MODE_DOT3_RGB:
132 case TEXENV_MODE_DOT3_RGB_EXT:
133 case TEXENV_MODE_DOT3_RGBA:
134 case TEXENV_MODE_DOT3_RGBA_EXT:
135 case TEXENV_MODE_MODULATE_ADD_ATI:
136 case TEXENV_MODE_MODULATE_SIGNED_ADD_ATI:
137 case TEXENV_MODE_MODULATE_SUBTRACT_ATI:
138 case TEXENV_MODE_ADD_PRODUCTS_NV:
139 case TEXENV_MODE_ADD_PRODUCTS_SIGNED_NV:
140 return GL_TRUE;
141 default:
142 assert(0);
143 return GL_FALSE;
144 }
145 }
146
147 #define VERT_BIT_TEX_ANY (0xff << VERT_ATTRIB_TEX0)
148
149 /**
150 * Identify all possible varying inputs. The fragment program will
151 * never reference non-varying inputs, but will track them via state
152 * constants instead.
153 *
154 * This function figures out all the inputs that the fragment program
155 * has access to and filters input bitmask.
156 */
157 static GLbitfield filter_fp_input_mask( GLbitfield fp_inputs,
158 struct gl_context *ctx )
159 {
160 if (ctx->VertexProgram._Overriden) {
161 /* Somebody's messing with the vertex program and we don't have
162 * a clue what's happening. Assume that it could be producing
163 * all possible outputs.
164 */
165 return fp_inputs;
166 }
167
168 if (ctx->RenderMode == GL_FEEDBACK) {
169 /* _NEW_RENDERMODE */
170 return fp_inputs & (VARYING_BIT_COL0 | VARYING_BIT_TEX0);
171 }
172
173 /* _NEW_PROGRAM */
174 const GLboolean vertexShader =
175 ctx->_Shader->CurrentProgram[MESA_SHADER_VERTEX] != NULL;
176 const GLboolean vertexProgram = _mesa_arb_vertex_program_enabled(ctx);
177
178 if (!(vertexProgram || vertexShader)) {
179 /* Fixed function vertex logic */
180 GLbitfield possible_inputs = 0;
181
182 /* _NEW_VARYING_VP_INPUTS */
183 GLbitfield varying_inputs = ctx->varying_vp_inputs;
184 /* We only update ctx->varying_vp_inputs when in VP_MODE_FF _VPMode */
185 assert(VP_MODE_FF == ctx->VertexProgram._VPMode);
186
187 /* These get generated in the setup routine regardless of the
188 * vertex program:
189 */
190 /* _NEW_POINT */
191 if (ctx->Point.PointSprite) {
192 /* All texture varyings are possible to use */
193 possible_inputs = VARYING_BITS_TEX_ANY;
194 }
195 else {
196 /* _NEW_TEXTURE_STATE */
197 const GLbitfield possible_tex_inputs =
198 ctx->Texture._TexGenEnabled |
199 ctx->Texture._TexMatEnabled |
200 ((varying_inputs & VERT_BIT_TEX_ANY) >> VERT_ATTRIB_TEX0);
201
202 possible_inputs = (possible_tex_inputs << VARYING_SLOT_TEX0);
203 }
204
205 /* First look at what values may be computed by the generated
206 * vertex program:
207 */
208 /* _NEW_LIGHT */
209 if (ctx->Light.Enabled) {
210 possible_inputs |= VARYING_BIT_COL0;
211
212 if (texenv_doing_secondary_color(ctx))
213 possible_inputs |= VARYING_BIT_COL1;
214 }
215
216 /* Then look at what might be varying as a result of enabled
217 * arrays, etc:
218 */
219 if (varying_inputs & VERT_BIT_COLOR0)
220 possible_inputs |= VARYING_BIT_COL0;
221 if (varying_inputs & VERT_BIT_COLOR1)
222 possible_inputs |= VARYING_BIT_COL1;
223
224 return fp_inputs & possible_inputs;
225 }
226
227 /* calculate from vp->outputs */
228 struct gl_program *vprog;
229
230 /* Choose GLSL vertex shader over ARB vertex program. Need this
231 * since vertex shader state validation comes after fragment state
232 * validation (see additional comments in state.c).
233 */
234 if (ctx->_Shader->CurrentProgram[MESA_SHADER_GEOMETRY] != NULL)
235 vprog = ctx->_Shader->CurrentProgram[MESA_SHADER_GEOMETRY];
236 else if (ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_EVAL] != NULL)
237 vprog = ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_EVAL];
238 else if (vertexShader)
239 vprog = ctx->_Shader->CurrentProgram[MESA_SHADER_VERTEX];
240 else
241 vprog = ctx->VertexProgram.Current;
242
243 GLbitfield possible_inputs = vprog->info.outputs_written;
244
245 /* These get generated in the setup routine regardless of the
246 * vertex program:
247 */
248 /* _NEW_POINT */
249 if (ctx->Point.PointSprite) {
250 /* All texture varyings are possible to use */
251 possible_inputs |= VARYING_BITS_TEX_ANY;
252 }
253
254 return fp_inputs & possible_inputs;
255 }
256
257
258 /**
259 * Examine current texture environment state and generate a unique
260 * key to identify it.
261 */
262 static GLuint make_state_key( struct gl_context *ctx, struct state_key *key )
263 {
264 GLbitfield inputs_referenced = VARYING_BIT_COL0;
265 GLbitfield mask;
266 GLuint keySize;
267
268 memset(key, 0, sizeof(*key));
269
270 /* _NEW_TEXTURE_OBJECT */
271 mask = ctx->Texture._EnabledCoordUnits;
272 int i = -1;
273 while (mask) {
274 i = u_bit_scan(&mask);
275 const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[i];
276 const struct gl_texture_object *texObj = texUnit->_Current;
277 const struct gl_tex_env_combine_packed *comb =
278 &ctx->Texture.FixedFuncUnit[i]._CurrentCombinePacked;
279
280 if (!texObj)
281 continue;
282
283 key->unit[i].enabled = 1;
284 inputs_referenced |= VARYING_BIT_TEX(i);
285
286 key->unit[i].source_index = texObj->TargetIndex;
287
288 const struct gl_sampler_object *samp = _mesa_get_samplerobj(ctx, i);
289 if (samp->CompareMode == GL_COMPARE_R_TO_TEXTURE) {
290 const GLenum format = _mesa_texture_base_format(texObj);
291 key->unit[i].shadow = (format == GL_DEPTH_COMPONENT ||
292 format == GL_DEPTH_STENCIL_EXT);
293 }
294
295 key->unit[i].ModeRGB = comb->ModeRGB;
296 key->unit[i].ModeA = comb->ModeA;
297 key->unit[i].ScaleShiftRGB = comb->ScaleShiftRGB;
298 key->unit[i].ScaleShiftA = comb->ScaleShiftA;
299 key->unit[i].NumArgsRGB = comb->NumArgsRGB;
300 key->unit[i].NumArgsA = comb->NumArgsA;
301
302 memcpy(key->unit[i].ArgsRGB, comb->ArgsRGB, sizeof comb->ArgsRGB);
303 memcpy(key->unit[i].ArgsA, comb->ArgsA, sizeof comb->ArgsA);
304 }
305
306 key->nr_enabled_units = i + 1;
307
308 /* _NEW_LIGHT | _NEW_FOG */
309 if (texenv_doing_secondary_color(ctx)) {
310 key->separate_specular = 1;
311 inputs_referenced |= VARYING_BIT_COL1;
312 }
313
314 /* _NEW_FOG */
315 key->fog_mode = ctx->Fog._PackedEnabledMode;
316
317 /* _NEW_BUFFERS */
318 key->num_draw_buffers = ctx->DrawBuffer->_NumColorDrawBuffers;
319
320 /* _NEW_COLOR */
321 if (ctx->Color.AlphaEnabled && key->num_draw_buffers == 0) {
322 /* if alpha test is enabled we need to emit at least one color */
323 key->num_draw_buffers = 1;
324 }
325
326 key->inputs_available = filter_fp_input_mask(inputs_referenced, ctx);
327
328 /* compute size of state key, ignoring unused texture units */
329 keySize = sizeof(*key) - sizeof(key->unit)
330 + key->nr_enabled_units * sizeof(key->unit[0]);
331
332 return keySize;
333 }
334
335
336 /** State used to build the fragment program:
337 */
338 class texenv_fragment_program : public ir_factory {
339 public:
340 struct gl_shader_program *shader_program;
341 struct gl_shader *shader;
342 exec_list *top_instructions;
343 struct state_key *state;
344
345 ir_variable *src_texture[MAX_TEXTURE_COORD_UNITS];
346 /* Reg containing each texture unit's sampled texture color,
347 * else undef.
348 */
349
350 /* Texcoord override from bumpmapping. */
351 ir_variable *texcoord_tex[MAX_TEXTURE_COORD_UNITS];
352
353 /* Reg containing texcoord for a texture unit,
354 * needed for bump mapping, else undef.
355 */
356
357 ir_rvalue *src_previous; /**< Reg containing color from previous
358 * stage. May need to be decl'd.
359 */
360 };
361
362 static ir_rvalue *
363 get_current_attrib(texenv_fragment_program *p, GLuint attrib)
364 {
365 ir_variable *current;
366 ir_rvalue *val;
367
368 current = p->shader->symbols->get_variable("gl_CurrentAttribFragMESA");
369 assert(current);
370 current->data.max_array_access = MAX2(current->data.max_array_access, (int)attrib);
371 val = new(p->mem_ctx) ir_dereference_variable(current);
372 ir_rvalue *index = new(p->mem_ctx) ir_constant(attrib);
373 return new(p->mem_ctx) ir_dereference_array(val, index);
374 }
375
376 static ir_rvalue *
377 get_gl_Color(texenv_fragment_program *p)
378 {
379 if (p->state->inputs_available & VARYING_BIT_COL0) {
380 ir_variable *var = p->shader->symbols->get_variable("gl_Color");
381 assert(var);
382 return new(p->mem_ctx) ir_dereference_variable(var);
383 } else {
384 return get_current_attrib(p, VERT_ATTRIB_COLOR0);
385 }
386 }
387
388 static ir_rvalue *
389 get_source(texenv_fragment_program *p,
390 GLuint src, GLuint unit)
391 {
392 ir_variable *var;
393 ir_dereference *deref;
394
395 switch (src) {
396 case TEXENV_SRC_TEXTURE:
397 return new(p->mem_ctx) ir_dereference_variable(p->src_texture[unit]);
398
399 case TEXENV_SRC_TEXTURE0:
400 case TEXENV_SRC_TEXTURE1:
401 case TEXENV_SRC_TEXTURE2:
402 case TEXENV_SRC_TEXTURE3:
403 case TEXENV_SRC_TEXTURE4:
404 case TEXENV_SRC_TEXTURE5:
405 case TEXENV_SRC_TEXTURE6:
406 case TEXENV_SRC_TEXTURE7:
407 return new(p->mem_ctx)
408 ir_dereference_variable(p->src_texture[src - TEXENV_SRC_TEXTURE0]);
409
410 case TEXENV_SRC_CONSTANT:
411 var = p->shader->symbols->get_variable("gl_TextureEnvColor");
412 assert(var);
413 deref = new(p->mem_ctx) ir_dereference_variable(var);
414 var->data.max_array_access = MAX2(var->data.max_array_access, (int)unit);
415 return new(p->mem_ctx) ir_dereference_array(deref,
416 new(p->mem_ctx) ir_constant(unit));
417
418 case TEXENV_SRC_PRIMARY_COLOR:
419 var = p->shader->symbols->get_variable("gl_Color");
420 assert(var);
421 return new(p->mem_ctx) ir_dereference_variable(var);
422
423 case TEXENV_SRC_ZERO:
424 return new(p->mem_ctx) ir_constant(0.0f);
425
426 case TEXENV_SRC_ONE:
427 return new(p->mem_ctx) ir_constant(1.0f);
428
429 case TEXENV_SRC_PREVIOUS:
430 if (!p->src_previous) {
431 return get_gl_Color(p);
432 } else {
433 return p->src_previous->clone(p->mem_ctx, NULL);
434 }
435
436 default:
437 assert(0);
438 return NULL;
439 }
440 }
441
442 static ir_rvalue *
443 emit_combine_source(texenv_fragment_program *p,
444 GLuint unit,
445 GLuint source,
446 GLuint operand)
447 {
448 ir_rvalue *src;
449
450 src = get_source(p, source, unit);
451
452 switch (operand) {
453 case TEXENV_OPR_ONE_MINUS_COLOR:
454 return sub(new(p->mem_ctx) ir_constant(1.0f), src);
455
456 case TEXENV_OPR_ALPHA:
457 return src->type->is_scalar() ? src : swizzle_w(src);
458
459 case TEXENV_OPR_ONE_MINUS_ALPHA: {
460 ir_rvalue *const scalar = src->type->is_scalar() ? src : swizzle_w(src);
461
462 return sub(new(p->mem_ctx) ir_constant(1.0f), scalar);
463 }
464
465 case TEXENV_OPR_COLOR:
466 return src;
467
468 default:
469 assert(0);
470 return src;
471 }
472 }
473
474 /**
475 * Check if the RGB and Alpha sources and operands match for the given
476 * texture unit's combinder state. When the RGB and A sources and
477 * operands match, we can emit fewer instructions.
478 */
479 static GLboolean args_match( const struct state_key *key, GLuint unit )
480 {
481 GLuint i, numArgs = key->unit[unit].NumArgsRGB;
482
483 for (i = 0; i < numArgs; i++) {
484 if (key->unit[unit].ArgsA[i].Source != key->unit[unit].ArgsRGB[i].Source)
485 return GL_FALSE;
486
487 switch (key->unit[unit].ArgsA[i].Operand) {
488 case TEXENV_OPR_ALPHA:
489 switch (key->unit[unit].ArgsRGB[i].Operand) {
490 case TEXENV_OPR_COLOR:
491 case TEXENV_OPR_ALPHA:
492 break;
493 default:
494 return GL_FALSE;
495 }
496 break;
497 case TEXENV_OPR_ONE_MINUS_ALPHA:
498 switch (key->unit[unit].ArgsRGB[i].Operand) {
499 case TEXENV_OPR_ONE_MINUS_COLOR:
500 case TEXENV_OPR_ONE_MINUS_ALPHA:
501 break;
502 default:
503 return GL_FALSE;
504 }
505 break;
506 default:
507 return GL_FALSE; /* impossible */
508 }
509 }
510
511 return GL_TRUE;
512 }
513
514 static ir_rvalue *
515 smear(ir_rvalue *val)
516 {
517 if (!val->type->is_scalar())
518 return val;
519
520 return swizzle_xxxx(val);
521 }
522
523 static ir_rvalue *
524 emit_combine(texenv_fragment_program *p,
525 GLuint unit,
526 GLuint nr,
527 GLuint mode,
528 const struct gl_tex_env_argument *opt)
529 {
530 ir_rvalue *src[MAX_COMBINER_TERMS];
531 ir_rvalue *tmp0, *tmp1;
532 GLuint i;
533
534 assert(nr <= MAX_COMBINER_TERMS);
535
536 for (i = 0; i < nr; i++)
537 src[i] = emit_combine_source( p, unit, opt[i].Source, opt[i].Operand );
538
539 switch (mode) {
540 case TEXENV_MODE_REPLACE:
541 return src[0];
542
543 case TEXENV_MODE_MODULATE:
544 return mul(src[0], src[1]);
545
546 case TEXENV_MODE_ADD:
547 return add(src[0], src[1]);
548
549 case TEXENV_MODE_ADD_SIGNED:
550 return add(add(src[0], src[1]), new(p->mem_ctx) ir_constant(-0.5f));
551
552 case TEXENV_MODE_INTERPOLATE:
553 /* Arg0 * (Arg2) + Arg1 * (1-Arg2) */
554 tmp0 = mul(src[0], src[2]);
555 tmp1 = mul(src[1], sub(new(p->mem_ctx) ir_constant(1.0f),
556 src[2]->clone(p->mem_ctx, NULL)));
557 return add(tmp0, tmp1);
558
559 case TEXENV_MODE_SUBTRACT:
560 return sub(src[0], src[1]);
561
562 case TEXENV_MODE_DOT3_RGBA:
563 case TEXENV_MODE_DOT3_RGBA_EXT:
564 case TEXENV_MODE_DOT3_RGB_EXT:
565 case TEXENV_MODE_DOT3_RGB: {
566 tmp0 = mul(src[0], new(p->mem_ctx) ir_constant(2.0f));
567 tmp0 = add(tmp0, new(p->mem_ctx) ir_constant(-1.0f));
568
569 tmp1 = mul(src[1], new(p->mem_ctx) ir_constant(2.0f));
570 tmp1 = add(tmp1, new(p->mem_ctx) ir_constant(-1.0f));
571
572 return dot(swizzle_xyz(smear(tmp0)), swizzle_xyz(smear(tmp1)));
573 }
574 case TEXENV_MODE_MODULATE_ADD_ATI:
575 return add(mul(src[0], src[2]), src[1]);
576
577 case TEXENV_MODE_MODULATE_SIGNED_ADD_ATI:
578 return add(add(mul(src[0], src[2]), src[1]),
579 new(p->mem_ctx) ir_constant(-0.5f));
580
581 case TEXENV_MODE_MODULATE_SUBTRACT_ATI:
582 return sub(mul(src[0], src[2]), src[1]);
583
584 case TEXENV_MODE_ADD_PRODUCTS_NV:
585 return add(mul(src[0], src[1]), mul(src[2], src[3]));
586
587 case TEXENV_MODE_ADD_PRODUCTS_SIGNED_NV:
588 return add(add(mul(src[0], src[1]), mul(src[2], src[3])),
589 new(p->mem_ctx) ir_constant(-0.5f));
590 default:
591 assert(0);
592 return src[0];
593 }
594 }
595
596 /**
597 * Generate instructions for one texture unit's env/combiner mode.
598 */
599 static ir_rvalue *
600 emit_texenv(texenv_fragment_program *p, GLuint unit)
601 {
602 const struct state_key *key = p->state;
603 GLboolean rgb_saturate, alpha_saturate;
604 GLuint rgb_shift, alpha_shift;
605
606 if (!key->unit[unit].enabled) {
607 return get_source(p, TEXENV_SRC_PREVIOUS, 0);
608 }
609
610 switch (key->unit[unit].ModeRGB) {
611 case TEXENV_MODE_DOT3_RGB_EXT:
612 alpha_shift = key->unit[unit].ScaleShiftA;
613 rgb_shift = 0;
614 break;
615 case TEXENV_MODE_DOT3_RGBA_EXT:
616 alpha_shift = 0;
617 rgb_shift = 0;
618 break;
619 default:
620 rgb_shift = key->unit[unit].ScaleShiftRGB;
621 alpha_shift = key->unit[unit].ScaleShiftA;
622 break;
623 }
624
625 /* If we'll do rgb/alpha shifting don't saturate in emit_combine().
626 * We don't want to clamp twice.
627 */
628 if (rgb_shift)
629 rgb_saturate = GL_FALSE; /* saturate after rgb shift */
630 else if (need_saturate(key->unit[unit].ModeRGB))
631 rgb_saturate = GL_TRUE;
632 else
633 rgb_saturate = GL_FALSE;
634
635 if (alpha_shift)
636 alpha_saturate = GL_FALSE; /* saturate after alpha shift */
637 else if (need_saturate(key->unit[unit].ModeA))
638 alpha_saturate = GL_TRUE;
639 else
640 alpha_saturate = GL_FALSE;
641
642 ir_variable *temp_var = p->make_temp(glsl_type::vec4_type, "texenv_combine");
643 ir_dereference *deref;
644 ir_rvalue *val;
645
646 /* Emit the RGB and A combine ops
647 */
648 if (key->unit[unit].ModeRGB == key->unit[unit].ModeA &&
649 args_match(key, unit)) {
650 val = emit_combine(p, unit,
651 key->unit[unit].NumArgsRGB,
652 key->unit[unit].ModeRGB,
653 key->unit[unit].ArgsRGB);
654 val = smear(val);
655 if (rgb_saturate)
656 val = saturate(val);
657
658 p->emit(assign(temp_var, val));
659 }
660 else if (key->unit[unit].ModeRGB == TEXENV_MODE_DOT3_RGBA_EXT ||
661 key->unit[unit].ModeRGB == TEXENV_MODE_DOT3_RGBA) {
662 ir_rvalue *val = emit_combine(p, unit,
663 key->unit[unit].NumArgsRGB,
664 key->unit[unit].ModeRGB,
665 key->unit[unit].ArgsRGB);
666 val = smear(val);
667 if (rgb_saturate)
668 val = saturate(val);
669 p->emit(assign(temp_var, val));
670 }
671 else {
672 /* Need to do something to stop from re-emitting identical
673 * argument calculations here:
674 */
675 val = emit_combine(p, unit,
676 key->unit[unit].NumArgsRGB,
677 key->unit[unit].ModeRGB,
678 key->unit[unit].ArgsRGB);
679 val = swizzle_xyz(smear(val));
680 if (rgb_saturate)
681 val = saturate(val);
682 p->emit(assign(temp_var, val, WRITEMASK_XYZ));
683
684 val = emit_combine(p, unit,
685 key->unit[unit].NumArgsA,
686 key->unit[unit].ModeA,
687 key->unit[unit].ArgsA);
688 val = swizzle_w(smear(val));
689 if (alpha_saturate)
690 val = saturate(val);
691 p->emit(assign(temp_var, val, WRITEMASK_W));
692 }
693
694 deref = new(p->mem_ctx) ir_dereference_variable(temp_var);
695
696 /* Deal with the final shift:
697 */
698 if (alpha_shift || rgb_shift) {
699 ir_constant *shift;
700
701 if (rgb_shift == alpha_shift) {
702 shift = new(p->mem_ctx) ir_constant((float)(1 << rgb_shift));
703 }
704 else {
705 ir_constant_data const_data;
706
707 const_data.f[0] = float(1 << rgb_shift);
708 const_data.f[1] = float(1 << rgb_shift);
709 const_data.f[2] = float(1 << rgb_shift);
710 const_data.f[3] = float(1 << alpha_shift);
711
712 shift = new(p->mem_ctx) ir_constant(glsl_type::vec4_type,
713 &const_data);
714 }
715
716 return saturate(mul(deref, shift));
717 }
718 else
719 return deref;
720 }
721
722
723 /**
724 * Generate instruction for getting a texture source term.
725 */
726 static void load_texture( texenv_fragment_program *p, GLuint unit )
727 {
728 ir_dereference *deref;
729
730 if (p->src_texture[unit])
731 return;
732
733 const GLuint texTarget = p->state->unit[unit].source_index;
734 ir_rvalue *texcoord;
735
736 if (!(p->state->inputs_available & (VARYING_BIT_TEX0 << unit))) {
737 texcoord = get_current_attrib(p, VERT_ATTRIB_TEX0 + unit);
738 } else if (p->texcoord_tex[unit]) {
739 texcoord = new(p->mem_ctx) ir_dereference_variable(p->texcoord_tex[unit]);
740 } else {
741 ir_variable *tc_array = p->shader->symbols->get_variable("gl_TexCoord");
742 assert(tc_array);
743 texcoord = new(p->mem_ctx) ir_dereference_variable(tc_array);
744 ir_rvalue *index = new(p->mem_ctx) ir_constant(unit);
745 texcoord = new(p->mem_ctx) ir_dereference_array(texcoord, index);
746 tc_array->data.max_array_access = MAX2(tc_array->data.max_array_access, (int)unit);
747 }
748
749 if (!p->state->unit[unit].enabled) {
750 p->src_texture[unit] = p->make_temp(glsl_type::vec4_type,
751 "dummy_tex");
752 p->emit(p->src_texture[unit]);
753
754 p->emit(assign(p->src_texture[unit], new(p->mem_ctx) ir_constant(0.0f)));
755 return ;
756 }
757
758 const glsl_type *sampler_type = NULL;
759 int coords = 0;
760
761 switch (texTarget) {
762 case TEXTURE_1D_INDEX:
763 if (p->state->unit[unit].shadow)
764 sampler_type = glsl_type::sampler1DShadow_type;
765 else
766 sampler_type = glsl_type::sampler1D_type;
767 coords = 1;
768 break;
769 case TEXTURE_1D_ARRAY_INDEX:
770 if (p->state->unit[unit].shadow)
771 sampler_type = glsl_type::sampler1DArrayShadow_type;
772 else
773 sampler_type = glsl_type::sampler1DArray_type;
774 coords = 2;
775 break;
776 case TEXTURE_2D_INDEX:
777 if (p->state->unit[unit].shadow)
778 sampler_type = glsl_type::sampler2DShadow_type;
779 else
780 sampler_type = glsl_type::sampler2D_type;
781 coords = 2;
782 break;
783 case TEXTURE_2D_ARRAY_INDEX:
784 if (p->state->unit[unit].shadow)
785 sampler_type = glsl_type::sampler2DArrayShadow_type;
786 else
787 sampler_type = glsl_type::sampler2DArray_type;
788 coords = 3;
789 break;
790 case TEXTURE_RECT_INDEX:
791 if (p->state->unit[unit].shadow)
792 sampler_type = glsl_type::sampler2DRectShadow_type;
793 else
794 sampler_type = glsl_type::sampler2DRect_type;
795 coords = 2;
796 break;
797 case TEXTURE_3D_INDEX:
798 assert(!p->state->unit[unit].shadow);
799 sampler_type = glsl_type::sampler3D_type;
800 coords = 3;
801 break;
802 case TEXTURE_CUBE_INDEX:
803 if (p->state->unit[unit].shadow)
804 sampler_type = glsl_type::samplerCubeShadow_type;
805 else
806 sampler_type = glsl_type::samplerCube_type;
807 coords = 3;
808 break;
809 case TEXTURE_EXTERNAL_INDEX:
810 assert(!p->state->unit[unit].shadow);
811 sampler_type = glsl_type::samplerExternalOES_type;
812 coords = 2;
813 break;
814 }
815
816 p->src_texture[unit] = p->make_temp(glsl_type::vec4_type,
817 "tex");
818
819 ir_texture *tex = new(p->mem_ctx) ir_texture(ir_tex);
820
821
822 char *sampler_name = ralloc_asprintf(p->mem_ctx, "sampler_%d", unit);
823 ir_variable *sampler = new(p->mem_ctx) ir_variable(sampler_type,
824 sampler_name,
825 ir_var_uniform);
826 p->top_instructions->push_head(sampler);
827
828 /* Set the texture unit for this sampler in the same way that
829 * layout(binding=X) would.
830 */
831 sampler->data.explicit_binding = true;
832 sampler->data.binding = unit;
833
834 deref = new(p->mem_ctx) ir_dereference_variable(sampler);
835 tex->set_sampler(deref, glsl_type::vec4_type);
836
837 tex->coordinate = new(p->mem_ctx) ir_swizzle(texcoord, 0, 1, 2, 3, coords);
838
839 if (p->state->unit[unit].shadow) {
840 texcoord = texcoord->clone(p->mem_ctx, NULL);
841 tex->shadow_comparator = new(p->mem_ctx) ir_swizzle(texcoord,
842 coords, 0, 0, 0,
843 1);
844 coords++;
845 }
846
847 texcoord = texcoord->clone(p->mem_ctx, NULL);
848 tex->projector = swizzle_w(texcoord);
849
850 p->emit(assign(p->src_texture[unit], tex));
851 }
852
853 static void
854 load_texenv_source(texenv_fragment_program *p,
855 GLuint src, GLuint unit)
856 {
857 switch (src) {
858 case TEXENV_SRC_TEXTURE:
859 load_texture(p, unit);
860 break;
861
862 case TEXENV_SRC_TEXTURE0:
863 case TEXENV_SRC_TEXTURE1:
864 case TEXENV_SRC_TEXTURE2:
865 case TEXENV_SRC_TEXTURE3:
866 case TEXENV_SRC_TEXTURE4:
867 case TEXENV_SRC_TEXTURE5:
868 case TEXENV_SRC_TEXTURE6:
869 case TEXENV_SRC_TEXTURE7:
870 load_texture(p, src - TEXENV_SRC_TEXTURE0);
871 break;
872
873 default:
874 /* not a texture src - do nothing */
875 break;
876 }
877 }
878
879
880 /**
881 * Generate instructions for loading all texture source terms.
882 */
883 static GLboolean
884 load_texunit_sources( texenv_fragment_program *p, GLuint unit )
885 {
886 const struct state_key *key = p->state;
887 GLuint i;
888
889 for (i = 0; i < key->unit[unit].NumArgsRGB; i++) {
890 load_texenv_source( p, key->unit[unit].ArgsRGB[i].Source, unit );
891 }
892
893 for (i = 0; i < key->unit[unit].NumArgsA; i++) {
894 load_texenv_source( p, key->unit[unit].ArgsA[i].Source, unit );
895 }
896
897 return GL_TRUE;
898 }
899
900 /**
901 * Applies the fog calculations.
902 *
903 * This is basically like the ARB_fragment_prorgam fog options. Note
904 * that ffvertex_prog.c produces fogcoord for us when
905 * GL_FOG_COORDINATE_EXT is set to GL_FRAGMENT_DEPTH_EXT.
906 */
907 static ir_rvalue *
908 emit_fog_instructions(texenv_fragment_program *p,
909 ir_rvalue *fragcolor)
910 {
911 struct state_key *key = p->state;
912 ir_rvalue *f, *temp;
913 ir_variable *params, *oparams;
914 ir_variable *fogcoord;
915
916 /* Temporary storage for the whole fog result. Fog calculations
917 * only affect rgb so we're hanging on to the .a value of fragcolor
918 * this way.
919 */
920 ir_variable *fog_result = p->make_temp(glsl_type::vec4_type, "fog_result");
921 p->emit(assign(fog_result, fragcolor));
922
923 fragcolor = swizzle_xyz(fog_result);
924
925 oparams = p->shader->symbols->get_variable("gl_FogParamsOptimizedMESA");
926 assert(oparams);
927 fogcoord = p->shader->symbols->get_variable("gl_FogFragCoord");
928 assert(fogcoord);
929 params = p->shader->symbols->get_variable("gl_Fog");
930 assert(params);
931 f = new(p->mem_ctx) ir_dereference_variable(fogcoord);
932
933 ir_variable *f_var = p->make_temp(glsl_type::float_type, "fog_factor");
934
935 switch (key->fog_mode) {
936 case FOG_LINEAR:
937 /* f = (end - z) / (end - start)
938 *
939 * gl_MesaFogParamsOptimized gives us (-1 / (end - start)) and
940 * (end / (end - start)) so we can generate a single MAD.
941 */
942 f = add(mul(f, swizzle_x(oparams)), swizzle_y(oparams));
943 break;
944 case FOG_EXP:
945 /* f = e^(-(density * fogcoord))
946 *
947 * gl_MesaFogParamsOptimized gives us density/ln(2) so we can
948 * use EXP2 which is generally the native instruction without
949 * having to do any further math on the fog density uniform.
950 */
951 f = mul(f, swizzle_z(oparams));
952 f = new(p->mem_ctx) ir_expression(ir_unop_neg, f);
953 f = new(p->mem_ctx) ir_expression(ir_unop_exp2, f);
954 break;
955 case FOG_EXP2:
956 /* f = e^(-(density * fogcoord)^2)
957 *
958 * gl_MesaFogParamsOptimized gives us density/sqrt(ln(2)) so we
959 * can do this like FOG_EXP but with a squaring after the
960 * multiply by density.
961 */
962 ir_variable *temp_var = p->make_temp(glsl_type::float_type, "fog_temp");
963 p->emit(assign(temp_var, mul(f, swizzle_w(oparams))));
964
965 f = mul(temp_var, temp_var);
966 f = new(p->mem_ctx) ir_expression(ir_unop_neg, f);
967 f = new(p->mem_ctx) ir_expression(ir_unop_exp2, f);
968 break;
969 }
970
971 p->emit(assign(f_var, saturate(f)));
972
973 f = sub(new(p->mem_ctx) ir_constant(1.0f), f_var);
974 temp = new(p->mem_ctx) ir_dereference_variable(params);
975 temp = new(p->mem_ctx) ir_dereference_record(temp, "color");
976 temp = mul(swizzle_xyz(temp), f);
977
978 p->emit(assign(fog_result, add(temp, mul(fragcolor, f_var)), WRITEMASK_XYZ));
979
980 return new(p->mem_ctx) ir_dereference_variable(fog_result);
981 }
982
983 static void
984 emit_instructions(texenv_fragment_program *p)
985 {
986 struct state_key *key = p->state;
987 GLuint unit;
988
989 if (key->nr_enabled_units) {
990 /* First pass - to support texture_env_crossbar, first identify
991 * all referenced texture sources and emit texld instructions
992 * for each:
993 */
994 for (unit = 0; unit < key->nr_enabled_units; unit++)
995 if (key->unit[unit].enabled) {
996 load_texunit_sources(p, unit);
997 }
998
999 /* Second pass - emit combine instructions to build final color:
1000 */
1001 for (unit = 0; unit < key->nr_enabled_units; unit++) {
1002 if (key->unit[unit].enabled) {
1003 p->src_previous = emit_texenv(p, unit);
1004 }
1005 }
1006 }
1007
1008 ir_rvalue *cf = get_source(p, TEXENV_SRC_PREVIOUS, 0);
1009
1010 if (key->separate_specular) {
1011 ir_variable *spec_result = p->make_temp(glsl_type::vec4_type,
1012 "specular_add");
1013 p->emit(assign(spec_result, cf));
1014
1015 ir_rvalue *secondary;
1016 if (p->state->inputs_available & VARYING_BIT_COL1) {
1017 ir_variable *var =
1018 p->shader->symbols->get_variable("gl_SecondaryColor");
1019 assert(var);
1020 secondary = swizzle_xyz(var);
1021 } else {
1022 secondary = swizzle_xyz(get_current_attrib(p, VERT_ATTRIB_COLOR1));
1023 }
1024
1025 p->emit(assign(spec_result, add(swizzle_xyz(spec_result), secondary),
1026 WRITEMASK_XYZ));
1027
1028 cf = new(p->mem_ctx) ir_dereference_variable(spec_result);
1029 }
1030
1031 if (key->fog_mode) {
1032 cf = emit_fog_instructions(p, cf);
1033 }
1034
1035 ir_variable *frag_color = p->shader->symbols->get_variable("gl_FragColor");
1036 assert(frag_color);
1037 p->emit(assign(frag_color, cf));
1038 }
1039
1040 /**
1041 * Generate a new fragment program which implements the context's
1042 * current texture env/combine mode.
1043 */
1044 static struct gl_shader_program *
1045 create_new_program(struct gl_context *ctx, struct state_key *key)
1046 {
1047 texenv_fragment_program p;
1048 unsigned int unit;
1049 _mesa_glsl_parse_state *state;
1050
1051 p.mem_ctx = ralloc_context(NULL);
1052 p.shader = _mesa_new_shader(0, MESA_SHADER_FRAGMENT);
1053 #ifdef DEBUG
1054 p.shader->SourceChecksum = 0xf18ed; /* fixed */
1055 #endif
1056 p.shader->ir = new(p.shader) exec_list;
1057 state = new(p.shader) _mesa_glsl_parse_state(ctx, MESA_SHADER_FRAGMENT,
1058 p.shader);
1059 p.shader->symbols = state->symbols;
1060 p.top_instructions = p.shader->ir;
1061 p.instructions = p.shader->ir;
1062 p.state = key;
1063 p.shader_program = _mesa_new_shader_program(0);
1064
1065 /* Tell the linker to ignore the fact that we're building a
1066 * separate shader, in case we're in a GLES2 context that would
1067 * normally reject that. The real problem is that we're building a
1068 * fixed function program in a GLES2 context at all, but that's a
1069 * big mess to clean up.
1070 */
1071 p.shader_program->SeparateShader = GL_TRUE;
1072
1073 /* The legacy GLSL shadow functions follow the depth texture
1074 * mode and return vec4. The GLSL 1.30 shadow functions return float and
1075 * ignore the depth texture mode. That's a shader and state dependency
1076 * that's difficult to deal with. st/mesa uses a simple but not
1077 * completely correct solution: if the shader declares GLSL >= 1.30 and
1078 * the depth texture mode is GL_ALPHA (000X), it sets the XXXX swizzle
1079 * instead. Thus, the GLSL 1.30 shadow function will get the result in .x
1080 * and legacy shadow functions will get it in .w as expected.
1081 * For the fixed-function fragment shader, use 120 to get correct behavior
1082 * for GL_ALPHA.
1083 */
1084 state->language_version = 120;
1085
1086 state->es_shader = false;
1087 if (_mesa_is_gles(ctx) && ctx->Extensions.OES_EGL_image_external)
1088 state->OES_EGL_image_external_enable = true;
1089 _mesa_glsl_initialize_types(state);
1090 _mesa_glsl_initialize_variables(p.instructions, state);
1091
1092 for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++) {
1093 p.src_texture[unit] = NULL;
1094 p.texcoord_tex[unit] = NULL;
1095 }
1096
1097 p.src_previous = NULL;
1098
1099 ir_function *main_f = new(p.mem_ctx) ir_function("main");
1100 p.emit(main_f);
1101 state->symbols->add_function(main_f);
1102
1103 ir_function_signature *main_sig =
1104 new(p.mem_ctx) ir_function_signature(glsl_type::void_type);
1105 main_sig->is_defined = true;
1106 main_f->add_signature(main_sig);
1107
1108 p.instructions = &main_sig->body;
1109 if (key->num_draw_buffers)
1110 emit_instructions(&p);
1111
1112 validate_ir_tree(p.shader->ir);
1113
1114 const struct gl_shader_compiler_options *options =
1115 &ctx->Const.ShaderCompilerOptions[MESA_SHADER_FRAGMENT];
1116
1117 /* Conservative approach: Don't optimize here, the linker does it too. */
1118 if (!ctx->Const.GLSLOptimizeConservatively) {
1119 while (do_common_optimization(p.shader->ir, false, false, options,
1120 ctx->Const.NativeIntegers))
1121 ;
1122 }
1123
1124 reparent_ir(p.shader->ir, p.shader->ir);
1125
1126 p.shader->CompileStatus = COMPILE_SUCCESS;
1127 p.shader->Version = state->language_version;
1128 p.shader_program->Shaders =
1129 (gl_shader **)malloc(sizeof(*p.shader_program->Shaders));
1130 p.shader_program->Shaders[0] = p.shader;
1131 p.shader_program->NumShaders = 1;
1132
1133 _mesa_glsl_link_shader(ctx, p.shader_program);
1134
1135 if (!p.shader_program->data->LinkStatus)
1136 _mesa_problem(ctx, "Failed to link fixed function fragment shader: %s\n",
1137 p.shader_program->data->InfoLog);
1138
1139 ralloc_free(p.mem_ctx);
1140 return p.shader_program;
1141 }
1142
1143 extern "C" {
1144
1145 /**
1146 * Return a fragment program which implements the current
1147 * fixed-function texture, fog and color-sum operations.
1148 */
1149 struct gl_shader_program *
1150 _mesa_get_fixed_func_fragment_program(struct gl_context *ctx)
1151 {
1152 struct gl_shader_program *shader_program;
1153 struct state_key key;
1154 GLuint keySize;
1155
1156 keySize = make_state_key(ctx, &key);
1157
1158 shader_program = (struct gl_shader_program *)
1159 _mesa_search_program_cache(ctx->FragmentProgram.Cache,
1160 &key, keySize);
1161
1162 if (!shader_program) {
1163 shader_program = create_new_program(ctx, &key);
1164
1165 _mesa_shader_cache_insert(ctx, ctx->FragmentProgram.Cache,
1166 &key, keySize, shader_program);
1167 }
1168
1169 return shader_program;
1170 }
1171
1172 }