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