Skip to content

Commit f8368b2

Browse files
committed
add option to enable pre-multiplying alpha for the background
1 parent ae750eb commit f8368b2

File tree

7 files changed

+36
-5
lines changed

7 files changed

+36
-5
lines changed

devices/rtx/device/gpu/gpu_objects.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,7 @@ struct RendererGPUData
693693
float inverseVolumeSamplingRate;
694694
float occlusionDistance;
695695
bool cullTriangleBF;
696+
bool premultiplyBackground;
696697
bool tonemap; // enable internal tonemapping during sample accumulation
697698
};
698699

devices/rtx/device/renderer/AmbientOcclusion_ptx.cu

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,10 @@ VISRTX_GLOBAL void __raygen__()
198198
color *= opacity;
199199

200200
const auto bg = getBackground(frameData, ss.screen, ray.dir);
201-
accumulateValue(color, vec3(bg) * bg.a, opacity);
202-
accumulateValue(opacity, bg.w, opacity);
201+
const bool premultiplyBg = rendererParams.premultiplyBackground;
202+
accumulateValue(
203+
color, premultiplyBg ? vec3(bg) * bg.a : vec3(bg), opacity);
204+
accumulateValue(opacity, bg.a, opacity);
203205
accumulateValue(outputColor, color, outputOpacity);
204206
accumulateValue(outputOpacity, opacity, outputOpacity);
205207
break;

devices/rtx/device/renderer/DirectLight_ptx.cu

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,8 +406,10 @@ VISRTX_GLOBAL void __raygen__()
406406
color *= opacity;
407407

408408
const auto bg = getBackground(frameData, ss.screen, ray.dir);
409-
accumulateValue(color, vec3(bg) * bg.a, opacity);
410-
accumulateValue(opacity, bg.w, opacity);
409+
const bool premultiplyBg = rendererParams.premultiplyBackground;
410+
accumulateValue(
411+
color, premultiplyBg ? vec3(bg) * bg.a : vec3(bg), opacity);
412+
accumulateValue(opacity, bg.a, opacity);
411413
accumulateValue(outputColor, color, outputOpacity);
412414
accumulateValue(outputOpacity, opacity, outputOpacity);
413415
break;

devices/rtx/device/renderer/Raycast_ptx.cu

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,9 @@ VISRTX_GLOBAL void __raygen__()
159159
color *= opacity;
160160

161161
const auto bg = getBackground(frameData, ss.screen, ray.dir);
162-
accumulateValue(color, vec3(bg) * bg.a, opacity);
162+
const bool premultiplyBg = rendererParams.premultiplyBackground;
163+
accumulateValue(
164+
color, premultiplyBg ? vec3(bg) * bg.a : vec3(bg), opacity);
163165
accumulateValue(opacity, bg.w, opacity);
164166
accumulateValue(outputColor, color, outputOpacity);
165167
accumulateValue(outputOpacity, opacity, outputOpacity);

devices/rtx/device/renderer/Renderer.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ void Renderer::commitParameters()
167167
m_cullTriangleBF = getParam<bool>("cullTriangleBackfaces", false);
168168
m_volumeSamplingRate =
169169
std::clamp(getParam<float>("volumeSamplingRate", 0.125f), 1e-3f, 10.f);
170+
m_premultiplyBackground = getParam<bool>("premultiplyBackground", false);
170171
if (m_checkerboard)
171172
m_spp = 1;
172173
}
@@ -207,6 +208,7 @@ void Renderer::populateFrameData(FrameGPUData &fd) const
207208
fd.renderer.inverseVolumeSamplingRate = 1.f / m_volumeSamplingRate;
208209
fd.renderer.numIterations = std::max(m_spp, 1);
209210
fd.renderer.maxRayDepth = m_maxRayDepth;
211+
fd.renderer.premultiplyBackground = m_premultiplyBackground;
210212
}
211213

212214
OptixPipeline Renderer::pipeline()

devices/rtx/device/renderer/Renderer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ struct Renderer : public Object
9090
bool m_tonemap{true}; // enable internal tonemapping during sample accumulation
9191
int m_sampleLimit{0};
9292
bool m_cullTriangleBF{false};
93+
bool m_premultiplyBackground{false};
9394
float m_volumeSamplingRate{1.f};
9495

9596
helium::ChangeObserverPtr<Array2D> m_backgroundImage;

devices/rtx/device/visrtx_device.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@
8383
"default": true,
8484
"description": "enable internal tonemapping during sample accumulation"
8585
},
86+
{
87+
"name": "premultiplyBackground",
88+
"types": ["ANARI_BOOL"],
89+
"tags": [],
90+
"default": false,
91+
"description": "pre-multiply alpha channel with background color"
92+
},
8693
{
8794
"name": "cullTriangleBackfaces",
8895
"types": ["ANARI_BOOL"],
@@ -184,6 +191,13 @@
184191
"default": true,
185192
"description": "enable internal tonemapping during sample accumulation"
186193
},
194+
{
195+
"name": "premultiplyBackground",
196+
"types": ["ANARI_BOOL"],
197+
"tags": [],
198+
"default": false,
199+
"description": "pre-multiply alpha channel with background color"
200+
},
187201
{
188202
"name": "cullTriangleBackfaces",
189203
"types": ["ANARI_BOOL"],
@@ -301,6 +315,13 @@
301315
"default": true,
302316
"description": "enable internal tonemapping during sample accumulation"
303317
},
318+
{
319+
"name": "premultiplyBackground",
320+
"types": ["ANARI_BOOL"],
321+
"tags": [],
322+
"default": false,
323+
"description": "pre-multiply alpha channel with background color"
324+
},
304325
{
305326
"name": "volumeSamplingRate",
306327
"types": ["ANARI_FLOAT32"],

0 commit comments

Comments
 (0)