Commit 046e53e3 by Jamie Madill

Add an interm ShaderSh class.

This class logic will eventually move to the GL layer. Keep it in the Renderer layer for now for refactoring purposes. BUG=angleproject:1159 Change-Id: I91843099367f9a0293cc43ab98626bf79eb75ebf Reviewed-on: https://chromium-review.googlesource.com/299875Reviewed-by: 's avatarCorentin Wallez <cwallez@chromium.org> Reviewed-by: 's avatarGeoff Lang <geofflang@chromium.org> Tested-by: 's avatarJamie Madill <jmadill@chromium.org>
parent 3d3d2f20
...@@ -136,7 +136,7 @@ void Shader::getTranslatedSourceWithDebugInfo(GLsizei bufSize, GLsizei *length, ...@@ -136,7 +136,7 @@ void Shader::getTranslatedSourceWithDebugInfo(GLsizei bufSize, GLsizei *length,
void Shader::compile(Compiler *compiler) void Shader::compile(Compiler *compiler)
{ {
mCompiled = mShader->compile(compiler, mSource); mCompiled = mShader->compile(compiler, mSource, 0);
} }
void Shader::addRef() void Shader::addRef()
......
...@@ -23,7 +23,9 @@ class ShaderImpl : angle::NonCopyable ...@@ -23,7 +23,9 @@ class ShaderImpl : angle::NonCopyable
ShaderImpl() : mShaderVersion(100) {} ShaderImpl() : mShaderVersion(100) {}
virtual ~ShaderImpl() { } virtual ~ShaderImpl() { }
virtual bool compile(gl::Compiler *compiler, const std::string &source) = 0; virtual bool compile(gl::Compiler *compiler,
const std::string &source,
int additionalOptions) = 0;
virtual std::string getDebugInfo() const = 0; virtual std::string getDebugInfo() const = 0;
virtual const std::string &getInfoLog() const { return mInfoLog; } virtual const std::string &getInfoLog() const { return mInfoLog; }
......
//
// Copyright 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.
//
// ShaderSh:
// Common class representing a shader compile with ANGLE's translator.
//
#include "libANGLE/renderer/ShaderSh.h"
#include "common/utilities.h"
#include "libANGLE/Caps.h"
#include "libANGLE/Compiler.h"
namespace rx
{
namespace
{
template <typename VarT>
std::vector<VarT> GetActiveShaderVariables(const std::vector<VarT> *variableList)
{
ASSERT(variableList);
std::vector<VarT> result;
for (size_t varIndex = 0; varIndex < variableList->size(); varIndex++)
{
const VarT &var = variableList->at(varIndex);
if (var.staticUse)
{
result.push_back(var);
}
}
return result;
}
template <typename VarT>
const std::vector<VarT> &GetShaderVariables(const std::vector<VarT> *variableList)
{
ASSERT(variableList);
return *variableList;
}
// true if varying x has a higher priority in packing than y
bool CompareVarying(const sh::Varying &x, const sh::Varying &y)
{
if (x.type == y.type)
{
return x.arraySize > y.arraySize;
}
// Special case for handling structs: we sort these to the end of the list
if (x.type == GL_STRUCT_ANGLEX)
{
return false;
}
if (y.type == GL_STRUCT_ANGLEX)
{
return true;
}
return gl::VariableSortOrder(x.type) < gl::VariableSortOrder(y.type);
}
} // anonymous namespace
ShaderSh::ShaderSh(GLenum type, const gl::Limitations &rendererLimitations)
: mShaderType(type), mRendererLimitations(rendererLimitations)
{
}
ShaderSh::~ShaderSh()
{
}
bool ShaderSh::compile(gl::Compiler *compiler, const std::string &source, int additionalOptions)
{
// Reset the previous state
mActiveAttributes.clear();
mVaryings.clear();
mUniforms.clear();
mInterfaceBlocks.clear();
mActiveOutputVariables.clear();
mTranslatedSource.clear();
ShHandle compilerHandle = compiler->getCompilerHandle(mShaderType);
int compileOptions = (SH_OBJECT_CODE | SH_VARIABLES | additionalOptions);
// Some targets (eg D3D11 Feature Level 9_3 and below) do not support non-constant loop indexes
// in fragment shaders. Shader compilation will fail. To provide a better error message we can
// instruct the compiler to pre-validate.
if (mRendererLimitations.shadersRequireIndexedLoopValidation)
{
compileOptions |= SH_VALIDATE_LOOP_INDEXING;
}
const char *sourceCString = source.c_str();
bool result = ShCompile(compilerHandle, &sourceCString, 1, compileOptions);
if (!result)
{
mInfoLog = ShGetInfoLog(compilerHandle);
TRACE("\n%s", mInfoLog.c_str());
return false;
}
mTranslatedSource = ShGetObjectCode(compilerHandle);
#ifndef NDEBUG
// Prefix translated shader with commented out un-translated shader.
// Useful in diagnostics tools which capture the shader source.
std::ostringstream shaderStream;
shaderStream << "// GLSL\n";
shaderStream << "//\n";
size_t curPos = 0;
while (curPos != std::string::npos)
{
size_t nextLine = source.find("\n", curPos);
size_t len = (nextLine == std::string::npos) ? std::string::npos : (nextLine - curPos + 1);
shaderStream << "// " << source.substr(curPos, len);
curPos = (nextLine == std::string::npos) ? std::string::npos : (nextLine + 1);
}
shaderStream << "\n\n";
shaderStream << mTranslatedSource;
mTranslatedSource = shaderStream.str();
#endif
// Gather the shader information
mShaderVersion = ShGetShaderVersion(compilerHandle);
mVaryings = GetShaderVariables(ShGetVaryings(compilerHandle));
mUniforms = GetShaderVariables(ShGetUniforms(compilerHandle));
mInterfaceBlocks = GetShaderVariables(ShGetInterfaceBlocks(compilerHandle));
if (mShaderType == GL_VERTEX_SHADER)
{
mActiveAttributes = GetActiveShaderVariables(ShGetAttributes(compilerHandle));
}
else
{
ASSERT(mShaderType == GL_FRAGMENT_SHADER);
// TODO(jmadill): Figure out why we only sort in the FS, and if we need to.
std::sort(mVaryings.begin(), mVaryings.end(), CompareVarying);
mActiveOutputVariables = GetActiveShaderVariables(ShGetOutputVariables(compilerHandle));
}
ASSERT(!mTranslatedSource.empty());
return true;
}
} // namespace rx
//
// Copyright 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.
//
// ShaderSh:
// Common class representing a shader compile with ANGLE's translator.
// TODO(jmadill): Move this to the GL layer.
//
#ifndef LIBANGLE_RENDERER_SHADERSH_H_
#define LIBANGLE_RENDERER_SHADERSH_H_
#include "libANGLE/renderer/ShaderImpl.h"
namespace gl
{
struct Limitations;
}
namespace rx
{
class ShaderSh : public ShaderImpl
{
public:
ShaderSh(GLenum type, const gl::Limitations &rendererLimitations);
~ShaderSh();
bool compile(gl::Compiler *compiler, const std::string &source, int additionalOptions) override;
protected:
GLenum mShaderType;
const gl::Limitations &mRendererLimitations;
};
} // namespace rx
#endif // LIBANGLE_RENDERER_SHADERSH_H_
...@@ -9,9 +9,7 @@ ...@@ -9,9 +9,7 @@
#ifndef LIBANGLE_RENDERER_D3D_SHADERD3D_H_ #ifndef LIBANGLE_RENDERER_D3D_SHADERD3D_H_
#define LIBANGLE_RENDERER_D3D_SHADERD3D_H_ #define LIBANGLE_RENDERER_D3D_SHADERD3D_H_
#include "libANGLE/renderer/d3d/WorkaroundsD3D.h" #include "libANGLE/renderer/ShaderSh.h"
#include "libANGLE/renderer/ShaderImpl.h"
#include "libANGLE/Shader.h"
#include <map> #include <map>
...@@ -19,20 +17,22 @@ namespace rx ...@@ -19,20 +17,22 @@ namespace rx
{ {
class DynamicHLSL; class DynamicHLSL;
class RendererD3D; class RendererD3D;
struct D3DCompilerWorkarounds;
class ShaderD3D : public ShaderImpl class ShaderD3D : public ShaderSh
{ {
friend class DynamicHLSL; friend class DynamicHLSL;
public: public:
ShaderD3D(GLenum type, RendererD3D *renderer); ShaderD3D(GLenum type, const gl::Limitations &limitations);
virtual ~ShaderD3D(); virtual ~ShaderD3D();
// ShaderImpl implementation // ShaderImpl implementation
virtual std::string getDebugInfo() const; bool compile(gl::Compiler *compiler, const std::string &source, int additionalOptions) override;
std::string getDebugInfo() const override;
// D3D-specific methods // D3D-specific methods
virtual void uncompile(); void uncompile();
unsigned int getUniformRegister(const std::string &uniformName) const; unsigned int getUniformRegister(const std::string &uniformName) const;
unsigned int getInterfaceBlockRegister(const std::string &blockName) const; unsigned int getInterfaceBlockRegister(const std::string &blockName) const;
void appendDebugInfo(const std::string &info) { mDebugInfo += info; } void appendDebugInfo(const std::string &info) { mDebugInfo += info; }
...@@ -47,16 +47,7 @@ class ShaderD3D : public ShaderImpl ...@@ -47,16 +47,7 @@ class ShaderD3D : public ShaderImpl
GLenum getShaderType() const; GLenum getShaderType() const;
ShShaderOutput getCompilerOutputType() const; ShShaderOutput getCompilerOutputType() const;
virtual bool compile(gl::Compiler *compiler, const std::string &source);
private: private:
void compileToHLSL(ShHandle compiler, const std::string &source);
void parseVaryings(ShHandle compiler);
void parseAttributes(ShHandle compiler);
GLenum mShaderType;
bool mUsesMultipleRenderTargets; bool mUsesMultipleRenderTargets;
bool mUsesFragColor; bool mUsesFragColor;
bool mUsesFragData; bool mUsesFragData;
......
...@@ -3005,7 +3005,7 @@ FramebufferImpl *Renderer11::createFramebuffer(const gl::Framebuffer::Data &data ...@@ -3005,7 +3005,7 @@ FramebufferImpl *Renderer11::createFramebuffer(const gl::Framebuffer::Data &data
ShaderImpl *Renderer11::createShader(GLenum type) ShaderImpl *Renderer11::createShader(GLenum type)
{ {
return new ShaderD3D(type, this); return new ShaderD3D(type, getRendererLimitations());
} }
ProgramImpl *Renderer11::createProgram(const gl::Program::Data &data) ProgramImpl *Renderer11::createProgram(const gl::Program::Data &data)
......
...@@ -2715,7 +2715,7 @@ FramebufferImpl *Renderer9::createFramebuffer(const gl::Framebuffer::Data &data) ...@@ -2715,7 +2715,7 @@ FramebufferImpl *Renderer9::createFramebuffer(const gl::Framebuffer::Data &data)
ShaderImpl *Renderer9::createShader(GLenum type) ShaderImpl *Renderer9::createShader(GLenum type)
{ {
return new ShaderD3D(type, this); return new ShaderD3D(type, getRendererLimitations());
} }
ProgramImpl *Renderer9::createProgram(const gl::Program::Data &data) ProgramImpl *Renderer9::createProgram(const gl::Program::Data &data)
......
...@@ -244,7 +244,7 @@ CompilerImpl *RendererGL::createCompiler() ...@@ -244,7 +244,7 @@ CompilerImpl *RendererGL::createCompiler()
ShaderImpl *RendererGL::createShader(GLenum type) ShaderImpl *RendererGL::createShader(GLenum type)
{ {
return new ShaderGL(type, mFunctions); return new ShaderGL(type, getRendererLimitations(), mFunctions);
} }
ProgramImpl *RendererGL::createProgram(const gl::Program::Data &data) ProgramImpl *RendererGL::createProgram(const gl::Program::Data &data)
......
...@@ -11,38 +11,15 @@ ...@@ -11,38 +11,15 @@
#include "common/debug.h" #include "common/debug.h"
#include "libANGLE/Compiler.h" #include "libANGLE/Compiler.h"
#include "libANGLE/renderer/gl/FunctionsGL.h" #include "libANGLE/renderer/gl/FunctionsGL.h"
#include "libANGLE/renderer/gl/RendererGL.h"
template <typename VarT>
static std::vector<VarT> GetFilteredShaderVariables(const std::vector<VarT> *variableList)
{
ASSERT(variableList);
std::vector<VarT> result;
for (size_t varIndex = 0; varIndex < variableList->size(); varIndex++)
{
const VarT &var = variableList->at(varIndex);
if (var.staticUse)
{
result.push_back(var);
}
}
return result;
}
template <typename VarT>
static const std::vector<VarT> &GetShaderVariables(const std::vector<VarT> *variableList)
{
ASSERT(variableList);
return *variableList;
}
namespace rx namespace rx
{ {
ShaderGL::ShaderGL(GLenum type, const FunctionsGL *functions) ShaderGL::ShaderGL(GLenum type,
: ShaderImpl(), const gl::Limitations &rendererLimitations,
mFunctions(functions), const FunctionsGL *functions)
mType(type), : ShaderSh(type, rendererLimitations), mFunctions(functions), mShaderID(0)
mShaderID(0)
{ {
ASSERT(mFunctions); ASSERT(mFunctions);
} }
...@@ -56,37 +33,26 @@ ShaderGL::~ShaderGL() ...@@ -56,37 +33,26 @@ ShaderGL::~ShaderGL()
} }
} }
bool ShaderGL::compile(gl::Compiler *compiler, const std::string &source) bool ShaderGL::compile(gl::Compiler *compiler, const std::string &source, int additionalOptionsIn)
{ {
// Reset the previous state // Reset the previous state
mActiveAttributes.clear();
mVaryings.clear();
mUniforms.clear();
mInterfaceBlocks.clear();
mActiveOutputVariables.clear();
if (mShaderID != 0) if (mShaderID != 0)
{ {
mFunctions->deleteShader(mShaderID); mFunctions->deleteShader(mShaderID);
mShaderID = 0; mShaderID = 0;
} }
// Translate the ESSL into GLSL int additionalOptions = (additionalOptionsIn | SH_INIT_GL_POSITION);
ShHandle compilerHandle = compiler->getCompilerHandle(mType); if (!ShaderSh::compile(compiler, source, additionalOptions))
int compileOptions = (SH_OBJECT_CODE | SH_VARIABLES | SH_INIT_GL_POSITION);
const char* sourceCString = source.c_str();
if (!ShCompile(compilerHandle, &sourceCString, 1, compileOptions))
{ {
mInfoLog = ShGetInfoLog(compilerHandle);
TRACE("\n%s", mInfoLog.c_str());
return false; return false;
} }
mTranslatedSource = ShGetObjectCode(compilerHandle); // Translate the ESSL into GLSL
const char* translatedSourceCString = mTranslatedSource.c_str(); const char *translatedSourceCString = mTranslatedSource.c_str();
// Generate a shader object and set the source // Generate a shader object and set the source
mShaderID = mFunctions->createShader(mType); mShaderID = mFunctions->createShader(mShaderType);
mFunctions->shaderSource(mShaderID, 1, &translatedSourceCString, nullptr); mFunctions->shaderSource(mShaderID, 1, &translatedSourceCString, nullptr);
mFunctions->compileShader(mShaderID); mFunctions->compileShader(mShaderID);
...@@ -111,25 +77,6 @@ bool ShaderGL::compile(gl::Compiler *compiler, const std::string &source) ...@@ -111,25 +77,6 @@ bool ShaderGL::compile(gl::Compiler *compiler, const std::string &source)
return false; return false;
} }
// Gather the shader information
mShaderVersion = ShGetShaderVersion(compilerHandle);
// TODO: refactor this out, gathering of the attributes, varyings and outputs should be done
// at the gl::Shader level
if (mType == GL_VERTEX_SHADER)
{
mActiveAttributes = GetFilteredShaderVariables(ShGetAttributes(compilerHandle));
}
mVaryings = GetShaderVariables(ShGetVaryings(compilerHandle));
mUniforms = GetShaderVariables(ShGetUniforms(compilerHandle));
mInterfaceBlocks = GetShaderVariables(ShGetInterfaceBlocks(compilerHandle));
if (mType == GL_FRAGMENT_SHADER)
{
mActiveOutputVariables = GetFilteredShaderVariables(ShGetOutputVariables(compilerHandle));
}
return true; return true;
} }
......
...@@ -9,20 +9,20 @@ ...@@ -9,20 +9,20 @@
#ifndef LIBANGLE_RENDERER_GL_SHADERGL_H_ #ifndef LIBANGLE_RENDERER_GL_SHADERGL_H_
#define LIBANGLE_RENDERER_GL_SHADERGL_H_ #define LIBANGLE_RENDERER_GL_SHADERGL_H_
#include "libANGLE/renderer/ShaderImpl.h" #include "libANGLE/renderer/ShaderSh.h"
namespace rx namespace rx
{ {
class FunctionsGL; class FunctionsGL;
class RendererGL;
class ShaderGL : public ShaderImpl class ShaderGL : public ShaderSh
{ {
public: public:
ShaderGL(GLenum type, const FunctionsGL *functions); ShaderGL(GLenum type, const gl::Limitations &rendererLimitations, const FunctionsGL *functions);
~ShaderGL() override; ~ShaderGL() override;
bool compile(gl::Compiler *compiler, const std::string &source) override; bool compile(gl::Compiler *compiler, const std::string &source, int additionalOptions) override;
std::string getDebugInfo() const override; std::string getDebugInfo() const override;
GLuint getShaderID() const; GLuint getShaderID() const;
...@@ -30,7 +30,6 @@ class ShaderGL : public ShaderImpl ...@@ -30,7 +30,6 @@ class ShaderGL : public ShaderImpl
private: private:
const FunctionsGL *mFunctions; const FunctionsGL *mFunctions;
GLenum mType;
GLuint mShaderID; GLuint mShaderID;
}; };
......
...@@ -147,6 +147,8 @@ ...@@ -147,6 +147,8 @@
'libANGLE/renderer/Renderer.cpp', 'libANGLE/renderer/Renderer.cpp',
'libANGLE/renderer/Renderer.h', 'libANGLE/renderer/Renderer.h',
'libANGLE/renderer/ShaderImpl.h', 'libANGLE/renderer/ShaderImpl.h',
'libANGLE/renderer/ShaderSh.cpp',
'libANGLE/renderer/ShaderSh.h',
'libANGLE/renderer/SurfaceImpl.cpp', 'libANGLE/renderer/SurfaceImpl.cpp',
'libANGLE/renderer/SurfaceImpl.h', 'libANGLE/renderer/SurfaceImpl.h',
'libANGLE/renderer/TextureImpl.h', 'libANGLE/renderer/TextureImpl.h',
......
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