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