New ConstantBuffer System
Since the last time I’ve done a lot of changes to my D3D11 Game Framework. I completely reworked my GraphicsSystem. The GraphicsCoreSystem is now gone, this means not every call to the GraphicsSystem is a virtual method call, any longer.
I had implemented this CoreSystem to make it more platform independent. But I changed my mind, I don’t think that I will use this Framework on any different platform then Windows Vista or Windows 7. These OS both support D3D11 so I will not try to use this framework on XP or any other Platform.
I also switched the SDK Version from November to March, without the need for any changes in code. This also has changed since D3D9, fortunately:)
I also moved my Math Code from D3DX Math to XNA-Math, which was added in the November SDK.
But now to the new D3D11 Constant Buffer System, I implemented. The sample way(shown in the Samples of the D3D11 SDK) is to have one struct for each ConstantBuffer. This was also the way I had implemented it. But this doesn’t work when loading shaders dynamically, because at compile time we don’t know anything about the ConstantBuffers the Shaders will be using.
So I started to design a system, which I got working so far. I think it is a good starting point and new features can be implemented on top of it. So lets have a look how it works.
The System now is divided into 3 classes.
- ConstantBufferBuilder
- ConstantBuffer
- ConstantBufferElement
Before we start talking about the system, lets have a look at all the Code involved, as it’s easier to understand having seen the code.
GanbatteEngine::Graphics::ConstantBufferBuilder builder2(L"VSPerPassCB",L"PerPass");
builder2.Add<XMMATRIX>(L"View");
builder2.Add<XMMATRIX>(L"Projection");
graphicsDevice->CreateConstantBuffer(builder,&VSPerPassCB);
ConstantBufferElement* element = VSPerPassCB->GetElementByName(L"View");
element->SetDataSourcePtr(&view);
renderingContext->UpdateConstantBuffer(VSPerPassCB);
renderingContext->SetConstantBuffer(0,VSPerPassCB,VertexShaderStageBind | PixelShaderStageBind);
This is all the code necessary to create and use ConstantBuffers.
Firstly we create a ConstantBufferBuilder. Like the name says already it’s only purpose is to build ConstantBuffers. It takes the name of the ConstantBuffer and the frequency of updates for this buffer.
The creation of a ConstantBuffer was moved into the Builder because a ConstantBuffer is not allowed to change after it was created. To make it easier to create the ConstantBuffers at runtime dynamically the Builder was introduced.
To create a ConstantBuffer we just call “CreateConstantBuffer” and pass the builder into it.
After the ConstantBuffer is created we can get access to every element in the buffer by its name.
When we have the “ConstantBufferElement” we can just set the pointer on the data for this element. This currently is just working on void* pointers, so we are not typesafe in this place. Maybe I will change it at someday, to make it typesafe.
Now we have just 2 methods left, the first is “UpdateConstantBuffer” we just call it passing in the ConstantBuffer. This will copy the data of the ConstantBuffer into GPU memory.
And of course “SetConstantBuffer”, this will set the ConstantBuffer on the rendering context. We call the method with the input Slot(it will be set it on this slot), the ConstantBuffer to set and we define on which ShaderStage the ConstantBuffer will be set. Every ShaderShader Stage which is accessing the ConstantBuffer elements, needs the ConstantBuffer bound to it.
As you can see the system is quite simple but also powerful. I really like it. So lets get on improving the other parts of the EffectFramework.

