mesa: change gl_link_status enums to uppercase
[mesa.git] / src / compiler / glsl / shader_cache.cpp
index 5064dc308d66c7b17f2837e3fb0eb2e4d85b2077..bf884af79063ad254cee702a4782640dbaa9e52a 100644 (file)
@@ -44,7 +44,6 @@
  * corrupt, etc) we will use a fallback path to compile and link the IR.
  */
 
-#include "blob.h"
 #include "compiler/shader_info.h"
 #include "glsl_symbol_table.h"
 #include "glsl_parser_extras.h"
 #include "main/core.h"
 #include "nir.h"
 #include "program.h"
-#include "util/disk_cache.h"
+#include "serialize.h"
+#include "shader_cache.h"
 #include "util/mesa-sha1.h"
-#include "util/string_to_uint_map.h"
+#include "string_to_uint_map.h"
 
 extern "C" {
 #include "main/enums.h"
@@ -74,315 +74,6 @@ compile_shaders(struct gl_context *ctx, struct gl_shader_program *prog) {
    }
 }
 
-static void
-encode_type_to_blob(struct blob *blob, const glsl_type *type)
-{
-   uint32_t encoding;
-
-   switch (type->base_type) {
-   case GLSL_TYPE_UINT:
-   case GLSL_TYPE_INT:
-   case GLSL_TYPE_FLOAT:
-   case GLSL_TYPE_BOOL:
-   case GLSL_TYPE_DOUBLE:
-   case GLSL_TYPE_UINT64:
-   case GLSL_TYPE_INT64:
-      encoding = (type->base_type << 24) |
-         (type->vector_elements << 4) |
-         (type->matrix_columns);
-      break;
-   case GLSL_TYPE_SAMPLER:
-      encoding = (type->base_type) << 24 |
-         (type->sampler_dimensionality << 4) |
-         (type->sampler_shadow << 3) |
-         (type->sampler_array << 2) |
-         (type->sampled_type);
-      break;
-   case GLSL_TYPE_SUBROUTINE:
-      encoding = type->base_type << 24;
-      blob_write_uint32(blob, encoding);
-      blob_write_string(blob, type->name);
-      return;
-   case GLSL_TYPE_IMAGE:
-      encoding = (type->base_type) << 24 |
-         (type->sampler_dimensionality << 3) |
-         (type->sampler_array << 2) |
-         (type->sampled_type);
-      break;
-   case GLSL_TYPE_ATOMIC_UINT:
-      encoding = (type->base_type << 24);
-      break;
-   case GLSL_TYPE_ARRAY:
-      blob_write_uint32(blob, (type->base_type) << 24);
-      blob_write_uint32(blob, type->length);
-      encode_type_to_blob(blob, type->fields.array);
-      return;
-   case GLSL_TYPE_STRUCT:
-   case GLSL_TYPE_INTERFACE:
-      blob_write_uint32(blob, (type->base_type) << 24);
-      blob_write_string(blob, type->name);
-      blob_write_uint32(blob, type->length);
-      blob_write_bytes(blob, type->fields.structure,
-                       sizeof(glsl_struct_field) * type->length);
-      for (unsigned i = 0; i < type->length; i++) {
-         encode_type_to_blob(blob, type->fields.structure[i].type);
-         blob_write_string(blob, type->fields.structure[i].name);
-      }
-
-      if (type->base_type == GLSL_TYPE_INTERFACE) {
-         blob_write_uint32(blob, type->interface_packing);
-         blob_write_uint32(blob, type->interface_row_major);
-      }
-      return;
-   case GLSL_TYPE_VOID:
-   case GLSL_TYPE_ERROR:
-   default:
-      assert(!"Cannot encode type!");
-      encoding = 0;
-      break;
-   }
-
-   blob_write_uint32(blob, encoding);
-}
-
-static const glsl_type *
-decode_type_from_blob(struct blob_reader *blob)
-{
-   uint32_t u = blob_read_uint32(blob);
-   glsl_base_type base_type = (glsl_base_type) (u >> 24);
-
-   switch (base_type) {
-   case GLSL_TYPE_UINT:
-   case GLSL_TYPE_INT:
-   case GLSL_TYPE_FLOAT:
-   case GLSL_TYPE_BOOL:
-   case GLSL_TYPE_DOUBLE:
-   case GLSL_TYPE_UINT64:
-   case GLSL_TYPE_INT64:
-      return glsl_type::get_instance(base_type, (u >> 4) & 0x0f, u & 0x0f);
-   case GLSL_TYPE_SAMPLER:
-      return glsl_type::get_sampler_instance((enum glsl_sampler_dim) ((u >> 4) & 0x07),
-                                             (u >> 3) & 0x01,
-                                             (u >> 2) & 0x01,
-                                             (glsl_base_type) ((u >> 0) & 0x03));
-   case GLSL_TYPE_SUBROUTINE:
-      return glsl_type::get_subroutine_instance(blob_read_string(blob));
-   case GLSL_TYPE_IMAGE:
-      return glsl_type::get_image_instance((enum glsl_sampler_dim) ((u >> 3) & 0x07),
-                                             (u >> 2) & 0x01,
-                                             (glsl_base_type) ((u >> 0) & 0x03));
-   case GLSL_TYPE_ATOMIC_UINT:
-      return glsl_type::atomic_uint_type;
-   case GLSL_TYPE_ARRAY: {
-      unsigned length = blob_read_uint32(blob);
-      return glsl_type::get_array_instance(decode_type_from_blob(blob),
-                                           length);
-   }
-   case GLSL_TYPE_STRUCT:
-   case GLSL_TYPE_INTERFACE: {
-      char *name = blob_read_string(blob);
-      unsigned num_fields = blob_read_uint32(blob);
-      glsl_struct_field *fields = (glsl_struct_field *)
-         blob_read_bytes(blob, sizeof(glsl_struct_field) * num_fields);
-      for (unsigned i = 0; i < num_fields; i++) {
-         fields[i].type = decode_type_from_blob(blob);
-         fields[i].name = blob_read_string(blob);
-      }
-
-      if (base_type == GLSL_TYPE_INTERFACE) {
-         enum glsl_interface_packing packing =
-            (glsl_interface_packing) blob_read_uint32(blob);
-         bool row_major = blob_read_uint32(blob);
-         return glsl_type::get_interface_instance(fields, num_fields,
-                                                  packing, row_major, name);
-      } else {
-         return glsl_type::get_record_instance(fields, num_fields, name);
-      }
-   }
-   case GLSL_TYPE_VOID:
-   case GLSL_TYPE_ERROR:
-   default:
-      assert(!"Cannot decode type!");
-      return NULL;
-   }
-}
-
-static void
-write_uniforms(struct blob *metadata, struct gl_shader_program *prog)
-{
-   blob_write_uint32(metadata, prog->SamplersValidated);
-   blob_write_uint32(metadata, prog->data->NumUniformStorage);
-   blob_write_uint32(metadata, prog->data->NumUniformDataSlots);
-
-   for (unsigned i = 0; i < prog->data->NumUniformStorage; i++) {
-      encode_type_to_blob(metadata, prog->data->UniformStorage[i].type);
-      blob_write_uint32(metadata, prog->data->UniformStorage[i].array_elements);
-      blob_write_string(metadata, prog->data->UniformStorage[i].name);
-      blob_write_uint32(metadata, prog->data->UniformStorage[i].storage -
-                                  prog->data->UniformDataSlots);
-      blob_write_uint32(metadata, prog->data->UniformStorage[i].remap_location);
-      blob_write_uint32(metadata, prog->data->UniformStorage[i].block_index);
-      blob_write_uint32(metadata, prog->data->UniformStorage[i].atomic_buffer_index);
-      blob_write_uint32(metadata, prog->data->UniformStorage[i].offset);
-      blob_write_uint32(metadata, prog->data->UniformStorage[i].array_stride);
-      blob_write_uint32(metadata, prog->data->UniformStorage[i].matrix_stride);
-      blob_write_uint32(metadata, prog->data->UniformStorage[i].row_major);
-      blob_write_uint32(metadata,
-                        prog->data->UniformStorage[i].num_compatible_subroutines);
-      blob_write_uint32(metadata,
-                        prog->data->UniformStorage[i].top_level_array_size);
-      blob_write_uint32(metadata,
-                        prog->data->UniformStorage[i].top_level_array_stride);
-   }
-}
-
-static void
-read_uniforms(struct blob_reader *metadata, struct gl_shader_program *prog)
-{
-   struct gl_uniform_storage *uniforms;
-   union gl_constant_value *data;
-
-   prog->SamplersValidated = blob_read_uint32(metadata);
-   prog->data->NumUniformStorage = blob_read_uint32(metadata);
-   prog->data->NumUniformDataSlots = blob_read_uint32(metadata);
-
-   uniforms = rzalloc_array(prog, struct gl_uniform_storage,
-                            prog->data->NumUniformStorage);
-   prog->data->UniformStorage = uniforms;
-
-   data = rzalloc_array(uniforms, union gl_constant_value,
-                        prog->data->NumUniformDataSlots);
-   prog->data->UniformDataSlots = data;
-
-   prog->UniformHash = new string_to_uint_map;
-
-   for (unsigned i = 0; i < prog->data->NumUniformStorage; i++) {
-      uniforms[i].type = decode_type_from_blob(metadata);
-      uniforms[i].array_elements = blob_read_uint32(metadata);
-      uniforms[i].name = ralloc_strdup(prog, blob_read_string (metadata));
-      uniforms[i].storage = data + blob_read_uint32(metadata);
-      uniforms[i].remap_location = blob_read_uint32(metadata);
-      uniforms[i].block_index = blob_read_uint32(metadata);
-      uniforms[i].atomic_buffer_index = blob_read_uint32(metadata);
-      uniforms[i].offset = blob_read_uint32(metadata);
-      uniforms[i].array_stride = blob_read_uint32(metadata);
-      uniforms[i].matrix_stride = blob_read_uint32(metadata);
-      uniforms[i].row_major = blob_read_uint32(metadata);
-      uniforms[i].num_compatible_subroutines = blob_read_uint32(metadata);
-      uniforms[i].top_level_array_size = blob_read_uint32(metadata);
-      uniforms[i].top_level_array_stride = blob_read_uint32(metadata);
-      prog->UniformHash->put(i, uniforms[i].name);
-   }
-}
-
-
-static void
-write_uniform_remap_table(struct blob *metadata,
-                          struct gl_shader_program *prog)
-{
-   blob_write_uint32(metadata, prog->NumUniformRemapTable);
-
-   for (unsigned i = 0; i < prog->NumUniformRemapTable; i++) {
-      blob_write_uint32(metadata, prog->UniformRemapTable[i] -
-                           prog->data->UniformStorage);
-   }
-}
-
-static void
-read_uniform_remap_table(struct blob_reader *metadata,
-                         struct gl_shader_program *prog)
-{
-   prog->NumUniformRemapTable = blob_read_uint32(metadata);
-
-   prog->UniformRemapTable =rzalloc_array(prog, struct gl_uniform_storage *,
-                                          prog->NumUniformRemapTable);
-
-   for (unsigned i = 0; i < prog->NumUniformRemapTable; i++) {
-      prog->UniformRemapTable[i] =
-         prog->data->UniformStorage + blob_read_uint32(metadata);
-   }
-}
-
-static void
-write_shader_parameters(struct blob *metadata,
-                        struct gl_program_parameter_list *params)
-{
-   blob_write_uint32(metadata, params->NumParameters);
-   uint32_t i = 0;
-
-   while (i < params->NumParameters) {
-      struct gl_program_parameter *param = &params->Parameters[i];
-
-      blob_write_uint32(metadata, param->Type);
-      blob_write_string(metadata, param->Name);
-      blob_write_uint32(metadata, param->Size);
-      blob_write_uint32(metadata, param->DataType);
-      blob_write_bytes(metadata, param->StateIndexes,
-                       sizeof(param->StateIndexes));
-
-      i += (param->Size + 3) / 4;
-   }
-
-   blob_write_bytes(metadata, params->ParameterValues,
-                    sizeof(gl_constant_value) * 4 * params->NumParameters);
-
-   blob_write_uint32(metadata, params->StateFlags);
-}
-
-static void
-read_shader_parameters(struct blob_reader *metadata,
-                       struct gl_program_parameter_list *params)
-{
-   gl_state_index state_indexes[STATE_LENGTH];
-   uint32_t i = 0;
-   uint32_t num_parameters = blob_read_uint32(metadata);
-
-   while (i < num_parameters) {
-      gl_register_file type = (gl_register_file) blob_read_uint32(metadata);
-      const char *name = blob_read_string(metadata);
-      unsigned size = blob_read_uint32(metadata);
-      unsigned data_type = blob_read_uint32(metadata);
-      blob_copy_bytes(metadata, (uint8_t *) state_indexes,
-                      sizeof(state_indexes));
-
-      _mesa_add_parameter(params, type, name, size, data_type,
-                          NULL, state_indexes);
-
-      i += (size + 3) / 4;
-   }
-
-   blob_copy_bytes(metadata, (uint8_t *) params->ParameterValues,
-                    sizeof(gl_constant_value) * 4 * params->NumParameters);
-
-   params->StateFlags = blob_read_uint32(metadata);
-}
-
-static void
-write_shader_metadata(struct blob *metadata, gl_linked_shader *shader)
-{
-   assert(shader->Program);
-   struct gl_program *glprog = shader->Program;
-
-   blob_write_bytes(metadata, glprog->TexturesUsed,
-                    sizeof(glprog->TexturesUsed));
-   blob_write_uint64(metadata, glprog->SamplersUsed);
-
-   write_shader_parameters(metadata, glprog->Parameters);
-}
-
-static void
-read_shader_metadata(struct blob_reader *metadata,
-                     struct gl_program *glprog,
-                     gl_linked_shader *linked)
-{
-   blob_copy_bytes(metadata, (uint8_t *) glprog->TexturesUsed,
-                   sizeof(glprog->TexturesUsed));
-   glprog->SamplersUsed = blob_read_uint64(metadata);
-
-   glprog->Parameters = _mesa_new_parameter_list();
-   read_shader_parameters(metadata, glprog->Parameters);
-}
-
 static void
 create_binding_str(const char *key, unsigned value, void *closure)
 {
@@ -390,36 +81,6 @@ create_binding_str(const char *key, unsigned value, void *closure)
    ralloc_asprintf_append(bindings_str, "%s:%u,", key, value);
 }
 
-static void
-create_linked_shader_and_program(struct gl_context *ctx,
-                                 gl_shader_stage stage,
-                                 struct gl_shader_program *prog,
-                                 struct blob_reader *metadata)
-{
-   struct gl_program *glprog;
-
-   struct gl_linked_shader *linked = rzalloc(NULL, struct gl_linked_shader);
-   linked->Stage = stage;
-
-   glprog = ctx->Driver.NewProgram(ctx, _mesa_shader_stage_to_program(stage),
-                                   prog->Name, false);
-   glprog->info.stage = stage;
-   linked->Program = glprog;
-
-   read_shader_metadata(metadata, glprog, linked);
-
-   /* Restore shader info */
-   blob_copy_bytes(metadata, (uint8_t *) &glprog->info, sizeof(shader_info));
-   if (glprog->info.name)
-      glprog->info.name = ralloc_strdup(glprog, blob_read_string(metadata));
-   if (glprog->info.label)
-      glprog->info.label = ralloc_strdup(glprog, blob_read_string(metadata));
-
-   _mesa_reference_shader_program_data(ctx, &glprog->sh.data, prog->data);
-   _mesa_reference_program(ctx, &linked->Program, glprog);
-   prog->_LinkedShaders[stage] = linked;
-}
-
 void
 shader_cache_write_program_metadata(struct gl_context *ctx,
                                     struct gl_shader_program *prog)
@@ -434,51 +95,46 @@ shader_cache_write_program_metadata(struct gl_context *ctx,
     * TODO: In future we should use another method to generate a key for ff
     * programs.
     */
-   if (*prog->data->sha1 == 0)
+   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_create(NULL);
-
-   write_uniforms(metadata, prog);
-
-   blob_write_uint32(metadata, prog->data->Version);
-   blob_write_uint32(metadata, prog->data->linked_stages);
-
-   for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
-      struct gl_linked_shader *sh = prog->_LinkedShaders[i];
-      if (sh) {
-         write_shader_metadata(metadata, sh);
+   struct blob metadata;
+   blob_init(&metadata);
 
-         /* Store nir shader info */
-         blob_write_bytes(metadata, &sh->Program->info, sizeof(shader_info));
+   serialize_glsl_program(&metadata, ctx, prog);
 
-         if (sh->Program->info.name)
-            blob_write_string(metadata, sh->Program->info.name);
-
-         if (sh->Program->info.label)
-            blob_write_string(metadata, sh->Program->info.label);
-      }
-   }
+   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;
 
-   write_uniform_remap_table(metadata, prog);
+   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) {
-         fprintf(stderr, "marking shader: %s\n",
-                 _mesa_sha1_format(sha1_buf, prog->Shaders[i]->sha1));
+         _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);
-
-   ralloc_free(metadata);
+   disk_cache_put(cache, prog->data->sha1, metadata.data, metadata.size,
+                  &cache_item_metadata);
 
    if (ctx->_Shader->Flags & GLSL_CACHE_INFO) {
-      fprintf(stderr, "putting program metadata in cache: %s\n",
-              _mesa_sha1_format(sha1_buf, prog->data->sha1));
+      _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);
 }
 
 bool
@@ -511,14 +167,38 @@ shader_cache_read_program_metadata(struct gl_context *ctx,
    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),
-                             _mesa_sha1_format(sha1buf, sh->sha1));
+                             _mesa_shader_stage_to_abbrev(sh->Stage), sha1buf);
    }
-   _mesa_sha1_compute(buf, strlen(buf), prog->data->sha1);
+   disk_cache_compute_key(cache, buf, strlen(buf), prog->data->sha1);
    ralloc_free(buf);
 
    size_t size;
@@ -540,30 +220,17 @@ shader_cache_read_program_metadata(struct gl_context *ctx,
    }
 
    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",
-              _mesa_sha1_format(sha1buf, prog->data->sha1));
+              sha1buf);
    }
 
    struct blob_reader metadata;
    blob_reader_init(&metadata, buffer, size);
 
-   assert(prog->data->UniformStorage == NULL);
-
-   read_uniforms(&metadata, prog);
-
-   prog->data->Version = blob_read_uint32(&metadata);
-   prog->data->linked_stages = blob_read_uint32(&metadata);
-
-   unsigned mask = prog->data->linked_stages;
-   while (mask) {
-      const int j = u_bit_scan(&mask);
-      create_linked_shader_and_program(ctx, (gl_shader_stage) j, prog,
-                                       &metadata);
-   }
-
-   read_uniform_remap_table(&metadata, prog);
+   bool deserialized = deserialize_glsl_program(&metadata, ctx, prog);
 
-   if (metadata.current != metadata.end || metadata.overrun) {
+   if (!deserialized || metadata.current != metadata.end || metadata.overrun) {
       /* Something has gone wrong discard the item from the cache and rebuild
        * from source.
        */
@@ -581,7 +248,24 @@ shader_cache_read_program_metadata(struct gl_context *ctx,
    }
 
    /* This is used to flag a shader retrieved from cache */
-   prog->data->LinkStatus = linking_skipped;
+   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);