Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

find_package(OpenGL REQUIRED)

option( TRACY_ENABLE "" ON)
if(CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
set(TRACY_ENABLE ON CACHE BOOL "" FORCE)
else()
set(TRACY_ENABLE OFF CACHE BOOL "" FORCE)
endif()

add_subdirectory(third_party/tracy)
add_subdirectory(lib lib)
add_subdirectory(mym mym)
Expand Down
6 changes: 3 additions & 3 deletions events.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

#include <SDL3/SDL.h>
#include <GLES3/gl3.h>
#include "arena.hpp"
#include "math.h"
#include <variant>

#include "events.h"
#include "mat4.h"
Expand All @@ -13,7 +13,7 @@

using namespace mym;

void processEvents(WindowState& window, Camera& camera, InputState& input, Scene& scene)
void processEvents(WindowState& window, Camera& camera, InputState& input, Scene& scene, Arena& frame_arena)
{
ZoneScoped;
// Handle events
Expand Down Expand Up @@ -71,7 +71,7 @@ void processEvents(WindowState& window, Camera& camera, InputState& input, Scene
);

// intersect scene
auto hits = rayIntersectsScene(worldRay, scene);
auto hits = rayIntersectsScene(worldRay, scene, frame_arena);

if (hits.size() == 0) break;

Expand Down
2 changes: 1 addition & 1 deletion include/events.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ typedef struct InputState {
} InputState;


void processEvents(WindowState& window, Camera& camera, InputState& input, Scene& scene);
void processEvents(WindowState& window, Camera& camera, InputState& input, Scene& scene, Arena& frame_arena);
12 changes: 9 additions & 3 deletions index.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

#include "arena.hpp"
#include "mat4.h"
#include "math_utils.h"
#include "camera.h"
Expand All @@ -12,6 +13,7 @@
#include "backends/imgui_impl_sdl3.h"
#include "backends/imgui_impl_opengl3.h"
#include <mutex>
#include <thread>

using namespace mym;
#define UPDATE_INTERVAL 10
Expand All @@ -36,6 +38,8 @@ void updateScene(Scene& scene, float dt) {
int main(int argc, char** argv)
{

Arena app_arena(4096);
Arena frame_arena(4096);

InputState input = {
.pointer_down = false,
Expand Down Expand Up @@ -174,7 +178,7 @@ int main(int argc, char** argv)
"floor"
);

auto scene_nodes = DArray<SceneNode*>();
std::vector<SceneNode*, ArenaAllocator<SceneNode*>> scene_nodes(&app_arena);
scene_nodes.push_back(&green_tree);
scene_nodes.push_back(&floor_model);

Expand Down Expand Up @@ -260,7 +264,8 @@ int main(int argc, char** argv)
&next_update_time,
&scene_mutex,
&camera_mutex,
&window_mutex
&window_mutex,
&frame_arena
] {

while(!window.should_close) {
Expand All @@ -273,9 +278,10 @@ int main(int argc, char** argv)
std::lock_guard<LockableBase(std::mutex)> camera_guard(camera_mutex);
std::lock_guard<LockableBase(std::mutex)> window_guard(window_mutex);
// event is forwarded to imgui in here
processEvents(window, camera, input, scene);
processEvents(window, camera, input, scene, frame_arena);
}

frame_arena.reset();
next_update_time += std::chrono::milliseconds(UPDATE_INTERVAL);
std::this_thread::sleep_until(next_update_time);
}
Expand Down
4 changes: 3 additions & 1 deletion lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 3.24)
project(NativeRendererLib)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set (CMAKE_CXX_STANDARD 20)

# find_package(OpenGL REQUIRED)
set(SDL_ALSA OFF)
Expand All @@ -25,7 +26,8 @@ add_library(lib SHARED
scene.cpp
raycast.cpp
loaders.cpp
include/mystl.hpp
arena.cpp
include/arena.hpp
)

target_include_directories(lib PUBLIC include)
Expand Down
55 changes: 55 additions & 0 deletions lib/arena.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include "arena.h"
#include <new>

Arena::Chunk* Arena::_make_chunk(size_t size) {
Chunk* chunk = static_cast<Chunk*>(malloc(sizeof(Chunk)));
if (!chunk) throw std::bad_alloc();
chunk->buffer = static_cast<char*>(malloc(size));
if (!chunk->buffer) { free(chunk); throw std::bad_alloc(); }
chunk->capacity = size;
chunk->offset = 0;
chunk->next = nullptr;
return chunk;
}

Arena::Arena(std::size_t chunk_size)
: _chunk_size(chunk_size) {
_first = _make_chunk(chunk_size);
_current = _first;
}

char* Arena::allocate(std::size_t size) {
while (_current->capacity - _current->offset < size) {
if (_current->next) {
// reuse existing chunk left over from before last reset
_current = _current->next;
} else {
size_t new_size = size > _chunk_size ? size : _chunk_size;
Chunk* next = _make_chunk(new_size);
_current->next = next;
_current = next;
}
}
char* ptr = _current->buffer + _current->offset;
_current->offset += size;
return ptr;
}

void Arena::reset() {
Chunk* chunk = _first;
while (chunk) {
chunk->offset = 0;
chunk = chunk->next;
}
_current = _first;
}

Arena::~Arena() {
Chunk* chunk = _first;
while (chunk) {
Chunk* next = chunk->next;
free(chunk->buffer);
free(chunk);
chunk = next;
}
}
33 changes: 33 additions & 0 deletions lib/include/arena.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once
#include <cstdlib>
#include <cstddef>

class Arena {

struct Chunk {
char* buffer;
size_t capacity;
size_t offset;
Chunk* next;
};

Chunk* _make_chunk(size_t size);

Chunk* _first;
Chunk* _current;
size_t _chunk_size;

public:
explicit Arena(std::size_t chunk_size);

// Allocate `size` raw bytes, returns a stable pointer
char* allocate(std::size_t size);

// Reset all chunks to reuse memory from the start
void reset();

// Non-copyable, non-movable
Arena(const Arena&) = delete;
Arena(Arena&&) = delete;
~Arena();
};
62 changes: 62 additions & 0 deletions lib/include/arena.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#pragma once
#include <cstdlib>
#include <cstddef>

class Arena {

struct Chunk {
char* buffer;
size_t capacity;
size_t offset;
Chunk* next;
};

Chunk* _make_chunk(size_t size);

Chunk* _first;
Chunk* _current;
size_t _chunk_size;

public:
explicit Arena(std::size_t chunk_size);

// Allocate `size` raw bytes, returns a stable pointer
char* allocate(std::size_t size);

// Reset all chunks to reuse memory from the start
void reset();

// Non-copyable, non-movable
Arena(const Arena&) = delete;
Arena(Arena&&) = delete;
~Arena();
};

template<class T>
class ArenaAllocator {
public:
using value_type = T;

ArenaAllocator(Arena* arena) noexcept : _arena(arena) {}

// Rebind constructor - lets std::vector create allocators for other types internally
template<class U>
ArenaAllocator(const ArenaAllocator<U>& other) noexcept : _arena(other._arena) {}

T* allocate(std::size_t n) {
return reinterpret_cast<T*>(_arena->allocate(sizeof(T) * n));
}

void deallocate(T*, std::size_t) noexcept {}

template<class U>
bool operator==(const ArenaAllocator<U>& other) const noexcept { return _arena == other._arena; }

template<class U>
bool operator!=(const ArenaAllocator<U>& other) const noexcept { return _arena != other._arena; }

private:
Arena* _arena;

template<class U> friend class ArenaAllocator;
};
3 changes: 1 addition & 2 deletions lib/include/loaders.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
#define LOADER_H

#include "scene.h"
#include "mystl.hpp"

char* get_shader_content(const char* fileName);

DArray<float> read_csv(const char* filename);
std::vector<float> read_csv(const char* filename);

SceneNode load_glb(const std::string&);

Expand Down
6 changes: 3 additions & 3 deletions lib/include/material.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include "vec.h"
#include <variant>
#include "mystl.hpp"
#include <vector>
#include <string>
#include <GLES3/gl3.h>

Expand Down Expand Up @@ -32,8 +32,8 @@ struct BasicColorMaterial {

struct BasicTextureMaterial
{
DArray<float> texture;
DArray<float> uvMap;
std::vector<float> texture;
std::vector<float> uvMap;
std::string texture_path; // path reported by Assimp (may be embedded like "*0")
TextureData texture_data; // loaded texture data (pixels, dimensions, etc.)
GLuint texture_id; // OpenGL texture ID once created by renderer
Expand Down
8 changes: 4 additions & 4 deletions lib/include/mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
#include <optional>
#include <assert.h>
#include "material.h"
#include "mystl.hpp"
#include <vector>

struct Vertices {
size_t vertex_count;
DArray<float> positions;
DArray<float> normals;
DArray<unsigned int> indices;
std::vector<float> positions;
std::vector<float> normals;
std::vector<unsigned int> indices;
size_t index_count;
};

Expand Down
Loading
Loading