18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-27 21:45:16 +01:00
Files
maszyna/ci_shadervalidator/main.cpp
jerrrrycho 57a0a71f48 fix(ci): build and run the shader validator again (#437)
* fix(ci): build and run the shader validator again

- gl/glsl_common.h and gl/shader.cpp include utilities/Globals.h and
  utilities/Logs.h now, so the stand-in headers of the validator move to
  ci_shadervalidator/utilities/.
- gl/ubo.h needs std::span and gl/shader.cpp uses contains(), so the
  script builds with -std=c++20. The stand-in stdafx.h gives <array>,
  <algorithm> and <string_view>, which the precompiled header of the
  simulator gives otherwise.
- build.sh runs from any directory and stops on the first error.
- glslang defines the GL_ARB_gpu_shader5 macro without the #extension
  directive, so light_common.glsl took its textureGather() path without
  the extension enabled and 25 material shaders failed on desktop GL with
  the shadows on. The validator adds the directive which the simulator
  gives on the drivers with the extension.
- The shader goes to the temporary directory of the system instead of a
  fixed /tmp, a shader which can't be preprocessed fails instead of
  ending the run, the shaders go in name order and the failed ones are
  listed at the end.

* fix(ci): write the validated shader to a unique temporary file

The shader validator wrote every preprocessed shader to a file with a
fixed name in the temporary directory (/tmp/shader on Linux) and opened
it with std::ofstream. In a shared /tmp anyone can create that name in
advance, as a file or a link to another file, and the validator would
then write through it (Sonar rule S5443, publicly writable directories).

The shader now goes to a file with a unique name which is created
exclusively: mkstemp() on Linux, and on Windows, which has no mkstemp(),
a random name opened with fopen_s(..., "wx"), which fails when the file
exists. The file is removed after glslangValidator has checked it, and
when it cannot be written completely. glslangValidator gets the stage
with -S, so the file still needs no extension.

* docs(ci): the standalone validator no longer checks the PCF fallback path

The comment of has_gl_extension() said the validator compiles the 16-tap
PCF fallback of light_common.glsl. Since the validator adds the
GL_ARB_gpu_shader5 directive itself, it checks the textureGather() path.
2026-11-25 05:12:15 +01:00

189 lines
5.4 KiB
C++

#include "stdafx.h"
#include "../gl/shader.h"
#include "../gl/glsl_common.h"
#include "utilities/Globals.h"
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <filesystem>
#include <vector>
#include <algorithm>
#ifdef _WIN32
#include <random>
#else
#include <unistd.h>
#endif
_global Global;
// writes the text to a new file with a unique name in the temporary directory of the system and returns its path,
// or an empty string when it cannot be written. the file is created exclusively, so nothing put in a shared /tmp
// under a name known in advance (a file or a link) is written through
std::string write_temp_file(std::string const &text)
{
std::string path = (std::filesystem::temp_directory_path() / "maszyna-shader-XXXXXX").string();
std::FILE *file = nullptr;
#ifdef _WIN32
// no mkstemp() in the CRT; a random name, opened with "x", which fails when the file is there already
std::random_device random;
for (int attempt = 0; attempt < 100 && file == nullptr; ++attempt) {
path.replace(path.size() - 6, 6, std::to_string(100000 + random() % 900000));
if (fopen_s(&file, path.c_str(), "wx") != 0)
file = nullptr;
}
#else
int const descriptor = mkstemp(path.data());
if (descriptor != -1) {
file = fdopen(descriptor, "w");
if (file == nullptr) {
close(descriptor);
std::remove(path.c_str());
}
}
#endif
if (file == nullptr)
return {};
bool const written = std::fwrite(text.data(), 1, text.size(), file) == text.size();
if (std::fclose(file) != 0 || !written) {
std::remove(path.c_str());
return {};
}
return path;
}
bool check_shader(std::string filename)
{
gl::shader shader_processor;
gl::glsl_common_setup();
std::pair<GLuint, std::string> source;
try {
source = shader_processor.process_source(filename, "../shaders/");
}
catch (gl::shader_exception const &e) {
std::cout << "failed to preprocess: " << e.what() << std::endl;
return false;
}
std::string stage;
if (source.first == GL_VERTEX_SHADER)
stage = "vert";
else if (source.first == GL_FRAGMENT_SHADER)
stage = "frag";
else if (source.first == GL_GEOMETRY_SHADER)
stage = "geom";
else
return false;
// glslang defines the GL_ARB_gpu_shader5 macro without the #extension directive, so light_common.glsl
// takes its textureGather() path, which needs the extension enabled on desktop GL 3.30. has_gl_extension()
// of gl/shader.cpp has no GL context here and says no, so the directive which the simulator gives on the
// drivers with the extension is added here
std::string const gpu_shader5 = "#extension GL_ARB_gpu_shader5 : enable\n";
if (!Global.gfx_usegles && source.second.find(gpu_shader5) == std::string::npos) {
std::size_t const version_end = source.second.find('\n');
if (version_end != std::string::npos)
source.second.insert(version_end + 1, gpu_shader5);
}
// the stage is given with -S, so the file needs no extension
std::string const path = write_temp_file(source.second);
if (path.empty()) {
std::cout << "cannot write the shader to the temporary directory" << std::endl;
return false;
}
int ret = system(("glslangValidator -S " + stage + " \"" + path + "\"").c_str());
std::remove(path.c_str());
return ret == 0;
}
bool check_shader_allopts(std::string filename)
{
bool suc = true;
std::cout << "features on..." << std::flush;
Global.gfx_shadowmap_enabled = true;
Global.gfx_envmap_enabled = true;
Global.gfx_postfx_motionblur_enabled = true;
Global.gfx_skippipeline = false;
Global.gfx_extraeffects = true;
Global.gfx_shadergamma = true;
// the default of the simulator, which compiles the alpha to coverage branches of the shaders
Global.iMultisampling = 2;
if (!check_shader(filename))
suc = false;
std::cout << "done, features off..." << std::flush;
Global.gfx_shadowmap_enabled = false;
Global.gfx_envmap_enabled = false;
Global.gfx_postfx_motionblur_enabled = false;
Global.gfx_skippipeline = true;
Global.gfx_extraeffects = false;
Global.gfx_shadergamma = false;
Global.iMultisampling = 0;
if (!check_shader(filename))
suc = false;
std::cout << "done" << std::flush;
return suc;
}
bool check_shader_confs(std::string filename)
{
bool suc = true;
std::cout << "desktop GL: " << std::flush;
Global.gfx_usegles = false;
if (!check_shader_allopts(filename))
suc = false;
std::cout << "; GLES: " << std::flush;
GLAD_GL_ES_VERSION_3_1 = 1;
Global.gfx_usegles = true;
if (!check_shader_allopts(filename))
suc = false;
std::cout << std::endl;
return suc;
}
int main()
{
// in name order, so the runs can be compared
std::vector<std::string> shaders;
for (auto &f : std::filesystem::recursive_directory_iterator("../shaders/")) {
if (!f.is_regular_file())
continue;
if (f.path().extension() != ".frag" && f.path().extension() != ".geom" && f.path().extension() != ".vert")
continue;
shaders.push_back(std::filesystem::relative(f.path(), "../shaders/").generic_string());
}
std::sort(shaders.begin(), shaders.end());
std::vector<std::string> failed;
for (auto const &relative : shaders) {
std::cout << "== validating shader " << relative << ": " << std::flush;
if (!check_shader_confs(relative))
failed.push_back(relative);
}
if (failed.empty())
std::cout << "==== all checks ok (" << shaders.size() << " shaders)" << std::endl;
else {
std::cout << "==== checks failed for " << failed.size() << " of " << shaders.size() << " shaders:" << std::endl;
for (auto const &relative : failed)
std::cout << relative << std::endl;
return 1;
}
return 0;
}