Merge branch 'master' into radeon-rewrite
[mesa.git] / src / mesa / state_tracker / st_gen_mipmap.c
1 /**************************************************************************
2 *
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28
29 #include "main/imports.h"
30 #include "main/mipmap.h"
31 #include "main/teximage.h"
32 #include "main/texformat.h"
33
34 #include "shader/prog_instruction.h"
35
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"
40
41 #include "cso_cache/cso_cache.h"
42 #include "cso_cache/cso_context.h"
43
44 #include "st_context.h"
45 #include "st_draw.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"
51
52
53 /**
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.
57 */
58 void
59 st_init_generate_mipmap(struct st_context *st)
60 {
61 st->gen_mipmap = util_create_gen_mipmap(st->pipe, st->cso_context);
62 }
63
64
65 void
66 st_destroy_generate_mipmap(struct st_context *st)
67 {
68 util_destroy_gen_mipmap(st->gen_mipmap);
69 st->gen_mipmap = NULL;
70 }
71
72
73 /**
74 * Generate mipmap levels using hardware rendering.
75 * \return TRUE if successful, FALSE if not possible
76 */
77 static boolean
78 st_render_mipmap(struct st_context *st,
79 GLenum target,
80 struct pipe_texture *pt,
81 uint baseLevel, uint lastLevel)
82 {
83 struct pipe_context *pipe = st->pipe;
84 struct pipe_screen *screen = pipe->screen;
85 const uint face = _mesa_tex_target_to_face(target);
86
87 assert(target != GL_TEXTURE_3D); /* not done yet */
88
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)) {
92 return FALSE;
93 }
94
95 util_gen_mipmap(st->gen_mipmap, pt, face, baseLevel, lastLevel,
96 PIPE_TEX_FILTER_LINEAR);
97
98 return TRUE;
99 }
100
101
102 static void
103 fallback_generate_mipmap(GLcontext *ctx, GLenum target,
104 struct gl_texture_object *texObj)
105 {
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;
112 uint dstLevel;
113 GLenum datatype;
114 GLuint comps;
115
116 assert(target != GL_TEXTURE_3D); /* not done yet */
117
118 _mesa_format_to_type_and_comps(texObj->Image[face][baseLevel]->TexFormat,
119 &datatype, &comps);
120
121 for (dstLevel = baseLevel + 1; dstLevel <= lastLevel; dstLevel++) {
122 const uint srcLevel = dstLevel - 1;
123 struct pipe_transfer *srcTrans, *dstTrans;
124 const ubyte *srcData;
125 ubyte *dstData;
126 int srcStride, dstStride;
127
128 srcTrans = st_cond_flush_get_tex_transfer(st_context(ctx), pt, face,
129 srcLevel, zslice,
130 PIPE_TRANSFER_READ, 0, 0,
131 pt->width[srcLevel],
132 pt->height[srcLevel]);
133
134 dstTrans = st_cond_flush_get_tex_transfer(st_context(ctx), pt, face,
135 dstLevel, zslice,
136 PIPE_TRANSFER_WRITE, 0, 0,
137 pt->width[dstLevel],
138 pt->height[dstLevel]);
139
140 srcData = (ubyte *) screen->transfer_map(screen, srcTrans);
141 dstData = (ubyte *) screen->transfer_map(screen, dstTrans);
142
143 srcStride = srcTrans->stride / srcTrans->block.size;
144 dstStride = dstTrans->stride / dstTrans->block.size;
145
146 _mesa_generate_mipmap_level(target, datatype, comps,
147 0 /*border*/,
148 pt->width[srcLevel], pt->height[srcLevel], pt->depth[srcLevel],
149 srcData,
150 srcStride, /* stride in texels */
151 pt->width[dstLevel], pt->height[dstLevel], pt->depth[dstLevel],
152 dstData,
153 dstStride); /* stride in texels */
154
155 screen->transfer_unmap(screen, srcTrans);
156 screen->transfer_unmap(screen, dstTrans);
157
158 screen->tex_transfer_destroy(srcTrans);
159 screen->tex_transfer_destroy(dstTrans);
160 }
161 }
162
163
164 void
165 st_generate_mipmap(GLcontext *ctx, GLenum target,
166 struct gl_texture_object *texObj)
167 {
168 struct st_context *st = ctx->st;
169 struct pipe_texture *pt = st_get_texobj_texture(texObj);
170 const uint baseLevel = texObj->BaseLevel;
171 uint lastLevel;
172 uint dstLevel;
173
174 if (!pt)
175 return;
176
177 lastLevel = pt->last_level;
178
179 if (!st_render_mipmap(st, target, pt, baseLevel, lastLevel)) {
180 fallback_generate_mipmap(ctx, target, texObj);
181 }
182
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;
194
195 dstImage = _mesa_get_tex_image(ctx, texObj, target, dstLevel);
196 if (!dstImage) {
197 _mesa_error(ctx, GL_OUT_OF_MEMORY, "generating mipmaps");
198 return;
199 }
200
201 if (dstImage->ImageOffsets)
202 _mesa_free(dstImage->ImageOffsets);
203
204 /* Free old image data */
205 if (dstImage->Data)
206 ctx->Driver.FreeTexImageData(ctx, dstImage);
207
208 /* initialize new image */
209 _mesa_init_teximage_fields(ctx, target, dstImage, dstWidth, dstHeight,
210 dstDepth, border, srcImage->InternalFormat);
211
212 dstImage->TexFormat = srcImage->TexFormat;
213
214 stImage = (struct st_texture_image *) dstImage;
215 pipe_texture_reference(&stImage->pt, pt);
216 }
217 }