2 * Copyright © 2018 Intel Corporation
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, sublicense,
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 shall be included
12 * in all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
24 * @file iris_border_color.c
26 * Each SAMPLER_STATE points to a SAMPLER_BORDER_COLOR_STATE entry,
27 * describing the color to return when sampling outside the texture
28 * when using CLAMP_TO_BORDER wrap modes.
30 * These must be stored relative to Dynamic State Base Address.
31 * Unfortunately, the hardware designers only gave us a 24-bit pointer
32 * rather than an actual graphics address, so it must be stored in the
33 * bottom 16MB of that memory zone. This means we can't simply use
34 * u_upload_mgr like we do for most state.
36 * To work around this, we maintain a single "border color pool" BO
37 * which we pin at the base of IRIS_MEMZONE_DYNAMIC. Since most border
38 * colors are the same (typically black or white), we maintain a hash
39 * table of known colors, and reuse the same entries. This avoids
40 * wasting a lot of space in the pool.
42 * If it ever does fill up, we simply flush.
46 #include "util/u_math.h"
47 #include "iris_binder.h"
48 #include "iris_bufmgr.h"
49 #include "iris_context.h"
51 #define BC_ALIGNMENT 64
54 color_equals(const void *a
, const void *b
)
56 return memcmp(a
, b
, sizeof(union pipe_color_union
)) == 0;
60 color_hash(const void *key
)
62 return _mesa_hash_data(key
, sizeof(union pipe_color_union
));
66 iris_reset_border_color_pool(struct iris_border_color_pool
*pool
,
67 struct iris_bufmgr
*bufmgr
)
69 _mesa_hash_table_clear(pool
->ht
, NULL
);
71 iris_bo_unreference(pool
->bo
);
73 pool
->bo
= iris_bo_alloc(bufmgr
, "border colors",
74 IRIS_BORDER_COLOR_POOL_SIZE
,
75 IRIS_MEMZONE_BORDER_COLOR_POOL
);
76 pool
->map
= iris_bo_map(NULL
, pool
->bo
, MAP_WRITE
);
78 /* Don't make 0 a valid offset - tools treat that as a NULL pointer. */
79 pool
->insert_point
= BC_ALIGNMENT
;
83 iris_init_border_color_pool(struct iris_context
*ice
)
85 struct iris_screen
*screen
= (void *) ice
->ctx
.screen
;
86 struct iris_bufmgr
*bufmgr
= screen
->bufmgr
;
88 struct iris_border_color_pool
*pool
= &ice
->state
.border_color_pool
;
91 pool
->ht
= _mesa_hash_table_create(ice
, color_hash
, color_equals
);
93 iris_reset_border_color_pool(pool
, bufmgr
);
97 * Reserve space for a number of border colors. If no space, flushes any
98 * batches that are referring to the old BO and makes a new one.
101 iris_border_color_pool_reserve(struct iris_context
*ice
, unsigned count
)
103 struct iris_border_color_pool
*pool
= &ice
->state
.border_color_pool
;
104 const unsigned remaining_entries
=
105 (IRIS_BORDER_COLOR_POOL_SIZE
- pool
->insert_point
) / BC_ALIGNMENT
;
107 if (remaining_entries
< count
) {
108 /* It's safe to flush because we're called outside of state upload. */
109 if (iris_batch_references(&ice
->render_batch
, pool
->bo
))
110 iris_batch_flush(&ice
->render_batch
);
112 iris_reset_border_color_pool(pool
, pool
->bo
->bufmgr
);
117 * Upload a border color (or use a cached version).
119 * Returns the offset into the border color pool BO. Note that you must
120 * reserve space ahead of time by calling iris_border_color_pool_reserve().
123 iris_upload_border_color(struct iris_context
*ice
,
124 union pipe_color_union
*color
)
126 struct iris_border_color_pool
*pool
= &ice
->state
.border_color_pool
;
128 uint32_t hash
= color_hash(color
);
129 struct hash_entry
*entry
=
130 _mesa_hash_table_search_pre_hashed(pool
->ht
, hash
, color
);
132 return (uintptr_t) entry
->data
;
134 assert(pool
->insert_point
+ BC_ALIGNMENT
< IRIS_BORDER_COLOR_POOL_SIZE
);
136 uint32_t offset
= pool
->insert_point
;
137 memcpy(pool
->map
+ offset
, color
, sizeof(*color
));
138 pool
->insert_point
+= BC_ALIGNMENT
;
140 _mesa_hash_table_insert_pre_hashed(pool
->ht
, hash
, color
,
141 (void *) (uintptr_t) offset
);