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