49a7e0babec06f93c54876a1772294ea5abf35c8
[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 fdl_slice *slice = fd_resource_slice(rsc, level);
41 uint32_t blocks;
42
43 if (rsc->layout.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 slice->pitch = util_format_get_nblocksx(format, slice->pitch) *
67 rsc->layout.cpp;
68
69 slice->offset = size;
70 /* 1d array and 2d array textures must all have the same layer size
71 * for each miplevel on a3xx. 3d textures can have different layer
72 * sizes for high levels, but the hw auto-sizer is buggy (or at least
73 * different than what this code does), so as soon as the layer size
74 * range gets into range, we stop reducing it.
75 */
76 if (prsc->target == PIPE_TEXTURE_3D && (
77 level == 1 ||
78 (level > 1 && fd_resource_slice(rsc, level - 1)->size0 > 0xf000)))
79 slice->size0 = align(blocks * rsc->layout.cpp, alignment);
80 else if (level == 0 || alignment == 1)
81 slice->size0 = align(blocks * rsc->layout.cpp, alignment);
82 else
83 slice->size0 = fd_resource_slice(rsc, level - 1)->size0;
84
85 size += slice->size0 * depth * prsc->array_size;
86
87 width = u_minify(width, 1);
88 height = u_minify(height, 1);
89 depth = u_minify(depth, 1);
90 }
91
92 return size;
93 }
94
95 uint32_t
96 fd3_setup_slices(struct fd_resource *rsc)
97 {
98 uint32_t alignment;
99
100 switch (rsc->base.target) {
101 case PIPE_TEXTURE_3D:
102 case PIPE_TEXTURE_1D_ARRAY:
103 case PIPE_TEXTURE_2D_ARRAY:
104 alignment = 4096;
105 break;
106 default:
107 alignment = 1;
108 break;
109 }
110
111 return setup_slices(rsc, alignment, rsc->base.format);
112 }
113
114 static bool
115 ok_format(enum pipe_format pfmt)
116 {
117 enum a3xx_color_fmt fmt = fd3_pipe2color(pfmt);
118
119 if (fmt == RB_NONE)
120 return false;
121
122 switch (pfmt) {
123 case PIPE_FORMAT_R8_UINT:
124 case PIPE_FORMAT_R8_SINT:
125 case PIPE_FORMAT_Z32_FLOAT:
126 return false;
127 default:
128 break;
129 }
130
131 return true;
132 }
133
134 unsigned
135 fd3_tile_mode(const struct pipe_resource *tmpl)
136 {
137 if (ok_format(tmpl->format))
138 return TILE_4X4;
139 return LINEAR;
140 }