-
Notifications
You must be signed in to change notification settings - Fork 16
/
d3d.cpp
371 lines (316 loc) · 11.6 KB
/
d3d.cpp
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
#if USE_D3D
#pragma comment( lib, "oculus/LibOVR/Lib/Windows/Win32/Release/VS2013/libovr.lib" )
#include "aggdraw.h"
#include "GPU_osd.h"
#include "main.h"
#include "lua-engine.h"
#include "mednafen/src/mednafen.h"
#include "pcejin.h"
#include "vb.h"
#include "oculus/Win32_DirectXAppUtil.h"
#include "OVR_CAPI_D3D.h"
//------------------------------------------------------------
// ovrSwapTextureSet wrapper class that also maintains the render target views
// needed for D3D11 rendering.
struct OculusTexture
{
ovrSession Session;
ovrTextureSwapChain TextureChain;
std::vector<ID3D11RenderTargetView*> TexRtv;
OculusTexture() :
Session( nullptr ),
TextureChain( nullptr )
{}
bool Init( ovrSession session, int sizeW, int sizeH )
{
Session = session;
ovrTextureSwapChainDesc desc = {};
desc.Type = ovrTexture_2D;
desc.ArraySize = 1;
desc.Format = OVR_FORMAT_R8G8B8A8_UNORM_SRGB;
desc.Width = sizeW;
desc.Height = sizeH;
desc.MipLevels = 1;
desc.SampleCount = 1;
desc.MiscFlags = ovrTextureMisc_DX_Typeless;
desc.BindFlags = ovrTextureBind_DX_RenderTarget;
desc.StaticImage = ovrFalse;
ovrResult result = ovr_CreateTextureSwapChainDX( session, DIRECTX.Device, &desc, &TextureChain );
if ( !OVR_SUCCESS( result ) )
return false;
int textureCount = 0;
ovr_GetTextureSwapChainLength( Session, TextureChain, &textureCount );
for ( int i = 0; i < textureCount; ++i )
{
ID3D11Texture2D* tex = nullptr;
ovr_GetTextureSwapChainBufferDX( Session, TextureChain, i, IID_PPV_ARGS( &tex ) );
D3D11_RENDER_TARGET_VIEW_DESC rtvd = {};
rtvd.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
rtvd.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
ID3D11RenderTargetView* rtv;
DIRECTX.Device->CreateRenderTargetView( tex, &rtvd, &rtv );
TexRtv.push_back( rtv );
tex->Release();
}
return true;
}
~OculusTexture()
{
for ( int i = 0; i < (int)TexRtv.size(); ++i )
{
Release( TexRtv[i] );
}
if ( TextureChain )
{
ovr_DestroyTextureSwapChain( Session, TextureChain );
}
}
ID3D11RenderTargetView* GetRTV()
{
int index = 0;
ovr_GetTextureSwapChainCurrentIndex( Session, TextureChain, &index );
return TexRtv[index];
}
// Commit changes
void Commit()
{
ovr_CommitTextureSwapChain( Session, TextureChain );
}
};
static DepthBuffer* s_eyeDepthBuffers[2];
static ovrRecti s_eyeRenderViewports[2];
static OculusTexture* s_eyeTextures[2];
static long long s_frameIndex = 0;
static ovrHmdDesc s_hmdDesc;
static ovrGraphicsLuid s_luid;
static ovrMirrorTexture s_mirrorTexture = NULL;
static Scene s_immersiveScene;
static Scene s_scene;
static ovrSession s_session;
static Texture* s_vbEyeTexture;
static void __forceinline convert32( int eye ){
uint8 *pb_ptr = (uint8*)convert_buffer;
MDFN_Surface* eyeSurface = eye == 0 ? espec.surface : espec.surface2;
for(int y = espec.DisplayRect.y; y < espec.DisplayRect.y + espec.DisplayRect.h; y++)
{
uint16 meow_width = (espec.LineWidths[0].w == ~0) ? espec.DisplayRect.w : espec.LineWidths[y].w;
int meow_x = (espec.LineWidths[0].w == ~0) ? espec.DisplayRect.x : espec.LineWidths[y].x;
uint32 *fb_line = eyeSurface->pixels + y * (MDFNGameInfo->pitch >> 2) + meow_x;
for(int x = 0; x < meow_width; x++)
{
uint32 pixel = fb_line[x];
int r, g, b;
eyeSurface->DecodeColor(pixel, r, g, b);
*pb_ptr++ = r;
*pb_ptr++ = g;
*pb_ptr++ = b;
*pb_ptr++ = 255;
}
}
}
void OculusInit()
{
ovrResult result = ovr_Initialize( nullptr );
assert( OVR_SUCCESS( result ) );
}
static bool SetupOculus()
{
ovrResult result = ovr_Create( &s_session, &s_luid );
if ( OVR_FAILURE( result ) )
{
MessageBoxA( NULL, "Oculus Rift not detected.", "", MB_OK );
return false;
}
s_hmdDesc = ovr_GetHmdDesc( s_session );
if ( !DIRECTX.InitDevice( s_hmdDesc.Resolution.w / 2, s_hmdDesc.Resolution.h / 2, reinterpret_cast<LUID*>( &s_luid ) ) )
{
MessageBoxA( NULL, "Failed to initialize graphics device.", "", MB_OK );
return false;
}
for ( int eye = 0 ; eye < 2 ; ++eye )
{
ovrSizei idealSize = ovr_GetFovTextureSize( s_session, (ovrEyeType)eye, s_hmdDesc.DefaultEyeFov[eye], 2.0f );
// Choose power-of-two sizes that will fit the ideal size
ovrSizei textureSize =
{
pow( 2, ceil( log( idealSize.w ) / log( 2 ) ) ),
pow( 2, ceil( log( idealSize.h ) / log( 2 ) ) )
};
s_eyeTextures[eye] = new OculusTexture();
if ( !s_eyeTextures[eye]->Init( s_session, textureSize.w, textureSize.h ) )
{
MessageBoxA( NULL, "Failed to create eye texture.", "", MB_OK );
return false;
}
s_eyeDepthBuffers[eye] = new DepthBuffer( DIRECTX.Device, textureSize.w, textureSize.h );
s_eyeRenderViewports[eye].Pos.x = textureSize.w / 2 - idealSize.w / 2;
s_eyeRenderViewports[eye].Pos.y = textureSize.h / 2 - idealSize.h / 2;
s_eyeRenderViewports[eye].Size = idealSize;
if ( !s_eyeTextures[eye]->TextureChain )
{
MessageBoxA( NULL, "Failed to create eye texture.", "", MB_OK );
return false;
}
}
// Create a mirror to see on the monitor.
ovrMirrorTextureDesc mirrorDesc = {};
mirrorDesc.Format = OVR_FORMAT_R8G8B8A8_UNORM_SRGB;
mirrorDesc.Width = DIRECTX.WinSizeW;
mirrorDesc.Height = DIRECTX.WinSizeH;
result = ovr_CreateMirrorTextureDX( s_session, DIRECTX.Device, &mirrorDesc, &s_mirrorTexture );
if ( OVR_FAILURE( result ) )
{
MessageBoxA( NULL, "Failed to create mirror texture.", "", MB_OK );
return false;
}
s_vbEyeTexture = new Texture( pcejin.width, pcejin.height, false );
s_immersiveScene.Add( new Model( new Material( s_vbEyeTexture, Material::MAT_TRANS | Material::MAT_POINT_FILTER ), -0.5f, -0.5f, 0.5f, 0.5f ) );
s_scene.Add( new Model( new Material( s_vbEyeTexture, Material::MAT_TRANS ), -0.5f, -0.5f, 0.5f, 0.5f ) );
ovr_SetTrackingOriginType( s_session, ovrTrackingOrigin_EyeLevel );
}
bool D3DInit()
{
DIRECTX.SetWindow( g_hWnd );
if ( !SetupOculus() )
{
return false;
}
return true;
}
static void UpdateTexture( ovrEyeType eye )
{
DIRECTX.Context->UpdateSubresource( s_vbEyeTexture->Tex, 0, NULL, convert_buffer, s_vbEyeTexture->SizeW * 4, s_vbEyeTexture->SizeW * s_vbEyeTexture->SizeH * 4 );
}
void render( ovrEyeType eye )
{
convert32( eye == ovrEye_Left ? 1 : 0 );
CallRegisteredLuaFunctions( LUACALL_AFTEREMULATIONGUI );
osd->update();
DrawHUD();
osd->clear();
aggDraw.hud->attach( convert_buffer, pcejin.width, pcejin.height, 4 * pcejin.width );
UpdateTexture( eye );
}
void render()
{
if( !pcejin.romLoaded || espec.skip )
{
return;
}
if( ( pcejin.width != espec.DisplayRect.w ) || ( pcejin.height != espec.DisplayRect.h ) )
{
pcejin.width = espec.DisplayRect.w;
pcejin.height = espec.DisplayRect.h;
if( !pcejin.maximized )
{
ScaleScreen( (float)pcejin.windowSize );
}
}
SetInputDisplayCharacters(pcejin.pads);
// Call ovr_GetRenderDesc each frame to get the ovrEyeRenderDesc, as the returned values (e.g. HmdToEyeOffset) may change at runtime.
ovrEyeRenderDesc eyeRenderDescs[2];
eyeRenderDescs[0] = ovr_GetRenderDesc( s_session, ovrEye_Left, s_hmdDesc.DefaultEyeFov[0] );
eyeRenderDescs[1] = ovr_GetRenderDesc( s_session, ovrEye_Right, s_hmdDesc.DefaultEyeFov[1] );
// Get both eye poses simultaneously, with IPD offset already included.
ovrPosef eyeRenderPoses[2];
ovrVector3f hmdToEyeOffsets[2] = { eyeRenderDescs[0].HmdToEyeOffset, eyeRenderDescs[1].HmdToEyeOffset };
double sensorSampleTime; // sensorSampleTime is fed into the layer later
ovr_GetEyePoses( s_session, s_frameIndex, ovrTrue, hmdToEyeOffsets, eyeRenderPoses, &sensorSampleTime );
bool immersiveMode = MDFN_IEN_VB::GetSplitMode() == MDFN_IEN_VB::VB3DMODE_OVR_IMMERSIVE;
for ( int eye = 0 ; eye < 2 ; ++eye )
{
// Clear and set up rendertarget
#ifdef _DEBUG
DIRECTX.SetAndClearRenderTarget( s_eyeTextures[eye]->GetRTV(), s_eyeDepthBuffers[eye], 0.0f, 1.0f );
#else // #ifdef _DEBUG
DIRECTX.SetAndClearRenderTarget( s_eyeTextures[eye]->GetRTV(), s_eyeDepthBuffers[eye] );
#endif // #else // #ifdef _DEBUG
DIRECTX.SetViewport( (float)s_eyeRenderViewports[eye].Pos.x, (float)s_eyeRenderViewports[eye].Pos.y,
(float)s_eyeRenderViewports[eye].Size.w, (float)s_eyeRenderViewports[eye].Size.h );
// Copy virtual boy frame from CPU to GPU
render( eyeRenderDescs[eye].Eye );
if ( immersiveMode )
{
//Get the pose information in XM format
XMVECTOR eyeQuat = XMVectorSet( eyeRenderPoses[eye].Orientation.x, eyeRenderPoses[eye].Orientation.y,
eyeRenderPoses[eye].Orientation.z, eyeRenderPoses[eye].Orientation.w );
XMVECTOR eyePos = XMVectorSet( eyeRenderPoses[eye].Position.x, eyeRenderPoses[eye].Position.y, eyeRenderPoses[eye].Position.z, 0 );
Camera finalCam( &eyePos, &eyeQuat );
XMMATRIX view = finalCam.GetViewMatrix();
ovrMatrix4f p = ovrMatrix4f_Projection( eyeRenderDescs[eye].Fov, 0.01f, 10000.0f, ovrProjection_None );
XMMATRIX proj = XMMatrixSet( p.M[0][0], p.M[1][0], p.M[2][0], p.M[3][0],
p.M[0][1], p.M[1][1], p.M[2][1], p.M[3][1],
p.M[0][2], p.M[1][2], p.M[2][2], p.M[3][2],
p.M[0][3], p.M[1][3], p.M[2][3], p.M[3][3] );
XMMATRIX viewProj = XMMatrixMultiply( view, proj );
float scaleFactor = 0.5f;
float aspectRatio = (float)pcejin.width / pcejin.height;
XMMATRIX worldMat = XMMatrixSet( scaleFactor * aspectRatio, 0.0f, 0.0f, 0.0f,
0.0f, scaleFactor, 0.0f, 0.0f,
0.0f, 0.0f, scaleFactor, 0.0f,
0.0f, 0.0f, -0.5f, 1.0f );
XMMATRIX worldViewProj = XMMatrixMultiply( worldMat, viewProj );
s_immersiveScene.Render( &worldViewProj, 1.0f, 1.0f, 1.0f, 1.0f, true );
}
else
{
float scaleFactor = 1.0f;
float aspectRatio = (float)pcejin.width / pcejin.height / ( (float)s_eyeRenderViewports[eye].Size.w / s_eyeRenderViewports[eye].Size.h );
XMMATRIX projMat = XMMatrixSet( scaleFactor, 0, 0, 0,
0, scaleFactor / aspectRatio, 0, 0,
0, 0, scaleFactor, 0,
0, 0, 0, 1 );
float ipdX = eyeRenderDescs[eye].HmdToEyeOffset.x * 5.0f;
float ipdY = eyeRenderDescs[eye].HmdToEyeOffset.y * 5.0f;
XMMATRIX projWithIPDMat = XMMatrixMultiply( projMat, XMMatrixTranslation( -ipdX, -ipdY, 0.0f ) );
s_scene.Render( &projWithIPDMat, 1.0f, 1.0f, 1.0f, 1.0f, true );
}
// Commit rendering to the swap chain
s_eyeTextures[eye]->Commit();
}
// Initialize our single full screen Fov layer.
ovrLayerEyeFov ld = {};
if ( immersiveMode )
{
ld.Header.Type = ovrLayerType_EyeFov;
ld.Header.Flags = ovrLayerFlag_HighQuality;
for ( int eye = 0 ; eye < 2 ; ++eye )
{
ld.ColorTexture[eye] = s_eyeTextures[eye]->TextureChain;
ld.Viewport[eye] = s_eyeRenderViewports[eye];
ld.Fov[eye] = s_hmdDesc.DefaultEyeFov[eye];
ld.RenderPose[eye] = eyeRenderPoses[eye];
ld.SensorSampleTime = sensorSampleTime;
}
}
else
{
ld.Header.Type = ovrLayerType_EyeFov;
ld.Header.Flags = ovrLayerFlag_HeadLocked | ovrLayerFlag_HighQuality;
for ( int eye = 0 ; eye < 2 ; ++eye )
{
ld.ColorTexture[eye] = s_eyeTextures[eye]->TextureChain;
ld.Viewport[eye] = s_eyeRenderViewports[eye];
ld.Fov[eye] = s_hmdDesc.DefaultEyeFov[eye];
ld.RenderPose[eye] = ovrPosef { ovrQuatf { 0.0f, 0.0f, 0.0f, 1.0f }, ovrVector3f { 0.0f, 0.0f, 0.0f } };
ld.SensorSampleTime = sensorSampleTime;
}
}
ovrLayerHeader* layers = &ld.Header;
ovrResult frameResult = ovr_SubmitFrame( s_session, s_frameIndex, nullptr, &layers, 1 );
assert( OVR_SUCCESS( frameResult ) );
ovrSessionStatus sessionStatus;
ovr_GetSessionStatus( s_session, &sessionStatus );
if ( sessionStatus.ShouldRecenter )
{
ovr_RecenterTrackingOrigin( s_session );
}
// Render mirror
ID3D11Texture2D* tex = NULL;
ovr_GetMirrorTextureBufferDX( s_session, s_mirrorTexture, IID_PPV_ARGS( &tex ) );
DIRECTX.Context->CopyResource( DIRECTX.BackBuffer, tex );
tex->Release();
DIRECTX.SwapChain->Present( 0, 0 );
s_frameIndex++;
}
#endif // #if USE_D3D