aboutsummaryrefslogtreecommitdiffstats
path: root/LibOVR/Src/CAPI/D3D9/CAPI_D3D9_DistortionRenderer.cpp
blob: 05508ea5811838c5d33930f5f4144c7e9f271b6d (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/************************************************************************************

Filename    :   CAPI_D3D1X_DistortionRenderer.cpp
Content     :   Experimental distortion renderer
Created     :   March 7th, 2014
Authors     :   Tom Heath

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.

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

#include "CAPI_D3D9_DistortionRenderer.h"
#define OVR_D3D_VERSION 9
#include "../../OVR_CAPI_D3D.h"

#include <initguid.h>
DEFINE_GUID(IID_OVRDirect3DDevice9EX, 0xe6d58f10, 0xffa1, 0x4748, 0x85, 0x9f, 0xbc, 0xd7, 0xea, 0xe8, 0xfc, 0x1);

namespace OVR { namespace CAPI { namespace D3D9 {


///QUESTION : Why not just a normal constructor?
CAPI::DistortionRenderer* DistortionRenderer::Create(ovrHmd hmd,
                                                     FrameTimeManager& timeManager,
                                                     const HMDRenderState& renderState)
{
    return new DistortionRenderer(hmd, timeManager, renderState);
}

DistortionRenderer::DistortionRenderer(ovrHmd hmd, FrameTimeManager& timeManager,
                                       const HMDRenderState& renderState)
  : CAPI::DistortionRenderer(ovrRenderAPI_D3D9, hmd, timeManager, renderState),
    Device(NULL),
    SwapChain(NULL),
    VertexDecl(NULL),
    PixelShader(NULL),
    VertexShader(NULL),
    VertexShaderTimewarp(NULL),
   //screenSize(),
    ResolutionInPixels(0,0)
  //eachEye[]
{
    ScreenSize.w = 0;
    ScreenSize.h = 0;
    InitLatencyTester(renderState);

    for (int i = 0; i < 2; ++i)
    {
        eachEye[i].dxIndices = nullptr;
        eachEye[i].dxVerts = nullptr;
    }
}


/**********************************************/
DistortionRenderer::~DistortionRenderer()
{
	//Release any memory
    if (eachEye[0].dxIndices)
    {
        eachEye[0].dxIndices->Release();
    }
    if (eachEye[0].dxVerts)
    {
        eachEye[0].dxVerts->Release();
    }
    if (eachEye[1].dxIndices)
    {
        eachEye[1].dxIndices->Release();
    }
    if (eachEye[1].dxVerts)
    {
        eachEye[1].dxVerts->Release();
    }
}


/******************************************************************************/
bool DistortionRenderer::Initialize(const ovrRenderAPIConfig* apiConfig)
{
	///QUESTION - what is returned bool for???  Are we happy with this true, if not config.
    const ovrD3D9Config * config = (const ovrD3D9Config*)apiConfig;
    if (!config)                return true; 
    if (!config->D3D9.pDevice)  return false;

    if (System::DirectDisplayEnabled())
    {
        Ptr<IUnknown> ovrDevice;
        if (config->D3D9.pDevice->QueryInterface(IID_OVRDirect3DDevice9EX, (void**)&ovrDevice.GetRawRef()) == E_NOINTERFACE)
        {
            OVR_DEBUG_LOG_TEXT(("ovr_Initialize() or ovr_InitializeRenderingShim() wasn't called before the D3D9 device was created."));
        }
    }

	//Glean all the required variables from the input structures
	Device         = config->D3D9.pDevice;
    SwapChain      = config->D3D9.pSwapChain;
	ScreenSize     = config->D3D9.Header.BackBufferSize;

	GfxState = *new GraphicsState(Device, RState.DistortionCaps);

	CreateVertexDeclaration();
	CreateDistortionShaders();
	CreateDistortionModels();

    return true;
}

void DistortionRenderer::InitLatencyTester(const HMDRenderState& RenderState)
{
    ResolutionInPixels = RenderState.OurHMDInfo.ResolutionInPixels;
}


/**************************************************************/
void DistortionRenderer::SubmitEye(int eyeId, const ovrTexture* eyeTexture)
{
	//Doesn't do a lot in here??
	const ovrD3D9Texture* tex = (const ovrD3D9Texture*)eyeTexture;

	//Write in values
    eachEye[eyeId].texture = tex->D3D9.pTexture;

	// Its only at this point we discover what the viewport of the texture is.
	// because presumably we allow users to realtime adjust the resolution.
    eachEye[eyeId].TextureSize    = tex->D3D9.Header.TextureSize;
    eachEye[eyeId].RenderViewport = tex->D3D9.Header.RenderViewport;

    const ovrEyeRenderDesc& erd = RState.EyeRenderDesc[eyeId];
    
    ovrHmd_GetRenderScaleAndOffset( erd.Fov,
                                    eachEye[eyeId].TextureSize, eachEye[eyeId].RenderViewport,
                                    eachEye[eyeId].UVScaleOffset );

	if (RState.DistortionCaps & ovrDistortionCap_FlipInput)
	{
		eachEye[eyeId].UVScaleOffset[0].y = -eachEye[eyeId].UVScaleOffset[0].y;
		eachEye[eyeId].UVScaleOffset[1].y = 1.0f - eachEye[eyeId].UVScaleOffset[1].y;
	}
}

void DistortionRenderer::renderEndFrame()
{
    RenderBothDistortionMeshes();

    if(RegisteredPostDistortionCallback)
        RegisteredPostDistortionCallback(Device);

    if (LatencyTest2Active)
    {
        renderLatencyPixel(LatencyTest2DrawColor);
    }
}

/******************************************************************/
void DistortionRenderer::EndFrame(bool swapBuffers)
{
	///QUESTION : Clear the screen? 
	///QUESTION : Ensure the screen is the render target

    // Don't spin if we are explicitly asked not to
    if (RState.DistortionCaps & ovrDistortionCap_TimeWarp &&
        !(RState.DistortionCaps & ovrDistortionCap_ProfileNoTimewarpSpinWaits))
    {
        if (!TimeManager.NeedDistortionTimeMeasurement())
        {
            // Wait for timewarp distortion if it is time and Gpu idle
            FlushGpuAndWaitTillTime(TimeManager.GetFrameTiming().TimewarpPointTime);

            renderEndFrame();
        }
        else
        {
            // If needed, measure distortion time so that TimeManager can better estimate
            // latency-reducing time-warp wait timing.
            WaitUntilGpuIdle();
            double  distortionStartTime = ovr_GetTimeInSeconds();

            renderEndFrame();

            WaitUntilGpuIdle();
            TimeManager.AddDistortionTimeMeasurement(ovr_GetTimeInSeconds() - distortionStartTime);
        }
    }
    else
    {
        renderEndFrame();
    }

    if (LatencyTestActive)
    {
        renderLatencyQuad(LatencyTestDrawColor);
    }

    if (swapBuffers)
    {
        if (SwapChain)
        {
            SwapChain->Present(NULL, NULL, NULL, NULL, 0);
        }
        else
        {
		    Device->Present( NULL, NULL, NULL, NULL );
        }

        // Force GPU to flush the scene, resulting in the lowest possible latency.
        // It's critical that this flush is *after* present.
        // Doesn't need to be done if running through the Oculus driver.
        if (RState.OurHMDInfo.InCompatibilityMode &&
            !(RState.DistortionCaps & ovrDistortionCap_ProfileNoTimewarpSpinWaits))
        {
            WaitUntilGpuIdle();
        }
    }
}


void DistortionRenderer::WaitUntilGpuIdle()
{
	if(Device)
    {
         IDirect3DQuery9* pEventQuery=NULL ;
         Device->CreateQuery(D3DQUERYTYPE_EVENT, &pEventQuery) ;

         if(pEventQuery!=NULL)
         {
            pEventQuery->Issue(D3DISSUE_END) ;
            while(S_FALSE == pEventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH)){}
            pEventQuery->Release();
         }
     }
}

double DistortionRenderer::FlushGpuAndWaitTillTime(double absTime)
{
	double initialTime = ovr_GetTimeInSeconds();
	if (initialTime >= absTime)
		return 0.0;

	WaitUntilGpuIdle();

    return WaitTillTime(absTime);
}


//-----------------------------------------------------------------------------
// Latency Tester Quad

static void ConvertSRGB(unsigned char c[3])
{
    for (int i = 0; i < 3; ++i)
    {
        double d = (double)c[i];
        double ds = d / 255.;

        if (ds <= 0.04045)
        {
            d /= 12.92;
        }
        else
        {
            d = 255. * pow((ds + 0.055) / 1.055, 2.4);
        }

        int color = (int)d;
        if (color < 0)
        {
            color = 0;
        }
        else if (color > 255)
        {
            color = 255;
        }

        c[i] = (unsigned char)color;
    }
}

void DistortionRenderer::renderLatencyQuad(unsigned char* color)
{
    D3DRECT rect = { ResolutionInPixels.w / 4, ResolutionInPixels.h / 4, ResolutionInPixels.w * 3 / 4, ResolutionInPixels.h * 3 / 4 };
    unsigned char c[3] = { color[0], color[1], color[2] };

    if (RState.DistortionCaps & ovrDistortionCap_SRGB)
    {
        ConvertSRGB(c);
    }

    Device->Clear(1, &rect, D3DCLEAR_TARGET, D3DCOLOR_RGBA(c[0], c[1], c[2], 255), 1, 0);
}

#ifdef OVR_BUILD_DEBUG
#define OVR_LATENCY_PIXEL_SIZE 20
#else
#define OVR_LATENCY_PIXEL_SIZE 5
#endif

void DistortionRenderer::renderLatencyPixel(unsigned char* color)
{
    D3DRECT rect = { ResolutionInPixels.w - OVR_LATENCY_PIXEL_SIZE, 0, ResolutionInPixels.w, OVR_LATENCY_PIXEL_SIZE };
    unsigned char c[3] = { color[0], color[1], color[2] };

    if (RState.DistortionCaps & ovrDistortionCap_SRGB)
    {
        ConvertSRGB(c);
    }

    Device->Clear(1, &rect, D3DCLEAR_TARGET, D3DCOLOR_RGBA(c[0], c[1], c[2], 255), 1, 0);
}


//-----------------------------------------------------------------------------
// GraphicsState

DistortionRenderer::GraphicsState::GraphicsState(IDirect3DDevice9* d, unsigned distortionCaps)
: Device(d)
, NumSavedStates(0)
, DistortionCaps(distortionCaps)
{
    #if defined(OVR_BUILD_DEBUG)
        memset(SavedState, 0, sizeof(SavedState));
    #endif
}

void DistortionRenderer::GraphicsState::RecordAndSetState(int which, int type, DWORD newValue)
{
	SavedStateType * sst = &SavedState[NumSavedStates++];
	sst->which = which;
	sst->type = type;
	if (which == 0)
	{
		Device->GetSamplerState(0, (D3DSAMPLERSTATETYPE)type, &sst->valueToRevertTo);
		Device->SetSamplerState(0, (D3DSAMPLERSTATETYPE)type, newValue);
	}
	else
	{
		Device->GetRenderState((D3DRENDERSTATETYPE)type, &sst->valueToRevertTo);
		Device->SetRenderState((D3DRENDERSTATETYPE)type, newValue);
	}
}

void DistortionRenderer::GraphicsState::Save()
{
	//Record and set rasterizer and sampler states.

	NumSavedStates=0;

    RecordAndSetState(0, D3DSAMP_MINFILTER,          D3DTEXF_LINEAR );
    RecordAndSetState(0, D3DSAMP_MAGFILTER,          D3DTEXF_LINEAR );
    RecordAndSetState(0, D3DSAMP_MIPFILTER,          D3DTEXF_LINEAR );
    RecordAndSetState(0, D3DSAMP_BORDERCOLOR,        0x000000 );
    RecordAndSetState(0, D3DSAMP_ADDRESSU,           D3DTADDRESS_BORDER );
    RecordAndSetState(0, D3DSAMP_ADDRESSV,           D3DTADDRESS_BORDER );
    RecordAndSetState(0, D3DSAMP_SRGBTEXTURE,        (DistortionCaps & ovrDistortionCap_SRGB) ? TRUE : FALSE );

	RecordAndSetState(1, D3DRS_MULTISAMPLEANTIALIAS, FALSE );
	RecordAndSetState(1, D3DRS_DITHERENABLE,         FALSE );
	RecordAndSetState(1, D3DRS_ZENABLE,              FALSE );
    RecordAndSetState(1, D3DRS_ZWRITEENABLE,         TRUE   );
    RecordAndSetState(1, D3DRS_ZFUNC,                D3DCMP_LESSEQUAL   );
    RecordAndSetState(1, D3DRS_CULLMODE ,            D3DCULL_CCW  );
   	RecordAndSetState(1, D3DRS_ALPHABLENDENABLE ,    FALSE );
   	RecordAndSetState(1, D3DRS_DEPTHBIAS ,           0 );
    RecordAndSetState(1, D3DRS_SRCBLEND ,            D3DBLEND_SRCALPHA );
    RecordAndSetState(1, D3DRS_DESTBLEND ,           D3DBLEND_INVSRCALPHA   );
   	RecordAndSetState(1, D3DRS_FILLMODE,             D3DFILL_SOLID );
    RecordAndSetState(1, D3DRS_ALPHATESTENABLE,      FALSE);
 	RecordAndSetState(1, D3DRS_DEPTHBIAS ,           0 );
    RecordAndSetState(1, D3DRS_LIGHTING,             FALSE );
   	RecordAndSetState(1, D3DRS_FOGENABLE,            FALSE );
    RecordAndSetState(1, D3DRS_SRGBWRITEENABLE,      (DistortionCaps & ovrDistortionCap_SRGB) ? TRUE : FALSE );
}


void DistortionRenderer::GraphicsState::Restore()
{
	for (int i = 0; i < NumSavedStates; i++)
	{
		SavedStateType * sst = &SavedState[i];
		if (sst->which == 0)
		{
			Device->SetSamplerState(0, (D3DSAMPLERSTATETYPE)sst->type, sst->valueToRevertTo);
		}
		else
		{
			Device->SetRenderState((D3DRENDERSTATETYPE)sst->type, sst->valueToRevertTo);
		}
	}
}


}}} // OVR::CAPI::D3D1X