freedreno: use rsc->slice accessor everywhere
[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 struct fd_screen *screen = fd_screen(prsc->screen);
33 uint32_t pitchalign = screen->gmem_alignw;
34 uint32_t level, size = 0;
35 uint32_t width = prsc->width0;
36 uint32_t height = prsc->height0;
37 uint32_t depth = prsc->depth0;
38
39 for (level = 0; level <= prsc->last_level; level++) {
40 struct fd_resource_slice *slice = fd_resource_slice(rsc, level);
41 uint32_t blocks;
42
43 if (rsc->tile_mode) {
44 if (prsc->target != PIPE_TEXTURE_CUBE) {
45 if (level == 0) {
46 width = util_next_power_of_two(width);
47 height = util_next_power_of_two(height);
48 }
49 width = MAX2(width, 8);
50 height = MAX2(height, 4);
51 // Multiplying by 4 is the result of the 4x4 tiling pattern.
52 slice->pitch = width * 4;
53 blocks = util_format_get_nblocks(format, width, height);
54 } else {
55 uint32_t twidth, theight;
56 twidth = align(width, 8);
57 theight = align(height, 4);
58 // Multiplying by 4 is the result of the 4x4 tiling pattern.
59 slice->pitch = twidth * 4;
60 blocks = util_format_get_nblocks(format, twidth, theight);
61 }
62 } else {
63 slice->pitch = width = align(width, pitchalign);
64 blocks = util_format_get_nblocks(format, slice->pitch, height);
65 }
66
67 slice->offset = size;
68 /* 1d array and 2d array textures must all have the same layer size
69 * for each miplevel on a3xx. 3d textures can have different layer
70 * sizes for high levels, but the hw auto-sizer is buggy (or at least
71 * different than what this code does), so as soon as the layer size
72 * range gets into range, we stop reducing it.
73 */
74 if (prsc->target == PIPE_TEXTURE_3D && (
75 level == 1 ||
76 (level > 1 && fd_resource_slice(rsc, level - 1)->size0 > 0xf000)))
77 slice->size0 = align(blocks * rsc->cpp, alignment);
78 else if (level == 0 || alignment == 1)
79 slice->size0 = align(blocks * rsc->cpp, alignment);
80 else
81 slice->size0 = fd_resource_slice(rsc, level - 1)->size0;
82
83 size += slice->size0 * depth * prsc->array_size;
84
85 width = u_minify(width, 1);
86 height = u_minify(height, 1);
87 depth = u_minify(depth, 1);
88 }
89
90 return size;
91 }
92
93 uint32_t
94 fd3_setup_slices(struct fd_resource *rsc)
95 {
96 uint32_t alignment;
97
98 switch (rsc->base.target) {
99 case PIPE_TEXTURE_3D:
100 case PIPE_TEXTURE_1D_ARRAY:
101 case PIPE_TEXTURE_2D_ARRAY:
102 alignment = 4096;
103 break;
104 default:
105 alignment = 1;
106 break;
107 }
108
109 return setup_slices(rsc, alignment, rsc->base.format);
110 }
111
112 static bool
113 ok_format(enum pipe_format pfmt)
114 {
115 enum a3xx_color_fmt fmt = fd3_pipe2color(pfmt);
116
117 if (fmt == ~0)
118 return false;
119
120 switch (pfmt) {
121 case PIPE_FORMAT_R8_UINT:
122 case PIPE_FORMAT_R8_SINT:
123 case PIPE_FORMAT_Z32_FLOAT:
124 return false;
125 default:
126 break;
127 }
128
129 return true;
130 }
131
132 unsigned
133 fd3_tile_mode(const struct pipe_resource *tmpl)
134 {
135 if (ok_format(tmpl->format))
136 return TILE_4X4;
137 return LINEAR;
138 }