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