Display Driver crashes when drawing a triangle?

I tried reading directxtutorials.com today, and when I did the "Drawing a Triangle" tutorial. Unfortunately, when I run it, my display driver crashes, then after it restores, I get an exception at mapping the vertex buffer.

I'll go ahead and put the 2 functions here. I'm sure that the problem is there somewhere:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Code found on directxtutorials.com

void BoxDemo::InitPipeline(){
	ID3D10Blob* VS, *PS;

	D3DX11CompileFromFile(L"VertexShader.hlsl", 0, 0, "main", "vs_5_0", 0, 0, 0, &VS, 0, 0);
	D3DX11CompileFromFile(L"VertexShader.hlsl", 0, 0, "PShader", "ps_5_0", 0, 0, 0, &PS, 0, 0); 
// I made a shader file called vertexshader, then threw the pixel shader in there too. 

	mDevice->CreateVertexShader(VS->GetBufferPointer(), VS->GetBufferSize(), NULL, &mVS);
	mDevice->CreatePixelShader(PS->GetBufferPointer(), PS->GetBufferSize(), NULL, &mPS);

	mDeviceContext->VSSetShader(mVS, 0, 0);
	mDeviceContext->PSSetShader(mPS, 0, 0);

	D3D11_INPUT_ELEMENT_DESC ied[] = 
	{
		{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
		{ "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
	};

	mDevice->CreateInputLayout(ied, 2, VS->GetBufferPointer(), VS->GetBufferSize(), &mInputLayout);
	mDeviceContext->IASetInputLayout(mInputLayout);
}

void BoxDemo::InitGraphics(){
	VERTEX OurVertices[] = 
	{
		{ 0.0f, 0.5f, 0.0f, D3DXCOLOR(1.0f, 0.0f, 0.0f, 1.0f) },
		{ 0.45f, -0.5, 0.0f, D3DXCOLOR(0.0f, 1.0f, 0.0f, 1.0f) },
		{ -0.45f, -0.5f, 0.0f, D3DXCOLOR(0.0f, 0.0f, 1.0f, 1.0f) }
	};

	// Create Vertex Buffer
	D3D11_BUFFER_DESC bd;
	ZeroMemory(&bd, sizeof(bd));
	bd.Usage = D3D11_USAGE_DYNAMIC;
	bd.ByteWidth = sizeof(VERTEX) * 3;
	bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
	bd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;

	mDevice->CreateBuffer(&bd, NULL, &VertexBuffer);

	D3D11_MAPPED_SUBRESOURCE ms;
	mDeviceContext->Map(VertexBuffer, 0, D3D11_MAP_READ_WRITE, 0, &ms); // Exception thrown. Access violation reading location
	memcpy(ms.pData, OurVertices, sizeof(OurVertices));
	mDeviceContext->Unmap(VertexBuffer, NULL);
}
Last edited on
You only have CPU Write Access... Only request a WRITEONLY mapping.
Hmm. I changed line 45 to
mDeviceContext->Map(VertexBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &ms);
And I kept the way 40 is written - there are only 2 constants to CPU_ACCESS - one for reading and one for writing.

Yet I still get the exception. And the display driver crash.
I do believe you can OR the CPU_ACCESS flags (unnecessary).
Check CreateBuffer's return value, make sure VertexBuffer is nonnull after the call.
Yep. It is null. Do you have an idea on how to fix that? I don't know how to check return values...

EDIT: I found the problem with the driver crash. My "DrawScene" function is called in the game loop (when no messages are being processed). Well, I think the DrawScene function is calling prior to the initialization functions. I am going to get to work on fixing that.
Last edited on
Store it as a HRESULT. Google the hex value, or put it in the Error Lookup tool that usually comes with Visual Studio.
The common HRESULTs are here: http://msdn.microsoft.com/en-us/library/windows/desktop/aa378137(v=vs.85).aspx
I... I got it. You have no idea how much pride and sense of accomplishment I have right now. Wow. I have been trying to draw something on a simple C++ program for months now.

It may not seemed like you helped, but I was about to call it a night until I saw your reply. I read it and said, "Maybe there is something wrong" I scanned my code, and found an answer. So... thanks
Topic archived. No new replies allowed.