st/mesa: don't check MaxTextureImageUnits in update_textures
[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 /**
56 * Get a pipe_sampler_view object from a texture unit.
57 */
58 GLboolean
59 st_update_single_texture(struct st_context *st,
60 struct pipe_sampler_view **sampler_view,
61 GLuint texUnit, unsigned glsl_version)
62 {
63 struct gl_context *ctx = st->ctx;
64 const struct gl_sampler_object *samp;
65 struct gl_texture_object *texObj;
66 struct st_texture_object *stObj;
67 GLboolean retval;
68
69 samp = _mesa_get_samplerobj(ctx, texUnit);
70
71 texObj = ctx->Texture.Unit[texUnit]._Current;
72 assert(texObj);
73
74 stObj = st_texture_object(texObj);
75
76 retval = st_finalize_texture(ctx, st->pipe, texObj, 0);
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 enum pipe_shader_type shader_stage,
108 const struct gl_program *prog,
109 struct pipe_sampler_view **sampler_views,
110 unsigned *num_textures)
111 {
112 const GLuint old_max = *num_textures;
113 GLbitfield samplers_used = prog->SamplersUsed;
114 GLbitfield free_slots = ~prog->SamplersUsed;
115 GLbitfield external_samplers_used = prog->ExternalSamplersUsed;
116 GLuint unit;
117
118 if (samplers_used == 0x0 && old_max == 0)
119 return;
120
121 *num_textures = 0;
122
123 /* loop over sampler units (aka tex image units) */
124 for (unit = 0; samplers_used || unit < old_max;
125 unit++, samplers_used >>= 1) {
126 struct pipe_sampler_view *sampler_view = NULL;
127
128 if (samplers_used & 1) {
129 /* prog->sh.data is NULL if it's ARB_fragment_program */
130 unsigned glsl_version = prog->sh.data ? prog->sh.data->Version : 0;
131 const GLuint texUnit = prog->SamplerUnits[unit];
132 GLboolean retval;
133
134 retval = st_update_single_texture(st, &sampler_view, texUnit,
135 glsl_version);
136 if (retval == GL_FALSE)
137 continue;
138
139 *num_textures = unit + 1;
140 }
141
142 pipe_sampler_view_reference(&(sampler_views[unit]), sampler_view);
143 }
144
145 /* For any external samplers with multiplaner YUV, stuff the additional
146 * sampler views we need at the end.
147 *
148 * Trying to cache the sampler view in the stObj looks painful, so just
149 * re-create the sampler view for the extra planes each time. Main use
150 * case is video playback (ie. fps games wouldn't be using this) so I
151 * guess no point to try to optimize this feature.
152 */
153 while (unlikely(external_samplers_used)) {
154 GLuint unit = u_bit_scan(&external_samplers_used);
155 GLuint extra = 0;
156 struct st_texture_object *stObj =
157 st_get_texture_object(st->ctx, prog, unit);
158 struct pipe_sampler_view tmpl;
159
160 if (!stObj)
161 continue;
162
163 /* use original view as template: */
164 tmpl = *sampler_views[unit];
165
166 switch (st_get_view_format(stObj)) {
167 case PIPE_FORMAT_NV12:
168 /* we need one additional R8G8 view: */
169 tmpl.format = PIPE_FORMAT_RG88_UNORM;
170 tmpl.swizzle_g = PIPE_SWIZZLE_Y; /* tmpl from Y plane is R8 */
171 extra = u_bit_scan(&free_slots);
172 sampler_views[extra] =
173 st->pipe->create_sampler_view(st->pipe, stObj->pt->next, &tmpl);
174 break;
175 case PIPE_FORMAT_IYUV:
176 /* we need two additional R8 views: */
177 tmpl.format = PIPE_FORMAT_R8_UNORM;
178 extra = u_bit_scan(&free_slots);
179 sampler_views[extra] =
180 st->pipe->create_sampler_view(st->pipe, stObj->pt->next, &tmpl);
181 extra = u_bit_scan(&free_slots);
182 sampler_views[extra] =
183 st->pipe->create_sampler_view(st->pipe, stObj->pt->next->next, &tmpl);
184 break;
185 default:
186 break;
187 }
188
189 *num_textures = MAX2(*num_textures, extra + 1);
190 }
191
192 cso_set_sampler_views(st->cso_context,
193 shader_stage,
194 *num_textures,
195 sampler_views);
196 }
197
198
199
200 void
201 st_update_vertex_textures(struct st_context *st)
202 {
203 const struct gl_context *ctx = st->ctx;
204
205 if (ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits > 0) {
206 update_textures(st,
207 PIPE_SHADER_VERTEX,
208 ctx->VertexProgram._Current,
209 st->state.sampler_views[PIPE_SHADER_VERTEX],
210 &st->state.num_sampler_views[PIPE_SHADER_VERTEX]);
211 }
212 }
213
214
215 void
216 st_update_fragment_textures(struct st_context *st)
217 {
218 const struct gl_context *ctx = st->ctx;
219
220 update_textures(st,
221 PIPE_SHADER_FRAGMENT,
222 ctx->FragmentProgram._Current,
223 st->state.sampler_views[PIPE_SHADER_FRAGMENT],
224 &st->state.num_sampler_views[PIPE_SHADER_FRAGMENT]);
225 }
226
227
228 void
229 st_update_geometry_textures(struct st_context *st)
230 {
231 const struct gl_context *ctx = st->ctx;
232
233 if (ctx->GeometryProgram._Current) {
234 update_textures(st,
235 PIPE_SHADER_GEOMETRY,
236 ctx->GeometryProgram._Current,
237 st->state.sampler_views[PIPE_SHADER_GEOMETRY],
238 &st->state.num_sampler_views[PIPE_SHADER_GEOMETRY]);
239 }
240 }
241
242
243 void
244 st_update_tessctrl_textures(struct st_context *st)
245 {
246 const struct gl_context *ctx = st->ctx;
247
248 if (ctx->TessCtrlProgram._Current) {
249 update_textures(st,
250 PIPE_SHADER_TESS_CTRL,
251 ctx->TessCtrlProgram._Current,
252 st->state.sampler_views[PIPE_SHADER_TESS_CTRL],
253 &st->state.num_sampler_views[PIPE_SHADER_TESS_CTRL]);
254 }
255 }
256
257
258 void
259 st_update_tesseval_textures(struct st_context *st)
260 {
261 const struct gl_context *ctx = st->ctx;
262
263 if (ctx->TessEvalProgram._Current) {
264 update_textures(st,
265 PIPE_SHADER_TESS_EVAL,
266 ctx->TessEvalProgram._Current,
267 st->state.sampler_views[PIPE_SHADER_TESS_EVAL],
268 &st->state.num_sampler_views[PIPE_SHADER_TESS_EVAL]);
269 }
270 }
271
272
273 void
274 st_update_compute_textures(struct st_context *st)
275 {
276 const struct gl_context *ctx = st->ctx;
277
278 if (ctx->ComputeProgram._Current) {
279 update_textures(st,
280 PIPE_SHADER_COMPUTE,
281 ctx->ComputeProgram._Current,
282 st->state.sampler_views[PIPE_SHADER_COMPUTE],
283 &st->state.num_sampler_views[PIPE_SHADER_COMPUTE]);
284 }
285 }