I'm developing a directx11 app and for some reason, my simple application crashes with an access violation error. Something causes it to crash and then my graphics drivers causes a reset.
Here's my initialization:
DirectXRendererImpl::DirectXRendererImpl(const EngineSettings& settings, Logger& logger) : mLogger(logger), mWindowHandle(GetActiveWindow()), mSwapchain(nullptr), mBackbuffer(nullptr), mDevice(nullptr), mContext(nullptr),
mForwardVertexShader(nullptr), mForwardPixelShader(nullptr), mVertexBuffer(nullptr), mInputLayout(nullptr)
{
// create swapchain, device and devicecontext
DXGI_SWAP_CHAIN_DESC swapChainDesc;
ZeroMemory(&swapChainDesc, sizeof(DXGI_SWAP_CHAIN_DESC));
swapChainDesc.BufferCount = 2;
swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapChainDesc.OutputWindow = mWindowHandle;
swapChainDesc.SampleDesc.Count = 1;
swapChainDesc.SampleDesc.Quality = 0;
swapChainDesc.Windowed = true;
const D3D_FEATURE_LEVEL featureLevel = D3D_FEATURE_LEVEL_11_0;
const UINT numFeatureLevels = 1;
HRESULT result = D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, NULL, &featureLevel, numFeatureLevels, D3D11_SDK_VERSION, &swapChainDesc, &mSwapchain, &mDevice, NULL, &mContext);
if (result != S_OK)
{
JONS_LOG_ERROR(mLogger, "DirectXRenderer::DirectXRenderer(): D3D11CreateDeviceAndSwapChain failed: code " + result);
throw std::runtime_error("DirectXRenderer::DirectXRenderer(): D3D11CreateDeviceAndSwapChain failed: code " + result);
}
// backbuffer rendertarget setup
ID3D11Texture2D* backbuffer = nullptr;
mSwapchain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&backbuffer);
mDevice->CreateRenderTargetView(backbuffer, NULL, &mBackbuffer);
backbuffer->Release();
mContext->OMSetRenderTargets(1, &mBackbuffer, NULL);
// setup viewport
// query width/height from d3d
ZeroMemory(&swapChainDesc, sizeof(DXGI_SWAP_CHAIN_DESC));
mSwapchain->GetDesc(&swapChainDesc);
D3D11_VIEWPORT viewport;
ZeroMemory(&viewport, sizeof(D3D11_VIEWPORT));
viewport.TopLeftX = 0;
viewport.TopLeftY = 0;
viewport.Width = static_cast<float>(swapChainDesc.BufferDesc.Width);
viewport.Height = static_cast<float>(swapChainDesc.BufferDesc.Height);
viewport.MinDepth = 0.0f;
viewport.MaxDepth = 1.0f;
mContext->RSSetViewports(1, &viewport);
// create shader objects
mDevice->CreateVertexShader(gForwardVertexShader, sizeof(gForwardVertexShader), NULL, &mForwardVertexShader);
mDevice->CreatePixelShader(gForwardPixelShader, sizeof(gForwardPixelShader), NULL, &mForwardPixelShader);
mContext->VSSetShader(mForwardVertexShader, NULL, NULL);
mContext->PSSetShader(mForwardPixelShader, NULL, NULL);
// fill vertex buffer
VERTEX OurVertices[] =
{
{ 0.0f, 0.5f, 0.0f },
{ 0.45f, -0.5, 0.0f},
{ -0.45f, -0.5f, 0.0f }
};
D3D11_BUFFER_DESC bufferDescription;
ZeroMemory(&bufferDescription, sizeof(D3D11_BUFFER_DESC));
bufferDescription.Usage = D3D11_USAGE_DEFAULT;
bufferDescription.ByteWidth = sizeof(OurVertices);
bufferDescription.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bufferDescription.CPUAccessFlags = 0;
bufferDescription.MiscFlags = 0;
D3D11_SUBRESOURCE_DATA initData;
ZeroMemory(&initData, sizeof(D3D11_SUBRESOURCE_DATA));
initData.pSysMem = OurVertices;
mDevice->CreateBuffer(&bufferDescription, &initData, &mVertexBuffer);
D3D11_INPUT_ELEMENT_DESC inputDescription;
ZeroMemory(&inputDescription, sizeof(D3D11_INPUT_ELEMENT_DESC));
inputDescription.SemanticName = "POSITION";
inputDescription.SemanticIndex = 0;
inputDescription.Format = DXGI_FORMAT_R32G32B32_FLOAT;
inputDescription.InputSlot = 0;
inputDescription.AlignedByteOffset = 0;
inputDescription.InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
inputDescription.InstanceDataStepRate = 0;
mDevice->CreateInputLayout(&inputDescription, 1, gForwardVertexShader, sizeof(gForwardVertexShader), &mInputLayout);
mContext->IASetInputLayout(mInputLayout);
// register as window subclass to listen for WM_SIZE events. etc
if (!SetWindowSubclass(mWindowHandle, WndProc, gSubClassID, 0))
{
JONS_LOG_ERROR(mLogger, "DirectXRenderer::DirectXRenderer(): SetWindowSubclass() failed");
throw std::runtime_error("DirectXRenderer::DirectXRenderer(): SetWindowSubclass() failed");
}
gDirectXRendererImpl = this;
}
The simple vertex shader:
struct VOut
{
float4 position : SV_POSITION;
float4 color : COLOR;
};
VOut main(float4 position : POSITION)
{
VOut output;
output.position = position;
output.color = float4(1.0, 0.0, 0.0, 0.0);
return output;
}
Simple pixel shader:
float4 main(float4 position : POSITION, float4 color : COLOR) : SV_TARGET
{
return color;
}
Rendering function is simply this:
void DirectXRendererImpl::Render(const RenderQueue& renderQueue, const RenderableLighting& lighting, const DebugOptions::RenderingMode debugMode, const DebugOptions::RenderingFlags debugExtra)
{
const FLOAT clearColor[4] = { 1.0f, 1.0f, 0.0f, 1.0f };
mContext->ClearRenderTargetView(mBackbuffer, clearColor);
uint32_t vertexSize = sizeof(VERTEX);
uint32_t offset = 0;
mContext->IASetVertexBuffers(0, 1, &mVertexBuffer, &vertexSize, &offset);
mContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
mContext->Draw(3, 0);
mSwapchain->Present(0, 0);
}
The app runs for a few seconds and then causes a full video driver reset. Is my app leaking memory or what is causing this?
EDIT: more specifically, this is the error:
First-chance exception at 0x0F69974F (nvwgf2um.dll) in ExampleGame.exe: 0xC0000005: Access violation writing location 0x00193000.
Unhandled exception at 0x0F69974F (nvwgf2um.dll) in ExampleGame.exe: 0xC0000005: Access violation writing location 0x00193000.
You have to use this->
bufferDescription.ByteWidth = sizeof(VERTEX) * sizeof(OurVertices);
otherwise program is crashed.
User contributions licensed under CC BY-SA 3.0