nuclear@0: /************************************************************************************ nuclear@0: nuclear@0: Filename : CAPI_DistortionRenderer.cpp nuclear@0: Content : Combines all of the rendering state associated with the HMD nuclear@0: Created : February 2, 2014 nuclear@0: Authors : Michael Antonov nuclear@0: nuclear@0: Copyright : Copyright 2014 Oculus VR, LLC All Rights reserved. nuclear@0: nuclear@0: Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License"); nuclear@0: you may not use the Oculus VR Rift SDK except in compliance with the License, nuclear@0: which is provided at the time of installation or download, or which nuclear@0: otherwise accompanies this software in either electronic or hard copy form. nuclear@0: nuclear@0: You may obtain a copy of the License at nuclear@0: nuclear@0: http://www.oculusvr.com/licenses/LICENSE-3.2 nuclear@0: nuclear@0: Unless required by applicable law or agreed to in writing, the Oculus VR SDK nuclear@0: distributed under the License is distributed on an "AS IS" BASIS, nuclear@0: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. nuclear@0: See the License for the specific language governing permissions and nuclear@0: limitations under the License. nuclear@0: nuclear@0: ************************************************************************************/ nuclear@0: nuclear@0: #include "CAPI_DistortionRenderer.h" nuclear@0: nuclear@0: #if defined (OVR_OS_WIN32) nuclear@0: nuclear@0: // TBD: Move to separate config file that handles back-ends. nuclear@0: #define OVR_D3D_VERSION 11 nuclear@0: #include "D3D1X/CAPI_D3D1X_DistortionRenderer.h" nuclear@0: #undef OVR_D3D_VERSION nuclear@0: nuclear@0: #define OVR_D3D_VERSION 10 nuclear@0: #include "D3D1X/CAPI_D3D1X_DistortionRenderer.h" nuclear@0: #undef OVR_D3D_VERSION nuclear@0: nuclear@0: #define OVR_D3D_VERSION 9 nuclear@0: #include "D3D9/CAPI_D3D9_DistortionRenderer.h" nuclear@0: #undef OVR_D3D_VERSION nuclear@0: nuclear@0: #endif nuclear@0: nuclear@0: #include "GL/CAPI_GL_DistortionRenderer.h" nuclear@0: nuclear@0: namespace OVR { namespace CAPI { nuclear@0: nuclear@0: //------------------------------------------------------------------------------------- nuclear@0: // ***** DistortionRenderer nuclear@0: nuclear@0: // TBD: Move to separate config file that handles back-ends. nuclear@0: nuclear@0: DistortionRenderer::CreateFunc DistortionRenderer::APICreateRegistry[ovrRenderAPI_Count] = nuclear@0: { nuclear@0: 0, // None nuclear@0: &GL::DistortionRenderer::Create, nuclear@0: 0, // Android_GLES nuclear@0: #if defined (OVR_OS_WIN32) nuclear@0: &D3D9::DistortionRenderer::Create, nuclear@0: &D3D10::DistortionRenderer::Create, nuclear@0: &D3D11::DistortionRenderer::Create nuclear@0: #else nuclear@0: 0, nuclear@0: 0, nuclear@0: 0 nuclear@0: #endif nuclear@0: }; nuclear@0: nuclear@0: void DistortionRenderer::SetLatencyTestColor(unsigned char* color) nuclear@0: { nuclear@0: if(color) nuclear@0: { nuclear@0: LatencyTestDrawColor[0] = color[0]; nuclear@0: LatencyTestDrawColor[1] = color[1]; nuclear@0: LatencyTestDrawColor[2] = color[2]; nuclear@0: } nuclear@0: nuclear@0: LatencyTestActive = color != NULL; nuclear@0: } nuclear@0: nuclear@0: void DistortionRenderer::SetLatencyTest2Color(unsigned char* color) nuclear@0: { nuclear@0: if(color) nuclear@0: { nuclear@0: LatencyTest2DrawColor[0] = color[0]; nuclear@0: LatencyTest2DrawColor[1] = color[1]; nuclear@0: LatencyTest2DrawColor[2] = color[2]; nuclear@0: } nuclear@0: nuclear@0: LatencyTest2Active = color != NULL; nuclear@0: } nuclear@0: nuclear@0: void DistortionRenderer::GetOverdriveScales(float& outRiseScale, float& outFallScale) nuclear@0: { nuclear@0: outRiseScale = 0.1f; nuclear@0: outFallScale = 0.05f; // falling issues are hardly visible nuclear@0: } nuclear@0: nuclear@0: double DistortionRenderer::WaitTillTime(double absTime) nuclear@0: { nuclear@0: double initialTime = ovr_GetTimeInSeconds(); nuclear@0: if (initialTime >= absTime) nuclear@0: return 0.0; nuclear@0: nuclear@0: double newTime = initialTime; nuclear@0: nuclear@0: while (newTime < absTime) nuclear@0: { nuclear@0: // TODO: Needs further testing before enabling it on all Windows configs nuclear@0: #if 0 //def OVR_OS_WIN32 nuclear@0: double remainingWaitTime = absTime - newTime; nuclear@0: nuclear@0: // don't yield if <2ms nuclear@0: if(remainingWaitTime > 0.002) nuclear@0: { nuclear@0: // round down wait time to closest 1 ms nuclear@0: int roundedWaitTime = (remainingWaitTime * 1000); nuclear@0: nuclear@0: waitableTimerInterval.QuadPart = -10000LL; // 10000 * 100 ns = 1 ms nuclear@0: waitableTimerInterval.QuadPart *= roundedWaitTime; nuclear@0: nuclear@0: SetWaitableTimer(timer, &waitableTimerInterval, 0, NULL, NULL, TRUE); nuclear@0: DWORD waitResult = WaitForSingleObject(timer, roundedWaitTime + 3); // give 3 ms extra time nuclear@0: OVR_UNUSED(waitResult); nuclear@0: nuclear@0: #ifdef OVR_BUILD_DEBUG nuclear@0: double sleptTime = ovr_GetTimeInSeconds() - newTime; nuclear@0: // Make sure we didn't sleep too long and it is reliable, otherwise we might miss v-sync causing a stutter nuclear@0: if (sleptTime > (roundedWaitTime + 2) * 0.001) nuclear@0: { nuclear@0: OVR_DEBUG_LOG_TEXT( nuclear@0: ("[DistortionRenderer::WaitTillTime] Sleep interval too long: %f\n", sleptTime)); nuclear@0: } nuclear@0: else nuclear@0: { nuclear@0: OVR_ASSERT(WAIT_OBJECT_0 == waitResult); nuclear@0: } nuclear@0: #endif nuclear@0: } nuclear@0: else nuclear@0: #endif nuclear@0: { nuclear@0: for (int j = 0; j < 5; j++) nuclear@0: OVR_PROCESSOR_PAUSE(); nuclear@0: } nuclear@0: nuclear@0: newTime = ovr_GetTimeInSeconds(); nuclear@0: } nuclear@0: nuclear@0: // How long we waited nuclear@0: return newTime - initialTime; nuclear@0: } nuclear@0: nuclear@0: }} // namespace OVR::CAPI nuclear@0: