Fix Options::force_temporary to work with OpenGL GLSL

Setting force_temporary to true produces invalid GLSL because sampler
variables are copied:

    highp sampler2D _377 = DiffuseMapTexture;

This change fixes the problem by always forwarding forwardable
variables. I also took an opportunity to restructure the code to make
it easier to read and add extra conditions to in the future.
This commit is contained in:
Arseny Kapoulkine 2018-10-30 10:45:41 -07:00
parent 236df43689
commit 7f055e8a68

View File

@ -5856,11 +5856,21 @@ std::pair<std::string, uint32_t> CompilerGLSL::flattened_access_chain_offset(con
bool CompilerGLSL::should_forward(uint32_t id)
{
// Immutable expression can always be forwarded.
// If not immutable, we can speculate about it by forwarding potentially mutable variables.
// If id is a variable we will try to forward it regardless of force_temporary check below
// This is important because otherwise we'll get local sampler copies (highp sampler2D foo = bar) that are invalid in OpenGL GLSL
auto *var = maybe_get<SPIRVariable>(id);
bool forward = var ? var->forwardable : false;
return (is_immutable(id) || forward) && !options.force_temporary;
if (var && var->forwardable)
return true;
// For debugging emit temporary variables for all expressions
if (options.force_temporary)
return false;
// Immutable expression can always be forwarded.
if (is_immutable(id))
return true;
return false;
}
void CompilerGLSL::track_expression_read(uint32_t id)