Skip to content

Implement MSAA #1480

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion libs/crunch
8 changes: 8 additions & 0 deletions src/engine/renderer/Material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -982,6 +982,9 @@ void BindShaderHeatHaze( Material* material ) {
gl_heatHazeShaderMaterial->SetUniform_DeformEnable( true );

// draw to background image
if ( r_msaa.Get() ) {
GL_BlitMSAAToFBO( tr.mainFBO[backEnd.currentMainFBO] );
}
R_BindFBO( tr.mainFBO[1 - backEnd.currentMainFBO] );
}

Expand Down Expand Up @@ -2286,6 +2289,11 @@ void MaterialSystem::RenderMaterial( Material& material, const uint32_t viewID )
R_BindFBO( tr.mainFBO[backEnd.currentMainFBO] );

RenderIndirect( material, viewID );

if ( r_msaa.Get() ) {
GL_BlitFBOToMSAA( tr.mainFBO[backEnd.currentMainFBO] );
R_BindFBO( tr.msaaFBO );
}
}

if ( r_showTris->integer
Expand Down
45 changes: 44 additions & 1 deletion src/engine/renderer/tr_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,26 @@ GLuint64 GL_BindToTMU( int unit, image_t *image )
return 0;
}

void GL_BlitFBOToMSAA( FBO_t* fbo ) {
R_BindFBO( GL_READ_FRAMEBUFFER, fbo );
R_BindFBO( GL_DRAW_FRAMEBUFFER, tr.msaaFBO );
glBlitFramebuffer( 0, 0, fbo->width, fbo->height, 0, 0, tr.msaaFBO->width, tr.msaaFBO->height,
GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT, GL_NEAREST );

R_BindFBO( GL_DRAW_FRAMEBUFFER, fbo );
glState.currentFBO = nullptr;
}

void GL_BlitMSAAToFBO( FBO_t* fbo ) {
R_BindFBO( GL_READ_FRAMEBUFFER, tr.msaaFBO );
R_BindFBO( GL_DRAW_FRAMEBUFFER, fbo );
glBlitFramebuffer( 0, 0, tr.msaaFBO->width, tr.msaaFBO->height, 0, 0, fbo->width, fbo->height,
GL_COLOR_BUFFER_BIT /* | GL_DEPTH_BUFFER_BIT */, GL_NEAREST );

R_BindFBO( GL_READ_FRAMEBUFFER, fbo );
glState.currentFBO = nullptr;
}

void GL_BlendFunc( GLenum sfactor, GLenum dfactor )
{
if ( glState.blendSrc != ( signed ) sfactor || glState.blendDst != ( signed ) dfactor )
Expand Down Expand Up @@ -3027,6 +3047,10 @@ void RB_RenderBloom()
GL_BindToTMU( 0, tr.currentRenderImage[backEnd.currentMainFBO] )
);

if ( r_msaa.Get() ) {
GL_BlitMSAAToFBO( tr.mainFBO[backEnd.currentMainFBO] );
}

R_BindFBO( tr.contrastRenderFBO );
GL_ClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
glClear( GL_COLOR_BUFFER_BIT );
Expand Down Expand Up @@ -3078,6 +3102,7 @@ void RB_RenderBloom()

gl_screenShader->SetUniform_CurrentMapBindless( GL_BindToTMU( 0, tr.bloomRenderFBOImage[flip ^ 1] ) );

<<<<<<< HEAD
GL_PushMatrix();

matrix_t ortho;
Expand All @@ -3091,6 +3116,11 @@ void RB_RenderBloom()
GL_PopMatrix();
}

if ( r_msaa.Get() ) {
GL_BlitFBOToMSAA( tr.mainFBO[backEnd.currentMainFBO] );
R_BindFBO( tr.msaaFBO );
}

GL_CheckErrors();
}

Expand All @@ -3110,6 +3140,10 @@ void RB_RenderMotionBlur()

gl_motionblurShader->BindProgram( 0 );

if ( r_msaa.Get() ) {
GL_BlitMSAAToFBO( tr.mainFBO[backEnd.currentMainFBO] );
}

// Swap main FBOs
gl_motionblurShader->SetUniform_ColorMapBindless(
GL_BindToTMU( 0, tr.currentRenderImage[backEnd.currentMainFBO] )
Expand All @@ -3125,6 +3159,11 @@ void RB_RenderMotionBlur()

Tess_InstantScreenSpaceQuad();

if ( r_msaa.Get() ) {
GL_BlitFBOToMSAA( tr.mainFBO[backEnd.currentMainFBO] );
R_BindFBO( tr.msaaFBO );
}

GL_CheckErrors();
}

Expand Down Expand Up @@ -4552,7 +4591,11 @@ static void RB_RenderView( bool depthPass )
backEnd.pc.c_surfaces += backEnd.viewParms.numDrawSurfs;

// disable offscreen rendering
R_BindFBO( tr.mainFBO[ backEnd.currentMainFBO ] );
if ( r_msaa.Get() ) {
R_BindFBO( tr.msaaFBO );
} else {
R_BindFBO( tr.mainFBO[backEnd.currentMainFBO] );
}

// we will need to change the projection matrix before drawing
// 2D images again
Expand Down
34 changes: 26 additions & 8 deletions src/engine/renderer/tr_fbo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,8 @@ R_AttachFBOTexture2D
*/
void R_AttachFBOTexture2D( int target, int texId, int index )
{
if ( target != GL_TEXTURE_2D && ( target < GL_TEXTURE_CUBE_MAP_POSITIVE_X || target > GL_TEXTURE_CUBE_MAP_NEGATIVE_Z ) )
if ( target != GL_TEXTURE_2D && target != GL_TEXTURE_2D_MULTISAMPLE
&& ( target < GL_TEXTURE_CUBE_MAP_POSITIVE_X || target > GL_TEXTURE_CUBE_MAP_NEGATIVE_Z ) )
{
Log::Warn("R_AttachFBOTexture2D: invalid target %i", target );
return;
Expand Down Expand Up @@ -355,28 +356,34 @@ void R_AttachFBOTexturePackedDepthStencil( int texId )
GL_fboShim.glFramebufferTexture2D( GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, texId, 0 );
}

void R_AttachFBOTexturePackedDepthStencilMSAA( int texId ) {
GL_fboShim.glFramebufferTexture2D( GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D_MULTISAMPLE, texId, 0 );
GL_fboShim.glFramebufferTexture2D( GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D_MULTISAMPLE, texId, 0 );
}

/*
============
R_BindFBO
============
*/
void R_BindFBO( FBO_t *fbo )
{
if ( !fbo )
{
R_BindFBO( GL_FRAMEBUFFER, fbo );
}

void R_BindFBO( const GLenum target, FBO_t* fbo ) {
if ( !fbo ) {
R_BindNullFBO();
return;
}

if ( r_logFile->integer )
{
if ( r_logFile->integer ) {
// don't just call LogComment, or we will get a call to va() every frame!
GLimp_LogComment( va( "--- R_BindFBO( %s ) ---\n", fbo->name ) );
}

if ( glState.currentFBO != fbo )
{
GL_fboShim.glBindFramebuffer( GL_FRAMEBUFFER, fbo->frameBuffer );
if ( glState.currentFBO != fbo ) {
GL_fboShim.glBindFramebuffer( target, fbo->frameBuffer );

glState.currentFBO = fbo;
}
Expand Down Expand Up @@ -436,6 +443,17 @@ void R_InitFBOs()
R_AttachFBOTexturePackedDepthStencil( tr.currentDepthImage->texnum );
R_CheckFBO( tr.mainFBO[1] );

if( r_msaa.Get() ) {
tr.msaaFBO = R_CreateFBO( "msaa", width, height );
R_BindFBO( tr.msaaFBO );
GL_CheckErrors();
R_AttachFBOTexture2D( GL_TEXTURE_2D_MULTISAMPLE, tr.currentRenderImageMSAA->texnum, 0 );
GL_CheckErrors();
R_AttachFBOTexturePackedDepthStencilMSAA( tr.currentDepthImageMSAA->texnum );
GL_CheckErrors();
R_CheckFBO( tr.msaaFBO );
}

if ( glConfig2.realtimeLighting
&& r_realtimeLightingRenderer.Get() == Util::ordinal( realtimeLightingRenderer_t::TILED ) )
{
Expand Down
Loading
Loading