Added few more stubs so that control reaches to DestroyDevice().
[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_address(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 ASSERTED 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_address(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 #if GEN_GEN >= 12
1355 /* Disable Primitive Replication. */
1356 blorp_emit(batch, GENX(3DSTATE_PRIMITIVE_REPLICATION), pr);
1357 #endif
1358 }
1359
1360 /******** This is the end of the pipeline setup code ********/
1361
1362 #endif /* GEN_GEN >= 6 */
1363
1364 #if GEN_GEN >= 7
1365 static void
1366 blorp_emit_memcpy(struct blorp_batch *batch,
1367 struct blorp_address dst,
1368 struct blorp_address src,
1369 uint32_t size)
1370 {
1371 assert(size % 4 == 0);
1372
1373 for (unsigned dw = 0; dw < size; dw += 4) {
1374 #if GEN_GEN >= 8
1375 blorp_emit(batch, GENX(MI_COPY_MEM_MEM), cp) {
1376 cp.DestinationMemoryAddress = dst;
1377 cp.SourceMemoryAddress = src;
1378 }
1379 #else
1380 /* IVB does not have a general purpose register for command streamer
1381 * commands. Therefore, we use an alternate temporary register.
1382 */
1383 #define BLORP_TEMP_REG 0x2440 /* GEN7_3DPRIM_BASE_VERTEX */
1384 blorp_emit(batch, GENX(MI_LOAD_REGISTER_MEM), load) {
1385 load.RegisterAddress = BLORP_TEMP_REG;
1386 load.MemoryAddress = src;
1387 }
1388 blorp_emit(batch, GENX(MI_STORE_REGISTER_MEM), store) {
1389 store.RegisterAddress = BLORP_TEMP_REG;
1390 store.MemoryAddress = dst;
1391 }
1392 #undef BLORP_TEMP_REG
1393 #endif
1394 dst.offset += 4;
1395 src.offset += 4;
1396 }
1397 }
1398 #endif
1399
1400 static void
1401 blorp_emit_surface_state(struct blorp_batch *batch,
1402 const struct brw_blorp_surface_info *surface,
1403 enum isl_aux_op aux_op,
1404 void *state, uint32_t state_offset,
1405 const bool color_write_disables[4],
1406 bool is_render_target)
1407 {
1408 const struct isl_device *isl_dev = batch->blorp->isl_dev;
1409 struct isl_surf surf = surface->surf;
1410
1411 if (surf.dim == ISL_SURF_DIM_1D &&
1412 surf.dim_layout == ISL_DIM_LAYOUT_GEN4_2D) {
1413 assert(surf.logical_level0_px.height == 1);
1414 surf.dim = ISL_SURF_DIM_2D;
1415 }
1416
1417 if (isl_aux_usage_has_hiz(surface->aux_usage)) {
1418 /* BLORP doesn't render with depth so we can't use HiZ */
1419 assert(!is_render_target);
1420 /* We can't reinterpret HiZ */
1421 assert(surface->surf.format == surface->view.format);
1422 }
1423 enum isl_aux_usage aux_usage = surface->aux_usage;
1424
1425 isl_channel_mask_t write_disable_mask = 0;
1426 if (is_render_target && GEN_GEN <= 5) {
1427 if (color_write_disables[0])
1428 write_disable_mask |= ISL_CHANNEL_RED_BIT;
1429 if (color_write_disables[1])
1430 write_disable_mask |= ISL_CHANNEL_GREEN_BIT;
1431 if (color_write_disables[2])
1432 write_disable_mask |= ISL_CHANNEL_BLUE_BIT;
1433 if (color_write_disables[3])
1434 write_disable_mask |= ISL_CHANNEL_ALPHA_BIT;
1435 }
1436
1437 const bool use_clear_address =
1438 GEN_GEN >= 10 && (surface->clear_color_addr.buffer != NULL);
1439
1440 isl_surf_fill_state(batch->blorp->isl_dev, state,
1441 .surf = &surf, .view = &surface->view,
1442 .aux_surf = &surface->aux_surf, .aux_usage = aux_usage,
1443 .address =
1444 blorp_get_surface_address(batch, surface->addr),
1445 .aux_address = aux_usage == ISL_AUX_USAGE_NONE ? 0 :
1446 blorp_get_surface_address(batch, surface->aux_addr),
1447 .clear_address = !use_clear_address ? 0 :
1448 blorp_get_surface_address(batch,
1449 surface->clear_color_addr),
1450 .mocs = surface->addr.mocs,
1451 .clear_color = surface->clear_color,
1452 .use_clear_address = use_clear_address,
1453 .write_disables = write_disable_mask);
1454
1455 blorp_surface_reloc(batch, state_offset + isl_dev->ss.addr_offset,
1456 surface->addr, 0);
1457
1458 if (aux_usage != ISL_AUX_USAGE_NONE) {
1459 /* On gen7 and prior, the bottom 12 bits of the MCS base address are
1460 * used to store other information. This should be ok, however, because
1461 * surface buffer addresses are always 4K page alinged.
1462 */
1463 assert((surface->aux_addr.offset & 0xfff) == 0);
1464 uint32_t *aux_addr = state + isl_dev->ss.aux_addr_offset;
1465 blorp_surface_reloc(batch, state_offset + isl_dev->ss.aux_addr_offset,
1466 surface->aux_addr, *aux_addr);
1467 }
1468
1469 if (aux_usage != ISL_AUX_USAGE_NONE && surface->clear_color_addr.buffer) {
1470 #if GEN_GEN >= 10
1471 assert((surface->clear_color_addr.offset & 0x3f) == 0);
1472 uint32_t *clear_addr = state + isl_dev->ss.clear_color_state_offset;
1473 blorp_surface_reloc(batch, state_offset +
1474 isl_dev->ss.clear_color_state_offset,
1475 surface->clear_color_addr, *clear_addr);
1476 #elif GEN_GEN >= 7
1477 /* Fast clears just whack the AUX surface and don't actually use the
1478 * clear color for anything. We can avoid the MI memcpy on that case.
1479 */
1480 if (aux_op != ISL_AUX_OP_FAST_CLEAR) {
1481 struct blorp_address dst_addr = blorp_get_surface_base_address(batch);
1482 dst_addr.offset += state_offset + isl_dev->ss.clear_value_offset;
1483 blorp_emit_memcpy(batch, dst_addr, surface->clear_color_addr,
1484 isl_dev->ss.clear_value_size);
1485 }
1486 #else
1487 unreachable("Fast clears are only supported on gen7+");
1488 #endif
1489 }
1490
1491 blorp_flush_range(batch, state, GENX(RENDER_SURFACE_STATE_length) * 4);
1492 }
1493
1494 static void
1495 blorp_emit_null_surface_state(struct blorp_batch *batch,
1496 const struct brw_blorp_surface_info *surface,
1497 uint32_t *state)
1498 {
1499 struct GENX(RENDER_SURFACE_STATE) ss = {
1500 .SurfaceType = SURFTYPE_NULL,
1501 .SurfaceFormat = ISL_FORMAT_R8G8B8A8_UNORM,
1502 .Width = surface->surf.logical_level0_px.width - 1,
1503 .Height = surface->surf.logical_level0_px.height - 1,
1504 .MIPCountLOD = surface->view.base_level,
1505 .MinimumArrayElement = surface->view.base_array_layer,
1506 .Depth = surface->view.array_len - 1,
1507 .RenderTargetViewExtent = surface->view.array_len - 1,
1508 #if GEN_GEN >= 6
1509 .NumberofMultisamples = ffs(surface->surf.samples) - 1,
1510 #endif
1511
1512 #if GEN_GEN >= 7
1513 .SurfaceArray = surface->surf.dim != ISL_SURF_DIM_3D,
1514 #endif
1515
1516 #if GEN_GEN >= 8
1517 .TileMode = YMAJOR,
1518 #else
1519 .TiledSurface = true,
1520 #endif
1521 };
1522
1523 GENX(RENDER_SURFACE_STATE_pack)(NULL, state, &ss);
1524
1525 blorp_flush_range(batch, state, GENX(RENDER_SURFACE_STATE_length) * 4);
1526 }
1527
1528 static void
1529 blorp_emit_surface_states(struct blorp_batch *batch,
1530 const struct blorp_params *params)
1531 {
1532 const struct isl_device *isl_dev = batch->blorp->isl_dev;
1533 uint32_t bind_offset = 0, surface_offsets[2];
1534 void *surface_maps[2];
1535
1536 UNUSED bool has_indirect_clear_color = false;
1537 if (params->use_pre_baked_binding_table) {
1538 bind_offset = params->pre_baked_binding_table_offset;
1539 } else {
1540 unsigned num_surfaces = 1 + params->src.enabled;
1541 blorp_alloc_binding_table(batch, num_surfaces,
1542 isl_dev->ss.size, isl_dev->ss.align,
1543 &bind_offset, surface_offsets, surface_maps);
1544
1545 if (params->dst.enabled) {
1546 blorp_emit_surface_state(batch, &params->dst,
1547 params->fast_clear_op,
1548 surface_maps[BLORP_RENDERBUFFER_BT_INDEX],
1549 surface_offsets[BLORP_RENDERBUFFER_BT_INDEX],
1550 params->color_write_disable, true);
1551 if (params->dst.clear_color_addr.buffer != NULL)
1552 has_indirect_clear_color = true;
1553 } else {
1554 assert(params->depth.enabled || params->stencil.enabled);
1555 const struct brw_blorp_surface_info *surface =
1556 params->depth.enabled ? &params->depth : &params->stencil;
1557 blorp_emit_null_surface_state(batch, surface,
1558 surface_maps[BLORP_RENDERBUFFER_BT_INDEX]);
1559 }
1560
1561 if (params->src.enabled) {
1562 blorp_emit_surface_state(batch, &params->src,
1563 params->fast_clear_op,
1564 surface_maps[BLORP_TEXTURE_BT_INDEX],
1565 surface_offsets[BLORP_TEXTURE_BT_INDEX],
1566 NULL, false);
1567 if (params->src.clear_color_addr.buffer != NULL)
1568 has_indirect_clear_color = true;
1569 }
1570 }
1571
1572 #if GEN_GEN >= 7
1573 if (has_indirect_clear_color) {
1574 /* Updating a surface state object may require that the state cache be
1575 * invalidated. From the SKL PRM, Shared Functions -> State -> State
1576 * Caching:
1577 *
1578 * Whenever the RENDER_SURFACE_STATE object in memory pointed to by
1579 * the Binding Table Pointer (BTP) and Binding Table Index (BTI) is
1580 * modified [...], the L1 state cache must be invalidated to ensure
1581 * the new surface or sampler state is fetched from system memory.
1582 */
1583 blorp_emit(batch, GENX(PIPE_CONTROL), pipe) {
1584 pipe.StateCacheInvalidationEnable = true;
1585 #if GEN_GEN >= 12
1586 pipe.TileCacheFlushEnable = true;
1587 #endif
1588 }
1589 }
1590 #endif
1591
1592 #if GEN_GEN >= 7
1593 blorp_emit(batch, GENX(3DSTATE_BINDING_TABLE_POINTERS_VS), bt);
1594 blorp_emit(batch, GENX(3DSTATE_BINDING_TABLE_POINTERS_HS), bt);
1595 blorp_emit(batch, GENX(3DSTATE_BINDING_TABLE_POINTERS_DS), bt);
1596 blorp_emit(batch, GENX(3DSTATE_BINDING_TABLE_POINTERS_GS), bt);
1597
1598 blorp_emit(batch, GENX(3DSTATE_BINDING_TABLE_POINTERS_PS), bt) {
1599 bt.PointertoPSBindingTable = bind_offset;
1600 }
1601 #elif GEN_GEN >= 6
1602 blorp_emit(batch, GENX(3DSTATE_BINDING_TABLE_POINTERS), bt) {
1603 bt.PSBindingTableChange = true;
1604 bt.PointertoPSBindingTable = bind_offset;
1605 }
1606 #else
1607 blorp_emit(batch, GENX(3DSTATE_BINDING_TABLE_POINTERS), bt) {
1608 bt.PointertoPSBindingTable = bind_offset;
1609 }
1610 #endif
1611 }
1612
1613 static void
1614 blorp_emit_depth_stencil_config(struct blorp_batch *batch,
1615 const struct blorp_params *params)
1616 {
1617 const struct isl_device *isl_dev = batch->blorp->isl_dev;
1618
1619 uint32_t *dw = blorp_emit_dwords(batch, isl_dev->ds.size / 4);
1620 if (dw == NULL)
1621 return;
1622
1623 struct isl_depth_stencil_hiz_emit_info info = { };
1624
1625 if (params->depth.enabled) {
1626 info.view = &params->depth.view;
1627 info.mocs = params->depth.addr.mocs;
1628 } else if (params->stencil.enabled) {
1629 info.view = &params->stencil.view;
1630 info.mocs = params->stencil.addr.mocs;
1631 }
1632
1633 if (params->depth.enabled) {
1634 info.depth_surf = &params->depth.surf;
1635
1636 info.depth_address =
1637 blorp_emit_reloc(batch, dw + isl_dev->ds.depth_offset / 4,
1638 params->depth.addr, 0);
1639
1640 info.hiz_usage = params->depth.aux_usage;
1641 if (isl_aux_usage_has_hiz(info.hiz_usage)) {
1642 info.hiz_surf = &params->depth.aux_surf;
1643
1644 struct blorp_address hiz_address = params->depth.aux_addr;
1645 #if GEN_GEN == 6
1646 /* Sandy bridge hardware does not technically support mipmapped HiZ.
1647 * However, we have a special layout that allows us to make it work
1648 * anyway by manually offsetting to the specified miplevel.
1649 */
1650 assert(info.hiz_surf->dim_layout == ISL_DIM_LAYOUT_GEN6_STENCIL_HIZ);
1651 uint32_t offset_B;
1652 isl_surf_get_image_offset_B_tile_sa(info.hiz_surf,
1653 info.view->base_level, 0, 0,
1654 &offset_B, NULL, NULL);
1655 hiz_address.offset += offset_B;
1656 #endif
1657
1658 info.hiz_address =
1659 blorp_emit_reloc(batch, dw + isl_dev->ds.hiz_offset / 4,
1660 hiz_address, 0);
1661
1662 info.depth_clear_value = params->depth.clear_color.f32[0];
1663 }
1664 }
1665
1666 if (params->stencil.enabled) {
1667 info.stencil_surf = &params->stencil.surf;
1668
1669 info.stencil_aux_usage = params->stencil.aux_usage;
1670 struct blorp_address stencil_address = params->stencil.addr;
1671 #if GEN_GEN == 6
1672 /* Sandy bridge hardware does not technically support mipmapped stencil.
1673 * However, we have a special layout that allows us to make it work
1674 * anyway by manually offsetting to the specified miplevel.
1675 */
1676 assert(info.stencil_surf->dim_layout == ISL_DIM_LAYOUT_GEN6_STENCIL_HIZ);
1677 uint32_t offset_B;
1678 isl_surf_get_image_offset_B_tile_sa(info.stencil_surf,
1679 info.view->base_level, 0, 0,
1680 &offset_B, NULL, NULL);
1681 stencil_address.offset += offset_B;
1682 #endif
1683
1684 info.stencil_address =
1685 blorp_emit_reloc(batch, dw + isl_dev->ds.stencil_offset / 4,
1686 stencil_address, 0);
1687 }
1688
1689 isl_emit_depth_stencil_hiz_s(isl_dev, dw, &info);
1690
1691 #if GEN_GEN >= 12
1692 /* GEN:BUG:1408224581
1693 *
1694 * Workaround: Gen12LP Astep only An additional pipe control with
1695 * post-sync = store dword operation would be required.( w/a is to
1696 * have an additional pipe control after the stencil state whenever
1697 * the surface state bits of this state is changing).
1698 */
1699 blorp_emit(batch, GENX(PIPE_CONTROL), pc) {
1700 pc.PostSyncOperation = WriteImmediateData;
1701 pc.Address = blorp_get_workaround_address(batch);
1702 }
1703 #endif
1704 }
1705
1706 #if GEN_GEN >= 8
1707 /* Emits the Optimized HiZ sequence specified in the BDW+ PRMs. The
1708 * depth/stencil buffer extents are ignored to handle APIs which perform
1709 * clearing operations without such information.
1710 * */
1711 static void
1712 blorp_emit_gen8_hiz_op(struct blorp_batch *batch,
1713 const struct blorp_params *params)
1714 {
1715 /* We should be performing an operation on a depth or stencil buffer.
1716 */
1717 assert(params->depth.enabled || params->stencil.enabled);
1718
1719 /* The stencil buffer should only be enabled on GEN == 12, if a fast clear
1720 * or full resolve operation is requested. On rest of the GEN, if a fast
1721 * clear operation is requested.
1722 */
1723 if (params->stencil.enabled) {
1724 #if GEN_GEN >= 12
1725 assert(params->hiz_op == ISL_AUX_OP_FAST_CLEAR ||
1726 params->hiz_op == ISL_AUX_OP_FULL_RESOLVE);
1727 #else
1728 assert(params->hiz_op == ISL_AUX_OP_FAST_CLEAR);
1729 #endif
1730 }
1731
1732 /* From the BDW PRM Volume 2, 3DSTATE_WM_HZ_OP:
1733 *
1734 * 3DSTATE_MULTISAMPLE packet must be used prior to this packet to change
1735 * the Number of Multisamples. This packet must not be used to change
1736 * Number of Multisamples in a rendering sequence.
1737 *
1738 * Since HIZ may be the first thing in a batch buffer, play safe and always
1739 * emit 3DSTATE_MULTISAMPLE.
1740 */
1741 blorp_emit_3dstate_multisample(batch, params);
1742
1743 /* From the BDW PRM Volume 7, Depth Buffer Clear:
1744 *
1745 * The clear value must be between the min and max depth values
1746 * (inclusive) defined in the CC_VIEWPORT. If the depth buffer format is
1747 * D32_FLOAT, then +/-DENORM values are also allowed.
1748 *
1749 * Set the bounds to match our hardware limits, [0.0, 1.0].
1750 */
1751 if (params->depth.enabled && params->hiz_op == ISL_AUX_OP_FAST_CLEAR) {
1752 assert(params->depth.clear_color.f32[0] >= 0.0f);
1753 assert(params->depth.clear_color.f32[0] <= 1.0f);
1754 blorp_emit_cc_viewport(batch);
1755 }
1756
1757 if (GEN_GEN >= 12 && params->stencil.enabled &&
1758 params->hiz_op == ISL_AUX_OP_FULL_RESOLVE) {
1759 /* GEN:BUG:1605967699
1760 *
1761 * This workaround requires that the Force Thread Dispatch Enable flag
1762 * needs to be set to ForceOFF on the first WM_HZ_OP state cycle
1763 * (followed by a CS Stall):
1764 *
1765 * "Workaround: There is a potential software workaround for the
1766 * issue by doing these 2 steps 1) setting the force thread dispatch
1767 * enable(bits 20:19) in the 3dstate_WM_body state to be set to
1768 * Force_OFF (value of 1) along with the first WM_HZ_OP state cycle.
1769 * The second WM_HZ_OP state which is required by programming
1770 * sequencing to complete the HZ_OP operation can reprogram the
1771 * 3dstate_WM_body to set to NORMAL(value of 0)."
1772 */
1773 blorp_emit(batch, GENX(3DSTATE_WM), wm) {
1774 wm.ForceThreadDispatchEnable = ForceOff;
1775 }
1776 blorp_emit(batch, GENX(PIPE_CONTROL), pipe) {
1777 pipe.CommandStreamerStallEnable = true;
1778 }
1779 } else {
1780 /* According to the SKL PRM formula for WM_INT::ThreadDispatchEnable, the
1781 * 3DSTATE_WM::ForceThreadDispatchEnable field can force WM thread dispatch
1782 * even when WM_HZ_OP is active. However, WM thread dispatch is normally
1783 * disabled for HiZ ops and it appears that force-enabling it can lead to
1784 * GPU hangs on at least Skylake. Since we don't know the current state of
1785 * the 3DSTATE_WM packet, just emit a dummy one prior to 3DSTATE_WM_HZ_OP.
1786 */
1787 blorp_emit(batch, GENX(3DSTATE_WM), wm);
1788 }
1789
1790 /* If we can't alter the depth stencil config and multiple layers are
1791 * involved, the HiZ op will fail. This is because the op requires that a
1792 * new config is emitted for each additional layer.
1793 */
1794 if (batch->flags & BLORP_BATCH_NO_EMIT_DEPTH_STENCIL) {
1795 assert(params->num_layers <= 1);
1796 } else {
1797 blorp_emit_depth_stencil_config(batch, params);
1798 }
1799
1800 blorp_emit(batch, GENX(3DSTATE_WM_HZ_OP), hzp) {
1801 switch (params->hiz_op) {
1802 case ISL_AUX_OP_FAST_CLEAR:
1803 hzp.StencilBufferClearEnable = params->stencil.enabled;
1804 hzp.DepthBufferClearEnable = params->depth.enabled;
1805 hzp.StencilClearValue = params->stencil_ref;
1806 hzp.FullSurfaceDepthandStencilClear = params->full_surface_hiz_op;
1807 break;
1808 case ISL_AUX_OP_FULL_RESOLVE:
1809 assert(params->full_surface_hiz_op);
1810 hzp.DepthBufferResolveEnable = params->depth.enabled;
1811 #if GEN_GEN >= 12
1812 if (params->stencil.enabled) {
1813 assert(params->stencil.aux_usage == ISL_AUX_USAGE_STC_CCS);
1814 hzp.StencilBufferResolveEnable = true;
1815 }
1816 #endif
1817 break;
1818 case ISL_AUX_OP_AMBIGUATE:
1819 assert(params->full_surface_hiz_op);
1820 hzp.HierarchicalDepthBufferResolveEnable = true;
1821 break;
1822 case ISL_AUX_OP_PARTIAL_RESOLVE:
1823 case ISL_AUX_OP_NONE:
1824 unreachable("Invalid HIZ op");
1825 }
1826
1827 hzp.NumberofMultisamples = ffs(params->num_samples) - 1;
1828 hzp.SampleMask = 0xFFFF;
1829
1830 /* Due to a hardware issue, this bit MBZ */
1831 assert(hzp.ScissorRectangleEnable == false);
1832
1833 /* Contrary to the HW docs both fields are inclusive */
1834 hzp.ClearRectangleXMin = params->x0;
1835 hzp.ClearRectangleYMin = params->y0;
1836
1837 /* Contrary to the HW docs both fields are exclusive */
1838 hzp.ClearRectangleXMax = params->x1;
1839 hzp.ClearRectangleYMax = params->y1;
1840 }
1841
1842 /* PIPE_CONTROL w/ all bits clear except for “Post-Sync Operation” must set
1843 * to “Write Immediate Data” enabled.
1844 */
1845 blorp_emit(batch, GENX(PIPE_CONTROL), pc) {
1846 pc.PostSyncOperation = WriteImmediateData;
1847 pc.Address = blorp_get_workaround_address(batch);
1848 }
1849
1850
1851 if (GEN_GEN >= 12 && params->stencil.enabled &&
1852 params->hiz_op == ISL_AUX_OP_FULL_RESOLVE) {
1853 /* GEN:BUG:1605967699
1854 *
1855 * The second WM_HZ_OP state which is required by programming
1856 * sequencing to complete the HZ_OP operation can reprogram the
1857 * 3dstate_WM_body to set to NORMAL(value of 0)."
1858 */
1859 blorp_emit(batch, GENX(3DSTATE_WM), wm);
1860 }
1861
1862 blorp_emit(batch, GENX(3DSTATE_WM_HZ_OP), hzp);
1863 }
1864 #endif
1865
1866 static void
1867 blorp_update_clear_color(struct blorp_batch *batch,
1868 const struct brw_blorp_surface_info *info,
1869 enum isl_aux_op op)
1870 {
1871 if (info->clear_color_addr.buffer && op == ISL_AUX_OP_FAST_CLEAR) {
1872 #if GEN_GEN == 11
1873 blorp_emit(batch, GENX(PIPE_CONTROL), pipe) {
1874 pipe.CommandStreamerStallEnable = true;
1875 }
1876
1877 /* 2 QWORDS */
1878 const unsigned inlinedata_dw = 2 * 2;
1879 const unsigned num_dwords = GENX(MI_ATOMIC_length) + inlinedata_dw;
1880
1881 struct blorp_address clear_addr = info->clear_color_addr;
1882 uint32_t *dw = blorp_emitn(batch, GENX(MI_ATOMIC), num_dwords,
1883 .DataSize = MI_ATOMIC_QWORD,
1884 .ATOMICOPCODE = MI_ATOMIC_OP_MOVE8B,
1885 .InlineData = true,
1886 .MemoryAddress = clear_addr);
1887 /* dw starts at dword 1, but we need to fill dwords 3 and 5 */
1888 dw[2] = info->clear_color.u32[0];
1889 dw[3] = 0;
1890 dw[4] = info->clear_color.u32[1];
1891 dw[5] = 0;
1892
1893 clear_addr.offset += 8;
1894 dw = blorp_emitn(batch, GENX(MI_ATOMIC), num_dwords,
1895 .DataSize = MI_ATOMIC_QWORD,
1896 .ATOMICOPCODE = MI_ATOMIC_OP_MOVE8B,
1897 .CSSTALL = true,
1898 .ReturnDataControl = true,
1899 .InlineData = true,
1900 .MemoryAddress = clear_addr);
1901 /* dw starts at dword 1, but we need to fill dwords 3 and 5 */
1902 dw[2] = info->clear_color.u32[2];
1903 dw[3] = 0;
1904 dw[4] = info->clear_color.u32[3];
1905 dw[5] = 0;
1906
1907 blorp_emit(batch, GENX(PIPE_CONTROL), pipe) {
1908 pipe.StateCacheInvalidationEnable = true;
1909 pipe.TextureCacheInvalidationEnable = true;
1910 }
1911 #elif GEN_GEN >= 9
1912
1913 /* According to GEN:BUG:2201730850, in the Clear Color Programming Note
1914 * under the Red channel, "Software shall write the converted Depth
1915 * Clear to this dword." The only depth formats listed under the red
1916 * channel are IEEE_FP and UNORM24_X8. These two requirements are
1917 * incompatible with the UNORM16 depth format, so just ignore that case
1918 * and simply perform the conversion for all depth formats.
1919 */
1920 union isl_color_value fixed_color = info->clear_color;
1921 if (GEN_GEN == 12 && isl_surf_usage_is_depth(info->surf.usage)) {
1922 isl_color_value_pack(&info->clear_color, info->surf.format,
1923 fixed_color.u32);
1924 }
1925
1926 for (int i = 0; i < 4; i++) {
1927 blorp_emit(batch, GENX(MI_STORE_DATA_IMM), sdi) {
1928 sdi.Address = info->clear_color_addr;
1929 sdi.Address.offset += i * 4;
1930 sdi.ImmediateData = fixed_color.u32[i];
1931 #if GEN_GEN >= 12
1932 if (i == 3)
1933 sdi.ForceWriteCompletionCheck = true;
1934 #endif
1935 }
1936 }
1937
1938 /* The RENDER_SURFACE_STATE::ClearColor field states that software should
1939 * write the converted depth value 16B after the clear address:
1940 *
1941 * 3D Sampler will always fetch clear depth from the location 16-bytes
1942 * above this address, where the clear depth, converted to native
1943 * surface format by software, will be stored.
1944 *
1945 */
1946 #if GEN_GEN >= 12
1947 if (isl_surf_usage_is_depth(info->surf.usage)) {
1948 blorp_emit(batch, GENX(MI_STORE_DATA_IMM), sdi) {
1949 sdi.Address = info->clear_color_addr;
1950 sdi.Address.offset += 4 * 4;
1951 sdi.ImmediateData = fixed_color.u32[0];
1952 sdi.ForceWriteCompletionCheck = true;
1953 }
1954 }
1955 #endif
1956
1957 #elif GEN_GEN >= 7
1958 blorp_emit(batch, GENX(MI_STORE_DATA_IMM), sdi) {
1959 sdi.Address = info->clear_color_addr;
1960 sdi.ImmediateData = ISL_CHANNEL_SELECT_RED << 25 |
1961 ISL_CHANNEL_SELECT_GREEN << 22 |
1962 ISL_CHANNEL_SELECT_BLUE << 19 |
1963 ISL_CHANNEL_SELECT_ALPHA << 16;
1964 if (isl_format_has_int_channel(info->view.format)) {
1965 for (unsigned i = 0; i < 4; i++) {
1966 assert(info->clear_color.u32[i] == 0 ||
1967 info->clear_color.u32[i] == 1);
1968 }
1969 sdi.ImmediateData |= (info->clear_color.u32[0] != 0) << 31;
1970 sdi.ImmediateData |= (info->clear_color.u32[1] != 0) << 30;
1971 sdi.ImmediateData |= (info->clear_color.u32[2] != 0) << 29;
1972 sdi.ImmediateData |= (info->clear_color.u32[3] != 0) << 28;
1973 } else {
1974 for (unsigned i = 0; i < 4; i++) {
1975 assert(info->clear_color.f32[i] == 0.0f ||
1976 info->clear_color.f32[i] == 1.0f);
1977 }
1978 sdi.ImmediateData |= (info->clear_color.f32[0] != 0.0f) << 31;
1979 sdi.ImmediateData |= (info->clear_color.f32[1] != 0.0f) << 30;
1980 sdi.ImmediateData |= (info->clear_color.f32[2] != 0.0f) << 29;
1981 sdi.ImmediateData |= (info->clear_color.f32[3] != 0.0f) << 28;
1982 }
1983 }
1984 #endif
1985 }
1986 }
1987
1988 /**
1989 * \brief Execute a blit or render pass operation.
1990 *
1991 * To execute the operation, this function manually constructs and emits a
1992 * batch to draw a rectangle primitive. The batchbuffer is flushed before
1993 * constructing and after emitting the batch.
1994 *
1995 * This function alters no GL state.
1996 */
1997 static void
1998 blorp_exec(struct blorp_batch *batch, const struct blorp_params *params)
1999 {
2000 if (!(batch->flags & BLORP_BATCH_NO_UPDATE_CLEAR_COLOR)) {
2001 blorp_update_clear_color(batch, &params->dst, params->fast_clear_op);
2002 blorp_update_clear_color(batch, &params->depth, params->hiz_op);
2003 }
2004
2005 #if GEN_GEN >= 8
2006 if (params->hiz_op != ISL_AUX_OP_NONE) {
2007 blorp_emit_gen8_hiz_op(batch, params);
2008 return;
2009 }
2010 #endif
2011
2012 blorp_emit_vertex_buffers(batch, params);
2013 blorp_emit_vertex_elements(batch, params);
2014
2015 blorp_emit_pipeline(batch, params);
2016
2017 blorp_emit_surface_states(batch, params);
2018
2019 if (!(batch->flags & BLORP_BATCH_NO_EMIT_DEPTH_STENCIL))
2020 blorp_emit_depth_stencil_config(batch, params);
2021
2022 blorp_emit(batch, GENX(3DPRIMITIVE), prim) {
2023 prim.VertexAccessType = SEQUENTIAL;
2024 prim.PrimitiveTopologyType = _3DPRIM_RECTLIST;
2025 #if GEN_GEN >= 7
2026 prim.PredicateEnable = batch->flags & BLORP_BATCH_PREDICATE_ENABLE;
2027 #endif
2028 prim.VertexCountPerInstance = 3;
2029 prim.InstanceCount = params->num_layers;
2030 }
2031 }
2032
2033 #endif /* BLORP_GENX_EXEC_H */