intel / DRI2: When available, use DRI2GetBuffersWithFormat
[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, 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
127 srcTrans = st_cond_flush_get_tex_transfer(st_context(ctx), pt, face,
128 srcLevel, zslice,
129 PIPE_TRANSFER_READ, 0, 0,
130 pt->width[srcLevel],
131 pt->height[srcLevel]);
132
133 dstTrans = st_cond_flush_get_tex_transfer(st_context(ctx), pt, face,
134 dstLevel, zslice,
135 PIPE_TRANSFER_WRITE, 0, 0,
136 pt->width[dstLevel],
137 pt->height[dstLevel]);
138
139 srcData = (ubyte *) screen->transfer_map(screen, srcTrans);
140 dstData = (ubyte *) screen->transfer_map(screen, dstTrans);
141
142 _mesa_generate_mipmap_level(target, datatype, comps,
143 0 /*border*/,
144 pt->width[srcLevel], pt->height[srcLevel], pt->depth[srcLevel],
145 srcData,
146 srcTrans->stride, /* stride in bytes */
147 pt->width[dstLevel], pt->height[dstLevel], pt->depth[dstLevel],
148 dstData,
149 dstTrans->stride); /* stride in bytes */
150
151 screen->transfer_unmap(screen, srcTrans);
152 screen->transfer_unmap(screen, dstTrans);
153
154 screen->tex_transfer_destroy(srcTrans);
155 screen->tex_transfer_destroy(dstTrans);
156 }
157 }
158
159
160 void
161 st_generate_mipmap(GLcontext *ctx, GLenum target,
162 struct gl_texture_object *texObj)
163 {
164 struct st_context *st = ctx->st;
165 struct pipe_texture *pt = st_get_texobj_texture(texObj);
166 const uint baseLevel = texObj->BaseLevel;
167 uint lastLevel;
168 uint dstLevel;
169
170 if (!pt)
171 return;
172
173 lastLevel = pt->last_level;
174
175 if (!st_render_mipmap(st, target, pt, baseLevel, lastLevel)) {
176 fallback_generate_mipmap(ctx, target, texObj);
177 }
178
179 /* Fill in the Mesa gl_texture_image fields */
180 for (dstLevel = baseLevel + 1; dstLevel <= lastLevel; dstLevel++) {
181 const uint srcLevel = dstLevel - 1;
182 const struct gl_texture_image *srcImage
183 = _mesa_get_tex_image(ctx, texObj, target, srcLevel);
184 struct gl_texture_image *dstImage;
185 struct st_texture_image *stImage;
186 uint dstWidth = pt->width[dstLevel];
187 uint dstHeight = pt->height[dstLevel];
188 uint dstDepth = pt->depth[dstLevel];
189 uint border = srcImage->Border;
190
191 dstImage = _mesa_get_tex_image(ctx, texObj, target, dstLevel);
192 if (!dstImage) {
193 _mesa_error(ctx, GL_OUT_OF_MEMORY, "generating mipmaps");
194 return;
195 }
196
197 if (dstImage->ImageOffsets)
198 _mesa_free(dstImage->ImageOffsets);
199
200 /* Free old image data */
201 if (dstImage->Data)
202 ctx->Driver.FreeTexImageData(ctx, dstImage);
203
204 /* initialize new image */
205 _mesa_init_teximage_fields(ctx, target, dstImage, dstWidth, dstHeight,
206 dstDepth, border, srcImage->InternalFormat);
207
208 dstImage->TexFormat = srcImage->TexFormat;
209
210 stImage = (struct st_texture_image *) dstImage;
211 pipe_texture_reference(&stImage->pt, pt);
212 }
213 }