2026-03-17 16:47:19 +01:00
|
|
|
#include <pd_system/gfx_opengl2.hpp>
|
2026-03-16 15:19:12 +01:00
|
|
|
|
2026-03-17 16:47:19 +01:00
|
|
|
#if defined(PD_ENABLE_OPENGL2)
|
|
|
|
|
#include <glad/glad.h>
|
2026-03-16 15:19:12 +01:00
|
|
|
|
2026-03-17 16:47:19 +01:00
|
|
|
#include <pd/drivers/drivers.hpp>
|
|
|
|
|
#include <pd_system/gl-helper.hpp>
|
2026-03-16 17:33:46 +01:00
|
|
|
|
2026-03-16 15:19:12 +01:00
|
|
|
namespace PD {
|
2026-03-17 16:47:19 +01:00
|
|
|
const char* GfxOpenGL2::pVertCode = R"(
|
2026-03-16 15:19:12 +01:00
|
|
|
#version 120
|
|
|
|
|
|
|
|
|
|
attribute vec2 pos;
|
|
|
|
|
attribute vec2 uv;
|
|
|
|
|
attribute vec4 color;
|
|
|
|
|
|
|
|
|
|
varying vec2 oUV;
|
|
|
|
|
varying vec4 oColor;
|
|
|
|
|
|
|
|
|
|
// Probably forgot about this matrix and
|
|
|
|
|
// searched hours for why the rendering isn't working :/
|
|
|
|
|
uniform mat4 projection;
|
|
|
|
|
|
|
|
|
|
void main() {
|
|
|
|
|
gl_Position = projection*vec4(pos, 0.0, 1.0);
|
|
|
|
|
oUV = uv;
|
|
|
|
|
oColor = color;
|
|
|
|
|
}
|
|
|
|
|
)";
|
|
|
|
|
|
2026-03-17 16:47:19 +01:00
|
|
|
const char* GfxOpenGL2::pFragCode = R"(
|
2026-03-16 15:19:12 +01:00
|
|
|
#version 120
|
|
|
|
|
|
|
|
|
|
varying vec2 oUV;
|
|
|
|
|
varying vec4 oColor;
|
|
|
|
|
|
|
|
|
|
uniform sampler2D tex;
|
|
|
|
|
uniform bool alfa;
|
|
|
|
|
|
|
|
|
|
void main() {
|
|
|
|
|
vec4 tc = texture2D(tex, oUV);
|
|
|
|
|
if (alfa) {
|
|
|
|
|
gl_FragColor = vec4(oColor.rgb, tc.a * oColor.a);
|
|
|
|
|
} else {
|
|
|
|
|
gl_FragColor = tc * oColor;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
)";
|
|
|
|
|
|
2026-03-17 16:47:19 +01:00
|
|
|
void GfxOpenGL2::pSetupShaderAttribs(u32 shader) {
|
2026-03-16 15:19:12 +01:00
|
|
|
GLint _pos = glGetAttribLocation(shader, "pos");
|
|
|
|
|
GLint _uv = glGetAttribLocation(shader, "uv");
|
|
|
|
|
GLint _color = glGetAttribLocation(shader, "color");
|
|
|
|
|
glVertexAttribPointer(_pos, 2, GL_FLOAT, GL_FALSE, sizeof(PD::Li::Vertex),
|
|
|
|
|
(void*)offsetof(PD::Li::Vertex, pos));
|
|
|
|
|
glEnableVertexAttribArray(_pos);
|
|
|
|
|
|
|
|
|
|
glVertexAttribPointer(_uv, 2, GL_FLOAT, GL_FALSE, sizeof(PD::Li::Vertex),
|
|
|
|
|
(void*)offsetof(PD::Li::Vertex, uv));
|
|
|
|
|
glEnableVertexAttribArray(_uv);
|
|
|
|
|
|
|
|
|
|
glVertexAttribPointer(_color, 4, GL_UNSIGNED_BYTE, GL_TRUE,
|
|
|
|
|
sizeof(PD::Li::Vertex),
|
|
|
|
|
(void*)offsetof(PD::Li::Vertex, color));
|
|
|
|
|
glEnableVertexAttribArray(_color);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-17 16:47:19 +01:00
|
|
|
void GfxOpenGL2::SysInit() {
|
|
|
|
|
pShader = CreateShaderProgram(pVertCode, pFragCode);
|
2026-03-16 15:19:12 +01:00
|
|
|
glUseProgram(pShader);
|
|
|
|
|
glGenBuffers(1, &VBO);
|
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
|
|
|
|
glGenBuffers(1, &IBO);
|
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
|
|
|
|
|
|
2026-03-17 16:47:19 +01:00
|
|
|
pSetupShaderAttribs(pShader);
|
2026-03-16 15:19:12 +01:00
|
|
|
pLocTex = glGetUniformLocation(pShader, "tex");
|
|
|
|
|
pLocAlfa = glGetUniformLocation(pShader, "alfa");
|
|
|
|
|
pLocProjection = glGetUniformLocation(pShader, "projection");
|
|
|
|
|
|
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
2026-03-16 17:33:46 +01:00
|
|
|
PDLOG(
|
2026-03-17 16:47:19 +01:00
|
|
|
"GfxOpenGL2::SysInit():\n pShader = {}\n pLocTex = {}\n pLocAlfa = "
|
2026-03-16 17:33:46 +01:00
|
|
|
"{}\n pLocProjection = {}\n VBO = {}\n IBO = {}",
|
|
|
|
|
pShader, pLocTex, pLocAlfa, pLocProjection, VBO, IBO);
|
2026-03-16 15:19:12 +01:00
|
|
|
}
|
|
|
|
|
|
2026-03-17 16:47:19 +01:00
|
|
|
void GfxOpenGL2::SysDeinit() {
|
2026-03-16 15:19:12 +01:00
|
|
|
glDeleteBuffers(1, &VBO);
|
|
|
|
|
glDeleteBuffers(1, &IBO);
|
2026-03-17 16:47:19 +01:00
|
|
|
PDLOG("GfxOpenGL2::SysDeinit()");
|
2026-03-16 15:19:12 +01:00
|
|
|
}
|
|
|
|
|
|
2026-03-17 16:47:19 +01:00
|
|
|
void GfxOpenGL2::Submit(size_t count, size_t start) {
|
2026-03-16 17:33:46 +01:00
|
|
|
BindTexture(CurrentTex);
|
|
|
|
|
glUseProgram(pShader);
|
|
|
|
|
glUniformMatrix4fv(pLocProjection, 1, GL_FALSE, Projection.m.data());
|
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, CurrentVertex * sizeof(PD::Li::Vertex),
|
|
|
|
|
GetVertexBufPtr(0), GL_DYNAMIC_DRAW);
|
|
|
|
|
|
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
|
|
|
|
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, CurrentIndex * sizeof(PD::u16),
|
|
|
|
|
GetIndexBufPtr(0), GL_DYNAMIC_DRAW);
|
|
|
|
|
|
2026-03-17 16:47:19 +01:00
|
|
|
pSetupShaderAttribs(pShader);
|
|
|
|
|
GLint ibo = 0;
|
|
|
|
|
glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &ibo);
|
|
|
|
|
glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_SHORT,
|
2026-03-16 17:33:46 +01:00
|
|
|
reinterpret_cast<void*>(start * sizeof(u16)));
|
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
|
|
|
|
BindTexture(0);
|
2026-03-16 15:19:12 +01:00
|
|
|
}
|
|
|
|
|
|
2026-03-17 16:47:19 +01:00
|
|
|
void GfxOpenGL2::BindTexture(TextureID id) {
|
2026-03-16 15:19:12 +01:00
|
|
|
glActiveTexture(GL_TEXTURE0);
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, (GLuint)id);
|
|
|
|
|
glUniform1i(pLocTex, 0);
|
|
|
|
|
GLint fmt = 0;
|
|
|
|
|
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT, &fmt);
|
|
|
|
|
glUniform1i(pLocAlfa, fmt == GL_ALPHA);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-17 16:47:19 +01:00
|
|
|
void GfxOpenGL2::SysReset() {
|
2026-03-16 17:33:46 +01:00
|
|
|
glEnable(GL_BLEND);
|
|
|
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
|
|
|
|
}
|
2026-03-16 15:19:12 +01:00
|
|
|
|
2026-03-17 16:47:19 +01:00
|
|
|
TextureID GfxOpenGL2::LoadTexture(const std::vector<PD::u8>& pixels, int w,
|
|
|
|
|
int h, TextureFormat type,
|
|
|
|
|
TextureFilter filter) {
|
2026-03-16 17:33:46 +01:00
|
|
|
GLuint texID;
|
|
|
|
|
glGenTextures(1, &texID);
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, texID);
|
|
|
|
|
|
|
|
|
|
// Set base format (Always using RGBA as base)
|
|
|
|
|
GLenum fmt = GL_RGBA;
|
2026-03-17 16:47:19 +01:00
|
|
|
if (type == TextureFormat::RGB24) {
|
2026-03-16 17:33:46 +01:00
|
|
|
fmt = GL_RGB;
|
2026-03-17 16:47:19 +01:00
|
|
|
} else if (type == TextureFormat::A8) {
|
2026-03-16 17:33:46 +01:00
|
|
|
fmt = GL_ALPHA;
|
2026-03-17 16:47:19 +01:00
|
|
|
}
|
2026-03-16 17:33:46 +01:00
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, fmt, w, h, 0, fmt, GL_UNSIGNED_BYTE,
|
|
|
|
|
pixels.data());
|
|
|
|
|
if (filter == TextureFilter::Linear) {
|
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
|
} else if (filter == TextureFilter::Nearest) {
|
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
|
|
|
}
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
2026-03-17 16:47:19 +01:00
|
|
|
PDLOG("GfxOpenGL2::LoadTexture -> [{}] {}, [{}, {}]", PD::ivec2(w, h), texID,
|
|
|
|
|
type, filter);
|
2026-03-16 17:33:46 +01:00
|
|
|
return texID;
|
2026-03-16 15:19:12 +01:00
|
|
|
}
|
|
|
|
|
|
2026-03-17 16:47:19 +01:00
|
|
|
void GfxOpenGL2::DeleteTexture(const TextureID& tex) {
|
2026-03-16 15:19:12 +01:00
|
|
|
GLuint tex_ = tex;
|
|
|
|
|
glDeleteTextures(1, &tex_);
|
|
|
|
|
}
|
2026-03-17 16:47:19 +01:00
|
|
|
} // namespace PD
|
|
|
|
|
#else
|
|
|
|
|
namespace PD {
|
|
|
|
|
void GfxOpenGL2::SysInit() {
|
|
|
|
|
PDLOG(
|
|
|
|
|
"GfxOpenGL2::SysInit: OpenGL2 Driver is not included in "
|
|
|
|
|
"palladium-system");
|
|
|
|
|
}
|
|
|
|
|
void GfxOpenGL2::SysDeinit() {}
|
|
|
|
|
void GfxOpenGL2::Submit(size_t count, size_t start) {}
|
|
|
|
|
void GfxOpenGL2::BindTexture(TextureID id) {}
|
|
|
|
|
void GfxOpenGL2::SysReset() {}
|
|
|
|
|
TextureID GfxOpenGL2::LoadTexture(const std::vector<PD::u8>& pixels, int w,
|
|
|
|
|
int h, TextureFormat type,
|
|
|
|
|
TextureFilter filter) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
void GfxOpenGL2::DeleteTexture(const TextureID& tex) {}
|
|
|
|
|
void GfxOpenGL2::pSetupShaderAttribs(u32 shader) {}
|
|
|
|
|
} // namespace PD
|
|
|
|
|
#endif
|