1 /*
2 * Copyright (c) 2012-2013 Etnaviv Project
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, sub license,
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
12 * next paragraph) shall be included in all copies or substantial portions
13 * of the 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 NON-INFRINGEMENT. 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
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Wladimir J. van der Laan <laanwj@gmail.com>
25 */
26
27 #include "etnaviv_surface.h"
28 #include "etnaviv_screen.h"
29
30 #include "etnaviv_clear_blit.h"
31 #include "etnaviv_context.h"
32 #include "etnaviv_translate.h"
33 #include "pipe/p_defines.h"
34 #include "pipe/p_state.h"
35 #include "util/u_inlines.h"
36 #include "util/u_math.h"
37 #include "util/u_memory.h"
38
39 #include "hw/common.xml.h"
40
41 #include "drm-uapi/drm_fourcc.h"
42
43 static struct etna_resource *
etna_render_handle_incompatible(struct pipe_context * pctx,struct pipe_resource * prsc,unsigned int level)44 etna_render_handle_incompatible(struct pipe_context *pctx,
45 struct pipe_resource *prsc,
46 unsigned int level)
47 {
48 struct etna_context *ctx = etna_context(pctx);
49 struct etna_screen *screen = ctx->screen;
50 struct etna_resource *res = etna_resource(prsc);
51 bool need_multitiled = screen->specs.pixel_pipes > 1 && !screen->specs.single_buffer;
52 bool want_supertiled = screen->specs.can_supertile;
53 unsigned int min_tilesize = etna_screen_get_tile_size(screen, TS_MODE_128B,
54 prsc->nr_samples > 1);
55
56 /* Resource is compatible if it is tiled or PE is able to render to linear
57 * and has multi tiling when required.
58 */
59 if ((res->layout != ETNA_LAYOUT_LINEAR ||
60 (VIV_FEATURE(screen, chipMinorFeatures2, LINEAR_PE) &&
61 (!VIV_FEATURE(screen, chipFeatures, FAST_CLEAR) ||
62 res->levels[level].stride % min_tilesize == 0))) &&
63 (!need_multitiled || (res->layout & ETNA_LAYOUT_BIT_MULTI)))
64 return res;
65
66 if (!res->render) {
67 struct pipe_resource templat = *prsc;
68 unsigned layout = ETNA_LAYOUT_TILED;
69 if (need_multitiled)
70 layout |= ETNA_LAYOUT_BIT_MULTI;
71 if (want_supertiled)
72 layout |= ETNA_LAYOUT_BIT_SUPER;
73
74 templat.bind &= (PIPE_BIND_DEPTH_STENCIL | PIPE_BIND_RENDER_TARGET |
75 PIPE_BIND_BLENDABLE);
76 res->render =
77 etna_resource_alloc(pctx->screen, layout,
78 DRM_FORMAT_MOD_LINEAR, &templat);
79 assert(res->render);
80 }
81 return etna_resource(res->render);
82 }
83
84 static struct pipe_surface *
etna_create_surface(struct pipe_context * pctx,struct pipe_resource * prsc,const struct pipe_surface * templat)85 etna_create_surface(struct pipe_context *pctx, struct pipe_resource *prsc,
86 const struct pipe_surface *templat)
87 {
88 struct etna_context *ctx = etna_context(pctx);
89 struct etna_screen *screen = ctx->screen;
90 unsigned layer = templat->u.tex.first_layer;
91 unsigned level = templat->u.tex.level;
92 struct etna_resource *rsc = etna_render_handle_incompatible(pctx, prsc, level);
93 struct etna_resource_level *lev = &rsc->levels[level];
94 struct etna_surface *surf = CALLOC_STRUCT(etna_surface);
95
96 if (!surf)
97 return NULL;
98
99 assert(templat->u.tex.first_layer == templat->u.tex.last_layer);
100 assert(layer <= util_max_layer(prsc, level));
101
102 surf->base.context = pctx;
103
104 pipe_reference_init(&surf->base.reference, 1);
105 pipe_resource_reference(&surf->base.texture, &rsc->base);
106 pipe_resource_reference(&surf->prsc, prsc);
107
108 /* Allocate a TS for the resource if there isn't one yet,
109 * and it is allowed by the hw (width is a multiple of 16).
110 * Avoid doing this for GPUs with MC1.0, as kernel sources
111 * indicate the tile status module bypasses the memory
112 * offset and MMU. */
113
114 if (VIV_FEATURE(screen, chipFeatures, FAST_CLEAR) &&
115 !rsc->ts_bo &&
116 /* needs to be RS/BLT compatible for transfer_map/unmap */
117 (rsc->levels[level].padded_width & ETNA_RS_WIDTH_MASK) == 0 &&
118 (rsc->levels[level].padded_height & ETNA_RS_HEIGHT_MASK) == 0 &&
119 etna_resource_hw_tileable(screen->specs.use_blt, prsc) &&
120 /* Multi-layer resources would need to keep much more state (TS valid and
121 * clear color per layer) and are unlikely to profit from TS usage. */
122 prsc->depth0 == 1 && prsc->array_size == 1) {
123 etna_screen_resource_alloc_ts(pctx->screen, rsc, 0);
124 }
125
126 surf->base.format = templat->format;
127 surf->base.width = rsc->levels[level].width;
128 surf->base.height = rsc->levels[level].height;
129 surf->base.writable = templat->writable; /* what is this for anyway */
130 surf->base.u = templat->u;
131 surf->level = lev;
132
133 /* XXX we don't really need a copy but it's convenient */
134 surf->offset = lev->offset + layer * lev->layer_stride;
135
136 /* Setup template relocations for this surface */
137 for (unsigned pipe = 0; pipe < screen->specs.pixel_pipes; ++pipe) {
138 surf->reloc[pipe].bo = rsc->bo;
139 surf->reloc[pipe].offset = surf->offset;
140 surf->reloc[pipe].flags = 0;
141 }
142
143 /* In single buffer mode, both pixel pipes must point to the same address,
144 * for multi-tiled surfaces on the other hand the second pipe is expected to
145 * point halfway the image vertically.
146 */
147 if (rsc->layout & ETNA_LAYOUT_BIT_MULTI)
148 surf->reloc[1].offset = surf->offset + lev->stride * lev->padded_height / 2;
149
150 if (surf->level->ts_size) {
151 unsigned int layer_offset = layer * lev->ts_layer_stride;
152 assert(layer_offset < surf->level->ts_size);
153
154 surf->ts_offset = surf->level->ts_offset + layer_offset;
155
156 surf->ts_reloc.bo = rsc->ts_bo;
157 surf->ts_reloc.offset = surf->ts_offset;
158 surf->ts_reloc.flags = 0;
159
160 if (!screen->specs.use_blt) {
161 /* This (ab)uses the RS as a plain buffer memset().
162 * Currently uses a fixed row size of 64 bytes. Some benchmarking with
163 * different sizes may be in order. */
164 struct etna_bo *ts_bo = etna_resource(surf->base.texture)->ts_bo;
165 etna_compile_rs_state(ctx, &surf->clear_command, &(struct rs_state) {
166 .source_format = RS_FORMAT_A8R8G8B8,
167 .dest_format = RS_FORMAT_A8R8G8B8,
168 .dest = ts_bo,
169 .dest_offset = surf->ts_offset,
170 .dest_stride = 0x40,
171 .dest_tiling = ETNA_LAYOUT_TILED,
172 .dither = {0xffffffff, 0xffffffff},
173 .width = 16,
174 .height = align(lev->ts_layer_stride / 0x40, 4),
175 .clear_value = {screen->specs.ts_clear_value},
176 .clear_mode = VIVS_RS_CLEAR_CONTROL_MODE_ENABLED1,
177 .clear_bits = 0xffff
178 });
179 }
180 }
181
182 return &surf->base;
183 }
184
185 static void
etna_surface_destroy(struct pipe_context * pctx,struct pipe_surface * psurf)186 etna_surface_destroy(struct pipe_context *pctx, struct pipe_surface *psurf)
187 {
188 pipe_resource_reference(&psurf->texture, NULL);
189 pipe_resource_reference(&etna_surface(psurf)->prsc, NULL);
190 FREE(psurf);
191 }
192
193 void
etna_surface_init(struct pipe_context * pctx)194 etna_surface_init(struct pipe_context *pctx)
195 {
196 pctx->create_surface = etna_create_surface;
197 pctx->surface_destroy = etna_surface_destroy;
198 }
199