xref: /aosp_15_r20/external/mesa3d/src/mesa/main/texenv.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Mesa 3-D graphics library
3  *
4  * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
5  * Copyright (C) 2009  VMware, Inc.  All Rights Reserved.
6  *
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:
13  *
14  * The above copyright notice and this permission notice shall be included
15  * in all copies or substantial portions of the Software.
16  *
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.
24  */
25 
26 /**
27  * \file texenv.c
28  *
29  * glTexEnv-related functions
30  */
31 
32 
33 #include "util/glheader.h"
34 #include "main/context.h"
35 #include "main/blend.h"
36 #include "main/enums.h"
37 #include "main/macros.h"
38 #include "main/mtypes.h"
39 #include "main/state.h"
40 #include "main/texstate.h"
41 #include "api_exec_decl.h"
42 
43 
44 #define TE_ERROR(errCode, msg, value)				\
45    _mesa_error(ctx, errCode, msg, _mesa_enum_to_string(value));
46 
47 
48 /** Set texture env mode */
49 static void
set_env_mode(struct gl_context * ctx,struct gl_fixedfunc_texture_unit * texUnit,GLenum mode)50 set_env_mode(struct gl_context *ctx,
51              struct gl_fixedfunc_texture_unit *texUnit,
52              GLenum mode)
53 {
54    GLboolean legal;
55 
56    if (texUnit->EnvMode == mode)
57       return;
58 
59    switch (mode) {
60    case GL_MODULATE:
61    case GL_BLEND:
62    case GL_DECAL:
63    case GL_REPLACE:
64    case GL_ADD:
65    case GL_COMBINE:
66       legal = GL_TRUE;
67       break;
68    case GL_REPLACE_EXT:
69       mode = GL_REPLACE; /* GL_REPLACE_EXT != GL_REPLACE */
70       legal = GL_TRUE;
71       break;
72    case GL_COMBINE4_NV:
73       legal = ctx->Extensions.NV_texture_env_combine4;
74       break;
75    default:
76       legal = GL_FALSE;
77    }
78 
79    if (legal) {
80       FLUSH_VERTICES(ctx, _NEW_TEXTURE_STATE, GL_TEXTURE_BIT);
81       texUnit->EnvMode = mode;
82    }
83    else {
84       TE_ERROR(GL_INVALID_ENUM, "glTexEnv(param=%s)", mode);
85    }
86 }
87 
88 
89 static void
set_env_color(struct gl_context * ctx,struct gl_fixedfunc_texture_unit * texUnit,const GLfloat * color)90 set_env_color(struct gl_context *ctx,
91               struct gl_fixedfunc_texture_unit *texUnit,
92               const GLfloat *color)
93 {
94    if (TEST_EQ_4V(color, texUnit->EnvColorUnclamped))
95       return;
96    FLUSH_VERTICES(ctx, _NEW_TEXTURE_STATE, GL_TEXTURE_BIT);
97    COPY_4FV(texUnit->EnvColorUnclamped, color);
98    texUnit->EnvColor[0] = CLAMP(color[0], 0.0F, 1.0F);
99    texUnit->EnvColor[1] = CLAMP(color[1], 0.0F, 1.0F);
100    texUnit->EnvColor[2] = CLAMP(color[2], 0.0F, 1.0F);
101    texUnit->EnvColor[3] = CLAMP(color[3], 0.0F, 1.0F);
102 }
103 
104 
105 /** Set an RGB or A combiner mode/function */
106 static bool
set_combiner_mode(struct gl_context * ctx,struct gl_fixedfunc_texture_unit * texUnit,GLenum pname,GLenum mode)107 set_combiner_mode(struct gl_context *ctx,
108                   struct gl_fixedfunc_texture_unit *texUnit,
109                   GLenum pname, GLenum mode)
110 {
111    GLboolean legal;
112 
113    switch (mode) {
114    case GL_REPLACE:
115    case GL_MODULATE:
116    case GL_ADD:
117    case GL_ADD_SIGNED:
118    case GL_INTERPOLATE:
119    case GL_SUBTRACT:
120       legal = GL_TRUE;
121       break;
122    case GL_DOT3_RGB_EXT:
123    case GL_DOT3_RGBA_EXT:
124       legal = (_mesa_is_desktop_gl_compat(ctx) &&
125                ctx->Extensions.EXT_texture_env_dot3 &&
126                pname == GL_COMBINE_RGB);
127       break;
128    case GL_DOT3_RGB:
129    case GL_DOT3_RGBA:
130       legal = (pname == GL_COMBINE_RGB);
131       break;
132    case GL_MODULATE_ADD_ATI:
133    case GL_MODULATE_SIGNED_ADD_ATI:
134    case GL_MODULATE_SUBTRACT_ATI:
135       legal = (_mesa_is_desktop_gl_compat(ctx) &&
136                ctx->Extensions.ATI_texture_env_combine3);
137       break;
138    default:
139       legal = GL_FALSE;
140    }
141 
142    if (!legal) {
143       TE_ERROR(GL_INVALID_ENUM, "glTexEnv(param=%s)", mode);
144       return false;
145    }
146 
147    switch (pname) {
148    case GL_COMBINE_RGB:
149       if (texUnit->Combine.ModeRGB == mode)
150          return true;
151       FLUSH_VERTICES(ctx, _NEW_TEXTURE_STATE, GL_TEXTURE_BIT);
152       texUnit->Combine.ModeRGB = mode;
153       break;
154 
155    case GL_COMBINE_ALPHA:
156       if (texUnit->Combine.ModeA == mode)
157          return true;
158       FLUSH_VERTICES(ctx, _NEW_TEXTURE_STATE, GL_TEXTURE_BIT);
159       texUnit->Combine.ModeA = mode;
160       break;
161    default:
162       TE_ERROR(GL_INVALID_ENUM, "glTexEnv(pname=%s)", pname);
163       return false;
164    }
165 
166    return true;
167 }
168 
169 
170 
171 /** Set an RGB or A combiner source term */
172 static bool
set_combiner_source(struct gl_context * ctx,struct gl_fixedfunc_texture_unit * texUnit,GLenum pname,GLenum param)173 set_combiner_source(struct gl_context *ctx,
174                     struct gl_fixedfunc_texture_unit *texUnit,
175                     GLenum pname, GLenum param)
176 {
177    GLuint term;
178    GLboolean alpha, legal;
179 
180    /*
181     * Translate pname to (term, alpha).
182     *
183     * The enums were given sequential values for a reason.
184     */
185    switch (pname) {
186    case GL_SOURCE0_RGB:
187    case GL_SOURCE1_RGB:
188    case GL_SOURCE2_RGB:
189    case GL_SOURCE3_RGB_NV:
190       term = pname - GL_SOURCE0_RGB;
191       alpha = GL_FALSE;
192       break;
193    case GL_SOURCE0_ALPHA:
194    case GL_SOURCE1_ALPHA:
195    case GL_SOURCE2_ALPHA:
196    case GL_SOURCE3_ALPHA_NV:
197       term = pname - GL_SOURCE0_ALPHA;
198       alpha = GL_TRUE;
199       break;
200    default:
201       TE_ERROR(GL_INVALID_ENUM, "glTexEnv(pname=%s)", pname);
202       return false;
203    }
204 
205    if ((term == 3) && (ctx->API != API_OPENGL_COMPAT
206                        || !ctx->Extensions.NV_texture_env_combine4)) {
207       TE_ERROR(GL_INVALID_ENUM, "glTexEnv(pname=%s)", pname);
208       return false;
209    }
210 
211    assert(term < MAX_COMBINER_TERMS);
212 
213    /*
214     * Error-check param (the source term)
215     */
216    switch (param) {
217    case GL_TEXTURE:
218    case GL_CONSTANT:
219    case GL_PRIMARY_COLOR:
220    case GL_PREVIOUS:
221       legal = GL_TRUE;
222       break;
223    case GL_TEXTURE0:
224    case GL_TEXTURE1:
225    case GL_TEXTURE2:
226    case GL_TEXTURE3:
227    case GL_TEXTURE4:
228    case GL_TEXTURE5:
229    case GL_TEXTURE6:
230    case GL_TEXTURE7:
231       legal = (param - GL_TEXTURE0 < ctx->Const.MaxTextureUnits);
232       break;
233    case GL_ZERO:
234       legal = (_mesa_is_desktop_gl_compat(ctx) &&
235                (ctx->Extensions.ATI_texture_env_combine3 ||
236                 ctx->Extensions.NV_texture_env_combine4));
237       break;
238    case GL_ONE:
239       legal = (_mesa_is_desktop_gl_compat(ctx) &&
240                ctx->Extensions.ATI_texture_env_combine3);
241       break;
242    default:
243       legal = GL_FALSE;
244    }
245 
246    if (!legal) {
247       TE_ERROR(GL_INVALID_ENUM, "glTexEnv(param=%s)", param);
248       return false;
249    }
250 
251    FLUSH_VERTICES(ctx, _NEW_TEXTURE_STATE, GL_TEXTURE_BIT);
252 
253    if (alpha)
254       texUnit->Combine.SourceA[term] = param;
255    else
256       texUnit->Combine.SourceRGB[term] = param;
257 
258    return true;
259 }
260 
261 
262 /** Set an RGB or A combiner operand term */
263 static bool
set_combiner_operand(struct gl_context * ctx,struct gl_fixedfunc_texture_unit * texUnit,GLenum pname,GLenum param)264 set_combiner_operand(struct gl_context *ctx,
265                      struct gl_fixedfunc_texture_unit *texUnit,
266                      GLenum pname, GLenum param)
267 {
268    GLuint term;
269    GLboolean alpha, legal;
270 
271    /* The enums were given sequential values for a reason.
272     */
273    switch (pname) {
274    case GL_OPERAND0_RGB:
275    case GL_OPERAND1_RGB:
276    case GL_OPERAND2_RGB:
277    case GL_OPERAND3_RGB_NV:
278       term = pname - GL_OPERAND0_RGB;
279       alpha = GL_FALSE;
280       break;
281    case GL_OPERAND0_ALPHA:
282    case GL_OPERAND1_ALPHA:
283    case GL_OPERAND2_ALPHA:
284    case GL_OPERAND3_ALPHA_NV:
285       term = pname - GL_OPERAND0_ALPHA;
286       alpha = GL_TRUE;
287       break;
288    default:
289       TE_ERROR(GL_INVALID_ENUM, "glTexEnv(pname=%s)", pname);
290       return false;
291    }
292 
293    if ((term == 3) && (ctx->API != API_OPENGL_COMPAT
294                        || !ctx->Extensions.NV_texture_env_combine4)) {
295       TE_ERROR(GL_INVALID_ENUM, "glTexEnv(pname=%s)", pname);
296       return false;
297    }
298 
299    assert(term < MAX_COMBINER_TERMS);
300 
301    /*
302     * Error-check param (the source operand)
303     */
304    switch (param) {
305    case GL_SRC_COLOR:
306    case GL_ONE_MINUS_SRC_COLOR:
307       legal = !alpha;
308       break;
309    case GL_ONE_MINUS_SRC_ALPHA:
310    case GL_SRC_ALPHA:
311       legal = GL_TRUE;
312       break;
313    default:
314       legal = GL_FALSE;
315    }
316 
317    if (!legal) {
318       TE_ERROR(GL_INVALID_ENUM, "glTexEnv(param=%s)", param);
319       return false;
320    }
321 
322    FLUSH_VERTICES(ctx, _NEW_TEXTURE_STATE, GL_TEXTURE_BIT);
323 
324    if (alpha)
325       texUnit->Combine.OperandA[term] = param;
326    else
327       texUnit->Combine.OperandRGB[term] = param;
328 
329    return true;
330 }
331 
332 
333 static bool
set_combiner_scale(struct gl_context * ctx,struct gl_fixedfunc_texture_unit * texUnit,GLenum pname,GLfloat scale)334 set_combiner_scale(struct gl_context *ctx,
335                    struct gl_fixedfunc_texture_unit *texUnit,
336                    GLenum pname, GLfloat scale)
337 {
338    GLuint shift;
339 
340    if (scale == 1.0F) {
341       shift = 0;
342    }
343    else if (scale == 2.0F) {
344       shift = 1;
345    }
346    else if (scale == 4.0F) {
347       shift = 2;
348    }
349    else {
350       _mesa_error( ctx, GL_INVALID_VALUE,
351                    "glTexEnv(GL_RGB_SCALE not 1, 2 or 4)" );
352       return false;
353    }
354 
355    switch (pname) {
356    case GL_RGB_SCALE:
357       if (texUnit->Combine.ScaleShiftRGB == shift)
358          return true;
359       FLUSH_VERTICES(ctx, _NEW_TEXTURE_STATE, GL_TEXTURE_BIT);
360       texUnit->Combine.ScaleShiftRGB = shift;
361       break;
362    case GL_ALPHA_SCALE:
363       if (texUnit->Combine.ScaleShiftA == shift)
364          return true;
365       FLUSH_VERTICES(ctx, _NEW_TEXTURE_STATE, GL_TEXTURE_BIT);
366       texUnit->Combine.ScaleShiftA = shift;
367       break;
368    default:
369       TE_ERROR(GL_INVALID_ENUM, "glTexEnv(pname=%s)", pname);
370       return false;
371    }
372 
373    return true;
374 }
375 
376 
377 static void
_mesa_texenvfv_indexed(struct gl_context * ctx,GLuint texunit,GLenum target,GLenum pname,const GLfloat * param)378 _mesa_texenvfv_indexed( struct gl_context* ctx, GLuint texunit, GLenum target,
379                         GLenum pname, const GLfloat *param )
380 {
381    const GLint iparam0 = (GLint) param[0];
382    GLuint maxUnit;
383 
384    maxUnit = (target == GL_POINT_SPRITE && pname == GL_COORD_REPLACE)
385       ? ctx->Const.MaxTextureCoordUnits : ctx->Const.MaxCombinedTextureImageUnits;
386    if (texunit >= maxUnit) {
387       _mesa_error(ctx, GL_INVALID_OPERATION, "glTexEnvfv(texunit=%d)", texunit);
388       return;
389    }
390 
391    if (target == GL_TEXTURE_ENV) {
392       struct gl_fixedfunc_texture_unit *texUnit =
393          _mesa_get_fixedfunc_tex_unit(ctx, texunit);
394 
395       /* The GL spec says that we should report an error if the unit is greater
396        * than GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, but in practice, only
397        * fixed-function units are usable. This is probably a spec bug.
398        * Ignore glTexEnv(GL_TEXTURE_ENV) calls for non-fixed-func units,
399        * because we don't want to process calls that have no effect.
400        */
401       if (!texUnit)
402          return;
403 
404       switch (pname) {
405       case GL_TEXTURE_ENV_MODE:
406          set_env_mode(ctx, texUnit, (GLenum) iparam0);
407          break;
408       case GL_TEXTURE_ENV_COLOR:
409          set_env_color(ctx, texUnit, param);
410          break;
411       case GL_COMBINE_RGB:
412       case GL_COMBINE_ALPHA:
413          if (!set_combiner_mode(ctx, texUnit, pname, (GLenum) iparam0))
414             return;
415 	 break;
416       case GL_SOURCE0_RGB:
417       case GL_SOURCE1_RGB:
418       case GL_SOURCE2_RGB:
419       case GL_SOURCE3_RGB_NV:
420       case GL_SOURCE0_ALPHA:
421       case GL_SOURCE1_ALPHA:
422       case GL_SOURCE2_ALPHA:
423       case GL_SOURCE3_ALPHA_NV:
424          if (!set_combiner_source(ctx, texUnit, pname, (GLenum) iparam0))
425             return;
426 	 break;
427       case GL_OPERAND0_RGB:
428       case GL_OPERAND1_RGB:
429       case GL_OPERAND2_RGB:
430       case GL_OPERAND3_RGB_NV:
431       case GL_OPERAND0_ALPHA:
432       case GL_OPERAND1_ALPHA:
433       case GL_OPERAND2_ALPHA:
434       case GL_OPERAND3_ALPHA_NV:
435          if (!set_combiner_operand(ctx, texUnit, pname, (GLenum) iparam0))
436             return;
437 	 break;
438       case GL_RGB_SCALE:
439       case GL_ALPHA_SCALE:
440          if (!set_combiner_scale(ctx, texUnit, pname, param[0]))
441             return;
442 	 break;
443       default:
444 	 _mesa_error( ctx, GL_INVALID_ENUM, "glTexEnv(pname)" );
445 	 return;
446       }
447    }
448    else if (target == GL_TEXTURE_FILTER_CONTROL_EXT) {
449       struct gl_texture_unit *texUnit =
450          _mesa_get_tex_unit(ctx, texunit);
451 
452       if (pname == GL_TEXTURE_LOD_BIAS_EXT) {
453 	 if (texUnit->LodBias == param[0])
454 	    return;
455 	 FLUSH_VERTICES(ctx, _NEW_TEXTURE_OBJECT, GL_TEXTURE_BIT);
456          texUnit->LodBias = param[0];
457          texUnit->LodBiasQuantized = util_quantize_lod_bias(param[0]);
458       }
459       else {
460          TE_ERROR(GL_INVALID_ENUM, "glTexEnv(pname=%s)", pname);
461 	 return;
462       }
463    }
464    else if (target == GL_POINT_SPRITE) {
465       if (pname == GL_COORD_REPLACE) {
466          /* It's kind of weird to set point state via glTexEnv,
467           * but that's what the spec calls for.
468           */
469          if (iparam0 == GL_TRUE) {
470             if (ctx->Point.CoordReplace & (1u << texunit))
471                return;
472             FLUSH_VERTICES(ctx, _NEW_POINT | _NEW_FF_VERT_PROGRAM,
473                            GL_POINT_BIT);
474             ctx->Point.CoordReplace |= (1u << texunit);
475          } else if (iparam0 == GL_FALSE) {
476             if (~(ctx->Point.CoordReplace) & (1u << texunit))
477                return;
478             FLUSH_VERTICES(ctx, _NEW_POINT | _NEW_FF_VERT_PROGRAM,
479                            GL_POINT_BIT);
480             ctx->Point.CoordReplace &= ~(1u << texunit);
481          } else {
482             _mesa_error( ctx, GL_INVALID_VALUE, "glTexEnv(param=0x%x)", iparam0);
483             return;
484          }
485       }
486       else {
487          _mesa_error( ctx, GL_INVALID_ENUM, "glTexEnv(pname=0x%x)", pname );
488          return;
489       }
490    }
491    else {
492       _mesa_error(ctx, GL_INVALID_ENUM, "glTexEnv(target=%s)",
493                   _mesa_enum_to_string(target));
494       return;
495    }
496 
497    if (MESA_VERBOSE&(VERBOSE_API|VERBOSE_TEXTURE))
498       _mesa_debug(ctx, "glTexEnv %s %s %.1f(%s) ...\n",
499                   _mesa_enum_to_string(target),
500                   _mesa_enum_to_string(pname),
501                   *param,
502                   _mesa_enum_to_string((GLenum) iparam0));
503 }
504 
505 
506 void GLAPIENTRY
_mesa_TexEnvfv(GLenum target,GLenum pname,const GLfloat * param)507 _mesa_TexEnvfv( GLenum target, GLenum pname, const GLfloat *param )
508 {
509    GET_CURRENT_CONTEXT(ctx);
510    _mesa_texenvfv_indexed(ctx, ctx->Texture.CurrentUnit, target, pname, param);
511 }
512 
513 
514 void GLAPIENTRY
_mesa_TexEnvf(GLenum target,GLenum pname,GLfloat param)515 _mesa_TexEnvf( GLenum target, GLenum pname, GLfloat param )
516 {
517    GLfloat p[4];
518    p[0] = param;
519    p[1] = p[2] = p[3] = 0.0;
520    _mesa_TexEnvfv( target, pname, p );
521 }
522 
523 
524 void GLAPIENTRY
_mesa_TexEnvi(GLenum target,GLenum pname,GLint param)525 _mesa_TexEnvi( GLenum target, GLenum pname, GLint param )
526 {
527    GLfloat p[4];
528    p[0] = (GLfloat) param;
529    p[1] = p[2] = p[3] = 0.0;
530    _mesa_TexEnvfv( target, pname, p );
531 }
532 
533 
534 void GLAPIENTRY
_mesa_TexEnviv(GLenum target,GLenum pname,const GLint * param)535 _mesa_TexEnviv( GLenum target, GLenum pname, const GLint *param )
536 {
537    GLfloat p[4];
538    if (pname == GL_TEXTURE_ENV_COLOR) {
539       p[0] = INT_TO_FLOAT( param[0] );
540       p[1] = INT_TO_FLOAT( param[1] );
541       p[2] = INT_TO_FLOAT( param[2] );
542       p[3] = INT_TO_FLOAT( param[3] );
543    }
544    else {
545       p[0] = (GLfloat) param[0];
546       p[1] = p[2] = p[3] = 0;  /* init to zero, just to be safe */
547    }
548    _mesa_TexEnvfv( target, pname, p );
549 }
550 
551 
552 void GLAPIENTRY
_mesa_MultiTexEnvfEXT(GLenum texunit,GLenum target,GLenum pname,GLfloat param)553 _mesa_MultiTexEnvfEXT( GLenum texunit, GLenum target,
554                        GLenum pname, GLfloat param )
555 {
556    GET_CURRENT_CONTEXT(ctx);
557    GLfloat p[4];
558    p[0] = param;
559    p[1] = p[2] = p[3] = 0.0;
560    _mesa_texenvfv_indexed(ctx, texunit - GL_TEXTURE0, target, pname, p);
561 }
562 
563 void GLAPIENTRY
_mesa_MultiTexEnvfvEXT(GLenum texunit,GLenum target,GLenum pname,const GLfloat * param)564 _mesa_MultiTexEnvfvEXT( GLenum texunit, GLenum target,
565                         GLenum pname, const GLfloat *param )
566 {
567    GET_CURRENT_CONTEXT(ctx);
568    _mesa_texenvfv_indexed(ctx, texunit - GL_TEXTURE0, target, pname, param);
569 }
570 
571 
572 void GLAPIENTRY
_mesa_MultiTexEnviEXT(GLenum texunit,GLenum target,GLenum pname,GLint param)573 _mesa_MultiTexEnviEXT( GLenum texunit, GLenum target,
574                        GLenum pname, GLint param )
575 {
576    GET_CURRENT_CONTEXT(ctx);
577    GLfloat p[4];
578    p[0] = (GLfloat) param;
579    p[1] = p[2] = p[3] = 0.0;
580    _mesa_texenvfv_indexed( ctx, texunit - GL_TEXTURE0, target, pname, p );
581 }
582 
583 
584 void GLAPIENTRY
_mesa_MultiTexEnvivEXT(GLenum texunit,GLenum target,GLenum pname,const GLint * param)585 _mesa_MultiTexEnvivEXT( GLenum texunit, GLenum target,
586                         GLenum pname, const GLint *param )
587 {
588    GET_CURRENT_CONTEXT(ctx);
589    GLfloat p[4];
590    if (pname == GL_TEXTURE_ENV_COLOR) {
591       p[0] = INT_TO_FLOAT( param[0] );
592       p[1] = INT_TO_FLOAT( param[1] );
593       p[2] = INT_TO_FLOAT( param[2] );
594       p[3] = INT_TO_FLOAT( param[3] );
595    }
596    else {
597       p[0] = (GLfloat) param[0];
598       p[1] = p[2] = p[3] = 0;  /* init to zero, just to be safe */
599    }
600    _mesa_texenvfv_indexed( ctx, texunit - GL_TEXTURE0, target, pname, p );
601 }
602 
603 
604 
605 
606 /**
607  * Helper for glGetTexEnvi/f()
608  * \return  value of queried pname or -1 if error.
609  */
610 static GLint
get_texenvi(struct gl_context * ctx,const struct gl_fixedfunc_texture_unit * texUnit,GLenum pname)611 get_texenvi(struct gl_context *ctx,
612             const struct gl_fixedfunc_texture_unit *texUnit,
613             GLenum pname)
614 {
615    switch (pname) {
616    case GL_TEXTURE_ENV_MODE:
617       return texUnit->EnvMode;
618       break;
619    case GL_COMBINE_RGB:
620       return texUnit->Combine.ModeRGB;
621    case GL_COMBINE_ALPHA:
622       return texUnit->Combine.ModeA;
623    case GL_SOURCE0_RGB:
624    case GL_SOURCE1_RGB:
625    case GL_SOURCE2_RGB: {
626       const unsigned rgb_idx = pname - GL_SOURCE0_RGB;
627       return texUnit->Combine.SourceRGB[rgb_idx];
628    }
629    case GL_SOURCE3_RGB_NV:
630       if (_mesa_is_desktop_gl_compat(ctx) && ctx->Extensions.NV_texture_env_combine4) {
631          return texUnit->Combine.SourceRGB[3];
632       }
633       else {
634          _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexEnvfv(pname)");
635       }
636       break;
637    case GL_SOURCE0_ALPHA:
638    case GL_SOURCE1_ALPHA:
639    case GL_SOURCE2_ALPHA: {
640       const unsigned alpha_idx = pname - GL_SOURCE0_ALPHA;
641       return texUnit->Combine.SourceA[alpha_idx];
642    }
643    case GL_SOURCE3_ALPHA_NV:
644       if (_mesa_is_desktop_gl_compat(ctx) && ctx->Extensions.NV_texture_env_combine4) {
645          return texUnit->Combine.SourceA[3];
646       }
647       else {
648          _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexEnvfv(pname)");
649       }
650       break;
651    case GL_OPERAND0_RGB:
652    case GL_OPERAND1_RGB:
653    case GL_OPERAND2_RGB: {
654       const unsigned op_rgb = pname - GL_OPERAND0_RGB;
655       return texUnit->Combine.OperandRGB[op_rgb];
656    }
657    case GL_OPERAND3_RGB_NV:
658       if (_mesa_is_desktop_gl_compat(ctx) && ctx->Extensions.NV_texture_env_combine4) {
659          return texUnit->Combine.OperandRGB[3];
660       }
661       else {
662          _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexEnvfv(pname)");
663       }
664       break;
665    case GL_OPERAND0_ALPHA:
666    case GL_OPERAND1_ALPHA:
667    case GL_OPERAND2_ALPHA: {
668       const unsigned op_alpha = pname - GL_OPERAND0_ALPHA;
669       return texUnit->Combine.OperandA[op_alpha];
670    }
671    case GL_OPERAND3_ALPHA_NV:
672       if (_mesa_is_desktop_gl_compat(ctx) && ctx->Extensions.NV_texture_env_combine4) {
673          return texUnit->Combine.OperandA[3];
674       }
675       else {
676          _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexEnvfv(pname)");
677       }
678       break;
679    case GL_RGB_SCALE:
680       return 1 << texUnit->Combine.ScaleShiftRGB;
681    case GL_ALPHA_SCALE:
682       return 1 << texUnit->Combine.ScaleShiftA;
683    default:
684       _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexEnvfv(pname)");
685       break;
686    }
687 
688    return -1; /* error */
689 }
690 
691 
692 static void
_mesa_gettexenvfv_indexed(GLuint texunit,GLenum target,GLenum pname,GLfloat * params)693 _mesa_gettexenvfv_indexed( GLuint texunit, GLenum target, GLenum pname, GLfloat *params )
694 {
695    GLuint maxUnit;
696    GET_CURRENT_CONTEXT(ctx);
697 
698    maxUnit = (target == GL_POINT_SPRITE && pname == GL_COORD_REPLACE)
699       ? ctx->Const.MaxTextureCoordUnits : ctx->Const.MaxCombinedTextureImageUnits;
700    if (texunit >= maxUnit) {
701       _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexEnvfv(texunit=%d)", texunit);
702       return;
703    }
704 
705    if (target == GL_TEXTURE_ENV) {
706       struct gl_fixedfunc_texture_unit *texUnit =
707          _mesa_get_fixedfunc_tex_unit(ctx, texunit);
708 
709       /* The GL spec says that we should report an error if the unit is greater
710        * than GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, but in practice, only
711        * fixed-function units are usable. This is probably a spec bug.
712        * Ignore calls for non-fixed-func units, because we don't process
713        * glTexEnv for them either.
714        */
715       if (!texUnit)
716          return;
717 
718       if (pname == GL_TEXTURE_ENV_COLOR) {
719          if (_mesa_get_clamp_fragment_color(ctx, ctx->DrawBuffer))
720             COPY_4FV( params, texUnit->EnvColor );
721          else
722             COPY_4FV( params, texUnit->EnvColorUnclamped );
723       }
724       else {
725          GLint val = get_texenvi(ctx, texUnit, pname);
726          if (val >= 0) {
727             *params = (GLfloat) val;
728          }
729       }
730    }
731    else if (target == GL_TEXTURE_FILTER_CONTROL_EXT) {
732       const struct gl_texture_unit *texUnit = _mesa_get_tex_unit(ctx, texunit);
733 
734       if (pname == GL_TEXTURE_LOD_BIAS_EXT) {
735          *params = texUnit->LodBias;
736       }
737       else {
738          _mesa_error( ctx, GL_INVALID_ENUM, "glGetTexEnvfv(pname)" );
739 	 return;
740       }
741    }
742    else if (target == GL_POINT_SPRITE) {
743       if (pname == GL_COORD_REPLACE) {
744          if (ctx->Point.CoordReplace & (1u << texunit))
745             *params = 1.0f;
746          else
747             *params = 0.0f;
748       }
749       else {
750          _mesa_error( ctx, GL_INVALID_ENUM, "glGetTexEnvfv(pname)" );
751          return;
752       }
753    }
754    else {
755       _mesa_error( ctx, GL_INVALID_ENUM, "glGetTexEnvfv(target)" );
756       return;
757    }
758 }
759 
760 
761 static void
_mesa_gettexenviv_indexed(GLuint texunit,GLenum target,GLenum pname,GLint * params)762 _mesa_gettexenviv_indexed( GLuint texunit, GLenum target,
763                            GLenum pname, GLint *params )
764 {
765    GLuint maxUnit;
766    GET_CURRENT_CONTEXT(ctx);
767 
768    maxUnit = (target == GL_POINT_SPRITE && pname == GL_COORD_REPLACE)
769       ? ctx->Const.MaxTextureCoordUnits : ctx->Const.MaxCombinedTextureImageUnits;
770    if (texunit >= maxUnit) {
771       _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexEnviv(texunit=%d)",
772                   texunit);
773       return;
774    }
775 
776    if (target == GL_TEXTURE_ENV) {
777       struct gl_fixedfunc_texture_unit *texUnit =
778          _mesa_get_fixedfunc_tex_unit(ctx, texunit);
779 
780       /* The GL spec says that we should report an error if the unit is greater
781        * than GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, but in practice, only
782        * fixed-function units are usable. This is probably a spec bug.
783        * Ignore calls for non-fixed-func units, because we don't process
784        * glTexEnv for them either.
785        */
786       if (!texUnit)
787          return;
788 
789       if (pname == GL_TEXTURE_ENV_COLOR) {
790          params[0] = FLOAT_TO_INT( texUnit->EnvColor[0] );
791          params[1] = FLOAT_TO_INT( texUnit->EnvColor[1] );
792          params[2] = FLOAT_TO_INT( texUnit->EnvColor[2] );
793          params[3] = FLOAT_TO_INT( texUnit->EnvColor[3] );
794       }
795       else {
796          GLint val = get_texenvi(ctx, texUnit, pname);
797          if (val >= 0) {
798             *params = val;
799          }
800       }
801    }
802    else if (target == GL_TEXTURE_FILTER_CONTROL_EXT) {
803       const struct gl_texture_unit *texUnit = _mesa_get_tex_unit(ctx, texunit);
804 
805       if (pname == GL_TEXTURE_LOD_BIAS_EXT) {
806          *params = (GLint) texUnit->LodBias;
807       }
808       else {
809          _mesa_error( ctx, GL_INVALID_ENUM, "glGetTexEnviv(pname)" );
810 	 return;
811       }
812    }
813    else if (target == GL_POINT_SPRITE) {
814       if (pname == GL_COORD_REPLACE) {
815          if (ctx->Point.CoordReplace & (1u << texunit))
816             *params = GL_TRUE;
817          else
818             *params = GL_FALSE;
819       }
820       else {
821          _mesa_error( ctx, GL_INVALID_ENUM, "glGetTexEnviv(pname)" );
822          return;
823       }
824    }
825    else {
826       _mesa_error( ctx, GL_INVALID_ENUM, "glGetTexEnviv(target)" );
827       return;
828    }
829 }
830 
831 
832 void GLAPIENTRY
_mesa_GetTexEnvfv(GLenum target,GLenum pname,GLfloat * params)833 _mesa_GetTexEnvfv( GLenum target, GLenum pname, GLfloat *params )
834 {
835    GET_CURRENT_CONTEXT(ctx);
836    _mesa_gettexenvfv_indexed(ctx->Texture.CurrentUnit, target, pname, params);
837 }
838 
839 
840 void GLAPIENTRY
_mesa_GetMultiTexEnvfvEXT(GLenum texunit,GLenum target,GLenum pname,GLfloat * params)841 _mesa_GetMultiTexEnvfvEXT( GLenum texunit, GLenum target,
842                            GLenum pname, GLfloat *params )
843 {
844    _mesa_gettexenvfv_indexed(texunit - GL_TEXTURE0, target, pname, params);
845 }
846 
847 
848 void GLAPIENTRY
_mesa_GetTexEnviv(GLenum target,GLenum pname,GLint * params)849 _mesa_GetTexEnviv( GLenum target, GLenum pname, GLint *params )
850 {
851    GET_CURRENT_CONTEXT(ctx);
852    _mesa_gettexenviv_indexed(ctx->Texture.CurrentUnit, target, pname, params);
853 }
854 
855 
856 void GLAPIENTRY
_mesa_GetMultiTexEnvivEXT(GLenum texunit,GLenum target,GLenum pname,GLint * params)857 _mesa_GetMultiTexEnvivEXT( GLenum texunit, GLenum target,
858                            GLenum pname, GLint *params )
859 {
860    _mesa_gettexenviv_indexed(texunit - GL_TEXTURE0, target, pname, params);
861 }
862