// Fill out your copyright notice in the Description page of Project Settings.
#include "MyShaderBPFunctionLibrary.h"
#include "DrawShader.h"
#include "Engine/TextureRenderTarget2D.h"
#include "Engine/World.h"
#include "GlobalShader.h"
#include "CommonRenderResources.h"
#include "ScreenRendering.h"
static void DrawTestShaderToRenderTexture_RenderThread(
FRHICommandListImmediate& RHICmdList,
FTexture* InTextureResource,
FTextureRenderTargetResource* OutGrayRTReource,
FTextureRenderTargetResource* OutScaleRTReource,
ERHIFeatureLevel::Type FeatureLevel,
const FLinearColor& Color,
float ColorScale)
{
check(IsInRenderingThread());
FRHITexture2D* OutGrayRTT = OutGrayRTReource->GetRenderTargetTexture();
FRHITexture2D* OutScaleRTT = OutScaleRTReource->GetRenderTargetTexture();
if (nullptr == OutGrayRTT || nullptr == OutScaleRTT)
return;
if (OutGrayRTT->GetSizeX() != OutScaleRTT->GetSizeX() || OutGrayRTT->GetSizeY() != OutScaleRTT->GetSizeY())
return;
FRHITexture* RTS[] = { OutGrayRTT, OutScaleRTT };
IRendererModule* RendererModule = &FModuleManager::GetModuleChecked<IRendererModule>("Renderer");
RHICmdList.Transition(FRHITransitionInfo(OutGrayRTT, ERHIAccess::SRVMask, ERHIAccess::RTV));
RHICmdList.Transition(FRHITransitionInfo(OutScaleRTT, ERHIAccess::SRVMask, ERHIAccess::RTV));
FRHIRenderPassInfo RPInfo(2, RTS, ERenderTargetActions::DontLoad_Store);
RHICmdList.BeginRenderPass(RPInfo, TEXT("Render Test Shader To Texture"));
{
// Get Shaders
FGlobalShaderMap* GlobalShaderMap = GetGlobalShaderMap(FeatureLevel);
TShaderMapRef<FScreenVS> VertexShader(GlobalShaderMap);
TShaderMapRef<FDrawPS> PixelShader(GlobalShaderMap);
// Setup Pipeline state
FGraphicsPipelineStateInitializer GraphicsPSOInit;
RHICmdList.ApplyCachedRenderTargets(GraphicsPSOInit);
GraphicsPSOInit.DepthStencilState = TStaticDepthStencilState<false, CF_Always>::GetRHI();
GraphicsPSOInit.BlendState = TStaticBlendState<>::GetRHI();
GraphicsPSOInit.RasterizerState = TStaticRasterizerState<>::GetRHI();
GraphicsPSOInit.PrimitiveType = PT_TriangleList;
GraphicsPSOInit.BoundShaderState.VertexDeclarationRHI = GFilterVertexDeclaration.VertexDeclarationRHI;
GraphicsPSOInit.BoundShaderState.VertexShaderRHI = VertexShader.GetVertexShader();
GraphicsPSOInit.BoundShaderState.PixelShaderRHI = PixelShader.GetPixelShader();
SetGraphicsPipelineState(RHICmdList, GraphicsPSOInit);
// Upate viewport
FIntPoint ViewportSize(OutGrayRTT->GetSizeX(), OutGrayRTT->GetSizeY());
RHICmdList.SetViewport(0, 0, 0.0f, ViewportSize.X, ViewportSize.Y, 1.0f);
// update shader uniform parameters
PixelShader->SetParam(RHICmdList, InTextureResource, Color, ColorScale);
RendererModule->DrawRectangle(RHICmdList, 0, 0, ViewportSize.X, ViewportSize.Y, 0, 0, 1, 1, ViewportSize, FIntPoint(1, 1), VertexShader);
}
RHICmdList.EndRenderPass();
RHICmdList.Transition(FRHITransitionInfo(OutGrayRTT, ERHIAccess::RTV, ERHIAccess::SRVMask));
RHICmdList.Transition(FRHITransitionInfo(OutScaleRTT, ERHIAccess::RTV, ERHIAccess::SRVMask));
}
void UMyShaderBPFunctionLibrary::DrawTestShaderToRenderTexture(
const UObject* WorldContextObject,
const UTexture2D* InTexture,
class UTextureRenderTarget2D* OutputRenderTargetA,
class UTextureRenderTarget2D* OutputRenderTargetB,
const FLinearColor& Color,
float ColorScale)
{
UWorld* World = WorldContextObject->GetWorld();
ERHIFeatureLevel::Type FeatureLevel = World->Scene->GetFeatureLevel();
if (FeatureLevel < ERHIFeatureLevel::SM5)
{
UE_LOG(LogTemp, Warning, TEXT("FeatureLevel < ERHIFeatureLevel::SM5"));
return;
}
if (nullptr == InTexture)
{
UE_LOG(LogTemp, Warning, TEXT("InTexture Is NULL"));
return;
}
FTextureRenderTargetResource* TextureRenderTargetSourceA = OutputRenderTargetA->GameThread_GetRenderTargetResource();
FTextureRenderTargetResource* TextureRenderTargetSourceB = OutputRenderTargetB->GameThread_GetRenderTargetResource();
FTexture* TextureSource = InTexture->Resource;
ENQUEUE_RENDER_COMMAND(TestShaderCommand)(
[TextureSource, TextureRenderTargetSourceA, TextureRenderTargetSourceB, FeatureLevel, Color, ColorScale](FRHICommandListImmediate& RHICmdList)
{
DrawTestShaderToRenderTexture_RenderThread(RHICmdList, TextureSource, TextureRenderTargetSourceA, TextureRenderTargetSourceB, FeatureLevel, Color, ColorScale);
}
);
}
评论0