Skip to content

Commit

Permalink
Support -fhlsl_functionality1
Browse files Browse the repository at this point in the history
  • Loading branch information
dneto0 committed Apr 17, 2018
1 parent 8dec681 commit 5fd1b25
Show file tree
Hide file tree
Showing 14 changed files with 204 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Revision history for Shaderc

v2018.0-dev 2018-02-27
- Start v2018.0-dev
- Support -fhlsl_functionality1 (also -fhlsl-functionality1)

v2017.2 2018-02-27
- Add a shared library version of libshaderc
Expand Down
12 changes: 12 additions & 0 deletions glslc/README.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ glslc [-c|-S|-E]
[-x ...] [-std=standard]
[ ... options for resource bindings ... ]
[-fhlsl-offsets]
[-fhlsl-functionality1]
[-fentry-point=<name>]
[-fauto-map-locations]
[-flimit=...]
Expand Down Expand Up @@ -354,6 +355,17 @@ specified) will trigger an error.
Use HLSL packing rules instead of GLSL rules when determining offsets of
members of blocks. This option is always on when compiling for HLSL.

[[option-fhlsl-functionality1]]
==== `-fhlsl-functionality1`

Enable extension `SPV_GOOGLE_hlsl_functionality1`, and instructs the compiler
to:

- Annotate HLSL semantic string decorations on interface objects
- Explicitly record the association of a UAV resource with its companion counter buffer.

This option can also be spelled with an underscore: `-fhlsl_functionality1`.

[[option-fentry-point]]
==== `-fentry-point=<name>`

Expand Down
5 changes: 5 additions & 0 deletions glslc/src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ An input file of - represents standard input.
don't have an explicit 'location' layout in the shader
source.
-fhlsl-iomap Use HLSL IO mappings for bindings.
-fhlsl_functionality1, -fhlsl-functionality1
Enable extension SPV_GOOGLE_hlsl_functionality1 for HLSL
compilation.
-fimage-binding-base [stage] <value>
Sets the lowest automatically assigned binding number for
images. Optionally only set it for a single shader stage.
Expand Down Expand Up @@ -336,6 +339,8 @@ int main(int argc, char** argv) {
compiler.options().SetHlslIoMapping(true);
} else if (arg == "-fhlsl-offsets") {
compiler.options().SetHlslOffsets(true);
} else if (arg == "-fhlsl_functionality1" || arg == "-fhlsl-functionality1") {
compiler.options().SetHlslFunctionality1(true);
} else if (((u_kind = shaderc_uniform_kind_image),
(arg == "-fimage-binding-base")) ||
((u_kind = shaderc_uniform_kind_texture),
Expand Down
63 changes: 63 additions & 0 deletions glslc/test/option_fhlsl_functionality1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Copyright 2018 The Shaderc Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import expect
from glslc_test_framework import inside_glslc_testsuite
from placeholder import FileShader

# An HLSL shader with a counter buffer with a counter increment.
HLSL_VERTEX_SHADER_WITH_COUNTER_BUFFER = """
RWStructuredBuffer<int> Ainc;
float4 main() : SV_Target0 {
return float4(Ainc.IncrementCounter(), 0, 1, 2);
}
"""


@inside_glslc_testsuite('OptionFHlslFunctionality1')
class TestHlslFunctionality1MentionsExtension(expect.ValidAssemblyFileWithSubstr):
"""Tests that -fhlsl_functionality1 enabled SPV_GOOGLE_hlsl_functionality1."""

shader = FileShader(HLSL_VERTEX_SHADER_WITH_COUNTER_BUFFER, '.frag')
glslc_args = ['-S', '-x', 'hlsl', '-fhlsl_functionality1', shader]
expected_assembly_substr = 'OpExtension "SPV_GOOGLE_hlsl_functionality1"'


@inside_glslc_testsuite('OptionFHlslFunctionality1')
class TestHlslFunctionality1DecoratesCounter(expect.ValidAssemblyFileWithSubstr):
"""Tests that -fhlsl_functionality1 decorates the output target"""

shader = FileShader(HLSL_VERTEX_SHADER_WITH_COUNTER_BUFFER, '.frag')
glslc_args = ['-S', '-x', 'hlsl', '-fhlsl_functionality1', shader]
expected_assembly_substr = 'OpDecorateStringGOOGLE'


## Next tests use the option with the hypen instead of underscore.

@inside_glslc_testsuite('OptionFHlslFunctionality1')
class TestHlslHyphenFunctionality1MentionsExtension(expect.ValidAssemblyFileWithSubstr):
"""Tests that -fhlsl-functionality1 enabled SPV_GOOGLE_hlsl_functionality1."""

shader = FileShader(HLSL_VERTEX_SHADER_WITH_COUNTER_BUFFER, '.frag')
glslc_args = ['-S', '-x', 'hlsl', '-fhlsl-functionality1', shader]
expected_assembly_substr = 'OpExtension "SPV_GOOGLE_hlsl_functionality1"'


@inside_glslc_testsuite('OptionFHlslFunctionality1')
class TestHlslHyphenFunctionality1DecoratesCounter(expect.ValidAssemblyFileWithSubstr):
"""Tests that -fhlsl-functionality1 decorates the output target"""

shader = FileShader(HLSL_VERTEX_SHADER_WITH_COUNTER_BUFFER, '.frag')
glslc_args = ['-S', '-x', 'hlsl', '-fhlsl-functionality1', shader]
expected_assembly_substr = 'OpDecorateStringGOOGLE'
3 changes: 3 additions & 0 deletions glslc/test/parameter_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ class HelpParameters(
don't have an explicit 'location' layout in the shader
source.
-fhlsl-iomap Use HLSL IO mappings for bindings.
-fhlsl_functionality1, -fhlsl-functionality1
Enable extension SPV_GOOGLE_hlsl_functionality1 for HLSL
compilation.
-fimage-binding-base [stage] <value>
Sets the lowest automatically assigned binding number for
images. Optionally only set it for a single shader stage.
Expand Down
5 changes: 5 additions & 0 deletions libshaderc/include/shaderc/shaderc.h
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,11 @@ SHADERC_EXPORT void shaderc_compile_options_set_hlsl_register_set_and_binding(
shaderc_compile_options_t options, const char* reg, const char* set,
const char* binding);

// Sets whether the compiler should enable extension
// SPV_GOOGLE_hlsl_functionality1.
SHADERC_EXPORT void shaderc_compile_options_set_hlsl_functionality1(
shaderc_compile_options_t options, bool enable);

// An opaque handle to the results of a call to any shaderc_compile_into_*()
// function.
typedef struct shaderc_compilation_result* shaderc_compilation_result_t;
Expand Down
6 changes: 6 additions & 0 deletions libshaderc/include/shaderc/shaderc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,12 @@ class CompileOptions {
options_, reg.c_str(), set.c_str(), binding.c_str());
}

// Sets whether the compiler should enable extension
// SPV_GOOGLE_hlsl_functionality1.
void SetHlslFunctionality1(bool enable) {
shaderc_compile_options_set_hlsl_functionality1(options_, enable);
}

private:
CompileOptions& operator=(const CompileOptions& other) = delete;
shaderc_compile_options_t options_;
Expand Down
6 changes: 6 additions & 0 deletions libshaderc/src/common_shaders_for_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,12 @@ const char kGlslMultipleFnShader[] =
int foo(int a) { return a; }
void main() { outVal = foo(inVal); })";

const char kHlslShaderWithCounterBuffer[] =
R"(RWStructuredBuffer<uint> Ainc;
float4 main() : SV_Target0 {
return float4(Ainc.IncrementCounter(), 0, 0, 0);
})";

#ifdef __cplusplus
}
#endif // __cplusplus
Expand Down
5 changes: 5 additions & 0 deletions libshaderc/src/shaderc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,11 @@ void shaderc_compile_options_set_hlsl_register_set_and_binding(
options->compiler.SetHlslRegisterSetAndBinding(reg, set, binding);
}

void shaderc_compile_options_set_hlsl_functionality1(
shaderc_compile_options_t options, bool enable) {
options->compiler.EnableHlslFunctionality1(enable);
}

shaderc_compiler_t shaderc_compiler_initialize() {
static shaderc_util::GlslangInitializer* initializer =
new shaderc_util::GlslangInitializer;
Expand Down
27 changes: 27 additions & 0 deletions libshaderc/src/shaderc_cpp_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1376,4 +1376,31 @@ TEST_F(CppInterface, HlslRegSetBindingForAllStagesRespected) {
EXPECT_THAT(disassembly_text, HasSubstr("OpDecorate %t4 Binding 16"));
}

TEST_F(CppInterface, HlslFunctionality1OffByDefault) {
CompileOptions options;
options.SetSourceLanguage(shaderc_source_language_hlsl);
const std::string disassembly_text = AssemblyOutput(
kHlslShaderWithCounterBuffer, shaderc_glsl_fragment_shader, options);
EXPECT_THAT(disassembly_text, Not(HasSubstr("OpDecorateStringGOOGLE")));
}

TEST_F(CppInterface, HlslFunctionality1Respected) {
CompileOptions options;
options.SetSourceLanguage(shaderc_source_language_hlsl);
options.SetHlslFunctionality1(true);
const std::string disassembly_text = AssemblyOutput(
kHlslShaderWithCounterBuffer, shaderc_glsl_fragment_shader, options);
EXPECT_THAT(disassembly_text, HasSubstr("OpDecorateStringGOOGLE"));
}

TEST_F(CppInterface, HlslFunctionality1SurvivesCloning) {
CompileOptions options;
options.SetSourceLanguage(shaderc_source_language_hlsl);
options.SetHlslFunctionality1(true);
CompileOptions cloned_options(options);
const std::string disassembly_text = AssemblyOutput(
kHlslShaderWithCounterBuffer, shaderc_glsl_fragment_shader, cloned_options);
EXPECT_THAT(disassembly_text, HasSubstr("OpDecorateStringGOOGLE"));
}

} // anonymous namespace
31 changes: 31 additions & 0 deletions libshaderc/src/shaderc_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1674,4 +1674,35 @@ TEST_F(CompileStringWithOptionsTest, HlslOffsetsOptionEnableRespected) {
EXPECT_THAT(disassembly_text, HasSubstr("OpMemberDecorate %B 1 Offset 4"));
}

TEST_F(CompileStringWithOptionsTest, HlslFunctionality1OffByDefault) {
shaderc_compile_options_set_source_language(options_.get(),
shaderc_source_language_hlsl);
const std::string disassembly_text =
CompilationOutput(kHlslShaderWithCounterBuffer, shaderc_fragment_shader,
options_.get(), OutputType::SpirvAssemblyText);
EXPECT_THAT(disassembly_text, Not(HasSubstr("OpDecorateStringGOOGLE"))) << disassembly_text;
}

TEST_F(CompileStringWithOptionsTest, HlslFunctionality1Respected) {
shaderc_compile_options_set_source_language(options_.get(),
shaderc_source_language_hlsl);
shaderc_compile_options_set_hlsl_functionality1(options_.get(), true);
const std::string disassembly_text =
CompilationOutput(kHlslShaderWithCounterBuffer, shaderc_fragment_shader,
options_.get(), OutputType::SpirvAssemblyText);
EXPECT_THAT(disassembly_text, HasSubstr("OpDecorateStringGOOGLE"));
}

TEST_F(CompileStringWithOptionsTest, HlslFunctionality1SurvivesCloning) {
shaderc_compile_options_set_source_language(options_.get(),
shaderc_source_language_hlsl);
shaderc_compile_options_set_hlsl_functionality1(options_.get(), true);
compile_options_ptr cloned_options(
shaderc_compile_options_clone(options_.get()));
const std::string disassembly_text =
CompilationOutput(kHlslShaderWithCounterBuffer, shaderc_fragment_shader,
cloned_options.get(), OutputType::SpirvAssemblyText);
EXPECT_THAT(disassembly_text, HasSubstr("OpDecorateStringGOOGLE"));
}

} // anonymous namespace
7 changes: 7 additions & 0 deletions libshaderc_util/include/libshaderc_util/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ class Compiler {
hlsl_iomap_(false),
hlsl_offsets_(false),
hlsl_legalization_enabled_(true),
hlsl_functionality1_enabled_(false),
hlsl_explicit_bindings_() {}

// Requests that the compiler place debug information into the object code,
Expand All @@ -215,6 +216,9 @@ class Compiler {
// Enables or disables HLSL legalization passes.
void EnableHlslLegalization(bool hlsl_legalization_enabled);

// Enables or disables extension SPV_GOOGLE_hlsl_functionality1
void EnableHlslFunctionality1(bool enable);

// When a warning is encountered it treat it as an error.
void SetWarningsAsErrors();

Expand Down Expand Up @@ -482,6 +486,9 @@ class Compiler {
// source language is HLSL.
bool hlsl_legalization_enabled_;

// True if the compiler should support extension SPV_GOOGLE_hlsl_functionality1.
bool hlsl_functionality1_enabled_;

// A sequence of triples, each triple representing a specific HLSL register
// name, and the set and binding numbers it should be mapped to, but in
// the form of strings. This is how Glslang wants to consume the data.
Expand Down
10 changes: 10 additions & 0 deletions libshaderc_util/src/compiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,9 @@ std::tuple<bool, std::vector<uint32_t>, size_t> Compiler::Compile(
target_client_info.client_version);
shader.setEnvTarget(target_client_info.target_language,
target_client_info.target_language_version);
if (hlsl_functionality1_enabled_) {
shader.setEnvTargetHlslFunctionality1();
}

// TODO(dneto): Generate source-level debug info if requested.
bool success = shader.parse(
Expand Down Expand Up @@ -432,6 +435,10 @@ void Compiler::EnableHlslLegalization(bool hlsl_legalization_enabled) {
hlsl_legalization_enabled_ = hlsl_legalization_enabled;
}

void Compiler::EnableHlslFunctionality1(bool enable) {
hlsl_functionality1_enabled_ = enable;
}

void Compiler::SetSuppressWarnings() { suppress_warnings_ = true; }

std::tuple<bool, std::string, std::string> Compiler::PreprocessShader(
Expand Down Expand Up @@ -461,6 +468,9 @@ std::tuple<bool, std::string, std::string> Compiler::PreprocessShader(
}
shader.setEnvClient(target_client_info.client,
target_client_info.client_version);
if (hlsl_functionality1_enabled_) {
shader.setEnvTargetHlslFunctionality1();
}

// The preprocessor might be sensitive to the target environment.
// So combine the existing rules with the just-give-me-preprocessor-output
Expand Down
23 changes: 23 additions & 0 deletions libshaderc_util/src/compiler_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,14 @@ float4 main(float2 loc: A) : SV_Target {
return sampleTexture(cts, loc);
})";

const char kHlslShaderWithCounterBuffer[] = R"(
RWStructuredBuffer<float4> Ainc;
float4 main() : SV_Target0 {
return float4(Ainc.IncrementCounter(), 0, 0, 0);
}
)";


// Returns the disassembly of the given SPIR-V binary, as a string.
// Assumes the disassembly will be successful when targeting Vulkan.
std::string Disassemble(const std::vector<uint32_t> binary) {
Expand Down Expand Up @@ -711,4 +719,19 @@ TEST_F(CompilerTest, HlslLegalizationDisabled) {
EXPECT_THAT(disassembly, HasSubstr("OpFunctionCall")) << disassembly;
}

TEST_F(CompilerTest, HlslFunctionality1Enabled) {
compiler_.SetSourceLanguage(Compiler::SourceLanguage::HLSL);
compiler_.EnableHlslFunctionality1(true);
const auto words =
SimpleCompilationBinary(kHlslShaderWithCounterBuffer, EShLangFragment);
const auto disassembly = Disassemble(words);
EXPECT_THAT(disassembly,
HasSubstr("OpExtension \"SPV_GOOGLE_hlsl_functionality1\""))
<< disassembly;
EXPECT_THAT(disassembly,
HasSubstr("OpDecorateStringGOOGLE %_entryPointOutput "
"HlslSemanticGOOGLE \"SV_TARGET0\""))
<< disassembly;
}

} // anonymous namespace

0 comments on commit 5fd1b25

Please sign in to comment.