etnaviv: flush resource when binding as sampler view
[mesa.git] / src / gallium / drivers / etnaviv / etnaviv_texture.c
1 /*
2 * Copyright (c) 2012-2015 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_texture.h"
28
29 #include "hw/common.xml.h"
30
31 #include "etnaviv_clear_blit.h"
32 #include "etnaviv_context.h"
33 #include "etnaviv_emit.h"
34 #include "etnaviv_format.h"
35 #include "etnaviv_translate.h"
36 #include "util/u_inlines.h"
37 #include "util/u_memory.h"
38
39 static void *
40 etna_create_sampler_state(struct pipe_context *pipe,
41 const struct pipe_sampler_state *ss)
42 {
43 struct etna_sampler_state *cs = CALLOC_STRUCT(etna_sampler_state);
44
45 if (!cs)
46 return NULL;
47
48 cs->TE_SAMPLER_CONFIG0 =
49 VIVS_TE_SAMPLER_CONFIG0_UWRAP(translate_texture_wrapmode(ss->wrap_s)) |
50 VIVS_TE_SAMPLER_CONFIG0_VWRAP(translate_texture_wrapmode(ss->wrap_t)) |
51 VIVS_TE_SAMPLER_CONFIG0_MIN(translate_texture_filter(ss->min_img_filter)) |
52 VIVS_TE_SAMPLER_CONFIG0_MIP(translate_texture_mipfilter(ss->min_mip_filter)) |
53 VIVS_TE_SAMPLER_CONFIG0_MAG(translate_texture_filter(ss->mag_img_filter)) |
54 COND(ss->normalized_coords, VIVS_TE_SAMPLER_CONFIG0_ROUND_UV);
55 cs->TE_SAMPLER_CONFIG1 = 0; /* VIVS_TE_SAMPLER_CONFIG1 (swizzle, extended
56 format) fully determined by sampler view */
57 cs->TE_SAMPLER_LOD_CONFIG =
58 COND(ss->lod_bias != 0.0, VIVS_TE_SAMPLER_LOD_CONFIG_BIAS_ENABLE) |
59 VIVS_TE_SAMPLER_LOD_CONFIG_BIAS(etna_float_to_fixp55(ss->lod_bias));
60
61 if (ss->min_mip_filter != PIPE_TEX_MIPFILTER_NONE) {
62 cs->min_lod = etna_float_to_fixp55(ss->min_lod);
63 cs->max_lod = etna_float_to_fixp55(ss->max_lod);
64 } else {
65 /* when not mipmapping, we need to set max/min lod so that always
66 * lowest LOD is selected */
67 cs->min_lod = cs->max_lod = etna_float_to_fixp55(ss->min_lod);
68 }
69
70 return cs;
71 }
72
73 static void
74 etna_bind_sampler_states(struct pipe_context *pctx, enum pipe_shader_type shader,
75 unsigned start_slot, unsigned num_samplers,
76 void **samplers)
77 {
78 /* bind fragment sampler */
79 struct etna_context *ctx = etna_context(pctx);
80 int offset;
81
82 switch (shader) {
83 case PIPE_SHADER_FRAGMENT:
84 offset = 0;
85 ctx->num_fragment_samplers = num_samplers;
86 break;
87 case PIPE_SHADER_VERTEX:
88 offset = ctx->specs.vertex_sampler_offset;
89 break;
90 default:
91 assert(!"Invalid shader");
92 return;
93 }
94
95 uint32_t mask = 1 << offset;
96 for (int idx = 0; idx < num_samplers; ++idx, mask <<= 1) {
97 ctx->sampler[offset + idx] = samplers[idx];
98 if (samplers[idx])
99 ctx->active_samplers |= mask;
100 else
101 ctx->active_samplers &= ~mask;
102 }
103
104 ctx->dirty |= ETNA_DIRTY_SAMPLERS;
105 }
106
107 static void
108 etna_delete_sampler_state(struct pipe_context *pctx, void *ss)
109 {
110 FREE(ss);
111 }
112
113 static void
114 etna_update_sampler_source(struct pipe_sampler_view *view)
115 {
116 struct etna_resource *res = etna_resource(view->texture);
117
118 if (res->texture && etna_resource_older(etna_resource(res->texture), res)) {
119 /* Texture is older than render buffer, copy the texture using RS */
120 etna_copy_resource(view->context, res->texture, view->texture, 0,
121 view->texture->last_level);
122 etna_resource(res->texture)->seqno = res->seqno;
123 } else if (etna_resource_needs_flush(res)) {
124 etna_copy_resource(view->context, view->texture, view->texture, 0, 0);
125 res->flush_seqno = res->seqno;
126 }
127 }
128
129 static bool
130 etna_resource_sampler_compatible(struct etna_resource *res)
131 {
132 if (util_format_is_compressed(res->base.format))
133 return true;
134
135 struct etna_screen *screen = etna_screen(res->base.screen);
136 /* This GPU supports texturing from supertiled textures? */
137 if (res->layout == ETNA_LAYOUT_SUPER_TILED && VIV_FEATURE(screen, chipMinorFeatures2, SUPERTILED_TEXTURE))
138 return true;
139
140 /* TODO: LINEAR_TEXTURE_SUPPORT */
141
142 /* Otherwise, only support tiled layouts */
143 if (res->layout != ETNA_LAYOUT_TILED)
144 return false;
145
146 /* If we have HALIGN support, we can allow for the RS padding */
147 if (VIV_FEATURE(screen, chipMinorFeatures1, TEXTURE_HALIGN))
148 return true;
149
150 /* Non-HALIGN GPUs only accept 4x4 tile-aligned textures */
151 if (res->halign != TEXTURE_HALIGN_FOUR)
152 return false;
153
154 return true;
155 }
156
157 static struct pipe_sampler_view *
158 etna_create_sampler_view(struct pipe_context *pctx, struct pipe_resource *prsc,
159 const struct pipe_sampler_view *so)
160 {
161 struct etna_sampler_view *sv = CALLOC_STRUCT(etna_sampler_view);
162 struct etna_resource *res = etna_resource(prsc);
163 struct etna_context *ctx = etna_context(pctx);
164
165 if (!sv)
166 return NULL;
167
168 if (!etna_resource_sampler_compatible(res)) {
169 /* The original resource is not compatible with the sampler.
170 * Allocate an appropriately tiled texture. */
171 if (!res->texture) {
172 struct pipe_resource templat = *prsc;
173
174 templat.bind &= ~(PIPE_BIND_DEPTH_STENCIL | PIPE_BIND_RENDER_TARGET |
175 PIPE_BIND_BLENDABLE);
176 res->texture =
177 etna_resource_alloc(pctx->screen, ETNA_LAYOUT_TILED, &templat);
178 }
179
180 if (!res->texture) {
181 free(sv);
182 return NULL;
183 }
184 res = etna_resource(res->texture);
185 }
186
187 sv->base = *so;
188 pipe_reference_init(&sv->base.reference, 1);
189 sv->base.texture = NULL;
190 pipe_resource_reference(&sv->base.texture, prsc);
191 sv->base.context = pctx;
192
193 /* merged with sampler state */
194 sv->TE_SAMPLER_CONFIG0 =
195 VIVS_TE_SAMPLER_CONFIG0_FORMAT(translate_texture_format(sv->base.format));
196 sv->TE_SAMPLER_CONFIG0_MASK = 0xffffffff;
197
198 switch (sv->base.target) {
199 case PIPE_TEXTURE_1D:
200 /* For 1D textures, we will have a height of 1, so we can use 2D
201 * but set T wrap to repeat */
202 sv->TE_SAMPLER_CONFIG0_MASK = ~VIVS_TE_SAMPLER_CONFIG0_VWRAP__MASK;
203 sv->TE_SAMPLER_CONFIG0 |= VIVS_TE_SAMPLER_CONFIG0_VWRAP(TEXTURE_WRAPMODE_REPEAT);
204 /* fallthrough */
205 case PIPE_TEXTURE_2D:
206 case PIPE_TEXTURE_RECT:
207 sv->TE_SAMPLER_CONFIG0 |= VIVS_TE_SAMPLER_CONFIG0_TYPE(TEXTURE_TYPE_2D);
208 break;
209 case PIPE_TEXTURE_CUBE:
210 sv->TE_SAMPLER_CONFIG0 |= VIVS_TE_SAMPLER_CONFIG0_TYPE(TEXTURE_TYPE_CUBE_MAP);
211 break;
212 default:
213 BUG("Unhandled texture target");
214 free(sv);
215 return NULL;
216 }
217
218 sv->TE_SAMPLER_CONFIG1 = VIVS_TE_SAMPLER_CONFIG1_SWIZZLE_R(so->swizzle_r) |
219 VIVS_TE_SAMPLER_CONFIG1_SWIZZLE_G(so->swizzle_g) |
220 VIVS_TE_SAMPLER_CONFIG1_SWIZZLE_B(so->swizzle_b) |
221 VIVS_TE_SAMPLER_CONFIG1_SWIZZLE_A(so->swizzle_a) |
222 VIVS_TE_SAMPLER_CONFIG1_HALIGN(res->halign);
223 sv->TE_SAMPLER_SIZE = VIVS_TE_SAMPLER_SIZE_WIDTH(res->base.width0) |
224 VIVS_TE_SAMPLER_SIZE_HEIGHT(res->base.height0);
225 sv->TE_SAMPLER_LOG_SIZE =
226 VIVS_TE_SAMPLER_LOG_SIZE_WIDTH(etna_log2_fixp55(res->base.width0)) |
227 VIVS_TE_SAMPLER_LOG_SIZE_HEIGHT(etna_log2_fixp55(res->base.height0));
228
229 /* Set up levels-of-detail */
230 for (int lod = 0; lod <= res->base.last_level; ++lod) {
231 sv->TE_SAMPLER_LOD_ADDR[lod].bo = res->bo;
232 sv->TE_SAMPLER_LOD_ADDR[lod].offset = res->levels[lod].offset;
233 sv->TE_SAMPLER_LOD_ADDR[lod].flags = ETNA_RELOC_READ;
234 }
235 sv->min_lod = sv->base.u.tex.first_level << 5;
236 sv->max_lod = MIN2(sv->base.u.tex.last_level, res->base.last_level) << 5;
237
238 /* Workaround for npot textures -- it appears that only CLAMP_TO_EDGE is
239 * supported when the appropriate capability is not set. */
240 if (!ctx->specs.npot_tex_any_wrap &&
241 (!util_is_power_of_two(res->base.width0) || !util_is_power_of_two(res->base.height0))) {
242 sv->TE_SAMPLER_CONFIG0_MASK = ~(VIVS_TE_SAMPLER_CONFIG0_UWRAP__MASK |
243 VIVS_TE_SAMPLER_CONFIG0_VWRAP__MASK);
244 sv->TE_SAMPLER_CONFIG0 |=
245 VIVS_TE_SAMPLER_CONFIG0_UWRAP(TEXTURE_WRAPMODE_CLAMP_TO_EDGE) |
246 VIVS_TE_SAMPLER_CONFIG0_VWRAP(TEXTURE_WRAPMODE_CLAMP_TO_EDGE);
247 }
248
249 return &sv->base;
250 }
251
252 static void
253 etna_sampler_view_destroy(struct pipe_context *pctx,
254 struct pipe_sampler_view *view)
255 {
256 pipe_resource_reference(&view->texture, NULL);
257 FREE(view);
258 }
259
260 static void
261 set_sampler_views(struct etna_context *ctx, unsigned start, unsigned end,
262 unsigned nr, struct pipe_sampler_view **views)
263 {
264 unsigned i, j;
265 uint32_t mask = 1 << start;
266
267 for (i = start, j = 0; j < nr; i++, j++, mask <<= 1) {
268 pipe_sampler_view_reference(&ctx->sampler_view[i], views[j]);
269 if (views[j])
270 ctx->active_sampler_views |= mask;
271 else
272 ctx->active_sampler_views &= ~mask;
273 }
274
275 for (; i < end; i++, mask <<= 1) {
276 pipe_sampler_view_reference(&ctx->sampler_view[i], NULL);
277 ctx->active_sampler_views &= ~mask;
278 }
279 }
280
281 static inline void
282 etna_fragtex_set_sampler_views(struct etna_context *ctx, unsigned nr,
283 struct pipe_sampler_view **views)
284 {
285 unsigned start = 0;
286 unsigned end = start + ctx->specs.fragment_sampler_count;
287
288 set_sampler_views(ctx, start, end, nr, views);
289 ctx->num_fragment_sampler_views = nr;
290 }
291
292
293 static inline void
294 etna_vertex_set_sampler_views(struct etna_context *ctx, unsigned nr,
295 struct pipe_sampler_view **views)
296 {
297 unsigned start = ctx->specs.vertex_sampler_offset;
298 unsigned end = start + ctx->specs.vertex_sampler_count;
299
300 set_sampler_views(ctx, start, end, nr, views);
301 }
302
303 static void
304 etna_set_sampler_views(struct pipe_context *pctx, enum pipe_shader_type shader,
305 unsigned start_slot, unsigned num_views,
306 struct pipe_sampler_view **views)
307 {
308 struct etna_context *ctx = etna_context(pctx);
309 assert(start_slot == 0);
310
311 ctx->dirty |= ETNA_DIRTY_SAMPLER_VIEWS | ETNA_DIRTY_TEXTURE_CACHES;
312
313 for (unsigned idx = 0; idx < num_views; ++idx) {
314 if (views[idx])
315 etna_update_sampler_source(views[idx]);
316 }
317
318 switch (shader) {
319 case PIPE_SHADER_FRAGMENT:
320 etna_fragtex_set_sampler_views(ctx, num_views, views);
321 break;
322 case PIPE_SHADER_VERTEX:
323 etna_vertex_set_sampler_views(ctx, num_views, views);
324 break;
325 default:;
326 }
327 }
328
329 static void
330 etna_texture_barrier(struct pipe_context *pctx, unsigned flags)
331 {
332 struct etna_context *ctx = etna_context(pctx);
333 /* clear color and texture cache to make sure that texture unit reads
334 * what has been written */
335 etna_set_state(ctx->stream, VIVS_GL_FLUSH_CACHE, VIVS_GL_FLUSH_CACHE_COLOR | VIVS_GL_FLUSH_CACHE_TEXTURE);
336 }
337
338 void
339 etna_texture_init(struct pipe_context *pctx)
340 {
341 pctx->create_sampler_state = etna_create_sampler_state;
342 pctx->bind_sampler_states = etna_bind_sampler_states;
343 pctx->delete_sampler_state = etna_delete_sampler_state;
344 pctx->set_sampler_views = etna_set_sampler_views;
345 pctx->create_sampler_view = etna_create_sampler_view;
346 pctx->sampler_view_destroy = etna_sampler_view_destroy;
347 pctx->texture_barrier = etna_texture_barrier;
348 }