Skip to content

Commit

Permalink
Implement incremental rendering for demo app
Browse files Browse the repository at this point in the history
  • Loading branch information
DragonStorm97 committed Oct 14, 2024
1 parent 9603728 commit fcc0cf1
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 11 deletions.
1 change: 1 addition & 0 deletions include/softrays/raytracer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class RayTracer {
[[nodiscard]] Ray GetRayForPixel(int x, int y, const Vec3& pixel00_loc, const Vec3& pixel_delta_u, const Vec3& pixel_delta_v) const;
[[nodiscard]] const std::vector<std::uint8_t>& GetRGBAData();
void SetupCamera();
void Render(int fromX, int fromY, int toX, int toY);
void Render();
[[nodiscard]] const std::vector<Colour>& GetPixelData() const { return PixelData; }
};
Expand Down
43 changes: 34 additions & 9 deletions softrays/apps/demo/src/demo.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "material.hpp"
#include "math.hpp"
#include "raytracer.hpp"
#include "shapes.hpp"
#include "utility.hpp"
Expand Down Expand Up @@ -29,7 +30,7 @@ void RenderLoopCallback(void* arg);
// but is slightly slower if build without

constexpr Dimension2d screen{.Width = 600, .Height = 600};
constexpr Dimension2d renderDim{.Width = 200, .Height = 200};
constexpr Dimension2d renderDim{.Width = 400, .Height = 400};
constexpr auto maxFps = 60;

class Renderer {
Expand All @@ -41,6 +42,10 @@ class Renderer {

Dimension2d ScreenDim{screen};
Dimension2d RenderDim{renderDim};
bool RenderAtScreenDim = true;
bool IncrementalRender = true;
std::size_t LastRenderedPixel{};
double LastCompleteDrawTime{};

void SetupViewport(const Dimension2d& dim)
{
Expand Down Expand Up @@ -80,21 +85,41 @@ class Renderer {

if (static_cast<std::size_t>(windowHeight) * static_cast<std::size_t>(windowWidth) != (static_cast<std::size_t>(ScreenDim.Width * ScreenDim.Height))) {
std::cout << "resizing viewport (" << windowWidth << "x" << windowHeight << ")(" << ScreenDim.Width << "x" << ScreenDim.Height << "\n";
SetupViewport({.Width = windowWidth, .Height = windowHeight});
ScreenDim.Width = windowWidth;
ScreenDim.Height = windowHeight;
if (RenderAtScreenDim) {
SetupViewport({.Width = windowWidth, .Height = windowHeight});
}
}

BeginDrawing();
ClearBackground(raylib::Color::DarkGray());

raytracer.Render();
if (IncrementalRender) {
if (LastRenderedPixel >= (static_cast<std::size_t>(RenderDim.Width) * static_cast<std::size_t>(RenderDim.Height))) {
LastRenderedPixel = 0;
std::cout << "Frame Render took:" << GetTime() - LastCompleteDrawTime << "s\n";
LastCompleteDrawTime = GetTime();
}
int nextY = static_cast<int>(LastRenderedPixel / static_cast<std::size_t>(RenderDim.Width));
int nextX = static_cast<int>(LastRenderedPixel % static_cast<std::size_t>(RenderDim.Width));
LastRenderedPixel += static_cast<std::size_t>(RenderDim.Width) / 4;
raytracer.Render(nextX, nextY, nextX + (renderDim.Width / 4), nextY + 1);
} else {
raytracer.Render();
}

RenderTarget.Update(raytracer.GetRGBAData().data());

RenderTarget.Draw(Rectangle{0, 0, static_cast<float>(RenderDim.Width), static_cast<float>(RenderDim.Height)}, Rectangle{0, 0, static_cast<float>(ScreenDim.Width), static_cast<float>(ScreenDim.Height)});
// NOTE: Render texture must be y-flipped due to default OpenGL coordinates (left-bottom) (our raytracer takes care of that already)
// target->Draw(Rectangle{0, 0, static_cast<float>(target->width), static_cast<float>(target->height)}, {0, 0}, WHITE);

// raylib::DrawText(TextFormat("%3.3f fps @ %3.4f seconds %1d spp", fps, time, raytracer.GetSamplesPerPixel()), 10, 10, 30, raylib::Color::Green()); // NOLINT
std::cout << fps << "fps | " << time << " Seconds @ " << raytracer.GetSamplesPerPixel() << '\n';
if (fps < 1.0) {
std::cout << fps << "fps | " << time << " Seconds @ " << raytracer.GetSamplesPerPixel() << '\n';
} else if (!std::isinf(fps) && !IncrementalRender) {
raylib::DrawText(TextFormat("%3.3f fps @ %3.4f seconds %1d spp", fps, time, raytracer.GetSamplesPerPixel()), 10, 10, 30, raylib::Color::Green()); // NOLINT
}

EndDrawing();
}
Expand Down Expand Up @@ -142,11 +167,11 @@ class Renderer {

// Have use lower quality settings for web builds
#if defined(PLATFORM_WEB)
raytracer.SetSamplesPerPixel(5);
raytracer.SetSamplesPerPixel(25);
raytracer.MaxDepth = 10;
#else
raytracer.SetSamplesPerPixel(20);
raytracer.MaxDepth = 10;
raytracer.SetSamplesPerPixel(50);
raytracer.MaxDepth = 20;
#endif
raytracer.FieldOfView = 40;
raytracer.LookFrom = Point3(13, 2, 3);
Expand Down Expand Up @@ -198,7 +223,7 @@ int main()
void RenderLoopCallback(void* arg)
{
static bool was_drawn = false;
if (!was_drawn) {
if (static_cast<Renderer*>(arg)->IncrementalRender || !was_drawn) {
static_cast<Renderer*>(arg)->UpdateDrawFrame();
was_drawn = true;
}
Expand Down
9 changes: 7 additions & 2 deletions softrays/library/src/raytracer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ void RayTracer::SetupCamera()
}

void RayTracer::Render()
{
Render(0, 0, ViewportDimensions.Width, ViewportDimensions.Height);
}

void RayTracer::Render(int fromX, int fromY, int toX, int toY)
{
SetupCamera();

Expand Down Expand Up @@ -68,8 +73,8 @@ void RayTracer::Render()
DefocusDisk_u = Camera_u * defocus_radius;
DefocusDisk_v = Camera_v * defocus_radius;

for (int y = 0; y < ViewportDimensions.Height; ++y) {
for (int x = 0; x < ViewportDimensions.Width; ++x) {
for (int y = fromY; y < toY; ++y) {
for (int x = fromX; x < toX; ++x) {
Colour pixel_colour{};
if (SamplesPerPixel > 1) {
for (int sample = 0; sample < SamplesPerPixel; ++sample) {
Expand Down

0 comments on commit fcc0cf1

Please sign in to comment.