aboutsummaryrefslogtreecommitdiff
path: root/NvCloth/samples/SampleBase
diff options
context:
space:
mode:
authormtamis <[email protected]>2017-02-28 18:24:59 +0100
committermtamis <[email protected]>2017-02-28 18:24:59 +0100
commit5581909a4d19db97304449f66404ff99a0429d3f (patch)
treea90f7eb85c095a8aba45cf5e909c82c1cdbed77d /NvCloth/samples/SampleBase
parentFix cmake visual studio project generation (locate_gw_root.bat) (diff)
downloadnvcloth-5581909a4d19db97304449f66404ff99a0429d3f.tar.xz
nvcloth-5581909a4d19db97304449f66404ff99a0429d3f.zip
Add visual samples.
Diffstat (limited to 'NvCloth/samples/SampleBase')
-rw-r--r--NvCloth/samples/SampleBase/Main.cpp34
-rw-r--r--NvCloth/samples/SampleBase/Sample.h26
-rw-r--r--NvCloth/samples/SampleBase/core/Application.cpp73
-rw-r--r--NvCloth/samples/SampleBase/core/Application.h55
-rw-r--r--NvCloth/samples/SampleBase/core/DeviceManager.cpp795
-rw-r--r--NvCloth/samples/SampleBase/core/DeviceManager.h166
-rw-r--r--NvCloth/samples/SampleBase/core/SampleController.cpp25
-rw-r--r--NvCloth/samples/SampleBase/core/SampleController.h42
-rw-r--r--NvCloth/samples/SampleBase/core/SampleManager.cpp60
-rw-r--r--NvCloth/samples/SampleBase/core/SampleManager.h91
-rw-r--r--NvCloth/samples/SampleBase/renderer/ClothRenderMesh.cpp211
-rw-r--r--NvCloth/samples/SampleBase/renderer/ClothRenderMesh.h68
-rw-r--r--NvCloth/samples/SampleBase/renderer/ConvexRenderMesh.cpp84
-rw-r--r--NvCloth/samples/SampleBase/renderer/ConvexRenderMesh.h34
-rw-r--r--NvCloth/samples/SampleBase/renderer/CustomRenderMesh.cpp98
-rw-r--r--NvCloth/samples/SampleBase/renderer/CustomRenderMesh.h43
-rw-r--r--NvCloth/samples/SampleBase/renderer/DebugRenderBuffer.h50
-rw-r--r--NvCloth/samples/SampleBase/renderer/Mesh.cpp13
-rw-r--r--NvCloth/samples/SampleBase/renderer/Mesh.h51
-rw-r--r--NvCloth/samples/SampleBase/renderer/PrimitiveRenderMesh.cpp205
-rw-r--r--NvCloth/samples/SampleBase/renderer/PrimitiveRenderMesh.h61
-rw-r--r--NvCloth/samples/SampleBase/renderer/PxPhysXCommonConfig.h92
-rw-r--r--NvCloth/samples/SampleBase/renderer/PxRenderBuffer.h157
-rw-r--r--NvCloth/samples/SampleBase/renderer/RenderMaterial.cpp206
-rw-r--r--NvCloth/samples/SampleBase/renderer/RenderMaterial.h118
-rw-r--r--NvCloth/samples/SampleBase/renderer/RenderUtils.h78
-rw-r--r--NvCloth/samples/SampleBase/renderer/Renderable.cpp48
-rw-r--r--NvCloth/samples/SampleBase/renderer/Renderable.h128
-rw-r--r--NvCloth/samples/SampleBase/renderer/Renderer.cpp730
-rw-r--r--NvCloth/samples/SampleBase/renderer/Renderer.h247
-rw-r--r--NvCloth/samples/SampleBase/renderer/RendererHBAO.cpp81
-rw-r--r--NvCloth/samples/SampleBase/renderer/RendererHBAO.h40
-rw-r--r--NvCloth/samples/SampleBase/renderer/RendererShadow.cpp417
-rw-r--r--NvCloth/samples/SampleBase/renderer/RendererShadow.h82
-rw-r--r--NvCloth/samples/SampleBase/renderer/ResourceManager.cpp210
-rw-r--r--NvCloth/samples/SampleBase/renderer/ResourceManager.h93
-rw-r--r--NvCloth/samples/SampleBase/renderer/ShaderUtils.h98
-rw-r--r--NvCloth/samples/SampleBase/renderer/SkinnedRenderMesh.cpp217
-rw-r--r--NvCloth/samples/SampleBase/renderer/SkinnedRenderMesh.h82
-rw-r--r--NvCloth/samples/SampleBase/scene/Scene.cpp162
-rw-r--r--NvCloth/samples/SampleBase/scene/Scene.h104
-rw-r--r--NvCloth/samples/SampleBase/scene/SceneController.cpp250
-rw-r--r--NvCloth/samples/SampleBase/scene/SceneController.h128
-rw-r--r--NvCloth/samples/SampleBase/scene/scenes/FreeFallScene.cpp116
-rw-r--r--NvCloth/samples/SampleBase/scene/scenes/FreeFallScene.h33
-rw-r--r--NvCloth/samples/SampleBase/scene/scenes/FrictionScene.cpp119
-rw-r--r--NvCloth/samples/SampleBase/scene/scenes/FrictionScene.h33
-rw-r--r--NvCloth/samples/SampleBase/scene/scenes/SimpleScene.cpp94
-rw-r--r--NvCloth/samples/SampleBase/scene/scenes/SimpleScene.h32
-rw-r--r--NvCloth/samples/SampleBase/scene/scenes/TetherScene.cpp103
-rw-r--r--NvCloth/samples/SampleBase/scene/scenes/TetherScene.h36
-rw-r--r--NvCloth/samples/SampleBase/scene/scenes/WindScene.cpp124
-rw-r--r--NvCloth/samples/SampleBase/scene/scenes/WindScene.h37
-rw-r--r--NvCloth/samples/SampleBase/ui/CommonUIController.cpp467
-rw-r--r--NvCloth/samples/SampleBase/ui/CommonUIController.h91
-rw-r--r--NvCloth/samples/SampleBase/ui/imgui_impl_dx11.cpp583
-rw-r--r--NvCloth/samples/SampleBase/ui/imgui_impl_dx11.h25
-rw-r--r--NvCloth/samples/SampleBase/utils/CallbackImplementations.cpp110
-rw-r--r--NvCloth/samples/SampleBase/utils/CallbackImplementations.h230
-rw-r--r--NvCloth/samples/SampleBase/utils/ClothMeshGenerator.cpp193
-rw-r--r--NvCloth/samples/SampleBase/utils/ClothMeshGenerator.h54
-rw-r--r--NvCloth/samples/SampleBase/utils/JobManager.cpp136
-rw-r--r--NvCloth/samples/SampleBase/utils/JobManager.h175
-rw-r--r--NvCloth/samples/SampleBase/utils/SampleProfiler.cpp223
-rw-r--r--NvCloth/samples/SampleBase/utils/SampleProfiler.h79
-rw-r--r--NvCloth/samples/SampleBase/utils/SampleTime.h58
-rw-r--r--NvCloth/samples/SampleBase/utils/UIHelpers.h56
-rw-r--r--NvCloth/samples/SampleBase/utils/Utils.cpp13
-rw-r--r--NvCloth/samples/SampleBase/utils/Utils.h89
69 files changed, 9362 insertions, 0 deletions
diff --git a/NvCloth/samples/SampleBase/Main.cpp b/NvCloth/samples/SampleBase/Main.cpp
new file mode 100644
index 0000000..433ed88
--- /dev/null
+++ b/NvCloth/samples/SampleBase/Main.cpp
@@ -0,0 +1,34 @@
+/*
+* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
+*
+* NVIDIA CORPORATION and its licensors retain all intellectual property
+* and proprietary rights in and to this software, related documentation
+* and any modifications thereto. Any use, reproduction, disclosure or
+* distribution of this software and related documentation without an express
+* license agreement from NVIDIA CORPORATION is strictly prohibited.
+*/
+
+#include "Sample.h"
+#include <sstream>
+
+#include <Windows.h>
+
+
+using namespace std;
+
+int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow)
+{
+ // Enable run-time memory check for debug builds.
+#if defined(DEBUG) | defined(_DEBUG)
+ _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
+ AllocConsole();
+#endif
+
+ SampleConfig config;
+ config.sampleName = L"NvCloth Samples Viewer";
+ //config.additionalResourcesDir.push_back(alternatePathArg.getValue().c_str());
+
+ int result = runSample(config);
+
+ return result;
+}
diff --git a/NvCloth/samples/SampleBase/Sample.h b/NvCloth/samples/SampleBase/Sample.h
new file mode 100644
index 0000000..9c9c502
--- /dev/null
+++ b/NvCloth/samples/SampleBase/Sample.h
@@ -0,0 +1,26 @@
+/*
+* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
+*
+* NVIDIA CORPORATION and its licensors retain all intellectual property
+* and proprietary rights in and to this software, related documentation
+* and any modifications thereto. Any use, reproduction, disclosure or
+* distribution of this software and related documentation without an express
+* license agreement from NVIDIA CORPORATION is strictly prohibited.
+*/
+
+#ifndef SAMPLE_H
+#define SAMPLE_H
+
+#include <string>
+#include <vector>
+
+
+struct SampleConfig
+{
+ std::wstring sampleName;
+ std::vector<std::string> additionalResourcesDir;
+};
+
+int runSample(const SampleConfig& config);
+
+#endif //SAMPLE_H \ No newline at end of file
diff --git a/NvCloth/samples/SampleBase/core/Application.cpp b/NvCloth/samples/SampleBase/core/Application.cpp
new file mode 100644
index 0000000..a7f81f1
--- /dev/null
+++ b/NvCloth/samples/SampleBase/core/Application.cpp
@@ -0,0 +1,73 @@
+/*
+* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
+*
+* NVIDIA CORPORATION and its licensors retain all intellectual property
+* and proprietary rights in and to this software, related documentation
+* and any modifications thereto. Any use, reproduction, disclosure or
+* distribution of this software and related documentation without an express
+* license agreement from NVIDIA CORPORATION is strictly prohibited.
+*/
+
+#include "Application.h"
+#include <DirectXMath.h>
+#include "XInput.h"
+#include "DXUTMisc.h"
+
+
+Application::Application(std::wstring sampleName)
+: m_sampleName(sampleName)
+{
+ m_deviceManager = new DeviceManager();
+}
+
+void Application::addControllerToFront(IApplicationController* controller)
+{
+ m_controllers.push_back(controller);
+}
+
+int Application::run()
+{
+ // FirstPersonCamera uses this timer, without it it will be FPS-dependent
+ DXUTGetGlobalTimer()->Start();
+
+ for (auto it = m_controllers.begin(); it != m_controllers.end(); it++)
+ m_deviceManager->AddControllerToFront(*it);
+
+ DeviceCreationParameters deviceParams;
+ deviceParams.swapChainFormat = DXGI_FORMAT_R8G8B8A8_UNORM_SRGB;
+ deviceParams.swapChainSampleCount = 4;
+ deviceParams.startFullscreen = false;
+ deviceParams.backBufferWidth = 1600;
+ deviceParams.backBufferHeight = 900;
+#if defined(DEBUG) | defined(_DEBUG)
+ deviceParams.createDeviceFlags = D3D11_CREATE_DEVICE_DEBUG;
+#endif
+ deviceParams.featureLevel = D3D_FEATURE_LEVEL_11_0;
+
+ if (FAILED(m_deviceManager->CreateWindowDeviceAndSwapChain(deviceParams, m_sampleName.c_str())))
+ {
+ MessageBoxA(nullptr, "Cannot initialize the D3D11 device with the requested parameters", "Error",
+ MB_OK | MB_ICONERROR);
+ return 1;
+ }
+
+ for (auto it = m_controllers.begin(); it != m_controllers.end(); it++)
+ (*it)->onInitialize();
+
+ for (auto it = m_controllers.begin(); it != m_controllers.end(); it++)
+ (*it)->onSampleStart();
+
+ m_deviceManager->SetVsyncEnabled(false);
+ m_deviceManager->MessageLoop();
+
+ for (auto it = m_controllers.rbegin(); it != m_controllers.rend(); it++)
+ (*it)->onSampleStop();
+
+ for (auto it = m_controllers.rbegin(); it != m_controllers.rend(); it++)
+ (*it)->onTerminate();
+
+ m_deviceManager->Shutdown();
+ delete m_deviceManager;
+
+ return 0;
+}
diff --git a/NvCloth/samples/SampleBase/core/Application.h b/NvCloth/samples/SampleBase/core/Application.h
new file mode 100644
index 0000000..286b40f
--- /dev/null
+++ b/NvCloth/samples/SampleBase/core/Application.h
@@ -0,0 +1,55 @@
+/*
+* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
+*
+* NVIDIA CORPORATION and its licensors retain all intellectual property
+* and proprietary rights in and to this software, related documentation
+* and any modifications thereto. Any use, reproduction, disclosure or
+* distribution of this software and related documentation without an express
+* license agreement from NVIDIA CORPORATION is strictly prohibited.
+*/
+
+#ifndef APPLICATION_H
+#define APPLICATION_H
+
+#include <DeviceManager.h>
+#include <vector>
+#include <string>
+
+/**
+ISampleController adds more onstart and onstop callbacks to IVisualController
+*/
+class IApplicationController : public IVisualController
+{
+ public:
+ virtual void onInitialize() {}
+ virtual void onSampleStart() {}
+ virtual void onSampleStop() {}
+ virtual void onTerminate() {}
+};
+
+
+/**
+Main manager which runs sample.
+You have to add controllers to it which will receive all the start, animate, render etc. callbacks.
+*/
+class Application
+{
+public:
+ Application(std::wstring sampleName);
+ void addControllerToFront(IApplicationController* controller);
+
+ const std::vector<IApplicationController*>& getControllers() const
+ {
+ return m_controllers;
+ }
+
+ int run();
+
+private:
+ DeviceManager* m_deviceManager;
+ std::vector<IApplicationController*> m_controllers;
+ std::wstring m_sampleName;
+};
+
+
+#endif //APPLICATION_H \ No newline at end of file
diff --git a/NvCloth/samples/SampleBase/core/DeviceManager.cpp b/NvCloth/samples/SampleBase/core/DeviceManager.cpp
new file mode 100644
index 0000000..26f9fbb
--- /dev/null
+++ b/NvCloth/samples/SampleBase/core/DeviceManager.cpp
@@ -0,0 +1,795 @@
+// TAGRELEASE: PUBLIC
+
+#include "DeviceManager.h"
+#include <WinUser.h>
+#include <Windows.h>
+#include <assert.h>
+#include <sstream>
+#include <algorithm>
+#include "SampleProfiler.h"
+
+#ifndef SAFE_RELEASE
+#define SAFE_RELEASE(p) { if (p) { (p)->Release(); (p)=NULL; } }
+#endif
+
+#define WINDOW_CLASS_NAME L"NvDX11"
+
+#define WINDOW_STYLE_NORMAL (WS_OVERLAPPEDWINDOW | WS_VISIBLE)
+#define WINDOW_STYLE_FULLSCREEN (WS_POPUP | WS_SYSMENU | WS_VISIBLE)
+
+// A singleton, sort of... To pass the events from WindowProc to the object.
+DeviceManager* g_DeviceManagerInstance = NULL;
+
+#undef min
+#undef max
+
+LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
+{
+ if(g_DeviceManagerInstance)
+ return g_DeviceManagerInstance->MsgProc(hWnd, uMsg, wParam, lParam);
+ else
+ return DefWindowProc(hWnd, uMsg, wParam, lParam);
+}
+
+DeviceManager* GetDeviceManager()
+{
+ return g_DeviceManagerInstance;
+}
+
+namespace
+{
+ bool IsNvDeviceID(UINT id)
+ {
+ return id == 0x10DE;
+ }
+
+ // Find an adapter whose name contains the given string.
+ IDXGIAdapter* FindAdapter(const WCHAR* targetName, bool& isNv)
+ {
+ IDXGIAdapter* targetAdapter = NULL;
+ IDXGIFactory* IDXGIFactory_0001 = NULL;
+ HRESULT hres = CreateDXGIFactory(__uuidof(IDXGIFactory), (void**)&IDXGIFactory_0001);
+ if (hres != S_OK)
+ {
+ printf("ERROR in CreateDXGIFactory, %s@%d.\nFor more info, get log from debug D3D runtime: (1) Install DX SDK, and enable Debug D3D from DX Control Panel Utility. (2) Install and start DbgView. (3) Try running the program again.\n",__FILE__,__LINE__);
+ return targetAdapter;
+ }
+
+ unsigned int adapterNo = 0;
+ while (SUCCEEDED(hres))
+ {
+ IDXGIAdapter* pAdapter = NULL;
+ hres = IDXGIFactory_0001->EnumAdapters(adapterNo, (IDXGIAdapter**)&pAdapter);
+
+ if (SUCCEEDED(hres))
+ {
+ DXGI_ADAPTER_DESC aDesc;
+ pAdapter->GetDesc(&aDesc);
+
+ // If no name is specified, return the first adapater. This is the same behaviour as the
+ // default specified for D3D11CreateDevice when no adapter is specified.
+ if (wcslen(targetName) == 0)
+ {
+ targetAdapter = pAdapter;
+ isNv = IsNvDeviceID(aDesc.VendorId);
+ break;
+ }
+
+ std::wstring aName = aDesc.Description;
+ if (aName.find(targetName) != std::string::npos)
+ {
+ targetAdapter = pAdapter;
+ isNv = IsNvDeviceID(aDesc.VendorId);
+ }
+ else
+ {
+ pAdapter->Release();
+ }
+ }
+
+ adapterNo++;
+ }
+
+ if (IDXGIFactory_0001)
+ IDXGIFactory_0001->Release();
+
+ return targetAdapter;
+ }
+
+ // Adjust window rect so that it is centred on the given adapter. Clamps to fit if it's too big.
+ RECT MoveWindowOntoAdapter(IDXGIAdapter* targetAdapter, const RECT& rect)
+ {
+ assert(targetAdapter != NULL);
+
+ RECT result = rect;
+ HRESULT hres = S_OK;
+ unsigned int outputNo = 0;
+ while (SUCCEEDED(hres))
+ {
+ IDXGIOutput* pOutput = NULL;
+ hres = targetAdapter->EnumOutputs(outputNo++, &pOutput);
+
+ if (SUCCEEDED(hres) && pOutput)
+ {
+ DXGI_OUTPUT_DESC OutputDesc;
+ pOutput->GetDesc( &OutputDesc );
+ const RECT desktop = OutputDesc.DesktopCoordinates;
+ const int centreX = (int) desktop.left + (int)(desktop.right - desktop.left) / 2;
+ const int centreY = (int) desktop.top + (int)(desktop.bottom - desktop.top) / 2;
+ const int winW = rect.right - rect.left;
+ const int winH = rect.bottom - rect.top;
+ int left = centreX - winW/2;
+ int right = left + winW;
+ int top = centreY - winH/2;
+ int bottom = top + winH;
+ result.left = std::max(left, (int) desktop.left);
+ result.right = std::min(right, (int) desktop.right);
+ result.bottom = std::min(bottom, (int) desktop.bottom);
+ result.top = std::max(top, (int) desktop.top);
+ pOutput->Release();
+
+ // If there is more than one output, go with the first found. Multi-monitor support could go here.
+ break;
+ }
+ }
+ return result;
+ }
+}
+
+HRESULT
+DeviceManager::CreateWindowDeviceAndSwapChain(const DeviceCreationParameters& params, std::wstring title)
+{
+ g_DeviceManagerInstance = this;
+ m_WindowTitle = title;
+
+ HINSTANCE hInstance = GetModuleHandle(NULL);
+ WNDCLASSEX windowClass = { sizeof(WNDCLASSEX), CS_HREDRAW | CS_VREDRAW, WindowProc,
+ 0L, 0L, hInstance, NULL, NULL, NULL, NULL, WINDOW_CLASS_NAME, NULL };
+
+ windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
+
+ RegisterClassEx(&windowClass);
+
+ UINT windowStyle = params.startFullscreen
+ ? WINDOW_STYLE_FULLSCREEN
+ : params.startMaximized
+ ? (WINDOW_STYLE_NORMAL | WS_MAXIMIZE)
+ : WINDOW_STYLE_NORMAL;
+
+ RECT rect = { 0, 0, params.backBufferWidth, params.backBufferHeight };
+ AdjustWindowRect(&rect, windowStyle, FALSE);
+
+ IDXGIAdapter* targetAdapter = FindAdapter(params.adapterNameSubstring, m_IsNvidia);
+ if (targetAdapter)
+ {
+ rect = MoveWindowOntoAdapter(targetAdapter, rect);
+ }
+ else
+ {
+ // We could silently use a default adapter in this case. I think it's better to choke.
+ std::wostringstream ostr;
+ ostr << L"Could not find an adapter matching \"" << params.adapterNameSubstring << "\"" << std::ends;
+ MessageBox(NULL, ostr.str().c_str(), m_WindowTitle.c_str(), MB_OK | MB_ICONERROR);
+ return E_FAIL;
+ }
+
+ m_hWnd = CreateWindowEx(
+ 0,
+ WINDOW_CLASS_NAME,
+ title.c_str(),
+ windowStyle,
+ rect.left,
+ rect.top,
+ rect.right - rect.left,
+ rect.bottom - rect.top,
+ GetDesktopWindow(),
+ NULL,
+ hInstance,
+ NULL
+ );
+
+ if(!m_hWnd)
+ {
+#ifdef DEBUG
+ DWORD errorCode = GetLastError();
+ printf("CreateWindowEx error code = 0x%x\n", errorCode);
+#endif
+
+ MessageBox(NULL, L"Cannot create window", m_WindowTitle.c_str(), MB_OK | MB_ICONERROR);
+ return E_FAIL;
+ }
+
+ UpdateWindow(m_hWnd);
+
+ HRESULT hr = E_FAIL;
+
+ RECT clientRect;
+ GetClientRect(m_hWnd, &clientRect);
+ UINT width = clientRect.right - clientRect.left;
+ UINT height = clientRect.bottom - clientRect.top;
+
+ ZeroMemory(&m_SwapChainDesc, sizeof(m_SwapChainDesc));
+ m_SwapChainDesc.BufferCount = params.swapChainBufferCount;
+ m_SwapChainDesc.BufferDesc.Width = width;
+ m_SwapChainDesc.BufferDesc.Height = height;
+ m_SwapChainDesc.BufferDesc.Format = params.swapChainFormat;
+ m_SwapChainDesc.BufferDesc.RefreshRate.Numerator = params.refreshRate;
+ m_SwapChainDesc.BufferDesc.RefreshRate.Denominator = 0;
+ m_SwapChainDesc.BufferUsage = params.swapChainUsage;
+ m_SwapChainDesc.OutputWindow = m_hWnd;
+ m_SwapChainDesc.SampleDesc.Count = params.swapChainSampleCount;
+ m_SwapChainDesc.SampleDesc.Quality = params.swapChainSampleQuality;
+ m_SwapChainDesc.Windowed = !params.startFullscreen;
+ m_SwapChainDesc.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
+
+ // The D3D documentation says that if adapter is non-null, driver type must be unknown. Why not put
+ // this logic in the CreateDevice fns then?!?
+ const D3D_DRIVER_TYPE dType = (targetAdapter)? D3D_DRIVER_TYPE_UNKNOWN: params.driverType;
+
+ hr = D3D11CreateDeviceAndSwapChain(
+ targetAdapter, // pAdapter
+ dType, // DriverType
+ NULL, // Software
+ params.createDeviceFlags, // Flags
+ &params.featureLevel, // pFeatureLevels
+ 1, // FeatureLevels
+ D3D11_SDK_VERSION, // SDKVersion
+ &m_SwapChainDesc, // pSwapChainDesc
+ &m_SwapChain, // ppSwapChain
+ &m_Device, // ppDevice
+ NULL, // pFeatureLevel
+ &m_ImmediateContext // ppImmediateContext
+ );
+
+ if (targetAdapter)
+ targetAdapter->Release();
+
+ if(FAILED(hr))
+ return hr;
+
+ m_DepthStencilDesc.ArraySize = 1;
+ m_DepthStencilDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
+ m_DepthStencilDesc.CPUAccessFlags = 0;
+ m_DepthStencilDesc.Format = params.depthStencilFormat;
+ m_DepthStencilDesc.Width = width;
+ m_DepthStencilDesc.Height = height;
+ m_DepthStencilDesc.MipLevels = 1;
+ m_DepthStencilDesc.MiscFlags = 0;
+ m_DepthStencilDesc.SampleDesc.Count = params.swapChainSampleCount;
+ m_DepthStencilDesc.SampleDesc.Quality = 0;
+ m_DepthStencilDesc.Usage = D3D11_USAGE_DEFAULT;
+
+ hr = CreateRenderTargetAndDepthStencil();
+
+ if(FAILED(hr))
+ return hr;
+
+ DeviceCreated();
+ BackBufferResized();
+
+ return S_OK;
+}
+
+void
+DeviceManager::Shutdown()
+{
+ if(m_SwapChain && GetWindowState() == kWindowFullscreen)
+ m_SwapChain->SetFullscreenState(false, NULL);
+
+ DeviceDestroyed();
+
+ SAFE_RELEASE(m_BackBufferRTV);
+ SAFE_RELEASE(m_DepthStencilDSV);
+ SAFE_RELEASE(m_DepthStencilBuffer);
+
+ g_DeviceManagerInstance = NULL;
+ SAFE_RELEASE(m_ImmediateContext);
+ SAFE_RELEASE(m_SwapChain);
+
+ ID3D11Debug * d3dDebug = nullptr;
+ if (nullptr != m_Device)
+ {
+ ID3D11DeviceContext* pCtx;
+ m_Device->GetImmediateContext(&pCtx);
+ pCtx->ClearState();
+ pCtx->Flush();
+ pCtx->Release();
+ if (SUCCEEDED(m_Device->QueryInterface(__uuidof(ID3D11Debug), reinterpret_cast<void**>(&d3dDebug))))
+ {
+ d3dDebug->ReportLiveDeviceObjects(D3D11_RLDO_DETAIL);
+ d3dDebug->Release();
+ }
+ }
+ SAFE_RELEASE(m_Device);
+
+ if(m_hWnd)
+ {
+ DestroyWindow(m_hWnd);
+ m_hWnd = NULL;
+ }
+}
+
+HRESULT
+DeviceManager::CreateRenderTargetAndDepthStencil()
+{
+ HRESULT hr;
+
+ ID3D11Texture2D *backBuffer = NULL;
+ hr = m_SwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&backBuffer);
+ if (FAILED(hr))
+ return hr;
+
+ hr = m_Device->CreateRenderTargetView(backBuffer, NULL, &m_BackBufferRTV);
+ backBuffer->Release();
+ if (FAILED(hr))
+ return hr;
+
+ if(m_DepthStencilDesc.Format != DXGI_FORMAT_UNKNOWN)
+ {
+ hr = m_Device->CreateTexture2D(&m_DepthStencilDesc, NULL, &m_DepthStencilBuffer);
+ if (FAILED(hr))
+ return hr;
+
+ hr = m_Device->CreateDepthStencilView(m_DepthStencilBuffer, NULL, &m_DepthStencilDSV);
+ if (FAILED(hr))
+ return hr;
+ }
+
+ return S_OK;
+}
+
+void
+DeviceManager::MessageLoop()
+{
+ MSG msg = {0};
+
+ LARGE_INTEGER perfFreq, previousTime;
+ QueryPerformanceFrequency(&perfFreq);
+ QueryPerformanceCounter(&previousTime);
+
+ while (WM_QUIT != msg.message)
+ {
+ if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
+ {
+ TranslateMessage(&msg);
+ DispatchMessage(&msg);
+ }
+ else
+ {
+ PROFILER_BEGIN("Main Loop");
+
+ LARGE_INTEGER newTime;
+ QueryPerformanceCounter(&newTime);
+
+ double elapsedSeconds = (m_FixedFrameInterval >= 0)
+ ? m_FixedFrameInterval
+ : (double)(newTime.QuadPart - previousTime.QuadPart) / (double)perfFreq.QuadPart;
+
+ if(m_SwapChain && GetWindowState() != kWindowMinimized)
+ {
+ Animate(elapsedSeconds);
+ Render();
+ m_SwapChain->Present(m_SyncInterval, 0);
+ Sleep(0);
+ }
+ else
+ {
+ // Release CPU resources when idle
+ Sleep(1);
+ }
+
+ {
+ m_vFrameTimes.push_back(elapsedSeconds);
+ double timeSum = 0;
+ for(auto it = m_vFrameTimes.begin(); it != m_vFrameTimes.end(); it++)
+ timeSum += *it;
+
+ if(timeSum > m_AverageTimeUpdateInterval)
+ {
+ m_AverageFrameTime = timeSum / (double)m_vFrameTimes.size();
+ m_vFrameTimes.clear();
+ }
+ }
+
+ previousTime = newTime;
+
+ PROFILER_END();
+ PROFILER_RESET();
+ }
+
+ }
+}
+
+LRESULT
+DeviceManager::MsgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
+{
+ switch(uMsg)
+ {
+ case WM_DESTROY:
+ case WM_CLOSE:
+ PostQuitMessage(0);
+ return 0;
+
+ case WM_SYSKEYDOWN:
+ if(wParam == VK_F4)
+ {
+ PostQuitMessage(0);
+ return 0;
+ }
+ break;
+
+ case WM_ENTERSIZEMOVE:
+ m_InSizingModalLoop = true;
+ m_NewWindowSize.cx = m_SwapChainDesc.BufferDesc.Width;
+ m_NewWindowSize.cy = m_SwapChainDesc.BufferDesc.Height;
+ break;
+
+ case WM_EXITSIZEMOVE:
+ m_InSizingModalLoop = false;
+ ResizeSwapChain();
+ break;
+
+ case WM_SIZE:
+ // Ignore the WM_SIZE event if there is no device,
+ // or if the window has been minimized (size == 0),
+ // or if it has been restored to the previous size (this part is tested inside ResizeSwapChain)
+ if (m_Device && (lParam != 0))
+ {
+ m_NewWindowSize.cx = LOWORD(lParam);
+ m_NewWindowSize.cy = HIWORD(lParam);
+
+ if(!m_InSizingModalLoop)
+ ResizeSwapChain();
+ }
+ }
+
+ if( uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST ||
+ uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST )
+ {
+ // processing messages front-to-back
+ for(auto it = m_vControllers.begin(); it != m_vControllers.end(); it++)
+ {
+ if((*it)->IsEnabled())
+ {
+ // for kb/mouse messages, 0 means the message has been handled
+ if(0 == (*it)->MsgProc(hWnd, uMsg, wParam, lParam))
+ return 0;
+ }
+ }
+ }
+
+ return DefWindowProc(hWnd, uMsg, wParam, lParam);
+}
+
+void
+DeviceManager::ResizeSwapChain()
+{
+ if(m_NewWindowSize.cx == (LONG)m_SwapChainDesc.BufferDesc.Width &&
+ m_NewWindowSize.cy == (LONG)m_SwapChainDesc.BufferDesc.Height)
+ return;
+
+ m_SwapChainDesc.BufferDesc.Width = m_NewWindowSize.cx;
+ m_SwapChainDesc.BufferDesc.Height = m_NewWindowSize.cy;
+
+ ID3D11RenderTargetView *nullRTV = NULL;
+ m_ImmediateContext->OMSetRenderTargets(1, &nullRTV, NULL);
+ SAFE_RELEASE(m_BackBufferRTV);
+ SAFE_RELEASE(m_DepthStencilDSV);
+ SAFE_RELEASE(m_DepthStencilBuffer);
+
+ if (m_SwapChain)
+ {
+ // Resize the swap chain
+ m_SwapChain->ResizeBuffers(m_SwapChainDesc.BufferCount, m_SwapChainDesc.BufferDesc.Width,
+ m_SwapChainDesc.BufferDesc.Height, m_SwapChainDesc.BufferDesc.Format,
+ m_SwapChainDesc.Flags);
+
+ m_DepthStencilDesc.Width = m_NewWindowSize.cx;
+ m_DepthStencilDesc.Height = m_NewWindowSize.cy;
+
+ CreateRenderTargetAndDepthStencil();
+
+ BackBufferResized();
+ }
+}
+
+void
+DeviceManager::Render()
+{
+ PROFILER_SCOPED_FUNCTION();
+
+ D3D11_VIEWPORT viewport = { 0.0f, 0.0f, (float)m_SwapChainDesc.BufferDesc.Width, (float)m_SwapChainDesc.BufferDesc.Height, 0.0f, 1.0f };
+
+ // rendering back-to-front
+ for(auto it = m_vControllers.rbegin(); it != m_vControllers.rend(); it++)
+ {
+ if((*it)->IsEnabled())
+ {
+ m_ImmediateContext->OMSetRenderTargets(1, &m_BackBufferRTV, m_DepthStencilDSV);
+ m_ImmediateContext->RSSetViewports(1, &viewport);
+
+ (*it)->Render(m_Device, m_ImmediateContext, m_BackBufferRTV, m_DepthStencilDSV);
+ }
+ }
+
+ m_ImmediateContext->OMSetRenderTargets(0, NULL, NULL);
+}
+
+void
+DeviceManager::Animate(double fElapsedTimeSeconds)
+{
+ PROFILER_SCOPED_FUNCTION();
+
+ // front-to-back, but the order shouldn't matter
+ for(auto it = m_vControllers.begin(); it != m_vControllers.end(); it++)
+ {
+ if((*it)->IsEnabled())
+ {
+ (*it)->Animate(fElapsedTimeSeconds);
+ }
+ }
+}
+
+void
+DeviceManager::DeviceCreated()
+{
+ // creating resources front-to-back
+ for(auto it = m_vControllers.begin(); it != m_vControllers.end(); it++)
+ {
+ (*it)->DeviceCreated(m_Device);
+ }
+}
+
+void
+DeviceManager::DeviceDestroyed()
+{
+ // releasing resources back-to-front
+ for(auto it = m_vControllers.rbegin(); it != m_vControllers.rend(); it++)
+ {
+ (*it)->DeviceDestroyed();
+ }
+}
+
+void
+DeviceManager::BackBufferResized()
+{
+ if(m_SwapChain == NULL)
+ return;
+
+ DXGI_SURFACE_DESC backSD;
+ backSD.Format = m_SwapChainDesc.BufferDesc.Format;
+ backSD.Width = m_SwapChainDesc.BufferDesc.Width;
+ backSD.Height = m_SwapChainDesc.BufferDesc.Height;
+ backSD.SampleDesc = m_SwapChainDesc.SampleDesc;
+
+ for(auto it = m_vControllers.begin(); it != m_vControllers.end(); it++)
+ {
+ (*it)->BackBufferResized(m_Device, &backSD);
+ }
+}
+
+HRESULT
+DeviceManager::ChangeBackBufferFormat(DXGI_FORMAT format, UINT sampleCount)
+{
+ HRESULT hr = E_FAIL;
+
+ if((format == DXGI_FORMAT_UNKNOWN || format == m_SwapChainDesc.BufferDesc.Format) &&
+ (sampleCount == 0 || sampleCount == m_SwapChainDesc.SampleDesc.Count))
+ return S_FALSE;
+
+ if(m_Device)
+ {
+ bool fullscreen = (GetWindowState() == kWindowFullscreen);
+ if(fullscreen)
+ m_SwapChain->SetFullscreenState(false, NULL);
+
+ IDXGISwapChain* newSwapChain = NULL;
+ DXGI_SWAP_CHAIN_DESC newSwapChainDesc = m_SwapChainDesc;
+
+ if(format != DXGI_FORMAT_UNKNOWN)
+ newSwapChainDesc.BufferDesc.Format = format;
+ if(sampleCount != 0)
+ newSwapChainDesc.SampleDesc.Count = sampleCount;
+
+ IDXGIAdapter* pDXGIAdapter = GetDXGIAdapter();
+
+ IDXGIFactory* pDXGIFactory = NULL;
+ pDXGIAdapter->GetParent(__uuidof(IDXGIFactory), reinterpret_cast<void**>(&pDXGIFactory));
+
+ hr = pDXGIFactory->CreateSwapChain(m_Device, &newSwapChainDesc, &newSwapChain);
+
+ pDXGIFactory->Release();
+ pDXGIAdapter->Release();
+
+ if (FAILED(hr))
+ {
+ if(fullscreen)
+ m_SwapChain->SetFullscreenState(true, NULL);
+
+ return hr;
+ }
+
+ SAFE_RELEASE(m_BackBufferRTV);
+ SAFE_RELEASE(m_SwapChain);
+ SAFE_RELEASE(m_DepthStencilBuffer);
+ SAFE_RELEASE(m_DepthStencilDSV);
+
+ m_SwapChain = newSwapChain;
+ m_SwapChainDesc = newSwapChainDesc;
+
+ m_DepthStencilDesc.SampleDesc.Count = sampleCount;
+
+ if(fullscreen)
+ m_SwapChain->SetFullscreenState(true, NULL);
+
+ CreateRenderTargetAndDepthStencil();
+ BackBufferResized();
+ }
+
+ return S_OK;
+}
+
+void
+DeviceManager::AddControllerToFront(IVisualController* pController)
+{
+ m_vControllers.remove(pController);
+ m_vControllers.push_front(pController);
+}
+
+void
+DeviceManager::AddControllerToBack(IVisualController* pController)
+{
+ m_vControllers.remove(pController);
+ m_vControllers.push_back(pController);
+}
+
+void
+DeviceManager::RemoveController(IVisualController* pController)
+{
+ m_vControllers.remove(pController);
+}
+
+HRESULT
+DeviceManager::ResizeWindow(int width, int height)
+{
+ if(m_SwapChain == NULL)
+ return E_FAIL;
+
+ RECT rect;
+ GetWindowRect(m_hWnd, &rect);
+
+ ShowWindow(m_hWnd, SW_RESTORE);
+
+ if(!MoveWindow(m_hWnd, rect.left, rect.top, width, height, true))
+ return E_FAIL;
+
+ // No need to call m_SwapChain->ResizeBackBuffer because MoveWindow will send WM_SIZE, which calls that function.
+
+ return S_OK;
+}
+
+HRESULT
+DeviceManager::EnterFullscreenMode(int width, int height)
+{
+ if(m_SwapChain == NULL)
+ return E_FAIL;
+
+ if(GetWindowState() == kWindowFullscreen)
+ return S_FALSE;
+
+ if(width <= 0 || height <= 0)
+ {
+ width = m_SwapChainDesc.BufferDesc.Width;
+ height = m_SwapChainDesc.BufferDesc.Height;
+ }
+
+ SetWindowLong(m_hWnd, GWL_STYLE, WINDOW_STYLE_FULLSCREEN);
+ MoveWindow(m_hWnd, 0, 0, width, height, true);
+
+ HRESULT hr = m_SwapChain->SetFullscreenState(true, NULL);
+
+ if(FAILED(hr))
+ {
+ SetWindowLong(m_hWnd, GWL_STYLE, WINDOW_STYLE_NORMAL);
+ return hr;
+ }
+
+ UpdateWindow(m_hWnd);
+ m_SwapChain->GetDesc(&m_SwapChainDesc);
+
+ return S_OK;
+}
+
+HRESULT
+DeviceManager::LeaveFullscreenMode(int windowWidth, int windowHeight)
+{
+ if(m_SwapChain == NULL)
+ return E_FAIL;
+
+ if(GetWindowState() != kWindowFullscreen)
+ return S_FALSE;
+
+ HRESULT hr = m_SwapChain->SetFullscreenState(false, NULL);
+ if(FAILED(hr)) return hr;
+
+ SetWindowLong(m_hWnd, GWL_STYLE, WINDOW_STYLE_NORMAL);
+
+ if(windowWidth <= 0 || windowHeight <= 0)
+ {
+ windowWidth = m_SwapChainDesc.BufferDesc.Width;
+ windowHeight = m_SwapChainDesc.BufferDesc.Height;
+ }
+
+ RECT rect = { 0, 0, windowWidth, windowHeight };
+ AdjustWindowRect(&rect, WINDOW_STYLE_NORMAL, FALSE);
+ MoveWindow(m_hWnd, 0, 0, rect.right - rect.left, rect.bottom - rect.top, true);
+ UpdateWindow(m_hWnd);
+
+ m_SwapChain->GetDesc(&m_SwapChainDesc);
+
+ return S_OK;
+}
+
+HRESULT
+DeviceManager::ToggleFullscreen()
+{
+ if(GetWindowState() == kWindowFullscreen)
+ return LeaveFullscreenMode();
+ else
+ return EnterFullscreenMode();
+}
+
+DeviceManager::WindowState
+DeviceManager::GetWindowState()
+{
+ if(m_SwapChain && !m_SwapChainDesc.Windowed)
+ return kWindowFullscreen;
+
+ if(m_hWnd == INVALID_HANDLE_VALUE)
+ return kWindowNone;
+
+ if(IsZoomed(m_hWnd))
+ return kWindowMaximized;
+
+ if(IsIconic(m_hWnd))
+ return kWindowMinimized;
+
+ return kWindowNormal;
+}
+
+HRESULT
+DeviceManager::GetDisplayResolution(int& width, int& height)
+{
+ if(m_hWnd != INVALID_HANDLE_VALUE)
+ {
+ HMONITOR monitor = MonitorFromWindow(m_hWnd, MONITOR_DEFAULTTOPRIMARY);
+ MONITORINFO info;
+ info.cbSize = sizeof(MONITORINFO);
+
+ if(GetMonitorInfo(monitor, &info))
+ {
+ width = info.rcMonitor.right - info.rcMonitor.left;
+ height = info.rcMonitor.bottom - info.rcMonitor.top;
+ return S_OK;
+ }
+ }
+
+ return E_FAIL;
+}
+
+IDXGIAdapter*
+DeviceManager::GetDXGIAdapter()
+{
+ if(!m_Device)
+ return NULL;
+
+ IDXGIDevice* pDXGIDevice = NULL;
+ m_Device->QueryInterface(__uuidof(IDXGIDevice), reinterpret_cast<void**>(&pDXGIDevice));
+
+ IDXGIAdapter* pDXGIAdapter = NULL;
+ pDXGIDevice->GetParent(__uuidof(IDXGIAdapter), reinterpret_cast<void**>(&pDXGIAdapter));
+
+ pDXGIDevice->Release();
+
+ return pDXGIAdapter;
+}
diff --git a/NvCloth/samples/SampleBase/core/DeviceManager.h b/NvCloth/samples/SampleBase/core/DeviceManager.h
new file mode 100644
index 0000000..98b8eee
--- /dev/null
+++ b/NvCloth/samples/SampleBase/core/DeviceManager.h
@@ -0,0 +1,166 @@
+// TAGRELEASE: PUBLIC
+
+#pragma once
+#include <Windows.h>
+#include <DXGI.h>
+#include <D3D11.h>
+#include <list>
+
+
+struct DeviceCreationParameters
+{
+ bool startMaximized;
+ bool startFullscreen;
+ int backBufferWidth;
+ int backBufferHeight;
+ int refreshRate;
+ int swapChainBufferCount;
+ DXGI_FORMAT swapChainFormat;
+ DXGI_FORMAT depthStencilFormat;
+ DXGI_USAGE swapChainUsage;
+ int swapChainSampleCount;
+ int swapChainSampleQuality;
+ UINT createDeviceFlags;
+ D3D_DRIVER_TYPE driverType;
+ D3D_FEATURE_LEVEL featureLevel;
+
+ // For use in the case of multiple adapters. If this is non-null, device creation will try to match
+ // the given string against an adapter name. If the specified string exists as a sub-string of the
+ // adapter name, the device and window will be created on that adapter. Case sensitive.
+ const WCHAR* adapterNameSubstring;
+
+ DeviceCreationParameters()
+ : startMaximized(false)
+ , startFullscreen(false)
+ , backBufferWidth(1280)
+ , backBufferHeight(720)
+ , refreshRate(0)
+ , swapChainBufferCount(1)
+ , swapChainFormat(DXGI_FORMAT_R8G8B8A8_UNORM)
+ , depthStencilFormat(DXGI_FORMAT_D24_UNORM_S8_UINT)
+ , swapChainUsage(DXGI_USAGE_SHADER_INPUT | DXGI_USAGE_RENDER_TARGET_OUTPUT)
+ , swapChainSampleCount(1)
+ , swapChainSampleQuality(0)
+ , createDeviceFlags(0)
+ , driverType(D3D_DRIVER_TYPE_HARDWARE)
+ , featureLevel(D3D_FEATURE_LEVEL_11_0)
+ , adapterNameSubstring(L"")
+ { }
+};
+
+#pragma warning(push)
+#pragma warning(disable: 4100) // unreferenced formal parameter
+class IVisualController
+{
+private:
+ bool m_Enabled;
+public:
+ IVisualController() : m_Enabled(true) { }
+
+ virtual LRESULT MsgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { return 1; }
+ virtual void Render(ID3D11Device* pDevice, ID3D11DeviceContext* pDeviceContext, ID3D11RenderTargetView* pRTV, ID3D11DepthStencilView* pDSV) { }
+ virtual void Animate(double fElapsedTimeSeconds) { }
+ virtual HRESULT DeviceCreated(ID3D11Device* pDevice) { return S_OK; }
+ virtual void DeviceDestroyed() { }
+ virtual void BackBufferResized(ID3D11Device* pDevice, const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc) { }
+
+ virtual void EnableController() { m_Enabled = true; }
+ virtual void DisableController() { m_Enabled = false; }
+ virtual bool IsEnabled() { return m_Enabled; }
+};
+#pragma warning(pop)
+
+class DeviceManager
+{
+public:
+ enum WindowState
+ {
+ kWindowNone,
+ kWindowNormal,
+ kWindowMinimized,
+ kWindowMaximized,
+ kWindowFullscreen
+ };
+
+protected:
+ ID3D11Device* m_Device;
+ ID3D11DeviceContext* m_ImmediateContext;
+ IDXGISwapChain* m_SwapChain;
+ ID3D11RenderTargetView* m_BackBufferRTV;
+ ID3D11Texture2D* m_DepthStencilBuffer;
+ ID3D11DepthStencilView* m_DepthStencilDSV;
+ DXGI_SWAP_CHAIN_DESC m_SwapChainDesc;
+ D3D11_TEXTURE2D_DESC m_DepthStencilDesc;
+ bool m_IsNvidia;
+ HWND m_hWnd;
+ std::list<IVisualController*> m_vControllers;
+ std::wstring m_WindowTitle;
+ double m_FixedFrameInterval;
+ UINT m_SyncInterval;
+ std::list<double> m_vFrameTimes;
+ double m_AverageFrameTime;
+ double m_AverageTimeUpdateInterval;
+ bool m_InSizingModalLoop;
+ SIZE m_NewWindowSize;
+private:
+ HRESULT CreateRenderTargetAndDepthStencil();
+ void ResizeSwapChain();
+public:
+
+ DeviceManager()
+ : m_Device(NULL)
+ , m_ImmediateContext(NULL)
+ , m_SwapChain(NULL)
+ , m_BackBufferRTV(NULL)
+ , m_DepthStencilBuffer(NULL)
+ , m_DepthStencilDSV(NULL)
+ , m_IsNvidia(false)
+ , m_hWnd(NULL)
+ , m_WindowTitle(L"")
+ , m_FixedFrameInterval(-1)
+ , m_SyncInterval(0)
+ , m_AverageFrameTime(0)
+ , m_AverageTimeUpdateInterval(0.5)
+ , m_InSizingModalLoop(false)
+ { }
+
+ virtual ~DeviceManager()
+ { Shutdown(); }
+
+ virtual HRESULT CreateWindowDeviceAndSwapChain(const DeviceCreationParameters& params, std::wstring windowTitle);
+ virtual HRESULT ChangeBackBufferFormat(DXGI_FORMAT format, UINT sampleCount);
+ virtual HRESULT ResizeWindow(int width, int height);
+ virtual HRESULT EnterFullscreenMode(int width = 0, int height = 0);
+ virtual HRESULT LeaveFullscreenMode(int windowWidth = 0, int windowHeight = 0);
+ virtual HRESULT ToggleFullscreen();
+
+ virtual void Shutdown();
+ virtual void MessageLoop();
+ virtual LRESULT MsgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
+ virtual void Render();
+ virtual void Animate(double fElapsedTimeSeconds);
+ virtual void DeviceCreated();
+ virtual void DeviceDestroyed();
+ virtual void BackBufferResized();
+
+ void AddControllerToFront(IVisualController* pController);
+ void AddControllerToBack(IVisualController* pController);
+ void RemoveController(IVisualController* pController);
+
+ void SetFixedFrameInterval(double seconds) { m_FixedFrameInterval = seconds; }
+ void DisableFixedFrameInterval() { m_FixedFrameInterval = -1; }
+
+ bool IsNvidia() const { return m_IsNvidia; }
+ HWND GetHWND() { return m_hWnd; }
+ ID3D11Device* GetDevice() { return m_Device; }
+ WindowState GetWindowState();
+ bool GetVsyncEnabled() { return m_SyncInterval > 0; }
+ void SetVsyncEnabled(bool enabled) { m_SyncInterval = enabled ? 1 : 0; }
+ HRESULT GetDisplayResolution(int& width, int& height);
+ IDXGIAdapter* GetDXGIAdapter();
+ double GetAverageFrameTime() { return m_AverageFrameTime; }
+ void SetAverageTimeUpdateInterval(double value) { m_AverageTimeUpdateInterval = value; }
+};
+
+
+DeviceManager* GetDeviceManager();
diff --git a/NvCloth/samples/SampleBase/core/SampleController.cpp b/NvCloth/samples/SampleBase/core/SampleController.cpp
new file mode 100644
index 0000000..82278d3
--- /dev/null
+++ b/NvCloth/samples/SampleBase/core/SampleController.cpp
@@ -0,0 +1,25 @@
+/*
+* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
+*
+* NVIDIA CORPORATION and its licensors retain all intellectual property
+* and proprietary rights in and to this software, related documentation
+* and any modifications thereto. Any use, reproduction, disclosure or
+* distribution of this software and related documentation without an express
+* license agreement from NVIDIA CORPORATION is strictly prohibited.
+*/
+
+#include "SampleController.h"
+#include "SceneController.h"
+#include "CommonUIController.h"
+
+SampleController::SampleController()
+{
+}
+
+SampleController::~SampleController()
+{
+}
+
+void SampleController::onSampleStart()
+{
+}
diff --git a/NvCloth/samples/SampleBase/core/SampleController.h b/NvCloth/samples/SampleBase/core/SampleController.h
new file mode 100644
index 0000000..2178a3a
--- /dev/null
+++ b/NvCloth/samples/SampleBase/core/SampleController.h
@@ -0,0 +1,42 @@
+/*
+* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
+*
+* NVIDIA CORPORATION and its licensors retain all intellectual property
+* and proprietary rights in and to this software, related documentation
+* and any modifications thereto. Any use, reproduction, disclosure or
+* distribution of this software and related documentation without an express
+* license agreement from NVIDIA CORPORATION is strictly prohibited.
+*/
+
+#ifndef SAMPLE_CONTROLLER_H
+#define SAMPLE_CONTROLLER_H
+
+#include "SampleManager.h"
+
+class SampleController : public ISampleController
+{
+public:
+ SampleController();
+ virtual ~SampleController();
+
+ virtual void onSampleStart();
+
+private:
+ SampleController& operator= (SampleController&);
+
+
+ //////// used controllers ////////
+
+ SceneController& getSceneController() const
+ {
+ return getManager()->getSceneController();
+ }
+
+ CommonUIController& getCommonUIController() const
+ {
+ return getManager()->getCommonUIController();
+ }
+
+};
+
+#endif \ No newline at end of file
diff --git a/NvCloth/samples/SampleBase/core/SampleManager.cpp b/NvCloth/samples/SampleBase/core/SampleManager.cpp
new file mode 100644
index 0000000..86438c6
--- /dev/null
+++ b/NvCloth/samples/SampleBase/core/SampleManager.cpp
@@ -0,0 +1,60 @@
+/*
+* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
+*
+* NVIDIA CORPORATION and its licensors retain all intellectual property
+* and proprietary rights in and to this software, related documentation
+* and any modifications thereto. Any use, reproduction, disclosure or
+* distribution of this software and related documentation without an express
+* license agreement from NVIDIA CORPORATION is strictly prohibited.
+*/
+
+#include "SampleManager.h"
+
+#include "Utils.h"
+
+#include "Renderer.h"
+#include "CommonUIController.h"
+#include "SceneController.h"
+#include "SampleController.h"
+
+
+SampleManager::SampleManager(const SampleConfig& config)
+: m_config(config)
+{
+}
+
+int SampleManager::run()
+{
+ Application app(getConfig().sampleName);
+
+ m_renderer = new Renderer();
+ m_sceneController = new SceneController();
+ m_sampleController = new SampleController();
+ m_commonUIController = new CommonUIController();
+
+ app.addControllerToFront(m_renderer);
+ app.addControllerToFront(m_sceneController);
+ app.addControllerToFront(m_sampleController);
+ app.addControllerToFront(m_commonUIController);
+
+ for (IApplicationController* c : app.getControllers())
+ {
+ (static_cast<ISampleController*>(c))->setManager(this);
+ }
+
+ int result = app.run();
+
+ delete m_renderer;
+ delete m_sceneController;
+ delete m_sampleController;
+ delete m_commonUIController;
+
+ return result;
+}
+
+
+int runSample(const SampleConfig& config)
+{
+ SampleManager sampleManager(config);
+ return sampleManager.run();
+} \ No newline at end of file
diff --git a/NvCloth/samples/SampleBase/core/SampleManager.h b/NvCloth/samples/SampleBase/core/SampleManager.h
new file mode 100644
index 0000000..653d285
--- /dev/null
+++ b/NvCloth/samples/SampleBase/core/SampleManager.h
@@ -0,0 +1,91 @@
+/*
+* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
+*
+* NVIDIA CORPORATION and its licensors retain all intellectual property
+* and proprietary rights in and to this software, related documentation
+* and any modifications thereto. Any use, reproduction, disclosure or
+* distribution of this software and related documentation without an express
+* license agreement from NVIDIA CORPORATION is strictly prohibited.
+*/
+
+#ifndef SAMPLE_MANAGER_H
+#define SAMPLE_MANAGER_H
+
+#include "Application.h"
+#include "Sample.h"
+
+
+class SampleManager;
+
+class ISampleController : public IApplicationController
+{
+public:
+
+ void setManager(SampleManager* manager)
+ {
+ m_manager = manager;
+ }
+protected:
+
+ SampleManager* getManager() const
+ {
+ return m_manager;
+ }
+
+private:
+ SampleManager* m_manager;
+};
+
+
+class Renderer;
+class PhysXController;
+class SceneController;
+class SampleController;
+class CommonUIController;
+
+
+/**
+*/
+class SampleManager
+{
+ public:
+ SampleManager(const SampleConfig& config);
+ int run();
+
+ Renderer& getRenderer()
+ {
+ return *m_renderer;
+ }
+
+ SceneController& getSceneController() const
+ {
+ return *m_sceneController;
+ }
+
+ SampleController& getSampleController() const
+ {
+ return *m_sampleController;
+ }
+
+ CommonUIController& getCommonUIController() const
+ {
+ return *m_commonUIController;
+ }
+
+ const SampleConfig& getConfig() const
+ {
+ return m_config;
+ }
+
+
+ private:
+ Renderer* m_renderer;
+ SceneController* m_sceneController;
+ SampleController* m_sampleController;
+ CommonUIController* m_commonUIController;
+
+ const SampleConfig& m_config;
+};
+
+
+#endif \ No newline at end of file
diff --git a/NvCloth/samples/SampleBase/renderer/ClothRenderMesh.cpp b/NvCloth/samples/SampleBase/renderer/ClothRenderMesh.cpp
new file mode 100644
index 0000000..60bb5f3
--- /dev/null
+++ b/NvCloth/samples/SampleBase/renderer/ClothRenderMesh.cpp
@@ -0,0 +1,211 @@
+/*
+* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
+*
+* NVIDIA CORPORATION and its licensors retain all intellectual property
+* and proprietary rights in and to this software, related documentation
+* and any modifications thereto. Any use, reproduction, disclosure or
+* distribution of this software and related documentation without an express
+* license agreement from NVIDIA CORPORATION is strictly prohibited.
+*/
+
+
+#include "ClothRenderMesh.h"
+#include "Renderer.h"
+
+#include <NvClothExt/ClothFabricCooker.h>
+#include "foundation/PxStrideIterator.h"
+
+using namespace nv;
+using namespace cloth;
+
+
+template <typename T>
+void gatherIndices(std::vector<uint16_t>& indices,
+ const BoundedData& triangles, const BoundedData& quads)
+{
+ PxStrideIterator<const T> tIt, qIt;
+
+ indices.reserve(triangles.count * 3 + quads.count * 6);
+
+ tIt = PxMakeIterator(reinterpret_cast<const T*>(triangles.data), triangles.stride);
+ for (PxU32 i = 0; i < triangles.count; ++i, ++tIt)
+ {
+ indices.push_back(static_cast<uint16_t>(tIt.ptr()[0]));
+ indices.push_back(static_cast<uint16_t>(tIt.ptr()[1]));
+ indices.push_back(static_cast<uint16_t>(tIt.ptr()[2]));
+ }
+ qIt = PxMakeIterator(reinterpret_cast<const T*>(quads.data), quads.stride);
+ for (PxU32 i = 0; i < quads.count; ++i, ++qIt)
+ {
+ indices.push_back(static_cast<uint16_t>(qIt.ptr()[0]));
+ indices.push_back(static_cast<uint16_t>(qIt.ptr()[1]));
+ indices.push_back(static_cast<uint16_t>(qIt.ptr()[2]));
+ indices.push_back(static_cast<uint16_t>(qIt.ptr()[0]));
+ indices.push_back(static_cast<uint16_t>(qIt.ptr()[2]));
+ indices.push_back(static_cast<uint16_t>(qIt.ptr()[3]));
+ }
+}
+
+ClothRenderMesh::ClothRenderMesh(const ClothMeshDesc& desc)
+{
+ uint32_t numVertices = desc.points.count;
+ mVertices.resize(numVertices);
+
+ PxStrideIterator<const PxVec3> pIt(reinterpret_cast<const PxVec3*>(desc.points.data), desc.points.stride);
+ for (PxU32 i = 0; i < numVertices; ++i)
+ {
+ mVertices[i].position = *pIt++;
+ mVertices[i].normal = PxVec3(0.f);
+ }
+
+ // build triangle indices
+ if (desc.flags & MeshFlag::e16_BIT_INDICES)
+ gatherIndices<PxU16>(mIndices, desc.triangles, desc.quads);
+ else
+ gatherIndices<PxU32>(mIndices, desc.triangles, desc.quads);
+
+ for (PxU32 i = 0; i < mIndices.size(); i += 3)
+ {
+ auto p0 = mVertices[mIndices[i]].position;
+ auto p1 = mVertices[mIndices[i + 1]].position;
+ auto p2 = mVertices[mIndices[i + 2]].position;
+
+ auto normal = ((p2 - p0).cross(p1 - p0)).getNormalized();
+
+ mVertices[mIndices[i]].normal += normal;
+ mVertices[mIndices[i + 1]].normal += normal;
+ mVertices[mIndices[i + 2]].normal += normal;
+ }
+
+ for (PxU32 i = 0; i < numVertices; ++i)
+ mVertices[i].normal.normalize();
+
+ std::vector<D3D11_INPUT_ELEMENT_DESC> layout;
+ layout.push_back({ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 });
+ layout.push_back({ "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 });
+
+ initialize(mVertices.data(), (uint32_t)mVertices.size(), sizeof(Vertex), layout, mIndices.data(), (uint32_t)mIndices.size() / 3);
+}
+
+ClothRenderMesh::ClothRenderMesh()
+ : mIndexBuffer(nullptr)
+{
+}
+
+ClothRenderMesh::ClothRenderMesh(const void* vertices, uint32_t numVertices, uint32_t vertexSize,
+ std::vector<D3D11_INPUT_ELEMENT_DESC>& inputDesc, const uint16_t* faces, uint32_t numFaces)
+ : mIndexBuffer(nullptr)
+{
+ initialize(vertices, numVertices, vertexSize, inputDesc, faces, numFaces);
+}
+
+void ClothRenderMesh::initialize(const void* vertices, uint32_t numVertices, uint32_t vertexSize,
+ std::vector<D3D11_INPUT_ELEMENT_DESC>& inputDesc, const uint16_t* faces, uint32_t numFaces)
+{
+ mDevice = GetDeviceManager()->GetDevice();
+
+ mInputDesc = inputDesc;
+ mNumVertices = numVertices;
+ mVertexSize = vertexSize;
+ mNumFaces = numFaces;
+
+ // VB
+ {
+ D3D11_SUBRESOURCE_DATA vertexBufferData;
+
+ ZeroMemory(&vertexBufferData, sizeof(vertexBufferData));
+ vertexBufferData.pSysMem = vertices;
+
+ D3D11_BUFFER_DESC bufferDesc;
+
+ memset(&bufferDesc, 0, sizeof(D3D11_BUFFER_DESC));
+ bufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
+ bufferDesc.ByteWidth = vertexSize * numVertices;
+ bufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
+ bufferDesc.MiscFlags = 0;
+ bufferDesc.Usage = D3D11_USAGE_DYNAMIC;
+
+ V(mDevice->CreateBuffer(&bufferDesc, &vertexBufferData, &mVertexBuffer));
+ }
+
+ // IB
+ if (faces != nullptr)
+ {
+ D3D11_SUBRESOURCE_DATA indexBufferData;
+
+ ZeroMemory(&indexBufferData, sizeof(indexBufferData));
+ indexBufferData.pSysMem = faces;
+
+ D3D11_BUFFER_DESC bufferDesc;
+
+ memset(&bufferDesc, 0, sizeof(D3D11_BUFFER_DESC));
+ bufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
+ bufferDesc.ByteWidth = sizeof(uint16_t) * numFaces*3;
+ bufferDesc.CPUAccessFlags = 0;
+ bufferDesc.MiscFlags = 0;
+ bufferDesc.Usage = D3D11_USAGE_IMMUTABLE;
+
+ V(mDevice->CreateBuffer(&bufferDesc, &indexBufferData, &mIndexBuffer));
+ }
+}
+
+ClothRenderMesh::~ClothRenderMesh()
+{
+ SAFE_RELEASE(mVertexBuffer);
+ SAFE_RELEASE(mIndexBuffer);
+}
+
+void ClothRenderMesh::update(const PxVec3* positions, uint32_t numVertices)
+{
+ PxStrideIterator<const PxVec3> pIt(positions, sizeof(PxVec3));
+ for (PxU32 i = 0; i < numVertices; ++i)
+ {
+ mVertices[i].position = *pIt++;
+ mVertices[i].normal = PxVec3(0.f);
+ }
+
+ for (PxU32 i = 0; i < mIndices.size(); i += 3)
+ {
+ auto p0 = mVertices[mIndices[i]].position;
+ auto p1 = mVertices[mIndices[i + 1]].position;
+ auto p2 = mVertices[mIndices[i + 2]].position;
+
+ auto normal = ((p2 - p0).cross(p1 - p0)).getNormalized();
+
+ mVertices[mIndices[i]].normal += normal;
+ mVertices[mIndices[i + 1]].normal += normal;
+ mVertices[mIndices[i + 2]].normal += normal;
+ }
+
+ for (PxU32 i = 0; i < numVertices; ++i)
+ mVertices[i].normal.normalize();
+
+ ID3D11DeviceContext* context;
+ mDevice->GetImmediateContext(&context);
+
+ {
+ D3D11_MAPPED_SUBRESOURCE mappedResource;
+ ZeroMemory(&mappedResource, sizeof(D3D11_MAPPED_SUBRESOURCE));
+
+ V(context->Map(mVertexBuffer, 0, D3D11_MAP_WRITE_DISCARD, NULL, &mappedResource));
+ memcpy(mappedResource.pData, mVertices.data(), sizeof(Vertex) * mVertices.size());
+
+ context->Unmap(mVertexBuffer, 0);
+ }
+}
+
+void ClothRenderMesh::render(ID3D11DeviceContext& context) const
+{
+ context.IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
+
+ UINT strides[1] = { mVertexSize };
+ UINT offsets[1] = { 0 };
+ context.IASetVertexBuffers(0, 1, &mVertexBuffer, strides, offsets);
+
+ context.IASetIndexBuffer(mIndexBuffer, DXGI_FORMAT_R16_UINT, 0);
+
+ if (mIndexBuffer)
+ context.DrawIndexed(mNumFaces*3, 0, 0);
+ else
+ context.Draw(mNumVertices, 0);
+} \ No newline at end of file
diff --git a/NvCloth/samples/SampleBase/renderer/ClothRenderMesh.h b/NvCloth/samples/SampleBase/renderer/ClothRenderMesh.h
new file mode 100644
index 0000000..1af0027
--- /dev/null
+++ b/NvCloth/samples/SampleBase/renderer/ClothRenderMesh.h
@@ -0,0 +1,68 @@
+/*
+* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
+*
+* NVIDIA CORPORATION and its licensors retain all intellectual property
+* and proprietary rights in and to this software, related documentation
+* and any modifications thereto. Any use, reproduction, disclosure or
+* distribution of this software and related documentation without an express
+* license agreement from NVIDIA CORPORATION is strictly prohibited.
+*/
+
+#ifndef CLOTH_RENDER_MESH_H
+#define CLOTH_RENDER_MESH_H
+
+#include "Renderable.h"
+
+namespace nv
+{
+namespace cloth
+{
+ class ClothMeshDesc;
+}
+}
+
+struct Vertex
+{
+ PxVec3 position;
+ PxVec3 normal;
+};
+
+/**
+Simple �loth render mesh
+*/
+class ClothRenderMesh : public IRenderMesh
+{
+public:
+ ClothRenderMesh(const void* vertices, uint32_t numVertices, uint32_t vertexSize,
+ std::vector<D3D11_INPUT_ELEMENT_DESC>& inputDesc, const uint16_t* faces = nullptr, uint32_t numFaces = 0);
+ ClothRenderMesh(const nv::cloth::ClothMeshDesc& desc);
+ virtual ~ClothRenderMesh();
+
+ void update(const PxVec3* positions, uint32_t numVertices);
+
+ const std::vector<D3D11_INPUT_ELEMENT_DESC>& getInputElementDesc() const { return mInputDesc; }
+ void render(ID3D11DeviceContext& context) const;
+
+protected:
+ ClothRenderMesh();
+ void initialize(const void* vertices, uint32_t numVertices, uint32_t vertexSize,
+ std::vector<D3D11_INPUT_ELEMENT_DESC>& inputDesc, const uint16_t* faces, uint32_t numFaces);
+
+private:
+ ID3D11Device* mDevice;
+
+ ID3D11Buffer* mVertexBuffer;
+ ID3D11Buffer* mIndexBuffer;
+
+ std::vector<Vertex> mVertices;
+ std::vector<uint16_t> mIndices;
+
+ uint32_t mNumFaces;
+ uint32_t mNumVertices;
+ uint32_t mVertexSize;
+
+ std::vector<D3D11_INPUT_ELEMENT_DESC> mInputDesc;
+};
+
+
+#endif //CLOTH_RENDER_MESH_H \ No newline at end of file
diff --git a/NvCloth/samples/SampleBase/renderer/ConvexRenderMesh.cpp b/NvCloth/samples/SampleBase/renderer/ConvexRenderMesh.cpp
new file mode 100644
index 0000000..11ba43b
--- /dev/null
+++ b/NvCloth/samples/SampleBase/renderer/ConvexRenderMesh.cpp
@@ -0,0 +1,84 @@
+/*
+* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
+*
+* NVIDIA CORPORATION and its licensors retain all intellectual property
+* and proprietary rights in and to this software, related documentation
+* and any modifications thereto. Any use, reproduction, disclosure or
+* distribution of this software and related documentation without an express
+* license agreement from NVIDIA CORPORATION is strictly prohibited.
+*/
+
+#if 0
+#include "ConvexRenderMesh.h"
+#include "Renderer.h"
+#include "PxConvexMesh.h"
+
+
+struct Vertex
+{
+ PxVec3 position;
+ PxVec3 normal;
+};
+
+ConvexRenderMesh::ConvexRenderMesh(const PxConvexMesh* mesh)
+{
+ const uint32_t nbPolygons = mesh->getNbPolygons();
+ const uint8_t* indexBuffer = mesh->getIndexBuffer();
+ const PxVec3* meshVertices = mesh->getVertices();
+
+ uint32_t nbVerts = 0;
+ uint32_t nbFaces = 0;
+
+ for (uint32_t i = 0; i < nbPolygons; i++)
+ {
+ PxHullPolygon data;
+ mesh->getPolygonData(i, data);
+ uint32_t nbPolyVerts = data.mNbVerts;
+ nbVerts += nbPolyVerts;
+ nbFaces += (nbPolyVerts - 2) * 3;
+ }
+
+ std::vector<Vertex> vertices;
+ std::vector<uint16_t> faces;
+
+ vertices.resize(nbVerts);
+ faces.resize(nbFaces);
+
+ uint32_t vertCounter = 0;
+ uint32_t facesCounter = 0;
+ for (uint32_t i = 0; i < nbPolygons; i++)
+ {
+ PxHullPolygon data;
+ mesh->getPolygonData(i, data);
+
+ PxVec3 normal(data.mPlane[0], data.mPlane[1], data.mPlane[2]);
+
+ uint32_t vI0 = vertCounter;
+ for (uint32_t vI = 0; vI < data.mNbVerts; vI++)
+ {
+ vertices[vertCounter].position = meshVertices[indexBuffer[data.mIndexBase + vI]];
+ vertices[vertCounter].normal = normal;
+ vertCounter++;
+ }
+
+ for (uint32_t vI = 1; vI < uint32_t(data.mNbVerts) - 1; vI++)
+ {
+ faces[facesCounter++] = uint16_t(vI0);
+ faces[facesCounter++] = uint16_t(vI0 + vI + 1);
+ faces[facesCounter++] = uint16_t(vI0 + vI);
+ }
+ }
+
+ std::vector<D3D11_INPUT_ELEMENT_DESC> layout;
+ layout.push_back({ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 });
+ layout.push_back({ "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 });
+
+ initialize(vertices.data(), (uint32_t)vertices.size(), sizeof(Vertex), layout, faces.data(), nbFaces);
+}
+
+
+ConvexRenderMesh::~ConvexRenderMesh()
+{
+}
+
+#endif \ No newline at end of file
diff --git a/NvCloth/samples/SampleBase/renderer/ConvexRenderMesh.h b/NvCloth/samples/SampleBase/renderer/ConvexRenderMesh.h
new file mode 100644
index 0000000..b6935d6
--- /dev/null
+++ b/NvCloth/samples/SampleBase/renderer/ConvexRenderMesh.h
@@ -0,0 +1,34 @@
+/*
+* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
+*
+* NVIDIA CORPORATION and its licensors retain all intellectual property
+* and proprietary rights in and to this software, related documentation
+* and any modifications thereto. Any use, reproduction, disclosure or
+* distribution of this software and related documentation without an express
+* license agreement from NVIDIA CORPORATION is strictly prohibited.
+*/
+
+#ifndef CONVEX_RENDER_MESH_H
+#define CONVEX_RENDER_MESH_H
+
+#include "CustomRenderMesh.h"
+
+namespace physx
+{
+class PxConvexMesh;
+}
+
+
+/**
+PxConvexMesh render mesh
+(this class relates to PhysX more then to Renderer)
+*/
+class ConvexRenderMesh : public CustomRenderMesh
+{
+public:
+ ConvexRenderMesh(const PxConvexMesh* mesh);
+ virtual ~ConvexRenderMesh();
+};
+
+
+#endif //CONVEX_RENDER_MESH_H \ No newline at end of file
diff --git a/NvCloth/samples/SampleBase/renderer/CustomRenderMesh.cpp b/NvCloth/samples/SampleBase/renderer/CustomRenderMesh.cpp
new file mode 100644
index 0000000..8dfbbb6
--- /dev/null
+++ b/NvCloth/samples/SampleBase/renderer/CustomRenderMesh.cpp
@@ -0,0 +1,98 @@
+/*
+* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
+*
+* NVIDIA CORPORATION and its licensors retain all intellectual property
+* and proprietary rights in and to this software, related documentation
+* and any modifications thereto. Any use, reproduction, disclosure or
+* distribution of this software and related documentation without an express
+* license agreement from NVIDIA CORPORATION is strictly prohibited.
+*/
+
+#include "CustomRenderMesh.h"
+
+
+CustomRenderMesh::CustomRenderMesh()
+ : m_indexBuffer(nullptr)
+{
+}
+
+CustomRenderMesh::CustomRenderMesh(const void* vertices, uint32_t numVertices, uint32_t vertexSize,
+ std::vector<D3D11_INPUT_ELEMENT_DESC>& inputDesc, const uint16_t* faces, uint32_t numFaces)
+ : m_indexBuffer(nullptr)
+{
+ initialize(vertices, numVertices, vertexSize, inputDesc, faces, numFaces);
+}
+
+void CustomRenderMesh::initialize(const void* vertices, uint32_t numVertices, uint32_t vertexSize,
+ std::vector<D3D11_INPUT_ELEMENT_DESC>& inputDesc, const uint16_t* faces, uint32_t numFaces)
+{
+ ID3D11Device* device = GetDeviceManager()->GetDevice();
+
+ m_inputDesc = inputDesc;
+ m_numVertices = numVertices;
+ m_vertexSize = vertexSize;
+ m_numFaces = numFaces;
+
+ // VB
+ {
+ D3D11_SUBRESOURCE_DATA vertexBufferData;
+
+ ZeroMemory(&vertexBufferData, sizeof(vertexBufferData));
+ vertexBufferData.pSysMem = vertices;
+
+ D3D11_BUFFER_DESC bufferDesc;
+
+ memset(&bufferDesc, 0, sizeof(D3D11_BUFFER_DESC));
+ bufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
+ bufferDesc.ByteWidth = vertexSize * numVertices;
+ bufferDesc.CPUAccessFlags = 0;
+ bufferDesc.MiscFlags = 0;
+ bufferDesc.Usage = D3D11_USAGE_IMMUTABLE;
+
+ V(device->CreateBuffer(&bufferDesc, &vertexBufferData, &m_vertexBuffer));
+ }
+
+ // IB
+ if (faces != nullptr)
+ {
+ D3D11_SUBRESOURCE_DATA indexBufferData;
+
+ ZeroMemory(&indexBufferData, sizeof(indexBufferData));
+ indexBufferData.pSysMem = faces;
+
+ D3D11_BUFFER_DESC bufferDesc;
+
+ memset(&bufferDesc, 0, sizeof(D3D11_BUFFER_DESC));
+ bufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
+ bufferDesc.ByteWidth = sizeof(uint16_t) * numFaces;
+ bufferDesc.CPUAccessFlags = 0;
+ bufferDesc.MiscFlags = 0;
+ bufferDesc.Usage = D3D11_USAGE_IMMUTABLE;
+
+ V(device->CreateBuffer(&bufferDesc, &indexBufferData, &m_indexBuffer));
+ }
+}
+
+CustomRenderMesh::~CustomRenderMesh()
+{
+ SAFE_RELEASE(m_vertexBuffer);
+ SAFE_RELEASE(m_indexBuffer);
+}
+
+
+void CustomRenderMesh::render(ID3D11DeviceContext& context) const
+{
+ context.IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
+
+ UINT strides[1] = { m_vertexSize };
+ UINT offsets[1] = { 0 };
+ context.IASetVertexBuffers(0, 1, &m_vertexBuffer, strides, offsets);
+
+ context.IASetIndexBuffer(m_indexBuffer, DXGI_FORMAT_R16_UINT, 0);
+
+ if (m_indexBuffer)
+ context.DrawIndexed(m_numFaces, 0, 0);
+ else
+ context.Draw(m_numVertices, 0);
+}
+
diff --git a/NvCloth/samples/SampleBase/renderer/CustomRenderMesh.h b/NvCloth/samples/SampleBase/renderer/CustomRenderMesh.h
new file mode 100644
index 0000000..c00eafc
--- /dev/null
+++ b/NvCloth/samples/SampleBase/renderer/CustomRenderMesh.h
@@ -0,0 +1,43 @@
+/*
+* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
+*
+* NVIDIA CORPORATION and its licensors retain all intellectual property
+* and proprietary rights in and to this software, related documentation
+* and any modifications thereto. Any use, reproduction, disclosure or
+* distribution of this software and related documentation without an express
+* license agreement from NVIDIA CORPORATION is strictly prohibited.
+*/
+
+#ifndef CUSTOM_RENDER_MESH_H
+#define CUSTOM_RENDER_MESH_H
+
+#include "Renderable.h"
+
+
+class CustomRenderMesh : public IRenderMesh
+{
+public:
+ const std::vector<D3D11_INPUT_ELEMENT_DESC>& getInputElementDesc() const { return m_inputDesc; }
+ void render(ID3D11DeviceContext& context) const;
+
+ CustomRenderMesh(const void* vertices, uint32_t numVertices, uint32_t vertexSize,
+ std::vector<D3D11_INPUT_ELEMENT_DESC>& inputDesc, const uint16_t* faces = nullptr, uint32_t numFaces = 0);
+ virtual ~CustomRenderMesh();
+
+protected:
+ CustomRenderMesh();
+ void initialize(const void* vertices, uint32_t numVertices, uint32_t vertexSize,
+ std::vector<D3D11_INPUT_ELEMENT_DESC>& inputDesc, const uint16_t* faces, uint32_t numFaces);
+
+private:
+ ID3D11Buffer* m_vertexBuffer;
+ ID3D11Buffer* m_indexBuffer;
+ uint32_t m_numFaces;
+ uint32_t m_numVertices;
+ uint32_t m_vertexSize;
+
+ std::vector<D3D11_INPUT_ELEMENT_DESC> m_inputDesc;
+};
+
+
+#endif //CUSTOM_RENDER_MESH_H \ No newline at end of file
diff --git a/NvCloth/samples/SampleBase/renderer/DebugRenderBuffer.h b/NvCloth/samples/SampleBase/renderer/DebugRenderBuffer.h
new file mode 100644
index 0000000..1ec11e8
--- /dev/null
+++ b/NvCloth/samples/SampleBase/renderer/DebugRenderBuffer.h
@@ -0,0 +1,50 @@
+/*
+* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
+*
+* NVIDIA CORPORATION and its licensors retain all intellectual property
+* and proprietary rights in and to this software, related documentation
+* and any modifications thereto. Any use, reproduction, disclosure or
+* distribution of this software and related documentation without an express
+* license agreement from NVIDIA CORPORATION is strictly prohibited.
+*/
+
+#ifndef DEBUGRENDERBUFFER_H
+#define DEBUGRENDERBUFFER_H
+
+#include "PxRenderBuffer.h"
+#include <vector>
+
+using namespace physx;
+
+
+/**
+Simple PxRenderBuffer implementation for easy debug primitives adding
+*/
+class DebugRenderBuffer : public PxRenderBuffer
+{
+public:
+ ~DebugRenderBuffer() {}
+
+ virtual PxU32 getNbPoints() const { return 0; }
+ virtual const PxDebugPoint* getPoints() const { return nullptr; }
+
+ virtual PxU32 getNbLines() const { return static_cast<PxU32>(m_lines.size()); }
+ virtual const PxDebugLine* getLines() const { return m_lines.data(); }
+
+ virtual PxU32 getNbTriangles() const { return 0; }
+ virtual const PxDebugTriangle* getTriangles() const { return nullptr; }
+
+ virtual PxU32 getNbTexts() const { return 0; }
+ virtual const PxDebugText* getTexts() const { return nullptr; }
+
+ virtual void append(const PxRenderBuffer& other) {}
+ virtual void clear()
+ {
+ m_lines.clear();
+ }
+
+ std::vector<PxDebugLine> m_lines;
+};
+
+
+#endif //DEBUGRENDERBUFFER_H \ No newline at end of file
diff --git a/NvCloth/samples/SampleBase/renderer/Mesh.cpp b/NvCloth/samples/SampleBase/renderer/Mesh.cpp
new file mode 100644
index 0000000..687249e
--- /dev/null
+++ b/NvCloth/samples/SampleBase/renderer/Mesh.cpp
@@ -0,0 +1,13 @@
+/*
+* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
+*
+* NVIDIA CORPORATION and its licensors retain all intellectual property
+* and proprietary rights in and to this software, related documentation
+* and any modifications thereto. Any use, reproduction, disclosure or
+* distribution of this software and related documentation without an express
+* license agreement from NVIDIA CORPORATION is strictly prohibited.
+*/
+
+
+#include "Mesh.h"
+
diff --git a/NvCloth/samples/SampleBase/renderer/Mesh.h b/NvCloth/samples/SampleBase/renderer/Mesh.h
new file mode 100644
index 0000000..cdc595d
--- /dev/null
+++ b/NvCloth/samples/SampleBase/renderer/Mesh.h
@@ -0,0 +1,51 @@
+/*
+* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
+*
+* NVIDIA CORPORATION and its licensors retain all intellectual property
+* and proprietary rights in and to this software, related documentation
+* and any modifications thereto. Any use, reproduction, disclosure or
+* distribution of this software and related documentation without an express
+* license agreement from NVIDIA CORPORATION is strictly prohibited.
+*/
+
+#ifndef MESH_H
+#define MESH_H
+
+#include <vector>
+#include "PxVec2.h"
+#include "PxVec3.h"
+
+
+class Mesh
+{
+ virtual uint32_t getVertexStride() = 0;
+ // ... TBD
+};
+
+/**
+SimpleMesh: position + normal + uv
+We use only this type everywhere, once other versions will be required we should generalize Mesh and refactor code.
+*/
+class SimpleMesh : public Mesh
+{
+public:
+
+ class Vertex
+ {
+ public:
+ physx::PxVec3 position;
+ physx::PxVec3 normal;
+ physx::PxVec2 uv;
+ };
+
+ virtual uint32_t getVertexStride() { return sizeof(Vertex); }
+
+ std::vector<Vertex> vertices;
+ std::vector<uint16_t> indices;
+
+ physx::PxVec3 extents;
+ physx::PxVec3 center;
+};
+
+
+#endif //MESH_H \ No newline at end of file
diff --git a/NvCloth/samples/SampleBase/renderer/PrimitiveRenderMesh.cpp b/NvCloth/samples/SampleBase/renderer/PrimitiveRenderMesh.cpp
new file mode 100644
index 0000000..3d324f8
--- /dev/null
+++ b/NvCloth/samples/SampleBase/renderer/PrimitiveRenderMesh.cpp
@@ -0,0 +1,205 @@
+/*
+* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
+*
+* NVIDIA CORPORATION and its licensors retain all intellectual property
+* and proprietary rights in and to this software, related documentation
+* and any modifications thereto. Any use, reproduction, disclosure or
+* distribution of this software and related documentation without an express
+* license agreement from NVIDIA CORPORATION is strictly prohibited.
+*/
+
+
+#include "PrimitiveRenderMesh.h"
+#include "Renderer.h"
+
+
+///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+// Base Mesh internal class
+///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+
+PrimitiveRenderMesh::PrimitiveRenderMesh(const float v[], UINT numVertices)
+{
+ std::vector<D3D11_INPUT_ELEMENT_DESC> layout;
+ layout.push_back({ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 });
+ layout.push_back({ "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 });
+
+ initialize(v, numVertices, sizeof(float) * 6, layout, nullptr, 0);
+}
+
+
+///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+// Box Mesh
+///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+
+const float boxVertices[] =
+{
+ -1.0f, -1.0f, -1.0f, 0.0f, 0.0f, -1.0f,
+ 1.0f, -1.0f, -1.0f, 0.0f, 0.0f, -1.0f,
+ 1.0f, 1.0f, -1.0f, 0.0f, 0.0f, -1.0f,
+ 1.0f, 1.0f, -1.0f, 0.0f, 0.0f, -1.0f,
+ -1.0f, 1.0f, -1.0f, 0.0f, 0.0f, -1.0f,
+ -1.0f, -1.0f, -1.0f, 0.0f, 0.0f, -1.0f,
+
+ -1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
+ 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
+ 1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
+ 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
+ -1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
+ -1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
+
+ -1.0f, 1.0f, 1.0f, -1.0f, 0.0f, 0.0f,
+ -1.0f, -1.0f, -1.0f, -1.0f, 0.0f, 0.0f,
+ -1.0f, 1.0f, -1.0f, -1.0f, 0.0f, 0.0f,
+ -1.0f, -1.0f, -1.0f, -1.0f, 0.0f, 0.0f,
+ -1.0f, 1.0f, 1.0f, -1.0f, 0.0f, 0.0f,
+ -1.0f, -1.0f, 1.0f, -1.0f, 0.0f, 0.0f,
+
+ 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
+ 1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 0.0f,
+ 1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 0.0f,
+ 1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 0.0f,
+ 1.0f, -1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
+ 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
+
+ -1.0f, -1.0f, -1.0f, 0.0f, -1.0f, 0.0f,
+ 1.0f, -1.0f, 1.0f, 0.0f, -1.0f, 0.0f,
+ 1.0f, -1.0f, -1.0f, 0.0f, -1.0f, 0.0f,
+ 1.0f, -1.0f, 1.0f, 0.0f, -1.0f, 0.0f,
+ -1.0f, -1.0f, -1.0f, 0.0f, -1.0f, 0.0f,
+ -1.0f, -1.0f, 1.0f, 0.0f, -1.0f, 0.0f,
+
+ -1.0f, 1.0f, -1.0f, 0.0f, 1.0f, 0.0f,
+ 1.0f, 1.0f, -1.0f, 0.0f, 1.0f, 0.0f,
+ 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f,
+ 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f,
+ -1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f,
+ -1.0f, 1.0f, -1.0f, 0.0f, 1.0f, 0.0f
+};
+
+BoxRenderMesh::BoxRenderMesh() : PrimitiveRenderMesh(boxVertices, sizeof(boxVertices) / (6 * sizeof(boxVertices[0]))) {}
+
+
+///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+// Plane Mesh
+///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+
+const float planeSize = 1.0f; // we use scaling instead
+const float planeTilesCount = 1000.0f;
+
+const float planeVertices[] =
+{
+ 0, planeSize, planeSize, 1.0f, 0.0f, 0.0f, planeTilesCount, planeTilesCount,
+ 0, planeSize, -planeSize, 1.0f, 0.0f, 0.0f, planeTilesCount, -planeTilesCount,
+ 0, -planeSize, -planeSize, 1.0f, 0.0f, 0.0f, -planeTilesCount, -planeTilesCount,
+ 0, -planeSize, -planeSize, 1.0f, 0.0f, 0.0f, -planeTilesCount, -planeTilesCount,
+ 0, -planeSize, planeSize, 1.0f, 0.0f, 0.0f, -planeTilesCount, planeTilesCount,
+ 0, planeSize, planeSize, 1.0f, 0.0f, 0.0f, planeTilesCount, planeTilesCount
+};
+
+PlaneRenderMesh::PlaneRenderMesh()
+{
+ std::vector<D3D11_INPUT_ELEMENT_DESC> layout;
+ layout.push_back({ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 });
+ layout.push_back({ "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 });
+ layout.push_back({ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 24, D3D11_INPUT_PER_VERTEX_DATA, 0 });
+
+ initialize(planeVertices, sizeof(planeVertices) / (8 * sizeof(planeVertices[0])), sizeof(float) * 8, layout, nullptr, 0);
+}
+
+
+///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+// Sphere Mesh
+///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+
+
+const uint32_t g_numSlices = 8; // along lines of longitude
+const uint32_t g_numStacks = 16; // along lines of latitude
+
+const uint32_t g_numSphereVertices = (g_numSlices * 2 + 1)*(g_numStacks + 1);
+const uint32_t g_numSphereIndices = g_numSlices * 2 * g_numStacks * 6;
+
+const uint32_t g_numConeVertices = (g_numSlices * 2 + 1) * 2;
+const uint32_t g_numConeIndices = g_numSlices * 2 * 6;
+
+PxVec3 g_spherePositions[g_numSphereVertices];
+uint16_t g_sphereIndices[g_numSphereIndices];
+
+void generateSphereMesh(uint16_t slices, uint16_t stacks, PxVec3* positions, uint16_t* indices)
+{
+ const PxF32 thetaStep = PxPi / stacks;
+ const PxF32 phiStep = PxTwoPi / (slices * 2);
+
+ PxF32 theta = 0.0f;
+
+ // generate vertices
+ for (uint16_t y = 0; y <= stacks; ++y)
+ {
+ PxF32 phi = 0.0f;
+
+ PxF32 cosTheta = PxCos(theta);
+ PxF32 sinTheta = PxSin(theta);
+
+ for (uint16_t x = 0; x <= slices * 2; ++x)
+ {
+ PxF32 cosPhi = PxCos(phi);
+ PxF32 sinPhi = PxSin(phi);
+
+ PxVec3 p(cosPhi*sinTheta, cosTheta, sinPhi*sinTheta);
+
+ // write vertex
+ *(positions++) = p;
+
+ phi += phiStep;
+ }
+
+ theta += thetaStep;
+ }
+
+ const uint16_t numRingQuads = 2 * slices;
+ const uint16_t numRingVerts = 2 * slices + 1;
+
+ // add faces
+ for (uint16_t y = 0; y < stacks; ++y)
+ {
+ for (uint16_t i = 0; i < numRingQuads; ++i)
+ {
+ // add a quad
+ *(indices++) = (y + 0)*numRingVerts + i;
+ *(indices++) = (y + 1)*numRingVerts + i;
+ *(indices++) = (y + 1)*numRingVerts + i + 1;
+
+ *(indices++) = (y + 1)*numRingVerts + i + 1;
+ *(indices++) = (y + 0)*numRingVerts + i + 1;
+ *(indices++) = (y + 0)*numRingVerts + i;
+ }
+ }
+}
+
+
+struct SphereVertex
+{
+ PxVec3 position;
+ PxVec3 normal;
+};
+
+SphereRenderMesh::SphereRenderMesh()
+{
+ generateSphereMesh(g_numSlices, g_numStacks, g_spherePositions, g_sphereIndices);
+
+ std::vector<SphereVertex> vertices;
+ for (uint32_t i = 0; i < g_numSphereVertices; i++)
+ {
+ vertices.push_back({ g_spherePositions[i], g_spherePositions[i] });
+ }
+
+ std::vector<D3D11_INPUT_ELEMENT_DESC> layout;
+ layout.push_back({ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 });
+ layout.push_back({ "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 });
+
+ initialize(vertices.data(), (uint32_t)vertices.size(), sizeof(SphereVertex), layout, g_sphereIndices, g_numSphereIndices);
+}
+
+
+SphereRenderMesh::~SphereRenderMesh()
+{
+}
diff --git a/NvCloth/samples/SampleBase/renderer/PrimitiveRenderMesh.h b/NvCloth/samples/SampleBase/renderer/PrimitiveRenderMesh.h
new file mode 100644
index 0000000..965e6cf
--- /dev/null
+++ b/NvCloth/samples/SampleBase/renderer/PrimitiveRenderMesh.h
@@ -0,0 +1,61 @@
+/*
+* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
+*
+* NVIDIA CORPORATION and its licensors retain all intellectual property
+* and proprietary rights in and to this software, related documentation
+* and any modifications thereto. Any use, reproduction, disclosure or
+* distribution of this software and related documentation without an express
+* license agreement from NVIDIA CORPORATION is strictly prohibited.
+*/
+
+#ifndef PRIMITIVE_RENDER_MESH_H
+#define PRIMITIVE_RENDER_MESH_H
+
+#include "Utils.h"
+#include <DirectXMath.h>
+
+#include <vector>
+#include "Renderable.h"
+#include "CustomRenderMesh.h"
+
+
+class PrimitiveRenderMesh : public CustomRenderMesh
+{
+protected:
+ PrimitiveRenderMesh(const float v[], UINT numVertices);
+};
+
+class BoxRenderMesh : public PrimitiveRenderMesh
+{
+public:
+ BoxRenderMesh();
+};
+
+
+class PlaneRenderMesh : public CustomRenderMesh
+{
+public:
+ PlaneRenderMesh();
+};
+
+
+class SphereRenderMesh : public CustomRenderMesh
+{
+public:
+ SphereRenderMesh();
+ virtual ~SphereRenderMesh();
+};
+
+
+struct PrimitiveRenderMeshType
+{
+ enum Enum
+ {
+ Box,
+ Plane,
+ Sphere,
+ Count
+ };
+};
+
+#endif //PRIMITIVE_RENDER_MESH_H \ No newline at end of file
diff --git a/NvCloth/samples/SampleBase/renderer/PxPhysXCommonConfig.h b/NvCloth/samples/SampleBase/renderer/PxPhysXCommonConfig.h
new file mode 100644
index 0000000..213eedd
--- /dev/null
+++ b/NvCloth/samples/SampleBase/renderer/PxPhysXCommonConfig.h
@@ -0,0 +1,92 @@
+// This code contains NVIDIA Confidential Information and is disclosed to you
+// under a form of NVIDIA software license agreement provided separately to you.
+//
+// Notice
+// NVIDIA Corporation and its licensors retain all intellectual property and
+// proprietary rights in and to this software and related documentation and
+// any modifications thereto. Any use, reproduction, disclosure, or
+// distribution of this software and related documentation without an express
+// license agreement from NVIDIA Corporation is strictly prohibited.
+//
+// ALL NVIDIA DESIGN SPECIFICATIONS, CODE ARE PROVIDED "AS IS.". NVIDIA MAKES
+// NO WARRANTIES, EXPRESSED, IMPLIED, STATUTORY, OR OTHERWISE WITH RESPECT TO
+// THE MATERIALS, AND EXPRESSLY DISCLAIMS ALL IMPLIED WARRANTIES OF NONINFRINGEMENT,
+// MERCHANTABILITY, AND FITNESS FOR A PARTICULAR PURPOSE.
+//
+// Information and code furnished is believed to be accurate and reliable.
+// However, NVIDIA Corporation assumes no responsibility for the consequences of use of such
+// information or for any infringement of patents or other rights of third parties that may
+// result from its use. No license is granted by implication or otherwise under any patent
+// or patent rights of NVIDIA Corporation. Details are subject to change without notice.
+// This code supersedes and replaces all information previously supplied.
+// NVIDIA Corporation products are not authorized for use as critical
+// components in life support devices or systems without express written approval of
+// NVIDIA Corporation.
+//
+// Copyright (c) 2008-2017 NVIDIA Corporation. All rights reserved.
+// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
+// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
+
+
+#ifndef PX_PHYSICS_COMMON_NX
+#define PX_PHYSICS_COMMON_NX
+
+/** \addtogroup common
+@{ */
+
+#include "foundation/Px.h"
+
+
+// define API function declaration (public API only needed because of extensions)
+#if defined PX_PHYSX_STATIC_LIB || defined PX_PHYSX_CORE_STATIC_LIB
+ #define PX_PHYSX_CORE_API
+#else
+ #if PX_WINDOWS
+ #if defined PX_PHYSX_CORE_EXPORTS
+ #define PX_PHYSX_CORE_API __declspec(dllexport)
+ #else
+ #define PX_PHYSX_CORE_API __declspec(dllimport)
+ #endif
+ #elif PX_UNIX_FAMILY
+ #define PX_PHYSX_CORE_API PX_UNIX_EXPORT
+ #else
+ #define PX_PHYSX_CORE_API
+ #endif
+#endif
+
+#if PX_WINDOWS && !defined(__CUDACC__)
+ #if defined PX_PHYSX_COMMON_EXPORTS
+ #define PX_PHYSX_COMMON_API __declspec(dllexport)
+ #else
+ #define PX_PHYSX_COMMON_API __declspec(dllimport)
+ #endif
+#elif PX_UNIX_FAMILY
+ #define PX_PHYSX_COMMON_API PX_UNIX_EXPORT
+#else
+ #define PX_PHYSX_COMMON_API
+#endif
+
+// Changing these parameters requires recompilation of the SDK
+
+#if !PX_DOXYGEN
+namespace physx
+{
+#endif
+ class PxCollection;
+ class PxBase;
+
+ class PxHeightField;
+ class PxHeightFieldDesc;
+
+ class PxTriangleMesh;
+ class PxConvexMesh;
+
+ typedef PxU32 PxTriangleID;
+ typedef PxU16 PxMaterialTableIndex;
+
+#if !PX_DOXYGEN
+} // namespace physx
+#endif
+
+/** @} */
+#endif
diff --git a/NvCloth/samples/SampleBase/renderer/PxRenderBuffer.h b/NvCloth/samples/SampleBase/renderer/PxRenderBuffer.h
new file mode 100644
index 0000000..8bbb37a
--- /dev/null
+++ b/NvCloth/samples/SampleBase/renderer/PxRenderBuffer.h
@@ -0,0 +1,157 @@
+// This code contains NVIDIA Confidential Information and is disclosed to you
+// under a form of NVIDIA software license agreement provided separately to you.
+//
+// Notice
+// NVIDIA Corporation and its licensors retain all intellectual property and
+// proprietary rights in and to this software and related documentation and
+// any modifications thereto. Any use, reproduction, disclosure, or
+// distribution of this software and related documentation without an express
+// license agreement from NVIDIA Corporation is strictly prohibited.
+//
+// ALL NVIDIA DESIGN SPECIFICATIONS, CODE ARE PROVIDED "AS IS.". NVIDIA MAKES
+// NO WARRANTIES, EXPRESSED, IMPLIED, STATUTORY, OR OTHERWISE WITH RESPECT TO
+// THE MATERIALS, AND EXPRESSLY DISCLAIMS ALL IMPLIED WARRANTIES OF NONINFRINGEMENT,
+// MERCHANTABILITY, AND FITNESS FOR A PARTICULAR PURPOSE.
+//
+// Information and code furnished is believed to be accurate and reliable.
+// However, NVIDIA Corporation assumes no responsibility for the consequences of use of such
+// information or for any infringement of patents or other rights of third parties that may
+// result from its use. No license is granted by implication or otherwise under any patent
+// or patent rights of NVIDIA Corporation. Details are subject to change without notice.
+// This code supersedes and replaces all information previously supplied.
+// NVIDIA Corporation products are not authorized for use as critical
+// components in life support devices or systems without express written approval of
+// NVIDIA Corporation.
+//
+// Copyright (c) 2008-2017 NVIDIA Corporation. All rights reserved.
+// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
+// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
+
+
+#ifndef PX_FOUNDATION_PXRENDERBUFFER_H
+#define PX_FOUNDATION_PXRENDERBUFFER_H
+
+/** \addtogroup common
+@{
+*/
+
+#include "PxPhysXCommonConfig.h"
+#include "foundation/PxVec3.h"
+#include "foundation/PxMat33.h"
+#include "foundation/PxBounds3.h"
+
+#if !PX_DOXYGEN
+namespace physx
+{
+#endif
+
+/**
+\brief Default color values used for debug rendering.
+*/
+struct PxDebugColor
+{
+ enum Enum
+ {
+ eARGB_BLACK = 0xff000000,
+ eARGB_RED = 0xffff0000,
+ eARGB_GREEN = 0xff00ff00,
+ eARGB_BLUE = 0xff0000ff,
+ eARGB_YELLOW = 0xffffff00,
+ eARGB_MAGENTA = 0xffff00ff,
+ eARGB_CYAN = 0xff00ffff,
+ eARGB_WHITE = 0xffffffff,
+ eARGB_GREY = 0xff808080,
+ eARGB_DARKRED = 0x88880000,
+ eARGB_DARKGREEN = 0x88008800,
+ eARGB_DARKBLUE = 0x88000088
+ };
+};
+
+/**
+\brief Used to store a single point and colour for debug rendering.
+*/
+struct PxDebugPoint
+{
+ PxDebugPoint(const PxVec3& p, const PxU32& c)
+ : pos(p), color(c) {}
+
+ PxVec3 pos;
+ PxU32 color;
+};
+
+/**
+\brief Used to store a single line and colour for debug rendering.
+*/
+struct PxDebugLine
+{
+ PxDebugLine(const PxVec3& p0, const PxVec3& p1, const PxU32& c)
+ : pos0(p0), color0(c), pos1(p1), color1(c) {}
+
+ PxVec3 pos0;
+ PxU32 color0;
+ PxVec3 pos1;
+ PxU32 color1;
+};
+
+/**
+\brief Used to store a single triangle and colour for debug rendering.
+*/
+struct PxDebugTriangle
+{
+ PxDebugTriangle(const PxVec3& p0, const PxVec3& p1, const PxVec3& p2, const PxU32& c)
+ : pos0(p0), color0(c), pos1(p1), color1(c), pos2(p2), color2(c) {}
+
+ PxVec3 pos0;
+ PxU32 color0;
+ PxVec3 pos1;
+ PxU32 color1;
+ PxVec3 pos2;
+ PxU32 color2;
+};
+
+/**
+\brief Used to store a text for debug rendering. Doesn't own 'string' array.
+*/
+struct PxDebugText
+{
+ PxDebugText() : string(0) {}
+
+ PxDebugText(const PxVec3& p, const PxReal& s, const PxU32& c, const char* str)
+ : position(p), size(s), color(c), string(str) {}
+
+ PxVec3 position;
+ PxReal size;
+ PxU32 color;
+ const char* string;
+};
+
+/**
+\brief Interface for points, lines, triangles, and text buffer.
+*/
+class PxRenderBuffer
+{
+public:
+ virtual ~PxRenderBuffer() {}
+
+ virtual PxU32 getNbPoints() const = 0;
+ virtual const PxDebugPoint* getPoints() const = 0;
+
+ virtual PxU32 getNbLines() const = 0;
+ virtual const PxDebugLine* getLines() const = 0;
+
+ virtual PxU32 getNbTriangles() const = 0;
+ virtual const PxDebugTriangle* getTriangles() const = 0;
+
+ virtual PxU32 getNbTexts() const = 0;
+ virtual const PxDebugText* getTexts() const = 0;
+
+ virtual void append(const PxRenderBuffer& other) = 0;
+ virtual void clear() = 0;
+};
+
+#if !PX_DOXYGEN
+} // namespace physx
+#endif
+
+/** @} */
+#endif
diff --git a/NvCloth/samples/SampleBase/renderer/RenderMaterial.cpp b/NvCloth/samples/SampleBase/renderer/RenderMaterial.cpp
new file mode 100644
index 0000000..eb07c84
--- /dev/null
+++ b/NvCloth/samples/SampleBase/renderer/RenderMaterial.cpp
@@ -0,0 +1,206 @@
+/*
+* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
+*
+* NVIDIA CORPORATION and its licensors retain all intellectual property
+* and proprietary rights in and to this software, related documentation
+* and any modifications thereto. Any use, reproduction, disclosure or
+* distribution of this software and related documentation without an express
+* license agreement from NVIDIA CORPORATION is strictly prohibited.
+*/
+
+#include "RenderMaterial.h"
+#include <DirectXMath.h>
+#include "ShaderUtils.h"
+#include "Renderer.h"
+
+
+///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+// RenderMaterial
+///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+
+RenderMaterial::RenderMaterial(ResourceManager& resourceCallback, const char* shaderFileName,
+ const char* textureFileName, BlendMode blendMode)
+{
+ this->initialize(resourceCallback, shaderFileName, textureFileName, blendMode);
+}
+
+void RenderMaterial::initialize(ResourceManager& resourceCallback, const char* shaderFileName, const char* textureFileName, BlendMode blendMode)
+{
+ std::vector<std::string> v;
+ v.push_back(shaderFileName);
+ initialize(resourceCallback, v, textureFileName, blendMode);
+}
+
+void RenderMaterial::initialize(ResourceManager& resourceCallback, std::vector<std::string> shaderFileNames, const char* textureFileName, BlendMode blendMode)
+{
+ mTextureSRV = nullptr;
+ mTexture = nullptr;
+ mBlendState = nullptr;
+ mTextureFileName = textureFileName;
+
+ for (uint32_t i = 0; i < shaderFileNames.size(); i++)
+ {
+ const ShaderFileResource* resource = resourceCallback.requestShaderFile(shaderFileNames[i].c_str());
+ if (resource)
+ {
+ std::string shaderFilePath = resource->path;
+ mShaderFilePathes.push_back(shaderFilePath);
+ }
+ }
+ mShaderGroups.reserve(mShaderFilePathes.size());
+
+ if (!mTextureFileName.empty())
+ {
+ mTexture = resourceCallback.requestTexture(mTextureFileName.c_str());
+ }
+
+ setBlending(blendMode);
+
+ reload();
+}
+
+void RenderMaterial::releaseReloadableResources()
+{
+ for (std::vector<ShaderGroup*>::iterator it = mShaderGroups.begin(); it != mShaderGroups.end(); it++)
+ {
+ delete *it;
+ }
+ mShaderGroups.clear();
+
+ SAFE_RELEASE(mTextureSRV);
+}
+
+RenderMaterial::~RenderMaterial()
+{
+ releaseReloadableResources();
+ SAFE_RELEASE(mBlendState);
+}
+
+void RenderMaterial::setBlending(BlendMode blendMode)
+{
+ mBlendMode = blendMode;
+
+ SAFE_RELEASE(mBlendState);
+
+ D3D11_BLEND_DESC desc;
+ ZeroMemory(&desc, sizeof(desc));
+
+ switch (blendMode)
+ {
+ case BLEND_NONE:
+ desc.RenderTarget[0].BlendEnable = FALSE;
+ desc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
+ break;
+ case BLEND_ALPHA_BLENDING:
+ desc.AlphaToCoverageEnable = FALSE;
+ desc.IndependentBlendEnable = TRUE;
+ desc.RenderTarget[0].BlendEnable = TRUE;
+ desc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
+ desc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
+ desc.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
+ desc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_INV_SRC_ALPHA;
+ desc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA;
+ desc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_SRC_ALPHA;
+ desc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
+ break;
+ case BLEND_ADDITIVE: // actually, is's additive by alpha
+ desc.AlphaToCoverageEnable = FALSE;
+ desc.IndependentBlendEnable = TRUE;
+ desc.RenderTarget[0].BlendEnable = TRUE;
+ desc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
+ desc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
+ desc.RenderTarget[0].DestBlend = D3D11_BLEND_ONE;
+ desc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ONE;
+ desc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA;
+ desc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_SRC_ALPHA;
+ desc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
+ break;
+ default:
+ PX_ALWAYS_ASSERT_MESSAGE("Unknown blend mode");
+ }
+
+ ID3D11Device* device = GetDeviceManager()->GetDevice();
+ V(device->CreateBlendState(&desc, &mBlendState));
+}
+
+void RenderMaterial::reload()
+{
+ releaseReloadableResources();
+
+ // load shaders
+ ID3D11Device* device = GetDeviceManager()->GetDevice();
+
+ for (std::vector<std::string>::iterator it = mShaderFilePathes.begin(); it != mShaderFilePathes.end(); it++)
+ {
+ const char* shaderFilePath = (*it).c_str();
+ ShaderGroup* shaderGroup = new ShaderGroup();
+ V(createShaderFromFile(device, shaderFilePath, "VS", &(shaderGroup->vs), shaderGroup->buffer));
+ createShaderFromFile(device, shaderFilePath, "PS", &shaderGroup->ps);
+ createShaderFromFile(device, shaderFilePath, "GS", &shaderGroup->gs);
+ mShaderGroups.push_back(shaderGroup);
+ }
+
+ // load texture
+ if (mTexture)
+ {
+ V(DirectX::CreateShaderResourceView(device, mTexture->image.GetImages(), mTexture->image.GetImageCount(),
+ mTexture->metaData, &mTextureSRV));
+ }
+}
+
+
+
+RenderMaterial::InstancePtr RenderMaterial::getMaterialInstance(const IRenderMesh* mesh)
+{
+ // look in cache
+ auto it = mRenderMeshToInstanceMap.find(mesh);
+ if (it != mRenderMeshToInstanceMap.end())
+ {
+ if (!(*it).second.expired())
+ {
+ return (*it).second.lock();
+ }
+ }
+
+ // create new
+ const std::vector<D3D11_INPUT_ELEMENT_DESC>& descs = mesh->getInputElementDesc();
+ RenderMaterial::InstancePtr instance = getMaterialInstance(&descs[0], (uint32_t)descs.size());
+ mRenderMeshToInstanceMap[mesh] = instance;
+ return instance;
+}
+
+RenderMaterial::InstancePtr RenderMaterial::getMaterialInstance(const D3D11_INPUT_ELEMENT_DESC* elementDescs, uint32_t numElements)
+{
+ ID3D11Device* device = GetDeviceManager()->GetDevice();
+
+ for (uint32_t i = 0; i < mShaderGroups.size(); i++)
+ {
+ if (mShaderGroups[i]->buffer == NULL)
+ continue;
+
+ ID3D11InputLayout* inputLayout = NULL;
+ device->CreateInputLayout(elementDescs, numElements, mShaderGroups[i]->buffer->GetBufferPointer(), mShaderGroups[i]->buffer->GetBufferSize(), &inputLayout);
+
+ if (inputLayout)
+ {
+ RenderMaterial::InstancePtr materialInstance(new Instance(*this, inputLayout, i));
+ return materialInstance;
+ }
+ }
+ PX_ALWAYS_ASSERT();
+ return NULL;
+}
+
+void RenderMaterial::Instance::bind(ID3D11DeviceContext& context, uint32_t slot, bool depthStencilOnly)
+{
+ mMaterial.mShaderGroups[mShaderNum]->Set(&context, !depthStencilOnly);
+
+ context.OMSetBlendState(mMaterial.mBlendState, nullptr, 0xFFFFFFFF);
+ context.PSSetShaderResources(slot, 1, &(mMaterial.mTextureSRV));
+ context.IASetInputLayout(mInputLayout);
+}
+
+bool RenderMaterial::Instance::isValid()
+{
+ return mMaterial.mShaderGroups.size() > 0 && mMaterial.mShaderGroups[mShaderNum]->IsValid();
+}
diff --git a/NvCloth/samples/SampleBase/renderer/RenderMaterial.h b/NvCloth/samples/SampleBase/renderer/RenderMaterial.h
new file mode 100644
index 0000000..3f95389
--- /dev/null
+++ b/NvCloth/samples/SampleBase/renderer/RenderMaterial.h
@@ -0,0 +1,118 @@
+/*
+* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
+*
+* NVIDIA CORPORATION and its licensors retain all intellectual property
+* and proprietary rights in and to this software, related documentation
+* and any modifications thereto. Any use, reproduction, disclosure or
+* distribution of this software and related documentation without an express
+* license agreement from NVIDIA CORPORATION is strictly prohibited.
+*/
+
+#ifndef RENDER_MATERIAL_H
+#define RENDER_MATERIAL_H
+
+#include "Utils.h"
+#include "DirectXTex.h"
+
+#include <string>
+#include <vector>
+#include <list>
+#include <map>
+#include <memory>
+
+
+class IRenderMesh;
+class ResourceManager;
+struct TextureResource;
+
+
+class RenderMaterial
+{
+ public:
+
+ enum BlendMode
+ {
+ BLEND_NONE,
+ BLEND_ALPHA_BLENDING,
+ BLEND_ADDITIVE
+ };
+
+ RenderMaterial(ResourceManager& resourceProvider, const char* shaderFileName, const char* textureFileName = "", BlendMode blendMode = BLEND_NONE);
+ ~RenderMaterial();
+
+ void setBlending(BlendMode blendMode);
+ BlendMode getBlending() const { return mBlendMode; }
+
+ void reload();
+
+ class Instance
+ {
+ public:
+ Instance(RenderMaterial& material, ID3D11InputLayout* inputLayout, uint32_t shaderNum = 0) : mMaterial(material), mInputLayout(inputLayout), mShaderNum(shaderNum) {}
+ ~Instance() { SAFE_RELEASE(mInputLayout); }
+
+ bool isValid();
+ void bind(ID3D11DeviceContext& context, uint32_t slot, bool depthStencilOnly = false);
+ RenderMaterial& getMaterial() const { return mMaterial; }
+ private:
+ RenderMaterial& mMaterial;
+ ID3D11InputLayout* mInputLayout;
+ uint32_t mShaderNum;
+ };
+
+ typedef std::shared_ptr<Instance> InstancePtr;
+
+ InstancePtr getMaterialInstance(const IRenderMesh* mesh);
+ InstancePtr getMaterialInstance(const D3D11_INPUT_ELEMENT_DESC* elementDescs, uint32_t numElements);
+
+ private:
+ void initialize(ResourceManager& resourceCallback, const char* shaderFileName, const char* textureFileName, BlendMode blendMode);
+ void initialize(ResourceManager&resourceProvider, std::vector<std::string> shaderFileNames, const char* textureFileName, BlendMode blendMode);
+
+ void releaseReloadableResources();
+
+ std::string mShaderFileName;
+ std::string mTextureFileName;
+
+ struct ShaderGroup
+ {
+ ShaderGroup() : vs(nullptr), gs(nullptr), ps(nullptr), buffer(nullptr)
+ {
+ }
+ ~ShaderGroup()
+ {
+ Release();
+ }
+ void Release()
+ {
+ SAFE_RELEASE(vs);
+ SAFE_RELEASE(gs);
+ SAFE_RELEASE(ps);
+ SAFE_RELEASE(buffer);
+ }
+ void Set(ID3D11DeviceContext* c, bool setPixelShader = true)
+ {
+ c->VSSetShader(vs, nullptr, 0);
+ c->GSSetShader(gs, nullptr, 0);
+ c->PSSetShader(setPixelShader ? ps : nullptr, nullptr, 0);
+ }
+ bool IsValid()
+ {
+ return vs != nullptr;
+ }
+ ID3D11VertexShader* vs;
+ ID3D11GeometryShader* gs;
+ ID3D11PixelShader* ps;
+ ID3DBlob* buffer;
+ };
+
+ std::map<const IRenderMesh*, std::weak_ptr<Instance>> mRenderMeshToInstanceMap;
+ const TextureResource* mTexture;
+ ID3D11ShaderResourceView* mTextureSRV;
+ std::vector<std::string> mShaderFilePathes;
+ std::vector<ShaderGroup*> mShaderGroups;
+ ID3D11BlendState* mBlendState;
+ BlendMode mBlendMode;
+};
+
+#endif \ No newline at end of file
diff --git a/NvCloth/samples/SampleBase/renderer/RenderUtils.h b/NvCloth/samples/SampleBase/renderer/RenderUtils.h
new file mode 100644
index 0000000..f4d007b
--- /dev/null
+++ b/NvCloth/samples/SampleBase/renderer/RenderUtils.h
@@ -0,0 +1,78 @@
+/*
+* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
+*
+* NVIDIA CORPORATION and its licensors retain all intellectual property
+* and proprietary rights in and to this software, related documentation
+* and any modifications thereto. Any use, reproduction, disclosure or
+* distribution of this software and related documentation without an express
+* license agreement from NVIDIA CORPORATION is strictly prohibited.
+*/
+
+#ifndef RENDER_UTILS_H
+#define RENDER_UTILS_H
+
+#include "DirectXTex.h"
+#include <DirectXMath.h>
+#include "PxMat44.h"
+#include "PxVec3.h"
+#include "PxVec4.h"
+
+static DirectX::XMFLOAT4 getRandomPastelColor()
+{
+ float r = ((double)rand() / (RAND_MAX)) * 0.5f + 0.5f;
+ float g = ((double)rand() / (RAND_MAX)) * 0.5f + 0.5f;
+ float b = ((double)rand() / (RAND_MAX)) * 0.5f + 0.5f;
+ return DirectX::XMFLOAT4(r, g, b, 1.0f);
+}
+
+static physx::PxMat44 XMMATRIXToPxMat44(const DirectX::XMMATRIX& mat)
+{
+ physx::PxMat44 m;
+ memcpy(const_cast<float*>(m.front()), &mat.r[0], 4 * 4 * sizeof(float));
+ return m;
+}
+
+static DirectX::XMMATRIX PxMat44ToXMMATRIX(const physx::PxMat44& mat)
+{
+ return DirectX::XMMATRIX(mat.front());
+}
+
+static physx::PxVec4 XMVECTORToPxVec4(const DirectX::XMVECTOR& vec)
+{
+ DirectX::XMFLOAT4 f;
+ DirectX::XMStoreFloat4(&f, vec);
+ return physx::PxVec4(f.x, f.y, f.z, f.w);
+}
+
+static physx::PxVec3 XMFLOAT3ToPxVec3(const DirectX::XMFLOAT3& vec)
+{
+ return physx::PxVec3(vec.x, vec.y, vec.z);
+}
+
+static physx::PxVec4 XMFLOAT4ToPxVec4(const DirectX::XMFLOAT4& vec)
+{
+ return physx::PxVec4(vec.x, vec.y, vec.z, vec.w);
+}
+
+static uint32_t XMFLOAT4ToU32Color(const DirectX::XMFLOAT4& color)
+{
+ uint32_t c = 0;
+ c |= (int)(color.w * 255); c <<= 8;
+ c |= (int)(color.z * 255); c <<= 8;
+ c |= (int)(color.y * 255); c <<= 8;
+ c |= (int)(color.x * 255);
+ return c;
+}
+
+static DirectX::XMFLOAT4 XMFLOAT4Lerp(const DirectX::XMFLOAT4 v0, const DirectX::XMFLOAT4 v1, float val)
+{
+ DirectX::XMFLOAT4 v(
+ v0.x * (1 - val) + v1.x * val,
+ v0.y * (1 - val) + v1.y * val,
+ v0.z * (1 - val) + v1.z * val,
+ v0.w * (1 - val) + v1.w * val
+ );
+ return v;
+}
+
+#endif //RENDER_UTILS_H \ No newline at end of file
diff --git a/NvCloth/samples/SampleBase/renderer/Renderable.cpp b/NvCloth/samples/SampleBase/renderer/Renderable.cpp
new file mode 100644
index 0000000..51a151d
--- /dev/null
+++ b/NvCloth/samples/SampleBase/renderer/Renderable.cpp
@@ -0,0 +1,48 @@
+/*
+* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
+*
+* NVIDIA CORPORATION and its licensors retain all intellectual property
+* and proprietary rights in and to this software, related documentation
+* and any modifications thereto. Any use, reproduction, disclosure or
+* distribution of this software and related documentation without an express
+* license agreement from NVIDIA CORPORATION is strictly prohibited.
+*/
+
+#include "Renderable.h"
+#include "Renderer.h"
+#include "RenderUtils.h"
+
+const DirectX::XMFLOAT4 DEFAULT_COLOR(0.5f, 0.5f, 0.5f, 1.0f);
+
+Renderable::Renderable(IRenderMesh& mesh, RenderMaterial& material) : m_mesh(mesh), m_scale(1, 1, 1), m_color(DEFAULT_COLOR), m_hidden(false), m_transform(PxIdentity)
+{
+ setMaterial(material);
+}
+
+void Renderable::setMaterial(RenderMaterial& material)
+{
+ m_materialInstance = material.getMaterialInstance(&m_mesh);
+}
+
+void Renderable::render(Renderer& renderer, bool depthStencilOnly) const
+{
+ if (!m_materialInstance->isValid())
+ {
+ PX_ALWAYS_ASSERT();
+ return;
+ }
+
+ m_materialInstance->bind(*renderer.m_context, 0, depthStencilOnly);
+
+ // setup object CB
+ {
+ D3D11_MAPPED_SUBRESOURCE mappedResource;
+ renderer.m_context->Map(renderer.m_objectCB, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
+ Renderer::CBObject* objectBuffer = (Renderer::CBObject*)mappedResource.pData;
+ objectBuffer->world = PxMat44ToXMMATRIX(getModelMatrix());
+ objectBuffer->color = getColor();
+ renderer.m_context->Unmap(renderer.m_objectCB, 0);
+ }
+
+ m_mesh.render(*renderer.m_context);
+}
diff --git a/NvCloth/samples/SampleBase/renderer/Renderable.h b/NvCloth/samples/SampleBase/renderer/Renderable.h
new file mode 100644
index 0000000..da94144
--- /dev/null
+++ b/NvCloth/samples/SampleBase/renderer/Renderable.h
@@ -0,0 +1,128 @@
+/*
+* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
+*
+* NVIDIA CORPORATION and its licensors retain all intellectual property
+* and proprietary rights in and to this software, related documentation
+* and any modifications thereto. Any use, reproduction, disclosure or
+* distribution of this software and related documentation without an express
+* license agreement from NVIDIA CORPORATION is strictly prohibited.
+*/
+
+#ifndef RENDERABLE_H
+#define RENDERABLE_H
+
+#include "RenderMaterial.h"
+#include <DirectXMath.h>
+#include "PxMat44.h"
+#include "PxVec3.h"
+#include "PxVec4.h"
+
+using namespace physx;
+
+class Renderer;
+
+/**
+RenderMesh interface, used by Renderable
+*/
+class IRenderMesh
+{
+public:
+ virtual ~IRenderMesh() {}
+ virtual const std::vector<D3D11_INPUT_ELEMENT_DESC>& getInputElementDesc() const = 0;
+ virtual void render(ID3D11DeviceContext& context) const = 0;
+};
+
+/**
+Renderable, represents single object renderer by Renderer.
+Basically Renderable = RenderMaterial + RenderMesh
+*/
+class Renderable
+{
+public:
+ //////// public API ////////
+
+ void setMaterial(RenderMaterial& material);
+
+ PxMat44 getModelMatrix() const
+ {
+ return PxMat44(m_transform) * PxMat44(PxVec4(m_scale, 1));
+ }
+
+ void setTransform(PxTransform& transform)
+ {
+ m_transform = transform;
+ }
+
+ const PxTransform& getTransform() const
+ {
+ return m_transform;
+ }
+
+ void setScale(PxVec3 scale)
+ {
+ m_scale = scale;
+ }
+
+ const PxVec3& getScale() const
+ {
+ return m_scale;
+ }
+
+ void setColor(DirectX::XMFLOAT4 color)
+ {
+ m_color = color;
+ }
+ DirectX::XMFLOAT4 getColor() const
+ {
+ return m_color;
+ }
+
+ void setHidden(bool hidden)
+ {
+ m_hidden = hidden;
+ }
+
+ bool isHidden() const
+ {
+ return m_hidden;
+ }
+
+ bool isTransparent() const
+ {
+ return !(m_materialInstance->getMaterial().getBlending() == RenderMaterial::BLEND_NONE);
+ }
+
+ RenderMaterial& getMaterial() const { return m_materialInstance->getMaterial(); }
+
+private:
+ //////// methods used by Renderer ////////
+
+ friend class Renderer;
+
+ void render(Renderer& renderer) const
+ {
+ render(renderer, false);
+ }
+
+ void renderDepthStencilOnly(Renderer& renderer) const
+ {
+ render(renderer, true);
+ }
+
+ Renderable(IRenderMesh& mesh, RenderMaterial& material);
+
+ void render(Renderer& renderer, bool depthStencilOnly) const;
+
+
+ //////// internal data ////////
+
+ DirectX::XMFLOAT4 m_color;
+ PxTransform m_transform;
+ PxVec3 m_scale;
+
+ RenderMaterial::InstancePtr m_materialInstance;
+ IRenderMesh& m_mesh;
+ bool m_hidden;
+};
+
+#endif //RENDERABLE_H \ No newline at end of file
diff --git a/NvCloth/samples/SampleBase/renderer/Renderer.cpp b/NvCloth/samples/SampleBase/renderer/Renderer.cpp
new file mode 100644
index 0000000..48685d9
--- /dev/null
+++ b/NvCloth/samples/SampleBase/renderer/Renderer.cpp
@@ -0,0 +1,730 @@
+/*
+* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
+*
+* NVIDIA CORPORATION and its licensors retain all intellectual property
+* and proprietary rights in and to this software, related documentation
+* and any modifications thereto. Any use, reproduction, disclosure or
+* distribution of this software and related documentation without an express
+* license agreement from NVIDIA CORPORATION is strictly prohibited.
+*/
+
+#include "Renderer.h"
+#include "RenderUtils.h"
+#include "UIHelpers.h"
+#include "SampleProfiler.h"
+
+#include "PxRenderBuffer.h"
+using namespace physx;
+
+#include <set>
+
+
+const float CAMERA_CLIP_NEAR = 1.0f;
+const float CAMERA_CLIP_FAR = 1000.00f;
+
+const float CLEAR_SCENE_COLOR[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
+
+
+///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+// Renderer
+///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+
+
+Renderer::Renderer()
+: m_cameraCB(nullptr)
+, m_worldCB(nullptr)
+, m_objectCB(nullptr)
+, m_RSState(nullptr)
+, m_opaqueRenderDSState(nullptr)
+, m_transparencyRenderDSState(nullptr)
+, m_DSTexture(nullptr)
+, m_DSView(nullptr)
+, m_DSTextureSRV(nullptr)
+, m_pointSampler(nullptr)
+, m_linearSampler(nullptr)
+, m_wireframeMode(false)
+, m_debugPrimitiveVB(nullptr)
+, m_debugPrimitiveVBVerticesCount(0)
+, m_shadowEnabled(true)
+, m_HBAOEnabled(true)
+, m_visibleOpaqueRenderablesCount(0)
+, m_visibleTransparentRenderablesCount(0)
+{
+ m_worldCBData.ambientColor = DirectX::XMFLOAT3(0.21f, 0.21f, 0.22f);
+ m_worldCBData.pointLightColor = DirectX::XMFLOAT3(1.0f, 1.0f, 1.0f);
+ m_worldCBData.pointLightPos = DirectX::XMFLOAT3(0.0f, 30.0f, 12.0f);
+ m_worldCBData.dirLightColor = DirectX::XMFLOAT3(0.0f, 0.0f, 0.0f);
+ m_worldCBData.dirLightDir = DirectX::XMFLOAT3(-0.08f, -0.34f, -0.91f);
+ m_worldCBData.specularPower = 140.0f;
+ m_worldCBData.specularIntensity = 0.4f;
+
+ toggleCameraSpeed(false);
+}
+
+Renderer::~Renderer()
+{
+}
+
+void Renderer::initializeDefaultRSState()
+{
+ SAFE_RELEASE(m_RSState);
+ D3D11_RASTERIZER_DESC desc;
+ ZeroMemory(&desc, sizeof(desc));
+ desc.CullMode = D3D11_CULL_NONE;
+ desc.FillMode = m_wireframeMode ? D3D11_FILL_WIREFRAME : D3D11_FILL_SOLID;
+ desc.AntialiasedLineEnable = FALSE;
+ desc.DepthBias = 0;
+ desc.DepthBiasClamp = 0;
+ desc.DepthClipEnable = TRUE;
+ desc.FrontCounterClockwise = FALSE;
+ desc.MultisampleEnable = TRUE;
+ desc.ScissorEnable = FALSE;
+ desc.SlopeScaledDepthBias = 0;
+
+ V(m_device->CreateRasterizerState(&desc, &m_RSState));
+}
+
+HRESULT Renderer::DeviceCreated(ID3D11Device* device)
+{
+ m_device = device;
+
+ // Camera constant buffer
+ {
+ D3D11_BUFFER_DESC buffer_desc;
+ ZeroMemory(&buffer_desc, sizeof(buffer_desc));
+ buffer_desc.Usage = D3D11_USAGE_DYNAMIC;
+ buffer_desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
+ buffer_desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
+ buffer_desc.ByteWidth = sizeof(CBCamera);
+ _ASSERT((buffer_desc.ByteWidth % 16) == 0);
+
+ V(device->CreateBuffer(&buffer_desc, nullptr, &m_cameraCB));
+ }
+
+ // World constant buffer
+ {
+ D3D11_BUFFER_DESC buffer_desc;
+ ZeroMemory(&buffer_desc, sizeof(buffer_desc));
+ buffer_desc.Usage = D3D11_USAGE_DYNAMIC;
+ buffer_desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
+ buffer_desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
+ buffer_desc.ByteWidth = sizeof(CBWorld);
+ _ASSERT((buffer_desc.ByteWidth % 16) == 0);
+
+ V(device->CreateBuffer(&buffer_desc, nullptr, &m_worldCB));
+ }
+
+ // Object constant buffer
+ {
+ D3D11_BUFFER_DESC buffer_desc;
+ ZeroMemory(&buffer_desc, sizeof(buffer_desc));
+ buffer_desc.Usage = D3D11_USAGE_DYNAMIC;
+ buffer_desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
+ buffer_desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
+ buffer_desc.ByteWidth = sizeof(CBObject);
+ _ASSERT((buffer_desc.ByteWidth % 16) == 0);
+
+ V(device->CreateBuffer(&buffer_desc, nullptr, &m_objectCB));
+ }
+
+ // Opaque Render Depth-Stencil state
+ {
+ D3D11_DEPTH_STENCIL_DESC desc;
+ ZeroMemory(&desc, sizeof(desc));
+ desc.StencilEnable = FALSE;
+ desc.DepthEnable = TRUE;
+ desc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
+ desc.DepthFunc = D3D11_COMPARISON_LESS_EQUAL;
+
+ V(device->CreateDepthStencilState(&desc, &m_opaqueRenderDSState));
+ }
+
+ // Transparency Render Depth-Stencil state
+ {
+ D3D11_DEPTH_STENCIL_DESC desc;
+ ZeroMemory(&desc, sizeof(desc));
+ desc.StencilEnable = FALSE;
+ desc.DepthEnable = TRUE;
+ desc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ZERO;
+ desc.DepthFunc = D3D11_COMPARISON_LESS_EQUAL;
+
+ V(device->CreateDepthStencilState(&desc, &m_transparencyRenderDSState));
+ }
+
+ // Linear sampler
+ {
+ D3D11_SAMPLER_DESC desc;
+ ZeroMemory(&desc, sizeof(desc));
+ desc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
+ desc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
+ desc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
+ desc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
+ desc.MaxAnisotropy = 1;
+ desc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
+ desc.MinLOD = 0;
+ desc.MaxLOD = D3D11_FLOAT32_MAX;
+ V(device->CreateSamplerState(&desc, &m_linearSampler));
+ }
+
+ // Point sampler
+ {
+ D3D11_SAMPLER_DESC desc;
+ ZeroMemory(&desc, sizeof(desc));
+ desc.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT;
+ desc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
+ desc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
+ desc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
+ desc.MaxAnisotropy = 1;
+ desc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
+ desc.MinLOD = 0;
+ desc.MaxLOD = D3D11_FLOAT32_MAX;
+ V(device->CreateSamplerState(&desc, &m_pointSampler));
+ }
+
+ // Rasterizer state
+ initializeDefaultRSState();
+
+ // init primitive render meshes
+ for (uint32_t i = 0; i < PrimitiveRenderMeshType::Count; i++)
+ {
+ m_primitiveRenderMeshes[i] = nullptr;
+ }
+
+ // init shadows
+ ID3D11DeviceContext* pd3dDeviceContext;
+ m_device->GetImmediateContext(&pd3dDeviceContext);
+ m_shadow.createResources(m_device, pd3dDeviceContext, &m_camera);
+
+ // init hbao
+ m_HBAO.createResources(m_device);
+
+ return S_OK;
+}
+
+void Renderer::DeviceDestroyed()
+{
+ SAFE_RELEASE(m_cameraCB);
+ SAFE_RELEASE(m_worldCB);
+ SAFE_RELEASE(m_objectCB);
+ SAFE_RELEASE(m_RSState);
+ SAFE_RELEASE(m_opaqueRenderDSState);
+ SAFE_RELEASE(m_transparencyRenderDSState);
+ SAFE_RELEASE(m_pointSampler);
+ SAFE_RELEASE(m_linearSampler);
+ SAFE_RELEASE(m_debugPrimitiveVB);
+ SAFE_RELEASE(m_DSTexture);
+ SAFE_RELEASE(m_DSView);
+ SAFE_RELEASE(m_DSTextureSRV);
+
+ for (uint32_t i = 0; i < PrimitiveRenderMeshType::Count; i++)
+ {
+ SAFE_DELETE(m_primitiveRenderMeshes[i]);
+ }
+}
+
+void Renderer::onInitialize()
+{
+ // search paths
+ m_resourceManager.addSearchDir("..\\..\\samples\\resources");
+ m_resourceManager.addSearchDir("..\\..\\..\\samples\\resources");
+ for (const std::string& d : getManager()->getConfig().additionalResourcesDir)
+ {
+ m_resourceManager.addSearchDir(d.c_str());
+ }
+
+ // debug primitive render material and input layout
+ {
+ m_debugPrimitiveRenderMaterial = new RenderMaterial(m_resourceManager, "debug_primitive", "");
+
+ D3D11_INPUT_ELEMENT_DESC layout[] = {
+ { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
+ { "COLOR", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 }
+ };
+
+ m_debugPrimitiveRenderMaterialInstance = m_debugPrimitiveRenderMaterial->getMaterialInstance(layout, ARRAYSIZE(layout));
+ }
+}
+
+void Renderer::onTerminate()
+{
+ SAFE_DELETE(m_debugPrimitiveRenderMaterial);
+}
+
+void Renderer::BackBufferResized(ID3D11Device* /*device*/, const DXGI_SURFACE_DESC* sd)
+{
+ // Setup the camera's projection parameters
+ m_screenWidth = sd->Width;
+ m_screenHeight = sd->Height;
+ float fAspectRatio = m_screenWidth / m_screenHeight;
+ m_camera.SetProjParams(DirectX::XM_PIDIV4, fAspectRatio, CAMERA_CLIP_NEAR, CAMERA_CLIP_FAR);
+
+ SAFE_RELEASE(m_DSTexture);
+ SAFE_RELEASE(m_DSView);
+ SAFE_RELEASE(m_DSTextureSRV);
+
+ // create a new Depth-Stencil texture
+ {
+ D3D11_TEXTURE2D_DESC desc;
+ ZeroMemory(&desc, sizeof(desc));
+ desc.Width = sd->Width;
+ desc.Height = sd->Height;
+ desc.MipLevels = 1;
+ desc.ArraySize = 1;
+ desc.Format = DXGI_FORMAT_R32_TYPELESS; // Use a typeless type here so that it can be both depth-stencil and shader resource.
+ desc.SampleDesc.Count = sd->SampleDesc.Count;
+ desc.SampleDesc.Quality = sd->SampleDesc.Quality;
+ desc.Usage = D3D11_USAGE_DEFAULT;
+ desc.BindFlags = D3D11_BIND_DEPTH_STENCIL | D3D11_BIND_SHADER_RESOURCE;
+ desc.CPUAccessFlags = 0;
+ desc.MiscFlags = 0;
+ V(m_device->CreateTexture2D(&desc, NULL, &m_DSTexture));
+ }
+
+ // create Depth-Stencil view
+ {
+ D3D11_DEPTH_STENCIL_VIEW_DESC desc;
+ ZeroMemory(&desc, sizeof(desc));
+ desc.ViewDimension = sd->SampleDesc.Count > 1 ? D3D11_DSV_DIMENSION_TEXTURE2DMS : D3D11_DSV_DIMENSION_TEXTURE2D;
+ desc.Format = DXGI_FORMAT_D32_FLOAT; // Make the view see this as D32_FLOAT instead of typeless
+ desc.Texture2D.MipSlice = 0;
+ V(m_device->CreateDepthStencilView(m_DSTexture, &desc, &m_DSView));
+ }
+
+ // create Depth-Stencil shader resource view
+ {
+ D3D11_SHADER_RESOURCE_VIEW_DESC desc;
+ ZeroMemory(&desc, sizeof(desc));
+ desc.Format = DXGI_FORMAT_R32_FLOAT; // Make the shaders see this as R32_FLOAT instead of typeless
+ desc.ViewDimension = sd->SampleDesc.Count > 1 ? D3D11_SRV_DIMENSION_TEXTURE2DMS : D3D11_SRV_DIMENSION_TEXTURE2D;
+ desc.Texture2D.MipLevels = 1;
+ desc.Texture2D.MostDetailedMip = 0;
+ V(m_device->CreateShaderResourceView(m_DSTexture, &desc, &m_DSTextureSRV));
+ }
+
+ // setup viewport
+ m_viewport.Width = (FLOAT)sd->Width;
+ m_viewport.Height = (FLOAT)sd->Height;
+ m_viewport.MinDepth = 0;
+ m_viewport.MaxDepth = 1;
+ m_viewport.TopLeftX = 0;
+ m_viewport.TopLeftY = 0;
+
+ // setup shadows
+ m_shadow.setScreenResolution(0, sd->Width, sd->Height, sd->SampleDesc.Count, nullptr);
+}
+
+void Renderer::setAllConstantBuffers(ID3D11DeviceContext* ctx)
+{
+ ID3D11Buffer* cbs[3] = { m_cameraCB, m_worldCB, m_objectCB };
+ ctx->VSSetConstantBuffers(0, 3, cbs);
+ ctx->GSSetConstantBuffers(0, 3, cbs);
+ ctx->PSSetConstantBuffers(0, 3, cbs);
+}
+
+void Renderer::Render(ID3D11Device* /*device*/, ID3D11DeviceContext* ctx, ID3D11RenderTargetView* pRTV,
+ ID3D11DepthStencilView*)
+{
+ PROFILER_SCOPED_FUNCTION();
+
+ m_context = ctx;
+
+ ctx->ClearRenderTargetView(pRTV, CLEAR_SCENE_COLOR);
+ ctx->ClearDepthStencilView(m_DSView, D3D11_CLEAR_DEPTH, 1.0, 0);
+ ctx->RSSetViewports(1, &m_viewport);
+
+ // needed matrices
+ DirectX::XMMATRIX viewMatrix = m_camera.GetViewMatrix();
+ DirectX::XMMATRIX projMatrix = m_camera.GetProjMatrix();
+ DirectX::XMMATRIX projMatrixInv = DirectX::XMMatrixInverse(NULL, projMatrix);
+ DirectX::XMMATRIX viewProjMatrix = viewMatrix * projMatrix;
+
+ // Fill Camera constant buffer
+ {
+ D3D11_MAPPED_SUBRESOURCE mappedResource;
+ ctx->Map(m_cameraCB, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
+ CBCamera* cameraBuffer = (CBCamera*)mappedResource.pData;
+ cameraBuffer->viewProjection = viewProjMatrix;
+ cameraBuffer->projectionInv = projMatrixInv;
+ DirectX::XMStoreFloat3(&(cameraBuffer->viewPos), m_camera.GetEyePt());
+ ctx->Unmap(m_cameraCB, 0);
+ }
+
+ // Fill World constant buffer
+ {
+ D3D11_MAPPED_SUBRESOURCE mappedResource;
+ ctx->Map(m_worldCB, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
+ CBWorld* worldBuffer = (CBWorld*)mappedResource.pData;
+ memcpy(worldBuffer, &m_worldCBData, sizeof(m_worldCBData));
+ //worldBuffer->ambientColor = m_CBWorldData.ambientColor;
+ //worldBuffer->pointLightPos = m_CBWorldData.pointLightPos;
+ //worldBuffer->pointLightColor = m_CBWorldData.pointLightColor;
+ //worldBuffer->dirLightDir = m_CBWorldData.dirLightDir;
+ //worldBuffer->specularPower = m_CBWorldData.specularPower;
+ //worldBuffer->dirLightColor = m_CBWorldData.dirLightColor;
+ //worldBuffer->specularIntensity = m_CBWorldData.specularIntensity;
+ ctx->Unmap(m_worldCB, 0);
+ }
+
+ ctx->RSSetState(m_RSState);
+ ctx->PSSetSamplers(0, 1, &m_linearSampler);
+ ctx->PSSetSamplers(1, 1, &m_pointSampler);
+
+
+ if (m_shadowEnabled)
+ {
+ // render depth only
+ {
+ ctx->OMSetRenderTargets(0, nullptr, m_DSView);
+ ctx->OMSetDepthStencilState(m_opaqueRenderDSState, 0xFF);
+
+ // set constants buffers
+ setAllConstantBuffers(ctx);
+
+ for (auto it = m_renderables.begin(); it != m_renderables.end(); it++)
+ {
+ if (!(*it)->isTransparent() && !(*it)->isHidden())
+ (*it)->renderDepthStencilOnly(*this);
+ }
+ }
+
+ // render shadow map
+ m_shadow.renderShadowMaps(this);
+
+ // render shadow buffer
+ ctx->OMSetRenderTargets(0, nullptr, nullptr);
+ m_shadow.renderShadowBuffer(m_DSTextureSRV, nullptr);
+ }
+
+ // Opaque render
+ {
+ ctx->RSSetViewports(1, &m_viewport);
+ ctx->RSSetState(m_RSState);
+ ctx->OMSetRenderTargets(1, &pRTV, m_DSView);
+ ctx->OMSetDepthStencilState(m_opaqueRenderDSState, 0xFF);
+
+ // set constants buffers
+ setAllConstantBuffers(ctx);
+
+ // Fill Camera constant buffer
+ {
+ D3D11_MAPPED_SUBRESOURCE mappedResource;
+ ctx->Map(m_cameraCB, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
+ CBCamera* cameraBuffer = (CBCamera*)mappedResource.pData;
+ cameraBuffer->viewProjection = viewProjMatrix;
+ cameraBuffer->projectionInv = projMatrixInv;
+ DirectX::XMStoreFloat3(&(cameraBuffer->viewPos), m_camera.GetEyePt());
+ ctx->Unmap(m_cameraCB, 0);
+ }
+
+ // Render opaque renderables
+ m_visibleOpaqueRenderablesCount = 0;
+ for (auto it = m_renderables.begin(); it != m_renderables.end(); it++)
+ {
+ if (!(*it)->isTransparent() && !(*it)->isHidden())
+ {
+ (*it)->render(*this);
+ m_visibleOpaqueRenderablesCount++;
+ }
+ }
+ }
+
+ // modulate shadows
+ if (m_shadowEnabled)
+ {
+ m_shadow.modulateShadowBuffer(pRTV);
+ }
+
+ // render AO
+ if (m_HBAOEnabled)
+ {
+ m_HBAO.renderAO(m_context, pRTV, m_DSTextureSRV, projMatrix);
+ }
+
+ ctx->RSSetViewports(1, &m_viewport);
+
+ // render debug render buffers
+ while (m_queuedRenderBuffers.size() > 0)
+ {
+ render(m_queuedRenderBuffers.back());
+ m_queuedRenderBuffers.pop_back();
+ }
+
+ // Transparency render
+ ctx->OMSetRenderTargets(1, &pRTV, m_DSView);
+ ctx->OMSetDepthStencilState(m_transparencyRenderDSState, 0xFF);
+
+ // depth as SRV isn't used now (uncommenting will produce a warning, probably need readonly depth?)
+ //ctx->PSSetShaderResources(1, 1, &mDSTextureSRV);
+
+ // Render transparent renderables
+ m_visibleTransparentRenderablesCount = 0;
+ for (auto it = m_renderables.begin(); it != m_renderables.end(); it++)
+ {
+ if ((*it)->isTransparent() && !(*it)->isHidden())
+ {
+ (*it)->render(*this);
+ m_visibleTransparentRenderablesCount++;
+ }
+ }
+
+ // shadows debug render
+ if (0)
+ {
+ m_shadow.displayMapFrustums(pRTV, m_DSView);
+ }
+
+ // Reset RT and SRV state
+ ID3D11ShaderResourceView* nullAttach[16] = { nullptr };
+ ctx->PSSetShaderResources(0, 16, nullAttach);
+ ctx->OMSetRenderTargets(0, nullptr, nullptr);
+}
+
+LRESULT Renderer::MsgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
+{
+ PX_UNUSED(hWnd);
+ PX_UNUSED(wParam);
+ PX_UNUSED(lParam);
+
+ if (uMsg == WM_KEYDOWN || uMsg == WM_KEYUP)
+ {
+ // Camera overspeed event
+ int iKeyPressed = static_cast<int>(wParam);
+ if (iKeyPressed == VK_SHIFT)
+ {
+ toggleCameraSpeed(uMsg == WM_KEYDOWN);
+ }
+ }
+
+ // Camera events
+ return m_camera.HandleMessages(hWnd, uMsg, wParam, lParam);
+}
+
+void Renderer::Animate(double fElapsedTimeSeconds)
+{
+ PROFILER_SCOPED_FUNCTION();
+
+ m_camera.FrameMove((float)fElapsedTimeSeconds);
+}
+
+void Renderer::renderDepthOnly(DirectX::XMMATRIX* viewProjectionSubstitute)
+{
+ // Fill Camera constant buffer
+ if (viewProjectionSubstitute)
+ {
+ D3D11_MAPPED_SUBRESOURCE mappedResource;
+ m_context->Map(m_cameraCB, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
+ CBCamera* cameraBuffer = (CBCamera*)mappedResource.pData;
+ cameraBuffer->viewProjection = *viewProjectionSubstitute;
+ m_context->Unmap(m_cameraCB, 0);
+ }
+
+ // set constants buffers
+ setAllConstantBuffers(m_context);
+
+ // render
+ for (auto it = m_renderables.begin(); it != m_renderables.end(); it++)
+ {
+ if (!(*it)->isTransparent() && !(*it)->isHidden())
+ (*it)->renderDepthStencilOnly(*this);
+ }
+}
+
+void Renderer::render(const PxRenderBuffer* renderBuffer)
+{
+ // points
+ uint32_t pointsCount = renderBuffer->getNbPoints();
+ if (pointsCount > 0)
+ {
+ RenderDebugVertex* verts = new RenderDebugVertex[pointsCount];
+ const PxDebugPoint* points = renderBuffer->getPoints();
+ for (uint32_t i = 0; i < pointsCount; i++)
+ {
+ verts[i].mPos = points[i].pos;
+ verts[i].mColor = points[i].color;
+ }
+
+ renderDebugPrimitive(verts, pointsCount, D3D11_PRIMITIVE_TOPOLOGY_POINTLIST);
+ delete[] verts;
+ }
+
+ // lines
+ uint32_t linesCount = renderBuffer->getNbLines();
+ if (linesCount > 0)
+ {
+ RenderDebugVertex* verts = new RenderDebugVertex[linesCount * 2];
+ const PxDebugLine* lines = renderBuffer->getLines();
+ for (uint32_t i = 0; i < linesCount; i++)
+ {
+ verts[i * 2].mPos = lines[i].pos0;
+ verts[i * 2].mColor = lines[i].color0;
+ verts[i * 2 + 1].mPos = lines[i].pos1;
+ verts[i * 2 + 1].mColor = lines[i].color1;
+ }
+
+ renderDebugPrimitive(verts, linesCount * 2, D3D11_PRIMITIVE_TOPOLOGY_LINELIST);
+ delete[] verts;
+ }
+
+ // triangles
+ uint32_t trianglesCount = renderBuffer->getNbTriangles();
+ if (trianglesCount > 0)
+ {
+ RenderDebugVertex* verts = new RenderDebugVertex[trianglesCount * 3];
+ const PxDebugTriangle* triangles = renderBuffer->getTriangles();
+ for (uint32_t i = 0; i < trianglesCount; i++)
+ {
+ verts[i * 3].mPos = triangles[i].pos0;
+ verts[i * 3].mColor = triangles[i].color0;
+ verts[i * 3 + 1].mPos = triangles[i].pos1;
+ verts[i * 3 + 1].mColor = triangles[i].color1;
+ verts[i * 3 + 2].mPos = triangles[i].pos2;
+ verts[i * 3 + 2].mColor = triangles[i].color2;
+ }
+
+ renderDebugPrimitive(verts, trianglesCount * 3, D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
+ delete[] verts;
+ }
+
+ // texts?
+ // ....
+}
+
+void Renderer::renderDebugPrimitive(const Renderer::RenderDebugVertex *vertices, uint32_t verticesCount, D3D11_PRIMITIVE_TOPOLOGY topology)
+{
+ m_context->IASetPrimitiveTopology(topology);
+
+ m_debugPrimitiveRenderMaterialInstance->bind(*m_context, 0);
+
+ D3D11_MAPPED_SUBRESOURCE mappedResource;
+ m_context->Map(m_objectCB, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
+ CBObject* objectBuffer = (CBObject*)mappedResource.pData;
+
+ objectBuffer->world = PxMat44ToXMMATRIX(PxMat44(PxIdentity));
+
+ m_context->Unmap(m_objectCB, 0);
+
+ if (m_debugPrimitiveVBVerticesCount < verticesCount)
+ {
+ m_debugPrimitiveVBVerticesCount = verticesCount;
+ SAFE_RELEASE(m_debugPrimitiveVB);
+
+ D3D11_BUFFER_DESC bufferDesc;
+
+ memset(&bufferDesc, 0, sizeof(D3D11_BUFFER_DESC));
+ bufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
+ bufferDesc.ByteWidth = sizeof(Renderer::RenderDebugVertex) * m_debugPrimitiveVBVerticesCount;
+ bufferDesc.CPUAccessFlags = 0;
+ bufferDesc.MiscFlags = 0;
+ bufferDesc.Usage = D3D11_USAGE_DEFAULT;
+
+ V(m_device->CreateBuffer(&bufferDesc, NULL, &m_debugPrimitiveVB));
+ }
+
+ CD3D11_BOX box(0, 0, 0, (LONG)(sizeof(Renderer::RenderDebugVertex) * verticesCount), 1, 1);
+ m_context->UpdateSubresource(m_debugPrimitiveVB, 0, &box, vertices, 0, 0);
+
+ ID3D11Buffer* pBuffers[1] = { m_debugPrimitiveVB };
+ UINT strides[1] = { sizeof(RenderDebugVertex) };
+ UINT offsets[1] = { 0 };
+ m_context->IASetVertexBuffers(0, 1, pBuffers, strides, offsets);
+
+ m_context->Draw(verticesCount, 0);
+}
+
+IRenderMesh* Renderer::getPrimitiveRenderMesh(PrimitiveRenderMeshType::Enum type)
+{
+ if (m_primitiveRenderMeshes[type] == NULL)
+ {
+ switch (type)
+ {
+ case PrimitiveRenderMeshType::Box:
+ m_primitiveRenderMeshes[type] = new BoxRenderMesh();
+ break;
+ case PrimitiveRenderMeshType::Plane:
+ m_primitiveRenderMeshes[type] = new PlaneRenderMesh();
+ break;
+ case PrimitiveRenderMeshType::Sphere:
+ m_primitiveRenderMeshes[type] = new SphereRenderMesh();
+ break;
+ default:
+ PX_ALWAYS_ASSERT_MESSAGE("Unsupported PxGeometryType");
+ return NULL;
+ }
+ }
+
+ return m_primitiveRenderMeshes[type];
+}
+
+
+Renderable* Renderer::createRenderable(IRenderMesh& mesh, RenderMaterial& material)
+{
+ Renderable* renderable = new Renderable(mesh, material);
+ m_renderables.emplace(renderable);
+ return renderable;
+}
+
+void Renderer::removeRenderable(Renderable* r)
+{
+ m_renderables.erase(m_renderables.find(r));
+ delete r;
+}
+
+void Renderer::toggleCameraSpeed(bool overspeed)
+{
+ m_camera.SetScalers(0.002f, overspeed ? 150.f : 25.f);
+}
+
+void Renderer::reloadShaders()
+{
+ // iterate Renderables materials and call reload()
+ std::set<RenderMaterial*> materials;
+ for (auto it = m_renderables.begin(); it != m_renderables.end(); it++)
+ {
+ materials.emplace(&((*it)->getMaterial()));
+ }
+ for (std::set<RenderMaterial*>::iterator it = materials.begin(); it != materials.end(); it++)
+ {
+ (*it)->reload();
+ }
+}
+
+void Renderer::drawUI()
+{
+ // Lighting
+ if (ImGui::TreeNode("Lighting"))
+ {
+ ImGui::ColorEdit3("Ambient Color", &(m_worldCBData.ambientColor.x));
+ ImGui::ColorEdit3("Point Light Color", &(m_worldCBData.pointLightColor.x));
+ ImGui::DragFloat3("Point Light Pos", &(m_worldCBData.pointLightPos.x));
+ ImGui::ColorEdit3("Dir Light Color", &(m_worldCBData.dirLightColor.x));
+ ImGui_DragFloat3Dir("Dir Light Dir", &(m_worldCBData.dirLightDir.x));
+ ImGui::DragFloat("Specular Power", &(m_worldCBData.specularPower), 1.0f, 1.0f, 500.0f);
+ ImGui::DragFloat("Specular Intensity", &(m_worldCBData.specularIntensity), 0.01f, 0.0f, 2.0f);
+
+ ImGui::TreePop();
+ }
+
+ // Shadow
+ if (ImGui::TreeNode("Shadow"))
+ {
+ ImGui::Checkbox("Shadows Enabled", &m_shadowEnabled);
+ if (m_shadowEnabled)
+ {
+ m_shadow.drawUI();
+ }
+
+ ImGui::TreePop();
+ }
+
+ // HBAO+
+ if (ImGui::TreeNode("HBAO+"))
+ {
+ ImGui::Checkbox("HBAO Enabled", &(m_HBAOEnabled));
+ if (m_HBAOEnabled)
+ {
+ m_HBAO.drawUI();
+ }
+
+ ImGui::TreePop();
+ }
+} \ No newline at end of file
diff --git a/NvCloth/samples/SampleBase/renderer/Renderer.h b/NvCloth/samples/SampleBase/renderer/Renderer.h
new file mode 100644
index 0000000..3dc8e56
--- /dev/null
+++ b/NvCloth/samples/SampleBase/renderer/Renderer.h
@@ -0,0 +1,247 @@
+/*
+* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
+*
+* NVIDIA CORPORATION and its licensors retain all intellectual property
+* and proprietary rights in and to this software, related documentation
+* and any modifications thereto. Any use, reproduction, disclosure or
+* distribution of this software and related documentation without an express
+* license agreement from NVIDIA CORPORATION is strictly prohibited.
+*/
+
+#ifndef RENDERER_H
+#define RENDERER_H
+
+#include "RenderMaterial.h"
+#include <DirectXMath.h>
+#include "XInput.h"
+#include "DXUTMisc.h"
+#include "DXUTCamera.h"
+#include "SampleManager.h"
+#include "Utils.h"
+#include "ResourceManager.h"
+#include "PrimitiveRenderMesh.h"
+#include "RendererShadow.h"
+#include "RendererHBAO.h"
+#include <unordered_set>
+
+class CFirstPersonCamera;
+class PhysXPrimitive;
+class RenderDebugImpl;
+
+namespace physx
+{
+class PxRenderBuffer;
+};
+
+/**
+3D World Renderer
+- use createRenderable() to add objects to render.
+- use queueRenderBuffer() every frame to render debug primitives.
+- contains ResourceManager to search for file and load resources.
+- contains RendererShadow and RendererHBAO, use them through getters to control shadows.
+*/
+class Renderer : public ISampleController
+{
+ friend class Renderable;
+
+ public:
+ //////// ctor ////////
+
+ Renderer();
+ ~Renderer();
+
+
+ //////// public API ////////
+
+ void reloadShaders();
+
+ bool getWireframeMode()
+ {
+ return m_wireframeMode;
+ }
+
+ void setWireframeMode(bool enabled)
+ {
+ if(m_wireframeMode != enabled)
+ {
+ m_wireframeMode = enabled;
+ initializeDefaultRSState();
+ }
+ }
+
+ IRenderMesh* getPrimitiveRenderMesh(PrimitiveRenderMeshType::Enum type);
+
+ Renderable* createRenderable(IRenderMesh& mesh, RenderMaterial& material);
+ void removeRenderable(Renderable* r);
+
+ void drawUI();
+
+
+ //////// public getters ////////
+
+ float getScreenWidth() const
+ {
+ return m_screenWidth;
+ }
+
+ float getScreenHeight() const
+ {
+ return m_screenHeight;
+ }
+
+ void queueRenderBuffer(const PxRenderBuffer* buffer)
+ {
+ m_queuedRenderBuffers.push_back(buffer);
+ }
+
+ ResourceManager& getResourceManager()
+ {
+ return m_resourceManager;
+ }
+
+ uint32_t getVisibleOpaqueRenderablesCount()
+ {
+ return m_visibleOpaqueRenderablesCount;
+ }
+
+ uint32_t getVisibleTransparentRenderablesCount()
+ {
+ return m_visibleTransparentRenderablesCount;
+ }
+
+ CFirstPersonCamera& getCamera()
+ {
+ return m_camera;
+ }
+
+
+ //////// public 'internal' methods ////////
+
+ // for internal usage (used by RenderShadows)
+ void renderDepthOnly(DirectX::XMMATRIX* viewProjectionSubstitute);
+
+ protected:
+
+ //////// controller callbacks ////////
+
+ virtual HRESULT DeviceCreated(ID3D11Device* pDevice);
+ virtual void DeviceDestroyed();
+ virtual LRESULT MsgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
+ virtual void Animate(double fElapsedTimeSeconds);
+ virtual void onInitialize();
+ virtual void onTerminate();
+ virtual void BackBufferResized(ID3D11Device* pDevice, const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc);
+ virtual void Render(ID3D11Device* /*device*/, ID3D11DeviceContext* ctx, ID3D11RenderTargetView* pRTV,
+ ID3D11DepthStencilView* pDSV);
+
+ private:
+
+ //////// internal methods ////////
+
+ struct RenderDebugVertex
+ {
+ PxVec3 mPos;
+ uint32_t mColor;
+ };
+
+ void render(const PxRenderBuffer* renderBuffer);
+ void render(Renderable* renderable);
+ void renderDebugPrimitive(const RenderDebugVertex *vertices, uint32_t verticesCount, D3D11_PRIMITIVE_TOPOLOGY topology);
+ void initializeDefaultRSState();
+ void setAllConstantBuffers(ID3D11DeviceContext* ctx);
+ void toggleCameraSpeed(bool overspeed);
+
+
+ //////// constant buffers ////////
+
+ struct CBCamera
+ {
+ DirectX::XMMATRIX viewProjection;
+ DirectX::XMMATRIX projectionInv;
+ DirectX::XMFLOAT3 viewPos;
+ float unusedPad;
+ };
+ struct CBWorld
+ {
+ DirectX::XMFLOAT3 ambientColor;
+ float unusedPad1;
+ DirectX::XMFLOAT3 pointLightPos;
+ float unusedPad2;
+ DirectX::XMFLOAT3 pointLightColor;
+ float unusedPad3;
+ DirectX::XMFLOAT3 dirLightDir;
+ float specularPower;
+ DirectX::XMFLOAT3 dirLightColor;
+ float specularIntensity; // TODO: actually it's per object property
+ };
+ struct CBObject
+ {
+ DirectX::XMMATRIX world;
+ DirectX::XMFLOAT4 color;
+ };
+
+
+ //////// internal data ////////
+
+ // camera
+ CFirstPersonCamera m_camera;
+ float m_screenWidth;
+ float m_screenHeight;
+
+ // resources
+ ResourceManager m_resourceManager;
+
+ // additional render modules(libs)
+ RendererShadow m_shadow;
+ bool m_shadowEnabled;
+ RendererHBAO m_HBAO;
+ bool m_HBAOEnabled;
+
+ // DX11 common
+ ID3D11Device* m_device;
+ ID3D11DeviceContext* m_context;
+ D3D11_VIEWPORT m_viewport;
+
+ // DX11 states
+ ID3D11RasterizerState* m_RSState;
+ ID3D11DepthStencilState* m_opaqueRenderDSState;
+ ID3D11DepthStencilState* m_transparencyRenderDSState;
+
+ // DX11 samplers
+ ID3D11SamplerState* m_pointSampler;
+ ID3D11SamplerState* m_linearSampler;
+
+ // Depth Buffer
+ ID3D11Texture2D* m_DSTexture;
+ ID3D11DepthStencilView* m_DSView;
+ ID3D11ShaderResourceView* m_DSTextureSRV;
+
+ // Constant Buffers
+ ID3D11Buffer* m_cameraCB;
+ ID3D11Buffer* m_worldCB;
+ CBWorld m_worldCBData;
+ ID3D11Buffer* m_objectCB;
+
+ // toggles (options)
+ bool m_wireframeMode;
+
+ // renderables
+ std::unordered_set<Renderable*> m_renderables;
+
+ // primitive meshes cache
+ IRenderMesh* m_primitiveRenderMeshes[PrimitiveRenderMeshType::Count];
+
+ // stats
+ uint32_t m_visibleOpaqueRenderablesCount;
+ uint32_t m_visibleTransparentRenderablesCount;
+
+ // Debug Render
+ RenderMaterial* m_debugPrimitiveRenderMaterial;
+ RenderMaterial::InstancePtr m_debugPrimitiveRenderMaterialInstance;
+ ID3D11Buffer* m_debugPrimitiveVB;
+ uint32_t m_debugPrimitiveVBVerticesCount;
+ std::vector<const PxRenderBuffer*> m_queuedRenderBuffers;
+};
+
+
+#endif \ No newline at end of file
diff --git a/NvCloth/samples/SampleBase/renderer/RendererHBAO.cpp b/NvCloth/samples/SampleBase/renderer/RendererHBAO.cpp
new file mode 100644
index 0000000..9f0f6de
--- /dev/null
+++ b/NvCloth/samples/SampleBase/renderer/RendererHBAO.cpp
@@ -0,0 +1,81 @@
+/*
+* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
+*
+* NVIDIA CORPORATION and its licensors retain all intellectual property
+* and proprietary rights in and to this software, related documentation
+* and any modifications thereto. Any use, reproduction, disclosure or
+* distribution of this software and related documentation without an express
+* license agreement from NVIDIA CORPORATION is strictly prohibited.
+*/
+
+
+#include "RendererHBAO.h"
+#include "Renderer.h"
+#include "imgui.h"
+
+///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+// Renderer HBAO (wrapper for hbao+)
+///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+
+
+RendererHBAO::RendererHBAO()
+{
+ m_SSAOContext = NULL;
+
+ // default parameters
+ m_SSAOParameters.Radius = 2.0f;
+}
+
+
+RendererHBAO::~RendererHBAO()
+{
+ releaseResources();
+}
+
+
+void RendererHBAO::createResources(ID3D11Device *pd3dDevice)
+{
+ GFSDK_SSAO_Status status;
+ status = GFSDK_SSAO_CreateContext_D3D11(pd3dDevice, &m_SSAOContext, nullptr);
+ assert(status == GFSDK_SSAO_OK);
+}
+
+
+void RendererHBAO::releaseResources()
+{
+ if (m_SSAOContext != NULL)
+ {
+ m_SSAOContext->Release();
+ }
+}
+
+
+void RendererHBAO::renderAO(ID3D11DeviceContext *pd3dDeviceContext, ID3D11RenderTargetView* pRTV, ID3D11ShaderResourceView* pDepthSRV, DirectX::XMMATRIX& projMatrix)
+{
+ GFSDK_SSAO_InputData_D3D11 InputData;
+ InputData.DepthData.pFullResDepthTextureSRV = pDepthSRV;
+ InputData.DepthData.DepthTextureType = GFSDK_SSAO_HARDWARE_DEPTHS;
+ InputData.DepthData.MetersToViewSpaceUnits = 1.0f;
+ InputData.DepthData.ProjectionMatrix.Data = GFSDK_SSAO_Float4x4(reinterpret_cast<const GFSDK_SSAO_FLOAT*>(&(projMatrix.r[0])));
+ InputData.DepthData.ProjectionMatrix.Layout = GFSDK_SSAO_ROW_MAJOR_ORDER;
+
+ GFSDK_SSAO_Output_D3D11 Output;
+ Output.pRenderTargetView = pRTV;// m_pSceneRTs->ColorRTV;
+ Output.Blend.Mode = GFSDK_SSAO_MULTIPLY_RGB;
+
+ m_SSAOContext->RenderAO(pd3dDeviceContext, InputData, m_SSAOParameters, Output);
+}
+
+
+void RendererHBAO::drawUI()
+{
+ ImGui::DragFloat("Radius", &(m_SSAOParameters.Radius), 0.05f, 0.0f, 100.0f);
+ ImGui::DragFloat("Bias", &(m_SSAOParameters.Bias), 0.01f, 0.0f, 0.5f);
+ ImGui::DragFloat("NearAO", &(m_SSAOParameters.NearAO), 0.01f, 1.0f, 4.0f);
+ ImGui::DragFloat("FarAO", &(m_SSAOParameters.FarAO), 0.01, 1.0f, 4.0f);
+ ImGui::DragFloat("PowerExponent", &(m_SSAOParameters.PowerExponent), 0.01f, 1.0f, 8.0f);
+ ImGui::Checkbox("ForegroundAO Enabled", (bool*)&(m_SSAOParameters.ForegroundAO.Enable));
+ ImGui::DragFloat("ForegroundAO ViewDepth", &(m_SSAOParameters.ForegroundAO.ForegroundViewDepth), 0.01f, 0.0f, 100.0f);
+ ImGui::Checkbox("BackgroundAO Enabled", (bool*)&(m_SSAOParameters.BackgroundAO.Enable));
+ ImGui::DragFloat("BackgroundAO ViewDepth", &(m_SSAOParameters.BackgroundAO.BackgroundViewDepth), 0.01f, 0.0f, 100.0f);
+} \ No newline at end of file
diff --git a/NvCloth/samples/SampleBase/renderer/RendererHBAO.h b/NvCloth/samples/SampleBase/renderer/RendererHBAO.h
new file mode 100644
index 0000000..c900b74
--- /dev/null
+++ b/NvCloth/samples/SampleBase/renderer/RendererHBAO.h
@@ -0,0 +1,40 @@
+/*
+* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
+*
+* NVIDIA CORPORATION and its licensors retain all intellectual property
+* and proprietary rights in and to this software, related documentation
+* and any modifications thereto. Any use, reproduction, disclosure or
+* distribution of this software and related documentation without an express
+* license agreement from NVIDIA CORPORATION is strictly prohibited.
+*/
+
+#ifndef RENDERER_HBAO_H
+#define RENDERER_HBAO_H
+
+#include <DirectXMath.h>
+#include "GFSDK_SSAO.h"
+
+
+class Renderer;
+
+class RendererHBAO
+{
+public:
+ RendererHBAO();
+ ~RendererHBAO();
+
+ void createResources(ID3D11Device *pd3dDevice);
+ void renderAO(ID3D11DeviceContext *pd3dDeviceContext, ID3D11RenderTargetView* pRTV, ID3D11ShaderResourceView* pDepthSRV, DirectX::XMMATRIX& projMatrix);
+
+ void drawUI();
+
+private:
+ void releaseResources();
+
+ GFSDK_SSAO_Parameters m_SSAOParameters;
+
+ GFSDK_SSAO_Context_D3D11* m_SSAOContext;
+};
+
+
+#endif \ No newline at end of file
diff --git a/NvCloth/samples/SampleBase/renderer/RendererShadow.cpp b/NvCloth/samples/SampleBase/renderer/RendererShadow.cpp
new file mode 100644
index 0000000..024a44b
--- /dev/null
+++ b/NvCloth/samples/SampleBase/renderer/RendererShadow.cpp
@@ -0,0 +1,417 @@
+/*
+* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
+*
+* NVIDIA CORPORATION and its licensors retain all intellectual property
+* and proprietary rights in and to this software, related documentation
+* and any modifications thereto. Any use, reproduction, disclosure or
+* distribution of this software and related documentation without an express
+* license agreement from NVIDIA CORPORATION is strictly prohibited.
+*/
+
+
+#include "RendererShadow.h"
+
+#include "XInput.h"
+#include "DXUTMisc.h"
+#include "DXUTCamera.h"
+#include "Renderer.h"
+#include "UIHelpers.h"
+
+#define CASCADES 1
+
+///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+// Renderer Shadows (wrapper for shadow_lib)
+///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+
+const float DEFAULT_LIGHT_SIZE = 3.0f;
+const DirectX::XMFLOAT3 DEFAULT_LIGHT_POS(-25, 25, 25);
+const DirectX::XMFLOAT3 DEFAULT_LIGHT_LOOK_AT(0, 0, 0);
+const DirectX::XMFLOAT3 DEFAULT_SHADOW_COLOR(0.25f, 0.25f, 0.25f);
+
+RendererShadow::RendererShadow()
+{
+ m_shadowLibContext = NULL;
+
+ m_PCSSEnabled = false;
+ m_lightSize = DEFAULT_LIGHT_SIZE;
+ m_lightPos = DEFAULT_LIGHT_POS;
+ m_lightLookAt = DEFAULT_LIGHT_LOOK_AT;
+ m_shadowColor = DEFAULT_SHADOW_COLOR;
+
+ m_worldSpaceBBox0.x = m_worldSpaceBBox0.y = m_worldSpaceBBox0.z = -100;
+ m_worldSpaceBBox1.x = m_worldSpaceBBox1.y = m_worldSpaceBBox1.z = 100;
+
+ // Penumbra params
+ m_PCSSParams.fMaxThreshold = 80.0f;
+ m_PCSSParams.fMaxClamp = 40.0f;
+ m_PCSSParams.fMinSizePercent = 3.0f;
+ m_PCSSParams.fMinWeightExponent = 5.0f;
+ m_PCSSParams.fMinWeightThresholdPercent = 20.0f;
+
+ m_softShadowTestScale = 0.002f;
+
+ memset(&m_shadowBufferSRV, 0, sizeof(m_shadowBufferSRV));
+
+ m_shadowMapHandle = NULL;
+ m_shadowBufferHandle = NULL;
+}
+
+
+RendererShadow::~RendererShadow()
+{
+ ReleaseResources();
+}
+
+
+void RendererShadow::createResources(ID3D11Device *pd3dDevice, ID3D11DeviceContext* context, CFirstPersonCamera* camera)
+{
+ m_camera = camera;
+
+#if !CASCADES
+ uint32_t shadowMapScale = 5;
+ uint32_t shadowMapWidth = 1024;
+ uint32_t shadowMapHeight = 1024;
+
+ // SM Desc
+ m_SMDesc.eViewType = GFSDK_ShadowLib_ViewType_Single;
+ m_SMDesc.eMapType = GFSDK_ShadowLib_MapType_Texture;
+#else
+
+ uint32_t shadowMapScale = 5;
+ uint32_t shadowMapWidth = 1024;
+ uint32_t shadowMapHeight = 1024;
+
+ // SM Desc
+ m_SMDesc.eViewType = GFSDK_ShadowLib_ViewType_Cascades_2;
+ m_SMDesc.eMapType = GFSDK_ShadowLib_MapType_TextureArray;
+#endif
+
+ m_SMDesc.uResolutionWidth = shadowMapWidth * shadowMapScale;
+ m_SMDesc.uResolutionHeight = shadowMapHeight * shadowMapScale;
+ m_SMDesc.uArraySize = m_SMDesc.eViewType;
+
+ for (int j = 0; j < GFSDK_ShadowLib_ViewType_Cascades_4; j++)
+ {
+ m_SMDesc.ViewLocation[j].uMapID = j;
+ m_SMDesc.ViewLocation[j].v2Origin.x = 0;
+ m_SMDesc.ViewLocation[j].v2Origin.y = 0;
+ m_SMDesc.ViewLocation[j].v2Dimension.x = shadowMapWidth * shadowMapScale;
+ m_SMDesc.ViewLocation[j].v2Dimension.y = shadowMapHeight * shadowMapScale;
+ }
+
+
+ // SM Render Params
+ m_SMRenderParams.iDepthBias = 1000;
+ m_SMRenderParams.fSlopeScaledDepthBias = 8;
+
+ // SB Render Params
+ m_SBRenderParams.eTechniqueType = GFSDK_ShadowLib_TechniqueType_PCSS;
+ m_SBRenderParams.eQualityType = GFSDK_ShadowLib_QualityType_High;
+
+ // DLL version
+ GFSDK_ShadowLib_Version DLLVersion;
+ GFSDK_ShadowLib_Status retCode = GFSDK_ShadowLib_GetDLLVersion(&DLLVersion);
+
+ // Header version
+ GFSDK_ShadowLib_Version headerVersion;
+ headerVersion.uMajor = GFSDK_SHADOWLIB_MAJOR_VERSION;
+ headerVersion.uMinor = GFSDK_SHADOWLIB_MINOR_VERSION;
+
+ // Do they match?
+ if (DLLVersion.uMajor == headerVersion.uMajor && DLLVersion.uMinor == headerVersion.uMinor)
+ {
+ GFSDK_ShadowLib_DeviceContext deviceAndContext;
+ deviceAndContext.pD3DDevice = pd3dDevice;
+ deviceAndContext.pDeviceContext = context;
+
+ retCode = GFSDK_ShadowLib_Create(&headerVersion, &m_shadowLibContext, &deviceAndContext, NULL);
+
+ if (retCode != GFSDK_ShadowLib_Status_Ok) assert(false);
+ }
+ else
+ {
+ assert(false);
+ }
+}
+
+
+void RendererShadow::ReleaseResources()
+{
+ SAFE_RELEASE(m_downsampledShadowMap.pTexture);
+ SAFE_RELEASE(m_downsampledShadowMap.pSRV);
+ SAFE_RELEASE(m_downsampledShadowMap.pRTV);
+
+ if (m_shadowLibContext != NULL)
+ {
+ m_shadowLibContext->Destroy();
+ m_shadowLibContext = NULL;
+ }
+}
+
+
+void RendererShadow::setScreenResolution(float FovyRad, UINT Width, UINT Height, UINT uSampleCount, ID3D11DepthStencilView* pReadOnlyDSV)
+{
+ changeShadowSettings(Width, Height, uSampleCount, pReadOnlyDSV);
+}
+
+
+void RendererShadow::changeShadowSettings(UINT Width, UINT Height, UINT uSampleCount, ID3D11DepthStencilView* pReadOnlyDSV)
+{
+ m_SBDesc.uResolutionWidth = Width;
+ m_SBDesc.uResolutionHeight = Height;
+ m_SBDesc.uSampleCount = uSampleCount;
+ m_SBDesc.ReadOnlyDSV.pDSV = pReadOnlyDSV;
+
+ reloadBuffers();
+}
+
+void RendererShadow::reloadBuffers()
+{
+ {
+ m_shadowLibContext->RemoveMap(&m_shadowMapHandle);
+
+ if (m_SMDesc.eMapType == GFSDK_ShadowLib_MapType_Texture &&
+ m_SMDesc.eViewType == GFSDK_ShadowLib_ViewType_Single &&
+ m_SBRenderParams.eTechniqueType == GFSDK_ShadowLib_TechniqueType_PCSS)
+ {
+ m_SMDesc.bDownsample = true;
+ }
+
+ m_shadowLibContext->AddMap(&m_SMDesc, &m_shadowMapHandle);
+ }
+
+ if (m_SMDesc.eMapType == GFSDK_ShadowLib_MapType_Texture && m_SMDesc.eViewType == GFSDK_ShadowLib_ViewType_Single)
+ {
+ m_downsampledShadowMap.uWidth = m_SMDesc.uResolutionWidth >> 1;
+ m_downsampledShadowMap.uHeight = m_SMDesc.uResolutionHeight >> 1;
+ m_downsampledShadowMap.uSampleCount = 1;
+ m_downsampledShadowMap.Format = DXGI_FORMAT_R32_FLOAT;
+ SAFE_RELEASE(m_downsampledShadowMap.pTexture);
+ SAFE_RELEASE(m_downsampledShadowMap.pSRV);
+ SAFE_RELEASE(m_downsampledShadowMap.pRTV);
+ m_shadowLibContext->DevModeCreateTexture2D(&m_downsampledShadowMap);
+ }
+
+ m_shadowLibContext->RemoveBuffer(&m_shadowBufferHandle);
+ m_shadowLibContext->AddBuffer(&m_SBDesc, &m_shadowBufferHandle);
+}
+
+
+
+//--------------------------------------------------------------------------------------
+// Data passed to the shadow map render function
+//--------------------------------------------------------------------------------------
+struct ShadowMapRenderFunctionParams
+{
+ Renderer* renderer;
+};
+static ShadowMapRenderFunctionParams s_RenderParams;
+
+//--------------------------------------------------------------------------------------
+// Shadow map render function
+//--------------------------------------------------------------------------------------
+static void ShadowMapRenderFunction(void* pParams, gfsdk_float4x4* pViewProj)
+{
+ ShadowMapRenderFunctionParams* pRP = (ShadowMapRenderFunctionParams*)pParams;
+
+ DirectX::XMMATRIX viewProjection;
+ memcpy(&viewProjection, &pViewProj->_11, sizeof(gfsdk_float4x4));
+
+ pRP->renderer->renderDepthOnly(&viewProjection);
+}
+
+void RendererShadow::renderShadowMaps(Renderer* renderer)
+{
+ // select technique
+ GFSDK_ShadowLib_TechniqueType technique = m_SBRenderParams.eTechniqueType;
+ m_SBRenderParams.eTechniqueType = m_PCSSEnabled ? GFSDK_ShadowLib_TechniqueType_PCSS : GFSDK_ShadowLib_TechniqueType_PCF;
+ if (technique != m_SBRenderParams.eTechniqueType)
+ reloadBuffers();
+
+
+ DirectX::XMMATRIX viewMatrix = m_camera->GetViewMatrix();
+ DirectX::XMMATRIX projMatrix = m_camera->GetProjMatrix();
+
+ memcpy(&m_SMRenderParams.m4x4EyeViewMatrix, &viewMatrix.r[0], sizeof(gfsdk_float4x4));
+ memcpy(&m_SMRenderParams.m4x4EyeProjectionMatrix, &projMatrix.r[0], sizeof(gfsdk_float4x4));
+
+ // TODO: (better world space bbox needed)
+ m_SMRenderParams.v3WorldSpaceBBox[0] = m_worldSpaceBBox0;
+ m_SMRenderParams.v3WorldSpaceBBox[1] = m_worldSpaceBBox1;
+
+ m_SMRenderParams.LightDesc.eLightType = GFSDK_ShadowLib_LightType_Directional;
+ memcpy(&m_SMRenderParams.LightDesc.v3LightPos, &m_lightPos.x, sizeof(gfsdk_float3));
+ memcpy(&m_SMRenderParams.LightDesc.v3LightLookAt, &m_lightLookAt.x, sizeof(gfsdk_float3));
+ m_SMRenderParams.LightDesc.fLightSize = m_lightSize;
+ m_SMRenderParams.LightDesc.bLightFalloff = false;
+
+ // Scene specific setup for the shadow map phase that follows
+ s_RenderParams.renderer = renderer;
+ m_SMRenderParams.fnpDrawFunction = GFSDK_ShadowLib_FunctionPointer(ShadowMapRenderFunction);
+ m_SMRenderParams.pDrawFunctionParams = &s_RenderParams;
+
+ // render shadow map
+ m_shadowLibContext->RenderMap(m_shadowMapHandle, &m_SMRenderParams);
+}
+
+
+void RendererShadow::renderShadowBuffer(ID3D11ShaderResourceView* pDepthStencilSRV, ID3D11ShaderResourceView* pResolvedDepthStencilSRV)
+{
+ if (m_SBRenderParams.eTechniqueType == GFSDK_ShadowLib_TechniqueType_PCSS &&
+ m_SMDesc.eMapType == GFSDK_ShadowLib_MapType_Texture &&
+ m_SMDesc.eViewType == GFSDK_ShadowLib_ViewType_Single)
+ {
+ m_tempResources.pDownsampledShadowMap = &m_downsampledShadowMap;
+ m_shadowLibContext->SetTempResources(&m_tempResources);
+ }
+
+ m_SBRenderParams.PCSSPenumbraParams = m_PCSSParams;
+ m_SBRenderParams.fSoftShadowTestScale = m_softShadowTestScale;
+
+ m_shadowLibContext->ClearBuffer(m_shadowBufferHandle);
+
+ m_SBRenderParams.DepthBufferDesc.DepthStencilSRV.pSRV = pDepthStencilSRV;
+
+ m_shadowLibContext->RenderBuffer(m_shadowMapHandle, m_shadowBufferHandle, &m_SBRenderParams);
+
+ m_shadowLibContext->FinalizeBuffer(m_shadowBufferHandle, &m_shadowBufferSRV);
+}
+
+
+void RendererShadow::modulateShadowBuffer(ID3D11RenderTargetView* pOutputRTV)
+{
+ GFSDK_ShadowLib_RenderTargetView ColorRTV;
+ ColorRTV.pRTV = pOutputRTV;
+
+ gfsdk_float3 v3ShadowColor = { m_shadowColor.x, m_shadowColor.y, m_shadowColor.z };
+ m_shadowLibContext->ModulateBuffer(m_shadowBufferHandle, &ColorRTV, v3ShadowColor, GFSDK_ShadowLib_ModulateBufferMask_RGB);
+}
+
+
+void RendererShadow::displayShadowMaps(ID3D11RenderTargetView* pOutputRTV, UINT Width, UINT Height)
+{
+ GFSDK_ShadowLib_RenderTargetView ColorRTV;
+ ColorRTV.pRTV = pOutputRTV;
+
+ float fMapResW = (float)m_SMDesc.uResolutionWidth;
+ float fMapResH = (float)m_SMDesc.uResolutionHeight;
+
+ float fWidthScale = Width / ((float)m_SMDesc.uArraySize * fMapResW);
+ fWidthScale = (fWidthScale > 1.0f) ? (1.0f) : (fWidthScale);
+
+ float fOneFifth = (float)Height / (5.0f);
+ float fHeightScale = fOneFifth / fMapResH;
+ fHeightScale = (fHeightScale > 1.0f) ? (1.0f) : (fHeightScale);
+
+ float fScale = (fHeightScale < fWidthScale) ? (fHeightScale) : (fWidthScale);
+
+ fMapResW = floorf(fMapResW * fScale);
+ fMapResH = floorf(fMapResH * fScale);
+
+ for (unsigned int j = 0; j < (unsigned int)m_SMDesc.uArraySize; j++)
+ {
+ m_shadowLibContext->DevModeDisplayMap(m_shadowBufferHandle,
+ &ColorRTV,
+ m_shadowMapHandle,
+ j,
+ j * (unsigned int)fMapResW + j,
+ Height - (unsigned int)fMapResH,
+ fScale);
+ }
+}
+
+
+void RendererShadow::displayMapFrustums(ID3D11RenderTargetView* pOutputRTV, ID3D11DepthStencilView* pDSV)
+{
+ gfsdk_float3 v3Color;
+ v3Color.x = 1.0f;
+ v3Color.y = 0.0f;
+ v3Color.z = 0.0f;
+
+ GFSDK_ShadowLib_RenderTargetView ColorRTV;
+ ColorRTV.pRTV = pOutputRTV;
+
+ GFSDK_ShadowLib_DepthStencilView DSV;
+ DSV.pDSV = pDSV;
+
+ unsigned int NumViews;
+ NumViews = m_SMDesc.eViewType;
+
+ for (unsigned int j = 0; j < NumViews; j++)
+ {
+ switch (j)
+ {
+ case 0:
+ v3Color.x = 1.0f;
+ v3Color.y = 0.0f;
+ v3Color.z = 0.0f;
+ break;
+ case 1:
+ v3Color.x = 0.0f;
+ v3Color.y = 1.0f;
+ v3Color.z = 0.0f;
+ break;
+ case 2:
+ v3Color.x = 0.0f;
+ v3Color.y = 0.0f;
+ v3Color.z = 1.0f;
+ break;
+ case 3:
+ v3Color.x = 1.0f;
+ v3Color.y = 1.0f;
+ v3Color.z = 0.0f;
+ break;
+ }
+
+ m_shadowLibContext->DevModeDisplayMapFrustum(m_shadowBufferHandle,
+ &ColorRTV,
+ &DSV,
+ m_shadowMapHandle,
+ j,
+ v3Color);
+ }
+}
+
+
+void RendererShadow::displayShadowBuffer(ID3D11RenderTargetView* pOutputRTV)
+{
+ gfsdk_float2 v2Scale;
+ v2Scale.x = 1.0f;
+ v2Scale.y = 1.0f;
+
+ GFSDK_ShadowLib_RenderTargetView ColorRTV;
+ ColorRTV.pRTV = pOutputRTV;
+
+ m_shadowLibContext->DevModeDisplayBuffer(m_shadowBufferHandle,
+ &ColorRTV,
+ v2Scale,
+ NULL);
+}
+
+
+void RendererShadow::toggleDisplayCascades(bool bToggle)
+{
+ m_shadowLibContext->DevModeToggleDebugCascadeShader(m_shadowBufferHandle,
+ bToggle);
+}
+
+
+void RendererShadow::drawUI()
+{
+ ImGui::Checkbox("PCSS", &m_PCSSEnabled);
+ ImGui::ColorEdit3("Shadow Color", &(m_shadowColor.x));
+ ImGui::DragFloat("Light Size", &m_lightSize, 0.05f, 0.0f, 100.0f);
+ ImGui::DragFloat3("Light Position", &(m_lightPos.x));
+ ImGui_DragFloat3Dir("Light LookAt", &(m_lightLookAt.x));
+ ImGui::DragFloat("SoftShadowTestScale", &(m_softShadowTestScale), 0.0001f, 0.0f, 10.0f);
+ if (m_PCSSEnabled)
+ {
+ ImGui::DragFloat("PCSS: fMaxClamp", &(m_PCSSParams.fMaxClamp), 0.001f, 0.0f, 100.0f);
+ ImGui::DragFloat("PCSS: fMaxThreshold", &(m_PCSSParams.fMaxThreshold), 0.001f, 0.0f, 100.0f);
+ ImGui::DragFloat("PCSS: fMinSizePercent", &(m_PCSSParams.fMinSizePercent), 0.001f, 0.0f, 100.0f);
+ ImGui::DragFloat("PCSS: fMinWeightExponent", &(m_PCSSParams.fMinWeightExponent), 0.001f, 0.0f, 100.0f);
+ ImGui::DragFloat("PCSS: fMinWeightThresholdPercent", &(m_PCSSParams.fMinWeightThresholdPercent), 0.001f, 0.0f, 100.0f);
+ ImGui::DragFloat("PCSS: fBlockerSearchDitherPercent", &(m_PCSSParams.fBlockerSearchDitherPercent), 0.001f, 0.0f, 100.0f);
+ ImGui::DragFloat("PCSS: fFilterDitherPercent", &(m_PCSSParams.fFilterDitherPercent), 0.001f, 0.0f, 100.0f);
+ }
+} \ No newline at end of file
diff --git a/NvCloth/samples/SampleBase/renderer/RendererShadow.h b/NvCloth/samples/SampleBase/renderer/RendererShadow.h
new file mode 100644
index 0000000..d8d027b
--- /dev/null
+++ b/NvCloth/samples/SampleBase/renderer/RendererShadow.h
@@ -0,0 +1,82 @@
+/*
+* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
+*
+* NVIDIA CORPORATION and its licensors retain all intellectual property
+* and proprietary rights in and to this software, related documentation
+* and any modifications thereto. Any use, reproduction, disclosure or
+* distribution of this software and related documentation without an express
+* license agreement from NVIDIA CORPORATION is strictly prohibited.
+*/
+
+#ifndef RENDERER_SHADOW_H
+#define RENDERER_SHADOW_H
+
+#include <DirectXMath.h>
+#include "Utils.h"
+#include "gfsdk_shadowlib.h"
+
+#include <string>
+
+
+class CFirstPersonCamera;
+class Renderer;
+
+class RendererShadow
+{
+public:
+ RendererShadow();
+ ~RendererShadow();
+
+ void createResources(ID3D11Device *pd3dDevice, ID3D11DeviceContext* context, CFirstPersonCamera* camera);
+
+ void setScreenResolution(float FovyRad, UINT Width, UINT Height, UINT uSampleCount, ID3D11DepthStencilView* pReadOnlyDSV);
+ void changeShadowSettings(UINT Width, UINT Height, UINT uSampleCount, ID3D11DepthStencilView* pReadOnlyDSV);
+ void renderShadowMaps(Renderer* renderer);
+ void renderShadowBuffer(ID3D11ShaderResourceView* pDepthStencilSRV, ID3D11ShaderResourceView* pResolvedDepthStencilSRV);
+ void modulateShadowBuffer(ID3D11RenderTargetView* pOutputRTV);
+ void displayShadowMaps(ID3D11RenderTargetView* pOutputRTV, UINT Width, UINT Height);
+ void displayMapFrustums(ID3D11RenderTargetView* pOutputRTV, ID3D11DepthStencilView* pDSV);
+ void displayShadowBuffer(ID3D11RenderTargetView* pOutputRTV);
+ void toggleDisplayCascades(bool bToggle);
+
+
+ void drawUI();
+
+private:
+ void reloadBuffers();
+ void ReleaseResources();
+
+
+ GFSDK_ShadowLib_Context* m_shadowLibContext;
+
+ GFSDK_ShadowLib_ShaderResourceView m_shadowBufferSRV;
+
+ GFSDK_ShadowLib_Map* m_shadowMapHandle;
+ GFSDK_ShadowLib_MapDesc m_SMDesc;
+ GFSDK_ShadowLib_BufferDesc m_SBDesc;
+ GFSDK_ShadowLib_MapRenderParams m_SMRenderParams;
+
+ GFSDK_ShadowLib_Buffer* m_shadowBufferHandle;
+ GFSDK_ShadowLib_BufferRenderParams m_SBRenderParams;
+
+ GFSDK_ShadowLib_TempResources m_tempResources;
+ GFSDK_ShadowLib_Texture2D m_downsampledShadowMap;
+
+ CFirstPersonCamera* m_camera;
+
+ // params
+ bool m_PCSSEnabled;
+ float m_lightSize;
+ DirectX::XMFLOAT3 m_lightPos;
+ DirectX::XMFLOAT3 m_lightLookAt;
+ DirectX::XMFLOAT3 m_shadowColor;
+ GFSDK_ShadowLib_PCSSPenumbraParams m_PCSSParams;
+ float m_softShadowTestScale;
+
+ gfsdk_float3 m_worldSpaceBBox0;
+ gfsdk_float3 m_worldSpaceBBox1;
+
+};
+
+
+#endif \ No newline at end of file
diff --git a/NvCloth/samples/SampleBase/renderer/ResourceManager.cpp b/NvCloth/samples/SampleBase/renderer/ResourceManager.cpp
new file mode 100644
index 0000000..fc76e1c
--- /dev/null
+++ b/NvCloth/samples/SampleBase/renderer/ResourceManager.cpp
@@ -0,0 +1,210 @@
+/*
+* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
+*
+* NVIDIA CORPORATION and its licensors retain all intellectual property
+* and proprietary rights in and to this software, related documentation
+* and any modifications thereto. Any use, reproduction, disclosure or
+* distribution of this software and related documentation without an express
+* license agreement from NVIDIA CORPORATION is strictly prohibited.
+*/
+
+#include "ResourceManager.h"
+#include "PxAssert.h"
+#include "Utils.h"
+
+#include <windows.h>
+#include <string.h>
+
+
+#define PATH_MAX_LEN 512
+
+
+ResourceManager::ResourceManager()
+{
+ // search for root folder by default
+ addSearchDir(".");
+}
+
+const ShaderFileResource* ResourceManager::requestShaderFile(const char* name)
+{
+ const Resource* resource = requestResource(eSHADER_FILE, name);
+ return resource != nullptr ? static_cast<const ShaderFileResource*>(resource) : nullptr;
+}
+
+const TextureResource* ResourceManager::requestTexture(const char* name)
+{
+ const Resource* resource = requestResource(eTEXTURE, name);
+ return resource != nullptr ? static_cast<const TextureResource*>(resource) : nullptr;
+}
+
+const Resource* ResourceManager::requestResource(ResourceType type, const char* name)
+{
+ // search in loaded
+ std::pair<ResourceType, std::string> key(type, name);
+ auto val = m_loadedResources.find(key);
+ if (val != m_loadedResources.end())
+ {
+ return val->second.get();
+ }
+
+ std::shared_ptr<Resource> resource;
+ if (type == eSHADER_FILE)
+ {
+ char path[PATH_MAX_LEN];
+ const char* exts[] = { "hlsl" };
+ if (findFile(name, std::vector<const char*>(exts, exts + sizeof(exts) / sizeof(exts[0])), path))
+ {
+ resource = std::shared_ptr<Resource>(new ShaderFileResource(path));
+ }
+ else
+ {
+ PX_ALWAYS_ASSERT_MESSAGE(name);
+ }
+ }
+ else if (type == eTEXTURE)
+ {
+ char path[PATH_MAX_LEN];
+ const char* exts[] = { "dds", "tga" };
+ if (findFile(name, std::vector<const char*>(exts, exts + sizeof(exts) / sizeof(exts[0])), path))
+ {
+ std::shared_ptr<TextureResource> textureResource(new TextureResource());
+ WCHAR wPath[MAX_PATH];
+ MultiByteToWideChar(CP_ACP, 0, path, -1, wPath, MAX_PATH);
+ wPath[MAX_PATH - 1] = 0;
+
+ const char* ext = strext(path);
+ if (::strcmp(ext, "dds") == 0)
+ {
+ V(DirectX::LoadFromDDSFile(wPath, DirectX::DDS_FLAGS_NONE, &textureResource->metaData,
+ textureResource->image));
+ }
+ else if (::strcmp(ext, "tga") == 0)
+ {
+ V(DirectX::LoadFromTGAFile(wPath, &textureResource->metaData,
+ textureResource->image));
+ }
+ else
+ {
+ PX_ALWAYS_ASSERT_MESSAGE("Unsupported texture extension");
+ }
+ resource = textureResource;
+ }
+ }
+
+ if (resource.get())
+ {
+ m_loadedResources.emplace(key, resource);
+ return resource.get();
+ }
+ else
+ {
+ PX_ALWAYS_ASSERT_MESSAGE(name);
+ return nullptr;
+ }
+}
+
+bool dirExists(const char* dir)
+{
+ DWORD ftyp = GetFileAttributesA(dir);
+ if (ftyp == INVALID_FILE_ATTRIBUTES)
+ return false; // something is wrong with path!
+
+ if (ftyp & FILE_ATTRIBUTE_DIRECTORY)
+ return true; // this is a directory!
+
+ return false; // this is not a directory!
+}
+
+bool ResourceManager::addSearchDir(const char* dir, bool recursive)
+{
+ if (dirExists(dir))
+ {
+ m_searchDirs.push_back(SearchDir(dir, recursive));
+ return true;
+ }
+ return false;
+}
+
+
+ResourceManager::~ResourceManager()
+{
+}
+
+
+bool ResourceManager::findFileInDir(std::string fileNameFull, const char* path, bool recursive, char* foundPath)
+{
+ WIN32_FIND_DATAA ffd;
+ char tmp[PATH_MAX_LEN];
+ ::snprintf(tmp, sizeof(tmp), "%s\\*", path);
+ HANDLE hFind = FindFirstFileA(tmp, &ffd);
+
+ if(INVALID_HANDLE_VALUE == hFind)
+ {
+ return NULL;
+ }
+
+ do
+ {
+ if (0 == ::strcmp(".", ffd.cFileName) || 0 == ::strcmp("..", ffd.cFileName))
+ continue;
+
+ if(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
+ {
+ ::snprintf(tmp, sizeof(tmp), "%s\\%s", path, ffd.cFileName);
+ if(findFileInDir(fileNameFull, tmp, recursive, foundPath))
+ return true;
+ }
+ else if (::stricmp(ffd.cFileName, fileNameFull.c_str()) == 0)
+ {
+ ::snprintf(foundPath, PATH_MAX_LEN, "%s\\%s", path, ffd.cFileName);
+ return true;
+ }
+ } while(FindNextFileA(hFind, &ffd) != 0);
+ // release handle
+ FindClose(hFind);
+ return false;
+}
+
+bool ResourceManager::findFile(std::string fileName, const std::vector<const char*>& exts, char* foundPath)
+{
+ std::string fileNameOnly = fileName;
+ size_t ind = fileNameOnly.find_last_of('/');
+ if (ind > 0)
+ fileNameOnly = fileNameOnly.substr(ind + 1);
+
+ for(size_t i = 0; i < m_searchDirs.size(); i++)
+ {
+ const SearchDir& searchDir = m_searchDirs[i];
+
+ for(size_t j = 0; j < exts.size(); j++)
+ {
+ const char* ext = exts[j];
+ const uint32_t fileMaxLen = 128;
+ char fileNameFull[fileMaxLen] = { 0 };
+
+ ::snprintf(fileNameFull, fileMaxLen, "%s.%s", fileNameOnly.c_str(), ext);
+ if(findFileInDir(fileNameFull, searchDir.path.c_str(), searchDir.recursive, foundPath))
+ return true;
+ }
+
+ if (findFileInDir(fileNameOnly.c_str(), searchDir.path.c_str(), searchDir.recursive, foundPath))
+ return true;
+ }
+ return false;
+}
+
+bool ResourceManager::findFile(std::string fileName, std::string& foundPath)
+{
+ std::vector<const char*> exts;
+ char path[PATH_MAX_LEN];
+ if (findFile(fileName, exts, path))
+ {
+ foundPath = path;
+ return true;
+ }
+ else
+ {
+ return false;
+ }
+}
+
diff --git a/NvCloth/samples/SampleBase/renderer/ResourceManager.h b/NvCloth/samples/SampleBase/renderer/ResourceManager.h
new file mode 100644
index 0000000..bc06c92
--- /dev/null
+++ b/NvCloth/samples/SampleBase/renderer/ResourceManager.h
@@ -0,0 +1,93 @@
+/*
+* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
+*
+* NVIDIA CORPORATION and its licensors retain all intellectual property
+* and proprietary rights in and to this software, related documentation
+* and any modifications thereto. Any use, reproduction, disclosure or
+* distribution of this software and related documentation without an express
+* license agreement from NVIDIA CORPORATION is strictly prohibited.
+*/
+
+#ifndef RESOURCE_MANAGER_H
+#define RESOURCE_MANAGER_H
+
+#include <vector>
+#include <string>
+#include <map>
+#include <memory>
+#include "DirectXTex.h"
+
+
+struct Resource
+{
+private:
+ Resource& operator = (const Resource&);
+};
+
+
+struct ShaderFileResource : public Resource
+{
+ ShaderFileResource(const std::string& p) : path(p) {}
+ std::string path;
+};
+
+
+struct TextureResource : public Resource
+{
+ DirectX::TexMetadata metaData;
+ DirectX::ScratchImage image;
+};
+
+
+/**
+ResourceManager used to look for files in provided dirs (see addSearchDir). Also it loads resources and caches them.
+*/
+class ResourceManager
+{
+public:
+ //////// ctor ////////
+
+ ResourceManager();
+ ~ResourceManager();
+
+ //////// public API ////////
+
+ bool addSearchDir(const char* dir, bool recursive = true);
+
+ const ShaderFileResource* requestShaderFile(const char* name);
+
+ const TextureResource* requestTexture(const char* name);
+
+ bool findFile(std::string fileName, std::string& foundPath);
+
+ bool findFile(std::string fileName, const std::vector<const char*>& exts, char* foundPath);
+
+
+private:
+ //////// internal methods ////////
+
+ enum ResourceType
+ {
+ eSHADER_FILE,
+ eTEXTURE
+ };
+
+ const Resource* requestResource(ResourceType type, const char* name);
+
+ bool findFileInDir(std::string fileNameFull, const char* path, bool recursive, char* foundPath);
+
+ struct SearchDir
+ {
+ SearchDir(std::string path_, bool recursive_) : path(path_), recursive(recursive_) {}
+
+ std::string path;
+ bool recursive;
+ };
+
+
+ //////// internal data ////////
+
+ std::vector<SearchDir> m_searchDirs;
+ std::map<std::pair<ResourceType, std::string>, std::shared_ptr<Resource>> m_loadedResources;
+};
+#endif \ No newline at end of file
diff --git a/NvCloth/samples/SampleBase/renderer/ShaderUtils.h b/NvCloth/samples/SampleBase/renderer/ShaderUtils.h
new file mode 100644
index 0000000..b43e0f2
--- /dev/null
+++ b/NvCloth/samples/SampleBase/renderer/ShaderUtils.h
@@ -0,0 +1,98 @@
+/*
+* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
+*
+* NVIDIA CORPORATION and its licensors retain all intellectual property
+* and proprietary rights in and to this software, related documentation
+* and any modifications thereto. Any use, reproduction, disclosure or
+* distribution of this software and related documentation without an express
+* license agreement from NVIDIA CORPORATION is strictly prohibited.
+*/
+
+#ifndef SHADER_UTILS_H
+#define SHADER_UTILS_H
+
+#include "Utils.h"
+#include <d3dcompiler.h>
+
+
+static HRESULT CompileShaderFromFile(const char* szFileName, LPCSTR szEntryPoint, LPCSTR szShaderModel,
+ ID3DBlob** ppBlobOut)
+{
+ HRESULT hr = S_OK;
+ ID3DBlob* pErrorBlob = NULL;
+
+ WCHAR wFileName[MAX_PATH];
+ MultiByteToWideChar(CP_ACP, 0, szFileName, -1, wFileName, MAX_PATH);
+ wFileName[MAX_PATH - 1] = 0;
+ hr = D3DCompileFromFile(wFileName, NULL, D3D_COMPILE_STANDARD_FILE_INCLUDE, szEntryPoint, szShaderModel, D3D10_SHADER_ENABLE_STRICTNESS, 0,
+ ppBlobOut, &pErrorBlob);
+ if(FAILED(hr))
+ {
+ OutputDebugStringA((char*)pErrorBlob->GetBufferPointer());
+ SAFE_RELEASE(pErrorBlob);
+ return hr;
+ }
+ SAFE_RELEASE(pErrorBlob);
+
+ return S_OK;
+}
+
+static HRESULT createShader(ID3D11Device* pDev, const void* pData, size_t len, ID3D11VertexShader** ppShd, bool)
+{
+ return pDev->CreateVertexShader(pData, len, nullptr, ppShd);
+}
+
+static HRESULT createShader(ID3D11Device* pDev, const void* pData, size_t len, ID3D11GeometryShader** ppShd,
+ bool forceFast)
+{
+ return pDev->CreateGeometryShader(pData, len, nullptr, ppShd);
+}
+
+static HRESULT createShader(ID3D11Device* pDev, const void* pData, size_t len, ID3D11PixelShader** ppShd, bool)
+{
+ return pDev->CreatePixelShader(pData, len, nullptr, ppShd);
+}
+
+static const char* shaderModel(ID3D11VertexShader**)
+{
+ return "vs_5_0";
+}
+
+static const char* shaderModel(ID3D11GeometryShader**)
+{
+ return "gs_5_0";
+}
+
+static const char* shaderModel(ID3D11PixelShader**)
+{
+ return "ps_5_0";
+}
+
+// Give back the shader buffer blob for use in CreateVertexLayout. Caller must release the blob.
+template <class S>
+static HRESULT createShaderFromFile(ID3D11Device* pDev, const char* szFileName, LPCSTR szEntryPoint, S** ppShd,
+ ID3DBlob*& pShaderBuffer, bool forceFast = false)
+{
+ HRESULT hr = CompileShaderFromFile(szFileName, szEntryPoint, shaderModel(ppShd), &pShaderBuffer);
+ if(SUCCEEDED(hr) && pShaderBuffer)
+ {
+ const void* shaderBufferData = pShaderBuffer->GetBufferPointer();
+ const UINT shaderBufferSize = pShaderBuffer->GetBufferSize();
+ createShader(pDev, shaderBufferData, shaderBufferSize, ppShd, forceFast);
+ }
+ return hr;
+}
+
+// Overloaded, same as above but don't give back the shader buffer blob.
+template <class S>
+static HRESULT createShaderFromFile(ID3D11Device* pDev, const char* szFileName, LPCSTR szEntryPoint, S** ppShd,
+ bool forceFast = false)
+{
+ ID3DBlob* pShaderBuffer = NULL;
+ HRESULT hr = createShaderFromFile(pDev, szFileName, szEntryPoint, ppShd, pShaderBuffer, forceFast);
+ SAFE_RELEASE(pShaderBuffer);
+ return hr;
+}
+
+
+#endif //SHADER_UTILS_H \ No newline at end of file
diff --git a/NvCloth/samples/SampleBase/renderer/SkinnedRenderMesh.cpp b/NvCloth/samples/SampleBase/renderer/SkinnedRenderMesh.cpp
new file mode 100644
index 0000000..9f0b022
--- /dev/null
+++ b/NvCloth/samples/SampleBase/renderer/SkinnedRenderMesh.cpp
@@ -0,0 +1,217 @@
+/*
+* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
+*
+* NVIDIA CORPORATION and its licensors retain all intellectual property
+* and proprietary rights in and to this software, related documentation
+* and any modifications thereto. Any use, reproduction, disclosure or
+* distribution of this software and related documentation without an express
+* license agreement from NVIDIA CORPORATION is strictly prohibited.
+*/
+
+
+#include "SkinnedRenderMesh.h"
+#include "Renderer.h"
+
+SkinnedRenderMesh::SkinnedRenderMesh(const std::vector<const SimpleMesh*>& meshes)
+{
+ PX_ASSERT_WITH_MESSAGE(meshes.size() <= MeshesCountMax, "meshes.size() have to be <= SkinnedRenderMesh::MeshesCountMax");
+
+ m_device = GetDeviceManager()->GetDevice();
+
+ // input element desc setup
+ m_inputDesc.push_back({ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 });
+ m_inputDesc.push_back({ "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 });
+ m_inputDesc.push_back({ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 24, D3D11_INPUT_PER_VERTEX_DATA, 0 });
+ m_inputDesc.push_back({ "TEXCOORD", 1, DXGI_FORMAT_R32_UINT, 1, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 });
+
+ // reserve VB
+ uint32_t verticesTotal = 0;
+ std::for_each(meshes.begin(), meshes.end(), [&](const SimpleMesh* c) { verticesTotal += (uint32_t)c->vertices.size(); });
+ std::vector<SimpleMesh::Vertex> vertexBuffer;
+ vertexBuffer.reserve(verticesTotal);
+
+ // reserve IB
+ uint32_t indicesTotal = 0;
+ std::for_each(meshes.begin(), meshes.end(), [&](const SimpleMesh* c) { indicesTotal += (uint32_t)c->indices.size(); });
+ m_indices.reserve(indicesTotal);
+
+ // fill VB, IB, MeshInfo
+ m_meshesInfo.resize(meshes.size());
+ for (uint32_t meshIndex = 0; meshIndex < meshes.size(); ++meshIndex)
+ {
+ const SimpleMesh* mesh = meshes[meshIndex];
+ MeshInfo& meshInfo = m_meshesInfo[meshIndex];
+
+ meshInfo.firstVertex = (uint32_t)vertexBuffer.size();
+ vertexBuffer.insert(vertexBuffer.end(), mesh->vertices.begin(), mesh->vertices.end());
+ meshInfo.verticesCount = (uint32_t)mesh->vertices.size();
+
+ meshInfo.firstIndex = (uint32_t)m_indices.size();
+ uint32_t indexOffset = meshInfo.firstVertex;
+ for (uint32_t index : mesh->indices)
+ {
+ m_indices.push_back((uint32_t)index + indexOffset);
+ }
+ meshInfo.indicesCount = (uint32_t)mesh->indices.size();
+ }
+
+ // vertex buffer
+ {
+ D3D11_SUBRESOURCE_DATA vertexBufferData;
+
+ ZeroMemory(&vertexBufferData, sizeof(vertexBufferData));
+ vertexBufferData.pSysMem = vertexBuffer.data();
+
+ D3D11_BUFFER_DESC bufferDesc;
+
+ memset(&bufferDesc, 0, sizeof(D3D11_BUFFER_DESC));
+ bufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
+ bufferDesc.ByteWidth = (uint32_t)(sizeof(SimpleMesh::Vertex) * vertexBuffer.size());
+ bufferDesc.CPUAccessFlags = 0;
+ bufferDesc.MiscFlags = 0;
+ bufferDesc.Usage = D3D11_USAGE_IMMUTABLE;
+
+ V(m_device->CreateBuffer(&bufferDesc, &vertexBufferData, &m_vertexBuffer));
+ }
+
+ // bone index buffer
+ {
+ D3D11_BUFFER_DESC bufferDesc;
+
+ memset(&bufferDesc, 0, sizeof(D3D11_BUFFER_DESC));
+ bufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
+ bufferDesc.ByteWidth = (uint32_t)(sizeof(uint32_t) * vertexBuffer.size());
+ bufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
+ bufferDesc.MiscFlags = 0;
+ bufferDesc.Usage = D3D11_USAGE_DYNAMIC;
+
+ V(m_device->CreateBuffer(&bufferDesc, nullptr, &m_boneIndexBuffer));
+ }
+
+ // index buffer
+ {
+ D3D11_BUFFER_DESC bufferDesc;
+
+ memset(&bufferDesc, 0, sizeof(D3D11_BUFFER_DESC));
+ bufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
+ bufferDesc.ByteWidth = (uint32_t)(sizeof(uint32_t) * m_indices.size());
+ bufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
+ bufferDesc.MiscFlags = 0;
+ bufferDesc.Usage = D3D11_USAGE_DYNAMIC;
+
+ V(m_device->CreateBuffer(&bufferDesc, nullptr, &m_indexBuffer));
+ }
+
+ // bone texture
+ {
+ D3D11_TEXTURE2D_DESC desc;
+ ZeroMemory(&desc, sizeof(desc));
+ desc.Width = 4;
+ desc.Height = (uint32_t)meshes.size();
+ desc.MipLevels = 1;
+ desc.ArraySize = 1;
+ desc.Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
+ desc.SampleDesc.Count = 1;
+ desc.SampleDesc.Quality = 0;
+ desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
+ desc.Usage = D3D11_USAGE_DYNAMIC;
+ desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
+
+ V(m_device->CreateTexture2D(&desc, nullptr, &m_boneTexture));
+ }
+
+ // bone texture SRV
+ {
+ D3D11_SHADER_RESOURCE_VIEW_DESC desc;
+ ZeroMemory(&desc, sizeof(desc));
+ desc.Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
+ desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
+ desc.Texture2D.MipLevels = 1;
+ desc.Texture2D.MostDetailedMip = 0;
+ V(m_device->CreateShaderResourceView(m_boneTexture, &desc, &m_boneTextureSRV));
+ }
+}
+
+SkinnedRenderMesh::~SkinnedRenderMesh()
+{
+ SAFE_RELEASE(m_vertexBuffer);
+ SAFE_RELEASE(m_boneIndexBuffer);
+ SAFE_RELEASE(m_indexBuffer);
+ SAFE_RELEASE(m_boneTexture);
+ SAFE_RELEASE(m_boneTextureSRV);
+}
+
+void SkinnedRenderMesh::updateVisibleMeshes(const std::vector<uint32_t>& visibleMeshes)
+{
+ ID3D11DeviceContext* context;
+ m_device->GetImmediateContext(&context);
+
+ // update bone index buffer
+ {
+ D3D11_MAPPED_SUBRESOURCE mappedRead;
+ V(context->Map(m_boneIndexBuffer, 0, D3D11_MAP_WRITE_DISCARD, NULL, &mappedRead));
+
+ uint32_t* boneIndexBuffer = (uint32_t*)mappedRead.pData;
+ for (uint32_t i = 0; i < visibleMeshes.size(); ++i)
+ {
+ const MeshInfo& info = m_meshesInfo[visibleMeshes[i]];
+ for (uint32_t v = info.firstVertex; v < info.firstVertex + info.verticesCount; ++v)
+ {
+ boneIndexBuffer[v] = i;
+ }
+ }
+
+ context->Unmap(m_boneIndexBuffer, 0);
+ }
+
+ // update index buffer
+ {
+ D3D11_MAPPED_SUBRESOURCE mappedRead;
+ V(context->Map(m_indexBuffer, 0, D3D11_MAP_WRITE_DISCARD, NULL, &mappedRead));
+
+ uint32_t* indexBuffer = (uint32_t*)mappedRead.pData;
+ uint32_t indexCount = 0;
+ for (uint32_t meshIndex : visibleMeshes)
+ {
+ const MeshInfo& info = m_meshesInfo[meshIndex];
+ memcpy(indexBuffer + indexCount, &m_indices[info.firstIndex], info.indicesCount * sizeof(uint32_t));
+ indexCount += info.indicesCount;
+ }
+ context->Unmap(m_indexBuffer, 0);
+ m_indexCount = indexCount;
+ PX_ASSERT(m_indexCount % 3 == 0);
+ }
+}
+
+void SkinnedRenderMesh::updateVisibleMeshTransforms(std::vector<PxMat44>& transforms)
+{
+ ID3D11DeviceContext* context;
+ m_device->GetImmediateContext(&context);
+
+ // update bone transform texture
+ {
+ D3D11_MAPPED_SUBRESOURCE mappedRead;
+ V(context->Map(m_boneTexture, 0, D3D11_MAP_WRITE_DISCARD, NULL, &mappedRead));
+ for (uint32_t i = 0; i < transforms.size(); ++i)
+ {
+ std::memcpy((uint8_t*)mappedRead.pData + i * mappedRead.RowPitch, &transforms[i], sizeof(PxMat44));
+ }
+ context->Unmap(m_boneTexture, 0);
+ }
+}
+
+void SkinnedRenderMesh::render(ID3D11DeviceContext& context) const
+{
+ context.IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
+
+ UINT strides[2] = { sizeof(SimpleMesh::Vertex), sizeof(uint32_t) };
+ UINT offsets[2] = { 0 };
+ ID3D11Buffer* buffers[2] = { m_vertexBuffer, m_boneIndexBuffer };
+ context.IASetVertexBuffers(0, 2, buffers, strides, offsets);
+
+ context.IASetIndexBuffer(m_indexBuffer, DXGI_FORMAT_R32_UINT, 0);
+
+ context.VSSetShaderResources(1, 1, &m_boneTextureSRV);
+
+ context.DrawIndexed(m_indexCount, 0, 0);
+} \ No newline at end of file
diff --git a/NvCloth/samples/SampleBase/renderer/SkinnedRenderMesh.h b/NvCloth/samples/SampleBase/renderer/SkinnedRenderMesh.h
new file mode 100644
index 0000000..cc30e89
--- /dev/null
+++ b/NvCloth/samples/SampleBase/renderer/SkinnedRenderMesh.h
@@ -0,0 +1,82 @@
+/*
+* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
+*
+* NVIDIA CORPORATION and its licensors retain all intellectual property
+* and proprietary rights in and to this software, related documentation
+* and any modifications thereto. Any use, reproduction, disclosure or
+* distribution of this software and related documentation without an express
+* license agreement from NVIDIA CORPORATION is strictly prohibited.
+*/
+
+#ifndef SKINNED_RENDER_MESH_H
+#define SKINNED_RENDER_MESH_H
+
+#include "Utils.h"
+#include <DirectXMath.h>
+
+#include <vector>
+#include "Renderable.h"
+#include "Mesh.h"
+
+/**
+SkinnedRenderMesh:
+ bonde indices are passed as vertex input,
+ bone transforms are stored in texture
+ max bone meshes count: SkinnedRenderMesh::MeshesCountMax
+*/
+class SkinnedRenderMesh : public IRenderMesh
+{
+public:
+ //////// ctor ////////
+
+ SkinnedRenderMesh(const std::vector<const SimpleMesh*>& meshes);
+ ~SkinnedRenderMesh();
+
+
+ //////// const ////////
+
+ static const uint32_t MeshesCountMax = D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION;
+
+
+ //////// public API ////////
+
+ void updateVisibleMeshes(const std::vector<uint32_t>& visibleMeshes);
+ void updateVisibleMeshTransforms(std::vector<PxMat44>& transforms);
+
+
+ //////// IRenderMesh implementation ////////
+
+ virtual const std::vector<D3D11_INPUT_ELEMENT_DESC>& getInputElementDesc() const { return m_inputDesc; }
+ virtual void render(ID3D11DeviceContext& context) const;
+
+private:
+ //////// internal data ////////
+
+ struct MeshInfo
+ {
+ uint32_t firstIndex;
+ uint32_t indicesCount;
+
+ uint32_t firstVertex;
+ uint32_t verticesCount;
+ };
+
+ std::vector<D3D11_INPUT_ELEMENT_DESC> m_inputDesc;
+
+ ID3D11Device* m_device;
+
+ ID3D11Buffer* m_vertexBuffer;
+ ID3D11Buffer* m_boneIndexBuffer;
+ ID3D11Buffer* m_indexBuffer;
+ ID3D11Texture2D* m_boneTexture;
+ ID3D11ShaderResourceView* m_boneTextureSRV;
+
+ uint32_t m_indexCount;
+
+ std::vector<MeshInfo> m_meshesInfo;
+ std::vector<uint32_t> m_indices;
+};
+
+
+
+#endif //SKINNED_RENDER_MESH_H \ No newline at end of file
diff --git a/NvCloth/samples/SampleBase/scene/Scene.cpp b/NvCloth/samples/SampleBase/scene/Scene.cpp
new file mode 100644
index 0000000..130dad3
--- /dev/null
+++ b/NvCloth/samples/SampleBase/scene/Scene.cpp
@@ -0,0 +1,162 @@
+/*
+* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
+*
+* NVIDIA CORPORATION and its licensors retain all intellectual property
+* and proprietary rights in and to this software, related documentation
+* and any modifications thereto. Any use, reproduction, disclosure or
+* distribution of this software and related documentation without an express
+* license agreement from NVIDIA CORPORATION is strictly prohibited.
+*/
+
+#include "Scene.h"
+#include <NvCloth/Cloth.h>
+#include <NvCloth/Solver.h>
+#include <NvCloth/Fabric.h>
+#include "Renderer.h"
+
+std::vector<SceneFactory> Scene::sSceneFactories;
+
+Scene::~Scene()
+{
+
+}
+
+namespace
+{
+template <typename T> void trackT(std::vector<T>& list, T object)
+{
+ list.push_back(object);
+}
+
+template <typename T> void untrackT(std::vector<T>& list, T object)
+{
+ for(auto it = list.begin(); it != list.end(); ++it)
+ {
+ if(*it == object)
+ {
+ list.erase(it);
+ break;
+ }
+ }
+}
+}
+void Scene::trackClothActor(ClothActor* clothActor)
+{
+ trackT(mClothList, clothActor);
+}
+void Scene::untrackClothActor(ClothActor* clothActor)
+{
+ untrackT(mClothList, clothActor);
+}
+void Scene::trackSolver(nv::cloth::Solver* solver)
+{
+ trackT(mSolverList, solver);
+ mSolverHelpers[solver].Initialize(solver, mSceneController->getJobManager());
+}
+void Scene::untrackSolver(nv::cloth::Solver* solver)
+{
+ untrackT(mSolverList, solver);
+ mSolverHelpers.erase(solver);
+}
+void Scene::trackFabric(nv::cloth::Fabric* fabric)
+{
+ trackT(mFabricList, fabric);
+}
+void Scene::untrackFabric(nv::cloth::Fabric* fabric)
+{
+ untrackT(mFabricList, fabric);
+}
+
+void Scene::addClothToSolver(ClothActor* clothActor, nv::cloth::Solver* solver)
+{
+ solver->addCloth(clothActor->mCloth);
+ assert(mClothSolverMap.find(clothActor) == mClothSolverMap.end());
+ mClothSolverMap[clothActor] = solver;
+}
+
+void Scene::trackRenderable(Renderable* renderable)
+{
+ trackT(mRenderableList, renderable);
+}
+void Scene::untrackRenderable(Renderable* renderable)
+{
+ untrackT(mRenderableList, renderable);
+}
+
+void Scene::autoDeinitialize()
+{
+ //Remove all cloths from solvers
+ for (auto it : mClothSolverMap)
+ {
+ it.second->removeCloth(it.first->mCloth);
+ }
+ mClothSolverMap.clear();
+
+ //Destroy all solvers
+ for (auto it : mSolverList)
+ {
+ delete it;
+ }
+ mSolverList.clear();
+ mSolverHelpers.clear();
+
+ //Destroy all cloths
+ for (auto it : mClothList)
+ {
+ delete it->mCloth;
+ mSceneController->getRenderer().removeRenderable(it->mClothRenderable);
+ delete it->mClothRenderMesh;
+ delete it;
+ }
+ mClothList.clear();
+
+ //Destroy all fabrics
+ for (auto it : mFabricList)
+ {
+ it->decRefCount();
+ }
+ mFabricList.clear();
+
+ //Destroy all renderables
+ for(auto it : mRenderableList)
+ {
+ mSceneController->getRenderer().removeRenderable(it);
+ }
+ mRenderableList.clear();
+}
+
+void Scene::doSimulationStep(float dt)
+{
+ startSimulationStep(dt);
+ waitForSimulationStep();
+ updateSimulationGraphics();
+}
+
+void Scene::startSimulationStep(float dt)
+{
+ for(auto it = mSolverHelpers.begin(); it != mSolverHelpers.end(); ++it)
+ {
+ it->second.StartSimulation(dt);
+ }
+}
+void Scene::waitForSimulationStep()
+{
+ for(auto it = mSolverHelpers.begin(); it != mSolverHelpers.end(); ++it)
+ {
+ it->second.WaitForSimulation();
+ }
+}
+
+void Scene::updateSimulationGraphics()
+{
+ for each (auto actor in mClothList)
+ {
+ nv::cloth::MappedRange<physx::PxVec4> particles = actor->mCloth->getCurrentParticles();
+ std::vector<PxVec3> particles3(particles.size());
+ for(uint32_t i = 0; i < particles.size(); ++i)
+ particles3[i] = particles[i].getXYZ();
+
+ actor->mClothRenderMesh->update(particles3.data(), particles.size());
+ }
+}
+
diff --git a/NvCloth/samples/SampleBase/scene/Scene.h b/NvCloth/samples/SampleBase/scene/Scene.h
new file mode 100644
index 0000000..3a11ec7
--- /dev/null
+++ b/NvCloth/samples/SampleBase/scene/Scene.h
@@ -0,0 +1,104 @@
+/*
+* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
+*
+* NVIDIA CORPORATION and its licensors retain all intellectual property
+* and proprietary rights in and to this software, related documentation
+* and any modifications thereto. Any use, reproduction, disclosure or
+* distribution of this software and related documentation without an express
+* license agreement from NVIDIA CORPORATION is strictly prohibited.
+*/
+
+#ifndef SCENE_H
+#define SCENE_H
+
+#include <vector>
+#include <map>
+#include "scene/SceneController.h"
+namespace nv
+{
+ namespace cloth
+ {
+ class Factory;
+ class Solver;
+ class Cloth;
+ class Fabric;
+ }
+}
+
+class Scene;
+struct SceneFactory
+{
+ SceneFactory(Scene* (*create)(SceneController*), const char* name):Create(create), mName(name) {}
+ Scene* (*Create)(SceneController*);
+ const char* mName;
+};
+
+#define DECLARE_SCENE_NAME(classname, name) static int classname##id = Scene::AddSceneFactory([](SceneController* c) {return (Scene*)new classname(c); }, name);
+#define DECLARE_SCENE(classname) DECLARE_SCENE_NAME(classname,#classname)
+
+class Scene
+{
+public:
+
+ Scene(SceneController* sceneController):mSceneController(sceneController) {}
+ virtual ~Scene();
+
+ virtual void Animate(double dt) { doSimulationStep(dt); }
+ virtual void drawUI() {}
+ virtual void drawStatsUI() {}
+
+ virtual void onInitialize() = 0;
+ virtual void onTerminate() { autoDeinitialize(); }
+
+ SceneController* getSceneController() { return mSceneController; }
+
+ static int AddSceneFactory(Scene* (*create)(SceneController*), const char* name) { sSceneFactories.push_back(SceneFactory(create, name)); return (int)sSceneFactories.size(); }
+ static Scene* CreateScene(int index, SceneController* controller) { return sSceneFactories[index].Create(controller); }
+ static const char* GetSceneName(int index) { return sSceneFactories[index].mName; }
+ static int GetSceneCount() { return (int)sSceneFactories.size(); }
+
+protected:
+ //Helper functions to enable automatic deinitialize
+ //Tracking an object will delete it when autoDeinitialize is called
+ //Untracking can be used if you delete it sooner than autoDeinitialize
+ void trackClothActor(ClothActor* clothActor);
+ void untrackClothActor(ClothActor* clothActor);
+
+ void trackSolver(nv::cloth::Solver* solver);
+ void untrackSolver(nv::cloth::Solver* solver);
+
+ void trackFabric(nv::cloth::Fabric* fabric);
+ void untrackFabric(nv::cloth::Fabric* fabric);
+
+ void addClothToSolver(ClothActor* clothActor, nv::cloth::Solver* solver); //Helps to detach cloths from solver at AutoDeinit.
+
+ void trackRenderable(Renderable* renderMesh);
+ void untrackRenderable(Renderable* renderMesh);
+
+ void autoDeinitialize();
+
+
+ void doSimulationStep(float dt);
+ void startSimulationStep(float dt);
+ void waitForSimulationStep();
+ void updateSimulationGraphics();
+
+private:
+ Scene& operator= (Scene&); // not implemented
+
+private:
+ SceneController* mSceneController;
+
+ std::vector<ClothActor*> mClothList;
+ std::vector<nv::cloth::Solver*> mSolverList;
+ std::map<nv::cloth::Solver*, MultithreadedSolverHelper> mSolverHelpers;
+ std::vector<nv::cloth::Fabric*> mFabricList;
+ std::map<ClothActor*, nv::cloth::Solver*> mClothSolverMap;
+ std::vector<Renderable*> mRenderableList;
+
+ static std::vector<SceneFactory> sSceneFactories;
+
+};
+
+
+#endif \ No newline at end of file
diff --git a/NvCloth/samples/SampleBase/scene/SceneController.cpp b/NvCloth/samples/SampleBase/scene/SceneController.cpp
new file mode 100644
index 0000000..90b499c
--- /dev/null
+++ b/NvCloth/samples/SampleBase/scene/SceneController.cpp
@@ -0,0 +1,250 @@
+/*
+* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
+*
+* NVIDIA CORPORATION and its licensors retain all intellectual property
+* and proprietary rights in and to this software, related documentation
+* and any modifications thereto. Any use, reproduction, disclosure or
+* distribution of this software and related documentation without an express
+* license agreement from NVIDIA CORPORATION is strictly prohibited.
+*/
+
+#include "SceneController.h"
+#include "RenderUtils.h"
+#include "Utils.h"
+
+#include "Renderer.h"
+
+#include "CommonUIController.h"
+
+// initialization of NvCloth dll
+#include <NvCloth/Callbacks.h>
+
+// example allocator callbacks
+#include "CallbackImplementations.h"
+
+// low level cloth
+//#include <NvCloth/Factory.h>
+#include <NvCloth/Fabric.h>
+#include <NvCloth/Cloth.h>
+#include <NvCloth/Solver.h>
+
+#include <NvClothExt/ClothFabricCooker.h>
+#include "ClothMeshGenerator.h"
+
+#include <algorithm>
+#include <imgui.h>
+#include <sstream>
+#include <tuple>
+
+#include "scene/Scene.h"
+#include "scene/scenes/SimpleScene.h"
+#include "scene/scenes/WindScene.h"
+
+JobManager SceneController::sJobManager;
+
+///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+// Controller
+///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+
+SceneController::SceneController() : mTimeScale(1.0f), mStartDelay(0.f)
+{
+ mActivePlatform = (int)nv::cloth::Platform::CPU;
+ mCUDAInitialized = false;
+ mDXInitialized = false;
+}
+
+SceneController::~SceneController()
+{
+}
+
+void SceneController::onSampleStart()
+{
+ // setup camera
+ CFirstPersonCamera* camera = &getRenderer().getCamera();
+ DirectX::XMVECTORF32 lookAtPt = { 0, 10, 0, 0 };
+ DirectX::XMVECTORF32 eyePt = { 0, 15, 25, 0 };
+ camera->SetViewParams(eyePt, lookAtPt);
+ camera->SetRotateButtons(false, false, true, false);
+ camera->SetEnablePositionMovement(true);
+
+ mStartDelay = 3.f;
+
+ mPhysXPrimitiveRenderMaterial = new RenderMaterial(getRenderer().getResourceManager(), "physx_primitive", "");
+ mPhysXPlaneRenderMaterial = new RenderMaterial(getRenderer().getResourceManager(), "physx_primitive_plane", "");
+
+ /*{
+ IRenderMesh* mesh = getRenderer().getPrimitiveRenderMesh(PrimitiveRenderMeshType::Plane);
+ mPlane = getRenderer().createRenderable(*mesh, *mPhysXPlaneRenderMaterial);
+ mPlane->setTransform(PxTransform(PxVec3(0.f, 0.f, 0.f), PxQuat(PxPiDivTwo, PxVec3(0.f, 0.f, 1.f))));
+ mPlane->setScale(PxVec3(1000.f));
+ }*/
+
+ for (uint32_t i = 0; i < 0; ++i)
+ {
+ IRenderMesh* mesh = getRenderer().getPrimitiveRenderMesh(PrimitiveRenderMeshType::Box);
+ auto box = getRenderer().createRenderable(*mesh, *mPhysXPrimitiveRenderMaterial);
+ box->setColor(getRandomPastelColor());
+ box->setScale(PxVec3(0.2f));
+ mBoxes.push_back(box);
+ }
+
+
+ ///
+
+ mActiveScene = new SimpleScene(this);
+ mActiveScene->onInitialize();
+ mActiveSceneIndex = 0;
+}
+
+void SceneController::onInitialize()
+{
+ NvClothEnvironment::AllocateEnv();
+
+ mFactories[(int)nv::cloth::Platform::CPU] = NvClothCreateFactoryCPU();
+ mFactories[(int)nv::cloth::Platform::CUDA] = nullptr;
+ mFactories[(int)nv::cloth::Platform::DX11] = nullptr;
+
+ //CUDA
+ do
+ {
+ assert(mCUDAInitialized == false);
+ cuInit(0);
+ int deviceCount = 0;
+ mCUDAInitialized = true;
+ mCUDAInitialized = (cuDeviceGetCount(&deviceCount) == CUDA_SUCCESS);
+ mCUDAInitialized &= deviceCount >= 1;
+ if(!mCUDAInitialized)
+ break;
+ mCUDAInitialized = cuCtxCreate(&mCUDAContext, 0, 0) == CUDA_SUCCESS;
+ if(!mCUDAInitialized)
+ break;
+
+ mFactories[(int)nv::cloth::Platform::CUDA] = NvClothCreateFactoryCUDA(mCUDAContext);
+ } while(false);
+
+ //DX11
+ do
+ {
+ assert(mDXInitialized == false);
+ mDXInitialized = true;
+ mDXDevice = GetDeviceManager()->GetDevice();
+
+ mGraphicsContextManager = new DxContextManagerCallbackImpl(mDXDevice);
+ mDXInitialized &= mGraphicsContextManager != nullptr;
+ if(!mDXInitialized)
+ break;
+
+ mFactories[(int)nv::cloth::Platform::DX11] = NvClothCreateFactoryDX11(mGraphicsContextManager);
+ mDXInitialized &= mFactories[(int)nv::cloth::Platform::DX11] != nullptr;
+ } while(false);
+}
+
+void SceneController::onSampleStop()
+{
+ mActiveScene->onTerminate();
+ delete mActiveScene;
+
+ for (auto b : mBoxes)
+ SAFE_DELETE(b);
+
+ mBoxes.clear();
+
+ NvClothDestroyFactory(mFactories[(int)nv::cloth::Platform::CPU]);
+ NvClothDestroyFactory(mFactories[(int)nv::cloth::Platform::CUDA]);
+ NvClothDestroyFactory(mFactories[(int)nv::cloth::Platform::DX11]);
+
+ //SAFE_DELETE(mPlane);
+ SAFE_DELETE(mPhysXPlaneRenderMaterial);
+ SAFE_DELETE(mPhysXPrimitiveRenderMaterial);
+}
+
+void SceneController::onTerminate()
+{
+}
+
+void SceneController::changeScene(int index)
+{
+ mActiveScene->onTerminate();
+ delete mActiveScene;
+
+ mActiveSceneIndex = index;
+
+ if(index < Scene::GetSceneCount())
+ mActiveScene = Scene::CreateScene(index, this);
+ else
+ {
+ mActiveSceneIndex = 0;
+ mActiveScene = Scene::CreateScene(0, this);
+ }
+ mActiveScene->onInitialize();
+}
+
+void SceneController::Animate(double dt)
+{
+ double simulationStep = dt * mTimeScale;
+ if(simulationStep > 1.0 / 60.0)
+ simulationStep = 1.0 / 60.0;
+
+ mActiveScene->Animate(simulationStep);
+
+}
+
+LRESULT SceneController::MsgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
+{
+ if (uMsg == WM_KEYDOWN)
+ {
+ int iKeyPressed = static_cast<int>(wParam);
+
+ if(iKeyPressed >= '1' && iKeyPressed <= '9')
+ {
+ changeScene(iKeyPressed - '1');
+ }
+
+ switch (iKeyPressed)
+ {
+ case 'R':
+ return 0;
+ case 'F':
+ return 0;
+ default:
+ break;
+ }
+ }
+
+ return 1;
+}
+
+void SceneController::drawUI()
+{
+ ///////////////////////////////////////////////////////////////////////////////////////////
+ // Cube
+ ///////////////////////////////////////////////////////////////////////////////////////////
+ {
+ ImGui::Text("Time Scale");
+ ImGui::DragFloat("Scale", &mTimeScale, 0.1f, 0.0f, 100.0f);
+ }
+
+ bool pressed = false;
+
+ pressed = pressed | ImGui::RadioButton("CPU", &mActivePlatform, (int)nv::cloth::Platform::CPU); ImGui::SameLine();
+ pressed = pressed | ImGui::RadioButton("DX11", &mActivePlatform, (int)nv::cloth::Platform::DX11); ImGui::SameLine();
+ pressed = pressed | ImGui::RadioButton("CUDA", &mActivePlatform, (int)nv::cloth::Platform::CUDA);
+
+ if(!getFactory())
+ mActivePlatform = (int)nv::cloth::Platform::CPU;
+
+ for(int i = 0; i < Scene::GetSceneCount(); i++)
+ {
+ pressed = pressed | ImGui::RadioButton(Scene::GetSceneName(i), &mActiveSceneIndex, i);
+ }
+ if(pressed)
+ changeScene(mActiveSceneIndex);
+
+ mActiveScene->drawUI();
+}
+
+void SceneController::drawStatsUI()
+{
+ mActiveScene->drawStatsUI();
+}
+
diff --git a/NvCloth/samples/SampleBase/scene/SceneController.h b/NvCloth/samples/SampleBase/scene/SceneController.h
new file mode 100644
index 0000000..d527597
--- /dev/null
+++ b/NvCloth/samples/SampleBase/scene/SceneController.h
@@ -0,0 +1,128 @@
+/*
+* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
+*
+* NVIDIA CORPORATION and its licensors retain all intellectual property
+* and proprietary rights in and to this software, related documentation
+* and any modifications thereto. Any use, reproduction, disclosure or
+* distribution of this software and related documentation without an express
+* license agreement from NVIDIA CORPORATION is strictly prohibited.
+*/
+
+#ifndef SCENE_CONTROLLER_H
+#define SCENE_CONTROLLER_H
+
+
+#include "SampleManager.h"
+#include <map>
+#include <vector>
+
+#include <NvCloth/Factory.h>
+#include "JobManager.h"
+
+#include "ClothRenderMesh.h"
+#include <cuda.h>
+#include "CallbackImplementations.h"
+
+namespace nv
+{
+ namespace cloth
+ {
+ class Factory;
+ class Solver;
+ class Cloth;
+ class Fabric;
+ class DxContextManagerCallback;
+ }
+}
+
+class RenderMaterial;
+class Renderable;
+
+struct ClothActor
+{
+ Renderable* mClothRenderable;
+ ClothRenderMesh* mClothRenderMesh;
+ nv::cloth::Cloth* mCloth;
+};
+
+class Scene;
+
+class SceneController : public ISampleController
+{
+public:
+
+ SceneController();
+ virtual ~SceneController();
+
+ virtual LRESULT MsgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
+ virtual void Animate(double dt);
+ void drawUI();
+ void drawStatsUI();
+
+ virtual void onInitialize();
+ virtual void onSampleStart();
+ virtual void onSampleStop();
+ virtual void onTerminate();
+
+ JobManager* getJobManager() { return &sJobManager; }
+ nv::cloth::Factory* getFactory() { return mFactories[mActivePlatform]; }
+
+ static JobManager sJobManager;
+
+ //////// used controllers ////////
+
+ Renderer& getRenderer() const
+ {
+ return getManager()->getRenderer();
+ }
+
+ CommonUIController& getCommonUIController() const
+ {
+ return getManager()->getCommonUIController();
+ }
+
+ RenderMaterial* getDefaultMaterial()
+ {
+ return mPhysXPrimitiveRenderMaterial;
+ }
+
+ RenderMaterial* getDefaultPlaneMaterial()
+ {
+ return mPhysXPlaneRenderMaterial;
+ }
+
+
+private:
+ SceneController& operator= (SceneController&); // not implemented
+
+ void changeScene(int index);
+
+ //////// internal data ////////
+
+ RenderMaterial* mPhysXPrimitiveRenderMaterial;
+ RenderMaterial* mPhysXPlaneRenderMaterial;
+
+ Renderable* mPlane;
+ std::vector<Renderable*> mBoxes;
+
+ float mTimeScale;
+ float mStartDelay;
+
+ // NvCloth
+ CpuDispatcher mDispatcher;
+ physx::PxTaskManager* mTaskManager;
+
+ nv::cloth::Factory* mFactories[3];
+ int mActivePlatform;
+ bool mCUDAInitialized;
+ CUcontext mCUDAContext;
+ bool mDXInitialized;
+ ID3D11Device* mDXDevice;
+ ID3D11DeviceContext* mDXDeviceContext;
+ nv::cloth::DxContextManagerCallback* mGraphicsContextManager;
+
+ Scene* mActiveScene;
+ int mActiveSceneIndex;
+};
+
+#endif \ No newline at end of file
diff --git a/NvCloth/samples/SampleBase/scene/scenes/FreeFallScene.cpp b/NvCloth/samples/SampleBase/scene/scenes/FreeFallScene.cpp
new file mode 100644
index 0000000..c1f6a00
--- /dev/null
+++ b/NvCloth/samples/SampleBase/scene/scenes/FreeFallScene.cpp
@@ -0,0 +1,116 @@
+/*
+* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
+*
+* NVIDIA CORPORATION and its licensors retain all intellectual property
+* and proprietary rights in and to this software, related documentation
+* and any modifications thereto. Any use, reproduction, disclosure or
+* distribution of this software and related documentation without an express
+* license agreement from NVIDIA CORPORATION is strictly prohibited.
+*/
+
+#include "FreeFallScene.h"
+#include "Scene/SceneController.h"
+#include <NvClothExt/ClothFabricCooker.h>
+#include "ClothMeshGenerator.h"
+#include <NvCloth/Fabric.h>
+#include <NvCloth/Solver.h>
+#include <NvCloth/Cloth.h>
+#include <NvCloth/Factory.h>
+#include "Renderer.h"
+#include "renderer/RenderUtils.h"
+
+DECLARE_SCENE_NAME(FreeFallScene, "Free Fall Scene")
+
+void FreeFallScene::initializeCloth(int index, physx::PxVec3 offset)
+{
+ ///////////////////////////////////////////////////////////////////////
+ ClothMeshData clothMesh;
+
+ physx::PxMat44 transform = PxTransform(PxVec3(0.f, 13.f, 0.f)+ offset, PxQuat(PxPi, PxVec3(1.f, 0.f, 0.f)));
+ clothMesh.GeneratePlaneCloth(1.f * (1 << index), 1.f * (1 << index), int(2.0f*(1 << index)), int(2.0f*(1 << index)), false, transform);
+
+ mClothActor[index] = new ClothActor;
+ nv::cloth::ClothMeshDesc meshDesc = clothMesh.GetClothMeshDesc();
+ {
+ mClothActor[index]->mClothRenderMesh = new ClothRenderMesh(meshDesc);
+ mClothActor[index]->mClothRenderable = getSceneController()->getRenderer().createRenderable(*(static_cast<IRenderMesh*>(mClothActor[index]->mClothRenderMesh)), *getSceneController()->getDefaultMaterial());
+ mClothActor[index]->mClothRenderable->setColor(getRandomPastelColor());
+ }
+
+ nv::cloth::Vector<int32_t>::Type phaseTypeInfo;
+ mFabric[index] = NvClothCookFabricFromMesh(getSceneController()->getFactory(), meshDesc, physx::PxVec3(0.0f, 0.0f, 1.0f), &phaseTypeInfo, false);
+ trackFabric(mFabric[index]);
+
+ // Initialize start positions and masses for the actual cloth instance
+ // (note: the particle/vertex positions do not have to match the mesh description here. Set the positions to the initial shape of this cloth instance)
+ std::vector<physx::PxVec4> particlesCopy;
+ particlesCopy.resize(clothMesh.mVertices.size());
+
+ physx::PxVec3 clothOffset = transform.getPosition();
+ for(int i = 0; i < (int)clothMesh.mVertices.size(); i++)
+ {
+ // To put attachment point closer to each other
+ if(clothMesh.mInvMasses[i] < 1e-6)
+ clothMesh.mVertices[i] = (clothMesh.mVertices[i] - clothOffset)*0.9f + clothOffset;
+
+ particlesCopy[i] = physx::PxVec4(clothMesh.mVertices[i], clothMesh.mInvMasses[i]); // w component is 1/mass, or 0.0f for anchored/fixed particles
+ }
+
+ // Create the cloth from the initial positions/masses and the fabric
+ mClothActor[index]->mCloth = getSceneController()->getFactory()->createCloth(nv::cloth::Range<physx::PxVec4>(&particlesCopy[0], &particlesCopy[0] + particlesCopy.size()), *mFabric[index]);
+ particlesCopy.clear(); particlesCopy.shrink_to_fit();
+
+ std::vector<physx::PxVec4> planes;
+ planes.push_back(physx::PxVec4(physx::PxVec3(0.0f, 1.f, 0.0f), -0.01f));
+
+ nv::cloth::Range<const physx::PxVec4> planesR(&planes[0], &planes[0] + planes.size());
+ mClothActor[index]->mCloth->setPlanes(planesR, 0, mClothActor[index]->mCloth->getNumPlanes());
+ std::vector<uint32_t> indices;
+ indices.resize(planes.size());
+ for(int i = 0; i < (int)indices.size(); i++)
+ indices[i] = 1 << i;
+ nv::cloth::Range<uint32_t> cind(&indices[0], &indices[0] + indices.size());
+ mClothActor[index]->mCloth->setConvexes(cind, 0, mClothActor[index]->mCloth->getNumConvexes());
+
+ mClothActor[index]->mCloth->setGravity(physx::PxVec3(0.0f, -1.0f, 0.0f));
+ mClothActor[index]->mCloth->setFriction(0.1);
+ mClothActor[index]->mCloth->setDragCoefficient(0.5);
+ mClothActor[index]->mCloth->setLiftCoefficient(0.0);
+
+ // Setup phase configs
+ std::vector<nv::cloth::PhaseConfig> phases(mFabric[index]->getNumPhases());
+ for(int i = 0; i < (int)phases.size(); i++)
+ {
+ phases[i].mPhaseIndex = i;
+ phases[i].mStiffness = 1.0f;
+ phases[i].mStiffnessMultiplier = 1.0f;
+ phases[i].mCompressionLimit = 1.0f;
+ phases[i].mStretchLimit = 1.0f;
+ }
+ mClothActor[index]->mCloth->setPhaseConfig(nv::cloth::Range<nv::cloth::PhaseConfig>(&phases.front(), &phases.back()));
+
+ mSolver = getSceneController()->getFactory()->createSolver();
+ trackSolver(mSolver);
+ trackClothActor(mClothActor[index]);
+
+ // Add the cloth to the solver for simulation
+ addClothToSolver(mClothActor[index], mSolver);
+}
+
+void FreeFallScene::onInitialize()
+{
+ float spaceX = -1.5f;
+
+ for(int i = 0; i < 4; ++i)
+ {
+ initializeCloth(i, physx::PxVec3(16.f + float((i+1)*(i+1)) * spaceX, 2.f, -7.f));
+ }
+
+ {
+ IRenderMesh* mesh = getSceneController()->getRenderer().getPrimitiveRenderMesh(PrimitiveRenderMeshType::Plane);
+ Renderable* plane = getSceneController()->getRenderer().createRenderable(*mesh, *getSceneController()->getDefaultPlaneMaterial());
+ plane->setTransform(PxTransform(PxVec3(0.f, 0.f, 0.f), PxQuat(PxPiDivTwo, PxVec3(0.f, 0.f, 1.f))));
+ plane->setScale(PxVec3(1000.f));
+ trackRenderable(plane);
+ }
+}
diff --git a/NvCloth/samples/SampleBase/scene/scenes/FreeFallScene.h b/NvCloth/samples/SampleBase/scene/scenes/FreeFallScene.h
new file mode 100644
index 0000000..3cab456
--- /dev/null
+++ b/NvCloth/samples/SampleBase/scene/scenes/FreeFallScene.h
@@ -0,0 +1,33 @@
+/*
+* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
+*
+* NVIDIA CORPORATION and its licensors retain all intellectual property
+* and proprietary rights in and to this software, related documentation
+* and any modifications thereto. Any use, reproduction, disclosure or
+* distribution of this software and related documentation without an express
+* license agreement from NVIDIA CORPORATION is strictly prohibited.
+*/
+
+#ifndef FREE_FALL_SCENE_H
+#define FREE_FALL_SCENE_H
+
+#include "scene/Scene.h"
+#include <foundation/PxVec3.h>
+
+class FreeFallScene : public Scene
+{
+public:
+
+ FreeFallScene(SceneController* sceneController): Scene(sceneController) {}
+
+ void initializeCloth(int index, physx::PxVec3 offset);
+ virtual void onInitialize() override;
+
+private:
+ nv::cloth::Fabric* mFabric[4];
+ nv::cloth::Solver* mSolver;
+ ClothActor* mClothActor[4];
+};
+
+
+#endif \ No newline at end of file
diff --git a/NvCloth/samples/SampleBase/scene/scenes/FrictionScene.cpp b/NvCloth/samples/SampleBase/scene/scenes/FrictionScene.cpp
new file mode 100644
index 0000000..3c181ea
--- /dev/null
+++ b/NvCloth/samples/SampleBase/scene/scenes/FrictionScene.cpp
@@ -0,0 +1,119 @@
+/*
+* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
+*
+* NVIDIA CORPORATION and its licensors retain all intellectual property
+* and proprietary rights in and to this software, related documentation
+* and any modifications thereto. Any use, reproduction, disclosure or
+* distribution of this software and related documentation without an express
+* license agreement from NVIDIA CORPORATION is strictly prohibited.
+*/
+
+#include "FrictionScene.h"
+#include "Scene/SceneController.h"
+#include <NvClothExt/ClothFabricCooker.h>
+#include "ClothMeshGenerator.h"
+#include <NvCloth/Fabric.h>
+#include <NvCloth/Solver.h>
+#include <NvCloth/Cloth.h>
+#include <NvCloth/Factory.h>
+#include "Renderer.h"
+#include "renderer/RenderUtils.h"
+
+DECLARE_SCENE_NAME(FrictionScene, "Friction Scene")
+
+void FrictionScene::initializeCloth(int index, physx::PxVec3 offset, float frictionCoef)
+{
+ ///////////////////////////////////////////////////////////////////////
+ ClothMeshData clothMesh;
+
+ physx::PxMat44 transform = PxTransform(PxVec3(0.f, 13.f, 0.f)+ offset, PxQuat(PxPi / 6.f, PxVec3(1.f, 0.f, 0.f)));
+ clothMesh.GeneratePlaneCloth(3.f, 3.f, 29, 29, false, transform);
+
+ mClothActor[index] = new ClothActor;
+ nv::cloth::ClothMeshDesc meshDesc = clothMesh.GetClothMeshDesc();
+ {
+ mClothActor[index]->mClothRenderMesh = new ClothRenderMesh(meshDesc);
+ mClothActor[index]->mClothRenderable = getSceneController()->getRenderer().createRenderable(*(static_cast<IRenderMesh*>(mClothActor[index]->mClothRenderMesh)), *getSceneController()->getDefaultMaterial());
+ mClothActor[index]->mClothRenderable->setColor(getRandomPastelColor());
+ }
+
+ nv::cloth::Vector<int32_t>::Type phaseTypeInfo;
+ mFabric[index] = NvClothCookFabricFromMesh(getSceneController()->getFactory(), meshDesc, physx::PxVec3(0.0f, 0.0f, 1.0f), &phaseTypeInfo, false);
+ trackFabric(mFabric[index]);
+
+ // Initialize start positions and masses for the actual cloth instance
+ // (note: the particle/vertex positions do not have to match the mesh description here. Set the positions to the initial shape of this cloth instance)
+ std::vector<physx::PxVec4> particlesCopy;
+ particlesCopy.resize(clothMesh.mVertices.size());
+
+ physx::PxVec3 clothOffset = transform.getPosition();
+ for(int i = 0; i < (int)clothMesh.mVertices.size(); i++)
+ {
+ // To put attachment point closer to each other
+ if(clothMesh.mInvMasses[i] < 1e-6)
+ clothMesh.mVertices[i] = (clothMesh.mVertices[i] - clothOffset)*0.9f + clothOffset;
+
+ particlesCopy[i] = physx::PxVec4(clothMesh.mVertices[i], clothMesh.mInvMasses[i]); // w component is 1/mass, or 0.0f for anchored/fixed particles
+ }
+
+ // Create the cloth from the initial positions/masses and the fabric
+ mClothActor[index]->mCloth = getSceneController()->getFactory()->createCloth(nv::cloth::Range<physx::PxVec4>(&particlesCopy[0], &particlesCopy[0] + particlesCopy.size()), *mFabric[index]);
+ particlesCopy.clear(); particlesCopy.shrink_to_fit();
+
+ std::vector<physx::PxVec4> planes;
+ planes.push_back(physx::PxVec4(PxQuat(PxPiDivFour*0.5f, PxVec3(1.f, 0.f, 0.f)).rotate(physx::PxVec3(0.0f, 1.f, 0.0f)), -0.01f));
+
+ nv::cloth::Range<const physx::PxVec4> planesR(&planes[0], &planes[0] + planes.size());
+ mClothActor[index]->mCloth->setPlanes(planesR, 0, mClothActor[index]->mCloth->getNumPlanes());
+ std::vector<uint32_t> indices;
+ indices.resize(planes.size());
+ for(int i = 0; i < (int)indices.size(); i++)
+ indices[i] = 1 << i;
+ nv::cloth::Range<uint32_t> cind(&indices[0], &indices[0] + indices.size());
+ mClothActor[index]->mCloth->setConvexes(cind, 0, mClothActor[index]->mCloth->getNumConvexes());
+
+ mClothActor[index]->mCloth->setGravity(physx::PxVec3(0.0f, -9.8f, 0.0f));
+
+ // Setup phase configs
+ std::vector<nv::cloth::PhaseConfig> phases(mFabric[index]->getNumPhases());
+ for(int i = 0; i < (int)phases.size(); i++)
+ {
+ phases[i].mPhaseIndex = i;
+ phases[i].mStiffness = 1.0f;
+ phases[i].mStiffnessMultiplier = 1.0f;
+ phases[i].mCompressionLimit = 1.0f;
+ phases[i].mStretchLimit = 1.0f;
+ }
+ mClothActor[index]->mCloth->setPhaseConfig(nv::cloth::Range<nv::cloth::PhaseConfig>(&phases.front(), &phases.back()));
+ mClothActor[index]->mCloth->setFriction(frictionCoef);
+
+ mSolver = getSceneController()->getFactory()->createSolver();
+ trackSolver(mSolver);
+ trackClothActor(mClothActor[index]);
+
+ // Add the cloth to the solver for simulation
+ addClothToSolver(mClothActor[index], mSolver);
+}
+
+void FrictionScene::onInitialize()
+{
+
+
+ float spaceX = -4.f;
+ float frictionDelta = 0.2f;
+
+ for(int i = 0; i < 4; ++i)
+ {
+ float friction = i > 0 ? float(i) * frictionDelta : 0.f; // 0.0, 0.2, 0.4, 0.6
+
+ initializeCloth(i, physx::PxVec3(4.f + float(i) * spaceX, 4.f, -18.f), friction);
+ }
+
+ {
+ IRenderMesh* mesh = getSceneController()->getRenderer().getPrimitiveRenderMesh(PrimitiveRenderMeshType::Plane);
+ Renderable* plane = getSceneController()->getRenderer().createRenderable(*mesh, *getSceneController()->getDefaultPlaneMaterial());
+ plane->setTransform(PxTransform(PxVec3(0.f, 0.f, 0.f), PxQuat(PxPiDivFour*0.5f, PxVec3(1.f, 0.f, 0.f))*PxQuat(PxPiDivTwo, PxVec3(0.f, 0.f, 1.f))));
+ plane->setScale(PxVec3(1000.f));
+ trackRenderable(plane);
+ }
+}
diff --git a/NvCloth/samples/SampleBase/scene/scenes/FrictionScene.h b/NvCloth/samples/SampleBase/scene/scenes/FrictionScene.h
new file mode 100644
index 0000000..4173e09
--- /dev/null
+++ b/NvCloth/samples/SampleBase/scene/scenes/FrictionScene.h
@@ -0,0 +1,33 @@
+/*
+* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
+*
+* NVIDIA CORPORATION and its licensors retain all intellectual property
+* and proprietary rights in and to this software, related documentation
+* and any modifications thereto. Any use, reproduction, disclosure or
+* distribution of this software and related documentation without an express
+* license agreement from NVIDIA CORPORATION is strictly prohibited.
+*/
+
+#ifndef FRICTION_SCENE_H
+#define FRICTION_SCENE_H
+
+#include "scene/Scene.h"
+#include <foundation/PxVec3.h>
+
+class FrictionScene : public Scene
+{
+public:
+
+ FrictionScene(SceneController* sceneController): Scene(sceneController) {}
+
+ void initializeCloth(int index, physx::PxVec3 offset, float frictionCoef);
+ virtual void onInitialize() override;
+
+private:
+ nv::cloth::Fabric* mFabric[4];
+ nv::cloth::Solver* mSolver;
+ ClothActor* mClothActor[4];
+};
+
+
+#endif \ No newline at end of file
diff --git a/NvCloth/samples/SampleBase/scene/scenes/SimpleScene.cpp b/NvCloth/samples/SampleBase/scene/scenes/SimpleScene.cpp
new file mode 100644
index 0000000..68bb96b
--- /dev/null
+++ b/NvCloth/samples/SampleBase/scene/scenes/SimpleScene.cpp
@@ -0,0 +1,94 @@
+/*
+* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
+*
+* NVIDIA CORPORATION and its licensors retain all intellectual property
+* and proprietary rights in and to this software, related documentation
+* and any modifications thereto. Any use, reproduction, disclosure or
+* distribution of this software and related documentation without an express
+* license agreement from NVIDIA CORPORATION is strictly prohibited.
+*/
+
+#include "SimpleScene.h"
+#include "Scene/SceneController.h"
+#include <NvClothExt/ClothFabricCooker.h>
+#include "ClothMeshGenerator.h"
+#include <NvCloth/Fabric.h>
+#include <NvCloth/Solver.h>
+#include <NvCloth/Cloth.h>
+#include <NvCloth/Factory.h>
+#include "Renderer.h"
+#include "renderer/RenderUtils.h"
+
+DECLARE_SCENE_NAME(SimpleScene,"Simple Scene")
+
+void SimpleScene::onInitialize()
+{
+ ///////////////////////////////////////////////////////////////////////
+ ClothMeshData clothMesh;
+
+ physx::PxMat44 transform = PxTransform(PxVec3(0.f, 13.f, 0.f), PxQuat(PxPi / 6.f, PxVec3(1.f, 0.f, 0.f)));
+ clothMesh.GeneratePlaneCloth(6.f, 7.f, 59, 69, false, transform);
+ clothMesh.AttachClothPlaneByAngles(59, 69);
+
+ mClothActor = new ClothActor;
+ nv::cloth::ClothMeshDesc meshDesc = clothMesh.GetClothMeshDesc();
+ {
+ mClothActor->mClothRenderMesh = new ClothRenderMesh(meshDesc);
+ mClothActor->mClothRenderable = getSceneController()->getRenderer().createRenderable(*(static_cast<IRenderMesh*>(mClothActor->mClothRenderMesh)), *getSceneController()->getDefaultMaterial());
+ mClothActor->mClothRenderable->setColor(getRandomPastelColor());
+ }
+
+ nv::cloth::Vector<int32_t>::Type phaseTypeInfo;
+ mFabric = NvClothCookFabricFromMesh(getSceneController()->getFactory(), meshDesc, physx::PxVec3(0.0f, -9.8f, 0.0f), &phaseTypeInfo, false);
+ trackFabric(mFabric);
+
+ // Initialize start positions and masses for the actual cloth instance
+ // (note: the particle/vertex positions do not have to match the mesh description here. Set the positions to the initial shape of this cloth instance)
+ std::vector<physx::PxVec4> particlesCopy;
+ particlesCopy.resize(clothMesh.mVertices.size());
+
+ physx::PxVec3 center = transform.transform(physx::PxVec3(0.0f, 0.0f, 0.0f));
+ for(int i = 0; i < (int)clothMesh.mVertices.size(); i++)
+ {
+ // To put attachment point closer to each other
+ if(clothMesh.mInvMasses[i] < 1e-6)
+ clothMesh.mVertices[i] = (clothMesh.mVertices[i] - center)*0.85f + center;
+
+ particlesCopy[i] = physx::PxVec4(clothMesh.mVertices[i], clothMesh.mInvMasses[i]); // w component is 1/mass, or 0.0f for anchored/fixed particles
+ }
+
+ // Create the cloth from the initial positions/masses and the fabric
+ mClothActor->mCloth = getSceneController()->getFactory()->createCloth(nv::cloth::Range<physx::PxVec4>(&particlesCopy[0], &particlesCopy[0] + particlesCopy.size()), *mFabric);
+ particlesCopy.clear(); particlesCopy.shrink_to_fit();
+
+ mClothActor->mCloth->setGravity(physx::PxVec3(0.0f, -9.8f, 0.0f));
+
+ // Setup phase configs
+ std::vector<nv::cloth::PhaseConfig> phases(mFabric->getNumPhases());
+ for(int i = 0; i < (int)phases.size(); i++)
+ {
+ phases[i].mPhaseIndex = i;
+ phases[i].mStiffness = 1.0f;
+ phases[i].mStiffnessMultiplier = 1.0f;
+ phases[i].mCompressionLimit = 1.0f;
+ phases[i].mStretchLimit = 1.0f;
+ }
+ mClothActor->mCloth->setPhaseConfig(nv::cloth::Range<nv::cloth::PhaseConfig>(&phases.front(), &phases.back()));
+ mClothActor->mCloth->setDragCoefficient(0.5f);
+ mClothActor->mCloth->setDragCoefficient(0.5f);
+
+ mSolver = getSceneController()->getFactory()->createSolver();
+ trackSolver(mSolver);
+ trackClothActor(mClothActor);
+
+ // Add the cloth to the solver for simulation
+ addClothToSolver(mClothActor, mSolver);
+
+ {
+ IRenderMesh* mesh = getSceneController()->getRenderer().getPrimitiveRenderMesh(PrimitiveRenderMeshType::Plane);
+ Renderable* plane = getSceneController()->getRenderer().createRenderable(*mesh, *getSceneController()->getDefaultPlaneMaterial());
+ plane->setTransform(PxTransform(PxVec3(0.f, 0.f, 0.f), PxQuat(PxPiDivTwo, PxVec3(0.f, 0.f, 1.f))));
+ plane->setScale(PxVec3(1000.f));
+ trackRenderable(plane);
+ }
+}
diff --git a/NvCloth/samples/SampleBase/scene/scenes/SimpleScene.h b/NvCloth/samples/SampleBase/scene/scenes/SimpleScene.h
new file mode 100644
index 0000000..2ee17c1
--- /dev/null
+++ b/NvCloth/samples/SampleBase/scene/scenes/SimpleScene.h
@@ -0,0 +1,32 @@
+/*
+* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
+*
+* NVIDIA CORPORATION and its licensors retain all intellectual property
+* and proprietary rights in and to this software, related documentation
+* and any modifications thereto. Any use, reproduction, disclosure or
+* distribution of this software and related documentation without an express
+* license agreement from NVIDIA CORPORATION is strictly prohibited.
+*/
+
+#ifndef SIMPLE_SCENE_H
+#define SIMPLE_SCENE_H
+
+#include "scene/Scene.h"
+
+class SimpleScene : public Scene
+{
+public:
+
+ SimpleScene(SceneController* sceneController):Scene(sceneController) {}
+
+ virtual void onInitialize() override;
+
+private:
+ nv::cloth::Fabric* mFabric;
+ nv::cloth::Solver* mSolver;
+ ClothActor* mClothActor;
+
+};
+
+
+#endif \ No newline at end of file
diff --git a/NvCloth/samples/SampleBase/scene/scenes/TetherScene.cpp b/NvCloth/samples/SampleBase/scene/scenes/TetherScene.cpp
new file mode 100644
index 0000000..b818d9d
--- /dev/null
+++ b/NvCloth/samples/SampleBase/scene/scenes/TetherScene.cpp
@@ -0,0 +1,103 @@
+/*
+* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
+*
+* NVIDIA CORPORATION and its licensors retain all intellectual property
+* and proprietary rights in and to this software, related documentation
+* and any modifications thereto. Any use, reproduction, disclosure or
+* distribution of this software and related documentation without an express
+* license agreement from NVIDIA CORPORATION is strictly prohibited.
+*/
+
+#include "TetherScene.h"
+#include "Scene/SceneController.h"
+#include <NvClothExt/ClothFabricCooker.h>
+#include "ClothMeshGenerator.h"
+#include <NvCloth/Fabric.h>
+#include <NvCloth/Solver.h>
+#include <NvCloth/Cloth.h>
+#include <NvCloth/Factory.h>
+#include "Renderer.h"
+#include "renderer/RenderUtils.h"
+
+DECLARE_SCENE_NAME(TetherScene, "Tether Scene")
+
+void TetherScene::initializeCloth(int index, physx::PxVec3 offset, float tetherStiffness)
+{
+ ///////////////////////////////////////////////////////////////////////
+ ClothMeshData clothMesh;
+
+ physx::PxMat44 transform = PxTransform(PxVec3(0.f, 13.f, 0.f)+ offset, PxQuat(PxPi / 6.f, PxVec3(1.f, 0.f, 0.f)));
+ clothMesh.GeneratePlaneCloth(6.f, 7.5f, 39, 59, false, transform);
+ clothMesh.AttachClothPlaneByAngles(39, 59);
+
+ mClothActor[index] = new ClothActor;
+ nv::cloth::ClothMeshDesc meshDesc = clothMesh.GetClothMeshDesc();
+ {
+ mClothActor[index]->mClothRenderMesh = new ClothRenderMesh(meshDesc);
+ mClothActor[index]->mClothRenderable = getSceneController()->getRenderer().createRenderable(*(static_cast<IRenderMesh*>(mClothActor[index]->mClothRenderMesh)), *getSceneController()->getDefaultMaterial());
+ mClothActor[index]->mClothRenderable->setColor(getRandomPastelColor());
+ }
+
+ nv::cloth::Vector<int32_t>::Type phaseTypeInfo;
+ mFabric[index] = NvClothCookFabricFromMesh(getSceneController()->getFactory(), meshDesc, physx::PxVec3(0.0f, 0.0f, 1.0f), &phaseTypeInfo, false);
+ trackFabric(mFabric[index]);
+
+ // Initialize start positions and masses for the actual cloth instance
+ // (note: the particle/vertex positions do not have to match the mesh description here. Set the positions to the initial shape of this cloth instance)
+ std::vector<physx::PxVec4> particlesCopy;
+ particlesCopy.resize(clothMesh.mVertices.size());
+
+ physx::PxVec3 clothOffset = transform.getPosition();
+ for(int i = 0; i < (int)clothMesh.mVertices.size(); i++)
+ {
+ // To put attachment point closer to each other
+ if(clothMesh.mInvMasses[i] < 1e-6)
+ clothMesh.mVertices[i] = (clothMesh.mVertices[i] - clothOffset)*0.8f + clothOffset;
+
+ particlesCopy[i] = physx::PxVec4(clothMesh.mVertices[i], clothMesh.mInvMasses[i]); // w component is 1/mass, or 0.0f for anchored/fixed particles
+ }
+
+ // Create the cloth from the initial positions/masses and the fabric
+ mClothActor[index]->mCloth = getSceneController()->getFactory()->createCloth(nv::cloth::Range<physx::PxVec4>(&particlesCopy[0], &particlesCopy[0] + particlesCopy.size()), *mFabric[index]);
+ particlesCopy.clear(); particlesCopy.shrink_to_fit();
+
+ mClothActor[index]->mCloth->setGravity(physx::PxVec3(0.0f, -9.8f, 0.0f));
+ mClothActor[index]->mCloth->setTetherConstraintStiffness(tetherStiffness);
+
+ // Setup phase configs
+ std::vector<nv::cloth::PhaseConfig> phases(mFabric[index]->getNumPhases());
+ for(int i = 0; i < (int)phases.size(); i++)
+ {
+ phases[i].mPhaseIndex = i;
+ phases[i].mStiffness = 1.0f;
+ phases[i].mStiffnessMultiplier = 1.0f;
+ phases[i].mCompressionLimit = 1.0f;
+ phases[i].mStretchLimit = 1.0f;
+ }
+ mClothActor[index]->mCloth->setPhaseConfig(nv::cloth::Range<nv::cloth::PhaseConfig>(&phases.front(), &phases.back()));
+
+
+ mSolver = getSceneController()->getFactory()->createSolver();
+ trackSolver(mSolver);
+ trackClothActor(mClothActor[index]);
+
+ // Add the cloth to the solver for simulation
+ addClothToSolver(mClothActor[index], mSolver);
+}
+
+void TetherScene::onInitialize()
+{
+
+ initializeCloth(0,physx::PxVec3(-5.0f,0.0f,0.0f),0);
+ initializeCloth(1, physx::PxVec3(4.0f, 0.0f, 0.0f),1);
+
+ mTime = 0.0f;
+
+ {
+ IRenderMesh* mesh = getSceneController()->getRenderer().getPrimitiveRenderMesh(PrimitiveRenderMeshType::Plane);
+ Renderable* plane = getSceneController()->getRenderer().createRenderable(*mesh, *getSceneController()->getDefaultPlaneMaterial());
+ plane->setTransform(PxTransform(PxVec3(0.f, 0.f, 0.f), PxQuat(PxPiDivTwo, PxVec3(0.f, 0.f, 1.f))));
+ plane->setScale(PxVec3(1000.f));
+ trackRenderable(plane);
+ }
+}
diff --git a/NvCloth/samples/SampleBase/scene/scenes/TetherScene.h b/NvCloth/samples/SampleBase/scene/scenes/TetherScene.h
new file mode 100644
index 0000000..728286b
--- /dev/null
+++ b/NvCloth/samples/SampleBase/scene/scenes/TetherScene.h
@@ -0,0 +1,36 @@
+/*
+* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
+*
+* NVIDIA CORPORATION and its licensors retain all intellectual property
+* and proprietary rights in and to this software, related documentation
+* and any modifications thereto. Any use, reproduction, disclosure or
+* distribution of this software and related documentation without an express
+* license agreement from NVIDIA CORPORATION is strictly prohibited.
+*/
+
+#ifndef TETHER_SCENE_H
+#define TETHER_SCENE_H
+
+#include "scene/Scene.h"
+#include <foundation/PxVec3.h>
+
+class TetherScene : public Scene
+{
+public:
+
+ TetherScene(SceneController* sceneController): Scene(sceneController) {}
+
+ void initializeCloth(int index, physx::PxVec3 offset, float tetherStiffness);
+ virtual void onInitialize() override;
+
+private:
+ nv::cloth::Fabric* mFabric[2];
+ nv::cloth::Solver* mSolver;
+ ClothActor* mClothActor[2];
+
+ float mTime;
+
+};
+
+
+#endif \ No newline at end of file
diff --git a/NvCloth/samples/SampleBase/scene/scenes/WindScene.cpp b/NvCloth/samples/SampleBase/scene/scenes/WindScene.cpp
new file mode 100644
index 0000000..51cec1d
--- /dev/null
+++ b/NvCloth/samples/SampleBase/scene/scenes/WindScene.cpp
@@ -0,0 +1,124 @@
+/*
+* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
+*
+* NVIDIA CORPORATION and its licensors retain all intellectual property
+* and proprietary rights in and to this software, related documentation
+* and any modifications thereto. Any use, reproduction, disclosure or
+* distribution of this software and related documentation without an express
+* license agreement from NVIDIA CORPORATION is strictly prohibited.
+*/
+
+#include "WindScene.h"
+#include "Scene/SceneController.h"
+#include <NvClothExt/ClothFabricCooker.h>
+#include "ClothMeshGenerator.h"
+#include <NvCloth/Fabric.h>
+#include <NvCloth/Solver.h>
+#include <NvCloth/Cloth.h>
+#include <NvCloth/Factory.h>
+#include "Renderer.h"
+#include "renderer/RenderUtils.h"
+
+DECLARE_SCENE_NAME(WindScene, "Wind Scene")
+
+void WindScene::Animate(double dt)
+{
+ mTime += dt;
+
+ if(mTime > 3.7f)
+ {
+ float dvx = 3.f * cos(25.f * mTime);
+ float vy = max(0.f, 0.9f * cos(11.f * mTime));
+ float dvz = 1.5f * sin(19.f * mTime);
+ for(int i = 0; i < 3; i++)
+ {
+ physx::PxVec3 wind = physx::PxVec3(2.5f + dvx, vy, 15.f + dvz);
+ mClothActor[i]->mCloth->setWindVelocity(wind);
+ }
+ }
+
+ doSimulationStep(dt);
+}
+
+void WindScene::initializeCloth(int index, physx::PxVec3 offset)
+{
+ ///////////////////////////////////////////////////////////////////////
+ ClothMeshData clothMesh;
+
+ physx::PxMat44 transform = PxTransform(PxVec3(0.f, 13.f, 0.f)+ offset, PxQuat(PxPi / 6.f, PxVec3(1.f, 0.f, 0.f)));
+ clothMesh.GeneratePlaneCloth(5.f, 6.f, 49, 59, false, transform);
+ clothMesh.AttachClothPlaneByAngles(49, 59);
+ clothMesh.SetInvMasses(0.2f + (float)index * 1.4f);
+
+ mClothActor[index] = new ClothActor;
+ nv::cloth::ClothMeshDesc meshDesc = clothMesh.GetClothMeshDesc();
+ {
+ mClothActor[index]->mClothRenderMesh = new ClothRenderMesh(meshDesc);
+ mClothActor[index]->mClothRenderable = getSceneController()->getRenderer().createRenderable(*(static_cast<IRenderMesh*>(mClothActor[index]->mClothRenderMesh)), *getSceneController()->getDefaultMaterial());
+ mClothActor[index]->mClothRenderable->setColor(getRandomPastelColor());
+ }
+
+ nv::cloth::Vector<int32_t>::Type phaseTypeInfo;
+ mFabric[index] = NvClothCookFabricFromMesh(getSceneController()->getFactory(), meshDesc, physx::PxVec3(0.0f, 0.0f, 1.0f), &phaseTypeInfo, false);
+ trackFabric(mFabric[index]);
+
+ // Initialize start positions and masses for the actual cloth instance
+ // (note: the particle/vertex positions do not have to match the mesh description here. Set the positions to the initial shape of this cloth instance)
+ std::vector<physx::PxVec4> particlesCopy;
+ particlesCopy.resize(clothMesh.mVertices.size());
+
+ physx::PxVec3 clothOffset = transform.getPosition();
+ for(int i = 0; i < (int)clothMesh.mVertices.size(); i++)
+ {
+ // To put attachment point closer to each other
+ if(clothMesh.mInvMasses[i] < 1e-6)
+ clothMesh.mVertices[i] = (clothMesh.mVertices[i] - clothOffset)*0.9f + clothOffset;
+
+ particlesCopy[i] = physx::PxVec4(clothMesh.mVertices[i], clothMesh.mInvMasses[i]); // w component is 1/mass, or 0.0f for anchored/fixed particles
+ }
+
+ // Create the cloth from the initial positions/masses and the fabric
+ mClothActor[index]->mCloth = getSceneController()->getFactory()->createCloth(nv::cloth::Range<physx::PxVec4>(&particlesCopy[0], &particlesCopy[0] + particlesCopy.size()), *mFabric[index]);
+ particlesCopy.clear(); particlesCopy.shrink_to_fit();
+
+ mClothActor[index]->mCloth->setGravity(physx::PxVec3(0.0f, -9.8f, 0.0f));
+
+ // Setup phase configs
+ std::vector<nv::cloth::PhaseConfig> phases(mFabric[index]->getNumPhases());
+ for(int i = 0; i < (int)phases.size(); i++)
+ {
+ phases[i].mPhaseIndex = i;
+ phases[i].mStiffness = 1.0f;
+ phases[i].mStiffnessMultiplier = 1.0f;
+ phases[i].mCompressionLimit = 1.0f;
+ phases[i].mStretchLimit = 1.0f;
+ }
+ mClothActor[index]->mCloth->setPhaseConfig(nv::cloth::Range<nv::cloth::PhaseConfig>(&phases.front(), &phases.back()));
+ mClothActor[index]->mCloth->setDragCoefficient(0.5f);
+ mClothActor[index]->mCloth->setLiftCoefficient(0.6f);
+
+ mSolver = getSceneController()->getFactory()->createSolver();
+ trackSolver(mSolver);
+ trackClothActor(mClothActor[index]);
+
+ // Add the cloth to the solver for simulation
+ addClothToSolver(mClothActor[index], mSolver);
+}
+
+void WindScene::onInitialize()
+{
+
+ initializeCloth(2,physx::PxVec3(-9.0f,0.0f,0.0f));
+ initializeCloth(1, physx::PxVec3(-2.0f, 0.0f, 0.0f));
+ initializeCloth(0, physx::PxVec3(5.0f, 0.0f, 0.0f));
+
+ mTime = 0.0f;
+
+ {
+ IRenderMesh* mesh = getSceneController()->getRenderer().getPrimitiveRenderMesh(PrimitiveRenderMeshType::Plane);
+ Renderable* plane = getSceneController()->getRenderer().createRenderable(*mesh, *getSceneController()->getDefaultPlaneMaterial());
+ plane->setTransform(PxTransform(PxVec3(0.f, 0.f, 0.f), PxQuat(PxPiDivTwo, PxVec3(0.f, 0.f, 1.f))));
+ plane->setScale(PxVec3(1000.f));
+ trackRenderable(plane);
+ }
+}
diff --git a/NvCloth/samples/SampleBase/scene/scenes/WindScene.h b/NvCloth/samples/SampleBase/scene/scenes/WindScene.h
new file mode 100644
index 0000000..d637bed
--- /dev/null
+++ b/NvCloth/samples/SampleBase/scene/scenes/WindScene.h
@@ -0,0 +1,37 @@
+/*
+* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
+*
+* NVIDIA CORPORATION and its licensors retain all intellectual property
+* and proprietary rights in and to this software, related documentation
+* and any modifications thereto. Any use, reproduction, disclosure or
+* distribution of this software and related documentation without an express
+* license agreement from NVIDIA CORPORATION is strictly prohibited.
+*/
+
+#ifndef WIND_SCENE_H
+#define WIND_SCENE_H
+
+#include "scene/Scene.h"
+#include <foundation/PxVec3.h>
+
+class WindScene : public Scene
+{
+public:
+
+ WindScene(SceneController* sceneController): Scene(sceneController) {}
+
+ virtual void Animate(double dt) override;
+ void initializeCloth(int index, physx::PxVec3 offset);
+ virtual void onInitialize() override;
+
+private:
+ nv::cloth::Fabric* mFabric[3];
+ nv::cloth::Solver* mSolver;
+ ClothActor* mClothActor[3];
+
+ float mTime;
+
+};
+
+
+#endif \ No newline at end of file
diff --git a/NvCloth/samples/SampleBase/ui/CommonUIController.cpp b/NvCloth/samples/SampleBase/ui/CommonUIController.cpp
new file mode 100644
index 0000000..0c0cac8
--- /dev/null
+++ b/NvCloth/samples/SampleBase/ui/CommonUIController.cpp
@@ -0,0 +1,467 @@
+/*
+* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
+*
+* NVIDIA CORPORATION and its licensors retain all intellectual property
+* and proprietary rights in and to this software, related documentation
+* and any modifications thereto. Any use, reproduction, disclosure or
+* distribution of this software and related documentation without an express
+* license agreement from NVIDIA CORPORATION is strictly prohibited.
+*/
+
+#include "CommonUIController.h"
+
+#include "Renderer.h"
+#include "SceneController.h"
+#include "SampleController.h"
+#include "SampleProfiler.h"
+
+#include <imgui.h>
+#include "imgui_impl_dx11.h"
+#include "UIHelpers.h"
+
+#include <cstdio>
+#include <inttypes.h>
+
+
+inline float memorySizeOutput(const char*& prefix, float value)
+{
+ for (prefix = "\0\0k\0M\0G\0T\0P\0E"; value >= 1024 && *prefix != 'E'; value /= 1024, prefix += 2);
+ return value;
+}
+
+CommonUIController::CommonUIController()
+{
+}
+
+HRESULT CommonUIController::DeviceCreated(ID3D11Device* pDevice)
+{
+ DeviceManager* manager = GetDeviceManager();
+ ID3D11DeviceContext* pd3dDeviceContext;
+ pDevice->GetImmediateContext(&pd3dDeviceContext);
+ ImGui_ImplDX11_Init(manager->GetHWND(), pDevice, pd3dDeviceContext);
+
+ ImGuiStyle& style = ImGui::GetStyle();
+ style.WindowRounding = 8.0f;
+ style.ScrollbarRounding = 8.0f;
+ style.FrameRounding = 8.0f;
+ //style.IndentSpacing = 20;
+ int mainColor[3] = { 110, 110, 110 }; // previous green one { 50, 110, 30 }
+ style.Colors[ImGuiCol_TitleBg] = ImColor(mainColor[0], mainColor[1], mainColor[2], 62);
+ style.Colors[ImGuiCol_TitleBgCollapsed] = ImColor(mainColor[0], mainColor[1], mainColor[2], 52);
+ style.Colors[ImGuiCol_TitleBgActive] = ImColor(mainColor[0], mainColor[1], mainColor[2], 87);
+ style.Colors[ImGuiCol_Header] = ImColor(mainColor[0], mainColor[1], mainColor[2], 52);
+ style.Colors[ImGuiCol_HeaderHovered] = ImColor(mainColor[0], mainColor[1], mainColor[2], 92);
+ style.Colors[ImGuiCol_HeaderActive] = ImColor(mainColor[0], mainColor[1], mainColor[2], 72);
+ style.Colors[ImGuiCol_ScrollbarBg] = ImColor(mainColor[0], mainColor[1], mainColor[2], 12);
+ style.Colors[ImGuiCol_ScrollbarGrab] = ImColor(mainColor[0], mainColor[1], mainColor[2], 52);
+ style.Colors[ImGuiCol_ScrollbarGrabHovered] = ImColor(mainColor[0], mainColor[1], mainColor[2], 92);
+ style.Colors[ImGuiCol_ScrollbarGrabActive] = ImColor(mainColor[0], mainColor[1], mainColor[2], 72);
+ style.Colors[ImGuiCol_Button] = ImColor(40, 100, 80, 30);
+ style.Colors[ImGuiCol_ButtonHovered] = ImColor(40, 100, 80, 100);
+ style.Colors[ImGuiCol_ButtonActive] = ImColor(40, 100, 80, 70);
+ style.Colors[ImGuiCol_PopupBg] = ImColor(10, 23, 18, 230);
+ style.Colors[ImGuiCol_TextSelectedBg] = ImColor(10, 23, 18, 180);
+ style.Colors[ImGuiCol_FrameBg] = ImColor(70, 70, 70, 30);
+ style.Colors[ImGuiCol_FrameBgHovered] = ImColor(70, 70, 70, 70);
+ style.Colors[ImGuiCol_FrameBgActive] = ImColor(70, 70, 70, 50);
+ style.Colors[ImGuiCol_ComboBg] = ImColor(20, 20, 20, 252);
+
+ return S_OK;
+}
+
+void CommonUIController::DeviceDestroyed()
+{
+ ImGui_ImplDX11_Shutdown();
+}
+
+extern LRESULT ImGui_ImplDX11_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
+
+LRESULT CommonUIController::MsgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
+{
+ PX_UNUSED(hWnd);
+ PX_UNUSED(wParam);
+ PX_UNUSED(lParam);
+
+ ImGui_ImplDX11_WndProcHandler(hWnd, uMsg, wParam, lParam);
+
+ if (uMsg == WM_KEYDOWN && !ImGui::GetIO().WantCaptureKeyboard)
+ {
+ int iKeyPressed = static_cast<int>(wParam);
+ switch (iKeyPressed)
+ {
+ case 'P':
+ {
+ //getPhysXController().setPaused(!getPhysXController().isPaused());
+ break;
+ }
+ case 'O':
+ {
+ getRenderer().setWireframeMode(!getRenderer().getWireframeMode());
+ break;
+ }
+ case 'I':
+ {
+ //getBlastController().debugRenderMode = (BlastFamily::DebugRenderMode)(((int)getBlastController().debugRenderMode + 1) % BlastFamily::DebugRenderMode::DEBUG_RENDER_MODES_COUNT);
+ break;
+ }
+ case VK_F5:
+ {
+ getRenderer().reloadShaders();
+ break;
+ }
+ default:
+ break;
+ }
+ }
+
+ if (ImGui::GetIO().WantCaptureMouse)
+ return 0;
+
+ return 1;
+}
+
+void CommonUIController::Animate(double fElapsedTimeSeconds)
+{
+ m_dt = (float)fElapsedTimeSeconds;
+}
+
+void CommonUIController::Render(ID3D11Device*, ID3D11DeviceContext*, ID3D11RenderTargetView*, ID3D11DepthStencilView*)
+{
+ ImGui_ImplDX11_NewFrame();
+ drawUI();
+ ImGui::Render();
+}
+
+void CommonUIController::addDelayedCall(const char* title, const char* message, std::function<void()> func, float delay)
+{
+ DelayedCall call = { func, title, message, delay, delay };
+ m_delayedCalls.emplace(call);
+}
+
+///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+// IMGUI UI
+///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+
+
+void CommonUIController::drawUI()
+{
+ const float padding = 8.0f;
+ ImGui::SetNextWindowPos(ImVec2(padding, padding), ImGuiSetCond_Once/*ImGuiSetCond_FirstUseEver*/);
+ ImGui::SetNextWindowSize(ImVec2(420, getRenderer().getScreenHeight() - 2 * padding), ImGuiSetCond_Once/*ImGuiSetCond_FirstUseEver*/);
+ ImGui::SetNextWindowCollapsed(false, ImGuiSetCond_Once);
+ ImGui::Begin("New Shiny UI", 0, ImGuiWindowFlags_NoTitleBar);
+ {
+ ImGui::PushItemWidth(ImGui::GetWindowSize().x * 0.5f);
+
+ ///////////////////////////////////////////////////////////////////////////////////////////
+ // Scene
+ ///////////////////////////////////////////////////////////////////////////////////////////
+ if (ImGui::CollapsingHeader("Scene"))
+ {
+ getSceneController().drawUI();
+ }
+
+
+ ///////////////////////////////////////////////////////////////////////////////////////////
+ // Stats
+ ///////////////////////////////////////////////////////////////////////////////////////////
+ if (ImGui::CollapsingHeader("Stats"))
+ {
+ // FPS
+ double averageTime = GetDeviceManager()->GetAverageFrameTime();
+ float fps = (averageTime > 0) ? 1.0 / averageTime : 0.0;
+ float frameMs = 1000.0f / fps;
+ ImGui::Text("Frame Time %.3f ms (%.1f FPS)", frameMs, fps);
+
+ static PlotLinesInstance<> fpsPlot;
+ fpsPlot.plot("FPS", frameMs, "ms/frame", 0.0f, 100.0f);
+
+ // Render stats
+ ImGui::PushStyleColor(ImGuiCol_Text, ImColor(0xFF, 0x3B, 0xD8, 0xFF));
+ ImGui::Text("Draw Calls (Opaque/Transparent): %d/%d", getRenderer().getVisibleOpaqueRenderablesCount(), getRenderer().getVisibleTransparentRenderablesCount());
+ ImGui::PopStyleColor();
+
+#if NV_PROFILE
+ // Sample Profiler
+ static bool s_showProfilerWindow = false;
+ if (ImGui::Button("Code Profiler"))
+ {
+ s_showProfilerWindow = !s_showProfilerWindow;
+ }
+ if (s_showProfilerWindow)
+ {
+ drawCodeProfiler(&s_showProfilerWindow);
+ }
+#endif
+ }
+
+ ///////////////////////////////////////////////////////////////////////////////////////////
+ // Application
+ ///////////////////////////////////////////////////////////////////////////////////////////
+ if (ImGui::CollapsingHeader("Application"))
+ {
+ // Reload Shaders
+ if (ImGui::Button("Reload Shaders (F5)"))
+ {
+ getRenderer().reloadShaders();
+ }
+
+ /*// ImGui Test Window (just in case)
+ static bool s_showTestWindow = false;
+ if (ImGui::Button("ImGui Test Window"))
+ {
+ s_showTestWindow = !s_showTestWindow;
+ }
+ if (s_showTestWindow)
+ {
+ ImGui::ShowTestWindow();
+ }*/
+ }
+
+
+ ///////////////////////////////////////////////////////////////////////////////////////////
+ // Debug Render
+ ///////////////////////////////////////////////////////////////////////////////////////////
+ if (ImGui::CollapsingHeader("Debug Render"))
+ {
+ // WireFrame
+ bool wireFrameEnabled = getRenderer().getWireframeMode();
+ if (ImGui::Checkbox("WireFrame (O)", &wireFrameEnabled))
+ {
+ getRenderer().setWireframeMode(wireFrameEnabled);
+ }
+
+ // - - - - - - - -
+ ImGui::Spacing();
+ }
+
+
+ ///////////////////////////////////////////////////////////////////////////////////////////
+ // Renderer
+ ///////////////////////////////////////////////////////////////////////////////////////////
+ if (ImGui::CollapsingHeader("Renderer"))
+ {
+ getRenderer().drawUI();
+ }
+
+
+ ///////////////////////////////////////////////////////////////////////////////////////////
+ // Hints
+ ///////////////////////////////////////////////////////////////////////////////////////////
+ if (ImGui::CollapsingHeader("Hints / Help"))
+ {
+ ImGui::BulletText("Rotate camera - RMB");
+ ImGui::BulletText("Move camera - WASDQE(SHIFT)");
+ //ImGui::BulletText("Play/Pause - P");
+ ImGui::BulletText("Reload shaders - F5");
+ ImGui::BulletText("Wireframe - O");
+ //ImGui::BulletText("Restart - R");
+ }
+
+ ImGui::PopItemWidth();
+ }
+ ImGui::End();
+
+ ///////////////////////////////////////////////////////////////////////////////////////////
+ // Mode Text
+ ///////////////////////////////////////////////////////////////////////////////////////////
+ /*{
+ ImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4());
+ ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0,0));
+
+ const char* text = "CENTERED TEXT, REMOVE ME";
+ ImVec2 size = ImGui::CalcTextSize(text);
+ ImGui::SetNextWindowPos(ImVec2((getRenderer().getScreenWidth() - size.x) / 2, 0));
+ ImGui::Begin("Mode Text", 0, ImGuiWindowFlags_NoInputs | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoTitleBar);
+ ImGui::Text(text);
+ ImGui::End();
+
+ ImGui::PopStyleVar();
+ ImGui::PopStyleColor();
+ }*/
+
+ ///////////////////////////////////////////////////////////////////////////////////////////
+ // FPS
+ ///////////////////////////////////////////////////////////////////////////////////////////
+ {
+ ImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4());
+ ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0, 0));
+
+ double averageTime = GetDeviceManager()->GetAverageFrameTime();
+ float fps = (averageTime > 0) ? 1.0 / averageTime : 0.0;
+ static char buf[32];
+ std::sprintf(buf, "%.1f FPS", fps);
+ ImVec2 size = ImGui::CalcTextSize(buf);
+
+ size.x += 20.0;
+ ImGui::SetNextWindowSize(size);
+ ImGui::SetNextWindowPos(ImVec2(getRenderer().getScreenWidth() - size.x, 0));
+ ImGui::Begin("FPS", 0, ImGuiWindowFlags_NoInputs | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoTitleBar);
+ ImGui::Text(buf);
+ ImGui::End();
+
+ ImGui::PopStyleVar();
+ ImGui::PopStyleColor();
+ }
+
+ ///////////////////////////////////////////////////////////////////////////////////////////
+ // Loading overlay
+ ///////////////////////////////////////////////////////////////////////////////////////////
+ if (!m_delayedCalls.empty())
+ {
+ DelayedCall& call = m_delayedCalls.front();
+ if (call.delay > 0)
+ {
+ const int height = 50;
+ const char* message = call.message;
+ const float alpha = PxClamp(lerp(0.0f, 1.0f, (call.delayTotal - call.delay) * 10.0f), 0.0f, 1.0f);
+
+ ImGui::PushStyleColor(ImGuiCol_WindowBg, ImColor(0, 0, 0, 200));
+ ImGui::PushStyleVar(ImGuiStyleVar_Alpha, alpha);
+ ImGui::SetNextWindowPosCenter();
+ ImVec2 size = ImGui::CalcTextSize(message);
+ int width = std::max<float>(200, size.x) + 50;
+ ImGui::SetNextWindowSize(ImVec2(width, height));
+ ImGui::Begin(call.title, 0, ImGuiWindowFlags_NoInputs | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoTitleBar);
+ ImGui::SetCursorPos(ImVec2((width - size.x) * 0.5f, (height - size.y) * 0.5f));
+ ImGui::Text(message);
+ ImGui::End();
+ ImGui::PopStyleVar();
+ ImGui::PopStyleColor();
+
+ call.delay -= PxClamp(m_dt, 0.0f, 0.1f);
+ }
+ else
+ {
+ call.func();
+ m_delayedCalls.pop();
+ }
+ }
+}
+
+
+void CommonUIController::drawCodeProfiler(bool* open)
+{
+ ImGuiWindowFlags window_flags = 0;
+ const float padding = 8.0f;
+ const float width = 550;
+ const float height = 580;
+ ImGui::SetNextWindowPos(ImVec2(getRenderer().getScreenWidth() - width - padding, padding), ImGuiSetCond_Once/*ImGuiSetCond_FirstUseEver*/);
+ ImGui::SetNextWindowSize(ImVec2(width, height), ImGuiSetCond_Once);
+ if (!ImGui::Begin("Code Profiler", open, window_flags))
+ {
+ // Early out if the window is collapsed, as an optimization.
+ ImGui::End();
+ return;
+ }
+
+ ///////////////////////////////////////////////////////////////////////////////////////////
+ // Control/Main Bar
+ ///////////////////////////////////////////////////////////////////////////////////////////
+ {
+ if (ImGui::Button("Reset"))
+ {
+ PROFILER_INIT();
+ }
+ ImGui::SameLine();
+ if (ImGui::Button("Dump To File (profile.txt)"))
+ {
+ SampleProfilerDumpToFile("profile.txt");
+ }
+ ImGui::SameLine();
+ ImGui::Text("Profiler overhead: %2.3f ms", SampleProfilerGetOverhead().count() * 0.001f);
+ }
+
+ ///////////////////////////////////////////////////////////////////////////////////////////
+ // Legend
+ ///////////////////////////////////////////////////////////////////////////////////////////
+ {
+ ImGui::Spacing(); ImGui::Separator(); ImGui::Spacing();
+ ImGui::Text("Legend: name | calls | time | max time");
+ ImGui::Spacing(); ImGui::Separator(); ImGui::Spacing();
+ }
+
+ ///////////////////////////////////////////////////////////////////////////////////////////
+ // Stats Tree
+ ///////////////////////////////////////////////////////////////////////////////////////////
+ ImGui::SetNextTreeNodeOpen(true, ImGuiSetCond_Once);
+ float plotMS = 0.0f;
+ float plotMaxMS = 0.0f;
+ const char* plotName = nullptr;
+ if (ImGui::TreeNode("Root"))
+ {
+ auto treeIt = SampleProfilerCreateTreeIterator();
+ if (treeIt)
+ {
+ uint32_t depth = 1;
+ uint32_t openeDepth = 1;
+ while (!treeIt->isDone())
+ {
+ const auto data = treeIt->data();
+
+ while (data->depth < depth)
+ {
+ ImGui::TreePop();
+ depth--;
+ }
+
+ const uint32_t maxLen = 30;
+ auto hash = data->hash;
+ static uint64_t selectedNodeHash = 0;
+ if (selectedNodeHash == hash)
+ {
+ plotMS = data->time.count() * 0.001f;
+ plotMaxMS = data->maxTime.count() * 0.001f;
+ plotName = data->name;
+ }
+ if (ImGui::TreeNodeEx(data->name, data->hasChilds ? 0 : ImGuiTreeNodeFlags_Leaf, "%-*.*s | %d | %2.3f ms | %2.3f ms",
+ maxLen, maxLen, data->name, data->calls, data->time.count() * 0.001f, data->maxTime.count() * 0.001f))
+ {
+ depth++;
+ treeIt->next();
+ }
+ else
+ {
+ treeIt->next();
+ while (!treeIt->isDone() && treeIt->data()->depth > depth)
+ treeIt->next();
+ }
+
+ if (ImGui::IsItemClicked())
+ {
+ selectedNodeHash = hash;
+ }
+ }
+
+ while (depth > 0)
+ {
+ ImGui::TreePop();
+ depth--;
+ }
+
+ treeIt->release();
+ }
+ else
+ {
+ ImGui::Text("Profiler Is Broken. Begin/End Mismatch.");
+ }
+ }
+
+ ///////////////////////////////////////////////////////////////////////////////////////////
+ // Selected Item Plot
+ ///////////////////////////////////////////////////////////////////////////////////////////
+ {
+ ImGui::Spacing(); ImGui::Separator(); ImGui::Spacing();
+ if (plotName)
+ {
+ static PlotLinesInstance<> selectedNodePlot;
+ selectedNodePlot.plot("", plotMS, plotName, 0.0f, plotMaxMS);
+ }
+ else
+ {
+ ImGui::Text("Select item to plot.");
+ }
+ }
+
+ ImGui::End();
+} \ No newline at end of file
diff --git a/NvCloth/samples/SampleBase/ui/CommonUIController.h b/NvCloth/samples/SampleBase/ui/CommonUIController.h
new file mode 100644
index 0000000..47953b7
--- /dev/null
+++ b/NvCloth/samples/SampleBase/ui/CommonUIController.h
@@ -0,0 +1,91 @@
+/*
+* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
+*
+* NVIDIA CORPORATION and its licensors retain all intellectual property
+* and proprietary rights in and to this software, related documentation
+* and any modifications thereto. Any use, reproduction, disclosure or
+* distribution of this software and related documentation without an express
+* license agreement from NVIDIA CORPORATION is strictly prohibited.
+*/
+
+#ifndef COMMON_UI_CONTROLLER_H
+#define COMMON_UI_CONTROLLER_H
+
+#include "SampleManager.h"
+#include <DirectXMath.h>
+#include <string>
+#include <list>
+#include <queue>
+#include <functional>
+
+
+class Renderer;
+class PhysXController;
+class BlastController;
+
+
+class CommonUIController : public ISampleController
+{
+ public:
+ CommonUIController();
+ virtual ~CommonUIController() {};
+
+ virtual HRESULT DeviceCreated(ID3D11Device* pDevice);
+ virtual void DeviceDestroyed();
+ virtual LRESULT MsgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
+ virtual void Animate(double fElapsedTimeSeconds);
+ virtual void Render(ID3D11Device*, ID3D11DeviceContext*, ID3D11RenderTargetView*, ID3D11DepthStencilView*);
+
+ void addDelayedCall(std::function<void()> func, const char* message)
+ {
+ addDelayedCall("PLEASE WAIT...", message, func);
+ }
+
+ void addPopupMessage(const char* title, const char* message, float duration = 2.f)
+ {
+ addDelayedCall(title, message, [] {}, duration);
+ }
+
+ private:
+ void addDelayedCall(const char* title, const char* message, std::function<void()> func, float delay = 0.1f);
+
+ void drawUI();
+ void drawCodeProfiler(bool*);
+
+
+ //////// used controllers ////////
+
+ Renderer& getRenderer() const
+ {
+ return getManager()->getRenderer();
+ }
+
+ SceneController& getSceneController() const
+ {
+ return getManager()->getSceneController();
+ }
+
+ SampleController& getSampleController() const
+ {
+ return getManager()->getSampleController();
+ }
+
+
+ //////// internal data ////////
+
+ struct DelayedCall
+ {
+ std::function<void()> func;
+ const char* title;
+ const char* message;
+ float delay;
+ float delayTotal;
+ };
+
+ std::queue<DelayedCall> m_delayedCalls;
+
+ float m_dt;
+
+};
+
+#endif \ No newline at end of file
diff --git a/NvCloth/samples/SampleBase/ui/imgui_impl_dx11.cpp b/NvCloth/samples/SampleBase/ui/imgui_impl_dx11.cpp
new file mode 100644
index 0000000..11f66f0
--- /dev/null
+++ b/NvCloth/samples/SampleBase/ui/imgui_impl_dx11.cpp
@@ -0,0 +1,583 @@
+// ImGui Win32 + DirectX11 binding
+// In this binding, ImTextureID is used to store a 'ID3D11ShaderResourceView*' texture identifier. Read the FAQ about ImTextureID in imgui.cpp.
+
+// You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this.
+// If you use this binding you'll need to call 4 functions: ImGui_ImplXXXX_Init(), ImGui_ImplXXXX_NewFrame(), ImGui::Render() and ImGui_ImplXXXX_Shutdown().
+// If you are new to ImGui, see examples/README.txt and documentation at the top of imgui.cpp.
+// https://github.com/ocornut/imgui
+
+#include <imgui.h>
+#include "imgui_impl_dx11.h"
+
+// DirectX
+#include <d3d11.h>
+#include <d3dcompiler.h>
+#define DIRECTINPUT_VERSION 0x0800
+#include <dinput.h>
+
+// Data
+static INT64 g_Time = 0;
+static INT64 g_TicksPerSecond = 0;
+
+static HWND g_hWnd = 0;
+static ID3D11Device* g_pd3dDevice = NULL;
+static ID3D11DeviceContext* g_pd3dDeviceContext = NULL;
+static ID3D11Buffer* g_pVB = NULL;
+static ID3D11Buffer* g_pIB = NULL;
+static ID3D10Blob * g_pVertexShaderBlob = NULL;
+static ID3D11VertexShader* g_pVertexShader = NULL;
+static ID3D11InputLayout* g_pInputLayout = NULL;
+static ID3D11Buffer* g_pVertexConstantBuffer = NULL;
+static ID3D10Blob * g_pPixelShaderBlob = NULL;
+static ID3D11PixelShader* g_pPixelShader = NULL;
+static ID3D11SamplerState* g_pFontSampler = NULL;
+static ID3D11ShaderResourceView*g_pFontTextureView = NULL;
+static ID3D11RasterizerState* g_pRasterizerState = NULL;
+static ID3D11BlendState* g_pBlendState = NULL;
+static ID3D11DepthStencilState* g_pDepthStencilState = NULL;
+static int g_VertexBufferSize = 5000, g_IndexBufferSize = 10000;
+
+struct VERTEX_CONSTANT_BUFFER
+{
+ float mvp[4][4];
+};
+
+// This is the main rendering function that you have to implement and provide to ImGui (via setting up 'RenderDrawListsFn' in the ImGuiIO structure)
+// If text or lines are blurry when integrating ImGui in your engine:
+// - in your Render function, try translating your projection matrix by (0.5f,0.5f) or (0.375f,0.375f)
+void ImGui_ImplDX11_RenderDrawLists(ImDrawData* draw_data)
+{
+ ID3D11DeviceContext* ctx = g_pd3dDeviceContext;
+
+ // Create and grow vertex/index buffers if needed
+ if (!g_pVB || g_VertexBufferSize < draw_data->TotalVtxCount)
+ {
+ if (g_pVB) { g_pVB->Release(); g_pVB = NULL; }
+ g_VertexBufferSize = draw_data->TotalVtxCount + 5000;
+ D3D11_BUFFER_DESC desc;
+ memset(&desc, 0, sizeof(D3D11_BUFFER_DESC));
+ desc.Usage = D3D11_USAGE_DYNAMIC;
+ desc.ByteWidth = g_VertexBufferSize * sizeof(ImDrawVert);
+ desc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
+ desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
+ desc.MiscFlags = 0;
+ if (g_pd3dDevice->CreateBuffer(&desc, NULL, &g_pVB) < 0)
+ return;
+ }
+ if (!g_pIB || g_IndexBufferSize < draw_data->TotalIdxCount)
+ {
+ if (g_pIB) { g_pIB->Release(); g_pIB = NULL; }
+ g_IndexBufferSize = draw_data->TotalIdxCount + 10000;
+ D3D11_BUFFER_DESC desc;
+ memset(&desc, 0, sizeof(D3D11_BUFFER_DESC));
+ desc.Usage = D3D11_USAGE_DYNAMIC;
+ desc.ByteWidth = g_IndexBufferSize * sizeof(ImDrawIdx);
+ desc.BindFlags = D3D11_BIND_INDEX_BUFFER;
+ desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
+ if (g_pd3dDevice->CreateBuffer(&desc, NULL, &g_pIB) < 0)
+ return;
+ }
+
+ // Copy and convert all vertices into a single contiguous buffer
+ D3D11_MAPPED_SUBRESOURCE vtx_resource, idx_resource;
+ if (ctx->Map(g_pVB, 0, D3D11_MAP_WRITE_DISCARD, 0, &vtx_resource) != S_OK)
+ return;
+ if (ctx->Map(g_pIB, 0, D3D11_MAP_WRITE_DISCARD, 0, &idx_resource) != S_OK)
+ return;
+ ImDrawVert* vtx_dst = (ImDrawVert*)vtx_resource.pData;
+ ImDrawIdx* idx_dst = (ImDrawIdx*)idx_resource.pData;
+ for (int n = 0; n < draw_data->CmdListsCount; n++)
+ {
+ const ImDrawList* cmd_list = draw_data->CmdLists[n];
+ memcpy(vtx_dst, &cmd_list->VtxBuffer[0], cmd_list->VtxBuffer.size() * sizeof(ImDrawVert));
+ memcpy(idx_dst, &cmd_list->IdxBuffer[0], cmd_list->IdxBuffer.size() * sizeof(ImDrawIdx));
+ vtx_dst += cmd_list->VtxBuffer.size();
+ idx_dst += cmd_list->IdxBuffer.size();
+ }
+ ctx->Unmap(g_pVB, 0);
+ ctx->Unmap(g_pIB, 0);
+
+ // Setup orthographic projection matrix into our constant buffer
+ {
+ D3D11_MAPPED_SUBRESOURCE mapped_resource;
+ if (ctx->Map(g_pVertexConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped_resource) != S_OK)
+ return;
+ VERTEX_CONSTANT_BUFFER* constant_buffer = (VERTEX_CONSTANT_BUFFER*)mapped_resource.pData;
+ float L = 0.0f;
+ float R = ImGui::GetIO().DisplaySize.x;
+ float B = ImGui::GetIO().DisplaySize.y;
+ float T = 0.0f;
+ float mvp[4][4] =
+ {
+ { 2.0f/(R-L), 0.0f, 0.0f, 0.0f },
+ { 0.0f, 2.0f/(T-B), 0.0f, 0.0f },
+ { 0.0f, 0.0f, 0.5f, 0.0f },
+ { (R+L)/(L-R), (T+B)/(B-T), 0.5f, 1.0f },
+ };
+ memcpy(&constant_buffer->mvp, mvp, sizeof(mvp));
+ ctx->Unmap(g_pVertexConstantBuffer, 0);
+ }
+
+ // Backup DX state that will be modified to restore it afterwards (unfortunately this is very ugly looking and verbose. Close your eyes!)
+ struct BACKUP_DX11_STATE
+ {
+ UINT ScissorRectsCount, ViewportsCount;
+ D3D11_RECT ScissorRects[D3D11_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE];
+ D3D11_VIEWPORT Viewports[D3D11_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE];
+ ID3D11RasterizerState* RS;
+ ID3D11BlendState* BlendState;
+ FLOAT BlendFactor[4];
+ UINT SampleMask;
+ UINT StencilRef;
+ ID3D11DepthStencilState* DepthStencilState;
+ ID3D11ShaderResourceView* PSShaderResource;
+ ID3D11SamplerState* PSSampler;
+ ID3D11PixelShader* PS;
+ ID3D11VertexShader* VS;
+ UINT PSInstancesCount, VSInstancesCount;
+ ID3D11ClassInstance* PSInstances[256], *VSInstances[256]; // 256 is max according to PSSetShader documentation
+ D3D11_PRIMITIVE_TOPOLOGY PrimitiveTopology;
+ ID3D11Buffer* IndexBuffer, *VertexBuffer, *VSConstantBuffer;
+ UINT IndexBufferOffset, VertexBufferStride, VertexBufferOffset;
+ DXGI_FORMAT IndexBufferFormat;
+ ID3D11InputLayout* InputLayout;
+ };
+ BACKUP_DX11_STATE old;
+ old.ScissorRectsCount = old.ViewportsCount = D3D11_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE;
+ ctx->RSGetScissorRects(&old.ScissorRectsCount, old.ScissorRects);
+ ctx->RSGetViewports(&old.ViewportsCount, old.Viewports);
+ ctx->RSGetState(&old.RS);
+ ctx->OMGetBlendState(&old.BlendState, old.BlendFactor, &old.SampleMask);
+ ctx->OMGetDepthStencilState(&old.DepthStencilState, &old.StencilRef);
+ ctx->PSGetShaderResources(0, 1, &old.PSShaderResource);
+ ctx->PSGetSamplers(0, 1, &old.PSSampler);
+ old.PSInstancesCount = old.VSInstancesCount = 256;
+ ctx->PSGetShader(&old.PS, old.PSInstances, &old.PSInstancesCount);
+ ctx->VSGetShader(&old.VS, old.VSInstances, &old.VSInstancesCount);
+ ctx->VSGetConstantBuffers(0, 1, &old.VSConstantBuffer);
+ ctx->IAGetPrimitiveTopology(&old.PrimitiveTopology);
+ ctx->IAGetIndexBuffer(&old.IndexBuffer, &old.IndexBufferFormat, &old.IndexBufferOffset);
+ ctx->IAGetVertexBuffers(0, 1, &old.VertexBuffer, &old.VertexBufferStride, &old.VertexBufferOffset);
+ ctx->IAGetInputLayout(&old.InputLayout);
+
+ // Setup viewport
+ D3D11_VIEWPORT vp;
+ memset(&vp, 0, sizeof(D3D11_VIEWPORT));
+ vp.Width = ImGui::GetIO().DisplaySize.x;
+ vp.Height = ImGui::GetIO().DisplaySize.y;
+ vp.MinDepth = 0.0f;
+ vp.MaxDepth = 1.0f;
+ vp.TopLeftX = vp.TopLeftY = 0.0f;
+ ctx->RSSetViewports(1, &vp);
+
+ // Bind shader and vertex buffers
+ unsigned int stride = sizeof(ImDrawVert);
+ unsigned int offset = 0;
+ ctx->IASetInputLayout(g_pInputLayout);
+ ctx->IASetVertexBuffers(0, 1, &g_pVB, &stride, &offset);
+ ctx->IASetIndexBuffer(g_pIB, sizeof(ImDrawIdx) == 2 ? DXGI_FORMAT_R16_UINT : DXGI_FORMAT_R32_UINT, 0);
+ ctx->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
+ ctx->VSSetShader(g_pVertexShader, NULL, 0);
+ ctx->VSSetConstantBuffers(0, 1, &g_pVertexConstantBuffer);
+ ctx->PSSetShader(g_pPixelShader, NULL, 0);
+ ctx->PSSetSamplers(0, 1, &g_pFontSampler);
+
+ // Setup render state
+ const float blend_factor[4] = { 0.f, 0.f, 0.f, 0.f };
+ ctx->OMSetBlendState(g_pBlendState, blend_factor, 0xffffffff);
+ ctx->OMSetDepthStencilState(g_pDepthStencilState, 0);
+ ctx->RSSetState(g_pRasterizerState);
+
+ // Render command lists
+ int vtx_offset = 0;
+ int idx_offset = 0;
+ for (int n = 0; n < draw_data->CmdListsCount; n++)
+ {
+ const ImDrawList* cmd_list = draw_data->CmdLists[n];
+ for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.size(); cmd_i++)
+ {
+ const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
+ if (pcmd->UserCallback)
+ {
+ pcmd->UserCallback(cmd_list, pcmd);
+ }
+ else
+ {
+ const D3D11_RECT r = { (LONG)pcmd->ClipRect.x, (LONG)pcmd->ClipRect.y, (LONG)pcmd->ClipRect.z, (LONG)pcmd->ClipRect.w };
+ ctx->PSSetShaderResources(0, 1, (ID3D11ShaderResourceView**)&pcmd->TextureId);
+ ctx->RSSetScissorRects(1, &r);
+ ctx->DrawIndexed(pcmd->ElemCount, idx_offset, vtx_offset);
+ }
+ idx_offset += pcmd->ElemCount;
+ }
+ vtx_offset += cmd_list->VtxBuffer.size();
+ }
+
+ // Restore modified DX state
+ ctx->RSSetScissorRects(old.ScissorRectsCount, old.ScissorRects);
+ ctx->RSSetViewports(old.ViewportsCount, old.Viewports);
+ ctx->RSSetState(old.RS); if (old.RS) old.RS->Release();
+ ctx->OMSetBlendState(old.BlendState, old.BlendFactor, old.SampleMask); if (old.BlendState) old.BlendState->Release();
+ ctx->OMSetDepthStencilState(old.DepthStencilState, old.StencilRef); if (old.DepthStencilState) old.DepthStencilState->Release();
+ ctx->PSSetShaderResources(0, 1, &old.PSShaderResource); if (old.PSShaderResource) old.PSShaderResource->Release();
+ ctx->PSSetSamplers(0, 1, &old.PSSampler); if (old.PSSampler) old.PSSampler->Release();
+ ctx->PSSetShader(old.PS, old.PSInstances, old.PSInstancesCount); if (old.PS) old.PS->Release();
+ for (UINT i = 0; i < old.PSInstancesCount; i++) if (old.PSInstances[i]) old.PSInstances[i]->Release();
+ ctx->VSSetShader(old.VS, old.VSInstances, old.VSInstancesCount); if (old.VS) old.VS->Release();
+ ctx->VSSetConstantBuffers(0, 1, &old.VSConstantBuffer); if (old.VSConstantBuffer) old.VSConstantBuffer->Release();
+ for (UINT i = 0; i < old.VSInstancesCount; i++) if (old.VSInstances[i]) old.VSInstances[i]->Release();
+ ctx->IASetPrimitiveTopology(old.PrimitiveTopology);
+ ctx->IASetIndexBuffer(old.IndexBuffer, old.IndexBufferFormat, old.IndexBufferOffset); if (old.IndexBuffer) old.IndexBuffer->Release();
+ ctx->IASetVertexBuffers(0, 1, &old.VertexBuffer, &old.VertexBufferStride, &old.VertexBufferOffset); if (old.VertexBuffer) old.VertexBuffer->Release();
+ ctx->IASetInputLayout(old.InputLayout); if (old.InputLayout) old.InputLayout->Release();
+}
+
+IMGUI_API LRESULT ImGui_ImplDX11_WndProcHandler(HWND, UINT msg, WPARAM wParam, LPARAM lParam)
+{
+ ImGuiIO& io = ImGui::GetIO();
+ switch (msg)
+ {
+ case WM_LBUTTONDOWN:
+ io.MouseDown[0] = true;
+ return true;
+ case WM_LBUTTONUP:
+ io.MouseDown[0] = false;
+ return true;
+ case WM_RBUTTONDOWN:
+ io.MouseDown[1] = true;
+ return true;
+ case WM_RBUTTONUP:
+ io.MouseDown[1] = false;
+ return true;
+ case WM_MBUTTONDOWN:
+ io.MouseDown[2] = true;
+ return true;
+ case WM_MBUTTONUP:
+ io.MouseDown[2] = false;
+ return true;
+ case WM_MOUSEWHEEL:
+ io.MouseWheel += GET_WHEEL_DELTA_WPARAM(wParam) > 0 ? +1.0f : -1.0f;
+ return true;
+ case WM_MOUSEMOVE:
+ io.MousePos.x = (signed short)(lParam);
+ io.MousePos.y = (signed short)(lParam >> 16);
+ return true;
+ case WM_KEYDOWN:
+ if (wParam < 256)
+ io.KeysDown[wParam] = 1;
+ return true;
+ case WM_KEYUP:
+ if (wParam < 256)
+ io.KeysDown[wParam] = 0;
+ return true;
+ case WM_CHAR:
+ // You can also use ToAscii()+GetKeyboardState() to retrieve characters.
+ if (wParam > 0 && wParam < 0x10000)
+ io.AddInputCharacter((unsigned short)wParam);
+ return true;
+ }
+ return 0;
+}
+
+static void ImGui_ImplDX11_CreateFontsTexture()
+{
+ // Build texture atlas
+ ImGuiIO& io = ImGui::GetIO();
+ unsigned char* pixels;
+ int width, height;
+ io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
+
+ // Upload texture to graphics system
+ {
+ D3D11_TEXTURE2D_DESC desc;
+ ZeroMemory(&desc, sizeof(desc));
+ desc.Width = width;
+ desc.Height = height;
+ desc.MipLevels = 1;
+ desc.ArraySize = 1;
+ desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
+ desc.SampleDesc.Count = 1;
+ desc.Usage = D3D11_USAGE_DEFAULT;
+ desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
+ desc.CPUAccessFlags = 0;
+
+ ID3D11Texture2D *pTexture = NULL;
+ D3D11_SUBRESOURCE_DATA subResource;
+ subResource.pSysMem = pixels;
+ subResource.SysMemPitch = desc.Width * 4;
+ subResource.SysMemSlicePitch = 0;
+ g_pd3dDevice->CreateTexture2D(&desc, &subResource, &pTexture);
+
+ // Create texture view
+ D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc;
+ ZeroMemory(&srvDesc, sizeof(srvDesc));
+ srvDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
+ srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
+ srvDesc.Texture2D.MipLevels = desc.MipLevels;
+ srvDesc.Texture2D.MostDetailedMip = 0;
+ g_pd3dDevice->CreateShaderResourceView(pTexture, &srvDesc, &g_pFontTextureView);
+ pTexture->Release();
+ }
+
+ // Store our identifier
+ io.Fonts->TexID = (void *)g_pFontTextureView;
+
+ // Create texture sampler
+ {
+ D3D11_SAMPLER_DESC desc;
+ ZeroMemory(&desc, sizeof(desc));
+ desc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
+ desc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
+ desc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
+ desc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
+ desc.MipLODBias = 0.f;
+ desc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
+ desc.MinLOD = 0.f;
+ desc.MaxLOD = 0.f;
+ g_pd3dDevice->CreateSamplerState(&desc, &g_pFontSampler);
+ }
+}
+
+bool ImGui_ImplDX11_CreateDeviceObjects()
+{
+ if (!g_pd3dDevice)
+ return false;
+ if (g_pFontSampler)
+ ImGui_ImplDX11_InvalidateDeviceObjects();
+
+ // By using D3DCompile() from <d3dcompiler.h> / d3dcompiler.lib, we introduce a dependency to a given version of d3dcompiler_XX.dll (see D3DCOMPILER_DLL_A)
+ // If you would like to use this DX11 sample code but remove this dependency you can:
+ // 1) compile once, save the compiled shader blobs into a file or source code and pass them to CreateVertexShader()/CreatePixelShader() [prefered solution]
+ // 2) use code to detect any version of the DLL and grab a pointer to D3DCompile from the DLL.
+ // See https://github.com/ocornut/imgui/pull/638 for sources and details.
+
+ // Create the vertex shader
+ {
+ static const char* vertexShader =
+ "cbuffer vertexBuffer : register(b0) \
+ {\
+ float4x4 ProjectionMatrix; \
+ };\
+ struct VS_INPUT\
+ {\
+ float2 pos : POSITION;\
+ float4 col : COLOR0;\
+ float2 uv : TEXCOORD0;\
+ };\
+ \
+ struct PS_INPUT\
+ {\
+ float4 pos : SV_POSITION;\
+ float4 col : COLOR0;\
+ float2 uv : TEXCOORD0;\
+ };\
+ \
+ PS_INPUT main(VS_INPUT input)\
+ {\
+ PS_INPUT output;\
+ output.pos = mul( ProjectionMatrix, float4(input.pos.xy, 0.f, 1.f));\
+ output.col = input.col;\
+ output.uv = input.uv;\
+ return output;\
+ }";
+
+ D3DCompile(vertexShader, strlen(vertexShader), NULL, NULL, NULL, "main", "vs_4_0", 0, 0, &g_pVertexShaderBlob, NULL);
+ if (g_pVertexShaderBlob == NULL) // NB: Pass ID3D10Blob* pErrorBlob to D3DCompile() to get error showing in (const char*)pErrorBlob->GetBufferPointer(). Make sure to Release() the blob!
+ return false;
+ if (g_pd3dDevice->CreateVertexShader((DWORD*)g_pVertexShaderBlob->GetBufferPointer(), g_pVertexShaderBlob->GetBufferSize(), NULL, &g_pVertexShader) != S_OK)
+ return false;
+
+ // Create the input layout
+ D3D11_INPUT_ELEMENT_DESC local_layout[] = {
+ { "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, (size_t)(&((ImDrawVert*)0)->pos), D3D11_INPUT_PER_VERTEX_DATA, 0 },
+ { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, (size_t)(&((ImDrawVert*)0)->uv), D3D11_INPUT_PER_VERTEX_DATA, 0 },
+ { "COLOR", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, (size_t)(&((ImDrawVert*)0)->col), D3D11_INPUT_PER_VERTEX_DATA, 0 },
+ };
+ if (g_pd3dDevice->CreateInputLayout(local_layout, 3, g_pVertexShaderBlob->GetBufferPointer(), g_pVertexShaderBlob->GetBufferSize(), &g_pInputLayout) != S_OK)
+ return false;
+
+ // Create the constant buffer
+ {
+ D3D11_BUFFER_DESC desc;
+ desc.ByteWidth = sizeof(VERTEX_CONSTANT_BUFFER);
+ desc.Usage = D3D11_USAGE_DYNAMIC;
+ desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
+ desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
+ desc.MiscFlags = 0;
+ g_pd3dDevice->CreateBuffer(&desc, NULL, &g_pVertexConstantBuffer);
+ }
+ }
+
+ // Create the pixel shader
+ {
+ static const char* pixelShader =
+ "struct PS_INPUT\
+ {\
+ float4 pos : SV_POSITION;\
+ float4 col : COLOR0;\
+ float2 uv : TEXCOORD0;\
+ };\
+ sampler sampler0;\
+ Texture2D texture0;\
+ \
+ float4 main(PS_INPUT input) : SV_Target\
+ {\
+ float4 out_col = input.col * texture0.Sample(sampler0, input.uv); \
+ return out_col; \
+ }";
+
+ D3DCompile(pixelShader, strlen(pixelShader), NULL, NULL, NULL, "main", "ps_4_0", 0, 0, &g_pPixelShaderBlob, NULL);
+ if (g_pPixelShaderBlob == NULL) // NB: Pass ID3D10Blob* pErrorBlob to D3DCompile() to get error showing in (const char*)pErrorBlob->GetBufferPointer(). Make sure to Release() the blob!
+ return false;
+ if (g_pd3dDevice->CreatePixelShader((DWORD*)g_pPixelShaderBlob->GetBufferPointer(), g_pPixelShaderBlob->GetBufferSize(), NULL, &g_pPixelShader) != S_OK)
+ return false;
+ }
+
+ // Create the blending setup
+ {
+ D3D11_BLEND_DESC desc;
+ ZeroMemory(&desc, sizeof(desc));
+ desc.AlphaToCoverageEnable = false;
+ desc.RenderTarget[0].BlendEnable = true;
+ desc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA;
+ desc.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
+ desc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
+ desc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_INV_SRC_ALPHA;
+ desc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO;
+ desc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
+ desc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
+ g_pd3dDevice->CreateBlendState(&desc, &g_pBlendState);
+ }
+
+ // Create the rasterizer state
+ {
+ D3D11_RASTERIZER_DESC desc;
+ ZeroMemory(&desc, sizeof(desc));
+ desc.FillMode = D3D11_FILL_SOLID;
+ desc.CullMode = D3D11_CULL_NONE;
+ desc.ScissorEnable = true;
+ desc.DepthClipEnable = true;
+ g_pd3dDevice->CreateRasterizerState(&desc, &g_pRasterizerState);
+ }
+
+ // Create depth-stencil State
+ {
+ D3D11_DEPTH_STENCIL_DESC desc;
+ ZeroMemory(&desc, sizeof(desc));
+ desc.DepthEnable = false;
+ desc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
+ desc.DepthFunc = D3D11_COMPARISON_ALWAYS;
+ desc.StencilEnable = false;
+ desc.FrontFace.StencilFailOp = desc.FrontFace.StencilDepthFailOp = desc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
+ desc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
+ desc.BackFace = desc.FrontFace;
+ g_pd3dDevice->CreateDepthStencilState(&desc, &g_pDepthStencilState);
+ }
+
+ ImGui_ImplDX11_CreateFontsTexture();
+
+ return true;
+}
+
+void ImGui_ImplDX11_InvalidateDeviceObjects()
+{
+ if (!g_pd3dDevice)
+ return;
+
+ if (g_pFontSampler) { g_pFontSampler->Release(); g_pFontSampler = NULL; }
+ if (g_pFontTextureView) { g_pFontTextureView->Release(); g_pFontTextureView = NULL; ImGui::GetIO().Fonts->TexID = 0; }
+ if (g_pIB) { g_pIB->Release(); g_pIB = NULL; }
+ if (g_pVB) { g_pVB->Release(); g_pVB = NULL; }
+
+ if (g_pBlendState) { g_pBlendState->Release(); g_pBlendState = NULL; }
+ if (g_pDepthStencilState) { g_pDepthStencilState->Release(); g_pDepthStencilState = NULL; }
+ if (g_pRasterizerState) { g_pRasterizerState->Release(); g_pRasterizerState = NULL; }
+ if (g_pPixelShader) { g_pPixelShader->Release(); g_pPixelShader = NULL; }
+ if (g_pPixelShaderBlob) { g_pPixelShaderBlob->Release(); g_pPixelShaderBlob = NULL; }
+ if (g_pVertexConstantBuffer) { g_pVertexConstantBuffer->Release(); g_pVertexConstantBuffer = NULL; }
+ if (g_pInputLayout) { g_pInputLayout->Release(); g_pInputLayout = NULL; }
+ if (g_pVertexShader) { g_pVertexShader->Release(); g_pVertexShader = NULL; }
+ if (g_pVertexShaderBlob) { g_pVertexShaderBlob->Release(); g_pVertexShaderBlob = NULL; }
+}
+
+bool ImGui_ImplDX11_Init(void* hwnd, ID3D11Device* device, ID3D11DeviceContext* device_context)
+{
+ g_hWnd = (HWND)hwnd;
+ g_pd3dDevice = device;
+ g_pd3dDeviceContext = device_context;
+
+ if (!QueryPerformanceFrequency((LARGE_INTEGER *)&g_TicksPerSecond))
+ return false;
+ if (!QueryPerformanceCounter((LARGE_INTEGER *)&g_Time))
+ return false;
+
+ ImGuiIO& io = ImGui::GetIO();
+ io.KeyMap[ImGuiKey_Tab] = VK_TAB; // Keyboard mapping. ImGui will use those indices to peek into the io.KeyDown[] array that we will update during the application lifetime.
+ io.KeyMap[ImGuiKey_LeftArrow] = VK_LEFT;
+ io.KeyMap[ImGuiKey_RightArrow] = VK_RIGHT;
+ io.KeyMap[ImGuiKey_UpArrow] = VK_UP;
+ io.KeyMap[ImGuiKey_DownArrow] = VK_DOWN;
+ io.KeyMap[ImGuiKey_PageUp] = VK_PRIOR;
+ io.KeyMap[ImGuiKey_PageDown] = VK_NEXT;
+ io.KeyMap[ImGuiKey_Home] = VK_HOME;
+ io.KeyMap[ImGuiKey_End] = VK_END;
+ io.KeyMap[ImGuiKey_Delete] = VK_DELETE;
+ io.KeyMap[ImGuiKey_Backspace] = VK_BACK;
+ io.KeyMap[ImGuiKey_Enter] = VK_RETURN;
+ io.KeyMap[ImGuiKey_Escape] = VK_ESCAPE;
+ io.KeyMap[ImGuiKey_A] = 'A';
+ io.KeyMap[ImGuiKey_C] = 'C';
+ io.KeyMap[ImGuiKey_V] = 'V';
+ io.KeyMap[ImGuiKey_X] = 'X';
+ io.KeyMap[ImGuiKey_Y] = 'Y';
+ io.KeyMap[ImGuiKey_Z] = 'Z';
+
+ io.RenderDrawListsFn = ImGui_ImplDX11_RenderDrawLists; // Alternatively you can set this to NULL and call ImGui::GetDrawData() after ImGui::Render() to get the same ImDrawData pointer.
+ io.ImeWindowHandle = g_hWnd;
+
+ return true;
+}
+
+void ImGui_ImplDX11_Shutdown()
+{
+ ImGui_ImplDX11_InvalidateDeviceObjects();
+ ImGui::Shutdown();
+ g_pd3dDevice = NULL;
+ g_pd3dDeviceContext = NULL;
+ g_hWnd = (HWND)0;
+}
+
+void ImGui_ImplDX11_NewFrame()
+{
+ if (!g_pFontSampler)
+ ImGui_ImplDX11_CreateDeviceObjects();
+
+ ImGuiIO& io = ImGui::GetIO();
+
+ // Setup display size (every frame to accommodate for window resizing)
+ RECT rect;
+ GetClientRect(g_hWnd, &rect);
+ io.DisplaySize = ImVec2((float)(rect.right - rect.left), (float)(rect.bottom - rect.top));
+
+ // Setup time step
+ INT64 current_time;
+ QueryPerformanceCounter((LARGE_INTEGER *)&current_time);
+ io.DeltaTime = (float)(current_time - g_Time) / g_TicksPerSecond;
+ g_Time = current_time;
+
+ // Read keyboard modifiers inputs
+ io.KeyCtrl = (GetKeyState(VK_CONTROL) & 0x8000) != 0;
+ io.KeyShift = (GetKeyState(VK_SHIFT) & 0x8000) != 0;
+ io.KeyAlt = (GetKeyState(VK_MENU) & 0x8000) != 0;
+ io.KeySuper = false;
+ // io.KeysDown : filled by WM_KEYDOWN/WM_KEYUP events
+ // io.MousePos : filled by WM_MOUSEMOVE events
+ // io.MouseDown : filled by WM_*BUTTON* events
+ // io.MouseWheel : filled by WM_MOUSEWHEEL events
+
+ // Hide OS mouse cursor if ImGui is drawing it
+ SetCursor(io.MouseDrawCursor ? NULL : LoadCursor(NULL, IDC_ARROW));
+
+ // Start the frame
+ ImGui::NewFrame();
+}
diff --git a/NvCloth/samples/SampleBase/ui/imgui_impl_dx11.h b/NvCloth/samples/SampleBase/ui/imgui_impl_dx11.h
new file mode 100644
index 0000000..7d6f710
--- /dev/null
+++ b/NvCloth/samples/SampleBase/ui/imgui_impl_dx11.h
@@ -0,0 +1,25 @@
+// ImGui Win32 + DirectX11 binding
+// In this binding, ImTextureID is used to store a 'ID3D11ShaderResourceView*' texture identifier. Read the FAQ about ImTextureID in imgui.cpp.
+
+// You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this.
+// If you use this binding you'll need to call 4 functions: ImGui_ImplXXXX_Init(), ImGui_ImplXXXX_NewFrame(), ImGui::Render() and ImGui_ImplXXXX_Shutdown().
+// If you are new to ImGui, see examples/README.txt and documentation at the top of imgui.cpp.
+// https://github.com/ocornut/imgui
+
+struct ID3D11Device;
+struct ID3D11DeviceContext;
+
+IMGUI_API bool ImGui_ImplDX11_Init(void* hwnd, ID3D11Device* device, ID3D11DeviceContext* device_context);
+IMGUI_API void ImGui_ImplDX11_Shutdown();
+IMGUI_API void ImGui_ImplDX11_NewFrame();
+
+// Use if you want to reset your rendering device without losing ImGui state.
+IMGUI_API void ImGui_ImplDX11_InvalidateDeviceObjects();
+IMGUI_API bool ImGui_ImplDX11_CreateDeviceObjects();
+
+// Handler for Win32 messages, update mouse/keyboard data.
+// You may or not need this for your implementation, but it can serve as reference for handling inputs.
+// Commented out to avoid dragging dependencies on <windows.h> types. You can copy the extern declaration in your code.
+/*
+IMGUI_API LRESULT ImGui_ImplDX11_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
+*/
diff --git a/NvCloth/samples/SampleBase/utils/CallbackImplementations.cpp b/NvCloth/samples/SampleBase/utils/CallbackImplementations.cpp
new file mode 100644
index 0000000..0a257ba
--- /dev/null
+++ b/NvCloth/samples/SampleBase/utils/CallbackImplementations.cpp
@@ -0,0 +1,110 @@
+/*
+* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
+*
+* NVIDIA CORPORATION and its licensors retain all intellectual property
+* and proprietary rights in and to this software, related documentation
+* and any modifications thereto. Any use, reproduction, disclosure or
+* distribution of this software and related documentation without an express
+* license agreement from NVIDIA CORPORATION is strictly prohibited.
+*/
+
+#include "CallbackImplementations.h"
+#include "JobManager.h"
+#include <iostream>
+
+//#if USE_DX11
+#include <d3d11.h>
+//#endif
+
+#include <PsThread.h>
+
+NvClothEnvironment* NvClothEnvironment::sEnv = nullptr;
+
+
+void ErrorCallback::reportError(physx::PxErrorCode::Enum code, const char* message, const char* file, int line)
+{
+ const char* codeName = "???";
+ switch(code)
+ {
+#define CASE(x) case physx::PxErrorCode::Enum::x : codeName = #x; break;
+ CASE(eNO_ERROR)
+ CASE(eDEBUG_INFO)
+ CASE(eDEBUG_WARNING)
+ CASE(eINVALID_PARAMETER)
+ CASE(eINVALID_OPERATION)
+ CASE(eOUT_OF_MEMORY)
+ CASE(eINTERNAL_ERROR)
+ CASE(eABORT)
+ CASE(ePERF_WARNING)
+ default:
+ ;
+#undef CASE
+ }
+
+ std::cout << "Log " << codeName << " from file:" << file << ":" << line << "\n MSG:" << message << std::endl;
+}
+
+//#if USE_DX11
+DxContextManagerCallbackImpl::DxContextManagerCallbackImpl(ID3D11Device* device, bool synchronizeResources)
+ :
+ mDevice(device),
+ mSynchronizeResources(synchronizeResources)
+{
+ mDevice->AddRef();
+ mDevice->GetImmediateContext(&mContext);
+#ifdef _DEBUG
+ mLockCountTls = physx::shdfnd::TlsAlloc();
+#endif
+}
+DxContextManagerCallbackImpl::~DxContextManagerCallbackImpl()
+{
+ mContext->Release();
+
+#if _DEBUG
+ ID3D11Debug* debugDevice;
+ mDevice->QueryInterface(&debugDevice);
+ if(debugDevice)
+ {
+ debugDevice->ReportLiveDeviceObjects(D3D11_RLDO_DETAIL);
+ debugDevice->Release();
+ }
+#endif
+
+ mDevice->Release();
+
+#if _DEBUG
+ physx::shdfnd::TlsFree(mLockCountTls);
+#endif
+}
+
+void DxContextManagerCallbackImpl::acquireContext()
+{
+
+ mMutex.lock();
+#if _DEBUG
+ physx::shdfnd::TlsSet(mLockCountTls, reinterpret_cast<void*>(reinterpret_cast<intptr_t>(physx::shdfnd::TlsGet(mLockCountTls)) + 1));
+#endif
+}
+void DxContextManagerCallbackImpl::releaseContext()
+{
+#if _DEBUG
+ physx::shdfnd::TlsSet(mLockCountTls, reinterpret_cast<void*>(reinterpret_cast<intptr_t>(physx::shdfnd::TlsGet(mLockCountTls)) - 1));
+#endif
+ mMutex.unlock();
+}
+ID3D11Device* DxContextManagerCallbackImpl::getDevice() const
+{
+ return mDevice;
+}
+ID3D11DeviceContext* DxContextManagerCallbackImpl::getContext() const
+{
+#if _DEBUG
+ assert(reinterpret_cast<intptr_t>(physx::shdfnd::TlsGet(mLockCountTls)) > 0);
+#endif
+ return mContext;
+}
+bool DxContextManagerCallbackImpl::synchronizeResources() const
+{
+ return mSynchronizeResources;
+}
+//#endif
diff --git a/NvCloth/samples/SampleBase/utils/CallbackImplementations.h b/NvCloth/samples/SampleBase/utils/CallbackImplementations.h
new file mode 100644
index 0000000..38a4b17
--- /dev/null
+++ b/NvCloth/samples/SampleBase/utils/CallbackImplementations.h
@@ -0,0 +1,230 @@
+/*
+* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
+*
+* NVIDIA CORPORATION and its licensors retain all intellectual property
+* and proprietary rights in and to this software, related documentation
+* and any modifications thereto. Any use, reproduction, disclosure or
+* distribution of this software and related documentation without an express
+* license agreement from NVIDIA CORPORATION is strictly prohibited.
+*/
+
+#pragma once
+#include <NvCloth/Callbacks.h>
+#include <foundation/PxAllocatorCallback.h>
+#include <foundation/PxErrorCallback.h>
+#include <foundation/PxAssert.h>
+#include <foundation/PxProfiler.h>
+
+#include <vector>
+#include <map>
+
+#if USE_CUDA
+#include <cuda.h>
+#endif
+
+#include <string>
+#include <sstream>
+#include <assert.h>
+#include <mutex>
+
+#include <NvCloth/DxContextManagerCallback.h>
+
+#ifdef _MSC_VER
+#include <Windows.h>
+#endif
+
+class Allocator : public physx::PxAllocatorCallback
+{
+ public:
+ Allocator()
+ {
+ mEnableLeakDetection = false;
+ }
+ virtual void* allocate(size_t size, const char* typeName, const char* filename, int line)
+ {
+ #ifdef _MSC_VER
+ void* ptr = _aligned_malloc(size, 16);
+ #else
+ void* ptr;
+ if(posix_memalign(&ptr, 16, size)) ptr = 0;
+ #endif
+ if (mEnableLeakDetection)
+ {
+ std::lock_guard<std::mutex> lock(mAllocationsMapLock);
+ mAllocations[ptr] = Allocation(size, typeName, filename, line);
+ }
+ return ptr;
+ }
+ virtual void deallocate(void* ptr)
+ {
+ if (mEnableLeakDetection && ptr)
+ {
+ std::lock_guard<std::mutex> lock(mAllocationsMapLock);
+ auto i = mAllocations.find(ptr);
+ if (i == mAllocations.end())
+ {
+ printf("Tried to deallocate %p which was not allocated with this allocator callback.",ptr);
+ }
+ else
+ {
+ mAllocations.erase(i);
+ }
+ }
+ #ifdef _MSC_VER
+ _aligned_free(ptr);
+ #else
+ free(ptr);
+ #endif
+ }
+
+ void StartTrackingLeaks()
+ {
+ std::lock_guard<std::mutex> lock(mAllocationsMapLock);
+ mAllocations.clear();
+ mEnableLeakDetection = true;
+ }
+
+ void StopTrackingLeaksAndReport()
+ {
+ std::lock_guard<std::mutex> lock(mAllocationsMapLock);
+ mEnableLeakDetection = false;
+
+ size_t totalBytes = 0;
+ std::stringstream message;
+ message << "Memory leaks detected:\n";
+ for (auto it = mAllocations.begin(); it != mAllocations.end(); ++it)
+ {
+ const Allocation& alloc = it->second;
+ message << "* Allocated ptr " << it->first << " of " << alloc.mSize << "bytes (type=" << alloc.mTypeName << ") at " << alloc.mFileName << ":" << alloc.mLine<<"\n";
+ totalBytes += alloc.mSize;
+ }
+ if (mAllocations.size()>0)
+ {
+ message << "=====Total of " << totalBytes << " bytes in " << mAllocations.size() << " allocations leaked=====";
+ const std::string& tmp = message.str();
+#ifdef _MSC_VER
+// OutputDebugString(tmp.c_str()); //Write to visual studio output so we can see it after the application closes
+#endif
+ printf("%s\n", tmp.c_str());
+ }
+
+ mAllocations.clear();
+ }
+ private:
+ bool mEnableLeakDetection;
+ struct Allocation
+ {
+ Allocation(){}
+ Allocation(size_t size, const char* typeName, const char* filename, int line)
+ : mSize(size), mTypeName(typeName), mFileName(filename), mLine(line)
+ {
+
+ }
+ size_t mSize;
+ std::string mTypeName;
+ std::string mFileName;
+ int mLine;
+ };
+ std::map<void*,Allocation> mAllocations;
+ std::mutex mAllocationsMapLock;
+};
+
+class ErrorCallback : public physx::PxErrorCallback
+{
+ public:
+ ErrorCallback(){}
+ virtual void reportError(physx::PxErrorCode::Enum code, const char* message, const char* file, int line);
+};
+
+
+class AssertHandler : public physx::PxAssertHandler
+{
+ public:
+ virtual void operator()(const char* exp, const char* file, int line, bool& ignore)
+ {
+ PX_UNUSED(ignore);
+ printf("NV_CLOTH_ASSERT(%s) from file:%s:%d Failed\n", exp, file, line);
+ assert(("Assertion failed, see log/console for more info.",0));
+ }
+};
+
+
+class NvClothEnvironment
+{
+ NvClothEnvironment()
+ {
+ SetUp();
+ }
+ virtual ~NvClothEnvironment()
+ {
+ TearDown();
+ }
+
+ static NvClothEnvironment* sEnv;
+
+ public:
+ static void AllocateEnv()
+ {
+ sEnv = new NvClothEnvironment;
+ }
+ static void FreeEnv(){ delete sEnv; sEnv = nullptr; }
+ static void ReportEnvFreed(){ sEnv = nullptr; } //google test will free it for us, so we just reset the value
+ static NvClothEnvironment* GetEnv(){ return sEnv; }
+
+ virtual void SetUp()
+ {
+ mAllocator = new Allocator;
+ mAllocator->StartTrackingLeaks();
+ mFoundationAllocator = new Allocator;
+ mFoundationAllocator->StartTrackingLeaks();
+ mErrorCallback = new ErrorCallback;
+ mAssertHandler = new AssertHandler;
+ nv::cloth::InitializeNvCloth(mAllocator, mErrorCallback, mAssertHandler, nullptr);
+#if USE_CUDA
+ cuInit(0);
+#endif
+ }
+ virtual void TearDown()
+ {
+ mAllocator->StopTrackingLeaksAndReport();
+ mFoundationAllocator->StopTrackingLeaksAndReport();
+ delete mErrorCallback;
+ delete mFoundationAllocator;
+ delete mAllocator;
+ delete mAssertHandler;
+ }
+
+ Allocator* GetAllocator(){ return mAllocator; }
+ Allocator* GetFoundationAllocator(){ return mFoundationAllocator; }
+ ErrorCallback* GetErrorCallback(){ return mErrorCallback; }
+ AssertHandler* GetAssertHandler(){ return mAssertHandler; }
+
+ private:
+ Allocator* mAllocator;
+ Allocator* mFoundationAllocator;
+ ErrorCallback* mErrorCallback;
+ AssertHandler* mAssertHandler;
+};
+
+//#if USE_DX11
+class DxContextManagerCallbackImpl : public nv::cloth::DxContextManagerCallback
+{
+public:
+ DxContextManagerCallbackImpl(ID3D11Device* device, bool synchronizeResources = false);
+ ~DxContextManagerCallbackImpl();
+ virtual void acquireContext() override;
+ virtual void releaseContext() override;
+ virtual ID3D11Device* getDevice() const override;
+ virtual ID3D11DeviceContext* getContext() const override;
+ virtual bool synchronizeResources() const override;
+
+private:
+ std::recursive_mutex mMutex;
+ ID3D11Device* mDevice;
+ ID3D11DeviceContext* mContext;
+ bool mSynchronizeResources;
+#ifdef _DEBUG
+ uint32_t mLockCountTls;
+#endif
+};
+//#endif
diff --git a/NvCloth/samples/SampleBase/utils/ClothMeshGenerator.cpp b/NvCloth/samples/SampleBase/utils/ClothMeshGenerator.cpp
new file mode 100644
index 0000000..d75bb25
--- /dev/null
+++ b/NvCloth/samples/SampleBase/utils/ClothMeshGenerator.cpp
@@ -0,0 +1,193 @@
+/*
+* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
+*
+* NVIDIA CORPORATION and its licensors retain all intellectual property
+* and proprietary rights in and to this software, related documentation
+* and any modifications thereto. Any use, reproduction, disclosure or
+* distribution of this software and related documentation without an express
+* license agreement from NVIDIA CORPORATION is strictly prohibited.
+*/
+
+#include "ClothMeshGenerator.h"
+
+void ClothMeshData::Clear()
+{
+ mVertices.clear();
+ mTriangles.clear();
+ mQuads.clear();
+}
+
+void ClothMeshData::GeneratePlaneCloth(float width, float height, int segmentsX, int segmentsY, bool createQuads, physx::PxMat44 transform, bool alternatingDiagonals)
+{
+
+/*
+GeneratePlaneCloth(x,y,2,2) generates:
+
+ v0______v1_____v2 v0______v1_____v2
+ | | | |\ |\ |
+ | Q0 | Q1 | | \t0 | \t2 |
+ | | | | t1 \ | t3 \ |
+ v3------v4-----v5 v3-----\v4----\v5
+ | | | | \ | \ |
+ | Q2 | Q3 | | \t4| \t6|
+ |______|______| |_t5_\_|_t7__\|
+ v6 v7 v8 v6 v7 v8
+*/
+
+
+// Submesh submesh;
+
+ Clear();
+ mVertices.resize((segmentsX + 1) * (segmentsY + 1));
+ mInvMasses.resize((segmentsX + 1) * (segmentsY + 1));
+ mTriangles.resize(segmentsX * segmentsY * 2);
+ if (createQuads)
+ mQuads.resize(segmentsX * segmentsY);
+
+ mMesh.vertices.resize(mVertices.size());
+ mMesh.indices.resize(3 * mTriangles.size());
+
+ physx::PxVec3 topLeft(-width * 0.5f, 0.f, -height * 0.5f);
+// vec3 topLeftGLM(-width * 0.5f, translateUp, -height * 0.5f);
+
+ //calculate uv scale and offset to keep texture aspect ratio 1:1
+ float uvSx = width > height ? 1.0f : width / height;
+ float uvSy = width > height ? height / width : 1.0f;
+ float uvOx = 0.5f * (1.0f - uvSx);
+ float uvOy = 0.5f * (1.0f - uvSy);
+
+ // Vertices
+ for (int y = 0; y < segmentsY + 1; y++)
+ {
+ for(int x = 0; x < segmentsX + 1; x++)
+ {
+ mVertices[x + y * (segmentsX + 1)] = transform.transform(topLeft + physx::PxVec3( ((float)x / (float)segmentsX) * width,
+ 0.f,
+ ((float)y / (float)segmentsY) * height));
+ mInvMasses[x + y * (segmentsX + 1)] = 1.0f;
+
+ mMesh.vertices[x + y * (segmentsX + 1)].position = transform.transform(topLeft + physx::PxVec3(((float)x / (float)segmentsX) * width,
+ 0.f,
+ ((float)y / (float)segmentsY) * height));
+ mMesh.vertices[x + y * (segmentsX + 1)].normal = transform.transform(physx::PxVec3(0.f, 1.f, 0.f));
+
+ mMesh.vertices[x + y * (segmentsX + 1)].uv = physx::PxVec2(uvOx + uvSx*(float)x / (float)segmentsX, uvOy + uvSy*(1.0f - (float)y / (float)segmentsY));
+ }
+ }
+
+ if (createQuads)
+ {
+ // Quads
+ for (int y = 0; y < segmentsY; y++)
+ {
+ for (int x = 0; x < segmentsX; x++)
+ {
+ mQuads[(x + y * segmentsX)] = Quad((uint32_t)(x + 0) + (y + 0) * (segmentsX + 1),
+ (uint32_t)(x + 1) + (y + 0) * (segmentsX + 1),
+ (uint32_t)(x + 1) + (y + 1) * (segmentsX + 1),
+ (uint32_t)(x + 0) + (y + 1) * (segmentsX + 1));
+ }
+ }
+ }
+
+ // Triangles
+ for (int y = 0; y < segmentsY; y++)
+ {
+ for(int x = 0; x < segmentsX; x++)
+ {
+ if(alternatingDiagonals && (x^y)&1)
+ {
+ //Top right to bottom left
+ mTriangles[(x + y * segmentsX) * 2 + 0] = Triangle( (uint32_t)(x + 0) + (y + 0) * (segmentsX + 1),
+ (uint32_t)(x + 1) + (y + 0) * (segmentsX + 1),
+ (uint32_t)(x + 0) + (y + 1) * (segmentsX + 1));
+
+ mTriangles[(x + y * segmentsX) * 2 + 1] = Triangle( (uint32_t)(x + 1) + (y + 0) * (segmentsX + 1),
+ (uint32_t)(x + 1) + (y + 1) * (segmentsX + 1),
+ (uint32_t)(x + 0) + (y + 1) * (segmentsX + 1));
+ }
+ else
+ {
+ //Top left to bottom right
+ mTriangles[(x + y * segmentsX) * 2 + 0] = Triangle( (uint32_t)(x + 0) + (y + 0) * (segmentsX + 1),
+ (uint32_t)(x + 1) + (y + 0) * (segmentsX + 1),
+ (uint32_t)(x + 1) + (y + 1) * (segmentsX + 1));
+
+ mTriangles[(x + y * segmentsX) * 2 + 1] = Triangle( (uint32_t)(x + 0) + (y + 0) * (segmentsX + 1),
+ (uint32_t)(x + 1) + (y + 1) * (segmentsX + 1),
+ (uint32_t)(x + 0) + (y + 1) * (segmentsX + 1));
+ }
+ }
+ }
+
+ for (int i = 0; i < (int)mTriangles.size(); i++)
+ {
+ mMesh.indices[3 * i] = mTriangles[i].a;
+ mMesh.indices[3 * i + 1] = mTriangles[i].b;
+ mMesh.indices[3 * i + 2] = mTriangles[i].c;
+ }
+}
+
+void ClothMeshData::AttachClothPlaneByAngles(int segmentsX, int segmentsY, bool attachByWidth)
+{
+ for (int y = 0; y < segmentsY + 1; y++)
+ for (int x = 0; x < segmentsX + 1; x++)
+ if ((attachByWidth && y == 0) || (!attachByWidth && x == 0))
+ if (x == 0 || x == segmentsX)
+ mInvMasses[x + y * (segmentsX + 1)] = 0.0f;
+}
+
+void ClothMeshData::AttachClothPlaneBySide(int segmentsX, int segmentsY, bool attachByWidth)
+{
+ for (int y = 0; y < segmentsY + 1; y++)
+ for (int x = 0; x < segmentsX + 1; x++)
+ if ((attachByWidth && y == 0) || (!attachByWidth && x == 0))
+ mInvMasses[x + y * (segmentsX + 1)] = 0.0f;
+}
+
+void ClothMeshData::SetInvMasses(float invMass)
+{
+ // Doesn't modify attached vertices
+ for (int i = 0; i < (int)mInvMasses.size(); ++i)
+ if (mInvMasses[i] > 1e-6f)
+ mInvMasses[i] = invMass;
+}
+
+void ClothMeshData::SetInvMassesFromDensity(float density)
+{
+ // Tempt code, should take into account triangle's areas
+ // Doesn't modify attached vertices
+ for (int i = 0; i < (int)mInvMasses.size(); ++i)
+ if (mInvMasses[i] > 1e-6f)
+ mInvMasses[i] = 1.f / density;
+}
+
+template <typename T>
+nv::cloth::BoundedData ToBoundedData(T& vector)
+{
+ nv::cloth::BoundedData d;
+ d.data = &vector[0];
+ d.stride = sizeof(vector[0]);
+ d.count = (physx::PxU32)vector.size();
+
+ return d;
+}
+
+nv::cloth::ClothMeshDesc ClothMeshData::GetClothMeshDesc()
+{
+ nv::cloth::ClothMeshDesc d;
+ d.setToDefault();
+ d.points = ToBoundedData(mVertices);
+ if (mQuads.size() != 0)
+ d.quads = ToBoundedData(mQuads);
+ if (mTriangles.size() != 0)
+ d.triangles = ToBoundedData(mTriangles);
+ d.invMasses = ToBoundedData(mInvMasses);
+
+ return d;
+}
+
+SimpleMesh ClothMeshData::GetRenderMesh()
+{
+ return mMesh;
+} \ No newline at end of file
diff --git a/NvCloth/samples/SampleBase/utils/ClothMeshGenerator.h b/NvCloth/samples/SampleBase/utils/ClothMeshGenerator.h
new file mode 100644
index 0000000..0f57230
--- /dev/null
+++ b/NvCloth/samples/SampleBase/utils/ClothMeshGenerator.h
@@ -0,0 +1,54 @@
+/*
+* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
+*
+* NVIDIA CORPORATION and its licensors retain all intellectual property
+* and proprietary rights in and to this software, related documentation
+* and any modifications thereto. Any use, reproduction, disclosure or
+* distribution of this software and related documentation without an express
+* license agreement from NVIDIA CORPORATION is strictly prohibited.
+*/
+
+#pragma once
+#include <vector>
+#include <foundation/PxVec3.h>
+#include <foundation/PxVec2.h>
+#include "NvClothExt/ClothFabricCooker.h"
+#include <foundation/PxMat44.h>
+#include "Mesh.h"
+
+struct ClothMeshData
+{
+ struct Triangle
+ {
+ Triangle(){}
+ Triangle(uint32_t _a, uint32_t _b, uint32_t _c) :
+ a(_a), b(_b), c(_c){}
+ uint32_t a, b, c;
+ };
+ struct Quad
+ {
+ Quad(){}
+ Quad(uint32_t _a, uint32_t _b, uint32_t _c, uint32_t _d) :
+ a(_a), b(_b), c(_c), d(_d){}
+ uint32_t a, b, c, d;
+ };
+ std::vector<physx::PxVec3> mVertices;
+ std::vector<physx::PxVec2> mUvs;
+ std::vector<Triangle> mTriangles;
+ std::vector<Quad> mQuads;
+ std::vector<physx::PxReal> mInvMasses;
+
+ SimpleMesh mMesh;
+
+ void Clear();
+ void GeneratePlaneCloth(float width, float height, int segmentsX, int segmentsY, bool createQuads = false, physx::PxMat44 transform = physx::PxIdentity, bool alternatingDiagonals = true);
+
+ void AttachClothPlaneByAngles(int segmentsX, int segmentsY, bool attachByWidth = true);
+ void AttachClothPlaneBySide(int segmentsX, int segmentsY, bool attachByWidth = true);
+
+ void SetInvMasses(float invMass);
+ void SetInvMassesFromDensity(float density); // Todo
+
+ nv::cloth::ClothMeshDesc GetClothMeshDesc();
+ SimpleMesh GetRenderMesh();
+};
diff --git a/NvCloth/samples/SampleBase/utils/JobManager.cpp b/NvCloth/samples/SampleBase/utils/JobManager.cpp
new file mode 100644
index 0000000..093db60
--- /dev/null
+++ b/NvCloth/samples/SampleBase/utils/JobManager.cpp
@@ -0,0 +1,136 @@
+/*
+* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
+*
+* NVIDIA CORPORATION and its licensors retain all intellectual property
+* and proprietary rights in and to this software, related documentation
+* and any modifications thereto. Any use, reproduction, disclosure or
+* distribution of this software and related documentation without an express
+* license agreement from NVIDIA CORPORATION is strictly prohibited.
+*/
+
+#include "JobManager.h"
+#define _USE_MATH_DEFINES
+#include <math.h>
+#include <NvCloth/Solver.h>
+
+void Job::Initialize(JobManager* parent, std::function<void(Job*)> function, int refcount)
+{
+ mFunction = function;
+ mParent = parent;
+ Reset(refcount);
+}
+
+Job::Job(const Job& job)
+{
+ mFunction = job.mFunction;
+ mParent = job.mParent;
+ mRefCount.store(job.mRefCount);
+ mFinished = job.mFinished;
+}
+
+void Job::Reset(int refcount)
+{
+ mRefCount = refcount;
+ mFinished = false;
+}
+
+void Job::Execute()
+{
+ if (mFunction)
+ mFunction(this);
+ else
+ ExecuteInternal();
+
+ mFinishedLock.lock();
+ mFinished = true;
+ mFinishedLock.unlock();
+ mFinishedEvent.notify_one();
+}
+
+void Job::AddReference()
+{
+ mRefCount++;
+}
+void Job::RemoveReference()
+{
+ if (0 == --mRefCount)
+ {
+ mParent->Submit(this);
+ }
+}
+
+void Job::Wait()
+{
+ std::unique_lock<std::mutex> lock(mFinishedLock);
+ mFinishedEvent.wait(lock, [this](){return mFinished;} );
+ return;
+}
+
+void JobManager::WorkerEntryPoint(JobManager* parrent)
+{
+ while (true)
+ {
+ Job* job;
+ {
+ std::unique_lock<std::mutex> lock(parrent->mJobQueueLock);
+ while (parrent->mJobQueue.size() == 0 && !parrent->mQuit)
+ parrent->mJobQueueEvent.wait(lock);
+
+ if (parrent->mQuit)
+ return;
+
+ job = parrent->mJobQueue.front();
+ parrent->mJobQueue.pop();
+ }
+ job->Execute();
+ }
+}
+
+void JobManager::Submit(Job* job)
+{
+ mJobQueueLock.lock();
+ mJobQueue.push(job);
+ mJobQueueLock.unlock();
+ mJobQueueEvent.notify_one();
+}
+
+void MultithreadedSolverHelper::Initialize(nv::cloth::Solver* solver, JobManager* jobManager)
+{
+ mSolver = solver;
+ mJobManager = jobManager;
+ mEndSimulationJob.Initialize(mJobManager, [this](Job*) {
+ mSolver->endSimulation();
+ });
+
+ mStartSimulationJob.Initialize(mJobManager, [this](Job*) {
+ mSolver->beginSimulation(mDt);
+ for(int j = 0; j < mSolver->getSimulationChunkCount(); j++)
+ mSimulationChunkJobs[j].RemoveReference();
+ });
+}
+
+void MultithreadedSolverHelper::StartSimulation(float dt)
+{
+ mDt = dt;
+
+ if (mSolver->getSimulationChunkCount() != mSimulationChunkJobs.size())
+ {
+ mSimulationChunkJobs.resize(mSolver->getSimulationChunkCount(), Job());
+ for (int j = 0; j < mSolver->getSimulationChunkCount(); j++)
+ mSimulationChunkJobs[j].Initialize(mJobManager, [this, j](Job*) {mSolver->simulateChunk(j); mEndSimulationJob.RemoveReference(); });
+ }
+ else
+ {
+ for (int j = 0; j < mSolver->getSimulationChunkCount(); j++)
+ mSimulationChunkJobs[j].Reset();
+ }
+ mStartSimulationJob.Reset();
+ mEndSimulationJob.Reset(mSolver->getSimulationChunkCount());
+ mStartSimulationJob.RemoveReference();
+
+}
+
+void MultithreadedSolverHelper::WaitForSimulation()
+{
+ mEndSimulationJob.Wait();
+} \ No newline at end of file
diff --git a/NvCloth/samples/SampleBase/utils/JobManager.h b/NvCloth/samples/SampleBase/utils/JobManager.h
new file mode 100644
index 0000000..648fa33
--- /dev/null
+++ b/NvCloth/samples/SampleBase/utils/JobManager.h
@@ -0,0 +1,175 @@
+/*
+* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
+*
+* NVIDIA CORPORATION and its licensors retain all intellectual property
+* and proprietary rights in and to this software, related documentation
+* and any modifications thereto. Any use, reproduction, disclosure or
+* distribution of this software and related documentation without an express
+* license agreement from NVIDIA CORPORATION is strictly prohibited.
+*/
+
+#pragma once
+
+#include <foundation/PxErrorCallback.h>
+#include <regex>
+#include "CallbackImplementations.h"
+#include <mutex>
+#include <thread>
+#include <condition_variable>
+
+#include <foundation/PxVec4.h>
+#include <foundation/PxVec3.h>
+#include <vector>
+#include <queue>
+#include <atomic>
+
+#include <task/PxTaskManager.h>
+#include <task/PxTask.h>
+
+namespace nv
+{
+namespace cloth
+{
+class Solver;
+}
+}
+
+///Dummy task that can be used as end node in a task graph.
+class DummyTask : public physx::PxTask
+{
+public:
+ DummyTask() { mFinished = false; mTm = nullptr; }
+ DummyTask(physx::PxTaskManager* tm) { mFinished = false; mTm = tm; mTm->submitUnnamedTask(*this); }
+ ~DummyTask() { mTm = nullptr; } //Way to catch race conditions. Will usually crash on nullptr if the task gets deleted before the taskmanager is done with it.
+
+ virtual void run() override { }
+ virtual void release() override { physx::PxTask::release(); mFinished = true; mWaitEvent.notify_all(); }
+ virtual const char* getName() const override { return "DummyTask"; }
+
+ void Reset(physx::PxTaskManager* tm) { mFinished = false; mTm = tm; mTm->submitUnnamedTask(*this); }
+
+ ///Use Wait to block the calling thread until this task is finished and save to delete
+ void Wait()
+ {
+ std::mutex eventLock;
+ std::unique_lock<std::mutex> lock(eventLock);
+ while (!mFinished) { mWaitEvent.wait(lock); }
+ }
+
+private:
+ std::condition_variable mWaitEvent;
+ bool mFinished;
+};
+
+class CpuDispatcher : public physx::PxCpuDispatcher
+{
+ virtual void submitTask(physx::PxBaseTask& task)
+ {
+ task.run();
+ task.release();
+ }
+ virtual uint32_t getWorkerCount() const { return 1; }
+};
+
+
+class JobManager;
+class Job
+{
+public:
+ Job() = default;
+ Job(const Job&);
+ void Initialize(JobManager* parent, std::function<void(Job*)> function = std::function<void(Job*)>(), int refcount = 1);
+ void Reset(int refcount = 1); //Call this before reusing a job that doesn't need to be reinitialized
+ void Execute();
+ void AddReference();
+ void RemoveReference();
+ void Wait(); //Block until job is finished
+private:
+ virtual void ExecuteInternal() {}
+
+ std::function<void(Job*)> mFunction;
+ JobManager* mParent;
+ std::atomic_int mRefCount;
+
+ bool mFinished;
+ std::mutex mFinishedLock;
+ std::condition_variable mFinishedEvent;
+};
+
+class JobManager
+{
+public:
+ JobManager()
+ {
+ mWorkerCount = 8;
+ mWorkerThreads = new std::thread[mWorkerCount];
+ mQuit = false;
+
+ for(int i = 0; i < mWorkerCount; i++)
+ mWorkerThreads[i] = std::thread(JobManager::WorkerEntryPoint, this);
+ }
+ ~JobManager()
+ {
+ if(!mQuit)
+ Quit();
+ }
+
+ void Quit()
+ {
+ std::unique_lock<std::mutex> lock(mJobQueueLock);
+ mQuit = true;
+ lock.unlock();
+ mJobQueueEvent.notify_all();
+ for(int i = 0; i < mWorkerCount; i++)
+ {
+ mWorkerThreads[i].join();
+ }
+ delete[] mWorkerThreads;
+ }
+
+ template <int count, typename F>
+ void ParallelLoop(F const& function)
+ {
+ /*for(int i = 0; i < count; i++)
+ function(i);*/
+ Job finalJob;
+ finalJob.Initialize(this, std::function<void(Job*)>(), count);
+ Job jobs[count];
+ for(int j = 0; j < count; j++)
+ {
+ jobs[j].Initialize(this, [j, &finalJob, function](Job*) {function(j); finalJob.RemoveReference(); });
+ jobs[j].RemoveReference();
+ }
+ finalJob.Wait();
+ }
+
+ static void WorkerEntryPoint(JobManager* parrent);
+private:
+ friend class Job;
+ void Submit(Job* job);
+
+ int mWorkerCount;
+ std::thread* mWorkerThreads;
+
+ std::mutex mJobQueueLock;
+ std::queue<Job*> mJobQueue;
+ std::condition_variable mJobQueueEvent;
+ bool mQuit;
+};
+
+class MultithreadedSolverHelper
+{
+public:
+ void Initialize(nv::cloth::Solver* solver, JobManager* jobManager);
+ void StartSimulation(float dt);
+ void WaitForSimulation();
+private:
+ Job mStartSimulationJob;
+ Job mEndSimulationJob;
+ std::vector<Job> mSimulationChunkJobs;
+
+ float mDt;
+
+ nv::cloth::Solver* mSolver;
+ JobManager* mJobManager;
+};
diff --git a/NvCloth/samples/SampleBase/utils/SampleProfiler.cpp b/NvCloth/samples/SampleBase/utils/SampleProfiler.cpp
new file mode 100644
index 0000000..0438b06
--- /dev/null
+++ b/NvCloth/samples/SampleBase/utils/SampleProfiler.cpp
@@ -0,0 +1,223 @@
+/*
+* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
+*
+* NVIDIA CORPORATION and its licensors retain all intellectual property
+* and proprietary rights in and to this software, related documentation
+* and any modifications thereto. Any use, reproduction, disclosure or
+* distribution of this software and related documentation without an express
+* license agreement from NVIDIA CORPORATION is strictly prohibited.
+*/
+
+
+#include "SampleProfiler.h"
+#include <map>
+#include <iostream>
+#include <fstream>
+#include <stack>
+
+using namespace std::chrono;
+
+struct ProfileData
+{
+ steady_clock::time_point start;
+
+ microseconds time;
+ microseconds prevTime;
+ microseconds maxTime;
+ uint32_t calls;
+ uint32_t prevCalls;
+
+ ProfileData() : time(0), prevTime(0), maxTime(0), calls(0), prevCalls(0)
+ {}
+};
+
+struct Node
+{
+ ProfileData data;
+ std::map<const char*, Node> childs;
+ Node* parent;
+};
+
+static std::map<const char*, Node> s_roots;
+static Node* s_currentNode;
+static bool s_beginEndMismatch;
+static microseconds s_overhead;
+static microseconds s_prevOverhead;
+
+void SampleProfilerInit()
+{
+ s_roots.clear();
+ s_currentNode = nullptr;
+ s_beginEndMismatch = false;
+ s_overhead = microseconds();
+}
+
+void SampleProfilerBegin(const char* name)
+{
+ auto start = steady_clock::now();
+ {
+ Node* parent = s_currentNode;
+ if (s_currentNode == nullptr)
+ {
+ s_currentNode = &s_roots[name];
+ }
+ else
+ {
+ s_currentNode = &s_currentNode->childs[name];
+ }
+ s_currentNode->parent = parent;
+ s_currentNode->data.calls++;
+ s_currentNode->data.start = steady_clock::now();
+ }
+ s_overhead += duration_cast<microseconds>(steady_clock::now() - start);
+}
+
+void SampleProfilerEnd()
+{
+ auto start = steady_clock::now();
+ {
+ if (s_currentNode)
+ {
+ auto& data = s_currentNode->data;
+ data.time += duration_cast<microseconds>(steady_clock::now() - data.start);
+ data.maxTime = data.time > data.maxTime ? data.time : data.maxTime;
+ s_currentNode = s_currentNode->parent;
+ }
+ else
+ {
+ s_beginEndMismatch = true;
+ }
+ }
+ s_overhead += duration_cast<microseconds>(steady_clock::now() - start);
+}
+
+struct SampleProfilerTreeIteratorImpl final : public SampleProfilerTreeIterator
+{
+ struct StackNode
+ {
+ Node* node;
+ const char* name;
+ };
+
+ SampleProfilerTreeIteratorImpl(std::map<const char*, Node>& roots)
+ {
+ for (auto& root : roots)
+ {
+ m_stack.emplace(StackNode { &root.second, root.first });
+ }
+
+ next();
+ }
+
+ virtual const Data* data() const override
+ {
+ return m_valid ? &m_data : nullptr;
+ }
+
+ Node* node()
+ {
+ return m_node;
+ }
+
+ virtual bool isDone() const
+ {
+ return !m_valid;
+ }
+
+ virtual void next()
+ {
+ if (!m_stack.empty())
+ {
+ auto& e = m_stack.top();
+ m_stack.pop();
+ m_node = e.node;
+ m_data.depth = 0;
+ m_data.hash = (uint64_t)m_node;
+ for (const Node* p = m_node; p != nullptr; p = p->parent)
+ {
+ m_data.depth++;
+ }
+ m_data.name = e.name;
+ m_data.calls = m_node->data.prevCalls;
+ m_data.time = m_node->data.prevTime;
+ m_data.maxTime = m_node->data.maxTime;
+ m_data.hasChilds = !m_node->childs.empty();
+
+ for (auto it = m_node->childs.rbegin(); it != m_node->childs.rend(); ++it)
+ {
+ m_stack.emplace(StackNode { &(*it).second, (*it).first });
+ }
+ m_valid = true;
+ }
+ else
+ {
+ m_valid = false;
+ }
+ }
+
+ virtual void release()
+ {
+ delete this;
+ }
+
+ bool m_valid;
+ Data m_data;
+ Node* m_node;
+ std::stack<StackNode > m_stack;
+};
+
+void SampleProfilerReset()
+{
+ for (SampleProfilerTreeIteratorImpl it(s_roots); !it.isDone(); it.next())
+ {
+ auto& data = it.node()->data;
+ data.prevTime = data.time;
+ data.prevCalls = data.calls;
+ data.time = microseconds();
+ data.calls = 0;
+ }
+ s_currentNode = nullptr;
+ s_beginEndMismatch = false;
+ s_prevOverhead = s_overhead;
+ s_overhead = microseconds();
+}
+
+bool SampleProfilerIsValid()
+{
+ return !s_beginEndMismatch;
+}
+
+microseconds SampleProfilerGetOverhead()
+{
+ return s_prevOverhead;
+}
+
+SampleProfilerTreeIterator* SampleProfilerCreateTreeIterator()
+{
+ return SampleProfilerIsValid() ? new SampleProfilerTreeIteratorImpl(s_roots) : nullptr;
+}
+
+void SampleProfilerDumpToFile(const char* path)
+{
+ std::ofstream myfile(path, std::ios_base::out);
+ if (myfile.is_open())
+ {
+ if (s_beginEndMismatch)
+ {
+ myfile << "Error: Begin/End Mismatch.\n";
+ }
+ else
+ {
+ myfile << "[Root]\n";
+ for(SampleProfilerTreeIteratorImpl it(s_roots); !it.isDone(); it.next())
+ {
+ auto data = it.data();
+ for (uint32_t i = 0; i < data->depth; ++i)
+ myfile << "\t";
+ myfile << data->name << " --> calls: " << data->calls << ", total: " << data->time.count() * 0.001 << "ms\n";
+ }
+ }
+
+ myfile.close();
+ }
+}
diff --git a/NvCloth/samples/SampleBase/utils/SampleProfiler.h b/NvCloth/samples/SampleBase/utils/SampleProfiler.h
new file mode 100644
index 0000000..af0002c
--- /dev/null
+++ b/NvCloth/samples/SampleBase/utils/SampleProfiler.h
@@ -0,0 +1,79 @@
+/*
+* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
+*
+* NVIDIA CORPORATION and its licensors retain all intellectual property
+* and proprietary rights in and to this software, related documentation
+* and any modifications thereto. Any use, reproduction, disclosure or
+* distribution of this software and related documentation without an express
+* license agreement from NVIDIA CORPORATION is strictly prohibited.
+*/
+
+#ifndef SAMPLEPROFILER_H
+#define SAMPLEPROFILER_H
+
+#include <chrono>
+
+#if NV_PROFILE
+
+void SampleProfilerInit();
+void SampleProfilerBegin(const char* name);
+void SampleProfilerEnd();
+void SampleProfilerReset();
+
+struct SampleProfilerScoped
+{
+ SampleProfilerScoped(const char* name)
+ {
+ SampleProfilerBegin(name);
+ }
+
+ ~SampleProfilerScoped()
+ {
+ SampleProfilerEnd();
+ }
+};
+
+#define PROFILER_INIT() SampleProfilerInit()
+#define PROFILER_BEGIN(x) SampleProfilerBegin(x)
+#define PROFILER_END() SampleProfilerEnd()
+#define PROFILER_SCOPED(x) SampleProfilerScoped __scopedProfiler__(x)
+#define PROFILER_SCOPED_FUNCTION() SampleProfilerScoped __scopedProfiler__(__FUNCTION__)
+#define PROFILER_RESET() SampleProfilerReset()
+
+#else
+
+#define PROFILER_INIT()
+#define PROFILER_BEGIN(x)
+#define PROFILER_END()
+#define PROFILER_SCOPED(x)
+#define PROFILER_SCOPED_FUNCTION()
+#define PROFILER_RESET()
+
+#endif
+
+void SampleProfilerDumpToFile(const char* path);
+bool SampleProfilerIsValid();
+std::chrono::microseconds SampleProfilerGetOverhead();
+
+struct SampleProfilerTreeIterator
+{
+ struct Data
+ {
+ uint64_t hash;
+ const char* name;
+ bool hasChilds;
+ uint32_t depth;
+ std::chrono::microseconds time;
+ std::chrono::microseconds maxTime;
+ uint32_t calls;
+ };
+
+ virtual const Data* data() const = 0;
+ virtual bool isDone() const = 0;
+ virtual void next() = 0;
+ virtual void release() = 0;
+};
+
+SampleProfilerTreeIterator* SampleProfilerCreateTreeIterator();
+
+#endif //SAMPLEPROFILER_H \ No newline at end of file
diff --git a/NvCloth/samples/SampleBase/utils/SampleTime.h b/NvCloth/samples/SampleBase/utils/SampleTime.h
new file mode 100644
index 0000000..eac895d
--- /dev/null
+++ b/NvCloth/samples/SampleBase/utils/SampleTime.h
@@ -0,0 +1,58 @@
+/*
+* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
+*
+* NVIDIA CORPORATION and its licensors retain all intellectual property
+* and proprietary rights in and to this software, related documentation
+* and any modifications thereto. Any use, reproduction, disclosure or
+* distribution of this software and related documentation without an express
+* license agreement from NVIDIA CORPORATION is strictly prohibited.
+*/
+
+#ifndef SAMPLE_TIME_H
+#define SAMPLE_TIME_H
+
+#include <stdint.h>
+
+class Time
+{
+public:
+ Time() : m_lastTickCount(getTimeTicks()) {}
+
+ double Time::getElapsedSeconds()
+ {
+ const int64_t lastTickCount = m_lastTickCount;
+ m_lastTickCount = getTimeTicks();
+ return (m_lastTickCount - lastTickCount) * s_secondsPerTick;
+ }
+
+ double Time::peekElapsedSeconds() const
+ {
+ return (getTimeTicks() - m_lastTickCount) * s_secondsPerTick;
+ }
+
+ double Time::getLastTime() const
+ {
+ return m_lastTickCount * s_secondsPerTick;
+ }
+
+private:
+ static double getTickDuration()
+ {
+ LARGE_INTEGER a;
+ QueryPerformanceFrequency(&a);
+ return 1.0 / (double)a.QuadPart;
+ }
+
+ int64_t getTimeTicks() const
+ {
+ LARGE_INTEGER a;
+ QueryPerformanceCounter(&a);
+ return a.QuadPart;
+ }
+
+ int64_t m_lastTickCount;
+ static const double s_secondsPerTick;
+};
+
+
+#endif \ No newline at end of file
diff --git a/NvCloth/samples/SampleBase/utils/UIHelpers.h b/NvCloth/samples/SampleBase/utils/UIHelpers.h
new file mode 100644
index 0000000..5c4dfee
--- /dev/null
+++ b/NvCloth/samples/SampleBase/utils/UIHelpers.h
@@ -0,0 +1,56 @@
+/*
+* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
+*
+* NVIDIA CORPORATION and its licensors retain all intellectual property
+* and proprietary rights in and to this software, related documentation
+* and any modifications thereto. Any use, reproduction, disclosure or
+* distribution of this software and related documentation without an express
+* license agreement from NVIDIA CORPORATION is strictly prohibited.
+*/
+
+#ifndef UI_HELPERS_H
+#define UI_HELPERS_H
+
+#include "imgui.h"
+#include "PxVec3.h"
+
+
+static void ImGui_DragFloat3Dir(const char* label, float v[3])
+{
+ if (ImGui::Button("Normalize"))
+ {
+ ((physx::PxVec3*)v)->normalize();
+ }
+ ImGui::SameLine();
+ ImGui::DragFloat3(label, v);
+};
+
+
+#define IM_ARRAYSIZE(_ARR) ((int)(sizeof(_ARR)/sizeof(*_ARR)))
+
+template<int valuesCount = 90>
+class PlotLinesInstance
+{
+public:
+ PlotLinesInstance()
+ {
+ memset(m_values, 0, sizeof(float) * valuesCount);
+ }
+
+ void plot(const char* label, float newValue, const char* overlay_text, float scale_min = FLT_MAX, float scale_max = FLT_MAX, ImVec2 graph_size = ImVec2(0, 80))
+ {
+ for (; ImGui::GetTime() > m_time + 1.0f / 60.0f; m_time += 1.0f / 60.0f)
+ {
+ m_values[m_offset] = newValue;
+ m_offset = (m_offset + 1) % valuesCount;
+ }
+ ImGui::PlotLines(label, m_values, valuesCount, m_offset, overlay_text, scale_min, scale_max, graph_size);
+ }
+
+private:
+ float m_values[valuesCount];
+ int m_offset;
+ float m_time = ImGui::GetTime();
+};
+
+#endif //UI_HELPERS_H \ No newline at end of file
diff --git a/NvCloth/samples/SampleBase/utils/Utils.cpp b/NvCloth/samples/SampleBase/utils/Utils.cpp
new file mode 100644
index 0000000..a271137
--- /dev/null
+++ b/NvCloth/samples/SampleBase/utils/Utils.cpp
@@ -0,0 +1,13 @@
+#include "Utils.h"
+
+#include <string>
+#include <cstdarg>
+
+HRESULT messagebox_printf(const char* caption, UINT mb_type, const char* format, ...)
+{
+ va_list args;
+ va_start(args, format);
+ char formatted_text[512];
+ _vsnprintf(formatted_text, 512, format, args);
+ return MessageBoxA(nullptr, formatted_text, caption, mb_type);
+}
diff --git a/NvCloth/samples/SampleBase/utils/Utils.h b/NvCloth/samples/SampleBase/utils/Utils.h
new file mode 100644
index 0000000..5e84e8e
--- /dev/null
+++ b/NvCloth/samples/SampleBase/utils/Utils.h
@@ -0,0 +1,89 @@
+#ifndef UTILS_H
+#define UTILS_H
+
+#include <DeviceManager.h>
+#include <assert.h>
+
+
+///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+// MACROS
+///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+
+#ifndef V_RETURN
+#define V_RETURN(x) \
+ { \
+ hr = (x); \
+ if(FAILED(hr)) \
+ { \
+ return hr; \
+ } \
+ }
+#endif
+
+#ifndef V
+#define V(x) \
+ { \
+ HRESULT hr = (x); \
+ _ASSERT(SUCCEEDED(hr)); \
+ }
+#endif
+
+#ifndef SAFE_RELEASE
+#define SAFE_RELEASE(p) \
+ { \
+ if(p) \
+ { \
+ (p)->Release(); \
+ (p) = NULL; \
+ } \
+ }
+#endif
+
+#ifndef SAFE_DELETE
+#define SAFE_DELETE(p) \
+ { \
+ if(p) \
+ { \
+ delete (p); \
+ (p) = NULL; \
+ } \
+ }
+#endif
+
+#define ASSERT_PRINT(cond, format, ...) \
+ if(!(cond)) \
+ { \
+ messagebox_printf("Assertion Failed!", MB_OK | MB_ICONERROR, #cond "\n" format, __VA_ARGS__); \
+ assert(cond); \
+ }
+
+HRESULT messagebox_printf(const char* caption, UINT mb_type, const char* format, ...);
+
+
+
+///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+
+static const char* strext(const char* str)
+{
+ const char* ext = NULL; // by default no extension found!
+ while (str)
+ {
+ str = strchr(str, '.');
+ if (str)
+ {
+ str++;
+ ext = str;
+ }
+ }
+ return ext;
+}
+
+static inline float lerp(float a, float b, float t) { return a + (b - a) * t; }
+
+///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+
+#endif \ No newline at end of file