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