ilo: use an accessor for dev->gen
[mesa.git] / src / gallium / drivers / ilo / ilo_format.h
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 2012-2013 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Chia-I Wu <olv@lunarg.com>
26 */
27
28 #ifndef ILO_FORMAT_H
29 #define ILO_FORMAT_H
30
31 #include "genhw/genhw.h"
32
33 #include "ilo_common.h"
34
35 struct ilo_screen;
36
37 void
38 ilo_init_format_functions(struct ilo_screen *is);
39
40 int
41 ilo_translate_color_format(const struct ilo_dev_info *dev,
42 enum pipe_format format);
43
44 /**
45 * Translate a pipe format to a hardware surface format suitable for
46 * the given purpose. Return -1 on errors.
47 *
48 * This is an inline function not only for performance reasons. There are
49 * caveats that the callers should be aware of before calling this function.
50 */
51 static inline int
52 ilo_translate_format(const struct ilo_dev_info *dev,
53 enum pipe_format format, unsigned bind)
54 {
55 switch (bind) {
56 case PIPE_BIND_RENDER_TARGET:
57 /*
58 * Some RGBX formats are not supported as render target formats. But we
59 * can use their RGBA counterparts and force the destination alpha to be
60 * one when blending is enabled.
61 */
62 switch (format) {
63 case PIPE_FORMAT_B8G8R8X8_UNORM:
64 return GEN6_FORMAT_B8G8R8A8_UNORM;
65 default:
66 return ilo_translate_color_format(dev, format);
67 }
68 break;
69 case PIPE_BIND_SAMPLER_VIEW:
70 /*
71 * For depth formats, we want the depth values to be returned as R
72 * values. But we assume in many places that the depth values are
73 * returned as I values (util_make_fragment_tex_shader_writedepth() is
74 * one such example). We have to live with that at least for now.
75 *
76 * For ETC1 format, the texture data will be decompressed before being
77 * written to the bo. See tex_staging_sys_convert_write().
78 */
79 switch (format) {
80 case PIPE_FORMAT_Z16_UNORM:
81 return GEN6_FORMAT_I16_UNORM;
82 case PIPE_FORMAT_Z32_FLOAT:
83 return GEN6_FORMAT_I32_FLOAT;
84 case PIPE_FORMAT_Z24X8_UNORM:
85 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
86 return GEN6_FORMAT_I24X8_UNORM;
87 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
88 return GEN6_FORMAT_I32X32_FLOAT;
89 case PIPE_FORMAT_ETC1_RGB8:
90 return GEN6_FORMAT_R8G8B8X8_UNORM;
91 default:
92 return ilo_translate_color_format(dev, format);
93 }
94 break;
95 case PIPE_BIND_VERTEX_BUFFER:
96 if (ilo_dev_gen(dev) >= ILO_GEN(7.5))
97 return ilo_translate_color_format(dev, format);
98
99 /*
100 * Some 3-component formats are not supported as vertex element formats.
101 * But since we move between vertices using vb->stride, we should be
102 * good to use their 4-component counterparts if we force the W
103 * component to be one. The only exception is that the vb boundary
104 * check for the last vertex may fail.
105 */
106 switch (format) {
107 case PIPE_FORMAT_R16G16B16_FLOAT:
108 return GEN6_FORMAT_R16G16B16A16_FLOAT;
109 case PIPE_FORMAT_R16G16B16_UINT:
110 return GEN6_FORMAT_R16G16B16A16_UINT;
111 case PIPE_FORMAT_R16G16B16_SINT:
112 return GEN6_FORMAT_R16G16B16A16_SINT;
113 case PIPE_FORMAT_R8G8B8_UINT:
114 return GEN6_FORMAT_R8G8B8A8_UINT;
115 case PIPE_FORMAT_R8G8B8_SINT:
116 return GEN6_FORMAT_R8G8B8A8_SINT;
117 default:
118 return ilo_translate_color_format(dev, format);
119 }
120 break;
121 default:
122 assert(!"cannot translate format");
123 break;
124 }
125
126 return -1;
127 }
128
129 static inline int
130 ilo_translate_render_format(const struct ilo_dev_info *dev,
131 enum pipe_format format)
132 {
133 return ilo_translate_format(dev, format, PIPE_BIND_RENDER_TARGET);
134 }
135
136 static inline int
137 ilo_translate_texture_format(const struct ilo_dev_info *dev,
138 enum pipe_format format)
139 {
140 return ilo_translate_format(dev, format, PIPE_BIND_SAMPLER_VIEW);
141 }
142
143 static inline int
144 ilo_translate_vertex_format(const struct ilo_dev_info *dev,
145 enum pipe_format format)
146 {
147 return ilo_translate_format(dev, format, PIPE_BIND_VERTEX_BUFFER);
148 }
149
150 #endif /* ILO_FORMAT_H */