Developer Documentation
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 #pragma once
45 
46 
47 #include <list>
48 #include <QDateTime>
49 #include <ACG/Config/ACGDefines.hh>
50 #include <ACG/GL/ShaderGenerator.hh>
51 
52 // forward declaration
53 namespace GLSL
54 {
55  class Program;
56 }
57 
58 
59 namespace ACG {
60 
61 
73 class ACGDLLEXPORT ShaderCache
74 {
75 public:
76 
78  virtual ~ShaderCache();
79 
84  static ShaderCache* getInstance();
85 
86 
93  GLSL::Program* getProgram(const ShaderGenDesc* _desc, const std::vector<unsigned int>& _mods);
94 
101  GLSL::Program* getProgram(const ShaderGenDesc* _desc, const std::vector<unsigned int>* _mods)
102  {
103  return _mods ? getProgram(_desc, *_mods) : getProgram(_desc);
104  }
105 
112  GLSL::Program* getProgram(const ShaderGenDesc* _desc);
113 
114 
129  GLSL::Program* getProgram(const char* _vertexShaderFile,
130  const char* _tessControlShaderFile,
131  const char* _tessEvalShaderFile,
132  const char* _geometryShaderFile,
133  const char* _fragmentShaderFile,
134  QStringList* _macros = 0, bool _verbose = true);
135 
147  GLSL::Program* getProgram(const char* _vertexShaderFile, const char* _fragmentShaderFile, QStringList* _macros = 0, bool _verbose = true);
148 
149 
160  GLSL::Program* getComputeProgram(const char* _computeShaderFile, QStringList* _macros = 0, bool _verbose = true);
161 
162 
163 
166  void clearCache();
167 
171  void setTimeCheck(bool _on){timeCheck_ = _on;}
172  bool getTimeCheck(){return timeCheck_;}
173 
174 
177  void setDebugOutputDir(const char* _outputDir);
178 
179 protected:
180  ShaderCache();
181 
182  struct CacheEntry
183  {
184  ShaderGenDesc desc;
185  std::vector<unsigned int> mods;
186 
187  // string-pointer in ShaderGenDesc may not be permanent,
188  // so copy string data here
189  QString strVertexTemplate;
190  QString strTessControlTemplate;
191  QString strTessEvaluationTemplate;
192  QString strGeometryTemplate;
193  QString strFragmentTemplate;
194 
195  QDateTime vertexFileLastMod;
196  QDateTime tessControlFileLastMod;
197  QDateTime tessEvaluationFileLastMod;
198  QDateTime geometryFileLastMod;
199  QDateTime fragmentFileLastMod;
200 
201  QStringList macros;
202  };
203 
205  bool compareTimeStamp(const CacheEntry* _a, const CacheEntry* _b);
206 
208  int compareShaderGenDescs(const CacheEntry* _a, const CacheEntry* _b);
209 
210 
211  typedef std::list<std::pair<CacheEntry, GLSL::Program*> > CacheList;
212 
214  CacheList cache_;
215 
217  CacheList cacheStatic_;
218 
221 
222  bool timeCheck_;
223 
225  QString dbgOutputDir_;
226 };
227 
228 
229 
230 //=============================================================================
231 } // namespace ACG
232 //=============================================================================
This namespace contains all the classes and functions for handling GLSL shader and program objects...
Definition: AntiAliasing.hh:66
Namespace providing different geometric functions concerning angles.
QString dbgOutputDir_
output directory for shaders in dynamic cache
Definition: ShaderCache.hh:225
void setTimeCheck(bool _on)
enable or disable checking of the time step of each file
Definition: ShaderCache.hh:171
GLSL program class.
Definition: GLSLShader.hh:211
CacheList cacheStatic_
cache containing static shaders loaded from files (separate from dynamic cache to reduce access time)...
Definition: ShaderCache.hh:217
CacheList cache_
cache containing dynamic shaders from ShaderProgGenerator
Definition: ShaderCache.hh:214
Cache for shaders.
Definition: ShaderCache.hh:73
GLSL::Program * getProgram(const ShaderGenDesc *_desc, const std::vector< unsigned int > *_mods)
Query a dynamically generated program from cache.
Definition: ShaderCache.hh:101
CacheList cacheComputeShaders_
cache for static compute shaders
Definition: ShaderCache.hh:220