Skip to content

Commit

Permalink
Factoring out Win32-specific code from DXSample
Browse files Browse the repository at this point in the history
In order to run the same code on multiple platforms (e.g. UWP, Xbox),
Win32-specific code needs to be decoupled from the DXSample base class.
This change is the first step in that direction.

+Win32Application class to encapsulate Win32-specific functionality.
*Generic Win32 OnEvent is replaced with specific handlers for messages
the samples currently handle.
  • Loading branch information
bobbrow committed Oct 7, 2015
1 parent 3106a7a commit 273fe2a
Show file tree
Hide file tree
Showing 140 changed files with 2,701 additions and 1,706 deletions.
15 changes: 5 additions & 10 deletions Samples/D3D1211On12/src/D3D1211On12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void D3D1211on12::LoadPipeline()
{
UINT d3d11DeviceFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
D2D1_FACTORY_OPTIONS d2dFactoryOptions = {};
#ifdef _DEBUG
#if defined(_DEBUG)
// Enable the D2D debug layer.
d2dFactoryOptions.debugLevel = D2D1_DEBUG_LEVEL_INFORMATION;

Expand Down Expand Up @@ -98,7 +98,7 @@ void D3D1211on12::LoadPipeline()
swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD;
swapChainDesc.OutputWindow = m_hwnd;
swapChainDesc.OutputWindow = Win32Application::GetHwnd();
swapChainDesc.SampleDesc.Count = 1;
swapChainDesc.Windowed = TRUE;

Expand All @@ -112,7 +112,7 @@ void D3D1211on12::LoadPipeline()
ThrowIfFailed(swapChain.As(&m_swapChain));

// This sample does not support fullscreen transitions.
ThrowIfFailed(factory->MakeWindowAssociation(m_hwnd, DXGI_MWA_NO_ALT_ENTER));
ThrowIfFailed(factory->MakeWindowAssociation(Win32Application::GetHwnd(), DXGI_MWA_NO_ALT_ENTER));

m_frameIndex = m_swapChain->GetCurrentBackBufferIndex();

Expand Down Expand Up @@ -230,7 +230,7 @@ void D3D1211on12::LoadAssets()
ComPtr<ID3DBlob> vertexShader;
ComPtr<ID3DBlob> pixelShader;

#ifdef _DEBUG
#if defined(_DEBUG)
// Enable better shader debugging with the graphics debugging tools.
UINT compileFlags = D3DCOMPILE_DEBUG | D3DCOMPILE_SKIP_OPTIMIZATION;
#else
Expand Down Expand Up @@ -374,7 +374,7 @@ void D3D1211on12::OnRender()
RenderUI();

// Present the frame.
ThrowIfFailed(m_swapChain->Present(0, 0));
ThrowIfFailed(m_swapChain->Present(1, 0));

MoveToNextFrame();
}
Expand Down Expand Up @@ -419,11 +419,6 @@ void D3D1211on12::OnDestroy()
CloseHandle(m_fenceEvent);
}

bool D3D1211on12::OnEvent(MSG)
{
return false;
}

void D3D1211on12::PopulateCommandList()
{
// Command list allocators can only be reset when the associated
Expand Down
2 changes: 0 additions & 2 deletions Samples/D3D1211On12/src/D3D1211On12.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@ class D3D1211on12 : public DXSample
public:
D3D1211on12(UINT width, UINT height, std::wstring name);

protected:
virtual void OnInit();
virtual void OnUpdate();
virtual void OnRender();
virtual void OnDestroy();
virtual bool OnEvent(MSG msg);

private:
static const UINT FrameCount = 3;
Expand Down
2 changes: 2 additions & 0 deletions Samples/D3D1211On12/src/D3D1211On12.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,15 @@
</CustomBuild>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Win32Application.h" />
<ClInclude Include="D3D1211On12.h" />
<ClInclude Include="d3dx12.h" />
<ClInclude Include="DXSample.h" />
<ClInclude Include="DXSampleHelper.h" />
<ClInclude Include="stdafx.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="Win32Application.cpp" />
<ClCompile Include="D3D1211On12.cpp" />
<ClCompile Include="DXSample.cpp" />
<ClCompile Include="Main.cpp" />
Expand Down
6 changes: 6 additions & 0 deletions Samples/D3D1211On12/src/D3D1211On12.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
<ClInclude Include="stdafx.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Win32Application.h">
<Filter>Header Files\Util</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="D3D1211On12.cpp">
Expand All @@ -58,5 +61,8 @@
<ClCompile Include="stdafx.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Win32Application.cpp">
<Filter>Source Files\Util</Filter>
</ClCompile>
</ItemGroup>
</Project>
94 changes: 7 additions & 87 deletions Samples/D3D1211On12/src/DXSample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,13 @@

#include "stdafx.h"
#include "DXSample.h"
#include <shellapi.h>

DXSample::DXSample(UINT width, UINT height, std::wstring name) :
m_width(width),
m_height(height),
m_title(name),
m_useWarpDevice(false)
{
ParseCommandLineArgs();

m_title = name + (m_useWarpDevice ? L" (WARP)" : L"");

WCHAR assetsPath[512];
GetAssetsPath(assetsPath, _countof(assetsPath));
m_assetsPath = assetsPath;
Expand All @@ -33,67 +29,6 @@ DXSample::~DXSample()
{
}

int DXSample::Run(HINSTANCE hInstance, int nCmdShow)
{
// Initialize the window class.
WNDCLASSEX windowClass = { 0 };
windowClass.cbSize = sizeof(WNDCLASSEX);
windowClass.style = CS_HREDRAW | CS_VREDRAW;
windowClass.lpfnWndProc = WindowProc;
windowClass.hInstance = hInstance;
windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
windowClass.lpszClassName = L"WindowClass1";
RegisterClassEx(&windowClass);

RECT windowRect = { 0, 0, static_cast<LONG>(m_width), static_cast<LONG>(m_height) };
AdjustWindowRect(&windowRect, WS_OVERLAPPEDWINDOW, FALSE);

// Create the window and store a handle to it.
m_hwnd = CreateWindowEx(NULL,
L"WindowClass1",
m_title.c_str(),
WS_OVERLAPPEDWINDOW,
300,
300,
windowRect.right - windowRect.left,
windowRect.bottom - windowRect.top,
NULL, // We have no parent window, NULL.
NULL, // We aren't using menus, NULL.
hInstance,
NULL); // We aren't using multiple windows, NULL.

ShowWindow(m_hwnd, nCmdShow);

// Initialize the sample. OnInit is defined in each child-implementation of DXSample.
OnInit();

// Main sample loop.
MSG msg = { 0 };
while (true)
{
// Process any messages in the queue.
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);

if (msg.message == WM_QUIT)
break;

// Pass events into our sample.
OnEvent(msg);
}

OnUpdate();
OnRender();
}

OnDestroy();

// Return this part of the WM_QUIT message to Windows.
return static_cast<char>(msg.wParam);
}

// Helper function for resolving the full path of assets.
std::wstring DXSample::GetAssetFullPath(LPCWSTR assetName)
{
Expand All @@ -102,7 +37,8 @@ std::wstring DXSample::GetAssetFullPath(LPCWSTR assetName)

// Helper function for acquiring the first available hardware adapter that supports Direct3D 12.
// If no such adapter can be found, *ppAdapter will be set to nullptr.
void DXSample::GetHardwareAdapter(_In_ IDXGIFactory4* pFactory, _Outptr_result_maybenull_ IDXGIAdapter1** ppAdapter)
_Use_decl_annotations_
void DXSample::GetHardwareAdapter(IDXGIFactory2* pFactory, IDXGIAdapter1** ppAdapter)
{
IDXGIAdapter1* pAdapter = nullptr;
*ppAdapter = nullptr;
Expand Down Expand Up @@ -134,36 +70,20 @@ void DXSample::GetHardwareAdapter(_In_ IDXGIFactory4* pFactory, _Outptr_result_m
void DXSample::SetCustomWindowText(LPCWSTR text)
{
std::wstring windowText = m_title + L": " + text;
SetWindowText(m_hwnd, windowText.c_str());
SetWindowText(Win32Application::GetHwnd(), windowText.c_str());
}

// Helper function for parsing any supplied command line args.
void DXSample::ParseCommandLineArgs()
_Use_decl_annotations_
void DXSample::ParseCommandLineArgs(WCHAR* argv[], int argc)
{
int argc;
LPWSTR *argv = CommandLineToArgvW(GetCommandLineW(), &argc);
for (int i = 1; i < argc; ++i)
{
if (_wcsnicmp(argv[i], L"-warp", wcslen(argv[i])) == 0 ||
_wcsnicmp(argv[i], L"/warp", wcslen(argv[i])) == 0)
{
m_useWarpDevice = true;
m_title = m_title + L" (WARP)";
}
}
LocalFree(argv);
}

// Main message handler for the sample.
LRESULT CALLBACK DXSample::WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
// Handle destroy/shutdown messages.
switch (message)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}

// Handle any messages the switch statement didn't.
return DefWindowProc(hWnd, message, wParam, lParam);
}
28 changes: 15 additions & 13 deletions Samples/D3D1211On12/src/DXSample.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,44 @@
#pragma once

#include "DXSampleHelper.h"
#include "Win32Application.h"

class DXSample
{
public:
DXSample(UINT width, UINT height, std::wstring name);
virtual ~DXSample();

int Run(HINSTANCE hInstance, int nCmdShow);
void SetCustomWindowText(LPCWSTR text);

protected:
virtual void OnInit() = 0;
virtual void OnUpdate() = 0;
virtual void OnRender() = 0;
virtual void OnDestroy() = 0;
virtual bool OnEvent(MSG msg) = 0;

std::wstring GetAssetFullPath(LPCWSTR assetName);
void GetHardwareAdapter(_In_ IDXGIFactory4* pFactory, _Outptr_result_maybenull_ IDXGIAdapter1** ppAdapter);
// Samples override the event handlers to handle specific messages.
virtual void OnKeyDown(UINT8 /*key*/) {}
virtual void OnKeyUp(UINT8 /*key*/) {}

// Accessors.
UINT GetWidth() const { return m_width; }
UINT GetHeight() const { return m_height; }
const WCHAR* GetTitle() const { return m_title.c_str(); }

static LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
void ParseCommandLineArgs(_In_reads_(argc) WCHAR* argv[], int argc);

protected:
std::wstring GetAssetFullPath(LPCWSTR assetName);
void GetHardwareAdapter(_In_ IDXGIFactory2* pFactory, _Outptr_result_maybenull_ IDXGIAdapter1** ppAdapter);
void SetCustomWindowText(LPCWSTR text);

// Viewport dimensions.
UINT m_width;
UINT m_height;
float m_aspectRatio;

// Window handle.
HWND m_hwnd;

// Adapter info.
bool m_useWarpDevice;

private:
void ParseCommandLineArgs();

// Root assets path.
std::wstring m_assetsPath;

Expand Down
2 changes: 1 addition & 1 deletion Samples/D3D1211On12/src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ _Use_decl_annotations_
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int nCmdShow)
{
D3D1211on12 sample(1280, 720, L"D3D12 11 on 12 Sample");
return sample.Run(hInstance, nCmdShow);
return Win32Application::Run(&sample, hInstance, nCmdShow);
}
Loading

0 comments on commit 273fe2a

Please sign in to comment.