ilo: hook up pipe context 3D functions
[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 "brw_defines.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(enum pipe_format format);
42
43 /**
44 * Translate a pipe format to a hardware surface format suitable for
45 * the given purpose. Return -1 on errors.
46 *
47 * This is an inline function not only for performance reasons. There are
48 * caveats that the callers should that before calling this function.
49 */
50 static inline int
51 ilo_translate_format(enum pipe_format format, unsigned bind)
52 {
53 switch (bind) {
54 case PIPE_BIND_RENDER_TARGET:
55 /*
56 * Some RGBX formats are not supported as render target formats. But we
57 * can use their RGBA counterparts and force the destination alpha to be
58 * one when blending is enabled.
59 */
60 switch (format) {
61 case PIPE_FORMAT_B8G8R8X8_UNORM:
62 return BRW_SURFACEFORMAT_B8G8R8A8_UNORM;
63 default:
64 return ilo_translate_color_format(format);
65 }
66 break;
67 case PIPE_BIND_SAMPLER_VIEW:
68 /*
69 * For depth formats, we want the depth values to be returned as R
70 * values. But we assume in many places that the depth values are
71 * returned as I values (util_make_fragment_tex_shader_writedepth() is
72 * one such example). We have to live with that at least for now.
73 */
74 switch (format) {
75 case PIPE_FORMAT_Z16_UNORM:
76 return BRW_SURFACEFORMAT_I16_UNORM;
77 case PIPE_FORMAT_Z32_FLOAT:
78 return BRW_SURFACEFORMAT_I32_FLOAT;
79 case PIPE_FORMAT_Z24X8_UNORM:
80 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
81 return BRW_SURFACEFORMAT_I24X8_UNORM;
82 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
83 return BRW_SURFACEFORMAT_I32X32_FLOAT;
84 default:
85 return ilo_translate_color_format(format);
86 }
87 break;
88 case PIPE_BIND_VERTEX_BUFFER:
89 /*
90 * Some 3-component formats are not supported as vertex element formats.
91 * But since we move between vertices using vb->stride, we should be
92 * good to use their 4-component counterparts if we force the W
93 * component to be one. The only exception is that the vb boundary
94 * check for the last vertex may fail.
95 */
96 switch (format) {
97 case PIPE_FORMAT_R16G16B16_FLOAT:
98 return BRW_SURFACEFORMAT_R16G16B16A16_FLOAT;
99 case PIPE_FORMAT_R16G16B16_UINT:
100 return BRW_SURFACEFORMAT_R16G16B16A16_UINT;
101 case PIPE_FORMAT_R16G16B16_SINT:
102 return BRW_SURFACEFORMAT_R16G16B16A16_SINT;
103 case PIPE_FORMAT_R8G8B8_UINT:
104 return BRW_SURFACEFORMAT_R8G8B8A8_UINT;
105 case PIPE_FORMAT_R8G8B8_SINT:
106 return BRW_SURFACEFORMAT_R8G8B8A8_SINT;
107 default:
108 return ilo_translate_color_format(format);
109 }
110 break;
111 default:
112 assert(!"cannot translate format");
113 break;
114 }
115
116 return -1;
117 }
118
119 static inline int
120 ilo_translate_render_format(enum pipe_format format)
121 {
122 return ilo_translate_format(format, PIPE_BIND_RENDER_TARGET);
123 }
124
125 static inline int
126 ilo_translate_texture_format(enum pipe_format format)
127 {
128 return ilo_translate_format(format, PIPE_BIND_SAMPLER_VIEW);
129 }
130
131 static inline int
132 ilo_translate_vertex_format(enum pipe_format format)
133 {
134 return ilo_translate_format(format, PIPE_BIND_VERTEX_BUFFER);
135 }
136
137 #endif /* ILO_FORMAT_H */