void App::RenderTerrain2( bool bReset, float fTime, float fElapsedTime ) { g_ResetTerrain = bReset; if (g_ResetTerrain) { g_ResetTerrain = false; DeformInitTerrain(context); } renderer->changeToMainFramebuffer(); ID3D11DeviceContext* pContext = GetApp()->GetContext(); ID3D11RenderTargetView* pBackBufferRTV; ID3D11DepthStencilView* pBackBufferDSV; pContext->OMGetRenderTargets(1, &pBackBufferRTV, &pBackBufferDSV); // Clear render target and the depth stencil float ClearColor[4] = { 1.0f, 0.0f, 1.0f, 1.0f }; // Purple to better spot terrain cracks (disable sky cube). //float ClearColor[4] = { 0.465f, 0.725f, 0.0f, 1.0f }; // NV green for colour-consistent illustrations. pContext->ClearRenderTargetView(pBackBufferRTV, ClearColor); pContext->ClearDepthStencilView(pBackBufferDSV, D3D11_CLEAR_DEPTH, 1.0, 0); // ¸®»çÀÌ¡½Ã Error! pContext->OMSetRenderTargets(1, &pBackBufferRTV, pBackBufferDSV); RenderToBackBuffer(renderer, fTime, fElapsedTime, NULL); }
static void DoRendering (const float* worldMatrix, const float* identityMatrix, float* projectionMatrix, const MyVertex* verts) { // Does actual rendering of a simple triangle #if SUPPORT_D3D11 // D3D11 case if (s_DeviceType == kUnityGfxRendererD3D11 && EnsureD3D11ResourcesAreCreated()) { ID3D11DeviceContext* ctx = NULL; g_D3D11Device->GetImmediateContext (&ctx); ID3D11RenderTargetView* pCurrentRenderTarget; ID3D11DepthStencilView* pCurrentDepthStencil; // Get the current render targets ctx->OMGetRenderTargets(1, &pCurrentRenderTarget, &pCurrentDepthStencil); ctx->OMSetRenderTargets(1, &g_pD3D11RenderTargetView, nullptr); /* // update native texture from code D3D11_TEXTURE2D_DESC desc; g_TexturePointer->GetDesc(&desc); unsigned char* data = new unsigned char[desc.Width*desc.Height * 4]; FillTextureFromCode(desc.Width, desc.Height, desc.Width * 4, data); ctx->UpdateSubresource(g_TexturePointer, 0, NULL, data, desc.Width * 4, 0); delete[] data; */ const float CLEAR_CLR[4] = { 1, 1, 0, 1 }; // Yellow ctx->ClearRenderTargetView(g_pD3D11RenderTargetView, CLEAR_CLR); // Restore the original render target ctx->OMSetRenderTargets(1, &pCurrentRenderTarget, pCurrentDepthStencil); // update constant buffer - just the world matrix in our case ctx->UpdateSubresource (g_D3D11CB, 0, NULL, worldMatrix, 64, 0); // set shaders ctx->VSSetConstantBuffers (0, 1, &g_D3D11CB); ctx->VSSetShader (g_D3D11VertexShader, NULL, 0); ctx->PSSetShader (g_D3D11PixelShader, NULL, 0); // update vertex buffer ctx->UpdateSubresource (g_D3D11VB, 0, NULL, verts, sizeof(verts[0])*3, 0); // set input assembler data and draw ctx->IASetInputLayout (g_D3D11InputLayout); ctx->IASetPrimitiveTopology (D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); UINT stride = sizeof(MyVertex); UINT offset = 0; ctx->IASetVertexBuffers (0, 1, &g_D3D11VB, &stride, &offset); ctx->Draw (3, 0); ctx->Release(); } #endif }
void CGUIWindowTestPatternDX::BeginRender() { ID3D11DeviceContext* pContext = g_Windowing.Get3D11Context(); ID3D11RenderTargetView* renderTarget; pContext->OMGetRenderTargets(1, &renderTarget, NULL); float color[4] = { 0.0f, 0.0f, 0.0f, 0.0f }; pContext->ClearRenderTargetView(renderTarget, color); renderTarget->Release(); }
HRESULT DxAssist::SetRenderTarget(DxRTDevice* RTDevice, uint w, uint h) { HRESULT hresult = E_FAIL; ID3D11Device* device = m_device; ID3D11DeviceContext* deviceContext = m_deviceContext; ID3D11RenderTargetView* poldRT = 0; ID3D11DepthStencilView* poldDS = 0; D3D11_VIEWPORT vp; const float clear[] = { 0, 0, 0, 90/255.0f}; //red,green,blue,alpha ASSERT(device); ASSERT(deviceContext); KGLOG_PROCESS_ERROR(RTDevice); KGLOG_PROCESS_ERROR(RTDevice->RTView); KGLOG_PROCESS_ERROR(RTDevice->RTDepthStencil); KGLOG_PROCESS_ERROR(RTDevice->RTDepthStencilBuf); deviceContext->OMGetRenderTargets(1, &poldRT, &poldDS ); deviceContext->OMSetRenderTargets(1, &RTDevice->RTView, RTDevice->RTDepthStencil); vp.TopLeftX = 0; vp.TopLeftY = 0; vp.Width = (float)w; vp.Height = (float)h; vp.MinDepth = 0.0f; vp.MaxDepth = 1.0f; deviceContext->RSSetViewports(1, &vp); deviceContext->ClearRenderTargetView(RTDevice->RTView, clear); deviceContext->ClearDepthStencilView(RTDevice->RTDepthStencil, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0); { RTSaveInfo info; info.RTView = poldRT; info.RTStencil = poldDS; s_RTSaveInfoStack.push(info); } hresult = S_OK; Exit0: return hresult; }
// Detour function that replaces the IDXGISwapChain::Present() API DllExport HRESULT __stdcall hook_DXGISwapChainPresent( IDXGISwapChain * This, UINT SyncInterval, UINT Flags ) { static int frame_interval; static LARGE_INTEGER initialTv, captureTv, freq; static int capture_initialized = 0; // int i; struct pooldata *data; struct vsource_frame *frame; // DXGI_SWAP_CHAIN_DESC pDESC; HRESULT hr = pDXGISwapChainPresent(This, SyncInterval, Flags); if(resolution_retrieved == 0) { if(DXGI_get_resolution(This) >= 0) { resolution_retrieved = 1; } return hr; } if(vsource_initialized == 0) { ga_error("video source not initialized.\n"); return hr; } This->GetDesc(&pDESC); pDXGI_FORMAT = pDESC.BufferDesc.Format; // extract screen format for sws_scale if(pDESC.BufferDesc.Width != game_width || pDESC.BufferDesc.Height != game_height) { ga_error("game width/height mismatched (%dx%d) != (%dx%d)\n", pDESC.BufferDesc.Width, pDESC.BufferDesc.Height, game_width, game_height); return hr; } // if (enable_server_rate_control && ga_hook_video_rate_control() < 0) return hr; if (dx_version == dx_none) { //bool check_result = FALSE; if (check_dx_device_version(This, IID_ID3D10Device)) { dx_version = dx_10; ga_error("[DXGISwapChain] DirectX 10\n"); } else if (check_dx_device_version(This, IID_ID3D10Device1)) { dx_version = dx_10_1; ga_error("[DXGISwapChain] DirectX 10.1\n"); } else if (check_dx_device_version(This, IID_ID3D11Device)) { dx_version = dx_11; ga_error("[DXGISwapChain] DirectX 11\n"); } } if (capture_initialized == 0) { frame_interval = 1000000/video_fps; // in the unif of us frame_interval++; QueryPerformanceFrequency(&freq); QueryPerformanceCounter(&initialTv); capture_initialized = 1; } else { QueryPerformanceCounter(&captureTv); } hr = 0; // d3d10 / d3d10.1 if (dx_version == dx_10 || dx_version == dx_10_1) { void *ppDevice; ID3D10Device *pDevice; //IUnknown *pDevice; if (dx_version == dx_10) { This->GetDevice(IID_ID3D10Device, &ppDevice); pDevice = (ID3D10Device *)ppDevice; } else if (dx_version == dx_10_1) { This->GetDevice(IID_ID3D10Device1, &ppDevice); pDevice = (ID3D10Device1 *)ppDevice; } else { OutputDebugString("Invalid DirectX version in IDXGISwapChain::Present"); return hr; } ID3D10RenderTargetView *pRTV = NULL; ID3D10Resource *pSrcResource = NULL; pDevice->OMGetRenderTargets(1, &pRTV, NULL); pRTV->GetResource(&pSrcResource); ID3D10Texture2D* pSrcBuffer = (ID3D10Texture2D *)pSrcResource; ID3D10Texture2D* pDstBuffer = NULL; D3D10_TEXTURE2D_DESC desc; pSrcBuffer->GetDesc(&desc); desc.BindFlags = 0; desc.CPUAccessFlags = D3D10_CPU_ACCESS_READ; desc.Usage = D3D10_USAGE_STAGING; hr = pDevice->CreateTexture2D(&desc, NULL, &pDstBuffer); if (FAILED(hr)) { OutputDebugString("Failed to create texture2D"); //assert(exp_state == exp_none); } pDevice->CopyResource(pDstBuffer, pSrcBuffer); D3D10_MAPPED_TEXTURE2D mapped_screen; hr = pDstBuffer->Map(0, D3D10_MAP_READ, 0, &mapped_screen); if (FAILED(hr)) { OutputDebugString("Failed to map from DstBuffer"); //assert(exp_state == exp_none); } // copy image do { unsigned char *src, *dst; data = g_pipe[0]->allocate_data(); frame = (struct vsource_frame*) data->ptr; frame->pixelformat = PIX_FMT_BGRA; frame->realwidth = desc.Width; frame->realheight = desc.Height; frame->realstride = desc.Width<<2; frame->realsize = frame->realwidth * frame->realstride; frame->linesize[0] = frame->realstride;//frame->stride; // src = (unsigned char*) mapped_screen.pData; dst = (unsigned char*) frame->imgbuf; for (i = 0; i < encoder_height; i++) { CopyMemory(dst, src, frame->realstride/*frame->stride*/); src += mapped_screen.RowPitch; dst += frame->realstride;//frame->stride; } frame->imgpts = pcdiff_us(captureTv, initialTv, freq)/frame_interval; } while(0); // duplicate from channel 0 to other channels for(i = 1; i < SOURCES; i++) { int j; struct pooldata *dupdata; struct vsource_frame *dupframe; dupdata = g_pipe[i]->allocate_data(); dupframe = (struct vsource_frame*) dupdata->ptr; // vsource_dup_frame(frame, dupframe); // g_pipe[i]->store_data(dupdata); g_pipe[i]->notify_all(); } g_pipe[0]->store_data(data); g_pipe[0]->notify_all(); pDstBuffer->Unmap(0); pDevice->Release(); pSrcResource->Release(); pSrcBuffer->Release(); pRTV->Release(); pDstBuffer->Release(); // d11 } else if (dx_version == dx_11) { void *ppDevice; This->GetDevice(IID_ID3D11Device, &ppDevice); ID3D11Device *pDevice = (ID3D11Device*) ppDevice; This->GetDevice(IID_ID3D11DeviceContext, &ppDevice); ID3D11DeviceContext *pDeviceContext = (ID3D11DeviceContext *) ppDevice; ID3D11RenderTargetView *pRTV = NULL; ID3D11Resource *pSrcResource = NULL; pDeviceContext->OMGetRenderTargets(1, &pRTV, NULL); pRTV->GetResource(&pSrcResource); ID3D11Texture2D *pSrcBuffer = (ID3D11Texture2D *)pSrcResource; ID3D11Texture2D *pDstBuffer = NULL; D3D11_TEXTURE2D_DESC desc; pSrcBuffer->GetDesc(&desc); desc.BindFlags = 0; desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ; desc.Usage = D3D11_USAGE_STAGING; hr = pDevice->CreateTexture2D(&desc, NULL, &pDstBuffer); if (FAILED(hr)) { OutputDebugString("Failed to create buffer"); //assert(exp_state == exp_none); } pDeviceContext->CopyResource(pDstBuffer, pSrcBuffer); D3D11_MAPPED_SUBRESOURCE mapped_screen; hr = pDeviceContext->Map(pDstBuffer, 0, D3D11_MAP_READ, 0, &mapped_screen); if (FAILED(hr)) { OutputDebugString("Failed to map from DeviceContext"); //assert(exp_state == exp_none); } // copy image do { unsigned char *src, *dst; data = g_pipe[0]->allocate_data(); frame = (struct vsource_frame*) data->ptr; frame->pixelformat = PIX_FMT_BGRA; frame->realwidth = desc.Width; frame->realheight = desc.Height; frame->realstride = desc.Width<<2; frame->realsize = frame->realwidth * frame->realstride; frame->linesize[0] = frame->realstride;//frame->stride; // src = (unsigned char*) mapped_screen.pData; dst = (unsigned char*) frame->imgbuf; for (i = 0; i < encoder_height; i++) { CopyMemory(dst, src, frame->realstride/*frame->stride*/); src += mapped_screen.RowPitch; dst += frame->realstride;//frame->stride; } frame->imgpts = pcdiff_us(captureTv, initialTv, freq)/frame_interval; } while(0); // duplicate from channel 0 to other channels for(i = 1; i < SOURCES; i++) { int j; struct pooldata *dupdata; struct vsource_frame *dupframe; dupdata = g_pipe[i]->allocate_data(); dupframe = (struct vsource_frame*) dupdata->ptr; // vsource_dup_frame(frame, dupframe); // g_pipe[i]->store_data(dupdata); g_pipe[i]->notify_all(); } g_pipe[0]->store_data(data); g_pipe[0]->notify_all(); pDeviceContext->Unmap(pDstBuffer, 0); pDevice->Release(); pDeviceContext->Release(); pSrcResource->Release(); pSrcBuffer->Release(); pRTV->Release(); pDstBuffer->Release(); } return hr; }
bool CScreenshotSurface::capture() { #if defined(TARGET_RASPBERRY_PI) g_RBP.GetDisplaySize(m_width, m_height); m_buffer = g_RBP.CaptureDisplay(m_width, m_height, &m_stride, true, false); if (!m_buffer) return false; #elif defined(HAS_DX) g_graphicsContext.Lock(); if (g_application.m_pPlayer->IsPlayingVideo()) { #ifdef HAS_VIDEO_PLAYBACK g_renderManager.SetupScreenshot(); #endif } g_application.RenderNoPresent(); g_Windowing.FinishCommandList(); ID3D11DeviceContext* pImdContext = g_Windowing.GetImmediateContext(); ID3D11DeviceContext* pContext = g_Windowing.Get3D11Context(); ID3D11Device* pDevice = g_Windowing.Get3D11Device(); ID3D11RenderTargetView* pRTView = nullptr; pContext->OMGetRenderTargets(1, &pRTView, nullptr); if (pRTView == nullptr) return false; ID3D11Resource *pRTResource = nullptr; pRTView->GetResource(&pRTResource); SAFE_RELEASE(pRTView); ID3D11Texture2D* pCopyTexture = nullptr; ID3D11Texture2D* pRTTexture = nullptr; HRESULT hr = pRTResource->QueryInterface(__uuidof(ID3D11Texture2D), reinterpret_cast<void**>(&pRTTexture)); SAFE_RELEASE(pRTResource); if (FAILED(hr)) return false; D3D11_TEXTURE2D_DESC desc; pRTTexture->GetDesc(&desc); desc.Usage = D3D11_USAGE_STAGING; desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ; desc.BindFlags = 0; if (SUCCEEDED(pDevice->CreateTexture2D(&desc, nullptr, &pCopyTexture))) { // take copy pImdContext->CopyResource(pCopyTexture, pRTTexture); D3D11_MAPPED_SUBRESOURCE res; if (SUCCEEDED(pImdContext->Map(pCopyTexture, 0, D3D11_MAP_READ, 0, &res))) { m_width = desc.Width; m_height = desc.Height; m_stride = res.RowPitch; m_buffer = new unsigned char[m_height * m_stride]; memcpy(m_buffer, res.pData, m_height * m_stride); pImdContext->Unmap(pCopyTexture, 0); } else CLog::Log(LOGERROR, "%s: MAP_READ failed.", __FUNCTION__); SAFE_RELEASE(pCopyTexture); } SAFE_RELEASE(pRTTexture); g_graphicsContext.Unlock(); #elif defined(HAS_GL) || defined(HAS_GLES) g_graphicsContext.BeginPaint(); if (g_application.m_pPlayer->IsPlayingVideo()) { #ifdef HAS_VIDEO_PLAYBACK g_renderManager.SetupScreenshot(); #endif } g_application.RenderNoPresent(); #ifndef HAS_GLES glReadBuffer(GL_BACK); #endif //get current viewport GLint viewport[4]; glGetIntegerv(GL_VIEWPORT, viewport); m_width = viewport[2] - viewport[0]; m_height = viewport[3] - viewport[1]; m_stride = m_width * 4; unsigned char* surface = new unsigned char[m_stride * m_height]; //read pixels from the backbuffer #if HAS_GLES == 2 glReadPixels(viewport[0], viewport[1], viewport[2], viewport[3], GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*)surface); #else glReadPixels(viewport[0], viewport[1], viewport[2], viewport[3], GL_BGRA, GL_UNSIGNED_BYTE, (GLvoid*)surface); #endif g_graphicsContext.EndPaint(); //make a new buffer and copy the read image to it with the Y axis inverted m_buffer = new unsigned char[m_stride * m_height]; for (int y = 0; y < m_height; y++) { #ifdef HAS_GLES // we need to save in BGRA order so XOR Swap RGBA -> BGRA unsigned char* swap_pixels = surface + (m_height - y - 1) * m_stride; for (int x = 0; x < m_width; x++, swap_pixels+=4) { std::swap(swap_pixels[0], swap_pixels[2]); } #endif memcpy(m_buffer + y * m_stride, surface + (m_height - y - 1) *m_stride, m_stride); } delete [] surface; #if defined(HAS_LIBAMCODEC) // Captures the current visible videobuffer and blend it into m_buffer (captured overlay) CScreenshotAML::CaptureVideoFrame(m_buffer, m_width, m_height); #endif #ifdef HAS_IMXVPU // Captures the current visible framebuffer page and blends it into the // captured GL overlay g_IMXContext.CaptureDisplay(m_buffer, m_width, m_height); #endif #else //nothing to take a screenshot from return false; #endif return true; }