Hash :
fbb1c792
Author :
Date :
2018-01-19T16:26:59
Store symbol names as a ImmutableString This will enable compile-time initialization of built-in symbols as well as reducing copying strings. Most of the code that deals with names is changed to use ImmutableString where it makes sense to avoid conversions. The lexer/parser now allocate const char pointers into pool memory instead of allocating TStrings. These are then converted to ImmutableString upon entering TParseContext. BUG=angleproject:2267 TEST=angle_unittests, angle_end2end_tests Change-Id: I244d6271ea1ecf7150d4f89dfa388a7745a1150c Reviewed-on: https://chromium-review.googlesource.com/881561 Commit-Queue: Olli Etuaho <oetuaho@nvidia.com> Reviewed-by: Corentin Wallez <cwallez@chromium.org>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
//
// Copyright (c) 2014 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.
//
// UniformHLSL.cpp:
// Methods for GLSL to HLSL translation for uniforms and uniform blocks.
//
#include "compiler/translator/UniformHLSL.h"
#include "common/utilities.h"
#include "compiler/translator/ImmutableStringBuilder.h"
#include "compiler/translator/StructureHLSL.h"
#include "compiler/translator/UtilsHLSL.h"
#include "compiler/translator/blocklayoutHLSL.h"
#include "compiler/translator/util.h"
namespace sh
{
namespace
{
constexpr const ImmutableString kAngleDecorString("angle_");
static const char *UniformRegisterPrefix(const TType &type)
{
if (IsSampler(type.getBasicType()))
{
return "s";
}
else
{
return "c";
}
}
static TString InterfaceBlockFieldTypeString(const TField &field, TLayoutBlockStorage blockStorage)
{
const TType &fieldType = *field.type();
const TLayoutMatrixPacking matrixPacking = fieldType.getLayoutQualifier().matrixPacking;
ASSERT(matrixPacking != EmpUnspecified);
const TStructure *structure = fieldType.getStruct();
if (fieldType.isMatrix())
{
// Use HLSL row-major packing for GLSL column-major matrices
const TString &matrixPackString =
(matrixPacking == EmpRowMajor ? "column_major" : "row_major");
return matrixPackString + " " + TypeString(fieldType);
}
else if (structure)
{
// Use HLSL row-major packing for GLSL column-major matrices
return QualifiedStructNameString(*structure, matrixPacking == EmpColumnMajor,
blockStorage == EbsStd140);
}
else
{
return TypeString(fieldType);
}
}
static TString InterfaceBlockStructName(const TInterfaceBlock &interfaceBlock)
{
return DecoratePrivate(interfaceBlock.name()) + "_type";
}
void OutputSamplerIndexArrayInitializer(TInfoSinkBase &out,
const TType &type,
unsigned int startIndex)
{
out << "{";
TType elementType(type);
elementType.toArrayElementType();
for (unsigned int i = 0u; i < type.getOutermostArraySize(); ++i)
{
if (i > 0u)
{
out << ", ";
}
if (elementType.isArray())
{
OutputSamplerIndexArrayInitializer(out, elementType,
startIndex + i * elementType.getArraySizeProduct());
}
else
{
out << (startIndex + i);
}
}
out << "}";
}
} // anonymous namespace
UniformHLSL::UniformHLSL(sh::GLenum shaderType,
StructureHLSL *structureHLSL,
ShShaderOutput outputType,
const std::vector<Uniform> &uniforms,
unsigned int firstUniformRegister)
: mUniformRegister(firstUniformRegister),
mUniformBlockRegister(0),
mTextureRegister(0),
mRWTextureRegister(0),
mSamplerCount(0),
mShaderType(shaderType),
mStructureHLSL(structureHLSL),
mOutputType(outputType),
mUniforms(uniforms)
{
}
void UniformHLSL::reserveUniformRegisters(unsigned int registerCount)
{
mUniformRegister = registerCount;
}
void UniformHLSL::reserveUniformBlockRegisters(unsigned int registerCount)
{
mUniformBlockRegister = registerCount;
}
const Uniform *UniformHLSL::findUniformByName(const ImmutableString &name) const
{
for (size_t uniformIndex = 0; uniformIndex < mUniforms.size(); ++uniformIndex)
{
if (name == mUniforms[uniformIndex].name)
{
return &mUniforms[uniformIndex];
}
}
return nullptr;
}
unsigned int UniformHLSL::assignUniformRegister(const TType &type,
const ImmutableString &name,
unsigned int *outRegisterCount)
{
unsigned int registerIndex;
const Uniform *uniform = findUniformByName(name);
ASSERT(uniform);
if (IsSampler(type.getBasicType()) ||
(IsImage(type.getBasicType()) && type.getMemoryQualifier().readonly))
{
registerIndex = mTextureRegister;
}
else if (IsImage(type.getBasicType()))
{
registerIndex = mRWTextureRegister;
}
else
{
registerIndex = mUniformRegister;
}
mUniformRegisterMap[uniform->name] = registerIndex;
unsigned int registerCount = HLSLVariableRegisterCount(*uniform, mOutputType);
if (IsSampler(type.getBasicType()) ||
(IsImage(type.getBasicType()) && type.getMemoryQualifier().readonly))
{
mTextureRegister += registerCount;
}
else if (IsImage(type.getBasicType()))
{
mRWTextureRegister += registerCount;
}
else
{
mUniformRegister += registerCount;
}
if (outRegisterCount)
{
*outRegisterCount = registerCount;
}
return registerIndex;
}
unsigned int UniformHLSL::assignSamplerInStructUniformRegister(const TType &type,
const TString &name,
unsigned int *outRegisterCount)
{
// Sampler that is a field of a uniform structure.
ASSERT(IsSampler(type.getBasicType()));
unsigned int registerIndex = mTextureRegister;
mUniformRegisterMap[std::string(name.c_str())] = registerIndex;
unsigned int registerCount = type.isArray() ? type.getArraySizeProduct() : 1u;
mTextureRegister += registerCount;
if (outRegisterCount)
{
*outRegisterCount = registerCount;
}
return registerIndex;
}
void UniformHLSL::outputHLSLSamplerUniformGroup(
TInfoSinkBase &out,
const HLSLTextureGroup textureGroup,
const TVector<const TVariable *> &group,
const TMap<const TVariable *, TString> &samplerInStructSymbolsToAPINames,
unsigned int *groupTextureRegisterIndex)
{
if (group.empty())
{
return;
}
unsigned int groupRegisterCount = 0;
for (const TVariable *uniform : group)
{
const TType &type = uniform->getType();
const ImmutableString &name = uniform->name();
unsigned int registerCount;
// The uniform might be just a regular sampler or one extracted from a struct.
unsigned int samplerArrayIndex = 0u;
const Uniform *uniformByName = findUniformByName(name);
if (uniformByName)
{
samplerArrayIndex = assignUniformRegister(type, name, ®isterCount);
}
else
{
ASSERT(samplerInStructSymbolsToAPINames.find(uniform) !=
samplerInStructSymbolsToAPINames.end());
samplerArrayIndex = assignSamplerInStructUniformRegister(
type, samplerInStructSymbolsToAPINames.at(uniform), ®isterCount);
}
groupRegisterCount += registerCount;
if (type.isArray())
{
out << "static const uint " << DecorateVariableIfNeeded(*uniform) << ArrayString(type)
<< " = ";
OutputSamplerIndexArrayInitializer(out, type, samplerArrayIndex);
out << ";\n";
}
else
{
out << "static const uint " << DecorateVariableIfNeeded(*uniform) << " = "
<< samplerArrayIndex << ";\n";
}
}
TString suffix = TextureGroupSuffix(textureGroup);
// Since HLSL_TEXTURE_2D is the first group, it has a fixed offset of zero.
if (textureGroup != HLSL_TEXTURE_2D)
{
out << "static const uint textureIndexOffset" << suffix << " = "
<< (*groupTextureRegisterIndex) << ";\n";
out << "static const uint samplerIndexOffset" << suffix << " = "
<< (*groupTextureRegisterIndex) << ";\n";
}
out << "uniform " << TextureString(textureGroup) << " textures" << suffix << "["
<< groupRegisterCount << "]"
<< " : register(t" << (*groupTextureRegisterIndex) << ");\n";
out << "uniform " << SamplerString(textureGroup) << " samplers" << suffix << "["
<< groupRegisterCount << "]"
<< " : register(s" << (*groupTextureRegisterIndex) << ");\n";
*groupTextureRegisterIndex += groupRegisterCount;
}
void UniformHLSL::outputHLSL4_0_FL9_3Sampler(TInfoSinkBase &out,
const TType &type,
const TVariable &variable,
const unsigned int registerIndex)
{
out << "uniform " << SamplerString(type.getBasicType()) << " sampler_"
<< DecorateVariableIfNeeded(variable) << ArrayString(type) << " : register(s"
<< str(registerIndex) << ");\n";
out << "uniform " << TextureString(type.getBasicType()) << " texture_"
<< DecorateVariableIfNeeded(variable) << ArrayString(type) << " : register(t"
<< str(registerIndex) << ");\n";
}
void UniformHLSL::outputHLSL4_1_FL11Texture(TInfoSinkBase &out,
const TType &type,
const TVariable &variable,
const unsigned int registerIndex)
{
// TODO(xinghua.cao@intel.com): if image2D variable is bound on one layer of Texture3D or
// Texture2DArray. Translate this variable to HLSL Texture3D object or HLSL Texture2DArray
// object, or create a temporary Texture2D to save content of the layer and bind the
// temporary Texture2D to image2D variable.
out << "uniform "
<< TextureString(type.getBasicType(), type.getLayoutQualifier().imageInternalFormat) << " "
<< DecorateVariableIfNeeded(variable) << ArrayString(type) << " : register(t"
<< str(registerIndex) << ");\n";
return;
}
void UniformHLSL::outputHLSL4_1_FL11RWTexture(TInfoSinkBase &out,
const TType &type,
const TVariable &variable,
const unsigned int registerIndex)
{
// TODO(xinghua.cao@intel.com): if image2D variable is bound on one layer of Texture3D or
// Texture2DArray. Translate this variable to HLSL RWTexture3D object or HLSL RWTexture2DArray
// object, or create a temporary Texture2D to save content of the layer and bind the
// temporary Texture2D to image2D variable.
if (mShaderType == GL_COMPUTE_SHADER)
{
out << "uniform "
<< RWTextureString(type.getBasicType(), type.getLayoutQualifier().imageInternalFormat)
<< " " << DecorateVariableIfNeeded(variable) << ArrayString(type) << " : register(u"
<< str(registerIndex) << ");\n";
}
else
{
// TODO(xinghua.cao@intel.com): Support images in vertex shader and fragment shader,
// which are needed to sync binding value when linking program.
}
return;
}
void UniformHLSL::outputUniform(TInfoSinkBase &out,
const TType &type,
const TVariable &variable,
const unsigned int registerIndex)
{
const TStructure *structure = type.getStruct();
// If this is a nameless struct, we need to use its full definition, rather than its (empty)
// name.
// TypeString() will invoke defineNameless in this case; qualifier prefixes are unnecessary for
// nameless structs in ES, as nameless structs cannot be used anywhere that layout qualifiers
// are permitted.
const TString &typeName = ((structure && structure->symbolType() != SymbolType::Empty)
? QualifiedStructNameString(*structure, false, false)
: TypeString(type));
const TString ®isterString =
TString("register(") + UniformRegisterPrefix(type) + str(registerIndex) + ")";
out << "uniform " << typeName << " ";
out << DecorateVariableIfNeeded(variable);
out << ArrayString(type) << " : " << registerString << ";\n";
}
void UniformHLSL::uniformsHeader(TInfoSinkBase &out,
ShShaderOutput outputType,
const ReferencedVariables &referencedUniforms,
TSymbolTable *symbolTable)
{
if (!referencedUniforms.empty())
{
out << "// Uniforms\n\n";
}
// In the case of HLSL 4, sampler uniforms need to be grouped by type before the code is
// written. They are grouped based on the combination of the HLSL texture type and
// HLSL sampler type, enumerated in HLSLTextureSamplerGroup.
TVector<TVector<const TVariable *>> groupedSamplerUniforms(HLSL_TEXTURE_MAX + 1);
TMap<const TVariable *, TString> samplerInStructSymbolsToAPINames;
TVector<const TVariable *> imageUniformsHLSL41Output;
for (auto &uniformIt : referencedUniforms)
{
// Output regular uniforms. Group sampler uniforms by type.
const TVariable &variable = *uniformIt.second;
const TType &type = variable.getType();
if (outputType == SH_HLSL_4_1_OUTPUT && IsSampler(type.getBasicType()))
{
HLSLTextureGroup group = TextureGroup(type.getBasicType());
groupedSamplerUniforms[group].push_back(&variable);
}
else if (outputType == SH_HLSL_4_0_FL9_3_OUTPUT && IsSampler(type.getBasicType()))
{
unsigned int registerIndex = assignUniformRegister(type, variable.name(), nullptr);
outputHLSL4_0_FL9_3Sampler(out, type, variable, registerIndex);
}
else if (outputType == SH_HLSL_4_1_OUTPUT && IsImage(type.getBasicType()))
{
imageUniformsHLSL41Output.push_back(&variable);
}
else
{
if (type.isStructureContainingSamplers())
{
TVector<const TVariable *> samplerSymbols;
TMap<const TVariable *, TString> symbolsToAPINames;
ImmutableStringBuilder namePrefix(kAngleDecorString.length() +
variable.name().length());
namePrefix << kAngleDecorString;
namePrefix << variable.name();
type.createSamplerSymbols(namePrefix, TString(variable.name().data()),
&samplerSymbols, &symbolsToAPINames, symbolTable);
for (const TVariable *sampler : samplerSymbols)
{
const TType &samplerType = sampler->getType();
if (outputType == SH_HLSL_4_1_OUTPUT)
{
HLSLTextureGroup group = TextureGroup(samplerType.getBasicType());
groupedSamplerUniforms[group].push_back(sampler);
samplerInStructSymbolsToAPINames[sampler] = symbolsToAPINames[sampler];
}
else if (outputType == SH_HLSL_4_0_FL9_3_OUTPUT)
{
unsigned int registerIndex = assignSamplerInStructUniformRegister(
samplerType, symbolsToAPINames[sampler], nullptr);
outputHLSL4_0_FL9_3Sampler(out, samplerType, *sampler, registerIndex);
}
else
{
ASSERT(outputType == SH_HLSL_3_0_OUTPUT);
unsigned int registerIndex = assignSamplerInStructUniformRegister(
samplerType, symbolsToAPINames[sampler], nullptr);
outputUniform(out, samplerType, *sampler, registerIndex);
}
}
}
unsigned int registerIndex = assignUniformRegister(type, variable.name(), nullptr);
outputUniform(out, type, variable, registerIndex);
}
}
if (outputType == SH_HLSL_4_1_OUTPUT)
{
unsigned int groupTextureRegisterIndex = 0;
// TEXTURE_2D is special, index offset is assumed to be 0 and omitted in that case.
ASSERT(HLSL_TEXTURE_MIN == HLSL_TEXTURE_2D);
for (int groupId = HLSL_TEXTURE_MIN; groupId < HLSL_TEXTURE_MAX; ++groupId)
{
outputHLSLSamplerUniformGroup(
out, HLSLTextureGroup(groupId), groupedSamplerUniforms[groupId],
samplerInStructSymbolsToAPINames, &groupTextureRegisterIndex);
}
mSamplerCount = groupTextureRegisterIndex;
for (const TVariable *image : imageUniformsHLSL41Output)
{
const TType &type = image->getType();
unsigned int registerIndex = assignUniformRegister(type, image->name(), nullptr);
if (type.getMemoryQualifier().readonly)
{
outputHLSL4_1_FL11Texture(out, type, *image, registerIndex);
}
else
{
outputHLSL4_1_FL11RWTexture(out, type, *image, registerIndex);
}
}
}
}
void UniformHLSL::samplerMetadataUniforms(TInfoSinkBase &out, const char *reg)
{
// If mSamplerCount is 0 the shader doesn't use any textures for samplers.
if (mSamplerCount > 0)
{
out << " struct SamplerMetadata\n"
" {\n"
" int baseLevel;\n"
" int internalFormatBits;\n"
" int wrapModes;\n"
" int padding;\n"
" };\n"
" SamplerMetadata samplerMetadata["
<< mSamplerCount << "] : packoffset(" << reg << ");\n";
}
}
TString UniformHLSL::uniformBlocksHeader(const ReferencedInterfaceBlocks &referencedInterfaceBlocks)
{
TString interfaceBlocks;
for (const auto &blockReference : referencedInterfaceBlocks)
{
const TInterfaceBlock &interfaceBlock = *blockReference.second->block;
const TVariable *instanceVariable = blockReference.second->instanceVariable;
if (instanceVariable != nullptr)
{
interfaceBlocks += uniformBlockStructString(interfaceBlock);
}
unsigned int activeRegister = mUniformBlockRegister;
mUniformBlockRegisterMap[interfaceBlock.name().data()] = activeRegister;
if (instanceVariable != nullptr && instanceVariable->getType().isArray())
{
unsigned int instanceArraySize = instanceVariable->getType().getOutermostArraySize();
for (unsigned int arrayIndex = 0; arrayIndex < instanceArraySize; arrayIndex++)
{
interfaceBlocks += uniformBlockString(interfaceBlock, instanceVariable,
activeRegister + arrayIndex, arrayIndex);
}
mUniformBlockRegister += instanceArraySize;
}
else
{
interfaceBlocks += uniformBlockString(interfaceBlock, instanceVariable, activeRegister,
GL_INVALID_INDEX);
mUniformBlockRegister += 1u;
}
}
return (interfaceBlocks.empty() ? "" : ("// Uniform Blocks\n\n" + interfaceBlocks));
}
TString UniformHLSL::uniformBlockString(const TInterfaceBlock &interfaceBlock,
const TVariable *instanceVariable,
unsigned int registerIndex,
unsigned int arrayIndex)
{
const TString &arrayIndexString = (arrayIndex != GL_INVALID_INDEX ? str(arrayIndex) : "");
const TString &blockName = TString(interfaceBlock.name().data()) + arrayIndexString;
TString hlsl;
hlsl += "cbuffer " + blockName + " : register(b" + str(registerIndex) +
")\n"
"{\n";
if (instanceVariable != nullptr)
{
hlsl += " " + InterfaceBlockStructName(interfaceBlock) + " " +
UniformBlockInstanceString(instanceVariable->name(), arrayIndex) + ";\n";
}
else
{
const TLayoutBlockStorage blockStorage = interfaceBlock.blockStorage();
hlsl += uniformBlockMembersString(interfaceBlock, blockStorage);
}
hlsl += "};\n\n";
return hlsl;
}
TString UniformHLSL::UniformBlockInstanceString(const ImmutableString &instanceName,
unsigned int arrayIndex)
{
if (arrayIndex != GL_INVALID_INDEX)
{
return DecoratePrivate(instanceName) + "_" + str(arrayIndex);
}
else
{
return Decorate(instanceName);
}
}
TString UniformHLSL::uniformBlockMembersString(const TInterfaceBlock &interfaceBlock,
TLayoutBlockStorage blockStorage)
{
TString hlsl;
Std140PaddingHelper padHelper = mStructureHLSL->getPaddingHelper();
for (unsigned int typeIndex = 0; typeIndex < interfaceBlock.fields().size(); typeIndex++)
{
const TField &field = *interfaceBlock.fields()[typeIndex];
const TType &fieldType = *field.type();
if (blockStorage == EbsStd140)
{
// 2 and 3 component vector types in some cases need pre-padding
hlsl += padHelper.prePaddingString(fieldType);
}
hlsl += " " + InterfaceBlockFieldTypeString(field, blockStorage) + " " +
Decorate(field.name()) + ArrayString(fieldType) + ";\n";
// must pad out after matrices and arrays, where HLSL usually allows itself room to pack
// stuff
if (blockStorage == EbsStd140)
{
const bool useHLSLRowMajorPacking =
(fieldType.getLayoutQualifier().matrixPacking == EmpColumnMajor);
hlsl += padHelper.postPaddingString(fieldType, useHLSLRowMajorPacking);
}
}
return hlsl;
}
TString UniformHLSL::uniformBlockStructString(const TInterfaceBlock &interfaceBlock)
{
const TLayoutBlockStorage blockStorage = interfaceBlock.blockStorage();
return "struct " + InterfaceBlockStructName(interfaceBlock) +
"\n"
"{\n" +
uniformBlockMembersString(interfaceBlock, blockStorage) + "};\n\n";
}
}