vc4: Introduce XML-based packet header generation like Intel's.
[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 bool glsl130_or_later)
193 {
194 switch (baseFormat) {
195 case GL_RGBA:
196 return SWIZZLE_XYZW;
197 case GL_RGB:
198 return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_ONE);
199 case GL_RG:
200 return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_ZERO, SWIZZLE_ONE);
201 case GL_RED:
202 return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_ZERO,
203 SWIZZLE_ZERO, SWIZZLE_ONE);
204 case GL_ALPHA:
205 return MAKE_SWIZZLE4(SWIZZLE_ZERO, SWIZZLE_ZERO,
206 SWIZZLE_ZERO, SWIZZLE_W);
207 case GL_LUMINANCE:
208 return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_X, SWIZZLE_X, SWIZZLE_ONE);
209 case GL_LUMINANCE_ALPHA:
210 return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_X, SWIZZLE_X, SWIZZLE_W);
211 case GL_INTENSITY:
212 return SWIZZLE_XXXX;
213 case GL_STENCIL_INDEX:
214 case GL_DEPTH_STENCIL:
215 case GL_DEPTH_COMPONENT:
216 /* Now examine the depth mode */
217 switch (depthMode) {
218 case GL_LUMINANCE:
219 return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_X, SWIZZLE_X, SWIZZLE_ONE);
220 case GL_INTENSITY:
221 return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_X, SWIZZLE_X, SWIZZLE_X);
222 case GL_ALPHA:
223 /* The texture(sampler*Shadow) functions from GLSL 1.30 ignore
224 * the depth mode and return float, while older shadow* functions
225 * and ARB_fp instructions return vec4 according to the depth mode.
226 *
227 * The problem with the GLSL 1.30 functions is that GL_ALPHA forces
228 * them to return 0, breaking them completely.
229 *
230 * A proper fix would increase code complexity and that's not worth
231 * it for a rarely used feature such as the GL_ALPHA depth mode
232 * in GL3. Therefore, change GL_ALPHA to GL_INTENSITY for all
233 * shaders that use GLSL 1.30 or later.
234 *
235 * BTW, it's required that sampler views are updated when
236 * shaders change (check_sampler_swizzle takes care of that).
237 */
238 if (glsl130_or_later)
239 return SWIZZLE_XXXX;
240 else
241 return MAKE_SWIZZLE4(SWIZZLE_ZERO, SWIZZLE_ZERO,
242 SWIZZLE_ZERO, SWIZZLE_X);
243 case GL_RED:
244 return MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_ZERO,
245 SWIZZLE_ZERO, SWIZZLE_ONE);
246 default:
247 assert(!"Unexpected depthMode");
248 return SWIZZLE_XYZW;
249 }
250 default:
251 assert(!"Unexpected baseFormat");
252 return SWIZZLE_XYZW;
253 }
254 }
255
256
257 static unsigned
258 get_texture_format_swizzle(const struct st_context *st,
259 const struct st_texture_object *stObj,
260 bool glsl130_or_later)
261 {
262 GLenum baseFormat = _mesa_base_tex_image(&stObj->base)->_BaseFormat;
263 unsigned tex_swizzle;
264 GLenum depth_mode = stObj->base.DepthMode;
265
266 /* In ES 3.0, DEPTH_TEXTURE_MODE is expected to be GL_RED for textures
267 * with depth component data specified with a sized internal format.
268 */
269 if (_mesa_is_gles3(st->ctx) &&
270 (baseFormat == GL_DEPTH_COMPONENT ||
271 baseFormat == GL_DEPTH_STENCIL ||
272 baseFormat == GL_STENCIL_INDEX)) {
273 const struct gl_texture_image *firstImage =
274 _mesa_base_tex_image(&stObj->base);
275 if (firstImage->InternalFormat != GL_DEPTH_COMPONENT &&
276 firstImage->InternalFormat != GL_DEPTH_STENCIL &&
277 firstImage->InternalFormat != GL_STENCIL_INDEX)
278 depth_mode = GL_RED;
279 }
280 tex_swizzle = compute_texture_format_swizzle(baseFormat,
281 depth_mode,
282 glsl130_or_later);
283
284 /* Combine the texture format swizzle with user's swizzle */
285 return swizzle_swizzle(stObj->base._Swizzle, tex_swizzle);
286 }
287
288
289 /**
290 * Return TRUE if the texture's sampler view swizzle is not equal to
291 * the texture's swizzle.
292 *
293 * \param stObj the st texture object,
294 */
295 MAYBE_UNUSED static boolean
296 check_sampler_swizzle(const struct st_context *st,
297 const struct st_texture_object *stObj,
298 const struct pipe_sampler_view *sv,
299 bool glsl130_or_later)
300 {
301 unsigned swizzle = get_texture_format_swizzle(st, stObj, glsl130_or_later);
302
303 return ((sv->swizzle_r != GET_SWZ(swizzle, 0)) ||
304 (sv->swizzle_g != GET_SWZ(swizzle, 1)) ||
305 (sv->swizzle_b != GET_SWZ(swizzle, 2)) ||
306 (sv->swizzle_a != GET_SWZ(swizzle, 3)));
307 }
308
309
310 static unsigned
311 last_level(const struct st_texture_object *stObj)
312 {
313 unsigned ret = MIN2(stObj->base.MinLevel + stObj->base._MaxLevel,
314 stObj->pt->last_level);
315 if (stObj->base.Immutable)
316 ret = MIN2(ret, stObj->base.MinLevel + stObj->base.NumLevels - 1);
317 return ret;
318 }
319
320
321 static unsigned
322 last_layer(const struct st_texture_object *stObj)
323 {
324 if (stObj->base.Immutable && stObj->pt->array_size > 1)
325 return MIN2(stObj->base.MinLayer + stObj->base.NumLayers - 1,
326 stObj->pt->array_size - 1);
327 return stObj->pt->array_size - 1;
328 }
329
330
331 /**
332 * Determine the format for the texture sampler view.
333 */
334 static enum pipe_format
335 get_sampler_view_format(struct st_context *st,
336 const struct st_texture_object *stObj,
337 const struct gl_sampler_object *samp)
338 {
339 enum pipe_format format;
340
341 GLenum baseFormat = _mesa_base_tex_image(&stObj->base)->_BaseFormat;
342 format = stObj->surface_based ? stObj->surface_format : stObj->pt->format;
343
344 if (baseFormat == GL_DEPTH_COMPONENT ||
345 baseFormat == GL_DEPTH_STENCIL ||
346 baseFormat == GL_STENCIL_INDEX) {
347 if (stObj->base.StencilSampling || baseFormat == GL_STENCIL_INDEX)
348 format = util_format_stencil_only(format);
349
350 return format;
351 }
352
353 /* If sRGB decoding is off, use the linear format */
354 if (samp->sRGBDecode == GL_SKIP_DECODE_EXT)
355 format = util_format_linear(format);
356
357 /* Use R8_UNORM for video formats */
358 switch (format) {
359 case PIPE_FORMAT_NV12:
360 case PIPE_FORMAT_IYUV:
361 format = PIPE_FORMAT_R8_UNORM;
362 break;
363 default:
364 break;
365 }
366 return format;
367 }
368
369
370 static struct pipe_sampler_view *
371 st_create_texture_sampler_view_from_stobj(struct st_context *st,
372 struct st_texture_object *stObj,
373 enum pipe_format format,
374 bool glsl130_or_later)
375 {
376 /* There is no need to clear this structure (consider CPU overhead). */
377 struct pipe_sampler_view templ;
378 unsigned swizzle = get_texture_format_swizzle(st, stObj, glsl130_or_later);
379
380 templ.format = format;
381
382 templ.u.tex.first_level = stObj->base.MinLevel + stObj->base.BaseLevel;
383 templ.u.tex.last_level = last_level(stObj);
384 assert(templ.u.tex.first_level <= templ.u.tex.last_level);
385 if (stObj->layer_override) {
386 templ.u.tex.first_layer = templ.u.tex.last_layer = stObj->layer_override;
387 } else {
388 templ.u.tex.first_layer = stObj->base.MinLayer;
389 templ.u.tex.last_layer = last_layer(stObj);
390 }
391 assert(templ.u.tex.first_layer <= templ.u.tex.last_layer);
392 templ.target = gl_target_to_pipe(stObj->base.Target);
393
394 templ.swizzle_r = GET_SWZ(swizzle, 0);
395 templ.swizzle_g = GET_SWZ(swizzle, 1);
396 templ.swizzle_b = GET_SWZ(swizzle, 2);
397 templ.swizzle_a = GET_SWZ(swizzle, 3);
398
399 return st->pipe->create_sampler_view(st->pipe, stObj->pt, &templ);
400 }
401
402
403 struct pipe_sampler_view *
404 st_get_texture_sampler_view_from_stobj(struct st_context *st,
405 struct st_texture_object *stObj,
406 const struct gl_sampler_object *samp,
407 bool glsl130_or_later)
408 {
409 struct pipe_sampler_view **sv;
410
411 sv = st_texture_get_sampler_view(st, stObj);
412
413 if (*sv) {
414 /* Debug check: make sure that the sampler view's parameters are
415 * what they're supposed to be.
416 */
417 MAYBE_UNUSED struct pipe_sampler_view *view = *sv;
418 assert(stObj->pt == view->texture);
419 assert(!check_sampler_swizzle(st, stObj, view, glsl130_or_later));
420 assert(get_sampler_view_format(st, stObj, samp) == view->format);
421 assert(gl_target_to_pipe(stObj->base.Target) == view->target);
422 assert(stObj->base.MinLevel + stObj->base.BaseLevel ==
423 view->u.tex.first_level);
424 assert(last_level(stObj) == view->u.tex.last_level);
425 assert(stObj->layer_override || stObj->base.MinLayer == view->u.tex.first_layer);
426 assert(stObj->layer_override || last_layer(stObj) == view->u.tex.last_layer);
427 assert(!stObj->layer_override ||
428 (stObj->layer_override == view->u.tex.first_layer &&
429 stObj->layer_override == view->u.tex.last_layer));
430 }
431 else {
432 /* create new sampler view */
433 enum pipe_format format = get_sampler_view_format(st, stObj, samp);
434
435 *sv = st_create_texture_sampler_view_from_stobj(st, stObj,
436 format, glsl130_or_later);
437
438 }
439
440 return *sv;
441 }
442
443
444 struct pipe_sampler_view *
445 st_get_buffer_sampler_view_from_stobj(struct st_context *st,
446 struct st_texture_object *stObj)
447 {
448 struct pipe_sampler_view **sv;
449 struct st_buffer_object *stBuf =
450 st_buffer_object(stObj->base.BufferObject);
451
452 if (!stBuf || !stBuf->buffer)
453 return NULL;
454
455 sv = st_texture_get_sampler_view(st, stObj);
456
457 struct pipe_resource *buf = stBuf->buffer;
458 struct pipe_sampler_view *view = *sv;
459
460 if (view && view->texture == buf) {
461 /* Debug check: make sure that the sampler view's parameters are
462 * what they're supposed to be.
463 */
464 assert(st_mesa_format_to_pipe_format(st, stObj->base._BufferObjectFormat)
465 == view->format);
466 assert(view->target == PIPE_BUFFER);
467 unsigned base = stObj->base.BufferOffset;
468 MAYBE_UNUSED unsigned size = MIN2(buf->width0 - base,
469 (unsigned) stObj->base.BufferSize);
470 assert(view->u.buf.offset == base);
471 assert(view->u.buf.size == size);
472 } else {
473 unsigned base = stObj->base.BufferOffset;
474
475 if (base >= buf->width0)
476 return NULL;
477
478 unsigned size = buf->width0 - base;
479 size = MIN2(size, (unsigned)stObj->base.BufferSize);
480 if (!size)
481 return NULL;
482
483 /* Create a new sampler view. There is no need to clear the entire
484 * structure (consider CPU overhead).
485 */
486 struct pipe_sampler_view templ;
487
488 templ.format =
489 st_mesa_format_to_pipe_format(st, stObj->base._BufferObjectFormat);
490 templ.target = PIPE_BUFFER;
491 templ.swizzle_r = PIPE_SWIZZLE_X;
492 templ.swizzle_g = PIPE_SWIZZLE_Y;
493 templ.swizzle_b = PIPE_SWIZZLE_Z;
494 templ.swizzle_a = PIPE_SWIZZLE_W;
495 templ.u.buf.offset = base;
496 templ.u.buf.size = size;
497
498 pipe_sampler_view_reference(sv, NULL);
499 *sv = st->pipe->create_sampler_view(st->pipe, buf, &templ);
500 }
501 return *sv;
502 }