Commit 7adfb184 by Olli Etuaho

Refactor common compiler test functionality into helper functions

Refactor translating a ESSL shader string into a target language so that compiler initialization and cleanup code can be reused between test classes. BUG=angleproject:817 TEST=angle_unittests Change-Id: Idb229dceb9e17b13ed6ad2a68ab55ed5c968780e Reviewed-on: https://chromium-review.googlesource.com/275814Tested-by: 's avatarOlli Etuaho <oetuaho@nvidia.com> Reviewed-by: 's avatarJamie Madill <jmadill@chromium.org>
parent 2a197a1b
......@@ -61,6 +61,8 @@
'<(angle_path)/src/tests/preprocessor_tests/space_test.cpp',
'<(angle_path)/src/tests/preprocessor_tests/token_test.cpp',
'<(angle_path)/src/tests/preprocessor_tests/version_test.cpp',
'<(angle_path)/src/tests/test_utils/compiler_test.cpp',
'<(angle_path)/src/tests/test_utils/compiler_test.h',
],
},
'dependencies':
......
......@@ -10,7 +10,7 @@
#include "angle_gl.h"
#include "gtest/gtest.h"
#include "GLSLANG/ShaderLang.h"
#include "compiler/translator/TranslatorGLSL.h"
#include "tests/test_utils/compiler_test.h"
namespace
{
......@@ -22,31 +22,19 @@ class EmulateBuiltInFunctionsTest : public testing::Test
EmulateBuiltInFunctionsTest() {}
protected:
void SetUp() override
{
ShBuiltInResources resources;
ShInitBuiltInResources(&resources);
mTranslatorGLSL = new TranslatorGLSL(GL_VERTEX_SHADER, SH_GLES2_SPEC, SH_GLSL_COMPATIBILITY_OUTPUT);
ASSERT_TRUE(mTranslatorGLSL->Init(resources));
}
void TearDown() override
{
SafeDelete(mTranslatorGLSL);
}
void compile(const std::string &shaderString)
{
const char *shaderStrings[] = { shaderString.c_str() };
bool compilationSuccess = mTranslatorGLSL->compile(shaderStrings, 1,
SH_OBJECT_CODE | SH_EMULATE_BUILT_IN_FUNCTIONS);
TInfoSink &infoSink = mTranslatorGLSL->getInfoSink();
mGLSLCode = infoSink.obj.c_str();
std::string infoLog;
bool compilationSuccess = compileTestShader(GL_VERTEX_SHADER,
SH_GLES2_SPEC,
SH_GLSL_COMPATIBILITY_OUTPUT,
shaderString,
SH_EMULATE_BUILT_IN_FUNCTIONS,
&mGLSLCode,
&infoLog);
if (!compilationSuccess)
{
FAIL() << "Shader compilation into GLSL failed " << infoSink.info.c_str();
FAIL() << "Shader compilation into GLSL failed " << infoLog;
}
}
......@@ -56,7 +44,6 @@ class EmulateBuiltInFunctionsTest : public testing::Test
}
private:
TranslatorGLSL *mTranslatorGLSL;
std::string mGLSLCode;
};
......
......@@ -10,8 +10,7 @@
#include "angle_gl.h"
#include "gtest/gtest.h"
#include "GLSLANG/ShaderLang.h"
#include "compiler/translator/TranslatorESSL.h"
#include "compiler/translator/TranslatorGLSL.h"
#include "tests/test_utils/compiler_test.h"
class DebugShaderPrecisionTest : public testing::Test
{
......@@ -19,40 +18,20 @@ class DebugShaderPrecisionTest : public testing::Test
DebugShaderPrecisionTest() {}
protected:
virtual void SetUp()
{
ShBuiltInResources resources;
ShInitBuiltInResources(&resources);
resources.WEBGL_debug_shader_precision = 1;
mTranslatorESSL = new TranslatorESSL(GL_FRAGMENT_SHADER, SH_GLES2_SPEC);
ASSERT_TRUE(mTranslatorESSL->Init(resources));
mTranslatorGLSL = new TranslatorGLSL(
GL_FRAGMENT_SHADER, SH_GLES2_SPEC, SH_GLSL_COMPATIBILITY_OUTPUT);
ASSERT_TRUE(mTranslatorGLSL->Init(resources));
}
virtual void TearDown()
{
delete mTranslatorESSL;
delete mTranslatorGLSL;
}
void compile(const std::string& shaderString)
{
const char *shaderStrings[] = { shaderString.c_str() };
bool compilationSuccess = mTranslatorESSL->compile(shaderStrings, 1, SH_OBJECT_CODE);
TInfoSink &infoSink = mTranslatorESSL->getInfoSink();
mESSLCode = infoSink.obj.c_str();
std::string infoLog;
bool compilationSuccess = compileWithSettings(SH_ESSL_OUTPUT, shaderString, &mESSLCode, &infoLog);
if (!compilationSuccess)
FAIL() << "Shader compilation into ESSL failed " << infoSink.info.c_str();
{
FAIL() << "Shader compilation into ESSL failed " << infoLog;
}
compilationSuccess = mTranslatorGLSL->compile(shaderStrings, 1, SH_OBJECT_CODE);
infoSink = mTranslatorGLSL->getInfoSink();
mGLSLCode = infoSink.obj.c_str();
compilationSuccess = compileWithSettings(SH_GLSL_COMPATIBILITY_OUTPUT, shaderString, &mGLSLCode, &infoLog);
if (!compilationSuccess)
FAIL() << "Shader compilation into GLSL failed " << infoSink.info.c_str();
{
FAIL() << "Shader compilation into GLSL failed " << infoLog;
}
}
bool foundInESSLCode(const char* stringToFind)
......@@ -76,8 +55,16 @@ class DebugShaderPrecisionTest : public testing::Test
}
private:
TranslatorESSL *mTranslatorESSL;
TranslatorGLSL *mTranslatorGLSL;
bool compileWithSettings(ShShaderOutput output, const std::string &shaderString,
std::string *translatedCode, std::string *infoLog)
{
ShBuiltInResources resources;
ShInitBuiltInResources(&resources);
resources.WEBGL_debug_shader_precision = 1;
return compileTestShader(GL_FRAGMENT_SHADER, SH_GLES2_SPEC, output, shaderString, &resources,
translatedCode, infoLog);
}
std::string mESSLCode;
std::string mGLSLCode;
};
......@@ -88,28 +75,10 @@ class NoDebugShaderPrecisionTest : public testing::Test
NoDebugShaderPrecisionTest() {}
protected:
virtual void SetUp()
{
ShBuiltInResources resources;
ShInitBuiltInResources(&resources);
mTranslator = new TranslatorGLSL(
GL_FRAGMENT_SHADER, SH_GLES2_SPEC, SH_GLSL_COMPATIBILITY_OUTPUT);
ASSERT_TRUE(mTranslator->Init(resources));
}
virtual void TearDown()
{
delete mTranslator;
}
bool compile(const std::string& shaderString)
{
const char *shaderStrings[] = { shaderString.c_str() };
bool compilationSuccess = mTranslator->compile(shaderStrings, 1, SH_OBJECT_CODE);
TInfoSink &infoSink = mTranslator->getInfoSink();
mCode = infoSink.obj.c_str();
return compilationSuccess;
return compileTestShader(GL_FRAGMENT_SHADER, SH_GLES2_SPEC, SH_GLSL_COMPATIBILITY_OUTPUT,
shaderString, &mCode, nullptr);
}
bool foundInCode(const char* stringToFind)
......@@ -118,7 +87,6 @@ class NoDebugShaderPrecisionTest : public testing::Test
}
private:
TranslatorGLSL *mTranslator;
std::string mCode;
};
......
......@@ -10,7 +10,7 @@
#include "angle_gl.h"
#include "gtest/gtest.h"
#include "GLSLANG/ShaderLang.h"
#include "compiler/translator/TranslatorESSL.h"
#include "tests/test_utils/compiler_test.h"
class NVDrawBuffersTest : public testing::Test
{
......@@ -18,7 +18,7 @@ class NVDrawBuffersTest : public testing::Test
NVDrawBuffersTest() {}
protected:
virtual void SetUp()
void compile(const std::string &shaderString)
{
ShBuiltInResources resources;
ShInitBuiltInResources(&resources);
......@@ -26,16 +26,22 @@ class NVDrawBuffersTest : public testing::Test
resources.EXT_draw_buffers = 1;
resources.NV_draw_buffers = 1;
mTranslator = new TranslatorESSL(GL_FRAGMENT_SHADER, SH_GLES2_SPEC);
ASSERT_TRUE(mTranslator->Init(resources));
std::string infoLog;
bool compilationSuccess = compileTestShader(GL_FRAGMENT_SHADER, SH_GLES2_SPEC, SH_ESSL_OUTPUT,
shaderString, &resources, &mGLSLCode, &infoLog);
if (!compilationSuccess)
{
FAIL() << "Shader compilation into ESSL failed " << infoLog;
}
}
virtual void TearDown()
bool foundInCode(const char *stringToFind)
{
delete mTranslator;
return mGLSLCode.find(stringToFind) != std::string::npos;
}
TranslatorESSL *mTranslator;
private:
std::string mGLSLCode;
};
TEST_F(NVDrawBuffersTest, NVDrawBuffers)
......@@ -47,14 +53,7 @@ TEST_F(NVDrawBuffersTest, NVDrawBuffers)
" gl_FragData[0] = vec4(1.0);\n"
" gl_FragData[1] = vec4(0.0);\n"
"}\n";
const char *shaderStrings[] = { shaderString.c_str() };
ASSERT_TRUE(mTranslator->compile(shaderStrings, 1, SH_OBJECT_CODE));
TInfoSink& infoSink = mTranslator->getInfoSink();
std::string objCode(infoSink.obj.c_str());
size_t nv_draw_buffers_ind = objCode.find("GL_NV_draw_buffers");
EXPECT_NE(std::string::npos, nv_draw_buffers_ind);
size_t ext_draw_buffers_ind = objCode.find("GL_EXT_draw_buffers");
EXPECT_EQ(std::string::npos, ext_draw_buffers_ind);
compile(shaderString);
ASSERT_TRUE(foundInCode("GL_NV_draw_buffers"));
ASSERT_FALSE(foundInCode("GL_EXT_draw_buffers"));
}
......@@ -10,7 +10,7 @@
#include "angle_gl.h"
#include "gtest/gtest.h"
#include "GLSLANG/ShaderLang.h"
#include "compiler/translator/TranslatorESSL.h"
#include "tests/test_utils/compiler_test.h"
namespace
{
......@@ -21,32 +21,17 @@ class PruneUnusedFunctionsTest : public testing::Test
PruneUnusedFunctionsTest() {}
protected:
void SetUp() override
{
ShBuiltInResources resources;
ShInitBuiltInResources(&resources);
resources.FragmentPrecisionHigh = 1;
mTranslator = new TranslatorESSL(GL_FRAGMENT_SHADER, SH_GLES3_SPEC);
ASSERT_TRUE(mTranslator->Init(resources));
}
void TearDown() override
{
SafeDelete(mTranslator);
}
void compile(const std::string &shaderString, bool prune)
{
const char *shaderStrings[] = { shaderString.c_str() };
int compilationFlags = SH_VARIABLES | SH_OBJECT_CODE | (prune ? 0 : SH_DONT_PRUNE_UNUSED_FUNCTIONS);
bool compilationSuccess = mTranslator->compile(shaderStrings, 1, compilationFlags);
TInfoSink &infoSink = mTranslator->getInfoSink();
int compilationFlags = SH_VARIABLES | (prune ? 0 : SH_DONT_PRUNE_UNUSED_FUNCTIONS);
std::string infoLog;
bool compilationSuccess = compileTestShader(GL_FRAGMENT_SHADER, SH_GLES3_SPEC, SH_ESSL_OUTPUT,
shaderString, compilationFlags, &mTranslatedSource, &infoLog);
if (!compilationSuccess)
{
FAIL() << "Shader compilation failed " << infoSink.info.str();
FAIL() << "Shader compilation failed " << infoLog;
}
mTranslatedSource = infoSink.obj.str();
}
bool kept(const char *functionName, int nOccurences) const
......@@ -70,7 +55,6 @@ class PruneUnusedFunctionsTest : public testing::Test
}
private:
TranslatorESSL *mTranslator;
std::string mTranslatedSource;
};
......
......@@ -8,9 +8,10 @@
//
#include "angle_gl.h"
#include "common/angleutils.h"
#include "gtest/gtest.h"
#include "GLSLANG/ShaderLang.h"
#include "compiler/translator/TranslatorHLSL.h"
#include "tests/test_utils/compiler_test.h"
namespace
{
......@@ -21,32 +22,15 @@ class UnrollFlattenTest : public testing::Test
UnrollFlattenTest() {}
protected:
void SetUp() override
{
ShBuiltInResources resources;
ShInitBuiltInResources(&resources);
resources.FragmentPrecisionHigh = 1;
mTranslator = new TranslatorHLSL(GL_FRAGMENT_SHADER, SH_GLES2_SPEC, SH_HLSL11_OUTPUT);
ASSERT_TRUE(mTranslator->Init(resources));
}
void TearDown() override
{
SafeDelete(mTranslator);
}
void compile(const std::string &shaderString)
{
const char *shaderStrings[] = { shaderString.c_str() };
bool compilationSuccess = mTranslator->compile(shaderStrings, 1, SH_VARIABLES | SH_OBJECT_CODE);
TInfoSink &infoSink = mTranslator->getInfoSink();
std::string infoLog;
bool compilationSuccess = compileTestShader(GL_FRAGMENT_SHADER, SH_GLES2_SPEC, SH_HLSL11_OUTPUT,
shaderString, SH_VARIABLES, &mTranslatedSource, &infoLog);
if (!compilationSuccess)
{
FAIL() << "Shader compilation failed " << infoSink.info.str();
FAIL() << "Shader compilation failed " << infoLog;
}
mTranslatedSource = infoSink.obj.str();
// Ignore the beginning of the shader to avoid the definitions of LOOP and FLATTEN
mCurrentPosition = mTranslatedSource.find("GL_USES_FRAG_COLOR");
}
......@@ -83,7 +67,6 @@ class UnrollFlattenTest : public testing::Test
static const char *FLATTEN;
private:
TranslatorHLSL *mTranslator;
std::string mTranslatedSource;
int mCurrentPosition;
......
......@@ -20,5 +20,7 @@
'TypeTracking_test.cpp',
'UnrollFlatten_test.cpp',
'VariablePacker_test.cpp',
'../test_utils/compiler_test.cpp',
'../test_utils/compiler_test.h',
],
}
//
// Copyright (c) 2015 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// compiler_test.cpp:
// utilities for compiler unit tests.
#include "compiler/translator/Compiler.h"
bool compileTestShader(sh::GLenum type,
ShShaderSpec spec,
ShShaderOutput output,
const std::string &shaderString,
ShBuiltInResources *resources,
int compileOptions,
std::string *translatedCode,
std::string *infoLog)
{
if (spec == SH_GLES3_SPEC || spec == SH_WEBGL2_SPEC)
{
resources->FragmentPrecisionHigh = 1;
}
TCompiler *translator = ConstructCompiler(type, spec, output);
if (!translator->Init(*resources))
{
SafeDelete(translator);
return false;
}
const char *shaderStrings[] = { shaderString.c_str() };
bool compilationSuccess = translator->compile(shaderStrings, 1, SH_OBJECT_CODE | compileOptions);
TInfoSink &infoSink = translator->getInfoSink();
if (translatedCode)
*translatedCode = infoSink.obj.c_str();
if (infoLog)
*infoLog = infoSink.info.c_str();
SafeDelete(translator);
return compilationSuccess;
}
bool compileTestShader(sh::GLenum type,
ShShaderSpec spec,
ShShaderOutput output,
const std::string &shaderString,
ShBuiltInResources *resources,
std::string *translatedCode,
std::string *infoLog)
{
return compileTestShader(type, spec, output, shaderString, resources, 0, translatedCode, infoLog);
}
bool compileTestShader(sh::GLenum type,
ShShaderSpec spec,
ShShaderOutput output,
const std::string &shaderString,
int compileOptions,
std::string *translatedCode,
std::string *infoLog)
{
ShBuiltInResources resources;
ShInitBuiltInResources(&resources);
return compileTestShader(type, spec, output, shaderString, &resources, compileOptions, translatedCode, infoLog);
}
bool compileTestShader(sh::GLenum type,
ShShaderSpec spec,
ShShaderOutput output,
const std::string &shaderString,
std::string *translatedCode,
std::string *infoLog)
{
return compileTestShader(type, spec, output, shaderString, 0, translatedCode, infoLog);
}
//
// Copyright (c) 2015 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// compiler_test.h:
// utilities for compiler unit tests.
#ifndef TESTS_TEST_UTILS_COMPILER_TEST_H_
#define TESTS_TEST_UTILS_COMPILER_TEST_H_
#include "GLSLANG/ShaderLang.h"
bool compileTestShader(sh::GLenum type,
ShShaderSpec spec,
ShShaderOutput output,
const std::string &shaderString,
ShBuiltInResources *resources,
int compileOptions,
std::string *translatedCode,
std::string *infoLog);
bool compileTestShader(sh::GLenum type,
ShShaderSpec spec,
ShShaderOutput output,
const std::string &shaderString,
ShBuiltInResources *resources,
std::string *translatedCode,
std::string *infoLog);
bool compileTestShader(sh::GLenum type,
ShShaderSpec spec,
ShShaderOutput output,
const std::string &shaderString,
int compileOptions,
std::string *translatedCode,
std::string *infoLog);
bool compileTestShader(sh::GLenum type,
ShShaderSpec spec,
ShShaderOutput output,
const std::string &shaderString,
std::string *translatedCode,
std::string *infoLog);
#endif // TESTS_TEST_UTILS_COMPILER_TEST_H_
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment