intel: Require ISL_AUX_USAGE_STC_CCS for stencil CCS
[mesa.git] / src / intel / blorp / blorp_genX_exec.h
1 /*
2 * Copyright © 2016 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 #ifndef BLORP_GENX_EXEC_H
25 #define BLORP_GENX_EXEC_H
26
27 #include "blorp_priv.h"
28 #include "dev/gen_device_info.h"
29 #include "common/gen_sample_positions.h"
30 #include "common/gen_l3_config.h"
31 #include "genxml/gen_macros.h"
32
33 /**
34 * This file provides the blorp pipeline setup and execution functionality.
35 * It defines the following function:
36 *
37 * static void
38 * blorp_exec(struct blorp_context *blorp, void *batch_data,
39 * const struct blorp_params *params);
40 *
41 * It is the job of whoever includes this header to wrap this in something
42 * to get an externally visible symbol.
43 *
44 * In order for the blorp_exec function to work, the driver must provide
45 * implementations of the following static helper functions.
46 */
47
48 static void *
49 blorp_emit_dwords(struct blorp_batch *batch, unsigned n);
50
51 static uint64_t
52 blorp_emit_reloc(struct blorp_batch *batch,
53 void *location, struct blorp_address address, uint32_t delta);
54
55 static void *
56 blorp_alloc_dynamic_state(struct blorp_batch *batch,
57 uint32_t size,
58 uint32_t alignment,
59 uint32_t *offset);
60 static void *
61 blorp_alloc_vertex_buffer(struct blorp_batch *batch, uint32_t size,
62 struct blorp_address *addr);
63 static void
64 blorp_vf_invalidate_for_vb_48b_transitions(struct blorp_batch *batch,
65 const struct blorp_address *addrs,
66 uint32_t *sizes,
67 unsigned num_vbs);
68
69 UNUSED static struct blorp_address
70 blorp_get_workaround_page(struct blorp_batch *batch);
71
72 static void
73 blorp_alloc_binding_table(struct blorp_batch *batch, unsigned num_entries,
74 unsigned state_size, unsigned state_alignment,
75 uint32_t *bt_offset, uint32_t *surface_offsets,
76 void **surface_maps);
77
78 static void
79 blorp_flush_range(struct blorp_batch *batch, void *start, size_t size);
80
81 static void
82 blorp_surface_reloc(struct blorp_batch *batch, uint32_t ss_offset,
83 struct blorp_address address, uint32_t delta);
84
85 static uint64_t
86 blorp_get_surface_address(struct blorp_batch *batch,
87 struct blorp_address address);
88
89 #if GEN_GEN >= 7 && GEN_GEN < 10
90 static struct blorp_address
91 blorp_get_surface_base_address(struct blorp_batch *batch);
92 #endif
93
94 #if GEN_GEN >= 7
95 static const struct gen_l3_config *
96 blorp_get_l3_config(struct blorp_batch *batch);
97 # else
98 static void
99 blorp_emit_urb_config(struct blorp_batch *batch,
100 unsigned vs_entry_size, unsigned sf_entry_size);
101 #endif
102
103 static void
104 blorp_emit_pipeline(struct blorp_batch *batch,
105 const struct blorp_params *params);
106
107 /***** BEGIN blorp_exec implementation ******/
108
109 static uint64_t
110 _blorp_combine_address(struct blorp_batch *batch, void *location,
111 struct blorp_address address, uint32_t delta)
112 {
113 if (address.buffer == NULL) {
114 return address.offset + delta;
115 } else {
116 return blorp_emit_reloc(batch, location, address, delta);
117 }
118 }
119
120 #define __gen_address_type struct blorp_address
121 #define __gen_user_data struct blorp_batch
122 #define __gen_combine_address _blorp_combine_address
123
124 #include "genxml/genX_pack.h"
125
126 #define _blorp_cmd_length(cmd) cmd ## _length
127 #define _blorp_cmd_length_bias(cmd) cmd ## _length_bias
128 #define _blorp_cmd_header(cmd) cmd ## _header
129 #define _blorp_cmd_pack(cmd) cmd ## _pack
130
131 #define blorp_emit(batch, cmd, name) \
132 for (struct cmd name = { _blorp_cmd_header(cmd) }, \
133 *_dst = blorp_emit_dwords(batch, _blorp_cmd_length(cmd)); \
134 __builtin_expect(_dst != NULL, 1); \
135 _blorp_cmd_pack(cmd)(batch, (void *)_dst, &name), \
136 _dst = NULL)
137
138 #define blorp_emitn(batch, cmd, n, ...) ({ \
139 uint32_t *_dw = blorp_emit_dwords(batch, n); \
140 if (_dw) { \
141 struct cmd template = { \
142 _blorp_cmd_header(cmd), \
143 .DWordLength = n - _blorp_cmd_length_bias(cmd), \
144 __VA_ARGS__ \
145 }; \
146 _blorp_cmd_pack(cmd)(batch, _dw, &template); \
147 } \
148 _dw ? _dw + 1 : NULL; /* Array starts at dw[1] */ \
149 })
150
151 #define STRUCT_ZERO(S) ({ struct S t; memset(&t, 0, sizeof(t)); t; })
152
153 #define blorp_emit_dynamic(batch, state, name, align, offset) \
154 for (struct state name = STRUCT_ZERO(state), \
155 *_dst = blorp_alloc_dynamic_state(batch, \
156 _blorp_cmd_length(state) * 4, \
157 align, offset); \
158 __builtin_expect(_dst != NULL, 1); \
159 _blorp_cmd_pack(state)(batch, (void *)_dst, &name), \
160 blorp_flush_range(batch, _dst, _blorp_cmd_length(state) * 4), \
161 _dst = NULL)
162
163 /* 3DSTATE_URB
164 * 3DSTATE_URB_VS
165 * 3DSTATE_URB_HS
166 * 3DSTATE_URB_DS
167 * 3DSTATE_URB_GS
168 *
169 * Assign the entire URB to the VS. Even though the VS disabled, URB space
170 * is still needed because the clipper loads the VUE's from the URB. From
171 * the Sandybridge PRM, Volume 2, Part 1, Section 3DSTATE,
172 * Dword 1.15:0 "VS Number of URB Entries":
173 * This field is always used (even if VS Function Enable is DISABLED).
174 *
175 * The warning below appears in the PRM (Section 3DSTATE_URB), but we can
176 * safely ignore it because this batch contains only one draw call.
177 * Because of URB corruption caused by allocating a previous GS unit
178 * URB entry to the VS unit, software is required to send a “GS NULL
179 * Fence” (Send URB fence with VS URB size == 1 and GS URB size == 0)
180 * plus a dummy DRAW call before any case where VS will be taking over
181 * GS URB space.
182 *
183 * If the 3DSTATE_URB_VS is emitted, than the others must be also.
184 * From the Ivybridge PRM, Volume 2 Part 1, section 1.7.1 3DSTATE_URB_VS:
185 *
186 * 3DSTATE_URB_HS, 3DSTATE_URB_DS, and 3DSTATE_URB_GS must also be
187 * programmed in order for the programming of this state to be
188 * valid.
189 */
190 static void
191 emit_urb_config(struct blorp_batch *batch,
192 const struct blorp_params *params,
193 enum gen_urb_deref_block_size *deref_block_size)
194 {
195 /* Once vertex fetcher has written full VUE entries with complete
196 * header the space requirement is as follows per vertex (in bytes):
197 *
198 * Header Position Program constants
199 * +--------+------------+-------------------+
200 * | 16 | 16 | n x 16 |
201 * +--------+------------+-------------------+
202 *
203 * where 'n' stands for number of varying inputs expressed as vec4s.
204 */
205 const unsigned num_varyings =
206 params->wm_prog_data ? params->wm_prog_data->num_varying_inputs : 0;
207 const unsigned total_needed = 16 + 16 + num_varyings * 16;
208
209 /* The URB size is expressed in units of 64 bytes (512 bits) */
210 const unsigned vs_entry_size = DIV_ROUND_UP(total_needed, 64);
211
212 const unsigned sf_entry_size =
213 params->sf_prog_data ? params->sf_prog_data->urb_entry_size : 0;
214
215 #if GEN_GEN >= 7
216 assert(sf_entry_size == 0);
217 const unsigned entry_size[4] = { vs_entry_size, 1, 1, 1 };
218
219 unsigned entries[4], start[4];
220 gen_get_urb_config(batch->blorp->compiler->devinfo,
221 blorp_get_l3_config(batch),
222 false, false, entry_size,
223 entries, start, deref_block_size);
224
225 #if GEN_GEN == 7 && !GEN_IS_HASWELL
226 /* From the IVB PRM Vol. 2, Part 1, Section 3.2.1:
227 *
228 * "A PIPE_CONTROL with Post-Sync Operation set to 1h and a depth stall
229 * needs to be sent just prior to any 3DSTATE_VS, 3DSTATE_URB_VS,
230 * 3DSTATE_CONSTANT_VS, 3DSTATE_BINDING_TABLE_POINTER_VS,
231 * 3DSTATE_SAMPLER_STATE_POINTER_VS command. Only one PIPE_CONTROL
232 * needs to be sent before any combination of VS associated 3DSTATE."
233 */
234 blorp_emit(batch, GENX(PIPE_CONTROL), pc) {
235 pc.DepthStallEnable = true;
236 pc.PostSyncOperation = WriteImmediateData;
237 pc.Address = blorp_get_workaround_page(batch);
238 }
239 #endif
240
241 for (int i = 0; i <= MESA_SHADER_GEOMETRY; i++) {
242 blorp_emit(batch, GENX(3DSTATE_URB_VS), urb) {
243 urb._3DCommandSubOpcode += i;
244 urb.VSURBStartingAddress = start[i];
245 urb.VSURBEntryAllocationSize = entry_size[i] - 1;
246 urb.VSNumberofURBEntries = entries[i];
247 }
248 }
249 #else /* GEN_GEN < 7 */
250 blorp_emit_urb_config(batch, vs_entry_size, sf_entry_size);
251 #endif
252 }
253
254 #if GEN_GEN >= 7
255 static void
256 blorp_emit_memcpy(struct blorp_batch *batch,
257 struct blorp_address dst,
258 struct blorp_address src,
259 uint32_t size);
260 #endif
261
262 static void
263 blorp_emit_vertex_data(struct blorp_batch *batch,
264 const struct blorp_params *params,
265 struct blorp_address *addr,
266 uint32_t *size)
267 {
268 const float vertices[] = {
269 /* v0 */ (float)params->x1, (float)params->y1, params->z,
270 /* v1 */ (float)params->x0, (float)params->y1, params->z,
271 /* v2 */ (float)params->x0, (float)params->y0, params->z,
272 };
273
274 void *data = blorp_alloc_vertex_buffer(batch, sizeof(vertices), addr);
275 memcpy(data, vertices, sizeof(vertices));
276 *size = sizeof(vertices);
277 blorp_flush_range(batch, data, *size);
278 }
279
280 static void
281 blorp_emit_input_varying_data(struct blorp_batch *batch,
282 const struct blorp_params *params,
283 struct blorp_address *addr,
284 uint32_t *size)
285 {
286 const unsigned vec4_size_in_bytes = 4 * sizeof(float);
287 const unsigned max_num_varyings =
288 DIV_ROUND_UP(sizeof(params->wm_inputs), vec4_size_in_bytes);
289 const unsigned num_varyings =
290 params->wm_prog_data ? params->wm_prog_data->num_varying_inputs : 0;
291
292 *size = 16 + num_varyings * vec4_size_in_bytes;
293
294 const uint32_t *const inputs_src = (const uint32_t *)&params->wm_inputs;
295 void *data = blorp_alloc_vertex_buffer(batch, *size, addr);
296 uint32_t *inputs = data;
297
298 /* Copy in the VS inputs */
299 assert(sizeof(params->vs_inputs) == 16);
300 memcpy(inputs, &params->vs_inputs, sizeof(params->vs_inputs));
301 inputs += 4;
302
303 if (params->wm_prog_data) {
304 /* Walk over the attribute slots, determine if the attribute is used by
305 * the program and when necessary copy the values from the input storage
306 * to the vertex data buffer.
307 */
308 for (unsigned i = 0; i < max_num_varyings; i++) {
309 const gl_varying_slot attr = VARYING_SLOT_VAR0 + i;
310
311 const int input_index = params->wm_prog_data->urb_setup[attr];
312 if (input_index < 0)
313 continue;
314
315 memcpy(inputs, inputs_src + i * 4, vec4_size_in_bytes);
316
317 inputs += 4;
318 }
319 }
320
321 blorp_flush_range(batch, data, *size);
322
323 if (params->dst_clear_color_as_input) {
324 #if GEN_GEN >= 7
325 /* In this case, the clear color isn't known statically and instead
326 * comes in through an indirect which we have to copy into the vertex
327 * buffer before we execute the 3DPRIMITIVE. We already copied the
328 * value of params->wm_inputs.clear_color into the vertex buffer in the
329 * loop above. Now we emit code to stomp it from the GPU with the
330 * actual clear color value.
331 */
332 assert(num_varyings == 1);
333
334 /* The clear color is the first thing after the header */
335 struct blorp_address clear_color_input_addr = *addr;
336 clear_color_input_addr.offset += 16;
337
338 const unsigned clear_color_size =
339 GEN_GEN < 10 ? batch->blorp->isl_dev->ss.clear_value_size : 4 * 4;
340 blorp_emit_memcpy(batch, clear_color_input_addr,
341 params->dst.clear_color_addr,
342 clear_color_size);
343 #else
344 unreachable("MCS partial resolve is not a thing on SNB and earlier");
345 #endif
346 }
347 }
348
349 static void
350 blorp_fill_vertex_buffer_state(struct blorp_batch *batch,
351 struct GENX(VERTEX_BUFFER_STATE) *vb,
352 unsigned idx,
353 struct blorp_address addr, uint32_t size,
354 uint32_t stride)
355 {
356 vb[idx].VertexBufferIndex = idx;
357 vb[idx].BufferStartingAddress = addr;
358 vb[idx].BufferPitch = stride;
359
360 #if GEN_GEN >= 6
361 vb[idx].MOCS = addr.mocs;
362 #endif
363
364 #if GEN_GEN >= 7
365 vb[idx].AddressModifyEnable = true;
366 #endif
367
368 #if GEN_GEN >= 8
369 vb[idx].BufferSize = size;
370 #elif GEN_GEN >= 5
371 vb[idx].BufferAccessType = stride > 0 ? VERTEXDATA : INSTANCEDATA;
372 vb[idx].EndAddress = vb[idx].BufferStartingAddress;
373 vb[idx].EndAddress.offset += size - 1;
374 #elif GEN_GEN == 4
375 vb[idx].BufferAccessType = stride > 0 ? VERTEXDATA : INSTANCEDATA;
376 vb[idx].MaxIndex = stride > 0 ? size / stride : 0;
377 #endif
378 }
379
380 static void
381 blorp_emit_vertex_buffers(struct blorp_batch *batch,
382 const struct blorp_params *params)
383 {
384 struct GENX(VERTEX_BUFFER_STATE) vb[3];
385 uint32_t num_vbs = 2;
386 memset(vb, 0, sizeof(vb));
387
388 struct blorp_address addrs[2] = {};
389 uint32_t sizes[2];
390 blorp_emit_vertex_data(batch, params, &addrs[0], &sizes[0]);
391 blorp_fill_vertex_buffer_state(batch, vb, 0, addrs[0], sizes[0],
392 3 * sizeof(float));
393
394 blorp_emit_input_varying_data(batch, params, &addrs[1], &sizes[1]);
395 blorp_fill_vertex_buffer_state(batch, vb, 1, addrs[1], sizes[1], 0);
396
397 blorp_vf_invalidate_for_vb_48b_transitions(batch, addrs, sizes, num_vbs);
398
399 const unsigned num_dwords = 1 + num_vbs * GENX(VERTEX_BUFFER_STATE_length);
400 uint32_t *dw = blorp_emitn(batch, GENX(3DSTATE_VERTEX_BUFFERS), num_dwords);
401 if (!dw)
402 return;
403
404 for (unsigned i = 0; i < num_vbs; i++) {
405 GENX(VERTEX_BUFFER_STATE_pack)(batch, dw, &vb[i]);
406 dw += GENX(VERTEX_BUFFER_STATE_length);
407 }
408 }
409
410 static void
411 blorp_emit_vertex_elements(struct blorp_batch *batch,
412 const struct blorp_params *params)
413 {
414 const unsigned num_varyings =
415 params->wm_prog_data ? params->wm_prog_data->num_varying_inputs : 0;
416 bool need_ndc = batch->blorp->compiler->devinfo->gen <= 5;
417 const unsigned num_elements = 2 + need_ndc + num_varyings;
418
419 struct GENX(VERTEX_ELEMENT_STATE) ve[num_elements];
420 memset(ve, 0, num_elements * sizeof(*ve));
421
422 /* Setup VBO for the rectangle primitive..
423 *
424 * A rectangle primitive (3DPRIM_RECTLIST) consists of only three
425 * vertices. The vertices reside in screen space with DirectX
426 * coordinates (that is, (0, 0) is the upper left corner).
427 *
428 * v2 ------ implied
429 * | |
430 * | |
431 * v1 ----- v0
432 *
433 * Since the VS is disabled, the clipper loads each VUE directly from
434 * the URB. This is controlled by the 3DSTATE_VERTEX_BUFFERS and
435 * 3DSTATE_VERTEX_ELEMENTS packets below. The VUE contents are as follows:
436 * dw0: Reserved, MBZ.
437 * dw1: Render Target Array Index. Below vertex fetcher gets programmed
438 * to assign this with primitive instance identifier which will be
439 * used for layered clears. All other renders have only one instance
440 * and therefore the value will be effectively zero.
441 * dw2: Viewport Index. The HiZ op disables viewport mapping and
442 * scissoring, so set the dword to 0.
443 * dw3: Point Width: The HiZ op does not emit the POINTLIST primitive,
444 * so set the dword to 0.
445 * dw4: Vertex Position X.
446 * dw5: Vertex Position Y.
447 * dw6: Vertex Position Z.
448 * dw7: Vertex Position W.
449 *
450 * dw8: Flat vertex input 0
451 * dw9: Flat vertex input 1
452 * ...
453 * dwn: Flat vertex input n - 8
454 *
455 * For details, see the Sandybridge PRM, Volume 2, Part 1, Section 1.5.1
456 * "Vertex URB Entry (VUE) Formats".
457 *
458 * Only vertex position X and Y are going to be variable, Z is fixed to
459 * zero and W to one. Header words dw0,2,3 are zero. There is no need to
460 * include the fixed values in the vertex buffer. Vertex fetcher can be
461 * instructed to fill vertex elements with constant values of one and zero
462 * instead of reading them from the buffer.
463 * Flat inputs are program constants that are not interpolated. Moreover
464 * their values will be the same between vertices.
465 *
466 * See the vertex element setup below.
467 */
468 unsigned slot = 0;
469
470 ve[slot] = (struct GENX(VERTEX_ELEMENT_STATE)) {
471 .VertexBufferIndex = 1,
472 .Valid = true,
473 .SourceElementFormat = ISL_FORMAT_R32G32B32A32_FLOAT,
474 .SourceElementOffset = 0,
475 .Component0Control = VFCOMP_STORE_SRC,
476
477 /* From Gen8 onwards hardware is no more instructed to overwrite
478 * components using an element specifier. Instead one has separate
479 * 3DSTATE_VF_SGVS (System Generated Value Setup) state packet for it.
480 */
481 #if GEN_GEN >= 8
482 .Component1Control = VFCOMP_STORE_0,
483 #elif GEN_GEN >= 5
484 .Component1Control = VFCOMP_STORE_IID,
485 #else
486 .Component1Control = VFCOMP_STORE_0,
487 #endif
488 .Component2Control = VFCOMP_STORE_0,
489 .Component3Control = VFCOMP_STORE_0,
490 #if GEN_GEN <= 5
491 .DestinationElementOffset = slot * 4,
492 #endif
493 };
494 slot++;
495
496 #if GEN_GEN <= 5
497 /* On Iron Lake and earlier, a native device coordinates version of the
498 * position goes right after the normal VUE header and before position.
499 * Since w == 1 for all of our coordinates, this is just a copy of the
500 * position.
501 */
502 ve[slot] = (struct GENX(VERTEX_ELEMENT_STATE)) {
503 .VertexBufferIndex = 0,
504 .Valid = true,
505 .SourceElementFormat = ISL_FORMAT_R32G32B32_FLOAT,
506 .SourceElementOffset = 0,
507 .Component0Control = VFCOMP_STORE_SRC,
508 .Component1Control = VFCOMP_STORE_SRC,
509 .Component2Control = VFCOMP_STORE_SRC,
510 .Component3Control = VFCOMP_STORE_1_FP,
511 .DestinationElementOffset = slot * 4,
512 };
513 slot++;
514 #endif
515
516 ve[slot] = (struct GENX(VERTEX_ELEMENT_STATE)) {
517 .VertexBufferIndex = 0,
518 .Valid = true,
519 .SourceElementFormat = ISL_FORMAT_R32G32B32_FLOAT,
520 .SourceElementOffset = 0,
521 .Component0Control = VFCOMP_STORE_SRC,
522 .Component1Control = VFCOMP_STORE_SRC,
523 .Component2Control = VFCOMP_STORE_SRC,
524 .Component3Control = VFCOMP_STORE_1_FP,
525 #if GEN_GEN <= 5
526 .DestinationElementOffset = slot * 4,
527 #endif
528 };
529 slot++;
530
531 for (unsigned i = 0; i < num_varyings; ++i) {
532 ve[slot] = (struct GENX(VERTEX_ELEMENT_STATE)) {
533 .VertexBufferIndex = 1,
534 .Valid = true,
535 .SourceElementFormat = ISL_FORMAT_R32G32B32A32_FLOAT,
536 .SourceElementOffset = 16 + i * 4 * sizeof(float),
537 .Component0Control = VFCOMP_STORE_SRC,
538 .Component1Control = VFCOMP_STORE_SRC,
539 .Component2Control = VFCOMP_STORE_SRC,
540 .Component3Control = VFCOMP_STORE_SRC,
541 #if GEN_GEN <= 5
542 .DestinationElementOffset = slot * 4,
543 #endif
544 };
545 slot++;
546 }
547
548 const unsigned num_dwords =
549 1 + GENX(VERTEX_ELEMENT_STATE_length) * num_elements;
550 uint32_t *dw = blorp_emitn(batch, GENX(3DSTATE_VERTEX_ELEMENTS), num_dwords);
551 if (!dw)
552 return;
553
554 for (unsigned i = 0; i < num_elements; i++) {
555 GENX(VERTEX_ELEMENT_STATE_pack)(batch, dw, &ve[i]);
556 dw += GENX(VERTEX_ELEMENT_STATE_length);
557 }
558
559 blorp_emit(batch, GENX(3DSTATE_VF_STATISTICS), vf) {
560 vf.StatisticsEnable = false;
561 }
562
563 #if GEN_GEN >= 8
564 /* Overwrite Render Target Array Index (2nd dword) in the VUE header with
565 * primitive instance identifier. This is used for layered clears.
566 */
567 blorp_emit(batch, GENX(3DSTATE_VF_SGVS), sgvs) {
568 sgvs.InstanceIDEnable = true;
569 sgvs.InstanceIDComponentNumber = COMP_1;
570 sgvs.InstanceIDElementOffset = 0;
571 }
572
573 for (unsigned i = 0; i < num_elements; i++) {
574 blorp_emit(batch, GENX(3DSTATE_VF_INSTANCING), vf) {
575 vf.VertexElementIndex = i;
576 vf.InstancingEnable = false;
577 }
578 }
579
580 blorp_emit(batch, GENX(3DSTATE_VF_TOPOLOGY), topo) {
581 topo.PrimitiveTopologyType = _3DPRIM_RECTLIST;
582 }
583 #endif
584 }
585
586 /* 3DSTATE_VIEWPORT_STATE_POINTERS */
587 static uint32_t
588 blorp_emit_cc_viewport(struct blorp_batch *batch)
589 {
590 uint32_t cc_vp_offset;
591 blorp_emit_dynamic(batch, GENX(CC_VIEWPORT), vp, 32, &cc_vp_offset) {
592 vp.MinimumDepth = 0.0;
593 vp.MaximumDepth = 1.0;
594 }
595
596 #if GEN_GEN >= 7
597 blorp_emit(batch, GENX(3DSTATE_VIEWPORT_STATE_POINTERS_CC), vsp) {
598 vsp.CCViewportPointer = cc_vp_offset;
599 }
600 #elif GEN_GEN == 6
601 blorp_emit(batch, GENX(3DSTATE_VIEWPORT_STATE_POINTERS), vsp) {
602 vsp.CCViewportStateChange = true;
603 vsp.PointertoCC_VIEWPORT = cc_vp_offset;
604 }
605 #endif
606
607 return cc_vp_offset;
608 }
609
610 static uint32_t
611 blorp_emit_sampler_state(struct blorp_batch *batch)
612 {
613 uint32_t offset;
614 blorp_emit_dynamic(batch, GENX(SAMPLER_STATE), sampler, 32, &offset) {
615 sampler.MipModeFilter = MIPFILTER_NONE;
616 sampler.MagModeFilter = MAPFILTER_LINEAR;
617 sampler.MinModeFilter = MAPFILTER_LINEAR;
618 sampler.MinLOD = 0;
619 sampler.MaxLOD = 0;
620 sampler.TCXAddressControlMode = TCM_CLAMP;
621 sampler.TCYAddressControlMode = TCM_CLAMP;
622 sampler.TCZAddressControlMode = TCM_CLAMP;
623 sampler.MaximumAnisotropy = RATIO21;
624 sampler.RAddressMinFilterRoundingEnable = true;
625 sampler.RAddressMagFilterRoundingEnable = true;
626 sampler.VAddressMinFilterRoundingEnable = true;
627 sampler.VAddressMagFilterRoundingEnable = true;
628 sampler.UAddressMinFilterRoundingEnable = true;
629 sampler.UAddressMagFilterRoundingEnable = true;
630 #if GEN_GEN > 6
631 sampler.NonnormalizedCoordinateEnable = true;
632 #endif
633 }
634
635 #if GEN_GEN >= 7
636 blorp_emit(batch, GENX(3DSTATE_SAMPLER_STATE_POINTERS_PS), ssp) {
637 ssp.PointertoPSSamplerState = offset;
638 }
639 #elif GEN_GEN == 6
640 blorp_emit(batch, GENX(3DSTATE_SAMPLER_STATE_POINTERS), ssp) {
641 ssp.VSSamplerStateChange = true;
642 ssp.GSSamplerStateChange = true;
643 ssp.PSSamplerStateChange = true;
644 ssp.PointertoPSSamplerState = offset;
645 }
646 #endif
647
648 return offset;
649 }
650
651 /* What follows is the code for setting up a "pipeline" on Sandy Bridge and
652 * later hardware. This file will be included by i965 for gen4-5 as well, so
653 * this code is guarded by GEN_GEN >= 6.
654 */
655 #if GEN_GEN >= 6
656
657 static void
658 blorp_emit_vs_config(struct blorp_batch *batch,
659 const struct blorp_params *params)
660 {
661 struct brw_vs_prog_data *vs_prog_data = params->vs_prog_data;
662 assert(!vs_prog_data || GEN_GEN < 11 ||
663 vs_prog_data->base.dispatch_mode == DISPATCH_MODE_SIMD8);
664
665 blorp_emit(batch, GENX(3DSTATE_VS), vs) {
666 if (vs_prog_data) {
667 vs.Enable = true;
668
669 vs.KernelStartPointer = params->vs_prog_kernel;
670
671 vs.DispatchGRFStartRegisterForURBData =
672 vs_prog_data->base.base.dispatch_grf_start_reg;
673 vs.VertexURBEntryReadLength =
674 vs_prog_data->base.urb_read_length;
675 vs.VertexURBEntryReadOffset = 0;
676
677 vs.MaximumNumberofThreads =
678 batch->blorp->isl_dev->info->max_vs_threads - 1;
679
680 #if GEN_GEN >= 8
681 vs.SIMD8DispatchEnable =
682 vs_prog_data->base.dispatch_mode == DISPATCH_MODE_SIMD8;
683 #endif
684 }
685 }
686 }
687
688 static void
689 blorp_emit_sf_config(struct blorp_batch *batch,
690 const struct blorp_params *params,
691 enum gen_urb_deref_block_size urb_deref_block_size)
692 {
693 const struct brw_wm_prog_data *prog_data = params->wm_prog_data;
694
695 /* 3DSTATE_SF
696 *
697 * Disable ViewportTransformEnable (dw2.1)
698 *
699 * From the SandyBridge PRM, Volume 2, Part 1, Section 1.3, "3D
700 * Primitives Overview":
701 * RECTLIST: Viewport Mapping must be DISABLED (as is typical with the
702 * use of screen- space coordinates).
703 *
704 * A solid rectangle must be rendered, so set FrontFaceFillMode (dw2.4:3)
705 * and BackFaceFillMode (dw2.5:6) to SOLID(0).
706 *
707 * From the Sandy Bridge PRM, Volume 2, Part 1, Section
708 * 6.4.1.1 3DSTATE_SF, Field FrontFaceFillMode:
709 * SOLID: Any triangle or rectangle object found to be front-facing
710 * is rendered as a solid object. This setting is required when
711 * (rendering rectangle (RECTLIST) objects.
712 */
713
714 #if GEN_GEN >= 8
715
716 blorp_emit(batch, GENX(3DSTATE_SF), sf) {
717 #if GEN_GEN >= 12
718 sf.DerefBlockSize = urb_deref_block_size;
719 #endif
720 }
721
722 blorp_emit(batch, GENX(3DSTATE_RASTER), raster) {
723 raster.CullMode = CULLMODE_NONE;
724 }
725
726 blorp_emit(batch, GENX(3DSTATE_SBE), sbe) {
727 sbe.VertexURBEntryReadOffset = 1;
728 if (prog_data) {
729 sbe.NumberofSFOutputAttributes = prog_data->num_varying_inputs;
730 sbe.VertexURBEntryReadLength = brw_blorp_get_urb_length(prog_data);
731 sbe.ConstantInterpolationEnable = prog_data->flat_inputs;
732 } else {
733 sbe.NumberofSFOutputAttributes = 0;
734 sbe.VertexURBEntryReadLength = 1;
735 }
736 sbe.ForceVertexURBEntryReadLength = true;
737 sbe.ForceVertexURBEntryReadOffset = true;
738
739 #if GEN_GEN >= 9
740 for (unsigned i = 0; i < 32; i++)
741 sbe.AttributeActiveComponentFormat[i] = ACF_XYZW;
742 #endif
743 }
744
745 #elif GEN_GEN >= 7
746
747 blorp_emit(batch, GENX(3DSTATE_SF), sf) {
748 sf.FrontFaceFillMode = FILL_MODE_SOLID;
749 sf.BackFaceFillMode = FILL_MODE_SOLID;
750
751 sf.MultisampleRasterizationMode = params->num_samples > 1 ?
752 MSRASTMODE_ON_PATTERN : MSRASTMODE_OFF_PIXEL;
753
754 #if GEN_GEN == 7
755 sf.DepthBufferSurfaceFormat = params->depth_format;
756 #endif
757 }
758
759 blorp_emit(batch, GENX(3DSTATE_SBE), sbe) {
760 sbe.VertexURBEntryReadOffset = 1;
761 if (prog_data) {
762 sbe.NumberofSFOutputAttributes = prog_data->num_varying_inputs;
763 sbe.VertexURBEntryReadLength = brw_blorp_get_urb_length(prog_data);
764 sbe.ConstantInterpolationEnable = prog_data->flat_inputs;
765 } else {
766 sbe.NumberofSFOutputAttributes = 0;
767 sbe.VertexURBEntryReadLength = 1;
768 }
769 }
770
771 #else /* GEN_GEN <= 6 */
772
773 blorp_emit(batch, GENX(3DSTATE_SF), sf) {
774 sf.FrontFaceFillMode = FILL_MODE_SOLID;
775 sf.BackFaceFillMode = FILL_MODE_SOLID;
776
777 sf.MultisampleRasterizationMode = params->num_samples > 1 ?
778 MSRASTMODE_ON_PATTERN : MSRASTMODE_OFF_PIXEL;
779
780 sf.VertexURBEntryReadOffset = 1;
781 if (prog_data) {
782 sf.NumberofSFOutputAttributes = prog_data->num_varying_inputs;
783 sf.VertexURBEntryReadLength = brw_blorp_get_urb_length(prog_data);
784 sf.ConstantInterpolationEnable = prog_data->flat_inputs;
785 } else {
786 sf.NumberofSFOutputAttributes = 0;
787 sf.VertexURBEntryReadLength = 1;
788 }
789 }
790
791 #endif /* GEN_GEN */
792 }
793
794 static void
795 blorp_emit_ps_config(struct blorp_batch *batch,
796 const struct blorp_params *params)
797 {
798 const struct brw_wm_prog_data *prog_data = params->wm_prog_data;
799
800 /* Even when thread dispatch is disabled, max threads (dw5.25:31) must be
801 * nonzero to prevent the GPU from hanging. While the documentation doesn't
802 * mention this explicitly, it notes that the valid range for the field is
803 * [1,39] = [2,40] threads, which excludes zero.
804 *
805 * To be safe (and to minimize extraneous code) we go ahead and fully
806 * configure the WM state whether or not there is a WM program.
807 */
808
809 #if GEN_GEN >= 8
810
811 blorp_emit(batch, GENX(3DSTATE_WM), wm);
812
813 blorp_emit(batch, GENX(3DSTATE_PS), ps) {
814 if (params->src.enabled) {
815 ps.SamplerCount = 1; /* Up to 4 samplers */
816 ps.BindingTableEntryCount = 2;
817 } else {
818 ps.BindingTableEntryCount = 1;
819 }
820
821 /* SAMPLER_STATE prefetching is broken on Gen11 - WA_1606682166 */
822 if (GEN_GEN == 11)
823 ps.SamplerCount = 0;
824
825 if (prog_data) {
826 ps._8PixelDispatchEnable = prog_data->dispatch_8;
827 ps._16PixelDispatchEnable = prog_data->dispatch_16;
828 ps._32PixelDispatchEnable = prog_data->dispatch_32;
829
830 /* From the Sky Lake PRM 3DSTATE_PS::32 Pixel Dispatch Enable:
831 *
832 * "When NUM_MULTISAMPLES = 16 or FORCE_SAMPLE_COUNT = 16, SIMD32
833 * Dispatch must not be enabled for PER_PIXEL dispatch mode."
834 *
835 * Since 16x MSAA is first introduced on SKL, we don't need to apply
836 * the workaround on any older hardware.
837 */
838 if (GEN_GEN >= 9 && !prog_data->persample_dispatch &&
839 params->num_samples == 16) {
840 assert(ps._8PixelDispatchEnable || ps._16PixelDispatchEnable);
841 ps._32PixelDispatchEnable = false;
842 }
843
844 ps.DispatchGRFStartRegisterForConstantSetupData0 =
845 brw_wm_prog_data_dispatch_grf_start_reg(prog_data, ps, 0);
846 ps.DispatchGRFStartRegisterForConstantSetupData1 =
847 brw_wm_prog_data_dispatch_grf_start_reg(prog_data, ps, 1);
848 ps.DispatchGRFStartRegisterForConstantSetupData2 =
849 brw_wm_prog_data_dispatch_grf_start_reg(prog_data, ps, 2);
850
851 ps.KernelStartPointer0 = params->wm_prog_kernel +
852 brw_wm_prog_data_prog_offset(prog_data, ps, 0);
853 ps.KernelStartPointer1 = params->wm_prog_kernel +
854 brw_wm_prog_data_prog_offset(prog_data, ps, 1);
855 ps.KernelStartPointer2 = params->wm_prog_kernel +
856 brw_wm_prog_data_prog_offset(prog_data, ps, 2);
857 }
858
859 /* 3DSTATE_PS expects the number of threads per PSD, which is always 64
860 * for pre Gen11 and 128 for gen11+; On gen11+ If a programmed value is
861 * k, it implies 2(k+1) threads. It implicitly scales for different GT
862 * levels (which have some # of PSDs).
863 *
864 * In Gen8 the format is U8-2 whereas in Gen9+ it is U9-1.
865 */
866 if (GEN_GEN >= 9)
867 ps.MaximumNumberofThreadsPerPSD = 64 - 1;
868 else
869 ps.MaximumNumberofThreadsPerPSD = 64 - 2;
870
871 switch (params->fast_clear_op) {
872 case ISL_AUX_OP_NONE:
873 break;
874 #if GEN_GEN >= 10
875 case ISL_AUX_OP_AMBIGUATE:
876 ps.RenderTargetFastClearEnable = true;
877 ps.RenderTargetResolveType = FAST_CLEAR_0;
878 break;
879 #endif
880 #if GEN_GEN >= 9
881 case ISL_AUX_OP_PARTIAL_RESOLVE:
882 ps.RenderTargetResolveType = RESOLVE_PARTIAL;
883 break;
884 case ISL_AUX_OP_FULL_RESOLVE:
885 ps.RenderTargetResolveType = RESOLVE_FULL;
886 break;
887 #else
888 case ISL_AUX_OP_FULL_RESOLVE:
889 ps.RenderTargetResolveEnable = true;
890 break;
891 #endif
892 case ISL_AUX_OP_FAST_CLEAR:
893 ps.RenderTargetFastClearEnable = true;
894 break;
895 default:
896 unreachable("Invalid fast clear op");
897 }
898 }
899
900 blorp_emit(batch, GENX(3DSTATE_PS_EXTRA), psx) {
901 if (prog_data) {
902 psx.PixelShaderValid = true;
903 psx.AttributeEnable = prog_data->num_varying_inputs > 0;
904 psx.PixelShaderIsPerSample = prog_data->persample_dispatch;
905 psx.PixelShaderComputedDepthMode = prog_data->computed_depth_mode;
906 #if GEN_GEN >= 9
907 psx.PixelShaderComputesStencil = prog_data->computed_stencil;
908 #endif
909 }
910
911 if (params->src.enabled)
912 psx.PixelShaderKillsPixel = true;
913 }
914
915 #elif GEN_GEN >= 7
916
917 blorp_emit(batch, GENX(3DSTATE_WM), wm) {
918 switch (params->hiz_op) {
919 case ISL_AUX_OP_FAST_CLEAR:
920 wm.DepthBufferClear = true;
921 break;
922 case ISL_AUX_OP_FULL_RESOLVE:
923 wm.DepthBufferResolveEnable = true;
924 break;
925 case ISL_AUX_OP_AMBIGUATE:
926 wm.HierarchicalDepthBufferResolveEnable = true;
927 break;
928 case ISL_AUX_OP_NONE:
929 break;
930 default:
931 unreachable("not reached");
932 }
933
934 if (prog_data) {
935 wm.ThreadDispatchEnable = true;
936 wm.PixelShaderComputedDepthMode = prog_data->computed_depth_mode;
937 }
938
939 if (params->src.enabled)
940 wm.PixelShaderKillsPixel = true;
941
942 if (params->num_samples > 1) {
943 wm.MultisampleRasterizationMode = MSRASTMODE_ON_PATTERN;
944 wm.MultisampleDispatchMode =
945 (prog_data && prog_data->persample_dispatch) ?
946 MSDISPMODE_PERSAMPLE : MSDISPMODE_PERPIXEL;
947 } else {
948 wm.MultisampleRasterizationMode = MSRASTMODE_OFF_PIXEL;
949 wm.MultisampleDispatchMode = MSDISPMODE_PERSAMPLE;
950 }
951 }
952
953 blorp_emit(batch, GENX(3DSTATE_PS), ps) {
954 ps.MaximumNumberofThreads =
955 batch->blorp->isl_dev->info->max_wm_threads - 1;
956
957 #if GEN_IS_HASWELL
958 ps.SampleMask = 1;
959 #endif
960
961 if (prog_data) {
962 ps._8PixelDispatchEnable = prog_data->dispatch_8;
963 ps._16PixelDispatchEnable = prog_data->dispatch_16;
964 ps._32PixelDispatchEnable = prog_data->dispatch_32;
965
966 ps.DispatchGRFStartRegisterForConstantSetupData0 =
967 brw_wm_prog_data_dispatch_grf_start_reg(prog_data, ps, 0);
968 ps.DispatchGRFStartRegisterForConstantSetupData1 =
969 brw_wm_prog_data_dispatch_grf_start_reg(prog_data, ps, 1);
970 ps.DispatchGRFStartRegisterForConstantSetupData2 =
971 brw_wm_prog_data_dispatch_grf_start_reg(prog_data, ps, 2);
972
973 ps.KernelStartPointer0 = params->wm_prog_kernel +
974 brw_wm_prog_data_prog_offset(prog_data, ps, 0);
975 ps.KernelStartPointer1 = params->wm_prog_kernel +
976 brw_wm_prog_data_prog_offset(prog_data, ps, 1);
977 ps.KernelStartPointer2 = params->wm_prog_kernel +
978 brw_wm_prog_data_prog_offset(prog_data, ps, 2);
979
980 ps.AttributeEnable = prog_data->num_varying_inputs > 0;
981 } else {
982 /* Gen7 hardware gets angry if we don't enable at least one dispatch
983 * mode, so just enable 16-pixel dispatch if we don't have a program.
984 */
985 ps._16PixelDispatchEnable = true;
986 }
987
988 if (params->src.enabled)
989 ps.SamplerCount = 1; /* Up to 4 samplers */
990
991 switch (params->fast_clear_op) {
992 case ISL_AUX_OP_NONE:
993 break;
994 case ISL_AUX_OP_FULL_RESOLVE:
995 ps.RenderTargetResolveEnable = true;
996 break;
997 case ISL_AUX_OP_FAST_CLEAR:
998 ps.RenderTargetFastClearEnable = true;
999 break;
1000 default:
1001 unreachable("Invalid fast clear op");
1002 }
1003 }
1004
1005 #else /* GEN_GEN <= 6 */
1006
1007 blorp_emit(batch, GENX(3DSTATE_WM), wm) {
1008 wm.MaximumNumberofThreads =
1009 batch->blorp->isl_dev->info->max_wm_threads - 1;
1010
1011 switch (params->hiz_op) {
1012 case ISL_AUX_OP_FAST_CLEAR:
1013 wm.DepthBufferClear = true;
1014 break;
1015 case ISL_AUX_OP_FULL_RESOLVE:
1016 wm.DepthBufferResolveEnable = true;
1017 break;
1018 case ISL_AUX_OP_AMBIGUATE:
1019 wm.HierarchicalDepthBufferResolveEnable = true;
1020 break;
1021 case ISL_AUX_OP_NONE:
1022 break;
1023 default:
1024 unreachable("not reached");
1025 }
1026
1027 if (prog_data) {
1028 wm.ThreadDispatchEnable = true;
1029
1030 wm._8PixelDispatchEnable = prog_data->dispatch_8;
1031 wm._16PixelDispatchEnable = prog_data->dispatch_16;
1032 wm._32PixelDispatchEnable = prog_data->dispatch_32;
1033
1034 wm.DispatchGRFStartRegisterForConstantSetupData0 =
1035 brw_wm_prog_data_dispatch_grf_start_reg(prog_data, wm, 0);
1036 wm.DispatchGRFStartRegisterForConstantSetupData1 =
1037 brw_wm_prog_data_dispatch_grf_start_reg(prog_data, wm, 1);
1038 wm.DispatchGRFStartRegisterForConstantSetupData2 =
1039 brw_wm_prog_data_dispatch_grf_start_reg(prog_data, wm, 2);
1040
1041 wm.KernelStartPointer0 = params->wm_prog_kernel +
1042 brw_wm_prog_data_prog_offset(prog_data, wm, 0);
1043 wm.KernelStartPointer1 = params->wm_prog_kernel +
1044 brw_wm_prog_data_prog_offset(prog_data, wm, 1);
1045 wm.KernelStartPointer2 = params->wm_prog_kernel +
1046 brw_wm_prog_data_prog_offset(prog_data, wm, 2);
1047
1048 wm.NumberofSFOutputAttributes = prog_data->num_varying_inputs;
1049 }
1050
1051 if (params->src.enabled) {
1052 wm.SamplerCount = 1; /* Up to 4 samplers */
1053 wm.PixelShaderKillsPixel = true; /* TODO: temporarily smash on */
1054 }
1055
1056 if (params->num_samples > 1) {
1057 wm.MultisampleRasterizationMode = MSRASTMODE_ON_PATTERN;
1058 wm.MultisampleDispatchMode =
1059 (prog_data && prog_data->persample_dispatch) ?
1060 MSDISPMODE_PERSAMPLE : MSDISPMODE_PERPIXEL;
1061 } else {
1062 wm.MultisampleRasterizationMode = MSRASTMODE_OFF_PIXEL;
1063 wm.MultisampleDispatchMode = MSDISPMODE_PERSAMPLE;
1064 }
1065 }
1066
1067 #endif /* GEN_GEN */
1068 }
1069
1070 static uint32_t
1071 blorp_emit_blend_state(struct blorp_batch *batch,
1072 const struct blorp_params *params)
1073 {
1074 struct GENX(BLEND_STATE) blend;
1075 memset(&blend, 0, sizeof(blend));
1076
1077 uint32_t offset;
1078 int size = GENX(BLEND_STATE_length) * 4;
1079 size += GENX(BLEND_STATE_ENTRY_length) * 4 * params->num_draw_buffers;
1080 uint32_t *state = blorp_alloc_dynamic_state(batch, size, 64, &offset);
1081 uint32_t *pos = state;
1082
1083 GENX(BLEND_STATE_pack)(NULL, pos, &blend);
1084 pos += GENX(BLEND_STATE_length);
1085
1086 for (unsigned i = 0; i < params->num_draw_buffers; ++i) {
1087 struct GENX(BLEND_STATE_ENTRY) entry = {
1088 .PreBlendColorClampEnable = true,
1089 .PostBlendColorClampEnable = true,
1090 .ColorClampRange = COLORCLAMP_RTFORMAT,
1091
1092 .WriteDisableRed = params->color_write_disable[0],
1093 .WriteDisableGreen = params->color_write_disable[1],
1094 .WriteDisableBlue = params->color_write_disable[2],
1095 .WriteDisableAlpha = params->color_write_disable[3],
1096 };
1097 GENX(BLEND_STATE_ENTRY_pack)(NULL, pos, &entry);
1098 pos += GENX(BLEND_STATE_ENTRY_length);
1099 }
1100
1101 blorp_flush_range(batch, state, size);
1102
1103 #if GEN_GEN >= 7
1104 blorp_emit(batch, GENX(3DSTATE_BLEND_STATE_POINTERS), sp) {
1105 sp.BlendStatePointer = offset;
1106 #if GEN_GEN >= 8
1107 sp.BlendStatePointerValid = true;
1108 #endif
1109 }
1110 #endif
1111
1112 #if GEN_GEN >= 8
1113 blorp_emit(batch, GENX(3DSTATE_PS_BLEND), ps_blend) {
1114 ps_blend.HasWriteableRT = true;
1115 }
1116 #endif
1117
1118 return offset;
1119 }
1120
1121 static uint32_t
1122 blorp_emit_color_calc_state(struct blorp_batch *batch,
1123 UNUSED const struct blorp_params *params)
1124 {
1125 uint32_t offset;
1126 blorp_emit_dynamic(batch, GENX(COLOR_CALC_STATE), cc, 64, &offset) {
1127 #if GEN_GEN <= 8
1128 cc.StencilReferenceValue = params->stencil_ref;
1129 #endif
1130 }
1131
1132 #if GEN_GEN >= 7
1133 blorp_emit(batch, GENX(3DSTATE_CC_STATE_POINTERS), sp) {
1134 sp.ColorCalcStatePointer = offset;
1135 #if GEN_GEN >= 8
1136 sp.ColorCalcStatePointerValid = true;
1137 #endif
1138 }
1139 #endif
1140
1141 return offset;
1142 }
1143
1144 static uint32_t
1145 blorp_emit_depth_stencil_state(struct blorp_batch *batch,
1146 const struct blorp_params *params)
1147 {
1148 #if GEN_GEN >= 8
1149 struct GENX(3DSTATE_WM_DEPTH_STENCIL) ds = {
1150 GENX(3DSTATE_WM_DEPTH_STENCIL_header),
1151 };
1152 #else
1153 struct GENX(DEPTH_STENCIL_STATE) ds = { 0 };
1154 #endif
1155
1156 if (params->depth.enabled) {
1157 ds.DepthBufferWriteEnable = true;
1158
1159 switch (params->hiz_op) {
1160 /* See the following sections of the Sandy Bridge PRM, Volume 2, Part1:
1161 * - 7.5.3.1 Depth Buffer Clear
1162 * - 7.5.3.2 Depth Buffer Resolve
1163 * - 7.5.3.3 Hierarchical Depth Buffer Resolve
1164 */
1165 case ISL_AUX_OP_FULL_RESOLVE:
1166 ds.DepthTestEnable = true;
1167 ds.DepthTestFunction = COMPAREFUNCTION_NEVER;
1168 break;
1169
1170 case ISL_AUX_OP_NONE:
1171 case ISL_AUX_OP_FAST_CLEAR:
1172 case ISL_AUX_OP_AMBIGUATE:
1173 ds.DepthTestEnable = false;
1174 break;
1175 case ISL_AUX_OP_PARTIAL_RESOLVE:
1176 unreachable("Invalid HIZ op");
1177 }
1178 }
1179
1180 if (params->stencil.enabled) {
1181 ds.StencilBufferWriteEnable = true;
1182 ds.StencilTestEnable = true;
1183 ds.DoubleSidedStencilEnable = false;
1184
1185 ds.StencilTestFunction = COMPAREFUNCTION_ALWAYS;
1186 ds.StencilPassDepthPassOp = STENCILOP_REPLACE;
1187
1188 ds.StencilWriteMask = params->stencil_mask;
1189 #if GEN_GEN >= 9
1190 ds.StencilReferenceValue = params->stencil_ref;
1191 #endif
1192 }
1193
1194 #if GEN_GEN >= 8
1195 uint32_t offset = 0;
1196 uint32_t *dw = blorp_emit_dwords(batch,
1197 GENX(3DSTATE_WM_DEPTH_STENCIL_length));
1198 if (!dw)
1199 return 0;
1200
1201 GENX(3DSTATE_WM_DEPTH_STENCIL_pack)(NULL, dw, &ds);
1202 #else
1203 uint32_t offset;
1204 void *state = blorp_alloc_dynamic_state(batch,
1205 GENX(DEPTH_STENCIL_STATE_length) * 4,
1206 64, &offset);
1207 GENX(DEPTH_STENCIL_STATE_pack)(NULL, state, &ds);
1208 blorp_flush_range(batch, state, GENX(DEPTH_STENCIL_STATE_length) * 4);
1209 #endif
1210
1211 #if GEN_GEN == 7
1212 blorp_emit(batch, GENX(3DSTATE_DEPTH_STENCIL_STATE_POINTERS), sp) {
1213 sp.PointertoDEPTH_STENCIL_STATE = offset;
1214 }
1215 #endif
1216
1217 return offset;
1218 }
1219
1220 static void
1221 blorp_emit_3dstate_multisample(struct blorp_batch *batch,
1222 const struct blorp_params *params)
1223 {
1224 blorp_emit(batch, GENX(3DSTATE_MULTISAMPLE), ms) {
1225 ms.NumberofMultisamples = __builtin_ffs(params->num_samples) - 1;
1226
1227 #if GEN_GEN >= 8
1228 /* The PRM says that this bit is valid only for DX9:
1229 *
1230 * SW can choose to set this bit only for DX9 API. DX10/OGL API's
1231 * should not have any effect by setting or not setting this bit.
1232 */
1233 ms.PixelPositionOffsetEnable = false;
1234 #elif GEN_GEN >= 7
1235
1236 switch (params->num_samples) {
1237 case 1:
1238 GEN_SAMPLE_POS_1X(ms.Sample);
1239 break;
1240 case 2:
1241 GEN_SAMPLE_POS_2X(ms.Sample);
1242 break;
1243 case 4:
1244 GEN_SAMPLE_POS_4X(ms.Sample);
1245 break;
1246 case 8:
1247 GEN_SAMPLE_POS_8X(ms.Sample);
1248 break;
1249 default:
1250 break;
1251 }
1252 #else
1253 GEN_SAMPLE_POS_4X(ms.Sample);
1254 #endif
1255 ms.PixelLocation = CENTER;
1256 }
1257 }
1258
1259 static void
1260 blorp_emit_pipeline(struct blorp_batch *batch,
1261 const struct blorp_params *params)
1262 {
1263 uint32_t blend_state_offset = 0;
1264 uint32_t color_calc_state_offset;
1265 uint32_t depth_stencil_state_offset;
1266
1267 enum gen_urb_deref_block_size urb_deref_block_size;
1268 emit_urb_config(batch, params, &urb_deref_block_size);
1269
1270 if (params->wm_prog_data) {
1271 blend_state_offset = blorp_emit_blend_state(batch, params);
1272 }
1273 color_calc_state_offset = blorp_emit_color_calc_state(batch, params);
1274 depth_stencil_state_offset = blorp_emit_depth_stencil_state(batch, params);
1275
1276 #if GEN_GEN == 6
1277 /* 3DSTATE_CC_STATE_POINTERS
1278 *
1279 * The pointer offsets are relative to
1280 * CMD_STATE_BASE_ADDRESS.DynamicStateBaseAddress.
1281 *
1282 * The HiZ op doesn't use BLEND_STATE or COLOR_CALC_STATE.
1283 *
1284 * The dynamic state emit helpers emit their own STATE_POINTERS packets on
1285 * gen7+. However, on gen6 and earlier, they're all lumpped together in
1286 * one CC_STATE_POINTERS packet so we have to emit that here.
1287 */
1288 blorp_emit(batch, GENX(3DSTATE_CC_STATE_POINTERS), cc) {
1289 cc.BLEND_STATEChange = true;
1290 cc.ColorCalcStatePointerValid = true;
1291 cc.DEPTH_STENCIL_STATEChange = true;
1292 cc.PointertoBLEND_STATE = blend_state_offset;
1293 cc.ColorCalcStatePointer = color_calc_state_offset;
1294 cc.PointertoDEPTH_STENCIL_STATE = depth_stencil_state_offset;
1295 }
1296 #else
1297 (void)blend_state_offset;
1298 (void)color_calc_state_offset;
1299 (void)depth_stencil_state_offset;
1300 #endif
1301
1302 #if GEN_GEN >= 12
1303 blorp_emit(batch, GENX(3DSTATE_CONSTANT_ALL), pc) {
1304 /* Update empty push constants for all stages (bitmask = 11111b) */
1305 pc.ShaderUpdateEnable = 0x1f;
1306 }
1307 #else
1308 blorp_emit(batch, GENX(3DSTATE_CONSTANT_VS), vs);
1309 #if GEN_GEN >= 7
1310 blorp_emit(batch, GENX(3DSTATE_CONSTANT_HS), hs);
1311 blorp_emit(batch, GENX(3DSTATE_CONSTANT_DS), DS);
1312 #endif
1313 blorp_emit(batch, GENX(3DSTATE_CONSTANT_GS), gs);
1314 blorp_emit(batch, GENX(3DSTATE_CONSTANT_PS), ps);
1315 #endif
1316
1317 if (params->src.enabled)
1318 blorp_emit_sampler_state(batch);
1319
1320 blorp_emit_3dstate_multisample(batch, params);
1321
1322 blorp_emit(batch, GENX(3DSTATE_SAMPLE_MASK), mask) {
1323 mask.SampleMask = (1 << params->num_samples) - 1;
1324 }
1325
1326 /* From the BSpec, 3D Pipeline > Geometry > Vertex Shader > State,
1327 * 3DSTATE_VS, Dword 5.0 "VS Function Enable":
1328 *
1329 * [DevSNB] A pipeline flush must be programmed prior to a
1330 * 3DSTATE_VS command that causes the VS Function Enable to
1331 * toggle. Pipeline flush can be executed by sending a PIPE_CONTROL
1332 * command with CS stall bit set and a post sync operation.
1333 *
1334 * We've already done one at the start of the BLORP operation.
1335 */
1336 blorp_emit_vs_config(batch, params);
1337 #if GEN_GEN >= 7
1338 blorp_emit(batch, GENX(3DSTATE_HS), hs);
1339 blorp_emit(batch, GENX(3DSTATE_TE), te);
1340 blorp_emit(batch, GENX(3DSTATE_DS), DS);
1341 blorp_emit(batch, GENX(3DSTATE_STREAMOUT), so);
1342 #endif
1343 blorp_emit(batch, GENX(3DSTATE_GS), gs);
1344
1345 blorp_emit(batch, GENX(3DSTATE_CLIP), clip) {
1346 clip.PerspectiveDivideDisable = true;
1347 }
1348
1349 blorp_emit_sf_config(batch, params, urb_deref_block_size);
1350 blorp_emit_ps_config(batch, params);
1351
1352 blorp_emit_cc_viewport(batch);
1353 }
1354
1355 /******** This is the end of the pipeline setup code ********/
1356
1357 #endif /* GEN_GEN >= 6 */
1358
1359 #if GEN_GEN >= 7
1360 static void
1361 blorp_emit_memcpy(struct blorp_batch *batch,
1362 struct blorp_address dst,
1363 struct blorp_address src,
1364 uint32_t size)
1365 {
1366 assert(size % 4 == 0);
1367
1368 for (unsigned dw = 0; dw < size; dw += 4) {
1369 #if GEN_GEN >= 8
1370 blorp_emit(batch, GENX(MI_COPY_MEM_MEM), cp) {
1371 cp.DestinationMemoryAddress = dst;
1372 cp.SourceMemoryAddress = src;
1373 }
1374 #else
1375 /* IVB does not have a general purpose register for command streamer
1376 * commands. Therefore, we use an alternate temporary register.
1377 */
1378 #define BLORP_TEMP_REG 0x2440 /* GEN7_3DPRIM_BASE_VERTEX */
1379 blorp_emit(batch, GENX(MI_LOAD_REGISTER_MEM), load) {
1380 load.RegisterAddress = BLORP_TEMP_REG;
1381 load.MemoryAddress = src;
1382 }
1383 blorp_emit(batch, GENX(MI_STORE_REGISTER_MEM), store) {
1384 store.RegisterAddress = BLORP_TEMP_REG;
1385 store.MemoryAddress = dst;
1386 }
1387 #undef BLORP_TEMP_REG
1388 #endif
1389 dst.offset += 4;
1390 src.offset += 4;
1391 }
1392 }
1393 #endif
1394
1395 static void
1396 blorp_emit_surface_state(struct blorp_batch *batch,
1397 const struct brw_blorp_surface_info *surface,
1398 enum isl_aux_op aux_op,
1399 void *state, uint32_t state_offset,
1400 const bool color_write_disables[4],
1401 bool is_render_target)
1402 {
1403 const struct isl_device *isl_dev = batch->blorp->isl_dev;
1404 struct isl_surf surf = surface->surf;
1405
1406 if (surf.dim == ISL_SURF_DIM_1D &&
1407 surf.dim_layout == ISL_DIM_LAYOUT_GEN4_2D) {
1408 assert(surf.logical_level0_px.height == 1);
1409 surf.dim = ISL_SURF_DIM_2D;
1410 }
1411
1412 if (isl_aux_usage_has_hiz(surface->aux_usage)) {
1413 /* BLORP doesn't render with depth so we can't use HiZ */
1414 assert(!is_render_target);
1415 /* We can't reinterpret HiZ */
1416 assert(surface->surf.format == surface->view.format);
1417 }
1418 enum isl_aux_usage aux_usage = surface->aux_usage;
1419
1420 isl_channel_mask_t write_disable_mask = 0;
1421 if (is_render_target && GEN_GEN <= 5) {
1422 if (color_write_disables[0])
1423 write_disable_mask |= ISL_CHANNEL_RED_BIT;
1424 if (color_write_disables[1])
1425 write_disable_mask |= ISL_CHANNEL_GREEN_BIT;
1426 if (color_write_disables[2])
1427 write_disable_mask |= ISL_CHANNEL_BLUE_BIT;
1428 if (color_write_disables[3])
1429 write_disable_mask |= ISL_CHANNEL_ALPHA_BIT;
1430 }
1431
1432 const bool use_clear_address =
1433 GEN_GEN >= 10 && (surface->clear_color_addr.buffer != NULL);
1434
1435 isl_surf_fill_state(batch->blorp->isl_dev, state,
1436 .surf = &surf, .view = &surface->view,
1437 .aux_surf = &surface->aux_surf, .aux_usage = aux_usage,
1438 .address =
1439 blorp_get_surface_address(batch, surface->addr),
1440 .aux_address = aux_usage == ISL_AUX_USAGE_NONE ? 0 :
1441 blorp_get_surface_address(batch, surface->aux_addr),
1442 .clear_address = !use_clear_address ? 0 :
1443 blorp_get_surface_address(batch,
1444 surface->clear_color_addr),
1445 .mocs = surface->addr.mocs,
1446 .clear_color = surface->clear_color,
1447 .use_clear_address = use_clear_address,
1448 .write_disables = write_disable_mask);
1449
1450 blorp_surface_reloc(batch, state_offset + isl_dev->ss.addr_offset,
1451 surface->addr, 0);
1452
1453 if (aux_usage != ISL_AUX_USAGE_NONE) {
1454 /* On gen7 and prior, the bottom 12 bits of the MCS base address are
1455 * used to store other information. This should be ok, however, because
1456 * surface buffer addresses are always 4K page alinged.
1457 */
1458 assert((surface->aux_addr.offset & 0xfff) == 0);
1459 uint32_t *aux_addr = state + isl_dev->ss.aux_addr_offset;
1460 blorp_surface_reloc(batch, state_offset + isl_dev->ss.aux_addr_offset,
1461 surface->aux_addr, *aux_addr);
1462 }
1463
1464 if (aux_usage != ISL_AUX_USAGE_NONE && surface->clear_color_addr.buffer) {
1465 #if GEN_GEN >= 10
1466 assert((surface->clear_color_addr.offset & 0x3f) == 0);
1467 uint32_t *clear_addr = state + isl_dev->ss.clear_color_state_offset;
1468 blorp_surface_reloc(batch, state_offset +
1469 isl_dev->ss.clear_color_state_offset,
1470 surface->clear_color_addr, *clear_addr);
1471 #elif GEN_GEN >= 7
1472 /* Fast clears just whack the AUX surface and don't actually use the
1473 * clear color for anything. We can avoid the MI memcpy on that case.
1474 */
1475 if (aux_op != ISL_AUX_OP_FAST_CLEAR) {
1476 struct blorp_address dst_addr = blorp_get_surface_base_address(batch);
1477 dst_addr.offset += state_offset + isl_dev->ss.clear_value_offset;
1478 blorp_emit_memcpy(batch, dst_addr, surface->clear_color_addr,
1479 isl_dev->ss.clear_value_size);
1480 }
1481 #else
1482 unreachable("Fast clears are only supported on gen7+");
1483 #endif
1484 }
1485
1486 blorp_flush_range(batch, state, GENX(RENDER_SURFACE_STATE_length) * 4);
1487 }
1488
1489 static void
1490 blorp_emit_null_surface_state(struct blorp_batch *batch,
1491 const struct brw_blorp_surface_info *surface,
1492 uint32_t *state)
1493 {
1494 struct GENX(RENDER_SURFACE_STATE) ss = {
1495 .SurfaceType = SURFTYPE_NULL,
1496 .SurfaceFormat = ISL_FORMAT_R8G8B8A8_UNORM,
1497 .Width = surface->surf.logical_level0_px.width - 1,
1498 .Height = surface->surf.logical_level0_px.height - 1,
1499 .MIPCountLOD = surface->view.base_level,
1500 .MinimumArrayElement = surface->view.base_array_layer,
1501 .Depth = surface->view.array_len - 1,
1502 .RenderTargetViewExtent = surface->view.array_len - 1,
1503 #if GEN_GEN >= 6
1504 .NumberofMultisamples = ffs(surface->surf.samples) - 1,
1505 #endif
1506
1507 #if GEN_GEN >= 7
1508 .SurfaceArray = surface->surf.dim != ISL_SURF_DIM_3D,
1509 #endif
1510
1511 #if GEN_GEN >= 8
1512 .TileMode = YMAJOR,
1513 #else
1514 .TiledSurface = true,
1515 #endif
1516 };
1517
1518 GENX(RENDER_SURFACE_STATE_pack)(NULL, state, &ss);
1519
1520 blorp_flush_range(batch, state, GENX(RENDER_SURFACE_STATE_length) * 4);
1521 }
1522
1523 static void
1524 blorp_emit_surface_states(struct blorp_batch *batch,
1525 const struct blorp_params *params)
1526 {
1527 const struct isl_device *isl_dev = batch->blorp->isl_dev;
1528 uint32_t bind_offset = 0, surface_offsets[2];
1529 void *surface_maps[2];
1530
1531 UNUSED bool has_indirect_clear_color = false;
1532 if (params->use_pre_baked_binding_table) {
1533 bind_offset = params->pre_baked_binding_table_offset;
1534 } else {
1535 unsigned num_surfaces = 1 + params->src.enabled;
1536 blorp_alloc_binding_table(batch, num_surfaces,
1537 isl_dev->ss.size, isl_dev->ss.align,
1538 &bind_offset, surface_offsets, surface_maps);
1539
1540 if (params->dst.enabled) {
1541 blorp_emit_surface_state(batch, &params->dst,
1542 params->fast_clear_op,
1543 surface_maps[BLORP_RENDERBUFFER_BT_INDEX],
1544 surface_offsets[BLORP_RENDERBUFFER_BT_INDEX],
1545 params->color_write_disable, true);
1546 if (params->dst.clear_color_addr.buffer != NULL)
1547 has_indirect_clear_color = true;
1548 } else {
1549 assert(params->depth.enabled || params->stencil.enabled);
1550 const struct brw_blorp_surface_info *surface =
1551 params->depth.enabled ? &params->depth : &params->stencil;
1552 blorp_emit_null_surface_state(batch, surface,
1553 surface_maps[BLORP_RENDERBUFFER_BT_INDEX]);
1554 }
1555
1556 if (params->src.enabled) {
1557 blorp_emit_surface_state(batch, &params->src,
1558 params->fast_clear_op,
1559 surface_maps[BLORP_TEXTURE_BT_INDEX],
1560 surface_offsets[BLORP_TEXTURE_BT_INDEX],
1561 NULL, false);
1562 if (params->src.clear_color_addr.buffer != NULL)
1563 has_indirect_clear_color = true;
1564 }
1565 }
1566
1567 #if GEN_GEN >= 7
1568 if (has_indirect_clear_color) {
1569 /* Updating a surface state object may require that the state cache be
1570 * invalidated. From the SKL PRM, Shared Functions -> State -> State
1571 * Caching:
1572 *
1573 * Whenever the RENDER_SURFACE_STATE object in memory pointed to by
1574 * the Binding Table Pointer (BTP) and Binding Table Index (BTI) is
1575 * modified [...], the L1 state cache must be invalidated to ensure
1576 * the new surface or sampler state is fetched from system memory.
1577 */
1578 blorp_emit(batch, GENX(PIPE_CONTROL), pipe) {
1579 pipe.StateCacheInvalidationEnable = true;
1580 #if GEN_GEN >= 12
1581 pipe.TileCacheFlushEnable = true;
1582 #endif
1583 }
1584 }
1585 #endif
1586
1587 #if GEN_GEN >= 7
1588 blorp_emit(batch, GENX(3DSTATE_BINDING_TABLE_POINTERS_VS), bt);
1589 blorp_emit(batch, GENX(3DSTATE_BINDING_TABLE_POINTERS_HS), bt);
1590 blorp_emit(batch, GENX(3DSTATE_BINDING_TABLE_POINTERS_DS), bt);
1591 blorp_emit(batch, GENX(3DSTATE_BINDING_TABLE_POINTERS_GS), bt);
1592
1593 blorp_emit(batch, GENX(3DSTATE_BINDING_TABLE_POINTERS_PS), bt) {
1594 bt.PointertoPSBindingTable = bind_offset;
1595 }
1596 #elif GEN_GEN >= 6
1597 blorp_emit(batch, GENX(3DSTATE_BINDING_TABLE_POINTERS), bt) {
1598 bt.PSBindingTableChange = true;
1599 bt.PointertoPSBindingTable = bind_offset;
1600 }
1601 #else
1602 blorp_emit(batch, GENX(3DSTATE_BINDING_TABLE_POINTERS), bt) {
1603 bt.PointertoPSBindingTable = bind_offset;
1604 }
1605 #endif
1606 }
1607
1608 static void
1609 blorp_emit_depth_stencil_config(struct blorp_batch *batch,
1610 const struct blorp_params *params)
1611 {
1612 const struct isl_device *isl_dev = batch->blorp->isl_dev;
1613
1614 uint32_t *dw = blorp_emit_dwords(batch, isl_dev->ds.size / 4);
1615 if (dw == NULL)
1616 return;
1617
1618 struct isl_depth_stencil_hiz_emit_info info = { };
1619
1620 if (params->depth.enabled) {
1621 info.view = &params->depth.view;
1622 info.mocs = params->depth.addr.mocs;
1623 } else if (params->stencil.enabled) {
1624 info.view = &params->stencil.view;
1625 info.mocs = params->stencil.addr.mocs;
1626 }
1627
1628 if (params->depth.enabled) {
1629 info.depth_surf = &params->depth.surf;
1630
1631 info.depth_address =
1632 blorp_emit_reloc(batch, dw + isl_dev->ds.depth_offset / 4,
1633 params->depth.addr, 0);
1634
1635 info.hiz_usage = params->depth.aux_usage;
1636 if (isl_aux_usage_has_hiz(info.hiz_usage)) {
1637 info.hiz_surf = &params->depth.aux_surf;
1638
1639 struct blorp_address hiz_address = params->depth.aux_addr;
1640 #if GEN_GEN == 6
1641 /* Sandy bridge hardware does not technically support mipmapped HiZ.
1642 * However, we have a special layout that allows us to make it work
1643 * anyway by manually offsetting to the specified miplevel.
1644 */
1645 assert(info.hiz_surf->dim_layout == ISL_DIM_LAYOUT_GEN6_STENCIL_HIZ);
1646 uint32_t offset_B;
1647 isl_surf_get_image_offset_B_tile_sa(info.hiz_surf,
1648 info.view->base_level, 0, 0,
1649 &offset_B, NULL, NULL);
1650 hiz_address.offset += offset_B;
1651 #endif
1652
1653 info.hiz_address =
1654 blorp_emit_reloc(batch, dw + isl_dev->ds.hiz_offset / 4,
1655 hiz_address, 0);
1656
1657 info.depth_clear_value = params->depth.clear_color.f32[0];
1658 }
1659 }
1660
1661 if (params->stencil.enabled) {
1662 info.stencil_surf = &params->stencil.surf;
1663
1664 info.stencil_aux_usage = params->stencil.aux_usage;
1665 struct blorp_address stencil_address = params->stencil.addr;
1666 #if GEN_GEN == 6
1667 /* Sandy bridge hardware does not technically support mipmapped stencil.
1668 * However, we have a special layout that allows us to make it work
1669 * anyway by manually offsetting to the specified miplevel.
1670 */
1671 assert(info.stencil_surf->dim_layout == ISL_DIM_LAYOUT_GEN6_STENCIL_HIZ);
1672 uint32_t offset_B;
1673 isl_surf_get_image_offset_B_tile_sa(info.stencil_surf,
1674 info.view->base_level, 0, 0,
1675 &offset_B, NULL, NULL);
1676 stencil_address.offset += offset_B;
1677 #endif
1678
1679 info.stencil_address =
1680 blorp_emit_reloc(batch, dw + isl_dev->ds.stencil_offset / 4,
1681 stencil_address, 0);
1682 }
1683
1684 isl_emit_depth_stencil_hiz_s(isl_dev, dw, &info);
1685
1686 #if GEN_GEN >= 12
1687 /* GEN:BUG:1408224581
1688 *
1689 * Workaround: Gen12LP Astep only An additional pipe control with
1690 * post-sync = store dword operation would be required.( w/a is to
1691 * have an additional pipe control after the stencil state whenever
1692 * the surface state bits of this state is changing).
1693 */
1694 blorp_emit(batch, GENX(PIPE_CONTROL), pc) {
1695 pc.PostSyncOperation = WriteImmediateData;
1696 pc.Address = blorp_get_workaround_page(batch);
1697 }
1698 #endif
1699 }
1700
1701 #if GEN_GEN >= 8
1702 /* Emits the Optimized HiZ sequence specified in the BDW+ PRMs. The
1703 * depth/stencil buffer extents are ignored to handle APIs which perform
1704 * clearing operations without such information.
1705 * */
1706 static void
1707 blorp_emit_gen8_hiz_op(struct blorp_batch *batch,
1708 const struct blorp_params *params)
1709 {
1710 /* We should be performing an operation on a depth or stencil buffer.
1711 */
1712 assert(params->depth.enabled || params->stencil.enabled);
1713
1714 /* The stencil buffer should only be enabled on GEN == 12, if a fast clear
1715 * or full resolve operation is requested. On rest of the GEN, if a fast
1716 * clear operation is requested.
1717 */
1718 if (params->stencil.enabled) {
1719 #if GEN_GEN >= 12
1720 assert(params->hiz_op == ISL_AUX_OP_FAST_CLEAR ||
1721 params->hiz_op == ISL_AUX_OP_FULL_RESOLVE);
1722 #else
1723 assert(params->hiz_op == ISL_AUX_OP_FAST_CLEAR);
1724 #endif
1725 }
1726
1727 /* From the BDW PRM Volume 2, 3DSTATE_WM_HZ_OP:
1728 *
1729 * 3DSTATE_MULTISAMPLE packet must be used prior to this packet to change
1730 * the Number of Multisamples. This packet must not be used to change
1731 * Number of Multisamples in a rendering sequence.
1732 *
1733 * Since HIZ may be the first thing in a batch buffer, play safe and always
1734 * emit 3DSTATE_MULTISAMPLE.
1735 */
1736 blorp_emit_3dstate_multisample(batch, params);
1737
1738 /* From the BDW PRM Volume 7, Depth Buffer Clear:
1739 *
1740 * The clear value must be between the min and max depth values
1741 * (inclusive) defined in the CC_VIEWPORT. If the depth buffer format is
1742 * D32_FLOAT, then +/-DENORM values are also allowed.
1743 *
1744 * Set the bounds to match our hardware limits, [0.0, 1.0].
1745 */
1746 if (params->depth.enabled && params->hiz_op == ISL_AUX_OP_FAST_CLEAR) {
1747 assert(params->depth.clear_color.f32[0] >= 0.0f);
1748 assert(params->depth.clear_color.f32[0] <= 1.0f);
1749 blorp_emit_cc_viewport(batch);
1750 }
1751
1752 if (GEN_GEN >= 12 && params->stencil.enabled &&
1753 params->hiz_op == ISL_AUX_OP_FULL_RESOLVE) {
1754 /* GEN:BUG:1605967699
1755 *
1756 * This workaround requires that the Force Thread Dispatch Enable flag
1757 * needs to be set to ForceOFF on the first WM_HZ_OP state cycle
1758 * (followed by a CS Stall):
1759 *
1760 * "Workaround: There is a potential software workaround for the
1761 * issue by doing these 2 steps 1) setting the force thread dispatch
1762 * enable(bits 20:19) in the 3dstate_WM_body state to be set to
1763 * Force_OFF (value of 1) along with the first WM_HZ_OP state cycle.
1764 * The second WM_HZ_OP state which is required by programming
1765 * sequencing to complete the HZ_OP operation can reprogram the
1766 * 3dstate_WM_body to set to NORMAL(value of 0)."
1767 */
1768 blorp_emit(batch, GENX(3DSTATE_WM), wm) {
1769 wm.ForceThreadDispatchEnable = ForceOff;
1770 }
1771 blorp_emit(batch, GENX(PIPE_CONTROL), pipe) {
1772 pipe.CommandStreamerStallEnable = true;
1773 }
1774 } else {
1775 /* According to the SKL PRM formula for WM_INT::ThreadDispatchEnable, the
1776 * 3DSTATE_WM::ForceThreadDispatchEnable field can force WM thread dispatch
1777 * even when WM_HZ_OP is active. However, WM thread dispatch is normally
1778 * disabled for HiZ ops and it appears that force-enabling it can lead to
1779 * GPU hangs on at least Skylake. Since we don't know the current state of
1780 * the 3DSTATE_WM packet, just emit a dummy one prior to 3DSTATE_WM_HZ_OP.
1781 */
1782 blorp_emit(batch, GENX(3DSTATE_WM), wm);
1783 }
1784
1785 /* If we can't alter the depth stencil config and multiple layers are
1786 * involved, the HiZ op will fail. This is because the op requires that a
1787 * new config is emitted for each additional layer.
1788 */
1789 if (batch->flags & BLORP_BATCH_NO_EMIT_DEPTH_STENCIL) {
1790 assert(params->num_layers <= 1);
1791 } else {
1792 blorp_emit_depth_stencil_config(batch, params);
1793 }
1794
1795 blorp_emit(batch, GENX(3DSTATE_WM_HZ_OP), hzp) {
1796 switch (params->hiz_op) {
1797 case ISL_AUX_OP_FAST_CLEAR:
1798 hzp.StencilBufferClearEnable = params->stencil.enabled;
1799 hzp.DepthBufferClearEnable = params->depth.enabled;
1800 hzp.StencilClearValue = params->stencil_ref;
1801 hzp.FullSurfaceDepthandStencilClear = params->full_surface_hiz_op;
1802 break;
1803 case ISL_AUX_OP_FULL_RESOLVE:
1804 assert(params->full_surface_hiz_op);
1805 hzp.DepthBufferResolveEnable = params->depth.enabled;
1806 #if GEN_GEN >= 12
1807 if (params->stencil.enabled) {
1808 assert(params->stencil.aux_usage == ISL_AUX_USAGE_STC_CCS);
1809 hzp.StencilBufferResolveEnable = true;
1810 }
1811 #endif
1812 break;
1813 case ISL_AUX_OP_AMBIGUATE:
1814 assert(params->full_surface_hiz_op);
1815 hzp.HierarchicalDepthBufferResolveEnable = true;
1816 break;
1817 case ISL_AUX_OP_PARTIAL_RESOLVE:
1818 case ISL_AUX_OP_NONE:
1819 unreachable("Invalid HIZ op");
1820 }
1821
1822 hzp.NumberofMultisamples = ffs(params->num_samples) - 1;
1823 hzp.SampleMask = 0xFFFF;
1824
1825 /* Due to a hardware issue, this bit MBZ */
1826 assert(hzp.ScissorRectangleEnable == false);
1827
1828 /* Contrary to the HW docs both fields are inclusive */
1829 hzp.ClearRectangleXMin = params->x0;
1830 hzp.ClearRectangleYMin = params->y0;
1831
1832 /* Contrary to the HW docs both fields are exclusive */
1833 hzp.ClearRectangleXMax = params->x1;
1834 hzp.ClearRectangleYMax = params->y1;
1835 }
1836
1837 /* PIPE_CONTROL w/ all bits clear except for “Post-Sync Operation” must set
1838 * to “Write Immediate Data” enabled.
1839 */
1840 blorp_emit(batch, GENX(PIPE_CONTROL), pc) {
1841 pc.PostSyncOperation = WriteImmediateData;
1842 pc.Address = blorp_get_workaround_page(batch);
1843 }
1844
1845
1846 if (GEN_GEN >= 12 && params->stencil.enabled &&
1847 params->hiz_op == ISL_AUX_OP_FULL_RESOLVE) {
1848 /* GEN:BUG:1605967699
1849 *
1850 * The second WM_HZ_OP state which is required by programming
1851 * sequencing to complete the HZ_OP operation can reprogram the
1852 * 3dstate_WM_body to set to NORMAL(value of 0)."
1853 */
1854 blorp_emit(batch, GENX(3DSTATE_WM), wm);
1855 }
1856
1857 blorp_emit(batch, GENX(3DSTATE_WM_HZ_OP), hzp);
1858 }
1859 #endif
1860
1861 static void
1862 blorp_update_clear_color(struct blorp_batch *batch,
1863 const struct brw_blorp_surface_info *info,
1864 enum isl_aux_op op)
1865 {
1866 if (info->clear_color_addr.buffer && op == ISL_AUX_OP_FAST_CLEAR) {
1867 #if GEN_GEN == 11
1868 blorp_emit(batch, GENX(PIPE_CONTROL), pipe) {
1869 pipe.CommandStreamerStallEnable = true;
1870 }
1871
1872 /* 2 QWORDS */
1873 const unsigned inlinedata_dw = 2 * 2;
1874 const unsigned num_dwords = GENX(MI_ATOMIC_length) + inlinedata_dw;
1875
1876 struct blorp_address clear_addr = info->clear_color_addr;
1877 uint32_t *dw = blorp_emitn(batch, GENX(MI_ATOMIC), num_dwords,
1878 .DataSize = MI_ATOMIC_QWORD,
1879 .ATOMICOPCODE = MI_ATOMIC_OP_MOVE8B,
1880 .InlineData = true,
1881 .MemoryAddress = clear_addr);
1882 /* dw starts at dword 1, but we need to fill dwords 3 and 5 */
1883 dw[2] = info->clear_color.u32[0];
1884 dw[3] = 0;
1885 dw[4] = info->clear_color.u32[1];
1886 dw[5] = 0;
1887
1888 clear_addr.offset += 8;
1889 dw = blorp_emitn(batch, GENX(MI_ATOMIC), num_dwords,
1890 .DataSize = MI_ATOMIC_QWORD,
1891 .ATOMICOPCODE = MI_ATOMIC_OP_MOVE8B,
1892 .CSSTALL = true,
1893 .ReturnDataControl = true,
1894 .InlineData = true,
1895 .MemoryAddress = clear_addr);
1896 /* dw starts at dword 1, but we need to fill dwords 3 and 5 */
1897 dw[2] = info->clear_color.u32[2];
1898 dw[3] = 0;
1899 dw[4] = info->clear_color.u32[3];
1900 dw[5] = 0;
1901
1902 blorp_emit(batch, GENX(PIPE_CONTROL), pipe) {
1903 pipe.StateCacheInvalidationEnable = true;
1904 pipe.TextureCacheInvalidationEnable = true;
1905 }
1906 #elif GEN_GEN >= 9
1907
1908 /* According to GEN:BUG:2201730850, in the Clear Color Programming Note
1909 * under the Red channel, "Software shall write the converted Depth
1910 * Clear to this dword." The only depth formats listed under the red
1911 * channel are IEEE_FP and UNORM24_X8. These two requirements are
1912 * incompatible with the UNORM16 depth format, so just ignore that case
1913 * and simply perform the conversion for all depth formats.
1914 */
1915 union isl_color_value fixed_color = info->clear_color;
1916 if (GEN_GEN == 12 && isl_surf_usage_is_depth(info->surf.usage)) {
1917 isl_color_value_pack(&info->clear_color, info->surf.format,
1918 fixed_color.u32);
1919 }
1920
1921 for (int i = 0; i < 4; i++) {
1922 blorp_emit(batch, GENX(MI_STORE_DATA_IMM), sdi) {
1923 sdi.Address = info->clear_color_addr;
1924 sdi.Address.offset += i * 4;
1925 sdi.ImmediateData = fixed_color.u32[i];
1926 #if GEN_GEN >= 12
1927 if (i == 3)
1928 sdi.ForceWriteCompletionCheck = true;
1929 #endif
1930 }
1931 }
1932
1933 /* The RENDER_SURFACE_STATE::ClearColor field states that software should
1934 * write the converted depth value 16B after the clear address:
1935 *
1936 * 3D Sampler will always fetch clear depth from the location 16-bytes
1937 * above this address, where the clear depth, converted to native
1938 * surface format by software, will be stored.
1939 *
1940 */
1941 #if GEN_GEN >= 12
1942 if (isl_surf_usage_is_depth(info->surf.usage)) {
1943 blorp_emit(batch, GENX(MI_STORE_DATA_IMM), sdi) {
1944 sdi.Address = info->clear_color_addr;
1945 sdi.Address.offset += 4 * 4;
1946 sdi.ImmediateData = fixed_color.u32[0];
1947 sdi.ForceWriteCompletionCheck = true;
1948 }
1949 }
1950 #endif
1951
1952 #elif GEN_GEN >= 7
1953 blorp_emit(batch, GENX(MI_STORE_DATA_IMM), sdi) {
1954 sdi.Address = info->clear_color_addr;
1955 sdi.ImmediateData = ISL_CHANNEL_SELECT_RED << 25 |
1956 ISL_CHANNEL_SELECT_GREEN << 22 |
1957 ISL_CHANNEL_SELECT_BLUE << 19 |
1958 ISL_CHANNEL_SELECT_ALPHA << 16;
1959 if (isl_format_has_int_channel(info->view.format)) {
1960 for (unsigned i = 0; i < 4; i++) {
1961 assert(info->clear_color.u32[i] == 0 ||
1962 info->clear_color.u32[i] == 1);
1963 }
1964 sdi.ImmediateData |= (info->clear_color.u32[0] != 0) << 31;
1965 sdi.ImmediateData |= (info->clear_color.u32[1] != 0) << 30;
1966 sdi.ImmediateData |= (info->clear_color.u32[2] != 0) << 29;
1967 sdi.ImmediateData |= (info->clear_color.u32[3] != 0) << 28;
1968 } else {
1969 for (unsigned i = 0; i < 4; i++) {
1970 assert(info->clear_color.f32[i] == 0.0f ||
1971 info->clear_color.f32[i] == 1.0f);
1972 }
1973 sdi.ImmediateData |= (info->clear_color.f32[0] != 0.0f) << 31;
1974 sdi.ImmediateData |= (info->clear_color.f32[1] != 0.0f) << 30;
1975 sdi.ImmediateData |= (info->clear_color.f32[2] != 0.0f) << 29;
1976 sdi.ImmediateData |= (info->clear_color.f32[3] != 0.0f) << 28;
1977 }
1978 }
1979 #endif
1980 }
1981 }
1982
1983 /**
1984 * \brief Execute a blit or render pass operation.
1985 *
1986 * To execute the operation, this function manually constructs and emits a
1987 * batch to draw a rectangle primitive. The batchbuffer is flushed before
1988 * constructing and after emitting the batch.
1989 *
1990 * This function alters no GL state.
1991 */
1992 static void
1993 blorp_exec(struct blorp_batch *batch, const struct blorp_params *params)
1994 {
1995 if (!(batch->flags & BLORP_BATCH_NO_UPDATE_CLEAR_COLOR)) {
1996 blorp_update_clear_color(batch, &params->dst, params->fast_clear_op);
1997 blorp_update_clear_color(batch, &params->depth, params->hiz_op);
1998 }
1999
2000 #if GEN_GEN >= 8
2001 if (params->hiz_op != ISL_AUX_OP_NONE) {
2002 blorp_emit_gen8_hiz_op(batch, params);
2003 return;
2004 }
2005 #endif
2006
2007 blorp_emit_vertex_buffers(batch, params);
2008 blorp_emit_vertex_elements(batch, params);
2009
2010 blorp_emit_pipeline(batch, params);
2011
2012 blorp_emit_surface_states(batch, params);
2013
2014 if (!(batch->flags & BLORP_BATCH_NO_EMIT_DEPTH_STENCIL))
2015 blorp_emit_depth_stencil_config(batch, params);
2016
2017 blorp_emit(batch, GENX(3DPRIMITIVE), prim) {
2018 prim.VertexAccessType = SEQUENTIAL;
2019 prim.PrimitiveTopologyType = _3DPRIM_RECTLIST;
2020 #if GEN_GEN >= 7
2021 prim.PredicateEnable = batch->flags & BLORP_BATCH_PREDICATE_ENABLE;
2022 #endif
2023 prim.VertexCountPerInstance = 3;
2024 prim.InstanceCount = params->num_layers;
2025 }
2026 }
2027
2028 #endif /* BLORP_GENX_EXEC_H */