Add SpirvHelper (Shader cross compilation)

- Make OpenGL2 and OpenGL3 use the same base shaders (using SpirvHelper)
- Add Transpose func to Mat4
This commit is contained in:
2026-03-18 15:17:48 +01:00
parent 186fce803e
commit 7d89ab1c47
13 changed files with 424 additions and 139 deletions

View File

@@ -39,7 +39,5 @@ 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

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

View File

@@ -0,0 +1,51 @@
#pragma once
namespace PD {
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;
}
}
)";
} // namespace Shaders
} // namespace PD

View File

@@ -0,0 +1,36 @@
#pragma once
#include <glslang/Public/ResourceLimits.h>
#include <glslang/Public/ShaderLang.h>
#include <pd/common.hpp>
namespace PD {
class SpirvHelper {
public:
enum class Format {
GLSL,
HLSL,
MSL, // Not supported yet
SPIRV,
};
enum class Stage {
Vertex = 0,
Geometry = 3,
Fragment = 4,
};
SpirvHelper() = default;
~SpirvHelper() = default;
static void Init();
static void Finalize();
static void SetupResources(TBuiltInResource& resources);
static std::vector<PD::u32> GLSL2SPV(Stage stage, const char* code,
bool vulkan_mode = false);
static std::string SPV2GLSL(const std::vector<PD::u32>& spirv, int version,
bool es = false);
static std::string SPV2HLSL(const std::vector<PD::u32>& spirv, int version);
};
} // namespace PD