Advertisement
snake5

some clean-ish rendering code

Sep 20th, 2015
396
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.93 KB | None | 0 0
  1. void IGame::OnDrawScene( SGRX_IRenderControl* ctrl, SGRX_RenderScene& info )
  2. {
  3. #define RT_MAIN 0xfff0
  4. #define RT_HBLUR 0xfff1
  5. #define RT_VBLUR 0xfff2
  6. #define RT_HBLUR2 0xfff3
  7. #define RT_VBLUR2 0xfff4
  8. #define RT_HPASS 0xfff5
  9. #define RT_DEPTH 0xfff6
  10.    
  11.     // preserve state
  12.     Mat4 viewMtx = g_BatchRenderer->viewMatrix;
  13.    
  14.     // shortcuts
  15.     SGRX_Scene* scene = info.scene;
  16.     BatchRenderer& br = GR2D_GetBatchRenderer();
  17.    
  18.     int W = GR_GetWidth();
  19.     int H = GR_GetHeight();
  20.     int W4 = TMAX( W / 4, 1 ), H4 = TMAX( H / 4, 1 );
  21.     int W16 = TMAX( W4 / 4, 1 ), H16 = TMAX( H4 / 4, 1 );
  22.    
  23.     // load shaders
  24.     PixelShaderHandle pppsh_final = GR_GetPixelShader( "sys_pp_final" );
  25.     PixelShaderHandle pppsh_highpass = GR_GetPixelShader( "sys_pp_highpass" );
  26.     PixelShaderHandle pppsh_blur = GR_GetPixelShader( "sys_pp_blur" );
  27.    
  28.     GR_PreserveResource( pppsh_final );
  29.     GR_PreserveResource( pppsh_highpass );
  30.     GR_PreserveResource( pppsh_blur );
  31.    
  32.     // initial actions
  33.     ctrl->RenderShadows( scene, 0 );
  34.     ctrl->SortRenderItems( scene );
  35.    
  36.     // prepare render targets
  37.     TextureHandle rttMAIN, rttHPASS, rttHBLUR, rttVBLUR, rttHBLUR2, rttVBLUR2, rttDEPTH;
  38.     DepthStencilSurfHandle dssMAIN;
  39.     if( info.enablePostProcessing )
  40.     {
  41.         rttMAIN = GR_GetRenderTarget( W, H, RT_FORMAT_COLOR_HDR16, RT_MAIN );
  42.         rttDEPTH = GR_GetRenderTarget( W, H, RT_FORMAT_COLOR_HDR16, RT_DEPTH );
  43.         rttHPASS = GR_GetRenderTarget( W, H, RT_FORMAT_COLOR_HDR16, RT_HPASS );
  44.         rttHBLUR = GR_GetRenderTarget( W4, H, RT_FORMAT_COLOR_HDR16, RT_HBLUR );
  45.         rttVBLUR = GR_GetRenderTarget( W4, H4, RT_FORMAT_COLOR_HDR16, RT_VBLUR );
  46.         rttHBLUR2 = GR_GetRenderTarget( W16, H4, RT_FORMAT_COLOR_HDR16, RT_HBLUR2 );
  47.         rttVBLUR2 = GR_GetRenderTarget( W16, H16, RT_FORMAT_COLOR_HDR16, RT_VBLUR2 );
  48.         dssMAIN = GR_GetDepthStencilSurface( W, H, RT_FORMAT_COLOR_HDR16, RT_MAIN );
  49.        
  50.         GR_PreserveResource( rttMAIN );
  51.         GR_PreserveResource( rttDEPTH );
  52.         GR_PreserveResource( rttHPASS );
  53.         GR_PreserveResource( rttHBLUR );
  54.         GR_PreserveResource( rttVBLUR );
  55.         GR_PreserveResource( rttHBLUR2 );
  56.         GR_PreserveResource( rttVBLUR2 );
  57.         GR_PreserveResource( dssMAIN );
  58.        
  59.         ctrl->SetRenderTargets( dssMAIN, SGRX_RT_ClearAll, 0, 0, 1, rttDEPTH );
  60.         ctrl->RenderTypes( scene, 0, 1, SGRX_TY_Solid );
  61.     }
  62.    
  63.     // draw things
  64.     ctrl->SetRenderTargets( dssMAIN, SGRX_RT_ClearAll, 0, 0, 1, rttMAIN );
  65.     if( info.viewport )
  66.         GR2D_SetViewport( info.viewport->x0, info.viewport->y0, info.viewport->x1, info.viewport->y1 );
  67.    
  68.     ctrl->RenderTypes( scene, 1, 1, SGRX_TY_Solid );
  69.     ctrl->RenderTypes( scene, 3, 4, SGRX_TY_Solid );
  70.     ctrl->RenderTypes( scene, 1, 1, SGRX_TY_Decal );
  71.     ctrl->RenderTypes( scene, 3, 4, SGRX_TY_Decal );
  72.     ctrl->RenderTypes( scene, 1, 1, SGRX_TY_Transparent );
  73.     ctrl->RenderTypes( scene, 3, 4, SGRX_TY_Transparent );
  74.     if( info.postdraw )
  75.     {
  76.         GR2D_SetViewMatrix( scene->camera.mView * scene->camera.mProj );
  77.         info.postdraw->PostDraw();
  78.     }
  79.    
  80.     // post-process
  81.     GR2D_SetViewMatrix( Mat4::CreateUI( 0, 0, 1, 1 ) );
  82.     if( info.enablePostProcessing )
  83.     {
  84.         br.Reset();
  85.         br.ShaderData.push_back( V4(0) );
  86.        
  87.         float spread = 3.5f;
  88.         ctrl->SetRenderTargets( NULL, SGRX_RT_ClearAll, 0, 0, 1, rttHPASS );
  89.         br.SetTexture( rttMAIN ).SetShader( pppsh_highpass ).Quad( 0, 0, 1, 1 ).Flush();
  90.        
  91.         br.ShaderData[0] = V4( 0, 0, safe_fdiv( spread, W ), 0 );
  92.         ctrl->SetRenderTargets( NULL, SGRX_RT_ClearAll, 0, 0, 1, rttHBLUR );
  93.         br.SetTexture( rttHPASS ).SetShader( pppsh_blur ).Quad( 0, 0, 1, 1 ).Flush();
  94.        
  95.         br.ShaderData[0] = V4( 0, 0, 0, safe_fdiv( spread, H ) );
  96.         ctrl->SetRenderTargets( NULL, SGRX_RT_ClearAll, 0, 0, 1, rttVBLUR );
  97.         br.SetTexture( rttHBLUR ).SetShader( pppsh_blur ).Quad( 0, 0, 1, 1 ).Flush();
  98.        
  99.         br.ShaderData[0] = V4( 0, 0, safe_fdiv( spread, W4 ), 0 );
  100.         ctrl->SetRenderTargets( NULL, SGRX_RT_ClearAll, 0, 0, 1, rttHBLUR2 );
  101.         br.SetTexture( rttVBLUR ).SetShader( pppsh_blur ).Quad( 0, 0, 1, 1 ).Flush();
  102.        
  103.         br.ShaderData[0] = V4( 0, 0, 0, safe_fdiv( spread, H4 ) );
  104.         ctrl->SetRenderTargets( NULL, SGRX_RT_ClearAll, 0, 0, 1, rttVBLUR2 );
  105.         br.SetTexture( rttHBLUR2 ).SetShader( pppsh_blur ).Quad( 0, 0, 1, 1 ).Flush();
  106.        
  107.         ctrl->SetRenderTargets( NULL, 0, 0, 0, 1 );
  108.         br.SetTexture( 0, rttMAIN )
  109.           .SetTexture( 2, rttVBLUR )
  110.           .SetTexture( 3, rttVBLUR2 )
  111.           .SetTexture( 4, rttDEPTH ).SetShader( pppsh_final ).VPQuad( info.viewport ).Flush();
  112.     }
  113.     else
  114.     {
  115.         ctrl->SetRenderTargets( NULL, 0, 0, 0, 1 );
  116.         br.Reset().SetTexture( rttMAIN ).VPQuad( info.viewport ).Flush();
  117.     }
  118.    
  119.     // debug rendering from camera viewpoint and optional depth clipping
  120.     if( info.debugdraw )
  121.     {
  122.         ctrl->SetRenderTargets( dssMAIN, 0, 0, 0, 1 );
  123.         if( info.viewport )
  124.             GR2D_SetViewport( info.viewport->x0, info.viewport->y0, info.viewport->x1, info.viewport->y1 );
  125.         GR2D_SetViewMatrix( scene->camera.mView * scene->camera.mProj );
  126.         info.debugdraw->DebugDraw();
  127.         if( info.viewport )
  128.             GR2D_UnsetViewport();
  129.         ctrl->SetRenderTargets( NULL, 0, 0, 0, 1 );
  130.     }
  131.    
  132.     GR2D_SetViewMatrix( viewMtx );
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement