/** * Create bound shader state using the vertex decl from the mesh draw policy * as well as the shaders needed to draw the mesh * @return new bound shader state object */ FBoundShaderStateInput FPositionOnlyDepthDrawingPolicy::GetBoundShaderStateInput(ERHIFeatureLevel::Type InFeatureLevel) { FVertexDeclarationRHIParamRef VertexDeclaration; VertexDeclaration = VertexFactory->GetPositionDeclaration(); checkSlow(MaterialRenderProxy->GetMaterial(InFeatureLevel)->GetBlendMode() == BLEND_Opaque); return FBoundShaderStateInput(VertexDeclaration, VertexShader->GetVertexShader(), FHullShaderRHIRef(), FDomainShaderRHIRef(), FPixelShaderRHIRef(), FGeometryShaderRHIRef()); }
void FDecalRendering::SetVertexShaderOnly(FRHICommandList& RHICmdList, const FViewInfo& View, const FMatrix& FrustumComponentToClip) { TShaderMapRef<FDeferredDecalVS> VertexShader(View.ShaderMap); RHICmdList.SetLocalBoundShaderState(RHICmdList.BuildLocalBoundShaderState(GetVertexDeclarationFVector4(), VertexShader->GetVertexShader(), FHullShaderRHIRef(), FDomainShaderRHIRef(), NULL, FGeometryShaderRHIRef())); VertexShader->SetParameters(RHICmdList, View, FrustumComponentToClip); }
void FDecalRendering::SetShader(FRHICommandList& RHICmdList, const FViewInfo& View, const FTransientDecalRenderData& DecalData, const FMatrix& FrustumComponentToClip) { const FMaterialShaderMap* MaterialShaderMap = DecalData.MaterialResource->GetRenderingThreadShaderMap(); auto PixelShader = MaterialShaderMap->GetShader<FDeferredDecalPS>(); TShaderMapRef<FDeferredDecalVS> VertexShader(View.ShaderMap); const EDebugViewShaderMode DebugViewShaderMode = View.Family->GetDebugViewShaderMode(); if (DebugViewShaderMode != DVSM_None) { // For this to work, decal VS must output compatible interpolants. Currently this requires to use FDebugPSInLean. // Here we pass nullptr for the material interface because the use of a static bound shader state is only compatible with unique shaders. IDebugViewModePSInterface* DebugPixelShader = FDebugViewMode::GetPSInterface(View.ShaderMap, nullptr, DebugViewShaderMode); const uint32 NumPixelShaderInstructions = PixelShader->GetNumInstructions(); const uint32 NumVertexShaderInstructions = VertexShader->GetNumInstructions(); static FGlobalBoundShaderState BoundShaderState[DVSM_MAX]; SetGlobalBoundShaderState(RHICmdList, View.GetFeatureLevel(), BoundShaderState[(uint32)DebugViewShaderMode], GetVertexDeclarationFVector4(), *VertexShader, DebugPixelShader->GetShader()); DebugPixelShader->SetParameters(RHICmdList, *VertexShader, PixelShader, DecalData.MaterialProxy, *DecalData.MaterialResource, View); DebugPixelShader->SetMesh(RHICmdList, View); } else { // first Bind, then SetParameters() RHICmdList.SetLocalBoundShaderState(RHICmdList.BuildLocalBoundShaderState(GetVertexDeclarationFVector4(), VertexShader->GetVertexShader(), FHullShaderRHIRef(), FDomainShaderRHIRef(), PixelShader->GetPixelShader(), FGeometryShaderRHIRef())); PixelShader->SetParameters(RHICmdList, View, DecalData.MaterialProxy, *DecalData.DecalProxy, DecalData.FadeAlpha); } // SetUniformBufferParameter() need to happen after the shader has been set otherwise a DebugBreak could occur. // we don't have the Primitive uniform buffer setup for decals (later we want to batch) { auto& PrimitiveVS = VertexShader->GetUniformBufferParameter<FPrimitiveUniformShaderParameters>(); auto& PrimitivePS = PixelShader->GetUniformBufferParameter<FPrimitiveUniformShaderParameters>(); // uncomment to track down usage of the Primitive uniform buffer // check(!PrimitiveVS.IsBound()); // check(!PrimitivePS.IsBound()); // to prevent potential shader error (UE-18852 ElementalDemo crashes due to nil constant buffer) SetUniformBufferParameter(RHICmdList, VertexShader->GetVertexShader(), PrimitiveVS, GIdentityPrimitiveUniformBuffer); if (DebugViewShaderMode == DVSM_None) { SetUniformBufferParameter(RHICmdList, PixelShader->GetPixelShader(), PrimitivePS, GIdentityPrimitiveUniformBuffer); } } VertexShader->SetParameters(RHICmdList, View, FrustumComponentToClip); }
void FDecalRendering::SetShader(FRHICommandList& RHICmdList, const FViewInfo& View, bool bShaderComplexity, const FTransientDecalRenderData& DecalData, const FMatrix& FrustumComponentToClip) { const FMaterialShaderMap* MaterialShaderMap = DecalData.MaterialResource->GetRenderingThreadShaderMap(); auto PixelShader = MaterialShaderMap->GetShader<FDeferredDecalPS>(); TShaderMapRef<FDeferredDecalVS> VertexShader(View.ShaderMap); if(bShaderComplexity) { TShaderMapRef<FShaderComplexityAccumulatePS> VisualizePixelShader(View.ShaderMap); const uint32 NumPixelShaderInstructions = PixelShader->GetNumInstructions(); const uint32 NumVertexShaderInstructions = VertexShader->GetNumInstructions(); static FGlobalBoundShaderState BoundShaderState; SetGlobalBoundShaderState(RHICmdList, View.GetFeatureLevel(), BoundShaderState, GetVertexDeclarationFVector4(), *VertexShader, *VisualizePixelShader); VisualizePixelShader->SetParameters(RHICmdList, NumVertexShaderInstructions, NumPixelShaderInstructions, View.GetFeatureLevel()); } else { // first Bind, then SetParameters() RHICmdList.SetLocalBoundShaderState(RHICmdList.BuildLocalBoundShaderState(GetVertexDeclarationFVector4(), VertexShader->GetVertexShader(), FHullShaderRHIRef(), FDomainShaderRHIRef(), PixelShader->GetPixelShader(), FGeometryShaderRHIRef())); PixelShader->SetParameters(RHICmdList, View, DecalData.MaterialProxy, *DecalData.DecalProxy, DecalData.FadeAlpha); } VertexShader->SetParameters(RHICmdList, View, FrustumComponentToClip); }
void FDecalRendering::SetShader(FRHICommandList& RHICmdList, const FViewInfo& View, bool bShaderComplexity, const FTransientDecalRenderData& DecalData, const FMatrix& FrustumComponentToClip) { const FMaterialShaderMap* MaterialShaderMap = DecalData.MaterialResource->GetRenderingThreadShaderMap(); auto PixelShader = MaterialShaderMap->GetShader<FDeferredDecalPS>(); TShaderMapRef<FDeferredDecalVS> VertexShader(View.ShaderMap); // we don't have the Primitive uniform buffer setup for decals (later we want to batch) { auto& PrimitiveVS = VertexShader->GetUniformBufferParameter<FPrimitiveUniformShaderParameters>(); auto& PrimitivePS = PixelShader->GetUniformBufferParameter<FPrimitiveUniformShaderParameters>(); // uncomment to track down usage of the Primitive uniform buffer // check(!PrimitiveVS.IsBound()); // check(!PrimitivePS.IsBound()); // to prevent potential shader error (UE-18852 ElementalDemo crashes due to nil constant buffer) SetUniformBufferParameter(RHICmdList, VertexShader->GetVertexShader(), PrimitiveVS, GIdentityPrimitiveUniformBuffer); SetUniformBufferParameter(RHICmdList, PixelShader->GetPixelShader(), PrimitivePS, GIdentityPrimitiveUniformBuffer); } if(bShaderComplexity) { TShaderMapRef<FShaderComplexityAccumulatePS> VisualizePixelShader(View.ShaderMap); const uint32 NumPixelShaderInstructions = PixelShader->GetNumInstructions(); const uint32 NumVertexShaderInstructions = VertexShader->GetNumInstructions(); static FGlobalBoundShaderState BoundShaderState; SetGlobalBoundShaderState(RHICmdList, View.GetFeatureLevel(), BoundShaderState, GetVertexDeclarationFVector4(), *VertexShader, *VisualizePixelShader); VisualizePixelShader->SetParameters(RHICmdList, NumVertexShaderInstructions, NumPixelShaderInstructions, View.GetFeatureLevel()); } else { // first Bind, then SetParameters() RHICmdList.SetLocalBoundShaderState(RHICmdList.BuildLocalBoundShaderState(GetVertexDeclarationFVector4(), VertexShader->GetVertexShader(), FHullShaderRHIRef(), FDomainShaderRHIRef(), PixelShader->GetPixelShader(), FGeometryShaderRHIRef())); PixelShader->SetParameters(RHICmdList, View, DecalData.MaterialProxy, *DecalData.DecalProxy, DecalData.FadeAlpha); } VertexShader->SetParameters(RHICmdList, View, FrustumComponentToClip); }
void FD3D11DynamicRHI::IssueLongGPUTask() { if (GMaxRHIFeatureLevel >= ERHIFeatureLevel::SM4) { int32 LargestViewportIndex = INDEX_NONE; int32 LargestViewportPixels = 0; for (int32 ViewportIndex = 0; ViewportIndex < Viewports.Num(); ViewportIndex++) { FD3D11Viewport* Viewport = Viewports[ViewportIndex]; if (Viewport->GetSizeXY().X * Viewport->GetSizeXY().Y > LargestViewportPixels) { LargestViewportPixels = Viewport->GetSizeXY().X * Viewport->GetSizeXY().Y; LargestViewportIndex = ViewportIndex; } } if (LargestViewportIndex >= 0) { FD3D11Viewport* Viewport = Viewports[LargestViewportIndex]; FRHICommandList_RecursiveHazardous RHICmdList(this); SetRenderTarget(RHICmdList, Viewport->GetBackBuffer(), FTextureRHIRef()); RHICmdList.SetBlendState(TStaticBlendState<CW_RGBA, BO_Add, BF_One, BF_One>::GetRHI(), FLinearColor::Black); RHICmdList.SetDepthStencilState(TStaticDepthStencilState<false, CF_Always>::GetRHI(), 0); RHICmdList.SetRasterizerState(TStaticRasterizerState<FM_Solid, CM_None>::GetRHI()); auto ShaderMap = GetGlobalShaderMap(GMaxRHIFeatureLevel); TShaderMapRef<TOneColorVS<true> > VertexShader(ShaderMap); TShaderMapRef<FLongGPUTaskPS> PixelShader(ShaderMap); RHICmdList.SetLocalBoundShaderState(RHICmdList.BuildLocalBoundShaderState(GD3D11Vector4VertexDeclaration.VertexDeclarationRHI, VertexShader->GetVertexShader(), FHullShaderRHIRef(), FDomainShaderRHIRef(), PixelShader->GetPixelShader(), FGeometryShaderRHIRef())); // Draw a fullscreen quad FVector4 Vertices[4]; Vertices[0].Set( -1.0f, 1.0f, 0, 1.0f ); Vertices[1].Set( 1.0f, 1.0f, 0, 1.0f ); Vertices[2].Set( -1.0f, -1.0f, 0, 1.0f ); Vertices[3].Set( 1.0f, -1.0f, 0, 1.0f ); DrawPrimitiveUP(RHICmdList, PT_TriangleStrip, 2, Vertices, sizeof(Vertices[0])); // Implicit flush. Always call flush when using a command list in RHI implementations before doing anything else. This is super hazardous. } } }
void FRCPassPostProcessMaterial::Process(FRenderingCompositePassContext& Context) { FMaterialRenderProxy* Proxy = MaterialInterface->GetRenderProxy(false); check(Proxy); const FMaterial* Material = Proxy->GetMaterial(Context.View.GetFeatureLevel()); check(Material); SCOPED_DRAW_EVENTF(Context.RHICmdList, PostProcessMaterial, TEXT("PostProcessMaterial Material=%s"), *Material->GetFriendlyName()); const FPooledRenderTargetDesc* InputDesc = GetInputDesc(ePId_Input0); if(!InputDesc) { // input is not hooked up correctly return; } const FSceneView& View = Context.View; const FSceneViewFamily& ViewFamily = *(View.Family); // hacky cast FRenderingCompositePassContext RenderingCompositePassContext(Context.RHICmdList, (FViewInfo&)View); RenderingCompositePassContext.Pass = this; FIntRect SrcRect = View.ViewRect; FIntRect DestRect = View.ViewRect; FIntPoint SrcSize = InputDesc->Extent; const FSceneRenderTargetItem& DestRenderTarget = PassOutputs[0].RequestSurface(Context); // Set the view family's render target/viewport. SetRenderTarget(Context.RHICmdList, DestRenderTarget.TargetableTexture, FTextureRHIParamRef()); if( ViewFamily.RenderTarget->GetRenderTargetTexture() != DestRenderTarget.TargetableTexture ) { Context.RHICmdList.Clear(true, FLinearColor::Black, false, 1.0f, false, 0, View.ViewRect); } Context.SetViewportAndCallRHI(View.ViewRect); // set the state Context.RHICmdList.SetBlendState(TStaticBlendState<>::GetRHI()); Context.RHICmdList.SetRasterizerState(TStaticRasterizerState<>::GetRHI()); Context.RHICmdList.SetDepthStencilState(TStaticDepthStencilState<false, CF_Always>::GetRHI()); float ScaleX = 1.0f / InputDesc->Extent.X; float ScaleY = 1.0f / InputDesc->Extent.Y; const FMaterialShaderMap* MaterialShaderMap = Material->GetRenderingThreadShaderMap(); FPostProcessMaterialPS* PixelShader = MaterialShaderMap->GetShader<FPostProcessMaterialPS>(); FPostProcessMaterialVS* VertexShader = MaterialShaderMap->GetShader<FPostProcessMaterialVS>(); Context.RHICmdList.SetLocalBoundShaderState(Context.RHICmdList.BuildLocalBoundShaderState(GetVertexDeclarationFVector4(), VertexShader->GetVertexShader(), FHullShaderRHIRef(), FDomainShaderRHIRef(), PixelShader->GetPixelShader(), FGeometryShaderRHIRef())); VertexShader->SetParameters(Context.RHICmdList, Context); PixelShader->SetParameters(Context.RHICmdList, Context, MaterialInterface->GetRenderProxy(false)); DrawRectangle( Context.RHICmdList, 0, 0, DestRect.Width(), DestRect.Height(), SrcRect.Min.X, SrcRect.Min.Y, SrcRect.Width(), SrcRect.Height(), DestRect.Size(), SrcSize, VertexShader, EDRF_UseTriangleOptimization); Context.RHICmdList.CopyToResolveTarget(DestRenderTarget.TargetableTexture, DestRenderTarget.ShaderResourceTexture, false, FResolveParams()); if(Material->NeedsGBuffer()) { GSceneRenderTargets.AdjustGBufferRefCount(-1); } }
bool RenderPreStencil(FRenderingCompositePassContext& Context, const FMaterialShaderMap* MaterialShaderMap, const FMatrix& ComponentToWorldMatrix, const FMatrix& FrustumComponentToClip) { const FSceneView& View = Context.View; float Distance = (View.ViewMatrices.ViewOrigin - ComponentToWorldMatrix.GetOrigin()).Size(); float Radius = ComponentToWorldMatrix.GetMaximumAxisScale(); // if not inside if(Distance > Radius) { float EstimatedDecalSize = Radius / Distance; float StencilSizeThreshold = CVarStencilSizeThreshold.GetValueOnRenderThread(); // Check if it's large enough on screen if(EstimatedDecalSize < StencilSizeThreshold) { return false; } } TShaderMapRef<FDeferredDecalVS> VertexShader(Context.GetShaderMap()); Context.RHICmdList.SetLocalBoundShaderState(Context.RHICmdList.BuildLocalBoundShaderState(GetVertexDeclarationFVector3(), VertexShader->GetVertexShader(), FHullShaderRHIRef(), FDomainShaderRHIRef(), NULL, FGeometryShaderRHIRef())); VertexShader->SetParameters(Context.RHICmdList, View, FrustumComponentToClip); // Set states, the state cache helps us avoiding redundant sets Context.RHICmdList.SetRasterizerState(TStaticRasterizerState<FM_Solid, CM_None>::GetRHI()); // all the same to have DX10 working Context.RHICmdList.SetBlendState(TStaticBlendState< CW_NONE, BO_Add, BF_SourceAlpha, BF_InverseSourceAlpha, BO_Add, BF_Zero, BF_One, // Emissive CW_NONE, BO_Add, BF_SourceAlpha, BF_InverseSourceAlpha, BO_Add, BF_Zero, BF_One, // Normal CW_NONE, BO_Add, BF_SourceAlpha, BF_InverseSourceAlpha, BO_Add, BF_Zero, BF_One, // Metallic, Specular, Roughness CW_NONE, BO_Add, BF_SourceAlpha, BF_InverseSourceAlpha, BO_Add, BF_Zero, BF_One // BaseColor >::GetRHI() ); // Carmack's reverse on the bounds //@todo-martinm Context.RHICmdList.SetDepthStencilState(TStaticDepthStencilState< false,CF_LessEqual, true,CF_Equal,SO_Keep,SO_Keep,SO_Increment, true,CF_Equal,SO_Keep,SO_Keep,SO_Decrement, 0x80,0x7f >::GetRHI()); // Render decal mask Context.RHICmdList.DrawIndexedPrimitive(GUnitCubeIndexBuffer.IndexBufferRHI, PT_TriangleList, 0, 0, 8, 0, GUnitCubeIndexBuffer.GetIndexCount() / 3, 1); return true; }
void SetShader(const FRenderingCompositePassContext& Context, bool bShaderComplexity, const FTransientDecalRenderData& DecalData, FShader* VertexShader) { const FSceneView& View = Context.View; const FMaterialShaderMap* MaterialShaderMap = DecalData.MaterialResource->GetRenderingThreadShaderMap(); auto PixelShader = MaterialShaderMap->GetShader<FDeferredDecalPS>(); if(bShaderComplexity) { const FViewInfo &ViewInfo = (const FViewInfo &)View; TShaderMapRef<FShaderComplexityAccumulatePS> VisualizePixelShader(ViewInfo.ShaderMap); const uint32 NumPixelShaderInstructions = PixelShader->GetNumInstructions(); const uint32 NumVertexShaderInstructions = VertexShader->GetNumInstructions(); static FGlobalBoundShaderState BoundShaderState; SetGlobalBoundShaderState(Context.RHICmdList, Context.GetFeatureLevel(), BoundShaderState, GetVertexDeclarationFVector3(), VertexShader, *VisualizePixelShader); VisualizePixelShader->SetParameters(Context.RHICmdList, NumVertexShaderInstructions, NumPixelShaderInstructions, View.GetFeatureLevel()); } else { // first Bind, then SetParameters() Context.RHICmdList.SetLocalBoundShaderState(Context.RHICmdList.BuildLocalBoundShaderState(GetVertexDeclarationFVector3(), VertexShader->GetVertexShader(), FHullShaderRHIRef(), FDomainShaderRHIRef(), PixelShader->GetPixelShader(), FGeometryShaderRHIRef())); PixelShader->SetParameters(Context.RHICmdList, View, DecalData.MaterialProxy, *DecalData.DecalProxy); } }