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