e8a46a722440f6190f8b0cd0e61d66d5e879522f
[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 * Try to find a matching sampler view for the given context.
47 * If none is found an empty slot is initialized with a
48 * template and returned instead.
49 */
50 static struct pipe_sampler_view **
51 st_texture_get_sampler_view(struct st_context *st,
52 struct st_texture_object *stObj)
53 {
54 struct pipe_sampler_view **free = NULL;
55 GLuint i;
56
57 for (i = 0; i < stObj->num_sampler_views; ++i) {
58 struct pipe_sampler_view **sv = &stObj->sampler_views[i];
59 /* Is the array entry used ? */
60 if (*sv) {
61 /* check if the context matches */
62 if ((*sv)->context == st->pipe) {
63 return sv;
64 }
65 } else {
66 /* Found a free slot, remember that */
67 free = sv;
68 }
69 }
70
71 /* Couldn't find a slot for our context, create a new one */
72
73 if (!free) {
74 /* Haven't even found a free one, resize the array */
75 unsigned new_size = (stObj->num_sampler_views + 1) *
76 sizeof(struct pipe_sampler_view *);
77 stObj->sampler_views = realloc(stObj->sampler_views, new_size);
78 free = &stObj->sampler_views[stObj->num_sampler_views++];
79 *free = NULL;
80 }
81
82 assert(*free == NULL);
83
84 return free;
85 }
86
87
88 /**
89 * For the given texture object, release any sampler views which belong
90 * to the calling context.
91 */
92 void
93 st_texture_release_sampler_view(struct st_context *st,
94 struct st_texture_object *stObj)
95 {
96 GLuint i;
97
98 for (i = 0; i < stObj->num_sampler_views; ++i) {
99 struct pipe_sampler_view **sv = &stObj->sampler_views[i];
100
101 if (*sv && (*sv)->context == st->pipe) {
102 pipe_sampler_view_reference(sv, NULL);
103 break;
104 }
105 }
106 }
107
108
109 /**
110 * Release all sampler views attached to the given texture object, regardless
111 * of the context.
112 */
113 void
114 st_texture_release_all_sampler_views(struct st_context *st,
115 struct st_texture_object *stObj)
116 {
117 GLuint i;
118
119 /* XXX This should use sampler_views[i]->pipe, not st->pipe */
120 for (i = 0; i < stObj->num_sampler_views; ++i)
121 pipe_sampler_view_release(st->pipe, &stObj->sampler_views[i]);
122 }
123
124
125 void
126 st_texture_free_sampler_views(struct st_texture_object *stObj)
127 {
128 free(stObj->sampler_views);
129 stObj->sampler_views = NULL;
130 stObj->num_sampler_views = 0;
131 }
132
133
134 /**
135 * Return swizzle1(swizzle2)
136 */
137 static unsigned
138 swizzle_swizzle(unsigned swizzle1, unsigned swizzle2)
139 {
140 unsigned i, swz[4];
141
142 if (swizzle1 == SWIZZLE_XYZW) {
143 /* identity swizzle, no change to swizzle2 */
144 return swizzle2;
145 }
146
147 for (i = 0; i < 4; i++) {
148 unsigned s = GET_SWZ(swizzle1, i);
149 switch (s) {
150 case SWIZZLE_X:
151 case SWIZZLE_Y:
152 case SWIZZLE_Z:
153 case SWIZZLE_W:
154 swz[i] = GET_SWZ(swizzle2, s);
155 break;
156 case SWIZZLE_ZERO:
157 swz[i] = SWIZZLE_ZERO;
158 break;
159 case SWIZZLE_ONE:
160 swz[i] = SWIZZLE_ONE;
161 break;
162 default:
163 assert(!"Bad swizzle term");
164 swz[i] = SWIZZLE_X;
165 }
166 }
167
168 return MAKE_SWIZZLE4(swz[0], swz[1], swz[2], swz[3]);
169 }
170
171
172 /**
173 * Given a user-specified texture base format, the actual gallium texture
174 * format and the current GL_DEPTH_MODE, return a texture swizzle.
175 *
176 * Consider the case where the user requests a GL_RGB internal texture
177 * format the driver actually uses an RGBA format. The A component should
178 * be ignored and sampling from the texture should always return (r,g,b,1).
179 * But if we rendered to the texture we might have written A values != 1.
180 * By sampling the texture with a ".xyz1" swizzle we'll get the expected A=1.
181 * This function computes the texture swizzle needed to get the expected
182 * values.
183 *
184 * In the case of depth textures, the GL_DEPTH_MODE state determines the
185 * texture swizzle.
186 *
187 * This result must be composed with the user-specified swizzle to get
188 * the final swizzle.
189 */
190 static unsigned
191 compute_texture_format_swizzle(GLenum baseFormat, GLenum depthMode,
192 enum pipe_format actualFormat,
193 unsigned glsl_version)
194 {
195 switch (baseFormat) {
196 case GL_RGBA:
197 return SWIZZLE_XYZW;
198 case GL_RGB:
199 if (util_format_has_alpha(actualFormat))
200 return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_ONE);
201 else
202 return SWIZZLE_XYZW;
203 case GL_RG:
204 if (util_format_get_nr_components(actualFormat) > 2)
205 return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_ZERO, SWIZZLE_ONE);
206 else
207 return SWIZZLE_XYZW;
208 case GL_RED:
209 if (util_format_get_nr_components(actualFormat) > 1)
210 return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_ZERO,
211 SWIZZLE_ZERO, SWIZZLE_ONE);
212 else
213 return SWIZZLE_XYZW;
214 case GL_ALPHA:
215 if (util_format_get_nr_components(actualFormat) > 1)
216 return MAKE_SWIZZLE4(SWIZZLE_ZERO, SWIZZLE_ZERO,
217 SWIZZLE_ZERO, SWIZZLE_W);
218 else
219 return SWIZZLE_XYZW;
220 case GL_LUMINANCE:
221 if (util_format_get_nr_components(actualFormat) > 1)
222 return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_X, SWIZZLE_X, SWIZZLE_ONE);
223 else
224 return SWIZZLE_XYZW;
225 case GL_LUMINANCE_ALPHA:
226 if (util_format_get_nr_components(actualFormat) > 2)
227 return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_X, SWIZZLE_X, SWIZZLE_W);
228 else
229 return SWIZZLE_XYZW;
230 case GL_INTENSITY:
231 if (util_format_get_nr_components(actualFormat) > 1)
232 return SWIZZLE_XXXX;
233 else
234 return SWIZZLE_XYZW;
235 case GL_STENCIL_INDEX:
236 case GL_DEPTH_STENCIL:
237 case GL_DEPTH_COMPONENT:
238 /* Now examine the depth mode */
239 switch (depthMode) {
240 case GL_LUMINANCE:
241 return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_X, SWIZZLE_X, SWIZZLE_ONE);
242 case GL_INTENSITY:
243 return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_X, SWIZZLE_X, SWIZZLE_X);
244 case GL_ALPHA:
245 /* The texture(sampler*Shadow) functions from GLSL 1.30 ignore
246 * the depth mode and return float, while older shadow* functions
247 * and ARB_fp instructions return vec4 according to the depth mode.
248 *
249 * The problem with the GLSL 1.30 functions is that GL_ALPHA forces
250 * them to return 0, breaking them completely.
251 *
252 * A proper fix would increase code complexity and that's not worth
253 * it for a rarely used feature such as the GL_ALPHA depth mode
254 * in GL3. Therefore, change GL_ALPHA to GL_INTENSITY for all
255 * shaders that use GLSL 1.30 or later.
256 *
257 * BTW, it's required that sampler views are updated when
258 * shaders change (check_sampler_swizzle takes care of that).
259 */
260 if (glsl_version && glsl_version >= 130)
261 return SWIZZLE_XXXX;
262 else
263 return MAKE_SWIZZLE4(SWIZZLE_ZERO, SWIZZLE_ZERO,
264 SWIZZLE_ZERO, SWIZZLE_X);
265 case GL_RED:
266 return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_ZERO,
267 SWIZZLE_ZERO, SWIZZLE_ONE);
268 default:
269 assert(!"Unexpected depthMode");
270 return SWIZZLE_XYZW;
271 }
272 default:
273 assert(!"Unexpected baseFormat");
274 return SWIZZLE_XYZW;
275 }
276 }
277
278
279 static unsigned
280 get_texture_format_swizzle(const struct st_context *st,
281 const struct st_texture_object *stObj,
282 unsigned glsl_version)
283 {
284 GLenum baseFormat = _mesa_base_tex_image(&stObj->base)->_BaseFormat;
285 unsigned tex_swizzle;
286 GLenum depth_mode = stObj->base.DepthMode;
287
288 /* In ES 3.0, DEPTH_TEXTURE_MODE is expected to be GL_RED for textures
289 * with depth component data specified with a sized internal format.
290 */
291 if (_mesa_is_gles3(st->ctx) &&
292 (baseFormat == GL_DEPTH_COMPONENT ||
293 baseFormat == GL_DEPTH_STENCIL ||
294 baseFormat == GL_STENCIL_INDEX)) {
295 const struct gl_texture_image *firstImage =
296 _mesa_base_tex_image(&stObj->base);
297 if (firstImage->InternalFormat != GL_DEPTH_COMPONENT &&
298 firstImage->InternalFormat != GL_DEPTH_STENCIL &&
299 firstImage->InternalFormat != GL_STENCIL_INDEX)
300 depth_mode = GL_RED;
301 }
302 tex_swizzle = compute_texture_format_swizzle(baseFormat,
303 depth_mode,
304 stObj->pt->format,
305 glsl_version);
306
307 /* Combine the texture format swizzle with user's swizzle */
308 return swizzle_swizzle(stObj->base._Swizzle, tex_swizzle);
309 }
310
311
312 /**
313 * Return TRUE if the texture's sampler view swizzle is not equal to
314 * the texture's swizzle.
315 *
316 * \param stObj the st texture object,
317 */
318 MAYBE_UNUSED static boolean
319 check_sampler_swizzle(const struct st_context *st,
320 const struct st_texture_object *stObj,
321 const struct pipe_sampler_view *sv, unsigned glsl_version)
322 {
323 unsigned swizzle = get_texture_format_swizzle(st, stObj, glsl_version);
324
325 return ((sv->swizzle_r != GET_SWZ(swizzle, 0)) ||
326 (sv->swizzle_g != GET_SWZ(swizzle, 1)) ||
327 (sv->swizzle_b != GET_SWZ(swizzle, 2)) ||
328 (sv->swizzle_a != GET_SWZ(swizzle, 3)));
329 }
330
331
332 static unsigned
333 last_level(const struct st_texture_object *stObj)
334 {
335 unsigned ret = MIN2(stObj->base.MinLevel + stObj->base._MaxLevel,
336 stObj->pt->last_level);
337 if (stObj->base.Immutable)
338 ret = MIN2(ret, stObj->base.MinLevel + stObj->base.NumLevels - 1);
339 return ret;
340 }
341
342
343 static unsigned
344 last_layer(const struct st_texture_object *stObj)
345 {
346 if (stObj->base.Immutable && stObj->pt->array_size > 1)
347 return MIN2(stObj->base.MinLayer + stObj->base.NumLayers - 1,
348 stObj->pt->array_size - 1);
349 return stObj->pt->array_size - 1;
350 }
351
352
353 /**
354 * Determine the format for the texture sampler view.
355 */
356 static enum pipe_format
357 get_sampler_view_format(struct st_context *st,
358 const struct st_texture_object *stObj,
359 const struct gl_sampler_object *samp)
360 {
361 enum pipe_format format;
362
363 GLenum baseFormat = _mesa_base_tex_image(&stObj->base)->_BaseFormat;
364 format = stObj->surface_based ? stObj->surface_format : stObj->pt->format;
365
366 if (baseFormat == GL_DEPTH_COMPONENT ||
367 baseFormat == GL_DEPTH_STENCIL ||
368 baseFormat == GL_STENCIL_INDEX) {
369 if (stObj->base.StencilSampling || baseFormat == GL_STENCIL_INDEX)
370 format = util_format_stencil_only(format);
371
372 return format;
373 }
374
375 /* If sRGB decoding is off, use the linear format */
376 if (samp->sRGBDecode == GL_SKIP_DECODE_EXT)
377 format = util_format_linear(format);
378
379 /* Use R8_UNORM for video formats */
380 switch (format) {
381 case PIPE_FORMAT_NV12:
382 case PIPE_FORMAT_IYUV:
383 format = PIPE_FORMAT_R8_UNORM;
384 break;
385 default:
386 break;
387 }
388 return format;
389 }
390
391
392 static struct pipe_sampler_view *
393 st_create_texture_sampler_view_from_stobj(struct st_context *st,
394 struct st_texture_object *stObj,
395 enum pipe_format format,
396 unsigned glsl_version)
397 {
398 struct pipe_sampler_view templ;
399 unsigned swizzle = get_texture_format_swizzle(st, stObj, glsl_version);
400
401 u_sampler_view_default_template(&templ, stObj->pt, format);
402
403 templ.u.tex.first_level = stObj->base.MinLevel + stObj->base.BaseLevel;
404 templ.u.tex.last_level = last_level(stObj);
405 assert(templ.u.tex.first_level <= templ.u.tex.last_level);
406 if (stObj->layer_override) {
407 templ.u.tex.first_layer = templ.u.tex.last_layer = stObj->layer_override;
408 } else {
409 templ.u.tex.first_layer = stObj->base.MinLayer;
410 templ.u.tex.last_layer = last_layer(stObj);
411 }
412 assert(templ.u.tex.first_layer <= templ.u.tex.last_layer);
413 templ.target = gl_target_to_pipe(stObj->base.Target);
414
415 templ.swizzle_r = GET_SWZ(swizzle, 0);
416 templ.swizzle_g = GET_SWZ(swizzle, 1);
417 templ.swizzle_b = GET_SWZ(swizzle, 2);
418 templ.swizzle_a = GET_SWZ(swizzle, 3);
419
420 return st->pipe->create_sampler_view(st->pipe, stObj->pt, &templ);
421 }
422
423
424 struct pipe_sampler_view *
425 st_get_texture_sampler_view_from_stobj(struct st_context *st,
426 struct st_texture_object *stObj,
427 const struct gl_sampler_object *samp,
428 unsigned glsl_version)
429 {
430 struct pipe_sampler_view **sv;
431
432 if (!stObj || !stObj->pt) {
433 return NULL;
434 }
435
436 sv = st_texture_get_sampler_view(st, stObj);
437
438 if (*sv) {
439 /* Debug check: make sure that the sampler view's parameters are
440 * what they're supposed to be.
441 */
442 MAYBE_UNUSED struct pipe_sampler_view *view = *sv;
443 assert(stObj->pt == view->texture);
444 assert(!check_sampler_swizzle(st, stObj, view, glsl_version));
445 assert(get_sampler_view_format(st, stObj, samp) == view->format);
446 assert(gl_target_to_pipe(stObj->base.Target) == view->target);
447 assert(stObj->base.MinLevel + stObj->base.BaseLevel ==
448 view->u.tex.first_level);
449 assert(last_level(stObj) == view->u.tex.last_level);
450 assert(stObj->layer_override || stObj->base.MinLayer == view->u.tex.first_layer);
451 assert(stObj->layer_override || last_layer(stObj) == view->u.tex.last_layer);
452 assert(!stObj->layer_override ||
453 (stObj->layer_override == view->u.tex.first_layer &&
454 stObj->layer_override == view->u.tex.last_layer));
455 }
456 else {
457 /* create new sampler view */
458 enum pipe_format format = get_sampler_view_format(st, stObj, samp);
459
460 *sv = st_create_texture_sampler_view_from_stobj(st, stObj,
461 format, glsl_version);
462
463 }
464
465 return *sv;
466 }
467
468
469 struct pipe_sampler_view *
470 st_get_buffer_sampler_view_from_stobj(struct st_context *st,
471 struct st_texture_object *stObj)
472 {
473 struct pipe_sampler_view **sv;
474 struct st_buffer_object *stBuf =
475 st_buffer_object(stObj->base.BufferObject);
476
477 if (!stBuf || !stBuf->buffer)
478 return NULL;
479
480 sv = st_texture_get_sampler_view(st, stObj);
481
482 struct pipe_resource *buf = stBuf->buffer;
483 struct pipe_sampler_view *view = *sv;
484
485 if (view && view->texture == buf) {
486 /* Debug check: make sure that the sampler view's parameters are
487 * what they're supposed to be.
488 */
489 assert(st_mesa_format_to_pipe_format(st, stObj->base._BufferObjectFormat)
490 == view->format);
491 assert(view->target == PIPE_BUFFER);
492 unsigned base = stObj->base.BufferOffset;
493 MAYBE_UNUSED unsigned size = MIN2(buf->width0 - base,
494 (unsigned) stObj->base.BufferSize);
495 assert(view->u.buf.offset == base);
496 assert(view->u.buf.size == size);
497 } else {
498 unsigned base = stObj->base.BufferOffset;
499
500 if (base >= buf->width0)
501 return NULL;
502
503 unsigned size = buf->width0 - base;
504 size = MIN2(size, (unsigned)stObj->base.BufferSize);
505 if (!size)
506 return NULL;
507
508 /* Create a new sampler view. There is no need to clear the entire
509 * structure (consider CPU overhead).
510 */
511 struct pipe_sampler_view templ;
512
513 templ.format =
514 st_mesa_format_to_pipe_format(st, stObj->base._BufferObjectFormat);
515 templ.target = PIPE_BUFFER;
516 templ.swizzle_r = PIPE_SWIZZLE_X;
517 templ.swizzle_g = PIPE_SWIZZLE_Y;
518 templ.swizzle_b = PIPE_SWIZZLE_Z;
519 templ.swizzle_a = PIPE_SWIZZLE_W;
520 templ.u.buf.offset = base;
521 templ.u.buf.size = size;
522
523 pipe_sampler_view_reference(sv, NULL);
524 *sv = st->pipe->create_sampler_view(st->pipe, buf, &templ);
525 }
526 return *sv;
527 }