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