From 21fa39b4f6283383fe185771dd2d35255ce925fc Mon Sep 17 00:00:00 2001 From: Lioncash Date: Thu, 19 Apr 2018 19:59:20 -0400 Subject: [PATCH 1/4] glsl_shader_decompiler: Append indentation without constructing a separate std::string The interface of std::string already lets us append N copies of a character to an existing string. --- src/video_core/renderer_opengl/gl_shader_decompiler.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/video_core/renderer_opengl/gl_shader_decompiler.cpp b/src/video_core/renderer_opengl/gl_shader_decompiler.cpp index 6233ee3585..389a23edb6 100644 --- a/src/video_core/renderer_opengl/gl_shader_decompiler.cpp +++ b/src/video_core/renderer_opengl/gl_shader_decompiler.cpp @@ -112,7 +112,7 @@ public: void AddLine(const std::string& text) { DEBUG_ASSERT(scope >= 0); if (!text.empty()) { - shader_source += std::string(static_cast(scope) * 4, ' '); + AppendIndentation(); } shader_source += text + '\n'; } @@ -124,6 +124,10 @@ public: int scope = 0; private: + void AppendIndentation() { + shader_source.append(static_cast(scope) * 4, ' '); + } + std::string shader_source; }; From 5a3da4f1a164f6e134415248e4f79f7b52c31077 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Thu, 19 Apr 2018 20:02:24 -0400 Subject: [PATCH 2/4] glsl_shader_decompiler: Add char overload for ShaderWriter's AddLine() Avoids constructing a std::string just to append a character. --- .../renderer_opengl/gl_shader_decompiler.cpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/video_core/renderer_opengl/gl_shader_decompiler.cpp b/src/video_core/renderer_opengl/gl_shader_decompiler.cpp index 389a23edb6..9e60c911cf 100644 --- a/src/video_core/renderer_opengl/gl_shader_decompiler.cpp +++ b/src/video_core/renderer_opengl/gl_shader_decompiler.cpp @@ -117,6 +117,13 @@ public: shader_source += text + '\n'; } + void AddLine(char character) { + DEBUG_ASSERT(scope >= 0); + AppendIndentation(); + shader_source += character; + shader_source += '\n'; + } + std::string GetResult() { return std::move(shader_source); } @@ -511,14 +518,14 @@ private: } --shader.scope; - shader.AddLine("}"); + shader.AddLine('}'); } shader.AddLine("default: return false;"); - shader.AddLine("}"); + shader.AddLine('}'); --shader.scope; - shader.AddLine("}"); + shader.AddLine('}'); shader.AddLine("return false;"); } @@ -568,7 +575,7 @@ private: unsigned const_buffer_layout = 0; for (const auto& entry : GetConstBuffersDeclarations()) { declarations.AddLine("layout(std430) buffer " + entry.GetName()); - declarations.AddLine("{"); + declarations.AddLine('{'); declarations.AddLine(" float c" + std::to_string(entry.GetIndex()) + "[];"); declarations.AddLine("};"); declarations.AddLine(""); From 55fef54f2e82981f1c5ea787a0ab34672bcdc5c0 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Thu, 19 Apr 2018 20:05:42 -0400 Subject: [PATCH 3/4] glsl_shader_decompiler: Add AddNewLine() function to ShaderWriter Avoids constructing a std::string just to append a newline character --- .../renderer_opengl/gl_shader_decompiler.cpp | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/video_core/renderer_opengl/gl_shader_decompiler.cpp b/src/video_core/renderer_opengl/gl_shader_decompiler.cpp index 9e60c911cf..c55febbfaa 100644 --- a/src/video_core/renderer_opengl/gl_shader_decompiler.cpp +++ b/src/video_core/renderer_opengl/gl_shader_decompiler.cpp @@ -114,13 +114,19 @@ public: if (!text.empty()) { AppendIndentation(); } - shader_source += text + '\n'; + shader_source += text; + AddNewLine(); } void AddLine(char character) { DEBUG_ASSERT(scope >= 0); AppendIndentation(); shader_source += character; + AddNewLine(); + } + + void AddNewLine() { + DEBUG_ASSERT(scope >= 0); shader_source += '\n'; } @@ -475,7 +481,7 @@ private: for (const auto& subroutine : subroutines) { shader.AddLine("bool " + subroutine.GetName() + "();"); } - shader.AddLine(""); + shader.AddNewLine(); // Add the main entry point shader.AddLine("bool exec_shader() {"); @@ -552,7 +558,7 @@ private: for (const auto& reg : declr_register) { declarations.AddLine("float " + reg + " = 0.0;"); } - declarations.AddLine(""); + declarations.AddNewLine(); for (const auto& index : declr_input_attribute) { // TODO(bunnei): Use proper number of elements for these @@ -561,7 +567,7 @@ private: static_cast(Attribute::Index::Attribute_0)) + ") in vec4 " + GetInputAttribute(index) + ";"); } - declarations.AddLine(""); + declarations.AddNewLine(); for (const auto& index : declr_output_attribute) { // TODO(bunnei): Use proper number of elements for these @@ -570,7 +576,7 @@ private: static_cast(Attribute::Index::Attribute_0)) + ") out vec4 " + GetOutputAttribute(index) + ";"); } - declarations.AddLine(""); + declarations.AddNewLine(); unsigned const_buffer_layout = 0; for (const auto& entry : GetConstBuffersDeclarations()) { @@ -578,7 +584,7 @@ private: declarations.AddLine('{'); declarations.AddLine(" float c" + std::to_string(entry.GetIndex()) + "[];"); declarations.AddLine("};"); - declarations.AddLine(""); + declarations.AddNewLine(); ++const_buffer_layout; } } From 2259f5d56b442866056b3b3011d834c7212b694a Mon Sep 17 00:00:00 2001 From: Lioncash Date: Thu, 19 Apr 2018 20:10:40 -0400 Subject: [PATCH 4/4] glsl_shader_decompiler: Use std::string_view instead of std::string for AddLine() This function doesn't need to take ownership of the string data being given to it, considering all we do is append the characters to the internal string instance. Instead, use a string view to simply reference the string data without any potential heap allocation. Now anything that is a raw const char* won't need to be converted to a std::string before appending. --- src/video_core/renderer_opengl/gl_shader_decompiler.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/video_core/renderer_opengl/gl_shader_decompiler.cpp b/src/video_core/renderer_opengl/gl_shader_decompiler.cpp index c55febbfaa..22a413b73a 100644 --- a/src/video_core/renderer_opengl/gl_shader_decompiler.cpp +++ b/src/video_core/renderer_opengl/gl_shader_decompiler.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include "common/assert.h" #include "common/common_types.h" #include "video_core/engines/shader_bytecode.h" @@ -109,7 +110,7 @@ private: class ShaderWriter { public: - void AddLine(const std::string& text) { + void AddLine(std::string_view text) { DEBUG_ASSERT(scope >= 0); if (!text.empty()) { AppendIndentation();