2 * Copyright (c) 2012-2015 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_texture_state.h"
29 #include "hw/common.xml.h"
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"
40 #include "drm-uapi/drm_fourcc.h"
43 etna_create_sampler_state_state(struct pipe_context
*pipe
,
44 const struct pipe_sampler_state
*ss
)
46 struct etna_sampler_state
*cs
= CALLOC_STRUCT(etna_sampler_state
);
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
));
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
;
64 cs
->TE_SAMPLER_CONFIG1
=
65 COND(ss
->seamless_cube_map
, VIVS_TE_SAMPLER_CONFIG1_SEAMLESS_CUBE_MAP
);
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
));
71 cs
->TE_SAMPLER_3D_CONFIG
=
72 VIVS_TE_SAMPLER_3D_CONFIG_WRAP(translate_texture_wrapmode(ss
->wrap_r
));
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
);
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
);
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
87 cs
->max_lod_min
= (ss
->min_img_filter
!= ss
->mag_img_filter
) ? 1 : 0;
93 etna_delete_sampler_state_state(struct pipe_context
*pctx
, void *ss
)
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
)
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
,
114 struct etna_resource
*res
= etna_texture_handle_incompatible(pctx
, prsc
);
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
;
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;
132 uint32_t base_height
= res
->base
.height0
;
133 uint32_t base_depth
= res
->base
.depth0
;
134 bool is_array
= false;
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
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
);
147 case PIPE_TEXTURE_1D_ARRAY
:
149 base_height
= res
->base
.array_size
;
151 case PIPE_TEXTURE_2D_ARRAY
:
153 base_depth
= res
->base
.array_size
;
159 if (res
->addressing_mode
== ETNA_ADDRESSING_MODE_LINEAR
) {
160 sv
->TE_SAMPLER_CONFIG0
|= VIVS_TE_SAMPLER_CONFIG0_ADDRESSING_MODE(TEXTURE_ADDRESSING_MODE_LINEAR
);
162 for (int lod
= 0; lod
<= res
->base
.last_level
; ++lod
)
163 sv
->TE_SAMPLER_LINEAR_STRIDE
[lod
] = res
->levels
[lod
].stride
;
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
));
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
));
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
;
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;
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
);
214 etna_sampler_view_state_destroy(struct pipe_context
*pctx
,
215 struct pipe_sampler_view
*view
)
217 pipe_resource_reference(&view
->texture
, NULL
);
221 #define EMIT_STATE(state_name, src_value) \
222 etna_coalsence_emit(stream, &coalesce, VIVS_##state_name, src_value)
224 #define EMIT_STATE_FIXP(state_name, src_value) \
225 etna_coalsence_emit_fixp(stream, &coalesce, VIVS_##state_name, src_value)
227 #define EMIT_STATE_RELOC(state_name, src_value) \
228 etna_coalsence_emit_reloc(stream, &coalesce, VIVS_##state_name, src_value)
230 /* Emit plain (non-descriptor) texture state */
232 etna_emit_texture_state(struct etna_context
*ctx
)
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
;
239 etna_coalesce_start(stream
, &coalesce
);
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
);
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
);
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
);
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
);
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 */
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
]);
277 val
= (ss
->TE_SAMPLER_CONFIG0
& sv
->TE_SAMPLER_CONFIG0_MASK
) |
278 sv
->TE_SAMPLER_CONFIG0
;
281 /*02000*/ EMIT_STATE(TE_SAMPLER_CONFIG0(x
), val
);
284 if (unlikely(dirty
& (ETNA_DIRTY_SAMPLER_VIEWS
))) {
285 struct etna_sampler_view
*sv
;
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
);
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
);
300 if (unlikely(dirty
& (ETNA_DIRTY_SAMPLER_VIEWS
| ETNA_DIRTY_SAMPLERS
))) {
301 struct etna_sampler_state
*ss
;
302 struct etna_sampler_view
*sv
;
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
]);
309 unsigned max_lod
= MAX2(MIN2(ss
->max_lod
, sv
->max_lod
), ss
->max_lod_min
);
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
)));
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
]);
323 /*02180*/ EMIT_STATE(TE_SAMPLER_3D_CONFIG(x
), ss
->TE_SAMPLER_3D_CONFIG
|
324 sv
->TE_SAMPLER_3D_CONFIG
);
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
]);
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
));
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
]);
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
]);
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
);
366 etna_coalesce_end(stream
, &coalesce
);
370 #undef EMIT_STATE_FIXP
371 #undef EMIT_STATE_RELOC
373 static struct etna_sampler_ts
*
374 etna_ts_for_sampler_view_state(struct pipe_sampler_view
*pview
)
376 struct etna_sampler_view
*sv
= etna_sampler_view(pview
);
381 etna_texture_state_init(struct pipe_context
*pctx
)
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
;