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/mipmap.h"
31 #include "main/teximage.h"
32 #include "main/texformat.h"
34 #include "shader/prog_instruction.h"
36 #include "pipe/p_context.h"
37 #include "pipe/p_defines.h"
38 #include "pipe/p_inlines.h"
39 #include "util/u_gen_mipmap.h"
41 #include "cso_cache/cso_cache.h"
42 #include "cso_cache/cso_context.h"
44 #include "st_context.h"
46 #include "st_gen_mipmap.h"
47 #include "st_program.h"
48 #include "st_texture.h"
49 #include "st_cb_texture.h"
50 #include "st_inlines.h"
54 * one-time init for generate mipmap
55 * XXX Note: there may be other times we need no-op/simple state like this.
56 * In that case, some code refactoring would be good.
59 st_init_generate_mipmap(struct st_context
*st
)
61 st
->gen_mipmap
= util_create_gen_mipmap(st
->pipe
, st
->cso_context
);
66 st_destroy_generate_mipmap(struct st_context
*st
)
68 util_destroy_gen_mipmap(st
->gen_mipmap
);
69 st
->gen_mipmap
= NULL
;
74 * Generate mipmap levels using hardware rendering.
75 * \return TRUE if successful, FALSE if not possible
78 st_render_mipmap(struct st_context
*st
,
80 struct pipe_texture
*pt
,
81 uint baseLevel
, uint lastLevel
)
83 struct pipe_context
*pipe
= st
->pipe
;
84 struct pipe_screen
*screen
= pipe
->screen
;
85 const uint face
= _mesa_tex_target_to_face(target
);
87 assert(target
!= GL_TEXTURE_3D
); /* not done yet */
89 /* check if we can render in the texture's format */
90 if (!screen
->is_format_supported(screen
, pt
->format
, pt
->target
,
91 PIPE_TEXTURE_USAGE_RENDER_TARGET
, 0)) {
95 util_gen_mipmap(st
->gen_mipmap
, pt
, face
, baseLevel
, lastLevel
,
96 PIPE_TEX_FILTER_LINEAR
);
103 fallback_generate_mipmap(GLcontext
*ctx
, GLenum target
,
104 struct gl_texture_object
*texObj
)
106 struct pipe_context
*pipe
= ctx
->st
->pipe
;
107 struct pipe_screen
*screen
= pipe
->screen
;
108 struct pipe_texture
*pt
= st_get_texobj_texture(texObj
);
109 const uint baseLevel
= texObj
->BaseLevel
;
110 const uint lastLevel
= pt
->last_level
;
111 const uint face
= _mesa_tex_target_to_face(target
), zslice
= 0;
116 assert(target
!= GL_TEXTURE_3D
); /* not done yet */
118 _mesa_format_to_type_and_comps(texObj
->Image
[face
][baseLevel
]->TexFormat
,
121 for (dstLevel
= baseLevel
+ 1; dstLevel
<= lastLevel
; dstLevel
++) {
122 const uint srcLevel
= dstLevel
- 1;
123 struct pipe_transfer
*srcTrans
, *dstTrans
;
124 const ubyte
*srcData
;
126 int srcStride
, dstStride
;
128 srcTrans
= st_cond_flush_get_tex_transfer(st_context(ctx
), pt
, face
,
130 PIPE_TRANSFER_READ
, 0, 0,
132 pt
->height
[srcLevel
]);
134 dstTrans
= st_cond_flush_get_tex_transfer(st_context(ctx
), pt
, face
,
136 PIPE_TRANSFER_WRITE
, 0, 0,
138 pt
->height
[dstLevel
]);
140 srcData
= (ubyte
*) screen
->transfer_map(screen
, srcTrans
);
141 dstData
= (ubyte
*) screen
->transfer_map(screen
, dstTrans
);
143 srcStride
= srcTrans
->stride
/ srcTrans
->block
.size
;
144 dstStride
= dstTrans
->stride
/ dstTrans
->block
.size
;
146 _mesa_generate_mipmap_level(target
, datatype
, comps
,
148 pt
->width
[srcLevel
], pt
->height
[srcLevel
], pt
->depth
[srcLevel
],
150 srcStride
, /* stride in texels */
151 pt
->width
[dstLevel
], pt
->height
[dstLevel
], pt
->depth
[dstLevel
],
153 dstStride
); /* stride in texels */
155 screen
->transfer_unmap(screen
, srcTrans
);
156 screen
->transfer_unmap(screen
, dstTrans
);
158 screen
->tex_transfer_destroy(srcTrans
);
159 screen
->tex_transfer_destroy(dstTrans
);
165 st_generate_mipmap(GLcontext
*ctx
, GLenum target
,
166 struct gl_texture_object
*texObj
)
168 struct st_context
*st
= ctx
->st
;
169 struct pipe_texture
*pt
= st_get_texobj_texture(texObj
);
170 const uint baseLevel
= texObj
->BaseLevel
;
177 lastLevel
= pt
->last_level
;
179 if (!st_render_mipmap(st
, target
, pt
, baseLevel
, lastLevel
)) {
180 fallback_generate_mipmap(ctx
, target
, texObj
);
183 /* Fill in the Mesa gl_texture_image fields */
184 for (dstLevel
= baseLevel
+ 1; dstLevel
<= lastLevel
; dstLevel
++) {
185 const uint srcLevel
= dstLevel
- 1;
186 const struct gl_texture_image
*srcImage
187 = _mesa_get_tex_image(ctx
, texObj
, target
, srcLevel
);
188 struct gl_texture_image
*dstImage
;
189 struct st_texture_image
*stImage
;
190 uint dstWidth
= pt
->width
[dstLevel
];
191 uint dstHeight
= pt
->height
[dstLevel
];
192 uint dstDepth
= pt
->depth
[dstLevel
];
193 uint border
= srcImage
->Border
;
195 dstImage
= _mesa_get_tex_image(ctx
, texObj
, target
, dstLevel
);
197 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "generating mipmaps");
201 /* Free old image data */
203 ctx
->Driver
.FreeTexImageData(ctx
, dstImage
);
205 /* initialize new image */
206 _mesa_init_teximage_fields(ctx
, target
, dstImage
, dstWidth
, dstHeight
,
207 dstDepth
, border
, srcImage
->InternalFormat
);
209 dstImage
->TexFormat
= srcImage
->TexFormat
;
211 stImage
= (struct st_texture_image
*) dstImage
;
212 pipe_texture_reference(&stImage
->pt
, pt
);