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