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