Jump to content
  • Advertisement
Sign in to follow this  
thefatshizms

World, view & projection spaces

This topic is 1787 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

Was trying to do this tutorial: http://www.braynzarsoft.net/index.php?p=D3D11WVP

 

After compiling it crashes with this message: Unhandled exception at 0x008F1CDA in Directx11.exe: 0xC0000005: Access violation reading location 0x00000000.

 

and highlights this line:  g_Device->CreateVertexShader( pVertexShaderBytecode->GetBufferPointer(), pVertexShaderBytecode->GetBufferSize(), NULL, &g_pVertexShader );

 

Full code:

#include "directx.h"
#include <D3Dcompiler.h>

struct Vertex {
	DirectX::XMFLOAT3 position;
	DirectX::XMFLOAT4 color;
};

struct PerObject
{
	DirectX::XMMATRIX  WVP;
};

PerObject PerObj;

Vertex triangle[] = {

	DirectX::XMFLOAT3( -0.5f, -0.5f, 0.5f), DirectX::XMFLOAT4(1.0f, 0.0f, 0.0f, 1.0f ),
	DirectX::XMFLOAT3( -0.5f,  0.5f, 0.5f), DirectX::XMFLOAT4(0.0f, 1.0f, 0.0f, 1.0f ),
	DirectX::XMFLOAT3(  0.5f,  0.5f, 0.5f), DirectX::XMFLOAT4(0.0f, 0.0f, 1.0f, 1.0f ),
	DirectX::XMFLOAT3(  0.5f, -0.5f, 0.5f), DirectX::XMFLOAT4(0.0f, 1.0f, 0.0f, 1.0f ),
};

Directx::Directx() : g_Swapchain(NULL), g_Device(NULL), g_Devicecontext(NULL), g_Rendertarget(NULL){}

Directx::~Directx() {}

bool Directx::InitializeD3D(HWND hwnd, int width, int height, bool fullscreen) { 
	
	DXGI_SWAP_CHAIN_DESC sd;

	ZeroMemory(&sd, sizeof(sd));

	sd.BufferCount = 1;
    sd.BufferDesc.Width = width;
    sd.BufferDesc.Height = height;
    sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
    sd.BufferDesc.RefreshRate.Numerator = 60;
    sd.BufferDesc.RefreshRate.Denominator = 1;
    sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
    sd.OutputWindow = hwnd;
    sd.SampleDesc.Count = 4;
    sd.SampleDesc.Quality = 0;
    sd.Windowed = (fullscreen) ? false : true;


	D3D_FEATURE_LEVEL featurelevels[] = 
	{
		D3D_FEATURE_LEVEL_11_0,
		D3D_FEATURE_LEVEL_10_1,
		D3D_FEATURE_LEVEL_10_0,
		D3D_FEATURE_LEVEL_9_3,
		D3D_FEATURE_LEVEL_9_2,
		D3D_FEATURE_LEVEL_9_1,
	};

	int numfeaturelevels = 5;

	if(FAILED(D3D11CreateDeviceAndSwapChain(NULL,  D3D_DRIVER_TYPE_HARDWARE, NULL, D3D11_CREATE_DEVICE_DEBUG, featurelevels, numfeaturelevels, D3D11_SDK_VERSION, &sd, &g_Swapchain, &g_Device, NULL, &g_Devicecontext)))
		return false;


	ID3D11Texture2D *pBackBuffer;
	HRESULT hr;

	if(FAILED(g_Swapchain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer)))
			return false;

	hr = g_Device->CreateRenderTargetView(pBackBuffer, NULL, &g_Rendertarget);
	pBackBuffer->Release();

	if(FAILED(hr))
			return false;

	g_Devicecontext->OMSetRenderTargets(1, &g_Rendertarget, g_DepthStencilview);

	D3D11_VIEWPORT vp;

	vp.Height = (FLOAT)height;
	vp.Width = (FLOAT)width;
	vp.MaxDepth = 1.0f;
	vp.MinDepth = 0.0f;
	vp.TopLeftX = 0;
	vp.TopLeftY = 0;

	D3D11_TEXTURE2D_DESC depthstencil;

	ZeroMemory(&depthstencil, sizeof(depthstencil));

	depthstencil.Width = width;
	depthstencil.Height = height;
	depthstencil.MipLevels = 1;
	depthstencil.ArraySize = 1;
	depthstencil.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
	depthstencil.SampleDesc.Count = 1;
	depthstencil.SampleDesc.Quality = 0;
	depthstencil.Usage = D3D11_USAGE_DEFAULT;
	depthstencil.BindFlags = D3D11_BIND_DEPTH_STENCIL;
	depthstencil.CPUAccessFlags = 0;
	depthstencil.MiscFlags = 0;

	g_Device->CreateTexture2D(&depthstencil, NULL, &g_Depthbuffer);
	g_Device->CreateDepthStencilView(g_Depthbuffer, NULL, &g_DepthStencilview);

	g_Devicecontext->RSSetViewports(1, &vp);

	D3D11_BUFFER_DESC cb;

	cb.Usage = D3D11_USAGE_DEFAULT;
	cb.ByteWidth = sizeof(g_PerObjectbuffer);
	cb.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
	cb.CPUAccessFlags = 0;
	cb.MiscFlags = 0;

	g_Device->CreateBuffer(&cb, NULL, &g_PerObjectbuffer);

	camPosition = DirectX::XMVectorSet(0.0f, 0.0f, -0.5f, 0.0f);
	camTarget = DirectX::XMVectorSet(0.0f, 0.0f, 0.0f, 0.0f);
	camUp = DirectX::XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);

	camView = DirectX::XMMatrixLookAtLH(camPosition, camTarget, camUp);

	camProjection = DirectX::XMMatrixPerspectiveFovLH(0.4f * 3.14f, (float)width/height, 1.0f, 1000.0f);

	return true; 
}

void Directx::Render() {

	float ClearColor[4] = { 0.0f, 0.125f, 0.6f, 1.0f };
	g_Devicecontext->ClearRenderTargetView(g_Rendertarget, ClearColor);
	g_Devicecontext->ClearDepthStencilView(g_DepthStencilview, D3D11_CLEAR_DEPTH|D3D11_CLEAR_STENCIL, 1.0f, 0);

	World = DirectX::XMMatrixIdentity();

	WVP = World * camView * camProjection;

	PerObj.WVP = DirectX::XMMatrixTranspose(WVP);

	g_Devicecontext->UpdateSubresource(g_PerObjectbuffer, 0, NULL, &PerObj, 0, 0);
	g_Devicecontext->VSGetConstantBuffers(0, 1, &g_PerObjectbuffer);


	g_Devicecontext->DrawIndexed(6, 0, 0);
	g_Swapchain->Present(0, 0);
}

void Directx::DrawTriangle() {

	DWORD indices[] = {
		0, 1, 2,
		0, 2, 3,
	};

	D3D11_BUFFER_DESC bd;
	ZeroMemory(&bd, sizeof(bd));

	bd.ByteWidth = sizeof(DWORD) * 2 * 3;
	bd.Usage = D3D11_USAGE_DEFAULT;
	bd.BindFlags = D3D11_BIND_INDEX_BUFFER;
	bd.CPUAccessFlags = 0;
	bd.MiscFlags = 0;

	D3D11_SUBRESOURCE_DATA data;

	ZeroMemory(&data, sizeof(data));

	data.pSysMem = indices;
	g_Device->CreateBuffer(&bd, &data, &g_Indexbuffer);

	g_Devicecontext->IASetIndexBuffer(g_Indexbuffer, DXGI_FORMAT_R32_UINT, 0);

	D3D11_BUFFER_DESC vertexBufferDesc;
	ZeroMemory( &vertexBufferDesc, sizeof(vertexBufferDesc) );

	vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
	vertexBufferDesc.ByteWidth = sizeof( Vertex ) * 4;
	vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
	vertexBufferDesc.CPUAccessFlags = 0;
	vertexBufferDesc.MiscFlags = 0;

	D3D11_SUBRESOURCE_DATA vertexBufferData; 

	ZeroMemory( &vertexBufferData, sizeof(vertexBufferData) );
	vertexBufferData.pSysMem = triangle;
	g_Device->CreateBuffer( &vertexBufferDesc, &vertexBufferData, &g_Vertexbuffer);


	CHAR* pVertexShaderCode =   "cbuffer cbPerObject { float4x4 WVP;};"
							"void vertex_shader( in float4 posIn : POSITION, in float4 colorIn : COLOR,"
                            "out float4 posOut : SV_POSITION, out float4 colorOut : COLOR )"
                            "{ VS_OUTPUT output; posOut = posIn; output.Pos = mul(inPos, WVP);"
                            "  colorOut = colorIn; return output; }";

	CHAR* pPixelShaderCode =    "float4 pixel_shader( VS_OUTPUT input) : SV_TARGET"    
                            "{ return input.color; }";

	ID3D10Blob* pVertexShaderBytecode = NULL;  
    ID3D10Blob* pCompileErrors = NULL;
	
	D3DCompile(pVertexShaderCode, lstrlenA( pVertexShaderCode ) + 1, "vertex shader", NULL, NULL, "vertex_shader", "vs_4_0", NULL, 0, &pVertexShaderBytecode, &pCompileErrors );                  
  
    if( pCompileErrors != NULL ) {         
		OutputDebugStringA( (CHAR*)pCompileErrors->GetBufferPointer() );    
        pCompileErrors->Release();   
    }   


    g_Device->CreateVertexShader( pVertexShaderBytecode->GetBufferPointer(), pVertexShaderBytecode->GetBufferSize(), NULL, &g_pVertexShader );      


    ID3D10Blob* pPixelShaderBytecode = NULL;  
    D3DCompile( pPixelShaderCode, lstrlenA( pPixelShaderCode ) + 1, "pixel shader", NULL, NULL, "pixel_shader", "ps_4_0", NULL, 0, &pPixelShaderBytecode, &pCompileErrors );    
        
	
	if( pCompileErrors != NULL ) {          
        OutputDebugStringA( (CHAR*)pCompileErrors->GetBufferPointer() );      
        pCompileErrors->Release();    
    }     



    g_Device->CreatePixelShader( pPixelShaderBytecode->GetBufferPointer(), pPixelShaderBytecode->GetBufferSize(), NULL, &g_pPixelShader );    
    pPixelShaderBytecode->Release();  



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

    UINT numElements = ARRAYSIZE( inputLayoutDesc );

    g_Device->CreateInputLayout( inputLayoutDesc, numElements, pVertexShaderBytecode->GetBufferPointer(), pVertexShaderBytecode->GetBufferSize(), &g_pInputLayout );    
    pVertexShaderBytecode->Release();    

    
    g_Devicecontext->IASetInputLayout( g_pInputLayout ); 
    UINT stride = sizeof( Vertex );  
    UINT offset = 0; 
    g_Devicecontext->IASetVertexBuffers(0, 1, &g_Vertexbuffer, &stride, &offset );       

    g_Devicecontext->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST );  

    g_Devicecontext->VSSetShader( g_pVertexShader, NULL, 0 );   
    g_Devicecontext->PSSetShader( g_pPixelShader, NULL, 0 );

}

void Directx::ShutdownDirectx() {

	g_Swapchain->Release();
	g_Device->Release();
	g_Devicecontext->Release();
	g_Rendertarget->Release();
	g_Vertexbuffer->Release();
	g_Indexbuffer->Release();
	g_pPixelShader->Release();
	g_pInputLayout->Release();
	g_pVertexShader->Release();
	g_Depthbuffer->Release();
	g_DepthStencilview->Release();
	g_PerObjectbuffer->Release();
}

Share this post


Link to post
Share on other sites
Advertisement
g_Device->CreateVertexShader( pVertexShaderBytecode->GetBufferPointer(), pVertexShaderBytecode->GetBufferSize(), NULL, &g_pVertexShader );

My quess is the following:

    if( pCompileErrors != NULL ) {         
		OutputDebugStringA( (CHAR*)pCompileErrors->GetBufferPointer() );    
        pCompileErrors->Release();   
    }   

In case of any compile error, pVertexShaderBytecode will likely still be a nullptr. You should eigther throw an exception or return in this error-check, since continuing will lead to errors like this. Your shader probably has some error, and thus said scenario occurs. Check whether pVertexShaderBytecode actually is 0 at this line. "Access violation reading location 0x00000000." basically means that something in your code at this line is a pointer that equals 0.

Share this post


Link to post
Share on other sites

Not sure about inspecting variables but when setting breakpoints it highlights this line when crashing:

 

0, 1, 2,

 

which is part of:

DWORD indices[] = {
		0, 1, 2,
		0, 2, 3,
	};

Share this post


Link to post
Share on other sites
g_Device->CreateVertexShader( pVertexShaderBytecode->GetBufferPointer(), pVertexShaderBytecode->GetBufferSize(), NULL, &g_pVertexShader );

My quess is the following:

    if( pCompileErrors != NULL ) {         
		OutputDebugStringA( (CHAR*)pCompileErrors->GetBufferPointer() );    
        pCompileErrors->Release();   
    }   

In case of any compile error, pVertexShaderBytecode will likely still be a nullptr. You should eigther throw an exception or return in this error-check, since continuing will lead to errors like this. Your shader probably has some error, and thus said scenario occurs. Check whether pVertexShaderBytecode actually is 0 at this line. "Access violation reading location 0x00000000." basically means that something in your code at this line is a pointer that equals 0.

 

I've changed it to this:

if( pCompileErrors != NULL ) {            
        pCompileErrors->Release();  
		return 0;
    }     

it now highlights line 379 on file DirectXMathMatrix.inl

 

and shows this:

http://puu.sh/5RVNJ.png

Share this post


Link to post
Share on other sites

When running the program it pops up with this message: "Unhandled exception at 0x00C41CD1 in Directx11.exe: 0xC0000005: Access violation reading location 0x00000000."

 

http://puu.sh/5TcwP.png

 

It highlights this line in particular when crashing: g_Device->CreateVertexShader( pVertexShaderBytecode->GetBufferPointer(), pVertexShaderBytecode->GetBufferSize(), NULL, &g_pVertexShader );

 

Updated source code:

http://pastebin.com/raNTzSP5

Share this post


Link to post
Share on other sites
ID3D10Blob* pVertexShaderBytecode = NULL;  
ID3D10Blob* pCompileErrors = NULL;
 
if( pCompileErrors != NULL ) {
     OutputDebugStringA( (CHAR*)pCompileErrors->GetBufferPointer() );
     pCompileErrors->Release();  
     return 0;
}
 
D3DCompile(pVertexShaderCode,  lstrlenA( pVertexShaderCode ) + 1, "vertex shader", NULL, NULL, "vertex_shader", "vs_4_0", NULL, 0, &pVertexShaderBytecode, &pCompileErrors );  

Here you're checking if the shader compiled succesfully, before actually compiling the shader. If you try compiling the shader before checking the compile succeeded, this should fix this issue. smile.png

Side note: You're compiling the shader each time you call DrawSquare() (perhaps this could use a better name?). It's much more efficient to compile the shader once, and then binding this compiled shader before drawing the triangle.

Edited by MajorTom

Share this post


Link to post
Share on other sites

Thanks for the reply :). I don't know why I moved it above as it's completely illogical. After moving it back I'm presented this: Unhandled exception at 0x7637C41F (KernelBase.dll) in Directx11.exe: 0x0000087D (parameters: 0x00000000, 0x003BDEA8, 0x003BD2E4).

 

Side note: I know it's inefficient and I plan to compile it once, but i'm more concerned on getting the application running before hand :)

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!