ovr_sdk

diff LibOVR/Src/CAPI/CAPI_DistortionRenderer.cpp @ 0:1b39a1b46319

initial 0.4.4
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 14 Jan 2015 06:51:16 +0200
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/LibOVR/Src/CAPI/CAPI_DistortionRenderer.cpp	Wed Jan 14 06:51:16 2015 +0200
     1.3 @@ -0,0 +1,157 @@
     1.4 +/************************************************************************************
     1.5 +
     1.6 +Filename    :   CAPI_DistortionRenderer.cpp
     1.7 +Content     :   Combines all of the rendering state associated with the HMD
     1.8 +Created     :   February 2, 2014
     1.9 +Authors     :   Michael Antonov
    1.10 +
    1.11 +Copyright   :   Copyright 2014 Oculus VR, LLC All Rights reserved.
    1.12 +
    1.13 +Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License"); 
    1.14 +you may not use the Oculus VR Rift SDK except in compliance with the License, 
    1.15 +which is provided at the time of installation or download, or which 
    1.16 +otherwise accompanies this software in either electronic or hard copy form.
    1.17 +
    1.18 +You may obtain a copy of the License at
    1.19 +
    1.20 +http://www.oculusvr.com/licenses/LICENSE-3.2 
    1.21 +
    1.22 +Unless required by applicable law or agreed to in writing, the Oculus VR SDK 
    1.23 +distributed under the License is distributed on an "AS IS" BASIS,
    1.24 +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    1.25 +See the License for the specific language governing permissions and
    1.26 +limitations under the License.
    1.27 +
    1.28 +************************************************************************************/
    1.29 +
    1.30 +#include "CAPI_DistortionRenderer.h"
    1.31 +
    1.32 +#if defined (OVR_OS_WIN32)
    1.33 +
    1.34 +// TBD: Move to separate config file that handles back-ends.
    1.35 +#define OVR_D3D_VERSION 11
    1.36 +#include "D3D1X/CAPI_D3D1X_DistortionRenderer.h"
    1.37 +#undef OVR_D3D_VERSION
    1.38 +
    1.39 +#define OVR_D3D_VERSION 10
    1.40 +#include "D3D1X/CAPI_D3D1X_DistortionRenderer.h"
    1.41 +#undef OVR_D3D_VERSION
    1.42 +
    1.43 +#define OVR_D3D_VERSION 9
    1.44 +#include "D3D9/CAPI_D3D9_DistortionRenderer.h"
    1.45 +#undef OVR_D3D_VERSION
    1.46 +
    1.47 +#endif
    1.48 +
    1.49 +#include "GL/CAPI_GL_DistortionRenderer.h"
    1.50 +
    1.51 +namespace OVR { namespace CAPI {
    1.52 +
    1.53 +//-------------------------------------------------------------------------------------
    1.54 +// ***** DistortionRenderer
    1.55 +
    1.56 +// TBD: Move to separate config file that handles back-ends.
    1.57 +
    1.58 +DistortionRenderer::CreateFunc DistortionRenderer::APICreateRegistry[ovrRenderAPI_Count] =
    1.59 +{
    1.60 +    0, // None
    1.61 +    &GL::DistortionRenderer::Create,
    1.62 +    0, // Android_GLES
    1.63 +#if defined (OVR_OS_WIN32)
    1.64 +    &D3D9::DistortionRenderer::Create,
    1.65 +    &D3D10::DistortionRenderer::Create,
    1.66 +    &D3D11::DistortionRenderer::Create
    1.67 +#else
    1.68 +    0,
    1.69 +    0,
    1.70 +    0
    1.71 +#endif
    1.72 +};
    1.73 +
    1.74 +void DistortionRenderer::SetLatencyTestColor(unsigned char* color)
    1.75 +{
    1.76 +    if(color)
    1.77 +    {
    1.78 +        LatencyTestDrawColor[0] = color[0];
    1.79 +        LatencyTestDrawColor[1] = color[1];
    1.80 +        LatencyTestDrawColor[2] = color[2];
    1.81 +    }
    1.82 +
    1.83 +    LatencyTestActive = color != NULL;
    1.84 +}
    1.85 +
    1.86 +void DistortionRenderer::SetLatencyTest2Color(unsigned char* color)
    1.87 +{
    1.88 +    if(color)
    1.89 +    {
    1.90 +        LatencyTest2DrawColor[0] = color[0];
    1.91 +        LatencyTest2DrawColor[1] = color[1];
    1.92 +        LatencyTest2DrawColor[2] = color[2];
    1.93 +    }
    1.94 +
    1.95 +    LatencyTest2Active = color != NULL;
    1.96 +}
    1.97 +
    1.98 +void DistortionRenderer::GetOverdriveScales(float& outRiseScale, float& outFallScale)
    1.99 +{
   1.100 +    outRiseScale = 0.1f;
   1.101 +    outFallScale = 0.05f;	// falling issues are hardly visible
   1.102 +}
   1.103 +
   1.104 +double DistortionRenderer::WaitTillTime(double absTime)
   1.105 +{
   1.106 +    double initialTime = ovr_GetTimeInSeconds();
   1.107 +    if (initialTime >= absTime)
   1.108 +        return 0.0;
   1.109 +
   1.110 +    double newTime = initialTime;
   1.111 +
   1.112 +    while (newTime < absTime)
   1.113 +    {
   1.114 +// TODO: Needs further testing before enabling it on all Windows configs
   1.115 +#if 0 //def OVR_OS_WIN32
   1.116 +        double remainingWaitTime = absTime - newTime;
   1.117 +
   1.118 +        // don't yield if <2ms
   1.119 +        if(remainingWaitTime > 0.002)
   1.120 +        {
   1.121 +            // round down wait time to closest 1 ms
   1.122 +            int roundedWaitTime = (remainingWaitTime * 1000);
   1.123 +
   1.124 +            waitableTimerInterval.QuadPart = -10000LL; // 10000 * 100 ns = 1 ms
   1.125 +            waitableTimerInterval.QuadPart *= roundedWaitTime;
   1.126 +
   1.127 +            SetWaitableTimer(timer, &waitableTimerInterval, 0, NULL, NULL, TRUE);
   1.128 +            DWORD waitResult = WaitForSingleObject(timer, roundedWaitTime + 3);   // give 3 ms extra time
   1.129 +            OVR_UNUSED(waitResult);
   1.130 +
   1.131 +#ifdef OVR_BUILD_DEBUG
   1.132 +            double sleptTime = ovr_GetTimeInSeconds() - newTime;
   1.133 +            // Make sure we didn't sleep too long and it is reliable, otherwise we might miss v-sync causing a stutter
   1.134 +            if (sleptTime > (roundedWaitTime + 2) * 0.001) 
   1.135 +            {
   1.136 +                OVR_DEBUG_LOG_TEXT(
   1.137 +                    ("[DistortionRenderer::WaitTillTime] Sleep interval too long: %f\n", sleptTime));
   1.138 +            }
   1.139 +            else
   1.140 +            {
   1.141 +                OVR_ASSERT(WAIT_OBJECT_0 == waitResult);
   1.142 +            }
   1.143 +#endif
   1.144 +        }
   1.145 +        else
   1.146 +#endif
   1.147 +        {
   1.148 +            for (int j = 0; j < 5; j++)
   1.149 +                OVR_PROCESSOR_PAUSE();
   1.150 +        }
   1.151 +
   1.152 +        newTime = ovr_GetTimeInSeconds();
   1.153 +    }
   1.154 +
   1.155 +    // How long we waited
   1.156 +    return newTime - initialTime;
   1.157 +}
   1.158 +
   1.159 +}} // namespace OVR::CAPI
   1.160 +