9f57d9a7775e75c4cc0d964d25468bdd747f4629
[mesa.git] / src / mesa / drivers / dri / i965 / blorp_clear.c
1 /*
2 * Copyright © 2013 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 "util/ralloc.h"
25
26 #include "blorp_priv.h"
27 #include "brw_meta_util.h"
28 #include "brw_defines.h"
29
30 #include "nir_builder.h"
31
32 #define FILE_DEBUG_FLAG DEBUG_BLORP
33
34 struct brw_blorp_const_color_prog_key
35 {
36 bool use_simd16_replicated_data;
37 bool pad[3];
38 };
39
40 static void
41 brw_blorp_params_get_clear_kernel(struct blorp_context *blorp,
42 struct brw_blorp_params *params,
43 bool use_replicated_data)
44 {
45 struct brw_blorp_const_color_prog_key blorp_key;
46 memset(&blorp_key, 0, sizeof(blorp_key));
47 blorp_key.use_simd16_replicated_data = use_replicated_data;
48
49 if (blorp->lookup_shader(blorp, &blorp_key, sizeof(blorp_key),
50 &params->wm_prog_kernel, &params->wm_prog_data))
51 return;
52
53 void *mem_ctx = ralloc_context(NULL);
54
55 nir_builder b;
56 nir_builder_init_simple_shader(&b, NULL, MESA_SHADER_FRAGMENT, NULL);
57 b.shader->info.name = ralloc_strdup(b.shader, "BLORP-clear");
58
59 nir_variable *v_color = nir_variable_create(b.shader, nir_var_shader_in,
60 glsl_vec4_type(), "v_color");
61 v_color->data.location = VARYING_SLOT_VAR0;
62 v_color->data.interpolation = INTERP_MODE_FLAT;
63
64 nir_variable *frag_color = nir_variable_create(b.shader, nir_var_shader_out,
65 glsl_vec4_type(),
66 "gl_FragColor");
67 frag_color->data.location = FRAG_RESULT_COLOR;
68
69 nir_copy_var(&b, frag_color, v_color);
70
71 struct brw_wm_prog_key wm_key;
72 brw_blorp_init_wm_prog_key(&wm_key);
73
74 struct brw_blorp_prog_data prog_data;
75 unsigned program_size;
76 const unsigned *program =
77 brw_blorp_compile_nir_shader(blorp, b.shader, &wm_key, use_replicated_data,
78 &prog_data, &program_size);
79
80 blorp->upload_shader(blorp, &blorp_key, sizeof(blorp_key),
81 program, program_size,
82 &prog_data, sizeof(prog_data),
83 &params->wm_prog_kernel, &params->wm_prog_data);
84
85 ralloc_free(mem_ctx);
86 }
87
88 void
89 blorp_fast_clear(struct blorp_batch *batch,
90 const struct brw_blorp_surf *surf,
91 uint32_t level, uint32_t layer,
92 uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1)
93 {
94 struct brw_blorp_params params;
95 brw_blorp_params_init(&params);
96
97 params.x0 = x0;
98 params.y0 = y0;
99 params.x1 = x1;
100 params.y1 = y1;
101
102 memset(&params.wm_inputs, 0xff, 4*sizeof(float));
103 params.fast_clear_op = BLORP_FAST_CLEAR_OP_CLEAR;
104
105 brw_get_fast_clear_rect(batch->blorp->isl_dev, surf->aux_surf,
106 &params.x0, &params.y0, &params.x1, &params.y1);
107
108 brw_blorp_params_get_clear_kernel(batch->blorp, &params, true);
109
110 brw_blorp_surface_info_init(batch->blorp, &params.dst, surf, level, layer,
111 surf->surf->format, true);
112
113 batch->blorp->exec(batch, &params);
114 }
115
116
117 void
118 blorp_clear(struct blorp_batch *batch,
119 const struct brw_blorp_surf *surf,
120 uint32_t level, uint32_t layer,
121 uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1,
122 enum isl_format format, union isl_color_value clear_color,
123 bool color_write_disable[4])
124 {
125 struct brw_blorp_params params;
126 brw_blorp_params_init(&params);
127
128 params.x0 = x0;
129 params.y0 = y0;
130 params.x1 = x1;
131 params.y1 = y1;
132
133 memcpy(&params.wm_inputs, clear_color.f32, sizeof(float) * 4);
134
135 bool use_simd16_replicated_data = true;
136
137 /* From the SNB PRM (Vol4_Part1):
138 *
139 * "Replicated data (Message Type = 111) is only supported when
140 * accessing tiled memory. Using this Message Type to access linear
141 * (untiled) memory is UNDEFINED."
142 */
143 if (surf->surf->tiling == ISL_TILING_LINEAR)
144 use_simd16_replicated_data = false;
145
146 /* Constant color writes ignore everyting in blend and color calculator
147 * state. This is not documented.
148 */
149 for (unsigned i = 0; i < 4; i++) {
150 params.color_write_disable[i] = color_write_disable[i];
151 if (color_write_disable[i])
152 use_simd16_replicated_data = false;
153 }
154
155 brw_blorp_params_get_clear_kernel(batch->blorp, &params,
156 use_simd16_replicated_data);
157
158 brw_blorp_surface_info_init(batch->blorp, &params.dst, surf, level, layer,
159 format, true);
160
161 batch->blorp->exec(batch, &params);
162 }
163
164 void
165 brw_blorp_ccs_resolve(struct blorp_batch *batch,
166 struct brw_blorp_surf *surf, enum isl_format format)
167 {
168 struct brw_blorp_params params;
169 brw_blorp_params_init(&params);
170
171 brw_blorp_surface_info_init(batch->blorp, &params.dst, surf,
172 0 /* level */, 0 /* layer */, format, true);
173
174 brw_get_ccs_resolve_rect(batch->blorp->isl_dev, &params.dst.aux_surf,
175 &params.x0, &params.y0,
176 &params.x1, &params.y1);
177
178 if (batch->blorp->isl_dev->info->gen >= 9) {
179 if (params.dst.aux_usage == ISL_AUX_USAGE_CCS_E)
180 params.fast_clear_op = BLORP_FAST_CLEAR_OP_RESOLVE_FULL;
181 else
182 params.fast_clear_op = BLORP_FAST_CLEAR_OP_RESOLVE_PARTIAL;
183 } else {
184 /* Broadwell and earlier do not have a partial resolve */
185 params.fast_clear_op = BLORP_FAST_CLEAR_OP_RESOLVE_FULL;
186 }
187
188 /* Note: there is no need to initialize push constants because it doesn't
189 * matter what data gets dispatched to the render target. However, we must
190 * ensure that the fragment shader delivers the data using the "replicated
191 * color" message.
192 */
193
194 brw_blorp_params_get_clear_kernel(batch->blorp, &params, true);
195
196 batch->blorp->exec(batch, &params);
197 }