Jump to content
  • Advertisement
Sign in to follow this  

DX11 My First Constant Buffer, What's Wrong?

This topic is 832 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all,

I've starting moving from effects11 to managing things myself, also getting rid of the deprecated effects11 library.

First stop: constant buffers.


Here's what I'm trying to do:

- create constant buffers for the shader constants in my effect file

- replace the calls to SetMatrix, SetFloat etc. by updating the constant buffer and setting it to the device context


As far as I know this should be possible. If not, let me know :)

Basically this is sort of hybrid usage of managing things yourself and using effects11, but for me it's a first step.

Effects11 in the underlying code also calls PSSetConstantBuffer, VSSetConstantBuffer etc., I believe.


Here's my attempt for creating, updating and setting the constant buffer.

I get no compiler errors or warning during runtime, I just don't see the effect of the 'should be' update constant buffer.

When I just set the DirLights and EyePosW value through the effects library, the render is fine.

Any thoughts?

// the effect file
cbuffer cbPerFrame : register (b0)
	DirectionalLight gDirLights[3];
	float3 gEyePosW;

// buffer struct
  DirectionalLight DirLights[3]; // 3x64 bytes
  DirectX::XMFLOAT3 EyePosW; // 12 bytes
  float padA; // 4 bytes

// creating the buffer
	CB_PER_FRAME cBufferPerFrame;
	cBufferPerFrame.DirLights[0] = mDirLights[0];
	cBufferPerFrame.DirLights[1] = mDirLights[1];
	cBufferPerFrame.DirLights[2] = mDirLights[2];
	cBufferPerFrame.EyePosW = mEyePosW;
	D3D11_BUFFER_DESC cBufferDesc;
	cBufferDesc.ByteWidth = sizeof(CB_PER_FRAME);
	cBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
	cBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
	cBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
	cBufferDesc.MiscFlags = 0;
	cBufferDesc.StructureByteStride = 0;

	initData.pSysMem = &cBufferPerFrame;
	initData.SysMemPitch = 0;
	initData.SysMemSlicePitch = 0;

	// create the buffer
	mCBuffer = NULL;
	HR(md3dDevice->CreateBuffer(&cBufferDesc, &initData, &mCBuffer));

// updating and setting the buffer
	D3D11_MAPPED_SUBRESOURCE mappedResource;
	md3dImmediateContext->Map(mCBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);

	CB_PER_FRAME* dataPtr = (CB_PER_FRAME*)mappedResource.pData;

	dataPtr->DirLights[0] = mDirLights[0];
	dataPtr->DirLights[1] = mDirLights[1];
	dataPtr->DirLights[2] = mDirLights[2];
	dataPtr->EyePosW = mEyePosW;
	md3dImmediateContext->Unmap(mCBuffer, 0);
	md3dImmediateContext->PSSetConstantBuffers(0, 1, &mCBuffer);

// and 'the old way'
//	ID3DX11EffectVectorVariable	*mHEyePosW;
//	ID3DX11EffectVariable		*mHDirLights;

//	void SetEyePosW(const DirectX::XMFLOAT3& v)			{ mHEyePosW->SetRawValue(&v, 0, sizeof(DirectX::XMFLOAT3)); }
//	void SetDirLights(const DirectionalLight* lights)	{ mHDirLights->SetRawValue(lights, 0, 3*sizeof(DirectionalLight)); }

	// per frame constants
//	CD3d11Effects::mBasicFX->SetDirLights(mDirLights);		// CB PER FRAME
//	CD3d11Effects::mBasicFX->SetEyePosW(mEyePosW);			// CB PER FRAME

Edited by cozzie

Share this post

Link to post
Share on other sites
Problem solved, I was setting the CB's too early, the effects11 lib was overwriting them

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!