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_program.h"
35 #include "brw_state.h"
40 gen_shader_sha1(struct brw_context
*brw
, struct gl_program
*prog
,
41 gl_shader_stage stage
, void *key
, unsigned char *out_sha1
)
44 unsigned char sha1
[20];
48 _mesa_sha1_format(sha1_buf
, prog
->sh
.data
->sha1
);
49 offset
+= snprintf(manifest
, sizeof(manifest
), "program: %s\n", sha1_buf
);
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
),
57 _mesa_sha1_compute(manifest
, strlen(manifest
), out_sha1
);
61 write_blob_program_data(struct blob
*binary
, gl_shader_stage stage
,
63 struct brw_stage_prog_data
*prog_data
)
65 /* Write prog_data to blob. */
66 blob_write_bytes(binary
, prog_data
, brw_prog_data_size(stage
));
68 /* Write program to blob. */
69 blob_write_bytes(binary
, program
, prog_data
->program_size
);
71 /* Write push params */
72 blob_write_bytes(binary
, prog_data
->param
,
73 sizeof(uint32_t) * prog_data
->nr_params
);
75 /* Write pull params */
76 blob_write_bytes(binary
, prog_data
->pull_param
,
77 sizeof(uint32_t) * prog_data
->nr_pull_params
);
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
)
85 /* Read shader prog_data from blob. */
86 blob_copy_bytes(binary
, prog_data
, brw_prog_data_size(stage
));
90 /* Read shader program from blob. */
91 *program
= blob_read_bytes(binary
, prog_data
->program_size
);
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
);
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
);
104 return (binary
->current
== binary
->end
&& !binary
->overrun
);
108 read_and_upload(struct brw_context
*brw
, struct disk_cache
*cache
,
109 struct gl_program
*prog
, gl_shader_stage stage
)
111 unsigned char binary_sha1
[20];
113 union brw_any_prog_key prog_key
;
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.
122 prog_key
.vs
.program_string_id
= 0;
124 case MESA_SHADER_TESS_CTRL
:
125 brw_tcs_populate_key(brw
, &prog_key
.tcs
);
126 prog_key
.tcs
.program_string_id
= 0;
128 case MESA_SHADER_TESS_EVAL
:
129 brw_tes_populate_key(brw
, &prog_key
.tes
);
130 prog_key
.tes
.program_string_id
= 0;
132 case MESA_SHADER_GEOMETRY
:
133 brw_gs_populate_key(brw
, &prog_key
.gs
);
134 prog_key
.gs
.program_string_id
= 0;
136 case MESA_SHADER_FRAGMENT
:
137 brw_wm_populate_key(brw
, &prog_key
.wm
);
138 prog_key
.wm
.program_string_id
= 0;
141 unreachable("Unsupported stage!");
144 gen_shader_sha1(brw
, prog
, stage
, &prog_key
, binary_sha1
);
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
) {
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
);
158 if (brw
->ctx
._Shader
->Flags
& GLSL_CACHE_INFO
) {
160 _mesa_sha1_format(sha1_buf
, binary_sha1
);
161 fprintf(stderr
, "attempting to populate bo cache with binary: %s\n",
165 struct blob_reader binary
;
166 blob_reader_init(&binary
, buffer
, buffer_size
);
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.
175 if (brw
->ctx
._Shader
->Flags
& GLSL_CACHE_INFO
) {
176 fprintf(stderr
, "Error reading program from cache (invalid i965 "
180 disk_cache_remove(cache
, binary_sha1
);
185 enum brw_cache_id cache_id
;
186 struct brw_stage_state
*stage_state
;
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
;
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
;
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
;
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
;
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
;
215 unreachable("Unsupported stage!");
218 brw_alloc_stage_scratch(brw
, stage_state
, prog_data
->total_scratch
);
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
);
225 prog
->program_written_to_cache
= true;
233 brw_disk_cache_upload_program(struct brw_context
*brw
, gl_shader_stage stage
)
235 struct disk_cache
*cache
= brw
->ctx
.Cache
;
239 struct gl_program
*prog
= brw
->ctx
._Shader
->CurrentProgram
[stage
];
243 if (prog
->sh
.data
->LinkStatus
!= linking_skipped
)
246 if (!read_and_upload(brw
, cache
, prog
, stage
))
249 if (brw
->ctx
._Shader
->Flags
& GLSL_CACHE_INFO
) {
250 fprintf(stderr
, "read gen program from cache\n");
256 /*FIXME: Fall back and compile from source here. */
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
)
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
);
276 unsigned char sha1
[20];
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
);
284 disk_cache_put(cache
, sha1
, binary
.data
, binary
.size
, NULL
);
286 prog
->program_written_to_cache
= true;
287 blob_finish(&binary
);
291 brw_disk_cache_write_program(struct brw_context
*brw
)
293 struct disk_cache
*cache
= brw
->ctx
.Cache
;
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;
304 write_program_data(brw
, prog
, &vs_key
, brw
->vs
.base
.prog_data
,
305 brw
->vs
.base
.prog_offset
, cache
,
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;
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
);
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;
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
);
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;
337 write_program_data(brw
, prog
, &gs_key
, brw
->gs
.base
.prog_data
,
338 brw
->gs
.base
.prog_offset
, cache
,
339 MESA_SHADER_GEOMETRY
);
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;
348 write_program_data(brw
, prog
, &wm_key
, brw
->wm
.base
.prog_data
,
349 brw
->wm
.base
.prog_offset
, cache
,
350 MESA_SHADER_FRAGMENT
);