@@ -36,6 +36,19 @@ ReservoirGrid genInitialSamples(const Scene& scene, const Trackball& camera, con
36
36
return initialSamples;
37
37
}
38
38
39
+ glm::vec3 finalShading (const Reservoir& reservoir, const Ray& primaryRay, const BvhInterface& bvh, const Features& features) {
40
+ glm::vec3 finalColor (0 .0f );
41
+ for (const SampleData& sample : reservoir.outputSamples ) {
42
+ glm::vec3 sampleColor = testVisibilityLightSample (sample.lightSample .position , bvh, features, primaryRay, reservoir.hitInfo ) ?
43
+ computeShading (sample.lightSample .position , sample.lightSample .color , features, primaryRay, reservoir.hitInfo ) :
44
+ glm::vec3 (0 .0f );
45
+ sampleColor *= sample.outputWeight ;
46
+ finalColor += sampleColor;
47
+ }
48
+ finalColor /= reservoir.outputSamples .size (); // Divide final shading value by number of samples
49
+ return finalColor;
50
+ }
51
+
39
52
void spatialReuse (ReservoirGrid& reservoirGrid, const BvhInterface& bvh, const Screen& screen, const Features& features) {
40
53
// Uniform selection of neighbours in N pixel Manhattan distance radius
41
54
std::random_device rd;
@@ -86,7 +99,7 @@ void spatialReuse(ReservoirGrid& reservoirGrid, const BvhInterface& bvh, const S
86
99
}
87
100
88
101
void temporalReuse (ReservoirGrid& reservoirGrid, ReservoirGrid& previousFrameGrid, const BvhInterface& bvh,
89
- Screen& screen, const glm::vec2 motionVector, const Features& features) {
102
+ Screen& screen, const Features& features) {
90
103
glm::ivec2 windowResolution = screen.resolution ();
91
104
#ifdef NDEBUG
92
105
#pragma omp parallel for schedule(guided)
@@ -116,12 +129,12 @@ void temporalReuse(ReservoirGrid& reservoirGrid, ReservoirGrid& previousFrameGri
116
129
}
117
130
}
118
131
119
- ReservoirGrid renderRayTracing (std::shared_ptr<ReservoirGrid> previousFrameGrid,
120
- const Scene& scene, const Trackball& camera,
121
- const BvhInterface& bvh, Screen& screen,
122
- const glm::vec2 motionVector, const Features& features) {
132
+ ReservoirGrid renderReSTIR (std::shared_ptr<ReservoirGrid> previousFrameGrid,
133
+ const Scene& scene, const Trackball& camera,
134
+ const BvhInterface& bvh, Screen& screen,
135
+ const Features& features) {
123
136
ReservoirGrid reservoirGrid = genInitialSamples (scene, camera, bvh, screen, features);
124
- if (features.temporalReuse && previousFrameGrid) { temporalReuse (reservoirGrid, *previousFrameGrid.get (), bvh, screen, motionVector, features); }
137
+ if (features.temporalReuse && previousFrameGrid) { temporalReuse (reservoirGrid, *previousFrameGrid.get (), bvh, screen, features); }
125
138
if (features.spatialReuse ) { spatialReuse (reservoirGrid, bvh, screen, features); }
126
139
127
140
// Final shading
@@ -132,16 +145,8 @@ ReservoirGrid renderRayTracing(std::shared_ptr<ReservoirGrid> previousFrameGrid,
132
145
for (int y = 0 ; y < windowResolution.y ; y++) {
133
146
for (int x = 0 ; x != windowResolution.x ; x++) {
134
147
// Compute shading from final sample(s)
135
- glm::vec3 finalColor (0 .0f );
136
- const Reservoir& reservoir = reservoirGrid[y][x];
137
- for (const SampleData& sample : reservoir.outputSamples ) {
138
- glm::vec3 sampleColor = testVisibilityLightSample (sample.lightSample .position , bvh, features, reservoir.cameraRay , reservoir.hitInfo ) ?
139
- computeShading (sample.lightSample .position , sample.lightSample .color , features, reservoir.cameraRay , reservoir.hitInfo ) :
140
- glm::vec3 (0 .0f );
141
- sampleColor *= sample.outputWeight ;
142
- finalColor += sampleColor;
143
- }
144
- finalColor /= reservoir.outputSamples .size (); // Divide final shading value by number of samples
148
+ const Reservoir& reservoir = reservoirGrid[y][x];
149
+ glm::vec3 finalColor = finalShading (reservoir, reservoir.cameraRay , bvh, features);
145
150
146
151
// Apply tone mapping and set final pixel color
147
152
if (features.enableToneMapping ) { finalColor = exposureToneMapping (finalColor, features); }
@@ -151,4 +156,33 @@ ReservoirGrid renderRayTracing(std::shared_ptr<ReservoirGrid> previousFrameGrid,
151
156
152
157
// Return current frame's final grid for temporal reuse
153
158
return reservoirGrid;
154
- }
159
+ }
160
+
161
+ void renderRMIS (const Scene& scene, const Trackball& camera, const BvhInterface& bvh, Screen& screen, const Features& features) {
162
+ ReservoirGrid reservoirGrid = genInitialSamples (scene, camera, bvh, screen, features);
163
+ glm::ivec2 windowResolution = screen.resolution ();
164
+
165
+ #ifdef NDEBUG
166
+ #pragma omp parallel for schedule(guided)
167
+ #endif
168
+ for (int y = 0 ; y < windowResolution.y ; y++) {
169
+ for (int x = 0 ; x != windowResolution.x ; x++) {
170
+ const Ray& primaryRay = reservoirGrid[y][x].cameraRay ;
171
+
172
+ // Combine samples from all pixels in 3x3 neighborhood
173
+ glm::vec3 finalColor (0 .0f );
174
+ for (int sampleY = y - 1 ; sampleY <= y + 1 ; sampleY++) {
175
+ for (int sampleX = x - 1 ; sampleX <= x + 1 ; sampleX++) {
176
+ int neighbourY = std::clamp (sampleY, 0 , windowResolution.y - 1 );
177
+ int neighbourX = std::clamp (sampleX, 0 , windowResolution.x - 1 );
178
+ finalColor += finalShading (reservoirGrid[neighbourY][neighbourX], primaryRay, bvh, features);
179
+ }
180
+ }
181
+ finalColor *= (1 .0f / 9 .0f ); // TODO: Change to actual OMIS MIS weights
182
+
183
+ // Apply tone mapping and set final pixel color
184
+ if (features.enableToneMapping ) { finalColor = exposureToneMapping (finalColor, features); }
185
+ screen.setPixel (x, y, finalColor);
186
+ }
187
+ }
188
+ }
0 commit comments