Dbuffer
?
?
DBufferA : PreMultColor(rgb), ColorOpacity(a)
?
?
DBufferB : PreMulWorldNormal(rgb), NormalOpacity(a)
?
?
- DBufferC : PreMulRoughness(r ), RoughnessOpacity(g)
?
?
(一)、DeferredShadingRender中对Buffer的处理
从调式和RenderDoc可以详细看到过程,这里就不做详细说明
?
?
结果:
?
?
(二),代码抓取
结局:
https://twitter.com/i/status/1096091562642001920
这里主要用两种方式去获取,一种是通过Fmemory,一种直接用Unreal提供CopyShareMip
具体如下:
?
?
以下为全部代码:
?
?
DynamicAccessGBufferRenderTarget.h:
//////////////////这里就没有按照Buff处理做分装了,直接怼的(嘿嘿)
#pragma once
#include <memory>
#include "Engine/Texture2D.h"
#include "Object.h"
#include "DynamicAccessGBufferRenderTarget.generated.h"
?
?
UENUM(BlueprintType)????????????????//"BlueprintType" is essential to include
enum class EDZShowGbuffer : uint8
{
VE_GDZSHOWBUFFERA ????????UMETA(DisplayName = "ShowGBuffA"),
VE_GDZSHOWBUFFERB ????????UMETA(DisplayName = "ShowGBuffB"),
VE_GDZSHOWBUFFERC????????UMETA(DisplayName = "ShowGBuffC"),
VE_GDZSHOWBUFFERD????????UMETA(DisplayName = "ShowGBuffD"),
VE_GDZSHOWBUFFERE????????UMETA(DisplayName = "ShowGBuffE"),
};
?
?
UENUM(BlueprintType)????????????????//"BlueprintType" is essential to include
enum class EDZCOPYGbufferType : uint8
{
VE_GDZSHOWGBUFFER_COPYMIP ????????UMETA(DisplayName = "COPY_Mip"),
VE_GDZSHOWBUFFERR_COPYMEMO ????????UMETA(DisplayName = "COPY_Memory"),
VE_GDZSHOWBUFFERC_CPUARRAY????????UMETA(DisplayName = "COPY_MemoryForWhile"),
};
?
?
USTRUCT()
struct FCommitRenderData
{
GENERATED_BODY()
public:
EDZCOPYGbufferType ECOPYBuffType;
EDZShowGbuffer EShowBuff;
UTextureRenderTarget2D* mTextureTarget;
EPixelFormat format;
int32 canvasWidth = 512;
int32 canvasHeight = 512;
bool isGetGBufferData = false;
};
?
?
UCLASS(Blueprintable, BlueprintType)
class DZRENDERSTUDIOPLUGIN_API UDynamicAccessGBufferRenderTarget : public UObject
{
GENERATED_BODY()
?
?
public:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = DynamicAccessGBufferRenderTarget)
EDZCOPYGbufferType ECOPYBuffType;
?
?
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = DynamicAccessGBufferRenderTarget)
EDZShowGbuffer EShowBuff;
?
?
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = DynamicAccessGBufferRenderTarget)
UTextureRenderTarget2D* mTextureTarget;
?
?
UFUNCTION(BlueprintCallable, Category = DynamicAccessGBufferRenderTarget)
void InitializeTexture(UTextureRenderTarget2D* pTextureTarget,EDZShowGbuffer pEShowBuff, EDZCOPYGbufferType pECOPYBuffType);
?
?
UFUNCTION(BlueprintCallable, Category = DynamicAccessGBufferRenderTarget)
void CaputerGBuffer();
?
?
UFUNCTION(BlueprintCallable, Category = DynamicAccessGBufferRenderTarget)
void ClearTexture();
?
?
UDynamicAccessGBufferRenderTarget();
~UDynamicAccessGBufferRenderTarget();
?
?
static void CaputerGBufer_Rebuild_RenderThread(FCommitRenderData* pRenderData);
static void CaputerGBuffer_RenderThread(FCommitRenderData* pRenderData);
?
?
static void Copy_TextureRHI(FRHICommandListImmediate& RHICmdList, FRHITexture2D* pDesc, FRHITexture2D* pSrc);
static void Copy_MemoryRHI(FRHICommandListImmediate& RHICmdList, FRHITexture2D* pDesc, FRHITexture2D* pSrc);
static void Copy_CPUArrayRHI();
?
?
private:
EPixelFormat format;
UPROPERTY()
FCommitRenderData CatchRenderData;
?
?
UPROPERTY()
bool isRebulidRenderTargetSuccess = false;
};
?
?
?
?
?
?
DynamicAccessGBufferRenderTarget.cpp
/**
* 1、这里步骤基本为先得到当前Buffer的大小,刷新RT目标的数据。并在基础属性变化时* 候再次刷新
* 2、抓取数据(由于BUFF在Render管线处理时候有获取和释放的处理也是为了能及时释* 放,
* 所以手动加1,当然成对的写法在DepthRender, Slate等都可以看到,这非常好。
* 3、CopyShare或者利用FMemory::Memcpy(但拷贝前需要加锁,完成后还需要解锁,所* * 以你懂的
**/
?
?
#include "DynamicAccessGBufferRenderTarget.h"
#include "private/PostProcess/SceneRenderTargets.h"
#include "Engine.h"
#include "RHICommandList.h"
?
?
?
?
?
?
?
?
UDynamicAccessGBufferRenderTarget::UDynamicAccessGBufferRenderTarget()
{
?
?
}
?
?
UDynamicAccessGBufferRenderTarget::~UDynamicAccessGBufferRenderTarget()
{
?
?
}
?
?
void UDynamicAccessGBufferRenderTarget::InitializeTexture(UTextureRenderTarget2D* pTextureTarget, EDZShowGbuffer pEShowBuff, EDZCOPYGbufferType pECOPYBuffType)
{
if (mTextureTarget != pTextureTarget || EShowBuff != pEShowBuff || ECOPYBuffType != pECOPYBuffType)
{
mTextureTarget = pTextureTarget;
EShowBuff = pEShowBuff;
ECOPYBuffType = pECOPYBuffType;
isRebulidRenderTargetSuccess = false;
}
?
?
}
?
?
void UDynamicAccessGBufferRenderTarget::ClearTexture()
{
?
?
}
?
?
?
?
void UDynamicAccessGBufferRenderTarget::CaputerGBuffer()
{
if (!CatchRenderData.isGetGBufferData)
{
isRebulidRenderTargetSuccess = false;
CatchRenderData.EShowBuff = EShowBuff;
CatchRenderData.ECOPYBuffType = ECOPYBuffType;
CatchRenderData.mTextureTarget = mTextureTarget;
ENQUEUE_UNIQUE_RENDER_COMMAND_ONEPARAMETER(
DZRenderSutioBP_AccessGBufferRenderTarget_init,
FCommitRenderData*, RenderData, &CatchRenderData,
{
UDynamicAccessGBufferRenderTarget::CaputerGBufer_Rebuild_RenderThread(RenderData);
}
);
}
else
{
if (!isRebulidRenderTargetSuccess)
{
if (IsInGameThread())
{
mTextureTarget->InitCustomFormat(CatchRenderData.canvasWidth, CatchRenderData.canvasHeight, CatchRenderData.format, false);
mTextureTarget->ForceRebuildPlatformData();
}
isRebulidRenderTargetSuccess = true;
}
else
{
CatchRenderData.EShowBuff = EShowBuff;
CatchRenderData.ECOPYBuffType = ECOPYBuffType;
CatchRenderData.mTextureTarget = mTextureTarget;
ENQUEUE_UNIQUE_RENDER_COMMAND_ONEPARAMETER(
DZRenderSutioBP_InterceptSceneGBufferToRenderTarget,
FCommitRenderData*, RenderData, &CatchRenderData,
{
UDynamicAccessGBufferRenderTarget::CaputerGBuffer_RenderThread(RenderData);
}
);
}
}
?
?
}
?
?
void UDynamicAccessGBufferRenderTarget::CaputerGBufer_Rebuild_RenderThread(FCommitRenderData* pRenderData)
{
FRHICommandListImmediate& RHICmdList = GRHICommandList.GetImmediateCommandList();
//计数加一避免Render完成后直接清空了GBuffer,但会慢一帧,你猜
FSceneRenderTargets::Get(RHICmdList).AdjustGBufferRefCount(RHICmdList, 1);
FSceneRenderTargets& SceneContext = FSceneRenderTargets::Get(RHICmdList);
if (SceneContext.GBufferA)
{
FTexture2DRHIRef vTextTarget;
switch (pRenderData->EShowBuff)
{
case EDZShowGbuffer::VE_GDZSHOWBUFFERA:
vTextTarget = SceneContext.GetGBufferATexture();
break;
case EDZShowGbuffer::VE_GDZSHOWBUFFERB:
vTextTarget = SceneContext.GetGBufferBTexture();
break;
case EDZShowGbuffer::VE_GDZSHOWBUFFERC:
vTextTarget = SceneContext.GetGBufferCTexture();
break;
case EDZShowGbuffer::VE_GDZSHOWBUFFERD:
vTextTarget = SceneContext.GetGBufferDTexture();
break;
case EDZShowGbuffer::VE_GDZSHOWBUFFERE:
vTextTarget = SceneContext.GetGBufferETexture();
break;
default:
vTextTarget = SceneContext.GetGBufferATexture();
break;
}
?
pRenderData->format = vTextTarget->GetFormat();
pRenderData->canvasWidth = vTextTarget->GetSizeX();
pRenderData->canvasHeight = vTextTarget->GetSizeY();
pRenderData->isGetGBufferData = true;
}
FSceneRenderTargets::Get(RHICmdList).AdjustGBufferRefCount(RHICmdList, -1);
}
?
?
void UDynamicAccessGBufferRenderTarget::CaputerGBuffer_RenderThread(FCommitRenderData* pRenderData)
{
FRHICommandListImmediate& RHICmdList = GRHICommandList.GetImmediateCommandList();
//计数加一避免Render完成后直接清空了GBuffer,但会慢一帧,你猜
FSceneRenderTargets::Get(RHICmdList).AdjustGBufferRefCount(RHICmdList, 1);
FSceneRenderTargets& SceneContext = FSceneRenderTargets::Get(RHICmdList);
if (SceneContext.GBufferA && pRenderData->mTextureTarget != nullptr)
{
FTexture2DRHIRef vTextTarget;
switch (pRenderData->EShowBuff)
{
case EDZShowGbuffer::VE_GDZSHOWBUFFERA:
vTextTarget = SceneContext.GetGBufferATexture();
break;
case EDZShowGbuffer::VE_GDZSHOWBUFFERB:
vTextTarget = SceneContext.GetGBufferBTexture();
break;
case EDZShowGbuffer::VE_GDZSHOWBUFFERC:
vTextTarget = SceneContext.GetGBufferCTexture();
break;
case EDZShowGbuffer::VE_GDZSHOWBUFFERD:
vTextTarget = SceneContext.GetGBufferDTexture();
break;
case EDZShowGbuffer::VE_GDZSHOWBUFFERE:
vTextTarget = SceneContext.GetGBufferETexture();
break;
break;
default:
vTextTarget = SceneContext.GetGBufferATexture();
break;
}
pRenderData->format = vTextTarget->GetFormat();
if (pRenderData->canvasWidth != vTextTarget->GetSizeX())
{
pRenderData->isGetGBufferData = false;
return;
}
FTextureReferenceRHIRef vTexRHIRef = pRenderData->mTextureTarget->TextureReference.TextureReferenceRHI;
FRHITexture* vRTTexture = vTexRHIRef->GetTextureReference()->GetReferencedTexture();
FRHITexture2D* vtex = (FRHITexture2D*)vRTTexture;
if (vtex == nullptr)
{
return;
}
switch (pRenderData->ECOPYBuffType)
{
case EDZCOPYGbufferType::VE_GDZSHOWGBUFFER_COPYMIP:
UDynamicAccessGBufferRenderTarget::Copy_TextureRHI(RHICmdList,vtex, vTextTarget);
break;
case EDZCOPYGbufferType::VE_GDZSHOWBUFFERR_COPYMEMO:
UDynamicAccessGBufferRenderTarget::Copy_MemoryRHI(RHICmdList, vtex, vTextTarget);
break;
case EDZCOPYGbufferType::VE_GDZSHOWBUFFERC_CPUARRAY:
UDynamicAccessGBufferRenderTarget::Copy_CPUArrayRHI();
break;
default:
break;
}
RHICmdList.CopySharedMips(vtex, vTextTarget);
}
//移除
FSceneRenderTargets::Get(RHICmdList).AdjustGBufferRefCount(RHICmdList, -1);
}
?
?
//利用SharedMip的共享方式在这里会快一些
//扩展方式
void UDynamicAccessGBufferRenderTarget::Copy_TextureRHI(FRHICommandListImmediate& RHICmdList,FRHITexture2D* pDesc,FRHITexture2D* pSrc)
{
RHICmdList.CopySharedMips(pDesc, pSrc);
}
?
?
//内存CPY,但需要锁住,这样就会让另外的渲染不能访问,会造成掉帧率。
void UDynamicAccessGBufferRenderTarget::Copy_MemoryRHI(FRHICommandListImmediate& RHICmdList, FRHITexture2D* pDesc, FRHITexture2D* pSrc)
{
uint32 Lolstrid = 0;
void * UAVRenderTargetData = RHILockTexture2D(pDesc, 0, RLM_WriteOnly, Lolstrid, true);
void * UAVCSData = RHILockTexture2D(pSrc, 0, RLM_ReadOnly, Lolstrid, true);
FMemory::Memcpy(UAVRenderTargetData, UAVCSData, GPixelFormats[pSrc->GetFormat()].BlockBytes * pSrc->GetSizeX() * pSrc->GetSizeY());
RHICmdList.UnlockTexture2D(pDesc, 0, false);
RHICmdList.UnlockTexture2D(pSrc, 0, false);
}
?
?
void UDynamicAccessGBufferRenderTarget::Copy_CPUArrayRHI()
{
?
?
}
?
?
?
?