/** * Draws stereoscopic frame. ***/ void StereoView::Draw(D3D9ProxySurface* stereoCapableSurface) { // Copy left and right surfaces to textures to use as shader input // TODO match aspect ratio of source in target ? IDirect3DSurface9* leftImage = stereoCapableSurface->getActualLeft(); IDirect3DSurface9* rightImage = stereoCapableSurface->getActualRight(); m_pActualDevice->StretchRect(leftImage, NULL, leftSurface, NULL, D3DTEXF_NONE); if (stereoCapableSurface->IsStereo()) m_pActualDevice->StretchRect(rightImage, NULL, rightSurface, NULL, D3DTEXF_NONE); else m_pActualDevice->StretchRect(leftImage, NULL, rightSurface, NULL, D3DTEXF_NONE); // how to save (backup) render states ? switch(howToSaveRenderStates) { case HowToSaveRenderStates::STATE_BLOCK: m_pActualDevice->CreateStateBlock(D3DSBT_ALL, &sb); break; case HowToSaveRenderStates::SELECTED_STATES_MANUALLY: SaveState(); break; case HowToSaveRenderStates::ALL_STATES_MANUALLY: SaveAllRenderStates(m_pActualDevice); SetAllRenderStatesDefault(m_pActualDevice); break; case HowToSaveRenderStates::DO_NOT_SAVE_AND_RESTORE: break; } // set states for fullscreen render SetState(); // all render settings start here m_pActualDevice->SetFVF(D3DFVF_TEXVERTEX); // swap eyes if(!swapEyes) { m_pActualDevice->SetTexture(0, leftTexture); m_pActualDevice->SetTexture(1, rightTexture); } else { m_pActualDevice->SetTexture(0, rightTexture); m_pActualDevice->SetTexture(1, leftTexture); } if (FAILED(m_pActualDevice->SetRenderTarget(0, backBuffer))) { OutputDebugString("SetRenderTarget backbuffer failed\n"); } if (FAILED(m_pActualDevice->SetStreamSource(0, screenVertexBuffer, 0, sizeof(TEXVERTEX)))) { OutputDebugString("SetStreamSource failed\n"); } UINT iPass, cPasses; if (FAILED(viewEffect->SetTechnique("ViewShader"))) { OutputDebugString("SetTechnique failed\n"); } SetViewEffectInitialValues(); // now, render if (FAILED(viewEffect->Begin(&cPasses, 0))) { OutputDebugString("Begin failed\n"); } for(iPass = 0; iPass < cPasses; iPass++) { if (FAILED(viewEffect->BeginPass(iPass))) { OutputDebugString("Beginpass failed\n"); } if (FAILED(m_pActualDevice->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 2))) { OutputDebugString("Draw failed\n"); } if (FAILED(viewEffect->EndPass())) { OutputDebugString("Beginpass failed\n"); } } if (FAILED(viewEffect->End())) { OutputDebugString("End failed\n"); } // how to restore render states ? switch(howToSaveRenderStates) { case HowToSaveRenderStates::STATE_BLOCK: // apply stored render states sb->Apply(); sb->Release(); sb = NULL; break; case HowToSaveRenderStates::SELECTED_STATES_MANUALLY: RestoreState(); break; case HowToSaveRenderStates::ALL_STATES_MANUALLY: RestoreAllRenderStates(m_pActualDevice); break; case HowToSaveRenderStates::DO_NOT_SAVE_AND_RESTORE: break; } }
/** * Render the Oculus Rift View. ***/ void* OculusRenderer::Provoke(void* pThis, int eD3D, int eD3DInterface, int eD3DMethod, DWORD dwNumberConnected, int& nProvokerIndex) { // return if wrong call if ((eD3D >= (int)AQU_DirectXVersion::DirectX_9_0) && (eD3D <= (int)AQU_DirectXVersion::DirectX_9_29)) { if (((eD3DInterface == INTERFACE_IDIRECT3DDEVICE9) && (eD3DMethod == METHOD_IDIRECT3DDEVICE9_PRESENT)) || ((eD3DInterface == INTERFACE_IDIRECT3DDEVICE9) && (eD3DMethod == METHOD_IDIRECT3DDEVICE9_ENDSCENE)) || ((eD3DInterface == INTERFACE_IDIRECT3DSWAPCHAIN9) && (eD3DMethod == METHOD_IDIRECT3DSWAPCHAIN9_PRESENT))) { (pThis); } else return nullptr; } else return nullptr; // get input data if (m_paInput[(int)ORN_Decommanders::LeftTexture]) m_pcTextureLeft = *(LPDIRECT3DTEXTURE9*)m_paInput[(int)ORN_Decommanders::LeftTexture]; else m_pcTextureLeft = nullptr; if (m_paInput[(int)ORN_Decommanders::RightTexture]) m_pcTextureRight = *(LPDIRECT3DTEXTURE9*)m_paInput[(int)ORN_Decommanders::RightTexture]; else m_pcTextureRight = nullptr; if (m_paInput[(int)ORN_Decommanders::DistortionVertexBufferLeft]) if (*(LPDIRECT3DVERTEXBUFFER9**)m_paInput[(int)ORN_Decommanders::DistortionVertexBufferLeft]) m_pcDistortionVertexBufferLeft = **(LPDIRECT3DVERTEXBUFFER9**)m_paInput[(int)ORN_Decommanders::DistortionVertexBufferLeft]; else m_pcDistortionVertexBufferLeft = nullptr; else m_pcDistortionVertexBufferLeft = nullptr; if (m_paInput[(int)ORN_Decommanders::DistortionVertexBufferRight]) if (*(LPDIRECT3DVERTEXBUFFER9**)m_paInput[(int)ORN_Decommanders::DistortionVertexBufferRight]) m_pcDistortionVertexBufferRight = **(LPDIRECT3DVERTEXBUFFER9**)m_paInput[(int)ORN_Decommanders::DistortionVertexBufferRight]; else m_pcDistortionVertexBufferRight = nullptr; else m_pcDistortionVertexBufferRight = nullptr; if (m_paInput[(int)ORN_Decommanders::DistortionIndexBufferLeft]) if (*(LPDIRECT3DINDEXBUFFER9**)m_paInput[(int)ORN_Decommanders::DistortionIndexBufferLeft]) m_pcDistortionIndexBufferLeft = **(LPDIRECT3DINDEXBUFFER9**)m_paInput[(int)ORN_Decommanders::DistortionIndexBufferLeft]; else m_pcDistortionIndexBufferLeft = nullptr; else m_pcDistortionIndexBufferLeft = nullptr; if (m_paInput[(int)ORN_Decommanders::DistortionIndexBufferRight]) if (*(LPDIRECT3DINDEXBUFFER9**)m_paInput[(int)ORN_Decommanders::DistortionIndexBufferRight]) m_pcDistortionIndexBufferRight = **(LPDIRECT3DINDEXBUFFER9**)m_paInput[(int)ORN_Decommanders::DistortionIndexBufferRight]; else m_pcDistortionIndexBufferRight = nullptr; else m_pcDistortionIndexBufferRight = nullptr; if (m_paInput[(int)ORN_Decommanders::OculusVertexDeclaration]) if (*(LPDIRECT3DVERTEXDECLARATION9**)m_paInput[(int)ORN_Decommanders::OculusVertexDeclaration]) m_pcVertexDecl = **(LPDIRECT3DVERTEXDECLARATION9**)m_paInput[(int)ORN_Decommanders::OculusVertexDeclaration]; else m_pcVertexDecl = nullptr; else m_pcVertexDecl = nullptr; if (m_paInput[(int)ORN_Decommanders::DefaultEyeFovLeft]) if (*(ovrFovPort**)m_paInput[(int)ORN_Decommanders::DefaultEyeFovLeft]) m_psFOVPortLeft = *(ovrFovPort**)m_paInput[(int)ORN_Decommanders::DefaultEyeFovLeft]; else m_psFOVPortLeft = nullptr; else m_psFOVPortLeft = nullptr; if (m_paInput[(int)ORN_Decommanders::DefaultEyeFovRight]) if (*(ovrFovPort**)m_paInput[(int)ORN_Decommanders::DefaultEyeFovRight]) m_psFOVPortRight = *(ovrFovPort**)m_paInput[(int)ORN_Decommanders::DefaultEyeFovRight]; else m_psFOVPortRight = nullptr; else m_psFOVPortRight = nullptr; // get device LPDIRECT3DDEVICE9 pcDevice = nullptr; bool bReleaseDevice = false; if (eD3DInterface == INTERFACE_IDIRECT3DDEVICE9) { pcDevice = (LPDIRECT3DDEVICE9)pThis; } else if (eD3DInterface == INTERFACE_IDIRECT3DSWAPCHAIN9) { LPDIRECT3DSWAPCHAIN9 pSwapChain = (LPDIRECT3DSWAPCHAIN9)pThis; if (!pSwapChain) { OutputDebugString(L"Oculus Renderer Node : No swapchain !"); return nullptr; } pSwapChain->GetDevice(&pcDevice); bReleaseDevice = true; } if (!pcDevice) { OutputDebugString(L"Oculus Renderer Node : No device !"); return nullptr; } // Original code (LibOVR) : // pShaderCode = ShaderCompile("precompiledVertexShaderSrc",VertexShaderSrc,"vs_2_0"); // pShaderCode = ShaderCompile("precompiledVertexShaderTimewarpSrc",VertexShaderTimewarpSrc,"vs_3_0"); // pShaderCode = ShaderCompile("precompiledPixelShaderSrc",PixelShaderSrc,"ps_3_0"); // pixel shader created ? if (!m_pcOculusPixelShader) { LPD3DXBUFFER pShader; // compile and create shader if (SUCCEEDED(D3DXCompileShader(PixelShaderSrc,strlen(PixelShaderSrc),NULL,NULL,"main","ps_3_0",NULL,&pShader,NULL,&m_pcOculusPixelShaderCT))) { OutputDebugString(L"Pixel shader compiled!"); pcDevice->CreatePixelShader((DWORD*)pShader->GetBufferPointer(), &m_pcOculusPixelShader); } } // vertex shader created ? if (!m_pcOculusVertexShader) { LPD3DXBUFFER pShader; // compile and create shader if (SUCCEEDED(D3DXCompileShader(VertexShaderSrc,strlen(VertexShaderSrc),NULL,NULL,"main","vs_2_0",NULL,&pShader,NULL,&m_pcOculusVertexShaderCT))) { OutputDebugString(L"Vertex shader compiled!"); pcDevice->CreateVertexShader((DWORD*)pShader->GetBufferPointer(), &m_pcOculusVertexShader); } } // side by side pixel shader ? if (!m_pcSideBySidePixelShader) { LPD3DXBUFFER pShader; // compile and create shader if (SUCCEEDED(D3DXCompileShader(PixelShaderSrcSideBySide,strlen(PixelShaderSrcSideBySide),NULL,NULL,"SBS","ps_2_0",NULL,&pShader,NULL,&m_pcSideBySidePixelShaderCT))) { OutputDebugString(L"Pixel shader compiled!"); pcDevice->CreatePixelShader((DWORD*)pShader->GetBufferPointer(), &m_pcSideBySidePixelShader); } } // test textures created ? if ((!m_pcTextureRightTest) || (!m_pcTextureLeftTest)) { HMODULE hModule = GetModuleHandle(L"OculusRenderer.dll"); if (!m_pcTextureLeftTest) { // create a test texture if (SUCCEEDED(D3DXCreateTextureFromResource(pcDevice, hModule, MAKEINTRESOURCE(IMG_BACKGROUND01), &m_pcTextureLeftTest))) OutputDebugString(L"Texture created !"); else m_pcTextureLeftTest = nullptr; } if (!m_pcTextureRightTest) { // create a test texture if (SUCCEEDED(D3DXCreateTextureFromResource(pcDevice, hModule, MAKEINTRESOURCE(IMG_BACKGROUND02), &m_pcTextureRightTest))) OutputDebugString(L"Texture created !"); else m_pcTextureRightTest = nullptr; } } // default vertex buffer ? if (!m_pcVertexBufferDefault) { InitDefaultVertexBuffer(pcDevice); if (!m_pcVertexBufferDefault) return nullptr; } // vertex buffers ? index buffers ? not all present ? if ((!m_pcDistortionVertexBufferLeft) || (!m_pcDistortionVertexBufferRight) || (!m_pcDistortionIndexBufferLeft) || (!m_pcDistortionIndexBufferRight) || (!m_pcVertexDecl)) { if (m_bBuffersConnected) { // clear all descriptions for safety ZeroMemory(&m_sVertexBufferDescLeft, sizeof(D3DVERTEXBUFFER_DESC)); ZeroMemory(&m_sVertexBufferDescRight, sizeof(D3DVERTEXBUFFER_DESC)); ZeroMemory(&m_sIndexBufferDescLeft, sizeof(D3DINDEXBUFFER_DESC)); ZeroMemory(&m_sIndexBufferDescRight, sizeof(D3DINDEXBUFFER_DESC)); m_bBuffersConnected = false; } } else { m_bBuffersConnected = true; // vertex buffer description ? left if (!m_sVertexBufferDescLeft.Size) m_pcDistortionVertexBufferLeft->GetDesc(&m_sVertexBufferDescLeft); // vertex buffer length matches oculus vertex type ? if ((m_sVertexBufferDescLeft.Size % sizeof(ovrDistortionVertex)) != 0) { OutputDebugString(L"OculusRenderer Node : Connected vertex buffer size mismatch !"); return nullptr; } // and right if (!m_sVertexBufferDescRight.Size) m_pcDistortionVertexBufferRight->GetDesc(&m_sVertexBufferDescRight); // vertex buffer length matches oculus vertex type ? if ((m_sVertexBufferDescRight.Size % sizeof(ovrDistortionVertex)) != 0) { OutputDebugString(L"OculusRenderer Node : Connected vertex buffer size mismatch !"); return nullptr; } // index buffer ? if ((!m_pcDistortionIndexBufferLeft) || (!m_pcDistortionIndexBufferRight)) return nullptr; // index buffer description ? if (!m_sIndexBufferDescLeft.Size) { m_pcDistortionIndexBufferLeft->GetDesc(&m_sIndexBufferDescLeft); // index buffer length matches vertex buffer size ? TODO !! /*if () { OutputDebugString(L"OculusRenderer Node : Connected index buffer size mismatch !"); return nullptr; }*/ } if (!m_sIndexBufferDescRight.Size) { m_pcDistortionIndexBufferRight->GetDesc(&m_sIndexBufferDescRight); // index buffer length matches vertex buffer size ? TODO !! /*if () { OutputDebugString(L"OculusRenderer Node : Connected index buffer size mismatch !"); return nullptr; }*/ } } // start to render pcDevice->BeginScene(); // save states IDirect3DStateBlock9* pStateBlock = nullptr; pcDevice->CreateStateBlock(D3DSBT_ALL, &pStateBlock); // set ALL render states to default SetAllRenderStatesDefault(pcDevice); // set states pcDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1); pcDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE); pcDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1); pcDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_CONSTANT); pcDevice->SetTextureStageState(0, D3DTSS_CONSTANT, 0xffffffff); pcDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); pcDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE); pcDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE); pcDevice->SetSamplerState(0, D3DSAMP_SRGBTEXTURE, 0); pcDevice->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP); pcDevice->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP); pcDevice->SetSamplerState(0, D3DSAMP_ADDRESSW, D3DTADDRESS_CLAMP); pcDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_ANISOTROPIC); pcDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_ANISOTROPIC); pcDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_NONE); D3DCOLOR clearColor = D3DCOLOR_RGBA(0, 0, 0, 0); pcDevice->Clear(0, NULL, D3DCLEAR_TARGET, clearColor, 0, 0); // required fields D3DSURFACE_DESC sSurfaceDesc; ovrSizei sTextureSize; ovrRecti sRenderViewport; ovrVector2f UVScaleOffset[2]; // LEFT EYE : // left eye, first set stream source, indices if (m_bBuffersConnected) { // no timewarp here, use standard vertex shader, set pixel shader, set vertex declaration pcDevice->SetVertexShader( m_pcOculusVertexShader); pcDevice->SetPixelShader( m_pcOculusPixelShader ); pcDevice->SetVertexDeclaration( m_pcVertexDecl ); // set texture if (m_pcTextureLeft) pcDevice->SetTexture( 0, m_pcTextureLeft ); else if (m_pcTextureLeftTest) pcDevice->SetTexture( 0, m_pcTextureLeftTest ); else pcDevice->SetTexture( 0, 0); // get texture size if (m_pcTextureLeft) m_pcTextureLeft->GetLevelDesc(0, &sSurfaceDesc); else if (m_pcTextureLeftTest) m_pcTextureLeftTest->GetLevelDesc(0, &sSurfaceDesc); else ZeroMemory(&sSurfaceDesc, sizeof(D3DSURFACE_DESC)); sTextureSize.w = (int)sSurfaceDesc.Width; sTextureSize.h = (int)sSurfaceDesc.Height; // set render viewport size the same size as the texture size (!) sRenderViewport.Pos.x = 0; sRenderViewport.Pos.y = 0; sRenderViewport.Size.w = sTextureSize.w; sRenderViewport.Size.h = sTextureSize.h; // get and set scale and offset if (m_psFOVPortLeft) ovrHmd_GetRenderScaleAndOffset(*m_psFOVPortLeft, sTextureSize, sRenderViewport, UVScaleOffset); else ovrHmd_GetRenderScaleAndOffset(m_sDefaultFOVPortLeft, sTextureSize, sRenderViewport, UVScaleOffset); pcDevice->SetVertexShaderConstantF( 0, ( FLOAT* )&UVScaleOffset[0], 1 ); pcDevice->SetVertexShaderConstantF( 2, ( FLOAT* )&UVScaleOffset[1], 1 ); pcDevice->SetStreamSource( 0, m_pcDistortionVertexBufferLeft,0, sizeof(ovrDistortionVertex) ); pcDevice->SetIndices( m_pcDistortionIndexBufferLeft); // draw pcDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST,0,0, m_sVertexBufferDescLeft.Size / sizeof(ovrDistortionVertex) , 0, m_sIndexBufferDescLeft.Size / 6 ); } else { pcDevice->SetSamplerState(1, D3DSAMP_SRGBTEXTURE, 0); pcDevice->SetSamplerState(1, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP); pcDevice->SetSamplerState(1, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP); pcDevice->SetSamplerState(1, D3DSAMP_ADDRESSW, D3DTADDRESS_CLAMP); pcDevice->SetSamplerState(1, D3DSAMP_MAGFILTER, D3DTEXF_ANISOTROPIC); pcDevice->SetSamplerState(1, D3DSAMP_MINFILTER, D3DTEXF_ANISOTROPIC); pcDevice->SetSamplerState(1, D3DSAMP_MIPFILTER, D3DTEXF_NONE); // side by side render pcDevice->SetVertexShader(NULL); pcDevice->SetPixelShader(m_pcSideBySidePixelShader); pcDevice->SetVertexDeclaration( NULL ); pcDevice->SetFVF(D3DFVF_TEXVERTEX); // set textures if (m_pcTextureLeft) pcDevice->SetTexture( 0, m_pcTextureLeft ); else if (m_pcTextureLeftTest) pcDevice->SetTexture( 0, m_pcTextureLeftTest ); else pcDevice->SetTexture( 0, 0); if (m_pcTextureRight) pcDevice->SetTexture( 1, m_pcTextureRight ); else if (m_pcTextureRightTest) pcDevice->SetTexture( 1, m_pcTextureRightTest ); else pcDevice->SetTexture( 1, 0); // set stream and draw pcDevice->SetStreamSource( 0, m_pcVertexBufferDefault,0, sizeof(TEXVERTEX) ); pcDevice->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 2); } // RIGHT EYE: if (m_bBuffersConnected) { // set texture if (m_pcTextureRight) pcDevice->SetTexture( 0, m_pcTextureRight ); else if (m_pcTextureRightTest) pcDevice->SetTexture( 0, m_pcTextureRightTest ); else pcDevice->SetTexture( 0, 0); // get texture size if (m_pcTextureRight) m_pcTextureRight->GetLevelDesc(0, &sSurfaceDesc); else if (m_pcTextureRightTest) m_pcTextureRightTest->GetLevelDesc(0, &sSurfaceDesc); else ZeroMemory(&sSurfaceDesc, sizeof(D3DSURFACE_DESC)); sTextureSize.w = (int)sSurfaceDesc.Width; sTextureSize.h = (int)sSurfaceDesc.Height; // get and set scale and offset if (m_psFOVPortRight) ovrHmd_GetRenderScaleAndOffset(*m_psFOVPortRight, sTextureSize, sRenderViewport, UVScaleOffset); else ovrHmd_GetRenderScaleAndOffset(m_sDefaultFOVPortRight, sTextureSize, sRenderViewport, UVScaleOffset); pcDevice->SetVertexShaderConstantF( 0, ( FLOAT* )&UVScaleOffset[0], 1 ); pcDevice->SetVertexShaderConstantF( 2, ( FLOAT* )&UVScaleOffset[1], 1 ); // set stream source, indices, draw pcDevice->SetStreamSource( 0, m_pcDistortionVertexBufferRight,0, sizeof(ovrDistortionVertex) ); pcDevice->SetIndices( m_pcDistortionIndexBufferRight); // draw pcDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST,0,0, m_sVertexBufferDescRight.Size / sizeof(ovrDistortionVertex) , 0, m_sIndexBufferDescRight.Size / 6 ); } pcDevice->EndScene(); // apply and release state block if (pStateBlock) { pStateBlock->Apply(); pStateBlock->Release(); } // release device if provided by swapchain if (bReleaseDevice) pcDevice->Release(); return nullptr; }