ilo: add BLT-based blitting methods to ilo_blitter
[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 * For ETC1 format, the texture data will be decompressed before being
75 * written to the bo. See tex_staging_sys_convert_write().
76 */
77 switch (format) {
78 case PIPE_FORMAT_Z16_UNORM:
79 return BRW_SURFACEFORMAT_I16_UNORM;
80 case PIPE_FORMAT_Z32_FLOAT:
81 return BRW_SURFACEFORMAT_I32_FLOAT;
82 case PIPE_FORMAT_Z24X8_UNORM:
83 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
84 return BRW_SURFACEFORMAT_I24X8_UNORM;
85 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
86 return BRW_SURFACEFORMAT_I32X32_FLOAT;
87 case PIPE_FORMAT_ETC1_RGB8:
88 return BRW_SURFACEFORMAT_R8G8B8X8_UNORM;
89 default:
90 return ilo_translate_color_format(format);
91 }
92 break;
93 case PIPE_BIND_VERTEX_BUFFER:
94 /*
95 * Some 3-component formats are not supported as vertex element formats.
96 * But since we move between vertices using vb->stride, we should be
97 * good to use their 4-component counterparts if we force the W
98 * component to be one. The only exception is that the vb boundary
99 * check for the last vertex may fail.
100 */
101 switch (format) {
102 case PIPE_FORMAT_R16G16B16_FLOAT:
103 return BRW_SURFACEFORMAT_R16G16B16A16_FLOAT;
104 case PIPE_FORMAT_R16G16B16_UINT:
105 return BRW_SURFACEFORMAT_R16G16B16A16_UINT;
106 case PIPE_FORMAT_R16G16B16_SINT:
107 return BRW_SURFACEFORMAT_R16G16B16A16_SINT;
108 case PIPE_FORMAT_R8G8B8_UINT:
109 return BRW_SURFACEFORMAT_R8G8B8A8_UINT;
110 case PIPE_FORMAT_R8G8B8_SINT:
111 return BRW_SURFACEFORMAT_R8G8B8A8_SINT;
112 default:
113 return ilo_translate_color_format(format);
114 }
115 break;
116 default:
117 assert(!"cannot translate format");
118 break;
119 }
120
121 return -1;
122 }
123
124 static inline int
125 ilo_translate_render_format(enum pipe_format format)
126 {
127 return ilo_translate_format(format, PIPE_BIND_RENDER_TARGET);
128 }
129
130 static inline int
131 ilo_translate_texture_format(enum pipe_format format)
132 {
133 return ilo_translate_format(format, PIPE_BIND_SAMPLER_VIEW);
134 }
135
136 static inline int
137 ilo_translate_vertex_format(enum pipe_format format)
138 {
139 return ilo_translate_format(format, PIPE_BIND_VERTEX_BUFFER);
140 }
141
142 #endif /* ILO_FORMAT_H */