Skip to content

Commit

Permalink
add bunch of new demos
Browse files Browse the repository at this point in the history
  • Loading branch information
epezent committed Jul 20, 2021
1 parent 26c4dc6 commit 5d91b64
Show file tree
Hide file tree
Showing 18 changed files with 28,285 additions and 2 deletions.
26,167 changes: 26,167 additions & 0 deletions 3rdparty/json.hpp

Large diffs are not rendered by default.

428 changes: 428 additions & 0 deletions 3rdparty/stb/stb_perlin.h

Large diffs are not rendered by default.

46 changes: 44 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ FetchContent_MakeAvailable(glfw)
FetchContent_Declare(iir GIT_REPOSITORY https://github.com/berndporr/iir1)
FetchContent_MakeAvailable(iir)

FetchContent_Declare(curl GIT_REPOSITORY https://github.com/curl/curl)
FetchContent_MakeAvailable(curl)

set(CPR_BUILD_TESTS_SSL OFF CACHE BOOL "" FORCE)
FetchContent_Declare(cpr GIT_REPOSITORY https://github.com/whoshuu/cpr.git)
FetchContent_MakeAvailable(cpr)

# LOCAL LIBS
set(KISS_SRC "3rdparty/kissfft/kiss_fft.h"
"3rdparty/kissfft/kiss_fftr.h"
Expand Down Expand Up @@ -66,14 +73,22 @@ include_directories(../imgui/ ../imgui/examples ../imgui/examples/libs/gl3w ../i
###############################################################################

set(IMPLOT_HEADERS ../implot/implot.h ../implot/implot_internal.h)
set(IMPLOT_SRC ../implot/implot.cpp ../implot/implot_items.cpp ../implot/implot_demo.cpp) # ../implot/backends/implot_impl_opengl3.cpp)
set(IMPLOT_SRC ../implot/implot.cpp ../implot/implot_items.cpp ../implot/implot_demo.cpp)

if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/../implot/backends/implot_impl_opengl3.cpp")
list(APPEND IMPLOT_SRC ../implot/backends/implot_impl_opengl3.cpp)
endif()

add_library(implot ${IMPLOT_HEADERS} ${IMPLOT_SRC})
target_link_libraries(implot PUBLIC imgui)
target_compile_definitions(implot PUBLIC IMPLOT_DEBUG IMPLOT_DLL_EXPORT IMPLOT_BACKEND_ENABLE_OPENGL3 IMGUI_IMPL_OPENGL_LOADER_GL3W)
set_property(TARGET implot PROPERTY CXX_STANDARD 11)
if(MSVC)
target_compile_options(implot PRIVATE /W4 /WX)
target_compile_options(implot PRIVATE /W4 /WX /FA /O2 /arch:AVX2 /fp:fast )
# set(MAX_INLINE_DEPTH 1000)
# list(APPEND COMMON_FLAGS "$<$<STREQUAL:${CMAKE_CXX_COMPILER_ID},Clang>:-mllvm>")
# list(APPEND COMMON_FLAGS "$<$<STREQUAL:${CMAKE_CXX_COMPILER_ID},Clang>:-inline-threshold=${MAX_INLINE_DEPTH}>")

else()
target_compile_options(implot PRIVATE -Wall -Wextra -pedantic -Werror)
endif()
Expand All @@ -87,6 +102,7 @@ include_directories(../implot/)
add_library(app
common/App.h
common/App.cpp
common/Shader.h
common/Fonts/Fonts.h
common/Fonts/FontAwesome5.cpp
common/Fonts/RobotoRegular.cpp
Expand All @@ -99,6 +115,14 @@ add_library(app
target_include_directories(app PUBLIC common)
target_link_libraries(app implot)

###############################################################################
# TEST APPS
###############################################################################

add_executable(benchmarks "tests/benchmarks.cpp")
target_link_libraries(benchmarks app)
target_compile_options(benchmarks PUBLIC)

###############################################################################
# DEMO APPS
###############################################################################
Expand All @@ -107,6 +131,24 @@ target_link_libraries(app implot)
add_executable(demo "demos/demo.cpp")
target_link_libraries(demo app)

add_executable(maps "demos/maps.cpp")
target_link_libraries(maps app cpr)

# GPU acceleration
add_executable(gpu "demos/gpu.cpp")
target_link_libraries(gpu app)
target_compile_features(gpu PRIVATE cxx_std_17)

# terrain visualizer with heatmaps
add_executable(terrain "demos/terrain.cpp")
target_link_libraries(terrain app)
target_include_directories(terrain PRIVATE "3rdparty")

add_executable(mandel "demos/mandel.cpp")
target_link_libraries(mandel app)
target_include_directories(mandel PRIVATE "3rdparty")
target_compile_options(mandel PRIVATE /FA /O2 /arch:AVX2 /fp:fast /Zi /openmp)

# imdesmos demo
add_executable(imdesmos "demos/imdesmos.cpp")
target_link_libraries(imdesmos app)
Expand Down
8 changes: 8 additions & 0 deletions common/Helpers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <math.h>
#include <cstdlib>

template <typename T>
inline T RandomRange(T min, T max) {
T scale = rand() / (T) RAND_MAX;
return min + scale * ( max - min );
}
122 changes: 122 additions & 0 deletions common/Shader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#pragma once
#include <GL/gl3w.h>
#include <fstream>
#include <sstream>
#include <iostream>

struct Shader {

bool LoadFromFile(const char* vertPath, const char* fragPath, const char* geomPath = NULL) {
std::ifstream vertFile, fragFile, geomFile;
std::string vertCode, fragCode, geomCode;
vertFile.exceptions (std::ifstream::failbit | std::ifstream::badbit);
fragFile.exceptions (std::ifstream::failbit | std::ifstream::badbit);
geomFile.exceptions (std::ifstream::failbit | std::ifstream::badbit);
try
{
std::stringstream vertStream, fragStream, geomStream;

vertFile.open(vertPath);
vertStream << vertFile.rdbuf();
vertFile.close();
vertCode = vertStream.str();

fragFile.open(fragPath);
fragStream << fragFile.rdbuf();
fragFile.close();
fragCode = fragStream.str();

if (geomPath != NULL) {
geomFile.open(geomPath);
geomStream << geomFile.rdbuf();
geomFile.close();
geomCode = geomStream.str();
}

}
catch(std::ifstream::failure e)
{
std::cout << "Failed to read shader files!" << std::endl;
return false;
}
return LoadFromString(vertCode.c_str(), fragCode.c_str(), geomPath == NULL ? NULL : geomCode.c_str());
}

bool LoadFromString(const char* vertSrc, const char* fragSrc, const char* geomSrc = NULL) {
int success;
char infoLog[512];
// compile vertex shader
GLuint vertShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertShader, 1, &vertSrc, NULL);
glCompileShader(vertShader);
glGetShaderiv(vertShader, GL_COMPILE_STATUS, &success);
if(!success) {
glGetShaderInfoLog(vertShader, 512, NULL, infoLog);
std::cout << "Failed to compile vertex shader!\n" << infoLog << std::endl;
return false;
}
// compiler fragment shader
GLuint fragShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragShader, 1, &fragSrc, NULL);
glCompileShader(fragShader);
glGetShaderiv(fragShader, GL_COMPILE_STATUS, &success);
if(!success) {
glGetShaderInfoLog(fragShader, 512, NULL, infoLog);
std::cout << "Failed to compile fragment shader!\n" << infoLog << std::endl;
return false;
}
GLuint geomShader;
if (geomSrc != NULL) {
geomShader = glCreateShader(GL_GEOMETRY_SHADER);
glShaderSource(geomShader, 1, &geomSrc, NULL);
glCompileShader(geomShader);
glGetShaderiv(geomShader, GL_COMPILE_STATUS, &success);
if(!success) {
glGetShaderInfoLog(geomShader, 512, NULL, infoLog);
std::cout << "Failed to compile geometry shader!\n" << infoLog << std::endl;
return false;
}
}
// compile program
ID = glCreateProgram();
glAttachShader(ID, vertShader);
if (geomSrc != NULL)
glAttachShader(ID, geomShader);
glAttachShader(ID, fragShader);
glLinkProgram(ID);
glGetProgramiv(ID, GL_LINK_STATUS, &success);
if(!success) {
glGetProgramInfoLog(ID, 512, NULL, infoLog);
std::cout << "Failed to link shader program!\n" << infoLog << std::endl;
return false;
}
// clean up
glDeleteShader(vertShader);
glDeleteShader(fragShader);
if (geomSrc != NULL)
glDeleteShader(geomShader);
return true;
}

~Shader() { if (ID != 0) glDeleteProgram(ID); }

void SetFloat(const char* name, float v) const {
glUniform1f(glGetUniformLocation(ID, name), v);
}

void SetFloat2(const char* name, const ImVec2& v) const {
glUniform2f(glGetUniformLocation(ID, name),v.x,v.y);
}

void SetFloat4(const char* name, const ImVec4& v) const {
glUniform4f(glGetUniformLocation(ID, name),v.x,v.y,v.z,v.w);
}

void SetMat4(const char* name, const float* v) const {
glUniformMatrix4fv(glGetUniformLocation(ID, name), 1, GL_FALSE, v);
}

void Use() { glUseProgram(ID); }

GLuint ID = 0;
};
114 changes: 114 additions & 0 deletions common/ThreadPool.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// Copyright (c) 2012 Jakob Progsch, Václav Zeman

// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.

// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:

// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.

// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.

// 3. This notice may not be removed or altered from any source
// distribution.

#include <vector>
#include <queue>
#include <memory>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <future>
#include <functional>
#include <stdexcept>

class ThreadPool {
public:
ThreadPool(size_t);
template<class F, class... Args>
auto enqueue(F&& f, Args&&... args)
-> std::future<typename std::result_of<F(Args...)>::type>;
~ThreadPool();
private:
// need to keep track of threads so we can join them
std::vector< std::thread > workers;
// the task queue
std::queue< std::function<void()> > tasks;

// synchronization
std::mutex queue_mutex;
std::condition_variable condition;
bool stop;
};

// the constructor just launches some amount of workers
inline ThreadPool::ThreadPool(size_t threads)
: stop(false)
{
for(size_t i = 0;i<threads;++i)
workers.emplace_back(
[this]
{
for(;;)
{
std::function<void()> task;

{
std::unique_lock<std::mutex> lock(this->queue_mutex);
this->condition.wait(lock,
[this]{ return this->stop || !this->tasks.empty(); });
if(this->stop && this->tasks.empty())
return;
task = std::move(this->tasks.front());
this->tasks.pop();
}

task();
}
}
);
}

// add new work item to the pool
template<class F, class... Args>
auto ThreadPool::enqueue(F&& f, Args&&... args)
-> std::future<typename std::result_of<F(Args...)>::type>
{
using return_type = typename std::result_of<F(Args...)>::type;

auto task = std::make_shared< std::packaged_task<return_type()> >(
std::bind(std::forward<F>(f), std::forward<Args>(args)...)
);

std::future<return_type> res = task->get_future();
{
std::unique_lock<std::mutex> lock(queue_mutex);

// don't allow enqueueing after stopping the pool
if(stop)
throw std::runtime_error("enqueue on stopped ThreadPool");

tasks.emplace([task](){ (*task)(); });
}
condition.notify_one();
return res;
}

// the destructor joins all threads
inline ThreadPool::~ThreadPool()
{
{
std::unique_lock<std::mutex> lock(queue_mutex);
stop = true;
}
condition.notify_all();
for(std::thread &worker: workers)
worker.join();
}
Loading

0 comments on commit 5d91b64

Please sign in to comment.