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; }
int test() { // Create a new block cache group. disk_cache_group_t *grp = disk_cache_group_new(); // And several new block caches. disk_cache_t *cache1 = disk_cache_new(grp, &dev); disk_cache_t *cache2 = disk_cache_new(grp, &dev); // Create some scratch vmspace. void *scratch1 = (void*)vmspace_alloc(&kernel_vmspace, 0x1000, 0); void *scratch2 = (void*)vmspace_alloc(&kernel_vmspace, 0x1000, 0); void *scratch3 = (void*)vmspace_alloc(&kernel_vmspace, 0x1000, 0); // Test caching of data. // CHECK: b1 = 1 kprintf("b1 = %d\n", disk_cache_get(cache1, 0x0, scratch1)); // CHECK: b2 = 1 kprintf("b2 = %d\n", disk_cache_get(cache2, 0x0, scratch2)); // CHECK: b3 = 1 kprintf("b3 = %d\n", disk_cache_get(cache1, 0x1000, scratch3)); // CHECK: c1[0] = 0x0 c1[1] = 0x4 // CHECK: c2[0] = 0x0 c2[1] = 0x4 // CHECK: c3[0] = 0x1000 c3[1] = 0x1004 kprintf("c1[0] = %#x c1[1] = %#x\n", ((uint32_t*)scratch1)[0], ((uint32_t*)scratch1)[1]); kprintf("c2[0] = %#x c2[1] = %#x\n", ((uint32_t*)scratch2)[0], ((uint32_t*)scratch2)[1]); kprintf("c3[0] = %#x c3[1] = %#x\n", ((uint32_t*)scratch3)[0], ((uint32_t*)scratch3)[1]); // Attempt to evict a page. It should fail because all allocated // pages have handles. // CHECK: evict = 0 kprintf("evict = %d\n", disk_cache_group_evict(grp, 0x1000)); // Now release one handle, and check it is still cached (and that there // are no handles left). unmap((uintptr_t)scratch2, 1); disk_cache_release(cache2, 0x0); // CHECK: released: iscached 1 n_handles 0 kprintf("released: iscached %d n_handles %d\n", disk_cache_is_cached(cache2, 0x0), disk_cache_get_n_handles(cache2, 0x0)); // Try eviction again. It should succeed. // CHECK: evict = 1 kprintf("evict = %d\n", disk_cache_group_evict(grp, 0x1000)); // And now the address should not be cached. // CHECK: released: iscached 0 n_handles 0 kprintf("released: iscached %d n_handles %d\n", disk_cache_is_cached(cache2, 0x0), disk_cache_get_n_handles(cache2, 0x0)); // Get another handle to one address, and check the #handles increases // and they map to the same phys address. // CHECK: b4 = 1 kprintf("b4 = %d\n", disk_cache_get(cache1, 0x1000, scratch2)); // CHECK: nhandles = 2 kprintf("nhandles = %d\n", disk_cache_get_n_handles(cache1, 0x1000)); // CHECK: m1 = [[addr:0x[a-f0-9]*]] // CHECK: m2 = [[addr]] unsigned flags; kprintf("m1 = %#x\nm2 = %#x\n", (uint32_t)get_mapping((uintptr_t)scratch3, &flags), (uint32_t)get_mapping((uintptr_t)scratch2, &flags)); return 0; }