i965/vec4: Allow constant propagation into dot product.
[mesa.git] / src / mesa / drivers / common / meta_generate_mipmap.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 2009 VMware, Inc. 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 "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 /**
26 * Meta operations. Some GL operations can be expressed in terms of
27 * other GL operations. For example, glBlitFramebuffer() can be done
28 * with texture mapping and glClear() can be done with polygon rendering.
29 *
30 * \author Brian Paul
31 */
32
33 #include "main/arrayobj.h"
34 #include "main/buffers.h"
35 #include "main/enums.h"
36 #include "main/enable.h"
37 #include "main/fbobject.h"
38 #include "main/macros.h"
39 #include "main/mipmap.h"
40 #include "main/teximage.h"
41 #include "main/texobj.h"
42 #include "main/texparam.h"
43 #include "main/varray.h"
44 #include "main/viewport.h"
45 #include "drivers/common/meta.h"
46
47 /**
48 * Bind a particular texture level/layer to mipmap->FBO's GL_COLOR_ATTACHMENT0.
49 */
50 static void
51 bind_fbo_image(struct gl_texture_object *texObj, GLenum target,
52 GLuint level, GLuint layer)
53 {
54 switch (target) {
55 case GL_TEXTURE_1D:
56 _mesa_FramebufferTexture1D(GL_FRAMEBUFFER,
57 GL_COLOR_ATTACHMENT0,
58 target,
59 texObj->Name,
60 level);
61 break;
62 case GL_TEXTURE_1D_ARRAY:
63 case GL_TEXTURE_2D_ARRAY:
64 case GL_TEXTURE_3D:
65 _mesa_FramebufferTextureLayer(GL_FRAMEBUFFER,
66 GL_COLOR_ATTACHMENT0,
67 texObj->Name,
68 level,
69 layer);
70 break;
71 default: /* 2D / cube */
72 _mesa_FramebufferTexture2D(GL_FRAMEBUFFER,
73 GL_COLOR_ATTACHMENT0,
74 target,
75 texObj->Name,
76 level);
77 }
78 }
79
80 /**
81 * Check if the call to _mesa_meta_GenerateMipmap() will require a
82 * software fallback. The fallback path will require that the texture
83 * images are mapped.
84 * \return GL_TRUE if a fallback is needed, GL_FALSE otherwise
85 */
86 static bool
87 fallback_required(struct gl_context *ctx, GLenum target,
88 struct gl_texture_object *texObj)
89 {
90 const GLuint fboSave = ctx->DrawBuffer->Name;
91 struct gen_mipmap_state *mipmap = &ctx->Meta->Mipmap;
92 struct gl_texture_image *baseImage;
93 GLuint srcLevel;
94 GLenum status;
95
96 /* check for fallbacks */
97 if (target == GL_TEXTURE_3D) {
98 _mesa_perf_debug(ctx, MESA_DEBUG_SEVERITY_HIGH,
99 "glGenerateMipmap() to %s target\n",
100 _mesa_lookup_enum_by_nr(target));
101 return true;
102 }
103
104 srcLevel = texObj->BaseLevel;
105 baseImage = _mesa_select_tex_image(ctx, texObj, target, srcLevel);
106 if (!baseImage) {
107 _mesa_perf_debug(ctx, MESA_DEBUG_SEVERITY_HIGH,
108 "glGenerateMipmap() couldn't find base teximage\n");
109 return true;
110 }
111
112 if (_mesa_is_format_compressed(baseImage->TexFormat)) {
113 _mesa_perf_debug(ctx, MESA_DEBUG_SEVERITY_HIGH,
114 "glGenerateMipmap() with %s format\n",
115 _mesa_get_format_name(baseImage->TexFormat));
116 return true;
117 }
118
119 if (_mesa_get_format_color_encoding(baseImage->TexFormat) == GL_SRGB &&
120 !ctx->Extensions.EXT_texture_sRGB_decode) {
121 /* The texture format is sRGB but we can't turn off sRGB->linear
122 * texture sample conversion. So we won't be able to generate the
123 * right colors when rendering. Need to use a fallback.
124 */
125 _mesa_perf_debug(ctx, MESA_DEBUG_SEVERITY_HIGH,
126 "glGenerateMipmap() of sRGB texture without "
127 "sRGB decode\n");
128 return true;
129 }
130
131 /*
132 * Test that we can actually render in the texture's format.
133 */
134 if (!mipmap->FBO)
135 _mesa_GenFramebuffers(1, &mipmap->FBO);
136 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT, mipmap->FBO);
137
138 bind_fbo_image(texObj, target, srcLevel, 0);
139
140 status = _mesa_CheckFramebufferStatus(GL_FRAMEBUFFER_EXT);
141
142 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT, fboSave);
143
144 if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
145 _mesa_perf_debug(ctx, MESA_DEBUG_SEVERITY_HIGH,
146 "glGenerateMipmap() got incomplete FBO\n");
147 return true;
148 }
149
150 return false;
151 }
152
153 void
154 _mesa_meta_glsl_generate_mipmap_cleanup(struct gen_mipmap_state *mipmap)
155 {
156 if (mipmap->VAO == 0)
157 return;
158 _mesa_DeleteVertexArrays(1, &mipmap->VAO);
159 mipmap->VAO = 0;
160 _mesa_DeleteBuffers(1, &mipmap->VBO);
161 mipmap->VBO = 0;
162
163 _mesa_meta_blit_shader_table_cleanup(&mipmap->shaders);
164 }
165
166 static GLboolean
167 prepare_mipmap_level(struct gl_context *ctx,
168 struct gl_texture_object *texObj, GLuint level,
169 GLsizei width, GLsizei height, GLsizei depth,
170 GLenum intFormat, mesa_format format)
171 {
172 if (texObj->Target == GL_TEXTURE_1D_ARRAY) {
173 /* Work around Mesa expecting the number of array slices in "height". */
174 height = depth;
175 depth = 1;
176 }
177
178 return _mesa_prepare_mipmap_level(ctx, texObj, level, width, height, depth,
179 0, intFormat, format);
180 }
181
182 /**
183 * Called via ctx->Driver.GenerateMipmap()
184 * Note: We don't yet support 3D textures, 1D/2D array textures or texture
185 * borders.
186 */
187 void
188 _mesa_meta_GenerateMipmap(struct gl_context *ctx, GLenum target,
189 struct gl_texture_object *texObj)
190 {
191 struct gen_mipmap_state *mipmap = &ctx->Meta->Mipmap;
192 struct vertex verts[4];
193 const GLuint baseLevel = texObj->BaseLevel;
194 const GLuint maxLevel = texObj->MaxLevel;
195 const GLint maxLevelSave = texObj->MaxLevel;
196 const GLboolean genMipmapSave = texObj->GenerateMipmap;
197 const GLuint currentTexUnitSave = ctx->Texture.CurrentUnit;
198 const GLboolean use_glsl_version = ctx->Extensions.ARB_vertex_shader &&
199 ctx->Extensions.ARB_fragment_shader;
200 GLenum faceTarget;
201 GLuint dstLevel;
202 GLuint samplerSave;
203
204 if (fallback_required(ctx, target, texObj)) {
205 _mesa_generate_mipmap(ctx, target, texObj);
206 return;
207 }
208
209 if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X &&
210 target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z) {
211 faceTarget = target;
212 target = GL_TEXTURE_CUBE_MAP;
213 } else {
214 faceTarget = target;
215 }
216
217 _mesa_meta_begin(ctx, MESA_META_ALL);
218
219 /* Choose between glsl version and fixed function version of
220 * GenerateMipmap function.
221 */
222 if (use_glsl_version) {
223 _mesa_meta_setup_vertex_objects(&mipmap->VAO, &mipmap->VBO, true,
224 2, 3, 0);
225 _mesa_meta_setup_blit_shader(ctx, target, &mipmap->shaders);
226 } else {
227 _mesa_meta_setup_ff_tnl_for_blit(&mipmap->VAO, &mipmap->VBO, 3);
228 _mesa_set_enable(ctx, target, GL_TRUE);
229 }
230
231 samplerSave = ctx->Texture.Unit[ctx->Texture.CurrentUnit].Sampler ?
232 ctx->Texture.Unit[ctx->Texture.CurrentUnit].Sampler->Name : 0;
233
234 if (currentTexUnitSave != 0)
235 _mesa_BindTexture(target, texObj->Name);
236
237 if (!mipmap->Sampler) {
238 _mesa_GenSamplers(1, &mipmap->Sampler);
239 _mesa_BindSampler(ctx->Texture.CurrentUnit, mipmap->Sampler);
240
241 _mesa_SamplerParameteri(mipmap->Sampler,
242 GL_TEXTURE_MIN_FILTER,
243 GL_LINEAR_MIPMAP_LINEAR);
244 _mesa_SamplerParameteri(mipmap->Sampler, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
245 _mesa_SamplerParameteri(mipmap->Sampler, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
246 _mesa_SamplerParameteri(mipmap->Sampler, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
247 _mesa_SamplerParameteri(mipmap->Sampler, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
248
249 /* We don't want to encode or decode sRGB values; treat them as linear.
250 * This is not technically correct for GLES3 but we don't get any API
251 * error at the moment.
252 */
253 if (ctx->Extensions.EXT_texture_sRGB_decode) {
254 _mesa_SamplerParameteri(mipmap->Sampler, GL_TEXTURE_SRGB_DECODE_EXT,
255 GL_SKIP_DECODE_EXT);
256 }
257 } else {
258 _mesa_BindSampler(ctx->Texture.CurrentUnit, mipmap->Sampler);
259 }
260
261 assert(mipmap->FBO != 0);
262 _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT, mipmap->FBO);
263
264 _mesa_TexParameteri(target, GL_GENERATE_MIPMAP, GL_FALSE);
265
266 /* Silence valgrind warnings about reading uninitialized stack. */
267 memset(verts, 0, sizeof(verts));
268
269 /* setup vertex positions */
270 verts[0].x = -1.0F;
271 verts[0].y = -1.0F;
272 verts[1].x = 1.0F;
273 verts[1].y = -1.0F;
274 verts[2].x = 1.0F;
275 verts[2].y = 1.0F;
276 verts[3].x = -1.0F;
277 verts[3].y = 1.0F;
278
279 /* texture is already locked, unlock now */
280 _mesa_unlock_texture(ctx, texObj);
281
282 for (dstLevel = baseLevel + 1; dstLevel <= maxLevel; dstLevel++) {
283 const struct gl_texture_image *srcImage;
284 const GLuint srcLevel = dstLevel - 1;
285 GLuint layer;
286 GLsizei srcWidth, srcHeight, srcDepth;
287 GLsizei dstWidth, dstHeight, dstDepth;
288
289 srcImage = _mesa_select_tex_image(ctx, texObj, faceTarget, srcLevel);
290 assert(srcImage->Border == 0);
291
292 /* src size */
293 srcWidth = srcImage->Width;
294 if (target == GL_TEXTURE_1D_ARRAY) {
295 srcHeight = 1;
296 srcDepth = srcImage->Height;
297 } else {
298 srcHeight = srcImage->Height;
299 srcDepth = srcImage->Depth;
300 }
301
302 /* new dst size */
303 dstWidth = minify(srcWidth, 1);
304 dstHeight = minify(srcHeight, 1);
305 dstDepth = target == GL_TEXTURE_3D ? minify(srcDepth, 1) : srcDepth;
306
307 if (dstWidth == srcWidth &&
308 dstHeight == srcHeight &&
309 dstDepth == srcDepth) {
310 /* all done */
311 break;
312 }
313
314 /* Allocate storage for the destination mipmap image(s) */
315
316 /* Set MaxLevel large enough to hold the new level when we allocate it */
317 _mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, dstLevel);
318
319 if (!prepare_mipmap_level(ctx, texObj, dstLevel,
320 dstWidth, dstHeight, dstDepth,
321 srcImage->InternalFormat,
322 srcImage->TexFormat)) {
323 /* All done. We either ran out of memory or we would go beyond the
324 * last valid level of an immutable texture if we continued.
325 */
326 break;
327 }
328
329 /* limit minification to src level */
330 _mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, srcLevel);
331
332 /* setup viewport */
333 _mesa_set_viewport(ctx, 0, 0, 0, dstWidth, dstHeight);
334 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0);
335
336 for (layer = 0; layer < dstDepth; ++layer) {
337 /* Setup texture coordinates */
338 _mesa_meta_setup_texture_coords(faceTarget,
339 layer,
340 0, 0, 1, /* width, height never used here */
341 verts[0].tex,
342 verts[1].tex,
343 verts[2].tex,
344 verts[3].tex);
345
346 /* upload vertex data */
347 _mesa_BufferData(GL_ARRAY_BUFFER_ARB, sizeof(verts),
348 verts, GL_DYNAMIC_DRAW_ARB);
349
350 bind_fbo_image(texObj, faceTarget, dstLevel, layer);
351
352 /* sanity check */
353 if (_mesa_CheckFramebufferStatus(GL_FRAMEBUFFER) !=
354 GL_FRAMEBUFFER_COMPLETE) {
355 _mesa_problem(ctx, "Unexpected incomplete framebuffer in "
356 "_mesa_meta_GenerateMipmap()");
357 break;
358 }
359
360 assert(dstWidth == ctx->DrawBuffer->Width);
361 if (target == GL_TEXTURE_1D_ARRAY) {
362 assert(dstHeight == 1);
363 } else {
364 assert(dstHeight == ctx->DrawBuffer->Height);
365 }
366
367 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
368 }
369 }
370
371 _mesa_lock_texture(ctx, texObj); /* relock */
372
373 _mesa_BindSampler(ctx->Texture.CurrentUnit, samplerSave);
374
375 _mesa_meta_end(ctx);
376
377 _mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, maxLevelSave);
378 if (genMipmapSave)
379 _mesa_TexParameteri(target, GL_GENERATE_MIPMAP, genMipmapSave);
380 }