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 realloc_bo(struct fd_resource
*rsc
, uint32_t size
)
47 struct fd_screen
*screen
= fd_screen(rsc
->base
.b
.screen
);
48 uint32_t flags
= DRM_FREEDRENO_GEM_CACHE_WCOMBINE
|
49 DRM_FREEDRENO_GEM_TYPE_KMEM
; /* TODO */
51 /* if we start using things other than write-combine,
52 * be sure to check for PIPE_RESOURCE_FLAG_MAP_COHERENT
58 rsc
->bo
= fd_bo_new(screen
->dev
, size
, flags
);
63 static void fd_resource_transfer_flush_region(struct pipe_context
*pctx
,
64 struct pipe_transfer
*ptrans
,
65 const struct pipe_box
*box
)
70 fd_resource_transfer_unmap(struct pipe_context
*pctx
,
71 struct pipe_transfer
*ptrans
)
73 struct fd_context
*ctx
= fd_context(pctx
);
74 struct fd_resource
*rsc
= fd_resource(ptrans
->resource
);
75 if (!(ptrans
->usage
& PIPE_TRANSFER_UNSYNCHRONIZED
))
76 fd_bo_cpu_fini(rsc
->bo
);
77 pipe_resource_reference(&ptrans
->resource
, NULL
);
78 util_slab_free(&ctx
->transfer_pool
, ptrans
);
82 fd_resource_transfer_map(struct pipe_context
*pctx
,
83 struct pipe_resource
*prsc
,
84 unsigned level
, unsigned usage
,
85 const struct pipe_box
*box
,
86 struct pipe_transfer
**pptrans
)
88 struct fd_context
*ctx
= fd_context(pctx
);
89 struct fd_resource
*rsc
= fd_resource(prsc
);
90 struct fd_resource_slice
*slice
= fd_resource_slice(rsc
, level
);
91 struct pipe_transfer
*ptrans
;
92 enum pipe_format format
= prsc
->format
;
98 DBG("prsc=%p, level=%u, usage=%x", prsc
, level
, usage
);
100 ptrans
= util_slab_alloc(&ctx
->transfer_pool
);
104 /* util_slab_alloc() doesn't zero: */
105 memset(ptrans
, 0, sizeof(*ptrans
));
107 pipe_resource_reference(&ptrans
->resource
, prsc
);
108 ptrans
->level
= level
;
109 ptrans
->usage
= usage
;
111 ptrans
->stride
= slice
->pitch
* rsc
->cpp
;
112 ptrans
->layer_stride
= slice
->size0
;
114 if (usage
& PIPE_TRANSFER_READ
)
115 op
|= DRM_FREEDRENO_PREP_READ
;
117 if (usage
& PIPE_TRANSFER_WRITE
)
118 op
|= DRM_FREEDRENO_PREP_WRITE
;
120 if (usage
& PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
) {
121 realloc_bo(rsc
, fd_bo_size(rsc
->bo
));
122 } else if (!(usage
& PIPE_TRANSFER_UNSYNCHRONIZED
)) {
123 /* If the GPU is writing to the resource, or if it is reading from the
124 * resource and we're trying to write to it, flush the renders.
127 ((ptrans
->usage
& PIPE_TRANSFER_WRITE
) && rsc
->reading
))
128 fd_context_render(pctx
);
130 /* The GPU keeps track of how the various bo's are being used, and
131 * will wait if necessary for the proper operation to have
134 ret
= fd_bo_cpu_prep(rsc
->bo
, ctx
->screen
->pipe
, op
);
139 buf
= fd_bo_map(rsc
->bo
);
141 fd_resource_transfer_unmap(pctx
, ptrans
);
147 if (rsc
->layer_first
) {
148 offset
= slice
->offset
+
149 box
->y
/ util_format_get_blockheight(format
) * ptrans
->stride
+
150 box
->x
/ util_format_get_blockwidth(format
) * rsc
->cpp
+
151 box
->z
* rsc
->layer_size
;
153 offset
= slice
->offset
+
154 box
->y
/ util_format_get_blockheight(format
) * ptrans
->stride
+
155 box
->x
/ util_format_get_blockwidth(format
) * rsc
->cpp
+
156 box
->z
* slice
->size0
;
162 fd_resource_transfer_unmap(pctx
, ptrans
);
167 fd_resource_destroy(struct pipe_screen
*pscreen
,
168 struct pipe_resource
*prsc
)
170 struct fd_resource
*rsc
= fd_resource(prsc
);
177 fd_resource_get_handle(struct pipe_screen
*pscreen
,
178 struct pipe_resource
*prsc
,
179 struct winsys_handle
*handle
)
181 struct fd_resource
*rsc
= fd_resource(prsc
);
183 return fd_screen_bo_get_handle(pscreen
, rsc
->bo
,
184 rsc
->slices
[0].pitch
* rsc
->cpp
, handle
);
188 static const struct u_resource_vtbl fd_resource_vtbl
= {
189 .resource_get_handle
= fd_resource_get_handle
,
190 .resource_destroy
= fd_resource_destroy
,
191 .transfer_map
= fd_resource_transfer_map
,
192 .transfer_flush_region
= fd_resource_transfer_flush_region
,
193 .transfer_unmap
= fd_resource_transfer_unmap
,
194 .transfer_inline_write
= u_default_transfer_inline_write
,
198 setup_slices(struct fd_resource
*rsc
, uint32_t alignment
)
200 struct pipe_resource
*prsc
= &rsc
->base
.b
;
201 uint32_t level
, size
= 0;
202 uint32_t width
= prsc
->width0
;
203 uint32_t height
= prsc
->height0
;
204 uint32_t depth
= prsc
->depth0
;
205 /* in layer_first layout, the level (slice) contains just one
206 * layer (since in fact the layer contains the slices)
208 uint32_t layers_in_level
= rsc
->layer_first
? 1 : prsc
->array_size
;
210 for (level
= 0; level
<= prsc
->last_level
; level
++) {
211 struct fd_resource_slice
*slice
= fd_resource_slice(rsc
, level
);
213 slice
->pitch
= width
= align(width
, 32);
214 slice
->offset
= size
;
215 /* 1d array and 2d array textures must all have the same layer size
216 * for each miplevel on a3xx. 3d textures can have different layer
217 * sizes for high levels, but the hw auto-sizer is buggy (or at least
218 * different than what this code does), so as soon as the layer size
219 * range gets into range, we stop reducing it.
221 if (prsc
->target
== PIPE_TEXTURE_3D
&& (
223 (level
> 1 && rsc
->slices
[level
- 1].size0
> 0xf000)))
224 slice
->size0
= align(slice
->pitch
* height
* rsc
->cpp
, alignment
);
225 else if (level
== 0 || rsc
->layer_first
|| alignment
== 1)
226 slice
->size0
= align(slice
->pitch
* height
* rsc
->cpp
, alignment
);
228 slice
->size0
= rsc
->slices
[level
- 1].size0
;
230 size
+= slice
->size0
* depth
* layers_in_level
;
232 width
= u_minify(width
, 1);
233 height
= u_minify(height
, 1);
234 depth
= u_minify(depth
, 1);
241 slice_alignment(struct pipe_screen
*pscreen
, const struct pipe_resource
*tmpl
)
243 /* on a3xx, 2d array and 3d textures seem to want their
244 * layers aligned to page boundaries:
246 switch (tmpl
->target
) {
247 case PIPE_TEXTURE_3D
:
248 case PIPE_TEXTURE_1D_ARRAY
:
249 case PIPE_TEXTURE_2D_ARRAY
:
257 * Create a new texture object, using the given template info.
259 static struct pipe_resource
*
260 fd_resource_create(struct pipe_screen
*pscreen
,
261 const struct pipe_resource
*tmpl
)
263 struct fd_resource
*rsc
= CALLOC_STRUCT(fd_resource
);
264 struct pipe_resource
*prsc
= &rsc
->base
.b
;
267 DBG("target=%d, format=%s, %ux%ux%u, array_size=%u, last_level=%u, "
268 "nr_samples=%u, usage=%u, bind=%x, flags=%x",
269 tmpl
->target
, util_format_name(tmpl
->format
),
270 tmpl
->width0
, tmpl
->height0
, tmpl
->depth0
,
271 tmpl
->array_size
, tmpl
->last_level
, tmpl
->nr_samples
,
272 tmpl
->usage
, tmpl
->bind
, tmpl
->flags
);
279 pipe_reference_init(&prsc
->reference
, 1);
280 prsc
->screen
= pscreen
;
282 rsc
->base
.vtbl
= &fd_resource_vtbl
;
283 rsc
->cpp
= util_format_get_blocksize(tmpl
->format
);
287 if (is_a4xx(fd_screen(pscreen
))) {
288 switch (tmpl
->target
) {
289 case PIPE_TEXTURE_3D
:
291 rsc
->layer_first
= false;
294 rsc
->layer_first
= true;
299 size
= setup_slices(rsc
, slice_alignment(pscreen
, tmpl
));
301 if (rsc
->layer_first
) {
302 rsc
->layer_size
= align(size
, 4096);
303 size
= rsc
->layer_size
* prsc
->array_size
;
306 realloc_bo(rsc
, size
);
312 fd_resource_destroy(pscreen
, prsc
);
317 * Create a texture from a winsys_handle. The handle is often created in
318 * another process by first creating a pipe texture and then calling
319 * resource_get_handle.
321 static struct pipe_resource
*
322 fd_resource_from_handle(struct pipe_screen
*pscreen
,
323 const struct pipe_resource
*tmpl
,
324 struct winsys_handle
*handle
)
326 struct fd_resource
*rsc
= CALLOC_STRUCT(fd_resource
);
327 struct fd_resource_slice
*slice
= &rsc
->slices
[0];
328 struct pipe_resource
*prsc
= &rsc
->base
.b
;
330 DBG("target=%d, format=%s, %ux%ux%u, array_size=%u, last_level=%u, "
331 "nr_samples=%u, usage=%u, bind=%x, flags=%x",
332 tmpl
->target
, util_format_name(tmpl
->format
),
333 tmpl
->width0
, tmpl
->height0
, tmpl
->depth0
,
334 tmpl
->array_size
, tmpl
->last_level
, tmpl
->nr_samples
,
335 tmpl
->usage
, tmpl
->bind
, tmpl
->flags
);
342 pipe_reference_init(&prsc
->reference
, 1);
343 prsc
->screen
= pscreen
;
345 rsc
->bo
= fd_screen_bo_from_handle(pscreen
, handle
, &slice
->pitch
);
349 rsc
->base
.vtbl
= &fd_resource_vtbl
;
350 rsc
->cpp
= util_format_get_blocksize(tmpl
->format
);
351 slice
->pitch
/= rsc
->cpp
;
358 fd_resource_destroy(pscreen
, prsc
);
362 static void fd_blitter_pipe_begin(struct fd_context
*ctx
);
363 static void fd_blitter_pipe_end(struct fd_context
*ctx
);
366 * _copy_region using pipe (3d engine)
369 fd_blitter_pipe_copy_region(struct fd_context
*ctx
,
370 struct pipe_resource
*dst
,
372 unsigned dstx
, unsigned dsty
, unsigned dstz
,
373 struct pipe_resource
*src
,
375 const struct pipe_box
*src_box
)
377 /* not until we allow rendertargets to be buffers */
378 if (dst
->target
== PIPE_BUFFER
|| src
->target
== PIPE_BUFFER
)
381 if (!util_blitter_is_copy_supported(ctx
->blitter
, dst
, src
))
384 fd_blitter_pipe_begin(ctx
);
385 util_blitter_copy_texture(ctx
->blitter
,
386 dst
, dst_level
, dstx
, dsty
, dstz
,
387 src
, src_level
, src_box
);
388 fd_blitter_pipe_end(ctx
);
394 * Copy a block of pixels from one resource to another.
395 * The resource must be of the same format.
396 * Resources with nr_samples > 1 are not allowed.
399 fd_resource_copy_region(struct pipe_context
*pctx
,
400 struct pipe_resource
*dst
,
402 unsigned dstx
, unsigned dsty
, unsigned dstz
,
403 struct pipe_resource
*src
,
405 const struct pipe_box
*src_box
)
407 struct fd_context
*ctx
= fd_context(pctx
);
409 /* TODO if we have 2d core, or other DMA engine that could be used
410 * for simple copies and reasonably easily synchronized with the 3d
411 * core, this is where we'd plug it in..
414 /* try blit on 3d pipe: */
415 if (fd_blitter_pipe_copy_region(ctx
,
416 dst
, dst_level
, dstx
, dsty
, dstz
,
417 src
, src_level
, src_box
))
420 /* else fallback to pure sw: */
421 util_resource_copy_region(pctx
,
422 dst
, dst_level
, dstx
, dsty
, dstz
,
423 src
, src_level
, src_box
);
427 * Optimal hardware path for blitting pixels.
428 * Scaling, format conversion, up- and downsampling (resolve) are allowed.
431 fd_blit(struct pipe_context
*pctx
, const struct pipe_blit_info
*blit_info
)
433 struct fd_context
*ctx
= fd_context(pctx
);
434 struct pipe_blit_info info
= *blit_info
;
436 if (info
.src
.resource
->nr_samples
> 1 &&
437 info
.dst
.resource
->nr_samples
<= 1 &&
438 !util_format_is_depth_or_stencil(info
.src
.resource
->format
) &&
439 !util_format_is_pure_integer(info
.src
.resource
->format
)) {
440 DBG("color resolve unimplemented");
444 if (util_try_blit_via_copy_region(pctx
, &info
)) {
448 if (info
.mask
& PIPE_MASK_S
) {
449 DBG("cannot blit stencil, skipping");
450 info
.mask
&= ~PIPE_MASK_S
;
453 if (!util_blitter_is_blit_supported(ctx
->blitter
, &info
)) {
454 DBG("blit unsupported %s -> %s",
455 util_format_short_name(info
.src
.resource
->format
),
456 util_format_short_name(info
.dst
.resource
->format
));
460 fd_blitter_pipe_begin(ctx
);
461 util_blitter_blit(ctx
->blitter
, &info
);
462 fd_blitter_pipe_end(ctx
);
466 fd_blitter_pipe_begin(struct fd_context
*ctx
)
468 util_blitter_save_vertex_buffer_slot(ctx
->blitter
, ctx
->vtx
.vertexbuf
.vb
);
469 util_blitter_save_vertex_elements(ctx
->blitter
, ctx
->vtx
.vtx
);
470 util_blitter_save_vertex_shader(ctx
->blitter
, ctx
->prog
.vp
);
471 util_blitter_save_rasterizer(ctx
->blitter
, ctx
->rasterizer
);
472 util_blitter_save_viewport(ctx
->blitter
, &ctx
->viewport
);
473 util_blitter_save_scissor(ctx
->blitter
, &ctx
->scissor
);
474 util_blitter_save_fragment_shader(ctx
->blitter
, ctx
->prog
.fp
);
475 util_blitter_save_blend(ctx
->blitter
, ctx
->blend
);
476 util_blitter_save_depth_stencil_alpha(ctx
->blitter
, ctx
->zsa
);
477 util_blitter_save_stencil_ref(ctx
->blitter
, &ctx
->stencil_ref
);
478 util_blitter_save_sample_mask(ctx
->blitter
, ctx
->sample_mask
);
479 util_blitter_save_framebuffer(ctx
->blitter
, &ctx
->framebuffer
);
480 util_blitter_save_fragment_sampler_states(ctx
->blitter
,
481 ctx
->fragtex
.num_samplers
,
482 (void **)ctx
->fragtex
.samplers
);
483 util_blitter_save_fragment_sampler_views(ctx
->blitter
,
484 ctx
->fragtex
.num_textures
, ctx
->fragtex
.textures
);
486 fd_hw_query_set_stage(ctx
, ctx
->ring
, FD_STAGE_BLIT
);
490 fd_blitter_pipe_end(struct fd_context
*ctx
)
492 fd_hw_query_set_stage(ctx
, ctx
->ring
, FD_STAGE_NULL
);
496 fd_flush_resource(struct pipe_context
*pctx
, struct pipe_resource
*prsc
)
498 struct fd_resource
*rsc
= fd_resource(prsc
);
501 fd_context_render(pctx
);
505 fd_resource_screen_init(struct pipe_screen
*pscreen
)
507 pscreen
->resource_create
= fd_resource_create
;
508 pscreen
->resource_from_handle
= fd_resource_from_handle
;
509 pscreen
->resource_get_handle
= u_resource_get_handle_vtbl
;
510 pscreen
->resource_destroy
= u_resource_destroy_vtbl
;
514 fd_resource_context_init(struct pipe_context
*pctx
)
516 pctx
->transfer_map
= u_transfer_map_vtbl
;
517 pctx
->transfer_flush_region
= u_transfer_flush_region_vtbl
;
518 pctx
->transfer_unmap
= u_transfer_unmap_vtbl
;
519 pctx
->transfer_inline_write
= u_transfer_inline_write_vtbl
;
520 pctx
->create_surface
= fd_create_surface
;
521 pctx
->surface_destroy
= fd_surface_destroy
;
522 pctx
->resource_copy_region
= fd_resource_copy_region
;
523 pctx
->blit
= fd_blit
;
524 pctx
->flush_resource
= fd_flush_resource
;