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 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
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 AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
25 * @file iris_border_color.c
27 * Each SAMPLER_STATE points to a SAMPLER_BORDER_COLOR_STATE entry,
28 * describing the color to return when sampling outside the texture
29 * when using CLAMP_TO_BORDER wrap modes.
31 * These must be stored relative to Dynamic State Base Address.
32 * Unfortunately, the hardware designers only gave us a 24-bit pointer
33 * rather than an actual graphics address, so it must be stored in the
34 * bottom 16MB of that memory zone. This means we can't simply use
35 * u_upload_mgr like we do for most state.
37 * To work around this, we maintain a single "border color pool" BO
38 * which we pin at the base of IRIS_MEMZONE_DYNAMIC. Since most border
39 * colors are the same (typically black or white), we maintain a hash
40 * table of known colors, and reuse the same entries. This avoids
41 * wasting a lot of space in the pool.
43 * If it ever does fill up, we simply flush.
47 #include "util/u_math.h"
48 #include "iris_binder.h"
49 #include "iris_bufmgr.h"
50 #include "iris_context.h"
52 #define BC_ALIGNMENT 64
55 color_equals(const void *a
, const void *b
)
57 return memcmp(a
, b
, sizeof(union pipe_color_union
)) == 0;
61 color_hash(const void *key
)
63 return _mesa_hash_data(key
, sizeof(union pipe_color_union
));
67 iris_reset_border_color_pool(struct iris_border_color_pool
*pool
,
68 struct iris_bufmgr
*bufmgr
)
70 _mesa_hash_table_clear(pool
->ht
, NULL
);
72 iris_bo_unreference(pool
->bo
);
74 pool
->bo
= iris_bo_alloc(bufmgr
, "border colors",
75 IRIS_BORDER_COLOR_POOL_SIZE
,
76 IRIS_MEMZONE_BORDER_COLOR_POOL
);
77 pool
->map
= iris_bo_map(NULL
, pool
->bo
, MAP_WRITE
);
79 /* Don't make 0 a valid offset - tools treat that as a NULL pointer. */
80 pool
->insert_point
= BC_ALIGNMENT
;
84 iris_init_border_color_pool(struct iris_context
*ice
)
86 struct iris_screen
*screen
= (void *) ice
->ctx
.screen
;
87 struct iris_bufmgr
*bufmgr
= screen
->bufmgr
;
89 struct iris_border_color_pool
*pool
= &ice
->state
.border_color_pool
;
92 pool
->ht
= _mesa_hash_table_create(ice
, color_hash
, color_equals
);
94 iris_reset_border_color_pool(pool
, bufmgr
);
98 * Reserve space for a number of border colors. If no space, flushes any
99 * batches that are referring to the old BO and makes a new one.
102 iris_border_color_pool_reserve(struct iris_context
*ice
, unsigned count
)
104 struct iris_border_color_pool
*pool
= &ice
->state
.border_color_pool
;
105 const unsigned remaining_entries
=
106 (IRIS_BORDER_COLOR_POOL_SIZE
- pool
->insert_point
) / BC_ALIGNMENT
;
108 if (remaining_entries
< count
) {
109 /* It's safe to flush because we're called outside of state upload. */
110 if (iris_batch_references(&ice
->render_batch
, pool
->bo
))
111 iris_batch_flush(&ice
->render_batch
);
113 iris_reset_border_color_pool(pool
, pool
->bo
->bufmgr
);
118 * Upload a border color (or use a cached version).
120 * Returns the offset into the border color pool BO. Note that you must
121 * reserve space ahead of time by calling iris_border_color_pool_reserve().
124 iris_upload_border_color(struct iris_context
*ice
,
125 union pipe_color_union
*color
)
127 struct iris_border_color_pool
*pool
= &ice
->state
.border_color_pool
;
129 uint32_t hash
= color_hash(color
);
130 struct hash_entry
*entry
=
131 _mesa_hash_table_search_pre_hashed(pool
->ht
, hash
, color
);
133 return (uintptr_t) entry
->data
;
135 assert(pool
->insert_point
+ BC_ALIGNMENT
< IRIS_BORDER_COLOR_POOL_SIZE
);
137 uint32_t offset
= pool
->insert_point
;
138 memcpy(pool
->map
+ offset
, color
, sizeof(*color
));
139 pool
->insert_point
+= BC_ALIGNMENT
;
141 _mesa_hash_table_insert_pre_hashed(pool
->ht
, hash
, color
,
142 (void *) (uintptr_t) offset
);