Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
ShaderCache.hh
1 /*===========================================================================*\
2  * *
3  * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39  * *
40 \*===========================================================================*/
41 
42 /*===========================================================================*\
43  * *
44  * $Revision$ *
45  * $Author$ *
46  * $Date$ *
47  * *
48 \*===========================================================================*/
49 
50 #pragma once
51 
52 
53 #include <list>
54 #include <QDateTime>
55 #include <ACG/Config/ACGDefines.hh>
56 #include <ACG/GL/ShaderGenerator.hh>
57 
58 // forward declaration
59 namespace GLSL
60 {
61  class Program;
62 }
63 
64 
65 namespace ACG {
66 
67 
79 class ACGDLLEXPORT ShaderCache
80 {
81 public:
82 
84  virtual ~ShaderCache();
85 
90  static ShaderCache* getInstance();
91 
92 
99  GLSL::Program* getProgram(const ShaderGenDesc* _desc, const std::vector<unsigned int>& _mods);
100 
107  GLSL::Program* getProgram(const ShaderGenDesc* _desc, const std::vector<unsigned int>* _mods)
108  {
109  return _mods ? getProgram(_desc, *_mods) : getProgram(_desc);
110  }
111 
118  GLSL::Program* getProgram(const ShaderGenDesc* _desc);
119 
120 
135  GLSL::Program* getProgram(const char* _vertexShaderFile,
136  const char* _tessControlShaderFile,
137  const char* _tessEvalShaderFile,
138  const char* _geometryShaderFile,
139  const char* _fragmentShaderFile,
140  QStringList* _macros = 0, bool _verbose = true);
141 
153  GLSL::Program* getProgram(const char* _vertexShaderFile, const char* _fragmentShaderFile, QStringList* _macros = 0, bool _verbose = true);
154 
155 
166  GLSL::Program* getComputeProgram(const char* _computeShaderFile, QStringList* _macros = 0, bool _verbose = true);
167 
168 
169 
172  void clearCache();
173 
177  void setTimeCheck(bool _on){timeCheck_ = _on;}
178  bool getTimeCheck(){return timeCheck_;}
179 
180 
183  void setDebugOutputDir(const char* _outputDir);
184 
185 protected:
186  ShaderCache();
187 
188  struct CacheEntry
189  {
190  ShaderGenDesc desc;
191  std::vector<unsigned int> mods;
192 
193  // string-pointer in ShaderGenDesc may not be permanent,
194  // so copy string data here
195  QString strVertexTemplate;
196  QString strTessControlTemplate;
197  QString strTessEvaluationTemplate;
198  QString strGeometryTemplate;
199  QString strFragmentTemplate;
200 
201  QDateTime vertexFileLastMod;
202  QDateTime tessControlFileLastMod;
203  QDateTime tessEvaluationFileLastMod;
204  QDateTime geometryFileLastMod;
205  QDateTime fragmentFileLastMod;
206 
207  QStringList macros;
208  };
209 
211  bool compareTimeStamp(const CacheEntry* _a, const CacheEntry* _b);
212 
214  int compareShaderGenDescs(const CacheEntry* _a, const CacheEntry* _b);
215 
216 
217  typedef std::list<std::pair<CacheEntry, GLSL::Program*> > CacheList;
218 
220  CacheList cache_;
221 
223  CacheList cacheStatic_;
224 
227 
228  bool timeCheck_;
229 
231  QString dbgOutputDir_;
232 };
233 
234 
235 
236 //=============================================================================
237 } // namespace ACG
238 //=============================================================================
Namespace providing different geometric functions concerning angles.
Definition: DBSCANT.cc:51
Cache for shaders.
Definition: ShaderCache.hh:79
CacheList cacheComputeShaders_
cache for static compute shaders
Definition: ShaderCache.hh:226
CacheList cacheStatic_
cache containing static shaders loaded from files (separate from dynamic cache to reduce access time)...
Definition: ShaderCache.hh:223
This namespace contains all the classes and functions for handling GLSL shader and program objects...
Definition: AntiAliasing.hh:75
GLSL::Program * getProgram(const ShaderGenDesc *_desc, const std::vector< unsigned int > *_mods)
Query a dynamically generated program from cache.
Definition: ShaderCache.hh:107
QString dbgOutputDir_
output directory for shaders in dynamic cache
Definition: ShaderCache.hh:231
GLSL program class.
Definition: GLSLShader.hh:217
CacheList cache_
cache containing dynamic shaders from ShaderProgGenerator
Definition: ShaderCache.hh:220
void setTimeCheck(bool _on)
enable or disable checking of the time step of each file
Definition: ShaderCache.hh:177