• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "d3d12_nir_passes.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_ssa_def *
52 (*shift_right_func)(nir_builder *build, nir_ssa_def *src0, nir_ssa_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_ssa_def *
from_10_10_10_2_scaled(nir_builder * b,nir_ssa_def * src,nir_ssa_def * lshift,shift_right_func shr)58 from_10_10_10_2_scaled(nir_builder *b, nir_ssa_def *src,
59                        nir_ssa_def *lshift, shift_right_func shr)
60 {
61    nir_ssa_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_ssa_def *
from_10_10_10_2_snorm(nir_builder * b,nir_ssa_def * src,nir_ssa_def * lshift)66 from_10_10_10_2_snorm(nir_builder *b, nir_ssa_def *src, nir_ssa_def *lshift)
67 {
68    nir_ssa_def *split = from_10_10_10_2_scaled(b, src, lshift, nir_ishr);
69    nir_ssa_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_ssa_def *
from_10_10_10_2_unorm(nir_builder * b,nir_ssa_def * src,nir_ssa_def * lshift)78 from_10_10_10_2_unorm(nir_builder *b, nir_ssa_def *src, nir_ssa_def *lshift)
79 {
80    nir_ssa_def *split = from_10_10_10_2_scaled(b, src, lshift, nir_ushr);
81    nir_ssa_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_ssa_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_ssa_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_ssa_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       return nir_vector_insert_imm(b, &intr->dest.ssa, nir_imm_int(b, 1), 3);
115    } else {
116       nir_ssa_def *src = nir_channel(b, &intr->dest.ssa, 0);
117 
118       switch (fmt) {
119       case PIPE_FORMAT_R10G10B10A2_SNORM:
120          return from_10_10_10_2_snorm(b, src, lshift_rgba(b));
121       case PIPE_FORMAT_B10G10R10A2_SNORM:
122          return from_10_10_10_2_snorm(b, src, lshift_bgra(b));
123       case PIPE_FORMAT_B10G10R10A2_UNORM:
124          return from_10_10_10_2_unorm(b, src, lshift_bgra(b));
125       case PIPE_FORMAT_R10G10B10A2_SSCALED:
126          return from_10_10_10_2_scaled(b, src, lshift_rgba(b), nir_ishr);
127       case PIPE_FORMAT_B10G10R10A2_SSCALED:
128          return from_10_10_10_2_scaled(b, src, lshift_bgra(b), nir_ishr);
129       case PIPE_FORMAT_R10G10B10A2_USCALED:
130          return from_10_10_10_2_scaled(b, src, lshift_rgba(b), nir_ushr);
131       case PIPE_FORMAT_B10G10R10A2_USCALED:
132          return from_10_10_10_2_scaled(b, src, lshift_bgra(b), nir_ushr);
133 
134       default:
135          unreachable("Unsupported emulated vertex format");
136       }
137    }
138 }
139 
140 /* Lower emulated vertex attribute input
141  * The vertex attributes are passed as R32_UINT that needs to be converted
142  * to one of the RGB10A2 formats that need to be emulated.
143  *
144  * @param target_formats contains the per attribute format to convert to
145  * or PIPE_FORMAT_NONE if no conversion is needed
146  */
147 bool
d3d12_nir_lower_vs_vertex_conversion(nir_shader * s,enum pipe_format target_formats[])148 d3d12_nir_lower_vs_vertex_conversion(nir_shader *s,
149                                      enum pipe_format target_formats[])
150 {
151    assert(s->info.stage == MESA_SHADER_VERTEX);
152 
153    bool result =
154          nir_shader_lower_instructions(s,
155                                        lower_vs_vertex_conversion_filter,
156                                        lower_vs_vertex_conversion_impl,
157                                        target_formats);
158    return result;
159 }
160