diff --git a/filament/backend/include/private/backend/Program.h b/filament/backend/include/private/backend/Program.h index 416ad70c5d..6f19badd51 100644 --- a/filament/backend/include/private/backend/Program.h +++ b/filament/backend/include/private/backend/Program.h @@ -93,13 +93,16 @@ public: } using ShaderBlob = utils::FixedCapacityVector; - std::array const& getShadersSource() const noexcept { + using ShaderSource = std::array; + ShaderSource const& getShadersSource() const noexcept { return mShadersSource; } UniformBlockInfo const& getUniformBlockInfo() const noexcept { return mUniformBlocks; } + UniformBlockInfo& getUniformBlockInfo() noexcept { return mUniformBlocks; } SamplerGroupInfo const& getSamplerGroupInfo() const { return mSamplerGroups; } + SamplerGroupInfo& getSamplerGroupInfo() { return mSamplerGroups; } const utils::CString& getName() const noexcept { return mName; } @@ -110,7 +113,7 @@ private: UniformBlockInfo mUniformBlocks = {}; SamplerGroupInfo mSamplerGroups = {}; - std::array mShadersSource; + ShaderSource mShadersSource; bool mHasSamplers = false; utils::CString mName; utils::Invocable mLogger; diff --git a/filament/backend/src/DriverBase.h b/filament/backend/src/DriverBase.h index 28819bb094..842b926e46 100644 --- a/filament/backend/src/DriverBase.h +++ b/filament/backend/src/DriverBase.h @@ -98,12 +98,8 @@ struct HwRenderPrimitive : public HwBase { }; struct HwProgram : public HwBase { -#ifndef NDEBUG utils::CString name; explicit HwProgram(utils::CString name) noexcept : name(std::move(name)) { } -#else - explicit HwProgram(const utils::CString&) noexcept { } -#endif HwProgram() noexcept = default; }; diff --git a/filament/backend/src/opengl/OpenGLDriver.cpp b/filament/backend/src/opengl/OpenGLDriver.cpp index b37a6d0b69..33291da1f9 100644 --- a/filament/backend/src/opengl/OpenGLDriver.cpp +++ b/filament/backend/src/opengl/OpenGLDriver.cpp @@ -450,7 +450,7 @@ void OpenGLDriver::createRenderPrimitiveR(Handle rph, int) { void OpenGLDriver::createProgramR(Handle ph, Program&& program) { DEBUG_MARKER() - construct(ph, this, program); + construct(ph, mContext, std::move(program)); CHECK_GL_ERROR(utils::slog.e) } diff --git a/filament/backend/src/opengl/OpenGLProgram.cpp b/filament/backend/src/opengl/OpenGLProgram.cpp index 1050d16aac..1eb3f67588 100644 --- a/filament/backend/src/opengl/OpenGLProgram.cpp +++ b/filament/backend/src/opengl/OpenGLProgram.cpp @@ -35,43 +35,96 @@ using namespace backend; static void logCompilationError(utils::io::ostream& out, backend::Program::Shader shaderType, const char* name, - GLuint shaderId, std::string_view source) noexcept; + GLuint shaderId) noexcept; static void logProgramLinkError(utils::io::ostream& out, const char* name, GLuint program) noexcept; -OpenGLProgram::OpenGLProgram(OpenGLDriver* gl, const Program& programBuilder) noexcept - : HwProgram(programBuilder.getName()), mIsValid(false) { +OpenGLProgram::OpenGLProgram() noexcept = default; - using Shader = Program::Shader; +OpenGLProgram::OpenGLProgram(OpenGLContext& context, Program&& programBuilder) noexcept + : HwProgram(programBuilder.getName()) { - const auto& shadersSource = programBuilder.getShadersSource(); - OpenGLContext& context = gl->getContext(); + auto samplerGroupInfo = std::move(programBuilder.getSamplerGroupInfo()); + auto uniformBlockInfo = std::move(programBuilder.getUniformBlockInfo()); + + // this cannot fail because we check compilation status after linking the program + // shaders[] is filled with id of shader stages present. + OpenGLProgram::compileShaders(context, programBuilder.getShadersSource(), gl.shaders); + + // link the program, this also cannot fail because status is checked later. + // TODO: defer this until beginRenderPass() + gl.program = OpenGLProgram::linkProgram(gl.shaders); + + // check status of program linking and shader compilation, logs error and free all resources + // in case of error. + // TODO: defer this until first use + bool success = OpenGLProgram::checkProgramStatus(name.c_str_safe(), + gl.program, gl.shaders); + + // Failing to compile a program can't be fatal, because this will happen a lot in + // the material tools. We need to have a better way to handle these errors and + // return to the editor. + if (UTILS_UNLIKELY(!success)) { + PANIC_LOG("Failed to compile GLSL program."); + } + + if (success) { + // TODO: defer this until first use + initializeProgramState(context, gl.program, + uniformBlockInfo, samplerGroupInfo); + } +} + +OpenGLProgram::~OpenGLProgram() noexcept { + const GLuint program = gl.program; + UTILS_NOUNROLL + for (GLuint shader: gl.shaders) { + if (shader) { + if (program) { + glDetachShader(program, shader); + } + glDeleteShader(shader); + } + } + if (program) { + glDeleteProgram(program); + } +} + +/* + * Compile shaders in the ShaderSource. This cannot fail because compilation failures are not + * checked until after the program is linked. + * This always returns the GL shader IDs or zero a shader stage is not present. + */ +void OpenGLProgram::compileShaders(OpenGLContext& context, + Program::ShaderSource const& shadersSource, + GLuint shaderIds[Program::SHADER_TYPE_COUNT]) noexcept { // build all shaders - #pragma nounroll + UTILS_NOUNROLL for (size_t i = 0; i < Program::SHADER_TYPE_COUNT; i++) { + Program::Shader type = static_cast(i); GLenum glShaderType; - Shader type = (Shader)i; switch (type) { - case Shader::VERTEX: + case Program::Shader::VERTEX: glShaderType = GL_VERTEX_SHADER; break; - case Shader::FRAGMENT: + case Program::Shader::FRAGMENT: glShaderType = GL_FRAGMENT_SHADER; break; } - if (!shadersSource[i].empty()) { - GLint status; - Program::ShaderBlob shader = shadersSource[i]; + if (UTILS_LIKELY(!shadersSource[i].empty())) { + Program::ShaderBlob const& shader = shadersSource[i]; + std::string_view shaderView(reinterpret_cast(shader.data()), shader.size()); std::string temp; - std::string_view shaderView((const char*)shader.data(), shader.size()); if (!context.ext.GOOGLE_cpp_style_line_directive) { // If usages of the Google-style line directive are present, remove them, as some // drivers don't allow the quotation marks. - if (UTILS_UNLIKELY(requestsGoogleLineDirectivesExtension(shaderView.data(), shaderView.size()))) { + if (UTILS_UNLIKELY(requestsGoogleLineDirectivesExtension( + shaderView.data(), shaderView.size()))) { temp = shaderView; // copy string removeGoogleLineDirectives(temp.data(), temp.size()); // length is unaffected shaderView = temp; @@ -138,124 +191,119 @@ highp uint packHalf2x16(vec2 v) { glCompileShader(shaderId); } - glGetShaderiv(shaderId, GL_COMPILE_STATUS, &status); - if (UTILS_UNLIKELY(status != GL_TRUE)) { - logCompilationError(slog.e, type, - programBuilder.getName().c_str_safe(), shaderId, shaderView); - glDeleteShader(shaderId); - return; - } - this->gl.shaders[i] = shaderId; - mValidShaderSet |= 1U << i; + shaderIds[i] = shaderId; } } - - // we need at least a vertex and fragment program - const uint8_t validShaderSet = mValidShaderSet; - const uint8_t mask = VERTEX_SHADER_BIT | FRAGMENT_SHADER_BIT; - if (UTILS_LIKELY((mValidShaderSet & mask) == mask)) { - GLint status; - GLuint program = glCreateProgram(); - for (size_t i = 0; i < Program::SHADER_TYPE_COUNT; i++) { - if (validShaderSet & (1U << i)) { - glAttachShader(program, this->gl.shaders[i]); - } - } - glLinkProgram(program); - - glGetProgramiv(program, GL_LINK_STATUS, &status); - if (UTILS_UNLIKELY(status != GL_TRUE)) { - logProgramLinkError(slog.e, programBuilder.getName().c_str_safe(), program); - glDeleteProgram(program); - return; - } - - this->gl.program = program; - - // Associate each UniformBlock in the program to a known binding. - auto const& uniformBlockInfo = programBuilder.getUniformBlockInfo(); - #pragma nounroll - for (GLuint binding = 0, n = uniformBlockInfo.size(); binding < n; binding++) { - auto const& name = uniformBlockInfo[binding]; - if (!name.empty()) { - GLint index = glGetUniformBlockIndex(program, name.c_str()); - if (index >= 0) { - glUniformBlockBinding(program, GLuint(index), binding); - } - CHECK_GL_ERROR(utils::slog.e) - } - } - - if (programBuilder.hasSamplers()) { - // if we have samplers, we need to do a bit of extra work - // activate this program so we can set all its samplers once and for all (glUniform1i) - context.useProgram(program); - - auto const& samplerGroupInfo = programBuilder.getSamplerGroupInfo(); - auto& indicesRun = mIndicesRuns; - uint8_t numUsedBindings = 0; - uint8_t tmu = 0; - - #pragma nounroll - for (size_t i = 0, c = samplerGroupInfo.size(); i < c; i++) { - auto const& groupInfo = samplerGroupInfo[i]; - auto const& samplers = groupInfo.samplers; - if (!samplers.empty()) { - // Cache the sampler uniform locations for each interface block - BlockInfo& info = mBlockInfos[numUsedBindings]; - info.binding = uint8_t(i); - uint8_t count = 0; - for (uint8_t j = 0, m = uint8_t(samplers.size()); j < m; ++j) { - // find its location and associate a TMU to it - GLint loc = glGetUniformLocation(program, samplers[j].name.c_str()); - if (loc >= 0) { - glUniform1i(loc, tmu); - indicesRun[tmu] = j; - count++; - tmu++; - } else { - // glGetUniformLocation could fail if the uniform is not used - // in the program. We should just ignore the error in that case. - } - } - if (count > 0) { - numUsedBindings++; - info.count = uint8_t(count - 1); - } - } - } - mUsedBindingsCount = numUsedBindings; - } - mIsValid = true; - } - - // Failing to compile a program can't be fatal, because this will happen a lot in - // the material tools. We need to have a better way to handle these errors and - // return to the editor. Also note the early "return" statements in this function. - if (UTILS_UNLIKELY(!isValid())) { - PANIC_LOG("Failed to compile GLSL program."); - } } -OpenGLProgram::~OpenGLProgram() noexcept { - const size_t validShaderSet = mValidShaderSet; - const bool isValid = mIsValid; - GLuint program = gl.program; - if (validShaderSet) { - UTILS_NOUNROLL - for (size_t i = 0; i < Program::SHADER_TYPE_COUNT; i++) { - if (validShaderSet & (1U << i)) { - const GLuint shader = gl.shaders[i]; - if (isValid) { - glDetachShader(program, shader); +/* + * Create a program from the given shader IDs and links it. This cannot fail because errors + * are checked later. This always returns a valid GL program ID (which doesn't mean the + * program itself is valid). + */ +GLuint OpenGLProgram::linkProgram(const GLuint shaderIds[Program::SHADER_TYPE_COUNT]) noexcept { + GLuint program = glCreateProgram(); + for (size_t i = 0; i < Program::SHADER_TYPE_COUNT; i++) { + if (shaderIds[i]) { + glAttachShader(program, shaderIds[i]); + } + } + glLinkProgram(program); + return program; +} + +/* + * Checks a program link status and logs errors and frees resources on failure. + * Returns true on success. + */ +bool OpenGLProgram::checkProgramStatus(const char* name, + GLuint& program, GLuint shaderIds[backend::Program::SHADER_TYPE_COUNT]) noexcept { + + GLint status; + glGetProgramiv(program, GL_LINK_STATUS, &status); + if (UTILS_LIKELY(status == GL_TRUE)) { + return true; + } + + // only if the link fails, we check the compilation status + UTILS_NOUNROLL + for (size_t i = 0; i < Program::SHADER_TYPE_COUNT; i++) { + const Program::Shader type = static_cast(i); + const GLuint shader = shaderIds[i]; + glGetShaderiv(shader, GL_COMPILE_STATUS, &status); + if (status != GL_TRUE) { + logCompilationError(slog.e, type, name, shader); + } + glDetachShader(program, shader); + glDeleteShader(shader); + shaderIds[i] = 0; + } + // log the link error as well + logProgramLinkError(slog.e, name, program); + glDeleteProgram(program); + program = 0; + return false; +} + +/* + * Initializes our internal state from a valid program. This must only be called after + * checkProgramStatus() has been successfully called. + */ +void OpenGLProgram::initializeProgramState(OpenGLContext& context, GLuint program, + Program::UniformBlockInfo const& uniformBlockInfo, + Program::SamplerGroupInfo const& samplerGroupInfo) noexcept { + + // Associate each UniformBlock in the program to a known binding. + UTILS_NOUNROLL + for (GLuint binding = 0, n = uniformBlockInfo.size(); binding < n; binding++) { + auto const& name = uniformBlockInfo[binding]; + if (!name.empty()) { + GLint index = glGetUniformBlockIndex(program, name.c_str()); + if (index >= 0) { + glUniformBlockBinding(program, GLuint(index), binding); + } + CHECK_GL_ERROR(utils::slog.e) + } + } + + auto& indicesRun = mIndicesRuns; + uint8_t numUsedBindings = 0; + uint8_t tmu = 0; + + UTILS_NOUNROLL + for (size_t i = 0, c = samplerGroupInfo.size(); i < c; i++) { + auto const& groupInfo = samplerGroupInfo[i]; + auto const& samplers = groupInfo.samplers; + if (!samplers.empty()) { + + // keep this in the loop so we skip it in the rare case a program doesn't have + // sampler. The context cache will prevent repeated calls to GL. + context.useProgram(program); + + // Cache the sampler uniform locations for each interface block + BlockInfo& info = mBlockInfos[numUsedBindings]; + info.binding = uint8_t(i); + uint8_t count = 0; + for (uint8_t j = 0, m = uint8_t(samplers.size()); j < m; ++j) { + // find its location and associate a TMU to it + GLint loc = glGetUniformLocation(program, samplers[j].name.c_str()); + if (loc >= 0) { + glUniform1i(loc, tmu); + indicesRun[tmu] = j; + count++; + tmu++; + } else { + // glGetUniformLocation could fail if the uniform is not used + // in the program. We should just ignore the error in that case. } - glDeleteShader(shader); + } + if (count > 0) { + numUsedBindings++; + info.count = uint8_t(count - 1); } } } - if (isValid) { - glDeleteProgram(program); - } + mUsedBindingsCount = numUsedBindings; } void OpenGLProgram::updateSamplers(OpenGLDriver* gld) noexcept { @@ -341,7 +389,7 @@ void OpenGLProgram::updateSamplers(OpenGLDriver* gld) noexcept { UTILS_NOINLINE void logCompilationError(io::ostream& out, Program::Shader shaderType, - const char* name, GLuint shaderId, std::string_view shader) noexcept { + const char* name, GLuint shaderId) noexcept { auto to_string = [](Program::Shader type) -> const char* { switch (type) { @@ -356,24 +404,6 @@ void logCompilationError(io::ostream& out, Program::Shader shaderType, out << "Compilation error in " << to_string(shaderType) << " shader \"" << name << "\":\n" << "\"" << error << "\"" << io::endl; - - size_t lc = 1; - size_t start = 0; - std::string line; - while (true) { - size_t end = shader.find('\n', start); - if (end == std::string::npos) { - line = shader.substr(start); - } else { - line = shader.substr(start, end - start); - } - out << lc++ << ": "<< line.c_str() << '\n'; - if (end == std::string::npos) { - break; - } - start = end + 1; - } - out << io::endl; } UTILS_NOINLINE diff --git a/filament/backend/src/opengl/OpenGLProgram.h b/filament/backend/src/opengl/OpenGLProgram.h index dd41ea3693..02360817f3 100644 --- a/filament/backend/src/opengl/OpenGLProgram.h +++ b/filament/backend/src/opengl/OpenGLProgram.h @@ -37,13 +37,13 @@ namespace filament { class OpenGLProgram : public backend::HwProgram { public: - OpenGLProgram() noexcept = default; - OpenGLProgram(OpenGLDriver* gl, const backend::Program& builder) noexcept; + OpenGLProgram() noexcept; + OpenGLProgram(OpenGLContext& context, backend::Program&& builder) noexcept; ~OpenGLProgram() noexcept; - bool isValid() const noexcept { return mIsValid; } + bool isValid() const noexcept { return gl.program != 0; } - void use(OpenGLDriver* const gl) noexcept { + void use(OpenGLDriver* const gld) noexcept { if (UTILS_UNLIKELY(mUsedBindingsCount)) { // We rely on GL state tracking to avoid unnecessary glBindTexture / glBindSampler // calls. @@ -57,13 +57,13 @@ public: // a bit less. Compared to what updateSamplers() actually does, which is // pretty little, I'm not sure we'll get ahead. - updateSamplers(gl); + updateSamplers(gld); } } struct { - GLuint shaders[backend::Program::SHADER_TYPE_COUNT]; - GLuint program; + GLuint shaders[backend::Program::SHADER_TYPE_COUNT] = {}; + GLuint program = 0; } gl; // 12 bytes private: @@ -71,6 +71,21 @@ private: static constexpr uint8_t VERTEX_SHADER_BIT = uint8_t(1) << size_t(backend::Program::Shader::VERTEX); static constexpr uint8_t FRAGMENT_SHADER_BIT = uint8_t(1) << size_t(backend::Program::Shader::FRAGMENT); + static void compileShaders(OpenGLContext& context, + const backend::Program::ShaderSource& shadersSource, + GLuint shaderIds[backend::Program::SHADER_TYPE_COUNT]) noexcept; + + static GLuint linkProgram(const GLuint shaderIds[backend::Program::SHADER_TYPE_COUNT]) noexcept; + + static bool checkProgramStatus(const char* name, + GLuint& program, GLuint shaderIds[backend::Program::SHADER_TYPE_COUNT]) noexcept; + + void initializeProgramState(OpenGLContext& context, GLuint program, + backend::Program::UniformBlockInfo const& uniformBlockInfo, + backend::Program::SamplerGroupInfo const& samplers) noexcept; + + void updateSamplers(OpenGLDriver* gld) noexcept; + struct BlockInfo { uint8_t binding : 3; // binding (i.e.: index in mSamplerBindings) uint8_t count : 5; // number of TMUs actually used minus 1 @@ -87,16 +102,12 @@ private: static_assert(sizeof(BlockInfo) == sizeof(uint8_t), "BlockInfo must be 8 bits"); uint8_t mUsedBindingsCount = 0; - uint8_t mValidShaderSet = 0; - bool mIsValid = false; // information about each USED sampler buffer (no gaps) std::array mBlockInfos; // 8 bytes // runs of indices into SamplerGroup -- run start index and size given by BlockInfo - std::array mIndicesRuns; // 16 bytes - - void updateSamplers(OpenGLDriver* gld) noexcept; + std::array mIndicesRuns; // 32 bytes };