-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
28,285 additions
and
2 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
Oops, something went wrong.