Debian Codegraph @

Sources/nCine/Graphics/RHI/GL/GLShader.h

raw
1 #pragma once
2
3 #ifndef DOXYGEN_GENERATING_OUTPUT
4 #define NCINE_INCLUDE_OPENGL
5 #include "../../../CommonHeaders.h"
6 #endif
7
8 #include "../../../../Main.h"
9
10 #include <Containers/StringView.h>
11
12 using namespace Death::Containers;
13
14 namespace nCine::RHI::GL
15 {
16 /**
17 @brief Wraps an OpenGL shader object
18
19 Manages the lifetime of a single shader object of a given stage (e.g., vertex or fragment),
20 loads its source from strings and/or a file, compiles it, and tracks the compilation status.
21 Compilation errors can be checked immediately or deferred.
22 */
23 class GLShader
24 {
25 public:
26 /** @brief Compilation status of the shader */
27 enum class Status
28 {
29 NotCompiled, /**< The shader has not been compiled yet */
30 CompilationFailed, /**< Compilation failed */
31 Compiled, /**< Compiled successfully */
32 CompiledWithDeferredChecks /**< Compiled, but the success status has not been checked yet */
33 };
34
35 /** @brief When the compilation status is checked */
36 enum class ErrorChecking
37 {
38 Immediate, /**< The compilation status is checked right after compiling */
39 Deferred /**< The compilation status check is postponed */
40 };
41
42 explicit GLShader(GLenum type);
43 GLShader(GLenum type, StringView filename);
44 ~GLShader();
45
46 GLShader(const GLShader&) = delete;
47 GLShader& operator=(const GLShader&) = delete;
48
49 /** @brief Returns the OpenGL handle of the shader object */
50 inline GLuint GetGLHandle() const {
51 return _glHandle;
52 }
53 /** @brief Returns the current compilation status */
54 inline Status GetStatus() const {
55 return _status;
56 }
57
58 /** @brief Loads the shader source from the given string */
59 bool LoadFromString(StringView string);
60 /** @brief Loads the shader source from the given string, then appends the specified file */
61 bool LoadFromStringAndFile(StringView string, StringView filename);
62 /** @brief Loads the shader source by concatenating the given strings in order */
63 bool LoadFromStrings(ArrayView<const StringView> strings);
64 /** @brief Loads the shader source by concatenating the given strings in order, then appends the specified file */
65 bool LoadFromStringsAndFile(ArrayView<const StringView> strings, StringView filename);
66 /** @brief Loads the shader source from the specified file */
67 bool LoadFromFile(StringView filename);
68
69 /**
70 * @brief Compiles the shader
71 *
72 * @param errorChecking Whether to check the compilation status immediately or defer it
73 * @param logOnErrors Whether to log the information log when compilation fails
74 * @return `true` on success, or when the status check is deferred
75 */
76 bool Compile(ErrorChecking errorChecking, bool logOnErrors);
77 /**
78 * @brief Checks the result of a deferred compilation
79 *
80 * @param logOnErrors Whether to log the information log when compilation failed
81 * @return `true` if the shader compiled successfully
82 */
83 bool CheckCompilation(bool logOnErrors);
84
85 /** @brief Sets an OpenGL object label for the shader, for debugging */
86 void SetObjectLabel(StringView label);
87
88 private:
89 static constexpr std::uint32_t MaxShaderSourceLength = 32 * 1024;
90
91 GLuint _glHandle;
92 Status _status;
93 };
94 }