1848b8605Smrg/**************************************************************************
2848b8605Smrg *
3848b8605Smrg * Copyright 2010 VMware, Inc.
4848b8605Smrg * All Rights Reserved.
5848b8605Smrg *
6848b8605Smrg * Permission is hereby granted, free of charge, to any person obtaining a
7848b8605Smrg * copy of this software and associated documentation files (the
8848b8605Smrg * "Software"), to deal in the Software without restriction, including
9848b8605Smrg * without limitation the rights to use, copy, modify, merge, publish,
10848b8605Smrg * distribute, sub license, and/or sell copies of the Software, and to
11848b8605Smrg * permit persons to whom the Software is furnished to do so, subject to
12848b8605Smrg * the following conditions:
13848b8605Smrg *
14848b8605Smrg * The above copyright notice and this permission notice (including the
15848b8605Smrg * next paragraph) shall be included in all copies or substantial portions
16848b8605Smrg * of the Software.
17848b8605Smrg *
18848b8605Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19848b8605Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20848b8605Smrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21848b8605Smrg * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22848b8605Smrg * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23848b8605Smrg * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24848b8605Smrg * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25848b8605Smrg *
26848b8605Smrg **************************************************************************/
27848b8605Smrg
28848b8605Smrg
29848b8605Smrg#include "u_format.h"
30848b8605Smrg#include "u_sampler.h"
31848b8605Smrg
32848b8605Smrg
33848b8605Smrg/**
34848b8605Smrg * Initialize a pipe_sampler_view.  'view' is considered to have
35848b8605Smrg * uninitialized contents.
36848b8605Smrg */
37848b8605Smrgstatic void
38848b8605Smrgdefault_template(struct pipe_sampler_view *view,
39848b8605Smrg                 const struct pipe_resource *texture,
40848b8605Smrg                 enum pipe_format format,
41848b8605Smrg                 unsigned expand_green_blue)
42848b8605Smrg{
43848b8605Smrg   memset(view, 0, sizeof(*view));
44848b8605Smrg
45848b8605Smrg   /* XXX: Check if format is compatible with texture->format.
46848b8605Smrg    */
47848b8605Smrg
48b8e80941Smrg   view->target = texture->target;
49848b8605Smrg   view->format = format;
50848b8605Smrg   view->u.tex.first_level = 0;
51848b8605Smrg   view->u.tex.last_level = texture->last_level;
52848b8605Smrg   view->u.tex.first_layer = 0;
53848b8605Smrg   view->u.tex.last_layer = texture->target == PIPE_TEXTURE_3D ?
54848b8605Smrg                               texture->depth0 - 1 : texture->array_size - 1;
55b8e80941Smrg   view->swizzle_r = PIPE_SWIZZLE_X;
56b8e80941Smrg   view->swizzle_g = PIPE_SWIZZLE_Y;
57b8e80941Smrg   view->swizzle_b = PIPE_SWIZZLE_Z;
58b8e80941Smrg   view->swizzle_a = PIPE_SWIZZLE_W;
59848b8605Smrg
60848b8605Smrg   /* Override default green and blue component expansion to the requested
61848b8605Smrg    * one.
62848b8605Smrg    *
63848b8605Smrg    * Gallium expands nonexistent components to (0,0,0,1), DX9 expands
64848b8605Smrg    * to (1,1,1,1).  Since alpha is always expanded to 1, and red is
65848b8605Smrg    * always present, we only really care about green and blue
66848b8605Smrg    * components.
67848b8605Smrg    *
68848b8605Smrg    * To make it look less hackish, one would have to add
69b8e80941Smrg    * PIPE_SWIZZLE_EXPAND to indicate components for expansion
70848b8605Smrg    * and then override without exceptions or favoring one component
71848b8605Smrg    * over another.
72848b8605Smrg    */
73848b8605Smrg   if (format != PIPE_FORMAT_A8_UNORM) {
74848b8605Smrg      const struct util_format_description *desc = util_format_description(format);
75848b8605Smrg
76848b8605Smrg      assert(desc);
77848b8605Smrg      if (desc) {
78b8e80941Smrg         if (desc->swizzle[1] == PIPE_SWIZZLE_0) {
79848b8605Smrg            view->swizzle_g = expand_green_blue;
80848b8605Smrg         }
81b8e80941Smrg         if (desc->swizzle[2] == PIPE_SWIZZLE_0) {
82848b8605Smrg            view->swizzle_b = expand_green_blue;
83848b8605Smrg         }
84848b8605Smrg      }
85848b8605Smrg   }
86848b8605Smrg}
87848b8605Smrg
88848b8605Smrgvoid
89848b8605Smrgu_sampler_view_default_template(struct pipe_sampler_view *view,
90848b8605Smrg                                const struct pipe_resource *texture,
91848b8605Smrg                                enum pipe_format format)
92848b8605Smrg{
93848b8605Smrg   /* Expand to (0, 0, 0, 1) */
94848b8605Smrg   default_template(view,
95848b8605Smrg                    texture,
96848b8605Smrg                    format,
97b8e80941Smrg                    PIPE_SWIZZLE_0);
98848b8605Smrg}
99848b8605Smrg
100848b8605Smrgvoid
101848b8605Smrgu_sampler_view_default_dx9_template(struct pipe_sampler_view *view,
102848b8605Smrg                                    const struct pipe_resource *texture,
103848b8605Smrg                                    enum pipe_format format)
104848b8605Smrg{
105848b8605Smrg   /* Expand to (1, 1, 1, 1) */
106848b8605Smrg   default_template(view,
107848b8605Smrg                    texture,
108848b8605Smrg                    format,
109b8e80941Smrg                    PIPE_SWIZZLE_1);
110848b8605Smrg}
111