[Mesa-dev] [PATCH shaderdb 3/3] run: shader program file created via GetProgramBinary (v2)

Dongwon Kim dongwon.kim at intel.com
Wed Feb 21 00:53:03 UTC 2018


extraction of linked binary program to a file using glGetProgramBinary.
This file is intended to be loaded by glProgramBinary in the graphic
application running on the target system.

A new option, '--out=<file name>' is available to be used for specifying
the output file name.

v2: 1. define MAX_LOG_LEN and use it as the size of gl log
    2. define MAX_PROG_SIZE and use it as the max size of extracted
       shader_program
    3. out_file is now pointer allocated by strdup for the file name

Signed-off-by: Dongwon Kim <dongwon.kim at intel.com>
---
 run.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 53 insertions(+), 4 deletions(-)

diff --git a/run.c b/run.c
index d066567..df466eb 100644
--- a/run.c
+++ b/run.c
@@ -52,6 +52,9 @@
 
 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
 
+#define MAX_LOG_LEN 4096
+#define MAX_PROG_SIZE (10*1024*1024) /* maximum 10MB for shader program */
+
 struct context_info {
     char *extension_string;
     int extension_string_len;
@@ -358,18 +361,20 @@ const struct platform platforms[] = {
 enum
 {
     PCI_ID_OVERRIDE_OPTION = CHAR_MAX + 1,
+    OUT_PROGRAM_OPTION,
 };
 
 const struct option const long_options[] =
 {
     {"pciid", required_argument, NULL, PCI_ID_OVERRIDE_OPTION},
+    {"out", required_argument, NULL, OUT_PROGRAM_OPTION},
     {NULL, 0, NULL, 0}
 };
 
 void print_usage(const char *prog_name)
 {
     fprintf(stderr,
-            "Usage: %s [-d <device>] [-j <max_threads>] [-o <driver>] [-p <platform>] [--pciid=<chip id of targetted gen arch>] <directories and *.shader_test files>\n",
+            "Usage: %s [-d <device>] [-j <max_threads>] [-o <driver>] [-p <platform>] [--pciid=<chip id of targetted gen arch>] [--out=<file name of output shader program>] <directories and *.shader_test files>\n",
             prog_name);
 }
 
@@ -450,6 +455,7 @@ main(int argc, char **argv)
     int opt;
     bool platf_overridden = 0;
     bool pci_id_overridden = 0;
+    char *out_file = NULL;
 
     max_threads = omp_get_max_threads();
 
@@ -518,6 +524,14 @@ main(int argc, char **argv)
             setenv("INTEL_DEVID_OVERRIDE", optarg, 1);
             pci_id_overridden = 1;
             break;
+        case OUT_PROGRAM_OPTION:
+            if (optarg[0] == 0) {
+                  fprintf(stderr, "Output file name is empty.\n");
+                  return -1;
+            }
+            out_file = strdup(optarg);
+            assert(out_file != NULL);
+            break;
         default:
             fprintf(stderr, "Unknown option: %x\n", opt);
             print_usage(argv[0]);
@@ -751,6 +765,8 @@ main(int argc, char **argv)
         EGLContext compat_ctx = create_context(egl_dpy, cfg, TYPE_COMPAT);
         if (compat_ctx == EGL_NO_CONTEXT) {
             fprintf(stderr, "ERROR: eglCreateContext() failed\n");
+            if (out_file)
+                free(out_file);
             exit(-1);
         }
 
@@ -858,18 +874,18 @@ main(int argc, char **argv)
                 }
             } else if (type == TYPE_CORE || type == TYPE_COMPAT || type == TYPE_ES) {
                 GLuint prog = glCreateProgram();
+                GLint param;
 
                 for (unsigned i = 0; i < num_shaders; i++) {
                     GLuint s = glCreateShader(shader[i].type);
                     glShaderSource(s, 1, &shader[i].text, &shader[i].length);
                     glCompileShader(s);
 
-                    GLint param;
                     glGetShaderiv(s, GL_COMPILE_STATUS, &param);
                     if (unlikely(!param)) {
-                        GLchar log[4096];
+                        GLchar log[MAX_LOG_LEN];
                         GLsizei length;
-                        glGetShaderInfoLog(s, 4096, &length, log);
+                        glGetShaderInfoLog(s, sizeof(log), &length, log);
 
                         fprintf(stderr, "ERROR: %s failed to compile:\n%s\n",
                                 current_shader_name, log);
@@ -879,6 +895,36 @@ main(int argc, char **argv)
                 }
 
                 glLinkProgram(prog);
+
+                glGetProgramiv(prog, GL_LINK_STATUS, &param);
+                if (unlikely(!param)) {
+                   GLchar log[MAX_LOG_LEN];
+                   GLsizei length;
+                   glGetProgramInfoLog(prog, sizeof(log), &length, log);
+
+                   fprintf(stderr, "ERROR: failed to link progam:\n%s\n",
+                           log);
+                } else {
+                   char *prog_buf = (char *)malloc(MAX_PROG_SIZE);
+                   GLenum format;
+                   GLsizei length;
+                   FILE *fp;
+
+                   glGetProgramBinary(prog, MAX_PROG_SIZE, &length, &format, prog_buf);
+
+                   param = glGetError();
+                   if (param != GL_NO_ERROR) {
+                      fprintf(stderr, "ERROR: failed to get Program Binary\n");
+                   } else {
+                      fp = fopen(out_file, "wb");
+                      fprintf(stdout, "Binary program is generated (%d Byte).\n", length);
+                      fprintf(stdout, "Binary Format is %d\n", format);
+                      fprintf(stdout, "Now writing to the file\n");
+                      fwrite(prog_buf, sizeof(char), length, fp);
+                      fclose(fp);
+                   }
+                   free(prog_buf);
+                }
                 glDeleteProgram(prog);
             } else {
                 for (unsigned i = 0; i < num_shaders; i++) {
@@ -932,5 +978,8 @@ main(int argc, char **argv)
  close_fd:
     close(fd);
 
+    if (out_file)
+        free(out_file);
+
     return ret;
 }
-- 
2.16.1



More information about the mesa-dev mailing list