diff --git a/include/render/gl.h b/include/render/gl.h index 22cbba4..d3a6e27 100644 --- a/include/render/gl.h +++ b/include/render/gl.h @@ -182,49 +182,36 @@ namespace fp { glUniform4f(getUniformLocation(name), x, y, z, w); } }; - - class shader { + + /** + * a basic computer shader class, contains the functions and resources required to use compute shaders! + */ + class compute_shader : public shader_base { private: - struct IntDefaultedToMinusOne { - GLint i = -1; - }; - // we can have shaders of many types in OpenGL - unsigned int programID = 0; - // but we will only make use of these two for now - unsigned int vertexShaderID = 0; - unsigned int fragmentShaderID = 0; + GLuint shaderID = 0; + public: + explicit compute_shader(const std::string& shader_source, bool loadAsString = true); + + inline void execute(int x, int y, int z) const { + bind(); + glDispatchCompute(x, y, z); + } + + ~compute_shader(); + }; + + class shader : public shader_base { + private: + GLuint vertexShaderID = 0; + GLuint fragmentShaderID = 0; // while these will remain unused. (Webgl2 apparently doesn't support them despite being based on GL4.3? that's a TODO!) - unsigned int geometryShaderID = 0; + GLuint geometryShaderID = 0; // this would be very useful however it is highly unlikely webgl will support it // im leaving some of this stuff in here because I might expand the native application to use some of it. // im trying to keep the web and native versions the same though - unsigned int tessellationShaderID = 0; - std::unordered_map uniformVars; - + GLuint tessellationShaderID = 0; + static unsigned int createShader(const std::string& source, int type); - - inline GLint getUniformLocation(const std::string &name) { - if (uniformVars[name].i != -1) - return uniformVars[name].i; - // caching the result is a lot faster since it won't change after the shader is created. - // TODO: look into this: https://webglfundamentals.org/webgl/lessons/webgl-qna-how-can-i-get-all-the-uniforms-and-uniformblocks.html - int loc = glGetUniformLocation(programID, name.c_str()); - uniformVars[name].i = loc; - return loc; - } - - static inline std::string removeEmptyFirstLines(const std::string& string){ - auto lines = blt::string::split(string, "\n"); - std::string new_source_string; - for (const auto& line : lines) { - if (!line.empty() && !blt::string::contains(line, "\"")) { - new_source_string += line; - new_source_string += "\n"; - } - } - return new_source_string; - } - public: /** * Creates a shader @@ -243,56 +230,16 @@ namespace fp { // used to set location of shared UBOs like the perspective and view matrix void setUniformBlockLocation(const std::string &name, int location) const; - // set various data-types. - inline void setBool(const std::string &name, bool value) { - glUniform1i(getUniformLocation(name), (int) value); - } - - inline void setInt(const std::string &name, int value) { - glUniform1i(getUniformLocation(name), value); - } - - inline void setFloat(const std::string &name, float value) { - glUniform1f(getUniformLocation(name), value); - } - - inline void setMatrix(const std::string &name, blt::mat4x4 &matrix) { - glUniformMatrix4fv(getUniformLocation(name), 1, GL_FALSE, matrix.ptr()); - } - - inline void setVec3(const std::string &name, const blt::vec3 &vec) { - glUniform3f(getUniformLocation(name), vec.x(), vec.y(), vec.z()); - } - - inline void setVec4(const std::string &name, const blt::vec4 &vec) { - // TODO: edit BLT to include a w component - glUniform4f(getUniformLocation(name), vec.x(), vec.y(), vec.z(), vec[3]); - } - - inline void setVec2(const std::string &name, float x, float y) { - glUniform2f(getUniformLocation(name), x, y); - } - - inline void setVec3(const std::string &name, float x, float y, float z) { - glUniform3f(getUniformLocation(name), x, y, z); - } - - inline void setVec4(const std::string &name, float x, float y, float z, float w) { - glUniform4f(getUniformLocation(name), x, y, z, w); - } - - inline void use() const { - glUseProgram(programID); - } + ~shader(); static void updateProjectionMatrix(const blt::mat4x4& projectionMatrix); static void updateOrthographicMatrix(const blt::mat4x4& orthoMatrix); static void updateViewMatrix(const blt::mat4x4& viewMatrix); // returns the perspective view matrix which is calculated per frame. (This is for optimization) static const blt::mat4x4& getPVM(); - - ~shader(); }; + + } diff --git a/include/util/threadpool.h b/include/util/threadpool.h new file mode 100644 index 0000000..126343a --- /dev/null +++ b/include/util/threadpool.h @@ -0,0 +1,23 @@ +/* + * Created by Brett on 22/04/23. + * Licensed under GNU General Public License V3.0 + * See LICENSE file for license detail + */ + +#ifndef FINALPROJECT_THREADPOOL_H +#define FINALPROJECT_THREADPOOL_H + +#include +#include + +namespace blt { + + class thread_pool { + private: + + public: + }; + +} + +#endif //FINALPROJECT_THREADPOOL_H diff --git a/include/world/world.h b/include/world/world.h index ba67244..04e376a 100644 --- a/include/world/world.h +++ b/include/world/world.h @@ -166,7 +166,7 @@ namespace fp { neighbours[Z_NEG] = getChunk(chunk_pos{pos.x, pos.y, pos.z - 1}); } - inline void insertChunk(chunk* chunk) { + inline void spawnChunk(chunk* chunk) { if (chunk == nullptr) return; chunk_storage.insert({chunk->getPos(), chunk}); diff --git a/src/render/gl.cpp b/src/render/gl.cpp index 5e5159e..ba8af8d 100644 --- a/src/render/gl.cpp +++ b/src/render/gl.cpp @@ -65,6 +65,18 @@ namespace fp::_static { } namespace fp { + static inline std::string removeEmptyFirstLines(const std::string& string){ + auto lines = blt::string::split(string, "\n"); + std::string new_source_string; + for (const auto& line : lines) { + if (!line.empty() && !blt::string::contains(line, "\"")) { + new_source_string += line; + new_source_string += "\n"; + } + } + return new_source_string; + } + VAO::VAO() { glGenVertexArrays(1, &vaoID); } @@ -96,7 +108,6 @@ namespace fp { VBOs.insert({-1, vbo}); } - unsigned int shader::createShader(const std::string& source, int type) { const char* shader_code = source.c_str(); // creates a Shader @@ -132,7 +143,7 @@ namespace fp { } shader::shader(const std::string& vertex, const std::string& fragment, const std::string& geometry, bool load_as_string) { - // load shader sources +// load shader sources bool load_geometry = !geometry.empty(); std::string vertex_source = vertex; std::string fragment_source = fragment; @@ -153,7 +164,7 @@ namespace fp { vertexShaderID = createShader(vertex_source, GL_VERTEX_SHADER); fragmentShaderID = createShader(fragment_source, GL_FRAGMENT_SHADER); if (load_geometry) - BLT_ERROR("Unable to load geometry shader because webgl doesn't support it!"); + geometryShaderID = createShader(geometry_source, GL_GEOMETRY_SHADER); // bind them to a program programID = glCreateProgram(); @@ -170,10 +181,10 @@ namespace fp { if (!success) { int log_length = 0; glGetProgramiv(programID, GL_INFO_LOG_LENGTH, &log_length); - + // scoped buffers will delete their memory when they go out of scope. blt::scoped_buffer infoLog{static_cast(log_length + 1)}; - + glGetProgramInfoLog(programID, log_length + 1, nullptr, infoLog.buffer); BLT_ERROR("--- --- --- --- --- --- --- --- ---"); BLT_ERROR("Unable to link program of ID: %d", programID); @@ -186,19 +197,18 @@ namespace fp { } glValidateProgram(programID); - use(); + bind(); setUniformBlockLocation("StandardMatrices", 0); glUseProgram(0); - } void shader::bindAttribute(int attribute, const std::string &name) const { - use(); + bind(); glBindAttribLocation(programID, attribute, name.c_str()); } void shader::setUniformBlockLocation(const std::string &name, int location) const { - use(); + bind(); glUniformBlockBinding(programID, glGetUniformBlockIndex(programID, name.c_str()), location); } diff --git a/src/render/ui/graphics.cpp b/src/render/ui/graphics.cpp index d76cb25..9a623f1 100644 --- a/src/render/ui/graphics.cpp +++ b/src/render/ui/graphics.cpp @@ -184,7 +184,7 @@ namespace fp::graphics { void render() { // generate planes - plane_shader->use(); + plane_shader->bind(); plane_vao->bind(); glDisable(GL_CULL_FACE); while (!plane_render_queue.empty()) { @@ -210,7 +210,7 @@ namespace fp::graphics { //glDisable(GL_CULL_FACE); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - text_shader->use(); + text_shader->bind(); quad_vao->bind(); glEnableVertexAttribArray(0); diff --git a/src/util/threadpool.cpp b/src/util/threadpool.cpp new file mode 100644 index 0000000..aade380 --- /dev/null +++ b/src/util/threadpool.cpp @@ -0,0 +1,6 @@ +/* + * Created by Brett on 22/04/23. + * Licensed under GNU General Public License V3.0 + * See LICENSE file for license detail + */ +#include \ No newline at end of file diff --git a/src/world/world.cpp b/src/world/world.cpp index a9a4236..89eaa61 100644 --- a/src/world/world.cpp +++ b/src/world/world.cpp @@ -125,19 +125,19 @@ std::queue chunks_to_generate{}; void fp::world::update() { auto target_delta = 1000000000 / std::stoi(fp::settings::get("FPS")); - while (fp::window::getCurrentDelta() < target_delta) { + while (fp::window::getCurrentDelta() < target_delta/2) { if (chunks_to_generate.empty()) break; const auto& front = chunks_to_generate.front(); - insertChunk(generateChunk(front)); + spawnChunk(generateChunk(front)); chunks_to_generate.pop(); } } void fp::world::render(fp::shader& shader) { - shader.use(); + shader.bind(); if (fp::window::isKeyPressed(GLFW_KEY_F) && fp::window::keyState()) fp::camera::isFrozen() ? fp::camera::unfreeze() : fp::camera::freeze();