Skip to content

Commit

Permalink
Switch from Luajit to Lua
Browse files Browse the repository at this point in the history
  • Loading branch information
himanshugoel2797 committed Aug 8, 2021
1 parent 7a49caf commit e495d93
Show file tree
Hide file tree
Showing 17 changed files with 685 additions and 446 deletions.
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,10 @@ _deps

# Custom rules (everything added below won't be overriden by 'Generate .gitignore File' if you use 'Update' option)

build/*
**/build/*
*.zip
/.vs
openxr/*
/out/build
test_builds/
test_builds/
test.glb
6 changes: 3 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[submodule "tinygltf"]
path = tinygltf
url = https://github.com/syoyo/tinygltf.git
[submodule "luajit"]
path = luajit
url = https://luajit.org/git/luajit.git
[submodule "Lua"]
path = Lua
url = https://github.com/himanshugoel2797/Lua.git
11 changes: 8 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ cmake_minimum_required(VERSION 3.9.4)
project(ProtoVoxel VERSION 0.1.0)

find_package(OpenGL REQUIRED)
find_package(Vulkan REQUIRED)

add_library(glad ${CMAKE_SOURCE_DIR}/glad/glad.c)
target_include_directories(glad PRIVATE ${CMAKE_SOURCE_DIR})
Expand All @@ -16,14 +15,20 @@ add_executable(ProtoVoxel main.cpp ${SRC_FILES})
set_property(TARGET ProtoVoxel PROPERTY CXX_STANDARD 17)
set_property(TARGET ProtoVoxel PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)

set(TINYGLTF_HEADER_ONLY ON CACHE INTERNAL "" FORCE)
set(TINYGLTF_INSTALL OFF CACHE INTERNAL "" FORCE)
add_subdirectory(${CMAKE_SOURCE_DIR}/tinygltf)

set_target_properties(ProtoVoxel PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})

add_subdirectory(lua)

target_compile_options(ProtoVoxel PRIVATE -mavx2 -mbmi)
target_link_directories(ProtoVoxel PRIVATE ${CMAKE_SOURCE_DIR}/glfw/lib-vc2019)
target_link_libraries(ProtoVoxel glfw glad imgui OpenGL::GL Vulkan::Vulkan ${CMAKE_DL_LIBS})
target_link_libraries(ProtoVoxel lua_static glfw glad imgui OpenGL::GL ${CMAKE_DL_LIBS})
target_compile_definitions(ProtoVoxel PRIVATE DEBUG)
target_compile_definitions(ProtoVoxel PRIVATE _USE_MATH_DEFINES)
target_include_directories(ProtoVoxel PRIVATE ${CMAKE_SOURCE_DIR}/src ${CMAKE_SOURCE_DIR} ${CMAKE_SOURCE_DIR}/glfw/include)
target_include_directories(ProtoVoxel PRIVATE ${CMAKE_SOURCE_DIR}/src ${CMAKE_SOURCE_DIR} ${CMAKE_SOURCE_DIR}/glfw/include ${CMAKE_SOURCE_DIR}/tinygltf ${CMAKE_SOURCE_DIR}/Lua/lua-5.4.3/include)

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
Expand Down
6 changes: 2 additions & 4 deletions CMakeSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
"installRoot": "${projectDir}\\out\\install\\${name}",
"cmakeCommandArgs": "",
"buildCommandArgs": "",
"ctestCommandArgs": "",
"variables": []
"ctestCommandArgs": ""
},
{
"name": "x64-Release",
Expand Down Expand Up @@ -42,8 +41,7 @@
"ctestCommandArgs": "",
"inheritEnvironments": [ "linux_x64" ],
"wslPath": "${defaultWSLPath}",
"addressSanitizerRuntimeFlags": "detect_leaks=0",
"variables": []
"addressSanitizerRuntimeFlags": "detect_leaks=0"
}
]
}
1 change: 1 addition & 0 deletions Lua
Submodule Lua added at 1b44ef
1 change: 0 additions & 1 deletion luajit
Submodule luajit deleted from 983d66
5 changes: 5 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "voxel/mortoncode.h"

#include "voxel/PerlinNoise.h"
#include "misc/model.h"

#include "imgui/imgui.h"

Expand All @@ -22,6 +23,7 @@ namespace PVC = ProtoVoxel::Core;
namespace PVG = ProtoVoxel::Graphics;
namespace PVV = ProtoVoxel::Voxel;
namespace PVSG = ProtoVoxel::SceneGraph;
namespace PVM = ProtoVoxel::Misc;

class TestScene : public PVSG::SceneBase
{
Expand All @@ -37,6 +39,9 @@ class TestScene : public PVSG::SceneBase
{
PVSG::SceneBase::Initialize();

PVM::Model mdl;
PVM::Model::LoadModel("test.glb", mdl);

palette.Initialize();
for (int i = 255; i >= 0; i--)
palette.Register(glm::vec4(0, i / 255.0, 0, 1));
Expand Down
21 changes: 20 additions & 1 deletion shaders/splatting/bucket.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@ struct draw_cmd_t {
ivec4 max_bnd;
};

struct draw_cmd_indexed_t {
uint count;
uint instanceCount;
uint firstIndex;
uint baseVertex;
uint baseInstance;
uint pd0;
uint pd1;
uint pd2;
ivec4 pos;
ivec4 min_bnd;
ivec4 max_bnd;
};

layout(std430, binding = 0) readonly restrict buffer InDrawCalls_t{
uint cnt;
uint pd0;
Expand Down Expand Up @@ -156,6 +170,7 @@ vec4 offsets[8] = vec4[] (

vec2 dvec0 = (max_comps.xy - min_comps.xy) * 1024.0f; //Convert to pixels
float max_rad = max(dvec0.x, dvec0.y);
float area = dvec0.x * dvec0.y;

//Choose a mipmap level to test against
float lod = ceil( log2 ( max_rad ));
Expand All @@ -167,8 +182,11 @@ vec4 offsets[8] = vec4[] (
float sampledDepth = min(min(samples.x, samples.y), min(samples.z, samples.w));
//min=farthest point, max=nearest point

if (max_rad == 0) //Out of view
if (area == 0)
return;

if (min_comps.z > 1)
return; //chunk is behind camera

if (sampledDepth >= max_comps.z) //If the chunk is fully behind the sampled depth, don't render it
{
Expand Down Expand Up @@ -228,6 +246,7 @@ vec4 offsets[8] = vec4[] (
uint idx = atomicAdd(out_draws.cnt, 1);
out_draws.cmds[idx].count = in_draws.cmds[DrawID].count;
out_draws.cmds[idx].instanceCount = 1;
//out_draws.cmds[idx].firstIndex = 0;
out_draws.cmds[idx].baseVertex = in_draws.cmds[DrawID].baseVertex;
out_draws.cmds[idx].baseInstance = 0;
out_draws.cmds[idx].pos = in_draws.cmds[DrawID].pos;
Expand Down
39 changes: 24 additions & 15 deletions shaders/splatting/vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -88,21 +88,30 @@ void main(){
bbox[6] /= bbox[6].w;
bbox[7] /= bbox[7].w;

vec2 max_comps = max( max( max( bbox[0].xy, bbox[1].xy),
max( bbox[2].xy, bbox[3].xy)),
max( max( bbox[4].xy, bbox[5].xy),
max( bbox[6].xy, bbox[7].xy)));

vec2 min_comps = min( min( min( bbox[0].xy, bbox[1].xy),
min( bbox[2].xy, bbox[3].xy)),
min( min( bbox[4].xy, bbox[5].xy),
min( bbox[6].xy, bbox[7].xy)));

vec2 dvec0 = (max_comps - min_comps);
vec3 max_comps = max( max( max( bbox[0].xyz, bbox[1].xyz),
max( bbox[2].xyz, bbox[3].xyz)),
max( max( bbox[4].xyz, bbox[5].xyz),
max( bbox[6].xyz, bbox[7].xyz)));

vec3 min_comps = min( min( min( bbox[0].xyz, bbox[1].xyz),
min( bbox[2].xyz, bbox[3].xyz)),
min( min( bbox[4].xyz, bbox[5].xyz),
min( bbox[6].xyz, bbox[7].xyz)));

max_comps.xy = clamp(max_comps.xy, vec2(-1), vec2(1));
min_comps.xy = clamp(min_comps.xy, vec2(-1), vec2(1));

vec2 dvec0 = (max_comps.xy - min_comps.xy);

float area = dvec0.x * dvec0.y;
float max_radius = max(dvec0.x, dvec0.y) * 0.5f;
gl_PointSize = 1024.0f * max_radius * 1.1f;

//gl_Position = vec4(x, y, z, 1);
gl_Position = GlobalParams.vp * vec4(UV, 1);
//gl_PointSize = (1024.0f * 1.5f) / gl_Position.w;
gl_PointSize = 1024.0f * max_radius * 1.1f;
gl_Position = vec4( (min_comps.xy + max_comps.xy) * 0.5f, 0.5f, 1.0f );

//TODO: Large performance regression when point is too large
if (area <= 0 || max_radius <= 0 || max_radius >= 1024.0f){
gl_Position = vec4(2, 2, 2, 1);
gl_PointSize = 1;
}
}
28 changes: 28 additions & 0 deletions src/core/scripting.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) 2021 Himanshu Goel
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT

#include "scripting.h"
#include "lua.hpp"

namespace PVC = ProtoVoxel::Core;

PVC::Script::Script(){
state = luaL_newstate();
luaL_openlibs(state);
//load any standard libraries
}

PVC::Script::~Script() {
lua_close(state);
}

void PVC::Script::LoadScript(const char* file) {
luaL_loadfile(state, file);
}

void PVC::Script::Call(const char* func) {
lua_getglobal(state, func);
lua_call(state, 0, 0);
}
19 changes: 19 additions & 0 deletions src/core/scripting.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) 2021 Himanshu Goel
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT

#pragma once
#include "lua.hpp"

namespace ProtoVoxel::Core {
class Script {
private:
lua_State* state;
public:
Script();
~Script();
void LoadScript(const char* file);
void Call(const char* func);
};
}
2 changes: 2 additions & 0 deletions src/core/window.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include "window.h"

#include "glad/glad.h"
#include "imgui/imgui.h"
#include "imgui/imgui_impl_glfw.h"
#include "imgui/imgui_impl_opengl3.h"
#include "voxel/mortoncode.h"
#include <stdio.h>
#include <unordered_map>

namespace PVC = ProtoVoxel::Core;
namespace PVG = ProtoVoxel::Graphics;
Expand Down
53 changes: 53 additions & 0 deletions src/misc/model.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (c) 2021 Himanshu Goel
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT

#define TINYGLTF_IMPLEMENTATION
#define STB_IMAGE_IMPLEMENTATION
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "tinygltf/tiny_gltf.h"
#include "model.h"


static tinygltf::TinyGLTF loader;

int ProtoVoxel::Misc::Model::LoadModel(const char *file, ProtoVoxel::Misc::Model &m)
{
tinygltf::Model mdl;
bool sts = loader.LoadBinaryFromFile(&mdl, nullptr, nullptr, file);
if (!sts) return -1;

for (auto node : mdl.nodes)
{
struct Mesh mesh;
if (node.mesh != -1)
{
auto mesh_l = mdl.meshes[node.mesh];

//Is a mesh, store the data
//extract mesh data and arrange for rendering, compute AABB too
//need texcoords, vertices and indices
//TODO: extract and store materials (pbr textures + derivative map)
//get mesh rendering working
//setup lua for object simulation
//rudimentary physics engine that simulates forces and gravity
//raycasting object selection
//basic imgui ui for adding parts
//clipmap sphere rendering
}

//Store properties
for (auto prop_key : node.extras.Keys()) {
auto prop = node.extras.Get(prop_key);
if (prop.IsString())
mesh.str_props[prop_key] = prop.Get<std::string>();

if (prop.IsNumber())
mesh.num_props[prop_key] = (float)prop.GetNumberAsDouble();
}
m.meshes.push_back(mesh);
}

return 0;
}
26 changes: 26 additions & 0 deletions src/misc/model.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) 2021 Himanshu Goel
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT

#pragma once
#include <string>
#include <vector>
#include <map>

namespace ProtoVoxel::Misc {
struct Mesh {
private:
public:
std::map<std::string, std::string> str_props;
std::map<std::string, float> num_props;
};

class Model {
private:
public:
std::vector<struct ProtoVoxel::Misc::Mesh> meshes;
Model() = default;
static int LoadModel(const char *file, Model &m);
};
}
11 changes: 2 additions & 9 deletions src/voxel/chunkmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ void PVV::ChunkManager::Initialize(ChunkPalette& palette)
uint32_t idx_offset = 0;
uint32_t pos_offset = 0;

auto startTime = std::chrono::high_resolution_clock::now().time_since_epoch().count();
draw_cmds.BeginFrame();
for (int i = 0; i < GridLen; i++)
{
Expand All @@ -66,10 +65,6 @@ void PVV::ChunkManager::Initialize(ChunkPalette& palette)
}
jobManager.EndFrame();

auto stopTime = std::chrono::high_resolution_clock::now().time_since_epoch().count();
std::cout << "Generation time: " << (stopTime - startTime) / 1000000.0 << "ms" << std::endl;
std::cout << "Splatted Voxels: " << idx_offset << std::endl;

PVG::ShaderSource vert(GL_VERTEX_SHADER), frag(GL_FRAGMENT_SHADER);
vert.SetSourceFile("shaders/splatting/vertex.glsl");
vert.Compile();
Expand All @@ -81,7 +76,6 @@ void PVV::ChunkManager::Initialize(ChunkPalette& palette)
render_prog.Attach(frag);
render_prog.Link();


PVG::ShaderSource vert_(GL_VERTEX_SHADER), frag_(GL_FRAGMENT_SHADER);
vert_.SetSourceFile("shaders/splatting/resolve_vtx.glsl");
vert_.Compile();
Expand Down Expand Up @@ -248,11 +242,11 @@ void PVV::ChunkManager::Render(PVG::GpuBuffer* camera_buffer, double time)
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glCullFace(GL_BACK);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
//glEnable(GL_CULL_FACE);
glEnable(GL_PROGRAM_POINT_SIZE);

PVG::GraphicsDevice::MultiDrawIndirectCount(PVG::Topology::Points, 16, 0, PVV::DrawCmdList::Stride, draw_count);

PVG::GraphicsDevice::BindGraphicsPipeline(resolvePipeline);
glDisable(GL_CULL_FACE);
glDrawArrays(GL_TRIANGLES, 0, 3);
Expand All @@ -267,7 +261,6 @@ void PVV::ChunkManager::Render(PVG::GpuBuffer* camera_buffer, double time)
PVG::GraphicsDevice::MultiDrawIndirectCount(PVG::Topology::Points, 16, 0, PVV::DrawCmdList::Stride, draw_count);



glBlitNamedFramebuffer(fbuf->GetID(), 0, 0, 0, 1024, 1024, 0, 0, 1024, 1024, GL_COLOR_BUFFER_BIT, GL_LINEAR);
glBindFramebuffer(GL_FRAMEBUFFER, 0);

Expand Down
Loading

0 comments on commit e495d93

Please sign in to comment.