Skip to content

Commit cab799f

Browse files
committed
matdbg would cause a crash if program was invalid
1 parent e95edef commit cab799f

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

filament/backend/src/opengl/OpenGLDriver.cpp

+12-8
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,13 @@ void OpenGLDriver::bindSampler(GLuint unit, GLuint sampler) noexcept {
304304
void OpenGLDriver::setPushConstant(backend::ShaderStage stage, uint8_t index,
305305
backend::PushConstantVariant value) {
306306
assert_invariant(stage == ShaderStage::VERTEX || stage == ShaderStage::FRAGMENT);
307+
308+
#if FILAMENT_ENABLE_MATDBG
309+
if (UTILS_UNLIKELY(!mValidProgram)) {
310+
return;
311+
}
312+
#endif
313+
307314
utils::Slice<std::pair<GLint, ConstantType>> constants;
308315
if (stage == ShaderStage::VERTEX) {
309316
constants = mCurrentPushConstants->vertexConstants;
@@ -340,15 +347,11 @@ void OpenGLDriver::bindTexture(GLuint unit, GLTexture const* t) noexcept {
340347
}
341348

342349
bool OpenGLDriver::useProgram(OpenGLProgram* p) noexcept {
343-
if (UTILS_UNLIKELY(!p->isValid())) {
344-
// If the program is not valid, we can't call use().
345-
return false;
346-
}
347-
348350
// set-up textures and samplers in the proper TMUs (as specified in setSamplers)
349-
p->use(this, mContext);
351+
bool const success = p->use(this, mContext);
352+
assert_invariant(success == p->isValid());
350353

351-
if (UTILS_UNLIKELY(mContext.isES2())) {
354+
if (UTILS_UNLIKELY(mContext.isES2() && success)) {
352355
for (uint32_t i = 0; i < Program::UNIFORM_BINDING_COUNT; i++) {
353356
auto [id, buffer, age] = mContext.getEs2UniformBinding(i);
354357
if (buffer) {
@@ -359,7 +362,8 @@ bool OpenGLDriver::useProgram(OpenGLProgram* p) noexcept {
359362
// when mPlatform.isSRGBSwapChainSupported() is false (no need to check though).
360363
p->setRec709ColorSpace(mRec709OutputColorspace);
361364
}
362-
return true;
365+
366+
return success;
363367
}
364368

365369

filament/backend/src/opengl/OpenGLProgram.h

+13-2
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,21 @@ class OpenGLProgram : public HwProgram {
5353

5454
bool isValid() const noexcept { return mToken || gl.program != 0; }
5555

56-
void use(OpenGLDriver* const gld, OpenGLContext& context) noexcept {
57-
if (UTILS_UNLIKELY(!gl.program)) {
56+
bool use(OpenGLDriver* const gld, OpenGLContext& context) noexcept {
57+
// both non-null is impossible by construction
58+
assert_invariant(!mToken || !gl.program);
59+
60+
if (UTILS_UNLIKELY(mToken && !gl.program)) {
61+
// first time a program is used
5862
initialize(*gld);
5963
}
6064

65+
if (UTILS_UNLIKELY(!gl.program)) {
66+
// compilation failed (token should be null)
67+
assert_invariant(!mToken);
68+
return false;
69+
}
70+
6171
context.useProgram(gl.program);
6272
if (UTILS_UNLIKELY(mUsedBindingsCount)) {
6373
// We rely on GL state tracking to avoid unnecessary glBindTexture / glBindSampler
@@ -74,6 +84,7 @@ class OpenGLProgram : public HwProgram {
7484

7585
updateSamplers(gld);
7686
}
87+
return true;
7788
}
7889

7990
// For ES2 only

filament/backend/src/opengl/ShaderCompilerService.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ ShaderCompilerService::program_token_t ShaderCompilerService::createProgram(
359359
GLuint ShaderCompilerService::getProgram(ShaderCompilerService::program_token_t& token) {
360360
GLuint const program = initialize(token);
361361
assert_invariant(token == nullptr);
362-
#ifndef FILAMENT_ENABLE_MATDBG
362+
#if !FILAMENT_ENABLE_MATDBG
363363
assert_invariant(program);
364364
#endif
365365
return program;

0 commit comments

Comments
 (0)