Scenegraph redesign and improving the draw 'loop'

Started by
2 comments, last by cozzie 10 years, 2 months ago

Hi,

I've decided to fully redesign my scenegraph/ renderqueue, meaning completely starting over from scratch.

Adding stuff on and on even makes it difficult for myself to understand the current approach and code.

New approach:

- make 2 'buckets' of renderables, opaque and blended

- simply loop through the buckets in my 2 main drawing functions

- the scenegraph will use the d3dscene class and it's objects and only saves ID's and other parameters dependent on actual rendering

First (pseudo) code:


typedef struct Q_Renderable
{
	unsigned int	Effect;
	unsigned int	Material;
	unsigned int	Mesh;
	unsigned int	Instance;
	unsigned int	Id;				// subset/ renderable of the mesh
	// bool			Visible;
}

class CSceneGraph
{
public:
	void Update(const Crealysm_d3drenderer::CD3dscene *pD3dscene);

	std::vector<Q_Renderable>	mOpaqueBucket;
	std::vector<Q_Renderable>	mBlendedBucket;

private:
	std::vector<Q_Mesh>		mMeshes;
	std::vector<Q_Material>	mMaterials;
	std::vector<Q_Instance>	mInstances;
	std::vector<Q_Light>	mLights;
	// etc., saves ID's, visibility state after culling, distances etc.
	
	// functions to update the renderables buckets, based on visibility, sorting, culling etc.
	// etc.
}

Then in the drawing function my approach is something like this:


bool CD3d::RenderSceneOpaque(const char *pTechnique, const CD3dcam &pCam)
{
	/** retrieve the 1st element from bucket and set all buffers/ shader constants **
	Q_Renderable last = mSceneGraph.mOpaqueBucket[0];
	
	ShaderSetTechnique(last.Effect, pTechnique);
	mD3dscene->mShaders[last.Effect].Begin();
	mD3dscene->ShaderSelectMaterial(last.Material, lastEffect);
	mD3dscene->mMeshes[last.Mesh].SetBuffers();
	mD3dscene->ShaderSetWorld(last.Instance, lastId);

	bool bucketDone = false;
	bool nextEffect = false;
	int renderable = 0;

	while(!bucketDone)
	{
		auto &next = mSceneGraph.mOpaqueBucket[renderable];
		if(next.Effect != last.Effect) 
		{
			mD3dscene->mShaders[last.Effect].End();

			ShaderSetTechnique(next.Effect, pTechnique);
			mD3dscene->mShaders[next.Effect].Begin();
			nextEffect = false;
		}

		for(unsigned int p=0;p<shader.GetNumPasses();++p)
		{
			mD3dscene->mShaders[next.Effect].BeginPass(p);

			if(next.Material	!= last.Material)	mD3dscene->ShaderSelectMaterial(next.Material, lastEffect);
			if(next.Mesh		!= last.Mesh)		mD3dscene->mMeshes[next.Mesh].SetBuffers();

			mD3dscene->ShaderSetWorld(next.Instance, next.Id);
			mD3dscene->mShaders[next.Effect].Commit();
			mD3dscene->mMeshes[next.Mesh].RenderSubMesh(next.Id, LIST);	// does the draw call
			mD3dscene->mShaders[next.Effect].EndPass(p);
		}
		last = mSceneGraph.mOpaqueBucket[renderable];	
		++renderable;

		if(renderable = (int)mSceneGraph.mOpaqueBucket.size()) bucketDone = true;
	}
}

(Nb. 1: I'm aware that this assumes all my effect files have a shared technique name)

(Nb. 2: I've left out error checking/ returning false to keep it readable for now)

My questions/ I'm curious about your opinion on:

1 - the approach itself

2 - although it might sound like 'pre optimization'/evil, would you store only visible renderables in the bucket, or do a 'if(visible)' check in the drawing loop? (more cpu activity per frame or maybe not). Only visibles gives a cleaner code draw loop for sure.

3 - what are your suggestions to 'clean' the somewhat ugly main drawing loop?

(it feels like it could be done easier and/or more efficiently)

Any input is appreciated.

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

Advertisement

I like the idea of buckets, however that is a renderer-specific thing, which should not fall in the scenegraph design really.

You should separate the scenegraph from rendering, the scenegraph is handling the scene hierarchy, culling, and so on.

Rendering is better to be separate from that process.

As a side-note, you may want to add render-order functionality to your engine, so that you can define the rendering order of different materials.

culling, and so on

It most certainly is not.
Scene Graphs

And here it is not implemented correctly. It is not a structure/class in itself. It is simply the fact that the most base class of all game entities has a parent-child relationship with itself. Meaning up to 1 parent and an array of children.


It is however correct to have 2 different render queues: One for opaques and one for translucents. But this is also unrelated to the scene graph. Read my link.

A scene manager has a list of scene objects as well as some form of spacial structure containing them all.
The scene objects between themselves create a scenegraph, apart from the scene manager/scene itself.
The scene manager uses as many render-queue pairs (solid/non-solid) instances as it needs (one for the main scene, one or more for each shadow-casting light, etc.) and handles culling and rendering (after the render queues sort) by itself on a high level.


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

Thanks both. Here's a bit more background on what I do at the moment:

- scene is loaded into a d3dscene object

- the d3dscene object has member vectors of d3dmesh, d3dmeshinstance, d3dlight, d3dmaterial etc. objects

- these objects keep all the properties of the meshes, instances, lights etc.

- then I have one 'scenegraph' object, which keeps the ID's, visibility state etc. of meshes, instances, materials, light etc.

(results of culling are also stored in there)

- based on this information, I 'rebuild' the two buckets, sorting etc.

The main render function uses the two buckets to render the frame.

Where the used ID's in the buckets match the ID's of the materials, meshes, instances etc. in the d3dscene object

Though I'm not sure if this would be the 'ideal' way to go, but it works quite fine and gives me the flexibility I need (up till now).

If I wanted to change rendering order or order things I just fill the buckets differently and keep my main draw function the same).

Besides the naming/ design, can you give me some pointers on how you would 'improve' the concept draw function?

(both and readabillity as efficiency). It doesn't quit feel çlean'.

update

@LSpiro; I've read the article, really clears things up. My conclusion is that up till now I've been only working on a renderqueue and not even have a scenegraph :)

To keep it clean I'll do some name changing in my engine codebase.

Really like to hear your opinions also on the draw function for rendering a bucket.
Below the improved version, with less 'data copying' which is not needed obvisously:


bool CD3d::RenderScene(const char *pTechnique, const CD3dcam &pCam, std::vector<Q_Renderable &pRenderBucket)
{
	/** retrieve the 1st element from bucket and do all bindings **
	Q_Renderable *last = &pRenderBucket[0];

	ShaderSetTechnique(last.Effect, pTechnique);
	mD3dscene->mShaders[last.Effect].Begin();
	mD3dscene->ShaderSelectMaterial(last.Material, lastEffect);
	mD3dscene->mMeshes[last.Mesh].SetBuffers();
	mD3dscene->ShaderSetWorld(last.Instance, lastId);
	
	bool bucketDone = false;
	int renderable = 0;

	while(!bucketDone)
	{
		auto &next = &pRenderBucket[renderable];
		if(next.Effect != last.Effect) 
		{
			mD3dscene->mShaders[last.Effect].End();

			ShaderSetTechnique(next.Effect, pTechnique);
			mD3dscene->mShaders[next.Effect].Begin();
		}

		for(unsigned int p=0;p<shader.GetNumPasses();++p)
		{
			mD3dscene->mShaders[next.Effect].BeginPass(p);

			if(next.Material	!= last.Material)	mD3dscene->ShaderSelectMaterial(next.Material, lastEffect);
			if(next.Mesh		!= last.Mesh)		mD3dscene->mMeshes[next.Mesh].SetBuffers();

			mD3dscene->ShaderSetWorld(next.Instance, next.Id);
			mD3dscene->mShaders[next.Effect].Commit();

			mD3dscene->mMeshes[next.Mesh].RenderSubMesh(next.Id, LIST);	// does the draw call
			mD3dscene->mShaders[next.Effect].EndPass(p);
		}
		last = &&pRenderBucket[renderable];	
		++renderable;

		if(renderable == (int)&pRenderBucket.size()) bucketDone = true;
	}
}

I've tried to get rid of doing the bindings of the first renderable outside the loop, but didn't manage this so far, because in that case I'll be trying to 'end' an effect which was not started at all with 'begin'.

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

This topic is closed to new replies.

Advertisement