Summary Files Commits Branches @commit @diff @stats
path: root / Year 3 / Raytracing / 0006-h12-Implemented-Russian-roulette-raytracing.patch
blob: 32372da4061d3f73e8e470ae3ca37056f29a8532 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
From 6cf50cd269931e95f058e17c15dc210fc49fa216 Mon Sep 17 00:00:00 2001
From: Syndamia <kamen@syndamia.com>
Date: Mon, 27 May 2024 17:46:57 +0300
Subject: [PATCH] [h12] Implemented Russian-roulette raytracing

---
 data/smallpt.hexray |  1 +
 flake.lock          | 27 +++++++++++++++++++++++++++
 flake.nix           | 26 ++++++++++++++++++++++++++
 src/main.cpp        | 14 ++++++++++++--
 src/scene.cpp       |  1 +
 src/scene.h         |  3 +++
 6 files changed, 70 insertions(+), 2 deletions(-)
 create mode 100644 flake.lock
 create mode 100644 flake.nix

diff --git a/data/smallpt.hexray b/data/smallpt.hexray
index dfdc6e5..cdb0cf9 100644
--- a/data/smallpt.hexray
+++ b/data/smallpt.hexray
@@ -11,6 +11,7 @@ GlobalSettings {
 	gi 			on
 	numPaths 	40
 	prepassSamples 300
+	roulette on
 }
 
 Camera camera {
diff --git a/flake.lock b/flake.lock
new file mode 100644
index 0000000..25db693
--- /dev/null
+++ b/flake.lock
@@ -0,0 +1,27 @@
+{
+  "nodes": {
+    "nixpkgs": {
+      "locked": {
+        "lastModified": 1716061101,
+        "narHash": "sha256-H0eCta7ahEgloGIwE/ihkyGstOGu+kQwAiHvwVoXaA0=",
+        "owner": "nixos",
+        "repo": "nixpkgs",
+        "rev": "e7cc61784ddf51c81487637b3031a6dd2d6673a2",
+        "type": "github"
+      },
+      "original": {
+        "owner": "nixos",
+        "ref": "nixos-23.11",
+        "repo": "nixpkgs",
+        "type": "github"
+      }
+    },
+    "root": {
+      "inputs": {
+        "nixpkgs": "nixpkgs"
+      }
+    }
+  },
+  "root": "root",
+  "version": 7
+}
diff --git a/flake.nix b/flake.nix
new file mode 100644
index 0000000..d0a99fd
--- /dev/null
+++ b/flake.nix
@@ -0,0 +1,26 @@
+# Този файл, заедно с flake.lock са си за моета система, да имам копие ако случайно изтрия нещо
+{
+  description = "hexray raytracer in FMI elective";
+
+  inputs = {
+    nixpkgs.url = "github:nixos/nixpkgs/nixos-23.11";
+  };
+
+  outputs = inputs:
+  let
+    pkgs = inputs.nixpkgs.legacyPackages."x86_64-linux";
+  in {
+    devShells."x86_64-linux".default = pkgs.mkShell {
+      nativeBuildInputs = with pkgs; [
+        cmake
+        pkg-config
+      ];
+
+      buildInputs = with pkgs; [
+        SDL2
+        openexr_3
+        zlib
+      ];
+    };
+  };
+}
diff --git a/src/main.cpp b/src/main.cpp
index 5af773d..47c0028 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -115,6 +115,16 @@ Color pathtrace(Ray ray, Color pathMultiplier = Color(1, 1, 1))
 	TraceContext tc;
 	auto earlyResult = tc.raycast(ray);
 	if (earlyResult) return (*earlyResult) * pathMultiplier;
+
+	double rating = pathMultiplier.intensity();
+	double rouletteContribution = 1.0;
+	if (scene.settings.roulette && ray.depth < 4 && rating < 1) {
+		double kill = randDouble();
+		if (kill <= 1 - rating) {
+			return Color(0, 0, 0);
+		}
+		rouletteContribution = 1 / rating;
+	}
 	// Continue building the path:
 	// Option A: continue randomly
 	Ray newRay = ray;
@@ -123,7 +133,7 @@ Color pathtrace(Ray ray, Color pathMultiplier = Color(1, 1, 1))
 	newRay.depth++;
 	tc.closestNode->shader->spawnRay(tc.closestIntersection, ray.dir, newRay, brdfColor, brdfPDF);
 	if (brdfPDF <= 0) return Color(0, 0, 0);
-	Color fromGI = pathtrace(newRay, pathMultiplier * brdfColor / brdfPDF);
+	Color fromGI = pathtrace(newRay, pathMultiplier * brdfColor / brdfPDF) * rouletteContribution;
 	//
 	// Option B: explicit light sampling
 	// scheme:
@@ -160,7 +170,7 @@ Color pathtrace(Ray ray, Color pathMultiplier = Color(1, 1, 1))
 	float pChooseLight = 1.0f / scene.lights.size();
 	float pHitLight = 1.0f / solidAngle;
 	float pThisPath = pChooseLight * pHitLight;
-	return fromGI + fromLight * pathMultiplier / pThisPath;
+	return fromGI + fromLight * pathMultiplier / pThisPath * rouletteContribution;
 }
 
 bool visible(Vector A, Vector B)
diff --git a/src/scene.cpp b/src/scene.cpp
index c909d0c..69f9206 100644
--- a/src/scene.cpp
+++ b/src/scene.cpp
@@ -772,6 +772,7 @@ void GlobalSettings::fillProperties(ParsedBlock& pb)
 	pb.getIntProp("prepassSamples", &prepassSamples, 0);
 	pb.getBoolProp("gi", &gi);
 	pb.getIntProp("numPaths", &numPaths, 1);
+	pb.getBoolProp("roulette", &roulette);
 }
 
 SceneElement* DefaultSceneParser::newSceneElement(const char* className)
diff --git a/src/scene.h b/src/scene.h
index 250d561..327c828 100644
--- a/src/scene.h
+++ b/src/scene.h
@@ -259,6 +259,9 @@ struct GlobalSettings: public SceneElement {
 	bool gi = false;
 	int numPaths = 32;
 
+	// Russian roulette-related
+	bool roulette;
+
 	void fillProperties(ParsedBlock& pb);
 	ElementType getElementType() const { return ELEM_SETTINGS; }
 };
-- 
2.42.0