xref: /aosp_15_r20/external/mesa3d/src/compiler/nir/nir_lower_fragcolor.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
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 <[email protected]>
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_intrin(nir_builder * b,nir_intrinsic_instr * instr,void * data)51 lower_fragcolor_intrin(nir_builder *b, nir_intrinsic_instr *instr, void *data)
52 {
53    unsigned *max_draw_buffers = data;
54 
55    nir_variable *out;
56    if (instr->intrinsic != nir_intrinsic_store_deref)
57       return false;
58 
59    out = nir_intrinsic_get_var(instr, 0);
60    if (out->data.location != FRAG_RESULT_COLOR || out->data.mode != nir_var_shader_out)
61       return false;
62    b->cursor = nir_after_instr(&instr->instr);
63 
64    nir_def *frag_color = instr->src[1].ssa;
65    ralloc_free(out->name);
66 
67    const char *name = out->data.index == 0 ? "gl_FragData[0]" : "gl_SecondaryFragDataEXT[0]";
68    const char *name_tmpl = out->data.index == 0 ? "gl_FragData[%u]" : "gl_SecondaryFragDataEXT[%u]";
69 
70    out->name = ralloc_strdup(out, name);
71 
72    /* translate gl_FragColor -> gl_FragData since this is already handled */
73    out->data.location = FRAG_RESULT_DATA0;
74    nir_component_mask_t writemask = nir_intrinsic_write_mask(instr);
75    b->shader->info.outputs_written &= ~BITFIELD64_BIT(FRAG_RESULT_COLOR);
76    b->shader->info.outputs_written |= BITFIELD64_BIT(FRAG_RESULT_DATA0);
77 
78    for (unsigned i = 1; i < *max_draw_buffers; i++) {
79       char name[28];
80       snprintf(name, sizeof(name), name_tmpl, i);
81       nir_variable *out_color = nir_variable_create(b->shader, nir_var_shader_out,
82                                                     out->type, name);
83       out_color->data.location = FRAG_RESULT_DATA0 + i;
84       out_color->data.location_frac = out->data.location_frac;
85       out_color->data.driver_location = b->shader->num_outputs++;
86       out_color->data.index = out->data.index;
87       nir_store_var(b, out_color, frag_color, writemask);
88       b->shader->info.outputs_written |= BITFIELD64_BIT(FRAG_RESULT_DATA0 + i);
89    }
90    return true;
91 }
92 
93 bool
nir_lower_fragcolor(nir_shader * shader,unsigned max_draw_buffers)94 nir_lower_fragcolor(nir_shader *shader, unsigned max_draw_buffers)
95 {
96    if (shader->info.stage != MESA_SHADER_FRAGMENT)
97       return false;
98 
99    return nir_shader_intrinsics_pass(shader, lower_fragcolor_intrin,
100                                      nir_metadata_control_flow,
101                                      &max_draw_buffers);
102 }
103