1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2011 Red Hat All Rights Reserved.
3bf215546Sopenharmony_ci * Copyright © 2014 Advanced Micro Devices, Inc.
4bf215546Sopenharmony_ci * All Rights Reserved.
5bf215546Sopenharmony_ci *
6bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining
7bf215546Sopenharmony_ci * a copy of this software and associated documentation files (the
8bf215546Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
9bf215546Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
10bf215546Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to
11bf215546Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
12bf215546Sopenharmony_ci * the following conditions:
13bf215546Sopenharmony_ci *
14bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15bf215546Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16bf215546Sopenharmony_ci * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17bf215546Sopenharmony_ci * NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS, AUTHORS
18bf215546Sopenharmony_ci * AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20bf215546Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21bf215546Sopenharmony_ci * USE OR OTHER DEALINGS IN THE SOFTWARE.
22bf215546Sopenharmony_ci *
23bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the
24bf215546Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions
25bf215546Sopenharmony_ci * of the Software.
26bf215546Sopenharmony_ci */
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci#include "amdgpu_winsys.h"
29bf215546Sopenharmony_ci#include "util/format/u_format.h"
30bf215546Sopenharmony_ci
31bf215546Sopenharmony_cistatic int amdgpu_surface_sanity(const struct pipe_resource *tex)
32bf215546Sopenharmony_ci{
33bf215546Sopenharmony_ci   switch (tex->target) {
34bf215546Sopenharmony_ci   case PIPE_TEXTURE_1D:
35bf215546Sopenharmony_ci      if (tex->height0 > 1)
36bf215546Sopenharmony_ci         return -EINVAL;
37bf215546Sopenharmony_ci      FALLTHROUGH;
38bf215546Sopenharmony_ci   case PIPE_TEXTURE_2D:
39bf215546Sopenharmony_ci   case PIPE_TEXTURE_RECT:
40bf215546Sopenharmony_ci      if (tex->depth0 > 1 || tex->array_size > 1)
41bf215546Sopenharmony_ci         return -EINVAL;
42bf215546Sopenharmony_ci      break;
43bf215546Sopenharmony_ci   case PIPE_TEXTURE_3D:
44bf215546Sopenharmony_ci      if (tex->array_size > 1)
45bf215546Sopenharmony_ci         return -EINVAL;
46bf215546Sopenharmony_ci      break;
47bf215546Sopenharmony_ci   case PIPE_TEXTURE_1D_ARRAY:
48bf215546Sopenharmony_ci      if (tex->height0 > 1)
49bf215546Sopenharmony_ci         return -EINVAL;
50bf215546Sopenharmony_ci      FALLTHROUGH;
51bf215546Sopenharmony_ci   case PIPE_TEXTURE_CUBE:
52bf215546Sopenharmony_ci   case PIPE_TEXTURE_2D_ARRAY:
53bf215546Sopenharmony_ci   case PIPE_TEXTURE_CUBE_ARRAY:
54bf215546Sopenharmony_ci      if (tex->depth0 > 1)
55bf215546Sopenharmony_ci         return -EINVAL;
56bf215546Sopenharmony_ci      break;
57bf215546Sopenharmony_ci   default:
58bf215546Sopenharmony_ci      return -EINVAL;
59bf215546Sopenharmony_ci   }
60bf215546Sopenharmony_ci   return 0;
61bf215546Sopenharmony_ci}
62bf215546Sopenharmony_ci
63bf215546Sopenharmony_cistatic int amdgpu_surface_init(struct radeon_winsys *rws,
64bf215546Sopenharmony_ci                               const struct pipe_resource *tex,
65bf215546Sopenharmony_ci                               uint64_t flags, unsigned bpe,
66bf215546Sopenharmony_ci                               enum radeon_surf_mode mode,
67bf215546Sopenharmony_ci                               struct radeon_surf *surf)
68bf215546Sopenharmony_ci{
69bf215546Sopenharmony_ci   struct amdgpu_winsys *ws = amdgpu_winsys(rws);
70bf215546Sopenharmony_ci   int r;
71bf215546Sopenharmony_ci
72bf215546Sopenharmony_ci   r = amdgpu_surface_sanity(tex);
73bf215546Sopenharmony_ci   if (r)
74bf215546Sopenharmony_ci      return r;
75bf215546Sopenharmony_ci
76bf215546Sopenharmony_ci   surf->blk_w = util_format_get_blockwidth(tex->format);
77bf215546Sopenharmony_ci   surf->blk_h = util_format_get_blockheight(tex->format);
78bf215546Sopenharmony_ci   surf->bpe = bpe;
79bf215546Sopenharmony_ci   surf->flags = flags;
80bf215546Sopenharmony_ci
81bf215546Sopenharmony_ci   struct ac_surf_config config;
82bf215546Sopenharmony_ci
83bf215546Sopenharmony_ci   config.info.width = tex->width0;
84bf215546Sopenharmony_ci   config.info.height = tex->height0;
85bf215546Sopenharmony_ci   config.info.depth = tex->depth0;
86bf215546Sopenharmony_ci   config.info.array_size = tex->array_size;
87bf215546Sopenharmony_ci   config.info.samples = tex->nr_samples;
88bf215546Sopenharmony_ci   config.info.storage_samples = tex->nr_storage_samples;
89bf215546Sopenharmony_ci   config.info.levels = tex->last_level + 1;
90bf215546Sopenharmony_ci   config.info.num_channels = util_format_get_nr_components(tex->format);
91bf215546Sopenharmony_ci   config.is_1d = tex->target == PIPE_TEXTURE_1D ||
92bf215546Sopenharmony_ci                  tex->target == PIPE_TEXTURE_1D_ARRAY;
93bf215546Sopenharmony_ci   config.is_3d = tex->target == PIPE_TEXTURE_3D;
94bf215546Sopenharmony_ci   config.is_cube = tex->target == PIPE_TEXTURE_CUBE;
95bf215546Sopenharmony_ci
96bf215546Sopenharmony_ci   /* Use different surface counters for color and FMASK, so that MSAA MRTs
97bf215546Sopenharmony_ci    * always use consecutive surface indices when FMASK is allocated between
98bf215546Sopenharmony_ci    * them.
99bf215546Sopenharmony_ci    */
100bf215546Sopenharmony_ci   config.info.surf_index = &ws->surf_index_color;
101bf215546Sopenharmony_ci   config.info.fmask_surf_index = &ws->surf_index_fmask;
102bf215546Sopenharmony_ci
103bf215546Sopenharmony_ci   if (flags & RADEON_SURF_Z_OR_SBUFFER)
104bf215546Sopenharmony_ci      config.info.surf_index = NULL;
105bf215546Sopenharmony_ci
106bf215546Sopenharmony_ci   return ac_compute_surface(ws->addrlib, &ws->info, &config, mode, surf);
107bf215546Sopenharmony_ci}
108bf215546Sopenharmony_ci
109bf215546Sopenharmony_civoid amdgpu_surface_init_functions(struct amdgpu_screen_winsys *ws)
110bf215546Sopenharmony_ci{
111bf215546Sopenharmony_ci   ws->base.surface_init = amdgpu_surface_init;
112bf215546Sopenharmony_ci}
113