replace _mesa_logbase2 with util_logbase2
[mesa.git] / src / mesa / state_tracker / st_shader_cache.c
index b9c4556d634da8d356e036f99a3a700d1a7df042..000d1c2688b7dc4030594efdc8ee1ad359025b1c 100644 (file)
@@ -44,8 +44,13 @@ static void
 write_stream_out_to_cache(struct blob *blob,
                           struct pipe_shader_state *state)
 {
-   blob_write_bytes(blob, &state->stream_output,
-                    sizeof(state->stream_output));
+   blob_write_uint32(blob, state->stream_output.num_outputs);
+   if (state->stream_output.num_outputs) {
+      blob_write_bytes(blob, &state->stream_output.stride,
+                       sizeof(state->stream_output.stride));
+      blob_write_bytes(blob, &state->stream_output.output,
+                       sizeof(state->stream_output.output));
+   }
 }
 
 static void
@@ -70,7 +75,13 @@ write_tgsi_to_cache(struct blob *blob, const struct tgsi_token *tokens,
 static void
 write_nir_to_cache(struct blob *blob, struct gl_program *prog)
 {
-   nir_serialize(blob, prog->nir, false);
+   struct st_program *stp = (struct st_program *)prog;
+
+   st_serialize_nir(stp);
+
+   blob_write_intptr(blob, stp->serialized_nir_size);
+   blob_write_bytes(blob, stp->serialized_nir, stp->serialized_nir_size);
+
    copy_blob_to_driver_cache_blob(blob, prog);
 }
 
@@ -86,13 +97,15 @@ st_serialise_ir_program(struct gl_context *ctx, struct gl_program *prog,
    blob_init(&blob);
 
    if (prog->info.stage == MESA_SHADER_VERTEX) {
-      blob_write_uint32(&blob, stp->num_inputs);
-      blob_write_bytes(&blob, stp->index_to_input,
-                       sizeof(stp->index_to_input));
-      blob_write_bytes(&blob, stp->input_to_index,
-                       sizeof(stp->input_to_index));
-      blob_write_bytes(&blob, stp->result_to_output,
-                       sizeof(stp->result_to_output));
+      struct st_vertex_program *stvp = (struct st_vertex_program *)stp;
+
+      blob_write_uint32(&blob, stvp->num_inputs);
+      blob_write_bytes(&blob, stvp->index_to_input,
+                       sizeof(stvp->index_to_input));
+      blob_write_bytes(&blob, stvp->input_to_index,
+                       sizeof(stvp->input_to_index));
+      blob_write_bytes(&blob, stvp->result_to_output,
+                       sizeof(stvp->result_to_output));
    }
 
    if (prog->info.stage == MESA_SHADER_VERTEX ||
@@ -137,8 +150,14 @@ static void
 read_stream_out_from_cache(struct blob_reader *blob_reader,
                            struct pipe_shader_state *state)
 {
-   blob_copy_bytes(blob_reader, (uint8_t *) &state->stream_output,
-                    sizeof(state->stream_output));
+   memset(&state->stream_output, 0, sizeof(state->stream_output));
+   state->stream_output.num_outputs = blob_read_uint32(blob_reader);
+   if (state->stream_output.num_outputs) {
+      blob_copy_bytes(blob_reader, &state->stream_output.stride,
+                      sizeof(state->stream_output.stride));
+      blob_copy_bytes(blob_reader, &state->stream_output.output,
+                      sizeof(state->stream_output.output));
+   }
 }
 
 static void
@@ -159,8 +178,6 @@ st_deserialise_ir_program(struct gl_context *ctx,
    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;
 
    st_set_prog_affected_state_flags(prog);
    _mesa_associate_uniform_storage(ctx, shProg, prog);
@@ -171,20 +188,17 @@ st_deserialise_ir_program(struct gl_context *ctx,
    struct blob_reader blob_reader;
    blob_reader_init(&blob_reader, buffer, size);
 
+   st_release_variants(st, stp);
+
    if (prog->info.stage == MESA_SHADER_VERTEX) {
-      st_release_vp_variants(st, stp);
-
-      stp->num_inputs = blob_read_uint32(&blob_reader);
-      blob_copy_bytes(&blob_reader, (uint8_t *) stp->index_to_input,
-                      sizeof(stp->index_to_input));
-      blob_copy_bytes(&blob_reader, (uint8_t *) stp->input_to_index,
-                      sizeof(stp->input_to_index));
-      blob_copy_bytes(&blob_reader, (uint8_t *) stp->result_to_output,
-                      sizeof(stp->result_to_output));
-   } else if (prog->info.stage == MESA_SHADER_FRAGMENT) {
-      st_release_fp_variants(st, stp);
-   } else {
-      st_release_common_variants(st, stp);
+      struct st_vertex_program *stvp = (struct st_vertex_program *)stp;
+      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));
    }
 
    if (prog->info.stage == MESA_SHADER_VERTEX ||
@@ -193,10 +207,14 @@ st_deserialise_ir_program(struct gl_context *ctx,
       read_stream_out_from_cache(&blob_reader, &stp->state);
 
    if (nir) {
+      assert(prog->nir == NULL);
+      assert(stp->serialized_nir == NULL);
+
       stp->state.type = PIPE_SHADER_IR_NIR;
-      stp->state.ir.nir = nir_deserialize(NULL, options, &blob_reader);
+      stp->serialized_nir_size = blob_read_intptr(&blob_reader);
+      stp->serialized_nir = malloc(stp->serialized_nir_size);
+      blob_copy_bytes(&blob_reader, stp->serialized_nir, stp->serialized_nir_size);
       stp->shader_program = shProg;
-      prog->nir = stp->state.ir.nir;
    } else {
       read_tgsi_from_cache(&blob_reader, &stp->state.tokens);
    }