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-2013 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 
32 namespace Ogre {
33 namespace RTShader {
34 
35 /** \addtogroup Core
36 *  @{
37 */
38 /** \addtogroup RTShader
39 *  @{
40 */
41 
42 /** GLSL ES target language writer implementation.
43 @see ProgramWriter.
44 */
45 class GLSLESProgramWriter : public ProgramWriter
46 {
47 	// Interface.
48 public:
49 
50 	/** Class constructor.
51 	*/
52 	GLSLESProgramWriter	();
53 
54 	/** Class destructor */
55 	virtual ~GLSLESProgramWriter	();
56 
57 
58 	/**
59 	@see ProgramWriter::writeSourceCode.
60 	*/
61 	virtual void            writeSourceCode			(std::ostream& os, Program* program);
62 
63 	/**
64 	@see ProgramWriter::getTargetLanguage.
65 	*/
getTargetLanguage()66 	virtual const String&   getTargetLanguage		() const { return TargetLanguage; }
67 
68 	static String TargetLanguage;
69 
70     protected:
71 	typedef		map<GpuConstantType, const char*>::type		GpuConstTypeToStringMap;
72 	typedef		map<Parameter::Semantic, const char*>::type	ParamSemanticToStringMap;
73 	typedef		map<Parameter::Content, const char*>	::type  ParamContentToStringMap;
74 	typedef		map<String, String>::type					StringMap;
75 	typedef		map<FunctionInvocation, String>::type		FunctionMap;
76 	typedef		vector<FunctionInvocation>::type            FunctionVector;
77     typedef     FunctionMap::const_iterator                 FunctionMapIterator;
78     typedef     FunctionVector::const_iterator              FunctionVectorIterator;
79     typedef     GpuConstTypeToStringMap::const_iterator     GpuConstTypeToStringMapIterator;
80 
81 	// Protected methods.
82 protected:
83 
84 	/** Initialize string maps. */
85 	void				initializeStringMaps		();
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 	/** Write a local parameter. */
98 	void				writeLocalParameter			(std::ostream& os, ParameterPtr parameter);
99 
100 	/** Write the input params of the function */
101 	void				writeInputParameters		(std::ostream& os, Function* function, GpuProgramType gpuType);
102 
103 	/** Write the output params of the function */
104 	void				writeOutParameters			(std::ostream& os, Function* function, GpuProgramType gpuType);
105 
106 	String processOperand(Operand op, GpuProgramType gpuType);
107 
108     /** Check if a string matches one of the GLSL ES basic types */
109     bool                isBasicType(String &type);
110 
111     /** Search within a function body for non-builtin functions that a given function invocation depends on. */
112     void                discoverFunctionDependencies(const FunctionInvocation &invoc, FunctionVector &depVector);
113 
114 	// Attributes.
115 protected:
116 	GpuConstTypeToStringMap		mGpuConstTypeMap;				// Map between GPU constant type to string value.
117 	ParamSemanticToStringMap	mParamSemanticMap;				// Map between parameter semantic to string value.
118 
119 	StringMap					mInputToGLStatesMap;			// Map parameter name to a new parameter name (sometimes renaming is required to match names between vertex and fragment shader)
120 	FunctionMap                 mFunctionCacheMap;              // Map function invocation to body.  Used as a cache to reduce library file reads and for inlining
121     StringMap                   mDefinesMap;                    // Map of #defines and the function library that contains them
122 	ParamContentToStringMap		mContentToPerVertexAttributes;	// Map parameter content to vertex attributes
123 	int							mGLSLVersion;					// Holds the current glsl es version
124 	StringVector				mFragInputParams;				// Holds the fragment input params
125     StringMap                   mCachedFunctionLibraries;       // Holds the cached function libraries
126 };
127 
128 /** GLSL ES program writer factory implementation.
129 @see ProgramWriterFactory
130 */
131 class ShaderProgramWriterGLSLESFactory : public ProgramWriterFactory
132 {
133 public:
ShaderProgramWriterGLSLESFactory()134 	ShaderProgramWriterGLSLESFactory() : mLanguage("glsles")
135 	{
136 	}
~ShaderProgramWriterGLSLESFactory()137 	virtual ~ShaderProgramWriterGLSLESFactory() {}
138 
139 	/**
140 	@see ProgramWriterFactory::getTargetLanguage
141 	*/
getTargetLanguage(void)142 	virtual const String& getTargetLanguage(void) const
143 	{
144 		return mLanguage;
145 	}
146 
147 	/**
148 	@see ProgramWriterFactory::create
149 	*/
create(void)150 	virtual ProgramWriter* create(void)
151 	{
152 		return OGRE_NEW GLSLESProgramWriter();
153 	}
154 
155 private:
156 	String mLanguage;
157 };
158 
159 /** @} */
160 /** @} */
161 
162 }
163 }
164 
165 #endif
166