1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © Microsoft Corporation
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
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21bf215546Sopenharmony_ci * IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#include "d3d12_format.h"
25bf215546Sopenharmony_ci
26bf215546Sopenharmony_ci#include "pipe/p_format.h"
27bf215546Sopenharmony_ci#include "pipe/p_video_enums.h"
28bf215546Sopenharmony_ci#include "util/format/u_format.h"
29bf215546Sopenharmony_ci#include "util/u_math.h"
30bf215546Sopenharmony_ci#include "util/compiler.h"
31bf215546Sopenharmony_ci
32bf215546Sopenharmony_ci#define MAP_FORMAT_YUV(NAME) \
33bf215546Sopenharmony_ci   [PIPE_FORMAT_ ## NAME] = DXGI_FORMAT_ ## NAME,
34bf215546Sopenharmony_ci#define MAP_FORMAT_NO_TYPELESS(BITS, TYPE) \
35bf215546Sopenharmony_ci   [PIPE_FORMAT_ ## BITS ## _ ## TYPE] = DXGI_FORMAT_ ## BITS ## _ ## TYPE,
36bf215546Sopenharmony_ci#define MAP_FORMAT2_NO_TYPELESS(BITS1, TYPE1, BITS2, TYPE2) \
37bf215546Sopenharmony_ci   [PIPE_FORMAT_ ## BITS1 ## _ ## TYPE1] = DXGI_FORMAT_ ## BITS2 ## _ ## TYPE2,
38bf215546Sopenharmony_ci
39bf215546Sopenharmony_ci#define MAP_FORMAT(BITS, TYPE) MAP_FORMAT_NO_TYPELESS(BITS, TYPE)
40bf215546Sopenharmony_ci#define MAP_FORMAT2(BITS1, TYPE1, BITS2, TYPE2) \
41bf215546Sopenharmony_ci   MAP_FORMAT2_NO_TYPELESS(BITS1, TYPE1, BITS2, TYPE2)
42bf215546Sopenharmony_ci
43bf215546Sopenharmony_ci#define MAP_FORMAT_CUSTOM_TYPELESS(BITS, TYPE, TYPELESS_BITS) \
44bf215546Sopenharmony_ci   MAP_FORMAT(BITS, TYPE)
45bf215546Sopenharmony_ci#define MAP_FORMAT2_CUSTOM_TYPELESS(BITS1, TYPE1, BITS2, TYPE2) \
46bf215546Sopenharmony_ci   MAP_FORMAT2(BITS1, TYPE1, BITS2, TYPE2)
47bf215546Sopenharmony_ci
48bf215546Sopenharmony_ci#define MAP_FORMAT_NORM(FMT) \
49bf215546Sopenharmony_ci   MAP_FORMAT(FMT, UNORM) \
50bf215546Sopenharmony_ci   MAP_FORMAT(FMT, SNORM)
51bf215546Sopenharmony_ci
52bf215546Sopenharmony_ci#define MAP_FORMAT_INT(FMT) \
53bf215546Sopenharmony_ci   MAP_FORMAT(FMT, UINT) \
54bf215546Sopenharmony_ci   MAP_FORMAT(FMT, SINT)
55bf215546Sopenharmony_ci
56bf215546Sopenharmony_ci#define MAP_FORMAT_SRGB(FMT) \
57bf215546Sopenharmony_ci   MAP_FORMAT2(FMT, SRGB, FMT, UNORM_SRGB)
58bf215546Sopenharmony_ci
59bf215546Sopenharmony_ci#define MAP_FORMAT_FLOAT(FMT) \
60bf215546Sopenharmony_ci   MAP_FORMAT(FMT, FLOAT)
61bf215546Sopenharmony_ci
62bf215546Sopenharmony_ci#define MAP_EMU_FORMAT_NO_ALPHA(BITS, TYPE) \
63bf215546Sopenharmony_ci   MAP_FORMAT2(L ## BITS, TYPE, R ## BITS, TYPE) \
64bf215546Sopenharmony_ci   MAP_FORMAT2(I ## BITS, TYPE, R ## BITS, TYPE) \
65bf215546Sopenharmony_ci   MAP_FORMAT2(L ## BITS ## A ## BITS, TYPE, R ## BITS ## G ## BITS, TYPE)
66bf215546Sopenharmony_ci
67bf215546Sopenharmony_ci#define MAP_EMU_FORMAT(BITS, TYPE) \
68bf215546Sopenharmony_ci   MAP_FORMAT2(A ## BITS, TYPE, R ## BITS, TYPE) \
69bf215546Sopenharmony_ci   MAP_EMU_FORMAT_NO_ALPHA(BITS, TYPE)
70bf215546Sopenharmony_ci
71bf215546Sopenharmony_ci#define MAP_FORMAT_X8(BITS, TYPE) \
72bf215546Sopenharmony_ci   MAP_FORMAT2(BITS ## X8, TYPE, BITS ## A8, TYPE) \
73bf215546Sopenharmony_ci
74bf215546Sopenharmony_ci#define FORMAT_TABLE() \
75bf215546Sopenharmony_ci   MAP_FORMAT_NORM(R8) \
76bf215546Sopenharmony_ci   MAP_FORMAT_INT(R8) \
77bf215546Sopenharmony_ci\
78bf215546Sopenharmony_ci   MAP_FORMAT_NORM(R8G8) \
79bf215546Sopenharmony_ci   MAP_FORMAT_INT(R8G8) \
80bf215546Sopenharmony_ci\
81bf215546Sopenharmony_ci   MAP_FORMAT_NORM(R8G8B8A8) \
82bf215546Sopenharmony_ci   MAP_FORMAT_INT(R8G8B8A8) \
83bf215546Sopenharmony_ci   MAP_FORMAT_SRGB(R8G8B8A8) \
84bf215546Sopenharmony_ci\
85bf215546Sopenharmony_ci   /* Since we report PIPE_CAP_RGB_OVERRIDE_DST_ALPHA_BLEND and other caps, \
86bf215546Sopenharmony_ci    * we can rely on st/mesa to force the alpha to 1 for these, so we can \
87bf215546Sopenharmony_ci    * just use RGBA. This is needed to support RGB configs, since some apps \
88bf215546Sopenharmony_ci    * will only choose RGB (not RGBA) configs. \
89bf215546Sopenharmony_ci    */ \
90bf215546Sopenharmony_ci   MAP_FORMAT_X8(R8G8B8, UNORM) \
91bf215546Sopenharmony_ci   MAP_FORMAT_X8(R8G8B8, SNORM) \
92bf215546Sopenharmony_ci   MAP_FORMAT_X8(R8G8B8, UINT) \
93bf215546Sopenharmony_ci   MAP_FORMAT_X8(R8G8B8, SINT) \
94bf215546Sopenharmony_ci\
95bf215546Sopenharmony_ci   MAP_FORMAT(B8G8R8X8, UNORM) \
96bf215546Sopenharmony_ci   MAP_FORMAT(B8G8R8A8, UNORM) \
97bf215546Sopenharmony_ci\
98bf215546Sopenharmony_ci   MAP_FORMAT_SRGB(B8G8R8A8) \
99bf215546Sopenharmony_ci\
100bf215546Sopenharmony_ci   MAP_FORMAT_INT(R32) \
101bf215546Sopenharmony_ci   MAP_FORMAT_FLOAT(R32) \
102bf215546Sopenharmony_ci   MAP_FORMAT_INT(R32G32) \
103bf215546Sopenharmony_ci   MAP_FORMAT_FLOAT(R32G32) \
104bf215546Sopenharmony_ci   MAP_FORMAT_INT(R32G32B32) \
105bf215546Sopenharmony_ci   MAP_FORMAT_FLOAT(R32G32B32) \
106bf215546Sopenharmony_ci   MAP_FORMAT_INT(R32G32B32A32) \
107bf215546Sopenharmony_ci   MAP_FORMAT_FLOAT(R32G32B32A32) \
108bf215546Sopenharmony_ci\
109bf215546Sopenharmony_ci   MAP_FORMAT_NORM(R16) \
110bf215546Sopenharmony_ci   MAP_FORMAT_INT(R16) \
111bf215546Sopenharmony_ci   MAP_FORMAT_FLOAT(R16) \
112bf215546Sopenharmony_ci\
113bf215546Sopenharmony_ci   MAP_FORMAT_NORM(R16G16) \
114bf215546Sopenharmony_ci   MAP_FORMAT_INT(R16G16) \
115bf215546Sopenharmony_ci   MAP_FORMAT_FLOAT(R16G16) \
116bf215546Sopenharmony_ci\
117bf215546Sopenharmony_ci   MAP_FORMAT_NORM(R16G16B16A16) \
118bf215546Sopenharmony_ci   MAP_FORMAT_INT(R16G16B16A16) \
119bf215546Sopenharmony_ci   MAP_FORMAT_FLOAT(R16G16B16A16) \
120bf215546Sopenharmony_ci\
121bf215546Sopenharmony_ci   MAP_FORMAT_NO_TYPELESS(A8, UNORM) \
122bf215546Sopenharmony_ci   MAP_EMU_FORMAT_NO_ALPHA(8, UNORM) \
123bf215546Sopenharmony_ci   MAP_EMU_FORMAT(8, SNORM) \
124bf215546Sopenharmony_ci   MAP_EMU_FORMAT(8, SINT) \
125bf215546Sopenharmony_ci   MAP_EMU_FORMAT(8, UINT) \
126bf215546Sopenharmony_ci   MAP_EMU_FORMAT(16, UNORM) \
127bf215546Sopenharmony_ci   MAP_EMU_FORMAT(16, SNORM) \
128bf215546Sopenharmony_ci   MAP_EMU_FORMAT(16, SINT) \
129bf215546Sopenharmony_ci   MAP_EMU_FORMAT(16, UINT) \
130bf215546Sopenharmony_ci   MAP_EMU_FORMAT(16, FLOAT) \
131bf215546Sopenharmony_ci   MAP_EMU_FORMAT(32, SINT) \
132bf215546Sopenharmony_ci   MAP_EMU_FORMAT(32, UINT) \
133bf215546Sopenharmony_ci   MAP_EMU_FORMAT(32, FLOAT) \
134bf215546Sopenharmony_ci\
135bf215546Sopenharmony_ci   MAP_FORMAT2_NO_TYPELESS(R9G9B9E5, FLOAT, R9G9B9E5, SHAREDEXP) \
136bf215546Sopenharmony_ci   MAP_FORMAT_NO_TYPELESS(R11G11B10, FLOAT) \
137bf215546Sopenharmony_ci   MAP_FORMAT(R10G10B10A2, UINT) \
138bf215546Sopenharmony_ci   MAP_FORMAT(R10G10B10A2, UNORM) \
139bf215546Sopenharmony_ci\
140bf215546Sopenharmony_ci   MAP_FORMAT_NO_TYPELESS(B5G6R5, UNORM) \
141bf215546Sopenharmony_ci   MAP_FORMAT_NO_TYPELESS(B5G5R5A1, UNORM) \
142bf215546Sopenharmony_ci   MAP_FORMAT2_NO_TYPELESS(B5G5R5X1, UNORM, B5G5R5A1, UNORM) \
143bf215546Sopenharmony_ci\
144bf215546Sopenharmony_ci   MAP_FORMAT_NO_TYPELESS(B4G4R4A4, UNORM) \
145bf215546Sopenharmony_ci\
146bf215546Sopenharmony_ci   MAP_FORMAT2(DXT1, RGB, BC1, UNORM) \
147bf215546Sopenharmony_ci   MAP_FORMAT2(DXT1, RGBA, BC1, UNORM) \
148bf215546Sopenharmony_ci   MAP_FORMAT2(DXT3, RGBA, BC2, UNORM) \
149bf215546Sopenharmony_ci   MAP_FORMAT2(DXT5, RGBA, BC3, UNORM) \
150bf215546Sopenharmony_ci\
151bf215546Sopenharmony_ci   MAP_FORMAT2(DXT1, SRGB, BC1, UNORM_SRGB) \
152bf215546Sopenharmony_ci   MAP_FORMAT2(DXT1, SRGBA, BC1, UNORM_SRGB) \
153bf215546Sopenharmony_ci   MAP_FORMAT2(DXT3, SRGBA, BC2, UNORM_SRGB) \
154bf215546Sopenharmony_ci   MAP_FORMAT2(DXT5, SRGBA, BC3, UNORM_SRGB) \
155bf215546Sopenharmony_ci\
156bf215546Sopenharmony_ci   MAP_FORMAT2(RGTC1, UNORM, BC4, UNORM) \
157bf215546Sopenharmony_ci   MAP_FORMAT2(RGTC1, SNORM, BC4, SNORM) \
158bf215546Sopenharmony_ci   MAP_FORMAT2(RGTC2, UNORM, BC5, UNORM) \
159bf215546Sopenharmony_ci   MAP_FORMAT2(RGTC2, SNORM, BC5, SNORM) \
160bf215546Sopenharmony_ci\
161bf215546Sopenharmony_ci   MAP_FORMAT2(BPTC, RGBA_UNORM, BC7, UNORM) \
162bf215546Sopenharmony_ci   MAP_FORMAT2(BPTC, SRGBA, BC7, UNORM_SRGB) \
163bf215546Sopenharmony_ci   MAP_FORMAT2(BPTC, RGB_FLOAT, BC6H, SF16) \
164bf215546Sopenharmony_ci   MAP_FORMAT2(BPTC, RGB_UFLOAT, BC6H, UF16) \
165bf215546Sopenharmony_ci\
166bf215546Sopenharmony_ci   MAP_FORMAT2(Z32, FLOAT, R32, TYPELESS) \
167bf215546Sopenharmony_ci   MAP_FORMAT2(Z16, UNORM, R16, TYPELESS) \
168bf215546Sopenharmony_ci   MAP_FORMAT2(Z24X8, UNORM, R24G8, TYPELESS) \
169bf215546Sopenharmony_ci   MAP_FORMAT2(X24S8, UINT, R24G8, TYPELESS) \
170bf215546Sopenharmony_ci\
171bf215546Sopenharmony_ci   MAP_FORMAT2(Z24_UNORM_S8, UINT, R24G8, TYPELESS) \
172bf215546Sopenharmony_ci   MAP_FORMAT2(Z32_FLOAT_S8X24, UINT, R32G8X24, TYPELESS) \
173bf215546Sopenharmony_ci   MAP_FORMAT2(X32_S8X24, UINT, R32G8X24, TYPELESS) \
174bf215546Sopenharmony_ci\
175bf215546Sopenharmony_ci   MAP_FORMAT_YUV(NV12)
176bf215546Sopenharmony_ci
177bf215546Sopenharmony_cistatic const DXGI_FORMAT formats[PIPE_FORMAT_COUNT] = {
178bf215546Sopenharmony_ci   FORMAT_TABLE()
179bf215546Sopenharmony_ci};
180bf215546Sopenharmony_ci
181bf215546Sopenharmony_ci#undef MAP_FORMAT
182bf215546Sopenharmony_ci#undef MAP_FORMAT2
183bf215546Sopenharmony_ci#undef MAP_FORMAT_CUSTOM_TYPELESS
184bf215546Sopenharmony_ci#undef MAP_FORMAT2_CUSTOM_TYPELESS
185bf215546Sopenharmony_ci
186bf215546Sopenharmony_ci#define MAP_FORMAT(BITS, TYPE) \
187bf215546Sopenharmony_ci   [PIPE_FORMAT_ ## BITS ## _ ## TYPE] = DXGI_FORMAT_ ## BITS ## _TYPELESS,
188bf215546Sopenharmony_ci
189bf215546Sopenharmony_ci#define MAP_FORMAT2(BITS1, TYPE1, BITS2, TYPE2) \
190bf215546Sopenharmony_ci   [PIPE_FORMAT_ ## BITS1 ## _ ## TYPE1] = DXGI_FORMAT_ ## BITS2 ## _TYPELESS,
191bf215546Sopenharmony_ci
192bf215546Sopenharmony_ci#define MAP_FORMAT_CUSTOM_TYPELESS(BITS1, TYPE, BITS2) \
193bf215546Sopenharmony_ci   MAP_FORMAT2(BITS1, TYPE, BITS2, TYPELESS)
194bf215546Sopenharmony_ci
195bf215546Sopenharmony_ci#define MAP_FORMAT2_CUSTOM_TYPELESS(BITS1, TYPE1, BITS2, TYPE2) \
196bf215546Sopenharmony_ci   MAP_FORMAT2(BITS1, TYPE1, BITS2, TYPELESS)
197bf215546Sopenharmony_ci
198bf215546Sopenharmony_cistatic const DXGI_FORMAT typeless_formats[PIPE_FORMAT_COUNT] = {
199bf215546Sopenharmony_ci   FORMAT_TABLE()
200bf215546Sopenharmony_ci};
201bf215546Sopenharmony_ci
202bf215546Sopenharmony_ciDXGI_FORMAT
203bf215546Sopenharmony_cid3d12_get_format(enum pipe_format format)
204bf215546Sopenharmony_ci{
205bf215546Sopenharmony_ci   return formats[format];
206bf215546Sopenharmony_ci}
207bf215546Sopenharmony_ci
208bf215546Sopenharmony_ciDXGI_FORMAT
209bf215546Sopenharmony_cid3d12_get_typeless_format(enum pipe_format format)
210bf215546Sopenharmony_ci{
211bf215546Sopenharmony_ci   return typeless_formats[format];
212bf215546Sopenharmony_ci}
213bf215546Sopenharmony_ci
214bf215546Sopenharmony_cienum pipe_format
215bf215546Sopenharmony_cid3d12_get_pipe_format(DXGI_FORMAT format)
216bf215546Sopenharmony_ci{
217bf215546Sopenharmony_ci   for (unsigned i = 0; i < ARRAY_SIZE(formats); ++i) {
218bf215546Sopenharmony_ci      if (formats[i] == format) {
219bf215546Sopenharmony_ci         return (enum pipe_format)i;
220bf215546Sopenharmony_ci      }
221bf215546Sopenharmony_ci   }
222bf215546Sopenharmony_ci   return PIPE_FORMAT_NONE;
223bf215546Sopenharmony_ci}
224bf215546Sopenharmony_ci
225bf215546Sopenharmony_cienum pipe_format
226bf215546Sopenharmony_cid3d12_get_default_pipe_format(DXGI_FORMAT format)
227bf215546Sopenharmony_ci{
228bf215546Sopenharmony_ci#define TYPELESS_TO(channels, suffix) \
229bf215546Sopenharmony_ci   case DXGI_FORMAT_##channels##_TYPELESS: \
230bf215546Sopenharmony_ci      return PIPE_FORMAT_##channels##_##suffix
231bf215546Sopenharmony_ci
232bf215546Sopenharmony_ci   switch (format) {
233bf215546Sopenharmony_ci      TYPELESS_TO(R8, UNORM);
234bf215546Sopenharmony_ci      TYPELESS_TO(R8G8, UNORM);
235bf215546Sopenharmony_ci      TYPELESS_TO(R8G8B8A8, UNORM);
236bf215546Sopenharmony_ci      TYPELESS_TO(B8G8R8X8, UNORM);
237bf215546Sopenharmony_ci      TYPELESS_TO(B8G8R8A8, UNORM);
238bf215546Sopenharmony_ci      TYPELESS_TO(R16, FLOAT);
239bf215546Sopenharmony_ci      TYPELESS_TO(R16G16, FLOAT);
240bf215546Sopenharmony_ci      TYPELESS_TO(R16G16B16A16, FLOAT);
241bf215546Sopenharmony_ci      TYPELESS_TO(R32, FLOAT);
242bf215546Sopenharmony_ci      TYPELESS_TO(R32G32, FLOAT);
243bf215546Sopenharmony_ci      TYPELESS_TO(R32G32B32, FLOAT);
244bf215546Sopenharmony_ci      TYPELESS_TO(R32G32B32A32, FLOAT);
245bf215546Sopenharmony_ci   case DXGI_FORMAT_BC1_TYPELESS:
246bf215546Sopenharmony_ci      return PIPE_FORMAT_DXT1_RGBA;
247bf215546Sopenharmony_ci   case DXGI_FORMAT_BC2_TYPELESS:
248bf215546Sopenharmony_ci      return PIPE_FORMAT_DXT3_RGBA;
249bf215546Sopenharmony_ci   case DXGI_FORMAT_BC3_TYPELESS:
250bf215546Sopenharmony_ci      return PIPE_FORMAT_DXT5_RGBA;
251bf215546Sopenharmony_ci   case DXGI_FORMAT_BC4_TYPELESS:
252bf215546Sopenharmony_ci      return PIPE_FORMAT_RGTC1_UNORM;
253bf215546Sopenharmony_ci   case DXGI_FORMAT_BC5_TYPELESS:
254bf215546Sopenharmony_ci      return PIPE_FORMAT_RGTC2_UNORM;
255bf215546Sopenharmony_ci   case DXGI_FORMAT_BC6H_TYPELESS:
256bf215546Sopenharmony_ci      return PIPE_FORMAT_BPTC_RGB_FLOAT;
257bf215546Sopenharmony_ci   case DXGI_FORMAT_BC7_TYPELESS:
258bf215546Sopenharmony_ci      return PIPE_FORMAT_BPTC_RGBA_UNORM;
259bf215546Sopenharmony_ci   default:
260bf215546Sopenharmony_ci      return PIPE_FORMAT_NONE;
261bf215546Sopenharmony_ci   }
262bf215546Sopenharmony_ci}
263bf215546Sopenharmony_ci
264bf215546Sopenharmony_ciDXGI_FORMAT
265bf215546Sopenharmony_cid3d12_get_resource_rt_format(enum pipe_format f)
266bf215546Sopenharmony_ci{
267bf215546Sopenharmony_ci   switch (f) {
268bf215546Sopenharmony_ci   case PIPE_FORMAT_Z16_UNORM:
269bf215546Sopenharmony_ci      return DXGI_FORMAT_D16_UNORM;
270bf215546Sopenharmony_ci   case PIPE_FORMAT_Z32_FLOAT:
271bf215546Sopenharmony_ci      return DXGI_FORMAT_D32_FLOAT;
272bf215546Sopenharmony_ci   case PIPE_FORMAT_Z24X8_UNORM:
273bf215546Sopenharmony_ci   case PIPE_FORMAT_X24S8_UINT:
274bf215546Sopenharmony_ci      return DXGI_FORMAT_D24_UNORM_S8_UINT;
275bf215546Sopenharmony_ci   case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
276bf215546Sopenharmony_ci   case PIPE_FORMAT_X32_S8X24_UINT:
277bf215546Sopenharmony_ci      return DXGI_FORMAT_D32_FLOAT_S8X24_UINT;
278bf215546Sopenharmony_ci   case PIPE_FORMAT_Z24_UNORM_S8_UINT:
279bf215546Sopenharmony_ci      return DXGI_FORMAT_D24_UNORM_S8_UINT;
280bf215546Sopenharmony_ci   default:
281bf215546Sopenharmony_ci      return d3d12_get_format(f);
282bf215546Sopenharmony_ci   }
283bf215546Sopenharmony_ci}
284bf215546Sopenharmony_ci
285bf215546Sopenharmony_ciDXGI_FORMAT
286bf215546Sopenharmony_cid3d12_get_resource_srv_format(enum pipe_format f, enum pipe_texture_target target)
287bf215546Sopenharmony_ci{
288bf215546Sopenharmony_ci   switch (f) {
289bf215546Sopenharmony_ci   case PIPE_FORMAT_Z16_UNORM:
290bf215546Sopenharmony_ci      return DXGI_FORMAT_R16_UNORM;
291bf215546Sopenharmony_ci   case PIPE_FORMAT_Z32_FLOAT:
292bf215546Sopenharmony_ci      return DXGI_FORMAT_R32_FLOAT;
293bf215546Sopenharmony_ci   case PIPE_FORMAT_Z24X8_UNORM:
294bf215546Sopenharmony_ci   case PIPE_FORMAT_Z24_UNORM_S8_UINT:
295bf215546Sopenharmony_ci      return DXGI_FORMAT_R24_UNORM_X8_TYPELESS;
296bf215546Sopenharmony_ci   case PIPE_FORMAT_X24S8_UINT:
297bf215546Sopenharmony_ci      return DXGI_FORMAT_X24_TYPELESS_G8_UINT;
298bf215546Sopenharmony_ci   case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
299bf215546Sopenharmony_ci      return DXGI_FORMAT_R32_FLOAT_X8X24_TYPELESS;
300bf215546Sopenharmony_ci   case PIPE_FORMAT_X32_S8X24_UINT:
301bf215546Sopenharmony_ci      return DXGI_FORMAT_X32_TYPELESS_G8X24_UINT;
302bf215546Sopenharmony_ci   case PIPE_FORMAT_A8_UNORM:
303bf215546Sopenharmony_ci      if (target == PIPE_BUFFER)
304bf215546Sopenharmony_ci         return DXGI_FORMAT_R8_UNORM; /* A8_UNORM is not supported for buffer SRV */
305bf215546Sopenharmony_ci      FALLTHROUGH;
306bf215546Sopenharmony_ci   default:
307bf215546Sopenharmony_ci      return d3d12_get_format(f);
308bf215546Sopenharmony_ci   }
309bf215546Sopenharmony_ci}
310bf215546Sopenharmony_ci
311bf215546Sopenharmony_ci#define DEF_SWIZZLE(name, X, Y, Z, W) \
312bf215546Sopenharmony_ci   static const enum pipe_swizzle name ## _SWIZZLE[PIPE_SWIZZLE_MAX] = \
313bf215546Sopenharmony_ci      { PIPE_SWIZZLE_ ## X, PIPE_SWIZZLE_ ## Y, PIPE_SWIZZLE_ ## Z, PIPE_SWIZZLE_ ## W, \
314bf215546Sopenharmony_ci        PIPE_SWIZZLE_0, PIPE_SWIZZLE_1, PIPE_SWIZZLE_NONE }
315bf215546Sopenharmony_ci
316bf215546Sopenharmony_cistruct d3d12_format_info
317bf215546Sopenharmony_cid3d12_get_format_info(enum pipe_format resource_format, enum pipe_format pformat, enum pipe_texture_target target)
318bf215546Sopenharmony_ci{
319bf215546Sopenharmony_ci   DEF_SWIZZLE(IDENTITY, X, Y, Z, W);
320bf215546Sopenharmony_ci   DEF_SWIZZLE(RGB1, X, Y, Z, 1);
321bf215546Sopenharmony_ci   DEF_SWIZZLE(ALPHA, 0, 0, 0, W);
322bf215546Sopenharmony_ci   DEF_SWIZZLE(BUFFER, 0, 0, 0, X);
323bf215546Sopenharmony_ci   DEF_SWIZZLE(INTENSITY, X, X, X, X);
324bf215546Sopenharmony_ci   DEF_SWIZZLE(LUMINANCE, X, X, X, 1);
325bf215546Sopenharmony_ci   DEF_SWIZZLE(LUMINANCE_ALPHA, X, X, X, Y);
326bf215546Sopenharmony_ci   DEF_SWIZZLE(DEPTH, X, X, X, X);
327bf215546Sopenharmony_ci   DEF_SWIZZLE(STENCIL, Y, Y, Y, Y);
328bf215546Sopenharmony_ci
329bf215546Sopenharmony_ci   const enum pipe_swizzle *swizzle = IDENTITY_SWIZZLE;
330bf215546Sopenharmony_ci   unsigned plane_slice = 0;
331bf215546Sopenharmony_ci
332bf215546Sopenharmony_ci   if (pformat == PIPE_FORMAT_DXT1_RGB ||
333bf215546Sopenharmony_ci       pformat == PIPE_FORMAT_DXT1_SRGB)
334bf215546Sopenharmony_ci      swizzle = RGB1_SWIZZLE;
335bf215546Sopenharmony_ci
336bf215546Sopenharmony_ci   const struct util_format_description
337bf215546Sopenharmony_ci      *format_desc = util_format_description(pformat);
338bf215546Sopenharmony_ci   unsigned plane_count = util_format_get_num_planes(resource_format);
339bf215546Sopenharmony_ci   if (!util_format_is_srgb(pformat)) {
340bf215546Sopenharmony_ci      if (target == PIPE_BUFFER && util_format_is_alpha(pformat)) {
341bf215546Sopenharmony_ci         swizzle = BUFFER_SWIZZLE;
342bf215546Sopenharmony_ci      } else if (plane_count > 1) {
343bf215546Sopenharmony_ci         for (plane_slice = 0; plane_slice < plane_count; ++plane_slice) {
344bf215546Sopenharmony_ci            if (util_format_get_plane_format(resource_format, plane_slice) == pformat)
345bf215546Sopenharmony_ci               break;
346bf215546Sopenharmony_ci         }
347bf215546Sopenharmony_ci         assert(plane_slice < plane_count);
348bf215546Sopenharmony_ci      } else if (pformat == PIPE_FORMAT_A8_UNORM) {
349bf215546Sopenharmony_ci         /* no need to swizzle, it's natively supported */
350bf215546Sopenharmony_ci      } else if (util_format_is_intensity(pformat)) {
351bf215546Sopenharmony_ci         swizzle = INTENSITY_SWIZZLE;
352bf215546Sopenharmony_ci      } else if (util_format_is_luminance(pformat)) {
353bf215546Sopenharmony_ci         swizzle = LUMINANCE_SWIZZLE;
354bf215546Sopenharmony_ci      } else if (util_format_is_luminance_alpha(pformat)) {
355bf215546Sopenharmony_ci         swizzle = LUMINANCE_ALPHA_SWIZZLE;
356bf215546Sopenharmony_ci      } else if (util_format_is_alpha(pformat)) {
357bf215546Sopenharmony_ci         swizzle = ALPHA_SWIZZLE;
358bf215546Sopenharmony_ci      } else if (util_format_has_depth(format_desc)) {
359bf215546Sopenharmony_ci         swizzle = DEPTH_SWIZZLE;
360bf215546Sopenharmony_ci      } else if (util_format_has_stencil(format_desc)) {
361bf215546Sopenharmony_ci         /* When reading from a stencil texture we have to use plane 1, and
362bf215546Sopenharmony_ci          * the formats X24S8 and X32_S8X24 have the actual data in the y-channel
363bf215546Sopenharmony_ci          * but the shader will read the x component so we need to adjust the swizzle. */
364bf215546Sopenharmony_ci         plane_slice = 1;
365bf215546Sopenharmony_ci         swizzle = STENCIL_SWIZZLE;
366bf215546Sopenharmony_ci      } else if (util_format_has_alpha1(pformat)) {
367bf215546Sopenharmony_ci         swizzle = RGB1_SWIZZLE;
368bf215546Sopenharmony_ci      }
369bf215546Sopenharmony_ci   }
370bf215546Sopenharmony_ci
371bf215546Sopenharmony_ci   return (struct d3d12_format_info) { .swizzle = swizzle, .plane_slice = plane_slice };
372bf215546Sopenharmony_ci}
373bf215546Sopenharmony_ci
374bf215546Sopenharmony_cienum pipe_format
375bf215546Sopenharmony_cid3d12_emulated_vtx_format(enum pipe_format fmt)
376bf215546Sopenharmony_ci{
377bf215546Sopenharmony_ci   switch (fmt) {
378bf215546Sopenharmony_ci   case PIPE_FORMAT_R10G10B10A2_SNORM:
379bf215546Sopenharmony_ci   case PIPE_FORMAT_R10G10B10A2_SSCALED:
380bf215546Sopenharmony_ci   case PIPE_FORMAT_R10G10B10A2_USCALED:
381bf215546Sopenharmony_ci   case PIPE_FORMAT_B10G10R10A2_UNORM:
382bf215546Sopenharmony_ci   case PIPE_FORMAT_B10G10R10A2_SNORM:
383bf215546Sopenharmony_ci   case PIPE_FORMAT_B10G10R10A2_SSCALED:
384bf215546Sopenharmony_ci   case PIPE_FORMAT_B10G10R10A2_USCALED:
385bf215546Sopenharmony_ci      return PIPE_FORMAT_R32_UINT;
386bf215546Sopenharmony_ci
387bf215546Sopenharmony_ci   case PIPE_FORMAT_R8G8B8_SINT:
388bf215546Sopenharmony_ci      return PIPE_FORMAT_R8G8B8A8_SINT;
389bf215546Sopenharmony_ci   case PIPE_FORMAT_R8G8B8_UINT:
390bf215546Sopenharmony_ci      return PIPE_FORMAT_R8G8B8A8_UINT;
391bf215546Sopenharmony_ci
392bf215546Sopenharmony_ci   case PIPE_FORMAT_R16G16B16_SINT:
393bf215546Sopenharmony_ci      return PIPE_FORMAT_R16G16B16A16_SINT;
394bf215546Sopenharmony_ci   case PIPE_FORMAT_R16G16B16_UINT:
395bf215546Sopenharmony_ci      return PIPE_FORMAT_R16G16B16A16_UINT;
396bf215546Sopenharmony_ci
397bf215546Sopenharmony_ci   case PIPE_FORMAT_R8G8B8A8_SSCALED:
398bf215546Sopenharmony_ci      return PIPE_FORMAT_R8G8B8A8_SINT;
399bf215546Sopenharmony_ci   case PIPE_FORMAT_R8G8B8A8_USCALED:
400bf215546Sopenharmony_ci      return PIPE_FORMAT_R8G8B8A8_UINT;
401bf215546Sopenharmony_ci   case PIPE_FORMAT_R16G16B16A16_SSCALED:
402bf215546Sopenharmony_ci      return PIPE_FORMAT_R16G16B16A16_SINT;
403bf215546Sopenharmony_ci   case PIPE_FORMAT_R16G16B16A16_USCALED:
404bf215546Sopenharmony_ci      return PIPE_FORMAT_R16G16B16A16_UINT;
405bf215546Sopenharmony_ci
406bf215546Sopenharmony_ci   default:
407bf215546Sopenharmony_ci      return fmt;
408bf215546Sopenharmony_ci   }
409bf215546Sopenharmony_ci}
410bf215546Sopenharmony_ci
411bf215546Sopenharmony_ci
412bf215546Sopenharmony_ciunsigned
413bf215546Sopenharmony_cid3d12_non_opaque_plane_count(DXGI_FORMAT format)
414bf215546Sopenharmony_ci{
415bf215546Sopenharmony_ci   switch (format) {
416bf215546Sopenharmony_ci   case DXGI_FORMAT_V208:
417bf215546Sopenharmony_ci   case DXGI_FORMAT_V408:
418bf215546Sopenharmony_ci      return 3;
419bf215546Sopenharmony_ci
420bf215546Sopenharmony_ci   case DXGI_FORMAT_NV12:
421bf215546Sopenharmony_ci   case DXGI_FORMAT_P010:
422bf215546Sopenharmony_ci   case DXGI_FORMAT_P016:
423bf215546Sopenharmony_ci   case DXGI_FORMAT_YUY2:
424bf215546Sopenharmony_ci   case DXGI_FORMAT_Y210:
425bf215546Sopenharmony_ci   case DXGI_FORMAT_Y216:
426bf215546Sopenharmony_ci   case DXGI_FORMAT_NV11:
427bf215546Sopenharmony_ci      return 2;
428bf215546Sopenharmony_ci
429bf215546Sopenharmony_ci   case DXGI_FORMAT_R24G8_TYPELESS:
430bf215546Sopenharmony_ci   case DXGI_FORMAT_R24_UNORM_X8_TYPELESS:
431bf215546Sopenharmony_ci   case DXGI_FORMAT_X24_TYPELESS_G8_UINT:
432bf215546Sopenharmony_ci   case DXGI_FORMAT_D24_UNORM_S8_UINT:
433bf215546Sopenharmony_ci   case DXGI_FORMAT_R32G8X24_TYPELESS:
434bf215546Sopenharmony_ci   case DXGI_FORMAT_R32_FLOAT_X8X24_TYPELESS:
435bf215546Sopenharmony_ci   case DXGI_FORMAT_X32_TYPELESS_G8X24_UINT:
436bf215546Sopenharmony_ci   case DXGI_FORMAT_D32_FLOAT_S8X24_UINT:
437bf215546Sopenharmony_ci      return 2;
438bf215546Sopenharmony_ci
439bf215546Sopenharmony_ci   default:
440bf215546Sopenharmony_ci      return 1;
441bf215546Sopenharmony_ci   }
442bf215546Sopenharmony_ci}
443bf215546Sopenharmony_ci
444bf215546Sopenharmony_ciunsigned
445bf215546Sopenharmony_cid3d12_get_format_start_plane(enum pipe_format fmt)
446bf215546Sopenharmony_ci{
447bf215546Sopenharmony_ci   const struct util_format_description *desc = util_format_description(fmt);
448bf215546Sopenharmony_ci   if (util_format_has_stencil(desc) && !util_format_has_depth(desc))
449bf215546Sopenharmony_ci      return 1;
450bf215546Sopenharmony_ci
451bf215546Sopenharmony_ci   return 0;
452bf215546Sopenharmony_ci}
453bf215546Sopenharmony_ci
454bf215546Sopenharmony_ciunsigned
455bf215546Sopenharmony_cid3d12_get_format_num_planes(enum pipe_format fmt)
456bf215546Sopenharmony_ci{
457bf215546Sopenharmony_ci   return util_format_is_depth_or_stencil(fmt) ?
458bf215546Sopenharmony_ci      util_bitcount(util_format_get_mask(fmt)) : 1;
459bf215546Sopenharmony_ci}
460bf215546Sopenharmony_ci
461bf215546Sopenharmony_ciDXGI_FORMAT
462bf215546Sopenharmony_cid3d12_convert_pipe_video_profile_to_dxgi_format(enum pipe_video_profile profile)
463bf215546Sopenharmony_ci{
464bf215546Sopenharmony_ci   switch (profile) {
465bf215546Sopenharmony_ci      case PIPE_VIDEO_PROFILE_MPEG4_AVC_BASELINE:
466bf215546Sopenharmony_ci      case PIPE_VIDEO_PROFILE_MPEG4_AVC_CONSTRAINED_BASELINE:
467bf215546Sopenharmony_ci      case PIPE_VIDEO_PROFILE_MPEG4_AVC_MAIN:
468bf215546Sopenharmony_ci      case PIPE_VIDEO_PROFILE_MPEG4_AVC_EXTENDED:
469bf215546Sopenharmony_ci      case PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH:
470bf215546Sopenharmony_ci         return DXGI_FORMAT_NV12;
471bf215546Sopenharmony_ci      case PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH10:
472bf215546Sopenharmony_ci         return DXGI_FORMAT_P010;
473bf215546Sopenharmony_ci      default:
474bf215546Sopenharmony_ci      {
475bf215546Sopenharmony_ci         unreachable("Unsupported pipe video profile");
476bf215546Sopenharmony_ci      } break;
477bf215546Sopenharmony_ci   }
478bf215546Sopenharmony_ci}
479bf215546Sopenharmony_ci
480bf215546Sopenharmony_ciDXGI_COLOR_SPACE_TYPE
481bf215546Sopenharmony_cid3d12_convert_from_legacy_color_space(bool rgb, uint32_t bits_per_element, bool studio_rgb, bool p709, bool studio_yuv)
482bf215546Sopenharmony_ci{
483bf215546Sopenharmony_ci   if (rgb) {
484bf215546Sopenharmony_ci      if (bits_per_element > 32) {
485bf215546Sopenharmony_ci         // All 16 bit color channel data is assumed to be linear rather than SRGB
486bf215546Sopenharmony_ci         return DXGI_COLOR_SPACE_RGB_FULL_G10_NONE_P709;
487bf215546Sopenharmony_ci      } else {
488bf215546Sopenharmony_ci         if (studio_rgb) {
489bf215546Sopenharmony_ci            return DXGI_COLOR_SPACE_RGB_STUDIO_G22_NONE_P709;
490bf215546Sopenharmony_ci         } else {
491bf215546Sopenharmony_ci            return DXGI_COLOR_SPACE_RGB_FULL_G22_NONE_P709;
492bf215546Sopenharmony_ci         }
493bf215546Sopenharmony_ci      }
494bf215546Sopenharmony_ci   } else {
495bf215546Sopenharmony_ci      if (p709) {
496bf215546Sopenharmony_ci         if (studio_yuv) {
497bf215546Sopenharmony_ci            return DXGI_COLOR_SPACE_YCBCR_STUDIO_G22_LEFT_P709;
498bf215546Sopenharmony_ci         } else {
499bf215546Sopenharmony_ci            return DXGI_COLOR_SPACE_YCBCR_FULL_G22_LEFT_P709;
500bf215546Sopenharmony_ci         }
501bf215546Sopenharmony_ci      } else {
502bf215546Sopenharmony_ci         if (studio_yuv) {
503bf215546Sopenharmony_ci            return DXGI_COLOR_SPACE_YCBCR_STUDIO_G22_LEFT_P601;
504bf215546Sopenharmony_ci         } else {
505bf215546Sopenharmony_ci            return DXGI_COLOR_SPACE_YCBCR_FULL_G22_LEFT_P601;
506bf215546Sopenharmony_ci         }
507bf215546Sopenharmony_ci      }
508bf215546Sopenharmony_ci   }
509bf215546Sopenharmony_ci}
510