mesa: Refactor bind_framebuffer to make _mesa_bind_framebuffers
[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_texture.h"
46 #include "st_format.h"
47 #include "st_cb_texture.h"
48 #include "pipe/p_context.h"
49 #include "util/u_format.h"
50 #include "util/u_inlines.h"
51 #include "cso_cache/cso_context.h"
52
53
54 /**
55 * Return swizzle1(swizzle2)
56 */
57 static unsigned
58 swizzle_swizzle(unsigned swizzle1, unsigned swizzle2)
59 {
60 unsigned i, swz[4];
61
62 for (i = 0; i < 4; i++) {
63 unsigned s = GET_SWZ(swizzle1, i);
64 switch (s) {
65 case SWIZZLE_X:
66 case SWIZZLE_Y:
67 case SWIZZLE_Z:
68 case SWIZZLE_W:
69 swz[i] = GET_SWZ(swizzle2, s);
70 break;
71 case SWIZZLE_ZERO:
72 swz[i] = SWIZZLE_ZERO;
73 break;
74 case SWIZZLE_ONE:
75 swz[i] = SWIZZLE_ONE;
76 break;
77 default:
78 assert(!"Bad swizzle term");
79 swz[i] = SWIZZLE_X;
80 }
81 }
82
83 return MAKE_SWIZZLE4(swz[0], swz[1], swz[2], swz[3]);
84 }
85
86
87 /**
88 * Given a user-specified texture base format, the actual gallium texture
89 * format and the current GL_DEPTH_MODE, return a texture swizzle.
90 *
91 * Consider the case where the user requests a GL_RGB internal texture
92 * format the driver actually uses an RGBA format. The A component should
93 * be ignored and sampling from the texture should always return (r,g,b,1).
94 * But if we rendered to the texture we might have written A values != 1.
95 * By sampling the texture with a ".xyz1" swizzle we'll get the expected A=1.
96 * This function computes the texture swizzle needed to get the expected
97 * values.
98 *
99 * In the case of depth textures, the GL_DEPTH_MODE state determines the
100 * texture swizzle.
101 *
102 * This result must be composed with the user-specified swizzle to get
103 * the final swizzle.
104 */
105 static unsigned
106 compute_texture_format_swizzle(GLenum baseFormat, GLenum depthMode,
107 enum pipe_format actualFormat,
108 unsigned glsl_version)
109 {
110 switch (baseFormat) {
111 case GL_RGBA:
112 return SWIZZLE_XYZW;
113 case GL_RGB:
114 if (util_format_has_alpha(actualFormat))
115 return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_ONE);
116 else
117 return SWIZZLE_XYZW;
118 case GL_RG:
119 if (util_format_get_nr_components(actualFormat) > 2)
120 return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_ZERO, SWIZZLE_ONE);
121 else
122 return SWIZZLE_XYZW;
123 case GL_RED:
124 if (util_format_get_nr_components(actualFormat) > 1)
125 return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_ZERO,
126 SWIZZLE_ZERO, SWIZZLE_ONE);
127 else
128 return SWIZZLE_XYZW;
129 case GL_ALPHA:
130 if (util_format_get_nr_components(actualFormat) > 1)
131 return MAKE_SWIZZLE4(SWIZZLE_ZERO, SWIZZLE_ZERO,
132 SWIZZLE_ZERO, SWIZZLE_W);
133 else
134 return SWIZZLE_XYZW;
135 case GL_LUMINANCE:
136 if (util_format_get_nr_components(actualFormat) > 1)
137 return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_X, SWIZZLE_X, SWIZZLE_ONE);
138 else
139 return SWIZZLE_XYZW;
140 case GL_LUMINANCE_ALPHA:
141 if (util_format_get_nr_components(actualFormat) > 2)
142 return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_X, SWIZZLE_X, SWIZZLE_W);
143 else
144 return SWIZZLE_XYZW;
145 case GL_INTENSITY:
146 if (util_format_get_nr_components(actualFormat) > 1)
147 return SWIZZLE_XXXX;
148 else
149 return SWIZZLE_XYZW;
150 case GL_STENCIL_INDEX:
151 case GL_DEPTH_STENCIL:
152 case GL_DEPTH_COMPONENT:
153 /* Now examine the depth mode */
154 switch (depthMode) {
155 case GL_LUMINANCE:
156 return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_X, SWIZZLE_X, SWIZZLE_ONE);
157 case GL_INTENSITY:
158 return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_X, SWIZZLE_X, SWIZZLE_X);
159 case GL_ALPHA:
160 /* The texture(sampler*Shadow) functions from GLSL 1.30 ignore
161 * the depth mode and return float, while older shadow* functions
162 * and ARB_fp instructions return vec4 according to the depth mode.
163 *
164 * The problem with the GLSL 1.30 functions is that GL_ALPHA forces
165 * them to return 0, breaking them completely.
166 *
167 * A proper fix would increase code complexity and that's not worth
168 * it for a rarely used feature such as the GL_ALPHA depth mode
169 * in GL3. Therefore, change GL_ALPHA to GL_INTENSITY for all
170 * shaders that use GLSL 1.30 or later.
171 *
172 * BTW, it's required that sampler views are updated when
173 * shaders change (check_sampler_swizzle takes care of that).
174 */
175 if (glsl_version && glsl_version >= 130)
176 return SWIZZLE_XXXX;
177 else
178 return MAKE_SWIZZLE4(SWIZZLE_ZERO, SWIZZLE_ZERO,
179 SWIZZLE_ZERO, SWIZZLE_X);
180 case GL_RED:
181 return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_ZERO,
182 SWIZZLE_ZERO, SWIZZLE_ONE);
183 default:
184 assert(!"Unexpected depthMode");
185 return SWIZZLE_XYZW;
186 }
187 default:
188 assert(!"Unexpected baseFormat");
189 return SWIZZLE_XYZW;
190 }
191 }
192
193
194 static unsigned
195 get_texture_format_swizzle(const struct st_context *st,
196 const struct st_texture_object *stObj,
197 unsigned glsl_version)
198 {
199 GLenum baseFormat = _mesa_texture_base_format(&stObj->base);
200 unsigned tex_swizzle;
201
202 if (baseFormat != GL_NONE) {
203 GLenum depth_mode = stObj->base.DepthMode;
204 /* In ES 3.0, DEPTH_TEXTURE_MODE is expected to be GL_RED for textures
205 * with depth component data specified with a sized internal format.
206 */
207 if (_mesa_is_gles3(st->ctx) &&
208 util_format_is_depth_or_stencil(stObj->pt->format)) {
209 const struct st_texture_image *firstImage =
210 st_texture_image_const(_mesa_base_tex_image(&stObj->base));
211 if (firstImage->base.InternalFormat != GL_DEPTH_COMPONENT &&
212 firstImage->base.InternalFormat != GL_DEPTH_STENCIL &&
213 firstImage->base.InternalFormat != GL_STENCIL_INDEX)
214 depth_mode = GL_RED;
215 }
216 tex_swizzle = compute_texture_format_swizzle(baseFormat,
217 depth_mode,
218 stObj->pt->format,
219 glsl_version);
220 }
221 else {
222 tex_swizzle = SWIZZLE_XYZW;
223 }
224
225 /* Combine the texture format swizzle with user's swizzle */
226 return swizzle_swizzle(stObj->base._Swizzle, tex_swizzle);
227 }
228
229
230 /**
231 * Return TRUE if the texture's sampler view swizzle is not equal to
232 * the texture's swizzle.
233 *
234 * \param stObj the st texture object,
235 */
236 static boolean
237 check_sampler_swizzle(const struct st_context *st,
238 const struct st_texture_object *stObj,
239 struct pipe_sampler_view *sv, unsigned glsl_version)
240 {
241 unsigned swizzle = get_texture_format_swizzle(st, stObj, glsl_version);
242
243 return ((sv->swizzle_r != GET_SWZ(swizzle, 0)) ||
244 (sv->swizzle_g != GET_SWZ(swizzle, 1)) ||
245 (sv->swizzle_b != GET_SWZ(swizzle, 2)) ||
246 (sv->swizzle_a != GET_SWZ(swizzle, 3)));
247 }
248
249
250 static unsigned last_level(struct st_texture_object *stObj)
251 {
252 unsigned ret = MIN2(stObj->base.MinLevel + stObj->base._MaxLevel,
253 stObj->pt->last_level);
254 if (stObj->base.Immutable)
255 ret = MIN2(ret, stObj->base.MinLevel + stObj->base.NumLevels - 1);
256 return ret;
257 }
258
259 static unsigned last_layer(struct st_texture_object *stObj)
260 {
261 if (stObj->base.Immutable && stObj->pt->array_size > 1)
262 return MIN2(stObj->base.MinLayer + stObj->base.NumLayers - 1,
263 stObj->pt->array_size - 1);
264 return stObj->pt->array_size - 1;
265 }
266
267 static struct pipe_sampler_view *
268 st_create_texture_sampler_view_from_stobj(struct st_context *st,
269 struct st_texture_object *stObj,
270 enum pipe_format format,
271 unsigned glsl_version)
272 {
273 struct pipe_sampler_view templ;
274 unsigned swizzle = get_texture_format_swizzle(st, stObj, glsl_version);
275
276 u_sampler_view_default_template(&templ,
277 stObj->pt,
278 format);
279
280 if (stObj->pt->target == PIPE_BUFFER) {
281 unsigned base, size;
282 unsigned f, n;
283 const struct util_format_description *desc
284 = util_format_description(templ.format);
285
286 base = stObj->base.BufferOffset;
287 if (base >= stObj->pt->width0)
288 return NULL;
289 size = MIN2(stObj->pt->width0 - base, (unsigned)stObj->base.BufferSize);
290
291 f = (base / (desc->block.bits / 8)) * desc->block.width;
292 n = (size / (desc->block.bits / 8)) * desc->block.width;
293 if (!n)
294 return NULL;
295 templ.u.buf.first_element = f;
296 templ.u.buf.last_element = f + (n - 1);
297 } else {
298 templ.u.tex.first_level = stObj->base.MinLevel + stObj->base.BaseLevel;
299 templ.u.tex.last_level = last_level(stObj);
300 assert(templ.u.tex.first_level <= templ.u.tex.last_level);
301 templ.u.tex.first_layer = stObj->base.MinLayer;
302 templ.u.tex.last_layer = last_layer(stObj);
303 assert(templ.u.tex.first_layer <= templ.u.tex.last_layer);
304 templ.target = gl_target_to_pipe(stObj->base.Target);
305 }
306
307 if (swizzle != SWIZZLE_NOOP) {
308 templ.swizzle_r = GET_SWZ(swizzle, 0);
309 templ.swizzle_g = GET_SWZ(swizzle, 1);
310 templ.swizzle_b = GET_SWZ(swizzle, 2);
311 templ.swizzle_a = GET_SWZ(swizzle, 3);
312 }
313
314 return st->pipe->create_sampler_view(st->pipe, stObj->pt, &templ);
315 }
316
317
318 static struct pipe_sampler_view *
319 st_get_texture_sampler_view_from_stobj(struct st_context *st,
320 struct st_texture_object *stObj,
321 enum pipe_format format,
322 unsigned glsl_version)
323 {
324 struct pipe_sampler_view **sv;
325 const struct st_texture_image *firstImage;
326 if (!stObj || !stObj->pt) {
327 return NULL;
328 }
329
330 sv = st_texture_get_sampler_view(st, stObj);
331
332 if (util_format_is_depth_and_stencil(format)) {
333 if (stObj->base.StencilSampling)
334 format = util_format_stencil_only(format);
335 else {
336 firstImage = st_texture_image_const(_mesa_base_tex_image(&stObj->base));
337 if (firstImage->base._BaseFormat == GL_STENCIL_INDEX)
338 format = util_format_stencil_only(format);
339 }
340 }
341
342 /* if sampler view has changed dereference it */
343 if (*sv) {
344 if (check_sampler_swizzle(st, stObj, *sv, glsl_version) ||
345 (format != (*sv)->format) ||
346 gl_target_to_pipe(stObj->base.Target) != (*sv)->target ||
347 stObj->base.MinLevel + stObj->base.BaseLevel != (*sv)->u.tex.first_level ||
348 last_level(stObj) != (*sv)->u.tex.last_level ||
349 stObj->base.MinLayer != (*sv)->u.tex.first_layer ||
350 last_layer(stObj) != (*sv)->u.tex.last_layer) {
351 pipe_sampler_view_reference(sv, NULL);
352 }
353 }
354
355 if (!*sv) {
356 *sv = st_create_texture_sampler_view_from_stobj(st, stObj,
357 format, glsl_version);
358
359 } else if ((*sv)->context != st->pipe) {
360 /* Recreate view in correct context, use existing view as template */
361 struct pipe_sampler_view *new_sv =
362 st->pipe->create_sampler_view(st->pipe, stObj->pt, *sv);
363 pipe_sampler_view_reference(sv, NULL);
364 *sv = new_sv;
365 }
366
367 return *sv;
368 }
369
370 static GLboolean
371 update_single_texture(struct st_context *st,
372 struct pipe_sampler_view **sampler_view,
373 GLuint texUnit, unsigned glsl_version)
374 {
375 struct gl_context *ctx = st->ctx;
376 const struct gl_sampler_object *samp;
377 struct gl_texture_object *texObj;
378 struct st_texture_object *stObj;
379 enum pipe_format view_format;
380 GLboolean retval;
381
382 samp = _mesa_get_samplerobj(ctx, texUnit);
383
384 texObj = ctx->Texture.Unit[texUnit]._Current;
385
386 if (!texObj) {
387 texObj = _mesa_get_fallback_texture(ctx, TEXTURE_2D_INDEX);
388 samp = &texObj->Sampler;
389 }
390 stObj = st_texture_object(texObj);
391
392 retval = st_finalize_texture(ctx, st->pipe, texObj);
393 if (!retval) {
394 /* out of mem */
395 return GL_FALSE;
396 }
397
398 /* Determine the format of the texture sampler view */
399 if (texObj->Target == GL_TEXTURE_BUFFER) {
400 view_format =
401 st_mesa_format_to_pipe_format(st, stObj->base._BufferObjectFormat);
402 }
403 else {
404 view_format =
405 stObj->surface_based ? stObj->surface_format : stObj->pt->format;
406
407 /* If sRGB decoding is off, use the linear format */
408 if (samp->sRGBDecode == GL_SKIP_DECODE_EXT) {
409 view_format = util_format_linear(view_format);
410 }
411 }
412
413 *sampler_view =
414 st_get_texture_sampler_view_from_stobj(st, stObj, view_format,
415 glsl_version);
416 return GL_TRUE;
417 }
418
419
420
421 static void
422 update_textures(struct st_context *st,
423 gl_shader_stage mesa_shader,
424 const struct gl_program *prog,
425 unsigned max_units,
426 struct pipe_sampler_view **sampler_views,
427 unsigned *num_textures)
428 {
429 const GLuint old_max = *num_textures;
430 GLbitfield samplers_used = prog->SamplersUsed;
431 GLuint unit;
432 struct gl_shader_program *shader =
433 st->ctx->_Shader->CurrentProgram[mesa_shader];
434 unsigned glsl_version = shader ? shader->Version : 0;
435 unsigned shader_stage = st_shader_stage_to_ptarget(mesa_shader);
436
437 if (samplers_used == 0x0 && old_max == 0)
438 return;
439
440 *num_textures = 0;
441
442 /* loop over sampler units (aka tex image units) */
443 for (unit = 0; unit < max_units; unit++, samplers_used >>= 1) {
444 struct pipe_sampler_view *sampler_view = NULL;
445
446 if (samplers_used & 1) {
447 const GLuint texUnit = prog->SamplerUnits[unit];
448 GLboolean retval;
449
450 retval = update_single_texture(st, &sampler_view, texUnit,
451 glsl_version);
452 if (retval == GL_FALSE)
453 continue;
454
455 *num_textures = unit + 1;
456 }
457 else if (samplers_used == 0 && unit >= old_max) {
458 /* if we've reset all the old views and we have no more new ones */
459 break;
460 }
461
462 pipe_sampler_view_reference(&(sampler_views[unit]), sampler_view);
463 }
464
465 cso_set_sampler_views(st->cso_context,
466 shader_stage,
467 *num_textures,
468 sampler_views);
469 }
470
471
472
473 static void
474 update_vertex_textures(struct st_context *st)
475 {
476 const struct gl_context *ctx = st->ctx;
477
478 if (ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits > 0) {
479 update_textures(st,
480 MESA_SHADER_VERTEX,
481 &ctx->VertexProgram._Current->Base,
482 ctx->Const.Program[MESA_SHADER_VERTEX].MaxTextureImageUnits,
483 st->state.sampler_views[PIPE_SHADER_VERTEX],
484 &st->state.num_sampler_views[PIPE_SHADER_VERTEX]);
485 }
486 }
487
488
489 static void
490 update_fragment_textures(struct st_context *st)
491 {
492 const struct gl_context *ctx = st->ctx;
493
494 update_textures(st,
495 MESA_SHADER_FRAGMENT,
496 &ctx->FragmentProgram._Current->Base,
497 ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits,
498 st->state.sampler_views[PIPE_SHADER_FRAGMENT],
499 &st->state.num_sampler_views[PIPE_SHADER_FRAGMENT]);
500 }
501
502
503 static void
504 update_geometry_textures(struct st_context *st)
505 {
506 const struct gl_context *ctx = st->ctx;
507
508 if (ctx->GeometryProgram._Current) {
509 update_textures(st,
510 MESA_SHADER_GEOMETRY,
511 &ctx->GeometryProgram._Current->Base,
512 ctx->Const.Program[MESA_SHADER_GEOMETRY].MaxTextureImageUnits,
513 st->state.sampler_views[PIPE_SHADER_GEOMETRY],
514 &st->state.num_sampler_views[PIPE_SHADER_GEOMETRY]);
515 }
516 }
517
518
519 static void
520 update_tessctrl_textures(struct st_context *st)
521 {
522 const struct gl_context *ctx = st->ctx;
523
524 if (ctx->TessCtrlProgram._Current) {
525 update_textures(st,
526 MESA_SHADER_TESS_CTRL,
527 &ctx->TessCtrlProgram._Current->Base,
528 ctx->Const.Program[MESA_SHADER_TESS_CTRL].MaxTextureImageUnits,
529 st->state.sampler_views[PIPE_SHADER_TESS_CTRL],
530 &st->state.num_sampler_views[PIPE_SHADER_TESS_CTRL]);
531 }
532 }
533
534
535 static void
536 update_tesseval_textures(struct st_context *st)
537 {
538 const struct gl_context *ctx = st->ctx;
539
540 if (ctx->TessEvalProgram._Current) {
541 update_textures(st,
542 MESA_SHADER_TESS_EVAL,
543 &ctx->TessEvalProgram._Current->Base,
544 ctx->Const.Program[MESA_SHADER_TESS_EVAL].MaxTextureImageUnits,
545 st->state.sampler_views[PIPE_SHADER_TESS_EVAL],
546 &st->state.num_sampler_views[PIPE_SHADER_TESS_EVAL]);
547 }
548 }
549
550
551 static void
552 update_compute_textures(struct st_context *st)
553 {
554 const struct gl_context *ctx = st->ctx;
555
556 if (ctx->ComputeProgram._Current) {
557 update_textures(st,
558 MESA_SHADER_COMPUTE,
559 &ctx->ComputeProgram._Current->Base,
560 ctx->Const.Program[MESA_SHADER_COMPUTE].MaxTextureImageUnits,
561 st->state.sampler_views[PIPE_SHADER_COMPUTE],
562 &st->state.num_sampler_views[PIPE_SHADER_COMPUTE]);
563 }
564 }
565
566
567 const struct st_tracked_state st_update_fragment_texture = {
568 "st_update_texture", /* name */
569 { /* dirty */
570 _NEW_TEXTURE, /* mesa */
571 ST_NEW_FRAGMENT_PROGRAM | ST_NEW_SAMPLER_VIEWS, /* st */
572 },
573 update_fragment_textures /* update */
574 };
575
576
577 const struct st_tracked_state st_update_vertex_texture = {
578 "st_update_vertex_texture", /* name */
579 { /* dirty */
580 _NEW_TEXTURE, /* mesa */
581 ST_NEW_VERTEX_PROGRAM | ST_NEW_SAMPLER_VIEWS, /* st */
582 },
583 update_vertex_textures /* update */
584 };
585
586
587 const struct st_tracked_state st_update_geometry_texture = {
588 "st_update_geometry_texture", /* name */
589 { /* dirty */
590 _NEW_TEXTURE, /* mesa */
591 ST_NEW_GEOMETRY_PROGRAM | ST_NEW_SAMPLER_VIEWS, /* st */
592 },
593 update_geometry_textures /* update */
594 };
595
596
597 const struct st_tracked_state st_update_tessctrl_texture = {
598 "st_update_tessctrl_texture", /* name */
599 { /* dirty */
600 _NEW_TEXTURE, /* mesa */
601 ST_NEW_TESSCTRL_PROGRAM | ST_NEW_SAMPLER_VIEWS, /* st */
602 },
603 update_tessctrl_textures /* update */
604 };
605
606
607 const struct st_tracked_state st_update_tesseval_texture = {
608 "st_update_tesseval_texture", /* name */
609 { /* dirty */
610 _NEW_TEXTURE, /* mesa */
611 ST_NEW_TESSEVAL_PROGRAM | ST_NEW_SAMPLER_VIEWS, /* st */
612 },
613 update_tesseval_textures /* update */
614 };
615
616
617 const struct st_tracked_state st_update_compute_texture = {
618 "st_update_compute_texture", /* name */
619 { /* dirty */
620 _NEW_TEXTURE, /* mesa */
621 ST_NEW_COMPUTE_PROGRAM | ST_NEW_SAMPLER_VIEWS, /* st */
622 },
623 update_compute_textures /* update */
624 };