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 @@
#include "compiler/translator/ImmutableStringBuilder.h"
#include <stdio.h>
namespace sh
{
......@@ -39,6 +41,14 @@ ImmutableStringBuilder &ImmutableStringBuilder::operator<<(const char &c)
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()
{
mData[mPos] = '\0';
......
......@@ -32,6 +32,8 @@ class ImmutableStringBuilder
// This invalidates the ImmutableStringBuilder, so it should only be called once.
operator ImmutableString();
void appendDecimal(const uint32_t &i);
template <typename T>
void appendHex(T number)
{
......
......@@ -393,7 +393,7 @@ TString OutputHLSL::generateStructMapping(const std::vector<MappedStruct> &std14
if (structType->isArray())
{
mappedStructs += ArrayString(*mappedStruct.field->type());
mappedStructs += ArrayString(*mappedStruct.field->type()).data();
}
mappedStructs += " =\n";
......@@ -404,33 +404,37 @@ TString OutputHLSL::generateStructMapping(const std::vector<MappedStruct> &std14
return mappedStructs;
}
void OutputHLSL::header(TInfoSinkBase &out,
const std::vector<MappedStruct> &std140Structs,
const BuiltInFunctionEmulator *builtInFunctionEmulator) const
void OutputHLSL::writeReferencedAttributes(TInfoSinkBase &out) const
{
TString varyings;
TString attributes;
TString mappedStructs = generateStructMapping(std140Structs);
for (const auto &attribute : mReferencedAttributes)
{
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)
{
const TType &type = varying.second->getType();
const ImmutableString &name = varying.second->name();
// Program linking depends on this exact format
varyings += TString("static ") + InterpolationString(type.getQualifier()) + " " +
TypeString(type) + " " + Decorate(name) + ArrayString(type) + " = " +
zeroInitializer(type) + ";\n";
out << "static " << InterpolationString(type.getQualifier()) << " " << TypeString(type)
<< " " << Decorate(name) << ArrayString(type) << " = " << zeroInitializer(type)
<< ";\n";
}
}
for (const auto &attribute : mReferencedAttributes)
{
const TType &type = attribute.second->getType();
const ImmutableString &name = attribute.second->name();
attributes += "static " + TypeString(type) + " " + Decorate(name) + ArrayString(type) +
" = " + zeroInitializer(type) + ";\n";
}
void OutputHLSL::header(TInfoSinkBase &out,
const std::vector<MappedStruct> &std140Structs,
const BuiltInFunctionEmulator *builtInFunctionEmulator) const
{
TString mappedStructs = generateStructMapping(std140Structs);
out << mStructureHLSL->structsHeader();
......@@ -491,7 +495,7 @@ void OutputHLSL::header(TInfoSinkBase &out,
IsExtensionEnabled(mExtensionBehavior, TExtension::EXT_draw_buffers);
out << "// Varyings\n";
out << varyings;
writeReferencedVaryings(out);
out << "\n";
if (mShaderVersion >= 300)
......@@ -644,7 +648,7 @@ void OutputHLSL::header(TInfoSinkBase &out,
else if (mShaderType == GL_VERTEX_SHADER)
{
out << "// Attributes\n";
out << attributes;
writeReferencedAttributes(out);
out << "\n"
"static float4 gl_Position = float4(0, 0, 0, 0);\n";
......@@ -665,7 +669,7 @@ void OutputHLSL::header(TInfoSinkBase &out,
out << "\n"
"// Varyings\n";
out << varyings;
writeReferencedVaryings(out);
out << "\n";
if (mUsesDepthRange)
......
......@@ -72,6 +72,8 @@ class OutputHLSL : public TIntermTraverser
}
protected:
void writeReferencedAttributes(TInfoSinkBase &out) const;
void writeReferencedVaryings(TInfoSinkBase &out) const;
void header(TInfoSinkBase &out,
const std::vector<MappedStruct> &std140Structs,
const BuiltInFunctionEmulator *builtInFunctionEmulator) const;
......
......@@ -53,7 +53,7 @@ TString Define(const TStructure &structure,
}
string += " " + fieldTypeString + " " + DecorateField(field->name(), structure) +
ArrayString(fieldType) + ";\n";
ArrayString(fieldType).data() + ";\n";
if (padHelper)
{
......@@ -76,7 +76,8 @@ TString WriteParameterList(const std::vector<TType> &parameters)
{
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)
{
......
......@@ -613,7 +613,7 @@ TString UniformHLSL::uniformBlockMembersString(const TInterfaceBlock &interfaceB
}
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
// stuff
......
......@@ -10,6 +10,7 @@
#include "common/utilities.h"
#include "compiler/preprocessor/numeric_lex.h"
#include "compiler/translator/ImmutableStringBuilder.h"
#include "compiler/translator/SymbolTable.h"
bool atoi_clamp(const char *str, unsigned int *value)
......@@ -472,24 +473,25 @@ GLenum GLVariablePrecision(const TType &type)
return GL_NONE;
}
TString ArrayString(const TType &type)
ImmutableString ArrayString(const TType &type)
{
TStringStream arrayString;
if (!type.isArray())
return arrayString.str();
return ImmutableString("");
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();
++arraySizeIter)
{
arrayString << "[";
if (*arraySizeIter > 0)
{
arrayString << (*arraySizeIter);
arrayString.appendDecimal(*arraySizeIter);
}
arrayString << "]";
}
return arrayString.str();
return arrayString;
}
ImmutableString GetTypeName(const TType &type, ShHashFunction64 hashFunction, NameMap *nameMap)
......
......@@ -46,7 +46,7 @@ InterpolationType GetInterpolationType(TQualifier qualifier);
// Returns array brackets including size with outermost array size first, as specified in GLSL ES
// 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);
......
......@@ -175,3 +175,27 @@ TEST_F(HLSLOutputTest, ParameterWithNoName)
})";
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