1 //
2 // Copyright (c) 2008-2017 the Urho3D project.
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21 //
22 
23 #pragma once
24 
25 #include "../Container/HashSet.h"
26 #include "../Core/Object.h"
27 #include "../Resource/XMLFile.h"
28 
29 namespace Urho3D
30 {
31 
32 class Graphics;
33 class ShaderVariation;
34 
35 /// Utility class for collecting used shader combinations during runtime for precaching.
36 class URHO3D_API ShaderPrecache : public Object
37 {
38     URHO3D_OBJECT(ShaderPrecache, Object);
39 
40 public:
41     /// Construct and begin collecting shader combinations. Load existing combinations from XML if the file exists.
42     ShaderPrecache(Context* context, const String& fileName);
43     /// Destruct. Write the collected shaders to XML.
44     ~ShaderPrecache();
45 
46     /// Collect a shader combination. Called by Graphics when shaders have been set.
47     void StoreShaders(ShaderVariation* vs, ShaderVariation* ps);
48 
49     /// Load shaders from an XML file.
50     static void LoadShaders(Graphics* graphics, Deserializer& source);
51 
52 private:
53     /// XML file name.
54     String fileName_;
55     /// XML file.
56     XMLFile xmlFile_;
57     /// Already encountered shader combinations, pointer version for fast queries.
58     HashSet<Pair<ShaderVariation*, ShaderVariation*> > usedPtrCombinations_;
59     /// Already encountered shader combinations.
60     HashSet<String> usedCombinations_;
61 };
62 
63 }
64