1bf215546Sopenharmony_ci/**
2bf215546Sopenharmony_ci * \file texobj.h
3bf215546Sopenharmony_ci * Texture object management.
4bf215546Sopenharmony_ci */
5bf215546Sopenharmony_ci
6bf215546Sopenharmony_ci/*
7bf215546Sopenharmony_ci * Mesa 3-D graphics library
8bf215546Sopenharmony_ci *
9bf215546Sopenharmony_ci * Copyright (C) 1999-2006  Brian Paul   All Rights Reserved.
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
12bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
13bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
14bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
16bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
17bf215546Sopenharmony_ci *
18bf215546Sopenharmony_ci * The above copyright notice and this permission notice shall be included
19bf215546Sopenharmony_ci * in all copies or substantial portions of the Software.
20bf215546Sopenharmony_ci *
21bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
24bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
25bf215546Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26bf215546Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27bf215546Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE.
28bf215546Sopenharmony_ci */
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_ci
31bf215546Sopenharmony_ci#ifndef TEXTOBJ_H
32bf215546Sopenharmony_ci#define TEXTOBJ_H
33bf215546Sopenharmony_ci
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_ci#include "glheader.h"
36bf215546Sopenharmony_ci#include "samplerobj.h"
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_ci
39bf215546Sopenharmony_ci#ifdef __cplusplus
40bf215546Sopenharmony_ciextern "C" {
41bf215546Sopenharmony_ci#endif
42bf215546Sopenharmony_ci
43bf215546Sopenharmony_ci
44bf215546Sopenharmony_ci/**
45bf215546Sopenharmony_ci * \name Internal functions
46bf215546Sopenharmony_ci */
47bf215546Sopenharmony_ci/*@{*/
48bf215546Sopenharmony_ci
49bf215546Sopenharmony_ciextern struct gl_texture_object *
50bf215546Sopenharmony_ci_mesa_lookup_texture(struct gl_context *ctx, GLuint id);
51bf215546Sopenharmony_ci
52bf215546Sopenharmony_ciextern struct gl_texture_object *
53bf215546Sopenharmony_ci_mesa_lookup_texture_err(struct gl_context *ctx, GLuint id, const char* func);
54bf215546Sopenharmony_ci
55bf215546Sopenharmony_ciextern struct gl_texture_object *
56bf215546Sopenharmony_ci_mesa_lookup_texture_locked(struct gl_context *ctx, GLuint id);
57bf215546Sopenharmony_ci
58bf215546Sopenharmony_ciextern struct gl_texture_object *
59bf215546Sopenharmony_ci_mesa_get_current_tex_object(struct gl_context *ctx, GLenum target);
60bf215546Sopenharmony_ci
61bf215546Sopenharmony_ciextern struct gl_texture_object *
62bf215546Sopenharmony_ci_mesa_get_texobj_by_target_and_texunit(struct gl_context *ctx, GLenum target,
63bf215546Sopenharmony_ci                                       GLuint texunit,
64bf215546Sopenharmony_ci                                       bool allowProxyTargets,
65bf215546Sopenharmony_ci                                       const char* caller);
66bf215546Sopenharmony_ci
67bf215546Sopenharmony_ciextern struct gl_texture_object *
68bf215546Sopenharmony_ci_mesa_new_texture_object( struct gl_context *ctx, GLuint name, GLenum target );
69bf215546Sopenharmony_ci
70bf215546Sopenharmony_ciextern int
71bf215546Sopenharmony_ci_mesa_tex_target_to_index(const struct gl_context *ctx, GLenum target);
72bf215546Sopenharmony_ci
73bf215546Sopenharmony_ciextern void
74bf215546Sopenharmony_ci_mesa_delete_texture_object( struct gl_context *ctx,
75bf215546Sopenharmony_ci                             struct gl_texture_object *obj );
76bf215546Sopenharmony_ci
77bf215546Sopenharmony_ciextern void
78bf215546Sopenharmony_ci_mesa_clear_texture_object(struct gl_context *ctx,
79bf215546Sopenharmony_ci                           struct gl_texture_object *obj,
80bf215546Sopenharmony_ci                           struct gl_texture_image *retainTexImage);
81bf215546Sopenharmony_ci
82bf215546Sopenharmony_ciextern void
83bf215546Sopenharmony_ci_mesa_reference_texobj_(struct gl_texture_object **ptr,
84bf215546Sopenharmony_ci                        struct gl_texture_object *tex);
85bf215546Sopenharmony_ci
86bf215546Sopenharmony_cistatic inline void
87bf215546Sopenharmony_ci_mesa_reference_texobj(struct gl_texture_object **ptr,
88bf215546Sopenharmony_ci                       struct gl_texture_object *tex)
89bf215546Sopenharmony_ci{
90bf215546Sopenharmony_ci   if (*ptr != tex)
91bf215546Sopenharmony_ci      _mesa_reference_texobj_(ptr, tex);
92bf215546Sopenharmony_ci}
93bf215546Sopenharmony_ci
94bf215546Sopenharmony_ci/**
95bf215546Sopenharmony_ci * Lock a texture for updating.  See also _mesa_lock_context_textures().
96bf215546Sopenharmony_ci */
97bf215546Sopenharmony_cistatic inline void
98bf215546Sopenharmony_ci_mesa_lock_texture(struct gl_context *ctx, struct gl_texture_object *texObj)
99bf215546Sopenharmony_ci{
100bf215546Sopenharmony_ci   if (!ctx->TexturesLocked)
101bf215546Sopenharmony_ci      simple_mtx_lock(&ctx->Shared->TexMutex);
102bf215546Sopenharmony_ci   ctx->Shared->TextureStateStamp++;
103bf215546Sopenharmony_ci   (void) texObj;
104bf215546Sopenharmony_ci}
105bf215546Sopenharmony_ci
106bf215546Sopenharmony_cistatic inline void
107bf215546Sopenharmony_ci_mesa_unlock_texture(struct gl_context *ctx, struct gl_texture_object *texObj)
108bf215546Sopenharmony_ci{
109bf215546Sopenharmony_ci   (void) texObj;
110bf215546Sopenharmony_ci   if (!ctx->TexturesLocked)
111bf215546Sopenharmony_ci      simple_mtx_unlock(&ctx->Shared->TexMutex);
112bf215546Sopenharmony_ci}
113bf215546Sopenharmony_ci
114bf215546Sopenharmony_ci
115bf215546Sopenharmony_ci/** Is the texture "complete" with respect to the given sampler state? */
116bf215546Sopenharmony_cistatic inline GLboolean
117bf215546Sopenharmony_ci_mesa_is_texture_complete(const struct gl_texture_object *texObj,
118bf215546Sopenharmony_ci                          const struct gl_sampler_object *sampler,
119bf215546Sopenharmony_ci                          bool linear_as_nearest_for_int_tex)
120bf215546Sopenharmony_ci{
121bf215546Sopenharmony_ci   struct gl_texture_image *img = texObj->Image[0][texObj->Attrib.BaseLevel];
122bf215546Sopenharmony_ci   bool isMultisample = img && img->NumSamples >= 2;
123bf215546Sopenharmony_ci
124bf215546Sopenharmony_ci   /*
125bf215546Sopenharmony_ci    * According to ARB_stencil_texturing, NEAREST_MIPMAP_NEAREST would
126bf215546Sopenharmony_ci    * be forbidden, however it is allowed per GL 4.5 rules, allow it
127bf215546Sopenharmony_ci    * even without GL 4.5 since it was a spec mistake.
128bf215546Sopenharmony_ci    */
129bf215546Sopenharmony_ci   /* Section 8.17 (texture completeness) of the OpenGL 4.6 core profile spec:
130bf215546Sopenharmony_ci    *
131bf215546Sopenharmony_ci    *  "The texture is not multisample; either the magnification filter is not
132bf215546Sopenharmony_ci    *  NEAREST, or the minification filter is neither NEAREST nor NEAREST_-
133bf215546Sopenharmony_ci    *  MIPMAP_NEAREST; and any of
134bf215546Sopenharmony_ci    *  – The internal format of the texture is integer.
135bf215546Sopenharmony_ci    *  – The internal format is STENCIL_INDEX.
136bf215546Sopenharmony_ci    *  – The internal format is DEPTH_STENCIL, and the value of DEPTH_-
137bf215546Sopenharmony_ci    *    STENCIL_TEXTURE_MODE for the texture is STENCIL_INDEX.""
138bf215546Sopenharmony_ci    */
139bf215546Sopenharmony_ci   /* GL_EXT_texture_filter_minmax further modifies this to explain it does
140bf215546Sopenharmony_ci    * not apply to MIN/MAX reduction, only WEIGHTED_AVERAGE (i.e. default)
141bf215546Sopenharmony_ci    */
142bf215546Sopenharmony_ci   if (!isMultisample &&
143bf215546Sopenharmony_ci       (texObj->_IsIntegerFormat ||
144bf215546Sopenharmony_ci        (texObj->StencilSampling &&
145bf215546Sopenharmony_ci         img->_BaseFormat == GL_DEPTH_STENCIL)) &&
146bf215546Sopenharmony_ci       sampler->Attrib.ReductionMode == GL_WEIGHTED_AVERAGE_EXT &&
147bf215546Sopenharmony_ci       (sampler->Attrib.MagFilter != GL_NEAREST ||
148bf215546Sopenharmony_ci        (sampler->Attrib.MinFilter != GL_NEAREST &&
149bf215546Sopenharmony_ci         sampler->Attrib.MinFilter != GL_NEAREST_MIPMAP_NEAREST))) {
150bf215546Sopenharmony_ci      /* If the format is integer, only nearest filtering is allowed,
151bf215546Sopenharmony_ci       * but some applications (eg: Grid Autosport) uses the default
152bf215546Sopenharmony_ci       * filtering values.
153bf215546Sopenharmony_ci       */
154bf215546Sopenharmony_ci      if (texObj->_IsIntegerFormat &&
155bf215546Sopenharmony_ci          linear_as_nearest_for_int_tex) {
156bf215546Sopenharmony_ci         /* Skip return */
157bf215546Sopenharmony_ci      } else {
158bf215546Sopenharmony_ci         return GL_FALSE;
159bf215546Sopenharmony_ci      }
160bf215546Sopenharmony_ci   }
161bf215546Sopenharmony_ci
162bf215546Sopenharmony_ci   /* Section 8.17 (texture completeness) of the OpenGL 4.6 core profile spec:
163bf215546Sopenharmony_ci    *
164bf215546Sopenharmony_ci    *  "The minification filter requires a mipmap (is neither NEAREST nor LINEAR),
165bf215546Sopenharmony_ci    *  the texture is not multisample, and the texture is not mipmap complete.""
166bf215546Sopenharmony_ci    */
167bf215546Sopenharmony_ci   if (!isMultisample &&_mesa_is_mipmap_filter(sampler))
168bf215546Sopenharmony_ci      return texObj->_MipmapComplete;
169bf215546Sopenharmony_ci   else
170bf215546Sopenharmony_ci      return texObj->_BaseComplete;
171bf215546Sopenharmony_ci}
172bf215546Sopenharmony_ci
173bf215546Sopenharmony_ci
174bf215546Sopenharmony_ciextern void
175bf215546Sopenharmony_ci_mesa_test_texobj_completeness( const struct gl_context *ctx,
176bf215546Sopenharmony_ci                                struct gl_texture_object *obj );
177bf215546Sopenharmony_ci
178bf215546Sopenharmony_ciextern GLboolean
179bf215546Sopenharmony_ci_mesa_cube_level_complete(const struct gl_texture_object *texObj,
180bf215546Sopenharmony_ci                          const GLint level);
181bf215546Sopenharmony_ci
182bf215546Sopenharmony_ciextern GLboolean
183bf215546Sopenharmony_ci_mesa_cube_complete(const struct gl_texture_object *texObj);
184bf215546Sopenharmony_ci
185bf215546Sopenharmony_ciextern void
186bf215546Sopenharmony_ci_mesa_dirty_texobj(struct gl_context *ctx, struct gl_texture_object *texObj);
187bf215546Sopenharmony_ci
188bf215546Sopenharmony_ciextern struct gl_texture_object *
189bf215546Sopenharmony_ci_mesa_get_fallback_texture(struct gl_context *ctx, gl_texture_index tex);
190bf215546Sopenharmony_ci
191bf215546Sopenharmony_ciextern GLuint
192bf215546Sopenharmony_ci_mesa_total_texture_memory(struct gl_context *ctx);
193bf215546Sopenharmony_ci
194bf215546Sopenharmony_ciextern GLenum
195bf215546Sopenharmony_ci_mesa_texture_base_format(const struct gl_texture_object *texObj);
196bf215546Sopenharmony_ci
197bf215546Sopenharmony_ciextern void
198bf215546Sopenharmony_ci_mesa_unlock_context_textures( struct gl_context *ctx );
199bf215546Sopenharmony_ci
200bf215546Sopenharmony_ciextern void
201bf215546Sopenharmony_ci_mesa_lock_context_textures( struct gl_context *ctx );
202bf215546Sopenharmony_ci
203bf215546Sopenharmony_ciextern struct gl_texture_object *
204bf215546Sopenharmony_ci_mesa_lookup_or_create_texture(struct gl_context *ctx, GLenum target,
205bf215546Sopenharmony_ci                               GLuint texName, bool no_error, bool is_ext_dsa,
206bf215546Sopenharmony_ci                               const char *name);
207bf215546Sopenharmony_ci
208bf215546Sopenharmony_ci/*@}*/
209bf215546Sopenharmony_ci
210bf215546Sopenharmony_ci#ifdef __cplusplus
211bf215546Sopenharmony_ci}
212bf215546Sopenharmony_ci#endif
213bf215546Sopenharmony_ci
214bf215546Sopenharmony_ci
215bf215546Sopenharmony_ci#endif
216