From 4260d2f18b43f62657591238aefe8f745054eeae Mon Sep 17 00:00:00 2001 From: Syndamia 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 savedEvents; bool isInteractive, mouseGrabbed; volatile static bool exitRequested = false; +static std::vector 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 getBucketsList(int bucketSize) { + bucketSize = decimate(bucketSize); std::vector 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 getBucketsList(int bucketSize = 64); /// updates a block of the screen (similar to displayVFB(), but for the specified rectangle only) -- 2.42.0