util/sha1: rework _mesa_sha1_{init,final}
[mesa.git] / src / intel / compiler / brw_nir_attribute_workarounds.c
1 /*
2 * Copyright © 2016 Intel 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 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * 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 NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include "compiler/nir/nir_builder.h"
25 #include "brw_nir.h"
26
27 /**
28 * Prior to Haswell, the hardware can't natively support GL_FIXED or
29 * 2_10_10_10_REV vertex formats. This pass inserts extra shader code
30 * to produce the correct values.
31 */
32
33 struct attr_wa_state {
34 nir_builder builder;
35 bool impl_progress;
36 bool use_legacy_snorm_formula;
37 const uint8_t *wa_flags;
38 };
39
40 static bool
41 apply_attr_wa_block(nir_block *block, struct attr_wa_state *state)
42 {
43 nir_builder *b = &state->builder;
44
45 nir_foreach_instr_safe(instr, block) {
46 if (instr->type != nir_instr_type_intrinsic)
47 continue;
48
49 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
50 if (intrin->intrinsic != nir_intrinsic_load_input)
51 continue;
52
53 uint8_t wa_flags = state->wa_flags[intrin->const_index[0]];
54 if (wa_flags == 0)
55 continue;
56
57 b->cursor = nir_after_instr(instr);
58
59 nir_ssa_def *val = &intrin->dest.ssa;
60
61 /* Do GL_FIXED rescaling for GLES2.0. Our GL_FIXED attributes
62 * come in as floating point conversions of the integer values.
63 */
64 if (wa_flags & BRW_ATTRIB_WA_COMPONENT_MASK) {
65 nir_ssa_def *scaled =
66 nir_fmul(b, val, nir_imm_float(b, 1.0f / 65536.0f));
67 nir_ssa_def *comps[4];
68 for (int i = 0; i < val->num_components; i++) {
69 bool rescale = i < (wa_flags & BRW_ATTRIB_WA_COMPONENT_MASK);
70 comps[i] = nir_channel(b, rescale ? scaled : val, i);
71 }
72 val = nir_vec(b, comps, val->num_components);
73 }
74
75 /* Do sign recovery for 2101010 formats if required. */
76 if (wa_flags & BRW_ATTRIB_WA_SIGN) {
77 /* sign recovery shift: <22, 22, 22, 30> */
78 nir_ssa_def *shift = nir_imm_ivec4(b, 22, 22, 22, 30);
79 val = nir_ishr(b, nir_ishl(b, val, shift), shift);
80 }
81
82 /* Apply BGRA swizzle if required. */
83 if (wa_flags & BRW_ATTRIB_WA_BGRA) {
84 val = nir_swizzle(b, val, (unsigned[4]){2,1,0,3}, 4, true);
85 }
86
87 if (wa_flags & BRW_ATTRIB_WA_NORMALIZE) {
88 /* ES 3.0 has different rules for converting signed normalized
89 * fixed-point numbers than desktop GL.
90 */
91 if ((wa_flags & BRW_ATTRIB_WA_SIGN) &&
92 !state->use_legacy_snorm_formula) {
93 /* According to equation 2.2 of the ES 3.0 specification,
94 * signed normalization conversion is done by:
95 *
96 * f = c / (2^(b-1)-1)
97 */
98 nir_ssa_def *es3_normalize_factor =
99 nir_imm_vec4(b, 1.0f / ((1 << 9) - 1), 1.0f / ((1 << 9) - 1),
100 1.0f / ((1 << 9) - 1), 1.0f / ((1 << 1) - 1));
101 val = nir_fmax(b,
102 nir_fmul(b, nir_i2f32(b, val), es3_normalize_factor),
103 nir_imm_float(b, -1.0f));
104 } else {
105 /* The following equations are from the OpenGL 3.2 specification:
106 *
107 * 2.1 unsigned normalization
108 * f = c/(2^n-1)
109 *
110 * 2.2 signed normalization
111 * f = (2c+1)/(2^n-1)
112 *
113 * Both of these share a common divisor, which we handle by
114 * multiplying by 1 / (2^b - 1) for b = <10, 10, 10, 2>.
115 */
116 nir_ssa_def *normalize_factor =
117 nir_imm_vec4(b, 1.0f / ((1 << 10) - 1), 1.0f / ((1 << 10) - 1),
118 1.0f / ((1 << 10) - 1), 1.0f / ((1 << 2) - 1));
119
120 if (wa_flags & BRW_ATTRIB_WA_SIGN) {
121 /* For signed normalization, the numerator is 2c+1. */
122 nir_ssa_def *two = nir_imm_float(b, 2.0f);
123 nir_ssa_def *one = nir_imm_float(b, 1.0f);
124 val = nir_fadd(b, nir_fmul(b, nir_i2f32(b, val), two), one);
125 } else {
126 /* For unsigned normalization, the numerator is just c. */
127 val = nir_u2f32(b, val);
128 }
129 val = nir_fmul(b, val, normalize_factor);
130 }
131 }
132
133 if (wa_flags & BRW_ATTRIB_WA_SCALE) {
134 val = (wa_flags & BRW_ATTRIB_WA_SIGN) ? nir_i2f32(b, val)
135 : nir_u2f32(b, val);
136 }
137
138 nir_ssa_def_rewrite_uses_after(&intrin->dest.ssa, nir_src_for_ssa(val),
139 val->parent_instr);
140 state->impl_progress = true;
141 }
142
143 return true;
144 }
145
146 bool
147 brw_nir_apply_attribute_workarounds(nir_shader *shader,
148 bool use_legacy_snorm_formula,
149 const uint8_t *attrib_wa_flags)
150 {
151 bool progress = false;
152 struct attr_wa_state state = {
153 .use_legacy_snorm_formula = use_legacy_snorm_formula,
154 .wa_flags = attrib_wa_flags,
155 };
156
157 nir_foreach_function(func, shader) {
158 if (!func->impl)
159 continue;
160
161 nir_builder_init(&state.builder, func->impl);
162 state.impl_progress = false;
163
164 nir_foreach_block(block, func->impl) {
165 apply_attr_wa_block(block, &state);
166 }
167
168 if (state.impl_progress) {
169 nir_metadata_preserve(func->impl, nir_metadata_block_index |
170 nir_metadata_dominance);
171 progress = true;
172 }
173 }
174
175 return progress;
176 }