2 * Copyright (c) 2012-2015 Etnaviv Project
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:
11 * The above copyright notice and this permission notice (including the
12 * next paragraph) shall be included in all copies or substantial portions
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.
24 * Wladimir J. van der Laan <laanwj@gmail.com>
27 #include "etnaviv_resource.h"
29 #include "hw/common.xml.h"
31 #include "etnaviv_context.h"
32 #include "etnaviv_debug.h"
33 #include "etnaviv_screen.h"
34 #include "etnaviv_translate.h"
36 #include "util/u_inlines.h"
37 #include "util/u_memory.h"
39 /* A tile is 4x4 pixels, having 'screen->specs.bits_per_tile' of tile status.
40 * So, in a buffer of N pixels, there are N / (4 * 4) tiles.
41 * We need N * screen->specs.bits_per_tile / (4 * 4) bits of tile status, or
42 * N * screen->specs.bits_per_tile / (4 * 4 * 8) bytes.
45 etna_screen_resource_alloc_ts(struct pipe_screen
*pscreen
,
46 struct etna_resource
*rsc
)
48 struct etna_screen
*screen
= etna_screen(pscreen
);
49 size_t rt_ts_size
, ts_layer_stride
, pixels
;
53 /* TS only for level 0 -- XXX is this formula correct? */
54 pixels
= rsc
->levels
[0].layer_stride
/ util_format_get_blocksize(rsc
->base
.format
);
55 ts_layer_stride
= align(pixels
* screen
->specs
.bits_per_tile
/ 0x80,
56 0x100 * screen
->specs
.pixel_pipes
);
57 rt_ts_size
= ts_layer_stride
* rsc
->base
.array_size
;
61 DBG_F(ETNA_DBG_RESOURCE_MSGS
, "%p: Allocating tile status of size %zu",
64 struct etna_bo
*rt_ts
;
65 rt_ts
= etna_bo_new(screen
->dev
, rt_ts_size
, DRM_ETNA_GEM_CACHE_WC
);
67 if (unlikely(!rt_ts
)) {
68 BUG("Problem allocating tile status for resource");
73 rsc
->levels
[0].ts_offset
= 0;
74 rsc
->levels
[0].ts_layer_stride
= ts_layer_stride
;
75 rsc
->levels
[0].ts_size
= rt_ts_size
;
77 /* It is important to initialize the TS, as random pattern
78 * can result in crashes. Do this on the CPU as this only happens once
79 * per surface anyway and it's a small area, so it may not be worth
80 * queuing this to the GPU. */
81 void *ts_map
= etna_bo_map(rt_ts
);
82 memset(ts_map
, screen
->specs
.ts_clear_value
, rt_ts_size
);
88 etna_screen_can_create_resource(struct pipe_screen
*pscreen
,
89 const struct pipe_resource
*templat
)
91 struct etna_screen
*screen
= etna_screen(pscreen
);
92 if (!translate_samples_to_xyscale(templat
->nr_samples
, NULL
, NULL
, NULL
))
95 /* templat->bind is not set here, so we must use the minimum sizes */
97 MIN2(screen
->specs
.max_rendertarget_size
, screen
->specs
.max_texture_size
);
99 if (templat
->width0
> max_size
|| templat
->height0
> max_size
)
106 setup_miptree(struct etna_resource
*rsc
, unsigned paddingX
, unsigned paddingY
,
107 unsigned msaa_xscale
, unsigned msaa_yscale
)
109 struct pipe_resource
*prsc
= &rsc
->base
;
110 unsigned level
, size
= 0;
111 unsigned width
= prsc
->width0
;
112 unsigned height
= prsc
->height0
;
113 unsigned depth
= prsc
->depth0
;
115 for (level
= 0; level
<= prsc
->last_level
; level
++) {
116 struct etna_resource_level
*mip
= &rsc
->levels
[level
];
119 mip
->height
= height
;
120 mip
->padded_width
= align(width
* msaa_xscale
, paddingX
);
121 mip
->padded_height
= align(height
* msaa_yscale
, paddingY
);
122 mip
->stride
= util_format_get_stride(prsc
->format
, mip
->padded_width
);
124 mip
->layer_stride
= mip
->stride
* util_format_get_nblocksy(prsc
->format
, mip
->padded_height
);
125 mip
->size
= prsc
->array_size
* mip
->layer_stride
;
127 /* align levels to 64 bytes to be able to render to them */
128 size
+= align(mip
->size
, ETNA_PE_ALIGNMENT
) * depth
;
130 width
= u_minify(width
, 1);
131 height
= u_minify(height
, 1);
132 depth
= u_minify(depth
, 1);
138 /* Create a new resource object, using the given template info */
139 struct pipe_resource
*
140 etna_resource_alloc(struct pipe_screen
*pscreen
, unsigned layout
,
141 const struct pipe_resource
*templat
)
143 struct etna_screen
*screen
= etna_screen(pscreen
);
146 DBG_F(ETNA_DBG_RESOURCE_MSGS
,
147 "target=%d, format=%s, %ux%ux%u, array_size=%u, "
148 "last_level=%u, nr_samples=%u, usage=%u, bind=%x, flags=%x",
149 templat
->target
, util_format_name(templat
->format
), templat
->width0
,
150 templat
->height0
, templat
->depth0
, templat
->array_size
,
151 templat
->last_level
, templat
->nr_samples
, templat
->usage
,
152 templat
->bind
, templat
->flags
);
154 /* Determine scaling for antialiasing, allow override using debug flag */
155 int nr_samples
= templat
->nr_samples
;
156 if ((templat
->bind
& (PIPE_BIND_RENDER_TARGET
| PIPE_BIND_DEPTH_STENCIL
)) &&
157 !(templat
->bind
& PIPE_BIND_SAMPLER_VIEW
)) {
158 if (DBG_ENABLED(ETNA_DBG_MSAA_2X
))
160 if (DBG_ENABLED(ETNA_DBG_MSAA_4X
))
164 int msaa_xscale
= 1, msaa_yscale
= 1;
165 if (!translate_samples_to_xyscale(nr_samples
, &msaa_xscale
, &msaa_yscale
, NULL
)) {
166 /* Number of samples not supported */
170 /* If we have the TEXTURE_HALIGN feature, we can always align to the
171 * resolve engine's width. If not, we must not align resources used
172 * only for textures. */
173 bool rs_align
= VIV_FEATURE(screen
, chipMinorFeatures1
, TEXTURE_HALIGN
) ||
174 !etna_resource_sampler_only(templat
);
176 /* Determine needed padding (alignment of height/width) */
177 unsigned paddingX
= 0, paddingY
= 0;
178 unsigned halign
= TEXTURE_HALIGN_FOUR
;
179 etna_layout_multiple(layout
, screen
->specs
.pixel_pipes
, rs_align
, &paddingX
,
181 assert(paddingX
&& paddingY
);
183 if (templat
->target
!= PIPE_BUFFER
)
184 etna_adjust_rs_align(screen
->specs
.pixel_pipes
, NULL
, &paddingY
);
186 struct etna_resource
*rsc
= CALLOC_STRUCT(etna_resource
);
191 rsc
->base
= *templat
;
192 rsc
->base
.screen
= pscreen
;
193 rsc
->base
.nr_samples
= nr_samples
;
194 rsc
->layout
= layout
;
195 rsc
->halign
= halign
;
197 pipe_reference_init(&rsc
->base
.reference
, 1);
198 list_inithead(&rsc
->list
);
200 size
= setup_miptree(rsc
, paddingX
, paddingY
, msaa_xscale
, msaa_yscale
);
202 uint32_t flags
= DRM_ETNA_GEM_CACHE_WC
;
203 if (templat
->bind
& PIPE_BIND_VERTEX_BUFFER
)
204 flags
|= DRM_ETNA_GEM_FORCE_MMU
;
205 struct etna_bo
*bo
= etna_bo_new(screen
->dev
, size
, flags
);
206 if (unlikely(bo
== NULL
)) {
207 BUG("Problem allocating video memory for resource");
212 rsc
->ts_bo
= 0; /* TS is only created when first bound to surface */
214 if (templat
->bind
& PIPE_BIND_SCANOUT
) {
215 struct winsys_handle handle
;
216 rsc
->scanout
= renderonly_scanout_for_resource(&rsc
->base
, screen
->ro
,
221 rsc
->external
= pscreen
->resource_from_handle(pscreen
, &rsc
->base
,
223 PIPE_HANDLE_USAGE_WRITE
);
224 close(handle
.handle
);
229 if (DBG_ENABLED(ETNA_DBG_ZERO
)) {
230 void *map
= etna_bo_map(bo
);
231 memset(map
, 0, size
);
241 static struct pipe_resource
*
242 etna_resource_create(struct pipe_screen
*pscreen
,
243 const struct pipe_resource
*templat
)
245 struct etna_screen
*screen
= etna_screen(pscreen
);
247 /* Figure out what tiling to use -- for now, assume that texture cannot be linear.
248 * there is a capability LINEAR_TEXTURE_SUPPORT (supported on gc880 and
249 * gc2000 at least), but not sure how it works.
250 * Buffers always have LINEAR layout.
252 unsigned layout
= ETNA_LAYOUT_LINEAR
;
253 if (etna_resource_sampler_only(templat
)) {
254 /* The buffer is only used for texturing, so create something
255 * directly compatible with the sampler. Such a buffer can
256 * never be rendered to. */
257 layout
= ETNA_LAYOUT_TILED
;
259 if (util_format_is_compressed(templat
->format
))
260 layout
= ETNA_LAYOUT_LINEAR
;
261 } else if (templat
->target
!= PIPE_BUFFER
) {
262 bool want_multitiled
= false;
263 bool want_supertiled
= screen
->specs
.can_supertile
;
265 /* When this GPU supports single-buffer rendering, don't ever enable
266 * multi-tiling. This replicates the blob behavior on GC3000.
268 if (!screen
->specs
.single_buffer
)
269 want_multitiled
= screen
->specs
.pixel_pipes
> 1;
271 /* Keep single byte blocksized resources as tiled, since we
272 * are unable to use the RS blit to de-tile them. However,
273 * if they're used as a render target or depth/stencil, they
274 * must be multi-tiled for GPUs with multiple pixel pipes.
275 * Ignore depth/stencil here, but it is an error for a render
278 if (util_format_get_blocksize(templat
->format
) == 1 &&
279 !(templat
->bind
& PIPE_BIND_DEPTH_STENCIL
)) {
280 assert(!(templat
->bind
& PIPE_BIND_RENDER_TARGET
&& want_multitiled
));
281 want_multitiled
= want_supertiled
= false;
284 layout
= ETNA_LAYOUT_BIT_TILE
;
286 layout
|= ETNA_LAYOUT_BIT_MULTI
;
288 layout
|= ETNA_LAYOUT_BIT_SUPER
;
291 if (templat
->target
== PIPE_TEXTURE_3D
)
292 layout
= ETNA_LAYOUT_LINEAR
;
294 return etna_resource_alloc(pscreen
, layout
, templat
);
298 etna_resource_changed(struct pipe_screen
*pscreen
, struct pipe_resource
*prsc
)
300 struct etna_resource
*res
= etna_resource(prsc
);
306 etna_resource_destroy(struct pipe_screen
*pscreen
, struct pipe_resource
*prsc
)
308 struct etna_resource
*rsc
= etna_resource(prsc
);
311 etna_bo_del(rsc
->bo
);
314 etna_bo_del(rsc
->ts_bo
);
317 renderonly_scanout_destroy(rsc
->scanout
, etna_screen(pscreen
)->ro
);
319 list_delinit(&rsc
->list
);
321 pipe_resource_reference(&rsc
->texture
, NULL
);
322 pipe_resource_reference(&rsc
->external
, NULL
);
327 static struct pipe_resource
*
328 etna_resource_from_handle(struct pipe_screen
*pscreen
,
329 const struct pipe_resource
*tmpl
,
330 struct winsys_handle
*handle
, unsigned usage
)
332 struct etna_screen
*screen
= etna_screen(pscreen
);
333 struct etna_resource
*rsc
;
334 struct etna_resource_level
*level
;
335 struct pipe_resource
*prsc
;
336 struct pipe_resource
*ptiled
= NULL
;
338 DBG("target=%d, format=%s, %ux%ux%u, array_size=%u, last_level=%u, "
339 "nr_samples=%u, usage=%u, bind=%x, flags=%x",
340 tmpl
->target
, util_format_name(tmpl
->format
), tmpl
->width0
,
341 tmpl
->height0
, tmpl
->depth0
, tmpl
->array_size
, tmpl
->last_level
,
342 tmpl
->nr_samples
, tmpl
->usage
, tmpl
->bind
, tmpl
->flags
);
344 rsc
= CALLOC_STRUCT(etna_resource
);
348 level
= &rsc
->levels
[0];
353 pipe_reference_init(&prsc
->reference
, 1);
354 list_inithead(&rsc
->list
);
355 prsc
->screen
= pscreen
;
357 rsc
->bo
= etna_screen_bo_from_handle(pscreen
, handle
, &level
->stride
);
363 level
->width
= tmpl
->width0
;
364 level
->height
= tmpl
->height0
;
366 /* We will be using the RS to copy with this resource, so we must
367 * ensure that it is appropriately aligned for the RS requirements. */
368 level
->padded_width
= level
->width
;
369 level
->padded_height
= level
->height
;
370 etna_adjust_rs_align(&level
->padded_width
, &level
->padded_height
);
372 level
->layer_stride
= level
->stride
* util_format_get_nblocksy(prsc
->format
,
373 level
->padded_height
);
375 /* The DDX must give us a BO which conforms to our padding size.
376 * The stride of the BO must be greater or equal to our padded
377 * stride. The size of the BO must accomodate the padded height. */
378 if (level
->stride
< util_format_get_stride(tmpl
->format
, level
->padded_width
)) {
379 BUG("BO stride is too small for RS engine width padding");
382 if (etna_bo_size(rsc
->bo
) < level
->stride
* level
->padded_height
) {
383 BUG("BO size is too small for RS engine height padding");
387 if (handle
->type
== DRM_API_HANDLE_TYPE_SHARED
&& tmpl
->bind
& PIPE_BIND_RENDER_TARGET
) {
388 /* Render targets are linear in Xorg but must be tiled
389 * here. It would be nice if dri_drawable_get_format()
390 * set scanout for these buffers too. */
392 ptiled
= etna_resource_create(pscreen
, tmpl
);
396 etna_resource(ptiled
)->external
= prsc
;
404 etna_resource_destroy(pscreen
, prsc
);
406 etna_resource_destroy(pscreen
, ptiled
);
412 etna_resource_get_handle(struct pipe_screen
*pscreen
,
413 struct pipe_context
*pctx
,
414 struct pipe_resource
*prsc
,
415 struct winsys_handle
*handle
, unsigned usage
)
417 struct etna_resource
*rsc
= etna_resource(prsc
);
418 /* Scanout is always attached to the base resource */
419 struct renderonly_scanout
*scanout
= rsc
->scanout
;
422 * External resources are preferred, so a import->export chain of
423 * render/sampler incompatible buffers yield the same handle.
426 rsc
= etna_resource(rsc
->external
);
428 if (handle
->type
== DRM_API_HANDLE_TYPE_KMS
&&
429 renderonly_get_handle(scanout
, handle
))
432 return etna_screen_bo_get_handle(pscreen
, rsc
->bo
, rsc
->levels
[0].stride
,
437 etna_resource_used(struct etna_context
*ctx
, struct pipe_resource
*prsc
,
438 enum etna_resource_status status
)
440 struct etna_resource
*rsc
;
445 rsc
= etna_resource(prsc
);
446 rsc
->status
|= status
;
448 /* TODO resources can actually be shared across contexts,
449 * so I'm not sure a single list-head will do the trick? */
450 debug_assert((rsc
->pending_ctx
== ctx
) || !rsc
->pending_ctx
);
451 list_delinit(&rsc
->list
);
452 list_addtail(&rsc
->list
, &ctx
->used_resources
);
453 rsc
->pending_ctx
= ctx
;
457 etna_resource_screen_init(struct pipe_screen
*pscreen
)
459 pscreen
->can_create_resource
= etna_screen_can_create_resource
;
460 pscreen
->resource_create
= etna_resource_create
;
461 pscreen
->resource_from_handle
= etna_resource_from_handle
;
462 pscreen
->resource_get_handle
= etna_resource_get_handle
;
463 pscreen
->resource_changed
= etna_resource_changed
;
464 pscreen
->resource_destroy
= etna_resource_destroy
;