Example #1
0
/* Test that data values are written and read with proper alignment. */
static void
test_alignment(void)
{
   void *ctx = ralloc_context(NULL);
   struct blob *blob;
   struct blob_reader reader;
   uint8_t bytes[] = "ABCDEFGHIJKLMNOP";
   size_t delta, last, num_bytes;

   blob = blob_create(ctx);

   /* First, write an intptr value to the blob and capture that size. This is
    * the expected offset between any pair of intptr values (if written with
    * alignment).
    */
   blob_write_intptr(blob, (intptr_t) blob);

   delta = blob->size;
   last = blob->size;

   /* Then loop doing the following:
    *
    *   1. Write an unaligned number of bytes
    *   2. Verify that write results in an unaligned size
    *   3. Write an intptr_t value
    *   2. Verify that that write results in an aligned size
    */
   for (num_bytes = 1; num_bytes < sizeof(intptr_t); num_bytes++) {
      blob_write_bytes(blob, bytes, num_bytes);

      expect_unequal(delta, blob->size - last, "unaligned write of bytes");

      blob_write_intptr(blob, (intptr_t) blob);

      expect_equal(2 * delta, blob->size - last, "aligned write of intptr");

      last = blob->size;
   }

   /* Finally, test that reading also does proper alignment. Since we know
    * that values were written with all the right alignment, all we have to do
    * here is verify that correct values are read.
    */
   blob_reader_init(&reader, blob->data, blob->size);

   expect_equal((intptr_t) blob, blob_read_intptr(&reader),
                "read of initial, aligned intptr_t");

   for (num_bytes = 1; num_bytes < sizeof(intptr_t); num_bytes++) {
      expect_equal_bytes(bytes, blob_read_bytes(&reader, num_bytes),
                         num_bytes, "unaligned read of bytes");
      expect_equal((intptr_t) blob, blob_read_intptr(&reader),
                   "aligned read of intptr_t");
   }

   ralloc_free(ctx);
}
Example #2
0
/* Test that we detect overrun. */
static void
test_overrun(void)
{
   struct blob blob;
   struct blob_reader reader;
   uint32_t value = 0xdeadbeef;

   blob_init(&blob);

   blob_write_uint32(&blob, value);

   blob_reader_init(&reader, blob.data, blob.size);

   expect_equal(value, blob_read_uint32(&reader), "read before overrun");
   expect_equal(false, reader.overrun, "overrun flag not set");
   expect_equal(0, blob_read_uint32(&reader), "read at overrun");
   expect_equal(true, reader.overrun, "overrun flag set");

   blob_finish(&blob);
}
Example #3
0
/* Test that we can read and write some large objects, (exercising the code in
 * the blob_write functions to realloc blob->data.
 */
static void
test_big_objects(void)
{
   void *ctx = ralloc_context(NULL);
   struct blob blob;
   struct blob_reader reader;
   int size = 1000;
   int count = 1000;
   size_t i;
   char *buf;

   blob_init(&blob);

   /* Initialize our buffer. */
   buf = ralloc_size(ctx, size);
   for (i = 0; i < size; i++) {
      buf[i] = i % 256;
   }

   /* Write it many times. */
   for (i = 0; i < count; i++) {
      blob_write_bytes(&blob, buf, size);
   }

   blob_reader_init(&reader, blob.data, blob.size);

   /* Read and verify it many times. */
   for (i = 0; i < count; i++) {
      expect_equal_bytes((uint8_t *) buf, blob_read_bytes(&reader, size), size,
                         "read of large objects");
   }

   expect_equal(reader.end - reader.data, reader.current - reader.data,
                "number of bytes read reading large objects");

   expect_equal(false, reader.overrun,
                "overrun flag not set reading large objects");

   blob_finish(&blob);
   ralloc_free(ctx);
}
Example #4
0
/* Test that we detect overrun. */
static void
test_overrun(void)
{
   void *ctx =ralloc_context(NULL);
   struct blob *blob;
   struct blob_reader reader;
   uint32_t value = 0xdeadbeef;

   blob = blob_create(ctx);

   blob_write_uint32(blob, value);

   blob_reader_init(&reader, blob->data, blob->size);

   expect_equal(value, blob_read_uint32(&reader), "read before overrun");
   expect_equal(false, reader.overrun, "overrun flag not set");
   expect_equal(0, blob_read_uint32(&reader), "read at overrun");
   expect_equal(true, reader.overrun, "overrun flag set");

   ralloc_free(ctx);
}
Example #5
0
/* Test at least one call of each blob_write_foo and blob_read_foo function,
 * verifying that we read out everything we wrote, that every bytes is
 * consumed, and that the overrun bit is not set.
 */
static void
test_write_and_read_functions (void)
{
   struct blob blob;
   struct blob_reader reader;
   ssize_t reserved;
   size_t str_offset, uint_offset;
   uint8_t reserve_buf[sizeof(reserve_test_str)];

   blob_init(&blob);

   /*** Test blob by writing one of every possible kind of value. */

   blob_write_bytes(&blob, bytes_test_str, sizeof(bytes_test_str));

   reserved = blob_reserve_bytes(&blob, sizeof(reserve_test_str));
   blob_overwrite_bytes(&blob, reserved, reserve_test_str, sizeof(reserve_test_str));

   /* Write a placeholder, (to be replaced later via overwrite_bytes) */
   str_offset = blob.size;
   blob_write_bytes(&blob, placeholder_str, sizeof(placeholder_str));

   blob_write_uint32(&blob, uint32_test);

   /* Write a placeholder, (to be replaced later via overwrite_uint32) */
   uint_offset = blob.size;
   blob_write_uint32(&blob, uint32_placeholder);

   blob_write_uint64(&blob, uint64_test);

   blob_write_intptr(&blob, (intptr_t) &blob);

   blob_write_string(&blob, string_test_str);

   /* Finally, overwrite our placeholders. */
   blob_overwrite_bytes(&blob, str_offset, overwrite_test_str,
                        sizeof(overwrite_test_str));
   blob_overwrite_uint32(&blob, uint_offset, uint32_overwrite);

   /*** Now read each value and verify. */
   blob_reader_init(&reader, blob.data, blob.size);

   expect_equal_str(bytes_test_str,
                    blob_read_bytes(&reader, sizeof(bytes_test_str)),
                    "blob_write/read_bytes");

   blob_copy_bytes(&reader, reserve_buf, sizeof(reserve_buf));
   expect_equal_str(reserve_test_str, (char *) reserve_buf,
                    "blob_reserve_bytes/blob_copy_bytes");

   expect_equal_str(overwrite_test_str,
                    blob_read_bytes(&reader, sizeof(overwrite_test_str)),
                    "blob_overwrite_bytes");

   expect_equal(uint32_test, blob_read_uint32(&reader),
                "blob_write/read_uint32");
   expect_equal(uint32_overwrite, blob_read_uint32(&reader),
                "blob_overwrite_uint32");
   expect_equal(uint64_test, blob_read_uint64(&reader),
                "blob_write/read_uint64");
   expect_equal((intptr_t) &blob, blob_read_intptr(&reader),
                "blob_write/read_intptr");
   expect_equal_str(string_test_str, blob_read_string(&reader),
                    "blob_write/read_string");

   expect_equal(reader.end - reader.data, reader.current - reader.data,
                "read_consumes_all_bytes");
   expect_equal(false, reader.overrun, "read_does_not_overrun");

   blob_finish(&blob);
}
static void
st_deserialise_ir_program(struct gl_context *ctx,
                          struct gl_shader_program *shProg,
                          struct gl_program *prog, bool nir)
{
   struct st_context *st = st_context(ctx);
   size_t size = prog->driver_cache_blob_size;
   uint8_t *buffer = (uint8_t *) prog->driver_cache_blob;
   const struct nir_shader_compiler_options *options =
      ctx->Const.ShaderCompilerOptions[prog->info.stage].NirOptions;

   assert(prog->driver_cache_blob && prog->driver_cache_blob_size > 0);

   struct blob_reader blob_reader;
   blob_reader_init(&blob_reader, buffer, size);

   switch (prog->info.stage) {
   case MESA_SHADER_VERTEX: {
      struct st_vertex_program *stvp = (struct st_vertex_program *) prog;

      st_release_vp_variants(st, stvp);

      stvp->num_inputs = blob_read_uint32(&blob_reader);
      blob_copy_bytes(&blob_reader, (uint8_t *) stvp->index_to_input,
                      sizeof(stvp->index_to_input));
      blob_copy_bytes(&blob_reader, (uint8_t *) stvp->input_to_index,
                      sizeof(stvp->input_to_index));
      blob_copy_bytes(&blob_reader, (uint8_t *) stvp->result_to_output,
                      sizeof(stvp->result_to_output));

      read_stream_out_from_cache(&blob_reader, &stvp->tgsi);

      if (nir) {
         stvp->tgsi.type = PIPE_SHADER_IR_NIR;
         stvp->shader_program = shProg;
         stvp->tgsi.ir.nir = nir_deserialize(NULL, options, &blob_reader);
         prog->nir = stvp->tgsi.ir.nir;
      } else {
         read_tgsi_from_cache(&blob_reader, &stvp->tgsi.tokens,
                              &stvp->num_tgsi_tokens);
      }

      if (st->vp == stvp)
         st->dirty |= ST_NEW_VERTEX_PROGRAM(st, stvp);

      break;
   }
   case MESA_SHADER_TESS_CTRL: {
      struct st_common_program *sttcp = st_common_program(prog);

      st_release_basic_variants(st, sttcp->Base.Target,
                                &sttcp->variants, &sttcp->tgsi);

      read_stream_out_from_cache(&blob_reader, &sttcp->tgsi);

      if (nir) {
         sttcp->tgsi.type = PIPE_SHADER_IR_NIR;
         sttcp->shader_program = shProg;
         sttcp->tgsi.ir.nir = nir_deserialize(NULL, options, &blob_reader);
         prog->nir = sttcp->tgsi.ir.nir;
      } else {
         read_tgsi_from_cache(&blob_reader, &sttcp->tgsi.tokens,
                              &sttcp->num_tgsi_tokens);
      }

      if (st->tcp == sttcp)
         st->dirty |= sttcp->affected_states;

      break;
   }
   case MESA_SHADER_TESS_EVAL: {
      struct st_common_program *sttep = st_common_program(prog);

      st_release_basic_variants(st, sttep->Base.Target,
                                &sttep->variants, &sttep->tgsi);

      read_stream_out_from_cache(&blob_reader, &sttep->tgsi);

      if (nir) {
         sttep->tgsi.type = PIPE_SHADER_IR_NIR;
         sttep->shader_program = shProg;
         sttep->tgsi.ir.nir = nir_deserialize(NULL, options, &blob_reader);
         prog->nir = sttep->tgsi.ir.nir;
      } else {
         read_tgsi_from_cache(&blob_reader, &sttep->tgsi.tokens,
                              &sttep->num_tgsi_tokens);
      }

      if (st->tep == sttep)
         st->dirty |= sttep->affected_states;

      break;
   }
   case MESA_SHADER_GEOMETRY: {
      struct st_common_program *stgp = st_common_program(prog);

      st_release_basic_variants(st, stgp->Base.Target, &stgp->variants,
                                &stgp->tgsi);

      read_stream_out_from_cache(&blob_reader, &stgp->tgsi);

      if (nir) {
         stgp->tgsi.type = PIPE_SHADER_IR_NIR;
         stgp->shader_program = shProg;
         stgp->tgsi.ir.nir = nir_deserialize(NULL, options, &blob_reader);
         prog->nir = stgp->tgsi.ir.nir;
      } else {
         read_tgsi_from_cache(&blob_reader, &stgp->tgsi.tokens,
                              &stgp->num_tgsi_tokens);
      }

      if (st->gp == stgp)
         st->dirty |= stgp->affected_states;

      break;
   }
   case MESA_SHADER_FRAGMENT: {
      struct st_fragment_program *stfp = (struct st_fragment_program *) prog;

      st_release_fp_variants(st, stfp);

      if (nir) {
         stfp->tgsi.type = PIPE_SHADER_IR_NIR;
         stfp->shader_program = shProg;
         stfp->tgsi.ir.nir = nir_deserialize(NULL, options, &blob_reader);
         prog->nir = stfp->tgsi.ir.nir;
      } else {
         read_tgsi_from_cache(&blob_reader, &stfp->tgsi.tokens,
                              &stfp->num_tgsi_tokens);
      }

      if (st->fp == stfp)
         st->dirty |= stfp->affected_states;

      break;
   }
   case MESA_SHADER_COMPUTE: {
      struct st_compute_program *stcp = (struct st_compute_program *) prog;

      st_release_cp_variants(st, stcp);

      if (nir) {
         stcp->tgsi.ir_type = PIPE_SHADER_IR_NIR;
         stcp->shader_program = shProg;
         stcp->tgsi.prog = nir_deserialize(NULL, options, &blob_reader);
         prog->nir = (nir_shader *) stcp->tgsi.prog;
      } else {
         read_tgsi_from_cache(&blob_reader,
                              (const struct tgsi_token**) &stcp->tgsi.prog,
                              &stcp->num_tgsi_tokens);
      }

      stcp->tgsi.req_local_mem = stcp->Base.info.cs.shared_size;
      stcp->tgsi.req_private_mem = 0;
      stcp->tgsi.req_input_mem = 0;

      if (st->cp == stcp)
         st->dirty |= stcp->affected_states;

      break;
   }
   default:
      unreachable("Unsupported stage");
   }

   /* Make sure we don't try to read more data than we wrote. This should
    * never happen in release builds but its useful to have this check to
    * catch development bugs.
    */
   if (blob_reader.current != blob_reader.end || blob_reader.overrun) {
      assert(!"Invalid TGSI shader disk cache item!");

      if (ctx->_Shader->Flags & GLSL_CACHE_INFO) {
         fprintf(stderr, "Error reading program from cache (invalid "
                 "TGSI cache item)\n");
      }
   }

   st_set_prog_affected_state_flags(prog);
   _mesa_associate_uniform_storage(ctx, shProg, prog, false);

   /* Create Gallium shaders now instead of on demand. */
   if (ST_DEBUG & DEBUG_PRECOMPILE ||
       st->shader_has_one_variant[prog->info.stage])
      st_precompile_shader_variant(st, prog);
}
Example #7
0
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;
}