From 7f055e8a6897da057fefa2ed9d551bea59644510 Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Tue, 30 Oct 2018 10:45:41 -0700 Subject: [PATCH] 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. --- spirv_glsl.cpp | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/spirv_glsl.cpp b/spirv_glsl.cpp index 240c6050..0b3bb6a2 100644 --- a/spirv_glsl.cpp +++ b/spirv_glsl.cpp @@ -5856,11 +5856,21 @@ std::pair 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(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)