Summary Files Commits Branches @commit @diff @stats
path: root / Year 3 / Raytracing / 0007-h13-Implemented-adaptive-resolution.patch
blob: c70ed034e1f5d7f73be998ad54425656692d47fe (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
From 4260d2f18b43f62657591238aefe8f745054eeae Mon Sep 17 00:00:00 2001
From: Syndamia <kamen@syndamia.com>
Date: Tue, 4 Jun 2024 09:51:36 +0300
Subject: [PATCH] [h13] Implemented adaptive resolution

---
 flake.lock     | 27 +++++++++++++++++++++++++++
 flake.nix      | 26 ++++++++++++++++++++++++++
 src/camera.cpp |  4 ++--
 src/main.cpp   | 21 +++++++++++++++++++--
 src/sdl.cpp    | 31 ++++++++++++++++++++++++++++---
 src/sdl.h      |  6 ++++++
 6 files changed, 108 insertions(+), 7 deletions(-)
 create mode 100644 flake.lock
 create mode 100644 flake.nix

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/camera.cpp b/src/camera.cpp
index 21dd49c..014f35d 100644
--- a/src/camera.cpp
+++ b/src/camera.cpp
@@ -29,8 +29,8 @@
 
 void Camera::beginFrame()
 {
-	m_width = frameWidth();
-	m_height = frameHeight();
+	m_width = renderWidth();
+	m_height = renderHeight();
 	//
 	m_topLeft.set(-aspectRatio, +1, +1);
 	m_topRight.set(+aspectRatio, +1, +1);
diff --git a/src/main.cpp b/src/main.cpp
index f15c4ff..bcd4f0e 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -195,7 +195,7 @@ static void detectAApixels()
 		{ -1,  0 },            { 1,  0 },
 		{ -1,  1 }, { 0,  1 }, { 1,  1 }
 	};
-	int W = frameWidth(), H = frameHeight();
+	int W = renderWidth(), H = renderHeight();
 	for (auto& r: buckets) {
 		for (int y = r.y0; y < r.y1; y++)
 			for (int x = r.x0; x < r.x1; x++) {
@@ -306,7 +306,7 @@ bool renderWithMonteCarlo(bool displayProgress, int raysPerPixel) // returns tru
 {
 	// compute the auto-focus, if required:
 	if (scene.camera->dof && scene.camera->autoFocus) {
-		Ray midRay = scene.camera->getScreenRay(frameWidth() * 0.5, frameHeight() * 0.5);
+		Ray midRay = scene.camera->getScreenRay(renderWidth() * 0.5, renderHeight() * 0.5);
 		double closestIntersectionDist = INF;
 		//
 		for (auto& node: scene.nodes) {
@@ -407,8 +407,25 @@ void gameloop()
 	const double SENSITIVITY = 0.1;
 	Uint32 startTicks = SDL_GetTicks();
 	int numFrames = 0;
+	Uint32 fpsTicks = startTicks;
+	if (isInteractive) {
+		maxDecimation();
+	}
 	bool runMode = false;
 	while (!checkForUserExit()) {
+		if (isInteractive && numFrames % 10 == 0) {
+			float fps = 10.0 / ((SDL_GetTicks() - fpsTicks) / 1000.0);
+			if (fps < 12) {
+				incDecimation();
+				buckets = getBucketsList();
+			}
+			else if (fps > 35) {
+				decDecimation();
+				buckets = getBucketsList();
+			}
+			fpsTicks = SDL_GetTicks();
+		}
+		//
 		scene.beginFrame();
 		Uint32 frameStart = SDL_GetTicks();
 		render(false);
diff --git a/src/sdl.cpp b/src/sdl.cpp
index 422b563..b1e8d4c 100644
--- a/src/sdl.cpp
+++ b/src/sdl.cpp
@@ -41,6 +41,12 @@ std::vector<SDL_Event> savedEvents;
 bool isInteractive, mouseGrabbed;
 volatile static bool exitRequested = false;
 
+static std::vector<float> decimationFactors{ 1, 1.5, 2.25, 3.375, 5, 7.6, 11.39, 17, 25.6, 38.44, 50 };
+static unsigned long currentDecimation = 0;
+static int decimate(int value) {
+	return value / decimationFactors[currentDecimation];
+}
+
 /// try to create a frame window with the given dimensions
 bool initGraphics(int frameWidth, int frameHeight)
 {
@@ -80,7 +86,7 @@ void displayVFB(Color vfb[VFB_MAX_SIZE][VFB_MAX_SIZE])
 	for (int y = 0; y < screen->h; y++) {
 		Uint32 *row = (Uint32*) ((Uint8*) screen->pixels + y * screen->pitch);
 		for (int x = 0; x < screen->w; x++)
-			row[x] = vfb[y][x].toRGB32(rs, gs, bs);
+			row[x] = vfb[decimate(y)][decimate(x)].toRGB32(rs, gs, bs);
 	}
 	showUpdatedFullscreen();
 }
@@ -227,6 +233,24 @@ int frameHeight(void)
 	return 0;
 }
 
+int renderWidth(void) {
+	return decimate(frameWidth());
+}
+int renderHeight(void) {
+	return decimate(frameHeight());
+}
+void incDecimation(void) {
+	if (currentDecimation < decimationFactors.size() - 1)
+		++currentDecimation;
+}
+void decDecimation(void) {
+	if (currentDecimation > 0)
+		--currentDecimation;
+}
+void maxDecimation(void) {
+	currentDecimation = decimationFactors.size() - 1;
+}
+
 void Rect::clip(int W, int H)
 {
 	x1 = std::min(x1, W);
@@ -237,9 +261,10 @@ void Rect::clip(int W, int H)
 
 std::vector<Rect> getBucketsList(int bucketSize)
 {
+	bucketSize = decimate(bucketSize);
 	std::vector<Rect> res;
-	int BW = (frameWidth() - 1) / bucketSize + 1;
-	int BH = (frameHeight() - 1) / bucketSize + 1;
+	int BW = (renderWidth() - 1) / bucketSize + 1;
+	int BH = (renderHeight() - 1) / bucketSize + 1;
 	for (int y = 0; y < BH; y++) {
 		for (int x = 0; x < BW; x++)
 			res.push_back(Rect(x * bucketSize, y * bucketSize, (x + 1) * bucketSize, (y + 1) * bucketSize));
diff --git a/src/sdl.h b/src/sdl.h
index 94721db..a5f66d6 100644
--- a/src/sdl.h
+++ b/src/sdl.h
@@ -60,6 +60,12 @@ void getSDLInputs(const Uint8*& keystate, int& mouseDeltaX, int& mouseDeltaY, st
 int frameWidth(void); //!< returns the frame width (pixels)
 int frameHeight(void); //!< returns the frame height (pixels)
 
+int renderWidth(void);
+int renderHeight(void);
+void incDecimation(void);
+void decDecimation(void);
+void maxDecimation(void);
+
 /// generate a list of buckets (image sub-rectangles) to be rendered, in a zigzag pattern
 std::vector<Rect> getBucketsList(int bucketSize = 64);
 /// updates a block of the screen (similar to displayVFB(), but for the specified rectangle only)
-- 
2.42.0