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