Mesa (main): mesa: add shader source SHA1s that are propagated up to glCompileShader

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Fri Nov 26 13:07:22 UTC 2021


Module: Mesa
Branch: main
Commit: 436444967793ec16c74c8773dd1979f67c7bb94d
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=436444967793ec16c74c8773dd1979f67c7bb94d

Author: Marek Olšák <marek.olsak at amd.com>
Date:   Thu Nov 18 08:44:29 2021 -0500

mesa: add shader source SHA1s that are propagated up to glCompileShader

glCompileShader can use two different sources, so we need 2 different SHA1s
there. Successful compilation sets compiled_source_sha1.

Reviewed-by: Pierre-Eric Pelloux-Prayer <pierre-eric.pelloux-prayer at amd.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/13869>

---

 src/compiler/glsl/glsl_parser_extras.cpp | 44 ++++++++++++++++++++++++--------
 src/mesa/main/mtypes.h                   |  6 +++++
 src/mesa/main/shaderapi.c                | 11 ++++++--
 3 files changed, 49 insertions(+), 12 deletions(-)

diff --git a/src/compiler/glsl/glsl_parser_extras.cpp b/src/compiler/glsl/glsl_parser_extras.cpp
index 5a92cd716f1..85bb06125c2 100644
--- a/src/compiler/glsl/glsl_parser_extras.cpp
+++ b/src/compiler/glsl/glsl_parser_extras.cpp
@@ -2146,8 +2146,9 @@ opt_shader_and_create_symbol_table(struct gl_context *ctx,
 
 static bool
 can_skip_compile(struct gl_context *ctx, struct gl_shader *shader,
-                 const char *source, bool force_recompile,
-                 bool source_has_shader_include)
+                 const char *source,
+                 const uint8_t source_sha1[SHA1_DIGEST_LENGTH],
+                 bool force_recompile, bool source_has_shader_include)
 {
    if (!force_recompile) {
       if (ctx->Cache) {
@@ -2168,8 +2169,15 @@ can_skip_compile(struct gl_context *ctx, struct gl_shader *shader,
              * we have no guarantee the shader include source tree has not
              * changed.
              */
-            shader->FallbackSource = source_has_shader_include ?
-               strdup(source) : NULL;
+            if (source_has_shader_include) {
+               shader->FallbackSource = strdup(source);
+               memcpy(shader->fallback_source_sha1, source_sha1,
+                      SHA1_DIGEST_LENGTH);
+            } else {
+               shader->FallbackSource = NULL;
+            }
+            memcpy(shader->compiled_source_sha1, source_sha1,
+                   SHA1_DIGEST_LENGTH);
             return true;
          }
       }
@@ -2189,8 +2197,16 @@ void
 _mesa_glsl_compile_shader(struct gl_context *ctx, struct gl_shader *shader,
                           bool dump_ast, bool dump_hir, bool force_recompile)
 {
-   const char *source = force_recompile && shader->FallbackSource ?
-      shader->FallbackSource : shader->Source;
+   const char *source;
+   const uint8_t *source_sha1;
+
+   if (force_recompile && shader->FallbackSource) {
+      source = shader->FallbackSource;
+      source_sha1 = shader->fallback_source_sha1;
+   } else {
+      source = shader->Source;
+      source_sha1 = shader->source_sha1;
+   }
 
    /* Note this will be true for shaders the have #include inside comments
     * however that should be rare enough not to worry about.
@@ -2204,7 +2220,8 @@ _mesa_glsl_compile_shader(struct gl_context *ctx, struct gl_shader *shader,
     * keep duplicate copies of the shader include source tree and paths.
     */
    if (!source_has_shader_include &&
-       can_skip_compile(ctx, shader, source, force_recompile, false))
+       can_skip_compile(ctx, shader, source, source_sha1, force_recompile,
+                        false))
       return;
 
     struct _mesa_glsl_parse_state *state =
@@ -2224,7 +2241,8 @@ _mesa_glsl_compile_shader(struct gl_context *ctx, struct gl_shader *shader,
     * include.
     */
    if (source_has_shader_include &&
-       can_skip_compile(ctx, shader, source, force_recompile, true))
+       can_skip_compile(ctx, shader, source, source_sha1, force_recompile,
+                        true))
       return;
 
    if (!state->error) {
@@ -2286,14 +2304,20 @@ _mesa_glsl_compile_shader(struct gl_context *ctx, struct gl_shader *shader,
       /* Copy pre-processed shader include to fallback source otherwise we
        * have no guarantee the shader include source tree has not changed.
        */
-      shader->FallbackSource = source_has_shader_include ?
-         strdup(source) : NULL;
+      if (source_has_shader_include) {
+         shader->FallbackSource = strdup(source);
+         memcpy(shader->fallback_source_sha1, source_sha1, SHA1_DIGEST_LENGTH);
+      } else {
+         shader->FallbackSource = NULL;
+      }
    }
 
    delete state->symbols;
    ralloc_free(state);
 
    if (ctx->Cache && shader->CompileStatus == COMPILE_SUCCESS) {
+      memcpy(shader->compiled_source_sha1, source_sha1, SHA1_DIGEST_LENGTH);
+
       char sha1_buf[41];
       disk_cache_put_key(ctx->Cache, shader->disk_cache_sha1);
       if (ctx->_Shader->Flags & GLSL_CACHE_INFO) {
diff --git a/src/mesa/main/mtypes.h b/src/mesa/main/mtypes.h
index e9d0fc37a62..20719c757b2 100644
--- a/src/mesa/main/mtypes.h
+++ b/src/mesa/main/mtypes.h
@@ -2684,6 +2684,12 @@ struct gl_shader
 
    /** SHA1 of the pre-processed source used by the disk cache. */
    uint8_t disk_cache_sha1[SHA1_DIGEST_LENGTH];
+   /** SHA1 of the original source before replacement, set by glShaderSource. */
+   uint8_t source_sha1[SHA1_DIGEST_LENGTH];
+   /** SHA1 of FallbackSource (a copy of some original source before replacement). */
+   uint8_t fallback_source_sha1[SHA1_DIGEST_LENGTH];
+   /** SHA1 of the current compiled source, set by successful glCompileShader. */
+   uint8_t compiled_source_sha1[SHA1_DIGEST_LENGTH];
 
    const GLchar *Source;  /**< Source code string */
    const GLchar *FallbackSource;  /**< Fallback string used by on-disk cache*/
diff --git a/src/mesa/main/shaderapi.c b/src/mesa/main/shaderapi.c
index 52cde5d1962..9200f0a225a 100644
--- a/src/mesa/main/shaderapi.c
+++ b/src/mesa/main/shaderapi.c
@@ -1181,7 +1181,8 @@ get_shader_source(struct gl_context *ctx, GLuint shader, GLsizei maxLength,
  * glShaderSource[ARB].
  */
 static void
-set_shader_source(struct gl_shader *sh, const GLchar *source)
+set_shader_source(struct gl_shader *sh, const GLchar *source,
+                  const uint8_t original_sha1[SHA1_DIGEST_LENGTH])
 {
    assert(sh);
 
@@ -1200,6 +1201,7 @@ set_shader_source(struct gl_shader *sh, const GLchar *source)
        * fallback.
        */
       sh->FallbackSource = sh->Source;
+      memcpy(sh->fallback_source_sha1, sh->source_sha1, SHA1_DIGEST_LENGTH);
       sh->Source = source;
    } else {
       /* free old shader source string and install new one */
@@ -1207,6 +1209,7 @@ set_shader_source(struct gl_shader *sh, const GLchar *source)
       sh->Source = source;
    }
 
+   memcpy(sh->source_sha1, original_sha1, SHA1_DIGEST_LENGTH);
 }
 
 static void
@@ -2178,6 +2181,10 @@ shader_source(struct gl_context *ctx, GLuint shaderObj, GLsizei count,
    source[totalLength - 1] = '\0';
    source[totalLength - 2] = '\0';
 
+   /* Compute the original source sha1 before shader replacement. */
+   uint8_t original_sha1[SHA1_DIGEST_LENGTH];
+   _mesa_sha1_compute(source, strlen(source), original_sha1);
+
 #ifdef ENABLE_SHADER_CACHE
    GLcharARB *replacement;
 
@@ -2193,7 +2200,7 @@ shader_source(struct gl_context *ctx, GLuint shaderObj, GLsizei count,
    }
 #endif /* ENABLE_SHADER_CACHE */
 
-   set_shader_source(sh, source);
+   set_shader_source(sh, source, original_sha1);
 
    free(offsets);
 }



More information about the mesa-commit mailing list