[Mesa-dev] [PATCH 38/40] st/mesa: implement a tgsi on-disk shader cache
Timothy Arceri
tarceri at itsqueeze.com
Tue Feb 7 03:42:42 UTC 2017
Implements a tgsi cache for the OpenGL state tracker.
---
src/mesa/state_tracker/st_glsl_to_tgsi.cpp | 217 +++++++++++++++++++++++++++++
src/mesa/state_tracker/st_program.c | 133 +++++++++++++++++-
2 files changed, 345 insertions(+), 5 deletions(-)
diff --git a/src/mesa/state_tracker/st_glsl_to_tgsi.cpp b/src/mesa/state_tracker/st_glsl_to_tgsi.cpp
index 5a65f32..452cfdd 100644
--- a/src/mesa/state_tracker/st_glsl_to_tgsi.cpp
+++ b/src/mesa/state_tracker/st_glsl_to_tgsi.cpp
@@ -32,6 +32,7 @@
#include "st_glsl_to_tgsi.h"
+#include "compiler/glsl/blob.h"
#include "compiler/glsl/glsl_parser_extras.h"
#include "compiler/glsl/ir_optimization.h"
#include "compiler/glsl/program.h"
@@ -47,6 +48,8 @@
#include "pipe/p_screen.h"
#include "tgsi/tgsi_ureg.h"
#include "tgsi/tgsi_info.h"
+#include "util/disk_cache.h"
+#include "util/mesa-sha1.h"
#include "util/u_math.h"
#include "util/u_memory.h"
#include "st_program.h"
@@ -7001,6 +7004,214 @@ has_unsupported_control_flow(exec_list *ir,
return visitor.unsupported;
}
+static void
+read_stream_out_from_cache(struct blob_reader *st_blob,
+ struct pipe_shader_state *tgsi)
+{
+ blob_copy_bytes(st_blob, (uint8_t *) &tgsi->stream_output,
+ sizeof(tgsi->stream_output));
+}
+
+static void
+read_tgsi_from_cache(struct blob_reader *st_blob,
+ struct pipe_shader_state *tgsi)
+{
+ uint32_t num_tokens = blob_read_uint32(st_blob);
+ unsigned tokens_size = num_tokens * sizeof(struct tgsi_token);
+ tgsi->tokens = (const tgsi_token*) MALLOC(tokens_size);
+ blob_copy_bytes(st_blob, (uint8_t *) tgsi->tokens, tokens_size);
+}
+
+static void
+load_tgsi_from_disk_cache(struct gl_context *ctx,
+ struct gl_shader_program *prog)
+{
+ unsigned char sha1[20];
+ char sha1_buf[41];
+ struct st_context *st = st_context(ctx);
+
+ for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
+ if (prog->_LinkedShaders[i] == NULL)
+ continue;
+
+ char *buf = ralloc_strdup(NULL, "tsgi_tokens ");
+ _mesa_sha1_format(sha1_buf,
+ prog->_LinkedShaders[i]->Program->sh.data->sha1);
+ ralloc_strcat(&buf, sha1_buf);
+
+ struct gl_program *glprog = prog->_LinkedShaders[i]->Program;
+ switch (glprog->info.stage) {
+ case MESA_SHADER_VERTEX:
+ ralloc_strcat(&buf, " vs");
+ _mesa_sha1_compute(buf, strlen(buf), sha1);
+ break;
+ case MESA_SHADER_TESS_EVAL:
+ ralloc_strcat(&buf, " tes");
+ _mesa_sha1_compute(buf, strlen(buf), sha1);
+ break;
+ case MESA_SHADER_TESS_CTRL:
+ ralloc_strcat(&buf, " tcs");
+ _mesa_sha1_compute(buf, strlen(buf), sha1);
+ break;
+ case MESA_SHADER_GEOMETRY:
+ ralloc_strcat(&buf, " gs");
+ _mesa_sha1_compute(buf, strlen(buf), sha1);
+ break;
+ case MESA_SHADER_FRAGMENT:
+ ralloc_strcat(&buf, " fs");
+ _mesa_sha1_compute(buf, strlen(buf), sha1);
+ break;
+ case MESA_SHADER_COMPUTE:
+ break;
+
+ default:
+ unreachable("Unsupported stage");
+ }
+
+ size_t size;
+ const tgsi_token *tgsi_tokens = NULL;
+ uint8_t *buffer = (uint8_t *) disk_cache_get(ctx->Cache, sha1, &size);
+ if (buffer) {
+ struct blob_reader st_blob;
+ blob_reader_init(&st_blob, buffer, size);
+
+ switch (glprog->info.stage) {
+ case MESA_SHADER_VERTEX: {
+ struct st_vertex_program *stvp =
+ (struct st_vertex_program *) glprog;
+
+ st_release_vp_variants(st, stvp);
+
+ stvp->num_inputs = blob_read_uint32(&st_blob);
+ blob_copy_bytes(&st_blob, (uint8_t *) stvp->index_to_input,
+ sizeof(stvp->index_to_input));
+ blob_copy_bytes(&st_blob, (uint8_t *) stvp->result_to_output,
+ sizeof(stvp->result_to_output));
+
+ read_stream_out_from_cache(&st_blob, &stvp->tgsi);
+ read_tgsi_from_cache(&st_blob, &stvp->tgsi);
+
+ if (st->vp == stvp)
+ st->dirty |= ST_NEW_VERTEX_PROGRAM(st, stvp);
+
+ tgsi_tokens = stvp->tgsi.tokens;
+ break;
+ }
+ case MESA_SHADER_TESS_EVAL: {
+ struct st_tesseval_program *sttep =
+ (struct st_tesseval_program *) glprog;
+
+ st_release_basic_variants(st, sttep->Base.Target,
+ &sttep->variants, &sttep->tgsi);
+
+ read_stream_out_from_cache(&st_blob, &sttep->tgsi);
+ read_tgsi_from_cache(&st_blob, &sttep->tgsi);
+
+ if (st->tep == sttep)
+ st->dirty |= sttep->affected_states;
+
+ tgsi_tokens = sttep->tgsi.tokens;
+ break;
+ }
+ case MESA_SHADER_TESS_CTRL: {
+ struct st_tessctrl_program *sttcp =
+ (struct st_tessctrl_program *) glprog;
+
+ st_release_basic_variants(st, sttcp->Base.Target,
+ &sttcp->variants, &sttcp->tgsi);
+
+ read_stream_out_from_cache(&st_blob, &sttcp->tgsi);
+ read_tgsi_from_cache(&st_blob, &sttcp->tgsi);
+
+ if (st->tcp == sttcp)
+ st->dirty |= sttcp->affected_states;
+
+ tgsi_tokens = sttcp->tgsi.tokens;
+ break;
+ }
+ case MESA_SHADER_GEOMETRY: {
+ struct st_geometry_program *stgp =
+ (struct st_geometry_program *) glprog;
+
+ st_release_basic_variants(st, stgp->Base.Target, &stgp->variants,
+ &stgp->tgsi);
+
+ read_stream_out_from_cache(&st_blob, &stgp->tgsi);
+ read_tgsi_from_cache(&st_blob, &stgp->tgsi);
+
+ if (st->gp == stgp)
+ st->dirty |= stgp->affected_states;
+
+ tgsi_tokens = stgp->tgsi.tokens;
+ break;
+ }
+ case MESA_SHADER_FRAGMENT: {
+ struct st_fragment_program *stfp =
+ (struct st_fragment_program *) glprog;
+
+ st_release_fp_variants(st, stfp);
+
+ read_tgsi_from_cache(&st_blob, &stfp->tgsi);
+
+ if (st->fp == stfp)
+ st->dirty |= stfp->affected_states;
+
+ tgsi_tokens = stfp->tgsi.tokens;
+ break;
+ }
+ case MESA_SHADER_COMPUTE:
+ /* TODO: add compute support. Note: support needs to be added to
+ * the GLSL IR cache first.
+ */
+ free(buf);
+ return;
+
+ default:
+ unreachable("Unsupported stage");
+ }
+
+ if (st_blob.current != st_blob.end || st_blob.overrun) {
+ /* Something very bad has gone wrong discard the item from the
+ * cache and rebuild/link from source.
+ */
+ 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");
+ }
+
+ disk_cache_remove(ctx->Cache, sha1);
+ for (unsigned i = 0; i < prog->NumShaders; i++) {
+ _mesa_glsl_compile_shader(ctx, prog->Shaders[i], false, false,
+ true);
+ }
+
+ prog->data->cache_fallback = true;
+ _mesa_glsl_link_shader(ctx, prog);
+
+ free(buf);
+ return;
+ }
+
+ free(buffer);
+ }
+
+ if (tgsi_tokens) {
+ if (ctx->_Shader->Flags & GLSL_CACHE_INFO) {
+ _mesa_sha1_format(sha1_buf, sha1);
+ fprintf(stderr, "%s tgsi_tokens retrieved from cache: %s\n",
+ _mesa_shader_stage_to_string(i), sha1_buf);
+ }
+
+ set_prog_affected_state_flags(glprog);
+ _mesa_associate_uniform_storage(ctx, prog, glprog->Parameters);
+ }
+
+ ralloc_free(buf);
+ }
+}
+
extern "C" {
/**
@@ -7012,6 +7223,12 @@ extern "C" {
GLboolean
st_link_shader(struct gl_context *ctx, struct gl_shader_program *prog)
{
+ /* Return early if we are loading the shader from on-disk cache */
+ if (ctx->Cache && prog->data->LinkStatus == linking_skipped) {
+ load_tgsi_from_disk_cache(ctx, prog);
+ return GL_TRUE;
+ }
+
struct pipe_screen *pscreen = ctx->st->pipe->screen;
assert(prog->data->LinkStatus);
diff --git a/src/mesa/state_tracker/st_program.c b/src/mesa/state_tracker/st_program.c
index b2d1586..419dabd 100644
--- a/src/mesa/state_tracker/st_program.c
+++ b/src/mesa/state_tracker/st_program.c
@@ -37,7 +37,10 @@
#include "program/prog_parameter.h"
#include "program/prog_print.h"
#include "program/programopt.h"
+#include "util/disk_cache.h"
+#include "util/mesa-sha1.h"
+#include "compiler/glsl/blob.h"
#include "compiler/nir/nir.h"
#include "pipe/p_context.h"
@@ -223,6 +226,111 @@ st_release_cp_variants(struct st_context *st, struct st_compute_program *stcp)
}
}
+static void
+write_stream_out_to_cache(struct blob *st_blob,
+ struct pipe_shader_state *tgsi)
+{
+ blob_write_bytes(st_blob, &tgsi->stream_output,
+ sizeof(tgsi->stream_output));
+}
+
+static void
+write_tgsi_to_cache(struct blob *st_blob, struct pipe_shader_state *tgsi,
+ struct st_context *st, unsigned char *sha1,
+ unsigned num_tokens)
+{
+ blob_write_uint32(st_blob, num_tokens);
+ blob_write_bytes(st_blob, tgsi->tokens,
+ num_tokens * sizeof(struct tgsi_token));
+
+ disk_cache_put(st->ctx->Cache, sha1, st_blob->data, st_blob->size);
+}
+
+/**
+ * Store tgsi and any other required state in on-disk shader cache.
+ */
+static void
+cache_tgsi(struct st_context *st, struct gl_program *prog,
+ struct pipe_shader_state *out_state, unsigned num_tokens)
+{
+ if (!st->ctx->Cache)
+ return;
+
+ /* Exit early when we are dealing with a ff shader with no source file to
+ * generate a source from.
+ */
+ if (*prog->sh.data->sha1 == 0)
+ return;
+
+ unsigned char sha1[20];
+ char sha1_buf[41];
+ char *buf = ralloc_strdup(NULL, "tsgi_tokens ");
+ struct blob *st_blob = blob_create(NULL);
+
+ _mesa_sha1_format(sha1_buf, prog->sh.data->sha1);
+ ralloc_strcat(&buf, sha1_buf);
+
+ switch (prog->info.stage) {
+ case MESA_SHADER_VERTEX: {
+ ralloc_strcat(&buf, " vs");
+ _mesa_sha1_compute(buf, strlen(buf), sha1);
+
+ struct st_vertex_program *stvp = (struct st_vertex_program *) prog;
+
+ blob_write_uint32(st_blob, stvp->num_inputs);
+ blob_write_bytes(st_blob, stvp->index_to_input,
+ sizeof(stvp->index_to_input));
+ blob_write_bytes(st_blob, stvp->result_to_output,
+ sizeof(stvp->result_to_output));
+
+ write_stream_out_to_cache(st_blob, &stvp->tgsi);
+ write_tgsi_to_cache(st_blob, &stvp->tgsi, st, sha1, num_tokens);
+ break;
+ }
+ case MESA_SHADER_TESS_EVAL:
+ ralloc_strcat(&buf, " tes");
+ _mesa_sha1_compute(buf, strlen(buf), sha1);
+ write_stream_out_to_cache(st_blob, out_state);
+ write_tgsi_to_cache(st_blob, out_state, st, sha1, num_tokens);
+ break;
+ case MESA_SHADER_TESS_CTRL:
+ ralloc_strcat(&buf, " tcs");
+ _mesa_sha1_compute(buf, strlen(buf), sha1);
+ write_stream_out_to_cache(st_blob, out_state);
+ write_tgsi_to_cache(st_blob, out_state, st, sha1, num_tokens);
+ break;
+ case MESA_SHADER_GEOMETRY:
+ ralloc_strcat(&buf, " gs");
+ _mesa_sha1_compute(buf, strlen(buf), sha1);
+ write_stream_out_to_cache(st_blob, out_state);
+ write_tgsi_to_cache(st_blob, out_state, st, sha1, num_tokens);
+ break;
+ case MESA_SHADER_FRAGMENT: {
+ ralloc_strcat(&buf, " fs");
+ _mesa_sha1_compute(buf, strlen(buf), sha1);
+
+ struct st_fragment_program *stfp = (struct st_fragment_program *) prog;
+ write_tgsi_to_cache(st_blob, &stfp->tgsi, st, sha1, num_tokens);
+ break;
+ }
+ case MESA_SHADER_COMPUTE:
+ ralloc_strcat(&buf, " cs");
+ _mesa_sha1_compute(buf, strlen(buf), sha1);
+ write_tgsi_to_cache(st_blob, out_state, st, sha1, num_tokens);
+ break;
+ default:
+ unreachable("Unsupported stage");
+ }
+
+ if (st->ctx->_Shader->Flags & GLSL_CACHE_INFO) {
+ _mesa_sha1_format(sha1_buf, sha1);
+ fprintf(stderr, "putting %s tgsi_tokens in cache: %s\n",
+ _mesa_shader_stage_to_string(prog->info.stage), sha1_buf);
+ }
+
+ ralloc_free(st_blob);
+ ralloc_free(buf);
+}
/**
* Translate a vertex program.
@@ -442,7 +550,6 @@ st_translate_vertex_program(struct st_context *st,
&stvp->tgsi.stream_output);
free_glsl_to_tgsi_visitor(stvp->glsl_to_tgsi);
- stvp->glsl_to_tgsi = NULL;
} else
error = st_translate_mesa_program(st->ctx,
PIPE_SHADER_VERTEX,
@@ -467,8 +574,15 @@ st_translate_vertex_program(struct st_context *st,
return false;
}
- stvp->tgsi.tokens = ureg_get_tokens(ureg, NULL);
+ unsigned num_tokens;
+ stvp->tgsi.tokens = ureg_get_tokens(ureg, &num_tokens);
ureg_destroy(ureg);
+
+ if (stvp->glsl_to_tgsi) {
+ stvp->glsl_to_tgsi = NULL;
+ cache_tgsi(st, &stvp->Base, NULL, num_tokens);
+ }
+
return stvp->tgsi.tokens != NULL;
}
@@ -890,7 +1004,6 @@ st_translate_fragment_program(struct st_context *st,
fs_output_semantic_index);
free_glsl_to_tgsi_visitor(stfp->glsl_to_tgsi);
- stfp->glsl_to_tgsi = NULL;
} else if (stfp->ati_fs)
st_translate_atifs_program(ureg,
stfp->ati_fs,
@@ -923,8 +1036,15 @@ st_translate_fragment_program(struct st_context *st,
fs_output_semantic_name,
fs_output_semantic_index);
- stfp->tgsi.tokens = ureg_get_tokens(ureg, NULL);
+ unsigned num_tokens;
+ stfp->tgsi.tokens = ureg_get_tokens(ureg, &num_tokens);
ureg_destroy(ureg);
+
+ if (stfp->glsl_to_tgsi) {
+ stfp->glsl_to_tgsi = NULL;
+ cache_tgsi(st, &stfp->Base, NULL, num_tokens);
+ }
+
return stfp->tgsi.tokens != NULL;
}
@@ -1459,13 +1579,16 @@ st_translate_program_common(struct st_context *st,
output_semantic_name,
output_semantic_index);
- out_state->tokens = ureg_get_tokens(ureg, NULL);
+ unsigned num_tokens;
+ out_state->tokens = ureg_get_tokens(ureg, &num_tokens);
ureg_destroy(ureg);
st_translate_stream_output_info(glsl_to_tgsi,
outputMapping,
&out_state->stream_output);
+ cache_tgsi(st, prog, out_state, num_tokens);
+
if ((ST_DEBUG & DEBUG_TGSI) && (ST_DEBUG & DEBUG_MESA)) {
_mesa_print_program(prog);
debug_printf("\n");
--
2.9.3
More information about the mesa-dev
mailing list