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 #include <stdio.h>
29
30 #include "st_context.h"
31 #include "st_format.h"
32 #include "st_texture.h"
33 #include "main/enums.h"
34
35 #include "pipe/p_state.h"
36 #include "pipe/p_context.h"
37 #include "pipe/p_defines.h"
38 #include "util/u_inlines.h"
39 #include "util/format/u_format.h"
40 #include "util/u_rect.h"
41 #include "util/u_math.h"
42 #include "util/u_memory.h"
43 #include "tgsi/tgsi_from_mesa.h"
44
45
46 #define DBG if(0) printf
47
48
49 /**
50 * Allocate a new pipe_resource object
51 * width0, height0, depth0 are the dimensions of the level 0 image
52 * (the highest resolution). last_level indicates how many mipmap levels
53 * to allocate storage for. For non-mipmapped textures, this will be zero.
54 */
55 struct pipe_resource *
st_texture_create(struct st_context * st,enum pipe_texture_target target,enum pipe_format format,GLuint last_level,GLuint width0,GLuint height0,GLuint depth0,GLuint layers,GLuint nr_samples,GLuint bind,bool sparse)56 st_texture_create(struct st_context *st,
57 enum pipe_texture_target target,
58 enum pipe_format format,
59 GLuint last_level,
60 GLuint width0,
61 GLuint height0,
62 GLuint depth0,
63 GLuint layers,
64 GLuint nr_samples,
65 GLuint bind,
66 bool sparse)
67 {
68 struct pipe_resource pt, *newtex;
69 struct pipe_screen *screen = st->screen;
70
71 assert(target < PIPE_MAX_TEXTURE_TYPES);
72 assert(width0 > 0);
73 assert(height0 > 0);
74 assert(depth0 > 0);
75 if (target == PIPE_TEXTURE_CUBE)
76 assert(layers == 6);
77
78 DBG("%s target %d format %s last_level %d\n", __func__,
79 (int) target, util_format_name(format), last_level);
80
81 assert(format);
82 assert(screen->is_format_supported(screen, format, target, 0, 0,
83 PIPE_BIND_SAMPLER_VIEW));
84
85 memset(&pt, 0, sizeof(pt));
86 pt.target = target;
87 pt.format = format;
88 pt.last_level = last_level;
89 pt.width0 = width0;
90 pt.height0 = height0;
91 pt.depth0 = depth0;
92 pt.array_size = layers;
93 pt.usage = PIPE_USAGE_DEFAULT;
94 pt.bind = bind;
95 /* only set this for OpenGL textures, not renderbuffers */
96 pt.flags = PIPE_RESOURCE_FLAG_TEXTURING_MORE_LIKELY;
97 pt.nr_samples = nr_samples;
98 pt.nr_storage_samples = nr_samples;
99
100 if (sparse)
101 pt.flags |= PIPE_RESOURCE_FLAG_SPARSE;
102
103 newtex = screen->resource_create(screen, &pt);
104
105 assert(!newtex || pipe_is_referenced(&newtex->reference));
106
107 return newtex;
108 }
109
110
111 /**
112 * In OpenGL the number of 1D array texture layers is the "height" and
113 * the number of 2D array texture layers is the "depth". In Gallium the
114 * number of layers in an array texture is a separate 'array_size' field.
115 * This function converts dimensions from the former to the later.
116 */
117 void
st_gl_texture_dims_to_pipe_dims(GLenum texture,unsigned widthIn,uint16_t heightIn,uint16_t depthIn,unsigned * widthOut,uint16_t * heightOut,uint16_t * depthOut,uint16_t * layersOut)118 st_gl_texture_dims_to_pipe_dims(GLenum texture,
119 unsigned widthIn,
120 uint16_t heightIn,
121 uint16_t depthIn,
122 unsigned *widthOut,
123 uint16_t *heightOut,
124 uint16_t *depthOut,
125 uint16_t *layersOut)
126 {
127 switch (texture) {
128 case GL_TEXTURE_1D:
129 case GL_PROXY_TEXTURE_1D:
130 assert(heightIn == 1);
131 assert(depthIn == 1);
132 *widthOut = widthIn;
133 *heightOut = 1;
134 *depthOut = 1;
135 *layersOut = 1;
136 break;
137 case GL_TEXTURE_1D_ARRAY:
138 case GL_PROXY_TEXTURE_1D_ARRAY:
139 assert(depthIn == 1);
140 *widthOut = widthIn;
141 *heightOut = 1;
142 *depthOut = 1;
143 *layersOut = heightIn;
144 break;
145 case GL_TEXTURE_2D:
146 case GL_PROXY_TEXTURE_2D:
147 case GL_TEXTURE_RECTANGLE:
148 case GL_PROXY_TEXTURE_RECTANGLE:
149 case GL_TEXTURE_EXTERNAL_OES:
150 case GL_PROXY_TEXTURE_2D_MULTISAMPLE:
151 case GL_TEXTURE_2D_MULTISAMPLE:
152 assert(depthIn == 1);
153 *widthOut = widthIn;
154 *heightOut = heightIn;
155 *depthOut = 1;
156 *layersOut = 1;
157 break;
158 case GL_TEXTURE_CUBE_MAP:
159 case GL_PROXY_TEXTURE_CUBE_MAP:
160 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
161 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
162 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
163 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
164 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
165 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
166 assert(depthIn == 1);
167 *widthOut = widthIn;
168 *heightOut = heightIn;
169 *depthOut = 1;
170 *layersOut = 6;
171 break;
172 case GL_TEXTURE_2D_ARRAY:
173 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
174 case GL_PROXY_TEXTURE_2D_ARRAY:
175 case GL_PROXY_TEXTURE_2D_MULTISAMPLE_ARRAY:
176 *widthOut = widthIn;
177 *heightOut = heightIn;
178 *depthOut = 1;
179 *layersOut = depthIn;
180 break;
181 case GL_TEXTURE_CUBE_MAP_ARRAY:
182 case GL_PROXY_TEXTURE_CUBE_MAP_ARRAY:
183 *widthOut = widthIn;
184 *heightOut = heightIn;
185 *depthOut = 1;
186 *layersOut = util_align_npot(depthIn, 6);
187 break;
188 default:
189 assert(0 && "Unexpected texture in st_gl_texture_dims_to_pipe_dims()");
190 case GL_TEXTURE_3D:
191 case GL_PROXY_TEXTURE_3D:
192 *widthOut = widthIn;
193 *heightOut = heightIn;
194 *depthOut = depthIn;
195 *layersOut = 1;
196 break;
197 }
198 }
199
200
201 /**
202 * Check if a texture image can be pulled into a unified mipmap texture.
203 */
204 GLboolean
st_texture_match_image(struct st_context * st,const struct pipe_resource * pt,const struct gl_texture_image * image)205 st_texture_match_image(struct st_context *st,
206 const struct pipe_resource *pt,
207 const struct gl_texture_image *image)
208 {
209 unsigned ptWidth;
210 uint16_t ptHeight, ptDepth, ptLayers;
211
212 /* Images with borders are never pulled into mipmap textures.
213 */
214 if (image->Border)
215 return GL_FALSE;
216
217 /* Check if this image's format matches the established texture's format.
218 */
219 if (st_mesa_format_to_pipe_format(st, image->TexFormat) != pt->format)
220 return GL_FALSE;
221
222 st_gl_texture_dims_to_pipe_dims(image->TexObject->Target,
223 image->Width, image->Height, image->Depth,
224 &ptWidth, &ptHeight, &ptDepth, &ptLayers);
225
226 /* Test if this image's size matches what's expected in the
227 * established texture.
228 */
229 if (ptWidth != u_minify(pt->width0, image->Level) ||
230 ptHeight != u_minify(pt->height0, image->Level) ||
231 ptDepth != u_minify(pt->depth0, image->Level) ||
232 ptLayers != pt->array_size)
233 return GL_FALSE;
234
235 if (image->Level > pt->last_level)
236 return GL_FALSE;
237
238 return GL_TRUE;
239 }
240
241
242 /**
243 * Map a texture image and return the address for a particular 2D face/slice/
244 * layer. The stImage indicates the cube face and mipmap level. The slice
245 * of the 3D texture is passed in 'zoffset'.
246 * \param usage one of the PIPE_MAP_x values
247 * \param x, y, w, h the region of interest of the 2D image.
248 * \return address of mapping or NULL if any error
249 */
250 GLubyte *
st_texture_image_map(struct st_context * st,struct gl_texture_image * stImage,enum pipe_map_flags usage,GLuint x,GLuint y,GLuint z,GLuint w,GLuint h,GLuint d,struct pipe_transfer ** transfer)251 st_texture_image_map(struct st_context *st, struct gl_texture_image *stImage,
252 enum pipe_map_flags usage,
253 GLuint x, GLuint y, GLuint z,
254 GLuint w, GLuint h, GLuint d,
255 struct pipe_transfer **transfer)
256 {
257 struct gl_texture_object *stObj = stImage->TexObject;
258 GLuint level;
259 void *map;
260
261 DBG("%s \n", __func__);
262
263 if (!stImage->pt)
264 return NULL;
265
266 if (stObj->pt != stImage->pt)
267 level = 0;
268 else
269 level = stImage->Level;
270
271 if (stObj->Immutable) {
272 level += stObj->Attrib.MinLevel;
273 z += stObj->Attrib.MinLayer;
274 if (stObj->pt->array_size > 1)
275 d = MIN2(d, stObj->Attrib.NumLayers);
276 }
277
278 z += stImage->Face;
279
280 map = pipe_texture_map_3d(st->pipe, stImage->pt, level, usage,
281 x, y, z, w, h, d, transfer);
282 if (map) {
283 /* Enlarge the transfer array if it's not large enough. */
284 if (z >= stImage->num_transfers) {
285 unsigned new_size = z + 1;
286
287 stImage->transfer = realloc(stImage->transfer,
288 new_size * sizeof(struct st_texture_image_transfer));
289 memset(&stImage->transfer[stImage->num_transfers], 0,
290 (new_size - stImage->num_transfers) *
291 sizeof(struct st_texture_image_transfer));
292 stImage->num_transfers = new_size;
293 }
294
295 assert(!stImage->transfer[z].transfer);
296 stImage->transfer[z].transfer = *transfer;
297 }
298 return map;
299 }
300
301
302 void
st_texture_image_unmap(struct st_context * st,struct gl_texture_image * stImage,unsigned slice)303 st_texture_image_unmap(struct st_context *st,
304 struct gl_texture_image *stImage, unsigned slice)
305 {
306 struct pipe_context *pipe = st->pipe;
307 struct gl_texture_object *stObj = stImage->TexObject;
308 struct pipe_transfer **transfer;
309
310 if (stObj->Immutable)
311 slice += stObj->Attrib.MinLayer;
312 transfer = &stImage->transfer[slice + stImage->Face].transfer;
313
314 DBG("%s\n", __func__);
315
316 pipe_texture_unmap(pipe, *transfer);
317 *transfer = NULL;
318 }
319
320
321 /**
322 * For debug only: get/print center pixel in the src resource.
323 */
324 static void
print_center_pixel(struct pipe_context * pipe,struct pipe_resource * src)325 print_center_pixel(struct pipe_context *pipe, struct pipe_resource *src)
326 {
327 struct pipe_transfer *xfer;
328 struct pipe_box region;
329 ubyte *map;
330
331 region.x = src->width0 / 2;
332 region.y = src->height0 / 2;
333 region.z = 0;
334 region.width = 1;
335 region.height = 1;
336 region.depth = 1;
337
338 map = pipe->texture_map(pipe, src, 0, PIPE_MAP_READ, ®ion, &xfer);
339
340 printf("center pixel: %d %d %d %d\n", map[0], map[1], map[2], map[3]);
341
342 pipe->texture_unmap(pipe, xfer);
343 }
344
345
346 /**
347 * Copy the image at level=0 in 'src' to the 'dst' resource at 'dstLevel'.
348 * This is used to copy mipmap images from one texture buffer to another.
349 * This typically happens when our initial guess at the total texture size
350 * is incorrect (see the guess_and_alloc_texture() function).
351 */
352 void
st_texture_image_copy(struct pipe_context * pipe,struct pipe_resource * dst,GLuint dstLevel,struct pipe_resource * src,GLuint srcLevel,GLuint face)353 st_texture_image_copy(struct pipe_context *pipe,
354 struct pipe_resource *dst, GLuint dstLevel,
355 struct pipe_resource *src, GLuint srcLevel,
356 GLuint face)
357 {
358 GLuint width = u_minify(dst->width0, dstLevel);
359 GLuint height = u_minify(dst->height0, dstLevel);
360 GLuint depth = u_minify(dst->depth0, dstLevel);
361 struct pipe_box src_box;
362 GLuint i;
363
364 if (u_minify(src->width0, srcLevel) != width ||
365 u_minify(src->height0, srcLevel) != height ||
366 u_minify(src->depth0, srcLevel) != depth) {
367 /* The source image size doesn't match the destination image size.
368 * This can happen in some degenerate situations such as rendering to a
369 * cube map face which was set up with mismatched texture sizes.
370 */
371 return;
372 }
373
374 src_box.x = 0;
375 src_box.y = 0;
376 src_box.width = width;
377 src_box.height = height;
378 src_box.depth = 1;
379
380 if (src->target == PIPE_TEXTURE_1D_ARRAY ||
381 src->target == PIPE_TEXTURE_2D_ARRAY ||
382 src->target == PIPE_TEXTURE_CUBE_ARRAY) {
383 face = 0;
384 depth = src->array_size;
385 }
386
387 /* Loop over 3D image slices */
388 /* could (and probably should) use "true" 3d box here -
389 but drivers can't quite handle it yet */
390 for (i = face; i < face + depth; i++) {
391 src_box.z = i;
392
393 if (0) {
394 print_center_pixel(pipe, src);
395 }
396
397 pipe->resource_copy_region(pipe,
398 dst,
399 dstLevel,
400 0, 0, i,/* destX, Y, Z */
401 src,
402 srcLevel,
403 &src_box);
404 }
405 }
406
407
408 struct pipe_resource *
st_create_color_map_texture(struct gl_context * ctx)409 st_create_color_map_texture(struct gl_context *ctx)
410 {
411 struct st_context *st = st_context(ctx);
412 struct pipe_resource *pt;
413 enum pipe_format format;
414 const uint texSize = 256; /* simple, and usually perfect */
415
416 /* find an RGBA texture format */
417 format = st_choose_format(st, GL_RGBA, GL_NONE, GL_NONE,
418 PIPE_TEXTURE_2D, 0, 0, PIPE_BIND_SAMPLER_VIEW,
419 false, false);
420
421 /* create texture for color map/table */
422 pt = st_texture_create(st, PIPE_TEXTURE_2D, format, 0,
423 texSize, texSize, 1, 1, 0, PIPE_BIND_SAMPLER_VIEW, false);
424 return pt;
425 }
426
427
428 /**
429 * Destroy bound texture handles for the given stage.
430 */
431 static void
st_destroy_bound_texture_handles_per_stage(struct st_context * st,enum pipe_shader_type shader)432 st_destroy_bound_texture_handles_per_stage(struct st_context *st,
433 enum pipe_shader_type shader)
434 {
435 struct st_bound_handles *bound_handles = &st->bound_texture_handles[shader];
436 struct pipe_context *pipe = st->pipe;
437 unsigned i;
438
439 if (likely(!bound_handles->num_handles))
440 return;
441
442 for (i = 0; i < bound_handles->num_handles; i++) {
443 uint64_t handle = bound_handles->handles[i];
444
445 pipe->make_texture_handle_resident(pipe, handle, false);
446 pipe->delete_texture_handle(pipe, handle);
447 }
448 free(bound_handles->handles);
449 bound_handles->handles = NULL;
450 bound_handles->num_handles = 0;
451 }
452
453
454 /**
455 * Destroy all bound texture handles in the context.
456 */
457 void
st_destroy_bound_texture_handles(struct st_context * st)458 st_destroy_bound_texture_handles(struct st_context *st)
459 {
460 unsigned i;
461
462 for (i = 0; i < PIPE_SHADER_TYPES; i++) {
463 st_destroy_bound_texture_handles_per_stage(st, i);
464 }
465 }
466
467
468 /**
469 * Destroy bound image handles for the given stage.
470 */
471 static void
st_destroy_bound_image_handles_per_stage(struct st_context * st,enum pipe_shader_type shader)472 st_destroy_bound_image_handles_per_stage(struct st_context *st,
473 enum pipe_shader_type shader)
474 {
475 struct st_bound_handles *bound_handles = &st->bound_image_handles[shader];
476 struct pipe_context *pipe = st->pipe;
477 unsigned i;
478
479 if (likely(!bound_handles->num_handles))
480 return;
481
482 for (i = 0; i < bound_handles->num_handles; i++) {
483 uint64_t handle = bound_handles->handles[i];
484
485 pipe->make_image_handle_resident(pipe, handle, GL_READ_WRITE, false);
486 pipe->delete_image_handle(pipe, handle);
487 }
488 free(bound_handles->handles);
489 bound_handles->handles = NULL;
490 bound_handles->num_handles = 0;
491 }
492
493
494 /**
495 * Destroy all bound image handles in the context.
496 */
497 void
st_destroy_bound_image_handles(struct st_context * st)498 st_destroy_bound_image_handles(struct st_context *st)
499 {
500 unsigned i;
501
502 for (i = 0; i < PIPE_SHADER_TYPES; i++) {
503 st_destroy_bound_image_handles_per_stage(st, i);
504 }
505 }
506
507
508 /**
509 * Create a texture handle from a texture unit.
510 */
511 static GLuint64
st_create_texture_handle_from_unit(struct st_context * st,struct gl_program * prog,GLuint texUnit)512 st_create_texture_handle_from_unit(struct st_context *st,
513 struct gl_program *prog, GLuint texUnit)
514 {
515 struct pipe_context *pipe = st->pipe;
516 struct pipe_sampler_view *view;
517 struct pipe_sampler_state sampler = {0};
518
519 /* TODO: Clarify the interaction of ARB_bindless_texture and EXT_texture_sRGB_decode */
520 view = st_update_single_texture(st, texUnit, prog->sh.data->Version >= 130,
521 true, false);
522 if (!view)
523 return 0;
524
525 if (view->target != PIPE_BUFFER)
526 st_convert_sampler_from_unit(st, &sampler, texUnit);
527
528 assert(st->ctx->Texture.Unit[texUnit]._Current);
529
530 return pipe->create_texture_handle(pipe, view, &sampler);
531 }
532
533
534 /**
535 * Create an image handle from an image unit.
536 */
537 static GLuint64
st_create_image_handle_from_unit(struct st_context * st,struct gl_program * prog,GLuint imgUnit)538 st_create_image_handle_from_unit(struct st_context *st,
539 struct gl_program *prog, GLuint imgUnit)
540 {
541 struct pipe_context *pipe = st->pipe;
542 struct pipe_image_view img;
543
544 st_convert_image_from_unit(st, &img, imgUnit, GL_READ_WRITE);
545
546 return pipe->create_image_handle(pipe, &img);
547 }
548
549
550 /**
551 * Make all bindless samplers bound to texture units resident in the context.
552 */
553 void
st_make_bound_samplers_resident(struct st_context * st,struct gl_program * prog)554 st_make_bound_samplers_resident(struct st_context *st,
555 struct gl_program *prog)
556 {
557 enum pipe_shader_type shader = pipe_shader_type_from_mesa(prog->info.stage);
558 struct st_bound_handles *bound_handles = &st->bound_texture_handles[shader];
559 struct pipe_context *pipe = st->pipe;
560 GLuint64 handle;
561 int i;
562
563 /* Remove previous bound texture handles for this stage. */
564 st_destroy_bound_texture_handles_per_stage(st, shader);
565
566 if (likely(!prog->sh.HasBoundBindlessSampler))
567 return;
568
569 for (i = 0; i < prog->sh.NumBindlessSamplers; i++) {
570 struct gl_bindless_sampler *sampler = &prog->sh.BindlessSamplers[i];
571
572 if (!sampler->bound)
573 continue;
574
575 /* Request a new texture handle from the driver and make it resident. */
576 handle = st_create_texture_handle_from_unit(st, prog, sampler->unit);
577 if (!handle)
578 continue;
579
580 pipe->make_texture_handle_resident(st->pipe, handle, true);
581
582 /* Overwrite the texture unit value by the resident handle before
583 * uploading the constant buffer.
584 */
585 *(uint64_t *)sampler->data = handle;
586
587 /* Store the handle in the context. */
588 bound_handles->handles = (uint64_t *)
589 realloc(bound_handles->handles,
590 (bound_handles->num_handles + 1) * sizeof(uint64_t));
591 bound_handles->handles[bound_handles->num_handles] = handle;
592 bound_handles->num_handles++;
593 }
594 }
595
596
597 /**
598 * Make all bindless images bound to image units resident in the context.
599 */
600 void
st_make_bound_images_resident(struct st_context * st,struct gl_program * prog)601 st_make_bound_images_resident(struct st_context *st,
602 struct gl_program *prog)
603 {
604 enum pipe_shader_type shader = pipe_shader_type_from_mesa(prog->info.stage);
605 struct st_bound_handles *bound_handles = &st->bound_image_handles[shader];
606 struct pipe_context *pipe = st->pipe;
607 GLuint64 handle;
608 int i;
609
610 /* Remove previous bound image handles for this stage. */
611 st_destroy_bound_image_handles_per_stage(st, shader);
612
613 if (likely(!prog->sh.HasBoundBindlessImage))
614 return;
615
616 for (i = 0; i < prog->sh.NumBindlessImages; i++) {
617 struct gl_bindless_image *image = &prog->sh.BindlessImages[i];
618
619 if (!image->bound)
620 continue;
621
622 /* Request a new image handle from the driver and make it resident. */
623 handle = st_create_image_handle_from_unit(st, prog, image->unit);
624 if (!handle)
625 continue;
626
627 pipe->make_image_handle_resident(st->pipe, handle, GL_READ_WRITE, true);
628
629 /* Overwrite the image unit value by the resident handle before uploading
630 * the constant buffer.
631 */
632 *(uint64_t *)image->data = handle;
633
634 /* Store the handle in the context. */
635 bound_handles->handles = (uint64_t *)
636 realloc(bound_handles->handles,
637 (bound_handles->num_handles + 1) * sizeof(uint64_t));
638 bound_handles->handles[bound_handles->num_handles] = handle;
639 bound_handles->num_handles++;
640 }
641 }
642