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

@@ -86,12 +86,5 @@ else()
spirv-cross-msl
spirv-cross-reflect
spirv-cross-util
glslang
glslang-default-resource-limits
#OGLCompiler
OSDependent
SPIRV
#SPVRemapper
#HLSL
)
endif()

24
backends/README.md Normal file
View File

@@ -0,0 +1,24 @@
# Backends
- [Graphics](#graphics)
- [OpenGL2](#opengl2-legacy)
- [OpenGL3](#opengl3-modern-opengl)
- [DirectX9](#directx9-could-potentially-cover-more-then-opengl2)
- [Citro3D](#citro3d-3ds)
## Graphics
### OpenGL2 (Legacy)
> [!NOTE]
> As SPIRV to GLSL 120 translation doesn't work, your'e forced to write separate shaders for this backend
### OpenGL3+ (Modern OpenGL)
### DirectX9 (Could potentially cover more than OpenGL2)
### Citro3D (3DS)
> [!NOTE]
> This backend requires shaders as binary file, directly in the code or as string for libpicasso
> The Fragment Stage is special and is still wip

View File

@@ -39,5 +39,7 @@ class GfxOpenGL2 : public GfxDriverBase<GfxOpenGL2Config> {
int pLocTex = 0;
int pLocAlfa = 0;
int pLocProjection = 0;
static const char* pVertCode;
static const char* pFragCode;
};
} // namespace PD

View File

@@ -1,51 +1,17 @@
#pragma once
namespace PD {
/**
* Shader blobs
* Required for OpenGL3, and maybe later for vulkan
* Shaders can be found in /tools/pdsg/shaders
*/
namespace Shaders {
inline static const char* VertCode = R"(
#version 460
layout(location = 0) in vec2 pos;
layout(location = 1) in vec2 uv;
layout(location = 2) in vec4 color;
layout(location = 0) out vec2 oUV;
layout(location = 1) out vec4 oColor;
// Probably forgot about this matrix and
// searched hours for why the rendering isn't working :/
layout(set = 0, binding = 0) uniform UBO {
mat4 projection;
} ubo;
void main() {
gl_Position = ubo.projection*vec4(pos, 0.0, 1.0);
oUV = uv;
oColor = color;
}
)";
inline static const char* FragCode = R"(
#version 460
layout(location = 0) in vec2 oUV;
layout(location = 1) in vec4 oColor;
uniform sampler2D tex;
layout(push_constant) uniform PushData {
int alfa;
} push;
out vec4 FragColor;
void main() {
vec4 tc = texture(tex, oUV);
if (push.alfa != 0) {
FragColor = vec4(oColor.rgb, tc.a * oColor.a);
} else {
FragColor = tc * oColor;
}
}
)";
// clang-format off
const unsigned int VertexShader[] = { 0x7230203, 0x10000, 0x8000B, 0x2B, 0x0, 0x20011, 0x1, 0x6000B, 0x1, 0x4C534C47, 0x6474732E, 0x3035342E, 0x0, 0x3000E, 0x0, 0x1, 0xB000F, 0x0, 0x4, 0x6E69616D, 0x0, 0xD, 0x19, 0x24, 0x25, 0x27, 0x29, 0x30003, 0x2, 0x1CC, 0x40005, 0x4, 0x6E69616D, 0x0, 0x60005, 0xB, 0x505F6C67, 0x65567265, 0x78657472, 0x0, 0x60006, 0xB, 0x0, 0x505F6C67, 0x7469736F, 0x6E6F69, 0x70006, 0xB, 0x1, 0x505F6C67, 0x746E696F, 0x657A6953, 0x0, 0x70006, 0xB, 0x2, 0x435F6C67, 0x4470696C, 0x61747369, 0x65636E, 0x70006, 0xB, 0x3, 0x435F6C67, 0x446C6C75, 0x61747369, 0x65636E, 0x30005, 0xD, 0x0, 0x30005, 0x11, 0x4F4255, 0x60006, 0x11, 0x0, 0x6A6F7270, 0x69746365, 0x6E6F, 0x30005, 0x13, 0x6F6275, 0x30005, 0x19, 0x736F70, 0x30005, 0x24, 0x56556F, 0x30005, 0x25, 0x7675, 0x40005, 0x27, 0x6C6F436F, 0x726F, 0x40005, 0x29, 0x6F6C6F63, 0x72, 0x30047, 0xB, 0x2, 0x50048, 0xB, 0x0, 0xB, 0x0, 0x50048, 0xB, 0x1, 0xB, 0x1, 0x50048, 0xB, 0x2, 0xB, 0x3, 0x50048, 0xB, 0x3, 0xB, 0x4, 0x30047, 0x11, 0x2, 0x40048, 0x11, 0x0, 0x5, 0x50048, 0x11, 0x0, 0x7, 0x10, 0x50048, 0x11, 0x0, 0x23, 0x0, 0x40047, 0x13, 0x21, 0x0, 0x40047, 0x13, 0x22, 0x0, 0x40047, 0x19, 0x1E, 0x0, 0x40047, 0x24, 0x1E, 0x0, 0x40047, 0x25, 0x1E, 0x1, 0x40047, 0x27, 0x1E, 0x1, 0x40047, 0x29, 0x1E, 0x2, 0x20013, 0x2, 0x30021, 0x3, 0x2, 0x30016, 0x6, 0x20, 0x40017, 0x7, 0x6, 0x4, 0x40015, 0x8, 0x20, 0x0, 0x4002B, 0x8, 0x9, 0x1, 0x4001C, 0xA, 0x6, 0x9, 0x6001E, 0xB, 0x7, 0x6, 0xA, 0xA, 0x40020, 0xC, 0x3, 0xB, 0x4003B, 0xC, 0xD, 0x3, 0x40015, 0xE, 0x20, 0x1, 0x4002B, 0xE, 0xF, 0x0, 0x40018, 0x10, 0x7, 0x4, 0x3001E, 0x11, 0x10, 0x40020, 0x12, 0x2, 0x11, 0x4003B, 0x12, 0x13, 0x2, 0x40020, 0x14, 0x2, 0x10, 0x40017, 0x17, 0x6, 0x2, 0x40020, 0x18, 0x1, 0x17, 0x4003B, 0x18, 0x19, 0x1, 0x4002B, 0x6, 0x1B, 0x0, 0x4002B, 0x6, 0x1C, 0x3F800000, 0x40020, 0x21, 0x3, 0x7, 0x40020, 0x23, 0x3, 0x17, 0x4003B, 0x23, 0x24, 0x3, 0x4003B, 0x18, 0x25, 0x1, 0x4003B, 0x21, 0x27, 0x3, 0x40020, 0x28, 0x1, 0x7, 0x4003B, 0x28, 0x29, 0x1, 0x50036, 0x2, 0x4, 0x0, 0x3, 0x200F8, 0x5, 0x50041, 0x14, 0x15, 0x13, 0xF, 0x4003D, 0x10, 0x16, 0x15, 0x4003D, 0x17, 0x1A, 0x19, 0x50051, 0x6, 0x1D, 0x1A, 0x0, 0x50051, 0x6, 0x1E, 0x1A, 0x1, 0x70050, 0x7, 0x1F, 0x1D, 0x1E, 0x1B, 0x1C, 0x50091, 0x7, 0x20, 0x16, 0x1F, 0x50041, 0x21, 0x22, 0xD, 0xF, 0x3003E, 0x22, 0x20, 0x4003D, 0x17, 0x26, 0x25, 0x3003E, 0x24, 0x26, 0x4003D, 0x7, 0x2A, 0x29, 0x3003E, 0x27, 0x2A, 0x100FD, 0x10038, };
// clang-format on
// clang-format off
const unsigned int FragmentShader[] = { 0x7230203, 0x10000, 0x8000B, 0x39, 0x0, 0x20011, 0x1, 0x6000B, 0x1, 0x4C534C47, 0x6474732E, 0x3035342E, 0x0, 0x3000E, 0x0, 0x1, 0x8000F, 0x0, 0x4, 0x6E69616D, 0x0, 0x11, 0x22, 0x24, 0x30003, 0x2, 0x1CC, 0x40005, 0x4, 0x6E69616D, 0x0, 0x30005, 0x9, 0x6374, 0x30005, 0xD, 0x786574, 0x30005, 0x11, 0x56556F, 0x50005, 0x16, 0x68737550, 0x61746144, 0x0, 0x50006, 0x16, 0x0, 0x61666C61, 0x0, 0x40005, 0x18, 0x68737570, 0x0, 0x50005, 0x22, 0x67617246, 0x6F6C6F43, 0x72, 0x40005, 0x24, 0x6C6F436F, 0x726F, 0x40047, 0xD, 0x21, 0x0, 0x40047, 0xD, 0x22, 0x0, 0x40047, 0x11, 0x1E, 0x0, 0x30047, 0x16, 0x2, 0x50048, 0x16, 0x0, 0x23, 0x0, 0x40047, 0x24, 0x1E, 0x1, 0x20013, 0x2, 0x30021, 0x3, 0x2, 0x30016, 0x6, 0x20, 0x40017, 0x7, 0x6, 0x4, 0x40020, 0x8, 0x7, 0x7, 0x90019, 0xA, 0x6, 0x1, 0x0, 0x0, 0x0, 0x1, 0x0, 0x3001B, 0xB, 0xA, 0x40020, 0xC, 0x0, 0xB, 0x4003B, 0xC, 0xD, 0x0, 0x40017, 0xF, 0x6, 0x2, 0x40020, 0x10, 0x1, 0xF, 0x4003B, 0x10, 0x11, 0x1, 0x4002B, 0x6, 0x13, 0x0, 0x40015, 0x15, 0x20, 0x1, 0x3001E, 0x16, 0x15, 0x40020, 0x17, 0x9, 0x16, 0x4003B, 0x17, 0x18, 0x9, 0x4002B, 0x15, 0x19, 0x0, 0x40020, 0x1A, 0x9, 0x15, 0x20014, 0x1D, 0x40020, 0x21, 0x3, 0x7, 0x4003B, 0x21, 0x22, 0x3, 0x40020, 0x23, 0x1, 0x7, 0x4003B, 0x23, 0x24, 0x1, 0x40017, 0x25, 0x6, 0x3, 0x40015, 0x28, 0x20, 0x0, 0x4002B, 0x28, 0x29, 0x3, 0x40020, 0x2A, 0x7, 0x6, 0x40020, 0x2D, 0x1, 0x6, 0x50036, 0x2, 0x4, 0x0, 0x3, 0x200F8, 0x5, 0x4003B, 0x8, 0x9, 0x7, 0x4003D, 0xB, 0xE, 0xD, 0x4003D, 0xF, 0x12, 0x11, 0x70058, 0x7, 0x14, 0xE, 0x12, 0x2, 0x13, 0x3003E, 0x9, 0x14, 0x50041, 0x1A, 0x1B, 0x18, 0x19, 0x4003D, 0x15, 0x1C, 0x1B, 0x500AB, 0x1D, 0x1E, 0x1C, 0x19, 0x300F7, 0x20, 0x0, 0x400FA, 0x1E, 0x1F, 0x35, 0x200F8, 0x1F, 0x4003D, 0x7, 0x26, 0x24, 0x8004F, 0x25, 0x27, 0x26, 0x26, 0x0, 0x1, 0x2, 0x50041, 0x2A, 0x2B, 0x9, 0x29, 0x4003D, 0x6, 0x2C, 0x2B, 0x50041, 0x2D, 0x2E, 0x24, 0x29, 0x4003D, 0x6, 0x2F, 0x2E, 0x50085, 0x6, 0x30, 0x2C, 0x2F, 0x50051, 0x6, 0x31, 0x27, 0x0, 0x50051, 0x6, 0x32, 0x27, 0x1, 0x50051, 0x6, 0x33, 0x27, 0x2, 0x70050, 0x7, 0x34, 0x31, 0x32, 0x33, 0x30, 0x3003E, 0x22, 0x34, 0x200F9, 0x20, 0x200F8, 0x35, 0x4003D, 0x7, 0x36, 0x9, 0x4003D, 0x7, 0x37, 0x24, 0x50085, 0x7, 0x38, 0x36, 0x37, 0x3003E, 0x22, 0x38, 0x200F9, 0x20, 0x200F8, 0x20, 0x100FD, 0x10038, };
// clang-format on
} // namespace Shaders
} // namespace PD

View File

@@ -1,10 +1,8 @@
#pragma once
#include <glslang/Public/ResourceLimits.h>
#include <glslang/Public/ShaderLang.h>
#include <pd/common.hpp>
struct TBuiltInResource;
namespace PD {
class SpirvHelper {
public:

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(); }