1 /*
2 -----------------------------------------------------------------------------
3 This source file is part of OGRE
4 (Object-oriented Graphics Rendering Engine)
5 For the latest info, see http://www.ogre3d.org
6 
7 Copyright (c) 2000-2014 Torus Knot Software Ltd
8 Permission is hereby granted, free of charge, to any person obtaining a copy
9 of this software and associated documentation files (the "Software"), to deal
10 in the Software without restriction, including without limitation the rights
11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 copies of the Software, and to permit persons to whom the Software is
13 furnished to do so, subject to the following conditions:
14 
15 The above copyright notice and this permission notice shall be included in
16 all copies or substantial portions of the Software.
17 
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 THE SOFTWARE.
25 -----------------------------------------------------------------------------
26 */
27 #ifndef _ShaderProgramWriterGLSLES_
28 #define _ShaderProgramWriterGLSLES_
29 
30 #include "OgreShaderProgramWriterManager.h"
31 #include "OgreShaderGLSLProgramWriter.h"
32 #include "OgreShaderParameter.h"
33 #include "OgreStringVector.h"
34 
35 namespace Ogre {
36 namespace RTShader {
37 
38     class Function;
39     class FunctionInvocation;
40     class Operand;
41     class Program;
42 
43 /** \addtogroup Optional
44 *  @{
45 */
46 /** \addtogroup RTShader
47 *  @{
48 */
49 
50 /** GLSL ES target language writer implementation.
51 @see ProgramWriter.
52 */
53 class GLSLESProgramWriter : public GLSLProgramWriter
54 {
55     // Interface.
56 public:
57 
58     /** Class constructor.
59     */
60     GLSLESProgramWriter ();
61 
62     /** Class destructor */
63     virtual ~GLSLESProgramWriter    ();
64 
65 
66     /**
67     @see ProgramWriter::writeSourceCode.
68     */
69     virtual void            writeSourceCode         (std::ostream& os, Program* program);
70 
71     /**
72     @see ProgramWriter::getTargetLanguage.
73     */
getTargetLanguage()74     virtual const String&   getTargetLanguage       () const { return TargetLanguage; }
75 
76     static String TargetLanguage;
77 
78     protected:
79     typedef     std::map<FunctionInvocation, String>       FunctionMap;
80     typedef     std::vector<FunctionInvocation>            FunctionVector;
81     typedef     FunctionMap::const_iterator                 FunctionMapIterator;
82     typedef     GpuConstTypeToStringMap::const_iterator     GpuConstTypeToStringMapIterator;
83 
84     // Protected methods.
85 protected:
86 
87     /** Cache functions of a dependency */
88     virtual void        cacheDependencyFunctions(const String & libName);
89 
90 
91     /** Create a FunctionInvocation object from a string taken out of a shader library. */
92     FunctionInvocation  *createInvocationFromString (const String & input);
93 
94     /** Write the program dependencies. */
95     void                writeProgramDependencies    (std::ostream& os, Program* program);
96 
97     String processOperand(Operand op, GpuProgramType gpuType);
98 
99     /** Check if a string matches one of the GLSL ES basic types */
100     bool                isBasicType(String &type);
101 
102     /** Search within a function body for non-builtin functions that a given function invocation depends on. */
103     void                discoverFunctionDependencies(const FunctionInvocation &invoc, FunctionVector &depVector);
104 
105     // Attributes.
106 protected:
107 
108     FunctionMap                 mFunctionCacheMap;              // Map function invocation to body.  Used as a cache to reduce library file reads and for inlining
109     StringMap                   mDefinesMap;                    // Map of #defines and the function library that contains them
110 
111     StringMap                   mCachedFunctionLibraries;       // Holds the cached function libraries
112 };
113 
114 /** GLSL ES program writer factory implementation.
115 @see ProgramWriterFactory
116 */
117 class ShaderProgramWriterGLSLESFactory : public ProgramWriterFactory
118 {
119 public:
ShaderProgramWriterGLSLESFactory()120     ShaderProgramWriterGLSLESFactory() : mLanguage("glsles")
121     {
122     }
~ShaderProgramWriterGLSLESFactory()123     virtual ~ShaderProgramWriterGLSLESFactory() {}
124 
125     /**
126     @see ProgramWriterFactory::getTargetLanguage
127     */
getTargetLanguage(void)128     virtual const String& getTargetLanguage(void) const
129     {
130         return mLanguage;
131     }
132 
133     /**
134     @see ProgramWriterFactory::create
135     */
create(void)136     virtual ProgramWriter* create(void)
137     {
138         return OGRE_NEW GLSLESProgramWriter();
139     }
140 
141 private:
142     String mLanguage;
143 };
144 
145 /** @} */
146 /** @} */
147 
148 }
149 }
150 
151 #endif
152