我是渲染API的新手,直到现在我一直都没事。我已经调试了一段时间来解决这个问题,但我无法弄清楚为什么这不起作用。所以我需要一些帮助。
我相信问题的根源在这里。
m_VertexShader->Release();
因为它没有返回HRESULT,所以我无法做到。
这是代码。
#include "shader.h"
ShaderProgram::ShaderProgram(Renderer& renderer, const char* vertShader, const char* pixShader)
: m_Renderer(renderer), m_VertexShaderSource(vertShader), m_PixelShaderSource(pixShader)
{
Load();
}
ShaderProgram::~ShaderProgram()
{
m_VertexShader->Release();
m_PixelShader->Release();
m_InputLayout->Release();
}
void ShaderProgram::Load()
{
vector<char> vertSource = FileReader::read_file(m_VertexShaderSource);
vector<char> fragSource = FileReader::read_file(m_PixelShaderSource);
auto resultVert = m_Renderer.getDevice()->CreateVertexShader(vertSource.data(), vertSource.size(), nullptr, &m_VertexShader);
auto resultFrag = m_Renderer.getDevice()->CreatePixelShader(fragSource.data(), fragSource.size(), nullptr, &m_PixelShader);
if (resultVert != S_OK || resultFrag != S_OK)
{
MessageBox(nullptr, "Failed to create shader!", "Error", MB_OK);
exit(0);
}
D3D11_INPUT_ELEMENT_DESC layout[]{
{ "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "COLOR", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 }
};
auto result = m_Renderer.getDevice()->CreateInputLayout(layout, 2, vertSource.data(), vertSource.size(), &m_InputLayout);
if (result != S_OK)
{
MessageBox(nullptr, "Could not create the input layout!", "Error", MB_OK);
exit(0);
}
这是我的渲染类。顺便说一下,我刚才刚刚添加了模板和深度测试,可能需要解决这个问题。
#include "renderer.h"
Renderer::Renderer(Window& window)
{
createDevice(window);
createRenderTarget();
createDepthStencil();
}
Renderer::~Renderer()
{
m_SwapChain->Release();
m_Device->Release();
m_DeviceContex->Release();
m_RenderTargetView->Release();
m_DepthStencilView->Release();
}
void Renderer::createDevice(Window& window)
{
DXGI_SWAP_CHAIN_DESC swapChain = { 0 };
swapChain.BufferCount = 1;
swapChain.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
swapChain.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapChain.OutputWindow = window.getHandle();
swapChain.SampleDesc.Count = 1;
swapChain.Windowed = true;
auto result = D3D11CreateDeviceAndSwapChain(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, 0, nullptr,
0, D3D11_SDK_VERSION, &swapChain, &m_SwapChain, &m_Device, nullptr, &m_DeviceContex);
if (result != S_OK)
{
MessageBox(nullptr, "Problem with creating DX11!", "Error", MB_OK);
exit(0);
}
}
void Renderer::createDepthStencil()
{
D3D11_TEXTURE2D_DESC depthStencilDesc;
depthStencilDesc.Width = m_BackBuffer.Width;
depthStencilDesc.Height = m_BackBuffer.Height;
depthStencilDesc.MipLevels = 1;
depthStencilDesc.ArraySize = 1;
depthStencilDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
depthStencilDesc.SampleDesc.Count = 1;
depthStencilDesc.SampleDesc.Quality = 0;
depthStencilDesc.Usage = D3D11_USAGE_DEFAULT;
depthStencilDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
depthStencilDesc.CPUAccessFlags = 0;
depthStencilDesc.MiscFlags = 0;
m_Device->CreateTexture2D(&depthStencilDesc, nullptr, &m_DepthStencilBuffer);
m_Device->CreateDepthStencilView(m_DepthStencilBuffer, nullptr, &m_DepthStencilView);
m_DepthStencilBuffer->Release();
}
void Renderer::createRenderTarget()
{
ID3D11Texture2D* backBuffer;
auto result = m_SwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (void**)&backBuffer);
if (result != S_OK)
{
MessageBox(nullptr, "Failed to get the swap chain buffer!", "Error", MB_OK);
exit(0);
}
m_Device->CreateRenderTargetView(backBuffer, nullptr, &m_RenderTargetView);
backBuffer->GetDesc(&m_BackBuffer);
backBuffer->Release();
}
void Renderer::beginFrame()
{
m_DeviceContex->OMSetRenderTargets(1, &m_RenderTargetView, m_DepthStencilView);
auto viewport = CD3D11_VIEWPORT(0.0f, 0.0f, (float) m_BackBuffer.Width, (float) m_BackBuffer.Height);
viewport.MinDepth = 0.0f;
viewport.MaxDepth = 1.0f;
m_DeviceContex->RSSetViewports(1, &viewport);
float clearColor[] = { 0.25f, 0.75f, 0.8f, 1.0f };
m_DeviceContex->ClearRenderTargetView(m_RenderTargetView, clearColor);
m_DeviceContex->ClearDepthStencilView(m_DepthStencilView, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0);
}
//Swaps the buffer!
void Renderer::endFrame()
{
m_SwapChain->Present(1, 0);
}
}
答案 0 :(得分:0)
在非调试模式下运行时是否会出现相同的错误?使用有限的信息,您看起来似乎没有找到您的调试符号表。如果它在非调试模式下完全正常,那么它将通过文档进行筛选以找出如何加载符号表。答案不是很多,但希望它可能会有所帮助
答案 1 :(得分:0)
你真的应该转向使用Microsoft::WRL::ComPtr
。使用原始指针,您可能会调用Release
太少或太多次,这就是这里发生的事情。请参阅this page。
它会删除愚蠢的事情,就像你释放它时保持指向m_DepthStencilView
的悬挂指针。您根本不需要将其变为成员变量,因为您只使用它来创建m_DepthStencilView。
#include <wrl/client.h>
using Microsoft::WRL::ComPtr;
...
ComPtr<ID3D11Texture2D> depthStencil;
m_Device->CreateTexture2D(&depthStencilDesc, nullptr, &depthStencil);
m_Device->CreateDepthStencilView(depthStencil.Get(), nullptr, &m_DepthStencilView);
查看Direct3D 11游戏Visual Studio模板,特别是DeviceResources.h / DeviceResources.cpp的实现。