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