Browse Source

- Finalizing the new material format
- Modifying the material code WIP

Wont compiley

Panagiotis Christopoulos Charitos 14 years ago
parent
commit
12d07c61d6

+ 16 - 8
anki/resource/Material.h

@@ -40,7 +40,7 @@ class ShaderProgram;
 ///
 /// 	[<levelsOfDetail>0 to N</levelsOfDetail>]
 ///
-/// 	[<castsShadow>true | false</castsShadow>]
+/// 	[<shadow>true | false</shadow>]
 ///
 /// 	[<blendFunctions> (2)
 /// 		<sFactor>GL_SOMETHING</sFactor>
@@ -53,21 +53,21 @@ class ShaderProgram;
 ///
 /// 	<shaderProgram>
 /// 		<vertexShader>
-/// 			<includes>
-/// 				<include>file.glsl</include>
-/// 				<include>file2.glsl</include>
+/// 			<includes> (5)
+/// 				<functionsFile>file.glsl</functionsFile>
+/// 				<codeFile>file2.glsl</codeFile>
 /// 			</includes>
 ///
-/// 			<inputs>
+/// 			[<inputs> (3)
 /// 				<input>
 /// 					<name>xx</name>
 /// 					<type>any_glsl_accepted_type</type>
-/// 					[<value> (3)
+/// 					[<value> (4)
 /// 						a_series_of_numbers |
 /// 						path/to/image.tga
 /// 					</value>]
 /// 				</input>
-/// 			</inputs>
+/// 			</inputs>]
 ///
 /// 			<operations>
 /// 				<operation>
@@ -86,8 +86,16 @@ class ShaderProgram;
 /// </material>
 /// @endcode
 /// (1): For the moment 0 means MS, 1 BS, 2 IS (aka light)
+///
 /// (2): Not relevant for light materials at the moment
-/// (3): The <value> tag is not present for build-in variables
+///
+/// (3): AKA uniforms
+///
+/// (4): The <value> tag is not present for build-in variables
+///
+/// (5): functionsFile means that the file contains only function declarations
+/// and the parser will expect only those. The codeFile means that the file
+/// may contain anything and will not get parsed
 class Material: public MaterialProperties
 {
 	public:

+ 85 - 2
anki/resource/MaterialShaderProgramCreator.cpp

@@ -83,7 +83,9 @@ MaterialShaderProgramCreator::~MaterialShaderProgramCreator()
 // parseShaderFileForFunctionDefinitions                                       =
 //==============================================================================
 void MaterialShaderProgramCreator::parseShaderFileForFunctionDefinitions(
-	const char* filename)
+	const char* filename,
+	boost::ptr_vector<FuncDefinition>& funcDefs,
+	ConstCharPtrHashMap<FuncDefinition*>::Type& funcNameToDef)
 {
 	scanner::Scanner scanner(filename, false);
 	const scanner::Token* token = &scanner.getCrntToken();
@@ -331,6 +333,86 @@ void MaterialShaderProgramCreator::getNextTokenAndSkipNewlines(
 }
 
 
+//==============================================================================
+void MaterialShaderProgramCreator::parseShaderTag(
+	const boost::property_tree::ptree& pt)
+{
+	//
+	// <includes></includes>
+	//
+	boost::ptr_vector<FuncDefinition> funcDefs;
+	ConstCharPtrHashMap<FuncDefinition*>::Type funcNameToDef;
+	std::vector<std::string> includeLines;
+
+	const ptree& includesPt = pt.get_child("includes");
+	BOOST_FOREACH(const ptree::value_type& v, includesPt)
+	{
+		if(v.first != "include")
+		{
+			throw ANKI_EXCEPTION("Expected include and not: " + v.first);
+		}
+
+		const std::string& fname = v.second.data();
+		parseShaderFileForFunctionDefinitions(fname.c_str(),
+			funcDefs, funcNameToDef);
+
+		includeLines.push_back("#pragma anki include \"" + fname + "\"");
+	}
+
+	std::sort(includeLines.begin(), includeLines.end(), compareStrings);
+	srcLines.insert(srcLines.end(), includeLines.begin(), includeLines.end());
+
+	//
+	// <inputs></inputs>
+	//
+
+	// Store the source of the uniform vars
+	std::vector<std::string> uniformsLines;
+
+	boost::optional<const ptree&> insPt = pt.get_child_optional("inputs");
+	if(insPt)
+	{
+		BOOST_FOREACH(const ptree::value_type& v, insPt.get())
+		{
+			if(v.first != "input")
+			{
+				throw ANKI_EXCEPTION("Expected <input> and not: " + v.first);
+			}
+
+			const ptree& inPt = v.second;
+			std::string line;
+			parseInputTag(inPt, line);
+			uniformsLines.push_back(line);
+		} // end for all ins
+
+		srcLines.push_back("");
+		std::sort(uniformsLines.begin(), uniformsLines.end(), compareStrings);
+		srcLines.insert(srcLines.end(), uniformsLines.begin(),
+			uniformsLines.end());
+	}
+
+	//
+	// <operations></operations>
+	//
+	srcLines.push_back("\nvoid main()\n{");
+
+	const ptree& opsPt = pt.get_child("operations");
+
+	BOOST_FOREACH(const ptree::value_type& v, opsPt)
+	{
+		if(v.first != "operation")
+		{
+			throw ANKI_EXCEPTION("Expected operation and not: " + v.first);
+		}
+
+		const ptree& opPt = v.second;
+		parseOperatorTag(opPt);
+	} // end for all operations
+
+	srcLines.push_back("}\n");
+}
+
+
 //==============================================================================
 // parseShaderProgramTag                                                       =
 //==============================================================================
@@ -455,6 +537,7 @@ void MaterialShaderProgramCreator::parseInputTag(
 	using namespace boost::property_tree;
 
 	const std::string& name = pt.get<std::string>("name");
+	const std::string& type = pt.get<std::string>("type");
 	boost::optional<const ptree&> valuePt = pt.get_child_optional("value");
 	GLenum glType;
 
@@ -502,7 +585,7 @@ void MaterialShaderProgramCreator::parseInputTag(
 	{
 		if(valuePt.get().size() != 1)
 		{
-			throw ANKI_EXCEPTION("Bad value for in: " + name);
+			throw ANKI_EXCEPTION("Bad <value> for <input>: " + name);
 		}
 
 		const ptree::value_type& v = valuePt.get().front();

+ 11 - 7
anki/resource/MaterialShaderProgramCreator.h

@@ -96,12 +96,6 @@ class MaterialShaderProgramCreator
 		bool usingTangentAttrib;
 		/// @}
 
-		/// Container that holds the function definitions
-		boost::ptr_vector<FuncDefinition> funcDefs;
-
-		/// Go from function name to function definition
-		ConstCharPtrHashMap<FuncDefinition*>::Type funcNameToDef;
-
 		/// The lines of the shader program source
 		std::vector<std::string> srcLines;
 
@@ -113,7 +107,12 @@ class MaterialShaderProgramCreator
 
 		/// Parses a glsl file for function definitions. It appends the
 		/// funcDefs container with new function definitions
-		void parseShaderFileForFunctionDefinitions(const char* filename);
+		/// @param[out] funcDefs Container that holds the function definitions
+		/// @param[out] funcNameToDef Go from function name to function
+		/// definition
+		void parseShaderFileForFunctionDefinitions(const char* filename,
+			boost::ptr_vector<FuncDefinition>& funcDefs,
+			ConstCharPtrHashMap<FuncDefinition*>::Type& funcNameToDef);
 
 		/// Used by parseShaderFileForFunctionDefinitions to skip preprocessor
 		/// definitions. Takes into account the backslashes. For example for
@@ -136,6 +135,11 @@ class MaterialShaderProgramCreator
 		/// @code <shaderProgram></shaderProgram> @endcode
 		void parseShaderProgramTag(const boost::property_tree::ptree& pt);
 
+		/// Parse what is within the
+		/// @code <*Shader></*Shader> @endcode where * may be fragment, vertex
+		/// etc
+		void parseShaderTag(const boost::property_tree::ptree& pt);
+
 		/// Parse what is within the @code <input></input> @endcode
 		void parseInputTag(const boost::property_tree::ptree& pt,
 			std::string& line);