2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2015-01-16 02:40:39 +03:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "WebGLShaderValidator.h"
|
|
|
|
|
2015-10-12 06:21:03 +03:00
|
|
|
#include "gfxPrefs.h"
|
2015-01-16 02:40:39 +03:00
|
|
|
#include "GLContext.h"
|
2018-03-30 02:54:09 +03:00
|
|
|
#include "mozilla/gfx/Logging.h"
|
2015-04-03 03:52:54 +03:00
|
|
|
#include "mozilla/Preferences.h"
|
2015-01-16 02:40:39 +03:00
|
|
|
#include "MurmurHash3.h"
|
|
|
|
#include "nsPrintfCString.h"
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include "WebGLContext.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace webgl {
|
|
|
|
|
|
|
|
uint64_t IdentifierHashFunc(const char* name, size_t len) {
|
|
|
|
// NB: we use the x86 function everywhere, even though it's suboptimal perf
|
|
|
|
// on x64. They return different results; not sure if that's a requirement.
|
|
|
|
uint64_t hash[2];
|
|
|
|
MurmurHash3_x86_128(name, len, 0, hash);
|
|
|
|
return hash[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
static ShCompileOptions ChooseValidatorCompileOptions(
|
|
|
|
const ShBuiltInResources& resources, const mozilla::gl::GLContext* gl) {
|
2016-12-22 03:37:25 +03:00
|
|
|
ShCompileOptions options = SH_VARIABLES | SH_ENFORCE_PACKING_RESTRICTIONS |
|
2017-10-11 23:02:56 +03:00
|
|
|
SH_OBJECT_CODE | SH_INIT_GL_POSITION |
|
|
|
|
SH_INITIALIZE_UNINITIALIZED_LOCALS |
|
|
|
|
SH_INIT_OUTPUT_VARIABLES;
|
2015-01-16 02:40:39 +03:00
|
|
|
|
|
|
|
#ifndef XP_MACOSX
|
|
|
|
// We want to do this everywhere, but to do this on Mac, we need
|
|
|
|
// to do it only on Mac OSX > 10.6 as this causes the shader
|
|
|
|
// compiler in 10.6 to crash
|
|
|
|
options |= SH_CLAMP_INDIRECT_ARRAY_BOUNDS;
|
|
|
|
#endif
|
|
|
|
|
2017-06-23 13:18:40 +03:00
|
|
|
if (gl->WorkAroundDriverBugs()) {
|
2017-06-23 04:54:16 +03:00
|
|
|
#ifdef XP_MACOSX
|
2015-01-16 02:40:39 +03:00
|
|
|
// Work around https://bugs.webkit.org/show_bug.cgi?id=124684,
|
|
|
|
// https://chromium.googlesource.com/angle/angle/+/5e70cf9d0b1bb
|
|
|
|
options |= SH_UNFOLD_SHORT_CIRCUIT;
|
|
|
|
|
2016-05-24 03:41:00 +03:00
|
|
|
// Work around that Mac drivers handle struct scopes incorrectly.
|
|
|
|
options |= SH_REGENERATE_STRUCT_NAMES;
|
2016-10-26 02:29:00 +03:00
|
|
|
options |= SH_INIT_OUTPUT_VARIABLES;
|
2017-06-26 18:39:30 +03:00
|
|
|
|
|
|
|
// Work around that Intel drivers on Mac OSX handle for-loop incorrectly.
|
|
|
|
if (gl->Vendor() == gl::GLVendor::Intel) {
|
|
|
|
options |= SH_ADD_AND_TRUE_TO_LOOP_CONDITION;
|
|
|
|
}
|
2017-06-23 04:54:16 +03:00
|
|
|
#endif
|
2017-06-20 07:00:49 +03:00
|
|
|
|
2017-06-23 04:54:16 +03:00
|
|
|
if (!gl->IsANGLE() && gl->Vendor() == gl::GLVendor::Intel) {
|
|
|
|
// Failures on at least Windows+Intel+OGL on:
|
|
|
|
// conformance/glsl/constructors/glsl-construct-mat2.html
|
|
|
|
options |= SH_SCALARIZE_VEC_AND_MAT_CONSTRUCTOR_ARGS;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-01-16 02:40:39 +03:00
|
|
|
|
2016-12-22 03:37:25 +03:00
|
|
|
if (gfxPrefs::WebGLAllANGLEOptions()) {
|
|
|
|
options = -1;
|
|
|
|
|
|
|
|
options ^= SH_INTERMEDIATE_TREE;
|
|
|
|
options ^= SH_LINE_DIRECTIVES;
|
|
|
|
options ^= SH_SOURCE_PATH;
|
|
|
|
|
|
|
|
options ^= SH_LIMIT_EXPRESSION_COMPLEXITY;
|
|
|
|
options ^= SH_LIMIT_CALL_STACK_DEPTH;
|
|
|
|
|
|
|
|
options ^= SH_EXPAND_SELECT_HLSL_INTEGER_POW_EXPRESSIONS;
|
|
|
|
options ^= SH_HLSL_GET_DIMENSIONS_IGNORES_BASE_LEVEL;
|
|
|
|
|
|
|
|
options ^= SH_DONT_REMOVE_INVARIANT_FOR_FRAGMENT_INPUT;
|
|
|
|
options ^= SH_REMOVE_INVARIANT_AND_CENTROID_FOR_ESSL3;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (resources.MaxExpressionComplexity > 0) {
|
|
|
|
options |= SH_LIMIT_EXPRESSION_COMPLEXITY;
|
|
|
|
}
|
|
|
|
if (resources.MaxCallStackDepth > 0) {
|
|
|
|
options |= SH_LIMIT_CALL_STACK_DEPTH;
|
|
|
|
}
|
|
|
|
|
2015-01-16 02:40:39 +03:00
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace webgl
|
|
|
|
|
|
|
|
////////////////////////////////////////
|
|
|
|
|
2018-10-26 19:30:48 +03:00
|
|
|
static ShShaderOutput ShaderOutput(gl::GLContext* gl) {
|
2015-11-27 23:54:50 +03:00
|
|
|
if (gl->IsGLES()) {
|
|
|
|
return SH_ESSL_OUTPUT;
|
|
|
|
} else {
|
|
|
|
uint32_t version = gl->ShadingLanguageVersion();
|
|
|
|
switch (version) {
|
|
|
|
case 100:
|
|
|
|
return SH_GLSL_COMPATIBILITY_OUTPUT;
|
|
|
|
case 120:
|
|
|
|
return SH_GLSL_COMPATIBILITY_OUTPUT;
|
|
|
|
case 130:
|
|
|
|
return SH_GLSL_130_OUTPUT;
|
|
|
|
case 140:
|
|
|
|
return SH_GLSL_140_OUTPUT;
|
|
|
|
case 150:
|
|
|
|
return SH_GLSL_150_CORE_OUTPUT;
|
|
|
|
case 330:
|
|
|
|
return SH_GLSL_330_CORE_OUTPUT;
|
|
|
|
case 400:
|
|
|
|
return SH_GLSL_400_CORE_OUTPUT;
|
|
|
|
case 410:
|
|
|
|
return SH_GLSL_410_CORE_OUTPUT;
|
|
|
|
case 420:
|
|
|
|
return SH_GLSL_420_CORE_OUTPUT;
|
|
|
|
case 430:
|
|
|
|
return SH_GLSL_430_CORE_OUTPUT;
|
|
|
|
case 440:
|
|
|
|
return SH_GLSL_440_CORE_OUTPUT;
|
|
|
|
default:
|
2018-03-30 02:54:09 +03:00
|
|
|
if (version >= 450) {
|
|
|
|
// "OpenGL 4.6 is also guaranteed to support all previous versions of
|
2018-11-27 13:08:58 +03:00
|
|
|
// the OpenGL Shading Language back to version 1.10."
|
2018-03-30 02:54:09 +03:00
|
|
|
return SH_GLSL_450_CORE_OUTPUT;
|
2015-11-27 23:54:50 +03:00
|
|
|
}
|
2018-03-30 02:54:09 +03:00
|
|
|
gfxCriticalNote << "Unexpected GLSL version: " << version;
|
2015-11-27 23:54:50 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-11-27 23:54:50 +03:00
|
|
|
|
2016-03-16 03:26:14 +03:00
|
|
|
return SH_GLSL_COMPATIBILITY_OUTPUT;
|
2015-11-27 23:54:50 +03:00
|
|
|
}
|
|
|
|
|
2015-01-16 02:40:39 +03:00
|
|
|
webgl::ShaderValidator* WebGLContext::CreateShaderValidator(
|
|
|
|
GLenum shaderType) const {
|
|
|
|
if (mBypassShaderValidation) return nullptr;
|
|
|
|
|
2016-07-21 11:15:02 +03:00
|
|
|
const auto spec = (IsWebGL2() ? SH_WEBGL2_SPEC : SH_WEBGL_SPEC);
|
|
|
|
const auto outputLanguage = ShaderOutput(gl);
|
2015-12-17 00:14:37 +03:00
|
|
|
|
2015-01-16 02:40:39 +03:00
|
|
|
ShBuiltInResources resources;
|
|
|
|
memset(&resources, 0, sizeof(resources));
|
2017-05-24 10:20:02 +03:00
|
|
|
sh::InitBuiltInResources(&resources);
|
2015-01-16 02:40:39 +03:00
|
|
|
|
|
|
|
resources.HashFunction = webgl::IdentifierHashFunc;
|
|
|
|
|
|
|
|
resources.MaxVertexAttribs = mGLMaxVertexAttribs;
|
|
|
|
resources.MaxVertexUniformVectors = mGLMaxVertexUniformVectors;
|
|
|
|
resources.MaxVaryingVectors = mGLMaxVaryingVectors;
|
|
|
|
resources.MaxVertexTextureImageUnits = mGLMaxVertexTextureImageUnits;
|
2017-07-27 10:49:21 +03:00
|
|
|
resources.MaxCombinedTextureImageUnits = mGLMaxCombinedTextureImageUnits;
|
|
|
|
resources.MaxTextureImageUnits = mGLMaxFragmentTextureImageUnits;
|
2015-01-16 02:40:39 +03:00
|
|
|
resources.MaxFragmentUniformVectors = mGLMaxFragmentUniformVectors;
|
2016-02-06 03:42:33 +03:00
|
|
|
|
|
|
|
const bool hasMRTs =
|
|
|
|
(IsWebGL2() || IsExtensionEnabled(WebGLExtensionID::WEBGL_draw_buffers));
|
|
|
|
resources.MaxDrawBuffers = (hasMRTs ? mGLMaxDrawBuffers : 1);
|
2015-01-16 02:40:39 +03:00
|
|
|
|
2016-01-25 00:21:00 +03:00
|
|
|
if (IsExtensionEnabled(WebGLExtensionID::EXT_frag_depth))
|
2015-01-16 02:40:39 +03:00
|
|
|
resources.EXT_frag_depth = 1;
|
|
|
|
|
2016-01-25 00:21:00 +03:00
|
|
|
if (IsExtensionEnabled(WebGLExtensionID::OES_standard_derivatives))
|
2015-01-16 02:40:39 +03:00
|
|
|
resources.OES_standard_derivatives = 1;
|
|
|
|
|
2016-01-25 00:21:00 +03:00
|
|
|
if (IsExtensionEnabled(WebGLExtensionID::WEBGL_draw_buffers))
|
2015-01-16 02:40:39 +03:00
|
|
|
resources.EXT_draw_buffers = 1;
|
|
|
|
|
2016-01-25 00:21:00 +03:00
|
|
|
if (IsExtensionEnabled(WebGLExtensionID::EXT_shader_texture_lod))
|
2015-01-16 02:40:39 +03:00
|
|
|
resources.EXT_shader_texture_lod = 1;
|
|
|
|
|
|
|
|
// Tell ANGLE to allow highp in frag shaders. (unless disabled)
|
|
|
|
// If underlying GLES doesn't have highp in frag shaders, it should complain
|
|
|
|
// anyways.
|
|
|
|
resources.FragmentPrecisionHigh = mDisableFragHighP ? 0 : 1;
|
|
|
|
|
|
|
|
if (gl->WorkAroundDriverBugs()) {
|
|
|
|
#ifdef XP_MACOSX
|
|
|
|
if (gl->Vendor() == gl::GLVendor::NVIDIA) {
|
|
|
|
// Work around bug 890432
|
|
|
|
resources.MaxExpressionComplexity = 1000;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
#endif
|
|
|
|
}
|
2015-01-16 02:40:39 +03:00
|
|
|
|
2016-12-22 03:37:25 +03:00
|
|
|
const auto compileOptions =
|
|
|
|
webgl::ChooseValidatorCompileOptions(resources, gl);
|
2015-01-16 02:40:39 +03:00
|
|
|
return webgl::ShaderValidator::Create(shaderType, spec, outputLanguage,
|
|
|
|
resources, compileOptions);
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////
|
|
|
|
|
|
|
|
namespace webgl {
|
|
|
|
|
|
|
|
/*static*/ ShaderValidator* ShaderValidator::Create(
|
|
|
|
GLenum shaderType, ShShaderSpec spec, ShShaderOutput outputLanguage,
|
2016-12-22 03:37:25 +03:00
|
|
|
const ShBuiltInResources& resources, ShCompileOptions compileOptions) {
|
2017-05-24 10:20:02 +03:00
|
|
|
ShHandle handle =
|
|
|
|
sh::ConstructCompiler(shaderType, spec, outputLanguage, &resources);
|
2015-01-16 02:40:39 +03:00
|
|
|
if (!handle) return nullptr;
|
|
|
|
|
|
|
|
return new ShaderValidator(handle, compileOptions,
|
2017-05-24 10:20:02 +03:00
|
|
|
resources.MaxVaryingVectors);
|
2015-01-16 02:40:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ShaderValidator::~ShaderValidator() { sh::Destruct(mHandle); }
|
|
|
|
|
|
|
|
bool ShaderValidator::ValidateAndTranslate(const char* source) {
|
|
|
|
MOZ_ASSERT(!mHasRun);
|
|
|
|
mHasRun = true;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-01-16 02:40:39 +03:00
|
|
|
const char* const parts[] = {source};
|
2017-05-24 10:20:02 +03:00
|
|
|
return sh::Compile(mHandle, parts, ArrayLength(parts), mCompileOptions);
|
2015-01-16 02:40:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void ShaderValidator::GetInfoLog(nsACString* out) const {
|
|
|
|
MOZ_ASSERT(mHasRun);
|
|
|
|
|
2017-05-24 10:20:02 +03:00
|
|
|
const std::string& log = sh::GetInfoLog(mHandle);
|
2015-02-05 22:39:18 +03:00
|
|
|
out->Assign(log.data(), log.length());
|
2015-01-16 02:40:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void ShaderValidator::GetOutput(nsACString* out) const {
|
|
|
|
MOZ_ASSERT(mHasRun);
|
|
|
|
|
2017-05-24 10:20:02 +03:00
|
|
|
const std::string& output = sh::GetObjectCode(mHandle);
|
2015-02-05 22:39:18 +03:00
|
|
|
out->Assign(output.data(), output.length());
|
2015-01-16 02:40:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template <size_t N>
|
|
|
|
static bool StartsWith(const std::string& haystack, const char (&needle)[N]) {
|
|
|
|
return haystack.compare(0, N - 1, needle) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ShaderValidator::CanLinkTo(const ShaderValidator* prev,
|
|
|
|
nsCString* const out_log) const {
|
2015-08-11 17:17:16 +03:00
|
|
|
if (!prev) {
|
|
|
|
nsPrintfCString error("Passed in NULL prev ShaderValidator.");
|
2015-12-10 17:58:18 +03:00
|
|
|
*out_log = error;
|
|
|
|
return false;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2017-05-24 10:20:02 +03:00
|
|
|
const auto shaderVersion = sh::GetShaderVersion(mHandle);
|
|
|
|
if (sh::GetShaderVersion(prev->mHandle) != shaderVersion) {
|
2015-12-10 17:58:18 +03:00
|
|
|
nsPrintfCString error(
|
|
|
|
"Vertex shader version %d does not match"
|
|
|
|
" fragment shader version %d.",
|
2017-05-24 10:20:02 +03:00
|
|
|
sh::GetShaderVersion(prev->mHandle), sh::GetShaderVersion(mHandle));
|
2015-08-11 17:17:16 +03:00
|
|
|
*out_log = error;
|
2015-12-10 17:58:18 +03:00
|
|
|
return false;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2017-05-24 10:20:02 +03:00
|
|
|
const std::vector<sh::Uniform>* vertPtr = sh::GetUniforms(prev->mHandle);
|
|
|
|
const std::vector<sh::Uniform>* fragPtr = sh::GetUniforms(mHandle);
|
2015-08-11 17:17:16 +03:00
|
|
|
if (!vertPtr || !fragPtr) {
|
|
|
|
nsPrintfCString error("Could not create uniform list.");
|
2015-12-10 17:58:18 +03:00
|
|
|
*out_log = error;
|
2015-08-11 17:17:16 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto itrFrag = fragPtr->begin(); itrFrag != fragPtr->end();
|
|
|
|
++itrFrag) {
|
2017-05-24 10:20:02 +03:00
|
|
|
for (auto itrVert = vertPtr->begin(); itrVert != vertPtr->end();
|
2015-08-11 17:17:16 +03:00
|
|
|
++itrVert) {
|
2017-05-24 10:20:02 +03:00
|
|
|
if (itrVert->name != itrFrag->name) continue;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-01-16 02:40:39 +03:00
|
|
|
if (!itrVert->isSameUniformAtLinkTime(*itrFrag)) {
|
2015-08-11 17:17:16 +03:00
|
|
|
nsPrintfCString error(
|
2016-12-31 05:52:49 +03:00
|
|
|
"Uniform `%s` is not linkable between"
|
2017-05-24 10:20:02 +03:00
|
|
|
" attached shaders.",
|
2015-01-16 02:40:39 +03:00
|
|
|
itrFrag->name.c_str());
|
2015-08-11 17:17:16 +03:00
|
|
|
*out_log = error;
|
2017-05-24 10:20:02 +03:00
|
|
|
return false;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
2017-05-24 10:20:02 +03:00
|
|
|
const auto vertVars = sh::GetInterfaceBlocks(prev->mHandle);
|
|
|
|
const auto fragVars = sh::GetInterfaceBlocks(mHandle);
|
|
|
|
if (!vertVars || !fragVars) {
|
2015-12-10 17:58:18 +03:00
|
|
|
nsPrintfCString error("Could not create uniform block list.");
|
|
|
|
*out_log = error;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-05-24 10:20:02 +03:00
|
|
|
for (const auto& fragVar : *fragVars) {
|
|
|
|
for (const auto& vertVar : *vertVars) {
|
|
|
|
if (vertVar.name != fragVar.name) continue;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-08-11 17:17:16 +03:00
|
|
|
if (!vertVar.isSameInterfaceBlockAtLinkTime(fragVar)) {
|
|
|
|
nsPrintfCString error(
|
|
|
|
"Interface block `%s` is not linkable between"
|
|
|
|
" attached shaders.",
|
|
|
|
fragVar.name.c_str());
|
|
|
|
*out_log = error;
|
|
|
|
return false;
|
|
|
|
}
|
2015-01-16 02:40:39 +03:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2016-12-31 05:52:49 +03:00
|
|
|
const auto& vertVaryings = sh::GetVaryings(prev->mHandle);
|
|
|
|
const auto& fragVaryings = sh::GetVaryings(mHandle);
|
|
|
|
if (!vertVaryings || !fragVaryings) {
|
|
|
|
nsPrintfCString error("Could not create varying list.");
|
|
|
|
*out_log = error;
|
|
|
|
return false;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
|
|
|
{
|
2016-09-08 11:28:30 +03:00
|
|
|
std::vector<sh::ShaderVariable> staticUseVaryingList;
|
2016-12-31 05:52:49 +03:00
|
|
|
|
|
|
|
for (const auto& fragVarying : *fragVaryings) {
|
|
|
|
static const char prefix[] = "gl_";
|
2016-07-18 09:11:57 +03:00
|
|
|
if (StartsWith(fragVarying.name, prefix)) {
|
|
|
|
if (fragVarying.staticUse) {
|
2016-09-08 11:28:30 +03:00
|
|
|
staticUseVaryingList.push_back(fragVarying);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-01-16 02:40:39 +03:00
|
|
|
continue;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2015-01-16 02:40:39 +03:00
|
|
|
bool definedInVertShader = false;
|
2015-03-12 06:12:43 +03:00
|
|
|
bool staticVertUse = false;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-12-31 05:52:49 +03:00
|
|
|
for (const auto& vertVarying : *vertVaryings) {
|
|
|
|
if (vertVarying.name != fragVarying.name) continue;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-12-31 05:52:49 +03:00
|
|
|
if (!vertVarying.isSameVaryingAtLinkTime(fragVarying, shaderVersion)) {
|
|
|
|
nsPrintfCString error(
|
|
|
|
"Varying `%s`is not linkable between"
|
|
|
|
" attached shaders.",
|
|
|
|
fragVarying.name.c_str());
|
|
|
|
*out_log = error;
|
|
|
|
return false;
|
|
|
|
}
|
2015-01-16 02:40:39 +03:00
|
|
|
|
2017-05-24 10:20:02 +03:00
|
|
|
definedInVertShader = true;
|
|
|
|
staticVertUse = vertVarying.staticUse;
|
|
|
|
break;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2016-07-18 09:11:57 +03:00
|
|
|
if (!definedInVertShader && fragVarying.staticUse) {
|
|
|
|
nsPrintfCString error(
|
|
|
|
"Varying `%s` has static-use in the frag"
|
|
|
|
" shader, but is undeclared in the vert"
|
|
|
|
" shader.",
|
|
|
|
fragVarying.name.c_str());
|
|
|
|
*out_log = error;
|
|
|
|
return false;
|
2015-01-16 02:40:39 +03:00
|
|
|
}
|
2015-03-12 06:12:43 +03:00
|
|
|
|
2017-05-24 10:20:02 +03:00
|
|
|
if (staticVertUse && fragVarying.staticUse) {
|
2016-09-08 11:28:30 +03:00
|
|
|
staticUseVaryingList.push_back(fragVarying);
|
2015-01-16 02:40:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-24 10:20:02 +03:00
|
|
|
if (!sh::CheckVariablesWithinPackingLimits(mMaxVaryingVectors,
|
2016-09-08 11:28:30 +03:00
|
|
|
staticUseVaryingList)) {
|
2015-01-16 02:40:39 +03:00
|
|
|
*out_log =
|
2015-03-12 06:12:43 +03:00
|
|
|
"Statically used varyings do not fit within packing limits. (see"
|
|
|
|
" GLSL ES Specification 1.0.17, p111)";
|
2015-01-16 02:40:39 +03:00
|
|
|
return false;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-21 11:15:02 +03:00
|
|
|
if (shaderVersion == 100) {
|
|
|
|
// Enforce ESSL1 invariant linking rules.
|
2016-07-18 09:11:57 +03:00
|
|
|
bool isInvariant_Position = false;
|
|
|
|
bool isInvariant_PointSize = false;
|
|
|
|
bool isInvariant_FragCoord = false;
|
|
|
|
bool isInvariant_PointCoord = false;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-07-18 09:11:57 +03:00
|
|
|
for (const auto& varying : *vertVaryings) {
|
|
|
|
if (varying.name == "gl_Position") {
|
|
|
|
isInvariant_Position = varying.isInvariant;
|
|
|
|
} else if (varying.name == "gl_PointSize") {
|
|
|
|
isInvariant_PointSize = varying.isInvariant;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2016-07-18 09:11:57 +03:00
|
|
|
|
|
|
|
for (const auto& varying : *fragVaryings) {
|
|
|
|
if (varying.name == "gl_FragCoord") {
|
|
|
|
isInvariant_FragCoord = varying.isInvariant;
|
|
|
|
} else if (varying.name == "gl_PointCoord") {
|
|
|
|
isInvariant_PointCoord = varying.isInvariant;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////
|
|
|
|
|
|
|
|
const auto fnCanBuiltInsLink = [](bool vertIsInvariant,
|
|
|
|
bool fragIsInvariant) {
|
|
|
|
if (vertIsInvariant) return true;
|
|
|
|
|
|
|
|
return !fragIsInvariant;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!fnCanBuiltInsLink(isInvariant_Position, isInvariant_FragCoord)) {
|
|
|
|
*out_log =
|
|
|
|
"gl_Position must be invariant if gl_FragCoord is. (see GLSL ES"
|
|
|
|
" Specification 1.0.17, p39)";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!fnCanBuiltInsLink(isInvariant_PointSize, isInvariant_PointCoord)) {
|
|
|
|
*out_log =
|
|
|
|
"gl_PointSize must be invariant if gl_PointCoord is. (see GLSL ES"
|
|
|
|
" Specification 1.0.17, p39)";
|
|
|
|
return false;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2016-07-18 09:11:57 +03:00
|
|
|
|
2015-01-16 02:40:39 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t ShaderValidator::CalcNumSamplerUniforms() const {
|
|
|
|
size_t accum = 0;
|
|
|
|
|
2017-05-24 10:20:02 +03:00
|
|
|
const std::vector<sh::Uniform>& uniforms = *sh::GetUniforms(mHandle);
|
2015-01-16 02:40:39 +03:00
|
|
|
|
|
|
|
for (auto itr = uniforms.begin(); itr != uniforms.end(); ++itr) {
|
|
|
|
GLenum type = itr->type;
|
|
|
|
if (type == LOCAL_GL_SAMPLER_2D || type == LOCAL_GL_SAMPLER_CUBE) {
|
2018-02-24 02:47:41 +03:00
|
|
|
accum += itr->getArraySizeProduct();
|
2015-01-16 02:40:39 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-01-16 02:40:39 +03:00
|
|
|
|
|
|
|
return accum;
|
|
|
|
}
|
|
|
|
|
2015-10-24 07:35:16 +03:00
|
|
|
size_t ShaderValidator::NumAttributes() const {
|
2017-05-24 10:20:02 +03:00
|
|
|
return sh::GetAttributes(mHandle)->size();
|
2015-10-24 07:35:16 +03:00
|
|
|
}
|
|
|
|
|
2015-01-16 02:40:39 +03:00
|
|
|
// Attribs cannot be structs or arrays, and neither can vertex inputs in ES3.
|
|
|
|
// Therefore, attrib names are always simple.
|
|
|
|
bool ShaderValidator::FindAttribUserNameByMappedName(
|
|
|
|
const std::string& mappedName,
|
|
|
|
const std::string** const out_userName) const {
|
2017-05-24 10:20:02 +03:00
|
|
|
const std::vector<sh::Attribute>& attribs = *sh::GetAttributes(mHandle);
|
2015-01-16 02:40:39 +03:00
|
|
|
for (auto itr = attribs.begin(); itr != attribs.end(); ++itr) {
|
|
|
|
if (itr->mappedName == mappedName) {
|
|
|
|
*out_userName = &(itr->name);
|
|
|
|
return true;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-01-16 02:40:39 +03:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ShaderValidator::FindAttribMappedNameByUserName(
|
|
|
|
const std::string& userName,
|
|
|
|
const std::string** const out_mappedName) const {
|
2017-05-24 10:20:02 +03:00
|
|
|
const std::vector<sh::Attribute>& attribs = *sh::GetAttributes(mHandle);
|
2015-01-16 02:40:39 +03:00
|
|
|
for (auto itr = attribs.begin(); itr != attribs.end(); ++itr) {
|
|
|
|
if (itr->name == userName) {
|
|
|
|
*out_mappedName = &(itr->mappedName);
|
|
|
|
return true;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-01-16 02:40:39 +03:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-11-14 06:27:17 +03:00
|
|
|
bool ShaderValidator::FindVaryingByMappedName(const std::string& mappedName,
|
|
|
|
std::string* const out_userName,
|
|
|
|
bool* const out_isArray) const {
|
2017-05-24 10:20:02 +03:00
|
|
|
const std::vector<sh::Varying>& varyings = *sh::GetVaryings(mHandle);
|
2015-11-14 06:27:17 +03:00
|
|
|
for (auto itr = varyings.begin(); itr != varyings.end(); ++itr) {
|
|
|
|
const sh::ShaderVariable* found;
|
|
|
|
if (!itr->findInfoByMappedName(mappedName, &found, out_userName)) continue;
|
|
|
|
|
|
|
|
*out_isArray = found->isArray();
|
|
|
|
return true;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2015-11-14 06:27:17 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-11-03 19:02:29 +03:00
|
|
|
bool ShaderValidator::FindVaryingMappedNameByUserName(
|
|
|
|
const std::string& userName,
|
|
|
|
const std::string** const out_mappedName) const {
|
2017-05-24 10:20:02 +03:00
|
|
|
const std::vector<sh::Varying>& attribs = *sh::GetVaryings(mHandle);
|
2015-11-03 19:02:29 +03:00
|
|
|
for (auto itr = attribs.begin(); itr != attribs.end(); ++itr) {
|
|
|
|
if (itr->name == userName) {
|
|
|
|
*out_mappedName = &(itr->mappedName);
|
|
|
|
return true;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-11-03 19:02:29 +03:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2015-01-16 02:40:39 +03:00
|
|
|
// This must handle names like "foo.bar[0]".
|
|
|
|
bool ShaderValidator::FindUniformByMappedName(const std::string& mappedName,
|
|
|
|
std::string* const out_userName,
|
|
|
|
bool* const out_isArray) const {
|
2017-05-24 10:20:02 +03:00
|
|
|
const std::vector<sh::Uniform>& uniforms = *sh::GetUniforms(mHandle);
|
2015-01-16 02:40:39 +03:00
|
|
|
for (auto itr = uniforms.begin(); itr != uniforms.end(); ++itr) {
|
|
|
|
const sh::ShaderVariable* found;
|
|
|
|
if (!itr->findInfoByMappedName(mappedName, &found, out_userName)) continue;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-01-16 02:40:39 +03:00
|
|
|
*out_isArray = found->isArray();
|
|
|
|
return true;
|
|
|
|
}
|
2016-03-23 10:18:00 +03:00
|
|
|
|
|
|
|
const size_t dotPos = mappedName.find(".");
|
|
|
|
|
|
|
|
const std::vector<sh::InterfaceBlock>& interfaces =
|
|
|
|
*sh::GetInterfaceBlocks(mHandle);
|
|
|
|
for (const auto& interface : interfaces) {
|
|
|
|
std::string mappedFieldName;
|
|
|
|
const bool hasInstanceName = !interface.instanceName.empty();
|
|
|
|
|
|
|
|
// If the InterfaceBlock has an instanceName, all variables defined
|
|
|
|
// within the block are qualified with the block name, as opposed
|
|
|
|
// to being placed in the global scope.
|
|
|
|
if (hasInstanceName) {
|
|
|
|
// If mappedName has no block name prefix, skip
|
|
|
|
if (std::string::npos == dotPos) continue;
|
|
|
|
|
|
|
|
// If mappedName has a block name prefix that doesn't match, skip
|
|
|
|
const std::string mappedInterfaceBlockName = mappedName.substr(0, dotPos);
|
|
|
|
if (interface.mappedName != mappedInterfaceBlockName) continue;
|
|
|
|
|
|
|
|
mappedFieldName = mappedName.substr(dotPos + 1);
|
|
|
|
} else {
|
|
|
|
mappedFieldName = mappedName;
|
|
|
|
}
|
|
|
|
|
2015-11-14 06:27:17 +03:00
|
|
|
for (const auto& field : interface.fields) {
|
|
|
|
const sh::ShaderVariable* found;
|
|
|
|
|
2016-03-23 10:18:00 +03:00
|
|
|
if (!field.findInfoByMappedName(mappedFieldName, &found, out_userName))
|
2015-11-14 06:27:17 +03:00
|
|
|
continue;
|
|
|
|
|
2016-03-23 10:18:00 +03:00
|
|
|
if (hasInstanceName) {
|
|
|
|
// Prepend the user name of the interface that matched
|
|
|
|
*out_userName = interface.name + "." + *out_userName;
|
|
|
|
}
|
|
|
|
|
2015-11-14 06:27:17 +03:00
|
|
|
*out_isArray = found->isArray();
|
|
|
|
return true;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-11-03 19:02:29 +03:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-12-24 00:29:07 +03:00
|
|
|
bool ShaderValidator::UnmapUniformBlockName(
|
|
|
|
const nsACString& baseMappedName, nsCString* const out_baseUserName) const {
|
2017-05-24 10:20:02 +03:00
|
|
|
const std::vector<sh::InterfaceBlock>& interfaces =
|
|
|
|
*sh::GetInterfaceBlocks(mHandle);
|
2015-11-03 19:02:29 +03:00
|
|
|
for (const auto& interface : interfaces) {
|
2016-12-24 00:29:07 +03:00
|
|
|
const nsDependentCString interfaceMappedName(interface.mappedName.data(),
|
|
|
|
interface.mappedName.size());
|
|
|
|
if (baseMappedName == interfaceMappedName) {
|
|
|
|
*out_baseUserName = interface.name.data();
|
2015-11-03 19:02:29 +03:00
|
|
|
return true;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-11-03 19:02:29 +03:00
|
|
|
|
2015-01-16 02:40:39 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-07-18 15:19:04 +03:00
|
|
|
void ShaderValidator::EnumerateFragOutputs(
|
|
|
|
std::map<nsCString, const nsCString>& out_FragOutputs) const {
|
2017-05-24 10:20:02 +03:00
|
|
|
const auto* fragOutputs = sh::GetOutputVariables(mHandle);
|
2016-07-18 15:19:04 +03:00
|
|
|
|
|
|
|
if (fragOutputs) {
|
|
|
|
for (const auto& fragOutput : *fragOutputs) {
|
|
|
|
out_FragOutputs.insert({nsCString(fragOutput.name.c_str()),
|
|
|
|
nsCString(fragOutput.mappedName.c_str())});
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2016-07-18 15:19:04 +03:00
|
|
|
}
|
|
|
|
|
2015-01-16 02:40:39 +03:00
|
|
|
} // namespace webgl
|
|
|
|
} // namespace mozilla
|