st/mesa: add/improve sampler view comments
[mesa.git] / src / mesa / state_tracker / st_sampler_view.c
1 /*
2 * Copyright 2016 VMware, Inc.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
15 * of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
20 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 #include "pipe/p_context.h"
27 #include "util/u_format.h"
28 #include "util/u_inlines.h"
29
30 #include "main/context.h"
31 #include "main/macros.h"
32 #include "main/mtypes.h"
33 #include "main/teximage.h"
34 #include "main/texobj.h"
35 #include "program/prog_instruction.h"
36
37 #include "st_context.h"
38 #include "st_sampler_view.h"
39 #include "st_texture.h"
40 #include "st_format.h"
41 #include "st_cb_bufferobjects.h"
42 #include "st_cb_texture.h"
43
44
45 /**
46 * Set the given view as the current context's view for the texture.
47 *
48 * Overwrites any pre-existing view of the context.
49 *
50 * Takes ownership of the view (i.e., stores the view without incrementing the
51 * reference count).
52 *
53 * \return the view, or NULL on error. In case of error, the reference to the
54 * view is released.
55 */
56 static struct pipe_sampler_view *
57 st_texture_set_sampler_view(struct st_context *st,
58 struct st_texture_object *stObj,
59 struct pipe_sampler_view *view,
60 bool glsl130_or_later, bool srgb_skip_decode)
61 {
62 struct st_sampler_views *views;
63 struct st_sampler_view *free = NULL;
64 struct st_sampler_view *sv;
65 GLuint i;
66
67 simple_mtx_lock(&stObj->validate_mutex);
68 views = stObj->sampler_views;
69
70 for (i = 0; i < views->count; ++i) {
71 sv = &views->views[i];
72
73 /* Is the array entry used ? */
74 if (sv->view) {
75 /* check if the context matches */
76 if (sv->view->context == st->pipe) {
77 pipe_sampler_view_release(st->pipe, &sv->view);
78 goto found;
79 }
80 } else {
81 /* Found a free slot, remember that */
82 free = sv;
83 }
84 }
85
86 /* Couldn't find a slot for our context, create a new one */
87 if (free) {
88 sv = free;
89 } else {
90 if (views->count >= views->max) {
91 /* Allocate a larger container. */
92 unsigned new_max = 2 * views->max;
93 unsigned new_size = sizeof(*views) + new_max * sizeof(views->views[0]);
94
95 if (new_max < views->max ||
96 new_max > (UINT_MAX - sizeof(*views)) / sizeof(views->views[0])) {
97 pipe_sampler_view_release(st->pipe, &view);
98 goto out;
99 }
100
101 struct st_sampler_views *new_views = malloc(new_size);
102 if (!new_views) {
103 pipe_sampler_view_release(st->pipe, &view);
104 goto out;
105 }
106
107 new_views->count = views->count;
108 new_views->max = new_max;
109 memcpy(&new_views->views[0], &views->views[0],
110 views->count * sizeof(views->views[0]));
111
112 /* Initialize the pipe_sampler_view pointers to zero so that we don't
113 * have to worry about racing against readers when incrementing
114 * views->count.
115 */
116 memset(&new_views->views[views->count], 0,
117 (new_max - views->count) * sizeof(views->views[0]));
118
119 /* Use memory release semantics to ensure that concurrent readers will
120 * get the correct contents of the new container.
121 *
122 * Also, the write should be atomic, but that's guaranteed anyway on
123 * all supported platforms.
124 */
125 p_atomic_set(&stObj->sampler_views, new_views);
126
127 /* We keep the old container around until the texture object is
128 * deleted, because another thread may still be reading from it. We
129 * double the size of the container each time, so we end up with
130 * at most twice the total memory allocation.
131 */
132 views->next = stObj->sampler_views_old;
133 stObj->sampler_views_old = views;
134
135 views = new_views;
136 }
137
138 sv = &views->views[views->count];
139
140 /* Since modification is guarded by the lock, only the write part of the
141 * increment has to be atomic, and that's already guaranteed on all
142 * supported platforms without using an atomic intrinsic.
143 */
144 views->count++;
145 }
146
147 found:
148 assert(sv->view == NULL);
149
150 sv->glsl130_or_later = glsl130_or_later;
151 sv->srgb_skip_decode = srgb_skip_decode;
152 sv->view = view;
153
154 out:
155 simple_mtx_unlock(&stObj->validate_mutex);
156 return view;
157 }
158
159
160 /**
161 * Return the most-recently validated sampler view for the texture \p stObj
162 * in the given context, if any.
163 *
164 * Performs no additional validation.
165 */
166 const struct st_sampler_view *
167 st_texture_get_current_sampler_view(const struct st_context *st,
168 const struct st_texture_object *stObj)
169 {
170 const struct st_sampler_views *views = p_atomic_read(&stObj->sampler_views);
171
172 for (unsigned i = 0; i < views->count; ++i) {
173 const struct st_sampler_view *sv = &views->views[i];
174 if (sv->view && sv->view->context == st->pipe)
175 return sv;
176 }
177
178 return NULL;
179 }
180
181
182 /**
183 * For the given texture object, release any sampler views which belong
184 * to the calling context. This is used to free any sampler views
185 * which belong to the context before the context is destroyed.
186 */
187 void
188 st_texture_release_sampler_view(struct st_context *st,
189 struct st_texture_object *stObj)
190 {
191 GLuint i;
192
193 simple_mtx_lock(&stObj->validate_mutex);
194 struct st_sampler_views *views = stObj->sampler_views;
195 for (i = 0; i < views->count; ++i) {
196 struct pipe_sampler_view **sv = &views->views[i].view;
197
198 if (*sv && (*sv)->context == st->pipe) {
199 pipe_sampler_view_reference(sv, NULL);
200 break;
201 }
202 }
203 simple_mtx_unlock(&stObj->validate_mutex);
204 }
205
206
207 /**
208 * Release all sampler views attached to the given texture object, regardless
209 * of the context. This is called fairly frequently. For example, whenever
210 * the texture's base level, max level or swizzle change.
211 */
212 void
213 st_texture_release_all_sampler_views(struct st_context *st,
214 struct st_texture_object *stObj)
215 {
216 GLuint i;
217
218 /* TODO: This happens while a texture is deleted, because the Driver API
219 * is asymmetric: the driver allocates the texture object memory, but
220 * mesa/main frees it.
221 */
222 if (!stObj->sampler_views)
223 return;
224
225 simple_mtx_lock(&stObj->validate_mutex);
226 struct st_sampler_views *views = stObj->sampler_views;
227 for (i = 0; i < views->count; ++i)
228 pipe_sampler_view_release(st->pipe, &views->views[i].view);
229 simple_mtx_unlock(&stObj->validate_mutex);
230 }
231
232
233 /*
234 * Free the texture's st_sampler_views objects. This should be called
235 * after st_texture_release_all_sampler_views().
236 */
237 void
238 st_texture_free_sampler_views(struct st_texture_object *stObj)
239 {
240 free(stObj->sampler_views);
241 stObj->sampler_views = NULL;
242
243 while (stObj->sampler_views_old) {
244 struct st_sampler_views *views = stObj->sampler_views_old;
245 stObj->sampler_views_old = views->next;
246 free(views);
247 }
248 }
249
250
251 /**
252 * Return swizzle1(swizzle2)
253 */
254 static unsigned
255 swizzle_swizzle(unsigned swizzle1, unsigned swizzle2)
256 {
257 unsigned i, swz[4];
258
259 if (swizzle1 == SWIZZLE_XYZW) {
260 /* identity swizzle, no change to swizzle2 */
261 return swizzle2;
262 }
263
264 for (i = 0; i < 4; i++) {
265 unsigned s = GET_SWZ(swizzle1, i);
266 switch (s) {
267 case SWIZZLE_X:
268 case SWIZZLE_Y:
269 case SWIZZLE_Z:
270 case SWIZZLE_W:
271 swz[i] = GET_SWZ(swizzle2, s);
272 break;
273 case SWIZZLE_ZERO:
274 swz[i] = SWIZZLE_ZERO;
275 break;
276 case SWIZZLE_ONE:
277 swz[i] = SWIZZLE_ONE;
278 break;
279 default:
280 assert(!"Bad swizzle term");
281 swz[i] = SWIZZLE_X;
282 }
283 }
284
285 return MAKE_SWIZZLE4(swz[0], swz[1], swz[2], swz[3]);
286 }
287
288
289 /**
290 * Given a user-specified texture base format, the actual gallium texture
291 * format and the current GL_DEPTH_MODE, return a texture swizzle.
292 *
293 * Consider the case where the user requests a GL_RGB internal texture
294 * format the driver actually uses an RGBA format. The A component should
295 * be ignored and sampling from the texture should always return (r,g,b,1).
296 * But if we rendered to the texture we might have written A values != 1.
297 * By sampling the texture with a ".xyz1" swizzle we'll get the expected A=1.
298 * This function computes the texture swizzle needed to get the expected
299 * values.
300 *
301 * In the case of depth textures, the GL_DEPTH_MODE state determines the
302 * texture swizzle.
303 *
304 * This result must be composed with the user-specified swizzle to get
305 * the final swizzle.
306 */
307 static unsigned
308 compute_texture_format_swizzle(GLenum baseFormat, GLenum depthMode,
309 bool glsl130_or_later)
310 {
311 switch (baseFormat) {
312 case GL_RGBA:
313 return SWIZZLE_XYZW;
314 case GL_RGB:
315 return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_ONE);
316 case GL_RG:
317 return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_ZERO, SWIZZLE_ONE);
318 case GL_RED:
319 return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_ZERO,
320 SWIZZLE_ZERO, SWIZZLE_ONE);
321 case GL_ALPHA:
322 return MAKE_SWIZZLE4(SWIZZLE_ZERO, SWIZZLE_ZERO,
323 SWIZZLE_ZERO, SWIZZLE_W);
324 case GL_LUMINANCE:
325 return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_X, SWIZZLE_X, SWIZZLE_ONE);
326 case GL_LUMINANCE_ALPHA:
327 return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_X, SWIZZLE_X, SWIZZLE_W);
328 case GL_INTENSITY:
329 return SWIZZLE_XXXX;
330 case GL_STENCIL_INDEX:
331 case GL_DEPTH_STENCIL:
332 case GL_DEPTH_COMPONENT:
333 /* Now examine the depth mode */
334 switch (depthMode) {
335 case GL_LUMINANCE:
336 return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_X, SWIZZLE_X, SWIZZLE_ONE);
337 case GL_INTENSITY:
338 return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_X, SWIZZLE_X, SWIZZLE_X);
339 case GL_ALPHA:
340 /* The texture(sampler*Shadow) functions from GLSL 1.30 ignore
341 * the depth mode and return float, while older shadow* functions
342 * and ARB_fp instructions return vec4 according to the depth mode.
343 *
344 * The problem with the GLSL 1.30 functions is that GL_ALPHA forces
345 * them to return 0, breaking them completely.
346 *
347 * A proper fix would increase code complexity and that's not worth
348 * it for a rarely used feature such as the GL_ALPHA depth mode
349 * in GL3. Therefore, change GL_ALPHA to GL_INTENSITY for all
350 * shaders that use GLSL 1.30 or later.
351 *
352 * BTW, it's required that sampler views are updated when
353 * shaders change (check_sampler_swizzle takes care of that).
354 */
355 if (glsl130_or_later)
356 return SWIZZLE_XXXX;
357 else
358 return MAKE_SWIZZLE4(SWIZZLE_ZERO, SWIZZLE_ZERO,
359 SWIZZLE_ZERO, SWIZZLE_X);
360 case GL_RED:
361 return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_ZERO,
362 SWIZZLE_ZERO, SWIZZLE_ONE);
363 default:
364 assert(!"Unexpected depthMode");
365 return SWIZZLE_XYZW;
366 }
367 default:
368 assert(!"Unexpected baseFormat");
369 return SWIZZLE_XYZW;
370 }
371 }
372
373
374 static unsigned
375 get_texture_format_swizzle(const struct st_context *st,
376 const struct st_texture_object *stObj,
377 bool glsl130_or_later)
378 {
379 GLenum baseFormat = _mesa_base_tex_image(&stObj->base)->_BaseFormat;
380 unsigned tex_swizzle;
381 GLenum depth_mode = stObj->base.DepthMode;
382
383 /* In ES 3.0, DEPTH_TEXTURE_MODE is expected to be GL_RED for textures
384 * with depth component data specified with a sized internal format.
385 */
386 if (_mesa_is_gles3(st->ctx) &&
387 (baseFormat == GL_DEPTH_COMPONENT ||
388 baseFormat == GL_DEPTH_STENCIL ||
389 baseFormat == GL_STENCIL_INDEX)) {
390 const struct gl_texture_image *firstImage =
391 _mesa_base_tex_image(&stObj->base);
392 if (firstImage->InternalFormat != GL_DEPTH_COMPONENT &&
393 firstImage->InternalFormat != GL_DEPTH_STENCIL &&
394 firstImage->InternalFormat != GL_STENCIL_INDEX)
395 depth_mode = GL_RED;
396 }
397 tex_swizzle = compute_texture_format_swizzle(baseFormat,
398 depth_mode,
399 glsl130_or_later);
400
401 /* Combine the texture format swizzle with user's swizzle */
402 return swizzle_swizzle(stObj->base._Swizzle, tex_swizzle);
403 }
404
405
406 /**
407 * Return TRUE if the texture's sampler view swizzle is not equal to
408 * the texture's swizzle.
409 *
410 * \param stObj the st texture object,
411 */
412 MAYBE_UNUSED static boolean
413 check_sampler_swizzle(const struct st_context *st,
414 const struct st_texture_object *stObj,
415 const struct pipe_sampler_view *sv,
416 bool glsl130_or_later)
417 {
418 unsigned swizzle = get_texture_format_swizzle(st, stObj, glsl130_or_later);
419
420 return ((sv->swizzle_r != GET_SWZ(swizzle, 0)) ||
421 (sv->swizzle_g != GET_SWZ(swizzle, 1)) ||
422 (sv->swizzle_b != GET_SWZ(swizzle, 2)) ||
423 (sv->swizzle_a != GET_SWZ(swizzle, 3)));
424 }
425
426
427 static unsigned
428 last_level(const struct st_texture_object *stObj)
429 {
430 unsigned ret = MIN2(stObj->base.MinLevel + stObj->base._MaxLevel,
431 stObj->pt->last_level);
432 if (stObj->base.Immutable)
433 ret = MIN2(ret, stObj->base.MinLevel + stObj->base.NumLevels - 1);
434 return ret;
435 }
436
437
438 static unsigned
439 last_layer(const struct st_texture_object *stObj)
440 {
441 if (stObj->base.Immutable && stObj->pt->array_size > 1)
442 return MIN2(stObj->base.MinLayer + stObj->base.NumLayers - 1,
443 stObj->pt->array_size - 1);
444 return stObj->pt->array_size - 1;
445 }
446
447
448 /**
449 * Determine the format for the texture sampler view.
450 */
451 static enum pipe_format
452 get_sampler_view_format(struct st_context *st,
453 const struct st_texture_object *stObj,
454 bool srgb_skip_decode)
455 {
456 enum pipe_format format;
457
458 GLenum baseFormat = _mesa_base_tex_image(&stObj->base)->_BaseFormat;
459 format = stObj->surface_based ? stObj->surface_format : stObj->pt->format;
460
461 if (baseFormat == GL_DEPTH_COMPONENT ||
462 baseFormat == GL_DEPTH_STENCIL ||
463 baseFormat == GL_STENCIL_INDEX) {
464 if (stObj->base.StencilSampling || baseFormat == GL_STENCIL_INDEX)
465 format = util_format_stencil_only(format);
466
467 return format;
468 }
469
470 /* If sRGB decoding is off, use the linear format */
471 if (srgb_skip_decode)
472 format = util_format_linear(format);
473
474 /* Use R8_UNORM for video formats */
475 switch (format) {
476 case PIPE_FORMAT_NV12:
477 case PIPE_FORMAT_IYUV:
478 format = PIPE_FORMAT_R8_UNORM;
479 break;
480 default:
481 break;
482 }
483 return format;
484 }
485
486
487 static struct pipe_sampler_view *
488 st_create_texture_sampler_view_from_stobj(struct st_context *st,
489 struct st_texture_object *stObj,
490 enum pipe_format format,
491 bool glsl130_or_later)
492 {
493 /* There is no need to clear this structure (consider CPU overhead). */
494 struct pipe_sampler_view templ;
495 unsigned swizzle = get_texture_format_swizzle(st, stObj, glsl130_or_later);
496
497 templ.format = format;
498
499 if (stObj->level_override) {
500 templ.u.tex.first_level = templ.u.tex.last_level = stObj->level_override;
501 } else {
502 templ.u.tex.first_level = stObj->base.MinLevel + stObj->base.BaseLevel;
503 templ.u.tex.last_level = last_level(stObj);
504 }
505 if (stObj->layer_override) {
506 templ.u.tex.first_layer = templ.u.tex.last_layer = stObj->layer_override;
507 } else {
508 templ.u.tex.first_layer = stObj->base.MinLayer;
509 templ.u.tex.last_layer = last_layer(stObj);
510 }
511 assert(templ.u.tex.first_layer <= templ.u.tex.last_layer);
512 assert(templ.u.tex.first_level <= templ.u.tex.last_level);
513 templ.target = gl_target_to_pipe(stObj->base.Target);
514
515 templ.swizzle_r = GET_SWZ(swizzle, 0);
516 templ.swizzle_g = GET_SWZ(swizzle, 1);
517 templ.swizzle_b = GET_SWZ(swizzle, 2);
518 templ.swizzle_a = GET_SWZ(swizzle, 3);
519
520 return st->pipe->create_sampler_view(st->pipe, stObj->pt, &templ);
521 }
522
523
524 struct pipe_sampler_view *
525 st_get_texture_sampler_view_from_stobj(struct st_context *st,
526 struct st_texture_object *stObj,
527 const struct gl_sampler_object *samp,
528 bool glsl130_or_later,
529 bool ignore_srgb_decode)
530 {
531 const struct st_sampler_view *sv;
532 bool srgb_skip_decode = false;
533
534 if (!ignore_srgb_decode && samp->sRGBDecode == GL_SKIP_DECODE_EXT)
535 srgb_skip_decode = true;
536
537 sv = st_texture_get_current_sampler_view(st, stObj);
538
539 if (sv &&
540 sv->glsl130_or_later == glsl130_or_later &&
541 sv->srgb_skip_decode == srgb_skip_decode) {
542 /* Debug check: make sure that the sampler view's parameters are
543 * what they're supposed to be.
544 */
545 struct pipe_sampler_view *view = sv->view;
546 assert(stObj->pt == view->texture);
547 assert(!check_sampler_swizzle(st, stObj, view, glsl130_or_later));
548 assert(get_sampler_view_format(st, stObj, srgb_skip_decode) == view->format);
549 assert(gl_target_to_pipe(stObj->base.Target) == view->target);
550 assert(stObj->level_override ||
551 stObj->base.MinLevel + stObj->base.BaseLevel == view->u.tex.first_level);
552 assert(stObj->level_override || last_level(stObj) == view->u.tex.last_level);
553 assert(stObj->layer_override || stObj->base.MinLayer == view->u.tex.first_layer);
554 assert(stObj->layer_override || last_layer(stObj) == view->u.tex.last_layer);
555 assert(!stObj->layer_override ||
556 (stObj->layer_override == view->u.tex.first_layer &&
557 stObj->layer_override == view->u.tex.last_layer));
558 return view;
559 }
560
561 /* create new sampler view */
562 enum pipe_format format = get_sampler_view_format(st, stObj,
563 srgb_skip_decode);
564 struct pipe_sampler_view *view =
565 st_create_texture_sampler_view_from_stobj(st, stObj, format,
566 glsl130_or_later);
567
568 view = st_texture_set_sampler_view(st, stObj, view,
569 glsl130_or_later, srgb_skip_decode);
570
571 return view;
572 }
573
574
575 struct pipe_sampler_view *
576 st_get_buffer_sampler_view_from_stobj(struct st_context *st,
577 struct st_texture_object *stObj)
578 {
579 const struct st_sampler_view *sv;
580 struct st_buffer_object *stBuf =
581 st_buffer_object(stObj->base.BufferObject);
582
583 if (!stBuf || !stBuf->buffer)
584 return NULL;
585
586 sv = st_texture_get_current_sampler_view(st, stObj);
587
588 struct pipe_resource *buf = stBuf->buffer;
589
590 if (sv) {
591 struct pipe_sampler_view *view = sv->view;
592
593 if (view->texture == buf) {
594 /* Debug check: make sure that the sampler view's parameters are
595 * what they're supposed to be.
596 */
597 assert(st_mesa_format_to_pipe_format(st,
598 stObj->base._BufferObjectFormat)
599 == view->format);
600 assert(view->target == PIPE_BUFFER);
601 unsigned base = stObj->base.BufferOffset;
602 MAYBE_UNUSED unsigned size = MIN2(buf->width0 - base,
603 (unsigned) stObj->base.BufferSize);
604 assert(view->u.buf.offset == base);
605 assert(view->u.buf.size == size);
606 return view;
607 }
608 }
609
610 unsigned base = stObj->base.BufferOffset;
611
612 if (base >= buf->width0)
613 return NULL;
614
615 unsigned size = buf->width0 - base;
616 size = MIN2(size, (unsigned)stObj->base.BufferSize);
617 if (!size)
618 return NULL;
619
620 /* Create a new sampler view. There is no need to clear the entire
621 * structure (consider CPU overhead).
622 */
623 struct pipe_sampler_view templ;
624
625 templ.format =
626 st_mesa_format_to_pipe_format(st, stObj->base._BufferObjectFormat);
627 templ.target = PIPE_BUFFER;
628 templ.swizzle_r = PIPE_SWIZZLE_X;
629 templ.swizzle_g = PIPE_SWIZZLE_Y;
630 templ.swizzle_b = PIPE_SWIZZLE_Z;
631 templ.swizzle_a = PIPE_SWIZZLE_W;
632 templ.u.buf.offset = base;
633 templ.u.buf.size = size;
634
635 struct pipe_sampler_view *view =
636 st->pipe->create_sampler_view(st->pipe, buf, &templ);
637
638 view = st_texture_set_sampler_view(st, stObj, view, false, false);
639
640 return view;
641 }