pan/bit: Add special unit test
[mesa.git] / src / panfrost / bifrost / cmdline.c
index 1d7d3d4a0a501118678c7844fe4a723cac730d76..095437b89fd4f15440583c2a0456fb43710bbb7b 100644 (file)
@@ -32,8 +32,8 @@
 #include "bifrost_compile.h"
 #include "test/bit.h"
 
-static void
-compile_shader(char **argv)
+static panfrost_program
+compile_shader(char **argv, bool vertex_only)
 {
         struct gl_shader_program *prog;
         nir_shader *nir[2];
@@ -43,8 +43,9 @@ compile_shader(char **argv)
         };
 
         struct standalone_options options = {
-                .glsl_version = 430,
+                .glsl_version = 300, /* ES - needed for precision */
                 .do_link = true,
+                .lower_precision = true
         };
 
         static struct gl_context local_ctx;
@@ -68,11 +69,16 @@ compile_shader(char **argv)
 
                 unsigned product_id = 0x7212; /* Mali G52 */
                 bifrost_compile_shader_nir(nir[i], &compiled, product_id);
+
+                if (vertex_only)
+                        return compiled;
         }
+
+        return compiled;
 }
 
 static void
-disassemble(const char *filename)
+disassemble(const char *filename, bool verbose)
 {
         FILE *fp = fopen(filename, "rb");
         assert(fp);
@@ -88,16 +94,76 @@ disassemble(const char *filename)
         }
         fclose(fp);
 
-        disassemble_bifrost(stdout, code, filesize, false);
+        disassemble_bifrost(stdout, code, filesize, verbose);
         free(code);
 }
 
 static void
-test(void)
+test_vertex(char **argv)
 {
         void *memctx = NULL; /* TODO */
         struct panfrost_device *dev = bit_initialize(memctx);
-        bit_sanity_check(dev);
+
+        float iubo[] = {
+                0.1, 0.2, 0.3, 0.4
+        };
+
+        float iattr[] = {
+                0.5, 0.6, 0.7, 0.8
+        };
+
+        float expected[] = {
+                0.6, 0.8, 1.0, 1.2
+        };
+
+        bit_vertex(dev, compile_shader(argv, true),
+                        (uint32_t *) iubo, sizeof(iubo),
+                        (uint32_t *) iattr, sizeof(iattr),
+                        (uint32_t *) expected, sizeof(expected),
+                        BIT_DEBUG_ALL);
+}
+
+static void
+tests(void)
+{
+        void *memctx = NULL; /* TODO */
+        struct panfrost_device *dev = bit_initialize(memctx);
+        bit_fmod(dev, BIT_DEBUG_FAIL);
+        bit_fma(dev, BIT_DEBUG_FAIL);
+        bit_csel(dev, BIT_DEBUG_FAIL);
+        bit_special(dev, BIT_DEBUG_FAIL);
+}
+
+static void
+run(const char *filename)
+{
+        FILE *fp = fopen(filename, "rb");
+        assert(fp);
+
+        fseek(fp, 0, SEEK_END);
+        unsigned filesize = ftell(fp);
+        rewind(fp);
+
+        unsigned char *code = malloc(filesize);
+        unsigned res = fread(code, 1, filesize, fp);
+        if (res != filesize) {
+                printf("Couldn't read full file\n");
+        }
+        fclose(fp);
+
+        void *memctx = NULL; /* TODO */
+        struct panfrost_device *dev = bit_initialize(memctx);
+
+        panfrost_program prog = {
+                .compiled = {
+                        .data = code,
+                        .size = filesize
+                },
+        };
+
+        bit_vertex(dev, prog, NULL, 0, NULL, 0, NULL, 0, BIT_DEBUG_FAIL);
+
+        free(code);
 }
 
 int
@@ -109,11 +175,17 @@ main(int argc, char **argv)
         }
 
         if (strcmp(argv[1], "compile") == 0)
-                compile_shader(&argv[2]);
+                compile_shader(&argv[2], false);
         else if (strcmp(argv[1], "disasm") == 0)
-                disassemble(argv[2]);
-        else if (strcmp(argv[1], "test") == 0)
-                test();
+                disassemble(argv[2], false);
+        else if (strcmp(argv[1], "disasm-verbose") == 0)
+                disassemble(argv[2], true);
+        else if (strcmp(argv[1], "tests") == 0)
+                tests();
+        else if (strcmp(argv[1], "test-vertex") == 0)
+                test_vertex(&argv[2]);
+        else if (strcmp(argv[1], "run") == 0)
+                run(argv[2]);
         else
                 unreachable("Unknown command. Valid: compile/disasm");