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:
@@ -3,4 +3,5 @@ cmake_minimum_required(VERSION 3.22)
|
||||
add_subdirectory(lazyvec)
|
||||
add_subdirectory(ppam)
|
||||
add_subdirectory(pdlm)
|
||||
add_subdirectory(pdfm)
|
||||
add_subdirectory(pdfm)
|
||||
add_subdirectory(pdsg)
|
||||
29
tools/pdsg/CMakeLists.txt
Normal file
29
tools/pdsg/CMakeLists.txt
Normal 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
|
||||
)
|
||||
34
tools/pdsg/include/spirv-helper.hpp
Normal file
34
tools/pdsg/include/spirv-helper.hpp
Normal 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);
|
||||
};
|
||||
45
tools/pdsg/source/main.cpp
Normal file
45
tools/pdsg/source/main.cpp
Normal 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;
|
||||
}
|
||||
184
tools/pdsg/source/spirv-helper.cpp
Normal file
184
tools/pdsg/source/spirv-helper.cpp
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user