NDEVR
API Documentation
ShaderTools.h
1#pragma once
2//#include <NDEVR/ShaderBuilderModule.h>
3#include <NDEVR/QTModelManager.h>
4#include <NDEVR/QTWindowManager.h>
5#include <NDEVR/Translator.h>
6#include <NDEVR/ModuleManager.h>
7#include <NDEVR/ModuleResourceManager.h>
8#include <NDEVR/BufferedScanner.h>
9#include <NDEVR/LocalApplicationLauncher.h>
10namespace NDEVR
11{
15 {
16 public:
17 File exe_location = File("$(VULKAN_DIR)/Bin/glslangValidator.exe");
18 LogPtr log = nullptr;
21 {
22 exe_location.expandEnvironmentalVars();
23 }
24
27 void compileIndividualShader(File compile_file, File output_location)
28 {
29 String args = "-V " + compile_file;
30 args += " -o " + output_location;
32 }
33
38 void compileShader(File reference, const StringView& out_name, const StringView& value, bool is_simple)
39 {
40 reference.expandEnvironmentalVars();
41 BufferedScanner s(reference);
42 String data;
43 while (s.nextLine())
44 {
45 if (s.currentLine().beginsWith("#define SIMPLE_SHADER"))
46 {
47 if (is_simple)
48 data += "\n#define SIMPLE_SHADER 1\n";
49 else
50 data += "\n#define SIMPLE_SHADER 0\n";
51
52 }
53 else if (s.currentLine().beginsWith("#define " + value))
54 data += "#define " + value + " 1\n";
55 else if (s.currentLine().beginsWith("\n#define " + value))
56 data += "\n#define " + value + " 1\n";
57 else
58 data += s.currentLine() + "\n";
59 }
60 StringView extension = reference.getPath(File::e_file_extension);
61 File new_file(reference.getParentDirectory().appendPath(out_name + extension));
63 new_file << data;
64 new_file.close();
65 File output = reference.getParentDirectory().appendPath(out_name + ".spv");
66 compileIndividualShader(new_file, output);
67 }
68
70 void compileShader(File reference)
71 {
72 File output = reference;
73 output.setPath(".spv", File::e_file_extension);
74 compileIndividualShader(reference, output);
75 }
76
78 void compileShaders(File reference)
79 {
80 compileShader(reference, "points_s", "POINT_SHADER", true);
81 compileShader(reference, "points", "POINT_SHADER", false);
82 compileShader(reference, "linework", "LINEWORK_SHADER", false);
83 compileShader(reference, "solid", "SOLID_SHADER", false);
84 compileShader(reference, "solid_grid", "SOLID_GRID_SHADER", false);
85 compileShader(reference, "solid_texture", "SOLID_TEXTURE_SHADER", false);
86 }
87
89 {
90 File folder("$(NDEVR_SOURCE_DIR)/VulkanInterface/Resources/Shaders/");
92 compileShaders(File(folder).appendPath("Gouraud/simple.frag"));
93 compileShaders(File(folder).appendPath("BackgroundGradiant/simple.frag"));
94 compileShaders(File(folder).appendPath("Normal/simple.vert"));
95 compileShaders(File(folder).appendPath("Integer/simple.vert"));
96 compileShader(File(folder).appendPath("Integer/tbn.vert"));
97 compileShader(File(folder).appendPath("Normal/tbn.vert"));
98 compileShader(File(folder).appendPath("Gouraud/tbn.frag"));
99
100 }
101 };
102
103}
Implentation of Scanner, however all data is cached.
bool nextLine(String &string, bool clear_string=true) final override
Advances to the next line and writes it into the provided string.
Logic for reading or writing to a file as well as navigating filesystems or other common file operati...
Definition File.h:53
FILE * open(OpenMode mode, bool append=false)
Opens the file with the specified mode.
@ e_ascii_write
Open for ASCII text writing.
Definition File.h:63
File & appendPath(const StringView &cs)
Appends a path component to this file path.
void close()
Closes the file if it is open.
void expandEnvironmentalVars()
Expands any environment variables present in the file path.
File getParentDirectory() const
Gets the parent directory of this file.
StringView getPath(uint01 file_parts) const
Retrieves a subset of the file path based on the specified parts.
@ e_file_extension
The file extension (e.g., ".txt").
Definition File.h:78
void setPath(const StringView &path, uint01 part)
Sets a specific part of the file path.
static void CallApplication(const StringView &application, const StringView &args=StringView(), bool wait_for_completion=true, bool windowless=false, LogPtr log=LogPtr())
Convenience function to launch an application in a single call.
A light-weight wrapper that will be a no-op if there is not a valid log reference,...
const String & currentLine() const
Returns a const reference to the current line being parsed.
Definition Scanner.h:108
void compileIndividualShader(File compile_file, File output_location)
Compiles a single shader file to SPIR-V.
Definition ShaderTools.h:27
ShaderTools()
Constructs ShaderTools and expands environment variables in the executable path.
Definition ShaderTools.h:20
LogPtr log
Log for compilation status messages.
Definition ShaderTools.h:18
void compileShader(File reference, const StringView &out_name, const StringView &value, bool is_simple)
Compiles a shader variant by enabling a preprocessor define and writing a temporary file.
Definition ShaderTools.h:38
void compileDefault()
Compiles all default shaders from the VulkanInterface resources directory.
Definition ShaderTools.h:88
void compileShader(File reference)
Compiles a single shader file to SPIR-V with the same base name.
Definition ShaderTools.h:70
File exe_location
Path to the glslangValidator executable.
Definition ShaderTools.h:17
void compileShaders(File reference)
Compiles all standard shader variants (points, linework, solid, etc.) from a reference file.
Definition ShaderTools.h:78
The core String View class for the NDEVR API.
Definition StringView.h:58
The core String class for the NDEVR API.
Definition String.h:95
bool beginsWith(const StringView &s, bool ignore_case=false) const
Tests if this String starts with the specified prefix.
The primary namespace for the NDEVR SDK.