This commit is contained in:
Branimir Karadžić
2016-04-04 22:38:45 -07:00
parent ea4a0d24af
commit d655fa1099
2 changed files with 85 additions and 90 deletions

View File

@@ -29,17 +29,16 @@ namespace bgfx
ovrResult initialized = ovr_Initialize(NULL);
ovrGraphicsLuid luid;
BX_WARN(initialized == ovrSuccess, "Unable to create OVR device.");
if (initialized != ovrSuccess)
{
BX_TRACE("Unable to create OVR device.");
return;
}
initialized = ovr_Create(&m_hmd, &luid);
if (initialized != ovrSuccess)
{
BX_WARN(initialized == ovrSuccess, "Unable to create OVR device.");
BX_TRACE("Unable to create OVR device.");
return;
}
@@ -62,12 +61,12 @@ namespace bgfx
{
BX_CHECK(!m_isenabled, "HMD not disabled.");
for (int i = 0; i < 2; i++)
for (uint32_t ii = 0; ii < 2; ++ii)
{
if (m_eyeBuffers[i])
if (m_eyeBuffers[ii])
{
m_eyeBuffers[i]->destroy(m_hmd);
BX_DELETE(g_allocator, m_eyeBuffers[i]);
m_eyeBuffers[ii]->destroy(m_hmd);
BX_DELETE(g_allocator, m_eyeBuffers[ii]);
}
}
@@ -102,9 +101,9 @@ namespace bgfx
return false;
}
for (int eyeIdx = 0; eyeIdx < ovrEye_Count; eyeIdx++)
for (uint32_t ii = 0; ii < 2; ++ii)
{
m_erd[eyeIdx] = ovr_GetRenderDesc(m_hmd, (ovrEyeType)eyeIdx, m_hmdDesc.DefaultEyeFov[eyeIdx]);
m_erd[ii] = ovr_GetRenderDesc(m_hmd, ovrEyeType(ii), m_hmdDesc.DefaultEyeFov[ii]);
}
m_isenabled = true;
@@ -141,9 +140,9 @@ namespace bgfx
}
// commit eyes to HMD
for (int eye = 0; eye < 2; eye++)
for (uint32_t ii = 0; ii < 2; ++ii)
{
ovr_CommitTextureSwapChain(m_hmd, m_eyeBuffers[eye]->m_swapTextureChain);
ovr_CommitTextureSwapChain(m_hmd, m_eyeBuffers[ii]->m_swapTextureChain);
}
_hmd.flags |= BGFX_HMD_RENDERING;
@@ -159,16 +158,16 @@ namespace bgfx
eyeLayer.Header.Type = ovrLayerType_EyeFov;
eyeLayer.Header.Flags = originBottomLeft ? ovrLayerFlag_TextureOriginAtBottomLeft : 0;
for (int eye = 0; eye < ovrEye_Count; eye++)
for (uint32_t ii = 0; ii < 2; ++ii)
{
eyeLayer.ColorTexture[eye] = m_eyeBuffers[eye]->m_swapTextureChain;
eyeLayer.Viewport[eye].Pos.x = 0;
eyeLayer.Viewport[eye].Pos.y = 0;
eyeLayer.Viewport[eye].Size.w = m_eyeBuffers[eye]->m_eyeTextureSize.w;
eyeLayer.Viewport[eye].Size.h = m_eyeBuffers[eye]->m_eyeTextureSize.h;
eyeLayer.Fov[eye] = m_hmdDesc.DefaultEyeFov[eye];
eyeLayer.RenderPose[eye] = m_pose[eye];
eyeLayer.SensorSampleTime = m_sensorSampleTime;
eyeLayer.ColorTexture[ii] = m_eyeBuffers[ii]->m_swapTextureChain;
eyeLayer.Viewport[ii].Pos.x = 0;
eyeLayer.Viewport[ii].Pos.y = 0;
eyeLayer.Viewport[ii].Size.w = m_eyeBuffers[ii]->m_eyeTextureSize.w;
eyeLayer.Viewport[ii].Size.h = m_eyeBuffers[ii]->m_eyeTextureSize.h;
eyeLayer.Fov[ii] = m_hmdDesc.DefaultEyeFov[ii];
eyeLayer.RenderPose[ii] = m_pose[ii];
eyeLayer.SensorSampleTime = m_sensorSampleTime;
}
// append all the layers to global list
@@ -201,7 +200,7 @@ namespace bgfx
{
if (NULL != m_hmd)
{
for (int ii = 0; ii < 2; ++ii)
for (uint32_t ii = 0; ii < 2; ++ii)
{
const ovrPosef& pose = m_pose[ii];
HMD::Eye& eye = _hmd.eye[ii];
@@ -220,9 +219,9 @@ namespace bgfx
eye.fov[3] = erd.Fov.RightTan;
ovrMatrix4f eyeProj = ovrMatrix4f_Projection(m_erd[ii].Fov, 0.01f, 1000.0f, ovrProjection_LeftHanded);
for (int jj = 0; jj < 4; ++jj)
for (uint32_t jj = 0; jj < 4; ++jj)
{
for (int kk = 0; kk < 4; ++kk)
for (uint32_t kk = 0; kk < 4; ++kk)
{
eye.projection[4 * jj + kk] = eyeProj.M[kk][jj];
}

View File

@@ -601,20 +601,16 @@ namespace bgfx { namespace d3d11
static PFN_GET_DEBUG_INTERFACE1 DXGIGetDebugInterface1;
#endif // USE_D3D11_DYNAMIC_LIB
#if BGFX_CONFIG_USE_OVR
#include <tinystl/vector.h>
// Oculus Rift eye buffer
struct OVRBufferDX11 : public OVRBufferI
struct OVRBufferD3D11 : public OVRBufferI
{
OVRBufferDX11(const ovrSession& session, int eyeIdx, ID3D11Device* d3dDevice, ID3D11DeviceContext* d3dCtx)
OVRBufferD3D11(const ovrSession& _session, int eyeIdx, ID3D11Device* _device, ID3D11DeviceContext* _deviceCtx)
{
m_d3dDevice = d3dDevice;
m_d3dContext = d3dCtx;
ovrHmdDesc hmdDesc = ovr_GetHmdDesc(session);
m_eyeTextureSize = ovr_GetFovTextureSize(session, (ovrEyeType)eyeIdx, hmdDesc.DefaultEyeFov[eyeIdx], 1.0f);
m_device = _device;
m_deviceCtx = _deviceCtx;
ovrHmdDesc hmdDesc = ovr_GetHmdDesc(_session);
m_eyeTextureSize = ovr_GetFovTextureSize(_session, (ovrEyeType)eyeIdx, hmdDesc.DefaultEyeFov[eyeIdx], 1.0f);
ovrTextureSwapChainDesc desc = {};
desc.Type = ovrTexture_2D;
@@ -628,28 +624,29 @@ namespace bgfx { namespace d3d11
desc.BindFlags = ovrTextureBind_DX_RenderTarget;
desc.StaticImage = ovrFalse;
ovrResult result = ovr_CreateTextureSwapChainDX(session, d3dDevice, &desc, &m_swapTextureChain);
ovrResult result = ovr_CreateTextureSwapChainDX(_session, _device, &desc, &m_swapTextureChain);
if (!OVR_SUCCESS(result))
if (!OVR_SUCCESS(result) )
{
BX_CHECK(false, "Could not create D3D11 OVR swap texture");
}
memset(m_eyeRtv, 0, sizeof(m_eyeRtv) );
int textureCount = 0;
ovr_GetTextureSwapChainLength(session, m_swapTextureChain, &textureCount);
ovr_GetTextureSwapChainLength(_session, m_swapTextureChain, &textureCount);
for (int i = 0; i < textureCount; ++i)
for (int ii = 0; ii < textureCount; ++ii)
{
ID3D11Texture2D* tex = NULL;
ovr_GetTextureSwapChainBufferDX(session, m_swapTextureChain, i, IID_PPV_ARGS(&tex));
ovr_GetTextureSwapChainBufferDX(_session, m_swapTextureChain, ii, IID_PPV_ARGS(&tex) );
D3D11_RENDER_TARGET_VIEW_DESC rtvd = {};
rtvd.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
rtvd.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
ID3D11RenderTargetView* rtv;
DX_CHECK(d3dDevice->CreateRenderTargetView(tex, &rtvd, &rtv));
m_eyeRtv.push_back(rtv);
tex->Release();
DX_CHECK(_device->CreateRenderTargetView(tex, &rtvd, &rtv) );
m_eyeRtv[ii] = rtv;
DX_RELEASE(tex, 1);
}
// setup depth buffer
@@ -666,21 +663,21 @@ namespace bgfx { namespace d3d11
dbDesc.MiscFlags = 0;
dbDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
ID3D11Texture2D* tex;
DX_CHECK(d3dDevice->CreateTexture2D(&dbDesc, NULL, &tex));
DX_CHECK(d3dDevice->CreateDepthStencilView(tex, NULL, &m_depthBuffer));
tex->Release();
DX_CHECK(_device->CreateTexture2D(&dbDesc, NULL, &tex) );
DX_CHECK(_device->CreateDepthStencilView(tex, NULL, &m_depthBuffer) );
DX_RELEASE(tex, 0);
}
void onRender(const ovrSession& session)
void onRender(const ovrSession& _session)
{
// Clear and set up rendertarget
int texIndex = 0;
ovr_GetTextureSwapChainCurrentIndex(session, m_swapTextureChain, &texIndex);
ovr_GetTextureSwapChainCurrentIndex(_session, m_swapTextureChain, &texIndex);
float black[] = { 0.f, 0.f, 0.f, 0.f }; // Important that alpha=0, if want pixels to be transparent, for manual layers
m_d3dContext->OMSetRenderTargets(1, &m_eyeRtv[texIndex], m_depthBuffer);
m_d3dContext->ClearRenderTargetView(m_eyeRtv[texIndex], black);
m_d3dContext->ClearDepthStencilView(m_depthBuffer, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1, 0);
m_deviceCtx->OMSetRenderTargets(1, &m_eyeRtv[texIndex], m_depthBuffer);
m_deviceCtx->ClearRenderTargetView(m_eyeRtv[texIndex], black);
m_deviceCtx->ClearDepthStencilView(m_depthBuffer, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1, 0);
D3D11_VIEWPORT D3Dvp;
D3Dvp.TopLeftX = 0;
@@ -689,45 +686,44 @@ namespace bgfx { namespace d3d11
D3Dvp.Height = (FLOAT)m_eyeTextureSize.h;
D3Dvp.MinDepth = 0;
D3Dvp.MaxDepth = 1;
m_d3dContext->RSSetViewports(1, &D3Dvp);
m_deviceCtx->RSSetViewports(1, &D3Dvp);
}
void destroy(const ovrSession& session)
void destroy(const ovrSession& _session)
{
for (size_t i = 0; i < m_eyeRtv.size(); ++i)
for (uint32_t ii = 0; ii < BX_COUNTOF(m_eyeRtv); ++ii)
{
m_eyeRtv[i]->Release();
DX_RELEASE(m_eyeRtv[ii], 0);
}
ovr_DestroyTextureSwapChain(session, m_swapTextureChain);
ovr_DestroyTextureSwapChain(_session, m_swapTextureChain);
m_depthBuffer->Release();
}
ID3D11Device* m_d3dDevice;
ID3D11DeviceContext* m_d3dContext;
stl::vector<ID3D11RenderTargetView *> m_eyeRtv;
ID3D11Device* m_device;
ID3D11DeviceContext* m_deviceCtx;
ID3D11RenderTargetView* m_eyeRtv[4];
ID3D11DepthStencilView* m_depthBuffer;
};
// Oculus Rift mirror
struct OVRMirrorDX11 : public OVRMirrorI
struct OVRMirrorD3D11 : public OVRMirrorI
{
OVRMirrorDX11(ID3D11Device* d3dDevice,
ID3D11DeviceContext* d3dCtx,
IDXGISwapChain* d3dSc) : m_d3dDevice(d3dDevice)
, m_d3dContext(d3dCtx)
, m_d3dSwapChain(d3dSc)
OVRMirrorD3D11(ID3D11Device* _device, ID3D11DeviceContext* _deviceCtx, IDXGISwapChain* _swapChain)
: m_device(_device)
, m_deviceCtx(_deviceCtx)
, m_swapChain(_swapChain)
{
}
void init(const ovrSession& session, int windowWidth, int windowHeight)
void init(const ovrSession& _session, int _width, int _height)
{
m_mirrorDesc.Format = OVR_FORMAT_R8G8B8A8_UNORM_SRGB;
m_mirrorDesc.Width = windowWidth;
m_mirrorDesc.Height = windowHeight;
ovrResult result = ovr_CreateMirrorTextureDX(session, m_d3dDevice, &m_mirrorDesc, &m_mirrorTexture);
m_mirrorDesc.Width = _width;
m_mirrorDesc.Height = _height;
ovrResult result = ovr_CreateMirrorTextureDX(_session, m_device, &m_mirrorDesc, &m_mirrorTexture);
if (!OVR_SUCCESS(result))
if (!OVR_SUCCESS(result) )
{
BX_CHECK(false, "Could not create D3D11 OVR mirror texture");
}
@@ -735,33 +731,33 @@ namespace bgfx { namespace d3d11
void destroy(const ovrSession& session)
{
if (!m_mirrorTexture)
return;
ovr_DestroyMirrorTexture(session, m_mirrorTexture);
m_mirrorTexture = NULL;
if (NULL != m_mirrorTexture)
{
ovr_DestroyMirrorTexture(session, m_mirrorTexture);
m_mirrorTexture = NULL;
}
}
void blit(const ovrSession& session)
{
if (!m_mirrorTexture)
return;
if (NULL != m_mirrorTexture)
{
ID3D11Texture2D* tex = NULL;
ovr_GetMirrorTextureBufferDX(session, m_mirrorTexture, IID_PPV_ARGS(&tex) );
ID3D11Texture2D* backBuffer;
DX_CHECK(m_swapChain->GetBuffer(0, IID_ID3D11Texture2D, (void**)&backBuffer) );
ID3D11Texture2D* tex = NULL;
ovr_GetMirrorTextureBufferDX(session, m_mirrorTexture, IID_PPV_ARGS(&tex));
ID3D11Texture2D* backBuffer;
DX_CHECK(m_d3dSwapChain->GetBuffer(0, IID_ID3D11Texture2D, (void**)&backBuffer));
m_deviceCtx->CopyResource(backBuffer, tex);
DX_CHECK(m_swapChain->Present(0, 0) );
m_d3dContext->CopyResource(backBuffer, tex);
DX_CHECK(m_d3dSwapChain->Present(0, 0));
tex->Release();
backBuffer->Release();
DX_RELEASE(tex, 1);
DX_RELEASE(backBuffer, 0);
}
}
ID3D11Device* m_d3dDevice;
ID3D11DeviceContext* m_d3dContext;
IDXGISwapChain* m_d3dSwapChain;
ID3D11Device* m_device;
ID3D11DeviceContext* m_deviceCtx;
IDXGISwapChain* m_swapChain;
};
#endif // BGFX_CONFIG_USE_OVR
@@ -3243,19 +3239,19 @@ BX_PRAGMA_DIAGNOSTIC_POP();
#if BGFX_CONFIG_USE_OVR
if (m_resolution.m_flags & (BGFX_RESET_HMD|BGFX_RESET_HMD_DEBUG) )
{
if (m_ovr.postReset())
if (m_ovr.postReset() )
{
for (int eyeIdx = 0; eyeIdx < ovrEye_Count; eyeIdx++)
{
// eye buffers need to be initialized only once during application lifetime
if (!m_ovr.m_eyeBuffers[eyeIdx])
{
m_ovr.m_eyeBuffers[eyeIdx] = BX_NEW(g_allocator, OVRBufferDX11(m_ovr.m_hmd, eyeIdx, m_device, m_deviceCtx));
m_ovr.m_eyeBuffers[eyeIdx] = BX_NEW(g_allocator, OVRBufferD3D11(m_ovr.m_hmd, eyeIdx, m_device, m_deviceCtx) );
}
}
// recreate mirror texture
m_ovr.m_mirror = BX_NEW(g_allocator, OVRMirrorDX11(m_device, m_deviceCtx, m_swapChain));
m_ovr.m_mirror = BX_NEW(g_allocator, OVRMirrorD3D11(m_device, m_deviceCtx, m_swapChain) );
m_ovr.m_mirror->init(m_ovr.m_hmd, m_resolution.m_width, m_resolution.m_height);
}
}