فهرست منبع

Compile shader bytecode into separate directories to keep the HLSL source directory clean.

Lasse Öörni 13 سال پیش
والد
کامیت
088561af13

+ 2 - 2
Bin/CompileAllShaders.bat

@@ -1,2 +1,2 @@
-shadercompiler CoreData/Shaders/HLSL/*.xml
-shadercompiler CoreData/Shaders/HLSL/*.xml -dSM3
+shadercompiler CoreData/Shaders/HLSL/*.xml CoreData/Shaders/HLSL/SM2
+shadercompiler CoreData/Shaders/HLSL/*.xml CoreData/Shaders/HLSL/SM3 -dSM3

+ 35 - 49
Engine/Graphics/Direct3D9/D3D9Shader.cpp

@@ -62,6 +62,19 @@ bool Shader::Load(Deserializer& source)
     if (!graphics)
         return false;
     
+    if (graphics->GetSM3Support())
+    {
+        vsExtension_ = ".vs3";
+        psExtension_ = ".ps3";
+        subDir_ = "SM3/";
+    }
+    else
+    {
+        vsExtension_ = ".vs2";
+        psExtension_ = ".ps2";
+        subDir_ = "SM2/";
+    }
+    
     // Get absolute file name of the shader in case we need to invoke ShaderCompiler. This only works if the shader was not
     // loaded from a package file
     fullFileName_.Clear();
@@ -160,61 +173,33 @@ ShaderVariation* Shader::GetVariation(ShaderType type, const String& name)
         return 0;
     
     StringHash nameHash(name);
+    ShaderParser& parser = type == VS ? vsParser_ : psParser_;
+    HashMap<StringHash, SharedPtr<ShaderVariation> >& variations = type == VS ? vsVariations_ : psVariations_;
     
-    if (type == VS)
+    if (parser.HasCombination(name))
     {
-        if (vsParser_.HasCombination(name))
+        HashMap<StringHash, SharedPtr<ShaderVariation> >::Iterator i = variations.Find(nameHash);
+        // Create the shader variation now if not created yet
+        if (i == variations.End())
         {
-            HashMap<StringHash, SharedPtr<ShaderVariation> >::Iterator i = vsVariations_.Find(nameHash);
-            // Create the shader variation now if not created yet
-            if (i == vsVariations_.End())
-            {
-                String path, fileName, extension;
-                SplitPath(GetName(), path, fileName, extension);
-                
-                String compiledShaderName = path + fileName;
-                if (!name.Empty())
-                    compiledShaderName += "_" + name;
-                compiledShaderName += graphics->GetSM3Support() ? ".vs3" : ".vs2";
-                
-                i = vsVariations_.Insert(MakePair(nameHash, SharedPtr<ShaderVariation>(new ShaderVariation(this, VS))));
-                i->second_->SetName(compiledShaderName);
-                
-                SetMemoryUse(GetMemoryUse() + sizeof(ShaderVariation));
-            }
+            String path, fileName, extension;
+            SplitPath(GetName(), path, fileName, extension);
             
-            return i->second_;
-        }
-        else
-            return 0;
-    }
-    else
-    {
-        if (psParser_.HasCombination(name))
-        {
-            HashMap<StringHash, SharedPtr<ShaderVariation> >::Iterator i = psVariations_.Find(nameHash);
-            // Create the shader variation now if not created yet
-            if (i == psVariations_.End())
-            {
-                String path, fileName, extension;
-                SplitPath(GetName(), path, fileName, extension);
-                
-                String compiledShaderName = path + fileName;
-                if (!name.Empty())
-                    compiledShaderName += "_" + name;
-                compiledShaderName += graphics->GetSM3Support() ? ".ps3" : ".ps2";
-                
-                i = psVariations_.Insert(MakePair(nameHash, SharedPtr<ShaderVariation>(new ShaderVariation(this, PS))));
-                i->second_->SetName(compiledShaderName);
-                
-                SetMemoryUse(GetMemoryUse() + sizeof(ShaderVariation));
-            }
+            String compiledShaderName = path + subDir_ + fileName;
+            if (!name.Empty())
+                compiledShaderName += "_" + name;
+            compiledShaderName += type == VS ? vsExtension_ : psExtension_;
+            
+            i = variations.Insert(MakePair(nameHash, SharedPtr<ShaderVariation>(new ShaderVariation(this, type))));
+            i->second_->SetName(compiledShaderName);
             
-            return i->second_;
+            SetMemoryUse(GetMemoryUse() + sizeof(ShaderVariation));
         }
-        else
-            return 0;
+        
+        return i->second_;
     }
+    else
+        return 0;
 }
 
 bool Shader::PrepareVariation(ShaderVariation* variation)
@@ -245,7 +230,8 @@ bool Shader::PrepareVariation(ShaderVariation* variation)
             LOGINFO("Compiling shader " + shaderName);
             
             Vector<String> arguments;
-            arguments.Push("\"" + fullFileName_ + "\"");
+            arguments.Push(fullFileName_);
+            arguments.Push(GetPath(fullFileName_) + subDir_);
             arguments.Push(variation->GetShaderType() == VS ? "-tVS" : "-tPS");
             arguments.Push("-v" + variationName);
             if (graphics->GetSM3Support())

+ 10 - 4
Engine/Graphics/Direct3D9/D3D9Shader.h

@@ -52,10 +52,6 @@ public:
     bool PrepareVariation(ShaderVariation* variation);
     
 private:
-    /// Absolute filename of the shader description file.
-    String fullFileName_;
-    /// Shader source last modified time.
-    unsigned sourceModifiedTime_;
     /// Vertex shader variation parser.
     ShaderParser vsParser_;
     /// Pixel shader variation parser.
@@ -64,4 +60,14 @@ private:
     HashMap<StringHash, SharedPtr<ShaderVariation> > vsVariations_;
     /// Pixel shader variations.
     HashMap<StringHash, SharedPtr<ShaderVariation> > psVariations_;
+    /// Absolute filename of the shader description file.
+    String fullFileName_;
+    /// Compiled vertex shader file extension.
+    String vsExtension_;
+    /// Compiled vertex shader file extension.
+    String psExtension_;
+    /// Subdirectory for compiled shaders.
+    String subDir_;
+    /// Shader source last modified time.
+    unsigned sourceModifiedTime_;
 };

+ 4 - 4
Tools/ShaderCompiler/ShaderCompiler.cpp

@@ -192,7 +192,7 @@ void Run(const Vector<String>& arguments)
     if (arguments.Size() < 1)
     {
         ErrorExit(
-            "ShaderCompiler <definitionfile> [options]\n\n"
+            "ShaderCompiler <definitionfile> <outputpath> [options]\n\n"
             "Options:\n"
             "-tVS|PS Compile only vertex or pixel shaders, by default compile both\n"
             "-vX     Compile only the shader variation X\n"
@@ -205,7 +205,7 @@ void Run(const Vector<String>& arguments)
     String path, file, extension;
     SplitPath(arguments[0], path, file, extension);
     inDir_ = AddTrailingSlash(path);
-    outDir_ = inDir_;
+    outDir_ = AddTrailingSlash(arguments[1]);
     
     for (unsigned i = 1; i < arguments.Size(); ++i)
     {
@@ -299,7 +299,7 @@ void CompileShader(const String& fileName)
                 
                 compile.type_ = VS;
                 compile.name_ = file;
-                compile.outFileName_ = inDir_ + file;
+                compile.outFileName_ = outDir_ + file;
                 if (!src.name_.Empty())
                 {
                     compile.name_ += "_" + src.name_;
@@ -332,7 +332,7 @@ void CompileShader(const String& fileName)
                 
                 compile.type_ = PS;
                 compile.name_ = file;
-                compile.outFileName_ = inDir_ + file;
+                compile.outFileName_ = outDir_ + file;
                 if (!src.name_.Empty())
                 {
                     compile.name_ += "_" + src.name_;