2 * Copyright © 2014 Intel Corporation
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:
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
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
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"
34 #include "brw_context.h"
35 #include "brw_program.h"
38 #include "brw_state.h"
43 gen_shader_sha1(struct brw_context
*brw
, struct gl_program
*prog
,
44 gl_shader_stage stage
, void *key
, unsigned char *out_sha1
)
47 unsigned char sha1
[20];
51 _mesa_sha1_format(sha1_buf
, prog
->sh
.data
->sha1
);
52 offset
+= snprintf(manifest
, sizeof(manifest
), "program: %s\n", sha1_buf
);
54 _mesa_sha1_compute(key
, brw_prog_key_size(stage
), sha1
);
55 _mesa_sha1_format(sha1_buf
, sha1
);
56 offset
+= snprintf(manifest
+ offset
, sizeof(manifest
) - offset
,
57 "%s_key: %s\n", _mesa_shader_stage_to_abbrev(stage
),
60 _mesa_sha1_compute(manifest
, strlen(manifest
), out_sha1
);
64 write_blob_program_data(struct blob
*binary
, gl_shader_stage stage
,
66 struct brw_stage_prog_data
*prog_data
)
68 /* Write prog_data to blob. */
69 blob_write_bytes(binary
, prog_data
, brw_prog_data_size(stage
));
71 /* Write program to blob. */
72 blob_write_bytes(binary
, program
, prog_data
->program_size
);
74 /* Write push params */
75 blob_write_bytes(binary
, prog_data
->param
,
76 sizeof(uint32_t) * prog_data
->nr_params
);
78 /* Write pull params */
79 blob_write_bytes(binary
, prog_data
->pull_param
,
80 sizeof(uint32_t) * prog_data
->nr_pull_params
);
84 read_blob_program_data(struct blob_reader
*binary
, struct gl_program
*prog
,
85 gl_shader_stage stage
, const uint8_t **program
,
86 struct brw_stage_prog_data
*prog_data
)
88 /* Read shader prog_data from blob. */
89 blob_copy_bytes(binary
, prog_data
, brw_prog_data_size(stage
));
93 /* Read shader program from blob. */
94 *program
= blob_read_bytes(binary
, prog_data
->program_size
);
96 /* Read push params */
97 prog_data
->param
= rzalloc_array(NULL
, uint32_t, prog_data
->nr_params
);
98 blob_copy_bytes(binary
, prog_data
->param
,
99 sizeof(uint32_t) * prog_data
->nr_params
);
101 /* Read pull params */
102 prog_data
->pull_param
= rzalloc_array(NULL
, uint32_t,
103 prog_data
->nr_pull_params
);
104 blob_copy_bytes(binary
, prog_data
->pull_param
,
105 sizeof(uint32_t) * prog_data
->nr_pull_params
);
107 return (binary
->current
== binary
->end
&& !binary
->overrun
);
111 read_and_upload(struct brw_context
*brw
, struct disk_cache
*cache
,
112 struct gl_program
*prog
, gl_shader_stage stage
)
114 unsigned char binary_sha1
[20];
116 union brw_any_prog_key prog_key
;
119 case MESA_SHADER_VERTEX
:
120 brw_vs_populate_key(brw
, &prog_key
.vs
);
121 /* We don't care what instance of the program it is for the disk cache
122 * hash lookup, so set the id to 0 for the sha1 hashing.
123 * program_string_id will be set below.
125 prog_key
.vs
.program_string_id
= 0;
127 case MESA_SHADER_TESS_CTRL
:
128 brw_tcs_populate_key(brw
, &prog_key
.tcs
);
129 prog_key
.tcs
.program_string_id
= 0;
131 case MESA_SHADER_TESS_EVAL
:
132 brw_tes_populate_key(brw
, &prog_key
.tes
);
133 prog_key
.tes
.program_string_id
= 0;
135 case MESA_SHADER_GEOMETRY
:
136 brw_gs_populate_key(brw
, &prog_key
.gs
);
137 prog_key
.gs
.program_string_id
= 0;
139 case MESA_SHADER_FRAGMENT
:
140 brw_wm_populate_key(brw
, &prog_key
.wm
);
141 prog_key
.wm
.program_string_id
= 0;
143 case MESA_SHADER_COMPUTE
:
144 brw_cs_populate_key(brw
, &prog_key
.cs
);
145 prog_key
.cs
.program_string_id
= 0;
148 unreachable("Unsupported stage!");
151 gen_shader_sha1(brw
, prog
, stage
, &prog_key
, binary_sha1
);
154 uint8_t *buffer
= disk_cache_get(cache
, binary_sha1
, &buffer_size
);
155 if (buffer
== NULL
) {
156 if (brw
->ctx
._Shader
->Flags
& GLSL_CACHE_INFO
) {
158 _mesa_sha1_format(sha1_buf
, binary_sha1
);
159 fprintf(stderr
, "No cached %s binary found for: %s\n",
160 _mesa_shader_stage_to_abbrev(stage
), sha1_buf
);
165 if (brw
->ctx
._Shader
->Flags
& GLSL_CACHE_INFO
) {
167 _mesa_sha1_format(sha1_buf
, binary_sha1
);
168 fprintf(stderr
, "attempting to populate bo cache with binary: %s\n",
172 struct blob_reader binary
;
173 blob_reader_init(&binary
, buffer
, buffer_size
);
175 const uint8_t *program
;
176 struct brw_stage_prog_data
*prog_data
=
177 ralloc_size(NULL
, sizeof(union brw_any_prog_data
));
178 if (!read_blob_program_data(&binary
, prog
, stage
, &program
, prog_data
)) {
179 /* Something very bad has gone wrong discard the item from the cache and
180 * rebuild from source.
182 if (brw
->ctx
._Shader
->Flags
& GLSL_CACHE_INFO
) {
183 fprintf(stderr
, "Error reading program from cache (invalid i965 "
187 disk_cache_remove(cache
, binary_sha1
);
192 enum brw_cache_id cache_id
;
193 struct brw_stage_state
*stage_state
;
196 case MESA_SHADER_VERTEX
:
197 prog_key
.vs
.program_string_id
= brw_program(prog
)->id
;
198 cache_id
= BRW_CACHE_VS_PROG
;
199 stage_state
= &brw
->vs
.base
;
201 case MESA_SHADER_TESS_CTRL
:
202 prog_key
.tcs
.program_string_id
= brw_program(prog
)->id
;
203 cache_id
= BRW_CACHE_TCS_PROG
;
204 stage_state
= &brw
->tcs
.base
;
206 case MESA_SHADER_TESS_EVAL
:
207 prog_key
.tes
.program_string_id
= brw_program(prog
)->id
;
208 cache_id
= BRW_CACHE_TES_PROG
;
209 stage_state
= &brw
->tes
.base
;
211 case MESA_SHADER_GEOMETRY
:
212 prog_key
.gs
.program_string_id
= brw_program(prog
)->id
;
213 cache_id
= BRW_CACHE_GS_PROG
;
214 stage_state
= &brw
->gs
.base
;
216 case MESA_SHADER_FRAGMENT
:
217 prog_key
.wm
.program_string_id
= brw_program(prog
)->id
;
218 cache_id
= BRW_CACHE_FS_PROG
;
219 stage_state
= &brw
->wm
.base
;
221 case MESA_SHADER_COMPUTE
:
222 prog_key
.cs
.program_string_id
= brw_program(prog
)->id
;
223 cache_id
= BRW_CACHE_CS_PROG
;
224 stage_state
= &brw
->cs
.base
;
227 unreachable("Unsupported stage!");
230 brw_alloc_stage_scratch(brw
, stage_state
, prog_data
->total_scratch
);
232 brw_upload_cache(&brw
->cache
, cache_id
, &prog_key
, brw_prog_key_size(stage
),
233 program
, prog_data
->program_size
, prog_data
,
234 brw_prog_data_size(stage
), &stage_state
->prog_offset
,
235 &stage_state
->prog_data
);
237 prog
->program_written_to_cache
= true;
245 brw_disk_cache_upload_program(struct brw_context
*brw
, gl_shader_stage stage
)
247 struct disk_cache
*cache
= brw
->ctx
.Cache
;
251 struct gl_program
*prog
= brw
->ctx
._Shader
->CurrentProgram
[stage
];
255 /* FIXME: For now we don't read from the cache if transform feedback is
256 * enabled via the API. However the shader cache does support transform
257 * feedback when enabled via in shader xfb qualifiers.
259 if (prog
->sh
.LinkedTransformFeedback
&&
260 prog
->sh
.LinkedTransformFeedback
->api_enabled
)
263 if (brw
->ctx
._Shader
->Flags
& GLSL_CACHE_FALLBACK
)
266 if (prog
->sh
.data
->LinkStatus
!= LINKING_SKIPPED
)
269 if (!read_and_upload(brw
, cache
, prog
, stage
))
272 if (brw
->ctx
._Shader
->Flags
& GLSL_CACHE_INFO
) {
273 fprintf(stderr
, "read gen program from cache\n");
279 prog
->program_written_to_cache
= false;
280 if (brw
->ctx
._Shader
->Flags
& GLSL_CACHE_INFO
) {
281 fprintf(stderr
, "falling back to nir %s.\n",
282 _mesa_shader_stage_to_abbrev(prog
->info
.stage
));
285 brw_program_deserialize_nir(&brw
->ctx
, prog
, stage
);
291 write_program_data(struct brw_context
*brw
, struct gl_program
*prog
,
292 void *key
, struct brw_stage_prog_data
*prog_data
,
293 uint32_t prog_offset
, struct disk_cache
*cache
,
294 gl_shader_stage stage
)
299 const void *program_map
= brw
->cache
.map
+ prog_offset
;
300 /* TODO: Improve perf for non-LLC. It would be best to save it at program
301 * generation time when the program is in normal memory accessible with
302 * cache to the CPU. Another easier change would be to use
303 * _mesa_streaming_load_memcpy to read from the program mapped memory. */
304 write_blob_program_data(&binary
, stage
, program_map
, prog_data
);
306 unsigned char sha1
[20];
308 gen_shader_sha1(brw
, prog
, stage
, key
, sha1
);
309 _mesa_sha1_format(buf
, sha1
);
310 if (brw
->ctx
._Shader
->Flags
& GLSL_CACHE_INFO
) {
311 fprintf(stderr
, "putting binary in cache: %s\n", buf
);
314 disk_cache_put(cache
, sha1
, binary
.data
, binary
.size
, NULL
);
316 prog
->program_written_to_cache
= true;
317 blob_finish(&binary
);
321 brw_disk_cache_write_render_programs(struct brw_context
*brw
)
323 struct disk_cache
*cache
= brw
->ctx
.Cache
;
327 struct gl_program
*prog
=
328 brw
->ctx
._Shader
->CurrentProgram
[MESA_SHADER_VERTEX
];
329 if (prog
&& !prog
->program_written_to_cache
) {
330 struct brw_vs_prog_key vs_key
;
331 brw_vs_populate_key(brw
, &vs_key
);
332 vs_key
.program_string_id
= 0;
334 write_program_data(brw
, prog
, &vs_key
, brw
->vs
.base
.prog_data
,
335 brw
->vs
.base
.prog_offset
, cache
,
339 prog
= brw
->ctx
._Shader
->CurrentProgram
[MESA_SHADER_TESS_CTRL
];
340 if (prog
&& !prog
->program_written_to_cache
) {
341 struct brw_tcs_prog_key tcs_key
;
342 brw_tcs_populate_key(brw
, &tcs_key
);
343 tcs_key
.program_string_id
= 0;
345 write_program_data(brw
, prog
, &tcs_key
, brw
->tcs
.base
.prog_data
,
346 brw
->tcs
.base
.prog_offset
, cache
,
347 MESA_SHADER_TESS_CTRL
);
350 prog
= brw
->ctx
._Shader
->CurrentProgram
[MESA_SHADER_TESS_EVAL
];
351 if (prog
&& !prog
->program_written_to_cache
) {
352 struct brw_tes_prog_key tes_key
;
353 brw_tes_populate_key(brw
, &tes_key
);
354 tes_key
.program_string_id
= 0;
356 write_program_data(brw
, prog
, &tes_key
, brw
->tes
.base
.prog_data
,
357 brw
->tes
.base
.prog_offset
, cache
,
358 MESA_SHADER_TESS_EVAL
);
361 prog
= brw
->ctx
._Shader
->CurrentProgram
[MESA_SHADER_GEOMETRY
];
362 if (prog
&& !prog
->program_written_to_cache
) {
363 struct brw_gs_prog_key gs_key
;
364 brw_gs_populate_key(brw
, &gs_key
);
365 gs_key
.program_string_id
= 0;
367 write_program_data(brw
, prog
, &gs_key
, brw
->gs
.base
.prog_data
,
368 brw
->gs
.base
.prog_offset
, cache
,
369 MESA_SHADER_GEOMETRY
);
372 prog
= brw
->ctx
._Shader
->CurrentProgram
[MESA_SHADER_FRAGMENT
];
373 if (prog
&& !prog
->program_written_to_cache
) {
374 struct brw_wm_prog_key wm_key
;
375 brw_wm_populate_key(brw
, &wm_key
);
376 wm_key
.program_string_id
= 0;
378 write_program_data(brw
, prog
, &wm_key
, brw
->wm
.base
.prog_data
,
379 brw
->wm
.base
.prog_offset
, cache
,
380 MESA_SHADER_FRAGMENT
);
385 brw_disk_cache_write_compute_program(struct brw_context
*brw
)
387 struct disk_cache
*cache
= brw
->ctx
.Cache
;
391 struct gl_program
*prog
=
392 brw
->ctx
._Shader
->CurrentProgram
[MESA_SHADER_COMPUTE
];
393 if (prog
&& !prog
->program_written_to_cache
) {
394 struct brw_cs_prog_key cs_key
;
395 brw_cs_populate_key(brw
, &cs_key
);
396 cs_key
.program_string_id
= 0;
398 write_program_data(brw
, prog
, &cs_key
, brw
->cs
.base
.prog_data
,
399 brw
->cs
.base
.prog_offset
, cache
,
400 MESA_SHADER_COMPUTE
);
405 brw_disk_cache_init(struct brw_context
*brw
)
407 #ifdef ENABLE_SHADER_CACHE
408 if (env_var_as_boolean("MESA_GLSL_CACHE_DISABLE", true))
412 MAYBE_UNUSED
int len
= snprintf(renderer
, sizeof(renderer
), "i965_%04x",
413 brw
->screen
->deviceID
);
414 assert(len
== sizeof(renderer
) - 1);
416 const struct build_id_note
*note
=
417 build_id_find_nhdr_for_addr(brw_disk_cache_init
);
418 assert(note
&& build_id_length(note
) == 20 /* sha1 */);
420 const uint8_t *id_sha1
= build_id_data(note
);
424 _mesa_sha1_format(timestamp
, id_sha1
);
426 brw
->ctx
.Cache
= disk_cache_create(renderer
, timestamp
, 0);