2 * Copyright (c) 2012-2013 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_surface.h"
28 #include "etnaviv_screen.h"
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_memory.h"
38 #include "hw/common.xml.h"
40 static struct pipe_surface
*
41 etna_create_surface(struct pipe_context
*pctx
, struct pipe_resource
*prsc
,
42 const struct pipe_surface
*templat
)
44 struct etna_context
*ctx
= etna_context(pctx
);
45 struct etna_resource
*rsc
= etna_resource(prsc
);
46 struct etna_surface
*surf
= CALLOC_STRUCT(etna_surface
);
51 assert(templat
->u
.tex
.first_layer
== templat
->u
.tex
.last_layer
);
52 unsigned layer
= templat
->u
.tex
.first_layer
;
53 unsigned level
= templat
->u
.tex
.level
;
54 assert(layer
< rsc
->base
.array_size
);
56 surf
->base
.context
= pctx
;
58 pipe_reference_init(&surf
->base
.reference
, 1);
59 pipe_resource_reference(&surf
->base
.texture
, &rsc
->base
);
61 /* Allocate a TS for the resource if there isn't one yet,
62 * and it is allowed by the hw (width is a multiple of 16).
63 * Avoid doing this for GPUs with MC1.0, as kernel sources
64 * indicate the tile status module bypasses the memory
67 if (VIV_FEATURE(ctx
->screen
, chipFeatures
, FAST_CLEAR
) &&
68 VIV_FEATURE(ctx
->screen
, chipMinorFeatures0
, MC20
) &&
69 !DBG_ENABLED(ETNA_DBG_NO_TS
) && !rsc
->ts_bo
&&
70 (rsc
->levels
[level
].padded_width
& ETNA_RS_WIDTH_MASK
) == 0 &&
71 (rsc
->levels
[level
].padded_height
& ETNA_RS_HEIGHT_MASK
) == 0) {
72 etna_screen_resource_alloc_ts(pctx
->screen
, rsc
);
75 surf
->base
.texture
= &rsc
->base
;
76 surf
->base
.format
= rsc
->base
.format
;
77 surf
->base
.width
= rsc
->levels
[level
].width
;
78 surf
->base
.height
= rsc
->levels
[level
].height
;
79 surf
->base
.writable
= templat
->writable
; /* what is this for anyway */
80 surf
->base
.u
= templat
->u
;
82 surf
->level
= &rsc
->levels
[level
]; /* Keep pointer to actual level to set
83 * clear color on underlying resource
84 * instead of surface */
85 surf
->surf
= rsc
->levels
[level
]; /* Make copy of level to narrow down
88 /* XXX we don't really need a copy but it's convenient */
89 surf
->surf
.offset
+= layer
* surf
->surf
.layer_stride
;
91 struct etna_resource_level
*lev
= &rsc
->levels
[level
];
93 /* Setup template relocations for this surface */
94 surf
->reloc
[0].bo
= rsc
->bo
;
95 surf
->reloc
[0].offset
= surf
->surf
.offset
;
96 surf
->reloc
[0].flags
= 0;
97 surf
->reloc
[1].bo
= rsc
->bo
;
98 surf
->reloc
[1].offset
= surf
->surf
.offset
+ lev
->stride
* lev
->padded_height
/ 2;
99 surf
->reloc
[1].flags
= 0;
101 if (surf
->surf
.ts_size
) {
102 unsigned int layer_offset
= layer
* surf
->surf
.ts_layer_stride
;
103 assert(layer_offset
< surf
->surf
.ts_size
);
105 surf
->surf
.ts_offset
+= layer_offset
;
106 surf
->surf
.ts_size
-= layer_offset
;
107 surf
->surf
.ts_valid
= false;
109 surf
->ts_reloc
.bo
= rsc
->ts_bo
;
110 surf
->ts_reloc
.offset
= surf
->surf
.ts_offset
;
111 surf
->ts_reloc
.flags
= 0;
113 /* This (ab)uses the RS as a plain buffer memset().
114 * Currently uses a fixed row size of 64 bytes. Some benchmarking with
115 * different sizes may be in order. */
116 struct etna_bo
*ts_bo
= etna_resource(surf
->base
.texture
)->ts_bo
;
117 etna_compile_rs_state(ctx
, &surf
->clear_command
, &(struct rs_state
) {
118 .source_format
= RS_FORMAT_A8R8G8B8
,
119 .dest_format
= RS_FORMAT_A8R8G8B8
,
121 .dest_offset
= surf
->surf
.ts_offset
,
123 .dest_tiling
= ETNA_LAYOUT_TILED
,
124 .dither
= {0xffffffff, 0xffffffff},
126 .height
= etna_align_up(surf
->surf
.ts_size
/ 0x40, 4),
127 .clear_value
= {ctx
->specs
.ts_clear_value
},
128 .clear_mode
= VIVS_RS_CLEAR_CONTROL_MODE_ENABLED1
,
132 etna_rs_gen_clear_surface(ctx
, surf
, surf
->level
->clear_value
);
139 etna_surface_destroy(struct pipe_context
*pctx
, struct pipe_surface
*psurf
)
141 pipe_resource_reference(&psurf
->texture
, NULL
);
146 etna_surface_init(struct pipe_context
*pctx
)
148 pctx
->create_surface
= etna_create_surface
;
149 pctx
->surface_destroy
= etna_surface_destroy
;