Browse Source

Removing exceptions

Panagiotis Christopoulos Charitos 11 years ago
parent
commit
79bfcb89c0
3 changed files with 86 additions and 26 deletions
  1. 7 7
      include/anki/scene/ModelNode.h
  2. 22 0
      include/anki/util/DArray.h
  3. 57 19
      src/scene/ModelNode.cpp

+ 7 - 7
include/anki/scene/ModelNode.h

@@ -29,17 +29,17 @@ class ModelPatchNode: public SceneNode,
 	friend class ModelNode;
 
 public:
-	ModelPatchNode(
-		const CString& name, SceneGraph* scene, // Scene
-		const ModelPatchBase* modelPatch); // Self
+	ModelPatchNode(SceneGraph* scene, const ModelPatchBase* modelPatch);
 
 	~ModelPatchNode();
 
+	ANKI_USE_RESULT Error create(const CString& name);
+
 	/// @name RenderComponent virtuals
 	/// @{
 
 	/// Implements RenderComponent::buildRendering
-	void buildRendering(RenderingBuildData& data);
+	ANKI_USE_RESULT Error buildRendering(RenderingBuildData& data);
 
 	/// Implements  RenderComponent::getMaterial
 	const Material& getMaterial()
@@ -71,10 +71,10 @@ public:
 private:
 	Obb m_obb; ///< In world space
 	const ModelPatchBase* m_modelPatch; ///< The resource
-	SceneVector<ObbSpatialComponent*> m_spatials;
+	SceneDArray<ObbSpatialComponent*> m_spatials;
 
-	void updateInstanceSpatials(
-		const SceneFrameVector<MoveComponent*>& instanceMoves);
+	ANKI_USE_RESULT Error updateInstanceSpatials(
+		const SceneFrameDArray<MoveComponent*>& instanceMoves);
 };
 
 /// The model scene node

+ 22 - 0
include/anki/util/DArray.h

@@ -179,6 +179,28 @@ public:
 		return err;
 	}
 
+	/// Grow the array.
+	ANKI_USE_RESULT Error resize(Allocator alloc, PtrSize size)
+	{
+		ANKI_ASSERT(size > 0);
+		DArray newArr;
+		Error err = newArr.create(alloc, size);
+
+		if(!err)
+		{
+			U minSize = std::min(size, m_size);
+			for(U i = 0; i < minSize; i++)
+			{
+				newArr[i] = (*this)[i];
+			}
+
+			destroy(alloc);
+			move(newArr);
+		}
+
+		return err;
+	}
+
 	/// Destroy the array.
 	void destroy(Allocator alloc)
 	{

+ 57 - 19
src/scene/ModelNode.cpp

@@ -19,26 +19,38 @@ namespace anki {
 
 //==============================================================================
 ModelPatchNode::ModelPatchNode(
-	const CString& name, SceneGraph* scene,
+	SceneGraph* scene,
 	const ModelPatchBase* modelPatch)
-:	SceneNode(name, scene),
+:	SceneNode(scene),
 	RenderComponent(this),
 	SpatialComponent(this), 
-	m_modelPatch(modelPatch),
-	m_spatials(getSceneAllocator())
+	m_modelPatch(modelPatch)
+{}
+
+//==============================================================================
+Error ModelPatchNode::create(const CString& name)
 {
-	addComponent(static_cast<RenderComponent*>(this));
-	addComponent(static_cast<SpatialComponent*>(this));
+	err = SceneNode::create(name, 2);
+
+	if(!err)
+	{
+		addComponent(static_cast<RenderComponent*>(this));
+		addComponent(static_cast<SpatialComponent*>(this));
 
-	RenderComponent::init();
+		err = RenderComponent::create();
+	}
+
+	return err;
 }
 
 //==============================================================================
 ModelPatchNode::~ModelPatchNode()
-{}
+{
+	m_spatials.destroy(getSceneAllocator());
+}
 
 //==============================================================================
-void ModelPatchNode::buildRendering(RenderingBuildData& data)
+Error ModelPatchNode::buildRendering(RenderingBuildData& data)
 {
 	// That will not work on multi-draw and instanced at the same time. Make
 	// sure that there is no multi-draw anywhere
@@ -53,26 +65,36 @@ void ModelPatchNode::buildRendering(RenderingBuildData& data)
 	GlCommandBufferHandle vertJobs;
 	GlProgramPipelineHandle ppline;
 
-	m_modelPatch->getRenderingDataSub(
+	Error err = m_modelPatch->getRenderingDataSub(
 		data.m_key, vertJobs, ppline, 
 		nullptr, 0,
 		indicesCountArray, indicesOffsetArray, drawcallCount);
+	if(err)
+	{
+		return err;
+	}
 
 	// Cannot accept multi-draw
 	ANKI_ASSERT(drawcallCount == 1);
 
 	// Set jobs
 	ppline.bind(data.m_jobs);
-	data.m_jobs.pushBackOtherCommandBuffer(vertJobs);
+	err = data.m_jobs.pushBackOtherCommandBuffer(vertJobs);
+	if(err)
+	{
+		return err;
+	}
 	
 	// Drawcall
 	U32 offset = indicesOffsetArray[0] / sizeof(U16);
-	data.m_jobs.drawElements(
+	err = data.m_jobs.drawElements(
 		data.m_key.m_tessellation ? GL_PATCHES : GL_TRIANGLES,
 		sizeof(U16),
 		indicesCountArray[0],
 		instancesCount,
 		offset);
+
+	return err;
 }
 
 //==============================================================================
@@ -101,27 +123,41 @@ void ModelPatchNode::getRenderWorldTransform(U index, Transform& trf)
 }
 
 //==============================================================================
-void ModelPatchNode::updateInstanceSpatials(
-	const SceneFrameVector<MoveComponent*>& instanceMoves)
+Error ModelPatchNode::updateInstanceSpatials(
+	const SceneFrameDArray<MoveComponent*>& instanceMoves)
 {
+	Error err = ErrorCode::NONE;
 	Bool fullUpdate = false;
 
-	if(m_spatials.size() < instanceMoves.size())
+	const U oldSize = m_spatials.getSize();
+	const U newSize = instanceMoves.getSize();
+
+	if(oldSize < newSize)
 	{
 		// We need to add spatials
 		
 		fullUpdate = true;
+		
+		err = m_spatials.resize(getSceneAllocator(), newSize);
+		if(err)
+		{
+			return err;
+		}
 
-		U diff = instanceMoves.size() - m_spatials.size();
-
+		U diff = newSize - oldSize;
+		U index = oldSize;
 		while(diff-- != 0)
 		{
 			ObbSpatialComponent* newSpatial = getSceneAllocator().
 				newInstance<ObbSpatialComponent>(this);
+			if(newSpatial == nullptr)
+			{
+				return ErrorCode::OUT_OF_MEMORY;
+			}
 
 			addComponent(newSpatial);
 
-			m_spatials.push_back(newSpatial);
+			m_spatials[index++] = newSpatial;
 		}
 	}
 	else if(m_spatials.size() > instanceMoves.size())
@@ -134,7 +170,7 @@ void ModelPatchNode::updateInstanceSpatials(
 		ANKI_ASSERT(0 && "TODO");
 	}
 
-	U count = instanceMoves.size();
+	U count = newSize;
 	while(count-- != 0)
 	{
 		ObbSpatialComponent& sp = *m_spatials[count];
@@ -148,6 +184,8 @@ void ModelPatchNode::updateInstanceSpatials(
 			sp.markForUpdate();
 		}
 	}
+
+	return err;
 }
 
 //==============================================================================