2012-12-13 04:07:16 +08:00
|
|
|
/*
|
|
|
|
|
* Mesa 3-D graphics library
|
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 2012-2013 LunarG, Inc.
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included
|
|
|
|
|
* in all copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
|
*
|
|
|
|
|
* Authors:
|
|
|
|
|
* Chia-I Wu <olv@lunarg.com>
|
|
|
|
|
*/
|
|
|
|
|
|
2014-08-08 15:36:36 +08:00
|
|
|
#include "ilo_layout.h"
|
2012-12-13 04:07:16 +08:00
|
|
|
#include "ilo_screen.h"
|
|
|
|
|
#include "ilo_resource.h"
|
|
|
|
|
|
2014-07-14 10:10:35 +08:00
|
|
|
/*
|
2014-07-15 09:54:08 +08:00
|
|
|
* From the Ivy Bridge PRM, volume 1 part 1, page 105:
|
|
|
|
|
*
|
|
|
|
|
* "In addition to restrictions on maximum height, width, and depth,
|
|
|
|
|
* surfaces are also restricted to a maximum size in bytes. This
|
|
|
|
|
* maximum is 2 GB for all products and all surface types."
|
2014-07-14 10:10:35 +08:00
|
|
|
*/
|
2014-08-08 15:36:36 +08:00
|
|
|
static const size_t ilo_max_resource_size = 1u << 31;
|
2014-07-14 10:10:35 +08:00
|
|
|
|
2014-07-24 10:32:31 +08:00
|
|
|
static const char *
|
|
|
|
|
resource_get_bo_name(const struct pipe_resource *templ)
|
|
|
|
|
{
|
|
|
|
|
static const char *target_names[PIPE_MAX_TEXTURE_TYPES] = {
|
|
|
|
|
[PIPE_BUFFER] = "buf",
|
|
|
|
|
[PIPE_TEXTURE_1D] = "tex-1d",
|
|
|
|
|
[PIPE_TEXTURE_2D] = "tex-2d",
|
|
|
|
|
[PIPE_TEXTURE_3D] = "tex-3d",
|
|
|
|
|
[PIPE_TEXTURE_CUBE] = "tex-cube",
|
|
|
|
|
[PIPE_TEXTURE_RECT] = "tex-rect",
|
|
|
|
|
[PIPE_TEXTURE_1D_ARRAY] = "tex-1d-array",
|
|
|
|
|
[PIPE_TEXTURE_2D_ARRAY] = "tex-2d-array",
|
|
|
|
|
[PIPE_TEXTURE_CUBE_ARRAY] = "tex-cube-array",
|
|
|
|
|
};
|
|
|
|
|
const char *name = target_names[templ->target];
|
|
|
|
|
|
|
|
|
|
if (templ->target == PIPE_BUFFER) {
|
|
|
|
|
switch (templ->bind) {
|
|
|
|
|
case PIPE_BIND_VERTEX_BUFFER:
|
|
|
|
|
name = "buf-vb";
|
|
|
|
|
break;
|
|
|
|
|
case PIPE_BIND_INDEX_BUFFER:
|
|
|
|
|
name = "buf-ib";
|
|
|
|
|
break;
|
|
|
|
|
case PIPE_BIND_CONSTANT_BUFFER:
|
|
|
|
|
name = "buf-cb";
|
|
|
|
|
break;
|
|
|
|
|
case PIPE_BIND_STREAM_OUTPUT:
|
|
|
|
|
name = "buf-so";
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return name;
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-26 12:36:33 +08:00
|
|
|
static bool
|
|
|
|
|
resource_get_cpu_init(const struct pipe_resource *templ)
|
2014-07-24 10:32:31 +08:00
|
|
|
{
|
|
|
|
|
return (templ->bind & (PIPE_BIND_DEPTH_STENCIL |
|
|
|
|
|
PIPE_BIND_RENDER_TARGET |
|
2014-08-26 12:36:33 +08:00
|
|
|
PIPE_BIND_STREAM_OUTPUT)) ? false : true;
|
2014-07-24 10:32:31 +08:00
|
|
|
}
|
|
|
|
|
|
2015-03-05 23:53:16 +08:00
|
|
|
static enum gen_surface_tiling
|
|
|
|
|
winsys_to_surface_tiling(enum intel_tiling_mode tiling)
|
|
|
|
|
{
|
|
|
|
|
switch (tiling) {
|
|
|
|
|
case INTEL_TILING_NONE:
|
|
|
|
|
return GEN6_TILING_NONE;
|
|
|
|
|
case INTEL_TILING_X:
|
|
|
|
|
return GEN6_TILING_X;
|
|
|
|
|
case INTEL_TILING_Y:
|
|
|
|
|
return GEN6_TILING_Y;
|
|
|
|
|
default:
|
|
|
|
|
assert(!"unknown tiling");
|
|
|
|
|
return GEN6_TILING_NONE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline enum intel_tiling_mode
|
|
|
|
|
surface_to_winsys_tiling(enum gen_surface_tiling tiling)
|
|
|
|
|
{
|
|
|
|
|
switch (tiling) {
|
|
|
|
|
case GEN6_TILING_NONE:
|
|
|
|
|
return INTEL_TILING_NONE;
|
|
|
|
|
case GEN6_TILING_X:
|
|
|
|
|
return INTEL_TILING_X;
|
|
|
|
|
case GEN6_TILING_Y:
|
|
|
|
|
return INTEL_TILING_Y;
|
|
|
|
|
default:
|
|
|
|
|
assert(!"unknown tiling");
|
|
|
|
|
return GEN6_TILING_NONE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-13 15:19:55 +08:00
|
|
|
static void
|
|
|
|
|
tex_free_slices(struct ilo_texture *tex)
|
|
|
|
|
{
|
2013-12-26 11:46:25 +08:00
|
|
|
FREE(tex->slices[0]);
|
2013-05-13 15:19:55 +08:00
|
|
|
}
|
2012-12-13 04:44:21 +08:00
|
|
|
|
2013-05-13 15:19:55 +08:00
|
|
|
static bool
|
|
|
|
|
tex_alloc_slices(struct ilo_texture *tex)
|
|
|
|
|
{
|
|
|
|
|
const struct pipe_resource *templ = &tex->base;
|
|
|
|
|
struct ilo_texture_slice *slices;
|
|
|
|
|
int depth, lv;
|
|
|
|
|
|
|
|
|
|
/* sum the depths of all levels */
|
|
|
|
|
depth = 0;
|
|
|
|
|
for (lv = 0; lv <= templ->last_level; lv++)
|
|
|
|
|
depth += u_minify(templ->depth0, lv);
|
2013-04-30 15:30:01 +08:00
|
|
|
|
2012-12-13 04:44:21 +08:00
|
|
|
/*
|
2013-05-13 15:19:55 +08:00
|
|
|
* There are (depth * tex->base.array_size) slices in total. Either depth
|
|
|
|
|
* is one (non-3D) or templ->array_size is one (non-array), but it does
|
|
|
|
|
* not matter.
|
2012-12-13 04:44:21 +08:00
|
|
|
*/
|
2013-05-13 15:19:55 +08:00
|
|
|
slices = CALLOC(depth * templ->array_size, sizeof(*slices));
|
|
|
|
|
if (!slices)
|
|
|
|
|
return false;
|
2012-12-13 04:44:21 +08:00
|
|
|
|
2013-12-26 11:46:25 +08:00
|
|
|
tex->slices[0] = slices;
|
2012-12-13 04:44:21 +08:00
|
|
|
|
2013-05-13 15:19:55 +08:00
|
|
|
/* point to the respective positions in the buffer */
|
|
|
|
|
for (lv = 1; lv <= templ->last_level; lv++) {
|
2013-12-26 11:46:25 +08:00
|
|
|
tex->slices[lv] = tex->slices[lv - 1] +
|
2013-05-13 15:19:55 +08:00
|
|
|
u_minify(templ->depth0, lv - 1) * templ->array_size;
|
2012-12-13 04:44:21 +08:00
|
|
|
}
|
|
|
|
|
|
2013-05-13 15:19:55 +08:00
|
|
|
return true;
|
2012-12-13 04:44:21 +08:00
|
|
|
}
|
|
|
|
|
|
2013-06-11 18:51:22 +08:00
|
|
|
static bool
|
2014-07-24 11:10:48 +08:00
|
|
|
tex_import_handle(struct ilo_texture *tex,
|
|
|
|
|
const struct winsys_handle *handle)
|
2012-12-13 04:44:21 +08:00
|
|
|
{
|
2013-05-13 15:19:55 +08:00
|
|
|
struct ilo_screen *is = ilo_screen(tex->base.screen);
|
2014-07-24 10:32:31 +08:00
|
|
|
const char *name = resource_get_bo_name(&tex->base);
|
2014-07-24 11:10:48 +08:00
|
|
|
enum intel_tiling_mode tiling;
|
|
|
|
|
unsigned long pitch;
|
2014-07-14 10:10:35 +08:00
|
|
|
|
2014-07-24 11:10:48 +08:00
|
|
|
tex->bo = intel_winsys_import_handle(is->winsys, name, handle,
|
2014-08-08 15:36:36 +08:00
|
|
|
tex->layout.bo_height, &tiling, &pitch);
|
2014-07-24 11:10:48 +08:00
|
|
|
if (!tex->bo)
|
|
|
|
|
return false;
|
2014-03-08 23:55:15 +08:00
|
|
|
|
2015-03-05 23:53:16 +08:00
|
|
|
if (!ilo_layout_update_for_imported_bo(&tex->layout,
|
|
|
|
|
winsys_to_surface_tiling(tiling), pitch)) {
|
2014-08-08 15:36:36 +08:00
|
|
|
ilo_err("imported handle has incompatible tiling/pitch\n");
|
2014-07-24 13:21:41 +08:00
|
|
|
intel_bo_unreference(tex->bo);
|
|
|
|
|
tex->bo = NULL;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-24 11:10:48 +08:00
|
|
|
return true;
|
|
|
|
|
}
|
2013-05-13 15:19:55 +08:00
|
|
|
|
2014-07-24 11:10:48 +08:00
|
|
|
static bool
|
|
|
|
|
tex_create_bo(struct ilo_texture *tex)
|
|
|
|
|
{
|
|
|
|
|
struct ilo_screen *is = ilo_screen(tex->base.screen);
|
|
|
|
|
const char *name = resource_get_bo_name(&tex->base);
|
2014-08-26 12:36:33 +08:00
|
|
|
const bool cpu_init = resource_get_cpu_init(&tex->base);
|
2015-03-05 23:53:16 +08:00
|
|
|
enum intel_tiling_mode tiling;
|
2013-05-13 15:19:55 +08:00
|
|
|
|
2015-03-05 23:53:16 +08:00
|
|
|
/* no native support */
|
|
|
|
|
if (tex->layout.tiling == GEN8_TILING_W)
|
|
|
|
|
tiling = INTEL_TILING_NONE;
|
|
|
|
|
else
|
|
|
|
|
tiling = surface_to_winsys_tiling(tex->layout.tiling);
|
|
|
|
|
|
|
|
|
|
tex->bo = intel_winsys_alloc_bo(is->winsys, name, tiling,
|
2014-08-26 12:36:33 +08:00
|
|
|
tex->layout.bo_stride, tex->layout.bo_height, cpu_init);
|
2013-06-11 18:51:22 +08:00
|
|
|
|
2014-07-24 11:10:48 +08:00
|
|
|
return (tex->bo != NULL);
|
2012-12-13 04:44:21 +08:00
|
|
|
}
|
|
|
|
|
|
2013-12-21 21:21:24 +08:00
|
|
|
static bool
|
|
|
|
|
tex_create_separate_stencil(struct ilo_texture *tex)
|
|
|
|
|
{
|
|
|
|
|
struct pipe_resource templ = tex->base;
|
|
|
|
|
struct pipe_resource *s8;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Unless PIPE_BIND_DEPTH_STENCIL is set, the resource may have other
|
|
|
|
|
* tilings. But that should be fine since it will never be bound as the
|
|
|
|
|
* stencil buffer, and our transfer code can handle all tilings.
|
|
|
|
|
*/
|
|
|
|
|
templ.format = PIPE_FORMAT_S8_UINT;
|
|
|
|
|
|
|
|
|
|
s8 = tex->base.screen->resource_create(tex->base.screen, &templ);
|
|
|
|
|
if (!s8)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
tex->separate_s8 = ilo_texture(s8);
|
|
|
|
|
|
2014-08-08 15:36:36 +08:00
|
|
|
assert(tex->separate_s8->layout.format == PIPE_FORMAT_S8_UINT);
|
2013-12-21 21:21:24 +08:00
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-06 23:32:32 +08:00
|
|
|
static bool
|
2014-08-08 15:36:36 +08:00
|
|
|
tex_create_hiz(struct ilo_texture *tex)
|
2014-01-06 23:32:32 +08:00
|
|
|
{
|
2014-08-08 15:36:36 +08:00
|
|
|
const struct pipe_resource *templ = &tex->base;
|
2014-01-06 23:32:32 +08:00
|
|
|
struct ilo_screen *is = ilo_screen(tex->base.screen);
|
2014-07-14 10:10:35 +08:00
|
|
|
unsigned lv;
|
2014-01-06 23:32:32 +08:00
|
|
|
|
2015-03-05 23:53:16 +08:00
|
|
|
tex->aux_bo = intel_winsys_alloc_buffer(is->winsys, "hiz texture",
|
|
|
|
|
tex->layout.aux_stride * tex->layout.aux_height, false);
|
2014-08-08 15:36:36 +08:00
|
|
|
if (!tex->aux_bo)
|
2014-01-06 23:32:32 +08:00
|
|
|
return false;
|
|
|
|
|
|
2014-01-14 14:51:51 +08:00
|
|
|
for (lv = 0; lv <= templ->last_level; lv++) {
|
2014-08-08 15:36:36 +08:00
|
|
|
if (tex->layout.aux_enables & (1 << lv)) {
|
|
|
|
|
const unsigned num_slices = (templ->target == PIPE_TEXTURE_3D) ?
|
|
|
|
|
u_minify(templ->depth0, lv) : templ->array_size;
|
|
|
|
|
unsigned flags = ILO_TEXTURE_HIZ;
|
2014-01-14 23:35:43 +08:00
|
|
|
|
|
|
|
|
/* this will trigger a HiZ resolve */
|
|
|
|
|
if (tex->imported)
|
|
|
|
|
flags |= ILO_TEXTURE_CPU_WRITE;
|
|
|
|
|
|
|
|
|
|
ilo_texture_set_slice_flags(tex, lv, 0, num_slices, flags, flags);
|
2014-01-14 14:51:51 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-06 23:32:32 +08:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-14 10:10:35 +08:00
|
|
|
static bool
|
2014-08-08 15:36:36 +08:00
|
|
|
tex_create_mcs(struct ilo_texture *tex)
|
2014-07-14 10:10:35 +08:00
|
|
|
{
|
2014-08-08 15:36:36 +08:00
|
|
|
struct ilo_screen *is = ilo_screen(tex->base.screen);
|
2014-07-14 10:10:35 +08:00
|
|
|
|
2014-08-08 15:36:36 +08:00
|
|
|
assert(tex->layout.aux_enables == (1 << (tex->base.last_level + 1)) - 1);
|
2014-07-14 10:10:35 +08:00
|
|
|
|
2015-03-05 23:53:16 +08:00
|
|
|
tex->aux_bo = intel_winsys_alloc_buffer(is->winsys, "mcs texture",
|
|
|
|
|
tex->layout.aux_stride * tex->layout.aux_height, false);
|
2014-08-08 15:36:36 +08:00
|
|
|
if (!tex->aux_bo)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
tex_destroy(struct ilo_texture *tex)
|
|
|
|
|
{
|
|
|
|
|
if (tex->aux_bo)
|
|
|
|
|
intel_bo_unreference(tex->aux_bo);
|
|
|
|
|
|
|
|
|
|
if (tex->separate_s8)
|
|
|
|
|
tex_destroy(tex->separate_s8);
|
|
|
|
|
|
|
|
|
|
if (tex->bo)
|
|
|
|
|
intel_bo_unreference(tex->bo);
|
|
|
|
|
|
|
|
|
|
tex_free_slices(tex);
|
|
|
|
|
FREE(tex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool
|
|
|
|
|
tex_alloc_bos(struct ilo_texture *tex,
|
|
|
|
|
const struct winsys_handle *handle)
|
|
|
|
|
{
|
|
|
|
|
struct ilo_screen *is = ilo_screen(tex->base.screen);
|
2014-07-14 10:10:35 +08:00
|
|
|
|
2014-07-24 11:10:48 +08:00
|
|
|
if (handle) {
|
2014-08-08 15:36:36 +08:00
|
|
|
if (!tex_import_handle(tex, handle))
|
2014-07-24 11:10:48 +08:00
|
|
|
return false;
|
2014-08-08 15:36:36 +08:00
|
|
|
} else {
|
2014-07-24 11:10:48 +08:00
|
|
|
if (!tex_create_bo(tex))
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2014-07-14 10:10:35 +08:00
|
|
|
|
|
|
|
|
/* allocate separate stencil resource */
|
2014-08-08 15:36:36 +08:00
|
|
|
if (tex->layout.separate_stencil && !tex_create_separate_stencil(tex))
|
2014-07-14 10:10:35 +08:00
|
|
|
return false;
|
|
|
|
|
|
2014-08-08 15:36:36 +08:00
|
|
|
switch (tex->layout.aux) {
|
|
|
|
|
case ILO_LAYOUT_AUX_HIZ:
|
|
|
|
|
if (!tex_create_hiz(tex)) {
|
|
|
|
|
/* Separate Stencil Buffer requires HiZ to be enabled */
|
2014-09-12 10:55:58 +08:00
|
|
|
if (ilo_dev_gen(&is->dev) == ILO_GEN(6) &&
|
|
|
|
|
tex->layout.separate_stencil)
|
2014-08-08 15:36:36 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case ILO_LAYOUT_AUX_MCS:
|
|
|
|
|
if (!tex_create_mcs(tex))
|
2014-07-14 10:10:35 +08:00
|
|
|
return false;
|
2014-08-08 15:36:36 +08:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
2014-07-14 10:10:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-28 09:28:05 +08:00
|
|
|
static bool
|
2014-08-08 15:36:36 +08:00
|
|
|
tex_init_layout(struct ilo_texture *tex)
|
2014-07-28 09:28:05 +08:00
|
|
|
{
|
2014-08-08 15:36:36 +08:00
|
|
|
struct ilo_screen *is = ilo_screen(tex->base.screen);
|
2014-07-28 09:28:05 +08:00
|
|
|
const struct pipe_resource *templ = &tex->base;
|
2014-08-08 15:36:36 +08:00
|
|
|
struct ilo_layout *layout = &tex->layout;
|
2014-07-28 09:28:05 +08:00
|
|
|
|
2014-08-08 15:36:36 +08:00
|
|
|
ilo_layout_init(layout, &is->dev, templ);
|
2014-07-28 09:28:05 +08:00
|
|
|
|
2014-08-08 15:36:36 +08:00
|
|
|
if (layout->bo_height > ilo_max_resource_size / layout->bo_stride)
|
|
|
|
|
return false;
|
2014-01-14 13:33:22 +08:00
|
|
|
|
2014-08-08 15:36:36 +08:00
|
|
|
if (templ->flags & PIPE_RESOURCE_FLAG_MAP_PERSISTENT) {
|
|
|
|
|
/* require on-the-fly tiling/untiling or format conversion */
|
2015-03-05 23:53:16 +08:00
|
|
|
if (layout->tiling == GEN8_TILING_W || layout->separate_stencil ||
|
2014-08-08 15:36:36 +08:00
|
|
|
layout->format != templ->format)
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2013-05-15 12:18:13 +08:00
|
|
|
|
2014-08-08 15:36:36 +08:00
|
|
|
if (!tex_alloc_slices(tex))
|
|
|
|
|
return false;
|
2014-07-14 10:10:35 +08:00
|
|
|
|
2014-08-08 15:36:36 +08:00
|
|
|
return true;
|
2012-12-13 04:44:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static struct pipe_resource *
|
2013-05-13 15:19:55 +08:00
|
|
|
tex_create(struct pipe_screen *screen,
|
|
|
|
|
const struct pipe_resource *templ,
|
|
|
|
|
const struct winsys_handle *handle)
|
2012-12-13 04:44:21 +08:00
|
|
|
{
|
2013-05-13 15:10:34 +08:00
|
|
|
struct ilo_texture *tex;
|
2012-12-13 04:44:21 +08:00
|
|
|
|
2013-05-13 15:10:34 +08:00
|
|
|
tex = CALLOC_STRUCT(ilo_texture);
|
|
|
|
|
if (!tex)
|
2012-12-13 04:44:21 +08:00
|
|
|
return NULL;
|
|
|
|
|
|
2013-05-13 15:10:34 +08:00
|
|
|
tex->base = *templ;
|
|
|
|
|
tex->base.screen = screen;
|
|
|
|
|
pipe_reference_init(&tex->base.reference, 1);
|
2012-12-13 04:44:21 +08:00
|
|
|
|
2013-05-13 15:19:55 +08:00
|
|
|
tex->imported = (handle != NULL);
|
|
|
|
|
|
2014-08-08 15:36:36 +08:00
|
|
|
if (!tex_init_layout(tex)) {
|
|
|
|
|
FREE(tex);
|
2012-12-13 04:44:21 +08:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-08 15:36:36 +08:00
|
|
|
if (!tex_alloc_bos(tex, handle)) {
|
2013-12-21 21:21:24 +08:00
|
|
|
tex_destroy(tex);
|
|
|
|
|
return NULL;
|
2013-05-15 12:18:13 +08:00
|
|
|
}
|
|
|
|
|
|
2013-05-13 15:10:34 +08:00
|
|
|
return &tex->base;
|
2012-12-13 04:44:21 +08:00
|
|
|
}
|
|
|
|
|
|
2013-05-13 15:19:55 +08:00
|
|
|
static bool
|
|
|
|
|
tex_get_handle(struct ilo_texture *tex, struct winsys_handle *handle)
|
|
|
|
|
{
|
2013-06-12 16:38:38 +08:00
|
|
|
struct ilo_screen *is = ilo_screen(tex->base.screen);
|
2015-03-05 23:53:16 +08:00
|
|
|
enum intel_tiling_mode tiling;
|
2013-05-13 15:19:55 +08:00
|
|
|
int err;
|
|
|
|
|
|
2015-03-05 23:53:16 +08:00
|
|
|
/* no native support */
|
|
|
|
|
if (tex->layout.tiling == GEN8_TILING_W)
|
|
|
|
|
tiling = INTEL_TILING_NONE;
|
|
|
|
|
else
|
|
|
|
|
tiling = surface_to_winsys_tiling(tex->layout.tiling);
|
|
|
|
|
|
|
|
|
|
err = intel_winsys_export_handle(is->winsys, tex->bo, tiling,
|
2014-08-08 15:36:36 +08:00
|
|
|
tex->layout.bo_stride, tex->layout.bo_height, handle);
|
2013-05-13 15:19:55 +08:00
|
|
|
|
|
|
|
|
return !err;
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-11 18:51:22 +08:00
|
|
|
static bool
|
|
|
|
|
buf_create_bo(struct ilo_buffer *buf)
|
2013-05-13 15:19:55 +08:00
|
|
|
{
|
|
|
|
|
struct ilo_screen *is = ilo_screen(buf->base.screen);
|
2014-07-24 10:32:31 +08:00
|
|
|
const char *name = resource_get_bo_name(&buf->base);
|
2014-08-26 12:36:33 +08:00
|
|
|
const bool cpu_init = resource_get_cpu_init(&buf->base);
|
2013-05-13 15:19:55 +08:00
|
|
|
|
2014-07-24 11:10:48 +08:00
|
|
|
buf->bo = intel_winsys_alloc_buffer(is->winsys, name,
|
2014-08-26 12:36:33 +08:00
|
|
|
buf->bo_size, cpu_init);
|
2013-05-13 15:19:55 +08:00
|
|
|
|
2014-07-24 11:10:48 +08:00
|
|
|
return (buf->bo != NULL);
|
2013-05-13 15:19:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
buf_destroy(struct ilo_buffer *buf)
|
|
|
|
|
{
|
2013-06-12 16:21:00 +08:00
|
|
|
intel_bo_unreference(buf->bo);
|
2013-05-13 15:19:55 +08:00
|
|
|
FREE(buf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static struct pipe_resource *
|
|
|
|
|
buf_create(struct pipe_screen *screen, const struct pipe_resource *templ)
|
|
|
|
|
{
|
2014-07-24 09:39:37 +08:00
|
|
|
const struct ilo_screen *is = ilo_screen(screen);
|
2013-05-13 15:19:55 +08:00
|
|
|
struct ilo_buffer *buf;
|
|
|
|
|
|
|
|
|
|
buf = CALLOC_STRUCT(ilo_buffer);
|
|
|
|
|
if (!buf)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
buf->base = *templ;
|
|
|
|
|
buf->base.screen = screen;
|
|
|
|
|
pipe_reference_init(&buf->base.reference, 1);
|
|
|
|
|
|
|
|
|
|
buf->bo_size = templ->width0;
|
|
|
|
|
|
2013-07-10 12:05:37 +08:00
|
|
|
/*
|
|
|
|
|
* From the Sandy Bridge PRM, volume 1 part 1, page 118:
|
|
|
|
|
*
|
|
|
|
|
* "For buffers, which have no inherent "height," padding requirements
|
|
|
|
|
* are different. A buffer must be padded to the next multiple of 256
|
|
|
|
|
* array elements, with an additional 16 bytes added beyond that to
|
|
|
|
|
* account for the L1 cache line."
|
|
|
|
|
*/
|
|
|
|
|
if (templ->bind & PIPE_BIND_SAMPLER_VIEW)
|
|
|
|
|
buf->bo_size = align(buf->bo_size, 256) + 16;
|
|
|
|
|
|
2014-07-24 09:39:37 +08:00
|
|
|
if ((templ->bind & PIPE_BIND_VERTEX_BUFFER) &&
|
2014-09-12 10:55:58 +08:00
|
|
|
ilo_dev_gen(&is->dev) < ILO_GEN(7.5)) {
|
2014-03-08 17:22:45 +08:00
|
|
|
/*
|
|
|
|
|
* As noted in ilo_translate_format(), we treat some 3-component formats
|
|
|
|
|
* as 4-component formats to work around hardware limitations. Imagine
|
|
|
|
|
* the case where the vertex buffer holds a single
|
|
|
|
|
* PIPE_FORMAT_R16G16B16_FLOAT vertex, and buf->bo_size is 6. The
|
|
|
|
|
* hardware would fail to fetch it at boundary check because the vertex
|
|
|
|
|
* buffer is expected to hold a PIPE_FORMAT_R16G16B16A16_FLOAT vertex
|
|
|
|
|
* and that takes at least 8 bytes.
|
|
|
|
|
*
|
|
|
|
|
* For the workaround to work, we should add 2 to the bo size. But that
|
|
|
|
|
* would waste a page when the bo size is already page aligned. Let's
|
|
|
|
|
* round it to page size for now and revisit this when needed.
|
|
|
|
|
*/
|
|
|
|
|
buf->bo_size = align(buf->bo_size, 4096);
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-14 10:10:35 +08:00
|
|
|
if (buf->bo_size < templ->width0 ||
|
2014-08-08 15:36:36 +08:00
|
|
|
buf->bo_size > ilo_max_resource_size ||
|
2014-07-14 10:10:35 +08:00
|
|
|
!buf_create_bo(buf)) {
|
2013-05-13 15:19:55 +08:00
|
|
|
FREE(buf);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &buf->base;
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-13 04:44:21 +08:00
|
|
|
static boolean
|
|
|
|
|
ilo_can_create_resource(struct pipe_screen *screen,
|
|
|
|
|
const struct pipe_resource *templ)
|
|
|
|
|
{
|
2014-08-08 15:36:36 +08:00
|
|
|
struct ilo_layout layout;
|
2013-05-13 15:19:55 +08:00
|
|
|
|
|
|
|
|
if (templ->target == PIPE_BUFFER)
|
2014-08-08 15:36:36 +08:00
|
|
|
return (templ->width0 <= ilo_max_resource_size);
|
2014-07-14 10:10:35 +08:00
|
|
|
|
2014-08-08 15:36:36 +08:00
|
|
|
memset(&layout, 0, sizeof(layout));
|
|
|
|
|
ilo_layout_init(&layout, &ilo_screen(screen)->dev, templ);
|
2012-12-13 04:44:21 +08:00
|
|
|
|
2014-08-08 15:36:36 +08:00
|
|
|
return (layout.bo_height <= ilo_max_resource_size / layout.bo_stride);
|
2012-12-13 04:44:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static struct pipe_resource *
|
|
|
|
|
ilo_resource_create(struct pipe_screen *screen,
|
|
|
|
|
const struct pipe_resource *templ)
|
|
|
|
|
{
|
2013-05-13 15:19:55 +08:00
|
|
|
if (templ->target == PIPE_BUFFER)
|
|
|
|
|
return buf_create(screen, templ);
|
|
|
|
|
else
|
|
|
|
|
return tex_create(screen, templ, NULL);
|
2012-12-13 04:44:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static struct pipe_resource *
|
|
|
|
|
ilo_resource_from_handle(struct pipe_screen *screen,
|
|
|
|
|
const struct pipe_resource *templ,
|
|
|
|
|
struct winsys_handle *handle)
|
|
|
|
|
{
|
2013-05-13 15:19:55 +08:00
|
|
|
if (templ->target == PIPE_BUFFER)
|
|
|
|
|
return NULL;
|
|
|
|
|
else
|
|
|
|
|
return tex_create(screen, templ, handle);
|
2012-12-13 04:44:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static boolean
|
|
|
|
|
ilo_resource_get_handle(struct pipe_screen *screen,
|
2013-05-13 15:10:34 +08:00
|
|
|
struct pipe_resource *res,
|
2012-12-13 04:44:21 +08:00
|
|
|
struct winsys_handle *handle)
|
|
|
|
|
{
|
2013-05-13 15:19:55 +08:00
|
|
|
if (res->target == PIPE_BUFFER)
|
|
|
|
|
return false;
|
|
|
|
|
else
|
|
|
|
|
return tex_get_handle(ilo_texture(res), handle);
|
2012-12-13 04:44:21 +08:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
ilo_resource_destroy(struct pipe_screen *screen,
|
2013-05-13 15:10:34 +08:00
|
|
|
struct pipe_resource *res)
|
2012-12-13 04:44:21 +08:00
|
|
|
{
|
2013-05-13 15:19:55 +08:00
|
|
|
if (res->target == PIPE_BUFFER)
|
|
|
|
|
buf_destroy(ilo_buffer(res));
|
|
|
|
|
else
|
|
|
|
|
tex_destroy(ilo_texture(res));
|
2012-12-13 04:44:21 +08:00
|
|
|
}
|
|
|
|
|
|
2012-12-13 04:07:16 +08:00
|
|
|
/**
|
|
|
|
|
* Initialize resource-related functions.
|
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
ilo_init_resource_functions(struct ilo_screen *is)
|
|
|
|
|
{
|
2012-12-13 04:44:21 +08:00
|
|
|
is->base.can_create_resource = ilo_can_create_resource;
|
|
|
|
|
is->base.resource_create = ilo_resource_create;
|
|
|
|
|
is->base.resource_from_handle = ilo_resource_from_handle;
|
|
|
|
|
is->base.resource_get_handle = ilo_resource_get_handle;
|
|
|
|
|
is->base.resource_destroy = ilo_resource_destroy;
|
2012-12-13 04:07:16 +08:00
|
|
|
}
|
|
|
|
|
|
2013-05-13 15:19:55 +08:00
|
|
|
bool
|
2014-07-24 11:10:48 +08:00
|
|
|
ilo_buffer_rename_bo(struct ilo_buffer *buf)
|
2013-05-13 15:19:55 +08:00
|
|
|
{
|
2014-07-24 11:10:48 +08:00
|
|
|
struct intel_bo *old_bo = buf->bo;
|
|
|
|
|
|
|
|
|
|
if (buf_create_bo(buf)) {
|
|
|
|
|
intel_bo_unreference(old_bo);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
buf->bo = old_bo;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2013-05-13 15:19:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
2014-07-24 11:10:48 +08:00
|
|
|
ilo_texture_rename_bo(struct ilo_texture *tex)
|
2013-05-13 15:19:55 +08:00
|
|
|
{
|
2014-07-24 11:10:48 +08:00
|
|
|
struct intel_bo *old_bo = tex->bo;
|
|
|
|
|
|
|
|
|
|
/* an imported texture cannot be renamed */
|
2013-05-13 15:19:55 +08:00
|
|
|
if (tex->imported)
|
|
|
|
|
return false;
|
|
|
|
|
|
2014-07-24 11:10:48 +08:00
|
|
|
if (tex_create_bo(tex)) {
|
|
|
|
|
intel_bo_unreference(old_bo);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
tex->bo = old_bo;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2013-05-13 15:19:55 +08:00
|
|
|
}
|