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