2 * Copyright (C) 2016 Rob Clark <robclark@freedesktop.org>
3 * Copyright © 2018 Google, Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * Rob Clark <robclark@freedesktop.org>
28 #include "pipe/p_state.h"
29 #include "util/u_string.h"
30 #include "util/u_memory.h"
31 #include "util/u_inlines.h"
32 #include "util/u_format.h"
33 #include "util/hash_table.h"
35 #include "fd6_texture.h"
36 #include "fd6_resource.h"
37 #include "fd6_format.h"
40 static void fd6_texture_state_destroy(struct fd6_texture_state
*state
);
42 static enum a6xx_tex_clamp
43 tex_clamp(unsigned wrap
, bool clamp_to_edge
, bool *needs_border
)
45 /* Hardware does not support _CLAMP, but we emulate it: */
46 if (wrap
== PIPE_TEX_WRAP_CLAMP
) {
47 wrap
= (clamp_to_edge
) ?
48 PIPE_TEX_WRAP_CLAMP_TO_EDGE
: PIPE_TEX_WRAP_CLAMP_TO_BORDER
;
52 case PIPE_TEX_WRAP_REPEAT
:
53 return A6XX_TEX_REPEAT
;
54 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
55 return A6XX_TEX_CLAMP_TO_EDGE
;
56 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
58 return A6XX_TEX_CLAMP_TO_BORDER
;
59 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
60 /* only works for PoT.. need to emulate otherwise! */
61 return A6XX_TEX_MIRROR_CLAMP
;
62 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
63 return A6XX_TEX_MIRROR_REPEAT
;
64 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
65 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
66 /* these two we could perhaps emulate, but we currently
67 * just don't advertise PIPE_CAP_TEXTURE_MIRROR_CLAMP
70 DBG("invalid wrap: %u", wrap
);
75 static enum a6xx_tex_filter
76 tex_filter(unsigned filter
, bool aniso
)
79 case PIPE_TEX_FILTER_NEAREST
:
80 return A6XX_TEX_NEAREST
;
81 case PIPE_TEX_FILTER_LINEAR
:
82 return aniso
? A6XX_TEX_ANISO
: A6XX_TEX_LINEAR
;
84 DBG("invalid filter: %u", filter
);
90 fd6_sampler_state_create(struct pipe_context
*pctx
,
91 const struct pipe_sampler_state
*cso
)
93 struct fd6_sampler_stateobj
*so
= CALLOC_STRUCT(fd6_sampler_stateobj
);
94 unsigned aniso
= util_last_bit(MIN2(cso
->max_anisotropy
>> 1, 8));
95 bool miplinear
= false;
102 so
->seqno
= ++fd6_context(fd_context(pctx
))->tex_seqno
;
104 if (cso
->min_mip_filter
== PIPE_TEX_MIPFILTER_LINEAR
)
108 * For nearest filtering, _CLAMP means _CLAMP_TO_EDGE; for linear
109 * filtering, _CLAMP means _CLAMP_TO_BORDER while additionally
110 * clamping the texture coordinates to [0.0, 1.0].
112 * The clamping will be taken care of in the shaders. There are two
113 * filters here, but let the minification one has a say.
115 clamp_to_edge
= (cso
->min_img_filter
== PIPE_TEX_FILTER_NEAREST
);
116 if (!clamp_to_edge
) {
117 so
->saturate_s
= (cso
->wrap_s
== PIPE_TEX_WRAP_CLAMP
);
118 so
->saturate_t
= (cso
->wrap_t
== PIPE_TEX_WRAP_CLAMP
);
119 so
->saturate_r
= (cso
->wrap_r
== PIPE_TEX_WRAP_CLAMP
);
122 so
->needs_border
= false;
124 COND(miplinear
, A6XX_TEX_SAMP_0_MIPFILTER_LINEAR_NEAR
) |
125 A6XX_TEX_SAMP_0_XY_MAG(tex_filter(cso
->mag_img_filter
, aniso
)) |
126 A6XX_TEX_SAMP_0_XY_MIN(tex_filter(cso
->min_img_filter
, aniso
)) |
127 A6XX_TEX_SAMP_0_ANISO(aniso
) |
128 A6XX_TEX_SAMP_0_WRAP_S(tex_clamp(cso
->wrap_s
, clamp_to_edge
, &so
->needs_border
)) |
129 A6XX_TEX_SAMP_0_WRAP_T(tex_clamp(cso
->wrap_t
, clamp_to_edge
, &so
->needs_border
)) |
130 A6XX_TEX_SAMP_0_WRAP_R(tex_clamp(cso
->wrap_r
, clamp_to_edge
, &so
->needs_border
));
133 COND(cso
->min_mip_filter
== PIPE_TEX_MIPFILTER_NONE
,
134 A6XX_TEX_SAMP_1_MIPFILTER_LINEAR_FAR
) |
135 COND(!cso
->seamless_cube_map
, A6XX_TEX_SAMP_1_CUBEMAPSEAMLESSFILTOFF
) |
136 COND(!cso
->normalized_coords
, A6XX_TEX_SAMP_1_UNNORM_COORDS
);
138 so
->texsamp0
|= A6XX_TEX_SAMP_0_LOD_BIAS(cso
->lod_bias
);
140 A6XX_TEX_SAMP_1_MIN_LOD(cso
->min_lod
) |
141 A6XX_TEX_SAMP_1_MAX_LOD(cso
->max_lod
);
143 if (cso
->compare_mode
)
144 so
->texsamp1
|= A6XX_TEX_SAMP_1_COMPARE_FUNC(cso
->compare_func
); /* maps 1:1 */
150 fd6_sampler_state_delete(struct pipe_context
*pctx
, void *hwcso
)
152 struct fd6_context
*fd6_ctx
= fd6_context(fd_context(pctx
));
153 struct fd6_sampler_stateobj
*samp
= hwcso
;
155 hash_table_foreach(fd6_ctx
->tex_cache
, entry
) {
156 struct fd6_texture_state
*state
= entry
->data
;
158 for (unsigned i
= 0; i
< ARRAY_SIZE(state
->key
.samp
); i
++) {
159 if (samp
->seqno
== state
->key
.samp
[i
].seqno
) {
160 fd6_texture_state_destroy(entry
->data
);
161 _mesa_hash_table_remove(fd6_ctx
->tex_cache
, entry
);
171 fd6_sampler_states_bind(struct pipe_context
*pctx
,
172 enum pipe_shader_type shader
, unsigned start
,
173 unsigned nr
, void **hwcso
)
175 struct fd_context
*ctx
= fd_context(pctx
);
176 struct fd6_context
*fd6_ctx
= fd6_context(ctx
);
177 uint16_t saturate_s
= 0, saturate_t
= 0, saturate_r
= 0;
183 for (i
= 0; i
< nr
; i
++) {
185 struct fd6_sampler_stateobj
*sampler
=
186 fd6_sampler_stateobj(hwcso
[i
]);
187 if (sampler
->saturate_s
)
188 saturate_s
|= (1 << i
);
189 if (sampler
->saturate_t
)
190 saturate_t
|= (1 << i
);
191 if (sampler
->saturate_r
)
192 saturate_r
|= (1 << i
);
196 fd_sampler_states_bind(pctx
, shader
, start
, nr
, hwcso
);
198 if (shader
== PIPE_SHADER_FRAGMENT
) {
203 fd6_ctx
->fsaturate_s
= saturate_s
;
204 fd6_ctx
->fsaturate_t
= saturate_t
;
205 fd6_ctx
->fsaturate_r
= saturate_r
;
206 } else if (shader
== PIPE_SHADER_VERTEX
) {
211 fd6_ctx
->vsaturate_s
= saturate_s
;
212 fd6_ctx
->vsaturate_t
= saturate_t
;
213 fd6_ctx
->vsaturate_r
= saturate_r
;
217 static struct pipe_sampler_view
*
218 fd6_sampler_view_create(struct pipe_context
*pctx
, struct pipe_resource
*prsc
,
219 const struct pipe_sampler_view
*cso
)
221 struct fd6_pipe_sampler_view
*so
= CALLOC_STRUCT(fd6_pipe_sampler_view
);
222 struct fd_resource
*rsc
= fd_resource(prsc
);
223 enum pipe_format format
= cso
->format
;
224 unsigned lvl
, layers
= 0;
229 fd6_validate_format(fd_context(pctx
), rsc
, format
);
231 if (format
== PIPE_FORMAT_X32_S8X24_UINT
) {
233 format
= rsc
->base
.format
;
237 pipe_reference(NULL
, &prsc
->reference
);
238 so
->base
.texture
= prsc
;
239 so
->base
.reference
.count
= 1;
240 so
->base
.context
= pctx
;
241 so
->seqno
= ++fd6_context(fd_context(pctx
))->tex_seqno
;
243 if (cso
->target
== PIPE_BUFFER
) {
244 unsigned elements
= cso
->u
.buf
.size
/ util_format_get_blocksize(format
);
248 A6XX_TEX_CONST_1_WIDTH(elements
& MASK(15)) |
249 A6XX_TEX_CONST_1_HEIGHT(elements
>> 15);
251 A6XX_TEX_CONST_2_UNK4
|
252 A6XX_TEX_CONST_2_UNK31
;
253 so
->offset
= cso
->u
.buf
.offset
;
257 lvl
= fd_sampler_first_level(cso
);
258 miplevels
= fd_sampler_last_level(cso
) - lvl
;
259 layers
= cso
->u
.tex
.last_layer
- cso
->u
.tex
.first_layer
+ 1;
261 so
->texconst0
|= A6XX_TEX_CONST_0_MIPLVLS(miplevels
);
263 A6XX_TEX_CONST_1_WIDTH(u_minify(prsc
->width0
, lvl
)) |
264 A6XX_TEX_CONST_1_HEIGHT(u_minify(prsc
->height0
, lvl
));
266 A6XX_TEX_CONST_2_FETCHSIZE(fd6_pipe2fetchsize(format
)) |
267 A6XX_TEX_CONST_2_PITCH(
268 util_format_get_nblocksx(
269 format
, rsc
->slices
[lvl
].pitch
) * rsc
->cpp
);
270 so
->offset
= fd_resource_offset(rsc
, lvl
, cso
->u
.tex
.first_layer
);
271 so
->ubwc_offset
= fd_resource_ubwc_offset(rsc
, lvl
, cso
->u
.tex
.first_layer
);
272 so
->ubwc_enabled
= fd_resource_ubwc_enabled(rsc
, lvl
);
275 so
->texconst0
|= fd6_tex_const_0(prsc
, lvl
, cso
->format
,
276 cso
->swizzle_r
, cso
->swizzle_g
,
277 cso
->swizzle_b
, cso
->swizzle_a
);
279 if (so
->ubwc_enabled
) {
280 so
->texconst9
|= A6XX_TEX_CONST_9_FLAG_BUFFER_ARRAY_PITCH(rsc
->ubwc_size
);
281 so
->texconst10
|= A6XX_TEX_CONST_10_FLAG_BUFFER_PITCH(rsc
->ubwc_pitch
);
284 so
->texconst2
|= A6XX_TEX_CONST_2_TYPE(fd6_tex_type(cso
->target
));
286 switch (cso
->target
) {
287 case PIPE_TEXTURE_RECT
:
288 case PIPE_TEXTURE_1D
:
289 case PIPE_TEXTURE_2D
:
291 A6XX_TEX_CONST_3_ARRAY_PITCH(rsc
->layer_size
);
293 A6XX_TEX_CONST_5_DEPTH(1);
295 case PIPE_TEXTURE_1D_ARRAY
:
296 case PIPE_TEXTURE_2D_ARRAY
:
298 A6XX_TEX_CONST_3_ARRAY_PITCH(rsc
->layer_size
);
300 A6XX_TEX_CONST_5_DEPTH(layers
);
302 case PIPE_TEXTURE_CUBE
:
303 case PIPE_TEXTURE_CUBE_ARRAY
:
305 A6XX_TEX_CONST_3_ARRAY_PITCH(rsc
->layer_size
);
307 A6XX_TEX_CONST_5_DEPTH(layers
/ 6);
309 case PIPE_TEXTURE_3D
:
311 A6XX_TEX_CONST_3_MIN_LAYERSZ(rsc
->slices
[prsc
->last_level
].size0
) |
312 A6XX_TEX_CONST_3_ARRAY_PITCH(rsc
->slices
[lvl
].size0
);
314 A6XX_TEX_CONST_5_DEPTH(u_minify(prsc
->depth0
, lvl
));
320 if (so
->ubwc_enabled
) {
321 so
->texconst3
|= A6XX_TEX_CONST_3_FLAG
| A6XX_TEX_CONST_3_UNK27
;
328 fd6_sampler_view_destroy(struct pipe_context
*pctx
,
329 struct pipe_sampler_view
*_view
)
331 struct fd6_context
*fd6_ctx
= fd6_context(fd_context(pctx
));
332 struct fd6_pipe_sampler_view
*view
= fd6_pipe_sampler_view(_view
);
334 hash_table_foreach(fd6_ctx
->tex_cache
, entry
) {
335 struct fd6_texture_state
*state
= entry
->data
;
337 for (unsigned i
= 0; i
< ARRAY_SIZE(state
->key
.view
); i
++) {
338 if (view
->seqno
== state
->key
.view
[i
].seqno
) {
339 fd6_texture_state_destroy(entry
->data
);
340 _mesa_hash_table_remove(fd6_ctx
->tex_cache
, entry
);
346 pipe_resource_reference(&view
->base
.texture
, NULL
);
353 key_hash(const void *_key
)
355 const struct fd6_texture_key
*key
= _key
;
356 uint32_t hash
= _mesa_fnv32_1a_offset_bias
;
357 hash
= _mesa_fnv32_1a_accumulate_block(hash
, key
, sizeof(*key
));
362 key_equals(const void *_a
, const void *_b
)
364 const struct fd6_texture_key
*a
= _a
;
365 const struct fd6_texture_key
*b
= _b
;
366 return memcmp(a
, b
, sizeof(struct fd6_texture_key
)) == 0;
369 struct fd6_texture_state
*
370 fd6_texture_state(struct fd_context
*ctx
, enum pipe_shader_type type
,
371 struct fd_texture_stateobj
*tex
)
373 struct fd6_context
*fd6_ctx
= fd6_context(ctx
);
374 struct fd6_texture_key key
;
375 bool needs_border
= false;
377 memset(&key
, 0, sizeof(key
));
379 for (unsigned i
= 0; i
< tex
->num_textures
; i
++) {
380 if (!tex
->textures
[i
])
383 struct fd6_pipe_sampler_view
*view
=
384 fd6_pipe_sampler_view(tex
->textures
[i
]);
386 key
.view
[i
].rsc_seqno
= fd_resource(view
->base
.texture
)->seqno
;
387 key
.view
[i
].seqno
= view
->seqno
;
390 for (unsigned i
= 0; i
< tex
->num_samplers
; i
++) {
391 if (!tex
->samplers
[i
])
394 struct fd6_sampler_stateobj
*sampler
=
395 fd6_sampler_stateobj(tex
->samplers
[i
]);
397 key
.samp
[i
].seqno
= sampler
->seqno
;
399 needs_border
|= sampler
->needs_border
;
403 key
.bcolor_offset
= fd6_border_color_offset(ctx
, type
, tex
);
405 uint32_t hash
= key_hash(&key
);
406 struct hash_entry
*entry
=
407 _mesa_hash_table_search_pre_hashed(fd6_ctx
->tex_cache
, hash
, &key
);
413 struct fd6_texture_state
*state
= CALLOC_STRUCT(fd6_texture_state
);
416 state
->stateobj
= fd_ringbuffer_new_object(ctx
->pipe
, 0x1000);
417 state
->needs_border
= needs_border
;
419 fd6_emit_textures(ctx
->pipe
, state
->stateobj
, type
, tex
, key
.bcolor_offset
,
422 /* NOTE: uses copy of key in state obj, because pointer passed by caller
423 * is probably on the stack
425 _mesa_hash_table_insert_pre_hashed(fd6_ctx
->tex_cache
, hash
,
432 fd6_texture_state_destroy(struct fd6_texture_state
*state
)
434 fd_ringbuffer_del(state
->stateobj
);
439 fd6_texture_init(struct pipe_context
*pctx
)
441 struct fd6_context
*fd6_ctx
= fd6_context(fd_context(pctx
));
443 pctx
->create_sampler_state
= fd6_sampler_state_create
;
444 pctx
->delete_sampler_state
= fd6_sampler_state_delete
;
445 pctx
->bind_sampler_states
= fd6_sampler_states_bind
;
447 pctx
->create_sampler_view
= fd6_sampler_view_create
;
448 pctx
->sampler_view_destroy
= fd6_sampler_view_destroy
;
449 pctx
->set_sampler_views
= fd_set_sampler_views
;
451 fd6_ctx
->tex_cache
= _mesa_hash_table_create(NULL
, key_hash
, key_equals
);
455 fd6_texture_fini(struct pipe_context
*pctx
)
457 struct fd6_context
*fd6_ctx
= fd6_context(fd_context(pctx
));
459 hash_table_foreach(fd6_ctx
->tex_cache
, entry
) {
460 fd6_texture_state_destroy(entry
->data
);
462 ralloc_free(fd6_ctx
->tex_cache
);