5f09d9bb0663bddad2af6dd86a02fb05017c426a
[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 "pipe/p_winsys.h"
40 #include "util/u_gen_mipmap.h"
41
42 #include "cso_cache/cso_cache.h"
43 #include "cso_cache/cso_context.h"
44
45 #include "st_context.h"
46 #include "st_draw.h"
47 #include "st_gen_mipmap.h"
48 #include "st_program.h"
49 #include "st_texture.h"
50 #include "st_cb_drawpixels.h"
51 #include "st_cb_texture.h"
52
53
54 /**
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.
58 */
59 void
60 st_init_generate_mipmap(struct st_context *st)
61 {
62 st->gen_mipmap = util_create_gen_mipmap(st->pipe);
63 }
64
65
66 void
67 st_destroy_generate_mipmpap(struct st_context *st)
68 {
69 util_destroy_gen_mipmap(st->gen_mipmap);
70 st->gen_mipmap = NULL;
71 }
72
73
74 /**
75 * Generate mipmap levels using hardware rendering.
76 * \return TRUE if successful, FALSE if not possible
77 */
78 static boolean
79 st_render_mipmap(struct st_context *st,
80 GLenum target,
81 struct pipe_texture *pt,
82 uint baseLevel, uint lastLevel)
83 {
84 struct pipe_context *pipe = st->pipe;
85 struct pipe_screen *screen = pipe->screen;
86 const uint face = _mesa_tex_target_to_face(target);
87
88 assert(target != GL_TEXTURE_3D); /* not done yet */
89
90 /* check if we can render in the texture's format */
91 if (!screen->is_format_supported(screen, pt->format, PIPE_SURFACE)) {
92 return FALSE;
93 }
94
95 util_gen_mipmap(st->gen_mipmap, pt, face, baseLevel, lastLevel);
96
97 /* restore pipe state */
98 #if 0
99 cso_set_rasterizer(st->cso_context, &st->state.rasterizer);
100 cso_set_samplers(st->cso_context, st->state.samplers_list);
101 pipe->bind_fs_state(pipe, st->fp->shader_program);
102 pipe->bind_vs_state(pipe, st->vp->shader_program);
103 pipe->set_sampler_textures(pipe, st->state.num_textures,
104 st->state.sampler_texture);
105 pipe->set_viewport_state(pipe, &st->state.viewport);
106 #else
107 /* XXX is this sufficient? */
108 st_invalidate_state(st->ctx, _NEW_COLOR | _NEW_TEXTURE);
109 #endif
110
111 return TRUE;
112 }
113
114
115 static void
116 fallback_generate_mipmap(GLcontext *ctx, GLenum target,
117 struct gl_texture_object *texObj)
118 {
119 struct pipe_context *pipe = ctx->st->pipe;
120 struct pipe_screen *screen = pipe->screen;
121 struct pipe_winsys *ws = pipe->winsys;
122 struct pipe_texture *pt = st_get_texobj_texture(texObj);
123 const uint baseLevel = texObj->BaseLevel;
124 const uint lastLevel = pt->last_level;
125 const uint face = _mesa_tex_target_to_face(target), zslice = 0;
126 uint dstLevel;
127 GLenum datatype;
128 GLuint comps;
129
130 assert(target != GL_TEXTURE_3D); /* not done yet */
131
132 _mesa_format_to_type_and_comps(texObj->Image[face][baseLevel]->TexFormat,
133 &datatype, &comps);
134
135 for (dstLevel = baseLevel + 1; dstLevel <= lastLevel; dstLevel++) {
136 const uint srcLevel = dstLevel - 1;
137 struct pipe_surface *srcSurf, *dstSurf;
138 const ubyte *srcData;
139 ubyte *dstData;
140
141 srcSurf = screen->get_tex_surface(screen, pt, face, srcLevel, zslice);
142 dstSurf = screen->get_tex_surface(screen, pt, face, dstLevel, zslice);
143
144 srcData = (ubyte *) ws->buffer_map(ws, srcSurf->buffer,
145 PIPE_BUFFER_USAGE_CPU_READ)
146 + srcSurf->offset;
147 dstData = (ubyte *) ws->buffer_map(ws, dstSurf->buffer,
148 PIPE_BUFFER_USAGE_CPU_WRITE)
149 + dstSurf->offset;
150
151 _mesa_generate_mipmap_level(target, datatype, comps,
152 0 /*border*/,
153 pt->width[srcLevel], pt->height[srcLevel], pt->depth[srcLevel],
154 srcSurf->pitch * srcSurf->cpp, /* stride in bytes */
155 srcData,
156 pt->width[dstLevel], pt->height[dstLevel], pt->depth[dstLevel],
157 dstSurf->pitch * dstSurf->cpp, /* stride in bytes */
158 dstData);
159
160 ws->buffer_unmap(ws, srcSurf->buffer);
161 ws->buffer_unmap(ws, dstSurf->buffer);
162
163 pipe_surface_reference(&srcSurf, NULL);
164 pipe_surface_reference(&dstSurf, NULL);
165 }
166 }
167
168
169 void
170 st_generate_mipmap(GLcontext *ctx, GLenum target,
171 struct gl_texture_object *texObj)
172 {
173 struct st_context *st = ctx->st;
174 struct pipe_texture *pt = st_get_texobj_texture(texObj);
175 const uint baseLevel = texObj->BaseLevel;
176 const uint lastLevel = pt->last_level;
177 uint dstLevel;
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 stImage->pt = pt;
216 }
217 }