Merge branch 'wip/nir-vtn' into vulkan
[mesa.git] / src / gallium / drivers / ilo / core / ilo_state_surface.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 2012-2015 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Chia-I Wu <olv@lunarg.com>
26 */
27
28 #include "ilo_debug.h"
29 #include "ilo_buffer.h"
30 #include "ilo_image.h"
31 #include "ilo_state_surface.h"
32
33 static bool
34 surface_set_gen6_null_SURFACE_STATE(struct ilo_state_surface *surf,
35 const struct ilo_dev *dev)
36 {
37 uint32_t dw0, dw3;
38
39 ILO_DEV_ASSERT(dev, 6, 6);
40
41 /*
42 * From the Sandy Bridge PRM, volume 4 part 1, page 71:
43 *
44 * "All of the remaining fields in surface state are ignored for null
45 * surfaces, with the following exceptions:
46 *
47 * - [DevSNB+]: Width, Height, Depth, and LOD fields must match the
48 * depth buffer's corresponding state for all render target
49 * surfaces, including null.
50 * - Surface Format must be R8G8B8A8_UNORM."
51 *
52 * From the Sandy Bridge PRM, volume 4 part 1, page 82:
53 *
54 * "If Surface Type is SURFTYPE_NULL, this field (Tiled Surface) must
55 * be true"
56 *
57 * Note that we ignore the first exception for all surface types.
58 */
59 dw0 = GEN6_SURFTYPE_NULL << GEN6_SURFACE_DW0_TYPE__SHIFT |
60 GEN6_FORMAT_R8G8B8A8_UNORM << GEN6_SURFACE_DW0_FORMAT__SHIFT;
61 dw3 = GEN6_TILING_X << GEN6_SURFACE_DW3_TILING__SHIFT;
62
63 STATIC_ASSERT(ARRAY_SIZE(surf->surface) >= 6);
64 surf->surface[0] = dw0;
65 surf->surface[1] = 0;
66 surf->surface[2] = 0;
67 surf->surface[3] = dw3;
68 surf->surface[4] = 0;
69 surf->surface[5] = 0;
70
71 return true;
72 }
73
74 static bool
75 surface_set_gen7_null_SURFACE_STATE(struct ilo_state_surface *surf,
76 const struct ilo_dev *dev)
77 {
78 uint32_t dw0;
79
80 ILO_DEV_ASSERT(dev, 7, 8);
81
82 dw0 = GEN6_SURFTYPE_NULL << GEN7_SURFACE_DW0_TYPE__SHIFT |
83 GEN6_FORMAT_R8G8B8A8_UNORM << GEN7_SURFACE_DW0_FORMAT__SHIFT;
84 if (ilo_dev_gen(dev) >= ILO_GEN(8))
85 dw0 |= GEN6_TILING_X << GEN8_SURFACE_DW0_TILING__SHIFT;
86 else
87 dw0 |= GEN6_TILING_X << GEN7_SURFACE_DW0_TILING__SHIFT;
88
89 STATIC_ASSERT(ARRAY_SIZE(surf->surface) >= 13);
90 surf->surface[0] = dw0;
91 memset(&surf->surface[1], 0, sizeof(uint32_t) *
92 (((ilo_dev_gen(dev) >= ILO_GEN(8)) ? 13 : 8) - 1));
93
94 return true;
95 }
96
97 static bool
98 surface_validate_gen6_buffer(const struct ilo_dev *dev,
99 const struct ilo_state_surface_buffer_info *info)
100 {
101 ILO_DEV_ASSERT(dev, 6, 8);
102
103 /* SVB writes are Gen6-only */
104 if (ilo_dev_gen(dev) >= ILO_GEN(7))
105 assert(info->access != ILO_STATE_SURFACE_ACCESS_DP_SVB);
106
107 if (info->offset + info->size > info->buf->bo_size) {
108 ilo_warn("invalid buffer range\n");
109 return false;
110 }
111
112 /*
113 * From the Sandy Bridge PRM, volume 4 part 1, page 81:
114 *
115 * "For surfaces of type SURFTYPE_BUFFER: [0,2047] -> [1B, 2048B]
116 * For surfaces of type SURFTYPE_STRBUF: [0,2047] -> [1B, 2048B]"
117 */
118 if (!info->struct_size || info->struct_size > 2048) {
119 ilo_warn("invalid buffer struct size\n");
120 return false;
121 }
122
123 /*
124 * From the Ivy Bridge PRM, volume 4 part 1, page 68:
125 *
126 * "The Base Address for linear render target surfaces and surfaces
127 * accessed with the typed surface read/write data port messages must
128 * be element-size aligned, for non-YUV surface formats, or a multiple
129 * of 2 element-sizes for YUV surface formats. Other linear surfaces
130 * have no alignment requirements (byte alignment is sufficient)."
131 *
132 * "Certain message types used to access surfaces have more stringent
133 * alignment requirements. Please refer to the specific message
134 * documentation for additional restrictions."
135 *
136 * From the Ivy Bridge PRM, volume 4 part 1, page 233, 235, and 237:
137 *
138 * "the surface base address must be OWord aligned"
139 *
140 * for OWord Block Read/Write, Unaligned OWord Block Read, and OWord Dual
141 * Block Read/Write.
142 *
143 * From the Ivy Bridge PRM, volume 4 part 1, page 246 and 249:
144 *
145 * "The surface base address must be DWord aligned"
146 *
147 * for DWord Scattered Read/Write and Byte Scattered Read/Write.
148 *
149 * We have to rely on users to correctly set info->struct_size here. DWord
150 * Scattered Read/Write has conflicting pitch and alignment, but we do not
151 * use them yet so we are fine.
152 *
153 * It is unclear if sampling engine surfaces require aligned offsets.
154 */
155 if (info->access != ILO_STATE_SURFACE_ACCESS_DP_SVB) {
156 assert(info->struct_size % info->format_size == 0);
157
158 if (info->offset % info->struct_size) {
159 ilo_warn("bad buffer offset\n");
160 return false;
161 }
162 }
163
164 if (info->format == GEN6_FORMAT_RAW) {
165 /*
166 * From the Sandy Bridge PRM, volume 4 part 1, page 97:
167 *
168 * ""RAW" is supported only with buffers and structured buffers
169 * accessed via the untyped surface read/write and untyped atomic
170 * operation messages, which do not have a column in the table."
171 *
172 * We do not have a specific access mode for untyped messages.
173 */
174 assert(info->access == ILO_STATE_SURFACE_ACCESS_DP_UNTYPED);
175
176 /*
177 * Nothing is said about Untyped* messages, but I guess they require the
178 * base address to be DWord aligned.
179 */
180 if (info->offset % 4) {
181 ilo_warn("bad RAW buffer offset\n");
182 return false;
183 }
184
185 if (info->struct_size > 1) {
186 /* no STRBUF on Gen6 */
187 if (ilo_dev_gen(dev) == ILO_GEN(6)) {
188 ilo_warn("no STRBUF support\n");
189 return false;
190 }
191
192 /*
193 * From the Ivy Bridge PRM, volume 4 part 1, page 70:
194 *
195 * "For linear surfaces with Surface Type of SURFTYPE_STRBUF, the
196 * pitch must be a multiple of 4 bytes."
197 */
198 if (info->struct_size % 4) {
199 ilo_warn("bad STRBUF pitch\n");
200 return false;
201 }
202 }
203 }
204
205 return true;
206 }
207
208 static bool
209 surface_get_gen6_buffer_struct_count(const struct ilo_dev *dev,
210 const struct ilo_state_surface_buffer_info *info,
211 uint32_t *count)
212 {
213 uint32_t max_struct, c;
214
215 ILO_DEV_ASSERT(dev, 6, 8);
216
217 c = info->size / info->struct_size;
218 if (info->access == ILO_STATE_SURFACE_ACCESS_DP_SVB &&
219 info->format_size < info->size - info->struct_size * c)
220 c++;
221
222 /*
223 * From the Sandy Bridge PRM, volume 4 part 1, page 77:
224 *
225 * "For buffer surfaces, the number of entries in the buffer ranges
226 * from 1 to 2^27."
227 *
228 * From the Ivy Bridge PRM, volume 4 part 1, page 68:
229 *
230 * "For typed buffer and structured buffer surfaces, the number of
231 * entries in the buffer ranges from 1 to 2^27. For raw buffer
232 * surfaces, the number of entries in the buffer is the number of
233 * bytes which can range from 1 to 2^30."
234 *
235 * From the Ivy Bridge PRM, volume 4 part 1, page 69:
236 *
237 * For SURFTYPE_BUFFER: The low two bits of this field (Width) must be
238 * 11 if the Surface Format is RAW (the size of the buffer must be a
239 * multiple of 4 bytes)."
240 */
241 max_struct = 1 << 27;
242 if (info->format == GEN6_FORMAT_RAW && info->struct_size == 1) {
243 if (ilo_dev_gen(dev) >= ILO_GEN(7))
244 max_struct = 1 << 30;
245
246 c &= ~3;
247 }
248
249 if (!c || c > max_struct) {
250 ilo_warn("too many or zero buffer structs\n");
251 return false;
252 }
253
254 *count = c - 1;
255
256 return true;
257 }
258
259 static bool
260 surface_set_gen6_buffer_SURFACE_STATE(struct ilo_state_surface *surf,
261 const struct ilo_dev *dev,
262 const struct ilo_state_surface_buffer_info *info)
263 {
264 uint32_t dw0, dw1, dw2, dw3;
265 uint32_t struct_count;
266 int width, height, depth;
267
268 ILO_DEV_ASSERT(dev, 6, 6);
269
270 if (!surface_validate_gen6_buffer(dev, info) ||
271 !surface_get_gen6_buffer_struct_count(dev, info, &struct_count))
272 return false;
273
274 /* bits [6:0] */
275 width = (struct_count & 0x0000007f);
276 /* bits [19:7] */
277 height = (struct_count & 0x000fff80) >> 7;
278 /* bits [26:20] */
279 depth = (struct_count & 0x07f00000) >> 20;
280
281 dw0 = GEN6_SURFTYPE_BUFFER << GEN6_SURFACE_DW0_TYPE__SHIFT |
282 info->format << GEN6_SURFACE_DW0_FORMAT__SHIFT;
283 dw1 = info->offset;
284 dw2 = height << GEN6_SURFACE_DW2_HEIGHT__SHIFT |
285 width << GEN6_SURFACE_DW2_WIDTH__SHIFT;
286 dw3 = depth << GEN6_SURFACE_DW3_DEPTH__SHIFT |
287 (info->struct_size - 1) << GEN6_SURFACE_DW3_PITCH__SHIFT;
288
289 STATIC_ASSERT(ARRAY_SIZE(surf->surface) >= 6);
290 surf->surface[0] = dw0;
291 surf->surface[1] = dw1;
292 surf->surface[2] = dw2;
293 surf->surface[3] = dw3;
294 surf->surface[4] = 0;
295 surf->surface[5] = 0;
296
297 surf->type = GEN6_SURFTYPE_BUFFER;
298 surf->min_lod = 0;
299 surf->mip_count = 0;
300
301 return true;
302 }
303
304 static bool
305 surface_set_gen7_buffer_SURFACE_STATE(struct ilo_state_surface *surf,
306 const struct ilo_dev *dev,
307 const struct ilo_state_surface_buffer_info *info)
308 {
309 uint32_t dw0, dw1, dw2, dw3, dw7;
310 enum gen_surface_type type;
311 uint32_t struct_count;
312 int width, height, depth;
313
314 ILO_DEV_ASSERT(dev, 7, 8);
315
316 if (!surface_validate_gen6_buffer(dev, info) ||
317 !surface_get_gen6_buffer_struct_count(dev, info, &struct_count))
318 return false;
319
320 type = (info->format == GEN6_FORMAT_RAW && info->struct_size > 1) ?
321 GEN7_SURFTYPE_STRBUF : GEN6_SURFTYPE_BUFFER;
322
323 /* bits [6:0] */
324 width = (struct_count & 0x0000007f);
325 /* bits [20:7] */
326 height = (struct_count & 0x001fff80) >> 7;
327 /* bits [30:21] */
328 depth = (struct_count & 0x7fe00000) >> 21;
329
330 dw0 = type << GEN7_SURFACE_DW0_TYPE__SHIFT |
331 info->format << GEN7_SURFACE_DW0_FORMAT__SHIFT;
332 dw1 = (ilo_dev_gen(dev) >= ILO_GEN(8)) ? 0 : info->offset;
333 dw2 = GEN_SHIFT32(height, GEN7_SURFACE_DW2_HEIGHT) |
334 GEN_SHIFT32(width, GEN7_SURFACE_DW2_WIDTH);
335 dw3 = GEN_SHIFT32(depth, GEN7_SURFACE_DW3_DEPTH) |
336 GEN_SHIFT32(info->struct_size - 1, GEN7_SURFACE_DW3_PITCH);
337
338 dw7 = 0;
339 if (ilo_dev_gen(dev) >= ILO_GEN(7.5)) {
340 dw7 |= GEN_SHIFT32(GEN75_SCS_RED, GEN75_SURFACE_DW7_SCS_R) |
341 GEN_SHIFT32(GEN75_SCS_GREEN, GEN75_SURFACE_DW7_SCS_G) |
342 GEN_SHIFT32(GEN75_SCS_BLUE, GEN75_SURFACE_DW7_SCS_B) |
343 GEN_SHIFT32(GEN75_SCS_ALPHA, GEN75_SURFACE_DW7_SCS_A);
344 }
345
346 STATIC_ASSERT(ARRAY_SIZE(surf->surface) >= 13);
347 surf->surface[0] = dw0;
348 surf->surface[1] = dw1;
349 surf->surface[2] = dw2;
350 surf->surface[3] = dw3;
351 surf->surface[4] = 0;
352 surf->surface[5] = 0;
353 surf->surface[6] = 0;
354 surf->surface[7] = dw7;
355 if (ilo_dev_gen(dev) >= ILO_GEN(8)) {
356 surf->surface[8] = info->offset;
357 surf->surface[9] = 0;
358 surf->surface[10] = 0;
359 surf->surface[11] = 0;
360 surf->surface[12] = 0;
361 }
362
363 surf->type = type;
364 surf->min_lod = 0;
365 surf->mip_count = 0;
366
367 return true;
368 }
369
370 static enum gen_surface_type
371 get_gen6_surface_type(const struct ilo_dev *dev, const struct ilo_image *img)
372 {
373 ILO_DEV_ASSERT(dev, 6, 8);
374
375 switch (img->target) {
376 case PIPE_TEXTURE_1D:
377 case PIPE_TEXTURE_1D_ARRAY:
378 return GEN6_SURFTYPE_1D;
379 case PIPE_TEXTURE_2D:
380 case PIPE_TEXTURE_CUBE:
381 case PIPE_TEXTURE_RECT:
382 case PIPE_TEXTURE_2D_ARRAY:
383 case PIPE_TEXTURE_CUBE_ARRAY:
384 return GEN6_SURFTYPE_2D;
385 case PIPE_TEXTURE_3D:
386 return GEN6_SURFTYPE_3D;
387 default:
388 assert(!"unknown texture target");
389 return GEN6_SURFTYPE_NULL;
390 }
391 }
392
393 static bool
394 surface_validate_gen6_image(const struct ilo_dev *dev,
395 const struct ilo_state_surface_image_info *info)
396 {
397 ILO_DEV_ASSERT(dev, 6, 8);
398
399 switch (info->access) {
400 case ILO_STATE_SURFACE_ACCESS_SAMPLER:
401 case ILO_STATE_SURFACE_ACCESS_DP_RENDER:
402 break;
403 case ILO_STATE_SURFACE_ACCESS_DP_TYPED:
404 assert(ilo_dev_gen(dev) >= ILO_GEN(7));
405 break;
406 default:
407 assert(!"unsupported surface access");
408 break;
409 }
410
411 /*
412 * From the Sandy Bridge PRM, volume 4 part 1, page 78:
413 *
414 * "For surface types other than SURFTYPE_BUFFER, the Width specified
415 * by this field must be less than or equal to the surface pitch
416 * (specified in bytes via the Surface Pitch field)."
417 */
418 assert(info->img->bo_stride && info->img->bo_stride <= 512 * 1024 &&
419 info->img->width0 <= info->img->bo_stride);
420
421 if (info->is_cube_map) {
422 assert(get_gen6_surface_type(dev, info->img) == GEN6_SURFTYPE_2D);
423
424 /*
425 * From the Sandy Bridge PRM, volume 4 part 1, page 78:
426 *
427 * "For cube maps, Width must be set equal to the Height."
428 */
429 assert(info->img->width0 == info->img->height0);
430 }
431
432 /*
433 * From the Sandy Bridge PRM, volume 4 part 1, page 72:
434 *
435 * "Tile Walk TILEWALK_YMAJOR is UNDEFINED for render target formats
436 * that have 128 bits-per-element (BPE)."
437 *
438 * "If Number of Multisamples is set to a value other than
439 * MULTISAMPLECOUNT_1, this field cannot be set to the following
440 * formats:
441 *
442 * - any format with greater than 64 bits per element
443 * - any compressed texture format (BC*)
444 * - any YCRCB* format"
445 *
446 * From the Ivy Bridge PRM, volume 4 part 1, page 63:
447 *
448 * If Number of Multisamples is set to a value other than
449 * MULTISAMPLECOUNT_1, this field cannot be set to the following
450 * formats: any format with greater than 64 bits per element, if
451 * Number of Multisamples is MULTISAMPLECOUNT_8, any compressed
452 * texture format (BC*), and any YCRCB* format.
453 *
454 * TODO
455 */
456
457 if (ilo_dev_gen(dev) < ILO_GEN(8) && info->img->tiling == GEN8_TILING_W) {
458 ilo_warn("tiling W is not supported\n");
459 return false;
460 }
461
462 return true;
463 }
464
465 static void
466 get_gen6_max_extent(const struct ilo_dev *dev,
467 const struct ilo_image *img,
468 uint16_t *max_w, uint16_t *max_h)
469 {
470 const uint16_t max_size = (ilo_dev_gen(dev) >= ILO_GEN(7)) ? 16384 : 8192;
471
472 ILO_DEV_ASSERT(dev, 6, 8);
473
474 switch (get_gen6_surface_type(dev, img)) {
475 case GEN6_SURFTYPE_1D:
476 *max_w = max_size;
477 *max_h = 1;
478 break;
479 case GEN6_SURFTYPE_2D:
480 *max_w = max_size;
481 *max_h = max_size;
482 break;
483 case GEN6_SURFTYPE_3D:
484 *max_w = 2048;
485 *max_h = 2048;
486 break;
487 default:
488 assert(!"invalid surface type");
489 *max_w = 1;
490 *max_h = 1;
491 break;
492 }
493 }
494
495 static bool
496 surface_get_gen6_image_extent(const struct ilo_dev *dev,
497 const struct ilo_state_surface_image_info *info,
498 uint16_t *width, uint16_t *height)
499 {
500 uint16_t w, h, max_w, max_h;
501
502 ILO_DEV_ASSERT(dev, 6, 8);
503
504 w = info->img->width0;
505 h = info->img->height0;
506
507 get_gen6_max_extent(dev, info->img, &max_w, &max_h);
508 assert(w && h && w <= max_w && h <= max_h);
509
510 *width = w - 1;
511 *height = h - 1;
512
513 return true;
514 }
515
516 static bool
517 surface_get_gen6_image_slices(const struct ilo_dev *dev,
518 const struct ilo_state_surface_image_info *info,
519 uint16_t *depth, uint16_t *min_array_elem,
520 uint16_t *rt_view_extent)
521 {
522 uint16_t max_slice, d;
523
524 ILO_DEV_ASSERT(dev, 6, 8);
525
526 /*
527 * From the Ivy Bridge PRM, volume 4 part 1, page 63:
528 *
529 * "If this field (Surface Array) is enabled, the Surface Type must be
530 * SURFTYPE_1D, SURFTYPE_2D, or SURFTYPE_CUBE. If this field is
531 * disabled and Surface Type is SURFTYPE_1D, SURFTYPE_2D, or
532 * SURFTYPE_CUBE, the Depth field must be set to zero."
533 *
534 * From the Ivy Bridge PRM, volume 4 part 1, page 69:
535 *
536 * "This field (Depth) specifies the total number of levels for a
537 * volume texture or the number of array elements allowed to be
538 * accessed starting at the Minimum Array Element for arrayed
539 * surfaces. If the volume texture is MIP-mapped, this field
540 * specifies the depth of the base MIP level."
541 *
542 * "For SURFTYPE_CUBE:For Sampling Engine Surfaces, the range of this
543 * field is [0,340], indicating the number of cube array elements
544 * (equal to the number of underlying 2D array elements divided by 6).
545 * For other surfaces, this field must be zero."
546 *
547 * "Errata: For SURFTYPE_CUBE sampling engine surfaces, the range of
548 * this field is limited to [0,85].
549 *
550 * Errata: If Surface Array is enabled, and Depth is between 1024 and
551 * 2047, an incorrect array slice may be accessed if the requested
552 * array index in the message is greater than or equal to 4096."
553 *
554 * The errata are for Gen7-specific, and they limit the number of useable
555 * layers to (86 * 6), about 512.
556 */
557
558 switch (get_gen6_surface_type(dev, info->img)) {
559 case GEN6_SURFTYPE_1D:
560 case GEN6_SURFTYPE_2D:
561 max_slice = (ilo_dev_gen(dev) >= ILO_GEN(7.5)) ? 2048 : 512;
562
563 assert(info->img->array_size <= max_slice);
564 max_slice = info->img->array_size;
565
566 d = info->slice_count;
567 if (info->is_cube_map) {
568 if (info->access == ILO_STATE_SURFACE_ACCESS_SAMPLER) {
569 if (!d || d % 6) {
570 ilo_warn("invalid cube slice count\n");
571 return false;
572 }
573
574 if (ilo_dev_gen(dev) == ILO_GEN(7) && d > 86 * 6) {
575 ilo_warn("cube slice count exceeds Gen7 limit\n");
576 return false;
577 }
578 } else {
579 /*
580 * Minumum Array Element and Depth must be 0; Render Target View
581 * Extent is ignored.
582 */
583 if (info->slice_base || d != 6) {
584 ilo_warn("no cube RT array support in data port\n");
585 return false;
586 }
587 }
588
589 d /= 6;
590 }
591
592 if (!info->is_array && d > 1) {
593 ilo_warn("non-array surface with non-zero depth\n");
594 return false;
595 }
596 break;
597 case GEN6_SURFTYPE_3D:
598 max_slice = 2048;
599
600 assert(info->img->depth0 <= max_slice);
601 max_slice = u_minify(info->img->depth0, info->level_base);
602
603 d = info->img->depth0;
604
605 if (info->is_array) {
606 ilo_warn("3D surfaces cannot be arrays\n");
607 return false;
608 }
609 break;
610 default:
611 assert(!"invalid surface type");
612 return false;
613 break;
614 }
615
616 if (!info->slice_count ||
617 info->slice_base + info->slice_count > max_slice) {
618 ilo_warn("invalid slice range\n");
619 return false;
620 }
621
622 assert(d);
623 *depth = d - 1;
624
625 /*
626 * From the Sandy Bridge PRM, volume 4 part 1, page 84:
627 *
628 * "For Sampling Engine and Render Target 1D and 2D Surfaces:
629 * This field (Minimum Array Element) indicates the minimum array
630 * element that can be accessed as part of this surface. This field
631 * is added to the delivered array index before it is used to address
632 * the surface.
633 *
634 * For Render Target 3D Surfaces:
635 * This field indicates the minimum `R' coordinate on the LOD
636 * currently being rendered to. This field is added to the delivered
637 * array index before it is used to address the surface.
638 *
639 * For Sampling Engine Cube Surfaces on [DevSNB+] only:
640 * This field indicates the minimum array element in the underlying 2D
641 * surface array that can be accessed as part of this surface (the
642 * cube array index is multipled by 6 to compute this value, although
643 * this field is not restricted to only multiples of 6). This field is
644 * added to the delivered array index before it is used to address the
645 * surface.
646 *
647 * For Other Surfaces:
648 * This field must be set to zero."
649 *
650 * On Gen7+, typed sufaces are treated like sampling engine 1D and 2D
651 * surfaces.
652 */
653 *min_array_elem = info->slice_base;
654
655 /*
656 * From the Sandy Bridge PRM, volume 4 part 1, page 84:
657 *
658 * "For Render Target 3D Surfaces:
659 * This field (Render Target View Extent) indicates the extent of the
660 * accessible `R' coordinates minus 1 on the LOD currently being
661 * rendered to.
662 *
663 * For Render Target 1D and 2D Surfaces:
664 * This field must be set to the same value as the Depth field.
665 *
666 * For Other Surfaces:
667 * This field is ignored."
668 */
669 *rt_view_extent = info->slice_count - 1;
670
671 return true;
672 }
673
674 static bool
675 surface_get_gen6_image_levels(const struct ilo_dev *dev,
676 const struct ilo_state_surface_image_info *info,
677 uint8_t *min_lod, uint8_t *mip_count)
678 {
679 uint8_t max_level = (ilo_dev_gen(dev) >= ILO_GEN(7)) ? 15 : 14;
680
681 ILO_DEV_ASSERT(dev, 6, 8);
682
683 assert(info->img->level_count <= max_level);
684 max_level = info->img->level_count;
685
686 if (!info->level_count ||
687 info->level_base + info->level_count > max_level) {
688 ilo_warn("invalid level range\n");
689 return false;
690 }
691
692 /*
693 * From the Sandy Bridge PRM, volume 4 part 1, page 79:
694 *
695 * "For Sampling Engine Surfaces:
696 * This field (MIP Count / LOD) indicates the number of MIP levels
697 * allowed to be accessed starting at Surface Min LOD, which must be
698 * less than or equal to the number of MIP levels actually stored in
699 * memory for this surface.
700 *
701 * Force the mip map access to be between the mipmap specified by the
702 * integer bits of the Min LOD and the ceiling of the value specified
703 * here.
704 *
705 * For Render Target Surfaces:
706 * This field defines the MIP level that is currently being rendered
707 * into. This is the absolute MIP level on the surface and is not
708 * relative to the Surface Min LOD field, which is ignored for render
709 * target surfaces.
710 *
711 * For Other Surfaces:
712 * This field is reserved : MBZ"
713 *
714 * From the Sandy Bridge PRM, volume 4 part 1, page 83:
715 *
716 * "For Sampling Engine Surfaces:
717 *
718 * This field (Surface Min LOD) indicates the most detailed LOD that
719 * can be accessed as part of this surface. This field is added to
720 * the delivered LOD (sample_l, ld, or resinfo message types) before
721 * it is used to address the surface.
722 *
723 * For Other Surfaces:
724 * This field is ignored."
725 *
726 * On Gen7+, typed sufaces are treated like sampling engine surfaces.
727 */
728 if (info->access == ILO_STATE_SURFACE_ACCESS_DP_RENDER) {
729 assert(info->level_count == 1);
730
731 *min_lod = 0;
732 *mip_count = info->level_base;
733 } else {
734 *min_lod = info->level_base;
735 *mip_count = info->level_count - 1;
736 }
737
738 return true;
739 }
740
741 static bool
742 surface_get_gen6_image_sample_count(const struct ilo_dev *dev,
743 const struct ilo_state_surface_image_info *info,
744 enum gen_sample_count *sample_count)
745 {
746 int min_gen;
747
748 ILO_DEV_ASSERT(dev, 6, 8);
749
750 switch (info->img->sample_count) {
751 case 1:
752 *sample_count = GEN6_NUMSAMPLES_1;
753 min_gen = ILO_GEN(6);
754 break;
755 case 2:
756 *sample_count = GEN8_NUMSAMPLES_2;
757 min_gen = ILO_GEN(8);
758 break;
759 case 4:
760 *sample_count = GEN6_NUMSAMPLES_4;
761 min_gen = ILO_GEN(6);
762 break;
763 case 8:
764 *sample_count = GEN7_NUMSAMPLES_8;
765 min_gen = ILO_GEN(7);
766 break;
767 case 16:
768 *sample_count = GEN8_NUMSAMPLES_16;
769 min_gen = ILO_GEN(8);
770 break;
771 default:
772 assert(!"invalid sample count");
773 *sample_count = GEN6_NUMSAMPLES_1;
774 break;
775 }
776
777 assert(ilo_dev_gen(dev) >= min_gen);
778
779 return true;
780 }
781
782 static bool
783 surface_get_gen6_image_alignments(const struct ilo_dev *dev,
784 const struct ilo_state_surface_image_info *info,
785 uint32_t *alignments)
786 {
787 uint32_t a = 0;
788 bool err = false;
789
790 ILO_DEV_ASSERT(dev, 6, 8);
791
792 if (ilo_dev_gen(dev) >= ILO_GEN(8)) {
793 switch (info->img->align_i) {
794 case 4:
795 a |= GEN8_SURFACE_DW0_HALIGN_4;
796 break;
797 case 8:
798 a |= GEN8_SURFACE_DW0_HALIGN_8;
799 break;
800 case 16:
801 a |= GEN8_SURFACE_DW0_HALIGN_16;
802 break;
803 default:
804 err = true;
805 break;
806 }
807
808 switch (info->img->align_j) {
809 case 4:
810 a |= GEN7_SURFACE_DW0_VALIGN_4;
811 break;
812 case 8:
813 a |= GEN8_SURFACE_DW0_VALIGN_8;
814 break;
815 case 16:
816 a |= GEN8_SURFACE_DW0_VALIGN_16;
817 break;
818 default:
819 err = true;
820 break;
821 }
822 } else if (ilo_dev_gen(dev) >= ILO_GEN(7)) {
823 switch (info->img->align_i) {
824 case 4:
825 a |= GEN7_SURFACE_DW0_HALIGN_4;
826 break;
827 case 8:
828 a |= GEN7_SURFACE_DW0_HALIGN_8;
829 break;
830 default:
831 err = true;
832 break;
833 }
834
835 switch (info->img->align_j) {
836 case 2:
837 a |= GEN7_SURFACE_DW0_VALIGN_2;
838 break;
839 case 4:
840 a |= GEN7_SURFACE_DW0_VALIGN_4;
841 break;
842 default:
843 err = true;
844 break;
845 }
846 } else {
847 if (info->img->align_i != 4)
848 err = true;
849
850 switch (info->img->align_j) {
851 case 2:
852 a |= GEN6_SURFACE_DW5_VALIGN_2;
853 break;
854 case 4:
855 a |= GEN6_SURFACE_DW5_VALIGN_4;
856 break;
857 default:
858 err = true;
859 break;
860 }
861 }
862
863 if (err)
864 assert(!"invalid HALIGN or VALIGN");
865
866 *alignments = a;
867
868 return true;
869 }
870
871 static bool
872 surface_set_gen6_image_SURFACE_STATE(struct ilo_state_surface *surf,
873 const struct ilo_dev *dev,
874 const struct ilo_state_surface_image_info *info)
875 {
876 uint16_t width, height, depth, array_base, view_extent;
877 uint8_t min_lod, mip_count;
878 enum gen_sample_count sample_count;
879 uint32_t alignments;
880 enum gen_surface_type type;
881 uint32_t dw0, dw2, dw3, dw4, dw5;
882
883 ILO_DEV_ASSERT(dev, 6, 6);
884
885 if (!surface_validate_gen6_image(dev, info) ||
886 !surface_get_gen6_image_extent(dev, info, &width, &height) ||
887 !surface_get_gen6_image_slices(dev, info, &depth, &array_base,
888 &view_extent) ||
889 !surface_get_gen6_image_levels(dev, info, &min_lod, &mip_count) ||
890 !surface_get_gen6_image_sample_count(dev, info, &sample_count) ||
891 !surface_get_gen6_image_alignments(dev, info, &alignments))
892 return false;
893
894 /* no ARYSPC_LOD0 */
895 assert(info->img->walk != ILO_IMAGE_WALK_LOD);
896 /* no UMS/CMS */
897 if (info->img->sample_count > 1)
898 assert(info->img->interleaved_samples);
899
900 type = (info->is_cube_map) ? GEN6_SURFTYPE_CUBE :
901 get_gen6_surface_type(dev, info->img);
902
903 dw0 = type << GEN6_SURFACE_DW0_TYPE__SHIFT |
904 info->format << GEN6_SURFACE_DW0_FORMAT__SHIFT |
905 GEN6_SURFACE_DW0_MIPLAYOUT_BELOW;
906
907 /*
908 * From the Sandy Bridge PRM, volume 4 part 1, page 74:
909 *
910 * "CUBE_AVERAGE may only be selected if all of the Cube Face Enable
911 * fields are equal to one."
912 *
913 * From the Sandy Bridge PRM, volume 4 part 1, page 75-76:
914 *
915 * "For SURFTYPE_CUBE Surfaces accessed via the Sampling Engine:
916 * Bits 5:0 of this field (Cube Face Enables) enable the individual
917 * faces of a cube map. Enabling a face indicates that the face is
918 * present in the cube map, while disabling it indicates that that
919 * face is represented by the texture map's border color. Refer to
920 * Memory Data Formats for the correlation between faces and the cube
921 * map memory layout. Note that storage for disabled faces must be
922 * provided.
923 *
924 * For other surfaces:
925 * This field is reserved : MBZ"
926 *
927 * "When TEXCOORDMODE_CLAMP is used when accessing a cube map, this
928 * field must be programmed to 111111b (all faces enabled)."
929 */
930 if (info->is_cube_map &&
931 info->access == ILO_STATE_SURFACE_ACCESS_SAMPLER) {
932 dw0 |= GEN6_SURFACE_DW0_CUBE_MAP_CORNER_MODE_AVERAGE |
933 GEN6_SURFACE_DW0_CUBE_FACE_ENABLES__MASK;
934 }
935
936 dw2 = height << GEN6_SURFACE_DW2_HEIGHT__SHIFT |
937 width << GEN6_SURFACE_DW2_WIDTH__SHIFT |
938 mip_count << GEN6_SURFACE_DW2_MIP_COUNT_LOD__SHIFT;
939
940 dw3 = depth << GEN6_SURFACE_DW3_DEPTH__SHIFT |
941 (info->img->bo_stride - 1) << GEN6_SURFACE_DW3_PITCH__SHIFT |
942 info->img->tiling << GEN6_SURFACE_DW3_TILING__SHIFT;
943
944 dw4 = min_lod << GEN6_SURFACE_DW4_MIN_LOD__SHIFT |
945 array_base << GEN6_SURFACE_DW4_MIN_ARRAY_ELEMENT__SHIFT |
946 view_extent << GEN6_SURFACE_DW4_RT_VIEW_EXTENT__SHIFT |
947 sample_count << GEN6_SURFACE_DW4_MULTISAMPLECOUNT__SHIFT;
948
949 dw5 = alignments;
950
951 STATIC_ASSERT(ARRAY_SIZE(surf->surface) >= 6);
952 surf->surface[0] = dw0;
953 surf->surface[1] = 0;
954 surf->surface[2] = dw2;
955 surf->surface[3] = dw3;
956 surf->surface[4] = dw4;
957 surf->surface[5] = dw5;
958
959 surf->type = type;
960 surf->min_lod = min_lod;
961 surf->mip_count = mip_count;
962
963 return true;
964 }
965
966 static bool
967 surface_set_gen7_image_SURFACE_STATE(struct ilo_state_surface *surf,
968 const struct ilo_dev *dev,
969 const struct ilo_state_surface_image_info *info)
970 {
971 uint16_t width, height, depth, array_base, view_extent;
972 uint8_t min_lod, mip_count;
973 uint32_t alignments;
974 enum gen_sample_count sample_count;
975 enum gen_surface_type type;
976 uint32_t dw0, dw1, dw2, dw3, dw4, dw5, dw7;
977
978 ILO_DEV_ASSERT(dev, 7, 8);
979
980 if (!surface_validate_gen6_image(dev, info) ||
981 !surface_get_gen6_image_extent(dev, info, &width, &height) ||
982 !surface_get_gen6_image_slices(dev, info, &depth, &array_base,
983 &view_extent) ||
984 !surface_get_gen6_image_levels(dev, info, &min_lod, &mip_count) ||
985 !surface_get_gen6_image_sample_count(dev, info, &sample_count) ||
986 !surface_get_gen6_image_alignments(dev, info, &alignments))
987 return false;
988
989 type = (info->is_cube_map) ? GEN6_SURFTYPE_CUBE :
990 get_gen6_surface_type(dev, info->img);
991
992 dw0 = type << GEN7_SURFACE_DW0_TYPE__SHIFT |
993 info->format << GEN7_SURFACE_DW0_FORMAT__SHIFT |
994 alignments;
995
996 if (info->is_array)
997 dw0 |= GEN7_SURFACE_DW0_IS_ARRAY;
998
999 if (ilo_dev_gen(dev) >= ILO_GEN(8)) {
1000 dw0 |= info->img->tiling << GEN8_SURFACE_DW0_TILING__SHIFT;
1001 } else {
1002 dw0 |= info->img->tiling << GEN7_SURFACE_DW0_TILING__SHIFT;
1003
1004 if (info->img->walk == ILO_IMAGE_WALK_LOD)
1005 dw0 |= GEN7_SURFACE_DW0_ARYSPC_LOD0;
1006 else
1007 dw0 |= GEN7_SURFACE_DW0_ARYSPC_FULL;
1008 }
1009
1010 /*
1011 * From the Ivy Bridge PRM, volume 4 part 1, page 67:
1012 *
1013 * "For SURFTYPE_CUBE Surfaces accessed via the Sampling Engine: Bits
1014 * 5:0 of this field (Cube Face Enables) enable the individual faces
1015 * of a cube map. Enabling a face indicates that the face is present
1016 * in the cube map, while disabling it indicates that that face is
1017 * represented by the texture map's border color. Refer to Memory Data
1018 * Formats for the correlation between faces and the cube map memory
1019 * layout. Note that storage for disabled faces must be provided. For
1020 * other surfaces this field is reserved and MBZ."
1021 *
1022 * "When TEXCOORDMODE_CLAMP is used when accessing a cube map, this
1023 * field must be programmed to 111111b (all faces enabled). This field
1024 * is ignored unless the Surface Type is SURFTYPE_CUBE."
1025 */
1026 if (info->is_cube_map &&
1027 info->access == ILO_STATE_SURFACE_ACCESS_SAMPLER)
1028 dw0 |= GEN7_SURFACE_DW0_CUBE_FACE_ENABLES__MASK;
1029
1030 dw1 = 0;
1031 if (ilo_dev_gen(dev) >= ILO_GEN(8)) {
1032 assert(info->img->walk_layer_height % 4 == 0);
1033 dw1 |= info->img->walk_layer_height / 4 <<
1034 GEN8_SURFACE_DW1_QPITCH__SHIFT;
1035 }
1036
1037 dw2 = height << GEN7_SURFACE_DW2_HEIGHT__SHIFT |
1038 width << GEN7_SURFACE_DW2_WIDTH__SHIFT;
1039
1040 dw3 = depth << GEN7_SURFACE_DW3_DEPTH__SHIFT |
1041 (info->img->bo_stride - 1) << GEN7_SURFACE_DW3_PITCH__SHIFT;
1042
1043 if (ilo_dev_gen(dev) == ILO_GEN(7.5))
1044 dw3 |= 0 << GEN75_SURFACE_DW3_INTEGER_SURFACE_FORMAT__SHIFT;
1045
1046 dw4 = array_base << GEN7_SURFACE_DW4_MIN_ARRAY_ELEMENT__SHIFT |
1047 view_extent << GEN7_SURFACE_DW4_RT_VIEW_EXTENT__SHIFT |
1048 sample_count << GEN7_SURFACE_DW4_MULTISAMPLECOUNT__SHIFT;
1049
1050 /*
1051 * MSFMT_MSS means the samples are not interleaved and MSFMT_DEPTH_STENCIL
1052 * means the samples are interleaved. The layouts are the same when the
1053 * number of samples is 1.
1054 */
1055 if (info->img->interleaved_samples && info->img->sample_count > 1) {
1056 assert(info->access != ILO_STATE_SURFACE_ACCESS_DP_RENDER);
1057 dw4 |= GEN7_SURFACE_DW4_MSFMT_DEPTH_STENCIL;
1058 } else {
1059 dw4 |= GEN7_SURFACE_DW4_MSFMT_MSS;
1060 }
1061
1062 dw5 = min_lod << GEN7_SURFACE_DW5_MIN_LOD__SHIFT |
1063 mip_count << GEN7_SURFACE_DW5_MIP_COUNT_LOD__SHIFT;
1064
1065 dw7 = 0;
1066 if (ilo_dev_gen(dev) >= ILO_GEN(7.5)) {
1067 dw7 |= GEN_SHIFT32(GEN75_SCS_RED, GEN75_SURFACE_DW7_SCS_R) |
1068 GEN_SHIFT32(GEN75_SCS_GREEN, GEN75_SURFACE_DW7_SCS_G) |
1069 GEN_SHIFT32(GEN75_SCS_BLUE, GEN75_SURFACE_DW7_SCS_B) |
1070 GEN_SHIFT32(GEN75_SCS_ALPHA, GEN75_SURFACE_DW7_SCS_A);
1071 }
1072
1073 STATIC_ASSERT(ARRAY_SIZE(surf->surface) >= 13);
1074 surf->surface[0] = dw0;
1075 surf->surface[1] = dw1;
1076 surf->surface[2] = dw2;
1077 surf->surface[3] = dw3;
1078 surf->surface[4] = dw4;
1079 surf->surface[5] = dw5;
1080 surf->surface[6] = 0;
1081 surf->surface[7] = dw7;
1082 if (ilo_dev_gen(dev) >= ILO_GEN(8)) {
1083 surf->surface[8] = 0;
1084 surf->surface[9] = 0;
1085 surf->surface[10] = 0;
1086 surf->surface[11] = 0;
1087 surf->surface[12] = 0;
1088 }
1089
1090 surf->type = type;
1091 surf->min_lod = min_lod;
1092 surf->mip_count = mip_count;
1093
1094 return true;
1095 }
1096
1097 bool
1098 ilo_state_surface_init_for_null(struct ilo_state_surface *surf,
1099 const struct ilo_dev *dev)
1100 {
1101 bool ret = true;
1102
1103 assert(ilo_is_zeroed(surf, sizeof(*surf)));
1104
1105 if (ilo_dev_gen(dev) >= ILO_GEN(7))
1106 ret &= surface_set_gen7_null_SURFACE_STATE(surf, dev);
1107 else
1108 ret &= surface_set_gen6_null_SURFACE_STATE(surf, dev);
1109
1110 surf->type = GEN6_SURFTYPE_NULL;
1111 surf->readonly = true;
1112
1113 assert(ret);
1114
1115 return ret;
1116 }
1117
1118 bool
1119 ilo_state_surface_init_for_buffer(struct ilo_state_surface *surf,
1120 const struct ilo_dev *dev,
1121 const struct ilo_state_surface_buffer_info *info)
1122 {
1123 bool ret = true;
1124
1125 assert(ilo_is_zeroed(surf, sizeof(*surf)));
1126
1127 if (ilo_dev_gen(dev) >= ILO_GEN(7))
1128 ret &= surface_set_gen7_buffer_SURFACE_STATE(surf, dev, info);
1129 else
1130 ret &= surface_set_gen6_buffer_SURFACE_STATE(surf, dev, info);
1131
1132 surf->readonly = info->readonly;
1133
1134 assert(ret);
1135
1136 return ret;
1137 }
1138
1139 bool
1140 ilo_state_surface_init_for_image(struct ilo_state_surface *surf,
1141 const struct ilo_dev *dev,
1142 const struct ilo_state_surface_image_info *info)
1143 {
1144 bool ret = true;
1145
1146 assert(ilo_is_zeroed(surf, sizeof(*surf)));
1147
1148 if (ilo_dev_gen(dev) >= ILO_GEN(7))
1149 ret &= surface_set_gen7_image_SURFACE_STATE(surf, dev, info);
1150 else
1151 ret &= surface_set_gen6_image_SURFACE_STATE(surf, dev, info);
1152
1153 surf->is_integer = info->is_integer;
1154 surf->readonly = info->readonly;
1155 surf->scanout = info->img->scanout;
1156
1157 assert(ret);
1158
1159 return ret;
1160 }
1161
1162 bool
1163 ilo_state_surface_set_scs(struct ilo_state_surface *surf,
1164 const struct ilo_dev *dev,
1165 enum gen_surface_scs rgba[4])
1166 {
1167 const uint32_t scs = GEN_SHIFT32(rgba[0], GEN75_SURFACE_DW7_SCS_R) |
1168 GEN_SHIFT32(rgba[1], GEN75_SURFACE_DW7_SCS_G) |
1169 GEN_SHIFT32(rgba[2], GEN75_SURFACE_DW7_SCS_B) |
1170 GEN_SHIFT32(rgba[3], GEN75_SURFACE_DW7_SCS_A);
1171
1172 ILO_DEV_ASSERT(dev, 6, 8);
1173
1174 assert(ilo_dev_gen(dev) >= ILO_GEN(7.5));
1175
1176 surf->surface[7] = (surf->surface[7] & ~GEN75_SURFACE_DW7_SCS__MASK) | scs;
1177
1178 return true;
1179 }