1/*
2 * Copyright © Microsoft 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
24#ifndef SPIRV_TO_DXIL_H
25#define SPIRV_TO_DXIL_H
26
27#include <stdbool.h>
28#include <stddef.h>
29#include <stdint.h>
30
31#ifdef __cplusplus
32extern "C" {
33#endif
34
35// NB: I've copy and pasted some types into this header so we don't have to
36// include other headers. This will surely break if any of these types change.
37
38// Copy of gl_shader_stage
39typedef enum {
40   DXIL_SPIRV_SHADER_NONE = -1,
41   DXIL_SPIRV_SHADER_VERTEX = 0,
42   DXIL_SPIRV_SHADER_TESS_CTRL = 1,
43   DXIL_SPIRV_SHADER_TESS_EVAL = 2,
44   DXIL_SPIRV_SHADER_GEOMETRY = 3,
45   DXIL_SPIRV_SHADER_FRAGMENT = 4,
46   DXIL_SPIRV_SHADER_COMPUTE = 5,
47   DXIL_SPIRV_SHADER_KERNEL = 6,
48} dxil_spirv_shader_stage;
49
50// Copy of nir_spirv_const_value
51typedef union {
52   bool b;
53   float f32;
54   double f64;
55   int8_t i8;
56   uint8_t u8;
57   int16_t i16;
58   uint16_t u16;
59   int32_t i32;
60   uint32_t u32;
61   int64_t i64;
62   uint64_t u64;
63} dxil_spirv_const_value;
64
65// Copy of nir_spirv_specialization
66struct dxil_spirv_specialization {
67   uint32_t id;
68   dxil_spirv_const_value value;
69   bool defined_on_module;
70};
71
72struct dxil_spirv_metadata {
73   bool requires_runtime_data;
74};
75
76struct dxil_spirv_object {
77   struct dxil_spirv_metadata metadata;
78   struct {
79      void *buffer;
80      size_t size;
81   } binary;
82};
83
84/* This struct describes the layout of data expected in the CB bound to
85 * runtime_data_cbv during compute shader execution */
86struct dxil_spirv_compute_runtime_data {
87   /* Total number of groups dispatched (i.e. value passed to Dispatch()) */
88   uint32_t group_count_x;
89   uint32_t group_count_y;
90   uint32_t group_count_z;
91};
92
93/* This struct describes the layout of data expected in the CB bound to
94 * runtime_data_cbv during vertex stages */
95struct dxil_spirv_vertex_runtime_data {
96   uint32_t first_vertex;
97   uint32_t base_instance;
98   bool is_indexed_draw;
99};
100
101struct dxil_spirv_runtime_conf {
102   struct {
103      uint32_t register_space;
104      uint32_t base_shader_register;
105   } runtime_data_cbv;
106
107   // Set true if vertex and instance ids have already been converted to
108   // zero-based. Otherwise, runtime_data will be required to lower them.
109   bool zero_based_vertex_instance_id;
110};
111
112/**
113 * Compile a SPIR-V module into DXIL.
114 * \param  words  SPIR-V module to compile
115 * \param  word_count  number of words in the SPIR-V module
116 * \param  specializations  specialization constants to compile with the shader
117 * \param  num_specializations  number of specialization constants
118 * \param  stage  shader stage
119 * \param  entry_point_name  name of shader entrypoint
120 * \param  conf  configuration for spriv_to_dxil
121 * \param  out_dxil  will contain the DXIL bytes on success (call spirv_to_dxil_free after use)
122 * \return  true if compilation succeeded
123 */
124bool
125spirv_to_dxil(const uint32_t *words, size_t word_count,
126              struct dxil_spirv_specialization *specializations,
127              unsigned int num_specializations, dxil_spirv_shader_stage stage,
128              const char *entry_point_name,
129              const struct dxil_spirv_runtime_conf *conf,
130              struct dxil_spirv_object *out_dxil);
131
132/**
133 * Free the buffer allocated by spirv_to_dxil.
134 */
135void
136spirv_to_dxil_free(struct dxil_spirv_object *dxil);
137
138uint64_t
139spirv_to_dxil_get_version(void);
140
141#ifdef __cplusplus
142}
143#endif
144
145#endif
146