Advertisement

Garbage Pointer When Setting Pipeline State

Started by September 13, 2019 09:18 AM
8 comments, last by Jman2 5 years, 4 months ago

Hello,

I have Emulated the PSO for DX11, i store Graphics Resources in a slot map essentially a wrapper aorund a vector and free list, when i call OMSetBlendState however, my pointer into the vector becomes garbage.

Here is the state before the call.

image.png.e5d998b50b07fb38ce4d98a497decf22.png

And After:

image.thumb.png.a976190f6e7911f89375236fca4ec693.png

See how suddenly the pointer to the pipelinestate is filled with garbage after the API call?

Here is the function as a whole:


void GraphicsDevice::SetPipelineState(PipelineStateHandle stateObject)
{
	D3D11::PipelineState* ptr = m_PipelineStates[stateObject];
	if (ptr)
	{
		ID3D11VertexShader* vs = ptr->m_VS;
		if (vs && vs != m_PrevVertexShader)
		{
			m_Context->VSSetShader(vs, nullptr, 0);
		}

		ID3D11PixelShader* ps = ptr->m_PS;
		if (ps && ps != m_PrevPixelShader)
		{
			m_Context->PSSetShader(ps, nullptr, 0);
		}

		ID3D11HullShader* hs = ptr->m_HS;
		if (hs && hs != m_PrevHullShader)
		{
			m_Context->HSSetShader(hs, nullptr, 0);
		}

		ID3D11DomainShader* ds = ptr->m_DS;
		if (ds && ds != m_PrevDomainShader)
		{
			m_Context->DSSetShader(ds, nullptr, 0);
		}

		ID3D11GeometryShader* gs = ptr->m_GS;
		if (gs && gs != m_PrevGeometryShader)
		{
			m_Context->GSSetShader(gs, nullptr, 0);
		}

		ID3D11BlendState* blendState = ptr->m_BlendState;
		if (blendState && blendState != m_PrevBlendState)
		{
			float bf[4] = { m_BlendFactor.R, m_BlendFactor.G, m_BlendFactor.B, m_BlendFactor.A };
			m_Context->OMSetBlendState(blendState, bf, ptr->m_SampleMask);
		}

		ID3D11DepthStencilState* depthState = ptr->m_DepthStencilState;
		if (depthState && depthState != m_PrevDepthStencilState)
		{
			m_Context->OMSetDepthStencilState(depthState, 0xFF);
		}

		ID3D11RasterizerState* rasterState = ptr->m_RasterizerState;
		if (rasterState && rasterState != m_PrevRasterizerState)
		{
			m_Context->RSSetState(rasterState);
		}

		ID3D11InputLayout* inputLayout = ptr->m_InputLayout;
		if (inputLayout && inputLayout != m_PrevInputLayout)
		{
			m_Context->IASetInputLayout(inputLayout);
		}

		if (ptr->m_PrimitiveTopologyType != m_PrevTopology)
		{
			m_Context->IASetPrimitiveTopology((D3D11_PRIMITIVE_TOPOLOGY)ptr->m_PrimitiveTopologyType);
		}
	}
}

Im not sure why heap would suddenly shift objects around when calling a dx11 api method.

Any advice would be greatly appreciated.
Thanks

Are you sure you're not doing something on another thread somewhere while you're calling this function?

Advertisement
20 hours ago, SoldierOfLight said:

Are you sure you're not doing something on another thread somewhere while you're calling this function?

 Yeah its only single threaded at the moment, for now i jsut cached all the dx11 interfaces at the start of the function as a work around. But it is very bizzare...

In the garbage data, the m_DS and m_GS pointed to some memory range of the d3d11_3SDKLayers.dll, that looks like some linkage errors. I've occurred such similar scenario (very rare) when the incremental linkage feature was activated and the linker can't resolve the segment offset correctly, then in debug runtime some weird memory addresses were pop up. Try to fully cleanly rebuild the solution and see if it's solved.

22 hours ago, zhangdoa said:

In the garbage data, the m_DS and m_GS pointed to some memory range of the d3d11_3SDKLayers.dll, that looks like some linkage errors. I've occurred such similar scenario (very rare) when the incremental linkage feature was activated and the linker can't resolve the segment offset correctly, then in debug runtime some weird memory addresses were pop up. Try to fully cleanly rebuild the solution and see if it's solved.

No success unfortunatly, its now spreading everywhere:

image.thumb.png.3ec49efcffae6f51a446413a09653fad.png

The Game class becomes null its not even a pointer, but calling functions on my graphics device which in tern uses dx11 complelty messes up memory all over the program. I will try linking using visual studio instead of #pragma coment etc. see if that fixes it. I have never had this happen in any of the engines ive built before 0.o

You might want to try taking a time travel debugging capture of your app. Using that, you can find exactly where your data is getting corrupted. If you do take one, and want to send the trace to me, I can also help you debug it.

Advertisement

Okay so the Second issue is actually fixed, it was a memory copy error because i elft an & from when it was a vector. The origional issue is still an "issue" although i just chached everything up front for now to avoid loosing the pointer when i call the d3d function.

Okay, so i think the entire issue is probably related to the handle based approach im using. I wanted to give it a go and have all memory within the GraphicsDevice and the user just gets handles but clealry its just a nightmare.

https://github.com/James-Emmett/CraftEngine/blob/master/Include/System/Containers/SlotMap.h

The above is the Data Structure i created to store memory and generations etc, its set up like this:


	//--Graphics Memory--
	SlotMap<D3D11::Buffer>        m_VertexBuffers   = SlotMap<D3D11::Buffer>(2048);
	SlotMap<D3D11::IndexBuffer>   m_IndexBuffers    = SlotMap<D3D11::IndexBuffer>(2048);
	SlotMap<D3D11::Buffer>        m_ConstantBuffers = SlotMap<D3D11::Buffer>(512);
	SlotMap<D3D11::Texture>       m_TextureBuffers  = SlotMap<D3D11::Texture>(2048);
	SlotMap<D3D11::RenderTarget>  m_RenderTargets   = SlotMap<D3D11::RenderTarget>(128);
	SlotMap<D3D11::DepthTarget>   m_DepthTargets    = SlotMap<D3D11::DepthTarget>(128);
	SlotMap<D3D11::PipelineState> m_PipelineStates  = SlotMap<D3D11::PipelineState>(512);

So everything is in one place, i define objects like this:


	struct Buffer
	{
		ID3D11Buffer* m_Buffer = nullptr;
		Uint32 m_ByteWidth     = 0;
		Uint32 m_Stride        = 0;
		Uint32 m_ElementCount  = 0;
		bool   m_IsDynamic     = false;

		void Release()
		{
			if (m_Buffer) { m_Buffer->Release(); }
		}
	};

Everytime i fetch memory it looks fine, when i use it everything just fails, DirectX states that memory has been "reclaimed" even though i havent called relase, and i hold the pointer in the buffer object etc.

The memory in this soloution is just so shacky that i think i might go back to a OOP XNA like soloution and abandon handles as its just such a nightmare to get it to function.

If anyone knows why this is failed Everywhere please let me know, in the meantime im rebuilding the entire thing as oop...

Thanks,

Okay i solved it, the slot map was returning a pointer to the local object urgg i feel so silly. Sometimes its like searching for a needle in a haystack, Thanks everyone for your input.

This topic is closed to new replies.

Advertisement