aboutsummaryrefslogtreecommitdiffstats
path: root/LibOVR/Src/CAPI/CAPI_DistortionRenderer.h
blob: 0c933878f1b77b8c383de4509fadda088f3755b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/************************************************************************************

Filename    :   CAPI_DistortionRenderer.h
Content     :   Abstract interface for platform-specific rendering of distortion
Created     :   February 2, 2014
Authors     :   Michael Antonov

Copyright   :   Copyright 2014 Oculus VR, LLC All Rights reserved.

Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License"); 
you may not use the Oculus VR Rift SDK except in compliance with the License, 
which is provided at the time of installation or download, or which 
otherwise accompanies this software in either electronic or hard copy form.

You may obtain a copy of the License at

http://www.oculusvr.com/licenses/LICENSE-3.2 

Unless required by applicable law or agreed to in writing, the Oculus VR SDK 
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

************************************************************************************/

#ifndef OVR_CAPI_DistortionRenderer_h
#define OVR_CAPI_DistortionRenderer_h

#include "CAPI_HMDRenderState.h"
#include "CAPI_FrameTimeManager.h"

typedef void (*PostDistortionCallback)(void* pRenderContext);

namespace OVR { namespace CAPI {

//-------------------------------------------------------------------------------------
// ***** CAPI::DistortionRenderer

// DistortionRenderer implements rendering of distortion and other overlay elements
// in platform-independent way.
// Platform-specific renderer back ends for CAPI are derived from this class.

class  DistortionRenderer : public RefCountBase<DistortionRenderer>
{
    // Quiet assignment compiler warning.
    void operator = (const DistortionRenderer&) { }
public:
    
    DistortionRenderer(ovrRenderAPIType api, ovrHmd hmd,
                       FrameTimeManager& timeManager,              
                       const HMDRenderState& renderState) :
		LastUsedOverdriveTextureIndex(-1),
        LatencyTestActive(false),
        LatencyTest2Active(false),
        RenderAPI(api),
        HMD(hmd),
        TimeManager(timeManager),
        RState(renderState),
        GfxState(),
        RegisteredPostDistortionCallback(NULL)
    {
#ifdef OVR_OS_WIN32
        timer = CreateWaitableTimer(NULL, TRUE, NULL);
        OVR_ASSERT(timer != NULL);
#endif
    }
    virtual ~DistortionRenderer()
    {
    }
    

    // Configures the Renderer based on externally passed API settings. Must be
    // called before use.
    // Under D3D, apiConfig includes D3D Device pointer, back buffer and other
    // needed structures.
    virtual bool Initialize(const ovrRenderAPIConfig* apiConfig) = 0;

    // Submits one eye texture for rendering. This is in the separate method to
    // allow "submit as you render" scenarios on horizontal screens where one
    // eye can be scanned out before the other.
    virtual void SubmitEye(int eyeId, const ovrTexture* eyeTexture) = 0;

    // Finish the frame, optionally swapping buffers.
    // Many implementations may actually apply the distortion here.
    virtual void EndFrame(bool swapBuffers) = 0;
    
    void RegisterPostDistortionCallback(PostDistortionCallback postDistortionCallback)
    {
        RegisteredPostDistortionCallback = postDistortionCallback;
    }

	// Stores the current graphics pipeline state so it can be restored later.
	void SaveGraphicsState() { if (GfxState && !(RState.DistortionCaps & ovrDistortionCap_NoRestore)) GfxState->Save(); }

	// Restores the saved graphics pipeline state.
	void RestoreGraphicsState() { if (GfxState && !(RState.DistortionCaps & ovrDistortionCap_NoRestore)) GfxState->Restore(); }

    // *** Creation Factory logic
    
    ovrRenderAPIType GetRenderAPI() const { return RenderAPI; }

    // Creation function for this interface, registered for API.
    typedef DistortionRenderer* (*CreateFunc)(ovrHmd hmd,
                                              FrameTimeManager &timeManager,
                                              const HMDRenderState& renderState);

    static CreateFunc APICreateRegistry[ovrRenderAPI_Count];

    // Color is expected to be 3 byte RGB
    void SetLatencyTestColor(unsigned char* color);
    void SetLatencyTest2Color(unsigned char* color);
    
protected:
	// Used for pixel luminance overdrive on DK2 displays
	// A copy of back buffer images will be ping ponged
	// TODO: figure out 0 dynamically based on DK2 latency?
	static const int	NumOverdriveTextures = 2;
	int					LastUsedOverdriveTextureIndex;

    bool                LatencyTestActive;
    unsigned char       LatencyTestDrawColor[3];
    bool                LatencyTest2Active;
    unsigned char       LatencyTest2DrawColor[3];

    bool IsOverdriveActive()
	{
		// doesn't make sense to use overdrive when vsync is disabled as we cannot guarantee
		// when the rendered frame will be displayed
		return LastUsedOverdriveTextureIndex >= 0 &&
                !((RState.EnabledHmdCaps & ovrHmdCap_NoVSync) > 0) &&
                (RState.DistortionCaps & ovrDistortionCap_Chromatic);
	}

    void GetOverdriveScales(float& outRiseScale, float& outFallScale);

    double WaitTillTime(double absTime);

#ifdef OVR_OS_WIN32
    HANDLE timer;
    LARGE_INTEGER waitableTimerInterval;
#endif

    class GraphicsState : public RefCountBase<GraphicsState>
    {
    public:
        GraphicsState() : IsValid(false) {}
        virtual ~GraphicsState() {}
        virtual void Save() = 0;
        virtual void Restore() = 0;
        
    protected:
        bool IsValid;
    };
    
    const ovrRenderAPIType  RenderAPI;
    const ovrHmd            HMD;
    FrameTimeManager&       TimeManager;
    const HMDRenderState&   RState;
    Ptr<GraphicsState>      GfxState;
    PostDistortionCallback  RegisteredPostDistortionCallback;
};

}} // namespace OVR::CAPI


#endif // OVR_CAPI_DistortionRenderer_h