1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20bf215546Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21bf215546Sopenharmony_ci * SOFTWARE.
22bf215546Sopenharmony_ci *
23bf215546Sopenharmony_ci * Authors:
24bf215546Sopenharmony_ci *    Rob Clark <robclark@freedesktop.org>
25bf215546Sopenharmony_ci */
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#include "pipe/p_defines.h"
28bf215546Sopenharmony_ci#include "util/format/u_format.h"
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_ci#include "fd2_util.h"
31bf215546Sopenharmony_ci
32bf215546Sopenharmony_cistatic enum a2xx_sq_surfaceformat
33bf215546Sopenharmony_cipipe2surface(enum pipe_format format, struct surface_format *fmt)
34bf215546Sopenharmony_ci{
35bf215546Sopenharmony_ci   const struct util_format_description *desc = util_format_description(format);
36bf215546Sopenharmony_ci
37bf215546Sopenharmony_ci   if (desc->layout != UTIL_FORMAT_LAYOUT_PLAIN) {
38bf215546Sopenharmony_ci      switch (format) {
39bf215546Sopenharmony_ci      /* Compressed textures. */
40bf215546Sopenharmony_ci      case PIPE_FORMAT_ETC1_RGB8:
41bf215546Sopenharmony_ci         return FMT_ETC1_RGB;
42bf215546Sopenharmony_ci      case PIPE_FORMAT_DXT1_RGB:
43bf215546Sopenharmony_ci      case PIPE_FORMAT_DXT1_RGBA:
44bf215546Sopenharmony_ci         return FMT_DXT1;
45bf215546Sopenharmony_ci      case PIPE_FORMAT_DXT3_RGBA:
46bf215546Sopenharmony_ci         return FMT_DXT2_3;
47bf215546Sopenharmony_ci      case PIPE_FORMAT_DXT5_RGBA:
48bf215546Sopenharmony_ci         return FMT_DXT4_5;
49bf215546Sopenharmony_ci      case PIPE_FORMAT_ATC_RGB:
50bf215546Sopenharmony_ci         return FMT_ATI_TC_555_565_RGB;
51bf215546Sopenharmony_ci      case PIPE_FORMAT_ATC_RGBA_EXPLICIT:
52bf215546Sopenharmony_ci         return FMT_ATI_TC_555_565_RGBA;
53bf215546Sopenharmony_ci      case PIPE_FORMAT_ATC_RGBA_INTERPOLATED:
54bf215546Sopenharmony_ci         return FMT_ATI_TC_555_565_RGBA_INTERP;
55bf215546Sopenharmony_ci      /* YUV buffers. */
56bf215546Sopenharmony_ci      case PIPE_FORMAT_UYVY:
57bf215546Sopenharmony_ci         return FMT_Y1_Cr_Y0_Cb;
58bf215546Sopenharmony_ci      case PIPE_FORMAT_YUYV:
59bf215546Sopenharmony_ci         return FMT_Cr_Y1_Cb_Y0;
60bf215546Sopenharmony_ci      default:
61bf215546Sopenharmony_ci         return ~0;
62bf215546Sopenharmony_ci      }
63bf215546Sopenharmony_ci   }
64bf215546Sopenharmony_ci
65bf215546Sopenharmony_ci   uint32_t channel_size = 0;
66bf215546Sopenharmony_ci   for (unsigned i = 0; i < 4; i++)
67bf215546Sopenharmony_ci      channel_size |= desc->channel[i].size << i * 8;
68bf215546Sopenharmony_ci
69bf215546Sopenharmony_ci   unsigned i = util_format_get_first_non_void_channel(format);
70bf215546Sopenharmony_ci   if (desc->channel[i].type == UTIL_FORMAT_TYPE_SIGNED ||
71bf215546Sopenharmony_ci       desc->channel[i].type == UTIL_FORMAT_TYPE_FIXED)
72bf215546Sopenharmony_ci      fmt->sign = SQ_TEX_SIGN_SIGNED;
73bf215546Sopenharmony_ci   if (!desc->channel[i].normalized)
74bf215546Sopenharmony_ci      fmt->num_format = SQ_TEX_NUM_FORMAT_INT;
75bf215546Sopenharmony_ci   if (desc->channel[i].type == UTIL_FORMAT_TYPE_FIXED)
76bf215546Sopenharmony_ci      fmt->exp_adjust = -16;
77bf215546Sopenharmony_ci
78bf215546Sopenharmony_ci      /* Note: the 3 channel 24bpp/48bpp/96bpp formats are only for vertex fetch
79bf215546Sopenharmony_ci       * we can use the 4 channel format and ignore the 4th component just isn't
80bf215546Sopenharmony_ci       * used
81bf215546Sopenharmony_ci       * XXX: is it possible for the extra loaded component to cause a MMU fault?
82bf215546Sopenharmony_ci       */
83bf215546Sopenharmony_ci
84bf215546Sopenharmony_ci#define CASE(r, g, b, a) case (r | g << 8 | b << 16 | a << 24)
85bf215546Sopenharmony_ci
86bf215546Sopenharmony_ci   /* clang-format off */
87bf215546Sopenharmony_ci   if (desc->channel[0].type == UTIL_FORMAT_TYPE_FLOAT) {
88bf215546Sopenharmony_ci      switch (channel_size) {
89bf215546Sopenharmony_ci      CASE(16,  0,  0,  0): return FMT_16_FLOAT;
90bf215546Sopenharmony_ci      CASE(16, 16,  0,  0): return FMT_16_16_FLOAT;
91bf215546Sopenharmony_ci      CASE(16, 16, 16,  0): return FMT_16_16_16_16_FLOAT; /* Note: only for vertex */
92bf215546Sopenharmony_ci      CASE(16, 16, 16, 16): return FMT_16_16_16_16_FLOAT;
93bf215546Sopenharmony_ci      CASE(32,  0,  0,  0): return FMT_32_FLOAT;
94bf215546Sopenharmony_ci      CASE(32, 32,  0,  0): return FMT_32_32_FLOAT;
95bf215546Sopenharmony_ci      CASE(32, 32, 32,  0): return FMT_32_32_32_FLOAT;
96bf215546Sopenharmony_ci      CASE(32, 32, 32, 32): return FMT_32_32_32_32_FLOAT;
97bf215546Sopenharmony_ci      }
98bf215546Sopenharmony_ci   } else {
99bf215546Sopenharmony_ci      switch (channel_size) {
100bf215546Sopenharmony_ci      CASE( 8,  0,  0,  0): return FMT_8;
101bf215546Sopenharmony_ci      CASE( 8,  8,  0,  0): return FMT_8_8;
102bf215546Sopenharmony_ci      CASE( 8,  8,  8,  0): return FMT_8_8_8_8; /* Note: only for vertex */
103bf215546Sopenharmony_ci      CASE( 8,  8,  8,  8): return FMT_8_8_8_8;
104bf215546Sopenharmony_ci      CASE(16,  0,  0,  0): return FMT_16;
105bf215546Sopenharmony_ci      CASE(16, 16,  0,  0): return FMT_16_16;
106bf215546Sopenharmony_ci      CASE(16, 16, 16,  0): return FMT_16_16_16_16; /* Note: only for vertex */
107bf215546Sopenharmony_ci      CASE(16, 16, 16, 16): return FMT_16_16_16_16;
108bf215546Sopenharmony_ci      CASE(32,  0,  0,  0): return FMT_32;
109bf215546Sopenharmony_ci      CASE(32, 32,  0,  0): return FMT_32_32;
110bf215546Sopenharmony_ci      CASE(32, 32, 32,  0): return FMT_32_32_32_32; /* Note: only for vertex */
111bf215546Sopenharmony_ci      CASE(32, 32, 32, 32): return FMT_32_32_32_32;
112bf215546Sopenharmony_ci      CASE( 4,  4,  4,  4): return FMT_4_4_4_4;
113bf215546Sopenharmony_ci      CASE( 5,  5,  5,  1): return FMT_1_5_5_5;
114bf215546Sopenharmony_ci      CASE( 5,  6,  5,  0): return FMT_5_6_5;
115bf215546Sopenharmony_ci      CASE(10, 10, 10,  2): return FMT_2_10_10_10;
116bf215546Sopenharmony_ci      CASE( 8, 24,  0,  0): return FMT_24_8;
117bf215546Sopenharmony_ci      CASE( 2,  3,  3,  0): return FMT_2_3_3; /* Note: R/B swapped */
118bf215546Sopenharmony_ci      }
119bf215546Sopenharmony_ci   }
120bf215546Sopenharmony_ci   /* clang-format on */
121bf215546Sopenharmony_ci#undef CASE
122bf215546Sopenharmony_ci
123bf215546Sopenharmony_ci   return ~0;
124bf215546Sopenharmony_ci}
125bf215546Sopenharmony_ci
126bf215546Sopenharmony_cistruct surface_format
127bf215546Sopenharmony_cifd2_pipe2surface(enum pipe_format format)
128bf215546Sopenharmony_ci{
129bf215546Sopenharmony_ci   struct surface_format fmt = {
130bf215546Sopenharmony_ci      .sign = SQ_TEX_SIGN_UNSIGNED,
131bf215546Sopenharmony_ci      .num_format = SQ_TEX_NUM_FORMAT_FRAC,
132bf215546Sopenharmony_ci      .exp_adjust = 0,
133bf215546Sopenharmony_ci   };
134bf215546Sopenharmony_ci   fmt.format = pipe2surface(format, &fmt);
135bf215546Sopenharmony_ci   return fmt;
136bf215546Sopenharmony_ci}
137bf215546Sopenharmony_ci
138bf215546Sopenharmony_cienum a2xx_colorformatx
139bf215546Sopenharmony_cifd2_pipe2color(enum pipe_format format)
140bf215546Sopenharmony_ci{
141bf215546Sopenharmony_ci   switch (format) {
142bf215546Sopenharmony_ci   /* 8-bit buffers. */
143bf215546Sopenharmony_ci   case PIPE_FORMAT_R8_UNORM:
144bf215546Sopenharmony_ci      return COLORX_8;
145bf215546Sopenharmony_ci   case PIPE_FORMAT_B2G3R3_UNORM:
146bf215546Sopenharmony_ci      return COLORX_2_3_3; /* note: untested */
147bf215546Sopenharmony_ci
148bf215546Sopenharmony_ci   /* 16-bit buffers. */
149bf215546Sopenharmony_ci   case PIPE_FORMAT_B5G6R5_UNORM:
150bf215546Sopenharmony_ci      return COLORX_5_6_5;
151bf215546Sopenharmony_ci   case PIPE_FORMAT_B5G5R5A1_UNORM:
152bf215546Sopenharmony_ci   case PIPE_FORMAT_B5G5R5X1_UNORM:
153bf215546Sopenharmony_ci      return COLORX_1_5_5_5;
154bf215546Sopenharmony_ci   case PIPE_FORMAT_B4G4R4A4_UNORM:
155bf215546Sopenharmony_ci   case PIPE_FORMAT_B4G4R4X4_UNORM:
156bf215546Sopenharmony_ci      return COLORX_4_4_4_4;
157bf215546Sopenharmony_ci   case PIPE_FORMAT_R8G8_UNORM:
158bf215546Sopenharmony_ci      return COLORX_8_8;
159bf215546Sopenharmony_ci
160bf215546Sopenharmony_ci   /* 32-bit buffers. */
161bf215546Sopenharmony_ci   case PIPE_FORMAT_B8G8R8A8_UNORM:
162bf215546Sopenharmony_ci   case PIPE_FORMAT_B8G8R8X8_UNORM:
163bf215546Sopenharmony_ci   case PIPE_FORMAT_R8G8B8A8_UNORM:
164bf215546Sopenharmony_ci   case PIPE_FORMAT_R8G8B8X8_UNORM:
165bf215546Sopenharmony_ci      return COLORX_8_8_8_8;
166bf215546Sopenharmony_ci   /* Note: snorm untested */
167bf215546Sopenharmony_ci   case PIPE_FORMAT_R8G8B8A8_SNORM:
168bf215546Sopenharmony_ci   case PIPE_FORMAT_R8G8B8X8_SNORM:
169bf215546Sopenharmony_ci      return COLORX_S8_8_8_8;
170bf215546Sopenharmony_ci
171bf215546Sopenharmony_ci   /* float buffers */
172bf215546Sopenharmony_ci   case PIPE_FORMAT_R16_FLOAT:
173bf215546Sopenharmony_ci      return COLORX_16_FLOAT;
174bf215546Sopenharmony_ci   case PIPE_FORMAT_R16G16_FLOAT:
175bf215546Sopenharmony_ci      return COLORX_16_16_FLOAT;
176bf215546Sopenharmony_ci   case PIPE_FORMAT_R16G16B16A16_FLOAT:
177bf215546Sopenharmony_ci      return COLORX_16_16_16_16_FLOAT;
178bf215546Sopenharmony_ci   case PIPE_FORMAT_R32_FLOAT:
179bf215546Sopenharmony_ci      return COLORX_32_FLOAT;
180bf215546Sopenharmony_ci   case PIPE_FORMAT_R32G32_FLOAT:
181bf215546Sopenharmony_ci      return COLORX_32_32_FLOAT;
182bf215546Sopenharmony_ci   case PIPE_FORMAT_R32G32B32A32_FLOAT:
183bf215546Sopenharmony_ci      return COLORX_32_32_32_32_FLOAT;
184bf215546Sopenharmony_ci
185bf215546Sopenharmony_ci   default:
186bf215546Sopenharmony_ci      return ~0;
187bf215546Sopenharmony_ci   }
188bf215546Sopenharmony_ci}
189bf215546Sopenharmony_ci
190bf215546Sopenharmony_cistatic inline enum sq_tex_swiz
191bf215546Sopenharmony_citex_swiz(unsigned swiz)
192bf215546Sopenharmony_ci{
193bf215546Sopenharmony_ci   switch (swiz) {
194bf215546Sopenharmony_ci   default:
195bf215546Sopenharmony_ci   case PIPE_SWIZZLE_X:
196bf215546Sopenharmony_ci      return SQ_TEX_X;
197bf215546Sopenharmony_ci   case PIPE_SWIZZLE_Y:
198bf215546Sopenharmony_ci      return SQ_TEX_Y;
199bf215546Sopenharmony_ci   case PIPE_SWIZZLE_Z:
200bf215546Sopenharmony_ci      return SQ_TEX_Z;
201bf215546Sopenharmony_ci   case PIPE_SWIZZLE_W:
202bf215546Sopenharmony_ci      return SQ_TEX_W;
203bf215546Sopenharmony_ci   case PIPE_SWIZZLE_0:
204bf215546Sopenharmony_ci      return SQ_TEX_ZERO;
205bf215546Sopenharmony_ci   case PIPE_SWIZZLE_1:
206bf215546Sopenharmony_ci      return SQ_TEX_ONE;
207bf215546Sopenharmony_ci   }
208bf215546Sopenharmony_ci}
209bf215546Sopenharmony_ci
210bf215546Sopenharmony_ciuint32_t
211bf215546Sopenharmony_cifd2_tex_swiz(enum pipe_format format, unsigned swizzle_r, unsigned swizzle_g,
212bf215546Sopenharmony_ci             unsigned swizzle_b, unsigned swizzle_a)
213bf215546Sopenharmony_ci{
214bf215546Sopenharmony_ci   const struct util_format_description *desc = util_format_description(format);
215bf215546Sopenharmony_ci   unsigned char swiz[4] = {
216bf215546Sopenharmony_ci      swizzle_r,
217bf215546Sopenharmony_ci      swizzle_g,
218bf215546Sopenharmony_ci      swizzle_b,
219bf215546Sopenharmony_ci      swizzle_a,
220bf215546Sopenharmony_ci   }, rswiz[4];
221bf215546Sopenharmony_ci
222bf215546Sopenharmony_ci   util_format_compose_swizzles(desc->swizzle, swiz, rswiz);
223bf215546Sopenharmony_ci
224bf215546Sopenharmony_ci   return A2XX_SQ_TEX_3_SWIZ_X(tex_swiz(rswiz[0])) |
225bf215546Sopenharmony_ci          A2XX_SQ_TEX_3_SWIZ_Y(tex_swiz(rswiz[1])) |
226bf215546Sopenharmony_ci          A2XX_SQ_TEX_3_SWIZ_Z(tex_swiz(rswiz[2])) |
227bf215546Sopenharmony_ci          A2XX_SQ_TEX_3_SWIZ_W(tex_swiz(rswiz[3]));
228bf215546Sopenharmony_ci}
229bf215546Sopenharmony_ci
230bf215546Sopenharmony_ciuint32_t
231bf215546Sopenharmony_cifd2_vtx_swiz(enum pipe_format format, unsigned swizzle)
232bf215546Sopenharmony_ci{
233bf215546Sopenharmony_ci   const struct util_format_description *desc = util_format_description(format);
234bf215546Sopenharmony_ci   unsigned char swiz[4], rswiz[4];
235bf215546Sopenharmony_ci
236bf215546Sopenharmony_ci   for (unsigned i = 0; i < 4; i++)
237bf215546Sopenharmony_ci      swiz[i] = (swizzle >> i * 3) & 7;
238bf215546Sopenharmony_ci
239bf215546Sopenharmony_ci   util_format_compose_swizzles(desc->swizzle, swiz, rswiz);
240bf215546Sopenharmony_ci
241bf215546Sopenharmony_ci   return rswiz[0] | rswiz[1] << 3 | rswiz[2] << 6 | rswiz[3] << 9;
242bf215546Sopenharmony_ci}
243