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 "core/ilo_state_vf.h"
29 #include "core/ilo_state_sol.h"
30 #include "core/ilo_state_surface.h"
32 #include "ilo_screen.h"
33 #include "ilo_resource.h"
36 * From the Ivy Bridge PRM, volume 1 part 1, page 105:
38 * "In addition to restrictions on maximum height, width, and depth,
39 * surfaces are also restricted to a maximum size in bytes. This
40 * maximum is 2 GB for all products and all surface types."
42 static const size_t ilo_max_resource_size
= 1u << 31;
45 resource_get_bo_name(const struct pipe_resource
*templ
)
47 static const char *target_names
[PIPE_MAX_TEXTURE_TYPES
] = {
48 [PIPE_BUFFER
] = "buf",
49 [PIPE_TEXTURE_1D
] = "tex-1d",
50 [PIPE_TEXTURE_2D
] = "tex-2d",
51 [PIPE_TEXTURE_3D
] = "tex-3d",
52 [PIPE_TEXTURE_CUBE
] = "tex-cube",
53 [PIPE_TEXTURE_RECT
] = "tex-rect",
54 [PIPE_TEXTURE_1D_ARRAY
] = "tex-1d-array",
55 [PIPE_TEXTURE_2D_ARRAY
] = "tex-2d-array",
56 [PIPE_TEXTURE_CUBE_ARRAY
] = "tex-cube-array",
58 const char *name
= target_names
[templ
->target
];
60 if (templ
->target
== PIPE_BUFFER
) {
61 switch (templ
->bind
) {
62 case PIPE_BIND_VERTEX_BUFFER
:
65 case PIPE_BIND_INDEX_BUFFER
:
68 case PIPE_BIND_CONSTANT_BUFFER
:
71 case PIPE_BIND_STREAM_OUTPUT
:
83 resource_get_cpu_init(const struct pipe_resource
*templ
)
85 return (templ
->bind
& (PIPE_BIND_DEPTH_STENCIL
|
86 PIPE_BIND_RENDER_TARGET
|
87 PIPE_BIND_STREAM_OUTPUT
)) ? false : true;
90 static enum gen_surface_tiling
91 winsys_to_surface_tiling(enum intel_tiling_mode tiling
)
94 case INTEL_TILING_NONE
:
95 return GEN6_TILING_NONE
;
101 assert(!"unknown tiling");
102 return GEN6_TILING_NONE
;
106 static inline enum intel_tiling_mode
107 surface_to_winsys_tiling(enum gen_surface_tiling tiling
)
110 case GEN6_TILING_NONE
:
111 return INTEL_TILING_NONE
;
113 return INTEL_TILING_X
;
115 return INTEL_TILING_Y
;
117 assert(!"unknown tiling");
118 return GEN6_TILING_NONE
;
123 tex_free_slices(struct ilo_texture
*tex
)
125 FREE(tex
->slices
[0]);
129 tex_alloc_slices(struct ilo_texture
*tex
)
131 const struct pipe_resource
*templ
= &tex
->base
;
132 struct ilo_texture_slice
*slices
;
135 /* sum the depths of all levels */
137 for (lv
= 0; lv
<= templ
->last_level
; lv
++)
138 depth
+= u_minify(templ
->depth0
, lv
);
141 * There are (depth * tex->base.array_size) slices in total. Either depth
142 * is one (non-3D) or templ->array_size is one (non-array), but it does
145 slices
= CALLOC(depth
* templ
->array_size
, sizeof(*slices
));
149 tex
->slices
[0] = slices
;
151 /* point to the respective positions in the buffer */
152 for (lv
= 1; lv
<= templ
->last_level
; lv
++) {
153 tex
->slices
[lv
] = tex
->slices
[lv
- 1] +
154 u_minify(templ
->depth0
, lv
- 1) * templ
->array_size
;
161 tex_create_bo(struct ilo_texture
*tex
)
163 struct ilo_screen
*is
= ilo_screen(tex
->base
.screen
);
164 const char *name
= resource_get_bo_name(&tex
->base
);
165 const bool cpu_init
= resource_get_cpu_init(&tex
->base
);
168 bo
= intel_winsys_alloc_bo(is
->dev
.winsys
, name
,
169 tex
->image
.bo_stride
* tex
->image
.bo_height
, cpu_init
);
171 /* set the tiling for transfer and export */
172 if (bo
&& (tex
->image
.tiling
== GEN6_TILING_X
||
173 tex
->image
.tiling
== GEN6_TILING_Y
)) {
174 const enum intel_tiling_mode tiling
=
175 surface_to_winsys_tiling(tex
->image
.tiling
);
177 if (intel_bo_set_tiling(bo
, tiling
, tex
->image
.bo_stride
)) {
185 intel_bo_unref(tex
->vma
.bo
);
186 ilo_vma_set_bo(&tex
->vma
, &is
->dev
, bo
, 0);
192 tex_create_separate_stencil(struct ilo_texture
*tex
)
194 struct pipe_resource templ
= tex
->base
;
195 struct pipe_resource
*s8
;
198 * Unless PIPE_BIND_DEPTH_STENCIL is set, the resource may have other
199 * tilings. But that should be fine since it will never be bound as the
200 * stencil buffer, and our transfer code can handle all tilings.
202 templ
.format
= PIPE_FORMAT_S8_UINT
;
204 /* no stencil texturing */
205 templ
.bind
&= ~PIPE_BIND_SAMPLER_VIEW
;
207 s8
= tex
->base
.screen
->resource_create(tex
->base
.screen
, &templ
);
211 tex
->separate_s8
= ilo_texture(s8
);
213 assert(tex
->separate_s8
->image
.format
== PIPE_FORMAT_S8_UINT
);
219 tex_create_hiz(struct ilo_texture
*tex
)
221 const struct pipe_resource
*templ
= &tex
->base
;
222 const uint32_t size
= tex
->image
.aux
.bo_stride
* tex
->image
.aux
.bo_height
;
223 struct ilo_screen
*is
= ilo_screen(tex
->base
.screen
);
226 bo
= intel_winsys_alloc_bo(is
->dev
.winsys
, "hiz texture", size
, false);
230 ilo_vma_init(&tex
->aux_vma
, &is
->dev
, size
, 4096);
231 ilo_vma_set_bo(&tex
->aux_vma
, &is
->dev
, bo
, 0);
236 for (lv
= 0; lv
<= templ
->last_level
; lv
++) {
237 if (tex
->image
.aux
.enables
& (1 << lv
)) {
238 const unsigned num_slices
= (templ
->target
== PIPE_TEXTURE_3D
) ?
239 u_minify(templ
->depth0
, lv
) : templ
->array_size
;
240 /* this will trigger HiZ resolves */
241 const unsigned flags
= ILO_TEXTURE_CPU_WRITE
;
243 ilo_texture_set_slice_flags(tex
, lv
, 0, num_slices
, flags
, flags
);
252 tex_create_mcs(struct ilo_texture
*tex
)
254 const uint32_t size
= tex
->image
.aux
.bo_stride
* tex
->image
.aux
.bo_height
;
255 struct ilo_screen
*is
= ilo_screen(tex
->base
.screen
);
258 assert(tex
->image
.aux
.enables
== (1 << (tex
->base
.last_level
+ 1)) - 1);
260 bo
= intel_winsys_alloc_bo(is
->dev
.winsys
, "mcs texture", size
, false);
264 ilo_vma_init(&tex
->aux_vma
, &is
->dev
, size
, 4096);
265 ilo_vma_set_bo(&tex
->aux_vma
, &is
->dev
, bo
, 0);
271 tex_destroy(struct ilo_texture
*tex
)
273 if (tex
->separate_s8
)
274 tex_destroy(tex
->separate_s8
);
276 intel_bo_unref(tex
->vma
.bo
);
277 intel_bo_unref(tex
->aux_vma
.bo
);
279 tex_free_slices(tex
);
284 tex_alloc_bos(struct ilo_texture
*tex
)
286 if (!tex
->imported
&& !tex_create_bo(tex
))
289 /* allocate separate stencil resource */
290 if (tex
->image
.separate_stencil
&& !tex_create_separate_stencil(tex
))
293 switch (tex
->image
.aux
.type
) {
294 case ILO_IMAGE_AUX_HIZ
:
295 if (!tex_create_hiz(tex
))
298 case ILO_IMAGE_AUX_MCS
:
299 if (!tex_create_mcs(tex
))
310 tex_import_handle(struct ilo_texture
*tex
,
311 const struct winsys_handle
*handle
)
313 struct ilo_screen
*is
= ilo_screen(tex
->base
.screen
);
314 const struct pipe_resource
*templ
= &tex
->base
;
315 const char *name
= resource_get_bo_name(&tex
->base
);
316 enum intel_tiling_mode tiling
;
320 bo
= intel_winsys_import_handle(is
->dev
.winsys
, name
, handle
,
321 tex
->image
.bo_height
, &tiling
, &pitch
);
325 if (!ilo_image_init_for_imported(&tex
->image
, &is
->dev
, templ
,
326 winsys_to_surface_tiling(tiling
), pitch
)) {
327 ilo_err("failed to import handle for texture\n");
332 ilo_vma_init(&tex
->vma
, &is
->dev
,
333 tex
->image
.bo_stride
* tex
->image
.bo_height
, 4096);
334 ilo_vma_set_bo(&tex
->vma
, &is
->dev
, bo
, 0);
336 tex
->imported
= true;
342 tex_init_image(struct ilo_texture
*tex
,
343 const struct winsys_handle
*handle
)
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
;
350 if (!tex_import_handle(tex
, handle
))
353 ilo_image_init(img
, &is
->dev
, templ
);
354 ilo_vma_init(&tex
->vma
, &is
->dev
,
355 img
->bo_stride
* img
->bo_height
, 4096);
358 if (img
->bo_height
> ilo_max_resource_size
/ img
->bo_stride
)
361 if (templ
->flags
& PIPE_RESOURCE_FLAG_MAP_PERSISTENT
) {
362 /* require on-the-fly tiling/untiling or format conversion */
363 if (img
->tiling
== GEN8_TILING_W
|| img
->separate_stencil
||
364 img
->format
!= templ
->format
)
368 if (!tex_alloc_slices(tex
))
374 static struct pipe_resource
*
375 tex_create(struct pipe_screen
*screen
,
376 const struct pipe_resource
*templ
,
377 const struct winsys_handle
*handle
)
379 struct ilo_texture
*tex
;
381 tex
= CALLOC_STRUCT(ilo_texture
);
386 tex
->base
.screen
= screen
;
387 pipe_reference_init(&tex
->base
.reference
, 1);
389 if (!tex_init_image(tex
, handle
)) {
394 if (!tex_alloc_bos(tex
)) {
403 tex_get_handle(struct ilo_texture
*tex
, struct winsys_handle
*handle
)
405 struct ilo_screen
*is
= ilo_screen(tex
->base
.screen
);
406 enum intel_tiling_mode tiling
;
409 /* must match what tex_create_bo() sets */
410 if (tex
->image
.tiling
== GEN8_TILING_W
)
411 tiling
= INTEL_TILING_NONE
;
413 tiling
= surface_to_winsys_tiling(tex
->image
.tiling
);
415 err
= intel_winsys_export_handle(is
->dev
.winsys
, tex
->vma
.bo
, tiling
,
416 tex
->image
.bo_stride
, tex
->image
.bo_height
, handle
);
422 buf_create_bo(struct ilo_buffer_resource
*buf
)
424 struct ilo_screen
*is
= ilo_screen(buf
->base
.screen
);
425 const char *name
= resource_get_bo_name(&buf
->base
);
426 const bool cpu_init
= resource_get_cpu_init(&buf
->base
);
429 bo
= intel_winsys_alloc_bo(is
->dev
.winsys
, name
, buf
->bo_size
, cpu_init
);
433 intel_bo_unref(buf
->vma
.bo
);
434 ilo_vma_set_bo(&buf
->vma
, &is
->dev
, bo
, 0);
440 buf_destroy(struct ilo_buffer_resource
*buf
)
442 intel_bo_unref(buf
->vma
.bo
);
446 static struct pipe_resource
*
447 buf_create(struct pipe_screen
*screen
, const struct pipe_resource
*templ
)
449 const struct ilo_screen
*is
= ilo_screen(screen
);
450 struct ilo_buffer_resource
*buf
;
454 buf
= CALLOC_STRUCT(ilo_buffer_resource
);
459 buf
->base
.screen
= screen
;
460 pipe_reference_init(&buf
->base
.reference
, 1);
462 size
= templ
->width0
;
465 * As noted in ilo_format_translate(), we treat some 3-component formats as
466 * 4-component formats to work around hardware limitations. Imagine the
467 * case where the vertex buffer holds a single PIPE_FORMAT_R16G16B16_FLOAT
468 * vertex, and buf->bo_size is 6. The hardware would fail to fetch it at
469 * boundary check because the vertex buffer is expected to hold a
470 * PIPE_FORMAT_R16G16B16A16_FLOAT vertex and that takes at least 8 bytes.
472 * For the workaround to work, we should add 2 to the bo size. But that
473 * would waste a page when the bo size is already page aligned. Let's
474 * round it to page size for now and revisit this when needed.
476 if ((templ
->bind
& PIPE_BIND_VERTEX_BUFFER
) &&
477 ilo_dev_gen(&is
->dev
) < ILO_GEN(7.5))
478 size
= align(size
, 4096);
480 if (templ
->bind
& PIPE_BIND_VERTEX_BUFFER
)
481 size
= ilo_state_vertex_buffer_size(&is
->dev
, size
, &alignment
);
482 if (templ
->bind
& PIPE_BIND_INDEX_BUFFER
)
483 size
= ilo_state_index_buffer_size(&is
->dev
, size
, &alignment
);
484 if (templ
->bind
& PIPE_BIND_STREAM_OUTPUT
)
485 size
= ilo_state_sol_buffer_size(&is
->dev
, size
, &alignment
);
488 ilo_vma_init(&buf
->vma
, &is
->dev
, buf
->bo_size
, 4096);
490 if (buf
->bo_size
< templ
->width0
|| buf
->bo_size
> ilo_max_resource_size
||
491 !buf_create_bo(buf
)) {
500 ilo_can_create_resource(struct pipe_screen
*screen
,
501 const struct pipe_resource
*templ
)
503 struct ilo_image img
;
505 if (templ
->target
== PIPE_BUFFER
)
506 return (templ
->width0
<= ilo_max_resource_size
);
508 memset(&img
, 0, sizeof(img
));
509 ilo_image_init(&img
, &ilo_screen(screen
)->dev
, templ
);
511 return (img
.bo_height
<= ilo_max_resource_size
/ img
.bo_stride
);
514 static struct pipe_resource
*
515 ilo_resource_create(struct pipe_screen
*screen
,
516 const struct pipe_resource
*templ
)
518 if (templ
->target
== PIPE_BUFFER
)
519 return buf_create(screen
, templ
);
521 return tex_create(screen
, templ
, NULL
);
524 static struct pipe_resource
*
525 ilo_resource_from_handle(struct pipe_screen
*screen
,
526 const struct pipe_resource
*templ
,
527 struct winsys_handle
*handle
)
529 if (templ
->target
== PIPE_BUFFER
)
532 return tex_create(screen
, templ
, handle
);
536 ilo_resource_get_handle(struct pipe_screen
*screen
,
537 struct pipe_resource
*res
,
538 struct winsys_handle
*handle
)
540 if (res
->target
== PIPE_BUFFER
)
543 return tex_get_handle(ilo_texture(res
), handle
);
548 ilo_resource_destroy(struct pipe_screen
*screen
,
549 struct pipe_resource
*res
)
551 if (res
->target
== PIPE_BUFFER
)
552 buf_destroy((struct ilo_buffer_resource
*) res
);
554 tex_destroy(ilo_texture(res
));
558 * Initialize resource-related functions.
561 ilo_init_resource_functions(struct ilo_screen
*is
)
563 is
->base
.can_create_resource
= ilo_can_create_resource
;
564 is
->base
.resource_create
= ilo_resource_create
;
565 is
->base
.resource_from_handle
= ilo_resource_from_handle
;
566 is
->base
.resource_get_handle
= ilo_resource_get_handle
;
567 is
->base
.resource_destroy
= ilo_resource_destroy
;
571 ilo_resource_rename_bo(struct pipe_resource
*res
)
573 if (res
->target
== PIPE_BUFFER
) {
574 return buf_create_bo((struct ilo_buffer_resource
*) res
);
576 struct ilo_texture
*tex
= ilo_texture(res
);
578 /* an imported texture cannot be renamed */
582 return tex_create_bo(tex
);