Remove texture and rglcore

This commit is contained in:
Tulpen 2023-06-28 22:43:01 +02:00
parent 1cca66b66f
commit 5fc2069fc0
No known key found for this signature in database
GPG key ID: 12294D73B907E784
9 changed files with 55 additions and 303 deletions

View file

@ -4,7 +4,6 @@
#include "../math/types.hpp" #include "../math/types.hpp"
#include "../resources/resource.hpp" #include "../resources/resource.hpp"
#include "../resources/texture.hpp"
#include "../resources/shader.hpp" #include "../resources/shader.hpp"
#include "../callback.hpp" #include "../callback.hpp"
@ -18,7 +17,7 @@ namespace hibis {
virtual void renderCurrent() = 0; virtual void renderCurrent() = 0;
virtual void drawText(Resource* resource, std::string text, IntVec2 pos, Color color) = 0; virtual void drawText(Resource* resource, std::string text, IntVec2 pos, Color color) = 0;
virtual void drawTexture(Texture* resource, IntRect size) = 0; virtual void drawTexture(Resource* resource, IntRect size) = 0;
virtual void useShader(Shader* shader, Point2D points[3]) = 0; virtual void useShader(Shader* shader, Point2D points[3]) = 0;
virtual void stopUsingShaders() = 0; virtual void stopUsingShaders() = 0;
@ -36,8 +35,8 @@ namespace hibis {
virtual void setWindowTitle(std::string title) = 0; virtual void setWindowTitle(std::string title) = 0;
bool mKeepOpen = true; bool mKeepOpen = true;
protected:
LoggerCallback mLogger; LoggerCallback mLogger;
protected:
bool mIsWireframeMode = false; bool mIsWireframeMode = false;
}; };
} }

View file

@ -1,14 +0,0 @@
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image/stb_image.h>
#include "texture.hpp"
namespace hibis {
Texture::Texture(const char* path) : mData(), mImageWidth(0), mImageHeight(0), mImageChannels(0) {
mData = stbi_load(path, &mImageWidth, &mImageHeight, &mImageChannels, 0);
}
Texture::~Texture() {
stbi_image_free(mData);
}
}

View file

@ -1,17 +0,0 @@
#pragma once
#include <vector>
#include "resource.hpp"
#include "../pragmautil.hpp"
namespace hibis {
TODO("Make this function fully")
class Texture : public Resource {
public:
Texture(const char* path);
~Texture();
unsigned char* mData;
int mImageWidth, mImageHeight, mImageChannels;
};
}

View file

@ -17,11 +17,10 @@ include_dirs = include_directories('./external')
# Files # Files
libhibis_src_core = files('core/engine/engine.cpp') libhibis_src_core = files('core/engine/engine.cpp')
libhibis_src_renderer = files('core/renderer/renderer.cpp') libhibis_src_renderer = files('core/renderer/renderer.cpp')
libhibis_src_resources = files('core/resources/texture.cpp', 'core/resources/font.cpp', 'core/resources/shader.cpp') libhibis_src_resources = files('core/resources/font.cpp', 'core/resources/shader.cpp')
libhibis_src = [libhibis_src_core, libhibis_src_renderer, libhibis_src_resources] libhibis_src = [libhibis_src_core, libhibis_src_renderer, libhibis_src_resources]
libhibis_rwgpu_src = files('renderer/rwgpu/rwgpu.cpp') libhibis_rwgpu_src = files('renderer/rwgpu/rwgpu.cpp')
libhibis_rglcore_src = files('renderer/rglcore/rglcore.cpp')
libhibis_test_src = files('test/app.cpp') libhibis_test_src = files('test/app.cpp')
# Dependencies # Dependencies
@ -37,6 +36,5 @@ libfreetype2 = dependency('freetype2')
# Compile # Compile
libhibis = library('hibis', libhibis_src, include_directories: include_dirs, dependencies: [libfreetype2]) libhibis = library('hibis', libhibis_src, include_directories: include_dirs, dependencies: [libfreetype2])
libhibis_rglcore = library('hibis_rglcore', libhibis_rglcore_src, include_directories: [include_dirs, './core'], link_with: libhibis, dependencies: [libfreetype2, libgl, libglfw, libglew, libfmt])
libhibis_rwgpu = library('hibis_rwgpu', libhibis_rwgpu_src, include_directories: [include_dirs, './core'], link_with: [libhibis, libwgpu_glfw], dependencies: [libfreetype2, libglfw, libfmt, libwgpu]) libhibis_rwgpu = library('hibis_rwgpu', libhibis_rwgpu_src, include_directories: [include_dirs, './core'], link_with: [libhibis, libwgpu_glfw], dependencies: [libfreetype2, libglfw, libfmt, libwgpu])
hibistest = executable('hibistest.exec', libhibis_test_src, include_directories: [include_dirs, './core', './renderer/rwgpu'], link_with: [libhibis, libhibis_rwgpu], dependencies: [libfreetype2, libglfw, libfmt, libwgpu]) hibistest = executable('hibistest.exec', libhibis_test_src, include_directories: [include_dirs, './core', './renderer/rwgpu'], link_with: [libhibis, libhibis_rwgpu], dependencies: [libfreetype2, libglfw, libfmt, libwgpu])

View file

@ -1,201 +0,0 @@
#include <fmt/format.h>
#include <fstream>
#include "rglcore.hpp"
namespace hibis::rglcore {
RGLCore::RGLCore(std::string title, IntVec2 size, LoggerCallback callback) : Renderer(callback), mTextures() {
mLogger(Information, "Preparing GLFW3...");
if (!glfwInit()) {
mLogger(Fatal, "GLFW couldn't be initialised!");
exit(1);
}
mLogger(Information, "Creating GLFWwindow...");
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
mWindow = glfwCreateWindow(size.x, size.y, title.c_str(), NULL, NULL);
if (!mWindow) {
mLogger(Fatal, "Window could not be created!");
exit(1);
}
glfwMakeContextCurrent(mWindow);
mLogger(Information, "Preparing GLEW...");
glewExperimental = GL_FALSE;
GLenum err = glewInit();
if (err != GLEW_OK) {
mLogger(Fatal, "GLEW could not be created due to GLEW err " + std::to_string(err));
exit(err);
}
mLogger(Information, "Setting viewport...");
glViewport(0, 0, size.x, size.y);
mLogger(Information, "Finished setting up RGLCore!");
}
RGLCore::~RGLCore() {
glfwDestroyWindow(mWindow);
glfwTerminate();
}
// Draw
void RGLCore::clearScreen(Color col) {
float r = (float)(col.r) / 255, g = (float)(col.g) / 255, b = (float)(col.b) / 255, a = (float)(col.a) / 255;
glClearColor(r, g, b, a);
glClear(GL_COLOR_BUFFER_BIT);
}
void RGLCore::renderCurrent() {
glfwSwapBuffers(mWindow);
}
void RGLCore::drawText(Resource* resource, std::string text, IntVec2 pos, Color color) {}
void RGLCore::drawTexture(Texture* resource, IntRect size) {
TODO("finish this")
return;
unsigned int glTextureID;
if (mTextures.find(resource) != mTextures.end()) {
glTextureID = mTextures[resource];
} else {
glGenTextures(1, &glTextureID);
glBindTexture(GL_TEXTURE_2D, glTextureID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, resource->mImageWidth, resource->mImageHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, resource->mData);
glGenerateMipmap(GL_TEXTURE_2D);
}
}
void RGLCore::useShader(Shader* shader, Point2D points[3]) {
glUseProgram(shader->mShaderProgram);
glBindVertexArray(shader->mShaderVAO);
glDrawArrays(GL_TRIANGLES, 0, 3);
}
void RGLCore::stopUsingShaders() {
glUseProgram(0);
}
// Pre and Post draw
void RGLCore::preDraw() {}
void RGLCore::postDraw() {}
void RGLCore::update() {
mKeepOpen = !glfwWindowShouldClose(mWindow);
glfwPollEvents();
}
void RGLCore::compileShader(Shader* shader) {
if (shader->mShaderProgram != 0) return;
char infoLog[512];
int success;
unsigned int shaderIDs[2] = {0, 0};
mLogger(Information, fmt::format("Compiling shader {}", shader->mShaderPaths[0]));
shaderIDs[0] = glCreateShader(GL_VERTEX_SHADER);
const std::string vertShaderStr = loadFile(shader->mShaderPaths[1]);
const char* vertShader = vertShaderStr.c_str();
glShaderSource(shaderIDs[0], 1, &vertShader, NULL);
glCompileShader(shaderIDs[0]);
glGetShaderiv(shaderIDs[0], GL_COMPILE_STATUS, &success);
if(!success) {
glGetShaderInfoLog(shaderIDs[0], 512, NULL, infoLog);
printf("%s", infoLog);
mLogger(Error, fmt::format("Couldn't compile shader '{}'! what: {}", shader->mShaderPaths[0], std::string(infoLog)));
}
mLogger(Information, fmt::format("Compiling shader {}", shader->mShaderPaths[1]));
shaderIDs[1] = glCreateShader(GL_FRAGMENT_SHADER);
const std::string fragShaderStr = loadFile(shader->mShaderPaths[1]);
const char* fragShader = fragShaderStr.c_str();
glShaderSource(shaderIDs[1], 1, &fragShader, NULL);
glCompileShader(shaderIDs[1]);
glGetShaderiv(shaderIDs[1], GL_COMPILE_STATUS, &success);
if(!success) {
glGetShaderInfoLog(shaderIDs[1], 512, NULL, infoLog);
mLogger(Error, fmt::format("Couldn't compile shader '{}'! what: {}", shader->mShaderPaths[1], std::string(infoLog)));
}
shader->mShaderProgram = glCreateProgram();
glAttachShader(shader->mShaderProgram, shaderIDs[0]);
glAttachShader(shader->mShaderProgram, shaderIDs[1]);
glLinkProgram(shader->mShaderProgram);
glGetProgramiv(shader->mShaderProgram, GL_LINK_STATUS, &success);
if (!success) {
TODO("what the fuck, this isn't helpful")
glGetShaderInfoLog(shader->mShaderProgram, 512, NULL, infoLog);
mLogger(Error, fmt::format("Couldn't link shader! what: {}", std::string(infoLog)));
}
else mLogger(Information, "Linked shader.");
glDeleteShader(shaderIDs[0]);
glDeleteShader(shaderIDs[1]);
GLfloat vertices[] = {
-0.5f, -0.5f * float(sqrt(3)) / 3, 0.0f, // Lower left corner
0.5f, -0.5f * float(sqrt(3)) / 3, 0.0f, // Lower right corner
0.0f, 0.5f * float(sqrt(3)) * 2 / 3, 0.0f // Upper corner
};
glGenVertexArrays(1, &shader->mShaderVAO);
glGenBuffers(1, &shader->mShaderVBO);
glBindVertexArray(shader->mShaderVAO);
glBindBuffer(GL_ARRAY_BUFFER, shader->mShaderVBO);
glBindBuffer(GL_ARRAY_BUFFER, shader->mShaderVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
mLogger(Information, "Cleaned up leftover shader objects.");
}
void RGLCore::toggleWireframe() {
if (mIsWireframeMode) glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
else glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
mIsWireframeMode = !mIsWireframeMode;
}
void RGLCore::setWindowTitle(std::string title) {
glfwSetWindowTitle(mWindow, title.c_str());
}
std::string RGLCore::loadFile(std::string path) {
std::string text, line;
std::ifstream textFile(path);
if (textFile.is_open()) {
while (getline(textFile, line)) {
text += line + '\n';
}
textFile.close();
} else {
text = "";
mLogger(Error, fmt::format("Couldn't load file '{}'", path));
}
return text;
}
}

View file

@ -1,46 +0,0 @@
#pragma once
#include <unordered_map>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <ft2build.h>
#include FT_FREETYPE_H
#include <callback.hpp>
#include <renderer/renderer.hpp>
namespace hibis::rglcore {
class RGLCore : public Renderer {
public:
RGLCore(std::string title, IntVec2 size, LoggerCallback callback);
~RGLCore();
// Draw
void clearScreen(Color col = Color {0, 0, 0, 255}) override;
void renderCurrent() override;
void drawText(Resource* resource, std::string text, IntVec2 pos, Color color) override;
void drawTexture(Texture* resource, IntRect size) override;
void useShader(Shader* shader, Point2D points[3]) override;
void stopUsingShaders() override;
// Pre and Post draw
void preDraw() override;
void postDraw() override;
// Update
void update() override;
// Util
void compileShader(Shader* shader) override;
void toggleWireframe() override;
void setWindowTitle(std::string title) override;
private:
GLFWwindow* mWindow;
std::unordered_map<Texture*, unsigned int> mTextures;
std::string loadFile(std::string path);
};
}

View file

@ -5,8 +5,17 @@
#include <glfw3webgpu/glfw3webgpu.h> #include <glfw3webgpu/glfw3webgpu.h>
#include <pragmautil.hpp>
#include "rwgpu.hpp" #include "rwgpu.hpp"
void onWindowResize(GLFWwindow* window, int width, int height) {
auto that = reinterpret_cast<hibis::rwgpu::RWGPU*>(glfwGetWindowUserPointer(window));
// Resize that so that this can work
if (that != nullptr) that->resize(width, height);
}
namespace hibis::rwgpu { namespace hibis::rwgpu {
RWGPU::RWGPU(std::string title, IntVec2 size, LoggerCallback callback) : Renderer(callback) { RWGPU::RWGPU(std::string title, IntVec2 size, LoggerCallback callback) : Renderer(callback) {
wgpu::InstanceDescriptor instanceDescriptor = {}; wgpu::InstanceDescriptor instanceDescriptor = {};
@ -39,6 +48,8 @@ namespace hibis::rwgpu {
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
mWindow = glfwCreateWindow(size.x, size.y, title.c_str(), NULL, NULL); mWindow = glfwCreateWindow(size.x, size.y, title.c_str(), NULL, NULL);
glfwSetWindowUserPointer(mWindow, this);
glfwSetFramebufferSizeCallback(mWindow, onWindowResize);
mWebGPUSurface = glfwGetWGPUSurface(mWebGPUInstance, mWindow); mWebGPUSurface = glfwGetWGPUSurface(mWebGPUInstance, mWindow);
@ -69,18 +80,7 @@ namespace hibis::rwgpu {
// }; // };
// wgpuQueueOnSubmittedWorkDone(mWebGPUQueue, onQueueWorkDone, nullptr /* pUserData */); // wgpuQueueOnSubmittedWorkDone(mWebGPUQueue, onQueueWorkDone, nullptr /* pUserData */);
WGPUSwapChainDescriptor swapChainDesc = {}; setupSwapChain(size.x, size.y);
swapChainDesc.nextInChain = nullptr;
swapChainDesc.width = size.x;
swapChainDesc.height = size.y;
WGPUTextureFormat swapChainFormat = wgpuSurfaceGetPreferredFormat(mWebGPUSurface, mWebGPUAdapter);
swapChainDesc.format = swapChainFormat;
swapChainDesc.usage = WGPUTextureUsage_RenderAttachment;
swapChainDesc.presentMode = WGPUPresentMode_Fifo;
mWebGPUSwapChain = wgpuDeviceCreateSwapChain(mWebGPUDevice, mWebGPUSurface, &swapChainDesc);
} }
RWGPU::~RWGPU() { RWGPU::~RWGPU() {
@ -166,6 +166,25 @@ namespace hibis::rwgpu {
return userData.device; return userData.device;
} }
void RWGPU::setupSwapChain(unsigned int width, unsigned int height) {
// Drop swap chain if it currently exists
if (mWebGPUSwapChain) mWebGPUSwapChain.drop();
// Remake swap chain
WGPUSwapChainDescriptor swapChainDesc = {};
swapChainDesc.nextInChain = nullptr;
swapChainDesc.width = width;
swapChainDesc.height = height;
WGPUTextureFormat swapChainFormat = wgpuSurfaceGetPreferredFormat(mWebGPUSurface, mWebGPUAdapter);
swapChainDesc.format = swapChainFormat;
swapChainDesc.usage = WGPUTextureUsage_RenderAttachment;
swapChainDesc.presentMode = WGPUPresentMode_Fifo;
mWebGPUSwapChain = wgpuDeviceCreateSwapChain(mWebGPUDevice, mWebGPUSurface, &swapChainDesc);
}
void RWGPU::clearScreen(Color col) { void RWGPU::clearScreen(Color col) {
mCurrentClearColor = {col.r, col.g, col.b, col.a}; mCurrentClearColor = {col.r, col.g, col.b, col.a};
} }
@ -179,7 +198,9 @@ namespace hibis::rwgpu {
WGPUCommandEncoderDescriptor encoderDesc = {}; WGPUCommandEncoderDescriptor encoderDesc = {};
encoderDesc.nextInChain = nullptr; encoderDesc.nextInChain = nullptr;
encoderDesc.label = "HibisDrawCommandEncoder"; encoderDesc.label = "HibisDrawCommandEncoder";
WGPUCommandEncoder encoder = wgpuDeviceCreateCommandEncoder(mWebGPUDevice, &encoderDesc); WGPUCommandEncoder encoder = mWebGPUDevice.createCommandEncoder(encoderDesc);
TODO("Implement Deferred Renderer")
WGPURenderPassDescriptor renderPassDesc = {}; WGPURenderPassDescriptor renderPassDesc = {};
@ -199,7 +220,7 @@ namespace hibis::rwgpu {
renderPassDesc.timestampWrites = nullptr; renderPassDesc.timestampWrites = nullptr;
renderPassDesc.nextInChain = nullptr; renderPassDesc.nextInChain = nullptr;
WGPURenderPassEncoder renderPass = wgpuCommandEncoderBeginRenderPass(encoder, &renderPassDesc); wgpu::RenderPassEncoder renderPass = wgpuCommandEncoderBeginRenderPass(encoder, &renderPassDesc);
wgpuRenderPassEncoderEnd(renderPass); wgpuRenderPassEncoderEnd(renderPass);
WGPUCommandBufferDescriptor cmdBufferDescriptor = {}; WGPUCommandBufferDescriptor cmdBufferDescriptor = {};
@ -213,7 +234,7 @@ namespace hibis::rwgpu {
} }
void RWGPU::drawText(Resource* resource, std::string text, IntVec2 pos, Color color) {} void RWGPU::drawText(Resource* resource, std::string text, IntVec2 pos, Color color) {}
void RWGPU::drawTexture(Texture* resource, IntRect size) {} void RWGPU::drawTexture(Resource* resource, IntRect size) {}
void RWGPU::useShader(Shader* shader, Point2D points[3]) {} void RWGPU::useShader(Shader* shader, Point2D points[3]) {}
void RWGPU::stopUsingShaders() {} void RWGPU::stopUsingShaders() {}
@ -228,5 +249,14 @@ namespace hibis::rwgpu {
void RWGPU::compileShader(Shader* shader) {} void RWGPU::compileShader(Shader* shader) {}
void RWGPU::toggleWireframe() {} void RWGPU::toggleWireframe() {}
void RWGPU::setWindowTitle(std::string title) {}
void RWGPU::setWindowTitle(std::string title) {
glfwSetWindowTitle(mWindow, title.c_str());
}
void RWGPU::resize(unsigned int width, unsigned int height) {
if (width < 0 || height < 0) return;
glfwSetWindowSize(mWindow, width, height);
setupSwapChain(width, height);
}
} }

View file

@ -19,7 +19,7 @@ namespace hibis::rwgpu {
void renderCurrent() override; void renderCurrent() override;
void drawText(Resource* resource, std::string text, IntVec2 pos, Color color) override; void drawText(Resource* resource, std::string text, IntVec2 pos, Color color) override;
void drawTexture(Texture* resource, IntRect size) override; void drawTexture(Resource* resource, IntRect size) override;
void useShader(Shader* shader, Point2D points[3]) override; void useShader(Shader* shader, Point2D points[3]) override;
void stopUsingShaders() override; void stopUsingShaders() override;
@ -35,12 +35,15 @@ namespace hibis::rwgpu {
void compileShader(Shader* shader) override; void compileShader(Shader* shader) override;
void toggleWireframe() override; void toggleWireframe() override;
void setWindowTitle(std::string title) override; void setWindowTitle(std::string title) override;
void resize(unsigned int width, unsigned int height);
std::vector<WGPUFeatureName> mWebGPUFeatures; std::vector<WGPUFeatureName> mWebGPUFeatures;
private: private:
WGPUAdapter requestAdapter(WGPURequestAdapterOptions const * options); WGPUAdapter requestAdapter(WGPURequestAdapterOptions const * options);
WGPUDevice requestDevice(WGPUDeviceDescriptor const * descriptor); WGPUDevice requestDevice(WGPUDeviceDescriptor const * descriptor);
void setupSwapChain(unsigned int width, unsigned int height);
GLFWwindow* mWindow; GLFWwindow* mWindow;
wgpu::Instance mWebGPUInstance = nullptr; wgpu::Instance mWebGPUInstance = nullptr;
wgpu::Adapter mWebGPUAdapter = nullptr; wgpu::Adapter mWebGPUAdapter = nullptr;

View file

@ -44,7 +44,7 @@ void logger(LoggingSeverity severity, std::string message) {
int main() { int main() {
logger(Information, fmt::format("PWD: {}", std::getenv("PWD"))); logger(Information, fmt::format("PWD: {}", std::getenv("PWD")));
RWGPU renderer = RWGPU("test", IntVec2 {800, 600}, &logger); RWGPU renderer = RWGPU("test", IntVec2 {800, 800}, &logger);
Engine engine = Engine(&renderer, &logger); Engine engine = Engine(&renderer, &logger);
/* /*
@ -66,7 +66,7 @@ int main() {
uint size = 16; uint size = 16;
uint f = 0; uint f = 0;
Point2D points[3] = {{-0.5f, -0.5f}, {0.5f, -0.5f}, {0.0f, 0.5f}}; //Point2D points[3] = {{-0.5f, -0.5f}, {0.5f, -0.5f}, {0.0f, 0.5f}};
*/ */
logger(Information, "Started Hibis test app! BEHOLD: Colours."); logger(Information, "Started Hibis test app! BEHOLD: Colours.");
while (renderer.mKeepOpen) { while (renderer.mKeepOpen) {