static void
read_tgsi_from_cache(struct blob_reader *blob_reader,
                     const struct tgsi_token **tokens,
                     unsigned *num_tokens)
{
   *num_tokens  = blob_read_uint32(blob_reader);
   unsigned tokens_size = *num_tokens * sizeof(struct tgsi_token);
   *tokens = (const struct tgsi_token*) MALLOC(tokens_size);
   blob_copy_bytes(blob_reader, (uint8_t *) *tokens, tokens_size);
}
Beispiel #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);
}
Beispiel #3
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);
}
Beispiel #4
0
static nir_constant *
read_constant(read_ctx *ctx, nir_variable *nvar)
{
   nir_constant *c = ralloc(nvar, nir_constant);

   blob_copy_bytes(ctx->blob, (uint8_t *)c->values, sizeof(c->values));
   c->num_elements = blob_read_uint32(ctx->blob);
   c->elements = ralloc_array(nvar, nir_constant *, c->num_elements);
   for (unsigned i = 0; i < c->num_elements; i++)
      c->elements[i] = read_constant(ctx, nvar);

   return c;
}
Beispiel #5
0
static nir_variable *
read_variable(read_ctx *ctx)
{
   nir_variable *var = rzalloc(ctx->nir, nir_variable);
   read_add_object(ctx, var);

   var->type = decode_type_from_blob(ctx->blob);
   bool has_name = blob_read_uint32(ctx->blob);
   if (has_name) {
      const char *name = blob_read_string(ctx->blob);
      var->name = ralloc_strdup(var, name);
   } else {
      var->name = NULL;
   }
   blob_copy_bytes(ctx->blob, (uint8_t *) &var->data, sizeof(var->data));
   var->num_state_slots = blob_read_uint32(ctx->blob);
   var->state_slots = ralloc_array(var, nir_state_slot, var->num_state_slots);
   blob_copy_bytes(ctx->blob, (uint8_t *) var->state_slots,
                   var->num_state_slots * sizeof(nir_state_slot));
   bool has_const_initializer = blob_read_uint32(ctx->blob);
   if (has_const_initializer)
      var->constant_initializer = read_constant(ctx, var);
   else
      var->constant_initializer = NULL;
   bool has_interface_type = blob_read_uint32(ctx->blob);
   if (has_interface_type)
      var->interface_type = decode_type_from_blob(ctx->blob);
   else
      var->interface_type = NULL;
   var->num_members = blob_read_uint32(ctx->blob);
   if (var->num_members > 0) {
      var->members = ralloc_array(var, struct nir_variable_data,
                                  var->num_members);
      blob_copy_bytes(ctx->blob, (uint8_t *) var->members,
                      var->num_members * sizeof(*var->members));
   }
Beispiel #6
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);
}