genmipmap.c revision af69d88d
1/*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
5 * Copyright (C) 1999-2013  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/*
28 * glGenerateMipmap function
29 */
30
31#include "context.h"
32#include "enums.h"
33#include "genmipmap.h"
34#include "glformats.h"
35#include "macros.h"
36#include "mtypes.h"
37#include "teximage.h"
38#include "texobj.h"
39
40
41/**
42 * Generate all the mipmap levels below the base level.
43 * Note: this GL function would be more useful if one could specify a
44 * cube face, a set of array slices, etc.
45 */
46void GLAPIENTRY
47_mesa_GenerateMipmap(GLenum target)
48{
49   struct gl_texture_image *srcImage;
50   struct gl_texture_object *texObj;
51   GLboolean error;
52
53   GET_CURRENT_CONTEXT(ctx);
54
55   FLUSH_VERTICES(ctx, 0);
56
57   switch (target) {
58   case GL_TEXTURE_1D:
59      error = _mesa_is_gles(ctx);
60      break;
61   case GL_TEXTURE_2D:
62      error = GL_FALSE;
63      break;
64   case GL_TEXTURE_3D:
65      error = ctx->API == API_OPENGLES;
66      break;
67   case GL_TEXTURE_CUBE_MAP:
68      error = !ctx->Extensions.ARB_texture_cube_map;
69      break;
70   case GL_TEXTURE_1D_ARRAY:
71      error = _mesa_is_gles(ctx) || !ctx->Extensions.EXT_texture_array;
72      break;
73   case GL_TEXTURE_2D_ARRAY:
74      error = (_mesa_is_gles(ctx) && ctx->Version < 30)
75         || !ctx->Extensions.EXT_texture_array;
76      break;
77   case GL_TEXTURE_CUBE_MAP_ARRAY:
78      error = _mesa_is_gles(ctx) ||
79              !ctx->Extensions.ARB_texture_cube_map_array;
80      break;
81   default:
82      error = GL_TRUE;
83   }
84
85   if (error) {
86      _mesa_error(ctx, GL_INVALID_ENUM, "glGenerateMipmapEXT(target=%s)",
87                  _mesa_lookup_enum_by_nr(target));
88      return;
89   }
90
91   texObj = _mesa_get_current_tex_object(ctx, target);
92
93   if (texObj->BaseLevel >= texObj->MaxLevel) {
94      /* nothing to do */
95      return;
96   }
97
98   if (texObj->Target == GL_TEXTURE_CUBE_MAP &&
99       !_mesa_cube_complete(texObj)) {
100      _mesa_error(ctx, GL_INVALID_OPERATION,
101                  "glGenerateMipmap(incomplete cube map)");
102      return;
103   }
104
105   _mesa_lock_texture(ctx, texObj);
106
107   srcImage = _mesa_select_tex_image(ctx, texObj, target, texObj->BaseLevel);
108   if (!srcImage) {
109      _mesa_unlock_texture(ctx, texObj);
110      _mesa_error(ctx, GL_INVALID_OPERATION,
111                  "glGenerateMipmap(zero size base image)");
112      return;
113   }
114
115   if (_mesa_is_enum_format_integer(srcImage->InternalFormat) ||
116       _mesa_is_depthstencil_format(srcImage->InternalFormat) ||
117       _mesa_is_stencil_format(srcImage->InternalFormat)) {
118      _mesa_unlock_texture(ctx, texObj);
119      _mesa_error(ctx, GL_INVALID_OPERATION,
120                  "glGenerateMipmap(invalid internal format)");
121      return;
122   }
123
124   if (target == GL_TEXTURE_CUBE_MAP) {
125      GLuint face;
126      for (face = 0; face < 6; face++)
127	 ctx->Driver.GenerateMipmap(ctx,
128				    GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB + face,
129				    texObj);
130   }
131   else {
132      ctx->Driver.GenerateMipmap(ctx, target, texObj);
133   }
134   _mesa_unlock_texture(ctx, texObj);
135}
136