3 Replies - 598 Views - Last Post: 03 January 2016 - 03:51 AM Rate Topic: -----

#1 nowayout2k  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 02-January 16

Trouble with DX11 Engine

Posted 02 January 2016 - 04:51 PM

I am following a tutorial for building a DX11Engine on youtube and I have become stuck. I am trying to render a sprite to the screen but when I try to run it I am getting

"Access violation reading location 0x00000000" because of the device being NULL.

The problem is occurring at the line:

result = device->CreateVertexShader(vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), NULL, &m_vertexShader);


Which is in Shader.cpp.

I can not figure out why the device is NULL.

I have uploaded my code on to Git Hub https://github.com/n...-CPP-DX11Engine . Thanks for your help in advance!

Is This A Good Question/Topic? 0
  • +

Replies To: Trouble with DX11 Engine

#2 BBeck  Icon User is offline

  • Here to help.
  • member icon


Reputation: 792
  • View blog
  • Posts: 1,886
  • Joined: 24-April 12

Re: Trouble with DX11 Engine

Posted 02 January 2016 - 09:35 PM

DX11 is a bit finicky about that stuff. There's a couple "gotchas" that I've encountered right there.

First, you can compile a shader at run time or precompile it. However, there's some problems I've encountered, especially precompiling it. I'll post my working code that works on my machine. But one of the catches there is that my machine is a Windows7 machine that has both the DX11 SDK and the Windows SDK installed. So, there's really two different versions of DX11 on my machine which is necessary for DX11 on Win7 especially when using deprecated functionality, which may be something close to what you've got going on.

Anyway, compiling at run time works great for me on this machine. I tried it on a machine that had the Windows SDK but not the DX11 SDK and it would not compile.

There's some deprecated functionality related to compiling shaders. They don't want you using anything older than Win10 and they don't want you using old versions of DX. So, it's not really backwards compatible out of the box.

I'm guessing that in your case vertexShaderBuffer returned NULL when you tried to compile because it failed to compile.

Notice I use "if(SUCCEEDED". There's a way to capture the return value and give an error message instead. However, I believe this is all deprecated functionality.

The full source code for my DX11 engine project is on my website. It's a very basic 3D engine, but as you may have figured out, 2D in DX11 is in 3D. So, it may be easier to learn 3D than 2D. Certainly, understanding the basics of 3D will be helpful, especially with matrices.


//=====================================================================================================================
//  ShaderClass::Initialize()
//
//	Purpose: 
//		To setup our shaders so that we can use them to draw to the screen.
//
//	Input:
//		ID3D11Device* GraphicsDevice - DirectX device COM interface
//		ID3D11DeviceContext* GraphicsDeviceContext - DirectX device context COM interface
//
//	Output:
//		bool - The shaders compiled and were setup properly if true.
//
//  Notes:
//		This is about half the code here. It takes the source code for the two shaders and compiles them. Then it tells DX
//	about them so that DX can use them. It then defines a vertex format. Then it defines two constant buffers to handle the
//	data that we are sending to these two shaders every frame. And it attaches all this to the graphic pipeline (graphics card)
//	to be used for drawing. You're pretty much ready to draw at that point.
//
//		The vertex format describes the "input layout". In other words it describes the structure of every vertex going in to
//	the vertex buffer. In our case, for this shader, we are going to define a vertex as a position, a texture coordinate,
//	a normal to describe what direction the vertex "faces", and a color for the vertex. Position is the only thing required
//	in order to have a vertex. We are not using textures yet, but I decided to throw it in for consistancy which is really
//	a waste of memory, but we'll want to include textures 99.9% of the time once we start doing them. Color might be useless
//	at that point since you'll take the color from the texture unless you want to do some sort of fancy trick with the color
//	(like make a black and white texture and use the vertex colors to control what color is in the white area without additional
//	textures). This is more of an example than anything, and you may want to optimize your vertex format to only include data
//	you actually use in the shader. If the shader doesn't care, then it's a waste to include it, but starting out including 
//	all four will be easier for now.
//
//		Each vertex element has a color format that describes how the data is laid out in that single element. The vertex buffer
//	is just a very long row of bytes that is not strongly typed at all. This vertex definition is what is typing it. So, you
//	have to break each element up into it's proper bytes. RGB is Red, Green, and Blue which can be combined to form any color.
//	In this case Position is defined as three 32 bit numbers. The colors there are totally irrelevant but it divides it nicely
//	into three 32 bit elements (X, Y, and Z). The Texture Coordinate is two elements (U & V). The normal is a 3D vector and
//	contains the X, Y, and Z values of the position of the arrow head. Oh hey! We actually have a color element! Go figure.
//	The vertex color is an RGBA which stands for red, green, blue, alpha. Alpha is transparency and represents what percentage
//	of the vertex color to use and what percentage to blend in with the background color to make it transparent. There are
//	different methods for representing RGBA and these are all 32 bit floating values where max value is 100% and min value is 0%.
//	By mixing the 3 colors you can create any imaginable color.
//
//=====================================================================================================================
bool ShaderClass::Initialize(ID3D11Device* GraphicsDevice, ID3D11DeviceContext* GraphicsDeviceContext)
{
	bool ShaderCompiledAndSetupProperly = false;	//Assume we've failed until we succeed.
    ID3D10Blob *VS, *PS;
	ID3DBlob *errMsg = NULL;

	
	if(SUCCEEDED(D3DX11CompileFromFile("PhongShader.hlsl", 0, 0, "VertexShaderMain", "vs_5_0", 0, 0, 0, &VS, 0, 0)))
	{
		if(SUCCEEDED(D3DX11CompileFromFile("PhongShader.hlsl", 0, 0, "PixelShaderMain", "ps_5_0", 0, 0, 0, &PS, 0, 0)))
		{ 
			if(SUCCEEDED(GraphicsDevice->CreateVertexShader(VS->GetBufferPointer(), VS->GetBufferSize(), NULL, &VertexShader)))
			{
				if(SUCCEEDED(GraphicsDevice->CreatePixelShader(PS->GetBufferPointer(), PS->GetBufferSize(), NULL, &PixelShader)))
				{
					// set the shader objects that will be used to render.
					GraphicsDeviceContext->VSSetShader(VertexShader, 0, 0);
					GraphicsDeviceContext->PSSetShader(PixelShader, 0, 0);

					// create the input layout object (vertex layout for the vertex buffer).
					D3D11_INPUT_ELEMENT_DESC InputElementDescription[] =
					{
						{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},		//Begins at offset 0 bytes.
						{"TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0},		//Begins at offset 12 bytes.
						{"NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 20, D3D11_INPUT_PER_VERTEX_DATA, 0},		//Begins at offset 20 bytes.
						{"COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 32, D3D11_INPUT_PER_VERTEX_DATA, 0},	//Begins at offset 32 bytes.
					};

					if(SUCCEEDED(GraphicsDevice->CreateInputLayout(InputElementDescription, 4, VS->GetBufferPointer(), VS->GetBufferSize(), &InputLayout)))
					{
						//WE ARE USING TWO DIFERENT VERTEX TYPES WITH THE SAME LAYOUT! 
						GraphicsDeviceContext->IASetInputLayout(InputLayout);
						
						if(AttachWVPMatricesToPipeline(GraphicsDevice)) 
						{
							if(AttachParametersToPipeline(GraphicsDevice))
							{
								D3D11_SAMPLER_DESC SamplerDescription;	//https://msdn.microsoft.com/en-us/library/windows/desktop/ff476207(v=vs.85).aspx
								
								ZeroMemory(&SamplerDescription, sizeof(SamplerDescription));
								SamplerDescription.Filter = D3D11_FILTER_ANISOTROPIC;
								SamplerDescription.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;	//Needed to repeat the texture across the surface.
								SamplerDescription.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
								SamplerDescription.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
								SamplerDescription.MinLOD = -D3D11_FLOAT32_MAX;
								SamplerDescription.MaxLOD = D3D11_FLOAT32_MAX;
								SamplerDescription.MipLODBias = 0.0f;
								SamplerDescription.MaxAnisotropy = 4;
								SamplerDescription.ComparisonFunc = D3D11_COMPARISON_NEVER;

								if (SUCCEEDED(GraphicsDevice->CreateSamplerState(&SamplerDescription, &SamplerState)))
								{
									ShaderCompiledAndSetupProperly = true;
								}
							}
						}
					}
				}
			}
		}
	}
	return ShaderCompiledAndSetupProperly;
}
//=====================================================================================================================



Also, pay attention to " ID3D10Blob *VS, *PS; ". I think that's more deprecated code.

This post has been edited by BBeck: 02 January 2016 - 09:38 PM

Was This Post Helpful? 1
  • +
  • -

#3 nowayout2k  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 02-January 16

Re: Trouble with DX11 Engine

Posted 03 January 2016 - 12:11 AM

Thanks for your answer BBeck it was very informative. I found out that I earier I had used
return 0
just so I could see if I could compile the code and then I never went back and changed it to
return device
. So my device was NULL. Stupid mistake on my part but I appreciate the answer.
Was This Post Helpful? 0
  • +
  • -

#4 BBeck  Icon User is offline

  • Here to help.
  • member icon


Reputation: 792
  • View blog
  • Posts: 1,886
  • Joined: 24-April 12

Re: Trouble with DX11 Engine

Posted 03 January 2016 - 03:51 AM

Glad to hear you got it solved. :-)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1