gpu base + opengl

This commit is contained in:
L-Nafaryus 2022-10-03 18:51:34 +05:00
parent 1d51b3bed9
commit 0aabe8ffac
19 changed files with 529 additions and 1 deletions

View File

@ -0,0 +1,30 @@
#include "buffer.hpp"
namespace hpr::gpu
{
Buffer::Buffer() :
Context {DeviceAPI::Unknown},
p_type {BufferType::Undefined},
p_size {0},
p_stride {0}
{}
Buffer::Buffer(DeviceAPI api) :
Context {api},
p_type {BufferType::Undefined},
p_size {0},
p_stride {0}
{}
Buffer::~Buffer()
{}
const BufferType Buffer::type() const
{
return p_type;
}
}

View File

@ -0,0 +1,47 @@
#pragma once
#include "context.hpp"
#include <string>
namespace hpr::gpu
{
class Buffer : public Context
{
public:
enum class BufferType
{
Undefined,
Vertex,
Index,
BufferTypeCount
};
protected:
BufferType p_type;
int p_size;
int p_stride;
public:
// Constructors
Buffer();
Buffer(DeviceAPI api);
virtual ~Buffer();
// Member functions
const int size() const;
const BufferType type() const;
};
} // end namespace hpr::gpu

View File

@ -0,0 +1,34 @@
#include "device.hpp"
#include "opengl/device.hpp"
namespace hpr::gpu
{
Device::Device() :
Context {DeviceAPI::Unknown}
{}
Device::Device(DeviceAPI api) :
Context {api}
{}
Device::~Device()
{}
void Device::create(Device **device, DeviceAPI api)
{
if (device == nullptr)
throw "Invalid parameter";
if (api == DeviceAPI::Unknown)
throw "Invalid parameter";
*device = nullptr;
if (api == DeviceAPI::OpenGL)
*device = new opengl::Device;
else
throw "Unsupported device";
}
}

View File

@ -0,0 +1,91 @@
#pragma once
#include "context.hpp"
#include "buffer.hpp"
#include "shader.hpp"
#include "shader_program.hpp"
#include "texture.hpp"
#include <vector>
namespace hpr::gpu
{
class Device : public Context
{
public:
enum class CullMode
{
Front,
Back,
None
};
protected:
std::vector<Buffer> p_buffers;
std::vector<Shader> p_shaders;
std::vector<ShaderProgram> p_shaderPrograms;
std::vector<Texture> p_textures;
protected:
// Constructors
Device();
Device(DeviceAPI api);
virtual ~Device();
public:
// Global functions
static void create(Device** device, DeviceAPI api);
// Member functions
// Setup
virtual bool initialize() = 0;
virtual bool destroy() = 0;
// State
virtual void faceCulling(bool enableFaceCulling, CullMode faceCullingMode = CullMode::None) = 0;
// Buffers
virtual void createVertexBuffer(Buffer **buffer, int size, char* data) = 0;
virtual void createIndexBuffer(Buffer **buffer, int size, char* data) = 0;
virtual void useVertexBuffer(Buffer* buffer, int stride, int offset);
virtual void useIndexBuffer(Buffer* buffer, int offset);
virtual bool destroyBuffer(Buffer*& buffer);
// Shaders
virtual void createVertexShader(Shader** shader, const std::string& filename, const std::string& label) = 0;
virtual void createFragmentShader(Shader** shader, const std::string& filename, const std::string& label) = 0;
virtual void createGeometryShader(Shader** shader, const std::string& filename, const std::string& label) = 0;
virtual bool destroyShader(Shader*& shader);
// Shader programs
virtual void createShaderProgram(ShaderProgram** program) = 0;
virtual void attachShader(ShaderProgram* program, Shader* shader);
virtual void linkProgram(ShaderProgram* program);
virtual void useShaderProgram(ShaderProgram* program);
virtual void destroyShaderProgram(ShaderProgram*& program, bool withShaders = false);
// Textures
virtual void createTexture(Texture** texture, const std::string& filename) = 0;
virtual void useTexture(Texture* texture, int slot);
virtual void destroyTexture(Texture*& texture);
};
}

View File

@ -0,0 +1,28 @@
#include "buffer.hpp"
namespace hpr::gpu::opengl
{
Buffer::Buffer() :
gpu::Buffer(DeviceAPI::OpenGL),
p_bufferIndex {0},
p_vertexArrayIndex {0}
{}
Buffer::~Buffer()
{}
const int Buffer::target() const
{
switch (p_type)
{
case BufferType::Vertex:
return GL_ARRAY_BUFFER;
case BufferType::Index:
return GL_ELEMENT_INDEX_BUFFER;
}
}
}

View File

@ -0,0 +1,30 @@
#pragma once
#include "../buffer.hpp"
namespace hpr::gpu::opengl
{
class Buffer : public gpu::Buffer
{
friend class Device;
protected:
unsigned int p_bufferIndex;
unsigned int p_vertexArrayIndex;
public:
// Constructors
Buffer();
virtual ~Buffer();
// Member functions
const int target() const;
};
}

View File

@ -0,0 +1,5 @@
//
// Created by L-Nafaryus on 10/3/2022.
//
#include "context.hpp"

View File

@ -0,0 +1,8 @@
//
// Created by L-Nafaryus on 10/3/2022.
//
#ifndef HYPORO_CONTEXT_HPP
#define HYPORO_CONTEXT_HPP
#endif //HYPORO_CONTEXT_HPP

View File

@ -0,0 +1,21 @@
#include "device.hpp"
#include "buffer.hpp
#include "shader.hpp"
#include "shader_program.hpp"
#include "texture.hpp"
namespace hpr::gpu::opengl
{
Device::Device() :
gpu::Device {DeviceAPI::OpenGL},
p_isInitialized {false}
{}
Device::~Device()
{}
}

View File

@ -0,0 +1,27 @@
#pragma once
#include "../device.hpp"
namespace hpr::gpu::opengl
{
class Device : public gpu::Device
{
protected:
bool p_isInitialized;
public:
// Constructors
Device();
~Device();
};
}

View File

@ -0,0 +1,19 @@
//
// Created by L-Nafaryus on 10/3/2022.
//
#include "shader.hpp"
namespace hpr::gpu::opengl
{
Shader::Shader() :
gpu::Shader(DeviceAPI::OpenGL),
p_shaderIndex {0}
{}
Shader::~Shader()
{}
}

View File

@ -0,0 +1,26 @@
#include "../shader.hpp"
namespace hpr::gpu::opengl
{
class Shader : public gpu::Shader
{
friend class Device;
protected:
unsigned int p_shaderIndex;
public:
// Constructors
Shader();
virtual ~Shader();
};
}

View File

@ -0,0 +1,22 @@
#include "shader_program.hpp"
#include "shader.hpp"
namespace hpr::gpu::opengl
{
ShaderProgram::ShaderProgram() :
gpu::ShaderProgram {DeviceAPI::OpenGL},
p_shaderProgramIndex {0}
{}
ShaderProgram::~ShaderProgram()
{}
Shader *ShaderProgram::shader(gpu::Shader::ShaderType type)
{
return static_cast<opengl::Shader*>(p_slots[(size_t)type]);
}
}

View File

@ -0,0 +1,35 @@
#pragma once
#include "../shader_program.hpp"
namespace hpr::gpu::opengl
{
// Forward declarations
class Shader;
//
class ShaderProgram : public gpu::ShaderProgram
{
friend class Device;
protected:
unsigned int p_shaderProgramIndex;
public:
ShaderProgram();
~ShaderProgram();
protected:
Shader* shader(gpu::Shader::ShaderType type);
};
}

View File

@ -0,0 +1,16 @@
#include "texture.hpp"
namespace hpr::gpu::opengl
{
Texture::Texture() :
gpu::Texture {DeviceAPI::OpenGL},
p_textureIndex {0}
{}
Texture::~Texture()
{}
}

View File

@ -0,0 +1,25 @@
#pragma once
#include "../texture.hpp"
namespace hpr::gpu::opengl
{
class Texture : public gpu::Texture
{
friend class Device;
protected:
unsigned int p_textureIndex;
public:
Texture();
~Texture();
};
}

View File

@ -9,7 +9,7 @@
namespace hpr::gpu namespace hpr::gpu
{ {
class ShaderProgram : Context class ShaderProgram : public Context
{ {
protected: protected:

View File

@ -0,0 +1,26 @@
#include "texture.hpp"
namespace hpr::gpu
{
Texture::Texture() :
Context {DeviceAPI::Unknown},
p_filename {},
p_width {0},
p_height {0}
{}
Texture::Texture(DeviceAPI api) :
Context {api},
p_filename {},
p_width {0},
p_height {0}
{}
Texture::~Texture()
{}
}

View File

@ -0,0 +1,38 @@
#pragma once
#include "context.hpp"
#include <string>
namespace hpr::gpu
{
class Texture : public Context
{
protected:
std::string p_filename;
int p_width;
int p_height;
public:
// Constructors
Texture();
Texture(DeviceAPI api);
virtual ~Texture();
// Member functions
const std::string filename() const();
const int width() const;
const int height() const;
};
} // end namespace hpr::gpu