PolyUWPCore.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. #include "polycode/core/PolyUWPCore.h"
  2. #include <ppltasks.h>
  3. using namespace concurrency;
  4. using namespace DirectX;
  5. using namespace Microsoft::WRL;
  6. using namespace Windows::Foundation;
  7. UWPCore::UWPCore(PolycodeView *view, int xRes, int yRes, bool fullScreen, bool vSync, int aaLevel, int anisotropyLevel, int frameRate, int monitorIndex, bool retinaSupport)
  8. : Core(xRes, yRes, fullScreen, vSync, aaLevel, anisotropyLevel, frameRate, monitorIndex) {
  9. m_Window = view->window;
  10. renderer = new Renderer();
  11. graphicsInterface = new DX11GraphicsInterface();
  12. renderer->setGraphicsInterface(this, graphicsInterface);
  13. services->setRenderer(renderer);
  14. setVideoMode(xRes, yRes, fullScreen, vSync, aaLevel, anisotropyLevel, retinaSupport);
  15. }
  16. UWPCore::~UWPCore() {
  17. }
  18. void UWPCore::Render() {
  19. renderer->beginFrame();
  20. services->Render(Polycode::Rectangle(0, 0, 640, 480));
  21. renderer->endFrame();
  22. }
  23. bool UWPCore::systemUpdate() {
  24. if (!running) {
  25. return false;
  26. }
  27. doSleep();
  28. updateCore();
  29. return running;
  30. }
  31. void UWPCore::setCursor(int cursorType) {
  32. }
  33. void launchThread(Threaded *target) {
  34. target->runThread();
  35. target->scheduledForRemoval = true;
  36. }
  37. void UWPCore::createThread(Threaded * target) {
  38. Core::createThread(target);
  39. std::thread *thread = new std::thread(launchThread, target);
  40. }
  41. void UWPCore::lockMutex(CoreMutex *mutex) {
  42. ((UWPCoreMutex*)mutex)->mutex.lock();
  43. }
  44. void UWPCore::unlockMutex(CoreMutex *mutex) {
  45. ((UWPCoreMutex*)mutex)->mutex.unlock();
  46. }
  47. CoreMutex *UWPCore::createMutex() {
  48. UWPCoreMutex *mutex = new UWPCoreMutex();
  49. return mutex;
  50. }
  51. void UWPCore::copyStringToClipboard(const String& str) {
  52. }
  53. String UWPCore::getClipboardString() {
  54. return "";
  55. }
  56. void UWPCore::createFolder(const String& folderPath) {
  57. }
  58. void UWPCore::copyDiskItem(const String& itemPath, const String& destItemPath) {
  59. }
  60. void UWPCore::moveDiskItem(const String& itemPath, const String& destItemPath) {
  61. }
  62. void UWPCore::removeDiskItem(const String& itemPath) {
  63. }
  64. String UWPCore::openFolderPicker() {
  65. return "";
  66. }
  67. std::vector<String> UWPCore::openFilePicker(std::vector<CoreFileExtension> extensions, bool allowMultiple) {
  68. std::vector<String> ret;
  69. return ret;
  70. }
  71. String UWPCore::saveFilePicker(std::vector<CoreFileExtension> extensions) {
  72. return "";
  73. }
  74. void UWPCore::handleVideoModeChange(VideoModeChangeInfo *modeInfo) {
  75. if (m_d3dContext == nullptr) {
  76. D3D_FEATURE_LEVEL featureLevels[] = {
  77. D3D_FEATURE_LEVEL_11_1,
  78. D3D_FEATURE_LEVEL_11_0,
  79. D3D_FEATURE_LEVEL_10_1,
  80. D3D_FEATURE_LEVEL_10_0,
  81. D3D_FEATURE_LEVEL_9_3,
  82. D3D_FEATURE_LEVEL_9_2,
  83. D3D_FEATURE_LEVEL_9_1
  84. };
  85. ComPtr<ID3D11Device> device;
  86. ComPtr<ID3D11DeviceContext> context;
  87. HRESULT hr = D3D11CreateDevice(
  88. nullptr, // Specify nullptr to use the default adapter.
  89. D3D_DRIVER_TYPE_HARDWARE, // Create a device using the hardware graphics driver.
  90. 0, // Should be 0 unless the driver is D3D_DRIVER_TYPE_SOFTWARE.
  91. 0, // Set debug and Direct2D compatibility flags.
  92. featureLevels, // List of feature levels this app can support.
  93. ARRAYSIZE(featureLevels), // Size of the list above.
  94. D3D11_SDK_VERSION, // Always set this to D3D11_SDK_VERSION for Windows Store apps.
  95. &device, // Returns the Direct3D device created.
  96. &m_d3dFeatureLevel, // Returns feature level of device created.
  97. &context // Returns the device immediate context.
  98. );
  99. if (FAILED(hr)) {
  100. D3D11CreateDevice(
  101. nullptr,
  102. D3D_DRIVER_TYPE_WARP, // Create a WARP device instead of a hardware device.
  103. 0,
  104. 0,
  105. featureLevels,
  106. ARRAYSIZE(featureLevels),
  107. D3D11_SDK_VERSION,
  108. &device,
  109. &m_d3dFeatureLevel,
  110. &context
  111. );
  112. }
  113. device.As(&m_d3dDevice);
  114. context.As(&m_d3dContext);
  115. }
  116. // ---------------- window stuff
  117. // Clear the previous window size specific context.
  118. ID3D11RenderTargetView* nullViews[] = { nullptr };
  119. m_d3dContext->OMSetRenderTargets(ARRAYSIZE(nullViews), nullViews, nullptr);
  120. m_d3dRenderTargetView = nullptr;
  121. m_d3dDepthStencilView = nullptr;
  122. m_d3dContext->Flush();
  123. if (m_swapChain != nullptr) {
  124. HRESULT hr = m_swapChain->ResizeBuffers(
  125. 2,
  126. modeInfo->xRes,
  127. modeInfo->yRes,
  128. DXGI_FORMAT_B8G8R8A8_UNORM,
  129. 0
  130. );
  131. } else {
  132. DXGI_SWAP_CHAIN_DESC1 swapChainDesc = { 0 };
  133. swapChainDesc.Width = lround(modeInfo->xRes);
  134. swapChainDesc.Height = lround(modeInfo->yRes);
  135. swapChainDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM; // This is the most common swap chain format.
  136. swapChainDesc.Stereo = false;
  137. swapChainDesc.SampleDesc.Count = 1; // Don't use multi-sampling.
  138. swapChainDesc.SampleDesc.Quality = 0;
  139. swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
  140. swapChainDesc.BufferCount = 2; // Use double-buffering to minimize latency.
  141. swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL; // All Windows Store apps must use this SwapEffect.
  142. swapChainDesc.Flags = 0;
  143. swapChainDesc.Scaling = DXGI_SCALING_NONE;
  144. swapChainDesc.AlphaMode = DXGI_ALPHA_MODE_IGNORE;
  145. // This sequence obtains the DXGI factory that was used to create the Direct3D device above.
  146. ComPtr<IDXGIDevice3> dxgiDevice;
  147. m_d3dDevice.As(&dxgiDevice);
  148. ComPtr<IDXGIAdapter> dxgiAdapter;
  149. dxgiDevice->GetAdapter(&dxgiAdapter);
  150. ComPtr<IDXGIFactory2> dxgiFactory;
  151. dxgiAdapter->GetParent(IID_PPV_ARGS(&dxgiFactory));
  152. dxgiFactory->CreateSwapChainForCoreWindow(
  153. m_d3dDevice.Get(),
  154. m_Window,
  155. &swapChainDesc,
  156. nullptr,
  157. &m_swapChain
  158. );
  159. dxgiDevice->SetMaximumFrameLatency(1);
  160. }
  161. // Create a render target view of the swap chain back buffer.
  162. ComPtr<ID3D11Texture2D> backBuffer;
  163. m_swapChain->GetBuffer(0, IID_PPV_ARGS(&backBuffer));
  164. m_d3dDevice->CreateRenderTargetView(
  165. backBuffer.Get(),
  166. nullptr,
  167. &m_d3dRenderTargetView
  168. );
  169. // Create a depth stencil view for use with 3D rendering if needed.
  170. CD3D11_TEXTURE2D_DESC depthStencilDesc(
  171. DXGI_FORMAT_D24_UNORM_S8_UINT,
  172. modeInfo->xRes,
  173. modeInfo->yRes,
  174. 1, // This depth stencil view has only one texture.
  175. 1, // Use a single mipmap level.
  176. D3D11_BIND_DEPTH_STENCIL
  177. );
  178. ComPtr<ID3D11Texture2D> depthStencil;
  179. m_d3dDevice->CreateTexture2D(
  180. &depthStencilDesc,
  181. nullptr,
  182. &depthStencil
  183. );
  184. CD3D11_DEPTH_STENCIL_VIEW_DESC depthStencilViewDesc(D3D11_DSV_DIMENSION_TEXTURE2D);
  185. m_d3dDevice->CreateDepthStencilView(
  186. depthStencil.Get(),
  187. &depthStencilViewDesc,
  188. &m_d3dDepthStencilView
  189. );
  190. // Set the 3D rendering viewport to target the entire window.
  191. CD3D11_VIEWPORT m_screenViewport = CD3D11_VIEWPORT(
  192. 0.0f,
  193. 0.0f,
  194. modeInfo->xRes,
  195. modeInfo->yRes
  196. );
  197. m_d3dContext->RSSetViewports(1, &m_screenViewport);
  198. graphicsInterface->setContext(m_d3dContext.Get());
  199. graphicsInterface->setViews(m_d3dRenderTargetView.Get(), m_d3dDepthStencilView.Get());
  200. }
  201. void UWPCore::flushRenderContext() {
  202. HRESULT hr = m_swapChain->Present(1, 0);
  203. m_d3dContext->DiscardView(m_d3dRenderTargetView.Get());
  204. m_d3dContext->DiscardView(m_d3dDepthStencilView.Get());
  205. }
  206. void UWPCore::resizeTo(int xRes, int yRes) {
  207. }
  208. void UWPCore::openURL(String url) {
  209. }
  210. unsigned int UWPCore::getTicks() {
  211. return 0;
  212. }
  213. String UWPCore::executeExternalCommand(String command, String args, String inDirectory) {
  214. return "";
  215. }
  216. bool UWPCore::systemParseFolder(const Polycode::String& pathString, bool showHidden, std::vector<OSFileEntry> &targetVector) {
  217. return false;
  218. }
  219. void Core::getScreenInfo(int *width, int *height, int *hz) {
  220. }