| 1 |
#include "GLShader.h" |
| 2 |
#include "GLDebug.h" |
| 3 |
#include "../../RenderResources.h" |
| 4 |
#include "../../../Application.h" |
| 5 |
#include "../../../../Main.h" |
| 6 |
|
| 7 |
#include <string> |
| 8 |
|
| 9 |
#include <IO/FileSystem.h> |
| 10 |
|
| 11 |
using namespace Death::Containers::Literals; |
| 12 |
using namespace Death::IO; |
| 13 |
|
| 14 |
namespace nCine::RHI::GL |
| 15 |
{ |
| 16 |
namespace |
| 17 |
{ |
| 18 |
#if defined(RHI_GL_PROFILE_ES2) |
| 19 |
// Real OpenGL|ES 2.0 profile: ESSL 100 (attribute/varying/gl_FragColor, no UBOs, no gl_VertexID). |
| 20 |
// The engine feeds the ProgramVariant's *100 (Essl100Emitter) sources on this path. |
| 21 |
static constexpr StringView CommonShaderVersion = "#version 100\n"_s; |
| 22 |
#elif (defined(RHI_GL_PROFILE_ES) && GL_ES_VERSION_3_0) || defined(DEATH_TARGET_EMSCRIPTEN) |
| 23 |
static constexpr StringView CommonShaderVersion = "#version 300 es\n"_s; |
| 24 |
#else |
| 25 |
static constexpr StringView CommonShaderVersion = "#version 330\n"_s; |
| 26 |
#endif |
| 27 |
|
| 28 |
#if defined(DEATH_TARGET_EMSCRIPTEN) |
| 29 |
static constexpr StringView CommonShaderDefines = "#define DEATH_TARGET_EMSCRIPTEN\n#line 0\n"_s; |
| 30 |
#elif defined(DEATH_TARGET_ANDROID) |
| 31 |
static constexpr StringView CommonShaderDefines = "#define DEATH_TARGET_ANDROID\n#line 0\n"_s; |
| 32 |
#elif defined(WITH_ANGLE) |
| 33 |
static constexpr StringView CommonShaderDefines = "#define WITH_ANGLE\n#line 0\n"_s; |
| 34 |
#else |
| 35 |
static constexpr StringView CommonShaderDefines = "#line 0\n"_s; |
| 36 |
#endif |
| 37 |
} |
| 38 |
|
| 39 |
GLShader::GLShader(GLenum type) |
| 40 |
: _glHandle(0), _status(Status::NotCompiled) |
| 41 |
{ |
| 42 |
_glHandle = glCreateShader(type); |
| 43 |
} |
| 44 |
|
| 45 |
GLShader::GLShader(GLenum type, StringView filename) |
| 46 |
: GLShader(type) |
| 47 |
{ |
| 48 |
LoadFromFile(filename); |
| 49 |
} |
| 50 |
|
| 51 |
GLShader::~GLShader() |
| 52 |
{ |
| 53 |
glDeleteShader(_glHandle); |
| 54 |
} |
| 55 |
|
| 56 |
bool GLShader::LoadFromString(StringView string) |
| 57 |
{ |
| 58 |
return LoadFromStringsAndFile(arrayView({ string }), {}); |
| 59 |
} |
| 60 |
|
| 61 |
bool GLShader::LoadFromStringAndFile(StringView string, StringView filename) |
| 62 |
{ |
| 63 |
return LoadFromStringsAndFile(arrayView({ string }), filename); |
| 64 |
} |
| 65 |
|
| 66 |
bool GLShader::LoadFromStrings(ArrayView<const StringView> strings) |
| 67 |
{ |
| 68 |
return LoadFromStringsAndFile(strings, {}); |
| 69 |
} |
| 70 |
|
| 71 |
bool GLShader::LoadFromStringsAndFile(ArrayView<const StringView> strings, StringView filename) |
| 72 |
{ |
| 73 |
if (strings.empty() && filename.empty()) { |
| 74 |
return false; |
| 75 |
} |
| 76 |
|
| 77 |
SmallVector<const char*, 10> sourceStrings; |
| 78 |
SmallVector<GLint, 10> sourceLengths; |
| 79 |
|
| 80 |
sourceStrings.push_back(CommonShaderVersion.data()); |
| 81 |
sourceLengths.push_back(static_cast<GLint>(CommonShaderVersion.size())); |
| 82 |
sourceStrings.push_back(CommonShaderDefines.data()); |
| 83 |
sourceLengths.push_back(static_cast<GLint>(CommonShaderDefines.size())); |
| 84 |
|
| 85 |
for (auto string : strings) { |
| 86 |
sourceStrings.push_back(string.data()); |
| 87 |
sourceLengths.push_back(static_cast<GLint>(string.size())); |
| 88 |
} |
| 89 |
|
| 90 |
String fileSource; |
| 91 |
if (!filename.empty()) { |
| 92 |
std::unique_ptr<Stream> fileHandle = fs::Open(filename, FileAccess::Read); |
| 93 |
if (!fileHandle->IsValid()) { |
| 94 |
return false; |
| 95 |
} |
| 96 |
|
| 97 |
const GLint fileLength = static_cast<int>(fileHandle->GetSize()); |
| 98 |
fileSource = String{NoInit, static_cast<std::size_t>(fileLength)}; |
| 99 |
fileHandle->Read(fileSource.data(), fileLength); |
| 100 |
|
| 101 |
sourceStrings.push_back(fileSource.data()); |
| 102 |
sourceLengths.push_back(static_cast<GLint>(fileLength)); |
| 103 |
|
| 104 |
SetObjectLabel(filename.data()); |
| 105 |
} |
| 106 |
|
| 107 |
std::size_t count = sourceStrings.size(); |
| 108 |
glShaderSource(_glHandle, GLsizei(count), sourceStrings.data(), sourceLengths.data()); |
| 109 |
return (count > 1); |
| 110 |
} |
| 111 |
|
| 112 |
bool GLShader::LoadFromFile(StringView filename) |
| 113 |
{ |
| 114 |
return LoadFromStringsAndFile({}, filename); |
| 115 |
} |
| 116 |
|
| 117 |
bool GLShader::Compile(ErrorChecking errorChecking, bool logOnErrors) |
| 118 |
{ |
| 119 |
glCompileShader(_glHandle); |
| 120 |
|
| 121 |
if (errorChecking == ErrorChecking::Immediate) { |
| 122 |
return CheckCompilation(logOnErrors); |
| 123 |
} else { |
| 124 |
_status = Status::CompiledWithDeferredChecks; |
| 125 |
return true; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
bool GLShader::CheckCompilation(bool logOnErrors) |
| 130 |
{ |
| 131 |
if (_status == Status::Compiled) { |
| 132 |
return true; |
| 133 |
} |
| 134 |
|
| 135 |
GLint status = GL_FALSE; |
| 136 |
glGetShaderiv(_glHandle, GL_COMPILE_STATUS, &status); |
| 137 |
if (status == GL_FALSE) { |
| 138 |
#if defined(DEATH_TRACE) |
| 139 |
if (logOnErrors) { |
| 140 |
GLint length = 0; |
| 141 |
glGetShaderiv(_glHandle, GL_INFO_LOG_LENGTH, &length); |
| 142 |
if (length > 0) { |
| 143 |
static char buffer[2048]; |
| 144 |
glGetShaderInfoLog(_glHandle, sizeof(buffer), &length, buffer); |
| 145 |
// Trim whitespace - driver messages usually contain newline(s) at the end |
| 146 |
*(MutableStringView(buffer).trimmed().end()) = '\0'; |
| 147 |
LOGW("Shader: {}", buffer); |
| 148 |
} |
| 149 |
#if !defined(RHI_GL_PROFILE_ES2) |
| 150 |
// The desktop ES2 test profile keeps effect shaders that fail the strict ESSL 100 compiler |
| 151 |
// non-fatal (logged, returns false) so the app still boots and the core sprite/menu path can be |
| 152 |
// validated; the GL 3.3 / ES 3.0 path keeps the immediate debug break on a shader error. |
| 153 |
DEATH_ASSERT_BREAK(); |
| 154 |
#endif |
| 155 |
} |
| 156 |
#endif |
| 157 |
_status = Status::CompilationFailed; |
| 158 |
return false; |
| 159 |
} |
| 160 |
|
| 161 |
_status = Status::Compiled; |
| 162 |
return true; |
| 163 |
} |
| 164 |
|
| 165 |
void GLShader::SetObjectLabel(StringView label) |
| 166 |
{ |
| 167 |
GLDebug::SetObjectLabel(GLDebug::LabelTypes::Shader, _glHandle, label); |
| 168 |
} |
| 169 |
} |