04b812ebf438957594980fcb211d085ddbebed09
[mesa.git] / src / gallium / drivers / freedreno / a4xx / fd4_resource.c
1 /*
2 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 * Authors:
24 * Rob Clark <robclark@freedesktop.org>
25 */
26
27 #include "fd4_resource.h"
28
29 static uint32_t
30 setup_slices(struct fd_resource *rsc, uint32_t alignment, enum pipe_format format)
31 {
32 struct pipe_resource *prsc = &rsc->base;
33 struct fd_screen *screen = fd_screen(prsc->screen);
34 enum util_format_layout layout = util_format_description(format)->layout;
35 uint32_t pitchalign = screen->gmem_alignw;
36 uint32_t level, size = 0;
37 uint32_t width = prsc->width0;
38 uint32_t height = prsc->height0;
39 uint32_t depth = prsc->depth0;
40 /* in layer_first layout, the level (slice) contains just one
41 * layer (since in fact the layer contains the slices)
42 */
43 uint32_t layers_in_level = rsc->layout.layer_first ? 1 : prsc->array_size;
44
45 for (level = 0; level <= prsc->last_level; level++) {
46 struct fdl_slice *slice = fd_resource_slice(rsc, level);
47 uint32_t blocks;
48
49 if (layout == UTIL_FORMAT_LAYOUT_ASTC)
50 width = util_align_npot(width, pitchalign * util_format_get_blockwidth(format));
51 else
52 width = align(width, pitchalign);
53 slice->pitch = util_format_get_nblocksx(format, width) * rsc->layout.cpp;
54 slice->offset = size;
55 blocks = util_format_get_nblocks(format, width, height);
56 /* 1d array and 2d array textures must all have the same layer size
57 * for each miplevel on a3xx. 3d textures can have different layer
58 * sizes for high levels, but the hw auto-sizer is buggy (or at least
59 * different than what this code does), so as soon as the layer size
60 * range gets into range, we stop reducing it.
61 */
62 if (prsc->target == PIPE_TEXTURE_3D && (
63 level == 1 ||
64 (level > 1 && fd_resource_slice(rsc, level - 1)->size0 > 0xf000)))
65 slice->size0 = align(blocks * rsc->layout.cpp, alignment);
66 else if (level == 0 || rsc->layout.layer_first || alignment == 1)
67 slice->size0 = align(blocks * rsc->layout.cpp, alignment);
68 else
69 slice->size0 = fd_resource_slice(rsc, level - 1)->size0;
70
71 size += slice->size0 * depth * layers_in_level;
72
73 width = u_minify(width, 1);
74 height = u_minify(height, 1);
75 depth = u_minify(depth, 1);
76 }
77
78 return size;
79 }
80
81 static uint32_t
82 slice_alignment(enum pipe_texture_target target)
83 {
84 /* on a3xx, 2d array and 3d textures seem to want their
85 * layers aligned to page boundaries:
86 */
87 switch (target) {
88 case PIPE_TEXTURE_3D:
89 case PIPE_TEXTURE_1D_ARRAY:
90 case PIPE_TEXTURE_2D_ARRAY:
91 return 4096;
92 default:
93 return 1;
94 }
95 }
96
97 /* cross generation texture layout to plug in to screen->setup_slices()..
98 * replace with generation specific one as-needed.
99 *
100 * TODO for a4xx probably can extract out the a4xx specific logic int
101 * a small fd4_setup_slices() wrapper that sets up layer_first, and then
102 * calls this.
103 */
104 uint32_t
105 fd4_setup_slices(struct fd_resource *rsc)
106 {
107 uint32_t alignment;
108
109 alignment = slice_alignment(rsc->base.target);
110
111 struct fd_screen *screen = fd_screen(rsc->base.screen);
112 if (is_a4xx(screen)) {
113 switch (rsc->base.target) {
114 case PIPE_TEXTURE_3D:
115 rsc->layout.layer_first = false;
116 break;
117 default:
118 rsc->layout.layer_first = true;
119 alignment = 1;
120 break;
121 }
122 }
123
124 return setup_slices(rsc, alignment, rsc->base.format);
125 }