Hash :
847638a6
Author :
Date :
2015-11-20T13:01:41
Re-land "D3D: Generate more shader debug info by default." Re-land with fix for shader size query. The debug info can be very useful to WebGL developers. Instead of hiding the feature behind an unsupported and broken flag, always enable it so that WebGL devs can access the useful translated info. Change the debug info policy to build the full info (including generated assembly) in Debug, and all info excepth the assembly in Release. BUG=angleproject:1179 Change-Id: I812b2c9ba98cb8c9d5ec95721441c70e8990b626 Reviewed-on: https://chromium-review.googlesource.com/313600 Tryjob-Request: Jamie Madill <jmadill@chromium.org> Reviewed-by: Geoff Lang <geofflang@chromium.org> Tested-by: Jamie Madill <jmadill@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
//
// Copyright (c) 2002-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.
//
// Shader.cpp: Implements the gl::Shader class and its derived classes
// VertexShader and FragmentShader. Implements GL shader objects and related
// functionality. [OpenGL ES 2.0.24] section 2.10 page 24 and section 3.8 page 84.
#include "libANGLE/Shader.h"
#include <sstream>
#include "common/utilities.h"
#include "GLSLANG/ShaderLang.h"
#include "libANGLE/Compiler.h"
#include "libANGLE/Constants.h"
#include "libANGLE/renderer/Renderer.h"
#include "libANGLE/renderer/ShaderImpl.h"
#include "libANGLE/ResourceManager.h"
namespace gl
{
namespace
{
template <typename VarT>
std::vector<VarT> GetActiveShaderVariables(const std::vector<VarT> *variableList)
{
ASSERT(variableList);
std::vector<VarT> result;
for (size_t varIndex = 0; varIndex < variableList->size(); varIndex++)
{
const VarT &var = variableList->at(varIndex);
if (var.staticUse)
{
result.push_back(var);
}
}
return result;
}
template <typename VarT>
const std::vector<VarT> &GetShaderVariables(const std::vector<VarT> *variableList)
{
ASSERT(variableList);
return *variableList;
}
} // anonymous namespace
// true if varying x has a higher priority in packing than y
bool CompareShaderVar(const sh::ShaderVariable &x, const sh::ShaderVariable &y)
{
if (x.type == y.type)
{
return x.arraySize > y.arraySize;
}
// Special case for handling structs: we sort these to the end of the list
if (x.type == GL_STRUCT_ANGLEX)
{
return false;
}
if (y.type == GL_STRUCT_ANGLEX)
{
return true;
}
return gl::VariableSortOrder(x.type) < gl::VariableSortOrder(y.type);
}
Shader::Data::Data(GLenum shaderType) : mShaderType(shaderType), mShaderVersion(100)
{
}
Shader::Data::~Data()
{
}
Shader::Shader(ResourceManager *manager,
rx::ImplFactory *implFactory,
const gl::Limitations &rendererLimitations,
GLenum type,
GLuint handle)
: mData(type),
mImplementation(implFactory->createShader(mData)),
mRendererLimitations(rendererLimitations),
mHandle(handle),
mType(type),
mRefCount(0),
mDeleteStatus(false),
mCompiled(false),
mResourceManager(manager)
{
ASSERT(mImplementation);
}
Shader::~Shader()
{
SafeDelete(mImplementation);
}
GLuint Shader::getHandle() const
{
return mHandle;
}
void Shader::setSource(GLsizei count, const char *const *string, const GLint *length)
{
std::ostringstream stream;
for (int i = 0; i < count; i++)
{
if (length == nullptr || length[i] < 0)
{
stream.write(string[i], strlen(string[i]));
}
else
{
stream.write(string[i], length[i]);
}
}
mData.mSource = stream.str();
}
int Shader::getInfoLogLength() const
{
if (mInfoLog.empty())
{
return 0;
}
return (static_cast<int>(mInfoLog.length()) + 1);
}
void Shader::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
{
int index = 0;
if (bufSize > 0)
{
index = std::min(bufSize - 1, static_cast<GLsizei>(mInfoLog.length()));
memcpy(infoLog, mInfoLog.c_str(), index);
infoLog[index] = '\0';
}
if (length)
{
*length = index;
}
}
int Shader::getSourceLength() const
{
return mData.mSource.empty() ? 0 : (static_cast<int>(mData.mSource.length()) + 1);
}
int Shader::getTranslatedSourceLength() const
{
if (mData.mTranslatedSource.empty())
{
return 0;
}
return (static_cast<int>(mData.mTranslatedSource.length()) + 1);
}
int Shader::getTranslatedSourceWithDebugInfoLength() const
{
const std::string &debugInfo = mImplementation->getDebugInfo();
if (debugInfo.empty())
{
return 0;
}
return (static_cast<int>(debugInfo.length()) + 1);
}
void Shader::getSourceImpl(const std::string &source, GLsizei bufSize, GLsizei *length, char *buffer)
{
int index = 0;
if (bufSize > 0)
{
index = std::min(bufSize - 1, static_cast<GLsizei>(source.length()));
memcpy(buffer, source.c_str(), index);
buffer[index] = '\0';
}
if (length)
{
*length = index;
}
}
void Shader::getSource(GLsizei bufSize, GLsizei *length, char *buffer) const
{
getSourceImpl(mData.mSource, bufSize, length, buffer);
}
void Shader::getTranslatedSource(GLsizei bufSize, GLsizei *length, char *buffer) const
{
getSourceImpl(mData.mTranslatedSource, bufSize, length, buffer);
}
void Shader::getTranslatedSourceWithDebugInfo(GLsizei bufSize, GLsizei *length, char *buffer) const
{
const std::string &debugInfo = mImplementation->getDebugInfo();
getSourceImpl(debugInfo, bufSize, length, buffer);
}
void Shader::compile(Compiler *compiler)
{
mData.mTranslatedSource.clear();
mInfoLog.clear();
mData.mShaderVersion = 100;
mData.mVaryings.clear();
mData.mUniforms.clear();
mData.mInterfaceBlocks.clear();
mData.mActiveAttributes.clear();
mData.mActiveOutputVariables.clear();
ShHandle compilerHandle = compiler->getCompilerHandle(mData.mShaderType);
std::stringstream sourceStream;
std::string sourcePath;
int additionalOptions =
mImplementation->prepareSourceAndReturnOptions(&sourceStream, &sourcePath);
int compileOptions = (SH_OBJECT_CODE | SH_VARIABLES | additionalOptions);
// Some targets (eg D3D11 Feature Level 9_3 and below) do not support non-constant loop indexes
// in fragment shaders. Shader compilation will fail. To provide a better error message we can
// instruct the compiler to pre-validate.
if (mRendererLimitations.shadersRequireIndexedLoopValidation)
{
compileOptions |= SH_VALIDATE_LOOP_INDEXING;
}
std::string sourceString = sourceStream.str();
std::vector<const char *> sourceCStrings;
if (!sourcePath.empty())
{
sourceCStrings.push_back(sourcePath.c_str());
}
sourceCStrings.push_back(sourceString.c_str());
bool result =
ShCompile(compilerHandle, &sourceCStrings[0], sourceCStrings.size(), compileOptions);
if (!result)
{
mInfoLog = ShGetInfoLog(compilerHandle);
TRACE("\n%s", mInfoLog.c_str());
mCompiled = false;
return;
}
mData.mTranslatedSource = ShGetObjectCode(compilerHandle);
#ifndef NDEBUG
// Prefix translated shader with commented out un-translated shader.
// Useful in diagnostics tools which capture the shader source.
std::ostringstream shaderStream;
shaderStream << "// GLSL\n";
shaderStream << "//\n";
size_t curPos = 0;
while (curPos != std::string::npos)
{
size_t nextLine = mData.mSource.find("\n", curPos);
size_t len = (nextLine == std::string::npos) ? std::string::npos : (nextLine - curPos + 1);
shaderStream << "// " << mData.mSource.substr(curPos, len);
curPos = (nextLine == std::string::npos) ? std::string::npos : (nextLine + 1);
}
shaderStream << "\n\n";
shaderStream << mData.mTranslatedSource;
mData.mTranslatedSource = shaderStream.str();
#endif
// Gather the shader information
mData.mShaderVersion = ShGetShaderVersion(compilerHandle);
mData.mVaryings = GetShaderVariables(ShGetVaryings(compilerHandle));
mData.mUniforms = GetShaderVariables(ShGetUniforms(compilerHandle));
mData.mInterfaceBlocks = GetShaderVariables(ShGetInterfaceBlocks(compilerHandle));
if (mData.mShaderType == GL_VERTEX_SHADER)
{
mData.mActiveAttributes = GetActiveShaderVariables(ShGetAttributes(compilerHandle));
}
else
{
ASSERT(mData.mShaderType == GL_FRAGMENT_SHADER);
// TODO(jmadill): Figure out why we only sort in the FS, and if we need to.
std::sort(mData.mVaryings.begin(), mData.mVaryings.end(), CompareShaderVar);
mData.mActiveOutputVariables =
GetActiveShaderVariables(ShGetOutputVariables(compilerHandle));
}
ASSERT(!mData.mTranslatedSource.empty());
mCompiled = mImplementation->postTranslateCompile(compiler, &mInfoLog);
}
void Shader::addRef()
{
mRefCount++;
}
void Shader::release()
{
mRefCount--;
if (mRefCount == 0 && mDeleteStatus)
{
mResourceManager->deleteShader(mHandle);
}
}
unsigned int Shader::getRefCount() const
{
return mRefCount;
}
bool Shader::isFlaggedForDeletion() const
{
return mDeleteStatus;
}
void Shader::flagForDeletion()
{
mDeleteStatus = true;
}
int Shader::getShaderVersion() const
{
return mData.mShaderVersion;
}
const std::vector<sh::Varying> &Shader::getVaryings() const
{
return mData.getVaryings();
}
const std::vector<sh::Uniform> &Shader::getUniforms() const
{
return mData.getUniforms();
}
const std::vector<sh::InterfaceBlock> &Shader::getInterfaceBlocks() const
{
return mData.getInterfaceBlocks();
}
const std::vector<sh::Attribute> &Shader::getActiveAttributes() const
{
return mData.getActiveAttributes();
}
const std::vector<sh::OutputVariable> &Shader::getActiveOutputVariables() const
{
return mData.getActiveOutputVariables();
}
int Shader::getSemanticIndex(const std::string &attributeName) const
{
if (!attributeName.empty())
{
const auto &activeAttributes = mData.getActiveAttributes();
int semanticIndex = 0;
for (size_t attributeIndex = 0; attributeIndex < activeAttributes.size(); attributeIndex++)
{
const sh::ShaderVariable &attribute = activeAttributes[attributeIndex];
if (attribute.name == attributeName)
{
return semanticIndex;
}
semanticIndex += gl::VariableRegisterCount(attribute.type);
}
}
return -1;
}
}