iris: sync bugfixes from brw_bufmgr
[mesa.git] / src / gallium / drivers / iris / iris_border_color.c
1 /*
2 * Copyright © 2018 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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.
22 */
23
24 /**
25 * @file iris_border_color.c
26 *
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.
30 *
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.
36 *
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.
42 *
43 * If it ever does fill up, we simply flush.
44 */
45
46 #include <stdlib.h>
47 #include "util/u_math.h"
48 #include "iris_binder.h"
49 #include "iris_bufmgr.h"
50 #include "iris_context.h"
51
52 #define BC_ALIGNMENT 64
53
54 static bool
55 color_equals(const void *a, const void *b)
56 {
57 return memcmp(a, b, sizeof(union pipe_color_union)) == 0;
58 }
59
60 static uint32_t
61 color_hash(const void *key)
62 {
63 return _mesa_hash_data(key, sizeof(union pipe_color_union));
64 }
65
66 static void
67 iris_reset_border_color_pool(struct iris_border_color_pool *pool,
68 struct iris_bufmgr *bufmgr)
69 {
70 _mesa_hash_table_clear(pool->ht, NULL);
71
72 iris_bo_unreference(pool->bo);
73
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);
78
79 /* Don't make 0 a valid offset - tools treat that as a NULL pointer. */
80 pool->insert_point = BC_ALIGNMENT;
81 }
82
83 void
84 iris_init_border_color_pool(struct iris_context *ice)
85 {
86 struct iris_screen *screen = (void *) ice->ctx.screen;
87 struct iris_bufmgr *bufmgr = screen->bufmgr;
88
89 struct iris_border_color_pool *pool = &ice->state.border_color_pool;
90
91 pool->bo = NULL;
92 pool->ht = _mesa_hash_table_create(ice, color_hash, color_equals);
93
94 iris_reset_border_color_pool(pool, bufmgr);
95 }
96
97 /**
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.
100 */
101 void
102 iris_border_color_pool_reserve(struct iris_context *ice, unsigned count)
103 {
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;
107
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);
112
113 iris_reset_border_color_pool(pool, pool->bo->bufmgr);
114 }
115 }
116
117 /**
118 * Upload a border color (or use a cached version).
119 *
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().
122 */
123 uint32_t
124 iris_upload_border_color(struct iris_context *ice,
125 union pipe_color_union *color)
126 {
127 struct iris_border_color_pool *pool = &ice->state.border_color_pool;
128
129 uint32_t hash = color_hash(color);
130 struct hash_entry *entry =
131 _mesa_hash_table_search_pre_hashed(pool->ht, hash, color);
132 if (entry)
133 return (uintptr_t) entry->data;
134
135 assert(pool->insert_point + BC_ALIGNMENT < IRIS_BORDER_COLOR_POOL_SIZE);
136
137 uint32_t offset = pool->insert_point;
138 memcpy(pool->map + offset, color, sizeof(*color));
139 pool->insert_point += BC_ALIGNMENT;
140
141 _mesa_hash_table_insert_pre_hashed(pool->ht, hash, color,
142 (void *) (uintptr_t) offset);
143 return offset;
144 }
145