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