Direct3D 12 : Please help me for invalid parameters in relation to a constant buffer creation

Started by
3 comments, last by MJP 2 years, 8 months ago

Hi !

I am trying to create a constant buffer for my shader and the variable “constantBufferUploadHeap” is nullptr after the "CreateCommittedResource" instruction with an error for “invalid parameter(s)”. I don't know if it comes from the alignment of data and if I have to add padding or not.

Here is my code :

struct VS_CONSTANT_BUFFER_G_BUFFER
{
	XMFLOAT4X4A viewWorld;
	XMFLOAT4X4A projViewWorld;
	bool normalMappingEnabled;
	bool padding[127];//in order to align data to 256 bytes
};
D3D12_HEAP_PROPERTIES heapProperties;
	heapProperties.Type = D3D12_HEAP_TYPE_UPLOAD;
	heapProperties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
	heapProperties.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
	heapProperties.CreationNodeMask = 1;
	heapProperties.VisibleNodeMask = 1;

	D3D12_RESOURCE_DESC resourceDesc;
	resourceDesc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
	resourceDesc.Alignment = D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT;// D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT;// ;
	resourceDesc.Width = sizeof(VS_CONSTANT_BUFFER_G_BUFFER);
	resourceDesc.Height = 1;
	resourceDesc.DepthOrArraySize = 1;
	resourceDesc.MipLevels = 1;
	resourceDesc.Format = DXGI_FORMAT_UNKNOWN;
	resourceDesc.SampleDesc.Count = 1;
	resourceDesc.SampleDesc.Quality = 0;
	resourceDesc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
	resourceDesc.Flags = D3D12_RESOURCE_FLAG_NONE;

	HRESULT hr = deviceDX12->CreateCommittedResource(
		&heapProperties, // this heap will be used to upload the constant buffer data
		D3D12_HEAP_FLAG_NONE, // no flags
		&resourceDesc, // size of the resource heap. Must be a multiple of 64KB for single-textures and constant buffers
		D3D12_RESOURCE_STATE_GENERIC_READ, // will be data that is read from so we keep it in the generic read state
		nullptr, // we do not have use an optimized clear value for constant buffers
		IID_PPV_ARGS(&constantBufferUploadHeap));

	HRESULT hr2 = deviceDX12->GetDeviceRemovedReason();

	if (FAILED(hr))
		cout << "CreateCommittedResource failed " << endl;

	constantBufferUploadHeap->SetName(L"Constant Buffer Upload Resource Heap");

	D3D12_CONSTANT_BUFFER_VIEW_DESC constantBufferGBufferMatrices;
	constantBufferGBufferMatrices.BufferLocation = constantBufferUploadHeap->GetGPUVirtualAddress();
	constantBufferGBufferMatrices.SizeInBytes = sizeof(VS_CONSTANT_BUFFER_G_BUFFER);
	deviceDX12->CreateConstantBufferView(&constantBufferGBufferMatrices, mainDescriptorHeap->GetCPUDescriptorHandleForHeapStart());
Advertisement

I no longer need help, I found the solution.

@theScore Please give your solution. Others cannot learn if you don't tell us what went wrong and how you corrected it.

It looks like the issue is the Alignment member of the D3D12_RESOURCE_DESC struct, which needs to be either 0 or D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT (which doesn't really matter for a committed resource, that's more for placed resources). D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT is for sub-allocating a constant buffer view from a larger resource. Either way I would recommend enabling the debug layer through ID3D12Debug::EnableDebugLayer, since that will tell you why the call to CreateCommittedResource failed.

Another bit of feedback: I would be very careful about using the C++ “bool” type in structs that are meant to mirror your HLSL constant buffer layout. bool is 4 bytes in HLSL and 1 byte in C++.

This topic is closed to new replies.

Advertisement