<div dir="ltr">On Fri, Aug 21, 2015 at 2:17 AM, Tapani Pälli <span dir="ltr"><<a href="mailto:tapani.palli@intel.com" target="_blank">tapani.palli@intel.com</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Patch adds shader source and replace functionality in to the compiler.<br></blockquote><div><br></div><div>I had some very primitive support for this sort of thing already. See SHADER_SUBST in shaderapi.c However, feel free to replace it with something better/nicer.<br></div><div><br></div><div>Also, please document this feature in docs/shading.html<br><br></div><div> <br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
This can be used to debug individual (failing) shaders and measure<br>
performance impact of each shader.<br>
<br>
Functionality is controlled via 2 environment variables:<br>
<br>
MESA_SHADER_DUMP - path where shader sources are dumped<br>
MESA_SHADER_READ - path where replacement shaders are read<br></blockquote><div><br></div><div>Let's put _PATH on the end of both of those to better describe the vars.<br><br> <br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
Signed-off-by: Tapani Pälli <<a href="mailto:tapani.palli@intel.com">tapani.palli@intel.com</a>><br>
Suggested-by: Eero Tamminen <<a href="mailto:eero.t.tamminen@intel.com">eero.t.tamminen@intel.com</a>><br>
---<br>
src/glsl/glsl_parser_extras.cpp | 76 +++++++++++++++++++++++++++++++++++++++++<br>
src/mesa/main/mtypes.h | 2 +-<br>
2 files changed, 77 insertions(+), 1 deletion(-)<br>
<br>
diff --git a/src/glsl/glsl_parser_extras.cpp b/src/glsl/glsl_parser_extras.cpp<br>
index 46896d7..17b79b0 100644<br>
--- a/src/glsl/glsl_parser_extras.cpp<br>
+++ b/src/glsl/glsl_parser_extras.cpp<br></blockquote><div><br></div><div>Are you sure this is the right file for this new code? Maybe it should go into a new source file. glsl_parser_extras.cpp already seems to have become a dumping ground for a lot of unrelated things.<br><br> <br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
@@ -1570,10 +1570,86 @@ set_shader_inout_layout(struct gl_shader *shader,<br>
<br>
extern "C" {<br>
<br>
+#include <sys/stat.h><br>
+#include "util/mesa-sha1.h"<br>
+<br>
+static void<br>
+generate_sha1(struct gl_shader *shader, char sha_str[64])<br>
</blockquote><div><br></div><div>const-qualify shader?<br><br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">+{<br>
+ unsigned char sha[20];<br>
+ _mesa_sha1_compute(shader->Source, strlen(shader->Source), sha);<br>
+ _mesa_sha1_format(sha_str, sha);<br>
+}<br>
+<br>
+static void<br>
+construct_name(struct gl_shader *shader, const char *path,<br></blockquote><div><br></div><div>const qualify shader?<br></div><div><br></div><div>Please add a comment to the function explaining what it does.<br><br> <br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+ char *name, unsigned length)<br>
+{<br>
+ char sha[64];<br>
+ static const char *types[] = {<br>
+ "VS", "TC", "TE", "GS", "FS", "CS",<br>
+ };<br>
+<br>
+ generate_sha1(shader, sha);<br>
+ _mesa_snprintf(name, length, "%s/%s_%s.glsl", path, types[shader->Stage],<br>
+ sha);<br>
+}<br>
+<br>
+static void<br>
+read_shader(struct gl_shader *shader)<br></blockquote><div><br></div><div>Please put a comment on the function explaining what it does.<br><br> <br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+{<br>
+ char name[PATH_MAX];<br>
+ static char *read_path = getenv("MESA_SHADER_READ");<br>
+<br>
+ if (!read_path)<br>
+ return;<br>
+<br>
+ construct_name(shader, read_path, name, PATH_MAX);<br>
+<br>
+ FILE *in = fopen(name, "r");<br>
+ if (in) {<br>
+ fseek(in, 0, SEEK_END);<br>
+ long size = ftell(in);<br>
+ rewind(in);<br>
+ char *source = (char *) malloc(size + 1);<br></blockquote><div><br></div><div>Should probably check for source==NULL here. Otherwise, tools like coverity will probably complain.<br><br> <br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+ fread(source, size, 1, in);<br>
+ source[size] = '\0';<br>
+ free(shader->Source);<br>
+ shader->Source = source;<br>
+ fclose(in);<br>
+ }<br>
+}<br>
+<br>
+static void<br>
+dump_shader(struct gl_shader *shader)<br></blockquote><div><br></div><div>const-qualify shader?<br><br> <br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+{<br>
+ char name[PATH_MAX];<br>
+ static char *dump_path = getenv("MESA_SHADER_DUMP");<br>
+<br>
+ if (!dump_path)<br>
+ return;<br>
+<br>
+ construct_name(shader, dump_path, name, PATH_MAX);<br>
+<br>
+ FILE *out = fopen(name, "w");<br>
+ if (out) {<br>
+ fprintf(out, "%s", shader->Source);<br>
</blockquote><div><br></div><div>fputs() instead?<br><br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">+ fclose(out);<br>
+ } else {<br>
+ fprintf(stderr, "could not open %s for dumping shader\n", name);<br></blockquote><div><br></div><div>Or, _mesa_warning()?<br><br> <br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+ }<br>
+}<br>
+<br>
void<br>
_mesa_glsl_compile_shader(struct gl_context *ctx, struct gl_shader *shader,<br>
bool dump_ast, bool dump_hir)<br>
{<br>
+ /* Dump original shader source to MESA_SHADER_DUMP and replace<br>
+ * if corresponding entry found from MESA_SHADER_READ path.<br>
+ */<br>
+ dump_shader(shader);<br>
+ read_shader(shader);<br>
+<br>
struct _mesa_glsl_parse_state *state =<br>
new(shader) _mesa_glsl_parse_state(ctx, shader->Stage, shader);<br>
const char *source = shader->Source;<br>
diff --git a/src/mesa/main/mtypes.h b/src/mesa/main/mtypes.h<br>
index f61a245..fb47a22 100644<br>
--- a/src/mesa/main/mtypes.h<br>
+++ b/src/mesa/main/mtypes.h<br>
@@ -2346,7 +2346,7 @@ struct gl_shader<br>
bool IsES; /**< True if this shader uses GLSL ES */<br>
<br>
GLuint SourceChecksum; /**< for debug/logging purposes */<br>
- const GLchar *Source; /**< Source code string */<br>
+ GLchar *Source; /**< Source code string */<br></blockquote><div><br></div><div>Not sure why that change is needed.<br><br></div><div>-Brian<br> <br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
struct gl_program *Program; /**< Post-compile assembly code */<br>
GLchar *InfoLog;<br>
<span class="HOEnZb"><font color="#888888">--<br>
2.4.3<br>
<br>
_______________________________________________<br>
mesa-dev mailing list<br>
<a href="mailto:mesa-dev@lists.freedesktop.org">mesa-dev@lists.freedesktop.org</a><br>
<a href="http://lists.freedesktop.org/mailman/listinfo/mesa-dev" rel="noreferrer" target="_blank">http://lists.freedesktop.org/mailman/listinfo/mesa-dev</a><br>
</font></span></blockquote></div><br></div></div>