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