Home | History | Annotate | Download | only in spirv
      1 /*
      2  * Copyright  2015 Intel Corporation
      3  *
      4  * Permission is hereby granted, free of charge, to any person obtaining a
      5  * copy of this software and associated documentation files (the "Software"),
      6  * to deal in the Software without restriction, including without limitation
      7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
      8  * and/or sell copies of the Software, and to permit persons to whom the
      9  * Software is furnished to do so, subject to the following conditions:
     10  *
     11  * The above copyright notice and this permission notice (including the next
     12  * paragraph) shall be included in all copies or substantial portions of the
     13  * Software.
     14  *
     15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
     21  * IN THE SOFTWARE.
     22  *
     23  * Authors:
     24  *    Jason Ekstrand (jason (at) jlekstrand.net)
     25  *
     26  */
     27 
     28 /*
     29  * A simple executable that opens a SPIR-V shader, converts it to NIR, and
     30  * dumps out the result.  This should be useful for testing the
     31  * spirv_to_nir code.
     32  */
     33 
     34 #include "spirv/nir_spirv.h"
     35 
     36 #include <sys/mman.h>
     37 #include <sys/types.h>
     38 #include <fcntl.h>
     39 #include <unistd.h>
     40 #include <stdio.h>
     41 #include <errno.h>
     42 #include <string.h>
     43 
     44 #define WORD_SIZE 4
     45 
     46 int main(int argc, char **argv)
     47 {
     48    int fd = open(argv[1], O_RDONLY);
     49    if (fd < 0)
     50    {
     51       fprintf(stderr, "Failed to open %s\n", argv[1]);
     52       return 1;
     53    }
     54 
     55    off_t len = lseek(fd, 0, SEEK_END);
     56    if (len % WORD_SIZE != 0)
     57    {
     58       fprintf(stderr, "File length isn't a multiple of the word size\n");
     59       fprintf(stderr, "Are you sure this is a valid SPIR-V shader?\n");
     60       close(fd);
     61       return 1;
     62    }
     63 
     64    size_t word_count = len / WORD_SIZE;
     65 
     66    const void *map = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
     67    if (map == MAP_FAILED)
     68    {
     69       fprintf(stderr, "Failed to mmap the file: errno=%d, %s\n",
     70               errno, strerror(errno));
     71       close(fd);
     72       return 1;
     73    }
     74 
     75    nir_function *func = spirv_to_nir(map, word_count, NULL, 0,
     76                                      MESA_SHADER_FRAGMENT, "main", NULL, NULL);
     77    nir_print_shader(func->shader, stderr);
     78 
     79    return 0;
     80 }
     81