cae  0.0.0
Cross-API graphics engine
Loading...
Searching...
No Matches
glsl.cpp
Go to the documentation of this file.
1#include "GLSL/GLSL.hpp"
2
3#include <SPIRV/GlslangToSpv.h>
4
5std::vector<uint32_t> cae::GLSL::compileGLSLtoSPIRV(const std::string &src, const ShaderStage stage)
6{
7 static bool glslangInitialized = false;
8 if (!glslangInitialized)
9 {
10 glslang::InitializeProcess();
11 glslangInitialized = true;
12 }
13
14 const EShLanguage lang = shaderStageToESh(stage);
15 glslang::TShader shader(lang);
16 const char *shaderStrings[1] = {src.c_str()};
17 shader.setStrings(shaderStrings, 1);
18
19 shader.setEnvInput(glslang::EShSourceGlsl, lang, glslang::EShClientVulkan, VERSION);
20 shader.setEnvClient(glslang::EShClientVulkan, glslang::EShTargetVulkan_1_3);
21 shader.setEnvTarget(glslang::EShTargetSpv, glslang::EShTargetSpv_1_6);
22
23 TBuiltInResource Resources = {};
24 Resources.maxLights = 32;
25 Resources.maxClipPlanes = 6;
26 Resources.maxTextureUnits = 32;
27 Resources.maxTextureCoords = 32;
28 Resources.maxVertexAttribs = 64;
29 Resources.maxVertexUniformComponents = 4096;
30 Resources.maxVaryingFloats = 64;
31 Resources.maxVertexTextureImageUnits = 32;
32 Resources.maxCombinedTextureImageUnits = 80;
33 Resources.maxTextureImageUnits = 32;
34 Resources.maxFragmentUniformComponents = 4096;
35 Resources.maxDrawBuffers = 32;
36 Resources.maxVertexUniformVectors = 128;
37 Resources.maxVaryingVectors = 8;
38 Resources.maxFragmentUniformVectors = 16;
39 Resources.maxVertexOutputVectors = 16;
40 Resources.maxFragmentInputVectors = 15;
41 Resources.minProgramTexelOffset = -8;
42 Resources.maxProgramTexelOffset = 7;
43 Resources.maxClipDistances = 8;
44 Resources.maxComputeWorkGroupCountX = 65535;
45 Resources.maxComputeWorkGroupCountY = 65535;
46 Resources.maxComputeWorkGroupCountZ = 65535;
47 Resources.maxComputeWorkGroupSizeX = 1024;
48 Resources.maxComputeWorkGroupSizeY = 1024;
49 Resources.maxComputeWorkGroupSizeZ = 64;
50 Resources.maxComputeUniformComponents = 1024;
51 Resources.maxComputeTextureImageUnits = 16;
52 Resources.maxComputeImageUniforms = 8;
53 Resources.maxComputeAtomicCounters = 8;
54 Resources.maxComputeAtomicCounterBuffers = 1;
55 Resources.maxVaryingComponents = 60;
56 Resources.maxVertexOutputComponents = 64;
57 Resources.maxGeometryInputComponents = 64;
58 Resources.maxGeometryOutputComponents = 128;
59 Resources.maxFragmentInputComponents = 128;
60 Resources.maxImageUnits = 8;
61 Resources.maxCombinedImageUnitsAndFragmentOutputs = 8;
62 Resources.maxCombinedShaderOutputResources = 8;
63
64 constexpr auto messages = static_cast<EShMessages>(EShMsgSpvRules | EShMsgVulkanRules);
65
66 if (!shader.parse(&Resources, VERSION, false, messages))
67 {
68 throw std::runtime_error("GLSL parsing failed: " + std::string(shader.getInfoLog()));
69 }
70
71 glslang::TProgram program;
72 program.addShader(&shader);
73
74 if (!program.link(messages))
75 {
76 throw std::runtime_error("GLSL linking failed: " + std::string(program.getInfoLog()));
77 }
78
79 std::vector<uint32_t> spirv;
80 glslang::GlslangToSpv(*program.getIntermediate(lang), spirv);
81
82 return spirv;
83}
This file contains the GLSL class declaration.
static std::vector< uint32_t > compileGLSLtoSPIRV(const std::string &src, ShaderStage stage)
Definition glsl.cpp:5
static EShLanguage shaderStageToESh(const ShaderStage stage)
Definition GLSL.hpp:54
constexpr auto VERSION
Definition GLSL.hpp:18