st_cb_bitmap: check NULL return from u_upload_alloc
[mesa.git] / src / mesa / state_tracker / st_atom_texture.c
1 /**************************************************************************
2 *
3 * Copyright 2007 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 * Authors:
30 * Keith Whitwell <keithw@vmware.com>
31 * Brian Paul
32 */
33
34
35 #include "main/context.h"
36 #include "main/macros.h"
37 #include "main/mtypes.h"
38 #include "main/samplerobj.h"
39 #include "main/teximage.h"
40 #include "main/texobj.h"
41 #include "program/prog_instruction.h"
42
43 #include "st_context.h"
44 #include "st_atom.h"
45 #include "st_sampler_view.h"
46 #include "st_texture.h"
47 #include "st_format.h"
48 #include "st_cb_texture.h"
49 #include "pipe/p_context.h"
50 #include "util/u_format.h"
51 #include "util/u_inlines.h"
52 #include "cso_cache/cso_context.h"
53
54
55 static GLboolean
56 update_single_texture(struct st_context *st,
57 struct pipe_sampler_view **sampler_view,
58 GLuint texUnit, unsigned glsl_version)
59 {
60 struct gl_context *ctx = st->ctx;
61 const struct gl_sampler_object *samp;
62 struct gl_texture_object *texObj;
63 struct st_texture_object *stObj;
64 GLboolean retval;
65
66 samp = _mesa_get_samplerobj(ctx, texUnit);
67
68 texObj = ctx->Texture.Unit[texUnit]._Current;
69
70 if (!texObj) {
71 texObj = _mesa_get_fallback_texture(ctx, TEXTURE_2D_INDEX);
72 samp = &texObj->Sampler;
73 }
74 stObj = st_texture_object(texObj);
75
76 retval = st_finalize_texture(ctx, st->pipe, texObj);
77 if (!retval) {
78 /* out of mem */
79 return GL_FALSE;
80 }
81
82 /* Check a few pieces of state outside the texture object to see if we
83 * need to force revalidation.
84 */
85 if (stObj->prev_glsl_version != glsl_version ||
86 stObj->prev_sRGBDecode != samp->sRGBDecode) {
87
88 st_texture_release_all_sampler_views(st, stObj);
89
90 stObj->prev_glsl_version = glsl_version;
91 stObj->prev_sRGBDecode = samp->sRGBDecode;
92 }
93
94 if (texObj->TargetIndex == TEXTURE_EXTERNAL_INDEX &&
95 stObj->pt->screen->resource_changed)
96 stObj->pt->screen->resource_changed(stObj->pt->screen, stObj->pt);
97
98 *sampler_view =
99 st_get_texture_sampler_view_from_stobj(st, stObj, samp, glsl_version);
100 return GL_TRUE;
101 }
102
103
104
105 static void
106 update_textures(struct st_context *st,
107 gl_shader_stage mesa_shader,
108 const struct gl_program *prog,
109 unsigned max_units,
110 struct pipe_sampler_view **sampler_views,
111 unsigned *num_textures)
112 {
113 const GLuint old_max = *num_textures;
114 GLbitfield samplers_used = prog->SamplersUsed;
115 GLbitfield free_slots = ~prog->SamplersUsed;
116 GLbitfield external_samplers_used = prog->ExternalSamplersUsed;
117 GLuint unit;
118 enum pipe_shader_type shader_stage = st_shader_stage_to_ptarget(mesa_shader);
119
120 if (samplers_used == 0x0 && old_max == 0)
121 return;
122
123 *num_textures = 0;
124
125 /* loop over sampler units (aka tex image units) */
126 for (unit = 0; unit < max_units; unit++, samplers_used >>= 1) {
127 struct pipe_sampler_view *sampler_view = NULL;
128
129 if (samplers_used & 1) {
130 /* prog->sh.data is NULL if it's ARB_fragment_program */
131 unsigned glsl_version = prog->sh.data ? prog->sh.data->Version : 0;
132 const GLuint texUnit = prog->SamplerUnits[unit];
133 GLboolean retval;
134
135 retval = update_single_texture(st, &sampler_view, texUnit,
136 glsl_version);
137 if (retval == GL_FALSE)
138 continue;
139
140 *num_textures = unit + 1;
141 }
142 else if (samplers_used == 0 && unit >= old_max) {
143 /* if we've reset all the old views and we have no more new ones */
144 break;
145 }
146
147 pipe_sampler_view_reference(&(sampler_views[unit]), sampler_view);
148 }
149
150 /* For any external samplers with multiplaner YUV, stuff the additional
151 * sampler views we need at the end.
152 *
153 * Trying to cache the sampler view in the stObj looks painful, so just
154 * re-create the sampler view for the extra planes each time. Main use
155 * case is video playback (ie. fps games wouldn't be using this) so I
156 * guess no point to try to optimize this feature.
157 */
158 while (unlikely(external_samplers_used)) {
159 GLuint unit = u_bit_scan(&external_samplers_used);
160 GLuint extra = 0;
161 struct st_texture_object *stObj =
162 st_get_texture_object(st->ctx, prog, unit);
163 struct pipe_sampler_view tmpl;
164
165 if (!stObj)
166 continue;
167
168 /* use original view as template: */
169 tmpl = *sampler_views[unit];
170
171 switch (st_get_view_format(stObj)) {
172 case PIPE_FORMAT_NV12:
173 /* we need one additional R8G8 view: */
174 tmpl.format = PIPE_FORMAT_RG88_UNORM;
175 tmpl.swizzle_g = PIPE_SWIZZLE_Y; /* tmpl from Y plane is R8 */
176 extra = u_bit_scan(&free_slots);
177 sampler_views[extra] =
178 st->pipe->create_sampler_view(st->pipe, stObj->pt->next, &tmpl);
179 break;
180 case PIPE_FORMAT_IYUV:
181 /* we need two additional R8 views: */
182 tmpl.format = PIPE_FORMAT_R8_UNORM;
183 extra = u_bit_scan(&free_slots);
184 sampler_views[extra] =
185 st->pipe->create_sampler_view(st->pipe, stObj->pt->next, &tmpl);
186 extra = u_bit_scan(&free_slots);
187 sampler_views[extra] =
188 st->pipe->create_sampler_view(st->pipe, stObj->pt->next->next, &tmpl);
189 break;
190 default:
191 break;
192 }
193
194 *num_textures = MAX2(*num_textures, extra + 1);
195 }
196
197 cso_set_sampler_views(st->cso_context,
198 shader_stage,
199 *num_textures,
200 sampler_views);
201 }
202
203
204
205 static void
206 update_vertex_textures(struct st_context *st)
207 {
208 const struct gl_context *ctx = st->ctx;
209
210 if (ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits > 0) {
211 update_textures(st,
212 MESA_SHADER_VERTEX,
213 ctx->VertexProgram._Current,
214 ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits,
215 st->state.sampler_views[PIPE_SHADER_VERTEX],
216 &st->state.num_sampler_views[PIPE_SHADER_VERTEX]);
217 }
218 }
219
220
221 static void
222 update_fragment_textures(struct st_context *st)
223 {
224 const struct gl_context *ctx = st->ctx;
225
226 update_textures(st,
227 MESA_SHADER_FRAGMENT,
228 ctx->FragmentProgram._Current,
229 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits,
230 st->state.sampler_views[PIPE_SHADER_FRAGMENT],
231 &st->state.num_sampler_views[PIPE_SHADER_FRAGMENT]);
232 }
233
234
235 static void
236 update_geometry_textures(struct st_context *st)
237 {
238 const struct gl_context *ctx = st->ctx;
239
240 if (ctx->GeometryProgram._Current) {
241 update_textures(st,
242 MESA_SHADER_GEOMETRY,
243 ctx->GeometryProgram._Current,
244 ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxTextureImageUnits,
245 st->state.sampler_views[PIPE_SHADER_GEOMETRY],
246 &st->state.num_sampler_views[PIPE_SHADER_GEOMETRY]);
247 }
248 }
249
250
251 static void
252 update_tessctrl_textures(struct st_context *st)
253 {
254 const struct gl_context *ctx = st->ctx;
255
256 if (ctx->TessCtrlProgram._Current) {
257 update_textures(st,
258 MESA_SHADER_TESS_CTRL,
259 ctx->TessCtrlProgram._Current,
260 ctx->Const.Program[MESA_SHADER_TESS_CTRL].MaxTextureImageUnits,
261 st->state.sampler_views[PIPE_SHADER_TESS_CTRL],
262 &st->state.num_sampler_views[PIPE_SHADER_TESS_CTRL]);
263 }
264 }
265
266
267 static void
268 update_tesseval_textures(struct st_context *st)
269 {
270 const struct gl_context *ctx = st->ctx;
271
272 if (ctx->TessEvalProgram._Current) {
273 update_textures(st,
274 MESA_SHADER_TESS_EVAL,
275 ctx->TessEvalProgram._Current,
276 ctx->Const.Program[MESA_SHADER_TESS_EVAL].MaxTextureImageUnits,
277 st->state.sampler_views[PIPE_SHADER_TESS_EVAL],
278 &st->state.num_sampler_views[PIPE_SHADER_TESS_EVAL]);
279 }
280 }
281
282
283 static void
284 update_compute_textures(struct st_context *st)
285 {
286 const struct gl_context *ctx = st->ctx;
287
288 if (ctx->ComputeProgram._Current) {
289 update_textures(st,
290 MESA_SHADER_COMPUTE,
291 ctx->ComputeProgram._Current,
292 ctx->Const.Program[MESA_SHADER_COMPUTE].MaxTextureImageUnits,
293 st->state.sampler_views[PIPE_SHADER_COMPUTE],
294 &st->state.num_sampler_views[PIPE_SHADER_COMPUTE]);
295 }
296 }
297
298
299 const struct st_tracked_state st_update_fragment_texture = {
300 update_fragment_textures /* update */
301 };
302
303
304 const struct st_tracked_state st_update_vertex_texture = {
305 update_vertex_textures /* update */
306 };
307
308
309 const struct st_tracked_state st_update_geometry_texture = {
310 update_geometry_textures /* update */
311 };
312
313
314 const struct st_tracked_state st_update_tessctrl_texture = {
315 update_tessctrl_textures /* update */
316 };
317
318
319 const struct st_tracked_state st_update_tesseval_texture = {
320 update_tesseval_textures /* update */
321 };
322
323
324 const struct st_tracked_state st_update_compute_texture = {
325 update_compute_textures /* update */
326 };