Skip to content

Commit

Permalink
Add --target-spv option to set target SPIR-V version (google#750)
Browse files Browse the repository at this point in the history
Adds shaderc_compile_options_set_targte_spirv and associated enums.
Defaults to SPIR-V 1.0.

Passes that down into Glslang so we generate a module with the
specified version of SPIR-V.

Refactor how we map to Glslang target and client, and test that
better.

Setting spirv version is just an override.

Plumb through to glslc
  • Loading branch information
dneto0 authored and zoddicus committed Jul 2, 2019
1 parent 8de4816 commit 23bbb32
Show file tree
Hide file tree
Showing 10 changed files with 407 additions and 89 deletions.
22 changes: 22 additions & 0 deletions glslc/README.asciidoc
Expand Up @@ -23,10 +23,12 @@ glslc [-c|-S|-E]
[-fhlsl-functionality1]
[-fentry-point=<name>]
[-fauto-map-locations]
[-finvert-y]
[-flimit=...]
[-flimit-file <resource-limits-file>]
[-fshader-stage=...]
[--target-env=...]
[--target-spv=...]
[-g]
[-O0|-Os]
[-Idirectory...]
Expand Down Expand Up @@ -134,6 +136,11 @@ multiple files generated. A filename of `-` represents standard output.

=== Language and Mode Selection Options

[[option-finverty]]
==== `-finverty`

Inverts position.Y output in a vertex shader.

[[option-flimit]]
==== `-flimit=`

Expand Down Expand Up @@ -233,6 +240,21 @@ Generated code uses SPIR-V 1.0, except that code compiled for Vulkan 1.1 uses SP

If this option is not specified, a default of `vulkan1.0` is used.

==== `--target-spv=`

`--target-spv=<value>` lets you specify the SPIR-V version to be used by the generated
module. The default is to use the highest version of SPIR-V required to be supported
by the target environment. For example, the default is SPIR-V 1.0 for Vulkan 1.0, and
and SPIR-V 1.3 for Vulkan 1.1.

The ``<value>`` can be one of the following:

* `spv1.0`
* `spv1.1`
* `spv1.2`
* `spv1.3`
* `spv1.4`

==== `-x`

`-x` lets you specify the language of the input shader files. Valid languages
Expand Down
28 changes: 28 additions & 0 deletions glslc/src/main.cc
Expand Up @@ -32,6 +32,7 @@
#include "libshaderc_util/string_piece.h"
#include "resource_parse.h"
#include "shader_stage.h"
#include "shaderc/env.h"
#include "shaderc/shaderc.h"
#include "spirv-tools/libspirv.h"

Expand Down Expand Up @@ -155,6 +156,14 @@ An input file of - represents standard input.
vulkan # Same as vulkan1.0
opengl4.5
opengl # Same as opengl4.5
--target-spv=<spirv-version>
Set the SPIR-V version to be used for the generated SPIR-V
module. The default is the highest version of SPIR-V
required to be supported for the target environment.
For example, default for vulkan1.0 is spv1.0, and
the default for vulkan1.1 is spv1.3.
Values are:
spv1.0, spv1.1, spv1.2, spv1.3, spv1.4
--version Display compiler version information.
-w Suppresses all warning messages.
-Werror Treat all warnings as errors.
Expand Down Expand Up @@ -430,6 +439,25 @@ int main(int argc, char** argv) {
return 1;
}
compiler.options().SetTargetEnvironment(target_env, version);
} else if (arg.starts_with("--target-spv=")) {
shaderc_spirv_version ver = shaderc_spirv_version_1_0;
const string_piece ver_str = arg.substr(std::strlen("--target-spv="));
if (ver_str == "spv1.0") {
ver = shaderc_spirv_version_1_0;
} else if (ver_str == "spv1.1") {
ver = shaderc_spirv_version_1_1;
} else if (ver_str == "spv1.2") {
ver = shaderc_spirv_version_1_2;
} else if (ver_str == "spv1.3") {
ver = shaderc_spirv_version_1_3;
} else if (ver_str == "spv1.4") {
ver = shaderc_spirv_version_1_4;
} else {
std::cerr << "glslc: error: invalid value '" << ver_str
<< "' in '--target-spv=" << ver_str << "'" << std::endl;
return 1;
}
compiler.options().SetTargetSpirv(ver);
} else if (arg.starts_with("-mfmt=")) {
const string_piece binary_output_format =
arg.substr(std::strlen("-mfmt="));
Expand Down
8 changes: 8 additions & 0 deletions glslc/test/parameter_tests.py
Expand Up @@ -160,6 +160,14 @@ class HelpParameters(
vulkan # Same as vulkan1.0
opengl4.5
opengl # Same as opengl4.5
--target-spv=<spirv-version>
Set the SPIR-V version to be used for the generated SPIR-V
module. The default is the highest version of SPIR-V
required to be supported for the target environment.
For example, default for vulkan1.0 is spv1.0, and
the default for vulkan1.1 is spv1.3.
Values are:
spv1.0, spv1.1, spv1.2, spv1.3, spv1.4
--version Display compiler version information.
-w Suppresses all warning messages.
-Werror Treat all warnings as errors.
Expand Down
14 changes: 14 additions & 0 deletions libshaderc/include/shaderc/env.h
Expand Up @@ -48,6 +48,20 @@ typedef enum {
shaderc_env_version_webgpu,
} shaderc_env_version;

// The known versions of SPIR-V.
typedef enum {
// Use the values used for word 1 of a SPIR-V binary:
// - bits 24 to 31: zero
// - bits 16 to 23: major version number
// - bits 8 to 15: minor version number
// - bits 0 to 7: zero
shaderc_spirv_version_1_0 = (((uint32_t)0x010000)),
shaderc_spirv_version_1_1 = (((uint32_t)0x010100)),
shaderc_spirv_version_1_2 = (((uint32_t)0x010200)),
shaderc_spirv_version_1_3 = (((uint32_t)0x010300)),
shaderc_spirv_version_1_4 = (((uint32_t)0x010400))
} shaderc_spirv_version;

#ifdef __cplusplus
}
#endif // __cplusplus
Expand Down
8 changes: 8 additions & 0 deletions libshaderc/include/shaderc/shaderc.h
Expand Up @@ -385,6 +385,14 @@ SHADERC_EXPORT void shaderc_compile_options_set_target_env(
shaderc_target_env target,
uint32_t version);

// Sets the target SPIR-V version. The generated module will use this version
// of SPIR-V. Each target environment determines what versions of SPIR-V
// it can consume. Defaults to the highest version of SPIR-V 1.0 which is
// required to be supported by the target environment. E.g. Default to SPIR-V
// 1.0 for Vulkan 1.0 and SPIR-V 1.3 for Vulkan 1.1.
SHADERC_EXPORT void shaderc_compile_options_set_target_spirv(
shaderc_compile_options_t options, shaderc_spirv_version version);

// Sets the compiler mode to treat all warnings as errors. Note the
// suppress-warnings mode overrides this option, i.e. if both
// warning-as-errors and suppress-warnings modes are set, warnings will not
Expand Down
9 changes: 9 additions & 0 deletions libshaderc/include/shaderc/shaderc.hpp
Expand Up @@ -246,6 +246,15 @@ class CompileOptions {
shaderc_compile_options_set_target_env(options_, target, version);
}

// Sets the target SPIR-V version. The generated module will use this version
// of SPIR-V. Each target environment determines what versions of SPIR-V
// it can consume. Defaults to the highest version of SPIR-V 1.0 which is
// required to be supported by the target environment. E.g. Default to SPIR-V
// 1.0 for Vulkan 1.0 and SPIR-V 1.3 for Vulkan 1.1.
void SetTargetSpirv(shaderc_spirv_version version) {
shaderc_compile_options_set_target_spirv(options_, version);
}

// Sets the compiler mode to make all warnings into errors. Note the
// suppress-warnings mode overrides this option, i.e. if both
// warning-as-errors and suppress-warnings modes are set on, warnings will not
Expand Down
7 changes: 7 additions & 0 deletions libshaderc/src/shaderc.cc
Expand Up @@ -477,6 +477,13 @@ void shaderc_compile_options_set_target_env(shaderc_compile_options_t options,
GetCompilerTargetEnvVersion(version));
}

void shaderc_compile_options_set_target_spirv(shaderc_compile_options_t options,
shaderc_spirv_version ver) {
// We made the values match, so we can get away with a static cast.
options->compiler.SetTargetSpirv(
static_cast<shaderc_util::Compiler::SpirvVersion>(ver));
}

void shaderc_compile_options_set_warnings_as_errors(
shaderc_compile_options_t options) {
options->compiler.SetWarningsAsErrors();
Expand Down
53 changes: 53 additions & 0 deletions libshaderc_util/include/libshaderc_util/compiler.h
Expand Up @@ -121,6 +121,15 @@ class Compiler {
OpenGL_4_5 = 450,
};

// SPIR-V version.
enum class SpirvVersion : uint32_t {
v1_0 = 0x010000u,
v1_1 = 0x010100u,
v1_2 = 0x010200u,
v1_3 = 0x010300u,
v1_4 = 0x010400u,
};

enum class OutputType {
SpirvBinary, // A binary module, as defined by the SPIR-V specification.
SpirvAssemblyText, // Assembly syntax defined by the SPIRV-Tools project.
Expand Down Expand Up @@ -222,6 +231,8 @@ class Compiler {
enabled_opt_passes_(),
target_env_(TargetEnv::Vulkan),
target_env_version_(TargetEnvVersion::Default),
target_spirv_version_(SpirvVersion::v1_0),
target_spirv_version_is_forced_(false),
source_language_(SourceLanguage::GLSL),
limits_(kDefaultTBuiltInResource),
auto_bind_uniforms_(false),
Expand Down Expand Up @@ -278,6 +289,12 @@ class Compiler {
void SetTargetEnv(TargetEnv env,
TargetEnvVersion version = TargetEnvVersion::Default);

// Sets the target version of SPIR-V. The module will use this version
// of SPIR-V. Defaults to the highest version of SPIR-V required to be
// supported by the target environment. E.g. default to SPIR-V 1.0 for
// Vulkan 1.0, and SPIR-V 1.3 for Vulkan 1.1.
void SetTargetSpirv(SpirvVersion version);

// Sets the souce language.
void SetSourceLanguage(SourceLanguage lang);

Expand Down Expand Up @@ -496,6 +513,11 @@ class Compiler {
// for those defaults.
TargetEnvVersion target_env_version_;

// The SPIR-V version to be used for the generated module. Defaults to 1.0.
SpirvVersion target_spirv_version_;
// True if the user explicitly set the target SPIR-V version.
bool target_spirv_version_is_forced_;

// The source language. Defaults to GLSL.
SourceLanguage source_language_;

Expand Down Expand Up @@ -589,5 +611,36 @@ inline Compiler::Stage ConvertToStage(EShLanguage stage) {
return Compiler::Stage::Compute;
}

// A GlslangClientInfo captures target client version and desired SPIR-V
// version.
struct GlslangClientInfo {
GlslangClientInfo() {}
GlslangClientInfo(const std::string& e, glslang::EShClient c,
glslang::EShTargetClientVersion cv,
glslang::EShTargetLanguage l,
glslang::EShTargetLanguageVersion lv)
: error(e),
client(c),
client_version(cv),
target_language(l),
target_language_version(lv) {}

std::string error; // Empty if ok, otherwise contains the error message.
glslang::EShClient client = glslang::EShClientNone;
glslang::EShTargetClientVersion client_version;
glslang::EShTargetLanguage target_language = glslang::EShTargetSpv;
glslang::EShTargetLanguageVersion target_language_version =
glslang::EShTargetSpv_1_0;
};

// Returns the mappings to Glslang client, client version, and SPIR-V version.
// Also indicates whether the input values were valid.
GlslangClientInfo GetGlslangClientInfo(
const std::string& error_tag, // Indicates source location, for errors.
shaderc_util::Compiler::TargetEnv env,
shaderc_util::Compiler::TargetEnvVersion env_version,
shaderc_util::Compiler::SpirvVersion spv_version,
bool spv_version_is_forced);

} // namespace shaderc_util
#endif // LIBSHADERC_UTIL_INC_COMPILER_H

0 comments on commit 23bbb32

Please sign in to comment.