Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add -fauto-map-locations option #423

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions glslc/src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ An input file of - represents standard input.
Automatically assign bindings to uniform variables that
don't have an explicit 'binding' layout in the shader
source.
-fauto-map-locations
Automatically assign locations to uniform variables that
don't have an explicit 'location' layout in the shader
source.
-fhlsl-iomap Use HLSL IO mappings for bindings.
-fimage-binding-base [stage] <value>
Sets the lowest automatically assigned binding number for
Expand Down Expand Up @@ -321,6 +325,8 @@ int main(int argc, char** argv) {
}
} else if (arg == "-fauto-bind-uniforms") {
compiler.options().SetAutoBindUniforms(true);
} else if (arg == "-fauto-map-locations") {
compiler.options().SetAutoMapLocations(true);
} else if (arg == "-fhlsl-iomap") {
compiler.options().SetHlslIoMapping(true);
} else if (arg == "-fhlsl-offsets") {
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 @@ -432,6 +432,11 @@ SHADERC_EXPORT void shaderc_compile_options_set_binding_base_for_stage(
shaderc_compile_options_t options, shaderc_shader_kind shader_kind,
shaderc_uniform_kind kind, uint32_t base);

// Sets whether the compiler should automatically assign locations to
// uniform variables that don't have explicit locations in the shader source.
SHADERC_EXPORT void shaderc_compile_options_set_auto_map_locations(
shaderc_compile_options_t options, bool auto_map);

// Sets a descriptor set and binding for an HLSL register in the given stage.
// This method keeps a copy of the string data.
SHADERC_EXPORT void shaderc_compile_options_set_hlsl_register_set_and_binding_for_stage(
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 @@ -286,6 +286,12 @@ class CompileOptions {
kind, base);
}

// Sets whether the compiler automatically assigns locations to
// uniform variables that don't have explicit locations.
void SetAutoMapLocations(bool auto_map) {
shaderc_compile_options_set_auto_map_locations(options_, auto_map);
}

// Sets a descriptor set and binding for an HLSL register in the given stage.
// Copies the parameter strings.
void SetHlslRegisterSetAndBindingForStage(shaderc_shader_kind shader_kind,
Expand Down
5 changes: 5 additions & 0 deletions libshaderc/src/shaderc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,11 @@ void shaderc_compile_options_set_binding_base_for_stage(
GetUniformKind(kind), base);
}

void shaderc_compile_options_set_auto_map_locations(
shaderc_compile_options_t options, bool auto_map) {
options->compiler.SetAutoMapLocations(auto_map);
}

void shaderc_compile_options_set_hlsl_register_set_and_binding_for_stage(
shaderc_compile_options_t options, shaderc_shader_kind shader_kind,
const char* reg, const char* set, const char* binding) {
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 @@ -184,6 +184,7 @@ class Compiler {
limits_(kDefaultTBuiltInResource),
auto_bind_uniforms_(false),
auto_binding_base_(),
auto_map_locations_(false),
hlsl_iomap_(false),
hlsl_offsets_(false),
hlsl_legalization_enabled_(true),
Expand Down Expand Up @@ -250,6 +251,10 @@ class Compiler {
auto_binding_base_[static_cast<int>(stage)][static_cast<int>(kind)] = base;
}

// Sets whether the compiler automatically assigns locations to
// uniform variables that don't have explicit locations.
void SetAutoMapLocations(bool auto_map) { auto_map_locations_ = auto_map; }

// Use HLSL IO mapping rules for bindings. Default is false.
void SetHlslIoMapping(bool hlsl_iomap) { hlsl_iomap_ = hlsl_iomap; }

Expand Down Expand Up @@ -439,6 +444,10 @@ class Compiler {
// The default is zero.
uint32_t auto_binding_base_[kNumStages][kNumUniformKinds];

// True if the compiler should automatically map uniforms that don't
// have explicit locations.
bool auto_map_locations_;

// True if the compiler should use HLSL IO mapping rules when compiling HLSL.
bool hlsl_iomap_;

Expand Down
1 change: 1 addition & 0 deletions libshaderc_util/src/compiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ std::tuple<bool, std::vector<uint32_t>, size_t> Compiler::Compile(
shader.setPreamble(preamble.c_str());
shader.setEntryPoint(entry_point_name);
shader.setAutoMapBindings(auto_bind_uniforms_);
shader.setAutoMapLocations(auto_map_locations_);
const auto& bases = auto_binding_base_[static_cast<int>(used_shader_stage)];
shader.setShiftImageBinding(bases[static_cast<int>(UniformKind::Image)]);
shader.setShiftSamplerBinding(bases[static_cast<int>(UniformKind::Sampler)]);
Expand Down
51 changes: 51 additions & 0 deletions libshaderc_util/src/compiler_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,26 @@ const char kGlslFragShaderNoExplicitBinding[] =
float x = my_ubo.x;
})";

// A GLSL vertex shader with the location defined for its non-opaque uniform
// variable.
const char kGlslVertShaderExplicitLocation[] =
R"(#version 450
layout(location = 10) uniform mat4 my_mat;
layout(location = 0) in vec4 my_vec;
void main(void) {
gl_Position = my_mat * my_vec;
})";

// A GLSL vertex shader without the location defined for its non-opaque uniform
// variable.
const char kGlslVertShaderNoExplicitLocation[] =
R"(#version 450
uniform mat4 my_mat;
layout(location = 0) in vec4 my_vec;
void main(void) {
gl_Position = my_mat * my_vec;
})";

// A GLSL vertex shader with a weirdly packed block.
const char kGlslShaderWeirdPacking[] =
R"(#version 450
Expand Down Expand Up @@ -636,6 +656,37 @@ TEST_F(CompilerTest, AutoMapBindingsSetsBindingsSetFragImageBindingBaseCompiledA
EXPECT_THAT(disassembly, HasSubstr("OpDecorate %my_ubo Binding 4"));
}

TEST_F(CompilerTest, NoAutoMapLocationsFailsCompilationOnOpenGLShader) {
compiler_.SetTargetEnv(Compiler::TargetEnv::OpenGL);
compiler_.SetAutoMapLocations(false);

const auto words = SimpleCompilationBinary(kGlslVertShaderExplicitLocation,
EShLangVertex);
const auto disassembly = Disassemble(words);
EXPECT_THAT(disassembly, HasSubstr("OpDecorate %my_mat Location 10"))
<< disassembly;

EXPECT_FALSE(
SimpleCompilationSucceeds(kGlslVertShaderNoExplicitLocation, EShLangVertex));
}

TEST_F(CompilerTest, AutoMapLocationsSetsLocationsOnOpenGLShader) {
compiler_.SetTargetEnv(Compiler::TargetEnv::OpenGL);
compiler_.SetAutoMapLocations(true);

const auto words_no_auto =
SimpleCompilationBinary(kGlslVertShaderExplicitLocation, EShLangVertex);
const auto disassembly_no_auto = Disassemble(words_no_auto);
EXPECT_THAT(disassembly_no_auto, HasSubstr("OpDecorate %my_mat Location 10"))
<< disassembly_no_auto;

const auto words_auto =
SimpleCompilationBinary(kGlslVertShaderNoExplicitLocation, EShLangVertex);
const auto disassembly_auto = Disassemble(words_auto);
EXPECT_THAT(disassembly_auto, HasSubstr("OpDecorate %my_mat Location 0"))
<< disassembly_auto;
}

TEST_F(CompilerTest, EmitMessageTextOnlyOnce) {
// Emit a warning by compiling a shader without a default entry point name.
// The warning should only be emitted once even though we do parsing, linking,
Expand Down