Merge pull request #181 from antiagainst/readme
[platform/upstream/SPIRV-Tools.git] / README.md
1 # SPIR-V Tools
2
3 [![Build Status](https://travis-ci.org/KhronosGroup/SPIRV-Tools.svg?branch=master)](https://travis-ci.org/KhronosGroup/SPIRV-Tools)
4
5 ## Overview
6
7 The SPIR-V Tools project provides an API and commands for processing SPIR-V
8 modules.
9
10 The project includes an assembler, binary module parser, disassembler, and
11 validator for SPIR-V, all based on a common static library. The library contains
12 all of the implementation details, and is used in the standalone tools whilst
13 also enabling integration into other code bases directly.
14
15 The interfaces are still under development, and are expected to change.
16
17 SPIR-V is defined by the Khronos Group Inc.
18 See the [SPIR-V Registry][spirv-registry] for the SPIR-V specification,
19 headers, and XML registry.
20
21 ## Supported features
22
23 ### Assembler, binary parser, and disassembler
24
25 * Based on SPIR-V 1.0 Revision 3.
26   * Supports GLSL std450 extended instructions.
27   * Supports OpenCL extended instructions.
28 * Assembler only does basic syntax checking.  No cross validation of
29   IDs or types is performed, except to check literal arguments to
30   `OpConstant`, `OpSpecConstant`, and `OpSwitch`.
31
32 See [`syntax.md`](syntax.md) for the assembly language syntax.
33
34 ### Validator
35
36 *Warning:* The validator is incomplete.
37
38 ## Source code
39
40 The SPIR-V Tools are maintained by members of the The Khronos Group Inc.,
41 at https://github.com/KhronosGroup/SPIRV-Tools.
42
43 Contributions via merge request are welcome. Changes should:
44 * Be provided under the [Khronos license](#license).
45 * Include tests to cover updated functionality.
46 * C++ code should follow the [Google C++ Style Guide][cpp-style-guide].
47 * Code should be formatted with `clang-format`.  Settings are defined by
48   the included [.clang-format](.clang-format) file.
49
50 We intend to maintain a linear history on the GitHub `master` branch.
51
52 ### Source code organization
53
54 * `external/googletest`: Intended location for the
55   [googletest][googletest] sources, not provided
56 * `include/`: API clients should add this directory to the include search path
57 * `include/spirv-tools/libspirv.h`: C API public interface
58 * `include/spirv/` : Contains header files from the SPIR-V Registry, required
59   by the public API.
60 * `source/`: API implementation
61 * `test/`: Tests, using the [googletest][googletest] framework
62 * `tools/`: Command line executables
63
64 ### Tests
65
66 The project contains a number of tests, used to drive development
67 and ensure correctness.  The tests are written using the
68 [googletest][googletest] framework.  The `googletest`
69 source is not provided with this project.  There are two ways to enable
70 tests:
71 * If SPIR-V Tools is configured as part of an enclosing project, then the
72   enclosing project should configure `googletest` before configuring SPIR-V Tools.
73 * If SPIR-V Tools is configured as a standalone project, then download the
74   `googletest` source into the `<spirv-dir>/external/googletest` directory before
75   configuring and building the project.
76
77 *Note*: You must use a version of googletest that includes
78 [a fix][googletest-pull-612] for [googletest issue 610][googletest-issue-610].
79 The fix is included on the googletest master branch any time after 2015-11-10.
80 In particular, googletest must be newer than version 1.7.0.
81
82 ## Build
83
84 The project uses [CMake][cmake] to generate platform-specific build
85 configurations.  To generate these build files, issue the following commands:
86
87 ```
88 mkdir <spirv-dir>/build
89 cd <spirv-dir>/build
90 cmake [-G <platform-generator>] <spirv-dir>
91 ```
92
93 Once the build files have been generated, build using your preferred
94 development environment.
95
96 ### CMake options
97
98 The following CMake options are supported:
99
100 * `SPIRV_COLOR_TERMINAL={ON|OFF}`, default `ON` - Enables color console output.
101 * `SPIRV_SKIP_EXECUTABLES={ON|OFF}`, default `OFF`- Build only the library, not
102   the command line tools.  This will also prevent the tests from being built.
103 * `SPIRV_USE_SANITIZER=<sanitizer>`, default is no sanitizing - On UNIX
104   platforms with an appropriate version of `clang` this option enables the use
105   of the sanitizers documented [here][clang-sanitizers].
106   This should only be used with a debug build.
107 * `SPIRV_WARN_EVERYTHING={ON|OFF}`, default `OFF` - On UNIX platforms enable
108   more strict warnings.  The code might not compile with this option enabled.
109   For Clang, enables `-Weverything`.  For GCC, enables `-Wpedantic`.
110   See [`CMakeLists.txt`](CMakeLists.txt) for details.
111 * `SPIRV_WERROR={ON|OFF}`, default `ON` - Forces a compilation error on any
112   warnings encountered by enabling the compiler-specific compiler front-end
113   option.
114
115 ## Library
116
117 ### Usage
118
119 The library provides a C API, but the internals use C++11.
120
121 In order to use the library from an application, the include path should point
122 to `<spirv-dir>/include`, which will enable the application to include the
123 header `<spirv-dir>/include/libspirv/libspirv.h` then linking against the
124 static library in `<spirv-build-dir>/libSPIRV-Tools.a` or
125 `<spirv-build-dir>/SPIRV-Tools.lib`.
126
127 * `SPIRV-Tools` CMake target: Creates the static library:
128   * `<spirv-build-dir>/libSPIRV-Tools.a` on Linux and OS X.
129   * `<spirv-build-dir>/libSPIRV-Tools.lib` on Windows.
130
131 #### Entry points
132
133 The interfaces are still under development, and are expected to change.
134
135 There are three main entry points into the library.
136
137 * `spvTextToBinary`: An assembler, translating text to a binary SPIR-V module.
138 * `spvBinaryToText`: A disassembler, translating a binary SPIR-V module to
139   text.
140 * `spvBinaryParse`: The entry point to a binary parser API.  It issues callbacks
141   for the header and each parsed instruction.  The disassembler is implemented
142   as a client of `spvBinaryParse`.
143 * `spvValidate` implements the validator functionality. *Incomplete*
144
145 ## Command line tools
146
147 Command line tools, which wrap the above library functions, are provided to
148 assemble or disassemble shader files.  It's a convention to name SPIR-V
149 assembly and binary files with suffix `.spvasm` and `.spv`, respectively.
150
151 ### Assembler tool
152
153 The assembler reads the assembly language text, and emits the binary form.
154
155 The standalone assembler is the exectuable called `spirv-as`, and is located in
156 `<spirv-build-dir>/spirv-as`.  The functionality of the assembler is implemented
157 by the `spvTextToBinary` library function.
158
159 * `spirv-as` - the standalone assembler
160   * `<spirv-dir>/spirv-as`
161
162 Use option `-h` to print help.
163
164 ### Disassembler tool
165
166 The disassembler reads the binary form, and emits assembly language text.
167
168 The standalone disassembler is the executable called `spirv-dis`, and is located in
169 `<spirv-build-dir>/spirv-dis`. The functionality of the disassembler is implemented
170 by the `spvBinaryToText` library function.
171
172 * `spirv-dis` - the standalone disassembler
173   * `<spirv-dir>/spirv-dis`
174
175 Use option `-h` to print help.
176
177 The output includes syntax colouring when printing to the standard output stream,
178 on Linux, Windows, and OS X.
179
180 ### Validator tool
181
182 *Warning:* This functionality is under development, and is incomplete.
183
184 The standalone validator is the executable called `spirv-val`, and is located in
185 `<spirv-build-dir>/spirv-val`. The functionality of the validator is implemented
186 by the `spvValidate` library function.
187
188 The validator operates on the binary form.
189
190 * `spirv-val` - the standalone validator
191   * `<spirv-dir>/spirv-val`
192
193 ### Tests
194
195 Tests are only built when googletest is found.
196
197 The `<spirv-build-dir>/UnitSPIRV` executable runs the project tests.
198 It supports the standard `googletest` command line options.
199
200 The project also adds a CMake test `spirv-tools-testsuite`, which executes
201 `UnitSPIRV`.  That way it's possible to run the tests using `ctest`.
202
203 ## Future Work
204 <a name="future"></a>
205
206 ### Assembler and disassembler
207
208 * The disassembler could emit helpful annotations in comments.  For example:
209   * Use variable name information from debug instructions to annotate
210     key operations on variables.
211   * Show control flow information by annotating `OpLabel` instructions with
212     that basic block's predecessors.
213 * Error messages could be improved.
214
215 ### Validator
216
217 This is a work in progress.
218
219 ## Licence
220 <a name="license"></a>
221 ```
222 Copyright (c) 2015-2016 The Khronos Group Inc.
223
224 Permission is hereby granted, free of charge, to any person obtaining a
225 copy of this software and/or associated documentation files (the
226 "Materials"), to deal in the Materials without restriction, including
227 without limitation the rights to use, copy, modify, merge, publish,
228 distribute, sublicense, and/or sell copies of the Materials, and to
229 permit persons to whom the Materials are furnished to do so, subject to
230 the following conditions:
231
232 The above copyright notice and this permission notice shall be included
233 in all copies or substantial portions of the Materials.
234
235 MODIFICATIONS TO THIS FILE MAY MEAN IT NO LONGER ACCURATELY REFLECTS
236 KHRONOS STANDARDS. THE UNMODIFIED, NORMATIVE VERSIONS OF KHRONOS
237 SPECIFICATIONS AND HEADER INFORMATION ARE LOCATED AT
238    https://www.khronos.org/registry/
239
240 THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
241 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
242 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
243 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
244 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
245 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
246 MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
247 ```
248
249 [spirv-registry]: https://www.khronos.org/registry/spir-v/
250 [googletest]: https://github.com/google/googletest
251 [googletest-pull-612]: https://github.com/google/googletest/pull/612
252 [googletest-issue-610]: https://github.com/google/googletest/issues/610
253 [CMake]: https://cmake.org/
254 [cpp-style-guide]: https://google.github.io/styleguide/cppguide.html
255 [clang-sanitizers]: http://clang.llvm.org/docs/UsersManual.html#controlling-code-generation