Path: blob/21.2-virgl/src/microsoft/spirv_to_dxil/spirv2dxil.c
4564 views
/*1* Copyright © 2015 Intel Corporation2* Copyright © Microsoft Corporation3*4* Permission is hereby granted, free of charge, to any person obtaining a5* copy of this software and associated documentation files (the "Software"),6* to deal in the Software without restriction, including without limitation7* the rights to use, copy, modify, merge, publish, distribute, sublicense,8* and/or sell copies of the Software, and to permit persons to whom the9* Software is furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice (including the next12* paragraph) shall be included in all copies or substantial portions of the13* Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL18* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER19* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING20* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS21* IN THE SOFTWARE.22*/2324/*25* A simple executable that opens a SPIR-V shader, converts it to DXIL via26* NIR, and dumps out the result. This should be useful for testing the27* nir_to_dxil code. Based on spirv2nir.c.28*/2930#include "nir_to_dxil.h"31#include "spirv/nir_spirv.h"32#include "spirv_to_dxil.h"3334#include "util/os_file.h"35#include <errno.h>36#include <getopt.h>37#include <stdio.h>38#include <string.h>3940#define WORD_SIZE 44142static gl_shader_stage43stage_to_enum(char *stage)44{45if (!strcmp(stage, "vertex"))46return MESA_SHADER_VERTEX;47else if (!strcmp(stage, "tess-ctrl"))48return MESA_SHADER_TESS_CTRL;49else if (!strcmp(stage, "tess-eval"))50return MESA_SHADER_TESS_EVAL;51else if (!strcmp(stage, "geometry"))52return MESA_SHADER_GEOMETRY;53else if (!strcmp(stage, "fragment"))54return MESA_SHADER_FRAGMENT;55else if (!strcmp(stage, "compute"))56return MESA_SHADER_COMPUTE;57else if (!strcmp(stage, "kernel"))58return MESA_SHADER_KERNEL;59else60return MESA_SHADER_NONE;61}6263int64main(int argc, char **argv)65{66gl_shader_stage shader_stage = MESA_SHADER_FRAGMENT;67char *entry_point = "main";68char *output_file = "";69int ch;7071static struct option long_options[] = {72{"stage", required_argument, 0, 's'},73{"entry", required_argument, 0, 'e'},74{"output", required_argument, 0, 'o'},75{0, 0, 0, 0}};7677while ((ch = getopt_long(argc, argv, "s:e:o:", long_options, NULL)) !=78-1) {79switch (ch) {80case 's':81shader_stage = stage_to_enum(optarg);82if (shader_stage == MESA_SHADER_NONE) {83fprintf(stderr, "Unknown stage %s\n", optarg);84return 1;85}86break;87case 'e':88entry_point = optarg;89break;90case 'o':91output_file = optarg;92break;93default:94fprintf(stderr, "Unrecognized option.\n");95return 1;96}97}9899if (optind != argc - 1) {100if (optind < argc)101fprintf(stderr, "Please specify only one input file.");102else103fprintf(stderr, "Please specify an input file.");104return 1;105}106107const char *filename = argv[optind];108size_t file_size;109char *file_contents = os_read_file(filename, &file_size);110if (!file_contents) {111fprintf(stderr, "Failed to open %s\n", filename);112return 1;113}114115if (file_size % WORD_SIZE != 0) {116fprintf(stderr, "%s size == %zu is not a multiple of %d\n", filename,117file_size, WORD_SIZE);118return 1;119}120121size_t word_count = file_size / WORD_SIZE;122123void *data;124size_t size;125if (spirv_to_dxil((uint32_t *)file_contents, word_count, NULL, 0,126(dxil_spirv_shader_stage)shader_stage, entry_point,127&data, &size)) {128FILE *file = fopen(output_file, "wb");129if (!file) {130fprintf(stderr, "Failed to open %s, %s\n", output_file,131strerror(errno));132spirv_to_dxil_free(data);133free(file_contents);134return 1;135}136137fwrite(data, sizeof(char), size, file);138fclose(file);139spirv_to_dxil_free(data);140} else {141fprintf(stderr, "Compilation failed\n");142return 1;143}144145free(file_contents);146147return 0;148}149150151