2 * Mesa 3-D graphics library
4 * Copyright (C) 2012-2013 LunarG, Inc.
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:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
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.
25 * Chia-I Wu <olv@lunarg.com>
28 #include "ilo_screen.h"
29 #include "ilo_resource.h"
32 * From the Ivy Bridge PRM, volume 1 part 1, page 105:
34 * "In addition to restrictions on maximum height, width, and depth,
35 * surfaces are also restricted to a maximum size in bytes. This
36 * maximum is 2 GB for all products and all surface types."
38 static const size_t ilo_max_resource_size
= 1u << 31;
41 resource_get_bo_name(const struct pipe_resource
*templ
)
43 static const char *target_names
[PIPE_MAX_TEXTURE_TYPES
] = {
44 [PIPE_BUFFER
] = "buf",
45 [PIPE_TEXTURE_1D
] = "tex-1d",
46 [PIPE_TEXTURE_2D
] = "tex-2d",
47 [PIPE_TEXTURE_3D
] = "tex-3d",
48 [PIPE_TEXTURE_CUBE
] = "tex-cube",
49 [PIPE_TEXTURE_RECT
] = "tex-rect",
50 [PIPE_TEXTURE_1D_ARRAY
] = "tex-1d-array",
51 [PIPE_TEXTURE_2D_ARRAY
] = "tex-2d-array",
52 [PIPE_TEXTURE_CUBE_ARRAY
] = "tex-cube-array",
54 const char *name
= target_names
[templ
->target
];
56 if (templ
->target
== PIPE_BUFFER
) {
57 switch (templ
->bind
) {
58 case PIPE_BIND_VERTEX_BUFFER
:
61 case PIPE_BIND_INDEX_BUFFER
:
64 case PIPE_BIND_CONSTANT_BUFFER
:
67 case PIPE_BIND_STREAM_OUTPUT
:
79 resource_get_cpu_init(const struct pipe_resource
*templ
)
81 return (templ
->bind
& (PIPE_BIND_DEPTH_STENCIL
|
82 PIPE_BIND_RENDER_TARGET
|
83 PIPE_BIND_STREAM_OUTPUT
)) ? false : true;
86 static enum gen_surface_tiling
87 winsys_to_surface_tiling(enum intel_tiling_mode tiling
)
90 case INTEL_TILING_NONE
:
91 return GEN6_TILING_NONE
;
97 assert(!"unknown tiling");
98 return GEN6_TILING_NONE
;
102 static inline enum intel_tiling_mode
103 surface_to_winsys_tiling(enum gen_surface_tiling tiling
)
106 case GEN6_TILING_NONE
:
107 return INTEL_TILING_NONE
;
109 return INTEL_TILING_X
;
111 return INTEL_TILING_Y
;
113 assert(!"unknown tiling");
114 return GEN6_TILING_NONE
;
119 tex_free_slices(struct ilo_texture
*tex
)
121 FREE(tex
->slices
[0]);
125 tex_alloc_slices(struct ilo_texture
*tex
)
127 const struct pipe_resource
*templ
= &tex
->base
;
128 struct ilo_texture_slice
*slices
;
131 /* sum the depths of all levels */
133 for (lv
= 0; lv
<= templ
->last_level
; lv
++)
134 depth
+= u_minify(templ
->depth0
, lv
);
137 * There are (depth * tex->base.array_size) slices in total. Either depth
138 * is one (non-3D) or templ->array_size is one (non-array), but it does
141 slices
= CALLOC(depth
* templ
->array_size
, sizeof(*slices
));
145 tex
->slices
[0] = slices
;
147 /* point to the respective positions in the buffer */
148 for (lv
= 1; lv
<= templ
->last_level
; lv
++) {
149 tex
->slices
[lv
] = tex
->slices
[lv
- 1] +
150 u_minify(templ
->depth0
, lv
- 1) * templ
->array_size
;
157 tex_import_handle(struct ilo_texture
*tex
,
158 const struct winsys_handle
*handle
)
160 struct ilo_screen
*is
= ilo_screen(tex
->base
.screen
);
161 const char *name
= resource_get_bo_name(&tex
->base
);
162 enum intel_tiling_mode tiling
;
166 bo
= intel_winsys_import_handle(is
->dev
.winsys
, name
, handle
,
167 tex
->image
.bo_height
, &tiling
, &pitch
);
171 if (!ilo_image_update_for_imported_bo(&tex
->image
,
172 winsys_to_surface_tiling(tiling
), pitch
)) {
173 ilo_err("imported handle has incompatible tiling/pitch\n");
178 ilo_image_set_bo(&tex
->image
, bo
);
185 tex_create_bo(struct ilo_texture
*tex
)
187 struct ilo_screen
*is
= ilo_screen(tex
->base
.screen
);
188 const char *name
= resource_get_bo_name(&tex
->base
);
189 const bool cpu_init
= resource_get_cpu_init(&tex
->base
);
192 bo
= intel_winsys_alloc_bo(is
->dev
.winsys
, name
,
193 tex
->image
.bo_stride
* tex
->image
.bo_height
, cpu_init
);
195 /* set the tiling for transfer and export */
196 if (bo
&& (tex
->image
.tiling
== GEN6_TILING_X
||
197 tex
->image
.tiling
== GEN6_TILING_Y
)) {
198 const enum intel_tiling_mode tiling
=
199 surface_to_winsys_tiling(tex
->image
.tiling
);
201 if (intel_bo_set_tiling(bo
, tiling
, tex
->image
.bo_stride
)) {
209 ilo_image_set_bo(&tex
->image
, bo
);
216 tex_create_separate_stencil(struct ilo_texture
*tex
)
218 struct pipe_resource templ
= tex
->base
;
219 struct pipe_resource
*s8
;
222 * Unless PIPE_BIND_DEPTH_STENCIL is set, the resource may have other
223 * tilings. But that should be fine since it will never be bound as the
224 * stencil buffer, and our transfer code can handle all tilings.
226 templ
.format
= PIPE_FORMAT_S8_UINT
;
228 /* no stencil texturing */
229 templ
.bind
&= ~PIPE_BIND_SAMPLER_VIEW
;
231 s8
= tex
->base
.screen
->resource_create(tex
->base
.screen
, &templ
);
235 tex
->separate_s8
= ilo_texture(s8
);
237 assert(tex
->separate_s8
->image
.format
== PIPE_FORMAT_S8_UINT
);
243 tex_create_hiz(struct ilo_texture
*tex
)
245 const struct pipe_resource
*templ
= &tex
->base
;
246 struct ilo_screen
*is
= ilo_screen(tex
->base
.screen
);
250 bo
= intel_winsys_alloc_bo(is
->dev
.winsys
, "hiz texture",
251 tex
->image
.aux_stride
* tex
->image
.aux_height
, false);
255 ilo_image_set_aux_bo(&tex
->image
, bo
);
257 for (lv
= 0; lv
<= templ
->last_level
; lv
++) {
258 if (tex
->image
.aux_enables
& (1 << lv
)) {
259 const unsigned num_slices
= (templ
->target
== PIPE_TEXTURE_3D
) ?
260 u_minify(templ
->depth0
, lv
) : templ
->array_size
;
261 unsigned flags
= ILO_TEXTURE_HIZ
;
263 /* this will trigger a HiZ resolve */
265 flags
|= ILO_TEXTURE_CPU_WRITE
;
267 ilo_texture_set_slice_flags(tex
, lv
, 0, num_slices
, flags
, flags
);
275 tex_create_mcs(struct ilo_texture
*tex
)
277 struct ilo_screen
*is
= ilo_screen(tex
->base
.screen
);
280 assert(tex
->image
.aux_enables
== (1 << (tex
->base
.last_level
+ 1)) - 1);
282 bo
= intel_winsys_alloc_bo(is
->dev
.winsys
, "mcs texture",
283 tex
->image
.aux_stride
* tex
->image
.aux_height
, false);
287 ilo_image_set_aux_bo(&tex
->image
, bo
);
293 tex_destroy(struct ilo_texture
*tex
)
295 if (tex
->separate_s8
)
296 tex_destroy(tex
->separate_s8
);
298 ilo_image_cleanup(&tex
->image
);
300 tex_free_slices(tex
);
305 tex_alloc_bos(struct ilo_texture
*tex
,
306 const struct winsys_handle
*handle
)
308 struct ilo_screen
*is
= ilo_screen(tex
->base
.screen
);
311 if (!tex_import_handle(tex
, handle
))
314 if (!tex_create_bo(tex
))
318 /* allocate separate stencil resource */
319 if (tex
->image
.separate_stencil
&& !tex_create_separate_stencil(tex
))
322 switch (tex
->image
.aux
) {
323 case ILO_IMAGE_AUX_HIZ
:
324 if (!tex_create_hiz(tex
)) {
325 /* Separate Stencil Buffer requires HiZ to be enabled */
326 if (ilo_dev_gen(&is
->dev
) == ILO_GEN(6) &&
327 tex
->image
.separate_stencil
)
331 case ILO_IMAGE_AUX_MCS
:
332 if (!tex_create_mcs(tex
))
343 tex_init_image(struct ilo_texture
*tex
)
345 struct ilo_screen
*is
= ilo_screen(tex
->base
.screen
);
346 const struct pipe_resource
*templ
= &tex
->base
;
347 struct ilo_image
*img
= &tex
->image
;
349 ilo_image_init(img
, &is
->dev
, templ
);
351 if (img
->bo_height
> ilo_max_resource_size
/ img
->bo_stride
)
354 if (templ
->flags
& PIPE_RESOURCE_FLAG_MAP_PERSISTENT
) {
355 /* require on-the-fly tiling/untiling or format conversion */
356 if (img
->tiling
== GEN8_TILING_W
|| img
->separate_stencil
||
357 img
->format
!= templ
->format
)
361 if (!tex_alloc_slices(tex
))
367 static struct pipe_resource
*
368 tex_create(struct pipe_screen
*screen
,
369 const struct pipe_resource
*templ
,
370 const struct winsys_handle
*handle
)
372 struct ilo_texture
*tex
;
374 tex
= CALLOC_STRUCT(ilo_texture
);
379 tex
->base
.screen
= screen
;
380 pipe_reference_init(&tex
->base
.reference
, 1);
382 tex
->imported
= (handle
!= NULL
);
384 if (!tex_init_image(tex
)) {
389 if (!tex_alloc_bos(tex
, handle
)) {
398 tex_get_handle(struct ilo_texture
*tex
, struct winsys_handle
*handle
)
400 struct ilo_screen
*is
= ilo_screen(tex
->base
.screen
);
401 enum intel_tiling_mode tiling
;
404 /* must match what tex_create_bo() sets */
405 if (tex
->image
.tiling
== GEN8_TILING_W
)
406 tiling
= INTEL_TILING_NONE
;
408 tiling
= surface_to_winsys_tiling(tex
->image
.tiling
);
410 err
= intel_winsys_export_handle(is
->dev
.winsys
, tex
->image
.bo
, tiling
,
411 tex
->image
.bo_stride
, tex
->image
.bo_height
, handle
);
417 buf_create_bo(struct ilo_buffer
*buf
)
419 struct ilo_screen
*is
= ilo_screen(buf
->base
.screen
);
420 const char *name
= resource_get_bo_name(&buf
->base
);
421 const bool cpu_init
= resource_get_cpu_init(&buf
->base
);
423 buf
->bo
= intel_winsys_alloc_bo(is
->dev
.winsys
, name
, buf
->bo_size
, cpu_init
);
425 return (buf
->bo
!= NULL
);
429 buf_destroy(struct ilo_buffer
*buf
)
431 intel_bo_unref(buf
->bo
);
435 static struct pipe_resource
*
436 buf_create(struct pipe_screen
*screen
, const struct pipe_resource
*templ
)
438 const struct ilo_screen
*is
= ilo_screen(screen
);
439 struct ilo_buffer
*buf
;
441 buf
= CALLOC_STRUCT(ilo_buffer
);
446 buf
->base
.screen
= screen
;
447 pipe_reference_init(&buf
->base
.reference
, 1);
449 buf
->bo_size
= templ
->width0
;
452 * From the Sandy Bridge PRM, volume 1 part 1, page 118:
454 * "For buffers, which have no inherent "height," padding requirements
455 * are different. A buffer must be padded to the next multiple of 256
456 * array elements, with an additional 16 bytes added beyond that to
457 * account for the L1 cache line."
459 if (templ
->bind
& PIPE_BIND_SAMPLER_VIEW
)
460 buf
->bo_size
= align(buf
->bo_size
, 256) + 16;
462 if ((templ
->bind
& PIPE_BIND_VERTEX_BUFFER
) &&
463 ilo_dev_gen(&is
->dev
) < ILO_GEN(7.5)) {
465 * As noted in ilo_format_translate(), we treat some 3-component formats
466 * as 4-component formats to work around hardware limitations. Imagine
467 * the case where the vertex buffer holds a single
468 * PIPE_FORMAT_R16G16B16_FLOAT vertex, and buf->bo_size is 6. The
469 * hardware would fail to fetch it at boundary check because the vertex
470 * buffer is expected to hold a PIPE_FORMAT_R16G16B16A16_FLOAT vertex
471 * and that takes at least 8 bytes.
473 * For the workaround to work, we should add 2 to the bo size. But that
474 * would waste a page when the bo size is already page aligned. Let's
475 * round it to page size for now and revisit this when needed.
477 buf
->bo_size
= align(buf
->bo_size
, 4096);
480 if (buf
->bo_size
< templ
->width0
||
481 buf
->bo_size
> ilo_max_resource_size
||
482 !buf_create_bo(buf
)) {
491 ilo_can_create_resource(struct pipe_screen
*screen
,
492 const struct pipe_resource
*templ
)
494 struct ilo_image img
;
496 if (templ
->target
== PIPE_BUFFER
)
497 return (templ
->width0
<= ilo_max_resource_size
);
499 memset(&img
, 0, sizeof(img
));
500 ilo_image_init(&img
, &ilo_screen(screen
)->dev
, templ
);
502 return (img
.bo_height
<= ilo_max_resource_size
/ img
.bo_stride
);
505 static struct pipe_resource
*
506 ilo_resource_create(struct pipe_screen
*screen
,
507 const struct pipe_resource
*templ
)
509 if (templ
->target
== PIPE_BUFFER
)
510 return buf_create(screen
, templ
);
512 return tex_create(screen
, templ
, NULL
);
515 static struct pipe_resource
*
516 ilo_resource_from_handle(struct pipe_screen
*screen
,
517 const struct pipe_resource
*templ
,
518 struct winsys_handle
*handle
)
520 if (templ
->target
== PIPE_BUFFER
)
523 return tex_create(screen
, templ
, handle
);
527 ilo_resource_get_handle(struct pipe_screen
*screen
,
528 struct pipe_resource
*res
,
529 struct winsys_handle
*handle
)
531 if (res
->target
== PIPE_BUFFER
)
534 return tex_get_handle(ilo_texture(res
), handle
);
539 ilo_resource_destroy(struct pipe_screen
*screen
,
540 struct pipe_resource
*res
)
542 if (res
->target
== PIPE_BUFFER
)
543 buf_destroy(ilo_buffer(res
));
545 tex_destroy(ilo_texture(res
));
549 * Initialize resource-related functions.
552 ilo_init_resource_functions(struct ilo_screen
*is
)
554 is
->base
.can_create_resource
= ilo_can_create_resource
;
555 is
->base
.resource_create
= ilo_resource_create
;
556 is
->base
.resource_from_handle
= ilo_resource_from_handle
;
557 is
->base
.resource_get_handle
= ilo_resource_get_handle
;
558 is
->base
.resource_destroy
= ilo_resource_destroy
;
562 ilo_buffer_rename_bo(struct ilo_buffer
*buf
)
564 struct intel_bo
*old_bo
= buf
->bo
;
566 if (buf_create_bo(buf
)) {
567 intel_bo_unref(old_bo
);
577 ilo_texture_rename_bo(struct ilo_texture
*tex
)
579 /* an imported texture cannot be renamed */
583 return tex_create_bo(tex
);