• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2019 Google, Inc.
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 "ir3_nir.h"
25 #include "compiler/nir/nir_builder.h"
26 
27 /**
28  * This pass lowers load_barycentric_at_offset to dsx.3d/dsy.3d and alu
29  * instructions.
30  */
31 
32 static nir_ssa_def *
load(nir_builder * b,unsigned ncomp,nir_intrinsic_op op)33 load(nir_builder *b, unsigned ncomp, nir_intrinsic_op op)
34 {
35 	nir_intrinsic_instr *load_size = nir_intrinsic_instr_create(b->shader, op);
36 	nir_ssa_dest_init(&load_size->instr, &load_size->dest, ncomp, 32, NULL);
37 	nir_builder_instr_insert(b, &load_size->instr);
38 
39 	return &load_size->dest.ssa;
40 }
41 
42 static nir_ssa_def *
ir3_nir_lower_load_barycentric_at_offset_instr(nir_builder * b,nir_instr * instr,void * data)43 ir3_nir_lower_load_barycentric_at_offset_instr(nir_builder *b,
44 	   nir_instr *instr, void *data)
45 {
46 	nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
47 
48 #define chan(var, c) nir_channel(b, var, c)
49 
50 	nir_ssa_def *off = intr->src[0].ssa;
51 	nir_ssa_def *ij = load(b, 2, nir_intrinsic_load_barycentric_pixel);
52 	nir_ssa_def *s  = load(b, 1, nir_intrinsic_load_size_ir3);
53 
54 	s = nir_frcp(b, s);
55 
56 	/* scaled ij with s as 3rd component: */
57 	nir_ssa_def *sij = nir_vec3(b,
58 			nir_fmul(b, chan(ij, 0), s),
59 			nir_fmul(b, chan(ij, 1), s),
60 			s);
61 
62 	nir_ssa_def *foo = nir_fddx(b, sij);
63 	nir_ssa_def *bar = nir_fddy(b, sij);
64 
65 	if (b->shader->info.stage == MESA_SHADER_FRAGMENT)
66 		b->shader->info.fs.needs_helper_invocations = true;
67 
68 	nir_ssa_def *x, *y, *z, *i, *j;
69 
70 	x = nir_ffma(b, chan(off, 0), chan(foo, 0), chan(sij, 0));
71 	y = nir_ffma(b, chan(off, 0), chan(foo, 1), chan(sij, 1));
72 	z = nir_ffma(b, chan(off, 0), chan(foo, 2), chan(sij, 2));
73 
74 	x = nir_ffma(b, chan(off, 1), chan(bar, 0), x);
75 	y = nir_ffma(b, chan(off, 1), chan(bar, 1), y);
76 	z = nir_ffma(b, chan(off, 1), chan(bar, 2), z);
77 
78 	/* convert back into primitive space: */
79 	z = nir_frcp(b, z);
80 	i = nir_fmul(b, z, x);
81 	j = nir_fmul(b, z, y);
82 
83 	ij = nir_vec2(b, i, j);
84 
85 	return ij;
86 }
87 
88 static bool
ir3_nir_lower_load_barycentric_at_offset_filter(const nir_instr * instr,const void * data)89 ir3_nir_lower_load_barycentric_at_offset_filter(const nir_instr *instr,
90 		const void *data)
91 {
92 	return (instr->type == nir_instr_type_intrinsic &&
93 			nir_instr_as_intrinsic(instr)->intrinsic ==
94 			nir_intrinsic_load_barycentric_at_offset);
95 }
96 
97 bool
ir3_nir_lower_load_barycentric_at_offset(nir_shader * shader)98 ir3_nir_lower_load_barycentric_at_offset(nir_shader *shader)
99 {
100 	debug_assert(shader->info.stage == MESA_SHADER_FRAGMENT);
101 
102 	return nir_shader_lower_instructions(shader,
103 			ir3_nir_lower_load_barycentric_at_offset_filter,
104 			ir3_nir_lower_load_barycentric_at_offset_instr,
105 			NULL);
106 }
107