iris: actually set KSP offsets
[mesa.git] / src / gallium / drivers / iris / iris_program_cache.c
1 /*
2 * Copyright © 2017 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 #include <stdio.h>
24 #include <errno.h>
25 #include "pipe/p_defines.h"
26 #include "pipe/p_state.h"
27 #include "pipe/p_context.h"
28 #include "pipe/p_screen.h"
29 #include "util/u_atomic.h"
30 #include "util/u_upload_mgr.h"
31 #include "compiler/nir/nir.h"
32 #include "compiler/nir/nir_builder.h"
33 #include "intel/compiler/brw_compiler.h"
34 #include "intel/compiler/brw_eu.h"
35 #include "intel/compiler/brw_nir.h"
36 #include "iris_context.h"
37
38 struct keybox {
39 uint8_t size;
40 enum iris_program_cache_id cache_id;
41 uint8_t data[0];
42 };
43
44 static struct keybox *
45 make_keybox(void *mem_ctx,
46 enum iris_program_cache_id cache_id,
47 const void *key)
48 {
49 static const unsigned key_sizes[] = {
50 [IRIS_CACHE_VS] = sizeof(struct brw_vs_prog_key),
51 [IRIS_CACHE_TCS] = sizeof(struct brw_tcs_prog_key),
52 [IRIS_CACHE_TES] = sizeof(struct brw_tes_prog_key),
53 [IRIS_CACHE_GS] = sizeof(struct brw_gs_prog_key),
54 [IRIS_CACHE_FS] = sizeof(struct brw_wm_prog_key),
55 [IRIS_CACHE_CS] = sizeof(struct brw_cs_prog_key),
56 //[IRIS_CACHE_BLORP_BLIT] = sizeof(struct brw_blorp_blit_prog_key),
57 };
58
59 struct keybox *keybox =
60 ralloc_size(mem_ctx, sizeof(struct keybox) + key_sizes[cache_id]);
61
62 keybox->cache_id = cache_id;
63 keybox->size = key_sizes[cache_id];
64 memcpy(keybox->data, key, key_sizes[cache_id]);
65
66 return keybox;
67 }
68
69 static uint32_t
70 keybox_hash(const void *void_key)
71 {
72 const struct keybox *key = void_key;
73 return _mesa_hash_data(&key->cache_id, key->size + sizeof(key->cache_id));
74 }
75
76 static bool
77 keybox_equals(const void *void_a, const void *void_b)
78 {
79 const struct keybox *a = void_a, *b = void_b;
80 if (a->size != b->size)
81 return false;
82
83 return memcmp(a->data, b->data, a->size) == 0;
84 }
85
86 static uint64_t
87 dirty_flag_for_cache(enum iris_program_cache_id cache_id)
88 {
89 assert(cache_id <= MESA_SHADER_STAGES);
90 return IRIS_DIRTY_VS << cache_id;
91 }
92
93 static unsigned
94 get_program_string_id(enum iris_program_cache_id cache_id, const void *key)
95 {
96 switch (cache_id) {
97 case IRIS_CACHE_VS:
98 return ((struct brw_vs_prog_key *) key)->program_string_id;
99 case IRIS_CACHE_TCS:
100 return ((struct brw_tcs_prog_key *) key)->program_string_id;
101 case IRIS_CACHE_TES:
102 return ((struct brw_tes_prog_key *) key)->program_string_id;
103 case IRIS_CACHE_GS:
104 return ((struct brw_gs_prog_key *) key)->program_string_id;
105 case IRIS_CACHE_CS:
106 return ((struct brw_cs_prog_key *) key)->program_string_id;
107 case IRIS_CACHE_FS:
108 return ((struct brw_wm_prog_key *) key)->program_string_id;
109 default:
110 unreachable("no program string id for this kind of program");
111 }
112 }
113
114 /**
115 * Looks for a program in the cache and binds it.
116 *
117 * If no program was found, returns false and leaves the binding alone.
118 */
119 bool
120 iris_bind_cached_shader(struct iris_context *ice,
121 enum iris_program_cache_id cache_id,
122 const void *key)
123 {
124 struct keybox *keybox = make_keybox(ice->shaders.cache, cache_id, key);
125 struct hash_entry *entry =
126 _mesa_hash_table_search(ice->shaders.cache, keybox);
127
128 if (!entry)
129 return false;
130
131 struct iris_compiled_shader *shader = entry->data;
132
133 if (cache_id <= MESA_SHADER_STAGES &&
134 memcmp(shader, ice->shaders.prog[cache_id], sizeof(*shader)) != 0) {
135 ice->shaders.prog[cache_id] = shader;
136 ice->state.dirty |= dirty_flag_for_cache(cache_id);
137 }
138
139 return true;
140 }
141
142 const void *
143 iris_find_previous_compile(const struct iris_context *ice,
144 enum iris_program_cache_id cache_id,
145 unsigned program_string_id)
146 {
147 hash_table_foreach(ice->shaders.cache, entry) {
148 const struct keybox *keybox = entry->key;
149 if (keybox->cache_id == cache_id &&
150 get_program_string_id(cache_id, keybox->data) == program_string_id) {
151 return keybox->data;
152 }
153 }
154
155 return NULL;
156 }
157
158 /**
159 * Look for an existing entry in the cache that has identical assembly code.
160 *
161 * This is useful for programs generating shaders at runtime, where multiple
162 * distinct shaders (from an API perspective) may compile to the same assembly
163 * in our backend. This saves space in the program cache buffer.
164 */
165 static const struct iris_compiled_shader *
166 find_existing_assembly(struct hash_table *cache,
167 const void *assembly,
168 unsigned assembly_size)
169 {
170 hash_table_foreach(cache, entry) {
171 const struct iris_compiled_shader *existing = entry->data;
172 if (existing->prog_data->program_size == assembly_size &&
173 memcmp(existing->map, assembly, assembly_size) == 0)
174 return existing;
175 }
176 return NULL;
177 }
178
179 /**
180 * Upload a new shader to the program cache, and bind it for use.
181 *
182 * \param prog_data must be ralloc'd and will be stolen.
183 */
184 void
185 iris_upload_and_bind_shader(struct iris_context *ice,
186 enum iris_program_cache_id cache_id,
187 const void *key,
188 const void *assembly,
189 struct brw_stage_prog_data *prog_data)
190 {
191 struct iris_screen *screen = (void *) ice->ctx.screen;
192 struct gen_device_info *devinfo = &screen->devinfo;
193 struct hash_table *cache = ice->shaders.cache;
194 struct iris_compiled_shader *shader =
195 ralloc_size(cache, sizeof(struct iris_compiled_shader) +
196 ice->state.derived_program_state_size(cache_id));
197 const struct iris_compiled_shader *existing =
198 find_existing_assembly(cache, assembly, prog_data->program_size);
199
200 /* If we can find a matching prog in the cache already, then reuse the
201 * existing stuff without creating new copy into the underlying buffer
202 * object. This is notably useful for programs generating shaders at
203 * runtime, where multiple shaders may compile to the same thing in our
204 * backend.
205 */
206 if (existing) {
207 shader->offset = existing->offset;
208 } else {
209 shader->buffer = NULL;
210 u_upload_alloc(ice->shaders.uploader, 0, prog_data->program_size,
211 64, &shader->offset, &shader->buffer, &shader->map);
212 memcpy(shader->map, assembly, prog_data->program_size);
213 }
214
215 shader->prog_data = prog_data;
216
217 ralloc_steal(shader, shader->prog_data);
218 ralloc_steal(shader->prog_data, prog_data->param);
219 ralloc_steal(shader->prog_data, prog_data->pull_param);
220
221 /* Store the 3DSTATE shader packets and other derived state. */
222 ice->state.set_derived_program_state(devinfo, cache_id, shader);
223
224 struct keybox *keybox = make_keybox(cache, cache_id, key);
225 _mesa_hash_table_insert(ice->shaders.cache, keybox, shader);
226
227 if (cache_id <= MESA_SHADER_STAGES) {
228 ice->shaders.prog[cache_id] = shader;
229 ice->state.dirty |= dirty_flag_for_cache(cache_id);
230 }
231 }
232
233 void
234 iris_init_program_cache(struct iris_context *ice)
235 {
236 ice->shaders.cache =
237 _mesa_hash_table_create(ice, keybox_hash, keybox_equals);
238
239 ice->shaders.uploader =
240 u_upload_create(&ice->ctx, 16384, PIPE_BIND_CUSTOM, PIPE_USAGE_IMMUTABLE,
241 IRIS_RESOURCE_FLAG_INSTRUCTION_CACHE);
242 }
243
244 void
245 iris_destroy_program_cache(struct iris_context *ice)
246 {
247 for (int i = 0; i < MESA_SHADER_STAGES; i++) {
248 ice->shaders.prog[i] = NULL;
249 }
250
251 u_upload_destroy(ice->shaders.uploader);
252
253 ralloc_free(ice->shaders.cache);
254 }
255
256 static const char *
257 cache_name(enum iris_program_cache_id cache_id)
258 {
259 if (cache_id == IRIS_CACHE_BLORP_BLIT)
260 return "BLORP";
261
262 return _mesa_shader_stage_to_string(cache_id);
263 }
264
265 void
266 iris_print_program_cache(struct iris_context *ice)
267 {
268 struct iris_screen *screen = (struct iris_screen *)ice->ctx.screen;
269 const struct gen_device_info *devinfo = &screen->devinfo;
270
271 hash_table_foreach(ice->shaders.cache, entry) {
272 const struct keybox *keybox = entry->key;
273 struct iris_compiled_shader *shader = entry->data;
274 fprintf(stderr, "%s:\n", cache_name(keybox->cache_id));
275 brw_disassemble(devinfo, shader->map, 0,
276 shader->prog_data->program_size, stderr);
277 }
278 }