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/disk_cache.h"
29 #include "util/macros.h"
30 #include "util/mesa-sha1.h"
32 #include "brw_context.h"
33 #include "brw_state.h"
38 gen_shader_sha1(struct brw_context
*brw
, struct gl_program
*prog
,
39 gl_shader_stage stage
, void *key
, unsigned char *out_sha1
)
42 unsigned char sha1
[20];
46 _mesa_sha1_format(sha1_buf
, prog
->sh
.data
->sha1
);
47 offset
+= snprintf(manifest
, sizeof(manifest
), "program: %s\n", sha1_buf
);
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
),
55 _mesa_sha1_compute(manifest
, strlen(manifest
), out_sha1
);
59 write_blob_program_data(struct blob
*binary
, gl_shader_stage stage
,
61 struct brw_stage_prog_data
*prog_data
)
63 /* Write prog_data to blob. */
64 blob_write_bytes(binary
, prog_data
, brw_prog_data_size(stage
));
66 /* Write program to blob. */
67 blob_write_bytes(binary
, program
, prog_data
->program_size
);
69 /* Write push params */
70 blob_write_bytes(binary
, prog_data
->param
,
71 sizeof(uint32_t) * prog_data
->nr_params
);
73 /* Write pull params */
74 blob_write_bytes(binary
, prog_data
->pull_param
,
75 sizeof(uint32_t) * prog_data
->nr_pull_params
);
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
)
83 /* Read shader prog_data from blob. */
84 blob_copy_bytes(binary
, prog_data
, brw_prog_data_size(stage
));
88 /* Read shader program from blob. */
89 *program
= blob_read_bytes(binary
, prog_data
->program_size
);
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
);
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
);
102 return (binary
->current
== binary
->end
&& !binary
->overrun
);
106 read_and_upload(struct brw_context
*brw
, struct disk_cache
*cache
,
107 struct gl_program
*prog
, gl_shader_stage stage
)
109 unsigned char binary_sha1
[20];
111 union brw_any_prog_key prog_key
;
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.
120 prog_key
.vs
.program_string_id
= 0;
122 case MESA_SHADER_FRAGMENT
:
123 brw_wm_populate_key(brw
, &prog_key
.wm
);
124 prog_key
.wm
.program_string_id
= 0;
127 unreachable("Unsupported stage!");
130 gen_shader_sha1(brw
, prog
, stage
, &prog_key
, binary_sha1
);
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
) {
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
);
144 if (brw
->ctx
._Shader
->Flags
& GLSL_CACHE_INFO
) {
146 _mesa_sha1_format(sha1_buf
, binary_sha1
);
147 fprintf(stderr
, "attempting to populate bo cache with binary: %s\n",
151 struct blob_reader binary
;
152 blob_reader_init(&binary
, buffer
, buffer_size
);
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.
161 if (brw
->ctx
._Shader
->Flags
& GLSL_CACHE_INFO
) {
162 fprintf(stderr
, "Error reading program from cache (invalid i965 "
166 disk_cache_remove(cache
, binary_sha1
);
171 enum brw_cache_id cache_id
;
172 struct brw_stage_state
*stage_state
;
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
;
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
;
186 unreachable("Unsupported stage!");
189 brw_alloc_stage_scratch(brw
, stage_state
, prog_data
->total_scratch
);
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
);
196 prog
->program_written_to_cache
= true;
204 brw_disk_cache_upload_program(struct brw_context
*brw
, gl_shader_stage stage
)
206 struct disk_cache
*cache
= brw
->ctx
.Cache
;
210 struct gl_program
*prog
= brw
->ctx
._Shader
->CurrentProgram
[stage
];
214 if (prog
->sh
.data
->LinkStatus
!= linking_skipped
)
217 if (!read_and_upload(brw
, cache
, prog
, stage
))
220 if (brw
->ctx
._Shader
->Flags
& GLSL_CACHE_INFO
) {
221 fprintf(stderr
, "read gen program from cache\n");
227 /*FIXME: Fall back and compile from source here. */
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
)
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
);
247 unsigned char sha1
[20];
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
);
255 disk_cache_put(cache
, sha1
, binary
.data
, binary
.size
, NULL
);
257 prog
->program_written_to_cache
= true;
258 blob_finish(&binary
);
262 brw_disk_cache_write_program(struct brw_context
*brw
)
264 struct disk_cache
*cache
= brw
->ctx
.Cache
;
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;
275 write_program_data(brw
, prog
, &vs_key
, brw
->vs
.base
.prog_data
,
276 brw
->vs
.base
.prog_offset
, cache
,
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;
286 write_program_data(brw
, prog
, &wm_key
, brw
->wm
.base
.prog_data
,
287 brw
->wm
.base
.prog_offset
, cache
,
288 MESA_SHADER_FRAGMENT
);