1 /**
2  * \file texobj.h
3  * Texture object management.
4  */
5 
6 /*
7  * Mesa 3-D graphics library
8  *
9  * Copyright (C) 1999-2006  Brian Paul   All Rights Reserved.
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining a
12  * copy of this software and associated documentation files (the "Software"),
13  * to deal in the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15  * and/or sell copies of the Software, and to permit persons to whom the
16  * Software is furnished to do so, subject to the following conditions:
17  *
18  * The above copyright notice and this permission notice shall be included
19  * in all copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
24  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
25  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27  * OTHER DEALINGS IN THE SOFTWARE.
28  */
29 
30 
31 #ifndef TEXTOBJ_H
32 #define TEXTOBJ_H
33 
34 
35 #include "glheader.h"
36 #include "samplerobj.h"
37 
38 
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 
43 
44 /**
45  * \name Internal functions
46  */
47 /*@{*/
48 
49 extern struct gl_texture_object *
50 _mesa_lookup_texture(struct gl_context *ctx, GLuint id);
51 
52 extern struct gl_texture_object *
53 _mesa_lookup_texture_err(struct gl_context *ctx, GLuint id, const char* func);
54 
55 extern struct gl_texture_object *
56 _mesa_lookup_texture_locked(struct gl_context *ctx, GLuint id);
57 
58 extern struct gl_texture_object *
59 _mesa_get_current_tex_object(struct gl_context *ctx, GLenum target);
60 
61 extern struct gl_texture_object *
62 _mesa_get_texobj_by_target_and_texunit(struct gl_context *ctx, GLenum target,
63                                        GLuint texunit,
64                                        bool allowProxyTargets,
65                                        const char* caller);
66 
67 extern struct gl_texture_object *
68 _mesa_new_texture_object( struct gl_context *ctx, GLuint name, GLenum target );
69 
70 extern void
71 _mesa_initialize_texture_object( struct gl_context *ctx,
72                                  struct gl_texture_object *obj,
73                                  GLuint name, GLenum target );
74 
75 extern int
76 _mesa_tex_target_to_index(const struct gl_context *ctx, GLenum target);
77 
78 extern void
79 _mesa_delete_texture_object( struct gl_context *ctx,
80                              struct gl_texture_object *obj );
81 
82 extern void
83 _mesa_clear_texture_object(struct gl_context *ctx,
84                            struct gl_texture_object *obj,
85                            struct gl_texture_image *retainTexImage);
86 
87 extern void
88 _mesa_reference_texobj_(struct gl_texture_object **ptr,
89                         struct gl_texture_object *tex);
90 
91 static inline void
_mesa_reference_texobj(struct gl_texture_object ** ptr,struct gl_texture_object * tex)92 _mesa_reference_texobj(struct gl_texture_object **ptr,
93                        struct gl_texture_object *tex)
94 {
95    if (*ptr != tex)
96       _mesa_reference_texobj_(ptr, tex);
97 }
98 
99 /**
100  * Lock a texture for updating.  See also _mesa_lock_context_textures().
101  */
102 static inline void
_mesa_lock_texture(struct gl_context * ctx,struct gl_texture_object * texObj)103 _mesa_lock_texture(struct gl_context *ctx, struct gl_texture_object *texObj)
104 {
105    if (!ctx->TexturesLocked)
106       mtx_lock(&ctx->Shared->TexMutex);
107    ctx->Shared->TextureStateStamp++;
108    (void) texObj;
109 }
110 
111 static inline void
_mesa_unlock_texture(struct gl_context * ctx,struct gl_texture_object * texObj)112 _mesa_unlock_texture(struct gl_context *ctx, struct gl_texture_object *texObj)
113 {
114    (void) texObj;
115    if (!ctx->TexturesLocked)
116       mtx_unlock(&ctx->Shared->TexMutex);
117 }
118 
119 
120 /** Is the texture "complete" with respect to the given sampler state? */
121 static inline GLboolean
_mesa_is_texture_complete(const struct gl_texture_object * texObj,const struct gl_sampler_object * sampler,bool linear_as_nearest_for_int_tex)122 _mesa_is_texture_complete(const struct gl_texture_object *texObj,
123                           const struct gl_sampler_object *sampler,
124                           bool linear_as_nearest_for_int_tex)
125 {
126    struct gl_texture_image *img = texObj->Image[0][texObj->Attrib.BaseLevel];
127    bool isMultisample = img && img->NumSamples >= 2;
128 
129    /*
130     * According to ARB_stencil_texturing, NEAREST_MIPMAP_NEAREST would
131     * be forbidden, however it is allowed per GL 4.5 rules, allow it
132     * even without GL 4.5 since it was a spec mistake.
133     */
134    /* Section 8.17 (texture completeness) of the OpenGL 4.6 core profile spec:
135     *
136     *  "The texture is not multisample; either the magnification filter is not
137     *  NEAREST, or the minification filter is neither NEAREST nor NEAREST_-
138     *  MIPMAP_NEAREST; and any of
139     *  – The internal format of the texture is integer.
140     *  – The internal format is STENCIL_INDEX.
141     *  – The internal format is DEPTH_STENCIL, and the value of DEPTH_-
142     *    STENCIL_TEXTURE_MODE for the texture is STENCIL_INDEX.""
143     */
144    /* GL_EXT_texture_filter_minmax further modifies this to explain it does
145     * not apply to MIN/MAX reduction, only WEIGHTED_AVERAGE (i.e. default)
146     */
147    if (!isMultisample &&
148        (texObj->_IsIntegerFormat ||
149         (texObj->StencilSampling &&
150          img->_BaseFormat == GL_DEPTH_STENCIL)) &&
151        sampler->Attrib.ReductionMode == GL_WEIGHTED_AVERAGE_EXT &&
152        (sampler->Attrib.MagFilter != GL_NEAREST ||
153         (sampler->Attrib.MinFilter != GL_NEAREST &&
154          sampler->Attrib.MinFilter != GL_NEAREST_MIPMAP_NEAREST))) {
155       /* If the format is integer, only nearest filtering is allowed,
156        * but some applications (eg: Grid Autosport) uses the default
157        * filtering values.
158        */
159       if (texObj->_IsIntegerFormat &&
160           linear_as_nearest_for_int_tex) {
161          /* Skip return */
162       } else {
163          return GL_FALSE;
164       }
165    }
166 
167    /* Section 8.17 (texture completeness) of the OpenGL 4.6 core profile spec:
168     *
169     *  "The minification filter requires a mipmap (is neither NEAREST nor LINEAR),
170     *  the texture is not multisample, and the texture is not mipmap complete.""
171     */
172    if (!isMultisample &&_mesa_is_mipmap_filter(sampler))
173       return texObj->_MipmapComplete;
174    else
175       return texObj->_BaseComplete;
176 }
177 
178 
179 extern void
180 _mesa_test_texobj_completeness( const struct gl_context *ctx,
181                                 struct gl_texture_object *obj );
182 
183 extern GLboolean
184 _mesa_cube_level_complete(const struct gl_texture_object *texObj,
185                           const GLint level);
186 
187 extern GLboolean
188 _mesa_cube_complete(const struct gl_texture_object *texObj);
189 
190 extern void
191 _mesa_dirty_texobj(struct gl_context *ctx, struct gl_texture_object *texObj);
192 
193 extern struct gl_texture_object *
194 _mesa_get_fallback_texture(struct gl_context *ctx, gl_texture_index tex);
195 
196 extern GLuint
197 _mesa_total_texture_memory(struct gl_context *ctx);
198 
199 extern GLenum
200 _mesa_texture_base_format(const struct gl_texture_object *texObj);
201 
202 extern void
203 _mesa_unlock_context_textures( struct gl_context *ctx );
204 
205 extern void
206 _mesa_lock_context_textures( struct gl_context *ctx );
207 
208 extern void
209 _mesa_delete_nameless_texture(struct gl_context *ctx,
210                               struct gl_texture_object *texObj);
211 
212 extern void
213 _mesa_bind_texture(struct gl_context *ctx, GLenum target,
214                    struct gl_texture_object *tex_obj);
215 
216 extern struct gl_texture_object *
217 _mesa_lookup_or_create_texture(struct gl_context *ctx, GLenum target,
218                                GLuint texName, bool no_error, bool is_ext_dsa,
219                                const char *name);
220 
221 /*@}*/
222 
223 /**
224  * \name API functions
225  */
226 /*@{*/
227 
228 void GLAPIENTRY
229 _mesa_GenTextures_no_error(GLsizei n, GLuint *textures);
230 
231 extern void GLAPIENTRY
232 _mesa_GenTextures(GLsizei n, GLuint *textures);
233 
234 void GLAPIENTRY
235 _mesa_CreateTextures_no_error(GLenum target, GLsizei n, GLuint *textures);
236 
237 extern void GLAPIENTRY
238 _mesa_CreateTextures(GLenum target, GLsizei n, GLuint *textures);
239 
240 void GLAPIENTRY
241 _mesa_DeleteTextures_no_error(GLsizei n, const GLuint *textures);
242 
243 extern void GLAPIENTRY
244 _mesa_DeleteTextures( GLsizei n, const GLuint *textures );
245 
246 
247 void GLAPIENTRY
248 _mesa_BindTexture_no_error(GLenum target, GLuint texture);
249 
250 extern void GLAPIENTRY
251 _mesa_BindTexture( GLenum target, GLuint texture );
252 
253 void GLAPIENTRY
254 _mesa_BindMultiTextureEXT(GLenum texunit, GLenum target, GLuint texture);
255 
256 void GLAPIENTRY
257 _mesa_BindTextureUnit_no_error(GLuint unit, GLuint texture);
258 
259 extern void GLAPIENTRY
260 _mesa_BindTextureUnit(GLuint unit, GLuint texture);
261 
262 void GLAPIENTRY
263 _mesa_BindTextures_no_error(GLuint first, GLsizei count,
264                             const GLuint *textures);
265 
266 extern void GLAPIENTRY
267 _mesa_BindTextures( GLuint first, GLsizei count, const GLuint *textures );
268 
269 
270 extern void GLAPIENTRY
271 _mesa_PrioritizeTextures( GLsizei n, const GLuint *textures,
272                           const GLclampf *priorities );
273 
274 
275 extern GLboolean GLAPIENTRY
276 _mesa_AreTexturesResident( GLsizei n, const GLuint *textures,
277                            GLboolean *residences );
278 
279 extern GLboolean GLAPIENTRY
280 _mesa_IsTexture( GLuint texture );
281 
282 void GLAPIENTRY
283 _mesa_InvalidateTexSubImage_no_error(GLuint texture, GLint level, GLint xoffset,
284                                      GLint yoffset, GLint zoffset,
285                                      GLsizei width, GLsizei height,
286                                      GLsizei depth);
287 
288 extern void GLAPIENTRY
289 _mesa_InvalidateTexSubImage(GLuint texture, GLint level, GLint xoffset,
290                             GLint yoffset, GLint zoffset, GLsizei width,
291                             GLsizei height, GLsizei depth);
292 void GLAPIENTRY
293 _mesa_InvalidateTexImage_no_error(GLuint texture, GLint level);
294 
295 extern void GLAPIENTRY
296 _mesa_InvalidateTexImage(GLuint texture, GLint level);
297 
298 /*@}*/
299 
300 
301 #ifdef __cplusplus
302 }
303 #endif
304 
305 
306 #endif
307