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.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_translate.h"
36 #include "util/u_inlines.h"
37 #include "util/u_memory.h"
40 etna_create_sampler_state(struct pipe_context
*pipe
,
41 const struct pipe_sampler_state
*ss
)
43 struct etna_sampler_state
*cs
= CALLOC_STRUCT(etna_sampler_state
);
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
));
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
);
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
);
74 etna_bind_sampler_states(struct pipe_context
*pctx
, enum pipe_shader_type shader
,
75 unsigned start_slot
, unsigned num_samplers
,
78 /* bind fragment sampler */
79 struct etna_context
*ctx
= etna_context(pctx
);
83 case PIPE_SHADER_FRAGMENT
:
85 ctx
->num_fragment_samplers
= num_samplers
;
87 case PIPE_SHADER_VERTEX
:
88 offset
= ctx
->specs
.vertex_sampler_offset
;
91 assert(!"Invalid shader");
95 uint32_t mask
= 1 << offset
;
96 for (int idx
= 0; idx
< num_samplers
; ++idx
, mask
<<= 1) {
97 ctx
->sampler
[offset
+ idx
] = samplers
[idx
];
99 ctx
->active_samplers
|= mask
;
101 ctx
->active_samplers
&= ~mask
;
104 ctx
->dirty
|= ETNA_DIRTY_SAMPLERS
;
108 etna_delete_sampler_state(struct pipe_context
*pctx
, void *ss
)
114 etna_update_sampler_source(struct pipe_sampler_view
*view
)
116 struct etna_resource
*res
= etna_resource(view
->texture
);
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
;
127 etna_resource_sampler_compatible(struct etna_resource
*res
)
129 if (util_format_is_compressed(res
->base
.format
))
132 /* The sampler (as we currently know it) only accepts tiled layouts */
133 if (res
->layout
!= ETNA_LAYOUT_TILED
)
136 /* If we have HALIGN support, we can allow for the RS padding */
137 struct etna_screen
*screen
= etna_screen(res
->base
.screen
);
138 if (VIV_FEATURE(screen
, chipMinorFeatures1
, TEXTURE_HALIGN
))
141 /* Non-HALIGN GPUs only accept 4x4 tile-aligned textures */
142 if (res
->halign
!= TEXTURE_HALIGN_FOUR
)
148 static struct pipe_sampler_view
*
149 etna_create_sampler_view(struct pipe_context
*pctx
, struct pipe_resource
*prsc
,
150 const struct pipe_sampler_view
*so
)
152 struct etna_sampler_view
*sv
= CALLOC_STRUCT(etna_sampler_view
);
153 struct etna_resource
*res
= etna_resource(prsc
);
154 struct etna_context
*ctx
= etna_context(pctx
);
159 if (!etna_resource_sampler_compatible(res
)) {
160 /* The original resource is not compatible with the sampler.
161 * Allocate an appropriately tiled texture. */
163 struct pipe_resource templat
= *prsc
;
165 templat
.bind
&= ~(PIPE_BIND_DEPTH_STENCIL
| PIPE_BIND_RENDER_TARGET
|
166 PIPE_BIND_BLENDABLE
);
168 etna_resource_alloc(pctx
->screen
, ETNA_LAYOUT_TILED
, &templat
);
175 res
= etna_resource(res
->texture
);
179 pipe_reference(NULL
, &prsc
->reference
);
180 sv
->base
.texture
= prsc
;
181 sv
->base
.reference
.count
= 1;
182 sv
->base
.context
= pctx
;
184 /* merged with sampler state */
185 sv
->TE_SAMPLER_CONFIG0
=
186 VIVS_TE_SAMPLER_CONFIG0_FORMAT(translate_texture_format(sv
->base
.format
));
187 sv
->TE_SAMPLER_CONFIG0_MASK
= 0xffffffff;
189 switch (sv
->base
.target
) {
190 case PIPE_TEXTURE_1D
:
191 /* For 1D textures, we will have a height of 1, so we can use 2D
192 * but set T wrap to repeat */
193 sv
->TE_SAMPLER_CONFIG0_MASK
= ~VIVS_TE_SAMPLER_CONFIG0_VWRAP__MASK
;
194 sv
->TE_SAMPLER_CONFIG0
|= VIVS_TE_SAMPLER_CONFIG0_VWRAP(TEXTURE_WRAPMODE_REPEAT
);
196 case PIPE_TEXTURE_2D
:
197 case PIPE_TEXTURE_RECT
:
198 sv
->TE_SAMPLER_CONFIG0
|= VIVS_TE_SAMPLER_CONFIG0_TYPE(TEXTURE_TYPE_2D
);
200 case PIPE_TEXTURE_CUBE
:
201 sv
->TE_SAMPLER_CONFIG0
|= VIVS_TE_SAMPLER_CONFIG0_TYPE(TEXTURE_TYPE_CUBE_MAP
);
204 BUG("Unhandled texture target");
209 sv
->TE_SAMPLER_CONFIG1
= VIVS_TE_SAMPLER_CONFIG1_SWIZZLE_R(so
->swizzle_r
) |
210 VIVS_TE_SAMPLER_CONFIG1_SWIZZLE_G(so
->swizzle_g
) |
211 VIVS_TE_SAMPLER_CONFIG1_SWIZZLE_B(so
->swizzle_b
) |
212 VIVS_TE_SAMPLER_CONFIG1_SWIZZLE_A(so
->swizzle_a
) |
213 VIVS_TE_SAMPLER_CONFIG1_HALIGN(res
->halign
);
214 sv
->TE_SAMPLER_SIZE
= VIVS_TE_SAMPLER_SIZE_WIDTH(res
->base
.width0
) |
215 VIVS_TE_SAMPLER_SIZE_HEIGHT(res
->base
.height0
);
216 sv
->TE_SAMPLER_LOG_SIZE
=
217 VIVS_TE_SAMPLER_LOG_SIZE_WIDTH(etna_log2_fixp55(res
->base
.width0
)) |
218 VIVS_TE_SAMPLER_LOG_SIZE_HEIGHT(etna_log2_fixp55(res
->base
.height0
));
220 /* Set up levels-of-detail */
221 for (int lod
= 0; lod
<= res
->base
.last_level
; ++lod
) {
222 sv
->TE_SAMPLER_LOD_ADDR
[lod
].bo
= res
->bo
;
223 sv
->TE_SAMPLER_LOD_ADDR
[lod
].offset
= res
->levels
[lod
].offset
;
224 sv
->TE_SAMPLER_LOD_ADDR
[lod
].flags
= ETNA_RELOC_READ
;
226 sv
->min_lod
= sv
->base
.u
.tex
.first_level
<< 5;
227 sv
->max_lod
= MIN2(sv
->base
.u
.tex
.last_level
, res
->base
.last_level
) << 5;
229 /* Workaround for npot textures -- it appears that only CLAMP_TO_EDGE is
230 * supported when the appropriate capability is not set. */
231 if (!ctx
->specs
.npot_tex_any_wrap
&&
232 (!util_is_power_of_two(res
->base
.width0
) || !util_is_power_of_two(res
->base
.height0
))) {
233 sv
->TE_SAMPLER_CONFIG0_MASK
= ~(VIVS_TE_SAMPLER_CONFIG0_UWRAP__MASK
|
234 VIVS_TE_SAMPLER_CONFIG0_VWRAP__MASK
);
235 sv
->TE_SAMPLER_CONFIG0
|=
236 VIVS_TE_SAMPLER_CONFIG0_UWRAP(TEXTURE_WRAPMODE_CLAMP_TO_EDGE
) |
237 VIVS_TE_SAMPLER_CONFIG0_VWRAP(TEXTURE_WRAPMODE_CLAMP_TO_EDGE
);
244 etna_sampler_view_destroy(struct pipe_context
*pctx
,
245 struct pipe_sampler_view
*view
)
247 pipe_resource_reference(&view
->texture
, NULL
);
252 set_sampler_views(struct etna_context
*ctx
, unsigned start
, unsigned end
,
253 unsigned nr
, struct pipe_sampler_view
**views
)
256 uint32_t mask
= 1 << start
;
258 for (i
= start
, j
= 0; j
< nr
; i
++, j
++, mask
<<= 1) {
259 pipe_sampler_view_reference(&ctx
->sampler_view
[i
], views
[j
]);
261 ctx
->active_sampler_views
|= mask
;
263 ctx
->active_sampler_views
&= ~mask
;
266 for (; i
< end
; i
++, mask
<<= 1) {
267 pipe_sampler_view_reference(&ctx
->sampler_view
[i
], NULL
);
268 ctx
->active_sampler_views
&= ~mask
;
273 etna_fragtex_set_sampler_views(struct etna_context
*ctx
, unsigned nr
,
274 struct pipe_sampler_view
**views
)
277 unsigned end
= start
+ ctx
->specs
.fragment_sampler_count
;
279 set_sampler_views(ctx
, start
, end
, nr
, views
);
280 ctx
->num_fragment_sampler_views
= nr
;
285 etna_vertex_set_sampler_views(struct etna_context
*ctx
, unsigned nr
,
286 struct pipe_sampler_view
**views
)
288 unsigned start
= ctx
->specs
.vertex_sampler_offset
;
289 unsigned end
= start
+ ctx
->specs
.vertex_sampler_count
;
291 set_sampler_views(ctx
, start
, end
, nr
, views
);
295 etna_set_sampler_views(struct pipe_context
*pctx
, enum pipe_shader_type shader
,
296 unsigned start_slot
, unsigned num_views
,
297 struct pipe_sampler_view
**views
)
299 struct etna_context
*ctx
= etna_context(pctx
);
300 assert(start_slot
== 0);
302 ctx
->dirty
|= ETNA_DIRTY_SAMPLER_VIEWS
| ETNA_DIRTY_TEXTURE_CACHES
;
304 for (unsigned idx
= 0; idx
< num_views
; ++idx
) {
306 etna_update_sampler_source(views
[idx
]);
310 case PIPE_SHADER_FRAGMENT
:
311 etna_fragtex_set_sampler_views(ctx
, num_views
, views
);
313 case PIPE_SHADER_VERTEX
:
314 etna_vertex_set_sampler_views(ctx
, num_views
, views
);
321 etna_texture_barrier(struct pipe_context
*pctx
, unsigned flags
)
323 struct etna_context
*ctx
= etna_context(pctx
);
324 /* clear color and texture cache to make sure that texture unit reads
325 * what has been written */
326 etna_set_state(ctx
->stream
, VIVS_GL_FLUSH_CACHE
, VIVS_GL_FLUSH_CACHE_COLOR
| VIVS_GL_FLUSH_CACHE_TEXTURE
);
330 etna_texture_init(struct pipe_context
*pctx
)
332 pctx
->create_sampler_state
= etna_create_sampler_state
;
333 pctx
->bind_sampler_states
= etna_bind_sampler_states
;
334 pctx
->delete_sampler_state
= etna_delete_sampler_state
;
335 pctx
->set_sampler_views
= etna_set_sampler_views
;
336 pctx
->create_sampler_view
= etna_create_sampler_view
;
337 pctx
->sampler_view_destroy
= etna_sampler_view_destroy
;
338 pctx
->texture_barrier
= etna_texture_barrier
;