Merge remote-tracking branch 'mesa-public/master' into vulkan
[mesa.git] / src / mesa / drivers / dri / i965 / brw_clear.c
1 /**************************************************************************
2 *
3 * Copyright 2003 VMware, Inc.
4 * Copyright 2009, 2012 Intel Corporation.
5 * All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 **************************************************************************/
28
29 #include "main/glheader.h"
30 #include "main/mtypes.h"
31 #include "main/condrender.h"
32 #include "swrast/swrast.h"
33 #include "drivers/common/meta.h"
34
35 #include "intel_batchbuffer.h"
36 #include "intel_blit.h"
37 #include "intel_fbo.h"
38 #include "intel_mipmap_tree.h"
39
40 #include "brw_context.h"
41 #include "brw_blorp.h"
42
43 #define FILE_DEBUG_FLAG DEBUG_BLIT
44
45 static const char *buffer_names[] = {
46 [BUFFER_FRONT_LEFT] = "front",
47 [BUFFER_BACK_LEFT] = "back",
48 [BUFFER_FRONT_RIGHT] = "front right",
49 [BUFFER_BACK_RIGHT] = "back right",
50 [BUFFER_DEPTH] = "depth",
51 [BUFFER_STENCIL] = "stencil",
52 [BUFFER_ACCUM] = "accum",
53 [BUFFER_AUX0] = "aux0",
54 [BUFFER_COLOR0] = "color0",
55 [BUFFER_COLOR1] = "color1",
56 [BUFFER_COLOR2] = "color2",
57 [BUFFER_COLOR3] = "color3",
58 [BUFFER_COLOR4] = "color4",
59 [BUFFER_COLOR5] = "color5",
60 [BUFFER_COLOR6] = "color6",
61 [BUFFER_COLOR7] = "color7",
62 };
63
64 static void
65 debug_mask(const char *name, GLbitfield mask)
66 {
67 GLuint i;
68
69 if (unlikely(INTEL_DEBUG & DEBUG_BLIT)) {
70 DBG("%s clear:", name);
71 for (i = 0; i < BUFFER_COUNT; i++) {
72 if (mask & (1 << i))
73 DBG(" %s", buffer_names[i]);
74 }
75 DBG("\n");
76 }
77 }
78
79 /**
80 * Returns true if the scissor is a noop (cuts out nothing).
81 */
82 static bool
83 noop_scissor(struct gl_context *ctx, struct gl_framebuffer *fb)
84 {
85 return ctx->Scissor.ScissorArray[0].X <= 0 &&
86 ctx->Scissor.ScissorArray[0].Y <= 0 &&
87 ctx->Scissor.ScissorArray[0].Width >= fb->Width &&
88 ctx->Scissor.ScissorArray[0].Height >= fb->Height;
89 }
90
91 /**
92 * Implements fast depth clears on gen6+.
93 *
94 * Fast clears basically work by setting a flag in each of the subspans
95 * represented in the HiZ buffer that says "When you need the depth values for
96 * this subspan, it's the hardware's current clear value." Then later rendering
97 * can just use the static clear value instead of referencing memory.
98 *
99 * The tricky part of the implementation is that you have to have the clear
100 * value that was used on the depth buffer in place for all further rendering,
101 * at least until a resolve to the real depth buffer happens.
102 */
103 static bool
104 brw_fast_clear_depth(struct gl_context *ctx)
105 {
106 struct brw_context *brw = brw_context(ctx);
107 struct gl_framebuffer *fb = ctx->DrawBuffer;
108 struct intel_renderbuffer *depth_irb =
109 intel_get_renderbuffer(fb, BUFFER_DEPTH);
110 struct intel_mipmap_tree *mt = depth_irb->mt;
111 struct gl_renderbuffer_attachment *depth_att = &fb->Attachment[BUFFER_DEPTH];
112
113 if (brw->gen < 6)
114 return false;
115
116 if (!intel_renderbuffer_has_hiz(depth_irb))
117 return false;
118
119 /* We only handle full buffer clears -- otherwise you'd have to track whether
120 * a previous clear had happened at a different clear value and resolve it
121 * first.
122 */
123 if ((ctx->Scissor.EnableFlags & 1) && !noop_scissor(ctx, fb)) {
124 perf_debug("Failed to fast clear %dx%d depth because of scissors. "
125 "Possible 5%% performance win if avoided.\n",
126 mt->logical_width0, mt->logical_height0);
127 return false;
128 }
129
130 uint32_t depth_clear_value;
131 switch (mt->format) {
132 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
133 case MESA_FORMAT_Z24_UNORM_S8_UINT:
134 /* From the Sandy Bridge PRM, volume 2 part 1, page 314:
135 *
136 * "[DevSNB+]: Several cases exist where Depth Buffer Clear cannot be
137 * enabled (the legacy method of clearing must be performed):
138 *
139 * - If the depth buffer format is D32_FLOAT_S8X24_UINT or
140 * D24_UNORM_S8_UINT.
141 */
142 return false;
143
144 case MESA_FORMAT_Z_FLOAT32:
145 depth_clear_value = float_as_int(ctx->Depth.Clear);
146 break;
147
148 case MESA_FORMAT_Z_UNORM16:
149 /* From the Sandy Bridge PRM, volume 2 part 1, page 314:
150 *
151 * "[DevSNB+]: Several cases exist where Depth Buffer Clear cannot be
152 * enabled (the legacy method of clearing must be performed):
153 *
154 * - DevSNB{W/A}]: When depth buffer format is D16_UNORM and the
155 * width of the map (LOD0) is not multiple of 16, fast clear
156 * optimization must be disabled.
157 */
158 if (brw->gen == 6 &&
159 (minify(mt->physical_width0,
160 depth_irb->mt_level - mt->first_level) % 16) != 0)
161 return false;
162 /* FALLTHROUGH */
163
164 default:
165 if (brw->gen >= 8)
166 depth_clear_value = float_as_int(ctx->Depth.Clear);
167 else
168 depth_clear_value = fb->_DepthMax * ctx->Depth.Clear;
169 break;
170 }
171
172 /* If we're clearing to a new clear value, then we need to resolve any clear
173 * flags out of the HiZ buffer into the real depth buffer.
174 */
175 if (mt->depth_clear_value != depth_clear_value) {
176 intel_miptree_all_slices_resolve_depth(brw, mt);
177 mt->depth_clear_value = depth_clear_value;
178 }
179
180 /* From the Sandy Bridge PRM, volume 2 part 1, page 313:
181 *
182 * "If other rendering operations have preceded this clear, a
183 * PIPE_CONTROL with write cache flush enabled and Z-inhibit disabled
184 * must be issued before the rectangle primitive used for the depth
185 * buffer clear operation.
186 */
187 intel_batchbuffer_emit_mi_flush(brw);
188
189 if (fb->MaxNumLayers > 0) {
190 for (unsigned layer = 0; layer < depth_irb->layer_count; layer++) {
191 intel_hiz_exec(brw, mt, depth_irb->mt_level,
192 depth_irb->mt_layer + layer,
193 GEN6_HIZ_OP_DEPTH_CLEAR);
194 }
195 } else {
196 intel_hiz_exec(brw, mt, depth_irb->mt_level, depth_irb->mt_layer,
197 GEN6_HIZ_OP_DEPTH_CLEAR);
198 }
199
200 if (brw->gen == 6) {
201 /* From the Sandy Bridge PRM, volume 2 part 1, page 314:
202 *
203 * "DevSNB, DevSNB-B{W/A}]: Depth buffer clear pass must be followed
204 * by a PIPE_CONTROL command with DEPTH_STALL bit set and Then
205 * followed by Depth FLUSH'
206 */
207 intel_batchbuffer_emit_mi_flush(brw);
208 }
209
210 /* Now, the HiZ buffer contains data that needs to be resolved to the depth
211 * buffer.
212 */
213 intel_renderbuffer_att_set_needs_depth_resolve(depth_att);
214
215 return true;
216 }
217
218 /**
219 * Called by ctx->Driver.Clear.
220 */
221 static void
222 brw_clear(struct gl_context *ctx, GLbitfield mask)
223 {
224 struct brw_context *brw = brw_context(ctx);
225 struct gl_framebuffer *fb = ctx->DrawBuffer;
226 bool partial_clear = ctx->Scissor.EnableFlags && !noop_scissor(ctx, fb);
227
228 if (!_mesa_check_conditional_render(ctx))
229 return;
230
231 if (mask & (BUFFER_BIT_FRONT_LEFT | BUFFER_BIT_FRONT_RIGHT)) {
232 brw->front_buffer_dirty = true;
233 }
234
235 intel_prepare_render(brw);
236 brw_workaround_depthstencil_alignment(brw, partial_clear ? 0 : mask);
237
238 if (mask & BUFFER_BIT_DEPTH) {
239 if (brw_fast_clear_depth(ctx)) {
240 DBG("fast clear: depth\n");
241 mask &= ~BUFFER_BIT_DEPTH;
242 }
243 }
244
245 /* Clear color buffers with fast clear or at least rep16 writes. */
246 if (brw->gen >= 6 && brw->gen < 9 && (mask & BUFFER_BITS_COLOR)) {
247 if (brw_meta_fast_clear(brw, fb, mask, partial_clear)) {
248 debug_mask("blorp color", mask & BUFFER_BITS_COLOR);
249 mask &= ~BUFFER_BITS_COLOR;
250 }
251 }
252
253 GLbitfield tri_mask = mask & (BUFFER_BITS_COLOR |
254 BUFFER_BIT_STENCIL |
255 BUFFER_BIT_DEPTH);
256
257 if (tri_mask) {
258 debug_mask("tri", tri_mask);
259 mask &= ~tri_mask;
260
261 if (ctx->API == API_OPENGLES) {
262 _mesa_meta_Clear(&brw->ctx, tri_mask);
263 } else {
264 _mesa_meta_glsl_Clear(&brw->ctx, tri_mask);
265 }
266 }
267
268 /* Any strange buffers get passed off to swrast */
269 if (mask) {
270 debug_mask("swrast", mask);
271 _swrast_Clear(ctx, mask);
272 }
273 }
274
275
276 void
277 intelInitClearFuncs(struct dd_function_table *functions)
278 {
279 functions->Clear = brw_clear;
280 }