mesa/i965/i915/r200: eliminate gl_vertex_program
[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 *sampler_view =
95 st_get_texture_sampler_view_from_stobj(st, stObj, samp, glsl_version);
96 return GL_TRUE;
97 }
98
99
100
101 static void
102 update_textures(struct st_context *st,
103 gl_shader_stage mesa_shader,
104 const struct gl_program *prog,
105 unsigned max_units,
106 struct pipe_sampler_view **sampler_views,
107 unsigned *num_textures)
108 {
109 const GLuint old_max = *num_textures;
110 GLbitfield samplers_used = prog->SamplersUsed;
111 GLbitfield free_slots = ~prog->SamplersUsed;
112 GLbitfield external_samplers_used = prog->ExternalSamplersUsed;
113 GLuint unit;
114 struct gl_shader_program *shader =
115 st->ctx->_Shader->CurrentProgram[mesa_shader];
116 unsigned glsl_version = shader ? shader->Version : 0;
117 enum pipe_shader_type shader_stage = st_shader_stage_to_ptarget(mesa_shader);
118
119 if (samplers_used == 0x0 && old_max == 0)
120 return;
121
122 *num_textures = 0;
123
124 /* loop over sampler units (aka tex image units) */
125 for (unit = 0; unit < max_units; unit++, samplers_used >>= 1) {
126 struct pipe_sampler_view *sampler_view = NULL;
127
128 if (samplers_used & 1) {
129 const GLuint texUnit = prog->SamplerUnits[unit];
130 GLboolean retval;
131
132 retval = update_single_texture(st, &sampler_view, texUnit,
133 glsl_version);
134 if (retval == GL_FALSE)
135 continue;
136
137 *num_textures = unit + 1;
138 }
139 else if (samplers_used == 0 && unit >= old_max) {
140 /* if we've reset all the old views and we have no more new ones */
141 break;
142 }
143
144 pipe_sampler_view_reference(&(sampler_views[unit]), sampler_view);
145 }
146
147 /* For any external samplers with multiplaner YUV, stuff the additional
148 * sampler views we need at the end.
149 *
150 * Trying to cache the sampler view in the stObj looks painful, so just
151 * re-create the sampler view for the extra planes each time. Main use
152 * case is video playback (ie. fps games wouldn't be using this) so I
153 * guess no point to try to optimize this feature.
154 */
155 while (unlikely(external_samplers_used)) {
156 GLuint unit = u_bit_scan(&external_samplers_used);
157 GLuint extra = 0;
158 struct st_texture_object *stObj =
159 st_get_texture_object(st->ctx, prog, unit);
160 struct pipe_sampler_view tmpl;
161
162 if (!stObj)
163 continue;
164
165 /* use original view as template: */
166 tmpl = *sampler_views[unit];
167
168 switch (st_get_view_format(stObj)) {
169 case PIPE_FORMAT_NV12:
170 /* we need one additional R8G8 view: */
171 tmpl.format = PIPE_FORMAT_RG88_UNORM;
172 tmpl.swizzle_g = PIPE_SWIZZLE_Y; /* tmpl from Y plane is R8 */
173 extra = u_bit_scan(&free_slots);
174 sampler_views[extra] =
175 st->pipe->create_sampler_view(st->pipe, stObj->pt->next, &tmpl);
176 break;
177 case PIPE_FORMAT_IYUV:
178 /* we need two additional R8 views: */
179 tmpl.format = PIPE_FORMAT_R8_UNORM;
180 extra = u_bit_scan(&free_slots);
181 sampler_views[extra] =
182 st->pipe->create_sampler_view(st->pipe, stObj->pt->next, &tmpl);
183 extra = u_bit_scan(&free_slots);
184 sampler_views[extra] =
185 st->pipe->create_sampler_view(st->pipe, stObj->pt->next->next, &tmpl);
186 break;
187 default:
188 break;
189 }
190
191 *num_textures = MAX2(*num_textures, extra + 1);
192 }
193
194 cso_set_sampler_views(st->cso_context,
195 shader_stage,
196 *num_textures,
197 sampler_views);
198 }
199
200
201
202 static void
203 update_vertex_textures(struct st_context *st)
204 {
205 const struct gl_context *ctx = st->ctx;
206
207 if (ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits > 0) {
208 update_textures(st,
209 MESA_SHADER_VERTEX,
210 ctx->VertexProgram._Current,
211 ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits,
212 st->state.sampler_views[PIPE_SHADER_VERTEX],
213 &st->state.num_sampler_views[PIPE_SHADER_VERTEX]);
214 }
215 }
216
217
218 static void
219 update_fragment_textures(struct st_context *st)
220 {
221 const struct gl_context *ctx = st->ctx;
222
223 update_textures(st,
224 MESA_SHADER_FRAGMENT,
225 &ctx->FragmentProgram._Current->Base,
226 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits,
227 st->state.sampler_views[PIPE_SHADER_FRAGMENT],
228 &st->state.num_sampler_views[PIPE_SHADER_FRAGMENT]);
229 }
230
231
232 static void
233 update_geometry_textures(struct st_context *st)
234 {
235 const struct gl_context *ctx = st->ctx;
236
237 if (ctx->GeometryProgram._Current) {
238 update_textures(st,
239 MESA_SHADER_GEOMETRY,
240 ctx->GeometryProgram._Current,
241 ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxTextureImageUnits,
242 st->state.sampler_views[PIPE_SHADER_GEOMETRY],
243 &st->state.num_sampler_views[PIPE_SHADER_GEOMETRY]);
244 }
245 }
246
247
248 static void
249 update_tessctrl_textures(struct st_context *st)
250 {
251 const struct gl_context *ctx = st->ctx;
252
253 if (ctx->TessCtrlProgram._Current) {
254 update_textures(st,
255 MESA_SHADER_TESS_CTRL,
256 ctx->TessCtrlProgram._Current,
257 ctx->Const.Program[MESA_SHADER_TESS_CTRL].MaxTextureImageUnits,
258 st->state.sampler_views[PIPE_SHADER_TESS_CTRL],
259 &st->state.num_sampler_views[PIPE_SHADER_TESS_CTRL]);
260 }
261 }
262
263
264 static void
265 update_tesseval_textures(struct st_context *st)
266 {
267 const struct gl_context *ctx = st->ctx;
268
269 if (ctx->TessEvalProgram._Current) {
270 update_textures(st,
271 MESA_SHADER_TESS_EVAL,
272 ctx->TessEvalProgram._Current,
273 ctx->Const.Program[MESA_SHADER_TESS_EVAL].MaxTextureImageUnits,
274 st->state.sampler_views[PIPE_SHADER_TESS_EVAL],
275 &st->state.num_sampler_views[PIPE_SHADER_TESS_EVAL]);
276 }
277 }
278
279
280 static void
281 update_compute_textures(struct st_context *st)
282 {
283 const struct gl_context *ctx = st->ctx;
284
285 if (ctx->ComputeProgram._Current) {
286 update_textures(st,
287 MESA_SHADER_COMPUTE,
288 &ctx->ComputeProgram._Current->Base,
289 ctx->Const.Program[MESA_SHADER_COMPUTE].MaxTextureImageUnits,
290 st->state.sampler_views[PIPE_SHADER_COMPUTE],
291 &st->state.num_sampler_views[PIPE_SHADER_COMPUTE]);
292 }
293 }
294
295
296 const struct st_tracked_state st_update_fragment_texture = {
297 update_fragment_textures /* update */
298 };
299
300
301 const struct st_tracked_state st_update_vertex_texture = {
302 update_vertex_textures /* update */
303 };
304
305
306 const struct st_tracked_state st_update_geometry_texture = {
307 update_geometry_textures /* update */
308 };
309
310
311 const struct st_tracked_state st_update_tessctrl_texture = {
312 update_tessctrl_textures /* update */
313 };
314
315
316 const struct st_tracked_state st_update_tesseval_texture = {
317 update_tesseval_textures /* update */
318 };
319
320
321 const struct st_tracked_state st_update_compute_texture = {
322 update_compute_textures /* update */
323 };