*** FML:这是 FRAPS。停止DX屏幕录制软件并且它可以工作。特别感谢 @zdd 进行了一些联合测试,帮助我解决了这个问题。***
记住:FRAPS + D3D11_CREATE_DEVICE_DEBUG +
SampleDesc.Count > 1
不要混合。
我对 DX 真的很陌生……就像一周前一样。但是这个问题很容易描述,即使我的术语和理解有限。我保持这DEBUG
一层,因为它提供了有价值的反馈。但反过来,它会产生一个我无法克服的问题。我花了 1 天的时间来一根一根地拉头发。
问题
如果我创建一个D3D11
设备:
vSwapChainDesc.SampleDesc.Count = 4;
vSwapChainDesc.SampleDesc.Quality = 3;
->Release()
...无论它是哪个对象,它都会在 final 上崩溃。完全相同的DAMN 代码:
vSwapChainDesc.SampleDesc.Count = 1;
vSwapChainDesc.SampleDesc.Quality = 0;
... 顺利清理 COM 引用。更不用说这两种变体都可以在没有标志的情况下正常工作。Release
DEBUG
在 中Output
,我得到一些神秘的错误说法live children without parents
。但是我只有 5 个与 COM 包装器一起使用的 COM 对象。
因此,我删除了 COM 包装器,为它们中的每一个手动执行->Release()
和操作。= nullptr
IMO ,不可能错误地发布某些东西。在决赛中->Release()
,它说它遇到了断点。
那么...有没有其他人遇到过这种行为?除非其他人经历了完全相同的事情,否则我认为我无法得到答案。太奇怪了……
编码
// Uncomment this and it all works... like magic!
// #undef _DEBUG
#pragma region Includes
#include <stdio.h>
#include <conio.h>
#include <map>
#include <windows.h>
#include <d3d11.h>
#pragma comment (lib, "dxgi.lib")
#pragma comment (lib, "d3d11.lib")
#pragma endregion
#pragma region Com Helpers
// track the refcounts on ->Release() :)
std::map<std::string, int> g_ComCounter;
template <typename Interface_t>
Interface_t** ComRelease(Interface_t** aPointer, LPCSTR aName) {
if(!aPointer) {
DebugBreak();
}
if(*aPointer) {
// save refcount for debug
g_ComCounter[aName] = (*aPointer)->Release();
*aPointer = nullptr;
}
printf("Destroyed %p (%s:%d).\r\n", *aPointer, aName, g_ComCounter[aName]);
return aPointer;
}
#define COM_RELEASE(Pointer) ComRelease(&Pointer, #Pointer)
template <typename Interface_t>
Interface_t** ComPointer(Interface_t** aPointer, LPCSTR aName) {
if(!aPointer) {
DebugBreak();
}
if(*aPointer) {
// save refcount for debug
g_ComCounter[aName] = (*aPointer)->Release();
*aPointer = nullptr;
}
printf("Prepared %p (%s:%d).\r\n", *aPointer, aName, g_ComCounter[aName]);
// Object is being Acquired
return aPointer;
}
#define COM_POINTER(Pointer) ComPointer(&Pointer, #Pointer)
#pragma endregion
#pragma region Window Proc
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMessage, WPARAM wParam, LPARAM lParam) {
PAINTSTRUCT vPainStruct = { 0 };
HDC vDc(nullptr);
switch(uMessage) {
case WM_PAINT:
vDc = BeginPaint(hWnd, &vPainStruct);
EndPaint(hWnd, &vPainStruct);
return FALSE;
break;
case WM_ERASEBKGND:
// Don't erase background!
return TRUE;
break;
case WM_CLOSE:
DestroyWindow(hWnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
break;
}
return DefWindowProc(hWnd, uMessage, wParam, lParam);
}
#pragma endregion
#ifdef _CONSOLE
int wmain(int aArgc, const WCHAR* aArgv[]) {
#else
int wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow) {
#endif
#pragma region Variables
HINSTANCE vInstance = GetModuleHandle(nullptr);
HWND vHwnd = nullptr;
D3D_DRIVER_TYPE vDriverType = D3D_DRIVER_TYPE_UNKNOWN;
D3D_FEATURE_LEVEL vFeatureLevel = D3D_FEATURE_LEVEL_11_0;
IDXGIFactory *vDxgiFactory = nullptr;
IDXGIAdapter *vDxgiAdapter = nullptr;
ID3D11Device *vD3dDevice = nullptr;
ID3D11DeviceContext *vD3dContext = nullptr;
IDXGISwapChain *vSwapChain = nullptr;
ID3D11Texture2D *vBackBuffer(nullptr);
ID3D11RenderTargetView *vRenderTargetView = nullptr;
#ifdef _DEBUG
ID3D11Debug *vDebugger = nullptr;
ID3D11InfoQueue *vInfoQueue = nullptr;
#endif
#pragma endregion
#pragma region Init Window
// Register class
WNDCLASSEX vWndClass;
ZeroMemory(&vWndClass, sizeof(vWndClass));
vWndClass.cbSize = sizeof(WNDCLASSEX);
vWndClass.style = 0; // CS_HREDRAW | CS_VREDRAW (draw in loop, no need for WM_PAINT)
vWndClass.lpfnWndProc = WndProc;
vWndClass.cbClsExtra = 0;
vWndClass.cbWndExtra = 0;
vWndClass.hInstance = vInstance;
vWndClass.hIcon = 0;
vWndClass.hCursor = LoadCursor(nullptr, IDC_ARROW);
vWndClass.hbrBackground = nullptr;
vWndClass.lpszMenuName = nullptr;
vWndClass.lpszClassName = L"D3d11Window";
vWndClass.hIconSm = 0;
if(!RegisterClassEx(&vWndClass)) {
DebugBreak();
return 0;
}
// Create window
RECT vWindowRect = { 0, 0, 640, 480 };
AdjustWindowRect(&vWindowRect, WS_OVERLAPPEDWINDOW, FALSE);
vHwnd = CreateWindowEx(
0, vWndClass.lpszClassName, L"D3D11", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT,
vWindowRect.right - vWindowRect.left, vWindowRect.bottom - vWindowRect.top,
nullptr, nullptr, vInstance,
nullptr);
if(!vHwnd) {
DebugBreak();
return 0;
}
ShowWindow(vHwnd, SW_SHOWDEFAULT);
#pragma endregion
#pragma region Initialization
RECT vClientRect = { 0 };
GetClientRect(vHwnd, &vClientRect);
UINT vWidth = vClientRect.right - vClientRect.left;
UINT vHeight = vClientRect.bottom - vClientRect.top;
if(FAILED(CreateDXGIFactory(__uuidof(IDXGIFactory), (void**)COM_POINTER(vDxgiFactory)))) {
DebugBreak();
return 0;
}
if(FAILED(vDxgiFactory->EnumAdapters(0, COM_POINTER(vDxgiAdapter)))) {
DebugBreak();
return 0;
}
D3D_FEATURE_LEVEL vRequestedFeatureLevels[] = {
D3D_FEATURE_LEVEL_11_0,
// D3D_FEATURE_LEVEL_10_1,
// D3D_FEATURE_LEVEL_10_0
};
UINT vNumFeatureLevels = ARRAYSIZE(vRequestedFeatureLevels);
UINT vDeviceFlags = 0;
#ifdef _DEBUG
vDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
#endif
if(FAILED(D3D11CreateDevice(
vDxgiAdapter,
vDriverType,
nullptr,
vDeviceFlags,
vRequestedFeatureLevels,
vNumFeatureLevels,
D3D11_SDK_VERSION,
COM_POINTER(vD3dDevice),
&vFeatureLevel,
COM_POINTER(vD3dContext)))) {
return 0;
}
#ifdef _DEBUG
if(FAILED(vD3dDevice->QueryInterface(__uuidof(ID3D11Debug), (LPVOID*)COM_POINTER(vDebugger)))) {
return 0;
}
if(FAILED(vDebugger->QueryInterface(__uuidof(ID3D11InfoQueue), (LPVOID*)COM_POINTER(vInfoQueue)))) {
return 0;
}
vInfoQueue->SetBreakOnSeverity(D3D11_MESSAGE_SEVERITY_CORRUPTION, true);
vInfoQueue->SetBreakOnSeverity(D3D11_MESSAGE_SEVERITY_ERROR, true);
vDebugger->ReportLiveDeviceObjects(D3D11_RLDO_SUMMARY | D3D11_RLDO_DETAIL);
#endif
UINT vMsaaQuality = 0;
if(FAILED(vD3dDevice->CheckMultisampleQualityLevels(DXGI_FORMAT_R8G8B8A8_UNORM, 4, &vMsaaQuality)) || (vMsaaQuality < 1)) {
return 0;
}
printf("MsaaQuality: %d for 4\r\n", vMsaaQuality);
DXGI_SWAP_CHAIN_DESC vSwapChainDesc;
ZeroMemory(&vSwapChainDesc, sizeof(vSwapChainDesc));
vSwapChainDesc.BufferCount = 2;
vSwapChainDesc.BufferDesc.Width = vWidth;
vSwapChainDesc.BufferDesc.Height = vHeight;
vSwapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
vSwapChainDesc.BufferDesc.RefreshRate.Numerator = 60;
vSwapChainDesc.BufferDesc.RefreshRate.Denominator = 1;
vSwapChainDesc.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
vSwapChainDesc.BufferDesc.Scaling = DXGI_MODE_SCALING_CENTERED;
vSwapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
vSwapChainDesc.SampleDesc.Count = 4; // set 1 for the code to work vSwapChainDesc.SampleDesc.Quality = vMsaaQuality - 1;
vSwapChainDesc.OutputWindow = vHwnd;
vSwapChainDesc.Windowed = true;
vSwapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
vSwapChainDesc.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
if(FAILED(vDxgiFactory->CreateSwapChain(vD3dDevice, &vSwapChainDesc, COM_POINTER(vSwapChain)))) {
return 0;
}
if(FAILED(vSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)COM_POINTER(vBackBuffer)))) {
return 0;
}
if(FAILED(vD3dDevice->CreateRenderTargetView(vBackBuffer, nullptr, COM_POINTER(vRenderTargetView)))) {
return 0;
}
vD3dContext->OMSetRenderTargets(1, &vRenderTargetView, nullptr);
D3D11_VIEWPORT vViewport = { 0 };
vViewport.Width = static_cast<FLOAT>(vWidth);
vViewport.Height = static_cast<FLOAT>(vHeight);
vViewport.MinDepth = D3D11_MIN_DEPTH;
vViewport.MaxDepth = D3D11_MAX_DEPTH;
vViewport.TopLeftX = 0;
vViewport.TopLeftY = 0;
vD3dContext->RSSetViewports(1, &vViewport);
#pragma endregion
#pragma region Game Loop
MSG vMessage = { 0 };
while(WM_QUIT != vMessage.message) {
while(PeekMessage(&vMessage, 0, 0, 0, PM_REMOVE)) {
TranslateMessage(&vMessage);
DispatchMessage(&vMessage);
}
if(WM_QUIT == vMessage.message) {
break;
}
#pragma region Render
float vClearColor[4] = { 0.0f, 0.0f, 0.0f, 1.0f };
vD3dContext->ClearRenderTargetView(vRenderTargetView, vClearColor);
vSwapChain->Present(true, 0); // vsync
#pragma endregion
}
#pragma endregion
#pragma region Cleanup
if(vSwapChain) {
vSwapChain->SetFullscreenState(false, nullptr);
}
if(vD3dContext) {
vD3dContext->ClearState();
}
COM_RELEASE(vRenderTargetView);
COM_RELEASE(vBackBuffer);
COM_RELEASE(vSwapChain);
COM_RELEASE(vD3dContext);
COM_RELEASE(vD3dDevice);
COM_RELEASE(vDxgiFactory);
COM_RELEASE(vDxgiAdapter);
#ifdef _DEBUG
COM_RELEASE(vDebugger);
COM_RELEASE(vInfoQueue);
#endif
printf("\r\nREF COUNTS AFTER RELEASE():\r\n");
for(const auto& vComCount : g_ComCounter) {
printf("%s:%d\r\n", vComCount.first.c_str(), vComCount.second);
}
#pragma endregion
_getch();
return (int)vMessage.wParam;
}
帮助!
如果你想让我评论代码以更好地解释逻辑,我会的。我收到的评论提到了清楚地解释问题和代码的事情......也许我不够清楚。
最后的想法
也许我没有调用的某些对象内部有某种释放函数。也许我做错了什么或者真的错了。但我是从一个有信誉的网站上得到的。它与Hello DirectX!
我目前正在沉迷的书中的代码相匹配。
这个片段被精简和简约。在试图找出问题时,这是一个更复杂的版本的精简版本。另外,我删除了所有 OOP,以便将其放在一个易于遵循的代码块中。
现在有最后一个问题:除了这个之外,还有另一种平滑线条的方法
SampleDesc
吗?
PS:它是 Windows 7 x64,VS 2013 + 2014 CTP。并且该卡在调试和发布模式下都支持它(边缘平滑)。只有在调试模式下失败的最终版本。