[Piglit] [PATCH] crucible: Convert from glslc to glslang

Jordan Justen jordan.l.justen at intel.com
Fri Apr 14 17:47:52 UTC 2017


This essentially reverts b5a52577781ff191766f49866ce00af749cb86b7, but
it seems that glslangValidator has since become a little more
friendly. Additionally, I used python tempfile module to simplify
creating tempfiles with the required extensions.

One reason to revert to using glslangValidator is just to remove the
additional shaderc/glslc dependency. An advantage of using
glslangValidator is that the SPIR-V binary and assembly code can be
generated with a single invocation of glslangValidator.

Cc: Jason Ekstrand <jason at jlekstrand.net>
Signed-off-by: Jordan Justen <jordan.l.justen at intel.com>
---
 INSTALL              |  2 +-
 Makefile.am          |  2 +-
 configure.ac         | 10 +++++-----
 misc/glsl_scraper.py | 48 +++++++++++++++++++++++++++++-------------------
 4 files changed, 36 insertions(+), 26 deletions(-)

diff --git a/INSTALL b/INSTALL
index 17a1a68..fad9989 100644
--- a/INSTALL
+++ b/INSTALL
@@ -3,7 +3,7 @@ How to Build
 
 External Dependencies
 ---------------------
-- glslc, from Google's shaderc project (http://github.com/google/shaderc)
+- glslang, from Khronos's glslang project (https://github.com/KhronosGroup/glslang)
 - AsciiDoc (http://asciidoc.org)
 - libxml2 (http://www.xmlsoft.org)
 - Python3
diff --git a/Makefile.am b/Makefile.am
index 4b4050c..49790de 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -136,7 +136,7 @@ bin_crucible_LDADD = $(MESA_LDFLAGS) -lm -lvulkan -lpthread $(libpng_LIBS) \
 		     $(LIBXML2_LIBS)
 
 %-spirv.h: %.c misc/glsl_scraper.py
-	$(AM_V_GEN) $(PYTHON3) $(srcdir)/misc/glsl_scraper.py --with-glslc=$(GLSLC) -o $@ $<
+	$(AM_V_GEN) $(PYTHON3) $(srcdir)/misc/glsl_scraper.py --with-glslang=$(GLSLANG) -o $@ $<
 
 %_gen.c: %_gen.py
 	$(AM_V_GEN) $(PYTHON3) $<
diff --git a/configure.ac b/configure.ac
index 9e137e7..b300674 100644
--- a/configure.ac
+++ b/configure.ac
@@ -48,11 +48,11 @@ AC_CHECK_PROGS([PYTHON2], [python2 python])
 AC_CHECK_PROGS([PYTHON3], [python3])
 AX_CHECK_PYTHON2_MODULE_OPENCV()
 
-AC_ARG_VAR([GLSLC], [path to glslc executable])
-AC_SUBST([GLSLC])
-AC_CHECK_PROGS([GLSLC], [glslc])
-if test "x$GLSLC" = "x"; then
-    AC_MSG_ERROR([failed to find glslc])
+AC_ARG_VAR([GLSLANG], [path to glslangValidator executable])
+AC_SUBST([GLSLANG])
+AC_CHECK_PROGS([GLSLANG], [glslangValidator])
+if test "x$GLSLANG" = "x"; then
+    AC_MSG_ERROR([failed to find glslangValidator])
 fi
 
 PKG_CHECK_MODULES([libpng16], [libpng16], [HAVE_LIBPNG16=1], [HAVE_LIBPNG16=0])
diff --git a/misc/glsl_scraper.py b/misc/glsl_scraper.py
index 4534555..df42b2e 100644
--- a/misc/glsl_scraper.py
+++ b/misc/glsl_scraper.py
@@ -30,31 +30,38 @@ class Shader:
     def glsl_source(self):
         return dedent(self.stream.getvalue())
 
-    def __run_glslc(self, extra_args=[]):
-        stage_flag = '-fshader-stage='
+    def __run_glslang(self, extra_args=[]):
         if self.stage == 'VERTEX':
-            stage_flag += 'vertex'
+            stage = 'vert'
         elif self.stage == 'TESS_CONTROL':
-            stage_flag += 'tesscontrol'
+            stage = 'tesc'
         elif self.stage == 'TESS_EVALUATION':
-            stage_flag += 'tesseval'
+            stage = 'tese'
         elif self.stage == 'GEOMETRY':
-            stage_flag += 'geometry'
+            stage = 'geom'
         elif self.stage == 'FRAGMENT':
-            stage_flag += 'fragment'
+            stage = 'frag'
         elif self.stage == 'COMPUTE':
-            stage_flag += 'compute'
+            stage = 'comp'
         else:
             assert False
 
-        with subprocess.Popen([glslc] + extra_args +
-                              [stage_flag, '-std=430core', '-o', '-', '-'],
+        stage_flags = ['-S', stage]
+
+        in_file = tempfile.NamedTemporaryFile(suffix='.'+stage)
+        src = ('#version 430\n' + self.glsl_source()).encode('utf-8')
+        in_file.write(src)
+        in_file.flush()
+        out_file = tempfile.NamedTemporaryFile(suffix='.spirv')
+        args = [glslang, '-H'] + extra_args + stage_flags
+        args += ['-o', out_file.name, in_file.name]
+        with subprocess.Popen(args,
                               stdout = subprocess.PIPE,
                               stderr = subprocess.PIPE,
                               stdin = subprocess.PIPE) as proc:
 
-            proc.stdin.write(self.glsl_source().encode('utf-8'))
             out, err = proc.communicate(timeout=30)
+            in_file.close()
 
             if proc.returncode != 0:
                 # Unfortunately, glslang dumps errors to standard out.
@@ -63,7 +70,10 @@ class Shader:
                 message = out.decode('utf-8') + '\n' + err.decode('utf-8')
                 raise ShaderCompileError(message.strip())
 
-            return out
+            out_file.seek(0)
+            spirv = out_file.read()
+            out_file.close()
+            return (spirv, out)
 
     def compile(self):
         def dwords(f):
@@ -74,9 +84,9 @@ class Shader:
                 assert len(dword_str) == 4
                 yield struct.unpack('I', dword_str)[0]
 
-        spirv = self.__run_glslc()
+        (spirv, assembly) = self.__run_glslang()
         self.dwords = list(dwords(io.BytesIO(spirv)))
-        self.assembly = str(self.__run_glslc(['-S']), 'utf-8')
+        self.assembly = str(assembly, 'utf-8')
 
     def _dump_glsl_code(self, f, var_name):
         # First dump the GLSL source as strings
@@ -234,10 +244,10 @@ def parse_args():
             formatter_class=argparse.RawDescriptionHelpFormatter)
     p.add_argument('-o', '--outfile', default='-',
                         help='Output to the given file (default: stdout).')
-    p.add_argument('--with-glslc', metavar='PATH',
-                        default='glslc',
-                        dest='glslc',
-                        help='Full path to the glslc shader compiler.')
+    p.add_argument('--with-glslang', metavar='PATH',
+                        default='glslangValidator',
+                        dest='glslang',
+                        help='Full path to the glslangValidator shader compiler.')
     p.add_argument('--glsl-only', action='store_true')
     p.add_argument('infile', metavar='INFILE')
 
@@ -247,7 +257,7 @@ def parse_args():
 args = parse_args()
 infname = args.infile
 outfname = args.outfile
-glslc = args.glslc
+glslang = args.glslang
 glsl_only = args.glsl_only
 
 with open_file(infname, 'r') as infile:
-- 
2.11.0



More information about the Piglit mailing list