/** Renders the SMAA-Effect */ XRESULT D3D11PfxRenderer::RenderSMAA() { D3D11GraphicsEngine* engine = (D3D11GraphicsEngine*)Engine::GraphicsEngine; FX_SMAA->RenderPostFX(engine->GetHDRBackBuffer()->GetShaderResView()); return XR_SUCCESS; }
/** Blurs the backbuffer and puts the result into TempBufferDS4_2*/ void D3D11PFX_HDR::CreateBloom(RenderToTextureBuffer* lum) { D3D11GraphicsEngine* engine = (D3D11GraphicsEngine *)Engine::GraphicsEngine; INT2 dsRes = INT2(Engine::GraphicsEngine->GetResolution().x / 4, Engine::GraphicsEngine->GetResolution().y / 4); engine->GetShaderManager()->GetVShader("VS_PFX")->Apply(); D3D11PShader* tonemapPS = engine->GetShaderManager()->GetPShader("PS_PFX_Tonemap"); tonemapPS->Apply(); HDRSettingsConstantBuffer hcb; hcb.HDR_LumWhite = Engine::GAPI->GetRendererState()->RendererSettings.HDRLumWhite; hcb.HDR_MiddleGray = Engine::GAPI->GetRendererState()->RendererSettings.HDRMiddleGray; hcb.HDR_Threshold = Engine::GAPI->GetRendererState()->RendererSettings.BloomThreshold; tonemapPS->GetConstantBuffer()[0]->UpdateBuffer(&hcb); tonemapPS->GetConstantBuffer()[0]->BindToPixelShader(0); lum->BindToPixelShader(engine->GetContext(), 1); FxRenderer->CopyTextureToRTV(engine->GetHDRBackBuffer()->GetShaderResView(), FxRenderer->GetTempBufferDS4_1()->GetRenderTargetView(), dsRes, true); D3D11PShader* gaussPS = engine->GetShaderManager()->GetPShader("PS_PFX_GaussBlur"); /** Pass 1: Blur-H */ // Apply PFX-VS D3D11PShader* simplePS = engine->GetShaderManager()->GetPShader("PS_PFX_Simple"); // Apply blur-H shader gaussPS->Apply(); // Update settings BlurConstantBuffer bcb; bcb.B_BlurSize = 1.0f; bcb.B_PixelSize = float2(1.0f / FxRenderer->GetTempBufferDS4_1()->GetSizeX(), 0.0f); gaussPS->GetConstantBuffer()[0]->UpdateBuffer(&bcb); gaussPS->GetConstantBuffer()[0]->BindToPixelShader(0); // Copy FxRenderer->CopyTextureToRTV(FxRenderer->GetTempBufferDS4_1()->GetShaderResView(), FxRenderer->GetTempBufferDS4_2()->GetRenderTargetView(), dsRes, true); /** Pass 2: Blur V */ // Update settings bcb.B_BlurSize = 1.0f; bcb.B_PixelSize = float2(0.0f, 1.0f / FxRenderer->GetTempBufferDS4_1()->GetSizeY()); bcb.B_Threshold = 0.0f; gaussPS->GetConstantBuffer()[0]->UpdateBuffer(&bcb); gaussPS->GetConstantBuffer()[0]->BindToPixelShader(0); // Copy FxRenderer->CopyTextureToRTV(FxRenderer->GetTempBufferDS4_2()->GetShaderResView(), FxRenderer->GetTempBufferDS4_1()->GetRenderTargetView(), dsRes, true); }
/** Draws this effect to the given buffer */ XRESULT D3D11PFX_HDR::Render(RenderToTextureBuffer* fxbuffer) { D3D11GraphicsEngine* engine = (D3D11GraphicsEngine *)Engine::GraphicsEngine; Engine::GAPI->GetRendererState()->BlendState.BlendEnabled = false; Engine::GAPI->GetRendererState()->BlendState.SetDirty(); // Save old rendertargets ID3D11RenderTargetView* oldRTV = NULL; ID3D11DepthStencilView* oldDSV = NULL; engine->GetContext()->OMGetRenderTargets(1, &oldRTV, &oldDSV); RenderToTextureBuffer* lum = CalcLuminance(); CreateBloom(lum); // Copy the original image to our temp-buffer FxRenderer->CopyTextureToRTV(engine->GetHDRBackBuffer()->GetShaderResView(), FxRenderer->GetTempBuffer()->GetRenderTargetView(), engine->GetResolution()); // Bind scene and luminance FxRenderer->GetTempBuffer()->BindToPixelShader(engine->GetContext(), 0); lum->BindToPixelShader(engine->GetContext(), 1); // Bind bloom FxRenderer->GetTempBufferDS4_1()->BindToPixelShader(engine->GetContext(), 2); // Draw the HDR-Shader D3D11PShader* hps = engine->GetShaderManager()->GetPShader("PS_PFX_HDR"); hps->Apply(); HDRSettingsConstantBuffer hcb; hcb.HDR_LumWhite = Engine::GAPI->GetRendererState()->RendererSettings.HDRLumWhite; hcb.HDR_MiddleGray = Engine::GAPI->GetRendererState()->RendererSettings.HDRMiddleGray; hcb.HDR_Threshold = Engine::GAPI->GetRendererState()->RendererSettings.BloomThreshold; hcb.HDR_BloomStrength = Engine::GAPI->GetRendererState()->RendererSettings.BloomStrength; hps->GetConstantBuffer()[0]->UpdateBuffer(&hcb); hps->GetConstantBuffer()[0]->BindToPixelShader(0); FxRenderer->CopyTextureToRTV(FxRenderer->GetTempBuffer()->GetShaderResView(), oldRTV, engine->GetResolution(), true); // Show lumBuffer //FxRenderer->CopyTextureToRTV(currentLum->GetShaderResView(), oldRTV, INT2(LUM_SIZE,LUM_SIZE), false); // Restore rendertargets ID3D11ShaderResourceView* srv = NULL; engine->GetContext()->PSSetShaderResources(1,1,&srv); engine->GetContext()->OMSetRenderTargets(1, &oldRTV, oldDSV); if(oldRTV)oldRTV->Release(); if(oldDSV)oldDSV->Release(); return XR_SUCCESS; }
/** Calcualtes the luminance */ RenderToTextureBuffer* D3D11PFX_HDR::CalcLuminance() { D3D11GraphicsEngine* engine = (D3D11GraphicsEngine *)Engine::GraphicsEngine; RenderToTextureBuffer* lumRTV; RenderToTextureBuffer* lastLum; RenderToTextureBuffer* currentLum; // Figure out which buffers to use where switch(ActiveLumBuffer) { case 0: lumRTV = LumBuffer1; lastLum = LumBuffer2; currentLum = LumBuffer3; break; case 1: lumRTV = LumBuffer3; lastLum = LumBuffer1; currentLum = LumBuffer2; break; case 2: lumRTV = LumBuffer2; lastLum = LumBuffer3; currentLum = LumBuffer1; break; } D3D11PShader* lps = engine->GetShaderManager()->GetPShader("PS_PFX_LumConvert"); lps->Apply(); // Convert the backbuffer to our luminance buffer FxRenderer->CopyTextureToRTV(engine->GetHDRBackBuffer()->GetShaderResView(), currentLum->GetRenderTargetView(), INT2(LUM_SIZE,LUM_SIZE), true); // Create the average luminance engine->GetContext()->GenerateMips(currentLum->GetShaderResView()); D3D11PShader* aps = engine->GetShaderManager()->GetPShader("PS_PFX_LumAdapt"); aps->Apply(); LumAdaptConstantBuffer lcb; lcb.LC_DeltaTime = Engine::GAPI->GetDeltaTime(); aps->GetConstantBuffer()[0]->UpdateBuffer(&lcb); aps->GetConstantBuffer()[0]->BindToPixelShader(0); // Bind luminances lastLum->BindToPixelShader(engine->GetContext(), 1); currentLum->BindToPixelShader(engine->GetContext(), 2); // Convert the backbuffer to our luminance buffer FxRenderer->CopyTextureToRTV(NULL, lumRTV->GetRenderTargetView(), INT2(LUM_SIZE,LUM_SIZE), true); // Create the average luminance engine->GetContext()->GenerateMips(lumRTV->GetShaderResView()); // Increment ActiveLumBuffer++; ActiveLumBuffer = (ActiveLumBuffer == 3 ? 0 : ActiveLumBuffer); return lumRTV; }
/** Draws this effect to the given buffer */ XRESULT D3D11PFX_GodRays::Render(RenderToTextureBuffer* fxbuffer) { D3D11GraphicsEngine* engine = (D3D11GraphicsEngine *)Engine::GraphicsEngine; D3DXVECTOR3 sunPosition = *Engine::GAPI->GetSky()->GetAtmosphereCB().AC_LightPos.toD3DXVECTOR3(); sunPosition *= Engine::GAPI->GetSky()->GetAtmosphereCB().AC_OuterRadius; sunPosition += Engine::GAPI->GetCameraPosition(); // Maybe use cameraposition from sky? D3DXMATRIX& view = Engine::GAPI->GetRendererState()->TransformState.TransformView; D3DXMATRIX& proj = Engine::GAPI->GetProjectionMatrix(); D3DXMATRIX viewProj = proj * view; D3DXMatrixTranspose(&viewProj, &viewProj); D3DXMatrixTranspose(&view, &view); D3DXVECTOR3 sunViewPosition; D3DXVec3TransformCoord(&sunViewPosition, &sunPosition, &view); // This is for checking if the light is behind the camera D3DXVec3TransformCoord(&sunPosition, &sunPosition, &viewProj); if(sunViewPosition.z < 0.0f) return XR_SUCCESS; // Don't render the godrays when the sun is behind the camera GodRayZoomConstantBuffer gcb; gcb.GR_Weight = 1.0f; gcb.GR_Decay = Engine::GAPI->GetRendererState()->RendererSettings.GodRayDecay; gcb.GR_Weight = Engine::GAPI->GetRendererState()->RendererSettings.GodRayWeight; gcb.GR_Density = Engine::GAPI->GetRendererState()->RendererSettings.GodRayDensity; gcb.GR_Center.x = sunPosition.x/2.0f +0.5f; gcb.GR_Center.y = sunPosition.y/-2.0f +0.5f; gcb.GR_ColorMod = Engine::GAPI->GetRendererState()->RendererSettings.GodRayColorMod; if(abs(gcb.GR_Center.x - 0.5f) > 0.5f) gcb.GR_Weight *= std::max(0.0f, 1.0f - (abs(gcb.GR_Center.x - 0.5f) - 0.5f) / 0.5f); if(abs(gcb.GR_Center.y - 0.5f) > 0.5f) gcb.GR_Weight *= std::max(0.0f, 1.0f - (abs(gcb.GR_Center.y - 0.5f) - 0.5f) / 0.5f); ID3D11RenderTargetView* oldRTV=NULL; ID3D11DepthStencilView* oldDSV=NULL; engine->GetContext()->OMGetRenderTargets(1, &oldRTV, &oldDSV); D3D11VShader* vs = engine->GetShaderManager()->GetVShader("VS_PFX"); D3D11PShader* maskPS = engine->GetShaderManager()->GetPShader("PS_PFX_GodRayMask"); D3D11PShader* zoomPS = engine->GetShaderManager()->GetPShader("PS_PFX_GodRayZoom"); maskPS->Apply(); vs->Apply(); // Draw downscaled mask engine->GetContext()->OMSetRenderTargets(1, FxRenderer->GetTempBufferDS4_1()->GetRenderTargetViewPtr(), NULL); engine->GetHDRBackBuffer()->BindToPixelShader(engine->GetContext(), 0); engine->GetGBuffer1()->BindToPixelShader(engine->GetContext(), 1); D3D11_VIEWPORT vp; vp.TopLeftX = 0.0f; vp.TopLeftY = 0.0f; vp.MinDepth = 0.0f; vp.MaxDepth = 1.0f; vp.Width = (float)FxRenderer->GetTempBufferDS4_1()->GetSizeX(); vp.Height = (float)FxRenderer->GetTempBufferDS4_1()->GetSizeY(); engine->GetContext()->RSSetViewports(1, &vp); FxRenderer->DrawFullScreenQuad(); // Zoom zoomPS->Apply(); zoomPS->GetConstantBuffer()[0]->UpdateBuffer(&gcb); zoomPS->GetConstantBuffer()[0]->BindToPixelShader(0); FxRenderer->CopyTextureToRTV(FxRenderer->GetTempBufferDS4_1()->GetShaderResView(), FxRenderer->GetTempBufferDS4_2()->GetRenderTargetView(), INT2(0,0), true); // Upscale and blend Engine::GAPI->GetRendererState()->BlendState.SetAdditiveBlending(); Engine::GAPI->GetRendererState()->BlendState.SetDirty(); FxRenderer->CopyTextureToRTV(FxRenderer->GetTempBufferDS4_2()->GetShaderResView(), oldRTV, INT2(engine->GetResolution().x, engine->GetResolution().y)); vp.Width = (float)engine->GetResolution().x; vp.Height = (float)engine->GetResolution().y; engine->GetContext()->RSSetViewports(1, &vp); engine->GetContext()->OMSetRenderTargets(1, &oldRTV, oldDSV); if(oldRTV)oldRTV->Release(); if(oldDSV)oldDSV->Release(); return XR_SUCCESS; }