@@ -403,6 +403,8 @@ int FRPRRendererWorker::ResizeFramebuffer()
403
403
DestroyFrameBuffer (&m_RprAovDepthResolvedBuffer);
404
404
DestroyFrameBuffer (&m_RprDiffuseAlbedoBuffer);
405
405
DestroyFrameBuffer (&m_RprDiffuseAlbedoResolvedBuffer);
406
+ DestroyFrameBuffer (&m_RprVarianceBuffer);
407
+ DestroyFrameBuffer (&m_RprVarianceResolvedBuffer);
406
408
407
409
m_RprFrameBufferFormat.num_components = 4 ;
408
410
m_RprFrameBufferFormat.type = RPR_COMPONENT_TYPE_FLOAT32;
@@ -425,15 +427,19 @@ int FRPRRendererWorker::ResizeFramebuffer()
425
427
ContextCreateFrameBuffer (m_RprContext, m_RprFrameBufferFormat, &m_RprFrameBufferDesc, &m_RprAovDepthResolvedBuffer) != RPR_SUCCESS ||
426
428
ContextCreateFrameBuffer (m_RprContext, m_RprFrameBufferFormat, &m_RprFrameBufferDesc, &m_RprDiffuseAlbedoBuffer) != RPR_SUCCESS ||
427
429
ContextCreateFrameBuffer (m_RprContext, m_RprFrameBufferFormat, &m_RprFrameBufferDesc, &m_RprDiffuseAlbedoResolvedBuffer) != RPR_SUCCESS ||
428
- RPR::Context::SetAOV (m_RprContext, RPR::EAOV::Color, m_RprColorFrameBuffer) != RPR_SUCCESS ||
429
- RPR::Context::SetAOV (m_RprContext, m_AOV, m_RprFrameBuffer) != RPR_SUCCESS)
430
+
431
+ RPR::Context::SetAOV (m_RprContext, RPR::EAOV::Color, m_RprColorFrameBuffer) != RPR_SUCCESS ||
432
+ RPR::Context::SetAOV (m_RprContext, m_AOV, m_RprFrameBuffer) != RPR_SUCCESS)
430
433
{
431
434
UE_LOG (LogRPRRenderer, Error, TEXT (" RPR FrameBuffer creation failed" ));
432
435
RPR::Error::LogLastError (m_RprContext);
433
436
}
434
437
else
435
438
UE_LOG (LogRPRRenderer, Log, TEXT (" Framebuffer successfully created (%d,%d)" ), m_Width, m_Height);
436
439
440
+ if (!RPR::GetSettings ()->IsHybrid && RPR::GetSettings ()->EnableAdaptiveSampling )
441
+ EnableAdaptiveSampling ();
442
+
437
443
m_Resize = false ;
438
444
m_ClearFramebuffer = true ;
439
445
m_DataLock.Unlock ();
@@ -443,6 +449,8 @@ int FRPRRendererWorker::ResizeFramebuffer()
443
449
444
450
void FRPRRendererWorker::ClearFramebuffer ()
445
451
{
452
+ bool allIsGood = true ;
453
+
446
454
if (rprFrameBufferClear (m_RprFrameBuffer) != RPR_SUCCESS ||
447
455
rprFrameBufferClear (m_RprResolvedFrameBuffer) != RPR_SUCCESS ||
448
456
rprFrameBufferClear (m_RprColorFrameBuffer) != RPR_SUCCESS ||
@@ -455,10 +463,21 @@ void FRPRRendererWorker::ClearFramebuffer()
455
463
rprFrameBufferClear (m_RprDiffuseAlbedoBuffer) != RPR_SUCCESS ||
456
464
rprFrameBufferClear (m_RprDiffuseAlbedoResolvedBuffer) != RPR_SUCCESS)
457
465
{
466
+ allIsGood = false ;
458
467
UE_LOG (LogRPRRenderer, Error, TEXT (" Couldn't clear framebuffer" ));
459
468
RPR::Error::LogLastError (m_RprContext);
460
469
}
461
- else
470
+
471
+ if (!RPR::GetSettings ()->IsHybrid )
472
+ if (rprFrameBufferClear (m_RprVarianceBuffer) != RPR_SUCCESS ||
473
+ rprFrameBufferClear (m_RprVarianceResolvedBuffer) != RPR_SUCCESS)
474
+ {
475
+ allIsGood = false ;
476
+ UE_LOG (LogRPRRenderer, Error, TEXT (" Couldn't clear variance framebuffers" ));
477
+ RPR::Error::LogLastError (m_RprContext);
478
+ }
479
+
480
+ if (allIsGood)
462
481
{
463
482
m_CurrentIteration = 0 ;
464
483
m_PreviousRenderedIteration = 0 ;
@@ -844,30 +863,62 @@ int FRPRRendererWorker::RunDenoiser()
844
863
return RPR_SUCCESS;
845
864
}
846
865
866
+ void FRPRRendererWorker::EnableAdaptiveSampling ()
867
+ {
868
+ if (ContextCreateFrameBuffer (m_RprContext, m_RprFrameBufferFormat, &m_RprFrameBufferDesc, &m_RprVarianceBuffer) != RPR_SUCCESS ||
869
+ ContextCreateFrameBuffer (m_RprContext, m_RprFrameBufferFormat, &m_RprFrameBufferDesc, &m_RprVarianceResolvedBuffer) != RPR_SUCCESS)
870
+ {
871
+ UE_LOG (LogRPRRenderer, Error, TEXT (" RPR VarianceBuffer creation failed" ));
872
+ RPR::Error::LogLastError (m_RprContext);
873
+ }
874
+
875
+ if (RPR::Context::SetAOV (m_RprContext, RPR::EAOV::Variance, m_RprVarianceBuffer) != RPR_SUCCESS)
876
+ {
877
+ UE_LOG (LogRPRRenderer, Error, TEXT (" Can't set AOV Variance" ));
878
+ RPR::Error::LogLastError (m_RprContext);
879
+ }
880
+ }
881
+
882
+ bool FRPRRendererWorker::IsAdaptiveSamplingFinalized ()
883
+ {
884
+ rpr_uint activePixels = 1 ;
885
+ RPR::Context::GetInfo (m_RprContext, RPR_CONTEXT_ACTIVE_PIXEL_COUNT, sizeof (activePixels), &activePixels);
886
+
887
+ return activePixels == 0 ;
888
+ }
889
+
847
890
uint32 FRPRRendererWorker::Run ()
848
891
{
849
892
URPRSettings *settings = RPR::GetSettings ();
850
893
851
894
bool denoised = false ;
852
- int status;
853
895
854
896
while (m_StopTaskCounter.GetValue () == 0 )
855
897
{
856
898
const bool isPaused = PreRenderLoop ();
899
+ const bool applyAdapitveSampling = !settings->IsHybrid && settings->EnableAdaptiveSampling && m_CurrentIteration > settings->SamplingMin ;
900
+ const bool adaptiveSamplingFinalized = applyAdapitveSampling ? IsAdaptiveSamplingFinalized () : false ;
901
+
902
+ const uint32 iterationCeil =
903
+ (applyAdapitveSampling && settings->MaximumRenderIterations < settings->SamplingMax )
904
+ ? settings->SamplingMax
905
+ : settings->MaximumRenderIterations ;
906
+
907
+ const bool iterationCeilReached = m_CurrentIteration >= iterationCeil;
857
908
858
- if (isPaused || m_CurrentIteration >= settings-> MaximumRenderIterations )
909
+ if (isPaused || iterationCeilReached || adaptiveSamplingFinalized )
859
910
{
860
- if (settings->UseDenoiser && !denoised && m_CurrentIteration >= settings-> MaximumRenderIterations )
911
+ if (settings->UseDenoiser && !denoised && (iterationCeilReached || adaptiveSamplingFinalized) )
861
912
{
862
- status = ApplyDenoiser ();
863
- if (status == RPR_SUCCESS) {
913
+ const bool isSuccess = ApplyDenoiser () == RPR_SUCCESS;
914
+
915
+ if (isSuccess) {
864
916
UE_LOG (LogRPRRenderer, Log, TEXT (" Denoiser applied" ));
865
- denoised = true ;
866
- }
867
- else {
868
- UE_LOG (LogRPRRenderer, Log, TEXT (" Denoiser apply failed. Ignore" ));
869
- denoised = false ;
917
+ } else {
918
+ UE_LOG (LogRPRRenderer, Log, TEXT (" Denoiser applying failed. Ignore" ));
870
919
}
920
+
921
+ denoised = isSuccess;
871
922
}
872
923
FPlatformProcess::Sleep (0 .1f );
873
924
continue ;
@@ -877,6 +928,12 @@ uint32 FRPRRendererWorker::Run()
877
928
{
878
929
SCOPE_CYCLE_COUNTER (STAT_ProRender_Render);
879
930
931
+ if (!settings->IsHybrid )
932
+ {
933
+ int status = rprContextSetParameterByKey1u (m_RprContext, RPR_CONTEXT_FRAMECOUNT, m_CurrentIteration);
934
+ CHECK_WARNING (status, TEXT (" Can't set CONTEXT_FRAMECOUNT" ));
935
+ }
936
+
880
937
// Render + Resolve
881
938
if (RPR::Context::Render (m_RprContext) != RPR_SUCCESS)
882
939
{
@@ -889,18 +946,26 @@ uint32 FRPRRendererWorker::Run()
889
946
{
890
947
SCOPE_CYCLE_COUNTER (STAT_ProRender_Resolve);
891
948
if (!settings->IsHybrid )
892
- if (RPR::Context::ResolveFrameBuffer (m_RprContext, m_RprFrameBuffer, m_RprResolvedFrameBuffer) != RPR_SUCCESS ||
893
- RPR::Context::ResolveFrameBuffer (m_RprContext, m_RprShadingNormalBuffer, m_RprShadingNormalResolvedBuffer) != RPR_SUCCESS ||
894
- RPR::Context::ResolveFrameBuffer (m_RprContext, m_RprAovDepthBuffer, m_RprAovDepthResolvedBuffer) != RPR_SUCCESS ||
895
- RPR::Context::ResolveFrameBuffer (m_RprContext, m_RprDiffuseAlbedoBuffer, m_RprDiffuseAlbedoResolvedBuffer) != RPR_SUCCESS ||
896
- RPR::Context::ResolveFrameBuffer (m_RprContext, m_RprWorldCoordinatesBuffer, m_RprWorldCoordinatesResolvedBuffer) != RPR_SUCCESS ||
897
- RPR::Context::ResolveFrameBuffer (m_RprContext, m_RprAovDepthBuffer, m_RprAovDepthResolvedBuffer) != RPR_SUCCESS ||
898
- RPR::Context::ResolveFrameBuffer (m_RprContext, m_RprDiffuseAlbedoBuffer, m_RprDiffuseAlbedoResolvedBuffer) != RPR_SUCCESS)
949
+ {
950
+ if (RPR::Context::ResolveFrameBuffer (m_RprContext, m_RprFrameBuffer, m_RprResolvedFrameBuffer) != RPR_SUCCESS ||
951
+ RPR::Context::ResolveFrameBuffer (m_RprContext, m_RprAovDepthBuffer, m_RprAovDepthResolvedBuffer) != RPR_SUCCESS ||
952
+ RPR::Context::ResolveFrameBuffer (m_RprContext, m_RprShadingNormalBuffer, m_RprShadingNormalResolvedBuffer) != RPR_SUCCESS ||
953
+ RPR::Context::ResolveFrameBuffer (m_RprContext, m_RprWorldCoordinatesBuffer, m_RprWorldCoordinatesResolvedBuffer) != RPR_SUCCESS ||
954
+ RPR::Context::ResolveFrameBuffer (m_RprContext, m_RprDiffuseAlbedoBuffer, m_RprDiffuseAlbedoResolvedBuffer) != RPR_SUCCESS)
899
955
{
900
956
RPR::Error::LogLastError (m_RprContext);
901
957
m_RenderLock.Unlock ();
902
958
UE_LOG (LogRPRRenderer, Error, TEXT (" Couldn't resolve framebuffer at iteration %d, stopping.." ), m_CurrentIteration);
903
959
}
960
+
961
+ if (settings->EnableAdaptiveSampling )
962
+ if (RPR::Context::ResolveFrameBuffer (m_RprContext, m_RprVarianceBuffer, m_RprVarianceResolvedBuffer) != RPR_SUCCESS)
963
+ {
964
+ RPR::Error::LogLastError (m_RprContext);
965
+ m_RenderLock.Unlock ();
966
+ UE_LOG (LogRPRRenderer, Error, TEXT (" Couldn't resolve Adaptive Sampling framebuffer at iteration %d, stopping.." ), m_CurrentIteration);
967
+ }
968
+ }
904
969
}
905
970
m_RenderLock.Unlock ();
906
971
@@ -1018,7 +1083,18 @@ int FRPRRendererWorker::DestroyBuffers()
1018
1083
CHECK_WARNING (status, TEXT (" can't destroy diffuse albedo framebuffer" ));
1019
1084
1020
1085
status = DestroyFrameBuffer (&m_RprDiffuseAlbedoResolvedBuffer);
1021
- CHECK_WARNING (status, TEXT (" can't destroy resolved framebuffer" ));
1086
+ CHECK_WARNING (status, TEXT (" can't destroy diffuse albedo resolved framebuffer" ));
1087
+
1088
+ if (m_RprVarianceBuffer) {
1089
+ status = RPR::Context::UnSetAOV (m_RprContext, RPR::EAOV::Variance);
1090
+ CHECK_WARNING (status, TEXT (" can't unset variance aov buffer" ));
1091
+
1092
+ status = DestroyFrameBuffer (&m_RprVarianceBuffer);
1093
+ CHECK_WARNING (status, TEXT (" can't destroy rpr variance framebuffer" ));
1094
+ }
1095
+
1096
+ status = DestroyFrameBuffer (&m_RprVarianceResolvedBuffer);
1097
+ CHECK_WARNING (status, TEXT (" can't destroy variance resolved framebuffer" ));
1022
1098
1023
1099
return RPR_SUCCESS;
1024
1100
}
0 commit comments