1bf215546Sopenharmony_ci/**************************************************************************
2bf215546Sopenharmony_ci *
3bf215546Sopenharmony_ci * Copyright 2010 VMware, Inc.
4bf215546Sopenharmony_ci * All Rights Reserved.
5bf215546Sopenharmony_ci *
6bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
7bf215546Sopenharmony_ci * copy of this software and associated documentation files (the
8bf215546Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
9bf215546Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
10bf215546Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to
11bf215546Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
12bf215546Sopenharmony_ci * the following conditions:
13bf215546Sopenharmony_ci *
14bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the
15bf215546Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions
16bf215546Sopenharmony_ci * of the Software.
17bf215546Sopenharmony_ci *
18bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20bf215546Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21bf215546Sopenharmony_ci * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22bf215546Sopenharmony_ci * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23bf215546Sopenharmony_ci * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24bf215546Sopenharmony_ci * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25bf215546Sopenharmony_ci *
26bf215546Sopenharmony_ci **************************************************************************/
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci
29bf215546Sopenharmony_ci#include "util/format/u_format.h"
30bf215546Sopenharmony_ci#include "u_sampler.h"
31bf215546Sopenharmony_ci
32bf215546Sopenharmony_ci
33bf215546Sopenharmony_ci/**
34bf215546Sopenharmony_ci * Initialize a pipe_sampler_view.  'view' is considered to have
35bf215546Sopenharmony_ci * uninitialized contents.
36bf215546Sopenharmony_ci */
37bf215546Sopenharmony_cistatic void
38bf215546Sopenharmony_cidefault_template(struct pipe_sampler_view *view,
39bf215546Sopenharmony_ci                 const struct pipe_resource *texture,
40bf215546Sopenharmony_ci                 enum pipe_format format,
41bf215546Sopenharmony_ci                 unsigned expand_green_blue)
42bf215546Sopenharmony_ci{
43bf215546Sopenharmony_ci   memset(view, 0, sizeof(*view));
44bf215546Sopenharmony_ci
45bf215546Sopenharmony_ci   /* XXX: Check if format is compatible with texture->format.
46bf215546Sopenharmony_ci    */
47bf215546Sopenharmony_ci
48bf215546Sopenharmony_ci   view->target = texture->target;
49bf215546Sopenharmony_ci   view->format = format;
50bf215546Sopenharmony_ci   view->u.tex.first_level = 0;
51bf215546Sopenharmony_ci   view->u.tex.last_level = texture->last_level;
52bf215546Sopenharmony_ci   view->u.tex.first_layer = 0;
53bf215546Sopenharmony_ci   view->u.tex.last_layer = texture->target == PIPE_TEXTURE_3D ?
54bf215546Sopenharmony_ci                               texture->depth0 - 1 : texture->array_size - 1;
55bf215546Sopenharmony_ci   view->swizzle_r = PIPE_SWIZZLE_X;
56bf215546Sopenharmony_ci   view->swizzle_g = PIPE_SWIZZLE_Y;
57bf215546Sopenharmony_ci   view->swizzle_b = PIPE_SWIZZLE_Z;
58bf215546Sopenharmony_ci   view->swizzle_a = PIPE_SWIZZLE_W;
59bf215546Sopenharmony_ci
60bf215546Sopenharmony_ci   /* Override default green and blue component expansion to the requested
61bf215546Sopenharmony_ci    * one.
62bf215546Sopenharmony_ci    *
63bf215546Sopenharmony_ci    * Gallium expands nonexistent components to (0,0,0,1), DX9 expands
64bf215546Sopenharmony_ci    * to (1,1,1,1).  Since alpha is always expanded to 1, and red is
65bf215546Sopenharmony_ci    * always present, we only really care about green and blue
66bf215546Sopenharmony_ci    * components.
67bf215546Sopenharmony_ci    *
68bf215546Sopenharmony_ci    * To make it look less hackish, one would have to add
69bf215546Sopenharmony_ci    * PIPE_SWIZZLE_EXPAND to indicate components for expansion
70bf215546Sopenharmony_ci    * and then override without exceptions or favoring one component
71bf215546Sopenharmony_ci    * over another.
72bf215546Sopenharmony_ci    */
73bf215546Sopenharmony_ci   if (format != PIPE_FORMAT_A8_UNORM) {
74bf215546Sopenharmony_ci      const struct util_format_description *desc = util_format_description(format);
75bf215546Sopenharmony_ci
76bf215546Sopenharmony_ci      if (desc->swizzle[1] == PIPE_SWIZZLE_0) {
77bf215546Sopenharmony_ci         view->swizzle_g = expand_green_blue;
78bf215546Sopenharmony_ci      }
79bf215546Sopenharmony_ci      if (desc->swizzle[2] == PIPE_SWIZZLE_0) {
80bf215546Sopenharmony_ci         view->swizzle_b = expand_green_blue;
81bf215546Sopenharmony_ci      }
82bf215546Sopenharmony_ci   }
83bf215546Sopenharmony_ci}
84bf215546Sopenharmony_ci
85bf215546Sopenharmony_civoid
86bf215546Sopenharmony_ciu_sampler_view_default_template(struct pipe_sampler_view *view,
87bf215546Sopenharmony_ci                                const struct pipe_resource *texture,
88bf215546Sopenharmony_ci                                enum pipe_format format)
89bf215546Sopenharmony_ci{
90bf215546Sopenharmony_ci   /* Expand to (0, 0, 0, 1) */
91bf215546Sopenharmony_ci   default_template(view,
92bf215546Sopenharmony_ci                    texture,
93bf215546Sopenharmony_ci                    format,
94bf215546Sopenharmony_ci                    PIPE_SWIZZLE_0);
95bf215546Sopenharmony_ci}
96bf215546Sopenharmony_ci
97bf215546Sopenharmony_civoid
98bf215546Sopenharmony_ciu_sampler_view_default_dx9_template(struct pipe_sampler_view *view,
99bf215546Sopenharmony_ci                                    const struct pipe_resource *texture,
100bf215546Sopenharmony_ci                                    enum pipe_format format)
101bf215546Sopenharmony_ci{
102bf215546Sopenharmony_ci   /* Expand to (1, 1, 1, 1) */
103bf215546Sopenharmony_ci   default_template(view,
104bf215546Sopenharmony_ci                    texture,
105bf215546Sopenharmony_ci                    format,
106bf215546Sopenharmony_ci                    PIPE_SWIZZLE_1);
107bf215546Sopenharmony_ci}
108