i965: Disable shader cache with INTEL_DEBUG=shader_time
[mesa.git] / src / mesa / drivers / dri / i965 / brw_disk_cache.c
1 /*
2 * Copyright © 2014 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 (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 NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include "compiler/blob.h"
25 #include "compiler/glsl/ir_uniform.h"
26 #include "compiler/glsl/shader_cache.h"
27 #include "main/mtypes.h"
28 #include "util/build_id.h"
29 #include "util/debug.h"
30 #include "util/disk_cache.h"
31 #include "util/macros.h"
32 #include "util/mesa-sha1.h"
33
34 #include "compiler/brw_eu.h"
35 #include "common/gen_debug.h"
36
37 #include "brw_context.h"
38 #include "brw_program.h"
39 #include "brw_cs.h"
40 #include "brw_gs.h"
41 #include "brw_state.h"
42 #include "brw_vs.h"
43 #include "brw_wm.h"
44
45 static bool
46 debug_enabled_for_stage(gl_shader_stage stage)
47 {
48 static const uint64_t stage_debug_flags[] = {
49 DEBUG_VS, DEBUG_TCS, DEBUG_TES, DEBUG_GS, DEBUG_WM, DEBUG_CS,
50 };
51 assert((int)stage >= 0 && stage < ARRAY_SIZE(stage_debug_flags));
52 return (INTEL_DEBUG & stage_debug_flags[stage]) != 0;
53 }
54
55 static void
56 gen_shader_sha1(struct brw_context *brw, struct gl_program *prog,
57 gl_shader_stage stage, void *key, unsigned char *out_sha1)
58 {
59 char sha1_buf[41];
60 unsigned char sha1[20];
61 char manifest[256];
62 int offset = 0;
63
64 _mesa_sha1_format(sha1_buf, prog->sh.data->sha1);
65 offset += snprintf(manifest, sizeof(manifest), "program: %s\n", sha1_buf);
66
67 _mesa_sha1_compute(key, brw_prog_key_size(stage), sha1);
68 _mesa_sha1_format(sha1_buf, sha1);
69 offset += snprintf(manifest + offset, sizeof(manifest) - offset,
70 "%s_key: %s\n", _mesa_shader_stage_to_abbrev(stage),
71 sha1_buf);
72
73 _mesa_sha1_compute(manifest, strlen(manifest), out_sha1);
74 }
75
76 static bool
77 read_blob_program_data(struct blob_reader *binary, struct gl_program *prog,
78 gl_shader_stage stage, const uint8_t **program,
79 struct brw_stage_prog_data *prog_data)
80 {
81 return
82 brw_read_blob_program_data(binary, prog, stage, program, prog_data) &&
83 (binary->current == binary->end);
84 }
85
86 static bool
87 read_and_upload(struct brw_context *brw, struct disk_cache *cache,
88 struct gl_program *prog, gl_shader_stage stage)
89 {
90 unsigned char binary_sha1[20];
91
92 union brw_any_prog_key prog_key;
93
94 switch (stage) {
95 case MESA_SHADER_VERTEX:
96 brw_vs_populate_key(brw, &prog_key.vs);
97 break;
98 case MESA_SHADER_TESS_CTRL:
99 brw_tcs_populate_key(brw, &prog_key.tcs);
100 break;
101 case MESA_SHADER_TESS_EVAL:
102 brw_tes_populate_key(brw, &prog_key.tes);
103 break;
104 case MESA_SHADER_GEOMETRY:
105 brw_gs_populate_key(brw, &prog_key.gs);
106 break;
107 case MESA_SHADER_FRAGMENT:
108 brw_wm_populate_key(brw, &prog_key.wm);
109 break;
110 case MESA_SHADER_COMPUTE:
111 brw_cs_populate_key(brw, &prog_key.cs);
112 break;
113 default:
114 unreachable("Unsupported stage!");
115 }
116
117 /* We don't care what instance of the program it is for the disk cache hash
118 * lookup, so set the id to 0 for the sha1 hashing. program_string_id will
119 * be set below.
120 */
121 brw_prog_key_set_id(&prog_key, stage, 0);
122
123 gen_shader_sha1(brw, prog, stage, &prog_key, binary_sha1);
124
125 size_t buffer_size;
126 uint8_t *buffer = disk_cache_get(cache, binary_sha1, &buffer_size);
127 if (buffer == NULL) {
128 if (brw->ctx._Shader->Flags & GLSL_CACHE_INFO) {
129 char sha1_buf[41];
130 _mesa_sha1_format(sha1_buf, binary_sha1);
131 fprintf(stderr, "No cached %s binary found for: %s\n",
132 _mesa_shader_stage_to_abbrev(stage), sha1_buf);
133 }
134 return false;
135 }
136
137 if (brw->ctx._Shader->Flags & GLSL_CACHE_INFO) {
138 char sha1_buf[41];
139 _mesa_sha1_format(sha1_buf, binary_sha1);
140 fprintf(stderr, "attempting to populate bo cache with binary: %s\n",
141 sha1_buf);
142 }
143
144 struct blob_reader binary;
145 blob_reader_init(&binary, buffer, buffer_size);
146
147 const uint8_t *program;
148 struct brw_stage_prog_data *prog_data =
149 ralloc_size(NULL, sizeof(union brw_any_prog_data));
150 if (!read_blob_program_data(&binary, prog, stage, &program, prog_data)) {
151 /* Something very bad has gone wrong discard the item from the cache and
152 * rebuild from source.
153 */
154 if (brw->ctx._Shader->Flags & GLSL_CACHE_INFO) {
155 fprintf(stderr, "Error reading program from cache (invalid i965 "
156 "cache item)\n");
157 }
158
159 disk_cache_remove(cache, binary_sha1);
160 ralloc_free(prog_data);
161 free(buffer);
162 return false;
163 }
164
165 enum brw_cache_id cache_id;
166 struct brw_stage_state *stage_state;
167
168 switch (stage) {
169 case MESA_SHADER_VERTEX:
170 cache_id = BRW_CACHE_VS_PROG;
171 stage_state = &brw->vs.base;
172 break;
173 case MESA_SHADER_TESS_CTRL:
174 cache_id = BRW_CACHE_TCS_PROG;
175 stage_state = &brw->tcs.base;
176 break;
177 case MESA_SHADER_TESS_EVAL:
178 cache_id = BRW_CACHE_TES_PROG;
179 stage_state = &brw->tes.base;
180 break;
181 case MESA_SHADER_GEOMETRY:
182 cache_id = BRW_CACHE_GS_PROG;
183 stage_state = &brw->gs.base;
184 break;
185 case MESA_SHADER_FRAGMENT:
186 cache_id = BRW_CACHE_FS_PROG;
187 stage_state = &brw->wm.base;
188 break;
189 case MESA_SHADER_COMPUTE:
190 cache_id = BRW_CACHE_CS_PROG;
191 stage_state = &brw->cs.base;
192 break;
193 default:
194 unreachable("Unsupported stage!");
195 }
196
197 brw_prog_key_set_id(&prog_key, stage, brw_program(prog)->id);
198
199 brw_alloc_stage_scratch(brw, stage_state, prog_data->total_scratch);
200
201 if (unlikely(debug_enabled_for_stage(stage))) {
202 fprintf(stderr, "NIR for %s program %d loaded from disk shader cache:\n",
203 _mesa_shader_stage_to_abbrev(stage), brw_program(prog)->id);
204 brw_program_deserialize_driver_blob(&brw->ctx, prog, stage);
205 nir_shader *nir = prog->nir;
206 nir_print_shader(nir, stderr);
207 fprintf(stderr, "Native code for %s %s shader %s from disk cache:\n",
208 nir->info.label ? nir->info.label : "unnamed",
209 _mesa_shader_stage_to_string(nir->info.stage), nir->info.name);
210 brw_disassemble(&brw->screen->devinfo, program, 0,
211 prog_data->program_size, stderr);
212 }
213
214 brw_upload_cache(&brw->cache, cache_id, &prog_key, brw_prog_key_size(stage),
215 program, prog_data->program_size, prog_data,
216 brw_prog_data_size(stage), &stage_state->prog_offset,
217 &stage_state->prog_data);
218
219 prog->program_written_to_cache = true;
220
221 ralloc_free(prog_data);
222 free(buffer);
223
224 return true;
225 }
226
227 bool
228 brw_disk_cache_upload_program(struct brw_context *brw, gl_shader_stage stage)
229 {
230 struct disk_cache *cache = brw->ctx.Cache;
231 if (cache == NULL)
232 return false;
233
234 struct gl_program *prog = brw->ctx._Shader->CurrentProgram[stage];
235 if (prog == NULL)
236 return false;
237
238 if (brw->ctx._Shader->Flags & GLSL_CACHE_FALLBACK)
239 goto fail;
240
241 if (!read_and_upload(brw, cache, prog, stage))
242 goto fail;
243
244 if (brw->ctx._Shader->Flags & GLSL_CACHE_INFO) {
245 fprintf(stderr, "read gen program from cache\n");
246 }
247
248 return true;
249
250 fail:
251 prog->program_written_to_cache = false;
252 if (brw->ctx._Shader->Flags & GLSL_CACHE_INFO) {
253 fprintf(stderr, "falling back to nir %s.\n",
254 _mesa_shader_stage_to_abbrev(prog->info.stage));
255 }
256
257 brw_program_deserialize_driver_blob(&brw->ctx, prog, stage);
258
259 return false;
260 }
261
262 static void
263 write_program_data(struct brw_context *brw, struct gl_program *prog,
264 void *key, struct brw_stage_prog_data *prog_data,
265 uint32_t prog_offset, struct disk_cache *cache,
266 gl_shader_stage stage)
267 {
268 struct blob binary;
269 blob_init(&binary);
270
271 const void *program_map = brw->cache.map + prog_offset;
272 /* TODO: Improve perf for non-LLC. It would be best to save it at program
273 * generation time when the program is in normal memory accessible with
274 * cache to the CPU. Another easier change would be to use
275 * _mesa_streaming_load_memcpy to read from the program mapped memory. */
276 brw_write_blob_program_data(&binary, stage, program_map, prog_data);
277
278 unsigned char sha1[20];
279 char buf[41];
280 gen_shader_sha1(brw, prog, stage, key, sha1);
281 _mesa_sha1_format(buf, sha1);
282 if (brw->ctx._Shader->Flags & GLSL_CACHE_INFO) {
283 fprintf(stderr, "putting binary in cache: %s\n", buf);
284 }
285
286 disk_cache_put(cache, sha1, binary.data, binary.size, NULL);
287
288 prog->program_written_to_cache = true;
289 blob_finish(&binary);
290 }
291
292 void
293 brw_disk_cache_write_render_programs(struct brw_context *brw)
294 {
295 struct disk_cache *cache = brw->ctx.Cache;
296 if (cache == NULL)
297 return;
298
299 struct gl_program *prog =
300 brw->ctx._Shader->CurrentProgram[MESA_SHADER_VERTEX];
301 if (prog && !prog->program_written_to_cache) {
302 struct brw_vs_prog_key vs_key;
303 brw_vs_populate_key(brw, &vs_key);
304 vs_key.program_string_id = 0;
305
306 write_program_data(brw, prog, &vs_key, brw->vs.base.prog_data,
307 brw->vs.base.prog_offset, cache,
308 MESA_SHADER_VERTEX);
309 }
310
311 prog = brw->ctx._Shader->CurrentProgram[MESA_SHADER_TESS_CTRL];
312 if (prog && !prog->program_written_to_cache) {
313 struct brw_tcs_prog_key tcs_key;
314 brw_tcs_populate_key(brw, &tcs_key);
315 tcs_key.program_string_id = 0;
316
317 write_program_data(brw, prog, &tcs_key, brw->tcs.base.prog_data,
318 brw->tcs.base.prog_offset, cache,
319 MESA_SHADER_TESS_CTRL);
320 }
321
322 prog = brw->ctx._Shader->CurrentProgram[MESA_SHADER_TESS_EVAL];
323 if (prog && !prog->program_written_to_cache) {
324 struct brw_tes_prog_key tes_key;
325 brw_tes_populate_key(brw, &tes_key);
326 tes_key.program_string_id = 0;
327
328 write_program_data(brw, prog, &tes_key, brw->tes.base.prog_data,
329 brw->tes.base.prog_offset, cache,
330 MESA_SHADER_TESS_EVAL);
331 }
332
333 prog = brw->ctx._Shader->CurrentProgram[MESA_SHADER_GEOMETRY];
334 if (prog && !prog->program_written_to_cache) {
335 struct brw_gs_prog_key gs_key;
336 brw_gs_populate_key(brw, &gs_key);
337 gs_key.program_string_id = 0;
338
339 write_program_data(brw, prog, &gs_key, brw->gs.base.prog_data,
340 brw->gs.base.prog_offset, cache,
341 MESA_SHADER_GEOMETRY);
342 }
343
344 prog = brw->ctx._Shader->CurrentProgram[MESA_SHADER_FRAGMENT];
345 if (prog && !prog->program_written_to_cache) {
346 struct brw_wm_prog_key wm_key;
347 brw_wm_populate_key(brw, &wm_key);
348 wm_key.program_string_id = 0;
349
350 write_program_data(brw, prog, &wm_key, brw->wm.base.prog_data,
351 brw->wm.base.prog_offset, cache,
352 MESA_SHADER_FRAGMENT);
353 }
354 }
355
356 void
357 brw_disk_cache_write_compute_program(struct brw_context *brw)
358 {
359 struct disk_cache *cache = brw->ctx.Cache;
360 if (cache == NULL)
361 return;
362
363 struct gl_program *prog =
364 brw->ctx._Shader->CurrentProgram[MESA_SHADER_COMPUTE];
365 if (prog && !prog->program_written_to_cache) {
366 struct brw_cs_prog_key cs_key;
367 brw_cs_populate_key(brw, &cs_key);
368 cs_key.program_string_id = 0;
369
370 write_program_data(brw, prog, &cs_key, brw->cs.base.prog_data,
371 brw->cs.base.prog_offset, cache,
372 MESA_SHADER_COMPUTE);
373 }
374 }
375
376 void
377 brw_disk_cache_init(struct intel_screen *screen)
378 {
379 #ifdef ENABLE_SHADER_CACHE
380 if (INTEL_DEBUG & DEBUG_DISK_CACHE_DISABLE_MASK)
381 return;
382
383 /* array length: print length + null char + 1 extra to verify it is unused */
384 char renderer[11];
385 MAYBE_UNUSED int len = snprintf(renderer, sizeof(renderer), "i965_%04x",
386 screen->deviceID);
387 assert(len == sizeof(renderer) - 2);
388
389 const struct build_id_note *note =
390 build_id_find_nhdr_for_addr(brw_disk_cache_init);
391 assert(note && build_id_length(note) == 20 /* sha1 */);
392
393 const uint8_t *id_sha1 = build_id_data(note);
394 assert(id_sha1);
395
396 char timestamp[41];
397 _mesa_sha1_format(timestamp, id_sha1);
398
399 const uint64_t driver_flags = INTEL_DEBUG & DEBUG_DISK_CACHE_MASK;
400 screen->disk_cache = disk_cache_create(renderer, timestamp, driver_flags);
401 #endif
402 }