Skip to content

Commit

Permalink
Expose setting relaxed Vulkan rules from glslang
Browse files Browse the repository at this point in the history
  • Loading branch information
dnfield authored and dneto0 committed Nov 24, 2023
1 parent 9b5ad16 commit 2d4950d
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ third_party/tint
android_test/libs
android_test/include
.DS_Store
.vscode/
6 changes: 6 additions & 0 deletions libshaderc/include/shaderc/shaderc.h
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,12 @@ SHADERC_EXPORT void shaderc_compile_options_set_hlsl_functionality1(
SHADERC_EXPORT void shaderc_compile_options_set_hlsl_16bit_types(
shaderc_compile_options_t options, bool enable);

// Enables or disables relaxed Vulkan rules.
//
// This allows most OpenGL shaders to compile under Vulkan semantics.
SHADERC_EXPORT void shaderc_compile_options_set_vulkan_rules_relaxed(
shaderc_compile_options_t options, bool enable);

// Sets whether the compiler should invert position.Y output in vertex shader.
SHADERC_EXPORT void shaderc_compile_options_set_invert_y(
shaderc_compile_options_t options, bool enable);
Expand Down
7 changes: 7 additions & 0 deletions libshaderc/include/shaderc/shaderc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,13 @@ class CompileOptions {
shaderc_compile_options_set_hlsl_16bit_types(options_, enable);
}

// Enables or disables relaxed Vulkan rules.
//
// This allows most OpenGL shaders to compile under Vulkan semantics.
void SetVulkanRulesRelaxed(bool enable) {
shaderc_compile_options_set_vulkan_rules_relaxed(options_, enable);
}

// Sets whether the compiler should invert position.Y output in vertex shader.
void SetInvertY(bool enable) {
shaderc_compile_options_set_invert_y(options_, enable);
Expand Down
5 changes: 5 additions & 0 deletions libshaderc/src/shaderc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,11 @@ void shaderc_compile_options_set_hlsl_16bit_types(
options->compiler.EnableHlsl16BitTypes(enable);
}

void shaderc_compile_options_set_vulkan_rules_relaxed(
shaderc_compile_options_t options, bool enable) {
options->compiler.SetVulkanRulesRelaxed(enable);
}

void shaderc_compile_options_set_invert_y(
shaderc_compile_options_t options, bool enable) {
options->compiler.EnableInvertY(enable);
Expand Down
9 changes: 9 additions & 0 deletions libshaderc_util/include/libshaderc_util/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,11 @@ class Compiler {
// Enables or disables HLSL 16-bit types.
void EnableHlsl16BitTypes(bool enable);

// Enables or disables relaxed Vulkan rules.
//
// This allows most OpenGL shaders to compile under Vulkan semantics.
void SetVulkanRulesRelaxed(bool enable);

// Enables or disables invert position.Y output in vertex shader.
void EnableInvertY(bool enable);

Expand Down Expand Up @@ -545,6 +550,10 @@ class Compiler {
// True if the compiler should support 16-bit HLSL types.
bool hlsl_16bit_types_enabled_;

// True if the compiler should relax Vulkan rules to allow OGL shaders to
// compile.
bool vulkan_rules_relaxed_ = false;

// True if the compiler should invert position.Y output in vertex shader.
bool invert_y_enabled_;

Expand Down
20 changes: 20 additions & 0 deletions libshaderc_util/src/compiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,22 @@ std::tuple<bool, std::vector<uint32_t>, size_t> Compiler::Compile(
if (hlsl_functionality1_enabled_) {
shader.setEnvTargetHlslFunctionality1();
}
if (vulkan_rules_relaxed_) {
glslang::EShSource language;
switch(source_language_) {
case SourceLanguage::GLSL:
language = glslang::EShSourceGlsl;
break;
case SourceLanguage::HLSL:
language = glslang::EShSourceHlsl;
break;
}
// This option will only be used if the Vulkan client is used.
// If new versions of GL_KHR_vulkan_glsl come out, it would make sense to
// let callers specify which version to use. For now, just use 100.
shader.setEnvInput(language, used_shader_stage, glslang::EShClientVulkan, 100);
shader.setEnvInputVulkanRulesRelaxed();
}
shader.setInvertY(invert_y_enabled_);
shader.setNanMinMaxClamp(nan_clamp_);

Expand Down Expand Up @@ -452,6 +468,10 @@ void Compiler::EnableHlslFunctionality1(bool enable) {
hlsl_functionality1_enabled_ = enable;
}

void Compiler::SetVulkanRulesRelaxed(bool enable) {
vulkan_rules_relaxed_ = enable;
}

void Compiler::EnableHlsl16BitTypes(bool enable) {
hlsl_16bit_types_enabled_ = enable;
}
Expand Down
24 changes: 24 additions & 0 deletions libshaderc_util/src/compiler_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,18 @@ const char kGlslVertShaderExplicitLocation[] =
gl_Position = my_mat * my_vec;
})";

// A GLSL fragment shader with the location defined for its non-opaque uniform
// variable.
const char kGlslFragShaderOpaqueUniforms[] =
R"(#version 320 es
precision lowp float;
layout(location = 0) out vec4 oColor;
layout(location = 0) uniform float a;
void main(void) {
oColor = vec4(1.0, 0.0, 0.0, a);
})";

// A GLSL vertex shader without the location defined for its non-opaque uniform
// variable.
const char kGlslVertShaderNoExplicitLocation[] =
Expand Down Expand Up @@ -813,6 +825,18 @@ TEST_F(CompilerTest, HlslFunctionality1Enabled) {
<< disassembly;
}

TEST_F(CompilerTest, RelaxedVulkanRulesEnabled) {
compiler_.SetSourceLanguage(Compiler::SourceLanguage::GLSL);
compiler_.SetAutoBindUniforms(true); // Uniform variable needs a binding
compiler_.SetVulkanRulesRelaxed(true);
const auto words =
SimpleCompilationBinary(kGlslFragShaderOpaqueUniforms, EShLangFragment);
const auto disassembly = Disassemble(words);
EXPECT_THAT(disassembly,
HasSubstr("OpMemberName %gl_DefaultUniformBlock 0 \"a\""))
<< disassembly;
}

TEST_F(CompilerTest, ClampMapsToFClampByDefault) {
const auto words =
SimpleCompilationBinary(kGlslShaderWithClamp, EShLangFragment);
Expand Down

0 comments on commit 2d4950d

Please sign in to comment.