1 /*
2 * Copyright © 2020 Mike Blumenkrantz
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 * Authors:
24 * Mike Blumenkrantz <michael.blumenkrantz@gmail.com>
25 */
26
27 #include "nir.h"
28 #include "nir_builder.h"
29
30 /**
31 * This pass splits gl_FragColor into gl_FragData[0-7] for drivers which handle
32 * the former but not the latter, e.g., zink.
33 */
34
35 /*
36 If a fragment shader writes to "gl_FragColor", DrawBuffersIndexedEXT
37 specifies a set of draw buffers into which the color written to
38 "gl_FragColor" is written. If a fragment shader writes to
39 gl_FragData, DrawBuffersIndexedEXT specifies a set of draw buffers
40 into which each of the multiple output colors defined by these
41 variables are separately written. If a fragment shader writes to
42 neither gl_FragColor nor gl_FragData, the values of the fragment
43 colors following shader execution are undefined, and may differ
44 for each fragment color.
45
46 - EXT_multiview_draw_buffers
47
48 */
49
50 static bool
lower_fragcolor_instr(nir_builder * b,nir_instr * intr,void * data)51 lower_fragcolor_instr(nir_builder *b, nir_instr *intr, void *data)
52 {
53 if (intr->type != nir_instr_type_intrinsic)
54 return false;
55
56 nir_intrinsic_instr *instr = nir_instr_as_intrinsic(intr);
57 unsigned *max_draw_buffers = data;
58
59 nir_variable *out;
60 if (instr->intrinsic != nir_intrinsic_store_deref)
61 return false;
62
63 out = nir_deref_instr_get_variable(nir_src_as_deref(instr->src[0]));
64 if (out->data.location != FRAG_RESULT_COLOR || out->data.mode != nir_var_shader_out)
65 return false;
66 b->cursor = nir_after_instr(&instr->instr);
67
68 assert(instr->src[1].is_ssa);
69 nir_ssa_def *frag_color = instr->src[1].ssa;
70 ralloc_free(out->name);
71
72 const char *name = out->data.index == 0 ? "gl_FragData[0]" :
73 "gl_SecondaryFragDataEXT[0]";
74 const char *name_tmpl = out->data.index == 0 ? "gl_FragData[%u]" :
75 "gl_SecondaryFragDataEXT[%u]";
76
77 out->name = ralloc_strdup(out, name);
78
79 /* translate gl_FragColor -> gl_FragData since this is already handled */
80 out->data.location = FRAG_RESULT_DATA0;
81 nir_component_mask_t writemask = nir_intrinsic_write_mask(instr);
82 b->shader->info.outputs_written &= ~BITFIELD64_BIT(FRAG_RESULT_COLOR);
83 b->shader->info.outputs_written |= BITFIELD64_BIT(FRAG_RESULT_DATA0);
84
85 for (unsigned i = 1; i < *max_draw_buffers; i++) {
86 char name[28];
87 snprintf(name, sizeof(name), name_tmpl, i);
88 nir_variable *out_color = nir_variable_create(b->shader, nir_var_shader_out,
89 out->type, name);
90 out_color->data.location = FRAG_RESULT_DATA0 + i;
91 out_color->data.driver_location = b->shader->num_outputs++;
92 out_color->data.index = out->data.index;
93 nir_store_var(b, out_color, frag_color, writemask);
94 b->shader->info.outputs_written |= BITFIELD64_BIT(FRAG_RESULT_DATA0 + i);
95 }
96 return true;
97 }
98
99 bool
nir_lower_fragcolor(nir_shader * shader,unsigned max_draw_buffers)100 nir_lower_fragcolor(nir_shader *shader, unsigned max_draw_buffers)
101 {
102 if (shader->info.stage != MESA_SHADER_FRAGMENT)
103 return false;
104
105 return nir_shader_instructions_pass(shader, lower_fragcolor_instr,
106 nir_metadata_block_index | nir_metadata_dominance, &max_draw_buffers);
107 }
108