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