Merge branch '7.8'
[mesa.git] / src / gallium / auxiliary / util / u_sampler.c
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 static void
34 default_template(struct pipe_sampler_view *view,
35 const struct pipe_resource *texture,
36 enum pipe_format format,
37 unsigned expand_green_blue)
38 {
39 /* XXX: Check if format is compatible with texture->format.
40 */
41
42 view->format = format;
43 view->first_level = 0;
44 view->last_level = texture->last_level;
45 view->swizzle_r = PIPE_SWIZZLE_RED;
46 view->swizzle_g = PIPE_SWIZZLE_GREEN;
47 view->swizzle_b = PIPE_SWIZZLE_BLUE;
48 view->swizzle_a = PIPE_SWIZZLE_ALPHA;
49
50 /* Override default green and blue component expansion to the requested
51 * one.
52 *
53 * Gallium expands nonexistent components to (0,0,0,1), DX9 expands
54 * to (1,1,1,1). Since alpha is always expanded to 1, and red is
55 * always present, we only really care about green and blue
56 * components.
57 *
58 * To make it look less hackish, one would have to add
59 * UTIL_FORMAT_SWIZZLE_EXPAND to indicate components for expansion
60 * and then override without exceptions or favoring one component
61 * over another.
62 */
63 if (format != PIPE_FORMAT_A8_UNORM) {
64 const struct util_format_description *desc = util_format_description(format);
65
66 assert(desc);
67 if (desc) {
68 if (desc->swizzle[1] == UTIL_FORMAT_SWIZZLE_0) {
69 view->swizzle_g = expand_green_blue;
70 }
71 if (desc->swizzle[2] == UTIL_FORMAT_SWIZZLE_0) {
72 view->swizzle_b = expand_green_blue;
73 }
74 }
75 }
76 }
77
78 void
79 u_sampler_view_default_template(struct pipe_sampler_view *view,
80 const struct pipe_resource *texture,
81 enum pipe_format format)
82 {
83 /* Expand to (0, 0, 0, 1) */
84 default_template(view,
85 texture,
86 format,
87 PIPE_SWIZZLE_ZERO);
88 }
89
90 void
91 u_sampler_view_default_dx9_template(struct pipe_sampler_view *view,
92 const struct pipe_resource *texture,
93 enum pipe_format format)
94 {
95 /* Expand to (1, 1, 1, 1) */
96 default_template(view,
97 texture,
98 format,
99 PIPE_SWIZZLE_ONE);
100 }