[29/35] util: Add a utility to assemble SPIR-V sources

Submitted by apinheiro on Aug. 9, 2018, 11:36 a.m.

Details

Message ID 20180809113614.5122-30-apinheiro@igalia.com
State New
Headers show
Series "ARB_gl_spirv: all tests so far" ( rev: 1 ) in Piglit

Not browsing as part of any series.

Commit Message

apinheiro Aug. 9, 2018, 11:36 a.m.
From: Neil Roberts <nroberts@igalia.com>

Adds piglit_assemble_spirv which invokes spirv-as to assemble a SPIR-V
source. The function is based on a static helper function that was in
shader_runner.
---
 tests/shaders/shader_runner.c | 45 ++++++-------------------------------------
 tests/util/piglit-shader.c    | 37 +++++++++++++++++++++++++++++++++++
 tests/util/piglit-shader.h    |  3 +++
 3 files changed, 46 insertions(+), 39 deletions(-)

Patch hide | download patch | download mbox

diff --git a/tests/shaders/shader_runner.c b/tests/shaders/shader_runner.c
index d96049687..cc377cb5e 100644
--- a/tests/shaders/shader_runner.c
+++ b/tests/shaders/shader_runner.c
@@ -720,8 +720,8 @@  program_binary_save_restore(bool script_command)
 }
 
 static enum piglit_result
-load_and_specialize_spirv(GLenum target,
-			  const char *binary, unsigned size)
+specialize_spirv(GLenum target,
+		 GLuint shader)
 {
 	if (glsl_in_use) {
 		printf("Cannot mix SPIR-V and non-SPIR-V shaders\n");
@@ -730,11 +730,6 @@  load_and_specialize_spirv(GLenum target,
 
 	spirv_in_use = true;
 
-	GLuint shader = glCreateShader(target);
-
-	glShaderBinary(1, &shader, GL_SHADER_BINARY_FORMAT_SPIR_V_ARB,
-		       binary, size);
-
 	const struct specialization_list *specs;
 
 	switch (target) {
@@ -822,15 +817,6 @@  assemble_spirv(GLenum target)
 		return PIGLIT_SKIP;
 	}
 
-	char *arguments[] = {
-		getenv("PIGLIT_SPIRV_AS_BINARY"),
-		"-o", "-",
-		NULL
-	};
-
-	if (arguments[0] == NULL)
-		arguments[0] = "spirv-as";
-
 	/* Strip comments from the source */
 	char *stripped_source = malloc(shader_string_size);
 	char *p = stripped_source;
@@ -853,32 +839,13 @@  assemble_spirv(GLenum target)
 		}
 	}
 
-	uint8_t *binary_source;
-	size_t binary_source_length;
-	bool res = piglit_subprocess(arguments,
-				     p - stripped_source,
-				     (const uint8_t *)
-				     stripped_source,
-				     &binary_source_length,
-				     &binary_source);
+	GLuint shader = piglit_assemble_spirv(target,
+					      p - stripped_source,
+					      stripped_source);
 
 	free(stripped_source);
 
-	if (!res) {
-		fprintf(stderr, "spirv-as failed\n");
-		return PIGLIT_FAIL;
-	}
-
-	enum piglit_result ret;
-
-	ret = load_and_specialize_spirv(target,
-					(const char *)
-					binary_source,
-					binary_source_length);
-
-	free(binary_source);
-
-	return ret;
+	return specialize_spirv(target, shader);
 }
 
 static enum piglit_result
diff --git a/tests/util/piglit-shader.c b/tests/util/piglit-shader.c
index 7ac5df14d..1938b1576 100644
--- a/tests/util/piglit-shader.c
+++ b/tests/util/piglit-shader.c
@@ -24,6 +24,7 @@ 
 #include <errno.h>
 
 #include "piglit-util-gl.h"
+#include "piglit-subprocess.h"
 
 void piglit_get_glsl_version(bool *es, int* major, int* minor)
 {
@@ -464,6 +465,42 @@  piglit_build_simple_program_multiple_shaders(GLenum target1,
 	return prog;
 }
 
+GLuint
+piglit_assemble_spirv(GLenum target,
+		      size_t source_length,
+		      const char *source)
+{
+	char *arguments[] = {
+		getenv("PIGLIT_SPIRV_AS_BINARY"),
+		"-o", "-",
+		NULL
+	};
+
+	if (arguments[0] == NULL)
+		arguments[0] = "spirv-as";
+
+	uint8_t *binary_source;
+	size_t binary_source_length;
+	bool res = piglit_subprocess(arguments,
+				     source_length,
+				     (const uint8_t *) source,
+				     &binary_source_length,
+				     &binary_source);
+
+	if (!res) {
+		fprintf(stderr, "spirv-as failed\n");
+		piglit_report_result(PIGLIT_FAIL);
+	}
+
+	GLuint shader = glCreateShader(target);
+	glShaderBinary(1, &shader, GL_SHADER_BINARY_FORMAT_SPIR_V_ARB,
+		       binary_source, binary_source_length);
+
+	free(binary_source);
+
+	return shader;
+}
+
 void
 piglit_require_GLSL(void)
 {
diff --git a/tests/util/piglit-shader.h b/tests/util/piglit-shader.h
index 9208451a8..aa29d0994 100644
--- a/tests/util/piglit-shader.h
+++ b/tests/util/piglit-shader.h
@@ -49,6 +49,9 @@  GLint piglit_build_simple_program_unlinked_multiple_shaders(GLenum target1,
 GLint piglit_build_simple_program_multiple_shaders(GLenum target1,
 						  const char *source1,
 						  ...);
+GLuint piglit_assemble_spirv(GLenum target,
+			     size_t source_length,
+			     const char *source);
 
 extern GLboolean piglit_program_pipeline_check_status(GLuint pipeline);
 extern GLboolean piglit_program_pipeline_check_status_quiet(GLuint pipeline);