etnaviv: handle PIPE_CAP_TGSI_FS_FBFETCH
[mesa.git] / src / gallium / drivers / etnaviv / etnaviv_surface.c
1 /*
2 * Copyright (c) 2012-2013 Etnaviv Project
3 *
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:
10 *
11 * The above copyright notice and this permission notice (including the
12 * next paragraph) shall be included in all copies or substantial portions
13 * of the Software.
14 *
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.
22 *
23 * Authors:
24 * Wladimir J. van der Laan <laanwj@gmail.com>
25 */
26
27 #include "etnaviv_surface.h"
28 #include "etnaviv_screen.h"
29
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"
37
38 #include "hw/common.xml.h"
39
40 static struct pipe_surface *
41 etna_create_surface(struct pipe_context *pctx, struct pipe_resource *prsc,
42 const struct pipe_surface *templat)
43 {
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);
47
48 if (!surf)
49 return NULL;
50
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);
55
56 surf->base.context = pctx;
57
58 pipe_reference_init(&surf->base.reference, 1);
59 pipe_resource_reference(&surf->base.texture, &rsc->base);
60
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
65 * offset and MMU. */
66
67 /* XXX for now, don't do TS for render textures as this path
68 * is not stable. */
69 if (VIV_FEATURE(ctx->screen, chipFeatures, FAST_CLEAR) &&
70 VIV_FEATURE(ctx->screen, chipMinorFeatures0, MC20) &&
71 !DBG_ENABLED(ETNA_DBG_NO_TS) && !rsc->ts_bo &&
72 !(rsc->base.bind & (PIPE_BIND_SAMPLER_VIEW)) &&
73 (rsc->levels[level].padded_width & ETNA_RS_WIDTH_MASK) == 0 &&
74 (rsc->levels[level].padded_height & ETNA_RS_HEIGHT_MASK) == 0) {
75 etna_screen_resource_alloc_ts(pctx->screen, rsc);
76 }
77
78 surf->base.texture = &rsc->base;
79 surf->base.format = rsc->base.format;
80 surf->base.width = rsc->levels[level].width;
81 surf->base.height = rsc->levels[level].height;
82 surf->base.writable = templat->writable; /* what is this for anyway */
83 surf->base.u = templat->u;
84
85 surf->level = &rsc->levels[level]; /* Keep pointer to actual level to set
86 * clear color on underlying resource
87 * instead of surface */
88 surf->surf = rsc->levels [level]; /* Make copy of level to narrow down
89 * address to layer */
90
91 /* XXX we don't really need a copy but it's convenient */
92 surf->surf.offset += layer * surf->surf.layer_stride;
93
94 struct etna_resource_level *lev = &rsc->levels[level];
95
96 /* Setup template relocations for this surface */
97 surf->reloc[0].bo = rsc->bo;
98 surf->reloc[0].offset = surf->surf.offset;
99 surf->reloc[0].flags = 0;
100 surf->reloc[1].bo = rsc->bo;
101 surf->reloc[1].offset = surf->surf.offset + lev->stride * lev->padded_height / 2;
102 surf->reloc[1].flags = 0;
103
104 if (surf->surf.ts_size) {
105 unsigned int layer_offset = layer * surf->surf.ts_layer_stride;
106 assert(layer_offset < surf->surf.ts_size);
107
108 surf->surf.ts_offset += layer_offset;
109 surf->surf.ts_size -= layer_offset;
110
111 surf->ts_reloc.bo = rsc->ts_bo;
112 surf->ts_reloc.offset = surf->surf.ts_offset;
113 surf->ts_reloc.flags = 0;
114
115 /* This (ab)uses the RS as a plain buffer memset().
116 * Currently uses a fixed row size of 64 bytes. Some benchmarking with
117 * different sizes may be in order. */
118 struct etna_bo *ts_bo = etna_resource(surf->base.texture)->ts_bo;
119 etna_compile_rs_state(ctx, &surf->clear_command, &(struct rs_state) {
120 .source_format = RS_FORMAT_A8R8G8B8,
121 .dest_format = RS_FORMAT_A8R8G8B8,
122 .dest = ts_bo,
123 .dest_offset = surf->surf.ts_offset,
124 .dest_stride = 0x40,
125 .dest_tiling = ETNA_LAYOUT_TILED,
126 .dither = {0xffffffff, 0xffffffff},
127 .width = 16,
128 .height = etna_align_up(surf->surf.ts_size / 0x40, 4),
129 .clear_value = {ctx->specs.ts_clear_value},
130 .clear_mode = VIVS_RS_CLEAR_CONTROL_MODE_ENABLED1,
131 .clear_bits = 0xffff
132 });
133 } else {
134 etna_rs_gen_clear_surface(ctx, surf, surf->level->clear_value);
135 }
136
137 return &surf->base;
138 }
139
140 static void
141 etna_surface_destroy(struct pipe_context *pctx, struct pipe_surface *psurf)
142 {
143 pipe_resource_reference(&psurf->texture, NULL);
144 FREE(psurf);
145 }
146
147 void
148 etna_surface_init(struct pipe_context *pctx)
149 {
150 pctx->create_surface = etna_create_surface;
151 pctx->surface_destroy = etna_surface_destroy;
152 }