Browse Source

Avoid string allocations during View::Define() and Renderer::GetShader().

Lasse Öörni 12 years ago
parent
commit
db713bdba2

+ 1 - 1
Source/Engine/Graphics/OpenGL/OGLShaderVariation.cpp

@@ -150,7 +150,7 @@ bool ShaderVariation::Create()
 
 void ShaderVariation::SetName(const String& name)
 {
-    name_ = name;
+    name_ = name.Trimmed().Replaced(' ', '_');
 }
 
 void ShaderVariation::SetDefines(const String& defines)

+ 6 - 1
Source/Engine/Graphics/Renderer.cpp

@@ -552,7 +552,12 @@ unsigned Renderer::GetNumOccluders(bool allViews) const
 
 ShaderVariation* Renderer::GetShader(ShaderType type, const String& name, const String& defines) const
 {
-    if (name != lastShaderName_ || !lastShader_)
+    return GetShader(type, name.CString(), defines.CString());
+}
+
+ShaderVariation* Renderer::GetShader(ShaderType type, const char* name, const char* defines) const
+{
+    if (lastShaderName_ != name || !lastShader_)
     {
         ResourceCache* cache = GetSubsystem<ResourceCache>();
         if (!cache)

+ 2 - 0
Source/Engine/Graphics/Renderer.h

@@ -289,6 +289,8 @@ public:
     VertexBuffer* GetInstancingBuffer() const { return dynamicInstancing_ ? instancingBuffer_ : (VertexBuffer*)0; }
     /// Return a shader variation by name and defines.
     ShaderVariation* GetShader(ShaderType type, const String& name, const String& defines = String::EMPTY) const;
+    /// Return a shader variation by name and defines.
+    ShaderVariation* GetShader(ShaderType type, const char* name, const char* defines) const;
     /// Return the frame update parameters.
     const FrameInfo& GetFrameInfo() const { return frame_; }
     

+ 7 - 12
Source/Engine/Graphics/Shader.cpp

@@ -102,6 +102,11 @@ bool Shader::Load(Deserializer& source)
 }
 
 ShaderVariation* Shader::GetVariation(ShaderType type, const String& defines)
+{
+    return GetVariation(type, defines.CString());
+}
+
+ShaderVariation* Shader::GetVariation(ShaderType type, const char* defines)
 {
     StringHash definesHash(defines);
     
@@ -112,12 +117,7 @@ ShaderVariation* Shader::GetVariation(ShaderType type, const String& defines)
         if (i == vsVariations_.End())
         {
             i = vsVariations_.Insert(MakePair(definesHash, SharedPtr<ShaderVariation>(new ShaderVariation(this, VS))));
-            String path, fileName, extension;
-            SplitPath(GetName(), path, fileName, extension);
-            String fullName = path + fileName + "_" + defines.Replaced(' ', '_');
-            if (fullName.EndsWith("_"))
-                fullName.Resize(fullName.Length() - 1);
-            i->second_->SetName(fullName);
+            i->second_->SetName(GetFileName(GetName()) + "_" + defines);
             i->second_->SetDefines(defines);
             
             SetMemoryUse(GetMemoryUse() + sizeof(ShaderVariation));
@@ -132,12 +132,7 @@ ShaderVariation* Shader::GetVariation(ShaderType type, const String& defines)
         if (i == psVariations_.End())
         {
             i = psVariations_.Insert(MakePair(definesHash, SharedPtr<ShaderVariation>(new ShaderVariation(this, PS))));
-            String path, fileName, extension;
-            SplitPath(GetName(), path, fileName, extension);
-            String fullName = path + fileName + "_" + defines.Replaced(' ', '_');
-            if (fullName.EndsWith("_"))
-                fullName.Resize(fullName.Length() - 1);
-            i->second_->SetName(fullName);
+            i->second_->SetName(GetFileName(GetName()) + "_" + defines);
             i->second_->SetDefines(defines);
             
             SetMemoryUse(GetMemoryUse() + sizeof(ShaderVariation));

+ 2 - 0
Source/Engine/Graphics/Shader.h

@@ -48,6 +48,8 @@ public:
     
     /// Return a variation with defines, which should be processed with SanitateDefines() if possible.
     ShaderVariation* GetVariation(ShaderType type, const String& defines);
+    /// Return a variation with defines, which should be processed with SanitateDefines() if possible.
+    ShaderVariation* GetVariation(ShaderType type, const char* defines);
     /// Return either vertex or pixel shader source code.
     const String& GetSourceCode(ShaderType type) const { return type == VS ? vsSourceCode_ : psSourceCode_; }
     

+ 7 - 10
Source/Engine/Graphics/View.cpp

@@ -360,17 +360,16 @@ bool View::Define(RenderSurface* renderTarget, Viewport* viewport)
             info.vertexLights_ = command.vertexLights_;
             
             // Check scenepass metadata for defining custom passes which interact with lighting
-            String metadata = command.metadata_.Trimmed().ToLower();
-            if (!metadata.Empty())
+            if (!command.metadata_.Empty())
             {
-                if (metadata == "gbuffer")
+                if (command.metadata_ == "gbuffer")
                     gBufferPassName_ = command.pass_;
-                else if (metadata == "base")
+                else if (command.metadata_ == "base" && command.pass_ != "base")
                 {
                     basePassName_ = command.pass_;
                     litBasePassName_ = "lit" + command.pass_;
                 }
-                else if (metadata == "alpha")
+                else if (command.metadata_ == "alpha" && command.pass_ != "alpha")
                 {
                     alphaPassName_ = command.pass_;
                     litAlphaPassName_ = "lit" + command.pass_;
@@ -384,11 +383,9 @@ bool View::Define(RenderSurface* renderTarget, Viewport* viewport)
             
             scenePasses_.Push(info);
         }
-        else if (command.type_ == CMD_FORWARDLIGHTS)
-        {
-            if (!command.pass_.Trimmed().Empty())
-                lightPassName_ = command.pass_;
-        }
+        // Allow a custom forward light pass
+        else if (command.type_ == CMD_FORWARDLIGHTS && !command.pass_.Empty())
+            lightPassName_ = command.pass_;
     }
     
     // Go through commands to check for deferred rendering

+ 2 - 2
Source/Engine/Input/Input.cpp

@@ -652,8 +652,8 @@ void Input::HandleSDLEvent(void* sdlEvent)
 
     case SDL_TEXTINPUT:
         {
-            String text(&evt.text.text[0]);
-            unsigned unicode = text.AtUTF8(0);
+            textInput_ = &evt.text.text[0];
+            unsigned unicode = textInput_.AtUTF8(0);
             if (unicode)
             {
                 using namespace Char;

+ 2 - 0
Source/Engine/Input/Input.h

@@ -224,6 +224,8 @@ private:
     HashSet<int> keyPress_;
     /// Active finger touches.
     HashMap<int, TouchState> touches_;
+    /// String for text input.
+    String textInput_;
     /// Opened joysticks.
     Vector<JoystickState> joysticks_;
     /// Mouse buttons' down state.