Pārlūkot izejas kodu

The material will calculate the uniform properties. Don't query GL

Panagiotis Christopoulos Charitos 11 gadi atpakaļ
vecāks
revīzija
fd111038b7

+ 3 - 0
include/anki/resource/Material.h

@@ -99,6 +99,9 @@ private:
 	/// Keep one program variable here for easy access of the common
 	/// variable stuff like name or GL data type etc
 	const GlProgramVariable* m_progVar;
+	I16 m_offset = -1;
+	I16 m_arrayStride = -1;
+	I16 m_matrixStride = -1;
 
 	Bool8 m_instanced;
 };

+ 24 - 2
include/anki/resource/MaterialProgramCreator.h

@@ -35,8 +35,8 @@ public:
 		MPString m_name;
 		MPString m_type;
 		MPStringList m_value;
-		Bool8 m_constant;
-		U16 m_arraySize;
+		Bool8 m_constant = false;
+		U16 m_arraySize = 0;
 		Bool8 m_instanced = false;
 
 		MPString m_line;
@@ -44,6 +44,13 @@ public:
 		GLbitfield m_shaderReferencedMask = 0; ///< Referenced by
 		Bool8 m_inBlock = true;
 
+		I16 m_binding = -1; ///< Texture unit
+		I32 m_offset = -1; ///< Offset inside the UBO
+		I32 m_arrayStride = -1;
+		/// Identifying the stride between columns of a column-major matrix or 
+		/// rows of a row-major matrix
+		I32 m_matrixStride = -1;
+
 		Input()
 		{}
 
@@ -79,6 +86,20 @@ public:
 			m_shaderDefinedMask = b.m_shaderDefinedMask;
 			m_shaderReferencedMask = b.m_shaderReferencedMask;
 			m_inBlock = b.m_inBlock;
+			m_binding = b.m_binding;
+			m_offset = b.m_offset;
+			m_arrayStride = b.m_arrayStride;
+			m_matrixStride = b.m_matrixStride;
+		}
+
+		Bool duplicate(const Input& b) const
+		{
+			return b.m_name == m_name
+				&& b.m_type == m_type
+				&& b.m_value == m_value
+				&& b.m_constant == m_constant
+				&& b.m_arraySize == m_arraySize
+				&& b.m_instanced == m_instanced;
 		}
 	};
 
@@ -115,6 +136,7 @@ private:
 	List<Input, TempResourceAllocator<U8>> m_inputs;
 	MPStringList m_uniformBlock;
 	GLbitfield m_uniformBlockReferencedMask = 0;
+	U32 m_blockSize = 0;
 	Bool8 m_instanced = false;
 	U32 m_texBinding = 0;
 	GLbitfield m_instanceIdMask = 0;

+ 27 - 6
src/resource/MaterialProgramCreator.cpp

@@ -367,11 +367,7 @@ Error MaterialProgramCreator::parseInputsTag(const XmlElement& programEl)
 		if(duplicateInp != nullptr)
 		{
 			// Duplicate. Make sure it's the same as the other shader
-			Bool same = duplicateInp->m_type == inpvar.m_type
-				|| duplicateInp->m_value == inpvar.m_value
-				|| duplicateInp->m_constant == inpvar.m_constant
-				|| duplicateInp->m_arraySize == inpvar.m_arraySize
-				|| duplicateInp->m_instanced == inpvar.m_instanced;
+			Bool same = duplicateInp->duplicate(inpvar);
 
 			if(!same)
 			{
@@ -425,15 +421,18 @@ Error MaterialProgramCreator::parseInputsTag(const XmlElement& programEl)
 
 				ANKI_CHECK(tmp.sprintf(
 					m_alloc, "layout(binding = %u) uniform %s",
-					m_texBinding++, &inpvar.m_line[0]));
+					m_texBinding, &inpvar.m_line[0]));
 
 				inpvar.m_line.destroy(m_alloc);
 				inpvar.m_line = std::move(tmp);
 
 				inpvar.m_inBlock = false;
+				inpvar.m_binding = m_texBinding;
+				++m_texBinding;
 			}
 			else
 			{
+				// In block
 				MPString tmp;
 
 				ANKI_CHECK(tmp.create(m_alloc, inpvar.m_line));
@@ -442,6 +441,28 @@ Error MaterialProgramCreator::parseInputsTag(const XmlElement& programEl)
 
 				m_uniformBlockReferencedMask |= glshaderbit;
 				inpvar.m_inBlock = true;
+
+				inpvar.m_offset = m_blockSize;
+				if(inpvar.m_type == "float" 
+					|| inpvar.m_type == "vec2"
+					|| inpvar.m_type == "vec3"
+					|| inpvar.m_type == "vec4")
+				{
+					inpvar.m_arrayStride = sizeof(Vec4);
+					m_blockSize += sizeof(Vec4) * inpvar.m_arraySize;
+				}
+				else if(inpvar.m_type == "mat3"
+					|| inpvar.m_type == "mat4")
+				{
+					inpvar.m_arrayStride = sizeof(Mat4);
+					m_blockSize += sizeof(Mat4) * inpvar.m_arraySize;
+					inpvar.m_matrixStride = sizeof(Vec4);				
+				}
+				else
+				{
+					ANKI_LOGE("Unsupported type %s", &inpvar.m_type[0]);
+					return ErrorCode::USER_DATA;
+				}
 			}
 		}
 		else