5b3fbf46bf44a65833bce87006c082bcc60c5cfb
[mesa.git] / src / gallium / drivers / etnaviv / etnaviv_texture_state.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_state.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_texture.h"
36 #include "etnaviv_translate.h"
37 #include "util/u_inlines.h"
38 #include "util/u_memory.h"
39
40 #include "drm-uapi/drm_fourcc.h"
41
42 static void *
43 etna_create_sampler_state_state(struct pipe_context *pipe,
44 const struct pipe_sampler_state *ss)
45 {
46 struct etna_sampler_state *cs = CALLOC_STRUCT(etna_sampler_state);
47
48 if (!cs)
49 return NULL;
50
51 cs->TE_SAMPLER_CONFIG0 =
52 VIVS_TE_SAMPLER_CONFIG0_UWRAP(translate_texture_wrapmode(ss->wrap_s)) |
53 VIVS_TE_SAMPLER_CONFIG0_VWRAP(translate_texture_wrapmode(ss->wrap_t)) |
54 VIVS_TE_SAMPLER_CONFIG0_MIN(translate_texture_filter(ss->min_img_filter)) |
55 VIVS_TE_SAMPLER_CONFIG0_MIP(translate_texture_mipfilter(ss->min_mip_filter)) |
56 VIVS_TE_SAMPLER_CONFIG0_MAG(translate_texture_filter(ss->mag_img_filter));
57
58 /* ROUND_UV improves precision - but not compatible with NEAREST filter */
59 if (ss->min_img_filter != PIPE_TEX_FILTER_NEAREST &&
60 ss->mag_img_filter != PIPE_TEX_FILTER_NEAREST) {
61 cs->TE_SAMPLER_CONFIG0 |= VIVS_TE_SAMPLER_CONFIG0_ROUND_UV;
62 }
63
64 cs->TE_SAMPLER_CONFIG1 =
65 COND(ss->seamless_cube_map, VIVS_TE_SAMPLER_CONFIG1_SEAMLESS_CUBE_MAP);
66
67 cs->TE_SAMPLER_LOD_CONFIG =
68 COND(ss->lod_bias != 0.0, VIVS_TE_SAMPLER_LOD_CONFIG_BIAS_ENABLE) |
69 VIVS_TE_SAMPLER_LOD_CONFIG_BIAS(etna_float_to_fixp55(ss->lod_bias));
70
71 cs->TE_SAMPLER_3D_CONFIG =
72 VIVS_TE_SAMPLER_3D_CONFIG_WRAP(translate_texture_wrapmode(ss->wrap_r));
73
74 if (ss->min_mip_filter != PIPE_TEX_MIPFILTER_NONE) {
75 cs->min_lod = etna_float_to_fixp55(ss->min_lod);
76 cs->max_lod = etna_float_to_fixp55(ss->max_lod);
77 } else {
78 /* when not mipmapping, we need to set max/min lod so that always
79 * lowest LOD is selected */
80 cs->min_lod = cs->max_lod = etna_float_to_fixp55(ss->min_lod);
81 }
82
83 /* if max_lod is 0, MIN filter will never be used (GC3000)
84 * when min filter is different from mag filter, we need HW to compute LOD
85 * the workaround is to set max_lod to at least 1
86 */
87 cs->max_lod_min = (ss->min_img_filter != ss->mag_img_filter) ? 1 : 0;
88
89 return cs;
90 }
91
92 static void
93 etna_delete_sampler_state_state(struct pipe_context *pctx, void *ss)
94 {
95 FREE(ss);
96 }
97
98 static struct pipe_sampler_view *
99 etna_create_sampler_view_state(struct pipe_context *pctx, struct pipe_resource *prsc,
100 const struct pipe_sampler_view *so)
101 {
102 struct etna_sampler_view *sv = CALLOC_STRUCT(etna_sampler_view);
103 struct etna_context *ctx = etna_context(pctx);
104 const uint32_t format = translate_texture_format(so->format);
105 const bool ext = !!(format & EXT_FORMAT);
106 const bool astc = !!(format & ASTC_FORMAT);
107 const uint32_t swiz = get_texture_swiz(so->format, so->swizzle_r,
108 so->swizzle_g, so->swizzle_b,
109 so->swizzle_a);
110
111 if (!sv)
112 return NULL;
113
114 struct etna_resource *res = etna_texture_handle_incompatible(pctx, prsc);
115 if (!res) {
116 free(sv);
117 return NULL;
118 }
119
120 sv->base = *so;
121 pipe_reference_init(&sv->base.reference, 1);
122 sv->base.texture = NULL;
123 pipe_resource_reference(&sv->base.texture, prsc);
124 sv->base.context = pctx;
125
126 /* merged with sampler state */
127 sv->TE_SAMPLER_CONFIG0 =
128 VIVS_TE_SAMPLER_CONFIG0_TYPE(translate_texture_target(sv->base.target)) |
129 COND(!ext && !astc, VIVS_TE_SAMPLER_CONFIG0_FORMAT(format));
130 sv->TE_SAMPLER_CONFIG0_MASK = 0xffffffff;
131
132 uint32_t base_height = res->base.height0;
133 uint32_t base_depth = res->base.depth0;
134 bool is_array = false;
135
136 switch (sv->base.target) {
137 case PIPE_TEXTURE_1D:
138 /* use 2D texture with T wrap to repeat for 1D texture
139 * TODO: check if old HW supports 1D texture
140 */
141 sv->TE_SAMPLER_CONFIG0_MASK = ~VIVS_TE_SAMPLER_CONFIG0_VWRAP__MASK;
142 sv->TE_SAMPLER_CONFIG0 &= ~VIVS_TE_SAMPLER_CONFIG0_TYPE__MASK;
143 sv->TE_SAMPLER_CONFIG0 |=
144 VIVS_TE_SAMPLER_CONFIG0_TYPE(TEXTURE_TYPE_2D) |
145 VIVS_TE_SAMPLER_CONFIG0_VWRAP(TEXTURE_WRAPMODE_REPEAT);
146 break;
147 case PIPE_TEXTURE_1D_ARRAY:
148 is_array = true;
149 base_height = res->base.array_size;
150 break;
151 case PIPE_TEXTURE_2D_ARRAY:
152 is_array = true;
153 base_depth = res->base.array_size;
154 break;
155 default:
156 break;
157 }
158
159 if (res->addressing_mode == ETNA_ADDRESSING_MODE_LINEAR) {
160 sv->TE_SAMPLER_CONFIG0 |= VIVS_TE_SAMPLER_CONFIG0_ADDRESSING_MODE(TEXTURE_ADDRESSING_MODE_LINEAR);
161
162 for (int lod = 0; lod <= res->base.last_level; ++lod)
163 sv->TE_SAMPLER_LINEAR_STRIDE[lod] = res->levels[lod].stride;
164
165 } else {
166 sv->TE_SAMPLER_CONFIG0 |= VIVS_TE_SAMPLER_CONFIG0_ADDRESSING_MODE(TEXTURE_ADDRESSING_MODE_TILED);
167 memset(&sv->TE_SAMPLER_LINEAR_STRIDE, 0, sizeof(sv->TE_SAMPLER_LINEAR_STRIDE));
168 }
169
170 sv->TE_SAMPLER_CONFIG1 |= COND(ext, VIVS_TE_SAMPLER_CONFIG1_FORMAT_EXT(format)) |
171 COND(astc, VIVS_TE_SAMPLER_CONFIG1_FORMAT_EXT(TEXTURE_FORMAT_EXT_ASTC)) |
172 COND(is_array, VIVS_TE_SAMPLER_CONFIG1_TEXTURE_ARRAY) |
173 VIVS_TE_SAMPLER_CONFIG1_HALIGN(res->halign) | swiz;
174 sv->TE_SAMPLER_ASTC0 = COND(astc, VIVS_NTE_SAMPLER_ASTC0_ASTC_FORMAT(format)) |
175 VIVS_NTE_SAMPLER_ASTC0_UNK8(0xc) |
176 VIVS_NTE_SAMPLER_ASTC0_UNK16(0xc) |
177 VIVS_NTE_SAMPLER_ASTC0_UNK24(0xc);
178 sv->TE_SAMPLER_SIZE = VIVS_TE_SAMPLER_SIZE_WIDTH(res->base.width0) |
179 VIVS_TE_SAMPLER_SIZE_HEIGHT(base_height);
180 sv->TE_SAMPLER_LOG_SIZE =
181 VIVS_TE_SAMPLER_LOG_SIZE_WIDTH(etna_log2_fixp55(res->base.width0)) |
182 VIVS_TE_SAMPLER_LOG_SIZE_HEIGHT(etna_log2_fixp55(base_height)) |
183 COND(util_format_is_srgb(so->format) && !astc, VIVS_TE_SAMPLER_LOG_SIZE_SRGB) |
184 COND(astc, VIVS_TE_SAMPLER_LOG_SIZE_ASTC);
185 sv->TE_SAMPLER_3D_CONFIG =
186 VIVS_TE_SAMPLER_3D_CONFIG_DEPTH(base_depth) |
187 VIVS_TE_SAMPLER_3D_CONFIG_LOG_DEPTH(etna_log2_fixp55(base_depth));
188
189 /* Set up levels-of-detail */
190 for (int lod = 0; lod <= res->base.last_level; ++lod) {
191 sv->TE_SAMPLER_LOD_ADDR[lod].bo = res->bo;
192 sv->TE_SAMPLER_LOD_ADDR[lod].offset = res->levels[lod].offset;
193 sv->TE_SAMPLER_LOD_ADDR[lod].flags = ETNA_RELOC_READ;
194 }
195 sv->min_lod = sv->base.u.tex.first_level << 5;
196 sv->max_lod = MIN2(sv->base.u.tex.last_level, res->base.last_level) << 5;
197
198 /* Workaround for npot textures -- it appears that only CLAMP_TO_EDGE is
199 * supported when the appropriate capability is not set. */
200 if (!ctx->specs.npot_tex_any_wrap &&
201 (!util_is_power_of_two_or_zero(res->base.width0) ||
202 !util_is_power_of_two_or_zero(res->base.height0))) {
203 sv->TE_SAMPLER_CONFIG0_MASK = ~(VIVS_TE_SAMPLER_CONFIG0_UWRAP__MASK |
204 VIVS_TE_SAMPLER_CONFIG0_VWRAP__MASK);
205 sv->TE_SAMPLER_CONFIG0 |=
206 VIVS_TE_SAMPLER_CONFIG0_UWRAP(TEXTURE_WRAPMODE_CLAMP_TO_EDGE) |
207 VIVS_TE_SAMPLER_CONFIG0_VWRAP(TEXTURE_WRAPMODE_CLAMP_TO_EDGE);
208 }
209
210 return &sv->base;
211 }
212
213 static void
214 etna_sampler_view_state_destroy(struct pipe_context *pctx,
215 struct pipe_sampler_view *view)
216 {
217 pipe_resource_reference(&view->texture, NULL);
218 FREE(view);
219 }
220
221 #define EMIT_STATE(state_name, src_value) \
222 etna_coalsence_emit(stream, &coalesce, VIVS_##state_name, src_value)
223
224 #define EMIT_STATE_FIXP(state_name, src_value) \
225 etna_coalsence_emit_fixp(stream, &coalesce, VIVS_##state_name, src_value)
226
227 #define EMIT_STATE_RELOC(state_name, src_value) \
228 etna_coalsence_emit_reloc(stream, &coalesce, VIVS_##state_name, src_value)
229
230 /* Emit plain (non-descriptor) texture state */
231 static void
232 etna_emit_texture_state(struct etna_context *ctx)
233 {
234 struct etna_cmd_stream *stream = ctx->stream;
235 uint32_t active_samplers = active_samplers_bits(ctx);
236 uint32_t dirty = ctx->dirty;
237 struct etna_coalesce coalesce;
238
239 etna_coalesce_start(stream, &coalesce);
240
241 if (unlikely(dirty & ETNA_DIRTY_SAMPLER_VIEWS)) {
242 for (int x = 0; x < VIVS_TS_SAMPLER__LEN; ++x) {
243 if ((1 << x) & active_samplers) {
244 struct etna_sampler_view *sv = etna_sampler_view(ctx->sampler_view[x]);
245 /*01720*/ EMIT_STATE(TS_SAMPLER_CONFIG(x), sv->ts.TS_SAMPLER_CONFIG);
246 }
247 }
248 for (int x = 0; x < VIVS_TS_SAMPLER__LEN; ++x) {
249 if ((1 << x) & active_samplers) {
250 struct etna_sampler_view *sv = etna_sampler_view(ctx->sampler_view[x]);
251 /*01740*/ EMIT_STATE_RELOC(TS_SAMPLER_STATUS_BASE(x), &sv->ts.TS_SAMPLER_STATUS_BASE);
252 }
253 }
254 for (int x = 0; x < VIVS_TS_SAMPLER__LEN; ++x) {
255 if ((1 << x) & active_samplers) {
256 struct etna_sampler_view *sv = etna_sampler_view(ctx->sampler_view[x]);
257 /*01760*/ EMIT_STATE(TS_SAMPLER_CLEAR_VALUE(x), sv->ts.TS_SAMPLER_CLEAR_VALUE);
258 }
259 }
260 for (int x = 0; x < VIVS_TS_SAMPLER__LEN; ++x) {
261 if ((1 << x) & active_samplers) {
262 struct etna_sampler_view *sv = etna_sampler_view(ctx->sampler_view[x]);
263 /*01780*/ EMIT_STATE(TS_SAMPLER_CLEAR_VALUE2(x), sv->ts.TS_SAMPLER_CLEAR_VALUE2);
264 }
265 }
266 }
267 if (unlikely(dirty & (ETNA_DIRTY_SAMPLER_VIEWS | ETNA_DIRTY_SAMPLERS))) {
268 for (int x = 0; x < VIVS_TE_SAMPLER__LEN; ++x) {
269 uint32_t val = 0; /* 0 == sampler inactive */
270
271 /* set active samplers to their configuration value (determined by both
272 * the sampler state and sampler view) */
273 if ((1 << x) & active_samplers) {
274 struct etna_sampler_state *ss = etna_sampler_state(ctx->sampler[x]);
275 struct etna_sampler_view *sv = etna_sampler_view(ctx->sampler_view[x]);
276
277 val = (ss->TE_SAMPLER_CONFIG0 & sv->TE_SAMPLER_CONFIG0_MASK) |
278 sv->TE_SAMPLER_CONFIG0;
279 }
280
281 /*02000*/ EMIT_STATE(TE_SAMPLER_CONFIG0(x), val);
282 }
283 }
284 if (unlikely(dirty & (ETNA_DIRTY_SAMPLER_VIEWS))) {
285 struct etna_sampler_view *sv;
286
287 for (int x = 0; x < VIVS_TE_SAMPLER__LEN; ++x) {
288 if ((1 << x) & active_samplers) {
289 sv = etna_sampler_view(ctx->sampler_view[x]);
290 /*02040*/ EMIT_STATE(TE_SAMPLER_SIZE(x), sv->TE_SAMPLER_SIZE);
291 }
292 }
293 for (int x = 0; x < VIVS_TE_SAMPLER__LEN; ++x) {
294 if ((1 << x) & active_samplers) {
295 sv = etna_sampler_view(ctx->sampler_view[x]);
296 /*02080*/ EMIT_STATE(TE_SAMPLER_LOG_SIZE(x), sv->TE_SAMPLER_LOG_SIZE);
297 }
298 }
299 }
300 if (unlikely(dirty & (ETNA_DIRTY_SAMPLER_VIEWS | ETNA_DIRTY_SAMPLERS))) {
301 struct etna_sampler_state *ss;
302 struct etna_sampler_view *sv;
303
304 for (int x = 0; x < VIVS_TE_SAMPLER__LEN; ++x) {
305 if ((1 << x) & active_samplers) {
306 ss = etna_sampler_state(ctx->sampler[x]);
307 sv = etna_sampler_view(ctx->sampler_view[x]);
308
309 unsigned max_lod = MAX2(MIN2(ss->max_lod, sv->max_lod), ss->max_lod_min);
310
311 /* min and max lod is determined both by the sampler and the view */
312 /*020C0*/ EMIT_STATE(TE_SAMPLER_LOD_CONFIG(x),
313 ss->TE_SAMPLER_LOD_CONFIG |
314 VIVS_TE_SAMPLER_LOD_CONFIG_MAX(max_lod) |
315 VIVS_TE_SAMPLER_LOD_CONFIG_MIN(MAX2(ss->min_lod, sv->min_lod)));
316 }
317 }
318 for (int x = 0; x < VIVS_TE_SAMPLER__LEN; ++x) {
319 if ((1 << x) & active_samplers) {
320 ss = etna_sampler_state(ctx->sampler[x]);
321 sv = etna_sampler_view(ctx->sampler_view[x]);
322
323 /*02180*/ EMIT_STATE(TE_SAMPLER_3D_CONFIG(x), ss->TE_SAMPLER_3D_CONFIG |
324 sv->TE_SAMPLER_3D_CONFIG);
325 }
326 }
327 for (int x = 0; x < VIVS_TE_SAMPLER__LEN; ++x) {
328 if ((1 << x) & active_samplers) {
329 ss = etna_sampler_state(ctx->sampler[x]);
330 sv = etna_sampler_view(ctx->sampler_view[x]);
331
332 /*021C0*/ EMIT_STATE(TE_SAMPLER_CONFIG1(x), ss->TE_SAMPLER_CONFIG1 |
333 sv->TE_SAMPLER_CONFIG1 |
334 COND(sv->ts.enable, VIVS_TE_SAMPLER_CONFIG1_USE_TS));
335 }
336 }
337 }
338 if (unlikely(dirty & (ETNA_DIRTY_SAMPLER_VIEWS))) {
339 for (int y = 0; y < VIVS_TE_SAMPLER_LOD_ADDR__LEN; ++y) {
340 for (int x = 0; x < VIVS_TE_SAMPLER__LEN; ++x) {
341 if ((1 << x) & active_samplers) {
342 struct etna_sampler_view *sv = etna_sampler_view(ctx->sampler_view[x]);
343 /*02400*/ EMIT_STATE_RELOC(TE_SAMPLER_LOD_ADDR(x, y),&sv->TE_SAMPLER_LOD_ADDR[y]);
344 }
345 }
346 }
347 }
348 if (unlikely(dirty & (ETNA_DIRTY_SAMPLER_VIEWS))) {
349 for (int y = 0; y < VIVS_TE_SAMPLER_LINEAR_STRIDE__LEN; ++y) {
350 for (int x = 0; x < VIVS_TE_SAMPLER__LEN; ++x) {
351 if ((1 << x) & active_samplers) {
352 struct etna_sampler_view *sv = etna_sampler_view(ctx->sampler_view[x]);
353 /*02C00*/ EMIT_STATE(TE_SAMPLER_LINEAR_STRIDE(x, y), sv->TE_SAMPLER_LINEAR_STRIDE[y]);
354 }
355 }
356 }
357 }
358 if (unlikely(ctx->specs.tex_astc && (dirty & (ETNA_DIRTY_SAMPLER_VIEWS)))) {
359 for (int x = 0; x < VIVS_TE_SAMPLER__LEN; ++x) {
360 if ((1 << x) & active_samplers) {
361 struct etna_sampler_view *sv = etna_sampler_view(ctx->sampler_view[x]);
362 /*10500*/ EMIT_STATE(NTE_SAMPLER_ASTC0(x), sv->TE_SAMPLER_ASTC0);
363 }
364 }
365 }
366 etna_coalesce_end(stream, &coalesce);
367 }
368
369 #undef EMIT_STATE
370 #undef EMIT_STATE_FIXP
371 #undef EMIT_STATE_RELOC
372
373 static struct etna_sampler_ts*
374 etna_ts_for_sampler_view_state(struct pipe_sampler_view *pview)
375 {
376 struct etna_sampler_view *sv = etna_sampler_view(pview);
377 return &sv->ts;
378 }
379
380 void
381 etna_texture_state_init(struct pipe_context *pctx)
382 {
383 struct etna_context *ctx = etna_context(pctx);
384 DBG("etnaviv: Using state-based texturing");
385 ctx->base.create_sampler_state = etna_create_sampler_state_state;
386 ctx->base.delete_sampler_state = etna_delete_sampler_state_state;
387 ctx->base.create_sampler_view = etna_create_sampler_view_state;
388 ctx->base.sampler_view_destroy = etna_sampler_view_state_destroy;
389 ctx->emit_texture_state = etna_emit_texture_state;
390 ctx->ts_for_sampler_view = etna_ts_for_sampler_view_state;
391 }