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-msl
spirv-cross-reflect spirv-cross-reflect
spirv-cross-util spirv-cross-util
glslang
glslang-default-resource-limits
#OGLCompiler
OSDependent
SPIRV
#SPVRemapper
#HLSL
) )
endif() 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 pLocTex = 0;
int pLocAlfa = 0; int pLocAlfa = 0;
int pLocProjection = 0; int pLocProjection = 0;
static const char* pVertCode;
static const char* pFragCode;
}; };
} // namespace PD } // namespace PD

View File

@@ -1,51 +1,17 @@
#pragma once #pragma once
namespace PD { namespace PD {
/**
* Shader blobs
* Required for OpenGL3, and maybe later for vulkan
* Shaders can be found in /tools/pdsg/shaders
*/
namespace Shaders { namespace Shaders {
inline static const char* VertCode = R"( // clang-format off
#version 460 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
layout(location = 0) in vec2 pos; // clang-format off
layout(location = 1) in vec2 uv; 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, };
layout(location = 2) in vec4 color; // clang-format on
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 Shaders
} // namespace PD } // namespace PD

View File

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

View File

@@ -6,11 +6,49 @@
#include <pd/drivers/drivers.hpp> #include <pd/drivers/drivers.hpp>
#include <pd_system/gl-helper.hpp> #include <pd_system/gl-helper.hpp>
#include <pd_system/shaders.hpp>
#include <pd_system/spirv-helper.hpp>
namespace PD { 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) { void GfxOpenGL2::pSetupShaderAttribs(u32 shader) {
GLint _pos = glGetAttribLocation(shader, "pos"); GLint _pos = glGetAttribLocation(shader, "pos");
GLint _uv = glGetAttribLocation(shader, "uv"); GLint _uv = glGetAttribLocation(shader, "uv");
@@ -30,17 +68,7 @@ void GfxOpenGL2::pSetupShaderAttribs(u32 shader) {
} }
void GfxOpenGL2::SysInit() { void GfxOpenGL2::SysInit() {
SpirvHelper::Init(); pShader = CreateShaderProgram(pVertCode, pFragCode);
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());
glUseProgram(pShader); glUseProgram(pShader);
glGenBuffers(1, &VBO); glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO); glBindBuffer(GL_ARRAY_BUFFER, VBO);
@@ -49,8 +77,8 @@ void GfxOpenGL2::SysInit() {
pSetupShaderAttribs(pShader); pSetupShaderAttribs(pShader);
pLocTex = glGetUniformLocation(pShader, "tex"); pLocTex = glGetUniformLocation(pShader, "tex");
pLocAlfa = glGetUniformLocation(pShader, "push.alfa"); pLocAlfa = glGetUniformLocation(pShader, "alfa");
pLocProjection = glGetUniformLocation(pShader, "ubo.projection"); pLocProjection = glGetUniformLocation(pShader, "projection");
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

View File

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

View File

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

View File

@@ -4,3 +4,4 @@ add_subdirectory(lazyvec)
add_subdirectory(ppam) add_subdirectory(ppam)
add_subdirectory(pdlm) add_subdirectory(pdlm)
add_subdirectory(pdfm) add_subdirectory(pdfm)
add_subdirectory(pdsg)

29
tools/pdsg/CMakeLists.txt Normal file
View File

@@ -0,0 +1,29 @@
cmake_minimum_required(VERSION 3.22)
project(pdsg LANGUAGES CXX VERSION 1.0.0)
### Requires C++ 20
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED true)
add_executable(pdsg
${CMAKE_CURRENT_SOURCE_DIR}/source/main.cpp
${CMAKE_CURRENT_SOURCE_DIR}/source/spirv-helper.cpp
)
target_include_directories(pdsg PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/include
)
target_link_libraries(pdsg PRIVATE
glslang
glslang-default-resource-limits
OSDependent
SPIRV
spirv-cross-core
spirv-cross-cpp
spirv-cross-glsl
spirv-cross-hlsl
spirv-cross-msl
spirv-cross-reflect
spirv-cross-util
)

View File

@@ -0,0 +1,34 @@
#pragma once
#include <cstdint>
#include <string>
#include <vector>
struct TBuiltInResource;
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<unsigned int> GLSL2SPV(Stage stage, const char* code);
static std::string SPV2GLSL(const std::vector<unsigned int>& spirv,
int version, bool es = false);
static std::string SPV2HLSL(const std::vector<unsigned int>& spirv,
int version);
};

View File

@@ -0,0 +1,45 @@
#include <format>
#include <fstream>
#include <iostream>
#include <spirv-helper.hpp>
#include <sstream>
template <typename... Args>
void FmtPrint(std::format_string<Args...> fmt, Args&&... args) {
std::cout << std::format(fmt, std::forward<Args>(args)...);
}
std::string LoadFile(const std::string& path) {
std::stringstream s;
std::ifstream f(path);
s << f.rdbuf();
return s.str();
}
int main(int argc, char** argv) {
if (argc != 4) return 0;
SpirvHelper::Stage mode = SpirvHelper::Stage::Vertex;
if (std::string(argv[3]) == "vertex") {
mode = SpirvHelper::Stage::Vertex;
} else if (std::string(argv[3]) == "fragment") {
mode = SpirvHelper::Stage::Fragment;
} else if (std::string(argv[3]) == "geometry") {
mode = SpirvHelper::Stage::Geometry;
}
FmtPrint("ProcessingMode: {}\n", (int)mode);
FmtPrint("Loading {}\n", argv[1]);
std::string shader = LoadFile(argv[1]);
FmtPrint("Generating SPIRV...\n");
auto res = SpirvHelper::GLSL2SPV(SpirvHelper::Stage::Vertex, shader.c_str());
FmtPrint("Result size: {}\n", res.size());
std::ofstream ff(argv[2]);
FmtPrint("Writing into {}", argv[2]);
ff << "#pragma once\n\n#include <cinttypes>\n\n// clang-format off\nconst "
"unsigned int pShader[] "
"= { ";
for (auto& it : res) {
ff << std::format("0x{:X}, ", it);
}
ff << "};\n// clang-format on\n";
return 0;
}

View File

@@ -0,0 +1,184 @@
#include <SPIRV/GlslangToSpv.h>
#include <glslang/Public/ResourceLimits.h>
#include <glslang/Public/ShaderLang.h>
#include <spirv-helper.hpp>
#include <spirv.hpp>
#include <spirv_glsl.hpp>
#include <spirv_hlsl.hpp>
void SpirvHelper::Init() { glslang::InitializeProcess(); }
void SpirvHelper::Finalize() { glslang::FinalizeProcess(); }
void SpirvHelper::SetupResources(TBuiltInResource& resources) {
resources.maxLights = 32;
resources.maxClipPlanes = 6;
resources.maxTextureUnits = 32;
resources.maxTextureCoords = 32;
resources.maxVertexAttribs = 64;
resources.maxVertexUniformComponents = 4096;
resources.maxVaryingFloats = 64;
resources.maxVertexTextureImageUnits = 32;
resources.maxCombinedTextureImageUnits = 80;
resources.maxTextureImageUnits = 32;
resources.maxFragmentUniformComponents = 4096;
resources.maxDrawBuffers = 32;
resources.maxVertexUniformVectors = 128;
resources.maxVaryingVectors = 8;
resources.maxFragmentUniformVectors = 16;
resources.maxVertexOutputVectors = 16;
resources.maxFragmentInputVectors = 15;
resources.minProgramTexelOffset = -8;
resources.maxProgramTexelOffset = 7;
resources.maxClipDistances = 8;
resources.maxComputeWorkGroupCountX = 65535;
resources.maxComputeWorkGroupCountY = 65535;
resources.maxComputeWorkGroupCountZ = 65535;
resources.maxComputeWorkGroupSizeX = 1024;
resources.maxComputeWorkGroupSizeY = 1024;
resources.maxComputeWorkGroupSizeZ = 64;
resources.maxComputeUniformComponents = 1024;
resources.maxComputeTextureImageUnits = 16;
resources.maxComputeImageUniforms = 8;
resources.maxComputeAtomicCounters = 8;
resources.maxComputeAtomicCounterBuffers = 1;
resources.maxVaryingComponents = 60;
resources.maxVertexOutputComponents = 64;
resources.maxGeometryInputComponents = 64;
resources.maxGeometryOutputComponents = 128;
resources.maxFragmentInputComponents = 128;
resources.maxImageUnits = 8;
resources.maxCombinedImageUnitsAndFragmentOutputs = 8;
resources.maxCombinedShaderOutputResources = 8;
resources.maxImageSamples = 0;
resources.maxVertexImageUniforms = 0;
resources.maxTessControlImageUniforms = 0;
resources.maxTessEvaluationImageUniforms = 0;
resources.maxGeometryImageUniforms = 0;
resources.maxFragmentImageUniforms = 8;
resources.maxCombinedImageUniforms = 8;
resources.maxGeometryTextureImageUnits = 16;
resources.maxGeometryOutputVertices = 256;
resources.maxGeometryTotalOutputComponents = 1024;
resources.maxGeometryUniformComponents = 1024;
resources.maxGeometryVaryingComponents = 64;
resources.maxTessControlInputComponents = 128;
resources.maxTessControlOutputComponents = 128;
resources.maxTessControlTextureImageUnits = 16;
resources.maxTessControlUniformComponents = 1024;
resources.maxTessControlTotalOutputComponents = 4096;
resources.maxTessEvaluationInputComponents = 128;
resources.maxTessEvaluationOutputComponents = 128;
resources.maxTessEvaluationTextureImageUnits = 16;
resources.maxTessEvaluationUniformComponents = 1024;
resources.maxTessPatchComponents = 120;
resources.maxPatchVertices = 32;
resources.maxTessGenLevel = 64;
resources.maxViewports = 16;
resources.maxVertexAtomicCounters = 0;
resources.maxTessControlAtomicCounters = 0;
resources.maxTessEvaluationAtomicCounters = 0;
resources.maxGeometryAtomicCounters = 0;
resources.maxFragmentAtomicCounters = 8;
resources.maxCombinedAtomicCounters = 8;
resources.maxAtomicCounterBindings = 1;
resources.maxVertexAtomicCounterBuffers = 0;
resources.maxTessControlAtomicCounterBuffers = 0;
resources.maxTessEvaluationAtomicCounterBuffers = 0;
resources.maxGeometryAtomicCounterBuffers = 0;
resources.maxFragmentAtomicCounterBuffers = 1;
resources.maxCombinedAtomicCounterBuffers = 1;
resources.maxAtomicCounterBufferSize = 16384;
resources.maxTransformFeedbackBuffers = 4;
resources.maxTransformFeedbackInterleavedComponents = 64;
resources.maxCullDistances = 8;
resources.maxCombinedClipAndCullDistances = 8;
resources.maxSamples = 4;
resources.maxMeshOutputVerticesNV = 256;
resources.maxMeshOutputPrimitivesNV = 512;
resources.maxMeshWorkGroupSizeX_NV = 32;
resources.maxMeshWorkGroupSizeY_NV = 1;
resources.maxMeshWorkGroupSizeZ_NV = 1;
resources.maxTaskWorkGroupSizeX_NV = 32;
resources.maxTaskWorkGroupSizeY_NV = 1;
resources.maxTaskWorkGroupSizeZ_NV = 1;
resources.maxMeshViewCountNV = 4;
resources.limits.nonInductiveForLoops = 1;
resources.limits.whileLoops = 1;
resources.limits.doWhileLoops = 1;
resources.limits.generalUniformIndexing = 1;
resources.limits.generalAttributeMatrixVectorIndexing = 1;
resources.limits.generalVaryingIndexing = 1;
resources.limits.generalSamplerIndexing = 1;
resources.limits.generalVariableIndexing = 1;
resources.limits.generalConstantMatrixVectorIndexing = 1;
}
std::vector<unsigned int> SpirvHelper::GLSL2SPV(Stage stage, const char* code) {
std::vector<unsigned int> spv;
EShLanguage estage = static_cast<EShLanguage>(stage);
glslang::TShader shader(estage);
glslang::TProgram program;
const char* strings[1];
TBuiltInResource resources = {};
SetupResources(resources);
EShMessages messages = (EShMessages)(EShMsgSpvRules | EShMsgVulkanRules);
strings[0] = code;
shader.setStrings(strings, 1);
shader.setAutoMapBindings(true);
shader.setAutoMapLocations(true);
if (!shader.parse(&resources, 100, false, messages)) {
puts(shader.getInfoLog());
puts(shader.getInfoDebugLog());
return spv;
}
program.addShader(&shader);
if (!program.link(messages)) {
puts(shader.getInfoLog());
puts(shader.getInfoDebugLog());
fflush(stdout);
return spv;
}
glslang::GlslangToSpv(*program.getIntermediate(estage), spv);
return spv;
}
std::string SpirvHelper::SPV2GLSL(const std::vector<unsigned int>& spirv,
int version, bool es) {
std::string ret;
spirv_cross::CompilerGLSL glsl(spirv);
spirv_cross::ShaderResources resources = glsl.get_shader_resources();
for (auto& resource : resources.stage_inputs) {
glsl.unset_decoration(resource.id, spv::DecorationLocation);
}
for (auto& resource : resources.stage_outputs) {
glsl.unset_decoration(resource.id, spv::DecorationLocation);
}
spirv_cross::CompilerGLSL::Options scoptions;
scoptions.version = version;
scoptions.es = es;
scoptions.emit_uniform_buffer_as_plain_uniforms = true;
scoptions.enable_420pack_extension = false;
glsl.set_common_options(scoptions);
ret = glsl.compile();
return ret;
}
std::string SpirvHelper::SPV2HLSL(const std::vector<unsigned int>& spirv,
int version) {
std::string ret;
spirv_cross::CompilerHLSL hlsl(spirv);
spirv_cross::CompilerHLSL::Options scoptions;
scoptions.shader_model = version;
scoptions.point_size_compat = false;
scoptions.force_storage_buffer_as_uav = false;
hlsl.set_hlsl_options(scoptions);
ret = hlsl.compile();
return ret;
}

View File

@@ -5,15 +5,18 @@ if(${CMAKE_SYSTEM_NAME} STREQUAL "Nintendo3DS")
else() else()
# GLAD # GLAD
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/glad) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/glad)
# GLSLANG
set(ENABLE_OPT OFF CACHE BOOL "" FORCE)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/glslang)
# SPIRV-Cross # SPIRV-Cross
set(SPIRV_CROSS_CLI OFF CACHE BOOL "" FORCE) set(SPIRV_CROSS_CLI OFF CACHE BOOL "" FORCE)
set(SPIRV_CROSS_ENABLE_TESTS OFF CACHE BOOL "" FORCE) set(SPIRV_CROSS_ENABLE_TESTS OFF CACHE BOOL "" FORCE)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/spirv-cross) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/spirv-cross)
endif() endif()
if(PD_BUILD_TOOLS)
# GLSLANG
set(ENABLE_OPT OFF CACHE BOOL "" FORCE)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/glslang)
endif()
# STB # STB
add_library(stb INTERFACE) add_library(stb INTERFACE)
target_include_directories(stb INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/stb) target_include_directories(stb INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/stb)