Skip to content

Commit 2d4950d

Browse files
dnfielddneto0
authored andcommitted
Expose setting relaxed Vulkan rules from glslang
1 parent 9b5ad16 commit 2d4950d

File tree

7 files changed

+72
-0
lines changed

7 files changed

+72
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ third_party/tint
1818
android_test/libs
1919
android_test/include
2020
.DS_Store
21+
.vscode/

libshaderc/include/shaderc/shaderc.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,12 @@ SHADERC_EXPORT void shaderc_compile_options_set_hlsl_functionality1(
489489
SHADERC_EXPORT void shaderc_compile_options_set_hlsl_16bit_types(
490490
shaderc_compile_options_t options, bool enable);
491491

492+
// Enables or disables relaxed Vulkan rules.
493+
//
494+
// This allows most OpenGL shaders to compile under Vulkan semantics.
495+
SHADERC_EXPORT void shaderc_compile_options_set_vulkan_rules_relaxed(
496+
shaderc_compile_options_t options, bool enable);
497+
492498
// Sets whether the compiler should invert position.Y output in vertex shader.
493499
SHADERC_EXPORT void shaderc_compile_options_set_invert_y(
494500
shaderc_compile_options_t options, bool enable);

libshaderc/include/shaderc/shaderc.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,13 @@ class CompileOptions {
353353
shaderc_compile_options_set_hlsl_16bit_types(options_, enable);
354354
}
355355

356+
// Enables or disables relaxed Vulkan rules.
357+
//
358+
// This allows most OpenGL shaders to compile under Vulkan semantics.
359+
void SetVulkanRulesRelaxed(bool enable) {
360+
shaderc_compile_options_set_vulkan_rules_relaxed(options_, enable);
361+
}
362+
356363
// Sets whether the compiler should invert position.Y output in vertex shader.
357364
void SetInvertY(bool enable) {
358365
shaderc_compile_options_set_invert_y(options_, enable);

libshaderc/src/shaderc.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,11 @@ void shaderc_compile_options_set_hlsl_16bit_types(
564564
options->compiler.EnableHlsl16BitTypes(enable);
565565
}
566566

567+
void shaderc_compile_options_set_vulkan_rules_relaxed(
568+
shaderc_compile_options_t options, bool enable) {
569+
options->compiler.SetVulkanRulesRelaxed(enable);
570+
}
571+
567572
void shaderc_compile_options_set_invert_y(
568573
shaderc_compile_options_t options, bool enable) {
569574
options->compiler.EnableInvertY(enable);

libshaderc_util/include/libshaderc_util/compiler.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,11 @@ class Compiler {
233233
// Enables or disables HLSL 16-bit types.
234234
void EnableHlsl16BitTypes(bool enable);
235235

236+
// Enables or disables relaxed Vulkan rules.
237+
//
238+
// This allows most OpenGL shaders to compile under Vulkan semantics.
239+
void SetVulkanRulesRelaxed(bool enable);
240+
236241
// Enables or disables invert position.Y output in vertex shader.
237242
void EnableInvertY(bool enable);
238243

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

553+
// True if the compiler should relax Vulkan rules to allow OGL shaders to
554+
// compile.
555+
bool vulkan_rules_relaxed_ = false;
556+
548557
// True if the compiler should invert position.Y output in vertex shader.
549558
bool invert_y_enabled_;
550559

libshaderc_util/src/compiler.cc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,22 @@ std::tuple<bool, std::vector<uint32_t>, size_t> Compiler::Compile(
294294
if (hlsl_functionality1_enabled_) {
295295
shader.setEnvTargetHlslFunctionality1();
296296
}
297+
if (vulkan_rules_relaxed_) {
298+
glslang::EShSource language;
299+
switch(source_language_) {
300+
case SourceLanguage::GLSL:
301+
language = glslang::EShSourceGlsl;
302+
break;
303+
case SourceLanguage::HLSL:
304+
language = glslang::EShSourceHlsl;
305+
break;
306+
}
307+
// This option will only be used if the Vulkan client is used.
308+
// If new versions of GL_KHR_vulkan_glsl come out, it would make sense to
309+
// let callers specify which version to use. For now, just use 100.
310+
shader.setEnvInput(language, used_shader_stage, glslang::EShClientVulkan, 100);
311+
shader.setEnvInputVulkanRulesRelaxed();
312+
}
297313
shader.setInvertY(invert_y_enabled_);
298314
shader.setNanMinMaxClamp(nan_clamp_);
299315

@@ -452,6 +468,10 @@ void Compiler::EnableHlslFunctionality1(bool enable) {
452468
hlsl_functionality1_enabled_ = enable;
453469
}
454470

471+
void Compiler::SetVulkanRulesRelaxed(bool enable) {
472+
vulkan_rules_relaxed_ = enable;
473+
}
474+
455475
void Compiler::EnableHlsl16BitTypes(bool enable) {
456476
hlsl_16bit_types_enabled_ = enable;
457477
}

libshaderc_util/src/compiler_test.cc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,18 @@ const char kGlslVertShaderExplicitLocation[] =
106106
gl_Position = my_mat * my_vec;
107107
})";
108108

109+
// A GLSL fragment shader with the location defined for its non-opaque uniform
110+
// variable.
111+
const char kGlslFragShaderOpaqueUniforms[] =
112+
R"(#version 320 es
113+
precision lowp float;
114+
115+
layout(location = 0) out vec4 oColor;
116+
layout(location = 0) uniform float a;
117+
void main(void) {
118+
oColor = vec4(1.0, 0.0, 0.0, a);
119+
})";
120+
109121
// A GLSL vertex shader without the location defined for its non-opaque uniform
110122
// variable.
111123
const char kGlslVertShaderNoExplicitLocation[] =
@@ -813,6 +825,18 @@ TEST_F(CompilerTest, HlslFunctionality1Enabled) {
813825
<< disassembly;
814826
}
815827

828+
TEST_F(CompilerTest, RelaxedVulkanRulesEnabled) {
829+
compiler_.SetSourceLanguage(Compiler::SourceLanguage::GLSL);
830+
compiler_.SetAutoBindUniforms(true); // Uniform variable needs a binding
831+
compiler_.SetVulkanRulesRelaxed(true);
832+
const auto words =
833+
SimpleCompilationBinary(kGlslFragShaderOpaqueUniforms, EShLangFragment);
834+
const auto disassembly = Disassemble(words);
835+
EXPECT_THAT(disassembly,
836+
HasSubstr("OpMemberName %gl_DefaultUniformBlock 0 \"a\""))
837+
<< disassembly;
838+
}
839+
816840
TEST_F(CompilerTest, ClampMapsToFClampByDefault) {
817841
const auto words =
818842
SimpleCompilationBinary(kGlslShaderWithClamp, EShLangFragment);

0 commit comments

Comments
 (0)