Original Post
Hi everyone
Well first off, my english is not very good, I'm still in school but I hope you can understand my problem.
I'v recently become very interested in programming as a hobby.
So I bought a book about C/C++ and began to implement some simple applications. The next thing I always wanted to try was a simple rotating 3D triangle.
So I did with the latest DXSDK and it worked well
But i can't activate multisampling. Now here's the problem. As long as i set the DXGI_SAMPLE_DESC to 1, 0 everything works fine until
I change the count to 2 or higher. Then, wenn calling SetFullscreenState, I got a lot of strange _com_errors in my debug window. I've searched the whole
internet for some help, but I could not find any.
Here's my simple code to demonstrate my issue:
Did I miss something?
I've set the compiler warning level to maximum to find errors, but nothing.
Well I hope, my english isn't too bad and you can help me on this.
Greetings from switzerland
Well first off, my english is not very good, I'm still in school but I hope you can understand my problem.
I'v recently become very interested in programming as a hobby.
So I bought a book about C/C++ and began to implement some simple applications. The next thing I always wanted to try was a simple rotating 3D triangle.
So I did with the latest DXSDK and it worked well
But i can't activate multisampling. Now here's the problem. As long as i set the DXGI_SAMPLE_DESC to 1, 0 everything works fine until
I change the count to 2 or higher. Then, wenn calling SetFullscreenState, I got a lot of strange _com_errors in my debug window. I've searched the whole
internet for some help, but I could not find any.
Here's my simple code to demonstrate my issue:
#define WIN32_LEAN_AND_MEAN
#pragma warning(disable:4668)
#pragma warning(disable:4820)
#pragma warning(disable:4061)
#pragma warning(disable:4917)
#pragma warning(disable:4365)
#include <Windows.h>
#include <DXGI.h>
#include <D3D11.h>
#pragma warning(default:4668)
#pragma warning(default:4820)
#pragma warning(default:4061)
#pragma warning(default:4917)
#pragma warning(default:4365)
#pragma comment(lib, "dxgi.lib")
#pragma comment(lib, "d3d11.lib")
namespace DX11Demo {
///////////////////////////////////////////////////////////////////////////////
template <class ComInterface>
static void ReleaseComInterface(ComInterface** comInterface)
{
if ((*comInterface))
{
(*comInterface)->Release();
(*comInterface) = NULL;
}
}
///////////////////////////////////////////////////////////////////////////////
#pragma pack(1)
__declspec(align(1)) class Demo
{
public:
static bool Execute(HINSTANCE hInstance);
private:
Demo();
~Demo();
Demo(const Demo&) {}
void operator=(const Demo&) {}
static Demo* Instance(bool reset, Demo* newDemo);
static LRESULT CALLBACK WindowProc(
HWND hWindow,
UINT message,
WPARAM wordParam,
LPARAM longParam);
bool InitializeWindow(HINSTANCE hInstance);
bool InitializeDeviceAndSwapChain();
bool InitializeViews();
bool ResizeBuffers(WPARAM wordParam, LPARAM longParam);
bool MessageLoop();
void Render();
bool Present();
void Cleanup();
void Defaults();
private:
bool m_quit;
bool m_minimized;
bool m_occluded;
bool m_ignoreSizeMessage;
bool m_callbackFailure;
HINSTANCE m_hInstance;
HWND m_hWindow;
ATOM m_windowClass;
IDXGISwapChain* m_swapChain;
ID3D11Device* m_device;
ID3D11DeviceContext* m_deviceContext;
ID3D11RenderTargetView* m_renderTargetView;
};
///////////////////////////////////////////////////////////////////////////////
bool Demo::Execute(HINSTANCE hInstance)
{
if (!hInstance || Instance(false, NULL))
return false;
Demo demo;
return
demo.InitializeWindow(hInstance) &&
demo.InitializeDeviceAndSwapChain() &&
demo.InitializeViews() &&
demo.MessageLoop();
}
///////////////////////////////////////////////////////////////////////////////
Demo::Demo()
{
Defaults();
Instance(true, this);
}
///////////////////////////////////////////////////////////////////////////////
Demo::~Demo()
{
Cleanup();
Instance(true, NULL);
}
///////////////////////////////////////////////////////////////////////////////
Demo* Demo::Instance(bool reset, Demo* newDemo)
{
static Demo* demo = NULL;
if (reset)
demo = newDemo;
return demo;
}
///////////////////////////////////////////////////////////////////////////////
LRESULT CALLBACK Demo::WindowProc(
HWND hWindow,
UINT message,
WPARAM wordParam,
LPARAM longParam)
{
Demo* demo = Instance(false, NULL);
if (!demo)
return 0;
switch (message)
{
case WM_CLOSE:
demo->m_quit = true;
return 0;
case WM_SIZE:
if (!demo->ResizeBuffers(wordParam, longParam))
{
demo->m_callbackFailure = true;
return 0;
}
break;
case WM_SYSCOMMAND:
switch (wordParam & 0xFFF0)
{
case SC_MONITORPOWER:
case SC_SCREENSAVE:
case SC_KEYMENU:
return 1;
}
break;
}
return ::DefWindowProcW(hWindow, message, wordParam, longParam);
}
///////////////////////////////////////////////////////////////////////////////
bool Demo::InitializeWindow(HINSTANCE hInstance)
{
if (!hInstance)
return false;
m_hInstance = hInstance;
HGDIOBJ hBlackBrush = ::GetStockObject(BLACK_BRUSH);
WNDCLASSEXW classDesc = {0};
classDesc.cbSize = sizeof(classDesc);
classDesc.cbClsExtra = 0;
classDesc.cbWndExtra = 0;
classDesc.hbrBackground = reinterpret_cast<HBRUSH>(hBlackBrush);
classDesc.hCursor = ::LoadCursorW(NULL, IDC_ARROW);
classDesc.hIcon = ::LoadIconW(NULL, IDI_APPLICATION);
classDesc.hIconSm = classDesc.hIcon;
classDesc.hInstance = m_hInstance;
classDesc.lpfnWndProc = &WindowProc
classDesc.lpszClassName = L"DX11Demo";
classDesc.lpszMenuName = NULL;
classDesc.style = CS_DBLCLKS;
m_windowClass = ::RegisterClassExW(&classDesc);
if (!m_windowClass)
return false;
DWORD windowStyle = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU;
RECT windowRect = {0, 0, 800, 600};
::AdjustWindowRect(&windowRect, windowStyle, FALSE);
m_hWindow = ::CreateWindowW(
L"DX11Demo",
L"DX11Demo",
windowStyle,
CW_USEDEFAULT,
CW_USEDEFAULT,
static_cast<int>(windowRect.right - windowRect.left),
static_cast<int>(windowRect.bottom - windowRect.top),
NULL,
NULL,
m_hInstance,
NULL);
if (!m_hWindow)
return false;
::ShowWindow(m_hWindow, SW_SHOWNORMAL);
::SetForegroundWindow(m_hWindow);
return true;
}
///////////////////////////////////////////////////////////////////////////////
bool Demo::InitializeDeviceAndSwapChain()
{
DXGI_MODE_DESC modeDesc = {0};
modeDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
modeDesc.Width = 800;
modeDesc.Height = 600;
modeDesc.RefreshRate.Numerator = 0;
modeDesc.RefreshRate.Denominator = 0;
modeDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
modeDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
DXGI_SAMPLE_DESC sampleDesc = {0};
sampleDesc.Count = 4; // -> '_com_error at memory ...'
sampleDesc.Quality = 0;
DXGI_SWAP_CHAIN_DESC swapChainDesc = {0};
swapChainDesc.BufferCount = 1;
swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapChainDesc.BufferDesc = modeDesc;
swapChainDesc.Flags = 0; // Flags
swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
swapChainDesc.OutputWindow = m_hWindow;
swapChainDesc.Windowed = TRUE;
swapChainDesc.SampleDesc = sampleDesc;
if (FAILED(::D3D11CreateDeviceAndSwapChain(
NULL,
D3D_DRIVER_TYPE_HARDWARE,
NULL,
D3D11_CREATE_DEVICE_DEBUG,
NULL,
0,
D3D11_SDK_VERSION,
&swapChainDesc,
&m_swapChain,
&m_device,
NULL, // Feature level
&m_deviceContext)))
return false;
return true;
}
///////////////////////////////////////////////////////////////////////////////
bool Demo::InitializeViews()
{
ID3D11Texture2D* backBuffer = NULL;
if (FAILED(m_swapChain->GetBuffer(
0,
__uuidof(ID3D11Texture2D),
reinterpret_cast<void**>(&backBuffer))))
return false;
HRESULT result = m_device->CreateRenderTargetView(
backBuffer,
NULL,
&m_renderTargetView);
ReleaseComInterface(&backBuffer);
if (FAILED(result))
return false;
m_deviceContext->OMSetRenderTargets(1, &m_renderTargetView, NULL);
return true;
}
///////////////////////////////////////////////////////////////////////////////
bool Demo::ResizeBuffers(WPARAM wordParam, LPARAM longParam)
{
m_minimized = (SIZE_MINIMIZED == wordParam) ? true : false;
if (m_minimized || m_ignoreSizeMessage || !m_swapChain)
return true;
m_deviceContext->OMSetRenderTargets(0, NULL, NULL);
ReleaseComInterface(&m_renderTargetView);
UINT clientWidth = static_cast<UINT>(LOWORD(longParam));
UINT clientHeight = static_cast<UINT>(HIWORD(longParam));
DXGI_SWAP_CHAIN_DESC swapChainDesc = {0};
m_swapChain->GetDesc(&swapChainDesc);
if (FAILED(m_swapChain->ResizeBuffers(
swapChainDesc.BufferCount,
clientWidth,
clientHeight,
swapChainDesc.BufferDesc.Format,
swapChainDesc.Flags)))
return false;
return InitializeViews();
}
///////////////////////////////////////////////////////////////////////////////
bool Demo::MessageLoop()
{
MSG message = {0};
while (!m_quit)
{
if (::PeekMessageW(&message, NULL, 0, 0, PM_REMOVE))
{
::TranslateMessage(&message);
::DispatchMessageW(&message);
if (m_callbackFailure)
return false;
continue;
}
if (m_minimized)
continue;
if (!m_occluded)
Render();
if (!Present())
return false;
}
return true;
}
///////////////////////////////////////////////////////////////////////////////
void Demo::Render()
{
FLOAT clearColor[4] = {0.0f, 0.125f, 0.3f, 1.0f};
m_deviceContext->ClearRenderTargetView(m_renderTargetView, clearColor);
}
///////////////////////////////////////////////////////////////////////////////
bool Demo::Present()
{
UINT flags = m_occluded ? DXGI_PRESENT_TEST : 0;
switch (m_swapChain->Present(1, flags))
{
case S_OK:
m_occluded = false;
break;
case DXGI_STATUS_OCCLUDED:
m_occluded = true;
break;
default:
return false;
}
return true;
}
///////////////////////////////////////////////////////////////////////////////
void Demo::Cleanup()
{
if (m_deviceContext)
m_deviceContext->ClearState();
if (m_swapChain)
{
m_ignoreSizeMessage = true;
m_swapChain->SetFullscreenState(FALSE, NULL);
}
if (m_hWindow)
::DestroyWindow(m_hWindow);
if (m_windowClass)
::UnregisterClassW(L"DX11Demo", m_hInstance);
ReleaseComInterface(&m_swapChain);
ReleaseComInterface(&m_device);
ReleaseComInterface(&m_deviceContext);
ReleaseComInterface(&m_renderTargetView);
Defaults();
}
///////////////////////////////////////////////////////////////////////////////
void Demo::Defaults()
{
m_quit = false;
m_minimized = false;
m_occluded = false;
m_ignoreSizeMessage = false;
m_callbackFailure = false;
m_hInstance = NULL;
m_hWindow = NULL;
m_windowClass = 0;
m_swapChain = NULL;
m_device = NULL;
m_deviceContext = NULL;
m_renderTargetView = NULL;
}
} // namespace DX11Demo
///////////////////////////////////////////////////////////////////////////////
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, char*, int)
{
if (!DX11Demo::Demo::Execute(hInstance))
{
::MessageBoxW(NULL, L"Runtime error", L"Error", MB_ICONERROR);
return -1;
}
return 0;
}
Did I miss something?
I've set the compiler warning level to maximum to find errors, but nothing.
Well I hope, my english isn't too bad and you can help me on this.
Greetings from switzerland