void shader_cache_write_program_metadata(struct gl_context *ctx, struct gl_shader_program *prog) { struct disk_cache *cache = ctx->Cache; if (!cache) return; /* Exit early when we are dealing with a ff shader with no source file to * generate a source from. * * TODO: In future we should use another method to generate a key for ff * programs. */ static const char zero[sizeof(prog->data->sha1)] = {0}; if (memcmp(prog->data->sha1, zero, sizeof(prog->data->sha1)) == 0) return; struct blob metadata; blob_init(&metadata); serialize_glsl_program(&metadata, ctx, prog); struct cache_item_metadata cache_item_metadata; cache_item_metadata.type = CACHE_ITEM_TYPE_GLSL; cache_item_metadata.keys = (cache_key *) malloc(prog->NumShaders * sizeof(cache_key)); cache_item_metadata.num_keys = prog->NumShaders; if (!cache_item_metadata.keys) goto fail; char sha1_buf[41]; for (unsigned i = 0; i < prog->NumShaders; i++) { disk_cache_put_key(cache, prog->Shaders[i]->sha1); memcpy(cache_item_metadata.keys[i], prog->Shaders[i]->sha1, sizeof(cache_key)); if (ctx->_Shader->Flags & GLSL_CACHE_INFO) { _mesa_sha1_format(sha1_buf, prog->Shaders[i]->sha1); fprintf(stderr, "marking shader: %s\n", sha1_buf); } } disk_cache_put(cache, prog->data->sha1, metadata.data, metadata.size, &cache_item_metadata); if (ctx->_Shader->Flags & GLSL_CACHE_INFO) { _mesa_sha1_format(sha1_buf, prog->data->sha1); fprintf(stderr, "putting program metadata in cache: %s\n", sha1_buf); } fail: free(cache_item_metadata.keys); blob_finish(&metadata); }
static void radv_dump_shader(struct radv_pipeline *pipeline, struct radv_shader_variant *shader, gl_shader_stage stage, FILE *f) { if (!shader) return; fprintf(f, "%s:\n\n", radv_get_shader_name(shader, stage)); if (shader->spirv) { unsigned char sha1[21]; char sha1buf[41]; _mesa_sha1_compute(shader->spirv, shader->spirv_size, sha1); _mesa_sha1_format(sha1buf, sha1); fprintf(f, "SPIRV (sha1: %s):\n", sha1buf); radv_print_spirv(shader->spirv, shader->spirv_size, f); } if (shader->nir) { fprintf(f, "NIR:\n"); nir_print_shader(shader->nir, f); } fprintf(f, "LLVM IR:\n%s\n", shader->llvm_ir_string); fprintf(f, "DISASM:\n%s\n", shader->disasm_string); radv_shader_dump_stats(pipeline->device, shader, stage, f); }
/* Return a filename within the cache's directory corresponding to 'key'. The * returned filename is ralloced with 'cache' as the parent context. * * Returns NULL if out of memory. */ static char * get_cache_file(struct program_cache *cache, cache_key key) { char buf[41]; _mesa_sha1_format(buf, key); return ralloc_asprintf(cache, "%s/%c%c/%s", cache->path, buf[0], buf[1], buf + 2); }
/* Create the directory that will be needed for the cache file for \key. * * Obviously, the implementation here must closely match * _get_cache_file above. */ static void make_cache_file_directory(struct program_cache *cache, cache_key key) { char *dir; char buf[41]; _mesa_sha1_format(buf, key); dir = ralloc_asprintf(cache, "%s/%c%c", cache->path, buf[0], buf[1]); mkdir_if_needed(dir); ralloc_free(dir); }
bool shader_cache_read_program_metadata(struct gl_context *ctx, struct gl_shader_program *prog) { /* Fixed function programs generated by Mesa are not cached. So don't * try to read metadata for them from the cache. */ if (prog->Name == 0) return false; struct disk_cache *cache = ctx->Cache; if (!cache) return false; /* Include bindings when creating sha1. These bindings change the resulting * binary so they are just as important as the shader source. */ char *buf = ralloc_strdup(NULL, "vb: "); prog->AttributeBindings->iterate(create_binding_str, &buf); ralloc_strcat(&buf, "fb: "); prog->FragDataBindings->iterate(create_binding_str, &buf); ralloc_strcat(&buf, "fbi: "); prog->FragDataIndexBindings->iterate(create_binding_str, &buf); /* SSO has an effect on the linked program so include this when generating * the sha also. */ ralloc_asprintf_append(&buf, "sso: %s\n", prog->SeparateShader ? "T" : "F"); /* A shader might end up producing different output depending on the glsl * version supported by the compiler. For example a different path might be * taken by the preprocessor, so add the version to the hash input. */ ralloc_asprintf_append(&buf, "api: %d glsl: %d fglsl: %d\n", ctx->API, ctx->Const.GLSLVersion, ctx->Const.ForceGLSLVersion); /* We run the preprocessor on shaders after hashing them, so we need to * add any extension override vars to the hash. If we don't do this the * preprocessor could result in different output and we could load the * wrong shader. */ char *ext_override = getenv("MESA_EXTENSION_OVERRIDE"); if (ext_override) { ralloc_asprintf_append(&buf, "ext:%s", ext_override); } /* DRI config options may also change the output from the compiler so * include them as an input to sha1 creation. */ char sha1buf[41]; _mesa_sha1_format(sha1buf, ctx->Const.dri_config_options_sha1); ralloc_strcat(&buf, sha1buf); for (unsigned i = 0; i < prog->NumShaders; i++) { struct gl_shader *sh = prog->Shaders[i]; _mesa_sha1_format(sha1buf, sh->sha1); ralloc_asprintf_append(&buf, "%s: %s\n", _mesa_shader_stage_to_abbrev(sh->Stage), sha1buf); } disk_cache_compute_key(cache, buf, strlen(buf), prog->data->sha1); ralloc_free(buf); size_t size; uint8_t *buffer = (uint8_t *) disk_cache_get(cache, prog->data->sha1, &size); if (buffer == NULL) { /* Cached program not found. We may have seen the individual shaders * before and skipped compiling but they may not have been used together * in this combination before. Fall back to linking shaders but first * re-compile the shaders. * * We could probably only compile the shaders which were skipped here * but we need to be careful because the source may also have been * changed since the last compile so for now we just recompile * everything. */ compile_shaders(ctx, prog); return false; } if (ctx->_Shader->Flags & GLSL_CACHE_INFO) { _mesa_sha1_format(sha1buf, prog->data->sha1); fprintf(stderr, "loading shader program meta data from cache: %s\n", sha1buf); } struct blob_reader metadata; blob_reader_init(&metadata, buffer, size); bool deserialized = deserialize_glsl_program(&metadata, ctx, prog); if (!deserialized || metadata.current != metadata.end || metadata.overrun) { /* Something has gone wrong discard the item from the cache and rebuild * from source. */ assert(!"Invalid GLSL shader disk cache item!"); if (ctx->_Shader->Flags & GLSL_CACHE_INFO) { fprintf(stderr, "Error reading program from cache (invalid GLSL " "cache item)\n"); } disk_cache_remove(cache, prog->data->sha1); compile_shaders(ctx, prog); free(buffer); return false; } /* This is used to flag a shader retrieved from cache */ prog->data->LinkStatus = linking_skipped; /* Since the program load was successful, CompileStatus of all shaders at * this point should normally be compile_skipped. However because of how * the eviction works, it may happen that some of the individual shader keys * have been evicted, resulting in unnecessary recompiles on this load, so * mark them again to skip such recompiles next time. */ char sha1_buf[41]; for (unsigned i = 0; i < prog->NumShaders; i++) { if (prog->Shaders[i]->CompileStatus == compiled_no_opts) { disk_cache_put_key(cache, prog->Shaders[i]->sha1); if (ctx->_Shader->Flags & GLSL_CACHE_INFO) { _mesa_sha1_format(sha1_buf, prog->Shaders[i]->sha1); fprintf(stderr, "re-marking shader: %s\n", sha1_buf); } } } free (buffer); return true; }