tu: Don't emit initial render target state in tile_load_ib
[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 }
906
907 if (params->src.enabled)
908 psx.PixelShaderKillsPixel = true;
909 }
910
911 #elif GEN_GEN >= 7
912
913 blorp_emit(batch, GENX(3DSTATE_WM), wm) {
914 switch (params->hiz_op) {
915 case ISL_AUX_OP_FAST_CLEAR:
916 wm.DepthBufferClear = true;
917 break;
918 case ISL_AUX_OP_FULL_RESOLVE:
919 wm.DepthBufferResolveEnable = true;
920 break;
921 case ISL_AUX_OP_AMBIGUATE:
922 wm.HierarchicalDepthBufferResolveEnable = true;
923 break;
924 case ISL_AUX_OP_NONE:
925 break;
926 default:
927 unreachable("not reached");
928 }
929
930 if (prog_data)
931 wm.ThreadDispatchEnable = true;
932
933 if (params->src.enabled)
934 wm.PixelShaderKillsPixel = true;
935
936 if (params->num_samples > 1) {
937 wm.MultisampleRasterizationMode = MSRASTMODE_ON_PATTERN;
938 wm.MultisampleDispatchMode =
939 (prog_data && prog_data->persample_dispatch) ?
940 MSDISPMODE_PERSAMPLE : MSDISPMODE_PERPIXEL;
941 } else {
942 wm.MultisampleRasterizationMode = MSRASTMODE_OFF_PIXEL;
943 wm.MultisampleDispatchMode = MSDISPMODE_PERSAMPLE;
944 }
945 }
946
947 blorp_emit(batch, GENX(3DSTATE_PS), ps) {
948 ps.MaximumNumberofThreads =
949 batch->blorp->isl_dev->info->max_wm_threads - 1;
950
951 #if GEN_IS_HASWELL
952 ps.SampleMask = 1;
953 #endif
954
955 if (prog_data) {
956 ps._8PixelDispatchEnable = prog_data->dispatch_8;
957 ps._16PixelDispatchEnable = prog_data->dispatch_16;
958 ps._32PixelDispatchEnable = prog_data->dispatch_32;
959
960 ps.DispatchGRFStartRegisterForConstantSetupData0 =
961 brw_wm_prog_data_dispatch_grf_start_reg(prog_data, ps, 0);
962 ps.DispatchGRFStartRegisterForConstantSetupData1 =
963 brw_wm_prog_data_dispatch_grf_start_reg(prog_data, ps, 1);
964 ps.DispatchGRFStartRegisterForConstantSetupData2 =
965 brw_wm_prog_data_dispatch_grf_start_reg(prog_data, ps, 2);
966
967 ps.KernelStartPointer0 = params->wm_prog_kernel +
968 brw_wm_prog_data_prog_offset(prog_data, ps, 0);
969 ps.KernelStartPointer1 = params->wm_prog_kernel +
970 brw_wm_prog_data_prog_offset(prog_data, ps, 1);
971 ps.KernelStartPointer2 = params->wm_prog_kernel +
972 brw_wm_prog_data_prog_offset(prog_data, ps, 2);
973
974 ps.AttributeEnable = prog_data->num_varying_inputs > 0;
975 } else {
976 /* Gen7 hardware gets angry if we don't enable at least one dispatch
977 * mode, so just enable 16-pixel dispatch if we don't have a program.
978 */
979 ps._16PixelDispatchEnable = true;
980 }
981
982 if (params->src.enabled)
983 ps.SamplerCount = 1; /* Up to 4 samplers */
984
985 switch (params->fast_clear_op) {
986 case ISL_AUX_OP_NONE:
987 break;
988 case ISL_AUX_OP_FULL_RESOLVE:
989 ps.RenderTargetResolveEnable = true;
990 break;
991 case ISL_AUX_OP_FAST_CLEAR:
992 ps.RenderTargetFastClearEnable = true;
993 break;
994 default:
995 unreachable("Invalid fast clear op");
996 }
997 }
998
999 #else /* GEN_GEN <= 6 */
1000
1001 blorp_emit(batch, GENX(3DSTATE_WM), wm) {
1002 wm.MaximumNumberofThreads =
1003 batch->blorp->isl_dev->info->max_wm_threads - 1;
1004
1005 switch (params->hiz_op) {
1006 case ISL_AUX_OP_FAST_CLEAR:
1007 wm.DepthBufferClear = true;
1008 break;
1009 case ISL_AUX_OP_FULL_RESOLVE:
1010 wm.DepthBufferResolveEnable = true;
1011 break;
1012 case ISL_AUX_OP_AMBIGUATE:
1013 wm.HierarchicalDepthBufferResolveEnable = true;
1014 break;
1015 case ISL_AUX_OP_NONE:
1016 break;
1017 default:
1018 unreachable("not reached");
1019 }
1020
1021 if (prog_data) {
1022 wm.ThreadDispatchEnable = true;
1023
1024 wm._8PixelDispatchEnable = prog_data->dispatch_8;
1025 wm._16PixelDispatchEnable = prog_data->dispatch_16;
1026 wm._32PixelDispatchEnable = prog_data->dispatch_32;
1027
1028 wm.DispatchGRFStartRegisterForConstantSetupData0 =
1029 brw_wm_prog_data_dispatch_grf_start_reg(prog_data, wm, 0);
1030 wm.DispatchGRFStartRegisterForConstantSetupData1 =
1031 brw_wm_prog_data_dispatch_grf_start_reg(prog_data, wm, 1);
1032 wm.DispatchGRFStartRegisterForConstantSetupData2 =
1033 brw_wm_prog_data_dispatch_grf_start_reg(prog_data, wm, 2);
1034
1035 wm.KernelStartPointer0 = params->wm_prog_kernel +
1036 brw_wm_prog_data_prog_offset(prog_data, wm, 0);
1037 wm.KernelStartPointer1 = params->wm_prog_kernel +
1038 brw_wm_prog_data_prog_offset(prog_data, wm, 1);
1039 wm.KernelStartPointer2 = params->wm_prog_kernel +
1040 brw_wm_prog_data_prog_offset(prog_data, wm, 2);
1041
1042 wm.NumberofSFOutputAttributes = prog_data->num_varying_inputs;
1043 }
1044
1045 if (params->src.enabled) {
1046 wm.SamplerCount = 1; /* Up to 4 samplers */
1047 wm.PixelShaderKillsPixel = true; /* TODO: temporarily smash on */
1048 }
1049
1050 if (params->num_samples > 1) {
1051 wm.MultisampleRasterizationMode = MSRASTMODE_ON_PATTERN;
1052 wm.MultisampleDispatchMode =
1053 (prog_data && prog_data->persample_dispatch) ?
1054 MSDISPMODE_PERSAMPLE : MSDISPMODE_PERPIXEL;
1055 } else {
1056 wm.MultisampleRasterizationMode = MSRASTMODE_OFF_PIXEL;
1057 wm.MultisampleDispatchMode = MSDISPMODE_PERSAMPLE;
1058 }
1059 }
1060
1061 #endif /* GEN_GEN */
1062 }
1063
1064 static uint32_t
1065 blorp_emit_blend_state(struct blorp_batch *batch,
1066 const struct blorp_params *params)
1067 {
1068 struct GENX(BLEND_STATE) blend;
1069 memset(&blend, 0, sizeof(blend));
1070
1071 uint32_t offset;
1072 int size = GENX(BLEND_STATE_length) * 4;
1073 size += GENX(BLEND_STATE_ENTRY_length) * 4 * params->num_draw_buffers;
1074 uint32_t *state = blorp_alloc_dynamic_state(batch, size, 64, &offset);
1075 uint32_t *pos = state;
1076
1077 GENX(BLEND_STATE_pack)(NULL, pos, &blend);
1078 pos += GENX(BLEND_STATE_length);
1079
1080 for (unsigned i = 0; i < params->num_draw_buffers; ++i) {
1081 struct GENX(BLEND_STATE_ENTRY) entry = {
1082 .PreBlendColorClampEnable = true,
1083 .PostBlendColorClampEnable = true,
1084 .ColorClampRange = COLORCLAMP_RTFORMAT,
1085
1086 .WriteDisableRed = params->color_write_disable[0],
1087 .WriteDisableGreen = params->color_write_disable[1],
1088 .WriteDisableBlue = params->color_write_disable[2],
1089 .WriteDisableAlpha = params->color_write_disable[3],
1090 };
1091 GENX(BLEND_STATE_ENTRY_pack)(NULL, pos, &entry);
1092 pos += GENX(BLEND_STATE_ENTRY_length);
1093 }
1094
1095 blorp_flush_range(batch, state, size);
1096
1097 #if GEN_GEN >= 7
1098 blorp_emit(batch, GENX(3DSTATE_BLEND_STATE_POINTERS), sp) {
1099 sp.BlendStatePointer = offset;
1100 #if GEN_GEN >= 8
1101 sp.BlendStatePointerValid = true;
1102 #endif
1103 }
1104 #endif
1105
1106 #if GEN_GEN >= 8
1107 blorp_emit(batch, GENX(3DSTATE_PS_BLEND), ps_blend) {
1108 ps_blend.HasWriteableRT = true;
1109 }
1110 #endif
1111
1112 return offset;
1113 }
1114
1115 static uint32_t
1116 blorp_emit_color_calc_state(struct blorp_batch *batch,
1117 UNUSED const struct blorp_params *params)
1118 {
1119 uint32_t offset;
1120 blorp_emit_dynamic(batch, GENX(COLOR_CALC_STATE), cc, 64, &offset) {
1121 #if GEN_GEN <= 8
1122 cc.StencilReferenceValue = params->stencil_ref;
1123 #endif
1124 }
1125
1126 #if GEN_GEN >= 7
1127 blorp_emit(batch, GENX(3DSTATE_CC_STATE_POINTERS), sp) {
1128 sp.ColorCalcStatePointer = offset;
1129 #if GEN_GEN >= 8
1130 sp.ColorCalcStatePointerValid = true;
1131 #endif
1132 }
1133 #endif
1134
1135 return offset;
1136 }
1137
1138 static uint32_t
1139 blorp_emit_depth_stencil_state(struct blorp_batch *batch,
1140 const struct blorp_params *params)
1141 {
1142 #if GEN_GEN >= 8
1143 struct GENX(3DSTATE_WM_DEPTH_STENCIL) ds = {
1144 GENX(3DSTATE_WM_DEPTH_STENCIL_header),
1145 };
1146 #else
1147 struct GENX(DEPTH_STENCIL_STATE) ds = { 0 };
1148 #endif
1149
1150 if (params->depth.enabled) {
1151 ds.DepthBufferWriteEnable = true;
1152
1153 switch (params->hiz_op) {
1154 /* See the following sections of the Sandy Bridge PRM, Volume 2, Part1:
1155 * - 7.5.3.1 Depth Buffer Clear
1156 * - 7.5.3.2 Depth Buffer Resolve
1157 * - 7.5.3.3 Hierarchical Depth Buffer Resolve
1158 */
1159 case ISL_AUX_OP_FULL_RESOLVE:
1160 ds.DepthTestEnable = true;
1161 ds.DepthTestFunction = COMPAREFUNCTION_NEVER;
1162 break;
1163
1164 case ISL_AUX_OP_NONE:
1165 case ISL_AUX_OP_FAST_CLEAR:
1166 case ISL_AUX_OP_AMBIGUATE:
1167 ds.DepthTestEnable = false;
1168 break;
1169 case ISL_AUX_OP_PARTIAL_RESOLVE:
1170 unreachable("Invalid HIZ op");
1171 }
1172 }
1173
1174 if (params->stencil.enabled) {
1175 ds.StencilBufferWriteEnable = true;
1176 ds.StencilTestEnable = true;
1177 ds.DoubleSidedStencilEnable = false;
1178
1179 ds.StencilTestFunction = COMPAREFUNCTION_ALWAYS;
1180 ds.StencilPassDepthPassOp = STENCILOP_REPLACE;
1181
1182 ds.StencilWriteMask = params->stencil_mask;
1183 #if GEN_GEN >= 9
1184 ds.StencilReferenceValue = params->stencil_ref;
1185 #endif
1186 }
1187
1188 #if GEN_GEN >= 8
1189 uint32_t offset = 0;
1190 uint32_t *dw = blorp_emit_dwords(batch,
1191 GENX(3DSTATE_WM_DEPTH_STENCIL_length));
1192 if (!dw)
1193 return 0;
1194
1195 GENX(3DSTATE_WM_DEPTH_STENCIL_pack)(NULL, dw, &ds);
1196 #else
1197 uint32_t offset;
1198 void *state = blorp_alloc_dynamic_state(batch,
1199 GENX(DEPTH_STENCIL_STATE_length) * 4,
1200 64, &offset);
1201 GENX(DEPTH_STENCIL_STATE_pack)(NULL, state, &ds);
1202 blorp_flush_range(batch, state, GENX(DEPTH_STENCIL_STATE_length) * 4);
1203 #endif
1204
1205 #if GEN_GEN == 7
1206 blorp_emit(batch, GENX(3DSTATE_DEPTH_STENCIL_STATE_POINTERS), sp) {
1207 sp.PointertoDEPTH_STENCIL_STATE = offset;
1208 }
1209 #endif
1210
1211 return offset;
1212 }
1213
1214 static void
1215 blorp_emit_3dstate_multisample(struct blorp_batch *batch,
1216 const struct blorp_params *params)
1217 {
1218 blorp_emit(batch, GENX(3DSTATE_MULTISAMPLE), ms) {
1219 ms.NumberofMultisamples = __builtin_ffs(params->num_samples) - 1;
1220
1221 #if GEN_GEN >= 8
1222 /* The PRM says that this bit is valid only for DX9:
1223 *
1224 * SW can choose to set this bit only for DX9 API. DX10/OGL API's
1225 * should not have any effect by setting or not setting this bit.
1226 */
1227 ms.PixelPositionOffsetEnable = false;
1228 #elif GEN_GEN >= 7
1229
1230 switch (params->num_samples) {
1231 case 1:
1232 GEN_SAMPLE_POS_1X(ms.Sample);
1233 break;
1234 case 2:
1235 GEN_SAMPLE_POS_2X(ms.Sample);
1236 break;
1237 case 4:
1238 GEN_SAMPLE_POS_4X(ms.Sample);
1239 break;
1240 case 8:
1241 GEN_SAMPLE_POS_8X(ms.Sample);
1242 break;
1243 default:
1244 break;
1245 }
1246 #else
1247 GEN_SAMPLE_POS_4X(ms.Sample);
1248 #endif
1249 ms.PixelLocation = CENTER;
1250 }
1251 }
1252
1253 static void
1254 blorp_emit_pipeline(struct blorp_batch *batch,
1255 const struct blorp_params *params)
1256 {
1257 uint32_t blend_state_offset = 0;
1258 uint32_t color_calc_state_offset;
1259 uint32_t depth_stencil_state_offset;
1260
1261 enum gen_urb_deref_block_size urb_deref_block_size;
1262 emit_urb_config(batch, params, &urb_deref_block_size);
1263
1264 if (params->wm_prog_data) {
1265 blend_state_offset = blorp_emit_blend_state(batch, params);
1266 }
1267 color_calc_state_offset = blorp_emit_color_calc_state(batch, params);
1268 depth_stencil_state_offset = blorp_emit_depth_stencil_state(batch, params);
1269
1270 #if GEN_GEN == 6
1271 /* 3DSTATE_CC_STATE_POINTERS
1272 *
1273 * The pointer offsets are relative to
1274 * CMD_STATE_BASE_ADDRESS.DynamicStateBaseAddress.
1275 *
1276 * The HiZ op doesn't use BLEND_STATE or COLOR_CALC_STATE.
1277 *
1278 * The dynamic state emit helpers emit their own STATE_POINTERS packets on
1279 * gen7+. However, on gen6 and earlier, they're all lumpped together in
1280 * one CC_STATE_POINTERS packet so we have to emit that here.
1281 */
1282 blorp_emit(batch, GENX(3DSTATE_CC_STATE_POINTERS), cc) {
1283 cc.BLEND_STATEChange = true;
1284 cc.ColorCalcStatePointerValid = true;
1285 cc.DEPTH_STENCIL_STATEChange = true;
1286 cc.PointertoBLEND_STATE = blend_state_offset;
1287 cc.ColorCalcStatePointer = color_calc_state_offset;
1288 cc.PointertoDEPTH_STENCIL_STATE = depth_stencil_state_offset;
1289 }
1290 #else
1291 (void)blend_state_offset;
1292 (void)color_calc_state_offset;
1293 (void)depth_stencil_state_offset;
1294 #endif
1295
1296 #if GEN_GEN >= 12
1297 blorp_emit(batch, GENX(3DSTATE_CONSTANT_ALL), pc) {
1298 /* Update empty push constants for all stages (bitmask = 11111b) */
1299 pc.ShaderUpdateEnable = 0x1f;
1300 }
1301 #else
1302 blorp_emit(batch, GENX(3DSTATE_CONSTANT_VS), vs);
1303 #if GEN_GEN >= 7
1304 blorp_emit(batch, GENX(3DSTATE_CONSTANT_HS), hs);
1305 blorp_emit(batch, GENX(3DSTATE_CONSTANT_DS), DS);
1306 #endif
1307 blorp_emit(batch, GENX(3DSTATE_CONSTANT_GS), gs);
1308 blorp_emit(batch, GENX(3DSTATE_CONSTANT_PS), ps);
1309 #endif
1310
1311 if (params->src.enabled)
1312 blorp_emit_sampler_state(batch);
1313
1314 blorp_emit_3dstate_multisample(batch, params);
1315
1316 blorp_emit(batch, GENX(3DSTATE_SAMPLE_MASK), mask) {
1317 mask.SampleMask = (1 << params->num_samples) - 1;
1318 }
1319
1320 /* From the BSpec, 3D Pipeline > Geometry > Vertex Shader > State,
1321 * 3DSTATE_VS, Dword 5.0 "VS Function Enable":
1322 *
1323 * [DevSNB] A pipeline flush must be programmed prior to a
1324 * 3DSTATE_VS command that causes the VS Function Enable to
1325 * toggle. Pipeline flush can be executed by sending a PIPE_CONTROL
1326 * command with CS stall bit set and a post sync operation.
1327 *
1328 * We've already done one at the start of the BLORP operation.
1329 */
1330 blorp_emit_vs_config(batch, params);
1331 #if GEN_GEN >= 7
1332 blorp_emit(batch, GENX(3DSTATE_HS), hs);
1333 blorp_emit(batch, GENX(3DSTATE_TE), te);
1334 blorp_emit(batch, GENX(3DSTATE_DS), DS);
1335 blorp_emit(batch, GENX(3DSTATE_STREAMOUT), so);
1336 #endif
1337 blorp_emit(batch, GENX(3DSTATE_GS), gs);
1338
1339 blorp_emit(batch, GENX(3DSTATE_CLIP), clip) {
1340 clip.PerspectiveDivideDisable = true;
1341 }
1342
1343 blorp_emit_sf_config(batch, params, urb_deref_block_size);
1344 blorp_emit_ps_config(batch, params);
1345
1346 blorp_emit_cc_viewport(batch);
1347 }
1348
1349 /******** This is the end of the pipeline setup code ********/
1350
1351 #endif /* GEN_GEN >= 6 */
1352
1353 #if GEN_GEN >= 7
1354 static void
1355 blorp_emit_memcpy(struct blorp_batch *batch,
1356 struct blorp_address dst,
1357 struct blorp_address src,
1358 uint32_t size)
1359 {
1360 assert(size % 4 == 0);
1361
1362 for (unsigned dw = 0; dw < size; dw += 4) {
1363 #if GEN_GEN >= 8
1364 blorp_emit(batch, GENX(MI_COPY_MEM_MEM), cp) {
1365 cp.DestinationMemoryAddress = dst;
1366 cp.SourceMemoryAddress = src;
1367 }
1368 #else
1369 /* IVB does not have a general purpose register for command streamer
1370 * commands. Therefore, we use an alternate temporary register.
1371 */
1372 #define BLORP_TEMP_REG 0x2440 /* GEN7_3DPRIM_BASE_VERTEX */
1373 blorp_emit(batch, GENX(MI_LOAD_REGISTER_MEM), load) {
1374 load.RegisterAddress = BLORP_TEMP_REG;
1375 load.MemoryAddress = src;
1376 }
1377 blorp_emit(batch, GENX(MI_STORE_REGISTER_MEM), store) {
1378 store.RegisterAddress = BLORP_TEMP_REG;
1379 store.MemoryAddress = dst;
1380 }
1381 #undef BLORP_TEMP_REG
1382 #endif
1383 dst.offset += 4;
1384 src.offset += 4;
1385 }
1386 }
1387 #endif
1388
1389 static void
1390 blorp_emit_surface_state(struct blorp_batch *batch,
1391 const struct brw_blorp_surface_info *surface,
1392 enum isl_aux_op aux_op,
1393 void *state, uint32_t state_offset,
1394 const bool color_write_disables[4],
1395 bool is_render_target)
1396 {
1397 const struct isl_device *isl_dev = batch->blorp->isl_dev;
1398 struct isl_surf surf = surface->surf;
1399
1400 if (surf.dim == ISL_SURF_DIM_1D &&
1401 surf.dim_layout == ISL_DIM_LAYOUT_GEN4_2D) {
1402 assert(surf.logical_level0_px.height == 1);
1403 surf.dim = ISL_SURF_DIM_2D;
1404 }
1405
1406 if (isl_aux_usage_has_hiz(surface->aux_usage)) {
1407 /* BLORP doesn't render with depth so we can't use HiZ */
1408 assert(!is_render_target);
1409 /* We can't reinterpret HiZ */
1410 assert(surface->surf.format == surface->view.format);
1411 }
1412 enum isl_aux_usage aux_usage = surface->aux_usage;
1413
1414 isl_channel_mask_t write_disable_mask = 0;
1415 if (is_render_target && GEN_GEN <= 5) {
1416 if (color_write_disables[0])
1417 write_disable_mask |= ISL_CHANNEL_RED_BIT;
1418 if (color_write_disables[1])
1419 write_disable_mask |= ISL_CHANNEL_GREEN_BIT;
1420 if (color_write_disables[2])
1421 write_disable_mask |= ISL_CHANNEL_BLUE_BIT;
1422 if (color_write_disables[3])
1423 write_disable_mask |= ISL_CHANNEL_ALPHA_BIT;
1424 }
1425
1426 const bool use_clear_address =
1427 GEN_GEN >= 10 && (surface->clear_color_addr.buffer != NULL);
1428
1429 isl_surf_fill_state(batch->blorp->isl_dev, state,
1430 .surf = &surf, .view = &surface->view,
1431 .aux_surf = &surface->aux_surf, .aux_usage = aux_usage,
1432 .address =
1433 blorp_get_surface_address(batch, surface->addr),
1434 .aux_address = aux_usage == ISL_AUX_USAGE_NONE ? 0 :
1435 blorp_get_surface_address(batch, surface->aux_addr),
1436 .clear_address = !use_clear_address ? 0 :
1437 blorp_get_surface_address(batch,
1438 surface->clear_color_addr),
1439 .mocs = surface->addr.mocs,
1440 .clear_color = surface->clear_color,
1441 .use_clear_address = use_clear_address,
1442 .write_disables = write_disable_mask);
1443
1444 blorp_surface_reloc(batch, state_offset + isl_dev->ss.addr_offset,
1445 surface->addr, 0);
1446
1447 if (aux_usage != ISL_AUX_USAGE_NONE) {
1448 /* On gen7 and prior, the bottom 12 bits of the MCS base address are
1449 * used to store other information. This should be ok, however, because
1450 * surface buffer addresses are always 4K page alinged.
1451 */
1452 assert((surface->aux_addr.offset & 0xfff) == 0);
1453 uint32_t *aux_addr = state + isl_dev->ss.aux_addr_offset;
1454 blorp_surface_reloc(batch, state_offset + isl_dev->ss.aux_addr_offset,
1455 surface->aux_addr, *aux_addr);
1456 }
1457
1458 if (aux_usage != ISL_AUX_USAGE_NONE && surface->clear_color_addr.buffer) {
1459 #if GEN_GEN >= 10
1460 assert((surface->clear_color_addr.offset & 0x3f) == 0);
1461 uint32_t *clear_addr = state + isl_dev->ss.clear_color_state_offset;
1462 blorp_surface_reloc(batch, state_offset +
1463 isl_dev->ss.clear_color_state_offset,
1464 surface->clear_color_addr, *clear_addr);
1465 #elif GEN_GEN >= 7
1466 /* Fast clears just whack the AUX surface and don't actually use the
1467 * clear color for anything. We can avoid the MI memcpy on that case.
1468 */
1469 if (aux_op != ISL_AUX_OP_FAST_CLEAR) {
1470 struct blorp_address dst_addr = blorp_get_surface_base_address(batch);
1471 dst_addr.offset += state_offset + isl_dev->ss.clear_value_offset;
1472 blorp_emit_memcpy(batch, dst_addr, surface->clear_color_addr,
1473 isl_dev->ss.clear_value_size);
1474 }
1475 #else
1476 unreachable("Fast clears are only supported on gen7+");
1477 #endif
1478 }
1479
1480 blorp_flush_range(batch, state, GENX(RENDER_SURFACE_STATE_length) * 4);
1481 }
1482
1483 static void
1484 blorp_emit_null_surface_state(struct blorp_batch *batch,
1485 const struct brw_blorp_surface_info *surface,
1486 uint32_t *state)
1487 {
1488 struct GENX(RENDER_SURFACE_STATE) ss = {
1489 .SurfaceType = SURFTYPE_NULL,
1490 .SurfaceFormat = ISL_FORMAT_R8G8B8A8_UNORM,
1491 .Width = surface->surf.logical_level0_px.width - 1,
1492 .Height = surface->surf.logical_level0_px.height - 1,
1493 .MIPCountLOD = surface->view.base_level,
1494 .MinimumArrayElement = surface->view.base_array_layer,
1495 .Depth = surface->view.array_len - 1,
1496 .RenderTargetViewExtent = surface->view.array_len - 1,
1497 #if GEN_GEN >= 6
1498 .NumberofMultisamples = ffs(surface->surf.samples) - 1,
1499 #endif
1500
1501 #if GEN_GEN >= 7
1502 .SurfaceArray = surface->surf.dim != ISL_SURF_DIM_3D,
1503 #endif
1504
1505 #if GEN_GEN >= 8
1506 .TileMode = YMAJOR,
1507 #else
1508 .TiledSurface = true,
1509 #endif
1510 };
1511
1512 GENX(RENDER_SURFACE_STATE_pack)(NULL, state, &ss);
1513
1514 blorp_flush_range(batch, state, GENX(RENDER_SURFACE_STATE_length) * 4);
1515 }
1516
1517 static void
1518 blorp_emit_surface_states(struct blorp_batch *batch,
1519 const struct blorp_params *params)
1520 {
1521 const struct isl_device *isl_dev = batch->blorp->isl_dev;
1522 uint32_t bind_offset = 0, surface_offsets[2];
1523 void *surface_maps[2];
1524
1525 UNUSED bool has_indirect_clear_color = false;
1526 if (params->use_pre_baked_binding_table) {
1527 bind_offset = params->pre_baked_binding_table_offset;
1528 } else {
1529 unsigned num_surfaces = 1 + params->src.enabled;
1530 blorp_alloc_binding_table(batch, num_surfaces,
1531 isl_dev->ss.size, isl_dev->ss.align,
1532 &bind_offset, surface_offsets, surface_maps);
1533
1534 if (params->dst.enabled) {
1535 blorp_emit_surface_state(batch, &params->dst,
1536 params->fast_clear_op,
1537 surface_maps[BLORP_RENDERBUFFER_BT_INDEX],
1538 surface_offsets[BLORP_RENDERBUFFER_BT_INDEX],
1539 params->color_write_disable, true);
1540 if (params->dst.clear_color_addr.buffer != NULL)
1541 has_indirect_clear_color = true;
1542 } else {
1543 assert(params->depth.enabled || params->stencil.enabled);
1544 const struct brw_blorp_surface_info *surface =
1545 params->depth.enabled ? &params->depth : &params->stencil;
1546 blorp_emit_null_surface_state(batch, surface,
1547 surface_maps[BLORP_RENDERBUFFER_BT_INDEX]);
1548 }
1549
1550 if (params->src.enabled) {
1551 blorp_emit_surface_state(batch, &params->src,
1552 params->fast_clear_op,
1553 surface_maps[BLORP_TEXTURE_BT_INDEX],
1554 surface_offsets[BLORP_TEXTURE_BT_INDEX],
1555 NULL, false);
1556 if (params->src.clear_color_addr.buffer != NULL)
1557 has_indirect_clear_color = true;
1558 }
1559 }
1560
1561 #if GEN_GEN >= 7
1562 if (has_indirect_clear_color) {
1563 /* Updating a surface state object may require that the state cache be
1564 * invalidated. From the SKL PRM, Shared Functions -> State -> State
1565 * Caching:
1566 *
1567 * Whenever the RENDER_SURFACE_STATE object in memory pointed to by
1568 * the Binding Table Pointer (BTP) and Binding Table Index (BTI) is
1569 * modified [...], the L1 state cache must be invalidated to ensure
1570 * the new surface or sampler state is fetched from system memory.
1571 */
1572 blorp_emit(batch, GENX(PIPE_CONTROL), pipe) {
1573 pipe.StateCacheInvalidationEnable = true;
1574 #if GEN_GEN >= 12
1575 pipe.TileCacheFlushEnable = true;
1576 #endif
1577 }
1578 }
1579 #endif
1580
1581 #if GEN_GEN >= 7
1582 blorp_emit(batch, GENX(3DSTATE_BINDING_TABLE_POINTERS_VS), bt);
1583 blorp_emit(batch, GENX(3DSTATE_BINDING_TABLE_POINTERS_HS), bt);
1584 blorp_emit(batch, GENX(3DSTATE_BINDING_TABLE_POINTERS_DS), bt);
1585 blorp_emit(batch, GENX(3DSTATE_BINDING_TABLE_POINTERS_GS), bt);
1586
1587 blorp_emit(batch, GENX(3DSTATE_BINDING_TABLE_POINTERS_PS), bt) {
1588 bt.PointertoPSBindingTable = bind_offset;
1589 }
1590 #elif GEN_GEN >= 6
1591 blorp_emit(batch, GENX(3DSTATE_BINDING_TABLE_POINTERS), bt) {
1592 bt.PSBindingTableChange = true;
1593 bt.PointertoPSBindingTable = bind_offset;
1594 }
1595 #else
1596 blorp_emit(batch, GENX(3DSTATE_BINDING_TABLE_POINTERS), bt) {
1597 bt.PointertoPSBindingTable = bind_offset;
1598 }
1599 #endif
1600 }
1601
1602 static void
1603 blorp_emit_depth_stencil_config(struct blorp_batch *batch,
1604 const struct blorp_params *params)
1605 {
1606 const struct isl_device *isl_dev = batch->blorp->isl_dev;
1607
1608 uint32_t *dw = blorp_emit_dwords(batch, isl_dev->ds.size / 4);
1609 if (dw == NULL)
1610 return;
1611
1612 struct isl_depth_stencil_hiz_emit_info info = { };
1613
1614 if (params->depth.enabled) {
1615 info.view = &params->depth.view;
1616 info.mocs = params->depth.addr.mocs;
1617 } else if (params->stencil.enabled) {
1618 info.view = &params->stencil.view;
1619 info.mocs = params->stencil.addr.mocs;
1620 }
1621
1622 if (params->depth.enabled) {
1623 info.depth_surf = &params->depth.surf;
1624
1625 info.depth_address =
1626 blorp_emit_reloc(batch, dw + isl_dev->ds.depth_offset / 4,
1627 params->depth.addr, 0);
1628
1629 info.hiz_usage = params->depth.aux_usage;
1630 if (isl_aux_usage_has_hiz(info.hiz_usage)) {
1631 info.hiz_surf = &params->depth.aux_surf;
1632
1633 struct blorp_address hiz_address = params->depth.aux_addr;
1634 #if GEN_GEN == 6
1635 /* Sandy bridge hardware does not technically support mipmapped HiZ.
1636 * However, we have a special layout that allows us to make it work
1637 * anyway by manually offsetting to the specified miplevel.
1638 */
1639 assert(info.hiz_surf->dim_layout == ISL_DIM_LAYOUT_GEN6_STENCIL_HIZ);
1640 uint32_t offset_B;
1641 isl_surf_get_image_offset_B_tile_sa(info.hiz_surf,
1642 info.view->base_level, 0, 0,
1643 &offset_B, NULL, NULL);
1644 hiz_address.offset += offset_B;
1645 #endif
1646
1647 info.hiz_address =
1648 blorp_emit_reloc(batch, dw + isl_dev->ds.hiz_offset / 4,
1649 hiz_address, 0);
1650
1651 info.depth_clear_value = params->depth.clear_color.f32[0];
1652 }
1653 }
1654
1655 if (params->stencil.enabled) {
1656 info.stencil_surf = &params->stencil.surf;
1657
1658 info.stencil_aux_usage = params->stencil.aux_usage;
1659 struct blorp_address stencil_address = params->stencil.addr;
1660 #if GEN_GEN == 6
1661 /* Sandy bridge hardware does not technically support mipmapped stencil.
1662 * However, we have a special layout that allows us to make it work
1663 * anyway by manually offsetting to the specified miplevel.
1664 */
1665 assert(info.stencil_surf->dim_layout == ISL_DIM_LAYOUT_GEN6_STENCIL_HIZ);
1666 uint32_t offset_B;
1667 isl_surf_get_image_offset_B_tile_sa(info.stencil_surf,
1668 info.view->base_level, 0, 0,
1669 &offset_B, NULL, NULL);
1670 stencil_address.offset += offset_B;
1671 #endif
1672
1673 info.stencil_address =
1674 blorp_emit_reloc(batch, dw + isl_dev->ds.stencil_offset / 4,
1675 stencil_address, 0);
1676 }
1677
1678 isl_emit_depth_stencil_hiz_s(isl_dev, dw, &info);
1679
1680 #if GEN_GEN >= 12
1681 /* GEN:BUG:1408224581
1682 *
1683 * Workaround: Gen12LP Astep only An additional pipe control with
1684 * post-sync = store dword operation would be required.( w/a is to
1685 * have an additional pipe control after the stencil state whenever
1686 * the surface state bits of this state is changing).
1687 */
1688 blorp_emit(batch, GENX(PIPE_CONTROL), pc) {
1689 pc.PostSyncOperation = WriteImmediateData;
1690 pc.Address = blorp_get_workaround_page(batch);
1691 }
1692 #endif
1693 }
1694
1695 #if GEN_GEN >= 8
1696 /* Emits the Optimized HiZ sequence specified in the BDW+ PRMs. The
1697 * depth/stencil buffer extents are ignored to handle APIs which perform
1698 * clearing operations without such information.
1699 * */
1700 static void
1701 blorp_emit_gen8_hiz_op(struct blorp_batch *batch,
1702 const struct blorp_params *params)
1703 {
1704 /* We should be performing an operation on a depth or stencil buffer.
1705 */
1706 assert(params->depth.enabled || params->stencil.enabled);
1707
1708 /* The stencil buffer should only be enabled on GEN == 12, if a fast clear
1709 * or full resolve operation is requested. On rest of the GEN, if a fast
1710 * clear operation is requested.
1711 */
1712 if (params->stencil.enabled) {
1713 #if GEN_GEN >= 12
1714 assert(params->hiz_op == ISL_AUX_OP_FAST_CLEAR ||
1715 params->hiz_op == ISL_AUX_OP_FULL_RESOLVE);
1716 #else
1717 assert(params->hiz_op == ISL_AUX_OP_FAST_CLEAR);
1718 #endif
1719 }
1720
1721 /* From the BDW PRM Volume 2, 3DSTATE_WM_HZ_OP:
1722 *
1723 * 3DSTATE_MULTISAMPLE packet must be used prior to this packet to change
1724 * the Number of Multisamples. This packet must not be used to change
1725 * Number of Multisamples in a rendering sequence.
1726 *
1727 * Since HIZ may be the first thing in a batch buffer, play safe and always
1728 * emit 3DSTATE_MULTISAMPLE.
1729 */
1730 blorp_emit_3dstate_multisample(batch, params);
1731
1732 /* From the BDW PRM Volume 7, Depth Buffer Clear:
1733 *
1734 * The clear value must be between the min and max depth values
1735 * (inclusive) defined in the CC_VIEWPORT. If the depth buffer format is
1736 * D32_FLOAT, then +/-DENORM values are also allowed.
1737 *
1738 * Set the bounds to match our hardware limits, [0.0, 1.0].
1739 */
1740 if (params->depth.enabled && params->hiz_op == ISL_AUX_OP_FAST_CLEAR) {
1741 assert(params->depth.clear_color.f32[0] >= 0.0f);
1742 assert(params->depth.clear_color.f32[0] <= 1.0f);
1743 blorp_emit_cc_viewport(batch);
1744 }
1745
1746 /* According to the SKL PRM formula for WM_INT::ThreadDispatchEnable, the
1747 * 3DSTATE_WM::ForceThreadDispatchEnable field can force WM thread dispatch
1748 * even when WM_HZ_OP is active. However, WM thread dispatch is normally
1749 * disabled for HiZ ops and it appears that force-enabling it can lead to
1750 * GPU hangs on at least Skylake. Since we don't know the current state of
1751 * the 3DSTATE_WM packet, just emit a dummy one prior to 3DSTATE_WM_HZ_OP.
1752 */
1753 blorp_emit(batch, GENX(3DSTATE_WM), wm);
1754
1755 /* If we can't alter the depth stencil config and multiple layers are
1756 * involved, the HiZ op will fail. This is because the op requires that a
1757 * new config is emitted for each additional layer.
1758 */
1759 if (batch->flags & BLORP_BATCH_NO_EMIT_DEPTH_STENCIL) {
1760 assert(params->num_layers <= 1);
1761 } else {
1762 blorp_emit_depth_stencil_config(batch, params);
1763 }
1764
1765 blorp_emit(batch, GENX(3DSTATE_WM_HZ_OP), hzp) {
1766 switch (params->hiz_op) {
1767 case ISL_AUX_OP_FAST_CLEAR:
1768 hzp.StencilBufferClearEnable = params->stencil.enabled;
1769 hzp.DepthBufferClearEnable = params->depth.enabled;
1770 hzp.StencilClearValue = params->stencil_ref;
1771 hzp.FullSurfaceDepthandStencilClear = params->full_surface_hiz_op;
1772 break;
1773 case ISL_AUX_OP_FULL_RESOLVE:
1774 assert(params->full_surface_hiz_op);
1775 hzp.DepthBufferResolveEnable = params->depth.enabled;
1776 #if GEN_GEN >= 12
1777 if (params->stencil.enabled) {
1778 assert(params->stencil.aux_usage == ISL_AUX_USAGE_CCS_E);
1779 hzp.StencilBufferResolveEnable = true;
1780 }
1781 #endif
1782 break;
1783 case ISL_AUX_OP_AMBIGUATE:
1784 assert(params->full_surface_hiz_op);
1785 hzp.HierarchicalDepthBufferResolveEnable = true;
1786 break;
1787 case ISL_AUX_OP_PARTIAL_RESOLVE:
1788 case ISL_AUX_OP_NONE:
1789 unreachable("Invalid HIZ op");
1790 }
1791
1792 hzp.NumberofMultisamples = ffs(params->num_samples) - 1;
1793 hzp.SampleMask = 0xFFFF;
1794
1795 /* Due to a hardware issue, this bit MBZ */
1796 assert(hzp.ScissorRectangleEnable == false);
1797
1798 /* Contrary to the HW docs both fields are inclusive */
1799 hzp.ClearRectangleXMin = params->x0;
1800 hzp.ClearRectangleYMin = params->y0;
1801
1802 /* Contrary to the HW docs both fields are exclusive */
1803 hzp.ClearRectangleXMax = params->x1;
1804 hzp.ClearRectangleYMax = params->y1;
1805 }
1806
1807 /* PIPE_CONTROL w/ all bits clear except for “Post-Sync Operation” must set
1808 * to “Write Immediate Data” enabled.
1809 */
1810 blorp_emit(batch, GENX(PIPE_CONTROL), pc) {
1811 pc.PostSyncOperation = WriteImmediateData;
1812 pc.Address = blorp_get_workaround_page(batch);
1813 }
1814
1815 blorp_emit(batch, GENX(3DSTATE_WM_HZ_OP), hzp);
1816 }
1817 #endif
1818
1819 static void
1820 blorp_update_clear_color(struct blorp_batch *batch,
1821 const struct brw_blorp_surface_info *info,
1822 enum isl_aux_op op)
1823 {
1824 if (info->clear_color_addr.buffer && op == ISL_AUX_OP_FAST_CLEAR) {
1825 #if GEN_GEN == 11
1826 blorp_emit(batch, GENX(PIPE_CONTROL), pipe) {
1827 pipe.CommandStreamerStallEnable = true;
1828 }
1829
1830 /* 2 QWORDS */
1831 const unsigned inlinedata_dw = 2 * 2;
1832 const unsigned num_dwords = GENX(MI_ATOMIC_length) + inlinedata_dw;
1833
1834 struct blorp_address clear_addr = info->clear_color_addr;
1835 uint32_t *dw = blorp_emitn(batch, GENX(MI_ATOMIC), num_dwords,
1836 .DataSize = MI_ATOMIC_QWORD,
1837 .ATOMICOPCODE = MI_ATOMIC_OP_MOVE8B,
1838 .InlineData = true,
1839 .MemoryAddress = clear_addr);
1840 /* dw starts at dword 1, but we need to fill dwords 3 and 5 */
1841 dw[2] = info->clear_color.u32[0];
1842 dw[3] = 0;
1843 dw[4] = info->clear_color.u32[1];
1844 dw[5] = 0;
1845
1846 clear_addr.offset += 8;
1847 dw = blorp_emitn(batch, GENX(MI_ATOMIC), num_dwords,
1848 .DataSize = MI_ATOMIC_QWORD,
1849 .ATOMICOPCODE = MI_ATOMIC_OP_MOVE8B,
1850 .CSSTALL = true,
1851 .ReturnDataControl = true,
1852 .InlineData = true,
1853 .MemoryAddress = clear_addr);
1854 /* dw starts at dword 1, but we need to fill dwords 3 and 5 */
1855 dw[2] = info->clear_color.u32[2];
1856 dw[3] = 0;
1857 dw[4] = info->clear_color.u32[3];
1858 dw[5] = 0;
1859
1860 blorp_emit(batch, GENX(PIPE_CONTROL), pipe) {
1861 pipe.StateCacheInvalidationEnable = true;
1862 pipe.TextureCacheInvalidationEnable = true;
1863 }
1864 #elif GEN_GEN >= 9
1865
1866 /* According to GEN:BUG:2201730850, in the Clear Color Programming Note
1867 * under the Red channel, "Software shall write the converted Depth
1868 * Clear to this dword." The only depth formats listed under the red
1869 * channel are IEEE_FP and UNORM24_X8. These two requirements are
1870 * incompatible with the UNORM16 depth format, so just ignore that case
1871 * and simply perform the conversion for all depth formats.
1872 */
1873 union isl_color_value fixed_color = info->clear_color;
1874 if (GEN_GEN == 12 && isl_surf_usage_is_depth(info->surf.usage)) {
1875 isl_color_value_pack(&info->clear_color, info->surf.format,
1876 fixed_color.u32);
1877 }
1878
1879 for (int i = 0; i < 4; i++) {
1880 blorp_emit(batch, GENX(MI_STORE_DATA_IMM), sdi) {
1881 sdi.Address = info->clear_color_addr;
1882 sdi.Address.offset += i * 4;
1883 sdi.ImmediateData = fixed_color.u32[i];
1884 #if GEN_GEN >= 12
1885 if (i == 3)
1886 sdi.ForceWriteCompletionCheck = true;
1887 #endif
1888 }
1889 }
1890
1891 /* The RENDER_SURFACE_STATE::ClearColor field states that software should
1892 * write the converted depth value 16B after the clear address:
1893 *
1894 * 3D Sampler will always fetch clear depth from the location 16-bytes
1895 * above this address, where the clear depth, converted to native
1896 * surface format by software, will be stored.
1897 *
1898 */
1899 #if GEN_GEN >= 12
1900 if (isl_surf_usage_is_depth(info->surf.usage)) {
1901 blorp_emit(batch, GENX(MI_STORE_DATA_IMM), sdi) {
1902 sdi.Address = info->clear_color_addr;
1903 sdi.Address.offset += 4 * 4;
1904 sdi.ImmediateData = fixed_color.u32[0];
1905 sdi.ForceWriteCompletionCheck = true;
1906 }
1907 }
1908 #endif
1909
1910 #elif GEN_GEN >= 7
1911 blorp_emit(batch, GENX(MI_STORE_DATA_IMM), sdi) {
1912 sdi.Address = info->clear_color_addr;
1913 sdi.ImmediateData = ISL_CHANNEL_SELECT_RED << 25 |
1914 ISL_CHANNEL_SELECT_GREEN << 22 |
1915 ISL_CHANNEL_SELECT_BLUE << 19 |
1916 ISL_CHANNEL_SELECT_ALPHA << 16;
1917 if (isl_format_has_int_channel(info->view.format)) {
1918 for (unsigned i = 0; i < 4; i++) {
1919 assert(info->clear_color.u32[i] == 0 ||
1920 info->clear_color.u32[i] == 1);
1921 }
1922 sdi.ImmediateData |= (info->clear_color.u32[0] != 0) << 31;
1923 sdi.ImmediateData |= (info->clear_color.u32[1] != 0) << 30;
1924 sdi.ImmediateData |= (info->clear_color.u32[2] != 0) << 29;
1925 sdi.ImmediateData |= (info->clear_color.u32[3] != 0) << 28;
1926 } else {
1927 for (unsigned i = 0; i < 4; i++) {
1928 assert(info->clear_color.f32[i] == 0.0f ||
1929 info->clear_color.f32[i] == 1.0f);
1930 }
1931 sdi.ImmediateData |= (info->clear_color.f32[0] != 0.0f) << 31;
1932 sdi.ImmediateData |= (info->clear_color.f32[1] != 0.0f) << 30;
1933 sdi.ImmediateData |= (info->clear_color.f32[2] != 0.0f) << 29;
1934 sdi.ImmediateData |= (info->clear_color.f32[3] != 0.0f) << 28;
1935 }
1936 }
1937 #endif
1938 }
1939 }
1940
1941 /**
1942 * \brief Execute a blit or render pass operation.
1943 *
1944 * To execute the operation, this function manually constructs and emits a
1945 * batch to draw a rectangle primitive. The batchbuffer is flushed before
1946 * constructing and after emitting the batch.
1947 *
1948 * This function alters no GL state.
1949 */
1950 static void
1951 blorp_exec(struct blorp_batch *batch, const struct blorp_params *params)
1952 {
1953 if (!(batch->flags & BLORP_BATCH_NO_UPDATE_CLEAR_COLOR)) {
1954 blorp_update_clear_color(batch, &params->dst, params->fast_clear_op);
1955 blorp_update_clear_color(batch, &params->depth, params->hiz_op);
1956 }
1957
1958 #if GEN_GEN >= 8
1959 if (params->hiz_op != ISL_AUX_OP_NONE) {
1960 blorp_emit_gen8_hiz_op(batch, params);
1961 return;
1962 }
1963 #endif
1964
1965 blorp_emit_vertex_buffers(batch, params);
1966 blorp_emit_vertex_elements(batch, params);
1967
1968 blorp_emit_pipeline(batch, params);
1969
1970 blorp_emit_surface_states(batch, params);
1971
1972 if (!(batch->flags & BLORP_BATCH_NO_EMIT_DEPTH_STENCIL))
1973 blorp_emit_depth_stencil_config(batch, params);
1974
1975 blorp_emit(batch, GENX(3DPRIMITIVE), prim) {
1976 prim.VertexAccessType = SEQUENTIAL;
1977 prim.PrimitiveTopologyType = _3DPRIM_RECTLIST;
1978 #if GEN_GEN >= 7
1979 prim.PredicateEnable = batch->flags & BLORP_BATCH_PREDICATE_ENABLE;
1980 #endif
1981 prim.VertexCountPerInstance = 3;
1982 prim.InstanceCount = params->num_layers;
1983 }
1984 }
1985
1986 #endif /* BLORP_GENX_EXEC_H */