Commit 203b26f2 by Jiawei Shao Committed by Commit Bot

ES31: Translate compute shader HLSL system variables in compile time

This patch moves the implementation of translating compute shader builtin variables from link time to compile time. Unlike graphics shaders that require the information from other shader stages, we actually have enough information to translate compute shader builtin variables in compile time. Many redundant codes in DynamicHLSL have been removed after this refactor. BUG=angleproject:1442 Change-Id: I7458006785ff966a00a3825610adc5566652c75e Reviewed-on: https://chromium-review.googlesource.com/1149609Reviewed-by: 's avatarJiajia Qin <jiajia.qin@intel.com> Reviewed-by: 's avatarJamie Madill <jmadill@chromium.org> Commit-Queue: Jiawei Shao <jiawei.shao@intel.com>
parent eb4b8697
...@@ -454,7 +454,7 @@ void OutputHLSL::writeReferencedVaryings(TInfoSinkBase &out) const ...@@ -454,7 +454,7 @@ void OutputHLSL::writeReferencedVaryings(TInfoSinkBase &out) const
{ {
for (const auto &varying : mReferencedVaryings) for (const auto &varying : mReferencedVaryings)
{ {
const TType &type = varying.second->getType(); const TType &type = varying.second->getType();
// Program linking depends on this exact format // Program linking depends on this exact format
out << "static " << InterpolationString(type.getQualifier()) << " " << TypeString(type) out << "static " << InterpolationString(type.getQualifier()) << " " << TypeString(type)
...@@ -782,28 +782,51 @@ void OutputHLSL::header(TInfoSinkBase &out, ...@@ -782,28 +782,51 @@ void OutputHLSL::header(TInfoSinkBase &out,
mUniformHLSL->samplerMetadataUniforms(out, "c1"); mUniformHLSL->samplerMetadataUniforms(out, "c1");
out << "};\n"; out << "};\n";
// Follow built-in variables would be initialized in std::ostringstream systemValueDeclaration;
// DynamicHLSL::generateComputeShaderLinkHLSL, if they std::ostringstream glBuiltinInitialization;
// are used in compute shader.
systemValueDeclaration << "\nstruct CS_INPUT\n{\n";
glBuiltinInitialization << "\nvoid initGLBuiltins(CS_INPUT input)\n"
<< "{\n";
if (mUsesWorkGroupID) if (mUsesWorkGroupID)
{ {
out << "static uint3 gl_WorkGroupID = uint3(0, 0, 0);\n"; out << "static uint3 gl_WorkGroupID = uint3(0, 0, 0);\n";
systemValueDeclaration << " uint3 dx_WorkGroupID : "
<< "SV_GroupID;\n";
glBuiltinInitialization << " gl_WorkGroupID = input.dx_WorkGroupID;\n";
} }
if (mUsesLocalInvocationID) if (mUsesLocalInvocationID)
{ {
out << "static uint3 gl_LocalInvocationID = uint3(0, 0, 0);\n"; out << "static uint3 gl_LocalInvocationID = uint3(0, 0, 0);\n";
systemValueDeclaration << " uint3 dx_LocalInvocationID : "
<< "SV_GroupThreadID;\n";
glBuiltinInitialization << " gl_LocalInvocationID = input.dx_LocalInvocationID;\n";
} }
if (mUsesGlobalInvocationID) if (mUsesGlobalInvocationID)
{ {
out << "static uint3 gl_GlobalInvocationID = uint3(0, 0, 0);\n"; out << "static uint3 gl_GlobalInvocationID = uint3(0, 0, 0);\n";
systemValueDeclaration << " uint3 dx_GlobalInvocationID : "
<< "SV_DispatchThreadID;\n";
glBuiltinInitialization << " gl_GlobalInvocationID = input.dx_GlobalInvocationID;\n";
} }
if (mUsesLocalInvocationIndex) if (mUsesLocalInvocationIndex)
{ {
out << "static uint gl_LocalInvocationIndex = uint(0);\n"; out << "static uint gl_LocalInvocationIndex = uint(0);\n";
systemValueDeclaration << " uint dx_LocalInvocationIndex : "
<< "SV_GroupIndex;\n";
glBuiltinInitialization
<< " gl_LocalInvocationIndex = input.dx_LocalInvocationIndex;\n";
} }
systemValueDeclaration << "};\n\n";
glBuiltinInitialization << "};\n\n";
out << systemValueDeclaration.str();
out << glBuiltinInitialization.str();
} }
if (!mappedStructs.empty()) if (!mappedStructs.empty())
...@@ -859,31 +882,6 @@ void OutputHLSL::header(TInfoSinkBase &out, ...@@ -859,31 +882,6 @@ void OutputHLSL::header(TInfoSinkBase &out,
out << "#define GL_USES_DEPTH_RANGE\n"; out << "#define GL_USES_DEPTH_RANGE\n";
} }
if (mUsesNumWorkGroups)
{
out << "#define GL_USES_NUM_WORK_GROUPS\n";
}
if (mUsesWorkGroupID)
{
out << "#define GL_USES_WORK_GROUP_ID\n";
}
if (mUsesLocalInvocationID)
{
out << "#define GL_USES_LOCAL_INVOCATION_ID\n";
}
if (mUsesGlobalInvocationID)
{
out << "#define GL_USES_GLOBAL_INVOCATION_ID\n";
}
if (mUsesLocalInvocationIndex)
{
out << "#define GL_USES_LOCAL_INVOCATION_INDEX\n";
}
if (mUsesXor) if (mUsesXor)
{ {
out << "bool xor(bool p, bool q)\n" out << "bool xor(bool p, bool q)\n"
...@@ -1753,7 +1751,14 @@ bool OutputHLSL::visitBlock(Visit visit, TIntermBlock *node) ...@@ -1753,7 +1751,14 @@ bool OutputHLSL::visitBlock(Visit visit, TIntermBlock *node)
out << "{\n"; out << "{\n";
if (isMainBlock) if (isMainBlock)
{ {
out << "@@ MAIN PROLOGUE @@\n"; if (mShaderType == GL_COMPUTE_SHADER)
{
out << "initGLBuiltins(input);\n";
}
else
{
out << "@@ MAIN PROLOGUE @@\n";
}
} }
} }
...@@ -2041,7 +2046,7 @@ bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node) ...@@ -2041,7 +2046,7 @@ bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
} }
else if (node->getFunction()->isImageFunction()) else if (node->getFunction()->isImageFunction())
{ {
const ImmutableString &name = node->getFunction()->name(); const ImmutableString &name = node->getFunction()->name();
TType type = (*arguments)[0]->getAsTyped()->getType(); TType type = (*arguments)[0]->getAsTyped()->getType();
const ImmutableString &imageFunctionName = mImageFunctionHLSL->useImageFunction( const ImmutableString &imageFunctionName = mImageFunctionHLSL->useImageFunction(
name, type.getBasicType(), type.getLayoutQualifier().imageInternalFormat, name, type.getBasicType(), type.getLayoutQualifier().imageInternalFormat,
......
...@@ -858,74 +858,6 @@ void DynamicHLSL::generateShaderLinkHLSL(const gl::Context *context, ...@@ -858,74 +858,6 @@ void DynamicHLSL::generateShaderLinkHLSL(const gl::Context *context,
(*shaderHLSL)[gl::ShaderType::Fragment] = pixelStream.str(); (*shaderHLSL)[gl::ShaderType::Fragment] = pixelStream.str();
} }
std::string DynamicHLSL::generateComputeShaderLinkHLSL(const gl::Context *context,
const gl::ProgramState &programData) const
{
gl::Shader *computeShaderGL = programData.getAttachedShader(ShaderType::Compute);
std::stringstream computeStream;
std::string translatedSource = computeShaderGL->getTranslatedSource(context);
bool usesWorkGroupID = translatedSource.find("GL_USES_WORK_GROUP_ID") != std::string::npos;
bool usesLocalInvocationID =
translatedSource.find("GL_USES_LOCAL_INVOCATION_ID") != std::string::npos;
bool usesGlobalInvocationID =
translatedSource.find("GL_USES_GLOBAL_INVOCATION_ID") != std::string::npos;
bool usesLocalInvocationIndex =
translatedSource.find("GL_USES_LOCAL_INVOCATION_INDEX") != std::string::npos;
computeStream << "\nstruct CS_INPUT\n{\n";
if (usesWorkGroupID)
{
computeStream << " uint3 dx_WorkGroupID : "
<< "SV_GroupID;\n";
}
if (usesLocalInvocationID)
{
computeStream << " uint3 dx_LocalInvocationID : "
<< "SV_GroupThreadID;\n";
}
if (usesGlobalInvocationID)
{
computeStream << " uint3 dx_GlobalInvocationID : "
<< "SV_DispatchThreadID;\n";
}
if (usesLocalInvocationIndex)
{
computeStream << " uint dx_LocalInvocationIndex : "
<< "SV_GroupIndex;\n";
}
computeStream << "};\n\n";
std::ostringstream computePrologue;
if (usesWorkGroupID)
{
computePrologue << " gl_WorkGroupID = input.dx_WorkGroupID;\n";
}
if (usesLocalInvocationID)
{
computePrologue << " gl_LocalInvocationID = input.dx_LocalInvocationID;\n";
}
if (usesGlobalInvocationID)
{
computePrologue << " gl_GlobalInvocationID = input.dx_GlobalInvocationID;\n";
}
if (usesLocalInvocationIndex)
{
computePrologue << " gl_LocalInvocationIndex = input.dx_LocalInvocationIndex;\n";
}
angle::ReplaceSubstring(&translatedSource, std::string(MAIN_PROLOGUE_STUB_STRING),
computePrologue.str());
computeStream << translatedSource;
return computeStream.str();
}
std::string DynamicHLSL::generateGeometryShaderPreamble(const VaryingPacking &varyingPacking, std::string DynamicHLSL::generateGeometryShaderPreamble(const VaryingPacking &varyingPacking,
const BuiltinVaryingsD3D &builtinsD3D, const BuiltinVaryingsD3D &builtinsD3D,
const bool hasANGLEMultiviewEnabled, const bool hasANGLEMultiviewEnabled,
......
...@@ -134,8 +134,6 @@ class DynamicHLSL : angle::NonCopyable ...@@ -134,8 +134,6 @@ class DynamicHLSL : angle::NonCopyable
const gl::VaryingPacking &varyingPacking, const gl::VaryingPacking &varyingPacking,
const BuiltinVaryingsD3D &builtinsD3D, const BuiltinVaryingsD3D &builtinsD3D,
gl::ShaderMap<std::string> *shaderHLSL) const; gl::ShaderMap<std::string> *shaderHLSL) const;
std::string generateComputeShaderLinkHLSL(const gl::Context *context,
const gl::ProgramState &programData) const;
std::string generateGeometryShaderPreamble(const gl::VaryingPacking &varyingPacking, std::string generateGeometryShaderPreamble(const gl::VaryingPacking &varyingPacking,
const BuiltinVaryingsD3D &builtinsD3D, const BuiltinVaryingsD3D &builtinsD3D,
......
...@@ -1513,7 +1513,9 @@ gl::LinkResult ProgramD3D::compileComputeExecutable(const gl::Context *context, ...@@ -1513,7 +1513,9 @@ gl::LinkResult ProgramD3D::compileComputeExecutable(const gl::Context *context,
// Ensure the compiler is initialized to avoid race conditions. // Ensure the compiler is initialized to avoid race conditions.
ANGLE_TRY(mRenderer->ensureHLSLCompilerInitialized()); ANGLE_TRY(mRenderer->ensureHLSLCompilerInitialized());
std::string computeShader = mDynamicHLSL->generateComputeShaderLinkHLSL(context, mState); gl::Shader *computeShaderGL = mState.getAttachedShader(gl::ShaderType::Compute);
ASSERT(computeShaderGL);
std::string computeShader = computeShaderGL->getTranslatedSource(context);
ShaderExecutableD3D *computeExecutable = nullptr; ShaderExecutableD3D *computeExecutable = nullptr;
ANGLE_TRY(mRenderer->compileToExecutable(infoLog, computeShader, gl::ShaderType::Compute, ANGLE_TRY(mRenderer->compileToExecutable(infoLog, computeShader, gl::ShaderType::Compute,
......
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