Add pdsg (palladium spirv-generator)

- remove glslang from pd-system
- use spirv blobs instead of glsl 460
- Use Legacy shaders for OpenGL2 backend
- add devnotes
This commit is contained in:
2026-03-18 21:21:00 +01:00
parent 7d89ab1c47
commit 71563e8979
14 changed files with 409 additions and 83 deletions

View File

@@ -6,11 +6,49 @@
#include <pd/drivers/drivers.hpp>
#include <pd_system/gl-helper.hpp>
#include <pd_system/shaders.hpp>
#include <pd_system/spirv-helper.hpp>
namespace PD {
const char* GfxOpenGL2::pVertCode = R"(
#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;
}
)";
const char* GfxOpenGL2::pFragCode = R"(
#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;
}
}
)";
void GfxOpenGL2::pSetupShaderAttribs(u32 shader) {
GLint _pos = glGetAttribLocation(shader, "pos");
GLint _uv = glGetAttribLocation(shader, "uv");
@@ -30,17 +68,7 @@ void GfxOpenGL2::pSetupShaderAttribs(u32 shader) {
}
void GfxOpenGL2::SysInit() {
SpirvHelper::Init();
auto vshader =
SpirvHelper::GLSL2SPV(SpirvHelper::Stage::Vertex, Shaders::VertCode);
auto fshader =
SpirvHelper::GLSL2SPV(SpirvHelper::Stage::Fragment, Shaders::FragCode);
SpirvHelper::Finalize();
std::string vcode = SpirvHelper::SPV2GLSL(vshader, 110, false);
std::string fcode = SpirvHelper::SPV2GLSL(fshader, 110, false);
PDLOG("Vertex: \n{}", vcode);
PDLOG("Fragment: \n{}", fcode);
pShader = CreateShaderProgram(vcode.c_str(), fcode.c_str());
pShader = CreateShaderProgram(pVertCode, pFragCode);
glUseProgram(pShader);
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
@@ -49,8 +77,8 @@ void GfxOpenGL2::SysInit() {
pSetupShaderAttribs(pShader);
pLocTex = glGetUniformLocation(pShader, "tex");
pLocAlfa = glGetUniformLocation(pShader, "push.alfa");
pLocProjection = glGetUniformLocation(pShader, "ubo.projection");
pLocAlfa = glGetUniformLocation(pShader, "alfa");
pLocProjection = glGetUniformLocation(pShader, "projection");
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

View File

@@ -11,14 +11,18 @@
namespace PD {
void GfxOpenGL3::SysInit() {
SpirvHelper::Init();
auto vshader =
SpirvHelper::GLSL2SPV(SpirvHelper::Stage::Vertex, Shaders::VertCode);
auto fshader =
SpirvHelper::GLSL2SPV(SpirvHelper::Stage::Fragment, Shaders::FragCode);
SpirvHelper::Finalize();
std::string vcode = SpirvHelper::SPV2GLSL(vshader, 330, false);
std::string fcode = SpirvHelper::SPV2GLSL(fshader, 330, false);
std::string vcode = SpirvHelper::SPV2GLSL(
std::vector<unsigned int>(
Shaders::VertexShader,
Shaders::VertexShader +
(sizeof(Shaders::VertexShader) / sizeof(unsigned int))),
330, false);
std::string fcode = SpirvHelper::SPV2GLSL(
std::vector<unsigned int>(
Shaders::FragmentShader,
Shaders::FragmentShader +
(sizeof(Shaders::FragmentShader) / sizeof(unsigned int))),
330, false);
PDLOG("Vertex: \n{}", vcode);
PDLOG("Fragment: \n{}", fcode);
pShader = CreateShaderProgram(vcode.c_str(), fcode.c_str());

View File

@@ -1,17 +1,28 @@
#if defined(PD_ENABLE_SPIRV_HELPER)
#if defined(PD_INCLUDE_GLSLANG)
#include <SPIRV/GlslangToSpv.h>
#include <glslang/Public/ResourceLimits.h>
#include <glslang/Public/ShaderLang.h>
#else
struct TBuiltInResource {};
#endif
#include <pd_system/spirv-helper.hpp>
#include <spirv.hpp>
#include <spirv_glsl.hpp>
#include <spirv_hlsl.hpp>
namespace PD {
#if defined(PD_INCLUDE_GLSLANG)
void SpirvHelper::Init() { glslang::InitializeProcess(); }
void SpirvHelper::Finalize() { glslang::FinalizeProcess(); }
#else
void SpirvHelper::Init() {}
void SpirvHelper::Finalize() {}
#endif
void SpirvHelper::SetupResources(TBuiltInResource& resources) {
#if defined(PD_INCLUDE_GLSLANG)
resources.maxLights = 32;
resources.maxClipPlanes = 6;
resources.maxTextureUnits = 32;
@@ -113,11 +124,13 @@ void SpirvHelper::SetupResources(TBuiltInResource& resources) {
resources.limits.generalSamplerIndexing = 1;
resources.limits.generalVariableIndexing = 1;
resources.limits.generalConstantMatrixVectorIndexing = 1;
#endif
}
std::vector<PD::u32> SpirvHelper::GLSL2SPV(Stage stage, const char* code,
bool vulkan_mode) {
std::vector<PD::u32> spv;
#if defined(PD_INCLUDE_GLSLANG)
EShLanguage estage = static_cast<EShLanguage>(stage);
glslang::TShader shader(estage);
glslang::TProgram program;
@@ -148,6 +161,7 @@ std::vector<PD::u32> SpirvHelper::GLSL2SPV(Stage stage, const char* code,
}
glslang::GlslangToSpv(*program.getIntermediate(estage), spv);
#endif
return spv;
}
@@ -186,6 +200,7 @@ std::string SpirvHelper::SPV2HLSL(const std::vector<PD::u32>& spirv,
}
} // namespace PD
#else
struct TBuiltInResource {};
namespace PD {
void SpirvHelper::Init() { glslang::InitializeProcess(); }
void SpirvHelper::Finalize() { glslang::FinalizeProcess(); }