1 /**************************************************************************
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
29 #include "main/imports.h"
30 #include "main/macros.h"
31 #include "main/mipmap.h"
32 #include "main/teximage.h"
33 #include "main/texformat.h"
35 #include "shader/prog_instruction.h"
37 #include "pipe/p_context.h"
38 #include "pipe/p_defines.h"
39 #include "pipe/p_inlines.h"
40 #include "util/u_gen_mipmap.h"
42 #include "cso_cache/cso_cache.h"
43 #include "cso_cache/cso_context.h"
45 #include "st_context.h"
47 #include "st_gen_mipmap.h"
48 #include "st_program.h"
49 #include "st_texture.h"
50 #include "st_cb_texture.h"
51 #include "st_inlines.h"
55 * one-time init for generate mipmap
56 * XXX Note: there may be other times we need no-op/simple state like this.
57 * In that case, some code refactoring would be good.
60 st_init_generate_mipmap(struct st_context
*st
)
62 st
->gen_mipmap
= util_create_gen_mipmap(st
->pipe
, st
->cso_context
);
67 st_destroy_generate_mipmap(struct st_context
*st
)
69 util_destroy_gen_mipmap(st
->gen_mipmap
);
70 st
->gen_mipmap
= NULL
;
75 * Generate mipmap levels using hardware rendering.
76 * \return TRUE if successful, FALSE if not possible
79 st_render_mipmap(struct st_context
*st
,
81 struct pipe_texture
*pt
,
82 uint baseLevel
, uint lastLevel
)
84 struct pipe_context
*pipe
= st
->pipe
;
85 struct pipe_screen
*screen
= pipe
->screen
;
86 const uint face
= _mesa_tex_target_to_face(target
);
88 assert(target
!= GL_TEXTURE_3D
); /* not done yet */
90 /* check if we can render in the texture's format */
91 if (!screen
->is_format_supported(screen
, pt
->format
, pt
->target
,
92 PIPE_TEXTURE_USAGE_RENDER_TARGET
, 0)) {
96 util_gen_mipmap(st
->gen_mipmap
, pt
, face
, baseLevel
, lastLevel
,
97 PIPE_TEX_FILTER_LINEAR
);
104 fallback_generate_mipmap(GLcontext
*ctx
, GLenum target
,
105 struct gl_texture_object
*texObj
)
107 struct pipe_context
*pipe
= ctx
->st
->pipe
;
108 struct pipe_screen
*screen
= pipe
->screen
;
109 struct pipe_texture
*pt
= st_get_texobj_texture(texObj
);
110 const uint baseLevel
= texObj
->BaseLevel
;
111 const uint lastLevel
= pt
->last_level
;
112 const uint face
= _mesa_tex_target_to_face(target
), zslice
= 0;
117 assert(target
!= GL_TEXTURE_3D
); /* not done yet */
119 _mesa_format_to_type_and_comps(texObj
->Image
[face
][baseLevel
]->TexFormat
,
122 for (dstLevel
= baseLevel
+ 1; dstLevel
<= lastLevel
; dstLevel
++) {
123 const uint srcLevel
= dstLevel
- 1;
124 struct pipe_transfer
*srcTrans
, *dstTrans
;
125 const ubyte
*srcData
;
127 int srcStride
, dstStride
;
129 srcTrans
= st_cond_flush_get_tex_transfer(st_context(ctx
), pt
, face
,
131 PIPE_TRANSFER_READ
, 0, 0,
133 pt
->height
[srcLevel
]);
135 dstTrans
= st_cond_flush_get_tex_transfer(st_context(ctx
), pt
, face
,
137 PIPE_TRANSFER_WRITE
, 0, 0,
139 pt
->height
[dstLevel
]);
141 srcData
= (ubyte
*) screen
->transfer_map(screen
, srcTrans
);
142 dstData
= (ubyte
*) screen
->transfer_map(screen
, dstTrans
);
144 srcStride
= srcTrans
->stride
/ srcTrans
->block
.size
;
145 dstStride
= dstTrans
->stride
/ dstTrans
->block
.size
;
147 _mesa_generate_mipmap_level(target
, datatype
, comps
,
149 pt
->width
[srcLevel
], pt
->height
[srcLevel
], pt
->depth
[srcLevel
],
151 srcStride
, /* stride in texels */
152 pt
->width
[dstLevel
], pt
->height
[dstLevel
], pt
->depth
[dstLevel
],
154 dstStride
); /* stride in texels */
156 screen
->transfer_unmap(screen
, srcTrans
);
157 screen
->transfer_unmap(screen
, dstTrans
);
159 screen
->tex_transfer_destroy(srcTrans
);
160 screen
->tex_transfer_destroy(dstTrans
);
166 * Compute the expected number of mipmap levels in the texture given
167 * the width/height/depth of the base image and the GL_TEXTURE_BASE_LEVEL/
168 * GL_TEXTURE_MAX_LEVEL settings. This will tell us how many mipmap
169 * level should be generated.
172 compute_num_levels(GLcontext
*ctx
,
173 struct gl_texture_object
*texObj
,
176 if (target
== GL_TEXTURE_RECTANGLE_ARB
) {
180 const GLuint maxLevels
= texObj
->MaxLevel
- texObj
->BaseLevel
+ 1;
181 const struct gl_texture_image
*baseImage
=
182 _mesa_get_tex_image(ctx
, texObj
, target
, texObj
->BaseLevel
);
183 GLuint size
, numLevels
;
185 size
= MAX2(baseImage
->Width2
, baseImage
->Height2
);
186 size
= MAX2(size
, baseImage
->Depth2
);
195 numLevels
= MIN2(numLevels
, maxLevels
);
203 st_generate_mipmap(GLcontext
*ctx
, GLenum target
,
204 struct gl_texture_object
*texObj
)
206 struct st_context
*st
= ctx
->st
;
207 struct pipe_texture
*pt
= st_get_texobj_texture(texObj
);
208 const uint baseLevel
= texObj
->BaseLevel
;
215 /* find expected last mipmap level */
216 lastLevel
= compute_num_levels(ctx
, texObj
, target
) - 1;
221 if (pt
->last_level
< lastLevel
) {
222 /* The current gallium texture doesn't have space for all the
223 * mipmap levels we need to generate. So allocate a new texture.
225 struct st_texture_object
*stObj
= st_texture_object(texObj
);
226 struct pipe_texture
*oldTex
= stObj
->pt
;
229 /* create new texture with space for more levels */
230 stObj
->pt
= st_texture_create(st
,
239 /* The texture isn't in a "complete" state yet so set the expected
240 * lastLevel here, since it won't get done in st_finalize_texture().
242 stObj
->lastLevel
= lastLevel
;
244 /* This will copy the old texture's base image into the new texture
245 * which we just allocated.
247 st_finalize_texture(ctx
, st
->pipe
, texObj
, &needFlush
);
249 /* release the old tex (will likely be freed too) */
250 pipe_texture_reference(&oldTex
, NULL
);
255 assert(lastLevel
<= pt
->last_level
);
257 /* Recall that the Mesa BaseLevel image is stored in the gallium
258 * texture's level[0] position. So pass baseLevel=0 here.
260 if (!st_render_mipmap(st
, target
, pt
, 0, lastLevel
)) {
261 fallback_generate_mipmap(ctx
, target
, texObj
);
264 /* Fill in the Mesa gl_texture_image fields */
265 for (dstLevel
= baseLevel
+ 1; dstLevel
<= lastLevel
; dstLevel
++) {
266 const uint srcLevel
= dstLevel
- 1;
267 const struct gl_texture_image
*srcImage
268 = _mesa_get_tex_image(ctx
, texObj
, target
, srcLevel
);
269 struct gl_texture_image
*dstImage
;
270 struct st_texture_image
*stImage
;
271 uint dstWidth
= pt
->width
[dstLevel
];
272 uint dstHeight
= pt
->height
[dstLevel
];
273 uint dstDepth
= pt
->depth
[dstLevel
];
274 uint border
= srcImage
->Border
;
276 dstImage
= _mesa_get_tex_image(ctx
, texObj
, target
, dstLevel
);
278 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "generating mipmaps");
282 /* Free old image data */
284 ctx
->Driver
.FreeTexImageData(ctx
, dstImage
);
286 /* initialize new image */
287 _mesa_init_teximage_fields(ctx
, target
, dstImage
, dstWidth
, dstHeight
,
288 dstDepth
, border
, srcImage
->InternalFormat
);
290 dstImage
->TexFormat
= srcImage
->TexFormat
;
292 stImage
= (struct st_texture_image
*) dstImage
;
293 pipe_texture_reference(&stImage
->pt
, pt
);