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
)
67 struct fd_context
*ctx
= fd_context(pctx
);
68 struct fd_resource
*rsc
= fd_resource(ptrans
->resource
);
71 fd_context_render(pctx
);
74 fd_pipe_wait(ctx
->screen
->pipe
, rsc
->timestamp
);
80 fd_resource_transfer_unmap(struct pipe_context
*pctx
,
81 struct pipe_transfer
*ptrans
)
83 struct fd_context
*ctx
= fd_context(pctx
);
84 struct fd_resource
*rsc
= fd_resource(ptrans
->resource
);
85 if (!(ptrans
->usage
& PIPE_TRANSFER_UNSYNCHRONIZED
))
86 fd_bo_cpu_fini(rsc
->bo
);
87 pipe_resource_reference(&ptrans
->resource
, NULL
);
88 util_slab_free(&ctx
->transfer_pool
, ptrans
);
92 fd_resource_transfer_map(struct pipe_context
*pctx
,
93 struct pipe_resource
*prsc
,
94 unsigned level
, unsigned usage
,
95 const struct pipe_box
*box
,
96 struct pipe_transfer
**pptrans
)
98 struct fd_context
*ctx
= fd_context(pctx
);
99 struct fd_resource
*rsc
= fd_resource(prsc
);
100 struct fd_resource_slice
*slice
= fd_resource_slice(rsc
, level
);
101 struct pipe_transfer
*ptrans
;
102 enum pipe_format format
= prsc
->format
;
108 DBG("prsc=%p, level=%u, usage=%x", prsc
, level
, usage
);
110 ptrans
= util_slab_alloc(&ctx
->transfer_pool
);
114 /* util_slab_alloc() doesn't zero: */
115 memset(ptrans
, 0, sizeof(*ptrans
));
117 pipe_resource_reference(&ptrans
->resource
, prsc
);
118 ptrans
->level
= level
;
119 ptrans
->usage
= usage
;
121 ptrans
->stride
= slice
->pitch
* rsc
->cpp
;
122 ptrans
->layer_stride
= slice
->size0
;
124 if (usage
& PIPE_TRANSFER_READ
)
125 op
|= DRM_FREEDRENO_PREP_READ
;
127 if (usage
& PIPE_TRANSFER_WRITE
)
128 op
|= DRM_FREEDRENO_PREP_WRITE
;
130 /* some state trackers (at least XA) don't do this.. */
131 if (!(usage
& (PIPE_TRANSFER_FLUSH_EXPLICIT
| PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
)))
132 fd_resource_transfer_flush_region(pctx
, ptrans
, box
);
134 if (usage
& PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
) {
135 realloc_bo(rsc
, fd_bo_size(rsc
->bo
));
136 } else if (!(usage
& PIPE_TRANSFER_UNSYNCHRONIZED
)) {
137 ret
= fd_bo_cpu_prep(rsc
->bo
, ctx
->screen
->pipe
, op
);
142 buf
= fd_bo_map(rsc
->bo
);
144 fd_resource_transfer_unmap(pctx
, ptrans
);
150 if (rsc
->layer_first
) {
151 offset
= slice
->offset
+
152 box
->y
/ util_format_get_blockheight(format
) * ptrans
->stride
+
153 box
->x
/ util_format_get_blockwidth(format
) * rsc
->cpp
+
154 box
->z
* rsc
->layer_size
;
156 offset
= slice
->offset
+
157 box
->y
/ util_format_get_blockheight(format
) * ptrans
->stride
+
158 box
->x
/ util_format_get_blockwidth(format
) * rsc
->cpp
+
159 box
->z
* slice
->size0
;
165 fd_resource_transfer_unmap(pctx
, ptrans
);
170 fd_resource_destroy(struct pipe_screen
*pscreen
,
171 struct pipe_resource
*prsc
)
173 struct fd_resource
*rsc
= fd_resource(prsc
);
180 fd_resource_get_handle(struct pipe_screen
*pscreen
,
181 struct pipe_resource
*prsc
,
182 struct winsys_handle
*handle
)
184 struct fd_resource
*rsc
= fd_resource(prsc
);
186 return fd_screen_bo_get_handle(pscreen
, rsc
->bo
,
187 rsc
->slices
[0].pitch
* rsc
->cpp
, handle
);
191 static const struct u_resource_vtbl fd_resource_vtbl
= {
192 .resource_get_handle
= fd_resource_get_handle
,
193 .resource_destroy
= fd_resource_destroy
,
194 .transfer_map
= fd_resource_transfer_map
,
195 .transfer_flush_region
= fd_resource_transfer_flush_region
,
196 .transfer_unmap
= fd_resource_transfer_unmap
,
197 .transfer_inline_write
= u_default_transfer_inline_write
,
201 setup_slices(struct fd_resource
*rsc
, uint32_t alignment
)
203 struct pipe_resource
*prsc
= &rsc
->base
.b
;
204 uint32_t level
, size
= 0;
205 uint32_t width
= prsc
->width0
;
206 uint32_t height
= prsc
->height0
;
207 uint32_t depth
= prsc
->depth0
;
208 /* in layer_first layout, the level (slice) contains just one
209 * layer (since in fact the layer contains the slices)
211 uint32_t layers_in_level
= rsc
->layer_first
? 1 : prsc
->array_size
;
213 for (level
= 0; level
<= prsc
->last_level
; level
++) {
214 struct fd_resource_slice
*slice
= fd_resource_slice(rsc
, level
);
216 slice
->pitch
= width
= align(width
, 32);
217 slice
->offset
= size
;
218 /* 1d array and 2d array textures must all have the same layer size
219 * for each miplevel on a3xx. 3d textures can have different layer
220 * sizes for high levels, but the hw auto-sizer is buggy (or at least
221 * different than what this code does), so as soon as the layer size
222 * range gets into range, we stop reducing it.
224 if (prsc
->target
== PIPE_TEXTURE_3D
&& (
226 (level
> 1 && rsc
->slices
[level
- 1].size0
> 0xf000)))
227 slice
->size0
= align(slice
->pitch
* height
* rsc
->cpp
, alignment
);
228 else if (level
== 0 || rsc
->layer_first
|| alignment
== 1)
229 slice
->size0
= align(slice
->pitch
* height
* rsc
->cpp
, alignment
);
231 slice
->size0
= rsc
->slices
[level
- 1].size0
;
233 size
+= slice
->size0
* depth
* layers_in_level
;
235 width
= u_minify(width
, 1);
236 height
= u_minify(height
, 1);
237 depth
= u_minify(depth
, 1);
244 slice_alignment(struct pipe_screen
*pscreen
, const struct pipe_resource
*tmpl
)
246 /* on a3xx, 2d array and 3d textures seem to want their
247 * layers aligned to page boundaries:
249 switch (tmpl
->target
) {
250 case PIPE_TEXTURE_3D
:
251 case PIPE_TEXTURE_1D_ARRAY
:
252 case PIPE_TEXTURE_2D_ARRAY
:
260 * Create a new texture object, using the given template info.
262 static struct pipe_resource
*
263 fd_resource_create(struct pipe_screen
*pscreen
,
264 const struct pipe_resource
*tmpl
)
266 struct fd_resource
*rsc
= CALLOC_STRUCT(fd_resource
);
267 struct pipe_resource
*prsc
= &rsc
->base
.b
;
270 DBG("target=%d, format=%s, %ux%ux%u, array_size=%u, last_level=%u, "
271 "nr_samples=%u, usage=%u, bind=%x, flags=%x",
272 tmpl
->target
, util_format_name(tmpl
->format
),
273 tmpl
->width0
, tmpl
->height0
, tmpl
->depth0
,
274 tmpl
->array_size
, tmpl
->last_level
, tmpl
->nr_samples
,
275 tmpl
->usage
, tmpl
->bind
, tmpl
->flags
);
282 pipe_reference_init(&prsc
->reference
, 1);
283 prsc
->screen
= pscreen
;
285 rsc
->base
.vtbl
= &fd_resource_vtbl
;
286 rsc
->cpp
= util_format_get_blocksize(tmpl
->format
);
290 if (is_a4xx(fd_screen(pscreen
))) {
291 switch (tmpl
->target
) {
292 case PIPE_TEXTURE_3D
:
294 rsc
->layer_first
= false;
297 rsc
->layer_first
= true;
302 size
= setup_slices(rsc
, slice_alignment(pscreen
, tmpl
));
304 if (rsc
->layer_first
) {
305 rsc
->layer_size
= align(size
, 4096);
306 size
= rsc
->layer_size
* prsc
->array_size
;
309 realloc_bo(rsc
, size
);
315 fd_resource_destroy(pscreen
, prsc
);
320 * Create a texture from a winsys_handle. The handle is often created in
321 * another process by first creating a pipe texture and then calling
322 * resource_get_handle.
324 static struct pipe_resource
*
325 fd_resource_from_handle(struct pipe_screen
*pscreen
,
326 const struct pipe_resource
*tmpl
,
327 struct winsys_handle
*handle
)
329 struct fd_resource
*rsc
= CALLOC_STRUCT(fd_resource
);
330 struct fd_resource_slice
*slice
= &rsc
->slices
[0];
331 struct pipe_resource
*prsc
= &rsc
->base
.b
;
333 DBG("target=%d, format=%s, %ux%ux%u, array_size=%u, last_level=%u, "
334 "nr_samples=%u, usage=%u, bind=%x, flags=%x",
335 tmpl
->target
, util_format_name(tmpl
->format
),
336 tmpl
->width0
, tmpl
->height0
, tmpl
->depth0
,
337 tmpl
->array_size
, tmpl
->last_level
, tmpl
->nr_samples
,
338 tmpl
->usage
, tmpl
->bind
, tmpl
->flags
);
345 pipe_reference_init(&prsc
->reference
, 1);
346 prsc
->screen
= pscreen
;
348 rsc
->bo
= fd_screen_bo_from_handle(pscreen
, handle
, &slice
->pitch
);
352 rsc
->base
.vtbl
= &fd_resource_vtbl
;
353 rsc
->cpp
= util_format_get_blocksize(tmpl
->format
);
354 slice
->pitch
/= rsc
->cpp
;
361 fd_resource_destroy(pscreen
, prsc
);
365 static void fd_blitter_pipe_begin(struct fd_context
*ctx
);
366 static void fd_blitter_pipe_end(struct fd_context
*ctx
);
369 * _copy_region using pipe (3d engine)
372 fd_blitter_pipe_copy_region(struct fd_context
*ctx
,
373 struct pipe_resource
*dst
,
375 unsigned dstx
, unsigned dsty
, unsigned dstz
,
376 struct pipe_resource
*src
,
378 const struct pipe_box
*src_box
)
380 /* not until we allow rendertargets to be buffers */
381 if (dst
->target
== PIPE_BUFFER
|| src
->target
== PIPE_BUFFER
)
384 if (!util_blitter_is_copy_supported(ctx
->blitter
, dst
, src
))
387 fd_blitter_pipe_begin(ctx
);
388 util_blitter_copy_texture(ctx
->blitter
,
389 dst
, dst_level
, dstx
, dsty
, dstz
,
390 src
, src_level
, src_box
);
391 fd_blitter_pipe_end(ctx
);
397 * Copy a block of pixels from one resource to another.
398 * The resource must be of the same format.
399 * Resources with nr_samples > 1 are not allowed.
402 fd_resource_copy_region(struct pipe_context
*pctx
,
403 struct pipe_resource
*dst
,
405 unsigned dstx
, unsigned dsty
, unsigned dstz
,
406 struct pipe_resource
*src
,
408 const struct pipe_box
*src_box
)
410 struct fd_context
*ctx
= fd_context(pctx
);
412 /* TODO if we have 2d core, or other DMA engine that could be used
413 * for simple copies and reasonably easily synchronized with the 3d
414 * core, this is where we'd plug it in..
417 /* try blit on 3d pipe: */
418 if (fd_blitter_pipe_copy_region(ctx
,
419 dst
, dst_level
, dstx
, dsty
, dstz
,
420 src
, src_level
, src_box
))
423 /* else fallback to pure sw: */
424 util_resource_copy_region(pctx
,
425 dst
, dst_level
, dstx
, dsty
, dstz
,
426 src
, src_level
, src_box
);
430 * Optimal hardware path for blitting pixels.
431 * Scaling, format conversion, up- and downsampling (resolve) are allowed.
434 fd_blit(struct pipe_context
*pctx
, const struct pipe_blit_info
*blit_info
)
436 struct fd_context
*ctx
= fd_context(pctx
);
437 struct pipe_blit_info info
= *blit_info
;
439 if (info
.src
.resource
->nr_samples
> 1 &&
440 info
.dst
.resource
->nr_samples
<= 1 &&
441 !util_format_is_depth_or_stencil(info
.src
.resource
->format
) &&
442 !util_format_is_pure_integer(info
.src
.resource
->format
)) {
443 DBG("color resolve unimplemented");
447 if (util_try_blit_via_copy_region(pctx
, &info
)) {
451 if (info
.mask
& PIPE_MASK_S
) {
452 DBG("cannot blit stencil, skipping");
453 info
.mask
&= ~PIPE_MASK_S
;
456 if (!util_blitter_is_blit_supported(ctx
->blitter
, &info
)) {
457 DBG("blit unsupported %s -> %s",
458 util_format_short_name(info
.src
.resource
->format
),
459 util_format_short_name(info
.dst
.resource
->format
));
463 fd_blitter_pipe_begin(ctx
);
464 util_blitter_blit(ctx
->blitter
, &info
);
465 fd_blitter_pipe_end(ctx
);
469 fd_blitter_pipe_begin(struct fd_context
*ctx
)
471 util_blitter_save_vertex_buffer_slot(ctx
->blitter
, ctx
->vtx
.vertexbuf
.vb
);
472 util_blitter_save_vertex_elements(ctx
->blitter
, ctx
->vtx
.vtx
);
473 util_blitter_save_vertex_shader(ctx
->blitter
, ctx
->prog
.vp
);
474 util_blitter_save_rasterizer(ctx
->blitter
, ctx
->rasterizer
);
475 util_blitter_save_viewport(ctx
->blitter
, &ctx
->viewport
);
476 util_blitter_save_scissor(ctx
->blitter
, &ctx
->scissor
);
477 util_blitter_save_fragment_shader(ctx
->blitter
, ctx
->prog
.fp
);
478 util_blitter_save_blend(ctx
->blitter
, ctx
->blend
);
479 util_blitter_save_depth_stencil_alpha(ctx
->blitter
, ctx
->zsa
);
480 util_blitter_save_stencil_ref(ctx
->blitter
, &ctx
->stencil_ref
);
481 util_blitter_save_sample_mask(ctx
->blitter
, ctx
->sample_mask
);
482 util_blitter_save_framebuffer(ctx
->blitter
, &ctx
->framebuffer
);
483 util_blitter_save_fragment_sampler_states(ctx
->blitter
,
484 ctx
->fragtex
.num_samplers
,
485 (void **)ctx
->fragtex
.samplers
);
486 util_blitter_save_fragment_sampler_views(ctx
->blitter
,
487 ctx
->fragtex
.num_textures
, ctx
->fragtex
.textures
);
489 fd_hw_query_set_stage(ctx
, ctx
->ring
, FD_STAGE_BLIT
);
493 fd_blitter_pipe_end(struct fd_context
*ctx
)
495 fd_hw_query_set_stage(ctx
, ctx
->ring
, FD_STAGE_NULL
);
499 fd_flush_resource(struct pipe_context
*pctx
, struct pipe_resource
*prsc
)
501 struct fd_resource
*rsc
= fd_resource(prsc
);
504 fd_context_render(pctx
);
508 fd_resource_screen_init(struct pipe_screen
*pscreen
)
510 pscreen
->resource_create
= fd_resource_create
;
511 pscreen
->resource_from_handle
= fd_resource_from_handle
;
512 pscreen
->resource_get_handle
= u_resource_get_handle_vtbl
;
513 pscreen
->resource_destroy
= u_resource_destroy_vtbl
;
517 fd_resource_context_init(struct pipe_context
*pctx
)
519 pctx
->transfer_map
= u_transfer_map_vtbl
;
520 pctx
->transfer_flush_region
= u_transfer_flush_region_vtbl
;
521 pctx
->transfer_unmap
= u_transfer_unmap_vtbl
;
522 pctx
->transfer_inline_write
= u_transfer_inline_write_vtbl
;
523 pctx
->create_surface
= fd_create_surface
;
524 pctx
->surface_destroy
= fd_surface_destroy
;
525 pctx
->resource_copy_region
= fd_resource_copy_region
;
526 pctx
->blit
= fd_blit
;
527 pctx
->flush_resource
= fd_flush_resource
;