summaryrefslogtreecommitdiffstats
path: root/src/gallium/drivers/ilo/ilo_format.h
blob: 0a19c02659e8ee352616fc4d93ff9a763b5b1a65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
 * Mesa 3-D graphics library
 *
 * Copyright (C) 2012-2013 LunarG, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 * Authors:
 *    Chia-I Wu <olv@lunarg.com>
 */

#ifndef ILO_FORMAT_H
#define ILO_FORMAT_H

#include "genhw/genhw.h"

#include "ilo_common.h"

bool
ilo_format_support_vb(const struct ilo_dev *dev,
                      enum pipe_format format);

bool
ilo_format_support_sol(const struct ilo_dev *dev,
                       enum pipe_format format);

bool
ilo_format_support_sampler(const struct ilo_dev *dev,
                           enum pipe_format format);

bool
ilo_format_support_rt(const struct ilo_dev *dev,
                      enum pipe_format format);

bool
ilo_format_support_zs(const struct ilo_dev *dev,
                      enum pipe_format format);

int
ilo_format_translate_color(const struct ilo_dev *dev,
                           enum pipe_format format);

/**
 * Translate a pipe format to a hardware surface format suitable for
 * the given purpose.  Return -1 on errors.
 *
 * This is an inline function not only for performance reasons.  There are
 * caveats that the callers should be aware of before calling this function.
 */
static inline int
ilo_format_translate(const struct ilo_dev *dev,
                     enum pipe_format format, unsigned bind)
{
   switch (bind) {
   case PIPE_BIND_RENDER_TARGET:
      /*
       * Some RGBX formats are not supported as render target formats.  But we
       * can use their RGBA counterparts and force the destination alpha to be
       * one when blending is enabled.
       */
      switch (format) {
      case PIPE_FORMAT_B8G8R8X8_UNORM:
         return GEN6_FORMAT_B8G8R8A8_UNORM;
      default:
         return ilo_format_translate_color(dev, format);
      }
      break;
   case PIPE_BIND_SAMPLER_VIEW:
      /*
       * For depth formats, we want the depth values to be returned as R
       * values.  But we assume in many places that the depth values are
       * returned as I values (util_make_fragment_tex_shader_writedepth() is
       * one such example).  We have to live with that at least for now.
       *
       * For ETC1 format, the texture data will be decompressed before being
       * written to the bo.  See tex_staging_sys_convert_write().
       */
      switch (format) {
      case PIPE_FORMAT_Z16_UNORM:
         return GEN6_FORMAT_I16_UNORM;
      case PIPE_FORMAT_Z32_FLOAT:
         return GEN6_FORMAT_I32_FLOAT;
      case PIPE_FORMAT_Z24X8_UNORM:
      case PIPE_FORMAT_Z24_UNORM_S8_UINT:
         return GEN6_FORMAT_I24X8_UNORM;
      case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
         return GEN6_FORMAT_I32X32_FLOAT;
      case PIPE_FORMAT_ETC1_RGB8:
         return GEN6_FORMAT_R8G8B8X8_UNORM;
      default:
         return ilo_format_translate_color(dev, format);
      }
      break;
   case PIPE_BIND_VERTEX_BUFFER:
      if (ilo_dev_gen(dev) >= ILO_GEN(7.5))
         return ilo_format_translate_color(dev, format);

      /*
       * Some 3-component formats are not supported as vertex element formats.
       * But since we move between vertices using vb->stride, we should be
       * good to use their 4-component counterparts if we force the W
       * component to be one.  The only exception is that the vb boundary
       * check for the last vertex may fail.
       */
      switch (format) {
      case PIPE_FORMAT_R16G16B16_FLOAT:
         return GEN6_FORMAT_R16G16B16A16_FLOAT;
      case PIPE_FORMAT_R16G16B16_UINT:
         return GEN6_FORMAT_R16G16B16A16_UINT;
      case PIPE_FORMAT_R16G16B16_SINT:
         return GEN6_FORMAT_R16G16B16A16_SINT;
      case PIPE_FORMAT_R8G8B8_UINT:
         return GEN6_FORMAT_R8G8B8A8_UINT;
      case PIPE_FORMAT_R8G8B8_SINT:
         return GEN6_FORMAT_R8G8B8A8_SINT;
      default:
         return ilo_format_translate_color(dev, format);
      }
      break;
   case PIPE_BIND_STREAM_OUTPUT:
      return ilo_format_translate_color(dev, format);
      break;
   default:
      assert(!"cannot translate format");
      break;
   }

   return -1;
}

static inline int
ilo_format_translate_render(const struct ilo_dev *dev,
                            enum pipe_format format)
{
   return ilo_format_translate(dev, format, PIPE_BIND_RENDER_TARGET);
}

static inline int
ilo_format_translate_texture(const struct ilo_dev *dev,
                             enum pipe_format format)
{
   return ilo_format_translate(dev, format, PIPE_BIND_SAMPLER_VIEW);
}

static inline int
ilo_format_translate_vertex(const struct ilo_dev *dev,
                            enum pipe_format format)
{
   return ilo_format_translate(dev, format, PIPE_BIND_VERTEX_BUFFER);
}

static inline enum gen_depth_format
ilo_format_translate_depth(const struct ilo_dev *dev,
                           enum pipe_format format)
{
   if (ilo_dev_gen(dev) >= ILO_GEN(7)) {
      switch (format) {
      case PIPE_FORMAT_Z32_FLOAT:
         return GEN6_ZFORMAT_D32_FLOAT;
      case PIPE_FORMAT_Z24X8_UNORM:
         return GEN6_ZFORMAT_D24_UNORM_X8_UINT;
      case PIPE_FORMAT_Z16_UNORM:
         return GEN6_ZFORMAT_D16_UNORM;
      default:
         assert(!"unknown depth format");
         return GEN6_ZFORMAT_D32_FLOAT;
      }
   } else {
      switch (format) {
      case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
         return GEN6_ZFORMAT_D32_FLOAT_S8X24_UINT;
      case PIPE_FORMAT_Z32_FLOAT:
         return GEN6_ZFORMAT_D32_FLOAT;
      case PIPE_FORMAT_Z24_UNORM_S8_UINT:
         return GEN6_ZFORMAT_D24_UNORM_S8_UINT;
      case PIPE_FORMAT_Z24X8_UNORM:
         return GEN6_ZFORMAT_D24_UNORM_X8_UINT;
      case PIPE_FORMAT_Z16_UNORM:
         return GEN6_ZFORMAT_D16_UNORM;
      default:
         assert(!"unknown depth format");
         return GEN6_ZFORMAT_D32_FLOAT;
      }
   }
}

#endif /* ILO_FORMAT_H */