virtual bool getStatus() { // ID3DUserDefinedAnnotation::GetStatus doesn't work with the Graphics Diagnostics tools in Visual Studio 2013. #if defined(_DEBUG) && defined(ANGLE_ENABLE_WINDOWS_STORE) // In the Windows Store, we can use IDXGraphicsAnalysis. The call to GetDebugInterface1 only succeeds if the app is under capture. // This should only be called in DEBUG mode. // If an app links against DXGIGetDebugInterface1 in release mode then it will fail Windows Store ingestion checks. IDXGraphicsAnalysis* graphicsAnalysis; DXGIGetDebugInterface1(0, IID_PPV_ARGS(&graphicsAnalysis)); bool underCapture = (graphicsAnalysis != NULL); SafeRelease(graphicsAnalysis); return underCapture; #endif // Otherwise, we have to return true here. return true; }
void XApp::init() { //// Basic initialization if (initialized) return; initialized = true; if (!ovrRendering) ovrMirror = false; if (ovrRendering) vr.init(); if (appName.length() == 0) { // no app name specified - just use first one from iterator auto it = appMap.begin(); XAppBase *a = it->second; if (a != nullptr) { appName = it->first; Log("WARNING: xapp not specified, using this app: " << appName.c_str() << endl); } } //assert(appName.length() > 0); app = getApp(appName); if (app != nullptr) { //Log("initializing " << appName.c_str() << "\n"); SetWindowText(getHWND(), string2wstring(app->getWindowTitle())); } else { Log("ERROR: xapp not available " << appName.c_str() << endl); // throw assertion error in debug mode assert(app != nullptr); } #ifdef _DEBUG // Enable the D3D12 debug layer. { ComPtr<ID3D12Debug> debugController; if (SUCCEEDED(D3D12GetDebugInterface(IID_PPV_ARGS(&debugController)))) { debugController->EnableDebugLayer(); } HRESULT getAnalysis = DXGIGetDebugInterface1(0, __uuidof(pGraphicsAnalysis), reinterpret_cast<void**>(&pGraphicsAnalysis)); } #endif //// Viewport and Scissor /* D3D12_RECT rect; if (GetWindowRect(getHWND(), &rect)) { int width = rect.right - rect.left; int height = rect.bottom - rect.top; viewport.MinDepth = 0.0f; viewport.TopLeftX = 0.0f; viewport.TopLeftY = 0.0f; viewport.Width = static_cast<float>(width); viewport.Height = static_cast<float>(height); viewport.MaxDepth = 1.0f; scissorRect.left = 0; scissorRect.top = 0; scissorRect.right = static_cast<LONG>(width); scissorRect.bottom = static_cast<LONG>(height); vr.adaptViews(viewport, scissorRect); } */ //// Pipeline ComPtr<IDXGIFactory4> factory; ThrowIfFailed(CreateDXGIFactory2(0 #ifdef _DEBUG | DXGI_CREATE_FACTORY_DEBUG #endif , IID_PPV_ARGS(&factory))); if (warp) { ComPtr<IDXGIAdapter> warpAdapter; ThrowIfFailed(factory->EnumWarpAdapter(IID_PPV_ARGS(&warpAdapter))); ThrowIfFailed(D3D12CreateDevice( warpAdapter.Get(), D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&device) )); } else { ThrowIfFailed(D3D12CreateDevice( nullptr, D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&device) )); } // disable auto alt-enter fullscreen switch (does leave an unresponsive window during debug sessions) ThrowIfFailed(factory->MakeWindowAssociation(getHWND(), DXGI_MWA_NO_ALT_ENTER)); //IDXGIFactory4 *parentFactoryPtr = nullptr; //if (SUCCEEDED(swapChain->GetParent(__uuidof(IDXGIFactory4), (void **)&parentFactoryPtr))) { // parentFactoryPtr->MakeWindowAssociation(getHWND(), DXGI_MWA_NO_ALT_ENTER); // parentFactoryPtr->Release(); //} // Describe and create the command queue. D3D12_COMMAND_QUEUE_DESC queueDesc = {}; queueDesc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE; queueDesc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT; ThrowIfFailed(device->CreateCommandQueue(&queueDesc, IID_PPV_ARGS(&commandQueue))); commandQueue->SetName(L"commandQueue_xapp"); calcBackbufferSizeAndAspectRatio(); camera.aspectRatio = aspectRatio; if (ovrRendering) { camera.aspectRatio /= 2.0f; } camera.projectionTransform(); // Describe the swap chain. DXGI_SWAP_CHAIN_DESC swapChainDesc = {}; swapChainDesc.BufferCount = FrameCount; swapChainDesc.BufferDesc.Width = backbufferWidth; swapChainDesc.BufferDesc.Height = backbufferHeight; swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD; swapChainDesc.OutputWindow = getHWND(); swapChainDesc.SampleDesc.Count = 1; swapChainDesc.Windowed = TRUE; ComPtr<IDXGISwapChain> swapChain0; // we cannot use create IDXGISwapChain3 directly - create IDXGISwapChain, then call As() to map to IDXGISwapChain3 ThrowIfFailed(factory->CreateSwapChain( commandQueue.Get(), // Swap chain needs the queue so that it can force a flush on it. &swapChainDesc, &swapChain0 )); ThrowIfFailed(swapChain0.As(&swapChain)); //swapChain = nullptr; frameIndex = xapp().swapChain->GetCurrentBackBufferIndex(); if (ovrRendering) { vr.initD3D(); } // Create descriptor heaps. { // Describe and create a render target view (RTV) descriptor heap. D3D12_DESCRIPTOR_HEAP_DESC rtvHeapDesc = {}; rtvHeapDesc.NumDescriptors = FrameCount; rtvHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV; rtvHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE; ThrowIfFailed(device->CreateDescriptorHeap(&rtvHeapDesc, IID_PPV_ARGS(&rtvHeap))); rtvHeap->SetName(L"rtvHeap_xapp"); rtvDescriptorSize = device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV); } // Create frame resources. { CD3DX12_CPU_DESCRIPTOR_HANDLE rtvHandle(rtvHeap->GetCPUDescriptorHandleForHeapStart()); // Create a RTV for each frame. for (UINT n = 0; n < FrameCount; n++) { ThrowIfFailed(swapChain->GetBuffer(n, IID_PPV_ARGS(&renderTargets[n]))); device->CreateRenderTargetView(renderTargets[n].Get(), nullptr, rtvHandle); wstringstream s; s << L"renderTarget_xapp[" << n << "]"; renderTargets[n]->SetName(s.str().c_str()); rtvHandle.Offset(1, rtvDescriptorSize); //ThrowIfFailed(device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&commandAllocators[n]))); // Describe and create a depth stencil view (DSV) descriptor heap. D3D12_DESCRIPTOR_HEAP_DESC dsvHeapDesc = {}; dsvHeapDesc.NumDescriptors = 1; dsvHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_DSV; dsvHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE; ThrowIfFailed(device->CreateDescriptorHeap(&dsvHeapDesc, IID_PPV_ARGS(&dsvHeaps[n]))); } // Create the depth stencil view for each frame for (UINT n = 0; n < FrameCount; n++) { D3D12_DEPTH_STENCIL_VIEW_DESC depthStencilDesc = {}; depthStencilDesc.Format = DXGI_FORMAT_D32_FLOAT; depthStencilDesc.ViewDimension = D3D12_DSV_DIMENSION_TEXTURE2D; depthStencilDesc.Flags = D3D12_DSV_FLAG_NONE; D3D12_CLEAR_VALUE depthOptimizedClearValue = {}; depthOptimizedClearValue.Format = DXGI_FORMAT_D32_FLOAT; depthOptimizedClearValue.DepthStencil.Depth = 1.0f; depthOptimizedClearValue.DepthStencil.Stencil = 0; ThrowIfFailed(device->CreateCommittedResource( &CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT), D3D12_HEAP_FLAG_NONE, &CD3DX12_RESOURCE_DESC::Tex2D(DXGI_FORMAT_D32_FLOAT, backbufferWidth, backbufferHeight, 1, 0, 1, 0, D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL), D3D12_RESOURCE_STATE_DEPTH_WRITE, &depthOptimizedClearValue, IID_PPV_ARGS(&depthStencils[n]) )); //NAME_D3D12_OBJECT(m_depthStencil); device->CreateDepthStencilView(depthStencils[n].Get(), &depthStencilDesc, dsvHeaps[n]->GetCPUDescriptorHandleForHeapStart()); wstringstream s; s << L"depthStencil_xapp[" << n << "]"; depthStencils[n]->SetName(s.str().c_str()); } } //// Assets // Create an empty root signature. { CD3DX12_ROOT_SIGNATURE_DESC rootSignatureDesc; rootSignatureDesc.Init(0, nullptr, 0, nullptr, D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT); ComPtr<ID3DBlob> signature; ComPtr<ID3DBlob> error; ThrowIfFailed(D3D12SerializeRootSignature(&rootSignatureDesc, D3D_ROOT_SIGNATURE_VERSION_1, &signature, &error)); ThrowIfFailed(device->CreateRootSignature(0, signature->GetBufferPointer(), signature->GetBufferSize(), IID_PPV_ARGS(&rootSignature))); } // 11 on 12 device support UINT d3d11DeviceFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT; #if defined(_DEBUG) if (disableDX11Debug == false) { d3d11DeviceFlags |= D3D11_CREATE_DEVICE_DEBUG; } #endif ThrowIfFailed(D3D11On12CreateDevice( device.Get(), d3d11DeviceFlags, nullptr, 0, reinterpret_cast<IUnknown**>(commandQueue.GetAddressOf()), 1, 0, &d3d11Device, &d3d11DeviceContext, nullptr )); // Query the 11On12 device from the 11 device. ThrowIfFailed(d3d11Device.As(&d3d11On12Device)); // feature levels: we need DX 10.1 as minimum D3D_FEATURE_LEVEL out_level; array<D3D_FEATURE_LEVEL, 3> levels{ { D3D_FEATURE_LEVEL_11_1, D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_1 } }; //ID3D11DeviceContext* context = nullptr; UINT flags = disableDX11Debug ? 0 : D3D11_CREATE_DEVICE_DEBUG; ThrowIfFailed(D3D11CreateDevice(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, flags, &levels[0], (UINT)levels.size(), D3D11_SDK_VERSION, &reald3d11Device, &out_level, &reald3d11DeviceContext)); // 11 on 12 end camera.ovrCamera = true; if (!ovrRendering) camera.ovrCamera = false; gametime.init(1); // init to real time camera.setSpeed(1.0f); initPakFiles(); app->init(); app->update(); }