iris: Track per-stage bind history, reduce work accordingly
[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 * 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:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
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.
21 */
22
23 /**
24 * @file iris_border_color.c
25 *
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.
29 *
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.
35 *
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.
41 *
42 * If it ever does fill up, we simply flush.
43 */
44
45 #include <stdlib.h>
46 #include "util/u_math.h"
47 #include "iris_binder.h"
48 #include "iris_bufmgr.h"
49 #include "iris_context.h"
50
51 #define BC_ALIGNMENT 64
52
53 static bool
54 color_equals(const void *a, const void *b)
55 {
56 return memcmp(a, b, sizeof(union pipe_color_union)) == 0;
57 }
58
59 static uint32_t
60 color_hash(const void *key)
61 {
62 return _mesa_hash_data(key, sizeof(union pipe_color_union));
63 }
64
65 static void
66 iris_reset_border_color_pool(struct iris_border_color_pool *pool,
67 struct iris_bufmgr *bufmgr)
68 {
69 _mesa_hash_table_clear(pool->ht, NULL);
70
71 iris_bo_unreference(pool->bo);
72
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);
77
78 /* Don't make 0 a valid offset - tools treat that as a NULL pointer. */
79 pool->insert_point = BC_ALIGNMENT;
80 }
81
82 void
83 iris_init_border_color_pool(struct iris_context *ice)
84 {
85 struct iris_screen *screen = (void *) ice->ctx.screen;
86 struct iris_bufmgr *bufmgr = screen->bufmgr;
87
88 struct iris_border_color_pool *pool = &ice->state.border_color_pool;
89
90 pool->bo = NULL;
91 pool->ht = _mesa_hash_table_create(ice, color_hash, color_equals);
92
93 iris_reset_border_color_pool(pool, bufmgr);
94 }
95
96 void
97 iris_destroy_border_color_pool(struct iris_context *ice)
98 {
99 struct iris_border_color_pool *pool = &ice->state.border_color_pool;
100 iris_bo_unreference(pool->bo);
101 ralloc_free(pool->ht);
102 }
103
104 /**
105 * Reserve space for a number of border colors. If no space, flushes any
106 * batches that are referring to the old BO and makes a new one.
107 */
108 void
109 iris_border_color_pool_reserve(struct iris_context *ice, unsigned count)
110 {
111 struct iris_border_color_pool *pool = &ice->state.border_color_pool;
112 const unsigned remaining_entries =
113 (IRIS_BORDER_COLOR_POOL_SIZE - pool->insert_point) / BC_ALIGNMENT;
114
115 if (remaining_entries < count) {
116 /* It's safe to flush because we're called outside of state upload. */
117 for (int i = 0; i < IRIS_BATCH_COUNT; i++) {
118 if (iris_batch_references(&ice->batches[i], pool->bo))
119 iris_batch_flush(&ice->batches[i]);
120 }
121
122 iris_reset_border_color_pool(pool, pool->bo->bufmgr);
123 }
124 }
125
126 /**
127 * Upload a border color (or use a cached version).
128 *
129 * Returns the offset into the border color pool BO. Note that you must
130 * reserve space ahead of time by calling iris_border_color_pool_reserve().
131 */
132 uint32_t
133 iris_upload_border_color(struct iris_context *ice,
134 union pipe_color_union *color)
135 {
136 struct iris_border_color_pool *pool = &ice->state.border_color_pool;
137
138 uint32_t hash = color_hash(color);
139 struct hash_entry *entry =
140 _mesa_hash_table_search_pre_hashed(pool->ht, hash, color);
141 if (entry)
142 return (uintptr_t) entry->data;
143
144 assert(pool->insert_point + BC_ALIGNMENT < IRIS_BORDER_COLOR_POOL_SIZE);
145
146 uint32_t offset = pool->insert_point;
147 memcpy(pool->map + offset, color, sizeof(*color));
148 pool->insert_point += BC_ALIGNMENT;
149
150 _mesa_hash_table_insert_pre_hashed(pool->ht, hash, color,
151 (void *) (uintptr_t) offset);
152 return offset;
153 }
154