Commit d8b1c5c5 by Olli Etuaho Committed by Commit Bot

Return ImmutableString from ArrayString()

This makes the compiler a few kilobytes smaller, and prepares getting rid of TString altogether. BUG=angleproject:2267 TEST=angle_unittests Change-Id: I93a003fe27b99bef72f872fa1066e2e108f934c5 Reviewed-on: https://chromium-review.googlesource.com/1107713Reviewed-by: 's avatarJamie Madill <jmadill@chromium.org> Reviewed-by: 's avatarCorentin Wallez <cwallez@chromium.org> Commit-Queue: Olli Etuaho <oetuaho@nvidia.com>
parent 26c61b24
...@@ -9,6 +9,8 @@ ...@@ -9,6 +9,8 @@
#include "compiler/translator/ImmutableStringBuilder.h" #include "compiler/translator/ImmutableStringBuilder.h"
#include <stdio.h>
namespace sh namespace sh
{ {
...@@ -39,6 +41,14 @@ ImmutableStringBuilder &ImmutableStringBuilder::operator<<(const char &c) ...@@ -39,6 +41,14 @@ ImmutableStringBuilder &ImmutableStringBuilder::operator<<(const char &c)
return *this; return *this;
} }
void ImmutableStringBuilder::appendDecimal(const uint32_t &u)
{
int numChars = snprintf(mData + mPos, mMaxLength - mPos, "%d", u);
ASSERT(numChars >= 0);
ASSERT(mPos + numChars <= mMaxLength);
mPos += numChars;
}
ImmutableStringBuilder::operator ImmutableString() ImmutableStringBuilder::operator ImmutableString()
{ {
mData[mPos] = '\0'; mData[mPos] = '\0';
......
...@@ -32,6 +32,8 @@ class ImmutableStringBuilder ...@@ -32,6 +32,8 @@ class ImmutableStringBuilder
// This invalidates the ImmutableStringBuilder, so it should only be called once. // This invalidates the ImmutableStringBuilder, so it should only be called once.
operator ImmutableString(); operator ImmutableString();
void appendDecimal(const uint32_t &i);
template <typename T> template <typename T>
void appendHex(T number) void appendHex(T number)
{ {
......
...@@ -393,7 +393,7 @@ TString OutputHLSL::generateStructMapping(const std::vector<MappedStruct> &std14 ...@@ -393,7 +393,7 @@ TString OutputHLSL::generateStructMapping(const std::vector<MappedStruct> &std14
if (structType->isArray()) if (structType->isArray())
{ {
mappedStructs += ArrayString(*mappedStruct.field->type()); mappedStructs += ArrayString(*mappedStruct.field->type()).data();
} }
mappedStructs += " =\n"; mappedStructs += " =\n";
...@@ -404,33 +404,37 @@ TString OutputHLSL::generateStructMapping(const std::vector<MappedStruct> &std14 ...@@ -404,33 +404,37 @@ TString OutputHLSL::generateStructMapping(const std::vector<MappedStruct> &std14
return mappedStructs; return mappedStructs;
} }
void OutputHLSL::header(TInfoSinkBase &out, void OutputHLSL::writeReferencedAttributes(TInfoSinkBase &out) const
const std::vector<MappedStruct> &std140Structs,
const BuiltInFunctionEmulator *builtInFunctionEmulator) const
{ {
TString varyings; for (const auto &attribute : mReferencedAttributes)
TString attributes; {
TString mappedStructs = generateStructMapping(std140Structs); const TType &type = attribute.second->getType();
const ImmutableString &name = attribute.second->name();
out << "static " << TypeString(type) << " " << Decorate(name) << ArrayString(type) << " = "
<< zeroInitializer(type) << ";\n";
}
}
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();
const ImmutableString &name = varying.second->name(); const ImmutableString &name = varying.second->name();
// Program linking depends on this exact format // Program linking depends on this exact format
varyings += TString("static ") + InterpolationString(type.getQualifier()) + " " + out << "static " << InterpolationString(type.getQualifier()) << " " << TypeString(type)
TypeString(type) + " " + Decorate(name) + ArrayString(type) + " = " + << " " << Decorate(name) << ArrayString(type) << " = " << zeroInitializer(type)
zeroInitializer(type) + ";\n"; << ";\n";
} }
}
for (const auto &attribute : mReferencedAttributes) void OutputHLSL::header(TInfoSinkBase &out,
{ const std::vector<MappedStruct> &std140Structs,
const TType &type = attribute.second->getType(); const BuiltInFunctionEmulator *builtInFunctionEmulator) const
const ImmutableString &name = attribute.second->name(); {
TString mappedStructs = generateStructMapping(std140Structs);
attributes += "static " + TypeString(type) + " " + Decorate(name) + ArrayString(type) +
" = " + zeroInitializer(type) + ";\n";
}
out << mStructureHLSL->structsHeader(); out << mStructureHLSL->structsHeader();
...@@ -491,7 +495,7 @@ void OutputHLSL::header(TInfoSinkBase &out, ...@@ -491,7 +495,7 @@ void OutputHLSL::header(TInfoSinkBase &out,
IsExtensionEnabled(mExtensionBehavior, TExtension::EXT_draw_buffers); IsExtensionEnabled(mExtensionBehavior, TExtension::EXT_draw_buffers);
out << "// Varyings\n"; out << "// Varyings\n";
out << varyings; writeReferencedVaryings(out);
out << "\n"; out << "\n";
if (mShaderVersion >= 300) if (mShaderVersion >= 300)
...@@ -644,7 +648,7 @@ void OutputHLSL::header(TInfoSinkBase &out, ...@@ -644,7 +648,7 @@ void OutputHLSL::header(TInfoSinkBase &out,
else if (mShaderType == GL_VERTEX_SHADER) else if (mShaderType == GL_VERTEX_SHADER)
{ {
out << "// Attributes\n"; out << "// Attributes\n";
out << attributes; writeReferencedAttributes(out);
out << "\n" out << "\n"
"static float4 gl_Position = float4(0, 0, 0, 0);\n"; "static float4 gl_Position = float4(0, 0, 0, 0);\n";
...@@ -665,7 +669,7 @@ void OutputHLSL::header(TInfoSinkBase &out, ...@@ -665,7 +669,7 @@ void OutputHLSL::header(TInfoSinkBase &out,
out << "\n" out << "\n"
"// Varyings\n"; "// Varyings\n";
out << varyings; writeReferencedVaryings(out);
out << "\n"; out << "\n";
if (mUsesDepthRange) if (mUsesDepthRange)
......
...@@ -72,6 +72,8 @@ class OutputHLSL : public TIntermTraverser ...@@ -72,6 +72,8 @@ class OutputHLSL : public TIntermTraverser
} }
protected: protected:
void writeReferencedAttributes(TInfoSinkBase &out) const;
void writeReferencedVaryings(TInfoSinkBase &out) const;
void header(TInfoSinkBase &out, void header(TInfoSinkBase &out,
const std::vector<MappedStruct> &std140Structs, const std::vector<MappedStruct> &std140Structs,
const BuiltInFunctionEmulator *builtInFunctionEmulator) const; const BuiltInFunctionEmulator *builtInFunctionEmulator) const;
......
...@@ -53,7 +53,7 @@ TString Define(const TStructure &structure, ...@@ -53,7 +53,7 @@ TString Define(const TStructure &structure,
} }
string += " " + fieldTypeString + " " + DecorateField(field->name(), structure) + string += " " + fieldTypeString + " " + DecorateField(field->name(), structure) +
ArrayString(fieldType) + ";\n"; ArrayString(fieldType).data() + ";\n";
if (padHelper) if (padHelper)
{ {
...@@ -76,7 +76,8 @@ TString WriteParameterList(const std::vector<TType> &parameters) ...@@ -76,7 +76,8 @@ TString WriteParameterList(const std::vector<TType> &parameters)
{ {
const TType &paramType = parameters[parameter]; const TType &paramType = parameters[parameter];
parameterList += TypeString(paramType) + " x" + str(parameter) + ArrayString(paramType); parameterList +=
TypeString(paramType) + " x" + str(parameter) + ArrayString(paramType).data();
if (parameter < parameters.size() - 1u) if (parameter < parameters.size() - 1u)
{ {
......
...@@ -613,7 +613,7 @@ TString UniformHLSL::uniformBlockMembersString(const TInterfaceBlock &interfaceB ...@@ -613,7 +613,7 @@ TString UniformHLSL::uniformBlockMembersString(const TInterfaceBlock &interfaceB
} }
hlsl += " " + InterfaceBlockFieldTypeString(field, blockStorage) + " " + hlsl += " " + InterfaceBlockFieldTypeString(field, blockStorage) + " " +
Decorate(field.name()) + ArrayString(fieldType) + ";\n"; Decorate(field.name()) + ArrayString(fieldType).data() + ";\n";
// must pad out after matrices and arrays, where HLSL usually allows itself room to pack // must pad out after matrices and arrays, where HLSL usually allows itself room to pack
// stuff // stuff
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "common/utilities.h" #include "common/utilities.h"
#include "compiler/preprocessor/numeric_lex.h" #include "compiler/preprocessor/numeric_lex.h"
#include "compiler/translator/ImmutableStringBuilder.h"
#include "compiler/translator/SymbolTable.h" #include "compiler/translator/SymbolTable.h"
bool atoi_clamp(const char *str, unsigned int *value) bool atoi_clamp(const char *str, unsigned int *value)
...@@ -472,24 +473,25 @@ GLenum GLVariablePrecision(const TType &type) ...@@ -472,24 +473,25 @@ GLenum GLVariablePrecision(const TType &type)
return GL_NONE; return GL_NONE;
} }
TString ArrayString(const TType &type) ImmutableString ArrayString(const TType &type)
{ {
TStringStream arrayString;
if (!type.isArray()) if (!type.isArray())
return arrayString.str(); return ImmutableString("");
const TVector<unsigned int> &arraySizes = *type.getArraySizes(); const TVector<unsigned int> &arraySizes = *type.getArraySizes();
constexpr const size_t kMaxDecimalDigitsPerSize = 10u;
ImmutableStringBuilder arrayString(arraySizes.size() * (kMaxDecimalDigitsPerSize + 2u));
for (auto arraySizeIter = arraySizes.rbegin(); arraySizeIter != arraySizes.rend(); for (auto arraySizeIter = arraySizes.rbegin(); arraySizeIter != arraySizes.rend();
++arraySizeIter) ++arraySizeIter)
{ {
arrayString << "["; arrayString << "[";
if (*arraySizeIter > 0) if (*arraySizeIter > 0)
{ {
arrayString << (*arraySizeIter); arrayString.appendDecimal(*arraySizeIter);
} }
arrayString << "]"; arrayString << "]";
} }
return arrayString.str(); return arrayString;
} }
ImmutableString GetTypeName(const TType &type, ShHashFunction64 hashFunction, NameMap *nameMap) ImmutableString GetTypeName(const TType &type, ShHashFunction64 hashFunction, NameMap *nameMap)
......
...@@ -46,7 +46,7 @@ InterpolationType GetInterpolationType(TQualifier qualifier); ...@@ -46,7 +46,7 @@ InterpolationType GetInterpolationType(TQualifier qualifier);
// Returns array brackets including size with outermost array size first, as specified in GLSL ES // Returns array brackets including size with outermost array size first, as specified in GLSL ES
// 3.10 section 4.1.9. // 3.10 section 4.1.9.
TString ArrayString(const TType &type); ImmutableString ArrayString(const TType &type);
ImmutableString GetTypeName(const TType &type, ShHashFunction64 hashFunction, NameMap *nameMap); ImmutableString GetTypeName(const TType &type, ShHashFunction64 hashFunction, NameMap *nameMap);
......
...@@ -175,3 +175,27 @@ TEST_F(HLSLOutputTest, ParameterWithNoName) ...@@ -175,3 +175,27 @@ TEST_F(HLSLOutputTest, ParameterWithNoName)
})"; })";
compile(shaderString); compile(shaderString);
} }
// Test that array dimensions are written out correctly.
TEST_F(HLSLOutputTest, Array)
{
const std::string &shaderString =
R"(#version 300 es
precision mediump float;
uniform float uf;
out vec4 my_FragColor;
void main()
{
my_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
float arr[2];
for (int i = 0; i < 2; ++i) {
arr[i] = uf * 2.0;
my_FragColor.x += arr[i];
}
})";
compile(shaderString);
EXPECT_TRUE(foundInCode("_arr[2]"));
}
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