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