Add SH_INIT_SHARED_VARIABLES flag
This option is used to initialize shared variables to zero at the
beginning of shader execution to avoid compute shaders being able to
read undefined values that could be coming from another webpage or
application.
It's implemented by declaring variables with initial value for HLSL.
For GLSL, it's not allowed to use declaraction initializer for shared
variables, so we need to explicitly assign them to zero at the
beginning of main(). This implementation is only for HLSL.
Bug: chromium:898030
Change-Id: Ic5906500bf4a35cd9a071923f82f32c5e2991be3
Reviewed-on: https://chromium-review.googlesource.com/c/1330310
Commit-Queue: Jie A Chen <jie.a.chen@intel.com>
Reviewed-by: Jamie Madill <jmadill@chromium.org>
diff --git a/src/compiler/translator/OutputHLSL.cpp b/src/compiler/translator/OutputHLSL.cpp
index fee56a4..05d892f 100644
--- a/src/compiler/translator/OutputHLSL.cpp
+++ b/src/compiler/translator/OutputHLSL.cpp
@@ -2028,12 +2028,11 @@
{
symbol->traverse(this);
out << ArrayString(symbol->getType());
- // We don't initialize shared variables because:
- // 1. It is very slow for D3D11 drivers to compile a compute shader if we add
- // code to initialize a groupshared array variable with a large array size.
- // 2. It is unnecessary to initialize shared variables, as GLSL even does not
- // allow initializing shared variables at all.
- if (declarator->getQualifier() != EvqShared)
+ // Add initializer only when requested. It is very slow for D3D11 drivers to
+ // compile a compute shader if we add code to initialize a groupshared array
+ // variable with a large array size.
+ if (declarator->getQualifier() != EvqShared ||
+ mCompileOptions & SH_INIT_SHARED_VARIABLES)
{
out << " = " + zeroInitializer(symbol->getType());
}