2 * Copyright © 2017 Timothy Arceri
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
21 * DEALINGS IN THE SOFTWARE.
26 #include "st_program.h"
27 #include "st_shader_cache.h"
28 #include "compiler/glsl/program.h"
29 #include "pipe/p_shader_tokens.h"
30 #include "program/ir_to_mesa.h"
31 #include "util/u_memory.h"
34 write_stream_out_to_cache(struct blob
*blob
,
35 struct pipe_shader_state
*tgsi
)
37 blob_write_bytes(blob
, &tgsi
->stream_output
,
38 sizeof(tgsi
->stream_output
));
42 write_tgsi_to_cache(struct blob
*blob
, struct pipe_shader_state
*tgsi
,
43 struct st_context
*st
, unsigned char *sha1
,
46 blob_write_uint32(blob
, num_tokens
);
47 blob_write_bytes(blob
, tgsi
->tokens
,
48 num_tokens
* sizeof(struct tgsi_token
));
50 disk_cache_put(st
->ctx
->Cache
, sha1
, blob
->data
, blob
->size
, NULL
);
54 * Store tgsi and any other required state in on-disk shader cache.
57 st_store_tgsi_in_disk_cache(struct st_context
*st
, struct gl_program
*prog
,
58 struct pipe_shader_state
*out_state
,
64 /* Exit early when we are dealing with a ff shader with no source file to
65 * generate a source from.
67 static const char zero
[sizeof(prog
->sh
.data
->sha1
)] = {0};
68 if (memcmp(prog
->sh
.data
->sha1
, zero
, sizeof(prog
->sh
.data
->sha1
)) == 0)
72 struct blob
*blob
= blob_create();
74 switch (prog
->info
.stage
) {
75 case MESA_SHADER_VERTEX
: {
76 struct st_vertex_program
*stvp
= (struct st_vertex_program
*) prog
;
79 blob_write_uint32(blob
, stvp
->num_inputs
);
80 blob_write_bytes(blob
, stvp
->index_to_input
,
81 sizeof(stvp
->index_to_input
));
82 blob_write_bytes(blob
, stvp
->result_to_output
,
83 sizeof(stvp
->result_to_output
));
85 write_stream_out_to_cache(blob
, &stvp
->tgsi
);
86 write_tgsi_to_cache(blob
, &stvp
->tgsi
, st
, sha1
, num_tokens
);
89 case MESA_SHADER_TESS_CTRL
:
90 case MESA_SHADER_TESS_EVAL
:
91 case MESA_SHADER_GEOMETRY
: {
92 struct st_common_program
*p
= st_common_program(prog
);
95 write_stream_out_to_cache(blob
, out_state
);
96 write_tgsi_to_cache(blob
, out_state
, st
, sha1
, num_tokens
);
99 case MESA_SHADER_FRAGMENT
: {
100 struct st_fragment_program
*stfp
= (struct st_fragment_program
*) prog
;
103 write_tgsi_to_cache(blob
, &stfp
->tgsi
, st
, sha1
, num_tokens
);
106 case MESA_SHADER_COMPUTE
: {
107 struct st_compute_program
*stcp
= (struct st_compute_program
*) prog
;
110 write_tgsi_to_cache(blob
, out_state
, st
, sha1
, num_tokens
);
114 unreachable("Unsupported stage");
117 if (st
->ctx
->_Shader
->Flags
& GLSL_CACHE_INFO
) {
119 _mesa_sha1_format(sha1_buf
, sha1
);
120 fprintf(stderr
, "putting %s tgsi_tokens in cache: %s\n",
121 _mesa_shader_stage_to_string(prog
->info
.stage
), sha1_buf
);
128 read_stream_out_from_cache(struct blob_reader
*blob_reader
,
129 struct pipe_shader_state
*tgsi
)
131 blob_copy_bytes(blob_reader
, (uint8_t *) &tgsi
->stream_output
,
132 sizeof(tgsi
->stream_output
));
136 read_tgsi_from_cache(struct blob_reader
*blob_reader
,
137 const struct tgsi_token
**tokens
)
139 uint32_t num_tokens
= blob_read_uint32(blob_reader
);
140 unsigned tokens_size
= num_tokens
* sizeof(struct tgsi_token
);
141 *tokens
= (const struct tgsi_token
*) MALLOC(tokens_size
);
142 blob_copy_bytes(blob_reader
, (uint8_t *) *tokens
, tokens_size
);
146 st_load_tgsi_from_disk_cache(struct gl_context
*ctx
,
147 struct gl_shader_program
*prog
)
152 unsigned char *stage_sha1
[MESA_SHADER_STAGES
];
155 /* Compute and store sha1 for each stage. These will be reused by the
156 * cache store pass if we fail to find the cached tgsi.
158 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
159 if (prog
->_LinkedShaders
[i
] == NULL
)
162 char *buf
= ralloc_strdup(NULL
, "tgsi_tokens ");
163 _mesa_sha1_format(sha1_buf
,
164 prog
->_LinkedShaders
[i
]->Program
->sh
.data
->sha1
);
165 ralloc_strcat(&buf
, sha1_buf
);
167 struct gl_program
*glprog
= prog
->_LinkedShaders
[i
]->Program
;
168 switch (glprog
->info
.stage
) {
169 case MESA_SHADER_VERTEX
: {
170 struct st_vertex_program
*stvp
= (struct st_vertex_program
*) glprog
;
171 stage_sha1
[i
] = stvp
->sha1
;
172 ralloc_strcat(&buf
, " vs");
173 disk_cache_compute_key(ctx
->Cache
, buf
, strlen(buf
), stage_sha1
[i
]);
176 case MESA_SHADER_TESS_CTRL
: {
177 struct st_common_program
*stcp
= st_common_program(glprog
);
178 stage_sha1
[i
] = stcp
->sha1
;
179 ralloc_strcat(&buf
, " tcs");
180 disk_cache_compute_key(ctx
->Cache
, buf
, strlen(buf
), stage_sha1
[i
]);
183 case MESA_SHADER_TESS_EVAL
: {
184 struct st_common_program
*step
= st_common_program(glprog
);
185 stage_sha1
[i
] = step
->sha1
;
186 ralloc_strcat(&buf
, " tes");
187 disk_cache_compute_key(ctx
->Cache
, buf
, strlen(buf
), stage_sha1
[i
]);
190 case MESA_SHADER_GEOMETRY
: {
191 struct st_common_program
*stgp
= st_common_program(glprog
);
192 stage_sha1
[i
] = stgp
->sha1
;
193 ralloc_strcat(&buf
, " gs");
194 disk_cache_compute_key(ctx
->Cache
, buf
, strlen(buf
), stage_sha1
[i
]);
197 case MESA_SHADER_FRAGMENT
: {
198 struct st_fragment_program
*stfp
=
199 (struct st_fragment_program
*) glprog
;
200 stage_sha1
[i
] = stfp
->sha1
;
201 ralloc_strcat(&buf
, " fs");
202 disk_cache_compute_key(ctx
->Cache
, buf
, strlen(buf
), stage_sha1
[i
]);
205 case MESA_SHADER_COMPUTE
: {
206 struct st_compute_program
*stcp
=
207 (struct st_compute_program
*) glprog
;
208 stage_sha1
[i
] = stcp
->sha1
;
209 ralloc_strcat(&buf
, " cs");
210 disk_cache_compute_key(ctx
->Cache
, buf
, strlen(buf
), stage_sha1
[i
]);
214 unreachable("Unsupported stage");
220 /* Now that we have created the sha1 keys that will be used for writting to
221 * the tgsi cache fallback to the regular glsl to tgsi path if we didn't
222 * load the GLSL IR from cache. We do this as glsl to tgsi can alter things
223 * such as gl_program_parameter_list which holds things like uniforms.
225 if (prog
->data
->LinkStatus
!= linking_skipped
)
228 uint8_t *buffer
= NULL
;
229 if (ctx
->_Shader
->Flags
& GLSL_CACHE_FALLBACK
) {
230 goto fallback_recompile
;
233 struct st_context
*st
= st_context(ctx
);
234 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
235 if (prog
->_LinkedShaders
[i
] == NULL
)
238 unsigned char *sha1
= stage_sha1
[i
];
240 buffer
= (uint8_t *) disk_cache_get(ctx
->Cache
, sha1
, &size
);
242 struct blob_reader blob_reader
;
243 blob_reader_init(&blob_reader
, buffer
, size
);
245 struct gl_program
*glprog
= prog
->_LinkedShaders
[i
]->Program
;
246 switch (glprog
->info
.stage
) {
247 case MESA_SHADER_VERTEX
: {
248 struct st_vertex_program
*stvp
=
249 (struct st_vertex_program
*) glprog
;
251 st_release_vp_variants(st
, stvp
);
253 stvp
->num_inputs
= blob_read_uint32(&blob_reader
);
254 blob_copy_bytes(&blob_reader
, (uint8_t *) stvp
->index_to_input
,
255 sizeof(stvp
->index_to_input
));
256 blob_copy_bytes(&blob_reader
, (uint8_t *) stvp
->result_to_output
,
257 sizeof(stvp
->result_to_output
));
259 read_stream_out_from_cache(&blob_reader
, &stvp
->tgsi
);
260 read_tgsi_from_cache(&blob_reader
, &stvp
->tgsi
.tokens
);
263 st
->dirty
|= ST_NEW_VERTEX_PROGRAM(st
, stvp
);
267 case MESA_SHADER_TESS_CTRL
: {
268 struct st_common_program
*sttcp
= st_common_program(glprog
);
270 st_release_basic_variants(st
, sttcp
->Base
.Target
,
271 &sttcp
->variants
, &sttcp
->tgsi
);
273 read_stream_out_from_cache(&blob_reader
, &sttcp
->tgsi
);
274 read_tgsi_from_cache(&blob_reader
, &sttcp
->tgsi
.tokens
);
276 if (st
->tcp
== sttcp
)
277 st
->dirty
|= sttcp
->affected_states
;
281 case MESA_SHADER_TESS_EVAL
: {
282 struct st_common_program
*sttep
= st_common_program(glprog
);
284 st_release_basic_variants(st
, sttep
->Base
.Target
,
285 &sttep
->variants
, &sttep
->tgsi
);
287 read_stream_out_from_cache(&blob_reader
, &sttep
->tgsi
);
288 read_tgsi_from_cache(&blob_reader
, &sttep
->tgsi
.tokens
);
290 if (st
->tep
== sttep
)
291 st
->dirty
|= sttep
->affected_states
;
295 case MESA_SHADER_GEOMETRY
: {
296 struct st_common_program
*stgp
= st_common_program(glprog
);
298 st_release_basic_variants(st
, stgp
->Base
.Target
, &stgp
->variants
,
301 read_stream_out_from_cache(&blob_reader
, &stgp
->tgsi
);
302 read_tgsi_from_cache(&blob_reader
, &stgp
->tgsi
.tokens
);
305 st
->dirty
|= stgp
->affected_states
;
309 case MESA_SHADER_FRAGMENT
: {
310 struct st_fragment_program
*stfp
=
311 (struct st_fragment_program
*) glprog
;
313 st_release_fp_variants(st
, stfp
);
315 read_tgsi_from_cache(&blob_reader
, &stfp
->tgsi
.tokens
);
318 st
->dirty
|= stfp
->affected_states
;
322 case MESA_SHADER_COMPUTE
: {
323 struct st_compute_program
*stcp
=
324 (struct st_compute_program
*) glprog
;
326 st_release_cp_variants(st
, stcp
);
328 read_tgsi_from_cache(&blob_reader
,
329 (const struct tgsi_token
**) &stcp
->tgsi
.prog
);
331 stcp
->tgsi
.req_local_mem
= stcp
->Base
.info
.cs
.shared_size
;
332 stcp
->tgsi
.req_private_mem
= 0;
333 stcp
->tgsi
.req_input_mem
= 0;
336 st
->dirty
|= stcp
->affected_states
;
341 unreachable("Unsupported stage");
344 if (blob_reader
.current
!= blob_reader
.end
|| blob_reader
.overrun
) {
345 /* Something very bad has gone wrong discard the item from the
346 * cache and rebuild/link from source.
348 assert(!"Invalid TGSI shader disk cache item!");
350 if (ctx
->_Shader
->Flags
& GLSL_CACHE_INFO
) {
351 fprintf(stderr
, "Error reading program from cache (invalid "
352 "TGSI cache item)\n");
355 disk_cache_remove(ctx
->Cache
, sha1
);
357 goto fallback_recompile
;
360 if (ctx
->_Shader
->Flags
& GLSL_CACHE_INFO
) {
361 _mesa_sha1_format(sha1_buf
, sha1
);
362 fprintf(stderr
, "%s tgsi_tokens retrieved from cache: %s\n",
363 _mesa_shader_stage_to_string(i
), sha1_buf
);
366 st_set_prog_affected_state_flags(glprog
);
367 _mesa_associate_uniform_storage(ctx
, prog
, glprog
, false);
369 /* Create Gallium shaders now instead of on demand. */
370 if (ST_DEBUG
& DEBUG_PRECOMPILE
||
371 st
->shader_has_one_variant
[glprog
->info
.stage
])
372 st_precompile_shader_variant(st
, glprog
);
376 /* Failed to find a matching cached shader so fallback to recompile.
378 if (ctx
->_Shader
->Flags
& GLSL_CACHE_INFO
) {
379 fprintf(stderr
, "TGSI cache item not found.\n");
382 goto fallback_recompile
;
391 if (ctx
->_Shader
->Flags
& GLSL_CACHE_INFO
)
392 fprintf(stderr
, "TGSI cache falling back to recompile.\n");
394 for (unsigned i
= 0; i
< prog
->NumShaders
; i
++) {
395 _mesa_glsl_compile_shader(ctx
, prog
->Shaders
[i
], false, false, true);
398 prog
->data
->skip_cache
= true;
399 _mesa_glsl_link_shader(ctx
, prog
);