Skip to content

Commit

Permalink
HACK: make compositor work. Get glname from undistorted texture and p…
Browse files Browse the repository at this point in the history
…ut to compositor. need vr_use_offscreen_rendering enabled and multicore rendering disabled to work
  • Loading branch information
mittorn committed Sep 16, 2023
1 parent 82d8055 commit f4e00c3
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 8 deletions.
1 change: 1 addition & 0 deletions materialsystem/cmaterialsystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,7 @@ class CMaterialSystem : public CTier2AppSystem< IMaterialSystemInternal >, publi
DELEGATE_TO_OBJECT_0( bool, OnFlushBufferedPrimitives, GetRenderContextInternal() );
void OnThreadEvent( uint32 threadEvent );
ShaderAPITextureHandle_t GetShaderAPITextureBindHandle( ITexture *pTexture, int nFrame, int nTextureChannel ); // JasonM ????
uint32_t GetShaderAPIGLTexture( ITexture *pTexture, int nFrame, int nTextureChannel ); // fuck



Expand Down
53 changes: 53 additions & 0 deletions materialsystem/gltexturehack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include "pch_materialsystem.h"
#include "togl/rendermechanism.h"

#define MATSYS_INTERNAL

#include "cmaterialsystem.h"
//-----------------------------------------------------------------------------
// The Base implementation of the shader rendering interface
//-----------------------------------------------------------------------------
class CShaderAPIBase : public IShaderAPI
{
public:
// constructor, destructor
CShaderAPIBase();
virtual ~CShaderAPIBase();

// Called when the device is initializing or shutting down
virtual bool OnDeviceInit() = 0;
virtual void OnDeviceShutdown() = 0;

// Pix events
virtual void BeginPIXEvent( unsigned long color, const char *szName ) = 0;
virtual void EndPIXEvent() = 0;
virtual void AdvancePIXFrame() = 0;

// Release, reacquire objects
virtual void ReleaseShaderObjects() = 0;
virtual void RestoreShaderObjects() = 0;

// Resets the render state to its well defined initial value
virtual void ResetRenderState( bool bFullReset = true ) = 0;

// Returns a d3d texture associated with a texture handle
virtual IDirect3DBaseTexture9* GetD3DTexture( ShaderAPITextureHandle_t hTexture ) = 0;

// Queues a non-full reset of render state next BeginFrame.
virtual void QueueResetRenderState() = 0;

// Methods of IShaderDynamicAPI
public:
virtual void GetCurrentColorCorrection( ShaderColorCorrectionInfo_t* pInfo );

protected:
};

uint32_t CMaterialSystem::GetShaderAPIGLTexture( ITexture *pTexture, int nFrame, int nTextureChannel )
{
ShaderAPITextureHandle_t handle = ShaderSystem()->GetShaderAPITextureBindHandle( pTexture, nFrame, nTextureChannel );
IDirect3DTexture9* pTex = ((CShaderAPIBase*)g_pShaderAPI)->GetD3DTexture(handle);
IDirect3DSurface9* surf = pTex->m_surfZero;
CGLMTex *tex = surf->m_tex;
return tex->GetTexName();
}
2 changes: 1 addition & 1 deletion materialsystem/mat_stub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2276,7 +2276,7 @@ class CDummyMaterialSystem : public IMaterialSystemStub, public CRefCounted1<IMa
{
return false;
}

virtual uint32_t GetShaderAPIGLTexture(ITexture*, int, int) {return 0;}
};


Expand Down
1 change: 1 addition & 0 deletions materialsystem/wscript
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def build(bld):
'cmatrendercontext.cpp',
'cmatqueuedrendercontext.cpp',
'ctexturecompositor.cpp',
'gltexturehack.cpp',
'../public/tier0/memoverride.cpp'
]

Expand Down
1 change: 1 addition & 0 deletions public/materialsystem/imaterialsystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -1093,6 +1093,7 @@ abstract_class IMaterialSystem : public IAppSystem

// Performs final verification of all compositor templates (after they've all been initially loaded).
virtual bool VerifyTextureCompositorTemplates( ) = 0;
virtual uint32_t GetShaderAPIGLTexture( ITexture *pTexture, int nFrame, int nTextureChannel ) = 0;
};


Expand Down
62 changes: 56 additions & 6 deletions sourcevr/sourcevirtualreality.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ void CSourceVirtualReality::GetViewportBounds( VREye eEye, int *pnX, int *pnY, i
}
else
{
uint32_t x, y, w, h;
uint32_t x = 0, y = 0, w = 640, h = 480;
// m_pHmd->GetEyeOutputViewport( SourceEyeToHmdEye( eEye ), &x, &y, &w, &h );
m_pExtDisplay->GetEyeOutputViewport( SourceEyeToHmdEye( eEye ), &x, &y, &w, &h );
if( pnX && pnY )
Expand Down Expand Up @@ -543,7 +543,13 @@ bool CSourceVirtualReality::SampleTrackingState ( float PlayerGameFov, float fPr
{
if( !m_pHmd || !m_bActive )
return false;

vr::VREvent_t event;
while( m_pHmd->PollNextEvent( &event, sizeof( event ) ) )
{
//ProcessVREvent( event );
}
vr::TrackedDevicePose_t m_rTrackedDevicePose[ vr::k_unMaxTrackedDeviceCount ];
vr::VRCompositor()->WaitGetPoses(m_rTrackedDevicePose, vr::k_unMaxTrackedDeviceCount, NULL, 0 );
// If tracker can't return a pose (it's possibly recalibrating itself)
// then we will freeze tracking at its current state, rather than
// snapping it back to the zero position
Expand Down Expand Up @@ -577,6 +583,7 @@ bool CSourceVirtualReality::SampleTrackingState ( float PlayerGameFov, float fPr

return true;
}
#include "togl/rendermechanism.h"


// ----------------------------------------------------------------------
Expand All @@ -594,10 +601,16 @@ bool CSourceVirtualReality::DoDistortionProcessing ( VREye eEye )
CMatRenderContextPtr pRenderContext( materials );

IMaterial *pDistortMaterial;
ITexture *pDistortTexture;

if( eEye == VREye_Left )
pDistortMaterial = m_DistortLeftMaterial;
else
pDistortMaterial = m_DistortRightMaterial;
if( eEye == VREye_Left )
pDistortTexture = m_pDistortionTextureLeft;
else
pDistortTexture = m_pDistortionTextureRight;

if( !UsingOffscreenRenderTarget() )
{
Expand All @@ -607,17 +620,46 @@ bool CSourceVirtualReality::DoDistortionProcessing ( VREye eEye )
return false;

Rect_t r;
r.x = !eEye?0:640;
r.y = 0;
r.width = 640;
r.height = 480;
this->GetViewportBounds( eEye, &r.x, &r.y, &r.width, &r.height );
pRenderContext->CopyRenderTargetToTextureEx( pFullFrameFB1, 0, &r, &r );
}

// This is where we are rendering to
uint32_t x, y, w, h;
x = !eEye?0:640;
y = 0;
w = 640;
h = 480;
m_pExtDisplay->GetEyeOutputViewport( SourceEyeToHmdEye( eEye ), &x, &y, &w, &h );

pRenderContext->DrawScreenSpaceRectangle ( pDistortMaterial,
x, y, w, h,
0, 0, distortionTextureSize-1,distortionTextureSize-1,distortionTextureSize,distortionTextureSize);
static int id = -1;
//static CDynamicFunctionOpenGL< true, GLvoid ( APIENTRY *)(GLenum pname, GLint *params), GLvoid > glGetIntegerv("glGetIntegerv");
// pRenderContext->Bind(pDistortMaterial);
// pRenderContext->Flush( true );
// ShaderAPITextureHandle_t hndl = materials->GetShaderAPITextureBindHandle(pDistortTexture,0,0);
//if(id < 0)
id = materials->GetShaderAPIGLTexture(m_pPredistortRT,0,0);

static int last_tex[2] = {-1, -1};
// glGetIntegerv(GL_TEXTURE_BINDING_2D, &id);
if(id > 0)
last_tex[eEye != VREye_Left] = id;
Msg("tex %d\n", id);
const vr::VRTextureBounds_t bounds = { 0.0f, 1.0f, 1.0f, 0.0f };
vr::Texture_t eyeTexture = {(void*)(uintptr_t)last_tex[eEye != VREye_Left], vr::TextureType_OpenGL, vr::ColorSpace_Gamma };
if(last_tex[eEye != VREye_Left] <= 0)
return true;
// if(eEye != VREye_Left)
// return 0;
glFinish();
vr::VRCompositor()->Submit(SourceEyeToHmdEye( eEye ), &eyeTexture, &bounds );

return true;
}
Expand Down Expand Up @@ -687,11 +729,15 @@ bool CSourceVirtualReality::CompositeHud ( VREye eEye, float ndcHudBounds[4], bo
CMatRenderContextPtr pRenderContext( materials );

uint32_t x, y, w, h;
x = !eEye?0:640;
y = 0;
w = 640;
h = 480;
m_pExtDisplay->GetEyeOutputViewport( SourceEyeToHmdEye( eEye ), &x, &y, &w, &h );

pRenderContext->DrawScreenSpaceRectangle ( pDistortHUDMaterial,
x, y, w, h,
0, 0, distortionTextureSize-1,distortionTextureSize-1,distortionTextureSize,distortionTextureSize);
// pRenderContext->DrawScreenSpaceRectangle ( pDistortHUDMaterial,
// x, y, w, h,
// 0, 0, distortionTextureSize-1,distortionTextureSize-1,distortionTextureSize,distortionTextureSize);

return true;
}
Expand All @@ -714,13 +760,17 @@ bool CSourceVirtualReality::StartTracker()
m_pHmd = vr::VR_Init( &err, vr::VRApplication_Scene );
m_pExtDisplay = vr::VRExtendedDisplay();
m_pChap = vr::VRChaperone();

if( err != vr::VRInitError_None )
{
Msg( "Unable to initialize HMD tracker. Error code %d\n", err );
return false;
}

if( !vr::VRCompositor() )
{
Msg("Compositor initialization failed. See log file for details");
}

m_pChap->ResetZeroPose(TrackingUniverseSeated);

m_bHaveValidPose = false;
Expand Down
3 changes: 2 additions & 1 deletion sourcevr/wscript
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def configure(conf):
conf.env.append_unique('DEFINES',['strncpy=use_Q_strncpy_instead',
'_snprintf=use_Q_snprintf_instead','SOURCEVR_DLL'])
conf.check_cfg(package='openvr', uselib_store='OPENVR', args=['--cflags', '--libs'])
conf.check(lib='GL', uselib_store='GL')

def build(bld):
source = [
Expand All @@ -30,7 +31,7 @@ def build(bld):

defines = []

libs = ['tier0','tier1','tier2', 'tier3','vstdlib','mathlib','OPENVR']
libs = ['tier0','tier1','tier2', 'tier3','vstdlib','mathlib','OPENVR', 'GL']

install_path = bld.env.LIBDIR

Expand Down

0 comments on commit f4e00c3

Please sign in to comment.