HLSL: Add location offsets per resource type

This PR adds the ability to offset sampler, texture, and UBO bindings
from provided base bindings, and to auto-number bindings that are not
provided with explicit register numbers. The mechanism works as
follows:

- Offsets may be given on the command line for all stages, or
  individually for one or more single stages, in which case the
  offset will be auto-selected according to the stage being
  compiled. There is also an API to set them. The new command line
  options are --shift-sampler-binding, --shift-texture-binding, and
  --shift-UBO-binding.

- Uniforms which are not given explicit bindings in the source code
  are auto-numbered if and only if they are in live code as
  determined by the algorithm used to build the reflection
  database, and the --auto-map-bindings option is given. This auto-numbering
  avoids using any binding slots which were explicitly provided in
  the code, whether or not that explicit use was live. E.g, "uniform
  Texture1D foo : register(t3);" with --shift-texture-binding 10 will
  reserve binding 13, whether or not foo is used in live code.

- Shorter synonyms for the command line options are available.  See
  the --help output.

The testing infrastructure is slightly extended to allow use of the
binding offset API, and two new tests spv.register.(no)autoassign.frag are
added for comparing the resulting SPIR-V.
This commit is contained in:
steve-lunarg
2016-09-07 15:20:19 -06:00
parent a1e2d4952e
commit 7f7c2ed780
13 changed files with 1191 additions and 10 deletions

View File

@@ -10,6 +10,7 @@ set(SOURCES
MachineIndependent/glslang.y
MachineIndependent/glslang_tab.cpp
MachineIndependent/Constant.cpp
MachineIndependent/iomapper.cpp
MachineIndependent/InfoSink.cpp
MachineIndependent/Initialize.cpp
MachineIndependent/IntermTraverse.cpp
@@ -55,6 +56,7 @@ set(HEADERS
MachineIndependent/glslang_tab.cpp.h
MachineIndependent/gl_types.h
MachineIndependent/Initialize.h
MachineIndependent/iomapper.h
MachineIndependent/LiveTraverser.h
MachineIndependent/localintermediate.h
MachineIndependent/ParseHelper.h

View File

@@ -60,6 +60,7 @@
#define SH_EXPORTING
#include "../Public/ShaderLang.h"
#include "reflection.h"
#include "iomapper.h"
#include "Initialize.h"
namespace { // anonymous namespace for file-local functions and symbols
@@ -1488,6 +1489,10 @@ void TShader::setEntryPoint(const char* entryPoint)
intermediate->setEntryPointName(entryPoint);
}
void TShader::setShiftSamplerBinding(unsigned int base) { intermediate->setShiftSamplerBinding(base); }
void TShader::setShiftTextureBinding(unsigned int base) { intermediate->setShiftTextureBinding(base); }
void TShader::setShiftUboBinding(unsigned int base) { intermediate->setShiftUboBinding(base); }
void TShader::setAutoMapBindings(bool map) { intermediate->setAutoMapBindings(map); }
//
// Turn the shader strings into a parse tree in the TIntermediate.
//
@@ -1548,7 +1553,7 @@ const char* TShader::getInfoDebugLog()
return infoSink->debug.c_str();
}
TProgram::TProgram() : pool(0), reflection(0), linked(false)
TProgram::TProgram() : pool(0), reflection(0), ioMapper(nullptr), linked(false)
{
infoSink = new TInfoSink;
for (int s = 0; s < EShLangCount; ++s) {
@@ -1700,4 +1705,24 @@ int TProgram::getAttributeType(int index) { return reflection->getAtt
void TProgram::dumpReflection() { reflection->dump(); }
//
// I/O mapping implementation.
//
bool TProgram::mapIO()
{
if (! linked || ioMapper)
return false;
ioMapper = new TIoMapper;
for (int s = 0; s < EShLangCount; ++s) {
if (intermediate[s]) {
if (! ioMapper->addStage((EShLanguage)s, *intermediate[s]))
return false;
}
}
return true;
}
} // end namespace glslang

View File

@@ -0,0 +1,249 @@
//
//Copyright (C) 2016 LunarG, Inc.
//
//All rights reserved.
//
//Redistribution and use in source and binary forms, with or without
//modification, are permitted provided that the following conditions
//are met:
//
// Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
//
// Neither the name of 3Dlabs Inc. Ltd. nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
//"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
//LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
//FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
//COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
//INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
//BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
//LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
//CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
//LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
//ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
//POSSIBILITY OF SUCH DAMAGE.
//
#include "../Include/Common.h"
#include "iomapper.h"
#include "LiveTraverser.h"
#include "localintermediate.h"
#include "gl_types.h"
#include <unordered_set>
#include <unordered_map>
//
// Map IO bindings.
//
// High-level algorithm for one stage:
//
// 1. Traverse all code (live+dead) to find the explicitly provided bindings.
//
// 2. Traverse (just) the live code to determine which non-provided bindings
// require auto-numbering. We do not auto-number dead ones.
//
// 3. Traverse all the code to apply the bindings:
// a. explicitly given bindings are offset according to their type
// b. implicit live bindings are auto-numbered into the holes, using
// any open binding slot.
// c. implicit dead bindings are left un-bound.
//
namespace glslang {
// Map of IDs to bindings
typedef std::unordered_map<unsigned int, int> TBindingMap;
typedef std::unordered_set<int> TUsedBindings;
// This traverses the AST to determine which bindings are used, and which are implicit
// (for subsequent auto-numbering)
class TBindingTraverser : public TLiveTraverser {
public:
TBindingTraverser(const TIntermediate& i, TBindingMap& bindingMap, TUsedBindings& usedBindings,
bool traverseDeadCode = false) :
TLiveTraverser(i, traverseDeadCode),
bindingMap(bindingMap),
usedBindings(usedBindings)
{ }
protected:
virtual void visitSymbol(TIntermSymbol* base) {
if (base->getQualifier().storage == EvqUniform)
addUniform(*base);
}
// Return the right binding base given the variable type.
int getBindingBase(const TType& type) {
if (type.getBasicType() == EbtSampler) {
const TSampler& sampler = type.getSampler();
if (sampler.isPureSampler())
return intermediate.getShiftSamplerBinding();
if (sampler.isTexture())
return intermediate.getShiftTextureBinding();
}
if (type.getQualifier().isUniformOrBuffer())
return intermediate.getShiftUboBinding();
return -1; // not a type with a binding
}
// Mark a given base symbol ID as being bound to 'binding'
void markBinding(const TIntermSymbol& base, int binding) {
bindingMap[base.getId()] = binding;
if (binding >= 0) {
// const TType& type = base.getType();
const unsigned int size = 1; // type.isArray() ? type.getCumulativeArraySize() : 1;
for (unsigned int offset=0; offset<size; ++offset)
usedBindings.insert(binding + offset);
}
}
// Mark the bindings that are given explicitly, and set ones that need
// implicit bindings to -1 for a subsequent pass. (Can't happen in this
// pass because explicit bindings in dead code reserve the location).
virtual void addUniform(TIntermSymbol& base)
{
// Skip ones we've already seen.
if (bindingMap.find(base.getId()) != bindingMap.end())
return;
const TType& type = base.getType();
const int bindingBase = getBindingBase(type);
// Return if it's not a type we bind
if (bindingBase == -1)
return;
if (type.getQualifier().hasBinding()) {
// It has a binding: keep that one.
markBinding(base, type.getQualifier().layoutBinding + bindingBase);
} else if (!traverseAll) {
// Mark it as something we need to dynamically create a binding for,
// only if we're walking just the live code. We don't auto-number
// in dead code.
markBinding(base, -1);
}
}
TBindingMap& bindingMap;
TUsedBindings& usedBindings;
};
// This traverses the AST and applies binding maps it's given.
class TIoMappingTraverser : public TBindingTraverser {
public:
TIoMappingTraverser(TIntermediate& i, TBindingMap& bindingMap, TUsedBindings& usedBindings,
bool traverseDeadCode) :
TBindingTraverser(i, bindingMap, usedBindings, traverseDeadCode),
nextBinding(1)
{ }
protected:
void addUniform(TIntermSymbol& base) override
{
// Skip things we don't intend to bind.
if (bindingMap.find(base.getId()) == bindingMap.end())
return;
const int existingBinding = bindingMap[base.getId()];
// Apply existing binding, if we were given one or already made one up.
if (existingBinding != -1) {
base.getWritableType().getQualifier().layoutBinding = existingBinding;
return;
}
if (intermediate.getAutoMapBindings()) {
// Otherwise, find a free spot for it.
const int freeBinding = getFreeBinding(base.getType());
markBinding(base, freeBinding);
base.getWritableType().getQualifier().layoutBinding = freeBinding;
}
}
// Search for N free consecutive binding slots in [base, base+required).
// E.g, if we want to reserve consecutive bindings for flattened arrays.
bool hasNFreeSlots(int base, int required) {
for (int binding = base; binding < (base + required); ++binding)
if (usedBindings.find(binding) != usedBindings.end())
return false;
return true;
}
// Find a free binding spot
int getFreeBinding(const TType&) {
while (!hasNFreeSlots(nextBinding, 1))
++nextBinding;
return nextBinding;
}
int nextBinding;
};
// Map I/O variables to provided offsets, and make bindings for
// unbound but live variables.
//
// Returns false if the input is too malformed to do this.
bool TIoMapper::addStage(EShLanguage, TIntermediate& intermediate)
{
// Trivial return if there is nothing to do.
if (intermediate.getShiftSamplerBinding() == 0 &&
intermediate.getShiftTextureBinding() == 0 &&
intermediate.getShiftUboBinding() == 0 &&
intermediate.getAutoMapBindings() == false)
return true;
if (intermediate.getNumEntryPoints() != 1 || intermediate.isRecursive())
return false;
TIntermNode* root = intermediate.getTreeRoot();
if (root == nullptr)
return false;
// The lifetime of this data spans several passes.
TBindingMap bindingMap;
TUsedBindings usedBindings;
TBindingTraverser it_binding_all(intermediate, bindingMap, usedBindings, true);
TBindingTraverser it_binding_live(intermediate, bindingMap, usedBindings, false);
TIoMappingTraverser it_iomap(intermediate, bindingMap, usedBindings, true);
// Traverse all (live+dead) code to find explicit bindings, so we can avoid those.
root->traverse(&it_binding_all);
// Traverse just live code to find things that need implicit bindings.
it_binding_live.pushFunction(intermediate.getEntryPointMangledName().c_str());
while (! it_binding_live.functions.empty()) {
TIntermNode* function = it_binding_live.functions.back();
it_binding_live.functions.pop_back();
function->traverse(&it_binding_live);
}
// Bind everything that needs a binding and doesn't have one.
root->traverse(&it_iomap);
return true;
}
} // end namespace glslang

View File

@@ -0,0 +1,61 @@
//
//Copyright (C) 2016 LunarG, Inc.
//
//All rights reserved.
//
//Redistribution and use in source and binary forms, with or without
//modification, are permitted provided that the following conditions
//are met:
//
// Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
//
// Neither the name of 3Dlabs Inc. Ltd. nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
//"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
//LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
//FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
//COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
//INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
//BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
//LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
//CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
//LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
//ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
//POSSIBILITY OF SUCH DAMAGE.
//
#ifndef _IOMAPPER_INCLUDED
#define _IOMAPPER_INCLUDED
#include "../Public/ShaderLang.h"
//
// A reflection database and its interface, consistent with the OpenGL API reflection queries.
//
namespace glslang {
class TIntermediate;
// I/O mapper
class TIoMapper {
public:
TIoMapper() {}
virtual ~TIoMapper() {}
// grow the reflection stage by stage
bool addStage(EShLanguage, TIntermediate&);
};
} // end namespace glslang
#endif // _IOMAPPER_INCLUDED

View File

@@ -141,7 +141,11 @@ public:
invocations(TQualifier::layoutNotSet), vertices(TQualifier::layoutNotSet), inputPrimitive(ElgNone), outputPrimitive(ElgNone),
pixelCenterInteger(false), originUpperLeft(false),
vertexSpacing(EvsNone), vertexOrder(EvoNone), pointMode(false), earlyFragmentTests(false), depthLayout(EldNone), depthReplacing(false), blendEquations(0),
multiStream(false), xfbMode(false)
multiStream(false), xfbMode(false),
shiftSamplerBinding(0),
shiftTextureBinding(0),
shiftUboBinding(0),
autoMapBindings(false)
{
localSize[0] = 1;
localSize[1] = 1;
@@ -163,6 +167,16 @@ public:
void setEntryPointMangledName(const char* ep) { entryPointMangledName = ep; }
const std::string& getEntryPointName() const { return entryPointName; }
const std::string& getEntryPointMangledName() const { return entryPointMangledName; }
void setShiftSamplerBinding(unsigned int shift) { shiftSamplerBinding = shift; }
unsigned int getShiftSamplerBinding() const { return shiftSamplerBinding; }
void setShiftTextureBinding(unsigned int shift) { shiftTextureBinding = shift; }
unsigned int getShiftTextureBinding() const { return shiftTextureBinding; }
void setShiftUboBinding(unsigned int shift) { shiftUboBinding = shift; }
unsigned int getShiftUboBinding() const { return shiftUboBinding; }
void setAutoMapBindings(bool map) { autoMapBindings = map; }
bool getAutoMapBindings() const { return autoMapBindings; }
void setVersion(int v) { version = v; }
int getVersion() const { return version; }
void setProfile(EProfile p) { profile = p; }
@@ -367,6 +381,11 @@ protected:
EShSource source; // source language, known a bit later
std::string entryPointName;
std::string entryPointMangledName;
unsigned int shiftSamplerBinding;
unsigned int shiftTextureBinding;
unsigned int shiftUboBinding;
bool autoMapBindings;
EProfile profile;
int version;
SpvVersion spvVersion;

View File

@@ -300,6 +300,10 @@ public:
const char* const* s, const int* l, const char* const* names, int n);
void setPreamble(const char* s) { preamble = s; }
void setEntryPoint(const char* entryPoint);
void setShiftSamplerBinding(unsigned int base);
void setShiftTextureBinding(unsigned int base);
void setShiftUboBinding(unsigned int base);
void setAutoMapBindings(bool map);
// Interface to #include handlers.
//
@@ -433,6 +437,7 @@ private:
};
class TReflection;
class TIoMapper;
// Make one TProgram per set of shaders that will get linked together. Add all
// the shaders that are to be linked together. After calling shader.parse()
@@ -470,6 +475,9 @@ public:
int getAttributeType(int index); // can be used for glGetActiveAttrib()
void dumpReflection();
// I/O mapping: apply base offsets and map live unbound variables
bool mapIO();
protected:
bool linkStage(EShLanguage, EShMessages);
@@ -479,6 +487,7 @@ protected:
bool newedIntermediate[EShLangCount]; // track which intermediate were "new" versus reusing a singleton unit in a stage
TInfoSink* infoSink;
TReflection* reflection;
TIoMapper* ioMapper;
bool linked;
private: