xref: /aosp_15_r20/external/mesa3d/src/microsoft/compiler/dxil_nir_lower_vs_vertex_conversion.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © Microsoft Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * on the rights to use, copy, modify, merge, publish, distribute, sub
8  * license, and/or sell copies of the Software, and to permit persons to whom
9  * the Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18  * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21  * USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 
24 #include "dxil_nir.h"
25 
26 #include "nir_builder.h"
27 #include "nir_builtin_builder.h"
28 
29 static enum pipe_format
get_input_target_format(nir_variable * var,const void * options)30 get_input_target_format(nir_variable *var, const void *options)
31 {
32    enum pipe_format *target_formats = (enum pipe_format *)options;
33    return target_formats[var->data.driver_location];
34 }
35 
36 static bool
lower_vs_vertex_conversion_filter(const nir_instr * instr,const void * options)37 lower_vs_vertex_conversion_filter(const nir_instr *instr, const void *options)
38 {
39    if (instr->type != nir_instr_type_intrinsic)
40       return false;
41 
42    nir_intrinsic_instr *inst = nir_instr_as_intrinsic(instr);
43    if (inst->intrinsic != nir_intrinsic_load_deref)
44       return false;
45 
46    nir_variable *var = nir_intrinsic_get_var(inst, 0);
47    return (var->data.mode == nir_var_shader_in) &&
48          (get_input_target_format(var, options) != PIPE_FORMAT_NONE);
49 }
50 
51 typedef  nir_def *
52 (*shift_right_func)(nir_builder *build, nir_def *src0, nir_def *src1);
53 
54 /* decoding the signed vs unsigned scaled format is handled
55  * by applying the signed or unsigned shift right function
56  * accordingly */
57 static nir_def *
from_10_10_10_2_scaled(nir_builder * b,nir_def * src,nir_def * lshift,shift_right_func shr)58 from_10_10_10_2_scaled(nir_builder *b, nir_def *src,
59                        nir_def *lshift, shift_right_func shr)
60 {
61    nir_def *rshift = nir_imm_ivec4(b, 22, 22, 22, 30);
62    return nir_i2f32(b, shr(b, nir_ishl(b, src, lshift), rshift));
63 }
64 
65 static nir_def *
from_10_10_10_2_snorm(nir_builder * b,nir_def * src,nir_def * lshift)66 from_10_10_10_2_snorm(nir_builder *b, nir_def *src, nir_def *lshift)
67 {
68    nir_def *split = from_10_10_10_2_scaled(b, src, lshift, nir_ishr);
69    nir_def *scale_rgb = nir_imm_vec4(b,
70                                          1.0f / 0x1ff,
71                                          1.0f / 0x1ff,
72                                          1.0f / 0x1ff,
73                                          1.0f);
74    return nir_fmul(b, split, scale_rgb);
75 }
76 
77 static nir_def *
from_10_10_10_2_unorm(nir_builder * b,nir_def * src,nir_def * lshift)78 from_10_10_10_2_unorm(nir_builder *b, nir_def *src, nir_def *lshift)
79 {
80    nir_def *split = from_10_10_10_2_scaled(b, src, lshift, nir_ushr);
81    nir_def *scale_rgb = nir_imm_vec4(b,
82                                          1.0f / 0x3ff,
83                                          1.0f / 0x3ff,
84                                          1.0f / 0x3ff,
85                                          1.0f / 3.0f);
86    return nir_fmul(b, split, scale_rgb);
87 }
88 
89 inline static nir_def *
lshift_rgba(nir_builder * b)90 lshift_rgba(nir_builder *b)
91 {
92    return nir_imm_ivec4(b, 22, 12, 2, 0);
93 }
94 
95 inline static nir_def *
lshift_bgra(nir_builder * b)96 lshift_bgra(nir_builder *b)
97 {
98    return nir_imm_ivec4(b, 2, 12, 22, 0);
99 }
100 
101 static nir_def *
lower_vs_vertex_conversion_impl(nir_builder * b,nir_instr * instr,void * options)102 lower_vs_vertex_conversion_impl(nir_builder *b, nir_instr *instr, void *options)
103 {
104    nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
105    nir_variable *var = nir_intrinsic_get_var(intr, 0);
106    enum pipe_format fmt = get_input_target_format(var, options);
107 
108    if (!util_format_has_alpha(fmt)) {
109       /* these formats need the alpha channel replaced with 1: */
110       assert(fmt == PIPE_FORMAT_R8G8B8_SINT ||
111              fmt == PIPE_FORMAT_R8G8B8_UINT ||
112              fmt == PIPE_FORMAT_R16G16B16_SINT ||
113              fmt == PIPE_FORMAT_R16G16B16_UINT);
114       if (intr->def.num_components == 3)
115          return NULL;
116       return nir_vector_insert_imm(b, &intr->def, nir_imm_int(b, 1), 3);
117    } else {
118       nir_def *src = nir_channel(b, &intr->def, 0);
119 
120       switch (fmt) {
121       case PIPE_FORMAT_R10G10B10A2_SNORM:
122          return from_10_10_10_2_snorm(b, src, lshift_rgba(b));
123       case PIPE_FORMAT_B10G10R10A2_SNORM:
124          return from_10_10_10_2_snorm(b, src, lshift_bgra(b));
125       case PIPE_FORMAT_B10G10R10A2_UNORM:
126          return from_10_10_10_2_unorm(b, src, lshift_bgra(b));
127       case PIPE_FORMAT_R10G10B10A2_SSCALED:
128          return from_10_10_10_2_scaled(b, src, lshift_rgba(b), nir_ishr);
129       case PIPE_FORMAT_B10G10R10A2_SSCALED:
130          return from_10_10_10_2_scaled(b, src, lshift_bgra(b), nir_ishr);
131       case PIPE_FORMAT_R10G10B10A2_USCALED:
132          return from_10_10_10_2_scaled(b, src, lshift_rgba(b), nir_ushr);
133       case PIPE_FORMAT_B10G10R10A2_USCALED:
134          return from_10_10_10_2_scaled(b, src, lshift_bgra(b), nir_ushr);
135       case PIPE_FORMAT_R8G8B8A8_USCALED:
136       case PIPE_FORMAT_R16G16B16A16_USCALED:
137          return nir_u2f32(b, &intr->def);
138       case PIPE_FORMAT_R8G8B8A8_SSCALED:
139       case PIPE_FORMAT_R16G16B16A16_SSCALED:
140          return nir_i2f32(b, &intr->def);
141 
142       default:
143          unreachable("Unsupported emulated vertex format");
144       }
145    }
146 }
147 
148 /* Lower emulated vertex attribute input
149  * The vertex attributes are passed as R32_UINT that needs to be converted
150  * to one of the RGB10A2 formats that need to be emulated.
151  *
152  * @param target_formats contains the per attribute format to convert to
153  * or PIPE_FORMAT_NONE if no conversion is needed
154  */
155 bool
dxil_nir_lower_vs_vertex_conversion(nir_shader * s,enum pipe_format target_formats[])156 dxil_nir_lower_vs_vertex_conversion(nir_shader *s,
157                                     enum pipe_format target_formats[])
158 {
159    assert(s->info.stage == MESA_SHADER_VERTEX);
160 
161    bool result =
162          nir_shader_lower_instructions(s,
163                                        lower_vs_vertex_conversion_filter,
164                                        lower_vs_vertex_conversion_impl,
165                                        target_formats);
166    return result;
167 }
168