diff options
Diffstat (limited to '3rdparty/bgfx/src')
-rw-r--r-- | 3rdparty/bgfx/src/amalgamated.cpp | 4 | ||||
-rw-r--r-- | 3rdparty/bgfx/src/bgfx.cpp | 4 | ||||
-rw-r--r-- | 3rdparty/bgfx/src/bgfx_shader.sh | 98 | ||||
-rw-r--r-- | 3rdparty/bgfx/src/debug_renderdoc.cpp (renamed from 3rdparty/bgfx/src/renderdoc.cpp) | 0 | ||||
-rw-r--r-- | 3rdparty/bgfx/src/debug_renderdoc.h (renamed from 3rdparty/bgfx/src/renderdoc.h) | 0 | ||||
-rw-r--r-- | 3rdparty/bgfx/src/glimports.h | 2 | ||||
-rw-r--r-- | 3rdparty/bgfx/src/hmd_ovr.cpp | 249 | ||||
-rw-r--r-- | 3rdparty/bgfx/src/hmd_ovr.h (renamed from 3rdparty/bgfx/src/ovr.h) | 98 | ||||
-rw-r--r-- | 3rdparty/bgfx/src/ovr.cpp | 328 | ||||
-rw-r--r-- | 3rdparty/bgfx/src/renderer_d3d11.cpp | 273 | ||||
-rw-r--r-- | 3rdparty/bgfx/src/renderer_d3d11.h | 4 | ||||
-rw-r--r-- | 3rdparty/bgfx/src/renderer_gl.cpp | 254 | ||||
-rw-r--r-- | 3rdparty/bgfx/src/renderer_gl.h | 20 |
13 files changed, 749 insertions, 585 deletions
diff --git a/3rdparty/bgfx/src/amalgamated.cpp b/3rdparty/bgfx/src/amalgamated.cpp index d61f06845cd..dc6c8f6cd50 100644 --- a/3rdparty/bgfx/src/amalgamated.cpp +++ b/3rdparty/bgfx/src/amalgamated.cpp @@ -9,8 +9,8 @@ #include "glcontext_ppapi.cpp" #include "glcontext_wgl.cpp" #include "image.cpp" -#include "ovr.cpp" -#include "renderdoc.cpp" +#include "hmd_ovr.cpp" +#include "debug_renderdoc.cpp" #include "renderer_d3d9.cpp" #include "renderer_d3d11.cpp" #include "renderer_d3d12.cpp" diff --git a/3rdparty/bgfx/src/bgfx.cpp b/3rdparty/bgfx/src/bgfx.cpp index 3a839c82815..3c91a05e5a5 100644 --- a/3rdparty/bgfx/src/bgfx.cpp +++ b/3rdparty/bgfx/src/bgfx.cpp @@ -1151,6 +1151,10 @@ namespace bgfx } BX_TRACE("Max FB attachments: %d", g_caps.maxFBAttachments); + BX_TRACE("NDC depth [%d, 1], origin %s left." + , g_caps.homogeneousDepth ? -1 : 0 + , g_caps.originBottomLeft ? "bottom" : "top" + ); } TextureFormat::Enum getViableTextureFormat(const ImageContainer& _imageContainer) diff --git a/3rdparty/bgfx/src/bgfx_shader.sh b/3rdparty/bgfx/src/bgfx_shader.sh index 3a1c669f321..099681b0449 100644 --- a/3rdparty/bgfx/src/bgfx_shader.sh +++ b/3rdparty/bgfx/src/bgfx_shader.sh @@ -88,6 +88,50 @@ struct BgfxSampler2D Texture2D m_texture; }; +struct BgfxISampler2D +{ + Texture2D<ivec4> m_texture; +}; + +struct BgfxUSampler2D +{ + Texture2D<uvec4> m_texture; +}; + +struct BgfxSampler2DShadow +{ + SamplerComparisonState m_sampler; + Texture2D m_texture; +}; + +struct BgfxSampler3D +{ + SamplerState m_sampler; + Texture3D m_texture; +}; + +struct BgfxISampler3D +{ + Texture3D<ivec4> m_texture; +}; + +struct BgfxUSampler3D +{ + Texture3D<uvec4> m_texture; +}; + +struct BgfxSamplerCube +{ + SamplerState m_sampler; + TextureCube m_texture; +}; + +struct BgfxSampler2DMS +{ + SamplerState m_sampler; + Texture2DMS<vec4> m_texture; +}; + vec4 bgfxTexture2D(BgfxSampler2D _sampler, vec2 _coord) { return _sampler.m_texture.Sample(_sampler.m_sampler, _coord); @@ -110,18 +154,6 @@ vec4 bgfxTexture2DProj(BgfxSampler2D _sampler, vec4 _coord) return _sampler.m_texture.Sample(_sampler.m_sampler, coord); } -struct BgfxSampler2DMS -{ - SamplerState m_sampler; - Texture2DMS m_texture; -}; - -struct BgfxSampler2DShadow -{ - SamplerComparisonState m_sampler; - Texture2D m_texture; -}; - float bgfxShadow2D(BgfxSampler2DShadow _sampler, vec3 _coord) { return _sampler.m_texture.SampleCmpLevelZero(_sampler.m_sampler, _coord.xy, _coord.z); @@ -133,22 +165,6 @@ float bgfxShadow2DProj(BgfxSampler2DShadow _sampler, vec4 _coord) return _sampler.m_texture.SampleCmpLevelZero(_sampler.m_sampler, coord.xy, coord.z); } -struct BgfxSampler3D -{ - SamplerState m_sampler; - Texture3D m_texture; -}; - -struct BgfxISampler3D -{ - Texture3D<ivec4> m_texture; -}; - -struct BgfxUSampler3D -{ - Texture3D<uvec4> m_texture; -}; - vec4 bgfxTexture3D(BgfxSampler3D _sampler, vec3 _coord) { return _sampler.m_texture.Sample(_sampler.m_sampler, _coord); @@ -173,12 +189,6 @@ uvec4 bgfxTexture3D(BgfxUSampler3D _sampler, vec3 _coord) return _sampler.m_texture.Load(uvec4(_coord * size, 0) ); } -struct BgfxSamplerCube -{ - SamplerState m_sampler; - TextureCube m_texture; -}; - vec4 bgfxTextureCube(BgfxSamplerCube _sampler, vec3 _coord) { return _sampler.m_texture.Sample(_sampler.m_sampler, _coord); @@ -194,6 +204,16 @@ vec4 bgfxTexelFetch(BgfxSampler2D _sampler, ivec2 _coord, int _lod) return _sampler.m_texture.Load(ivec3(_coord, _lod) ); } +ivec4 bgfxTexelFetch(BgfxISampler2D _sampler, ivec2 _coord, int _lod) +{ + return _sampler.m_texture.Load(ivec3(_coord, _lod) ); +} + +uvec4 bgfxTexelFetch(BgfxUSampler2D _sampler, ivec2 _coord, int _lod) +{ + return _sampler.m_texture.Load(ivec3(_coord, _lod) ); +} + vec4 bgfxTexelFetch(BgfxSampler2DMS _sampler, ivec2 _coord, int _sampleIdx) { return _sampler.m_texture.Load(_coord, _sampleIdx); @@ -208,6 +228,12 @@ vec4 bgfxTexelFetch(BgfxSampler3D _sampler, ivec3 _coord, int _lod) uniform SamplerState _name ## Sampler : register(s[_reg]); \ uniform Texture2D _name ## Texture : register(t[_reg]); \ static BgfxSampler2D _name = { _name ## Sampler, _name ## Texture } +# define ISAMPLER2D(_name, _reg) \ + uniform Texture2D<ivec4> _name ## Texture : register(t[_reg]); \ + static BgfxISampler2D _name = { _name ## Texture } +# define USAMPLER2D(_name, _reg) \ + uniform Texture2D<uvec4> _name ## Texture : register(t[_reg]); \ + static BgfxUSampler2D _name = { _name ## Texture } # define sampler2D BgfxSampler2D # define texture2D(_sampler, _coord) bgfxTexture2D(_sampler, _coord) # define texture2DLod(_sampler, _coord, _level) bgfxTexture2DLod(_sampler, _coord, _level) @@ -364,8 +390,12 @@ vec4 mod(vec4 _a, vec4 _b) { return _a - _b * floor(_a / _b); } # define SAMPLER2DSHADOW(_name, _reg) uniform sampler2DShadow _name # if BGFX_SHADER_LANGUAGE_GLSL >= 130 +# define ISAMPLER2D(_name, _reg) uniform isampler2D _name +# define USAMPLER2D(_name, _reg) uniform usampler2D _name # define ISAMPLER3D(_name, _reg) uniform isampler3D _name # define USAMPLER3D(_name, _reg) uniform usampler3D _name +ivec4 texture2D(isampler2D _sampler, vec2 _coord) { return texture(_sampler, _coord); } +uvec4 texture2D(usampler2D _sampler, vec2 _coord) { return texture(_sampler, _coord); } ivec4 texture3D(isampler3D _sampler, vec3 _coord) { return texture(_sampler, _coord); } uvec4 texture3D(usampler3D _sampler, vec3 _coord) { return texture(_sampler, _coord); } # endif // BGFX_SHADER_LANGUAGE_GLSL >= 130 diff --git a/3rdparty/bgfx/src/renderdoc.cpp b/3rdparty/bgfx/src/debug_renderdoc.cpp index 0cb7be99d64..0cb7be99d64 100644 --- a/3rdparty/bgfx/src/renderdoc.cpp +++ b/3rdparty/bgfx/src/debug_renderdoc.cpp diff --git a/3rdparty/bgfx/src/renderdoc.h b/3rdparty/bgfx/src/debug_renderdoc.h index 803805ea581..803805ea581 100644 --- a/3rdparty/bgfx/src/renderdoc.h +++ b/3rdparty/bgfx/src/debug_renderdoc.h diff --git a/3rdparty/bgfx/src/glimports.h b/3rdparty/bgfx/src/glimports.h index f288a6a3597..7e110f06928 100644 --- a/3rdparty/bgfx/src/glimports.h +++ b/3rdparty/bgfx/src/glimports.h @@ -515,6 +515,7 @@ GL_IMPORT_OES__(true, PFNGLBINDVERTEXARRAYPROC, glBindVertexA GL_IMPORT_OES__(true, PFNGLDELETEVERTEXARRAYSPROC, glDeleteVertexArrays); GL_IMPORT_OES__(true, PFNGLGENVERTEXARRAYSPROC, glGenVertexArrays); +GL_IMPORT_____x(true, PFNGLCLIPCONTROLPROC, glClipControl); GL_IMPORT_____x(true, PFNGLENABLEIPROC, glEnablei); GL_IMPORT_____x(true, PFNGLDISABLEIPROC, glDisablei); GL_IMPORT_____x(true, PFNGLBLENDEQUATIONIPROC, glBlendEquationi); @@ -556,6 +557,7 @@ GL_IMPORT_NV___(true, PFNGLQUERYCOUNTERPROC, glQueryCounte GL_IMPORT (true, PFNGLINVALIDATEFRAMEBUFFERPROC, glInvalidateFramebuffer, glDiscardFramebufferEXT); # elif !BGFX_USE_GL_DYNAMIC_LIB +GL_IMPORT______(true, PFNGLCLIPCONTROLPROC, glClipControl); GL_IMPORT______(true, PFNGLGETSTRINGIPROC, glGetStringi); GL_IMPORT______(true, PFNGLTEXIMAGE3DPROC, glTexImage3D); diff --git a/3rdparty/bgfx/src/hmd_ovr.cpp b/3rdparty/bgfx/src/hmd_ovr.cpp new file mode 100644 index 00000000000..e8d465e4259 --- /dev/null +++ b/3rdparty/bgfx/src/hmd_ovr.cpp @@ -0,0 +1,249 @@ +/* + * Copyright 2011-2016 Branimir Karadzic. All rights reserved. + * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause + */ + +#include "hmd_ovr.h" + +#if BGFX_CONFIG_USE_OVR + +namespace bgfx +{ + OVR::OVR() + : m_hmd(NULL) + , m_isenabled(false) + , m_mirror(NULL) + , m_hmdFrameReady(-1) + , m_frameIndex(0) + , m_sensorSampleTime(0) + { + memset(m_eyeBuffers, 0, sizeof(m_eyeBuffers)); + } + + OVR::~OVR() + { + BX_CHECK(NULL == m_hmd, "OVR not shutdown properly."); + } + + void OVR::init() + { + ovrResult initialized = ovr_Initialize(NULL); + ovrGraphicsLuid luid; + + BX_WARN(initialized == ovrSuccess, "Unable to create OVR device."); + + if (initialized != ovrSuccess) + { + return; + } + + initialized = ovr_Create(&m_hmd, &luid); + if (initialized != ovrSuccess) + { + BX_WARN(initialized == ovrSuccess, "Unable to create OVR device."); + return; + } + + m_hmdDesc = ovr_GetHmdDesc(m_hmd); + + BX_TRACE("HMD: %s, %s, firmware: %d.%d" + , m_hmdDesc.ProductName + , m_hmdDesc.Manufacturer + , m_hmdDesc.FirmwareMajor + , m_hmdDesc.FirmwareMinor + ); + + ovrSizei sizeL = ovr_GetFovTextureSize(m_hmd, ovrEye_Left, m_hmdDesc.DefaultEyeFov[0], 1.0f); + ovrSizei sizeR = ovr_GetFovTextureSize(m_hmd, ovrEye_Right, m_hmdDesc.DefaultEyeFov[1], 1.0f); + m_hmdSize.w = sizeL.w + sizeR.w; + m_hmdSize.h = bx::uint32_max(sizeL.h, sizeR.h); + } + + void OVR::shutdown() + { + BX_CHECK(!m_isenabled, "HMD not disabled."); + + for (int i = 0; i < 2; i++) + { + if (m_eyeBuffers[i]) + { + m_eyeBuffers[i]->destroy(m_hmd); + BX_DELETE(g_allocator, m_eyeBuffers[i]); + } + } + + if (m_mirror) + { + m_mirror->destroy(m_hmd); + BX_DELETE(g_allocator, m_mirror); + } + + ovr_Destroy(m_hmd); + m_hmd = NULL; + ovr_Shutdown(); + } + + void OVR::getViewport(uint8_t _eye, Rect* _viewport) + { + _viewport->m_x = 0; + _viewport->m_y = 0; + _viewport->m_width = m_eyeBuffers[_eye]->m_eyeTextureSize.w; + _viewport->m_height = m_eyeBuffers[_eye]->m_eyeTextureSize.h; + } + + void OVR::renderEyeStart(uint8_t _eye) + { + m_eyeBuffers[_eye]->onRender(m_hmd); + } + + bool OVR::postReset() + { + if (NULL == m_hmd) + { + return false; + } + + for (int eyeIdx = 0; eyeIdx < ovrEye_Count; eyeIdx++) + { + m_erd[eyeIdx] = ovr_GetRenderDesc(m_hmd, (ovrEyeType)eyeIdx, m_hmdDesc.DefaultEyeFov[eyeIdx]); + } + + m_isenabled = true; + + return true; + } + + void OVR::preReset() + { + if (m_isenabled) + { + // on window resize this will recreate the mirror texture in ovrPostReset + m_mirror->destroy(m_hmd); + BX_DELETE(g_allocator, m_mirror); + m_mirror = NULL; + m_isenabled = false; + } + } + + void OVR::commitEye(uint8_t _eye) + { + if (m_isenabled) + { + m_hmdFrameReady = ovr_CommitTextureSwapChain(m_hmd, m_eyeBuffers[_eye]->m_swapTextureChain); + } + } + + bool OVR::swap(HMD& _hmd, bool originBottomLeft) + { + _hmd.flags = BGFX_HMD_NONE; + + if (NULL != m_hmd) + { + _hmd.flags |= BGFX_HMD_DEVICE_RESOLUTION; + _hmd.deviceWidth = m_hmdDesc.Resolution.w; + _hmd.deviceHeight = m_hmdDesc.Resolution.h; + } + + if (!m_isenabled || !OVR_SUCCESS(m_hmdFrameReady)) + { + return false; + } + + _hmd.flags |= BGFX_HMD_RENDERING; + + // finish frame for current eye + ovrViewScaleDesc viewScaleDesc; + viewScaleDesc.HmdSpaceToWorldScaleInMeters = 1.0f; + viewScaleDesc.HmdToEyeOffset[0] = m_hmdToEyeOffset[0]; + viewScaleDesc.HmdToEyeOffset[1] = m_hmdToEyeOffset[1]; + + // create the main eye layer + ovrLayerEyeFov eyeLayer; + eyeLayer.Header.Type = ovrLayerType_EyeFov; + eyeLayer.Header.Flags = originBottomLeft ? ovrLayerFlag_TextureOriginAtBottomLeft : 0; + + for (int eye = 0; eye < ovrEye_Count; eye++) + { + 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; + } + + // append all the layers to global list + ovrLayerHeader* layerList = &eyeLayer.Header; + + ovr_SubmitFrame(m_hmd, m_frameIndex, NULL, &layerList, 1); + + // perform mirror texture blit right after the entire frame is submitted to HMD + m_mirror->blit(m_hmd); + + m_hmdToEyeOffset[0] = m_erd[0].HmdToEyeOffset; + m_hmdToEyeOffset[1] = m_erd[1].HmdToEyeOffset; + + ovr_GetEyePoses(m_hmd, m_frameIndex, ovrTrue, m_hmdToEyeOffset, m_pose, &m_sensorSampleTime); + + getEyePose(_hmd); + + return true; + } + + void OVR::recenter() + { + if (NULL != m_hmd) + { + ovr_RecenterTrackingOrigin(m_hmd); + } + } + + void OVR::getEyePose(HMD& _hmd) + { + if (NULL != m_hmd) + { + for (int ii = 0; ii < 2; ++ii) + { + const ovrPosef& pose = m_pose[ii]; + HMD::Eye& eye = _hmd.eye[ii]; + eye.rotation[0] = pose.Orientation.x; + eye.rotation[1] = pose.Orientation.y; + eye.rotation[2] = pose.Orientation.z; + eye.rotation[3] = pose.Orientation.w; + eye.translation[0] = pose.Position.x; + eye.translation[1] = pose.Position.y; + eye.translation[2] = pose.Position.z; + + const ovrEyeRenderDesc& erd = m_erd[ii]; + eye.fov[0] = erd.Fov.UpTan; + eye.fov[1] = erd.Fov.DownTan; + eye.fov[2] = erd.Fov.LeftTan; + 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 (int kk = 0; kk < 4; ++kk) + { + eye.projection[4 * jj + kk] = eyeProj.M[kk][jj]; + } + } + + eye.viewOffset[0] = erd.HmdToEyeOffset.x; + eye.viewOffset[1] = erd.HmdToEyeOffset.y; + eye.viewOffset[2] = erd.HmdToEyeOffset.z; + + eye.pixelsPerTanAngle[0] = erd.PixelsPerTanAngleAtCenter.x; + eye.pixelsPerTanAngle[1] = erd.PixelsPerTanAngleAtCenter.y; + } + } + + _hmd.width = uint16_t(m_hmdSize.w); + _hmd.height = uint16_t(m_hmdSize.h); + } + +} // namespace bgfx + +#endif // BGFX_CONFIG_USE_OVR diff --git a/3rdparty/bgfx/src/ovr.h b/3rdparty/bgfx/src/hmd_ovr.h index b717d0d47a8..f8d41449514 100644 --- a/3rdparty/bgfx/src/ovr.h +++ b/3rdparty/bgfx/src/hmd_ovr.h @@ -14,36 +14,42 @@ # define OVR_VERSION_(_a, _b, _c) (_a * 10000 + _b * 100 + _c) # define OVR_VERSION OVR_VERSION_(OVR_PRODUCT_VERSION, OVR_MAJOR_VERSION, OVR_MINOR_VERSION) -# define OVR_VERSION_042 OVR_VERSION_(0, 4, 2) -# define OVR_VERSION_043 OVR_VERSION_(0, 4, 3) -# define OVR_VERSION_044 OVR_VERSION_(0, 4, 4) -# define OVR_VERSION_050 OVR_VERSION_(0, 5, 0) -# if OVR_VERSION < OVR_VERSION_050 -# include <OVR.h> -# else -# include <OVR_CAPI.h> -# endif // OVR_VERSION < OVR_VERSION_050 +# include <OVR_CAPI.h> # if BGFX_CONFIG_RENDERER_DIRECT3D11 -# if OVR_VERSION < OVR_VERSION_050 -# define OVR_D3D_VERSION 11 -# include <OVR_D3D.h> -# else -# include <OVR_CAPI_D3D.h> -# endif +# include <OVR_CAPI_D3D.h> # endif // BGFX_CONFIG_RENDERER_DIRECT3D11 # if BGFX_CONFIG_RENDERER_OPENGL -# if OVR_VERSION < OVR_VERSION_050 -# include <OVR_GL.h> -# else -# include <OVR_CAPI_GL.h> -# endif +# include <OVR_CAPI_GL.h> # endif // BGFX_CONFIG_RENDERER_OPENGL namespace bgfx { + // single eye buffer + struct OVRBufferI + { + virtual ~OVRBufferI() {}; + virtual void onRender(const ovrSession &session) = 0; + virtual void destroy(const ovrSession &session) = 0; + + ovrSizei m_eyeTextureSize; + ovrTextureSwapChain m_swapTextureChain; + }; + + // mirrored window output + struct OVRMirrorI + { + virtual ~OVRMirrorI() {}; + virtual void init(const ovrSession &session, int windowWidth, int windowHeight) = 0; + virtual void destroy(const ovrSession &session) = 0; + virtual void blit(const ovrSession &session) = 0; + + ovrMirrorTexture m_mirrorTexture; + ovrMirrorTextureDesc m_mirrorDesc; + }; + struct OVR { OVR(); @@ -59,37 +65,31 @@ namespace bgfx return m_isenabled; } - bool isDebug() const - { - return m_debug; - } - void init(); void shutdown(); void getViewport(uint8_t _eye, Rect* _viewport); - bool postReset(void* _nwh, ovrRenderAPIConfig* _config, bool _debug = false); - void postReset(const ovrTexture& _texture); + void renderEyeStart(uint8_t _eye); + bool postReset(); void preReset(); - bool swap(HMD& _hmd); + void commitEye(uint8_t _eye); + bool swap(HMD& _hmd, bool originBottomLeft); void recenter(); void getEyePose(HMD& _hmd); - void getSize(uint32_t& _width, uint32_t& _height) const - { - _width = m_rtSize.w; - _height = m_rtSize.h; - } - ovrHmd m_hmd; - ovrFrameTiming m_timing; + ovrSession m_hmd; + ovrHmdDesc m_hmdDesc; ovrEyeRenderDesc m_erd[2]; - ovrRecti m_rect[2]; - ovrPosef m_pose[2]; - ovrTexture m_texture[2]; - ovrSizei m_rtSize; - bool m_warning; + ovrRecti m_rect[2]; + ovrPosef m_pose[2]; + ovrVector3f m_hmdToEyeOffset[2]; + ovrSizei m_hmdSize; + ovrResult m_hmdFrameReady; + OVRBufferI *m_eyeBuffers[2]; + OVRMirrorI *m_mirror; + long long m_frameIndex; + double m_sensorSampleTime; bool m_isenabled; - bool m_debug; }; } // namespace bgfx @@ -139,7 +139,15 @@ namespace bgfx _viewport->m_height = 0; } - bool swap(HMD& _hmd) + void commitEye(uint8_t /*_eye*/) + { + } + + void renderEyeStart(uint8_t /*_eye*/) + { + } + + bool swap(HMD& _hmd, bool /*originBottomLeft*/) { _hmd.flags = BGFX_HMD_NONE; getEyePose(_hmd); @@ -155,12 +163,6 @@ namespace bgfx _hmd.width = 0; _hmd.height = 0; } - - void getSize(uint32_t& _width, uint32_t& _height) const - { - _width = 0; - _height = 0; - } }; } // namespace bgfx diff --git a/3rdparty/bgfx/src/ovr.cpp b/3rdparty/bgfx/src/ovr.cpp deleted file mode 100644 index 5cd62ef3956..00000000000 --- a/3rdparty/bgfx/src/ovr.cpp +++ /dev/null @@ -1,328 +0,0 @@ -/* - * Copyright 2011-2016 Branimir Karadzic. All rights reserved. - * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause - */ - -#include "ovr.h" - -#if BGFX_CONFIG_USE_OVR - -namespace bgfx -{ -#if OVR_VERSION <= OVR_VERSION_050 -# define OVR_EYE_BUFFER 100 -#else -# define OVR_EYE_BUFFER 8 -#endif // OVR_VERSION... - - OVR::OVR() - : m_hmd(NULL) - , m_isenabled(false) - , m_debug(false) - { - } - - OVR::~OVR() - { - BX_CHECK(NULL == m_hmd, "OVR not shutdown properly."); - } - - void OVR::init() - { - bool initialized = !!ovr_Initialize(); - BX_WARN(initialized, "Unable to create OVR device."); - if (!initialized) - { - return; - } - - m_hmd = ovrHmd_Create(0); - if (NULL == m_hmd) - { - m_hmd = ovrHmd_CreateDebug(ovrHmd_DK2); - BX_WARN(NULL != m_hmd, "Unable to create OVR device."); - if (NULL == m_hmd) - { - return; - } - } - - BX_TRACE("HMD: %s, %s, firmware: %d.%d" - , m_hmd->ProductName - , m_hmd->Manufacturer - , m_hmd->FirmwareMajor - , m_hmd->FirmwareMinor - ); - - ovrSizei sizeL = ovrHmd_GetFovTextureSize(m_hmd, ovrEye_Left, m_hmd->DefaultEyeFov[0], 1.0f); - ovrSizei sizeR = ovrHmd_GetFovTextureSize(m_hmd, ovrEye_Right, m_hmd->DefaultEyeFov[1], 1.0f); - m_rtSize.w = sizeL.w + sizeR.w + OVR_EYE_BUFFER; - m_rtSize.h = bx::uint32_max(sizeL.h, sizeR.h); - m_warning = true; - } - - void OVR::shutdown() - { - BX_CHECK(!m_isenabled, "HMD not disabled."); - ovrHmd_Destroy(m_hmd); - m_hmd = NULL; - ovr_Shutdown(); - } - - void OVR::getViewport(uint8_t _eye, Rect* _viewport) - { - _viewport->m_x = _eye * (m_rtSize.w + OVR_EYE_BUFFER + 1)/2; - _viewport->m_y = 0; - _viewport->m_width = (m_rtSize.w - OVR_EYE_BUFFER)/2; - _viewport->m_height = m_rtSize.h; - } - - bool OVR::postReset(void* _nwh, ovrRenderAPIConfig* _config, bool _debug) - { - if (_debug) - { - switch (_config->Header.API) - { -#if BGFX_CONFIG_RENDERER_DIRECT3D11 - case ovrRenderAPI_D3D11: - { - ovrD3D11ConfigData* data = (ovrD3D11ConfigData*)_config; -# if OVR_VERSION > OVR_VERSION_043 - m_rtSize = data->Header.BackBufferSize; -# else - m_rtSize = data->Header.RTSize; -# endif // OVR_VERSION > OVR_VERSION_043 - } - break; -#endif // BGFX_CONFIG_RENDERER_DIRECT3D11 - -#if BGFX_CONFIG_RENDERER_OPENGL - case ovrRenderAPI_OpenGL: - { - ovrGLConfigData* data = (ovrGLConfigData*)_config; -# if OVR_VERSION > OVR_VERSION_043 - m_rtSize = data->Header.BackBufferSize; -# else - m_rtSize = data->Header.RTSize; -# endif // OVR_VERSION > OVR_VERSION_043 - } - break; -#endif // BGFX_CONFIG_RENDERER_OPENGL - - case ovrRenderAPI_None: - default: - BX_CHECK(false, "You should not be here!"); - break; - } - - m_debug = true; - return false; - } - - if (NULL == m_hmd) - { - return false; - } - - m_isenabled = true; - - ovrBool result; - result = ovrHmd_AttachToWindow(m_hmd, _nwh, NULL, NULL); - if (!result) { goto ovrError; } - - ovrFovPort eyeFov[2] = { m_hmd->DefaultEyeFov[0], m_hmd->DefaultEyeFov[1] }; - result = ovrHmd_ConfigureRendering(m_hmd - , _config - , 0 -#if OVR_VERSION < OVR_VERSION_050 - | ovrDistortionCap_Chromatic // permanently enabled >= v5.0 -#endif - | ovrDistortionCap_Vignette - | ovrDistortionCap_TimeWarp - | ovrDistortionCap_Overdrive - | ovrDistortionCap_NoRestore - | ovrDistortionCap_HqDistortion - , eyeFov - , m_erd - ); - if (!result) { goto ovrError; } - - ovrHmd_SetEnabledCaps(m_hmd - , 0 - | ovrHmdCap_LowPersistence - | ovrHmdCap_DynamicPrediction - ); - - result = ovrHmd_ConfigureTracking(m_hmd - , 0 - | ovrTrackingCap_Orientation - | ovrTrackingCap_MagYawCorrection - | ovrTrackingCap_Position - , 0 - ); - - if (!result) - { -ovrError: - BX_TRACE("Failed to initialize OVR."); - m_isenabled = false; - return false; - } - - m_warning = true; - return true; - } - - void OVR::postReset(const ovrTexture& _texture) - { - if (NULL != m_hmd) - { - m_texture[0] = _texture; - m_texture[1] = _texture; - - ovrRecti rect; - rect.Pos.x = 0; - rect.Pos.y = 0; - rect.Size.w = (m_rtSize.w - OVR_EYE_BUFFER)/2; - rect.Size.h = m_rtSize.h; - - m_texture[0].Header.RenderViewport = rect; - - rect.Pos.x += rect.Size.w + OVR_EYE_BUFFER; - m_texture[1].Header.RenderViewport = rect; - - m_timing = ovrHmd_BeginFrame(m_hmd, 0); -#if OVR_VERSION > OVR_VERSION_042 - m_pose[0] = ovrHmd_GetHmdPosePerEye(m_hmd, ovrEye_Left); - m_pose[1] = ovrHmd_GetHmdPosePerEye(m_hmd, ovrEye_Right); -#else - m_pose[0] = ovrHmd_GetEyePose(m_hmd, ovrEye_Left); - m_pose[1] = ovrHmd_GetEyePose(m_hmd, ovrEye_Right); -#endif // OVR_VERSION > OVR_VERSION_042 - } - } - - void OVR::preReset() - { - if (m_isenabled) - { - ovrHmd_EndFrame(m_hmd, m_pose, m_texture); - ovrHmd_AttachToWindow(m_hmd, NULL, NULL, NULL); - ovrHmd_ConfigureRendering(m_hmd, NULL, 0, NULL, NULL); - m_isenabled = false; - } - - m_debug = false; - } - - bool OVR::swap(HMD& _hmd) - { - _hmd.flags = BGFX_HMD_NONE; - - if (NULL != m_hmd) - { - _hmd.flags |= BGFX_HMD_DEVICE_RESOLUTION; - _hmd.deviceWidth = m_hmd->Resolution.w; - _hmd.deviceHeight = m_hmd->Resolution.h; - } - - if (!m_isenabled) - { - return false; - } - - _hmd.flags |= BGFX_HMD_RENDERING; - ovrHmd_EndFrame(m_hmd, m_pose, m_texture); - - if (m_warning) - { - m_warning = !ovrHmd_DismissHSWDisplay(m_hmd); - } - - m_timing = ovrHmd_BeginFrame(m_hmd, 0); - -#if OVR_VERSION > OVR_VERSION_042 - m_pose[0] = ovrHmd_GetHmdPosePerEye(m_hmd, ovrEye_Left); - m_pose[1] = ovrHmd_GetHmdPosePerEye(m_hmd, ovrEye_Right); -#else - m_pose[0] = ovrHmd_GetEyePose(m_hmd, ovrEye_Left); - m_pose[1] = ovrHmd_GetEyePose(m_hmd, ovrEye_Right); -#endif // OVR_VERSION > OVR_VERSION_042 - - getEyePose(_hmd); - - return true; - } - - void OVR::recenter() - { - if (NULL != m_hmd) - { - ovrHmd_RecenterPose(m_hmd); - } - } - - void OVR::getEyePose(HMD& _hmd) - { - if (NULL != m_hmd) - { - for (int ii = 0; ii < 2; ++ii) - { - const ovrPosef& pose = m_pose[ii]; - HMD::Eye& eye = _hmd.eye[ii]; - eye.rotation[0] = pose.Orientation.x; - eye.rotation[1] = pose.Orientation.y; - eye.rotation[2] = pose.Orientation.z; - eye.rotation[3] = pose.Orientation.w; - eye.translation[0] = pose.Position.x; - eye.translation[1] = pose.Position.y; - eye.translation[2] = pose.Position.z; - - const ovrEyeRenderDesc& erd = m_erd[ii]; - eye.fov[0] = erd.Fov.UpTan; - eye.fov[1] = erd.Fov.DownTan; - eye.fov[2] = erd.Fov.LeftTan; - eye.fov[3] = erd.Fov.RightTan; -#if OVR_VERSION > OVR_VERSION_042 - eye.viewOffset[0] = erd.HmdToEyeViewOffset.x; - eye.viewOffset[1] = erd.HmdToEyeViewOffset.y; - eye.viewOffset[2] = erd.HmdToEyeViewOffset.z; -#else - eye.viewOffset[0] = erd.ViewAdjust.x; - eye.viewOffset[1] = erd.ViewAdjust.y; - eye.viewOffset[2] = erd.ViewAdjust.z; -#endif // OVR_VERSION > OVR_VERSION_042 - eye.pixelsPerTanAngle[0] = erd.PixelsPerTanAngleAtCenter.x; - eye.pixelsPerTanAngle[1] = erd.PixelsPerTanAngleAtCenter.y; - } - } - else - { - for (int ii = 0; ii < 2; ++ii) - { - _hmd.eye[ii].rotation[0] = 0.0f; - _hmd.eye[ii].rotation[1] = 0.0f; - _hmd.eye[ii].rotation[2] = 0.0f; - _hmd.eye[ii].rotation[3] = 1.0f; - _hmd.eye[ii].translation[0] = 0.0f; - _hmd.eye[ii].translation[1] = 0.0f; - _hmd.eye[ii].translation[2] = 0.0f; - _hmd.eye[ii].fov[0] = 1.32928634f; - _hmd.eye[ii].fov[1] = 1.32928634f; - _hmd.eye[ii].fov[2] = 0 == ii ? 1.05865765f : 1.09236801f; - _hmd.eye[ii].fov[3] = 0 == ii ? 1.09236801f : 1.05865765f; - _hmd.eye[ii].viewOffset[0] = 0 == ii ? 0.0355070010f : -0.0375000015f; - _hmd.eye[ii].viewOffset[1] = 0.0f; - _hmd.eye[ii].viewOffset[2] = 0 == ii ? 0.00150949787f : -0.00150949787f; - _hmd.eye[ii].pixelsPerTanAngle[0] = 1; - _hmd.eye[ii].pixelsPerTanAngle[1] = 1; - } - } - - _hmd.width = uint16_t(m_rtSize.w); - _hmd.height = uint16_t(m_rtSize.h); - } - -} // namespace bgfx - -#endif // BGFX_CONFIG_USE_OVR diff --git a/3rdparty/bgfx/src/renderer_d3d11.cpp b/3rdparty/bgfx/src/renderer_d3d11.cpp index cd794028c30..6d21d13612d 100644 --- a/3rdparty/bgfx/src/renderer_d3d11.cpp +++ b/3rdparty/bgfx/src/renderer_d3d11.cpp @@ -601,6 +601,170 @@ 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 + { + OVRBufferDX11(const ovrSession& session, int eyeIdx, ID3D11Device* d3dDevice, ID3D11DeviceContext* d3dCtx) + { + m_d3dDevice = d3dDevice; + m_d3dContext = d3dCtx; + ovrHmdDesc hmdDesc = ovr_GetHmdDesc(session); + m_eyeTextureSize = ovr_GetFovTextureSize(session, (ovrEyeType)eyeIdx, hmdDesc.DefaultEyeFov[eyeIdx], 1.0f); + + ovrTextureSwapChainDesc desc = {}; + desc.Type = ovrTexture_2D; + desc.ArraySize = 1; + desc.Format = OVR_FORMAT_R8G8B8A8_UNORM_SRGB; + desc.Width = m_eyeTextureSize.w; + desc.Height = m_eyeTextureSize.h; + desc.MipLevels = 1; + desc.SampleCount = 1; + desc.MiscFlags = ovrTextureMisc_DX_Typeless; + desc.BindFlags = ovrTextureBind_DX_RenderTarget; + desc.StaticImage = ovrFalse; + + ovrResult result = ovr_CreateTextureSwapChainDX(session, d3dDevice, &desc, &m_swapTextureChain); + + if (!OVR_SUCCESS(result)) + { + BX_CHECK(false, "Could not create D3D11 OVR swap texture"); + } + + int textureCount = 0; + ovr_GetTextureSwapChainLength(session, m_swapTextureChain, &textureCount); + + for (int i = 0; i < textureCount; ++i) + { + ID3D11Texture2D* tex = NULL; + ovr_GetTextureSwapChainBufferDX(session, m_swapTextureChain, 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; + DX_CHECK(d3dDevice->CreateRenderTargetView(tex, &rtvd, &rtv)); + m_eyeRtv.push_back(rtv); + tex->Release(); + } + + // setup depth buffer + D3D11_TEXTURE2D_DESC dbDesc; + dbDesc.Width = m_eyeTextureSize.w; + dbDesc.Height = m_eyeTextureSize.h; + dbDesc.MipLevels = 1; + dbDesc.ArraySize = 1; + dbDesc.Format = DXGI_FORMAT_D32_FLOAT; + dbDesc.SampleDesc.Count = 1; + dbDesc.SampleDesc.Quality = 0; + dbDesc.Usage = D3D11_USAGE_DEFAULT; + dbDesc.CPUAccessFlags = 0; + 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(); + } + + void onRender(const ovrSession& session) + { + // Clear and set up rendertarget + int texIndex = 0; + 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); + + D3D11_VIEWPORT D3Dvp; + D3Dvp.TopLeftX = 0; + D3Dvp.TopLeftY = 0; + D3Dvp.Width = (FLOAT)m_eyeTextureSize.w; + D3Dvp.Height = (FLOAT)m_eyeTextureSize.h; + D3Dvp.MinDepth = 0; + D3Dvp.MaxDepth = 1; + m_d3dContext->RSSetViewports(1, &D3Dvp); + } + + void destroy(const ovrSession& session) + { + for (size_t i = 0; i < m_eyeRtv.size(); ++i) + { + m_eyeRtv[i]->Release(); + } + + ovr_DestroyTextureSwapChain(session, m_swapTextureChain); + m_depthBuffer->Release(); + } + + ID3D11Device* m_d3dDevice; + ID3D11DeviceContext* m_d3dContext; + stl::vector<ID3D11RenderTargetView *> m_eyeRtv; + ID3D11DepthStencilView* m_depthBuffer; + }; + + // Oculus Rift mirror + struct OVRMirrorDX11 : public OVRMirrorI + { + OVRMirrorDX11(ID3D11Device* d3dDevice, + ID3D11DeviceContext* d3dCtx, + IDXGISwapChain* d3dSc) : m_d3dDevice(d3dDevice) + , m_d3dContext(d3dCtx) + , m_d3dSwapChain(d3dSc) + { + } + + void init(const ovrSession& session, int windowWidth, int windowHeight) + { + 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); + + if (!OVR_SUCCESS(result)) + { + BX_CHECK(false, "Could not create D3D11 OVR mirror texture"); + } + } + + void destroy(const ovrSession& session) + { + if (!m_mirrorTexture) + return; + + ovr_DestroyMirrorTexture(session, m_mirrorTexture); + m_mirrorTexture = NULL; + } + + void blit(const ovrSession& session) + { + if (!m_mirrorTexture) + return; + + 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_d3dContext->CopyResource(backBuffer, tex); + DX_CHECK(m_d3dSwapChain->Present(0, 0)); + + tex->Release(); + backBuffer->Release(); + } + + ID3D11Device* m_d3dDevice; + ID3D11DeviceContext* m_d3dContext; + IDXGISwapChain* m_d3dSwapChain; + }; +#endif // BGFX_CONFIG_USE_OVR + struct RendererContextD3D11 : public RendererContextI { RendererContextD3D11() @@ -635,8 +799,6 @@ namespace bgfx { namespace d3d11 , m_fsChanges(0) , m_rtMsaa(false) , m_timerQuerySupport(false) - , m_ovrRtv(NULL) - , m_ovrDsv(NULL) { m_fbh.idx = invalidHandle; memset(&m_adapterDesc, 0, sizeof(m_adapterDesc) ); @@ -2001,10 +2163,6 @@ BX_PRAGMA_DIAGNOSTIC_POP(); uint32_t width = getBufferWidth(); uint32_t height = getBufferHeight(); - if (m_ovr.isEnabled() ) - { - m_ovr.getSize(width, height); - } FrameBufferHandle fbh = BGFX_INVALID_HANDLE; setFrameBuffer(fbh, false); @@ -2192,7 +2350,7 @@ BX_PRAGMA_DIAGNOSTIC_POP(); if (SUCCEEDED(hr) ) { - if (!m_ovr.swap(_hmd) ) + if (!m_ovr.swap(_hmd, false) ) { hr = m_swapChain->Present(syncInterval, 0); } @@ -3083,77 +3241,22 @@ BX_PRAGMA_DIAGNOSTIC_POP(); void ovrPostReset() { #if BGFX_CONFIG_USE_OVR - if (m_flags & (BGFX_RESET_HMD|BGFX_RESET_HMD_DEBUG) ) + if (m_resolution.m_flags & (BGFX_RESET_HMD|BGFX_RESET_HMD_DEBUG) ) { - ovrD3D11Config config; - config.D3D11.Header.API = ovrRenderAPI_D3D11; -# if OVR_VERSION > OVR_VERSION_043 - config.D3D11.Header.BackBufferSize.w = m_scd.BufferDesc.Width; - config.D3D11.Header.BackBufferSize.h = m_scd.BufferDesc.Height; - config.D3D11.pBackBufferUAV = NULL; -# else - config.D3D11.Header.RTSize.w = m_scd.BufferDesc.Width; - config.D3D11.Header.RTSize.h = m_scd.BufferDesc.Height; -# endif // OVR_VERSION > OVR_VERSION_042 - config.D3D11.Header.Multisample = 0; - config.D3D11.pDevice = m_device; - config.D3D11.pDeviceContext = m_deviceCtx; - config.D3D11.pBackBufferRT = m_backBufferColor; - config.D3D11.pSwapChain = m_swapChain; - if (m_ovr.postReset(g_platformData.nwh, &config.Config, !!(m_flags & BGFX_RESET_HMD_DEBUG) ) ) + if (m_ovr.postReset()) { - uint32_t size = sizeof(uint32_t) + sizeof(TextureCreate); - const Memory* mem = alloc(size); - - bx::StaticMemoryBlockWriter writer(mem->data, mem->size); - uint32_t magic = BGFX_CHUNK_MAGIC_TEX; - bx::write(&writer, magic); - - TextureCreate tc; - tc.m_flags = BGFX_TEXTURE_RT|( ((m_flags & BGFX_RESET_MSAA_MASK) >> BGFX_RESET_MSAA_SHIFT) << BGFX_TEXTURE_RT_MSAA_SHIFT); - tc.m_width = m_ovr.m_rtSize.w; - tc.m_height = m_ovr.m_rtSize.h; - tc.m_sides = 0; - tc.m_depth = 0; - tc.m_numMips = 1; - tc.m_format = uint8_t(bgfx::TextureFormat::BGRA8); - tc.m_cubeMap = false; - tc.m_mem = NULL; - bx::write(&writer, tc); - m_ovrRT.create(mem, tc.m_flags, 0); - - release(mem); - - DX_CHECK(m_device->CreateRenderTargetView(m_ovrRT.m_ptr, NULL, &m_ovrRtv) ); - - D3D11_TEXTURE2D_DESC dsd; - dsd.Width = m_ovr.m_rtSize.w; - dsd.Height = m_ovr.m_rtSize.h; - dsd.MipLevels = 1; - dsd.ArraySize = 1; - dsd.Format = DXGI_FORMAT_D24_UNORM_S8_UINT; - dsd.SampleDesc = m_scd.SampleDesc; - dsd.Usage = D3D11_USAGE_DEFAULT; - dsd.BindFlags = D3D11_BIND_DEPTH_STENCIL; - dsd.CPUAccessFlags = 0; - dsd.MiscFlags = 0; - - ID3D11Texture2D* depthStencil; - DX_CHECK(m_device->CreateTexture2D(&dsd, NULL, &depthStencil) ); - DX_CHECK(m_device->CreateDepthStencilView(depthStencil, NULL, &m_ovrDsv) ); - DX_RELEASE(depthStencil, 0); - - ovrD3D11Texture texture; - texture.D3D11.Header.API = ovrRenderAPI_D3D11; - texture.D3D11.Header.TextureSize = m_ovr.m_rtSize; - texture.D3D11.pTexture = m_ovrRT.m_texture2d; - texture.D3D11.pSRView = m_ovrRT.m_srv; - m_ovr.postReset(texture.Texture); - - bx::xchg(m_ovrRtv, m_backBufferColor); - - BX_CHECK(NULL == m_backBufferDepthStencil, ""); - bx::xchg(m_ovrDsv, m_backBufferDepthStencil); + 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)); + } + } + + // recreate mirror texture + m_ovr.m_mirror = BX_NEW(g_allocator, OVRMirrorDX11(m_device, m_deviceCtx, m_swapChain)); + m_ovr.m_mirror->init(m_ovr.m_hmd, m_resolution.m_width, m_resolution.m_height); } } #endif // BGFX_CONFIG_USE_OVR @@ -3163,16 +3266,6 @@ BX_PRAGMA_DIAGNOSTIC_POP(); { #if BGFX_CONFIG_USE_OVR m_ovr.preReset(); - if (NULL != m_ovrRtv) - { - bx::xchg(m_ovrRtv, m_backBufferColor); - bx::xchg(m_ovrDsv, m_backBufferDepthStencil); - BX_CHECK(NULL == m_backBufferDepthStencil, ""); - - DX_RELEASE(m_ovrRtv, 0); - DX_RELEASE(m_ovrDsv, 0); - m_ovrRT.destroy(); - } #endif // BGFX_CONFIG_USE_OVR } @@ -3564,9 +3657,6 @@ BX_PRAGMA_DIAGNOSTIC_POP(); bool m_timerQuerySupport; OVR m_ovr; - TextureD3D11 m_ovrRT; - ID3D11RenderTargetView* m_ovrRtv; - ID3D11DepthStencilView* m_ovrDsv; }; static RendererContextD3D11* s_renderD3D11; @@ -4864,7 +4954,7 @@ BX_PRAGMA_DIAGNOSTIC_POP(); _render->m_hmdInitialized = m_ovr.isInitialized(); - const bool hmdEnabled = m_ovr.isEnabled() || m_ovr.isDebug(); + const bool hmdEnabled = m_ovr.isEnabled(); ViewState viewState(_render, hmdEnabled); bool wireframe = !!(_render->m_debug&BGFX_DEBUG_WIREFRAME); @@ -4987,6 +5077,9 @@ BX_PRAGMA_DIAGNOSTIC_POP(); if (m_ovr.isEnabled() ) { m_ovr.getViewport(eye, &viewState.m_rect); + // commit previous eye to HMD and start rendering new frame + m_ovr.commitEye(eye); + m_ovr.renderEyeStart(eye); } else { diff --git a/3rdparty/bgfx/src/renderer_d3d11.h b/3rdparty/bgfx/src/renderer_d3d11.h index dbe16a984d0..c1b4917ff80 100644 --- a/3rdparty/bgfx/src/renderer_d3d11.h +++ b/3rdparty/bgfx/src/renderer_d3d11.h @@ -32,8 +32,8 @@ BX_PRAGMA_DIAGNOSTIC_POP() #include "renderer.h" #include "renderer_d3d.h" #include "shader_dxbc.h" -#include "ovr.h" -#include "renderdoc.h" +#include "hmd_ovr.h" +#include "debug_renderdoc.h" #ifndef D3DCOLOR_ARGB # define D3DCOLOR_ARGB(_a, _r, _g, _b) ( (DWORD)( ( ( (_a)&0xff)<<24)|( ( (_r)&0xff)<<16)|( ( (_g)&0xff)<<8)|( (_b)&0xff) ) ) diff --git a/3rdparty/bgfx/src/renderer_gl.cpp b/3rdparty/bgfx/src/renderer_gl.cpp index ca09b64eba1..84afafbd5df 100644 --- a/3rdparty/bgfx/src/renderer_gl.cpp +++ b/3rdparty/bgfx/src/renderer_gl.cpp @@ -1274,6 +1274,144 @@ namespace bgfx { namespace gl BX_UNUSED(supported); } +#if BGFX_CONFIG_USE_OVR + + // Oculus Rift eye buffer + struct OVRBufferGL : public OVRBufferI + { + OVRBufferGL(const ovrSession& session, int eyeIdx) + { + ovrHmdDesc hmdDesc = ovr_GetHmdDesc(session); + m_eyeTextureSize = ovr_GetFovTextureSize(session, (ovrEyeType)eyeIdx, hmdDesc.DefaultEyeFov[eyeIdx], 1.0f); + + ovrTextureSwapChainDesc desc = {}; + desc.Type = ovrTexture_2D; + desc.ArraySize = 1; + desc.Width = m_eyeTextureSize.w; + desc.Height = m_eyeTextureSize.h; + desc.MipLevels = 1; + desc.Format = OVR_FORMAT_R8G8B8A8_UNORM_SRGB; + desc.SampleCount = 1; + desc.StaticImage = ovrFalse; + + ovr_CreateTextureSwapChainGL(session, &desc, &m_swapTextureChain); + + int textureCount = 0; + ovr_GetTextureSwapChainLength(session, m_swapTextureChain, &textureCount); + + for (int j = 0; j < textureCount; ++j) + { + GLuint chainTexId; + ovr_GetTextureSwapChainBufferGL(session, m_swapTextureChain, j, &chainTexId); + GL_CHECK(glBindTexture(GL_TEXTURE_2D, chainTexId)); + + GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)); + GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)); + GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)); + GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)); + } + + GL_CHECK(glGenFramebuffers(1, &m_eyeFbo)); + + // create depth buffer + GL_CHECK(glGenTextures(1, &m_depthBuffer)); + GL_CHECK(glBindTexture(GL_TEXTURE_2D, m_depthBuffer)); + GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)); + GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)); + GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)); + GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)); + + GL_CHECK(glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, m_eyeTextureSize.w, m_eyeTextureSize.h, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, NULL)); + } + + void onRender(const ovrSession& session) + { + // set the current eye texture in swap chain + int curIndex; + ovr_GetTextureSwapChainCurrentIndex(session, m_swapTextureChain, &curIndex); + ovr_GetTextureSwapChainBufferGL(session, m_swapTextureChain, curIndex, &m_eyeTexId); + + GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, m_eyeFbo)); + GL_CHECK(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_eyeTexId, 0)); + GL_CHECK(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, m_depthBuffer, 0)); + + GL_CHECK(glViewport(0, 0, m_eyeTextureSize.w, m_eyeTextureSize.h)); + GL_CHECK(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)); + } + + void destroy(const ovrSession& session) + { + GL_CHECK(glDeleteFramebuffers(1, &m_eyeFbo)); + GL_CHECK(glDeleteTextures(1, &m_depthBuffer)); + + ovr_DestroyTextureSwapChain(session, m_swapTextureChain); + } + + GLuint m_eyeFbo; + GLuint m_eyeTexId; + GLuint m_depthBuffer; + }; + + // Oculus Rift mirror + struct OVRMirrorGL : public OVRMirrorI + { + void init(const ovrSession& session, int windowWidth, int windowHeight) + { + memset(&m_mirrorDesc, 0, sizeof(m_mirrorDesc)); + m_mirrorDesc.Width = windowWidth; + m_mirrorDesc.Height = windowHeight; + m_mirrorDesc.Format = OVR_FORMAT_R8G8B8A8_UNORM_SRGB; + + ovr_CreateMirrorTextureGL(session, &m_mirrorDesc, &m_mirrorTexture); + + // Fallback to doing nothing if mirror was not created. This is to prevent errors with fast window resizes + if (!m_mirrorTexture) + return; + + // Configure the mirror read buffer + GLuint texId; + ovr_GetMirrorTextureBufferGL(session, m_mirrorTexture, &texId); + GL_CHECK(glGenFramebuffers(1, &m_mirrorFBO)); + GL_CHECK(glBindFramebuffer(GL_READ_FRAMEBUFFER, m_mirrorFBO)); + GL_CHECK(glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texId, 0)); + GL_CHECK(glFramebufferRenderbuffer(GL_READ_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, 0)); + GL_CHECK(glBindFramebuffer(GL_READ_FRAMEBUFFER, 0)); + + if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) + { + GL_CHECK(glDeleteFramebuffers(1, &m_mirrorFBO)); + BX_CHECK(false, "Could not initialize VR buffers!"); + } + } + + void destroy(const ovrSession& session) + { + if (!m_mirrorTexture) + return; + + GL_CHECK(glDeleteFramebuffers(1, &m_mirrorFBO)); + ovr_DestroyMirrorTexture(session, m_mirrorTexture); + m_mirrorTexture = NULL; + } + + void blit(const ovrSession& /*session*/) + { + if (!m_mirrorTexture) + return; + + // Blit mirror texture to back buffer + GL_CHECK(glBindFramebuffer(GL_READ_FRAMEBUFFER, m_mirrorFBO)); + GL_CHECK(glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0)); + GLint w = m_mirrorDesc.Width; + GLint h = m_mirrorDesc.Height; + GL_CHECK(glBlitFramebuffer(0, h, w, 0, 0, 0, w, h, GL_COLOR_BUFFER_BIT, GL_NEAREST)); + GL_CHECK(glBindFramebuffer(GL_READ_FRAMEBUFFER, 0)); + } + + GLuint m_mirrorFBO; + }; +#endif // BGFX_CONFIG_USE_OVR + struct RendererContextGL : public RendererContextI { RendererContextGL() @@ -1304,7 +1442,6 @@ namespace bgfx { namespace gl , m_hash( (BX_PLATFORM_WINDOWS<<1) | BX_ARCH_64BIT) , m_backBufferFbo(0) , m_msaaBackBufferFbo(0) - , m_ovrFbo(0) { memset(m_msaaBackBufferRbos, 0, sizeof(m_msaaBackBufferRbos) ); } @@ -1818,6 +1955,17 @@ namespace bgfx { namespace gl ); } +// if (s_extension[Extension::ARB_clip_control].m_supported) +// { +// GL_CHECK(glClipControl(GL_LOWER_LEFT, GL_ZERO_TO_ONE) ); +// g_caps.originBottomLeft = true; +// } +// else + { + g_caps.homogeneousDepth = true; + g_caps.originBottomLeft = true; + } + m_vaoSupport = !!(BGFX_CONFIG_RENDERER_OPENGLES >= 30) || s_extension[Extension::ARB_vertex_array_object].m_supported || s_extension[Extension::OES_vertex_array_object].m_supported @@ -2096,10 +2244,9 @@ namespace bgfx { namespace gl m_glctx.swap(m_frameBuffers[m_windows[ii].idx].m_swapChain); } - if (!m_ovr.swap(_hmd) ) - { - m_glctx.swap(); - } + m_ovr.swap(_hmd, true); + // need to swap GL render context even if OVR is enabled to get the mirror texture in the output + m_glctx.swap(); } } @@ -2386,10 +2533,6 @@ namespace bgfx { namespace gl uint32_t width = m_resolution.m_width; uint32_t height = m_resolution.m_height; - if (m_ovr.isEnabled() ) - { - m_ovr.getSize(width, height); - } GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, m_backBufferFbo) ); GL_CHECK(glViewport(0, 0, width, height) ); @@ -2846,63 +2989,22 @@ namespace bgfx { namespace gl void ovrPostReset() { #if BGFX_CONFIG_USE_OVR - if (m_resolution.m_flags & (BGFX_RESET_HMD|BGFX_RESET_HMD_DEBUG) ) - { - ovrGLConfig config; - config.OGL.Header.API = ovrRenderAPI_OpenGL; -# if OVR_VERSION > OVR_VERSION_043 - config.OGL.Header.BackBufferSize.w = m_resolution.m_width; - config.OGL.Header.BackBufferSize.h = m_resolution.m_height; -# else - config.OGL.Header.RTSize.w = m_resolution.m_width; - config.OGL.Header.RTSize.h = m_resolution.m_height; -# endif // OVR_VERSION > OVR_VERSION_043 - config.OGL.Header.Multisample = 0; - config.OGL.Window = (HWND)g_platformData.nwh; - config.OGL.DC = GetDC(config.OGL.Window); - if (m_ovr.postReset(g_platformData.nwh, &config.Config, !!(m_resolution.m_flags & BGFX_RESET_HMD_DEBUG) ) ) + if (m_resolution.m_flags & (BGFX_RESET_HMD | BGFX_RESET_HMD_DEBUG)) + { + if (m_ovr.postReset()) { - uint32_t size = sizeof(uint32_t) + sizeof(TextureCreate); - const Memory* mem = alloc(size); - - bx::StaticMemoryBlockWriter writer(mem->data, mem->size); - uint32_t magic = BGFX_CHUNK_MAGIC_TEX; - bx::write(&writer, magic); - - TextureCreate tc; - tc.m_flags = BGFX_TEXTURE_RT|( ((m_resolution.m_flags & BGFX_RESET_MSAA_MASK) >> BGFX_RESET_MSAA_SHIFT) << BGFX_TEXTURE_RT_MSAA_SHIFT);; - tc.m_width = m_ovr.m_rtSize.w; - tc.m_height = m_ovr.m_rtSize.h; - tc.m_sides = 0; - tc.m_depth = 0; - tc.m_numMips = 1; - tc.m_format = uint8_t(bgfx::TextureFormat::BGRA8); - tc.m_cubeMap = false; - tc.m_mem = NULL; - bx::write(&writer, tc); - - m_ovrRT.create(mem, tc.m_flags, 0); - release(mem); - - m_ovrFbo = m_msaaBackBufferFbo; - - GL_CHECK(glGenFramebuffers(1, &m_msaaBackBufferFbo) ); - GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, m_msaaBackBufferFbo) ); - - GL_CHECK(glFramebufferTexture2D(GL_FRAMEBUFFER - , GL_COLOR_ATTACHMENT0 - , GL_TEXTURE_2D - , m_ovrRT.m_id - , 0 - ) ); - - GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, m_ovrFbo) ); + 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, OVRBufferGL(m_ovr.m_hmd, eyeIdx)); + } + } - ovrGLTexture texture; - texture.OGL.Header.API = ovrRenderAPI_OpenGL; - texture.OGL.Header.TextureSize = m_ovr.m_rtSize; - texture.OGL.TexId = m_ovrRT.m_id; - m_ovr.postReset(texture.Texture); + // recreate mirror texture + m_ovr.m_mirror = BX_NEW(g_allocator, OVRMirrorGL); + m_ovr.m_mirror->init(m_ovr.m_hmd, m_resolution.m_width, m_resolution.m_height); } } #endif // BGFX_CONFIG_USE_OVR @@ -2912,14 +3014,6 @@ namespace bgfx { namespace gl { #if BGFX_CONFIG_USE_OVR m_ovr.preReset(); - if (m_ovr.isEnabled() ) - { - GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, 0) ); - GL_CHECK(glDeleteFramebuffers(1, &m_msaaBackBufferFbo) ); - m_msaaBackBufferFbo = m_ovrFbo; - m_ovrFbo = 0; - m_ovrRT.destroy(); - } #endif // BGFX_CONFIG_USE_OVR } @@ -3343,8 +3437,6 @@ namespace bgfx { namespace gl const char* m_glslVersion; OVR m_ovr; - TextureGL m_ovrRT; - GLint m_ovrFbo; }; RendererContextGL* s_renderGL; @@ -3636,7 +3728,7 @@ namespace bgfx { namespace gl } m_numPredefined = 0; - m_numSamplers = 0; + m_numSamplers = 0; BX_TRACE("Uniforms (%d):", activeUniforms); for (int32_t ii = 0; ii < activeUniforms; ++ii) @@ -5428,7 +5520,7 @@ namespace bgfx { namespace gl _render->m_hmdInitialized = m_ovr.isInitialized(); - const bool hmdEnabled = m_ovr.isEnabled() || m_ovr.isDebug(); + const bool hmdEnabled = m_ovr.isEnabled(); ViewState viewState(_render, hmdEnabled); uint16_t programIdx = invalidHandle; @@ -5478,12 +5570,13 @@ namespace bgfx { namespace gl m_occlusionQuery.resolve(_render); } + uint8_t eye = 0; + if (0 == (_render->m_debug&BGFX_DEBUG_IFH) ) { GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, m_msaaBackBufferFbo) ); bool viewRestart = false; - uint8_t eye = 0; uint8_t restartState = 0; viewState.m_rect = _render->m_rect[0]; @@ -5565,6 +5658,9 @@ namespace bgfx { namespace gl if (m_ovr.isEnabled() ) { m_ovr.getViewport(eye, &viewState.m_rect); + // commit previous eye to HMD and start rendering new frame + m_ovr.commitEye(eye); + m_ovr.renderEyeStart(eye); } else { @@ -5584,7 +5680,7 @@ namespace bgfx { namespace gl } const Rect& scissorRect = _render->m_scissor[view]; - viewHasScissor = !scissorRect.isZero(); + viewHasScissor = !scissorRect.isZero(); viewScissorRect = viewHasScissor ? scissorRect : viewState.m_rect; GL_CHECK(glViewport(viewState.m_rect.m_x @@ -5666,7 +5762,7 @@ namespace bgfx { namespace gl const RenderCompute& compute = renderItem.compute; ProgramGL& program = m_program[key.m_program]; - GL_CHECK(glUseProgram(program.m_id) ); + GL_CHECK(glUseProgram(program.m_id) ); GLbitfield barrier = 0; for (uint32_t ii = 0; ii < BGFX_MAX_COMPUTE_BINDINGS; ++ii) diff --git a/3rdparty/bgfx/src/renderer_gl.h b/3rdparty/bgfx/src/renderer_gl.h index 65aebcbaa39..3bed1f17c80 100644 --- a/3rdparty/bgfx/src/renderer_gl.h +++ b/3rdparty/bgfx/src/renderer_gl.h @@ -107,8 +107,8 @@ typedef uint64_t GLuint64; #endif // BGFX_CONFIG_RENDERER_OPENGL #include "renderer.h" -#include "ovr.h" -#include "renderdoc.h" +#include "hmd_ovr.h" +#include "debug_renderdoc.h" #ifndef GL_LUMINANCE # define GL_LUMINANCE 0x1909 @@ -791,6 +791,22 @@ typedef uint64_t GLuint64; # define GL_CONSERVATIVE_RASTERIZATION_NV 0x9346 #endif // GL_CONSERVATIVE_RASTERIZATION_NV +#ifndef GL_NEGATIVE_ONE_TO_ONE +# define GL_NEGATIVE_ONE_TO_ONE 0x935E +#endif // GL_NEGATIVE_ONE_TO_ONE + +#ifndef GL_ZERO_TO_ONE +# define GL_ZERO_TO_ONE 0x935F +#endif // GL_ZERO_TO_ONE + +#ifndef GL_LOWER_LEFT +# define GL_LOWER_LEFT 0x8CA1 +#endif // GL_LOWER_LEFT + +#ifndef GL_UPPER_LEFT +# define GL_UPPER_LEFT 0x8CA2 +#endif // GL_UPPER_LEFT + // _KHR or _ARB... #define GL_DEBUG_OUTPUT_SYNCHRONOUS 0x8242 #define GL_DEBUG_NEXT_LOGGED_MESSAGE_LENGTH 0x8243 |