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