Hooked in the ability to do postfx after everything else is rendered

This commit is contained in:
Robert Beckebans 2023-12-28 14:39:59 +01:00
parent 49130533c6
commit ed6b61a9f9
12 changed files with 129 additions and 57 deletions

View file

@ -0,0 +1,6 @@
cd ..
del /s /q build
mkdir build
cd build
cmake -G "Visual Studio 16" -A x64 -DFFMPEG=OFF -DBINKDEC=ON -DUSE_VULKAN=OFF ../neo
pause

View file

@ -429,7 +429,6 @@ idPlayerView::SingleView
*/
void idPlayerView::SingleView( const renderView_t* view, idMenuHandler_HUD* hudManager )
{
// normal rendering
if( !view )
{

View file

@ -404,6 +404,8 @@ void idCommonLocal::Draw()
// draw the half console / notify console on top of everything
console->Draw( false );
renderSystem->DrawCRTPostFX();
}
}

View file

@ -5374,6 +5374,10 @@ void idRenderBackend::ExecuteBackEndCommands( const emptyCommand_t* cmds )
break;
}
case RC_CRT_POST_PROCESS:
CRTPostProcess();
break;
default:
common->Error( "RB_ExecuteBackEndCommands: bad commandId" );
break;
@ -6171,3 +6175,74 @@ void idRenderBackend::PostProcess( const void* data )
renderLog.CloseBlock();
renderLog.CloseMainBlock();
}
void idRenderBackend::CRTPostProcess()
{
//renderLog.OpenMainBlock( MRB_POSTPROCESS );
renderLog.OpenBlock( "Render_CRTPostFX", colorBlue );
GL_State( GLS_SRCBLEND_ONE | GLS_DSTBLEND_ZERO | GLS_DEPTHMASK | GLS_DEPTHFUNC_ALWAYS | GLS_CULL_TWOSIDED );
int screenWidth = renderSystem->GetWidth();
int screenHeight = renderSystem->GetHeight();
// set the window clipping
GL_Viewport( 0, 0, screenWidth, screenHeight );
GL_Scissor( 0, 0, screenWidth, screenHeight );
if( r_useCRTPostFX.GetInteger() > 0 )
{
BlitParameters blitParms;
blitParms.sourceTexture = ( nvrhi::ITexture* )globalImages->ldrImage->GetTextureID();
blitParms.targetFramebuffer = globalFramebuffers.smaaBlendFBO->GetApiObject();
blitParms.targetViewport = nvrhi::Viewport( renderSystem->GetWidth(), renderSystem->GetHeight() );
commonPasses.BlitTexture( commandList, blitParms, &bindingCache );
GL_SelectTexture( 0 );
globalImages->smaaBlendImage->Bind();
globalFramebuffers.ldrFBO->Bind();
GL_SelectTexture( 1 );
globalImages->blueNoiseImage256->Bind();
renderProgManager.BindShader_CrtNewPixie();
float jitterTexOffset[4];
jitterTexOffset[0] = 1.0f / globalImages->blueNoiseImage256->GetUploadWidth();
jitterTexOffset[1] = 1.0f / globalImages->blueNoiseImage256->GetUploadHeight();
if( r_shadowMapRandomizeJitter.GetBool() )
{
jitterTexOffset[2] = Sys_Milliseconds() / 1000.0f;
jitterTexOffset[3] = tr.frameCount % 64;
}
else
{
jitterTexOffset[2] = 0.0f;
jitterTexOffset[3] = 0.0f;
}
SetFragmentParm( RENDERPARM_JITTERTEXOFFSET, jitterTexOffset ); // rpJitterTexOffset
// Draw
DrawElementsWithCounters( &unitSquareSurface );
}
GL_SelectTexture( 0 );
renderProgManager.Unbind();
// copy LDR result to DX12 / Vulkan swapchain image
BlitParameters blitParms;
blitParms.sourceTexture = ( nvrhi::ITexture* )globalImages->ldrImage->GetTextureID();
blitParms.targetFramebuffer = deviceManager->GetCurrentFramebuffer();
blitParms.targetViewport = nvrhi::Viewport( renderSystem->GetWidth(), renderSystem->GetHeight() );
commonPasses.BlitTexture( commandList, blitParms, &bindingCache );
GL_SelectTexture( 0 );
globalImages->currentRenderImage->Bind();
renderLog.CloseBlock();
//renderLog.CloseMainBlock();
}

View file

@ -208,7 +208,9 @@ private:
// Experimental feature
void MotionBlur();
void PostProcess( const void* data );
void CRTPostProcess();
private:
void GL_StartFrame();

View file

@ -699,11 +699,12 @@ TR_CMDS
enum renderCommand_t
{
RC_NOP,
RC_DRAW_VIEW_3D, // may be at a reduced resolution, will be upsampled before 2D GUIs
RC_DRAW_VIEW_GUI, // not resolution scaled
RC_DRAW_VIEW_3D, // may be at a reduced resolution, will be upsampled before 2D GUIs
RC_DRAW_VIEW_GUI, // not resolution scaled
RC_SET_BUFFER,
RC_COPY_RENDER,
RC_POST_PROCESS,
RC_POST_PROCESS, // postfx after scene rendering is done but before GUI rendering
RC_CRT_POST_PROCESS, // CRT simulation after everything has been rendered on the final swapchain image
};
struct emptyCommand_t
@ -746,6 +747,12 @@ struct postProcessCommand_t
viewDef_t* viewDef;
};
struct crtPostProcessCommand_t
{
renderCommand_t commandId;
renderCommand_t* next;
};
//=======================================================================
// this is the inital allocation for max number of drawsurfs
@ -932,6 +939,8 @@ public:
virtual void DrawBigChar( int x, int y, int ch );
virtual void DrawBigStringExt( int x, int y, const char* string, const idVec4& setColor, bool forceColor );
virtual void DrawCRTPostFX(); // RB
virtual void WriteDemoPics();
virtual void WriteEndFrame();
virtual void DrawDemoPics();
@ -950,7 +959,6 @@ public:
virtual void CropRenderSize( int width, int height );
virtual void CropRenderSize( int x, int y, int width, int height, bool topLeftAncor );
virtual void CaptureRenderToImage( const char* imageName, bool clearColorAfterCopy = false );
virtual void CaptureRenderToFile( const char* fileName, bool fixAlpha );
virtual void UnCrop();
virtual bool UploadImage( const char* imageName, const byte* data, int width, int height );
@ -1283,6 +1291,8 @@ extern idCVar r_taaClampingFactor;
extern idCVar r_taaNewFrameWeight;
extern idCVar r_taaMaxRadiance;
extern idCVar r_taaMotionVectors;
extern idCVar r_useCRTPostFX;
// RB end
/*

View file

@ -522,6 +522,7 @@ void idRenderProgManager::Init( nvrhi::IDevice* device )
{ BUILTIN_WOBBLESKY, "builtin/legacy/wobblesky", "", {}, false, SHADER_STAGE_DEFAULT, LAYOUT_DRAW_VERT, BINDING_LAYOUT_DEFAULT },
{ BUILTIN_POSTPROCESS, "builtin/post/postprocess", "", {}, false, SHADER_STAGE_DEFAULT, LAYOUT_DRAW_VERT, BINDING_LAYOUT_POST_PROCESS_FINAL },
{ BUILTIN_POSTPROCESS_RETRO_C64, "builtin/post/retro_c64", "", {}, false, SHADER_STAGE_DEFAULT, LAYOUT_DRAW_VERT, BINDING_LAYOUT_POST_PROCESS_FINAL },
{ BUILTIN_CRT_NUPIXIE, "builtin/post/crt_newpixie", "", {}, false, SHADER_STAGE_DEFAULT, LAYOUT_DRAW_VERT, BINDING_LAYOUT_POST_PROCESS_FINAL },
{ BUILTIN_SCREEN, "builtin/post/screen", "", {}, false, SHADER_STAGE_DEFAULT, LAYOUT_DRAW_VERT, BINDING_LAYOUT_DEFAULT },
{ BUILTIN_TONEMAP, "builtin/post/tonemap", "", { { "BRIGHTPASS", "0" }, { "HDR_DEBUG", "0"} }, false, SHADER_STAGE_DEFAULT, LAYOUT_DRAW_VERT, BINDING_LAYOUT_DEFAULT },

View file

@ -365,8 +365,9 @@ enum
BUILTIN_SKYBOX,
BUILTIN_WOBBLESKY,
BUILTIN_POSTPROCESS,
BUILTIN_POSTPROCESS_RETRO_C64,
// RB begin
BUILTIN_POSTPROCESS_RETRO_C64,
BUILTIN_CRT_NUPIXIE,
BUILTIN_SCREEN,
BUILTIN_TONEMAP,
BUILTIN_BRIGHTPASS,
@ -823,6 +824,11 @@ public:
BindShader_Builtin( BUILTIN_POSTPROCESS_RETRO_C64 );
}
void BindShader_CrtNewPixie()
{
BindShader_Builtin( BUILTIN_CRT_NUPIXIE );
}
void BindShader_Screen()
{
BindShader_Builtin( BUILTIN_SCREEN );

View file

@ -782,7 +782,6 @@ const emptyCommand_t* idRenderSystemLocal::SwapCommandBuffers_FinishCommandBuffe
// set the time for shader effects in 2D rendering
frameShaderTime = Sys_Milliseconds() * 0.001;
// RB: TODO RC_SET_BUFFER is not handled in OpenGL
setBufferCommand_t* cmd2 = ( setBufferCommand_t* )R_GetCommandBuffer( sizeof( *cmd2 ) );
cmd2->commandId = RC_SET_BUFFER;
cmd2->buffer = 0;
@ -1038,53 +1037,6 @@ void idRenderSystemLocal::CaptureRenderToImage( const char* imageName, bool clea
guiModel->Clear();
}
/*
==============
idRenderSystemLocal::CaptureRenderToFile
==============
*/
void idRenderSystemLocal::CaptureRenderToFile( const char* fileName, bool fixAlpha )
{
if( !IsInitialized() )
{
return;
}
idScreenRect& rc = renderCrops[currentRenderCrop];
guiModel->EmitFullScreen();
guiModel->Clear();
RenderCommandBuffers( frameData->cmdHead );
// TODO implement for NVRHI
#if !defined( USE_VULKAN ) && !defined( USE_NVRHI )
glReadBuffer( GL_BACK );
// include extra space for OpenGL padding to word boundaries
int c = ( rc.GetWidth() + 3 ) * rc.GetHeight();
byte* data = ( byte* )R_StaticAlloc( c * 3 );
glReadPixels( rc.x1, rc.y1, rc.GetWidth(), rc.GetHeight(), GL_RGB, GL_UNSIGNED_BYTE, data );
byte* data2 = ( byte* )R_StaticAlloc( c * 4 );
for( int i = 0 ; i < c ; i++ )
{
data2[ i * 4 ] = data[ i * 3 ];
data2[ i * 4 + 1 ] = data[ i * 3 + 1 ];
data2[ i * 4 + 2 ] = data[ i * 3 + 2 ];
data2[ i * 4 + 3 ] = 0xff;
}
R_WriteTGA( fileName, data2, rc.GetWidth(), rc.GetHeight(), true );
R_StaticFree( data );
R_StaticFree( data2 );
#endif
}
/*
==============
@ -1153,3 +1105,19 @@ bool idRenderSystemLocal::UploadImage( const char* imageName, const byte* data,
return true;
}
// RB
void idRenderSystemLocal::DrawCRTPostFX()
{
if( !IsInitialized() )
{
return;
}
guiModel->EmitFullScreen();
guiModel->Clear();
crtPostProcessCommand_t* cmd = ( crtPostProcessCommand_t* )R_GetCommandBuffer( sizeof( *cmd ) );
cmd->commandId = RC_CRT_POST_PROCESS;
}

View file

@ -306,6 +306,8 @@ public:
virtual void DrawBigChar( int x, int y, int ch ) = 0;
virtual void DrawBigStringExt( int x, int y, const char* string, const idVec4& setColor, bool forceColor ) = 0;
virtual void DrawCRTPostFX() = 0; // RB
// dump all 2D drawing so far this frame to the demo file
virtual void WriteDemoPics() = 0;
virtual void WriteEndFrame() = 0;
@ -359,9 +361,6 @@ public:
virtual void CropRenderSize( int width, int height ) = 0;
virtual void CropRenderSize( int x, int y, int width, int height, bool topLeftAncor ) = 0;
virtual void CaptureRenderToImage( const char* imageName, bool clearColorAfterCopy = false ) = 0;
// fixAlpha will set all the alpha channel values to 0xff, which allows screen captures
// to use the default tga loading code without having dimmed down areas in many places
virtual void CaptureRenderToFile( const char* fileName, bool fixAlpha = false ) = 0;
virtual void UnCrop() = 0;
// the image has to be already loaded ( most straightforward way would be through a FindMaterial )

View file

@ -302,6 +302,8 @@ idCVar r_taaClampingFactor( "r_taaClampingFactor", "1.0", CVAR_RENDERER | CVAR_F
idCVar r_taaNewFrameWeight( "r_taaNewFrameWeight", "0.1", CVAR_RENDERER | CVAR_FLOAT, "" );
idCVar r_taaMaxRadiance( "r_taaMaxRadiance", "10000", CVAR_RENDERER | CVAR_FLOAT, "" );
idCVar r_taaMotionVectors( "r_taaMotionVectors", "1", CVAR_RENDERER | CVAR_BOOL, "" );
idCVar r_useCRTPostFX( "r_useCRTPostFX", "1", CVAR_RENDERER | CVAR_ARCHIVE | CVAR_INTEGER, "CRT simulation: 1 = New Pixie", 0, 1 );
// RB end
const char* fileExten[4] = { "tga", "png", "jpg", "exr" };

View file

@ -54,6 +54,8 @@ builtin/post/postprocess.vs.hlsl -T vs_5_0
builtin/post/postprocess.ps.hlsl -T ps_5_0
builtin/post/retro_c64.vs.hlsl -T vs_5_0
builtin/post/retro_c64.ps.hlsl -T ps_5_0
builtin/post/crt_newpixie.vs.hlsl -T vs_5_0
builtin/post/crt_newpixie.ps.hlsl -T ps_5_0
builtin/post/screen.vs.hlsl -T vs_5_0
builtin/post/screen.ps.hlsl -T ps_5_0
builtin/post/tonemap.vs.hlsl -T vs_5_0 -D BRIGHTPASS={0,1} -D HDR_DEBUG={0,1}