• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 return the black border. We
43  * can't simply flush since the BO is shared by every context. If we
44  * ever need we may choose to have multiple BOs, refcount them and
45  * then recycle when unused.
46  */
47 
48 #include <stdlib.h>
49 #include "util/u_math.h"
50 #include "iris_binder.h"
51 #include "iris_bufmgr.h"
52 #include "iris_context.h"
53 
54 #define BC_ALIGNMENT 64
55 #define BC_BLACK 64
56 
57 static bool
color_equals(const void * a,const void * b)58 color_equals(const void *a, const void *b)
59 {
60    return memcmp(a, b, sizeof(union pipe_color_union)) == 0;
61 }
62 
63 static uint32_t
color_hash(const void * key)64 color_hash(const void *key)
65 {
66    return _mesa_hash_data(key, sizeof(union pipe_color_union));
67 }
68 
69 void
iris_init_border_color_pool(struct iris_bufmgr * bufmgr,struct iris_border_color_pool * pool)70 iris_init_border_color_pool(struct iris_bufmgr *bufmgr,
71                             struct iris_border_color_pool *pool)
72 {
73    simple_mtx_init(&pool->lock, mtx_plain);
74 
75    pool->ht = _mesa_hash_table_create(NULL, color_hash, color_equals);
76 
77    pool->bo = iris_bo_alloc(bufmgr, "border colors",
78                             IRIS_BORDER_COLOR_POOL_SIZE, 64,
79                             IRIS_MEMZONE_BORDER_COLOR_POOL, 0);
80    pool->map = iris_bo_map(NULL, pool->bo, MAP_WRITE);
81 
82    /* Don't make 0 a valid offset - tools treat that as a NULL pointer. */
83    pool->insert_point = BC_ALIGNMENT;
84 
85    union pipe_color_union black = {.f = { 0.0, 0.0, 0.0, 1.0 }};
86    ASSERTED uint32_t black_offset = iris_upload_border_color(pool, &black);
87    assert(black_offset == BC_BLACK);
88 }
89 
90 void
iris_destroy_border_color_pool(struct iris_border_color_pool * pool)91 iris_destroy_border_color_pool(struct iris_border_color_pool *pool)
92 {
93    iris_bo_unreference(pool->bo);
94    ralloc_free(pool->ht);
95    simple_mtx_destroy(&pool->lock);
96 }
97 
98 /**
99  * Upload a border color (or use a cached version).
100  *
101  * Returns the offset into the border color pool BO.  Note that you must
102  * reserve space ahead of time by calling iris_border_color_pool_reserve().
103  */
104 uint32_t
iris_upload_border_color(struct iris_border_color_pool * pool,union pipe_color_union * color)105 iris_upload_border_color(struct iris_border_color_pool *pool,
106                          union pipe_color_union *color)
107 {
108    uint32_t offset;
109    uint32_t hash = color_hash(color);
110 
111    simple_mtx_lock(&pool->lock);
112 
113    struct hash_entry *entry =
114       _mesa_hash_table_search_pre_hashed(pool->ht, hash, color);
115    if (entry) {
116       offset = (uintptr_t) entry->data;
117       goto out;
118    }
119 
120    if (pool->insert_point + BC_ALIGNMENT > IRIS_BORDER_COLOR_POOL_SIZE) {
121       static bool warned = false;
122       if (!warned) {
123          fprintf(stderr, "Border color pool is full. Using black instead.\n");
124          warned = true;
125       }
126       offset = BC_BLACK;
127       goto out;
128    }
129 
130    offset = pool->insert_point;
131    memcpy(pool->map + offset, color, sizeof(*color));
132    pool->insert_point += BC_ALIGNMENT;
133 
134    _mesa_hash_table_insert_pre_hashed(pool->ht, hash, pool->map + offset,
135                                       (void *) (uintptr_t) offset);
136 out:
137    simple_mtx_unlock(&pool->lock);
138    return offset;
139 }
140 
141