Home | History | Annotate | Download | only in util
      1 /**************************************************************************
      2  *
      3  * Copyright 2010 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 #include "u_format.h"
     30 #include "u_sampler.h"
     31 
     32 
     33 /**
     34  * Initialize a pipe_sampler_view.  'view' is considered to have
     35  * uninitialized contents.
     36  */
     37 static void
     38 default_template(struct pipe_sampler_view *view,
     39                  const struct pipe_resource *texture,
     40                  enum pipe_format format,
     41                  unsigned expand_green_blue)
     42 {
     43    memset(view, 0, sizeof(*view));
     44 
     45    /* XXX: Check if format is compatible with texture->format.
     46     */
     47 
     48    view->target = texture->target;
     49    view->format = format;
     50    view->u.tex.first_level = 0;
     51    view->u.tex.last_level = texture->last_level;
     52    view->u.tex.first_layer = 0;
     53    view->u.tex.last_layer = texture->target == PIPE_TEXTURE_3D ?
     54                                texture->depth0 - 1 : texture->array_size - 1;
     55    view->swizzle_r = PIPE_SWIZZLE_X;
     56    view->swizzle_g = PIPE_SWIZZLE_Y;
     57    view->swizzle_b = PIPE_SWIZZLE_Z;
     58    view->swizzle_a = PIPE_SWIZZLE_W;
     59 
     60    /* Override default green and blue component expansion to the requested
     61     * one.
     62     *
     63     * Gallium expands nonexistent components to (0,0,0,1), DX9 expands
     64     * to (1,1,1,1).  Since alpha is always expanded to 1, and red is
     65     * always present, we only really care about green and blue
     66     * components.
     67     *
     68     * To make it look less hackish, one would have to add
     69     * PIPE_SWIZZLE_EXPAND to indicate components for expansion
     70     * and then override without exceptions or favoring one component
     71     * over another.
     72     */
     73    if (format != PIPE_FORMAT_A8_UNORM) {
     74       const struct util_format_description *desc = util_format_description(format);
     75 
     76       assert(desc);
     77       if (desc) {
     78          if (desc->swizzle[1] == PIPE_SWIZZLE_0) {
     79             view->swizzle_g = expand_green_blue;
     80          }
     81          if (desc->swizzle[2] == PIPE_SWIZZLE_0) {
     82             view->swizzle_b = expand_green_blue;
     83          }
     84       }
     85    }
     86 }
     87 
     88 void
     89 u_sampler_view_default_template(struct pipe_sampler_view *view,
     90                                 const struct pipe_resource *texture,
     91                                 enum pipe_format format)
     92 {
     93    /* Expand to (0, 0, 0, 1) */
     94    default_template(view,
     95                     texture,
     96                     format,
     97                     PIPE_SWIZZLE_0);
     98 }
     99 
    100 void
    101 u_sampler_view_default_dx9_template(struct pipe_sampler_view *view,
    102                                     const struct pipe_resource *texture,
    103                                     enum pipe_format format)
    104 {
    105    /* Expand to (1, 1, 1, 1) */
    106    default_template(view,
    107                     texture,
    108                     format,
    109                     PIPE_SWIZZLE_1);
    110 }
    111