1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
4 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * Rob Clark <robclark@freedesktop.org>
29 #include "util/u_format.h"
30 #include "util/u_inlines.h"
31 #include "util/u_transfer.h"
32 #include "util/u_string.h"
33 #include "util/u_surface.h"
35 #include "freedreno_resource.h"
36 #include "freedreno_screen.h"
37 #include "freedreno_surface.h"
38 #include "freedreno_context.h"
39 #include "freedreno_query_hw.h"
40 #include "freedreno_util.h"
45 fd_invalidate_resource(struct fd_context
*ctx
, struct pipe_resource
*prsc
)
49 /* Go through the entire state and see if the resource is bound
50 * anywhere. If it is, mark the relevant state as dirty. This is called on
55 for (i
= 1; i
< PIPE_MAX_CONSTANT_BUFFERS
&& !(ctx
->dirty
& FD_DIRTY_CONSTBUF
); i
++) {
56 if (ctx
->constbuf
[PIPE_SHADER_VERTEX
].cb
[i
].buffer
== prsc
)
57 ctx
->dirty
|= FD_DIRTY_CONSTBUF
;
58 if (ctx
->constbuf
[PIPE_SHADER_FRAGMENT
].cb
[i
].buffer
== prsc
)
59 ctx
->dirty
|= FD_DIRTY_CONSTBUF
;
63 for (i
= 0; i
< ctx
->vtx
.vertexbuf
.count
&& !(ctx
->dirty
& FD_DIRTY_VTXBUF
); i
++) {
64 if (ctx
->vtx
.vertexbuf
.vb
[i
].buffer
== prsc
)
65 ctx
->dirty
|= FD_DIRTY_VTXBUF
;
69 if (ctx
->indexbuf
.buffer
== prsc
)
70 ctx
->dirty
|= FD_DIRTY_INDEXBUF
;
73 for (i
= 0; i
< ctx
->verttex
.num_textures
&& !(ctx
->dirty
& FD_DIRTY_VERTTEX
); i
++) {
74 if (ctx
->verttex
.textures
[i
]->texture
== prsc
)
75 ctx
->dirty
|= FD_DIRTY_VERTTEX
;
77 for (i
= 0; i
< ctx
->fragtex
.num_textures
&& !(ctx
->dirty
& FD_DIRTY_FRAGTEX
); i
++) {
78 if (ctx
->fragtex
.textures
[i
]->texture
== prsc
)
79 ctx
->dirty
|= FD_DIRTY_FRAGTEX
;
84 realloc_bo(struct fd_resource
*rsc
, uint32_t size
)
86 struct fd_screen
*screen
= fd_screen(rsc
->base
.b
.screen
);
87 uint32_t flags
= DRM_FREEDRENO_GEM_CACHE_WCOMBINE
|
88 DRM_FREEDRENO_GEM_TYPE_KMEM
; /* TODO */
90 /* if we start using things other than write-combine,
91 * be sure to check for PIPE_RESOURCE_FLAG_MAP_COHERENT
97 rsc
->bo
= fd_bo_new(screen
->dev
, size
, flags
);
99 rsc
->dirty
= rsc
->reading
= false;
100 list_delinit(&rsc
->list
);
101 util_range_set_empty(&rsc
->valid_buffer_range
);
104 static void fd_resource_transfer_flush_region(struct pipe_context
*pctx
,
105 struct pipe_transfer
*ptrans
,
106 const struct pipe_box
*box
)
108 struct fd_resource
*rsc
= fd_resource(ptrans
->resource
);
110 if (ptrans
->resource
->target
== PIPE_BUFFER
)
111 util_range_add(&rsc
->valid_buffer_range
,
112 ptrans
->box
.x
+ box
->x
,
113 ptrans
->box
.x
+ box
->x
+ box
->width
);
117 fd_resource_transfer_unmap(struct pipe_context
*pctx
,
118 struct pipe_transfer
*ptrans
)
120 struct fd_context
*ctx
= fd_context(pctx
);
121 struct fd_resource
*rsc
= fd_resource(ptrans
->resource
);
122 if (!(ptrans
->usage
& PIPE_TRANSFER_UNSYNCHRONIZED
))
123 fd_bo_cpu_fini(rsc
->bo
);
125 util_range_add(&rsc
->valid_buffer_range
,
127 ptrans
->box
.x
+ ptrans
->box
.width
);
129 pipe_resource_reference(&ptrans
->resource
, NULL
);
130 util_slab_free(&ctx
->transfer_pool
, ptrans
);
134 fd_resource_transfer_map(struct pipe_context
*pctx
,
135 struct pipe_resource
*prsc
,
136 unsigned level
, unsigned usage
,
137 const struct pipe_box
*box
,
138 struct pipe_transfer
**pptrans
)
140 struct fd_context
*ctx
= fd_context(pctx
);
141 struct fd_resource
*rsc
= fd_resource(prsc
);
142 struct fd_resource_slice
*slice
= fd_resource_slice(rsc
, level
);
143 struct pipe_transfer
*ptrans
;
144 enum pipe_format format
= prsc
->format
;
150 DBG("prsc=%p, level=%u, usage=%x", prsc
, level
, usage
);
152 ptrans
= util_slab_alloc(&ctx
->transfer_pool
);
156 /* util_slab_alloc() doesn't zero: */
157 memset(ptrans
, 0, sizeof(*ptrans
));
159 pipe_resource_reference(&ptrans
->resource
, prsc
);
160 ptrans
->level
= level
;
161 ptrans
->usage
= usage
;
163 ptrans
->stride
= slice
->pitch
* rsc
->cpp
;
164 ptrans
->layer_stride
= slice
->size0
;
166 if (usage
& PIPE_TRANSFER_READ
)
167 op
|= DRM_FREEDRENO_PREP_READ
;
169 if (usage
& PIPE_TRANSFER_WRITE
)
170 op
|= DRM_FREEDRENO_PREP_WRITE
;
172 if (usage
& PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
) {
173 realloc_bo(rsc
, fd_bo_size(rsc
->bo
));
174 fd_invalidate_resource(ctx
, prsc
);
175 } else if ((usage
& PIPE_TRANSFER_WRITE
) &&
176 prsc
->target
== PIPE_BUFFER
&&
177 !util_ranges_intersect(&rsc
->valid_buffer_range
,
178 box
->x
, box
->x
+ box
->width
)) {
179 /* We are trying to write to a previously uninitialized range. No need
182 } else if (!(usage
& PIPE_TRANSFER_UNSYNCHRONIZED
)) {
183 /* If the GPU is writing to the resource, or if it is reading from the
184 * resource and we're trying to write to it, flush the renders.
187 ((ptrans
->usage
& PIPE_TRANSFER_WRITE
) && rsc
->reading
))
188 fd_context_render(pctx
);
190 /* The GPU keeps track of how the various bo's are being used, and
191 * will wait if necessary for the proper operation to have
194 ret
= fd_bo_cpu_prep(rsc
->bo
, ctx
->screen
->pipe
, op
);
199 buf
= fd_bo_map(rsc
->bo
);
201 fd_resource_transfer_unmap(pctx
, ptrans
);
207 if (rsc
->layer_first
) {
208 offset
= slice
->offset
+
209 box
->y
/ util_format_get_blockheight(format
) * ptrans
->stride
+
210 box
->x
/ util_format_get_blockwidth(format
) * rsc
->cpp
+
211 box
->z
* rsc
->layer_size
;
213 offset
= slice
->offset
+
214 box
->y
/ util_format_get_blockheight(format
) * ptrans
->stride
+
215 box
->x
/ util_format_get_blockwidth(format
) * rsc
->cpp
+
216 box
->z
* slice
->size0
;
222 fd_resource_transfer_unmap(pctx
, ptrans
);
227 fd_resource_destroy(struct pipe_screen
*pscreen
,
228 struct pipe_resource
*prsc
)
230 struct fd_resource
*rsc
= fd_resource(prsc
);
233 list_delinit(&rsc
->list
);
234 util_range_destroy(&rsc
->valid_buffer_range
);
239 fd_resource_get_handle(struct pipe_screen
*pscreen
,
240 struct pipe_resource
*prsc
,
241 struct winsys_handle
*handle
)
243 struct fd_resource
*rsc
= fd_resource(prsc
);
245 return fd_screen_bo_get_handle(pscreen
, rsc
->bo
,
246 rsc
->slices
[0].pitch
* rsc
->cpp
, handle
);
250 static const struct u_resource_vtbl fd_resource_vtbl
= {
251 .resource_get_handle
= fd_resource_get_handle
,
252 .resource_destroy
= fd_resource_destroy
,
253 .transfer_map
= fd_resource_transfer_map
,
254 .transfer_flush_region
= fd_resource_transfer_flush_region
,
255 .transfer_unmap
= fd_resource_transfer_unmap
,
256 .transfer_inline_write
= u_default_transfer_inline_write
,
260 setup_slices(struct fd_resource
*rsc
, uint32_t alignment
)
262 struct pipe_resource
*prsc
= &rsc
->base
.b
;
263 uint32_t level
, size
= 0;
264 uint32_t width
= prsc
->width0
;
265 uint32_t height
= prsc
->height0
;
266 uint32_t depth
= prsc
->depth0
;
267 /* in layer_first layout, the level (slice) contains just one
268 * layer (since in fact the layer contains the slices)
270 uint32_t layers_in_level
= rsc
->layer_first
? 1 : prsc
->array_size
;
272 for (level
= 0; level
<= prsc
->last_level
; level
++) {
273 struct fd_resource_slice
*slice
= fd_resource_slice(rsc
, level
);
275 slice
->pitch
= width
= align(width
, 32);
276 slice
->offset
= size
;
277 /* 1d array and 2d array textures must all have the same layer size
278 * for each miplevel on a3xx. 3d textures can have different layer
279 * sizes for high levels, but the hw auto-sizer is buggy (or at least
280 * different than what this code does), so as soon as the layer size
281 * range gets into range, we stop reducing it.
283 if (prsc
->target
== PIPE_TEXTURE_3D
&& (
285 (level
> 1 && rsc
->slices
[level
- 1].size0
> 0xf000)))
286 slice
->size0
= align(slice
->pitch
* height
* rsc
->cpp
, alignment
);
287 else if (level
== 0 || rsc
->layer_first
|| alignment
== 1)
288 slice
->size0
= align(slice
->pitch
* height
* rsc
->cpp
, alignment
);
290 slice
->size0
= rsc
->slices
[level
- 1].size0
;
292 size
+= slice
->size0
* depth
* layers_in_level
;
294 width
= u_minify(width
, 1);
295 height
= u_minify(height
, 1);
296 depth
= u_minify(depth
, 1);
303 slice_alignment(struct pipe_screen
*pscreen
, const struct pipe_resource
*tmpl
)
305 /* on a3xx, 2d array and 3d textures seem to want their
306 * layers aligned to page boundaries:
308 switch (tmpl
->target
) {
309 case PIPE_TEXTURE_3D
:
310 case PIPE_TEXTURE_1D_ARRAY
:
311 case PIPE_TEXTURE_2D_ARRAY
:
319 * Create a new texture object, using the given template info.
321 static struct pipe_resource
*
322 fd_resource_create(struct pipe_screen
*pscreen
,
323 const struct pipe_resource
*tmpl
)
325 struct fd_resource
*rsc
= CALLOC_STRUCT(fd_resource
);
326 struct pipe_resource
*prsc
= &rsc
->base
.b
;
329 DBG("target=%d, format=%s, %ux%ux%u, array_size=%u, last_level=%u, "
330 "nr_samples=%u, usage=%u, bind=%x, flags=%x",
331 tmpl
->target
, util_format_name(tmpl
->format
),
332 tmpl
->width0
, tmpl
->height0
, tmpl
->depth0
,
333 tmpl
->array_size
, tmpl
->last_level
, tmpl
->nr_samples
,
334 tmpl
->usage
, tmpl
->bind
, tmpl
->flags
);
341 pipe_reference_init(&prsc
->reference
, 1);
342 list_inithead(&rsc
->list
);
343 prsc
->screen
= pscreen
;
345 util_range_init(&rsc
->valid_buffer_range
);
347 rsc
->base
.vtbl
= &fd_resource_vtbl
;
348 rsc
->cpp
= util_format_get_blocksize(tmpl
->format
);
352 if (is_a4xx(fd_screen(pscreen
))) {
353 switch (tmpl
->target
) {
354 case PIPE_TEXTURE_3D
:
356 rsc
->layer_first
= false;
359 rsc
->layer_first
= true;
364 size
= setup_slices(rsc
, slice_alignment(pscreen
, tmpl
));
366 if (rsc
->layer_first
) {
367 rsc
->layer_size
= align(size
, 4096);
368 size
= rsc
->layer_size
* prsc
->array_size
;
371 realloc_bo(rsc
, size
);
377 fd_resource_destroy(pscreen
, prsc
);
382 * Create a texture from a winsys_handle. The handle is often created in
383 * another process by first creating a pipe texture and then calling
384 * resource_get_handle.
386 static struct pipe_resource
*
387 fd_resource_from_handle(struct pipe_screen
*pscreen
,
388 const struct pipe_resource
*tmpl
,
389 struct winsys_handle
*handle
)
391 struct fd_resource
*rsc
= CALLOC_STRUCT(fd_resource
);
392 struct fd_resource_slice
*slice
= &rsc
->slices
[0];
393 struct pipe_resource
*prsc
= &rsc
->base
.b
;
395 DBG("target=%d, format=%s, %ux%ux%u, array_size=%u, last_level=%u, "
396 "nr_samples=%u, usage=%u, bind=%x, flags=%x",
397 tmpl
->target
, util_format_name(tmpl
->format
),
398 tmpl
->width0
, tmpl
->height0
, tmpl
->depth0
,
399 tmpl
->array_size
, tmpl
->last_level
, tmpl
->nr_samples
,
400 tmpl
->usage
, tmpl
->bind
, tmpl
->flags
);
407 pipe_reference_init(&prsc
->reference
, 1);
408 list_inithead(&rsc
->list
);
409 prsc
->screen
= pscreen
;
411 util_range_init(&rsc
->valid_buffer_range
);
413 rsc
->bo
= fd_screen_bo_from_handle(pscreen
, handle
, &slice
->pitch
);
417 rsc
->base
.vtbl
= &fd_resource_vtbl
;
418 rsc
->cpp
= util_format_get_blocksize(tmpl
->format
);
419 slice
->pitch
/= rsc
->cpp
;
426 fd_resource_destroy(pscreen
, prsc
);
430 static void fd_blitter_pipe_begin(struct fd_context
*ctx
);
431 static void fd_blitter_pipe_end(struct fd_context
*ctx
);
434 * _copy_region using pipe (3d engine)
437 fd_blitter_pipe_copy_region(struct fd_context
*ctx
,
438 struct pipe_resource
*dst
,
440 unsigned dstx
, unsigned dsty
, unsigned dstz
,
441 struct pipe_resource
*src
,
443 const struct pipe_box
*src_box
)
445 /* not until we allow rendertargets to be buffers */
446 if (dst
->target
== PIPE_BUFFER
|| src
->target
== PIPE_BUFFER
)
449 if (!util_blitter_is_copy_supported(ctx
->blitter
, dst
, src
))
452 fd_blitter_pipe_begin(ctx
);
453 util_blitter_copy_texture(ctx
->blitter
,
454 dst
, dst_level
, dstx
, dsty
, dstz
,
455 src
, src_level
, src_box
);
456 fd_blitter_pipe_end(ctx
);
462 * Copy a block of pixels from one resource to another.
463 * The resource must be of the same format.
464 * Resources with nr_samples > 1 are not allowed.
467 fd_resource_copy_region(struct pipe_context
*pctx
,
468 struct pipe_resource
*dst
,
470 unsigned dstx
, unsigned dsty
, unsigned dstz
,
471 struct pipe_resource
*src
,
473 const struct pipe_box
*src_box
)
475 struct fd_context
*ctx
= fd_context(pctx
);
477 /* TODO if we have 2d core, or other DMA engine that could be used
478 * for simple copies and reasonably easily synchronized with the 3d
479 * core, this is where we'd plug it in..
482 /* try blit on 3d pipe: */
483 if (fd_blitter_pipe_copy_region(ctx
,
484 dst
, dst_level
, dstx
, dsty
, dstz
,
485 src
, src_level
, src_box
))
488 /* else fallback to pure sw: */
489 util_resource_copy_region(pctx
,
490 dst
, dst_level
, dstx
, dsty
, dstz
,
491 src
, src_level
, src_box
);
495 * Optimal hardware path for blitting pixels.
496 * Scaling, format conversion, up- and downsampling (resolve) are allowed.
499 fd_blit(struct pipe_context
*pctx
, const struct pipe_blit_info
*blit_info
)
501 struct fd_context
*ctx
= fd_context(pctx
);
502 struct pipe_blit_info info
= *blit_info
;
504 if (info
.src
.resource
->nr_samples
> 1 &&
505 info
.dst
.resource
->nr_samples
<= 1 &&
506 !util_format_is_depth_or_stencil(info
.src
.resource
->format
) &&
507 !util_format_is_pure_integer(info
.src
.resource
->format
)) {
508 DBG("color resolve unimplemented");
512 if (util_try_blit_via_copy_region(pctx
, &info
)) {
516 if (info
.mask
& PIPE_MASK_S
) {
517 DBG("cannot blit stencil, skipping");
518 info
.mask
&= ~PIPE_MASK_S
;
521 if (!util_blitter_is_blit_supported(ctx
->blitter
, &info
)) {
522 DBG("blit unsupported %s -> %s",
523 util_format_short_name(info
.src
.resource
->format
),
524 util_format_short_name(info
.dst
.resource
->format
));
528 fd_blitter_pipe_begin(ctx
);
529 util_blitter_blit(ctx
->blitter
, &info
);
530 fd_blitter_pipe_end(ctx
);
534 fd_blitter_pipe_begin(struct fd_context
*ctx
)
536 util_blitter_save_vertex_buffer_slot(ctx
->blitter
, ctx
->vtx
.vertexbuf
.vb
);
537 util_blitter_save_vertex_elements(ctx
->blitter
, ctx
->vtx
.vtx
);
538 util_blitter_save_vertex_shader(ctx
->blitter
, ctx
->prog
.vp
);
539 util_blitter_save_rasterizer(ctx
->blitter
, ctx
->rasterizer
);
540 util_blitter_save_viewport(ctx
->blitter
, &ctx
->viewport
);
541 util_blitter_save_scissor(ctx
->blitter
, &ctx
->scissor
);
542 util_blitter_save_fragment_shader(ctx
->blitter
, ctx
->prog
.fp
);
543 util_blitter_save_blend(ctx
->blitter
, ctx
->blend
);
544 util_blitter_save_depth_stencil_alpha(ctx
->blitter
, ctx
->zsa
);
545 util_blitter_save_stencil_ref(ctx
->blitter
, &ctx
->stencil_ref
);
546 util_blitter_save_sample_mask(ctx
->blitter
, ctx
->sample_mask
);
547 util_blitter_save_framebuffer(ctx
->blitter
, &ctx
->framebuffer
);
548 util_blitter_save_fragment_sampler_states(ctx
->blitter
,
549 ctx
->fragtex
.num_samplers
,
550 (void **)ctx
->fragtex
.samplers
);
551 util_blitter_save_fragment_sampler_views(ctx
->blitter
,
552 ctx
->fragtex
.num_textures
, ctx
->fragtex
.textures
);
554 fd_hw_query_set_stage(ctx
, ctx
->ring
, FD_STAGE_BLIT
);
558 fd_blitter_pipe_end(struct fd_context
*ctx
)
560 fd_hw_query_set_stage(ctx
, ctx
->ring
, FD_STAGE_NULL
);
564 fd_flush_resource(struct pipe_context
*pctx
, struct pipe_resource
*prsc
)
566 struct fd_resource
*rsc
= fd_resource(prsc
);
569 fd_context_render(pctx
);
573 fd_resource_screen_init(struct pipe_screen
*pscreen
)
575 pscreen
->resource_create
= fd_resource_create
;
576 pscreen
->resource_from_handle
= fd_resource_from_handle
;
577 pscreen
->resource_get_handle
= u_resource_get_handle_vtbl
;
578 pscreen
->resource_destroy
= u_resource_destroy_vtbl
;
582 fd_resource_context_init(struct pipe_context
*pctx
)
584 pctx
->transfer_map
= u_transfer_map_vtbl
;
585 pctx
->transfer_flush_region
= u_transfer_flush_region_vtbl
;
586 pctx
->transfer_unmap
= u_transfer_unmap_vtbl
;
587 pctx
->transfer_inline_write
= u_transfer_inline_write_vtbl
;
588 pctx
->create_surface
= fd_create_surface
;
589 pctx
->surface_destroy
= fd_surface_destroy
;
590 pctx
->resource_copy_region
= fd_resource_copy_region
;
591 pctx
->blit
= fd_blit
;
592 pctx
->flush_resource
= fd_flush_resource
;