i965: add initial implementation of on disk shader cache
[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/disk_cache.h"
29 #include "util/macros.h"
30 #include "util/mesa-sha1.h"
31
32 #include "brw_context.h"
33 #include "brw_state.h"
34 #include "brw_vs.h"
35 #include "brw_wm.h"
36
37 static void
38 gen_shader_sha1(struct brw_context *brw, struct gl_program *prog,
39 gl_shader_stage stage, void *key, unsigned char *out_sha1)
40 {
41 char sha1_buf[41];
42 unsigned char sha1[20];
43 char manifest[256];
44 int offset = 0;
45
46 _mesa_sha1_format(sha1_buf, prog->sh.data->sha1);
47 offset += snprintf(manifest, sizeof(manifest), "program: %s\n", sha1_buf);
48
49 _mesa_sha1_compute(key, brw_prog_key_size(stage), sha1);
50 _mesa_sha1_format(sha1_buf, sha1);
51 offset += snprintf(manifest + offset, sizeof(manifest) - offset,
52 "%s_key: %s\n", _mesa_shader_stage_to_abbrev(stage),
53 sha1_buf);
54
55 _mesa_sha1_compute(manifest, strlen(manifest), out_sha1);
56 }
57
58 static void
59 write_blob_program_data(struct blob *binary, gl_shader_stage stage,
60 const void *program,
61 struct brw_stage_prog_data *prog_data)
62 {
63 /* Write prog_data to blob. */
64 blob_write_bytes(binary, prog_data, brw_prog_data_size(stage));
65
66 /* Write program to blob. */
67 blob_write_bytes(binary, program, prog_data->program_size);
68
69 /* Write push params */
70 blob_write_bytes(binary, prog_data->param,
71 sizeof(uint32_t) * prog_data->nr_params);
72
73 /* Write pull params */
74 blob_write_bytes(binary, prog_data->pull_param,
75 sizeof(uint32_t) * prog_data->nr_pull_params);
76 }
77
78 static bool
79 read_blob_program_data(struct blob_reader *binary, struct gl_program *prog,
80 gl_shader_stage stage, const uint8_t **program,
81 struct brw_stage_prog_data *prog_data)
82 {
83 /* Read shader prog_data from blob. */
84 blob_copy_bytes(binary, prog_data, brw_prog_data_size(stage));
85 if (binary->overrun)
86 return false;
87
88 /* Read shader program from blob. */
89 *program = blob_read_bytes(binary, prog_data->program_size);
90
91 /* Read push params */
92 prog_data->param = rzalloc_array(NULL, uint32_t, prog_data->nr_params);
93 blob_copy_bytes(binary, prog_data->param,
94 sizeof(uint32_t) * prog_data->nr_params);
95
96 /* Read pull params */
97 prog_data->pull_param = rzalloc_array(NULL, uint32_t,
98 prog_data->nr_pull_params);
99 blob_copy_bytes(binary, prog_data->pull_param,
100 sizeof(uint32_t) * prog_data->nr_pull_params);
101
102 return (binary->current == binary->end && !binary->overrun);
103 }
104
105 static bool
106 read_and_upload(struct brw_context *brw, struct disk_cache *cache,
107 struct gl_program *prog, gl_shader_stage stage)
108 {
109 unsigned char binary_sha1[20];
110
111 union brw_any_prog_key prog_key;
112
113 switch (stage) {
114 case MESA_SHADER_VERTEX:
115 brw_vs_populate_key(brw, &prog_key.vs);
116 /* We don't care what instance of the program it is for the disk cache
117 * hash lookup, so set the id to 0 for the sha1 hashing.
118 * program_string_id will be set below.
119 */
120 prog_key.vs.program_string_id = 0;
121 break;
122 case MESA_SHADER_FRAGMENT:
123 brw_wm_populate_key(brw, &prog_key.wm);
124 prog_key.wm.program_string_id = 0;
125 break;
126 default:
127 unreachable("Unsupported stage!");
128 }
129
130 gen_shader_sha1(brw, prog, stage, &prog_key, binary_sha1);
131
132 size_t buffer_size;
133 uint8_t *buffer = disk_cache_get(cache, binary_sha1, &buffer_size);
134 if (buffer == NULL) {
135 if (brw->ctx._Shader->Flags & GLSL_CACHE_INFO) {
136 char sha1_buf[41];
137 _mesa_sha1_format(sha1_buf, binary_sha1);
138 fprintf(stderr, "No cached %s binary found for: %s\n",
139 _mesa_shader_stage_to_abbrev(stage), sha1_buf);
140 }
141 return false;
142 }
143
144 if (brw->ctx._Shader->Flags & GLSL_CACHE_INFO) {
145 char sha1_buf[41];
146 _mesa_sha1_format(sha1_buf, binary_sha1);
147 fprintf(stderr, "attempting to populate bo cache with binary: %s\n",
148 sha1_buf);
149 }
150
151 struct blob_reader binary;
152 blob_reader_init(&binary, buffer, buffer_size);
153
154 const uint8_t *program;
155 struct brw_stage_prog_data *prog_data =
156 ralloc_size(NULL, sizeof(union brw_any_prog_data));
157 if (!read_blob_program_data(&binary, prog, stage, &program, prog_data)) {
158 /* Something very bad has gone wrong discard the item from the cache and
159 * rebuild from source.
160 */
161 if (brw->ctx._Shader->Flags & GLSL_CACHE_INFO) {
162 fprintf(stderr, "Error reading program from cache (invalid i965 "
163 "cache item)\n");
164 }
165
166 disk_cache_remove(cache, binary_sha1);
167 free(buffer);
168 return false;
169 }
170
171 enum brw_cache_id cache_id;
172 struct brw_stage_state *stage_state;
173
174 switch (stage) {
175 case MESA_SHADER_VERTEX:
176 prog_key.vs.program_string_id = brw_program(prog)->id;
177 cache_id = BRW_CACHE_VS_PROG;
178 stage_state = &brw->vs.base;
179 break;
180 case MESA_SHADER_FRAGMENT:
181 prog_key.wm.program_string_id = brw_program(prog)->id;
182 cache_id = BRW_CACHE_FS_PROG;
183 stage_state = &brw->wm.base;
184 break;
185 default:
186 unreachable("Unsupported stage!");
187 }
188
189 brw_alloc_stage_scratch(brw, stage_state, prog_data->total_scratch);
190
191 brw_upload_cache(&brw->cache, cache_id, &prog_key, brw_prog_key_size(stage),
192 program, prog_data->program_size, prog_data,
193 brw_prog_data_size(stage), &stage_state->prog_offset,
194 &stage_state->prog_data);
195
196 prog->program_written_to_cache = true;
197
198 free(buffer);
199
200 return true;
201 }
202
203 bool
204 brw_disk_cache_upload_program(struct brw_context *brw, gl_shader_stage stage)
205 {
206 struct disk_cache *cache = brw->ctx.Cache;
207 if (cache == NULL)
208 return false;
209
210 struct gl_program *prog = brw->ctx._Shader->CurrentProgram[stage];
211 if (prog == NULL)
212 return false;
213
214 if (prog->sh.data->LinkStatus != linking_skipped)
215 goto fail;
216
217 if (!read_and_upload(brw, cache, prog, stage))
218 goto fail;
219
220 if (brw->ctx._Shader->Flags & GLSL_CACHE_INFO) {
221 fprintf(stderr, "read gen program from cache\n");
222 }
223
224 return true;
225
226 fail:
227 /*FIXME: Fall back and compile from source here. */
228 return false;
229 }
230
231 static void
232 write_program_data(struct brw_context *brw, struct gl_program *prog,
233 void *key, struct brw_stage_prog_data *prog_data,
234 uint32_t prog_offset, struct disk_cache *cache,
235 gl_shader_stage stage)
236 {
237 struct blob binary;
238 blob_init(&binary);
239
240 const void *program_map = brw->cache.map + prog_offset;
241 /* TODO: Improve perf for non-LLC. It would be best to save it at program
242 * generation time when the program is in normal memory accessible with
243 * cache to the CPU. Another easier change would be to use
244 * _mesa_streaming_load_memcpy to read from the program mapped memory. */
245 write_blob_program_data(&binary, stage, program_map, prog_data);
246
247 unsigned char sha1[20];
248 char buf[41];
249 gen_shader_sha1(brw, prog, stage, key, sha1);
250 _mesa_sha1_format(buf, sha1);
251 if (brw->ctx._Shader->Flags & GLSL_CACHE_INFO) {
252 fprintf(stderr, "putting binary in cache: %s\n", buf);
253 }
254
255 disk_cache_put(cache, sha1, binary.data, binary.size, NULL);
256
257 prog->program_written_to_cache = true;
258 blob_finish(&binary);
259 }
260
261 void
262 brw_disk_cache_write_program(struct brw_context *brw)
263 {
264 struct disk_cache *cache = brw->ctx.Cache;
265 if (cache == NULL)
266 return;
267
268 struct gl_program *prog =
269 brw->ctx._Shader->CurrentProgram[MESA_SHADER_VERTEX];
270 if (prog && !prog->program_written_to_cache) {
271 struct brw_vs_prog_key vs_key;
272 brw_vs_populate_key(brw, &vs_key);
273 vs_key.program_string_id = 0;
274
275 write_program_data(brw, prog, &vs_key, brw->vs.base.prog_data,
276 brw->vs.base.prog_offset, cache,
277 MESA_SHADER_VERTEX);
278 }
279
280 prog = brw->ctx._Shader->CurrentProgram[MESA_SHADER_FRAGMENT];
281 if (prog && !prog->program_written_to_cache) {
282 struct brw_wm_prog_key wm_key;
283 brw_wm_populate_key(brw, &wm_key);
284 wm_key.program_string_id = 0;
285
286 write_program_data(brw, prog, &wm_key, brw->wm.base.prog_data,
287 brw->wm.base.prog_offset, cache,
288 MESA_SHADER_FRAGMENT);
289 }
290 }