1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
3bf215546Sopenharmony_ci * Copyright (C) 2019 Khaled Emara <ekhaled1836@gmail.com>
4bf215546Sopenharmony_ci *
5bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
6bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
7bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
8bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
10bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
11bf215546Sopenharmony_ci *
12bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
13bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
14bf215546Sopenharmony_ci * Software.
15bf215546Sopenharmony_ci *
16bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21bf215546Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22bf215546Sopenharmony_ci * SOFTWARE.
23bf215546Sopenharmony_ci */
24bf215546Sopenharmony_ci
25bf215546Sopenharmony_ci#include "fd3_resource.h"
26bf215546Sopenharmony_ci#include "fd3_format.h"
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_cistatic uint32_t
29bf215546Sopenharmony_cisetup_slices(struct fd_resource *rsc, uint32_t alignment,
30bf215546Sopenharmony_ci             enum pipe_format format)
31bf215546Sopenharmony_ci{
32bf215546Sopenharmony_ci   struct pipe_resource *prsc = &rsc->b.b;
33bf215546Sopenharmony_ci   uint32_t level, size = 0;
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_ci   /* 32 pixel alignment */
36bf215546Sopenharmony_ci   fdl_set_pitchalign(&rsc->layout, fdl_cpp_shift(&rsc->layout) + 5);
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_ci   for (level = 0; level <= prsc->last_level; level++) {
39bf215546Sopenharmony_ci      struct fdl_slice *slice = fd_resource_slice(rsc, level);
40bf215546Sopenharmony_ci      uint32_t pitch = fdl_pitch(&rsc->layout, level);
41bf215546Sopenharmony_ci      uint32_t height = u_minify(prsc->height0, level);
42bf215546Sopenharmony_ci      if (rsc->layout.tile_mode) {
43bf215546Sopenharmony_ci         height = align(height, 4);
44bf215546Sopenharmony_ci         if (prsc->target != PIPE_TEXTURE_CUBE)
45bf215546Sopenharmony_ci            height = util_next_power_of_two(height);
46bf215546Sopenharmony_ci      }
47bf215546Sopenharmony_ci
48bf215546Sopenharmony_ci      uint32_t nblocksy = util_format_get_nblocksy(format, height);
49bf215546Sopenharmony_ci
50bf215546Sopenharmony_ci      slice->offset = size;
51bf215546Sopenharmony_ci      /* 1d array and 2d array textures must all have the same layer size
52bf215546Sopenharmony_ci       * for each miplevel on a3xx. 3d textures can have different layer
53bf215546Sopenharmony_ci       * sizes for high levels, but the hw auto-sizer is buggy (or at least
54bf215546Sopenharmony_ci       * different than what this code does), so as soon as the layer size
55bf215546Sopenharmony_ci       * range gets into range, we stop reducing it.
56bf215546Sopenharmony_ci       */
57bf215546Sopenharmony_ci      if (prsc->target == PIPE_TEXTURE_3D &&
58bf215546Sopenharmony_ci          (level == 1 ||
59bf215546Sopenharmony_ci           (level > 1 && fd_resource_slice(rsc, level - 1)->size0 > 0xf000)))
60bf215546Sopenharmony_ci         slice->size0 = align(nblocksy * pitch, alignment);
61bf215546Sopenharmony_ci      else if (level == 0 || alignment == 1)
62bf215546Sopenharmony_ci         slice->size0 = align(nblocksy * pitch, alignment);
63bf215546Sopenharmony_ci      else
64bf215546Sopenharmony_ci         slice->size0 = fd_resource_slice(rsc, level - 1)->size0;
65bf215546Sopenharmony_ci
66bf215546Sopenharmony_ci      size += slice->size0 * u_minify(prsc->depth0, level) * prsc->array_size;
67bf215546Sopenharmony_ci   }
68bf215546Sopenharmony_ci
69bf215546Sopenharmony_ci   return size;
70bf215546Sopenharmony_ci}
71bf215546Sopenharmony_ci
72bf215546Sopenharmony_ciuint32_t
73bf215546Sopenharmony_cifd3_setup_slices(struct fd_resource *rsc)
74bf215546Sopenharmony_ci{
75bf215546Sopenharmony_ci   uint32_t alignment;
76bf215546Sopenharmony_ci
77bf215546Sopenharmony_ci   switch (rsc->b.b.target) {
78bf215546Sopenharmony_ci   case PIPE_TEXTURE_3D:
79bf215546Sopenharmony_ci   case PIPE_TEXTURE_1D_ARRAY:
80bf215546Sopenharmony_ci   case PIPE_TEXTURE_2D_ARRAY:
81bf215546Sopenharmony_ci      alignment = 4096;
82bf215546Sopenharmony_ci      break;
83bf215546Sopenharmony_ci   default:
84bf215546Sopenharmony_ci      alignment = 1;
85bf215546Sopenharmony_ci      break;
86bf215546Sopenharmony_ci   }
87bf215546Sopenharmony_ci
88bf215546Sopenharmony_ci   return setup_slices(rsc, alignment, rsc->b.b.format);
89bf215546Sopenharmony_ci}
90bf215546Sopenharmony_ci
91bf215546Sopenharmony_cistatic bool
92bf215546Sopenharmony_ciok_format(enum pipe_format pfmt)
93bf215546Sopenharmony_ci{
94bf215546Sopenharmony_ci   enum a3xx_color_fmt fmt = fd3_pipe2color(pfmt);
95bf215546Sopenharmony_ci
96bf215546Sopenharmony_ci   if (fmt == RB_NONE)
97bf215546Sopenharmony_ci      return false;
98bf215546Sopenharmony_ci
99bf215546Sopenharmony_ci   switch (pfmt) {
100bf215546Sopenharmony_ci   case PIPE_FORMAT_R8_UINT:
101bf215546Sopenharmony_ci   case PIPE_FORMAT_R8_SINT:
102bf215546Sopenharmony_ci   case PIPE_FORMAT_Z32_FLOAT:
103bf215546Sopenharmony_ci      return false;
104bf215546Sopenharmony_ci   default:
105bf215546Sopenharmony_ci      break;
106bf215546Sopenharmony_ci   }
107bf215546Sopenharmony_ci
108bf215546Sopenharmony_ci   return true;
109bf215546Sopenharmony_ci}
110bf215546Sopenharmony_ci
111bf215546Sopenharmony_ciunsigned
112bf215546Sopenharmony_cifd3_tile_mode(const struct pipe_resource *tmpl)
113bf215546Sopenharmony_ci{
114bf215546Sopenharmony_ci   if (ok_format(tmpl->format))
115bf215546Sopenharmony_ci      return TILE_4X4;
116bf215546Sopenharmony_ci   return LINEAR;
117bf215546Sopenharmony_ci}
118