CARLA
Loading...
Searching...
No Matches
RTLensEngineAdapter.cpp
Go to the documentation of this file.
1// Copyright (c) 2026 Computer Vision Center (CVC) at the Universitat Autonoma
2// de Barcelona (UAB).
3//
4// This work is licensed under the terms of the MIT license.
5// For a copy, see <https://opensource.org/licenses/MIT>.
6
8#include "Carla.h"
9
10#include <util/ue-header-guard-begin.h>
11#include "HAL/IConsoleManager.h"
12#include <util/ue-header-guard-end.h>
13
15{
16
18{
19 static IConsoleVariable *CVar =
20 IConsoleManager::Get().FindConsoleVariable(TEXT("r.RayTracing.NonBlockingPipelineCreation"));
21 // Value the engine had before this sensor overrode it; INDEX_NONE while not
22 // overridden.
23 static int32 ValueBeforeOverride = INDEX_NONE;
24 static bool bWarnedConsoleOverride = false;
25
26 if (CVar == nullptr)
27 {
28 return; // engine built without the cvar (no ray tracing support)
29 }
30
31 if (!bBlocking)
32 {
33 if (ValueBeforeOverride != INDEX_NONE)
34 {
35 CVar->Set(ValueBeforeOverride, ECVF_SetByCode);
36 UE_LOG(LogCarla, Log,
37 TEXT("rt_lens: asynchronous mode, r.RayTracing.NonBlockingPipelineCreation restored to %d"),
38 ValueBeforeOverride);
39 ValueBeforeOverride = INDEX_NONE;
40 }
41 return;
42 }
43
44 if (CVar->GetInt() == 0)
45 {
46 return; // already blocking (by us, by the ini or by the console)
47 }
48
49 // SetByConsole outranks SetByCode: the user forced non-blocking creation on
50 // purpose; say once what that costs and respect it.
51 if (((uint32)CVar->GetFlags() & ECVF_SetByMask) > (uint32)ECVF_SetByCode)
52 {
53 if (!bWarnedConsoleOverride)
54 {
55 bWarnedConsoleOverride = true;
56 UE_LOG(LogCarla, Warning,
57 TEXT("rt_lens: r.RayTracing.NonBlockingPipelineCreation is forced to %d from the console; ")
58 TEXT("synchronous-mode frames will miss freshly spawned actors while their ray tracing ")
59 TEXT("pipeline compiles (they render with the black, untagged PathTracingDefaultOpaqueCHS)."),
60 CVar->GetInt());
61 }
62 return;
63 }
64
65 ValueBeforeOverride = CVar->GetInt();
66 CVar->Set(0, ECVF_SetByCode);
67 UE_LOG(LogCarla, Log,
68 TEXT("rt_lens: synchronous mode, r.RayTracing.NonBlockingPipelineCreation 0 (was %d): ")
69 TEXT("captures wait for the ray tracing material pipeline instead of rendering new actors ")
70 TEXT("with the fallback hit shader"),
71 ValueBeforeOverride);
72}
73
74void ApplyLensModel(FPostProcessSettings &PostProcessSettings, const FLensModelDescriptor &Descriptor)
75{
76 PostProcessSettings.bOverride_PathTracingLensModel = true;
77 PostProcessSettings.bOverride_PathTracingLensFocal = true;
78 PostProcessSettings.bOverride_PathTracingLensCoeffs01 = true;
79 PostProcessSettings.bOverride_PathTracingLensCoeffs23 = true;
80 PostProcessSettings.bOverride_PathTracingLensThetaMax = true;
81 PostProcessSettings.bOverride_PathTracingLensCAScale = true;
82
83 PostProcessSettings.PathTracingLensModel = (int32)Descriptor.Model;
84
85 PostProcessSettings.PathTracingLensFocal = FVector4(
86 Descriptor.FocalX, Descriptor.FocalY, Descriptor.CenterX, Descriptor.CenterY);
87
88 // Model-specific packing (see CameraModelUtil.h): KannalaBrandt uses
89 // Coeffs[0..3], BrownConrady uses Coeffs[0..4] (p2 lands in Coeffs23.X, see
90 // the Scene.h field comment). Unused trailing slots are zero-filled.
91 const auto CoeffAt = [&Descriptor](int32 i) -> float
92 {
93 return Descriptor.Coeffs.IsValidIndex(i) ? Descriptor.Coeffs[i] : 0.0f;
94 };
95 PostProcessSettings.PathTracingLensCoeffs01 = FVector4(
96 CoeffAt(0), CoeffAt(1), CoeffAt(2), CoeffAt(3));
97 PostProcessSettings.PathTracingLensCoeffs23 = FVector4(
98 CoeffAt(4), CoeffAt(5), CoeffAt(6), CoeffAt(7));
99
100 PostProcessSettings.PathTracingLensThetaMax = Descriptor.ThetaMax;
101 PostProcessSettings.PathTracingLensCAScale = FVector2f(Descriptor.CAScaleR, Descriptor.CAScaleB);
102
103 if (Descriptor.Model == ECameraModel::LUT1D && Descriptor.LUT.Num() > 0)
104 {
105 // TODO(rt-lens-engine): FPostProcessSettings has no per-view LUT resource
106 // slot yet (a buffer, not a handful of scalars -- doesn't fit the
107 // PathTracingLens* field pattern). LUT1D cameras currently render with
108 // whatever LensModels.ush falls back to when no LUT is bound.
109 UE_LOG(LogCarla, Warning,
110 TEXT("RTLensEngineAdapter: camera_model=lut has no per-view upload path for its LUT ")
111 TEXT("samples yet; the path tracer will not see them."));
112 }
113}
114
115} // namespace RTLensEngineAdapter
void SetBlockingRayTracingPipelineCreation(bool bBlocking)
void ApplyLensModel(FPostProcessSettings &PostProcessSettings, const FLensModelDescriptor &Descriptor)
TArray< float > LUT
TArray< float > Coeffs