remove batched rendering code
This commit is contained in:
@ -4,8 +4,6 @@
|
|||||||
#include <raylib.h>
|
#include <raylib.h>
|
||||||
|
|
||||||
#define PRINT_TIMINGS
|
#define PRINT_TIMINGS
|
||||||
// #define BATCHING
|
|
||||||
#define INSTANCING // Fastest (so far)
|
|
||||||
// #define WEB
|
// #define WEB
|
||||||
|
|
||||||
// Window
|
// Window
|
||||||
|
|||||||
@ -51,10 +51,6 @@ private:
|
|||||||
|
|
||||||
Material vertex_mat;
|
Material vertex_mat;
|
||||||
|
|
||||||
// Batching
|
|
||||||
float *cube;
|
|
||||||
Mesh graph;
|
|
||||||
|
|
||||||
// Instancing
|
// Instancing
|
||||||
int transforms_size;
|
int transforms_size;
|
||||||
Matrix *transforms;
|
Matrix *transforms;
|
||||||
@ -68,8 +64,8 @@ public:
|
|||||||
public:
|
public:
|
||||||
Renderer()
|
Renderer()
|
||||||
: camera(OrbitCamera3D(Vector3(0, 0, 0), CAMERA_DISTANCE)),
|
: camera(OrbitCamera3D(Vector3(0, 0, 0), CAMERA_DISTANCE)),
|
||||||
mark_solutions(false), connect_solutions(false), cube(nullptr),
|
mark_solutions(false), connect_solutions(false), transforms_size(0),
|
||||||
transforms_size(0), transforms(nullptr) {
|
transforms(nullptr) {
|
||||||
render_target = LoadRenderTexture(GetScreenWidth() / 2.0,
|
render_target = LoadRenderTexture(GetScreenWidth() / 2.0,
|
||||||
GetScreenHeight() - MENU_HEIGHT);
|
GetScreenHeight() - MENU_HEIGHT);
|
||||||
klotski_target = LoadRenderTexture(GetScreenWidth() / 2.0,
|
klotski_target = LoadRenderTexture(GetScreenWidth() / 2.0,
|
||||||
@ -92,12 +88,6 @@ public:
|
|||||||
|
|
||||||
UnloadMaterial(vertex_mat);
|
UnloadMaterial(vertex_mat);
|
||||||
|
|
||||||
// Batching
|
|
||||||
if (cube != nullptr) {
|
|
||||||
MemFree(cube);
|
|
||||||
UnloadMesh(graph);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Instancing
|
// Instancing
|
||||||
if (transforms != nullptr) {
|
if (transforms != nullptr) {
|
||||||
MemFree(transforms);
|
MemFree(transforms);
|
||||||
@ -108,26 +98,18 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
private:
|
||||||
auto UpdateCamera(const MassSpringSystem &mass_springs,
|
|
||||||
const State ¤t_state) -> void;
|
|
||||||
|
|
||||||
auto UpdateTextureSizes() -> void;
|
|
||||||
|
|
||||||
#ifdef BATCHING
|
|
||||||
auto AllocateGraphBatching() -> void;
|
|
||||||
|
|
||||||
auto ReallocateGraphBatchingIfNecessary(const MassSpringSystem &mass_springs)
|
|
||||||
-> void;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef INSTANCING
|
|
||||||
auto AllocateGraphInstancing(const MassSpringSystem &mass_springs) -> void;
|
auto AllocateGraphInstancing(const MassSpringSystem &mass_springs) -> void;
|
||||||
|
|
||||||
auto
|
auto
|
||||||
ReallocateGraphInstancingIfNecessary(const MassSpringSystem &mass_springs)
|
ReallocateGraphInstancingIfNecessary(const MassSpringSystem &mass_springs)
|
||||||
-> void;
|
-> void;
|
||||||
#endif
|
|
||||||
|
public:
|
||||||
|
auto UpdateCamera(const MassSpringSystem &mass_springs,
|
||||||
|
const State ¤t_state) -> void;
|
||||||
|
|
||||||
|
auto UpdateTextureSizes() -> void;
|
||||||
|
|
||||||
auto DrawMassSprings(const MassSpringSystem &mass_springs,
|
auto DrawMassSprings(const MassSpringSystem &mass_springs,
|
||||||
const State ¤t_state,
|
const State ¤t_state,
|
||||||
|
|||||||
114
src/renderer.cpp
114
src/renderer.cpp
@ -117,58 +117,6 @@ auto Renderer::UpdateTextureSizes() -> void {
|
|||||||
menu_target = LoadRenderTexture(width * 2, MENU_HEIGHT);
|
menu_target = LoadRenderTexture(width * 2, MENU_HEIGHT);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef BATCHING
|
|
||||||
auto Renderer::AllocateGraphBatching() -> void {
|
|
||||||
int vertices = 36;
|
|
||||||
int max_masses = 100000;
|
|
||||||
|
|
||||||
graph = {0};
|
|
||||||
graph.vertexCount = max_masses * vertices;
|
|
||||||
graph.triangleCount = max_masses * 12;
|
|
||||||
graph.vertices = (float *)MemAlloc(graph.vertexCount * 3 * sizeof(float));
|
|
||||||
|
|
||||||
memset(graph.vertices, 0, graph.vertexCount * 3 * sizeof(float));
|
|
||||||
|
|
||||||
UploadMesh(&graph, true);
|
|
||||||
|
|
||||||
Mesh indexed_cube = GenMeshCube(VERTEX_SIZE, VERTEX_SIZE, VERTEX_SIZE);
|
|
||||||
cube = (float *)MemAlloc(indexed_cube.triangleCount * 3 * 3 * sizeof(float));
|
|
||||||
for (int i = 0; i < indexed_cube.triangleCount * 3; ++i) {
|
|
||||||
int idx = indexed_cube.indices[i];
|
|
||||||
cube[i * 3 + 0] = indexed_cube.vertices[idx * 3 + 0];
|
|
||||||
cube[i * 3 + 1] = indexed_cube.vertices[idx * 3 + 1];
|
|
||||||
cube[i * 3 + 2] = indexed_cube.vertices[idx * 3 + 2];
|
|
||||||
}
|
|
||||||
UnloadMesh(indexed_cube);
|
|
||||||
|
|
||||||
std::cout << "Allocated graph mesh." << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto Renderer::ReallocateGraphBatchingIfNecessary(
|
|
||||||
const MassSpringSystem &mass_springs) -> void {
|
|
||||||
if (graph.vertexCount / 3 > mass_springs.masses.size()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
int vertices = 36;
|
|
||||||
int max_masses = mass_springs.masses.size() * 2;
|
|
||||||
|
|
||||||
UnloadMesh(graph);
|
|
||||||
|
|
||||||
graph = {0};
|
|
||||||
graph.vertexCount = max_masses * vertices;
|
|
||||||
graph.triangleCount = max_masses * 12;
|
|
||||||
graph.vertices = (float *)MemAlloc(graph.vertexCount * 3 * sizeof(float));
|
|
||||||
|
|
||||||
memset(graph.vertices, 0, graph.vertexCount * 3 * sizeof(float));
|
|
||||||
|
|
||||||
UploadMesh(&graph, true);
|
|
||||||
|
|
||||||
std::cout << "Reallocated graph mesh." << std::endl;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef INSTANCING
|
|
||||||
auto Renderer::AllocateGraphInstancing(const MassSpringSystem &mass_springs)
|
auto Renderer::AllocateGraphInstancing(const MassSpringSystem &mass_springs)
|
||||||
-> void {
|
-> void {
|
||||||
cube_instance = GenMeshCube(VERTEX_SIZE, VERTEX_SIZE, VERTEX_SIZE);
|
cube_instance = GenMeshCube(VERTEX_SIZE, VERTEX_SIZE, VERTEX_SIZE);
|
||||||
@ -194,12 +142,24 @@ auto Renderer::ReallocateGraphInstancingIfNecessary(
|
|||||||
transforms_size = mass_springs.masses.size();
|
transforms_size = mass_springs.masses.size();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
auto Renderer::DrawMassSprings(const MassSpringSystem &mass_springs,
|
auto Renderer::DrawMassSprings(const MassSpringSystem &mass_springs,
|
||||||
const State ¤t_state,
|
const State ¤t_state,
|
||||||
const std::unordered_set<State> &winning_states)
|
const std::unordered_set<State> &winning_states)
|
||||||
-> void {
|
-> void {
|
||||||
|
// Prepare cube instancing
|
||||||
|
if (transforms == nullptr) {
|
||||||
|
AllocateGraphInstancing(mass_springs);
|
||||||
|
}
|
||||||
|
ReallocateGraphInstancingIfNecessary(mass_springs);
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
for (const auto &[state, mass] : mass_springs.masses) {
|
||||||
|
transforms[i] =
|
||||||
|
MatrixTranslate(mass.position.x, mass.position.y, mass.position.z);
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
|
||||||
BeginTextureMode(render_target);
|
BeginTextureMode(render_target);
|
||||||
ClearBackground(RAYWHITE);
|
ClearBackground(RAYWHITE);
|
||||||
|
|
||||||
@ -217,56 +177,8 @@ auto Renderer::DrawMassSprings(const MassSpringSystem &mass_springs,
|
|||||||
rlEnd();
|
rlEnd();
|
||||||
|
|
||||||
// Draw masses (instanced)
|
// Draw masses (instanced)
|
||||||
#ifdef INSTANCING
|
|
||||||
if (transforms == nullptr) {
|
|
||||||
AllocateGraphInstancing(mass_springs);
|
|
||||||
}
|
|
||||||
ReallocateGraphInstancingIfNecessary(mass_springs);
|
|
||||||
|
|
||||||
int i = 0;
|
|
||||||
for (const auto &[state, mass] : mass_springs.masses) {
|
|
||||||
transforms[i] =
|
|
||||||
MatrixTranslate(mass.position.x, mass.position.y, mass.position.z);
|
|
||||||
++i;
|
|
||||||
}
|
|
||||||
DrawMeshInstanced(cube_instance, vertex_mat, transforms,
|
DrawMeshInstanced(cube_instance, vertex_mat, transforms,
|
||||||
mass_springs.masses.size());
|
mass_springs.masses.size());
|
||||||
#endif
|
|
||||||
|
|
||||||
// Draw masses (batched)
|
|
||||||
#ifdef BATCHING
|
|
||||||
if (cube == nullptr) {
|
|
||||||
AllocateGraphBatching();
|
|
||||||
}
|
|
||||||
ReallocateGraphBatchingIfNecessary(mass_springs);
|
|
||||||
|
|
||||||
int vertices = 36;
|
|
||||||
int current_size = mass_springs.masses.size();
|
|
||||||
int i = 0;
|
|
||||||
for (const auto &[state, mass] : mass_springs.masses) {
|
|
||||||
for (int v = 0; v < vertices; ++v) {
|
|
||||||
int dst = (i * vertices + v) * 3;
|
|
||||||
int src = v * 3;
|
|
||||||
graph.vertices[dst + 0] = cube[src + 0] + mass.position.x;
|
|
||||||
graph.vertices[dst + 1] = cube[src + 1] + mass.position.y;
|
|
||||||
graph.vertices[dst + 2] = cube[src + 2] + mass.position.z;
|
|
||||||
}
|
|
||||||
++i;
|
|
||||||
}
|
|
||||||
UpdateMeshBuffer(graph, 0, graph.vertices,
|
|
||||||
current_size * vertices * 3 * sizeof(float), 0);
|
|
||||||
|
|
||||||
// Temporarily reduce the vertex count to the used part (we overallocate)
|
|
||||||
int full_size = graph.vertexCount;
|
|
||||||
graph.vertexCount = current_size * vertices;
|
|
||||||
graph.triangleCount = current_size * 12;
|
|
||||||
|
|
||||||
DrawMesh(graph, vertex_mat, MatrixIdentity());
|
|
||||||
|
|
||||||
// Restore the vertex count
|
|
||||||
graph.vertexCount = full_size;
|
|
||||||
graph.triangleCount = full_size / 3;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// Mark current state
|
// Mark current state
|
||||||
const Mass ¤t_mass = mass_springs.GetMass(current_state);
|
const Mass ¤t_mass = mass_springs.GetMass(current_state);
|
||||||
|
|||||||
Reference in New Issue
Block a user