glsl: lower mediump temporaries to 16 bits except structures (v2)
[mesa.git] / src / mesa / state_tracker / st_gen_mipmap.c
1 /**************************************************************************
2 *
3 * Copyright 2008 VMware, Inc.
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 VMWARE 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/errors.h"
30
31 #include "main/mipmap.h"
32 #include "main/teximage.h"
33
34 #include "pipe/p_context.h"
35 #include "pipe/p_defines.h"
36 #include "util/u_inlines.h"
37 #include "util/format/u_format.h"
38 #include "util/u_gen_mipmap.h"
39
40 #include "st_debug.h"
41 #include "st_context.h"
42 #include "st_texture.h"
43 #include "st_util.h"
44 #include "st_gen_mipmap.h"
45 #include "st_cb_bitmap.h"
46 #include "st_cb_texture.h"
47
48
49 /**
50 * Called via ctx->Driver.GenerateMipmap().
51 */
52 void
53 st_generate_mipmap(struct gl_context *ctx, GLenum target,
54 struct gl_texture_object *texObj)
55 {
56 struct st_context *st = st_context(ctx);
57 struct st_texture_object *stObj = st_texture_object(texObj);
58 struct pipe_resource *pt = st_get_texobj_resource(texObj);
59 uint baseLevel = texObj->BaseLevel;
60 enum pipe_format format;
61 uint lastLevel, first_layer, last_layer;
62
63 if (!pt)
64 return;
65
66 if (texObj->Immutable)
67 baseLevel += texObj->MinLevel;
68
69 /* not sure if this ultimately actually should work,
70 but we're not supporting multisampled textures yet. */
71 assert(pt->nr_samples < 2);
72
73 /* find expected last mipmap level to generate*/
74 lastLevel = _mesa_compute_num_levels(ctx, texObj, target) - 1;
75
76 if (texObj->Immutable)
77 lastLevel += texObj->MinLevel;
78
79 if (lastLevel == 0)
80 return;
81
82 st_flush_bitmap_cache(st);
83 st_invalidate_readpix_cache(st);
84
85 /* The texture isn't in a "complete" state yet so set the expected
86 * lastLevel here, since it won't get done in st_finalize_texture().
87 */
88 stObj->lastLevel = lastLevel;
89
90 if (!texObj->Immutable) {
91 const GLboolean genSave = texObj->GenerateMipmap;
92
93 /* Temporarily set GenerateMipmap to true so that allocate_full_mipmap()
94 * makes the right decision about full mipmap allocation.
95 */
96 texObj->GenerateMipmap = GL_TRUE;
97
98 _mesa_prepare_mipmap_levels(ctx, texObj, baseLevel, lastLevel);
99
100 texObj->GenerateMipmap = genSave;
101
102 /* At this point, memory for all the texture levels has been
103 * allocated. However, the base level image may be in one resource
104 * while the subsequent/smaller levels may be in another resource.
105 * Finalizing the texture will copy the base images from the former
106 * resource to the latter.
107 *
108 * After this, we'll have all mipmap levels in one resource.
109 */
110 st_finalize_texture(ctx, st->pipe, texObj, 0);
111 }
112
113 pt = stObj->pt;
114 if (!pt) {
115 _mesa_error(ctx, GL_OUT_OF_MEMORY, "mipmap generation");
116 return;
117 }
118
119 assert(pt->last_level >= lastLevel);
120
121 if (pt->target == PIPE_TEXTURE_CUBE) {
122 first_layer = last_layer = _mesa_tex_target_to_face(target);
123 }
124 else {
125 first_layer = 0;
126 last_layer = util_max_layer(pt, baseLevel);
127 }
128
129 if (stObj->surface_based)
130 format = stObj->surface_format;
131 else
132 format = pt->format;
133
134 if (texObj->Sampler.sRGBDecode == GL_SKIP_DECODE_EXT)
135 format = util_format_linear(format);
136
137 /* First see if the driver supports hardware mipmap generation,
138 * if not then generate the mipmap by rendering/texturing.
139 * If that fails, use the software fallback.
140 */
141 if (!st->pipe->screen->get_param(st->pipe->screen,
142 PIPE_CAP_GENERATE_MIPMAP) ||
143 !st->pipe->generate_mipmap(st->pipe, pt, format, baseLevel,
144 lastLevel, first_layer, last_layer)) {
145
146 if (!util_gen_mipmap(st->pipe, pt, format, baseLevel, lastLevel,
147 first_layer, last_layer, PIPE_TEX_FILTER_LINEAR)) {
148 _mesa_generate_mipmap(ctx, target, texObj);
149 }
150 }
151 }