Brandon Jones | f05cdee | 2014-08-27 15:24:07 -0700 | [diff] [blame^] | 1 | #include "precompiled.h" |
| 2 | // |
| 3 | // Copyright (c) 2014 The ANGLE Project Authors. All rights reserved. |
| 4 | // Use of this source code is governed by a BSD-style license that can be |
| 5 | // found in the LICENSE file. |
| 6 | // |
| 7 | |
| 8 | // ShaderD3D.cpp: Defines the rx::ShaderD3D class which implements rx::ShaderImpl. |
| 9 | |
| 10 | #include "libGLESv2/renderer/d3d/ShaderD3D.h" |
| 11 | |
| 12 | #include "common/utilities.h" |
| 13 | #include "libGLESv2/Shader.h" |
| 14 | #include "libGLESv2/renderer/Renderer.h" |
| 15 | #include "libGLESv2/main.h" |
| 16 | |
| 17 | namespace rx |
| 18 | { |
| 19 | |
| 20 | void *ShaderD3D::mFragmentCompiler = NULL; |
| 21 | void *ShaderD3D::mVertexCompiler = NULL; |
| 22 | |
| 23 | template <typename VarT> |
| 24 | const std::vector<VarT> *GetShaderVariables(const std::vector<VarT> *variableList) |
| 25 | { |
| 26 | // TODO: handle staticUse. for now, assume all returned variables are active. |
| 27 | ASSERT(variableList); |
| 28 | return variableList; |
| 29 | } |
| 30 | |
| 31 | ShaderD3D::ShaderD3D(rx::Renderer *renderer) |
| 32 | : ShaderImpl(), |
| 33 | mRenderer(renderer), |
| 34 | mShaderVersion(100) |
| 35 | { |
| 36 | uncompile(); |
| 37 | initializeCompiler(); |
| 38 | } |
| 39 | |
| 40 | ShaderD3D::~ShaderD3D() |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | ShaderD3D *ShaderD3D::makeShaderD3D(ShaderImpl *impl) |
| 45 | { |
| 46 | ASSERT(HAS_DYNAMIC_TYPE(ShaderD3D*, impl)); |
| 47 | return static_cast<ShaderD3D*>(impl); |
| 48 | } |
| 49 | |
| 50 | const ShaderD3D *ShaderD3D::makeShaderD3D(const ShaderImpl *impl) |
| 51 | { |
| 52 | ASSERT(HAS_DYNAMIC_TYPE(const ShaderD3D*, impl)); |
| 53 | return static_cast<const ShaderD3D*>(impl); |
| 54 | } |
| 55 | |
| 56 | // Perform a one-time initialization of the shader compiler (or after being destructed by releaseCompiler) |
| 57 | void ShaderD3D::initializeCompiler() |
| 58 | { |
| 59 | if (!mFragmentCompiler) |
| 60 | { |
| 61 | int result = ShInitialize(); |
| 62 | |
| 63 | if (result) |
| 64 | { |
| 65 | ShShaderOutput hlslVersion = (mRenderer->getMajorShaderModel() >= 4) ? SH_HLSL11_OUTPUT : SH_HLSL9_OUTPUT; |
| 66 | |
| 67 | ShBuiltInResources resources; |
| 68 | ShInitBuiltInResources(&resources); |
| 69 | |
| 70 | // TODO(geofflang): use context's caps |
| 71 | const gl::Caps &caps = mRenderer->getRendererCaps(); |
| 72 | const gl::Extensions &extensions = mRenderer->getRendererExtensions(); |
| 73 | |
| 74 | resources.MaxVertexAttribs = caps.maxVertexAttributes; |
| 75 | resources.MaxVertexUniformVectors = caps.maxVertexUniformVectors; |
| 76 | resources.MaxVaryingVectors = mRenderer->getMaxVaryingVectors(); |
| 77 | resources.MaxVertexTextureImageUnits = caps.maxVertexTextureImageUnits; |
| 78 | resources.MaxCombinedTextureImageUnits = mRenderer->getMaxCombinedTextureImageUnits(); |
| 79 | resources.MaxTextureImageUnits = caps.maxTextureImageUnits; |
| 80 | resources.MaxFragmentUniformVectors = caps.maxFragmentUniformVectors; |
| 81 | resources.MaxDrawBuffers = caps.maxDrawBuffers; |
| 82 | resources.OES_standard_derivatives = extensions.standardDerivatives; |
| 83 | resources.EXT_draw_buffers = extensions.drawBuffers; |
| 84 | resources.EXT_shader_texture_lod = 1; |
| 85 | // resources.OES_EGL_image_external = mRenderer->getShareHandleSupport() ? 1 : 0; // TODO: commented out until the extension is actually supported. |
| 86 | resources.FragmentPrecisionHigh = 1; // Shader Model 2+ always supports FP24 (s16e7) which corresponds to highp |
| 87 | resources.EXT_frag_depth = 1; // Shader Model 2+ always supports explicit depth output |
| 88 | // GLSL ES 3.0 constants |
| 89 | resources.MaxVertexOutputVectors = caps.maxVertexOutputComponents / 4; |
| 90 | resources.MaxFragmentInputVectors = caps.maxFragmentInputComponents / 4; |
| 91 | resources.MinProgramTexelOffset = caps.minProgramTexelOffset; |
| 92 | resources.MaxProgramTexelOffset = caps.maxProgramTexelOffset; |
| 93 | |
| 94 | mFragmentCompiler = ShConstructCompiler(GL_FRAGMENT_SHADER, SH_GLES2_SPEC, hlslVersion, &resources); |
| 95 | mVertexCompiler = ShConstructCompiler(GL_VERTEX_SHADER, SH_GLES2_SPEC, hlslVersion, &resources); |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | void ShaderD3D::releaseCompiler() |
| 101 | { |
| 102 | ShDestruct(mFragmentCompiler); |
| 103 | ShDestruct(mVertexCompiler); |
| 104 | |
| 105 | mFragmentCompiler = NULL; |
| 106 | mVertexCompiler = NULL; |
| 107 | |
| 108 | ShFinalize(); |
| 109 | } |
| 110 | |
| 111 | void ShaderD3D::parseVaryings(void *compiler) |
| 112 | { |
| 113 | if (!mHlsl.empty()) |
| 114 | { |
| 115 | const std::vector<sh::Varying> *activeVaryings = ShGetVaryings(compiler); |
| 116 | ASSERT(activeVaryings); |
| 117 | |
| 118 | for (size_t varyingIndex = 0; varyingIndex < activeVaryings->size(); varyingIndex++) |
| 119 | { |
| 120 | mVaryings.push_back(gl::PackedVarying((*activeVaryings)[varyingIndex])); |
| 121 | } |
| 122 | |
| 123 | mUsesMultipleRenderTargets = mHlsl.find("GL_USES_MRT") != std::string::npos; |
| 124 | mUsesFragColor = mHlsl.find("GL_USES_FRAG_COLOR") != std::string::npos; |
| 125 | mUsesFragData = mHlsl.find("GL_USES_FRAG_DATA") != std::string::npos; |
| 126 | mUsesFragCoord = mHlsl.find("GL_USES_FRAG_COORD") != std::string::npos; |
| 127 | mUsesFrontFacing = mHlsl.find("GL_USES_FRONT_FACING") != std::string::npos; |
| 128 | mUsesPointSize = mHlsl.find("GL_USES_POINT_SIZE") != std::string::npos; |
| 129 | mUsesPointCoord = mHlsl.find("GL_USES_POINT_COORD") != std::string::npos; |
| 130 | mUsesDepthRange = mHlsl.find("GL_USES_DEPTH_RANGE") != std::string::npos; |
| 131 | mUsesFragDepth = mHlsl.find("GL_USES_FRAG_DEPTH") != std::string::npos; |
| 132 | mUsesDiscardRewriting = mHlsl.find("ANGLE_USES_DISCARD_REWRITING") != std::string::npos; |
| 133 | mUsesNestedBreak = mHlsl.find("ANGLE_USES_NESTED_BREAK") != std::string::npos; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | void ShaderD3D::resetVaryingsRegisterAssignment() |
| 138 | { |
| 139 | for (unsigned int varyingIndex = 0; varyingIndex < mVaryings.size(); varyingIndex++) |
| 140 | { |
| 141 | mVaryings[varyingIndex].resetRegisterAssignment(); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | // initialize/clean up previous state |
| 146 | void ShaderD3D::uncompile() |
| 147 | { |
| 148 | // set by compileToHLSL |
| 149 | mHlsl.clear(); |
| 150 | mInfoLog.clear(); |
| 151 | |
| 152 | // set by parseVaryings |
| 153 | mVaryings.clear(); |
| 154 | |
| 155 | mUsesMultipleRenderTargets = false; |
| 156 | mUsesFragColor = false; |
| 157 | mUsesFragData = false; |
| 158 | mUsesFragCoord = false; |
| 159 | mUsesFrontFacing = false; |
| 160 | mUsesPointSize = false; |
| 161 | mUsesPointCoord = false; |
| 162 | mUsesDepthRange = false; |
| 163 | mUsesFragDepth = false; |
| 164 | mShaderVersion = 100; |
| 165 | mUsesDiscardRewriting = false; |
| 166 | mUsesNestedBreak = false; |
| 167 | |
| 168 | mActiveUniforms.clear(); |
| 169 | mActiveInterfaceBlocks.clear(); |
| 170 | } |
| 171 | |
| 172 | void ShaderD3D::compileToHLSL(void *compiler, const std::string &source) |
| 173 | { |
| 174 | // ensure the compiler is loaded |
| 175 | initializeCompiler(); |
| 176 | |
| 177 | int compileOptions = SH_OBJECT_CODE; |
| 178 | std::string sourcePath; |
| 179 | if (gl::perfActive()) |
| 180 | { |
| 181 | sourcePath = getTempPath(); |
| 182 | writeFile(sourcePath.c_str(), source.c_str(), source.length()); |
| 183 | compileOptions |= SH_LINE_DIRECTIVES; |
| 184 | } |
| 185 | |
| 186 | int result; |
| 187 | if (sourcePath.empty()) |
| 188 | { |
| 189 | const char* sourceStrings[] = |
| 190 | { |
| 191 | source.c_str(), |
| 192 | }; |
| 193 | |
| 194 | result = ShCompile(compiler, sourceStrings, ArraySize(sourceStrings), compileOptions); |
| 195 | } |
| 196 | else |
| 197 | { |
| 198 | const char* sourceStrings[] = |
| 199 | { |
| 200 | sourcePath.c_str(), |
| 201 | source.c_str(), |
| 202 | }; |
| 203 | |
| 204 | result = ShCompile(compiler, sourceStrings, ArraySize(sourceStrings), compileOptions | SH_SOURCE_PATH); |
| 205 | } |
| 206 | |
| 207 | size_t shaderVersion = 100; |
| 208 | ShGetInfo(compiler, SH_SHADER_VERSION, &shaderVersion); |
| 209 | |
| 210 | mShaderVersion = static_cast<int>(shaderVersion); |
| 211 | |
| 212 | if (shaderVersion == 300 && mRenderer->getCurrentClientVersion() < 3) |
| 213 | { |
| 214 | mInfoLog = "GLSL ES 3.00 is not supported by OpenGL ES 2.0 contexts"; |
| 215 | TRACE("\n%s", mInfoLog.c_str()); |
| 216 | } |
| 217 | else if (result) |
| 218 | { |
| 219 | size_t objCodeLen = 0; |
| 220 | ShGetInfo(compiler, SH_OBJECT_CODE_LENGTH, &objCodeLen); |
| 221 | |
| 222 | char* outputHLSL = new char[objCodeLen]; |
| 223 | ShGetObjectCode(compiler, outputHLSL); |
| 224 | |
| 225 | #ifdef _DEBUG |
| 226 | std::ostringstream hlslStream; |
| 227 | hlslStream << "// GLSL\n"; |
| 228 | hlslStream << "//\n"; |
| 229 | |
| 230 | size_t curPos = 0; |
| 231 | while (curPos != std::string::npos) |
| 232 | { |
| 233 | size_t nextLine = source.find("\n", curPos); |
| 234 | size_t len = (nextLine == std::string::npos) ? std::string::npos : (nextLine - curPos + 1); |
| 235 | |
| 236 | hlslStream << "// " << source.substr(curPos, len); |
| 237 | |
| 238 | curPos = (nextLine == std::string::npos) ? std::string::npos : (nextLine + 1); |
| 239 | } |
| 240 | hlslStream << "\n\n"; |
| 241 | hlslStream << outputHLSL; |
| 242 | mHlsl = hlslStream.str(); |
| 243 | #else |
| 244 | mHlsl = outputHLSL; |
| 245 | #endif |
| 246 | |
| 247 | SafeDeleteArray(outputHLSL); |
| 248 | |
| 249 | mActiveUniforms = *GetShaderVariables(ShGetUniforms(compiler)); |
| 250 | |
| 251 | for (size_t uniformIndex = 0; uniformIndex < mActiveUniforms.size(); uniformIndex++) |
| 252 | { |
| 253 | const sh::Uniform &uniform = mActiveUniforms[uniformIndex]; |
| 254 | |
| 255 | unsigned int index = -1; |
| 256 | bool result = ShGetUniformRegister(compiler, uniform.name.c_str(), &index); |
| 257 | UNUSED_ASSERTION_VARIABLE(result); |
| 258 | ASSERT(result); |
| 259 | |
| 260 | mUniformRegisterMap[uniform.name] = index; |
| 261 | } |
| 262 | |
| 263 | mActiveInterfaceBlocks = *GetShaderVariables(ShGetInterfaceBlocks(compiler)); |
| 264 | |
| 265 | for (size_t blockIndex = 0; blockIndex < mActiveInterfaceBlocks.size(); blockIndex++) |
| 266 | { |
| 267 | const sh::InterfaceBlock &interfaceBlock = mActiveInterfaceBlocks[blockIndex]; |
| 268 | |
| 269 | unsigned int index = -1; |
| 270 | bool result = ShGetInterfaceBlockRegister(compiler, interfaceBlock.name.c_str(), &index); |
| 271 | UNUSED_ASSERTION_VARIABLE(result); |
| 272 | ASSERT(result); |
| 273 | |
| 274 | mInterfaceBlockRegisterMap[interfaceBlock.name] = index; |
| 275 | } |
| 276 | } |
| 277 | else |
| 278 | { |
| 279 | size_t infoLogLen = 0; |
| 280 | ShGetInfo(compiler, SH_INFO_LOG_LENGTH, &infoLogLen); |
| 281 | |
| 282 | char* infoLog = new char[infoLogLen]; |
| 283 | ShGetInfoLog(compiler, infoLog); |
| 284 | mInfoLog = infoLog; |
| 285 | |
| 286 | TRACE("\n%s", mInfoLog.c_str()); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | rx::D3DWorkaroundType ShaderD3D::getD3DWorkarounds() const |
| 291 | { |
| 292 | if (mUsesDiscardRewriting) |
| 293 | { |
| 294 | // ANGLE issue 486: |
| 295 | // Work-around a D3D9 compiler bug that presents itself when using conditional discard, by disabling optimization |
| 296 | return rx::ANGLE_D3D_WORKAROUND_SKIP_OPTIMIZATION; |
| 297 | } |
| 298 | |
| 299 | if (mUsesNestedBreak) |
| 300 | { |
| 301 | // ANGLE issue 603: |
| 302 | // Work-around a D3D9 compiler bug that presents itself when using break in a nested loop, by maximizing optimization |
| 303 | // We want to keep the use of ANGLE_D3D_WORKAROUND_MAX_OPTIMIZATION minimal to prevent hangs, so usesDiscard takes precedence |
| 304 | return rx::ANGLE_D3D_WORKAROUND_MAX_OPTIMIZATION; |
| 305 | } |
| 306 | |
| 307 | return rx::ANGLE_D3D_WORKAROUND_NONE; |
| 308 | } |
| 309 | |
| 310 | // true if varying x has a higher priority in packing than y |
| 311 | bool ShaderD3D::compareVarying(const gl::PackedVarying &x, const gl::PackedVarying &y) |
| 312 | { |
| 313 | if (x.type == y.type) |
| 314 | { |
| 315 | return x.arraySize > y.arraySize; |
| 316 | } |
| 317 | |
| 318 | // Special case for handling structs: we sort these to the end of the list |
| 319 | if (x.type == GL_STRUCT_ANGLEX) |
| 320 | { |
| 321 | return false; |
| 322 | } |
| 323 | |
| 324 | if (y.type == GL_STRUCT_ANGLEX) |
| 325 | { |
| 326 | return true; |
| 327 | } |
| 328 | |
| 329 | return gl::VariableSortOrder(x.type) <= gl::VariableSortOrder(y.type); |
| 330 | } |
| 331 | |
| 332 | unsigned int ShaderD3D::getUniformRegister(const std::string &uniformName) const |
| 333 | { |
| 334 | ASSERT(mUniformRegisterMap.count(uniformName) > 0); |
| 335 | return mUniformRegisterMap.find(uniformName)->second; |
| 336 | } |
| 337 | |
| 338 | unsigned int ShaderD3D::getInterfaceBlockRegister(const std::string &blockName) const |
| 339 | { |
| 340 | ASSERT(mInterfaceBlockRegisterMap.count(blockName) > 0); |
| 341 | return mInterfaceBlockRegisterMap.find(blockName)->second; |
| 342 | } |
| 343 | |
| 344 | ShShaderOutput ShaderD3D::getCompilerOutputType(GLenum shader) |
| 345 | { |
| 346 | void *compiler = NULL; |
| 347 | |
| 348 | switch (shader) |
| 349 | { |
| 350 | case GL_VERTEX_SHADER: compiler = mVertexCompiler; break; |
| 351 | case GL_FRAGMENT_SHADER: compiler = mFragmentCompiler; break; |
| 352 | default: UNREACHABLE(); return SH_HLSL9_OUTPUT; |
| 353 | } |
| 354 | |
| 355 | size_t outputType = 0; |
| 356 | ShGetInfo(compiler, SH_OUTPUT_TYPE, &outputType); |
| 357 | |
| 358 | return static_cast<ShShaderOutput>(outputType); |
| 359 | } |
| 360 | |
| 361 | VertexShaderD3D::VertexShaderD3D(rx::Renderer *renderer) : ShaderD3D(renderer) |
| 362 | { |
| 363 | } |
| 364 | |
| 365 | VertexShaderD3D::~VertexShaderD3D() |
| 366 | { |
| 367 | } |
| 368 | |
| 369 | VertexShaderD3D *VertexShaderD3D::makeVertexShaderD3D(ShaderImpl *impl) |
| 370 | { |
| 371 | ASSERT(HAS_DYNAMIC_TYPE(VertexShaderD3D*, impl)); |
| 372 | return static_cast<VertexShaderD3D*>(impl); |
| 373 | } |
| 374 | |
| 375 | const VertexShaderD3D *VertexShaderD3D::makeVertexShaderD3D(const ShaderImpl *impl) |
| 376 | { |
| 377 | ASSERT(HAS_DYNAMIC_TYPE(const VertexShaderD3D*, impl)); |
| 378 | return static_cast<const VertexShaderD3D*>(impl); |
| 379 | } |
| 380 | |
| 381 | bool VertexShaderD3D::compile(const std::string &source) |
| 382 | { |
| 383 | uncompile(); |
| 384 | |
| 385 | compileToHLSL(mVertexCompiler, source); |
| 386 | parseAttributes(); |
| 387 | parseVaryings(mVertexCompiler); |
| 388 | |
| 389 | return !getTranslatedSource().empty(); |
| 390 | } |
| 391 | |
| 392 | void VertexShaderD3D::uncompile() |
| 393 | { |
| 394 | ShaderD3D::uncompile(); |
| 395 | |
| 396 | // set by ParseAttributes |
| 397 | mActiveAttributes.clear(); |
| 398 | } |
| 399 | |
| 400 | void VertexShaderD3D::parseAttributes() |
| 401 | { |
| 402 | const std::string &hlsl = getTranslatedSource(); |
| 403 | if (!hlsl.empty()) |
| 404 | { |
| 405 | mActiveAttributes = *GetShaderVariables(ShGetAttributes(mVertexCompiler)); |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | int VertexShaderD3D::getSemanticIndex(const std::string &attributeName) |
| 410 | { |
| 411 | if (!attributeName.empty()) |
| 412 | { |
| 413 | int semanticIndex = 0; |
| 414 | for (unsigned int attributeIndex = 0; attributeIndex < mActiveAttributes.size(); attributeIndex++) |
| 415 | { |
| 416 | const sh::ShaderVariable &attribute = mActiveAttributes[attributeIndex]; |
| 417 | |
| 418 | if (attribute.name == attributeName) |
| 419 | { |
| 420 | return semanticIndex; |
| 421 | } |
| 422 | |
| 423 | semanticIndex += gl::VariableRegisterCount(attribute.type); |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | return -1; |
| 428 | } |
| 429 | |
| 430 | FragmentShaderD3D::FragmentShaderD3D(rx::Renderer *renderer) : ShaderD3D(renderer) |
| 431 | { |
| 432 | } |
| 433 | |
| 434 | FragmentShaderD3D::~FragmentShaderD3D() |
| 435 | { |
| 436 | } |
| 437 | |
| 438 | FragmentShaderD3D *FragmentShaderD3D::makeFragmentShaderD3D(ShaderImpl *impl) |
| 439 | { |
| 440 | ASSERT(HAS_DYNAMIC_TYPE(FragmentShaderD3D*, impl)); |
| 441 | return static_cast<FragmentShaderD3D*>(impl); |
| 442 | } |
| 443 | |
| 444 | const FragmentShaderD3D *FragmentShaderD3D::makeFragmentShaderD3D(const ShaderImpl *impl) |
| 445 | { |
| 446 | ASSERT(HAS_DYNAMIC_TYPE(const FragmentShaderD3D*, impl)); |
| 447 | return static_cast<const FragmentShaderD3D*>(impl); |
| 448 | } |
| 449 | |
| 450 | bool FragmentShaderD3D::compile(const std::string &source) |
| 451 | { |
| 452 | uncompile(); |
| 453 | |
| 454 | compileToHLSL(mFragmentCompiler, source); |
| 455 | parseVaryings(mFragmentCompiler); |
| 456 | std::sort(mVaryings.begin(), mVaryings.end(), compareVarying); |
| 457 | |
| 458 | const std::string &hlsl = getTranslatedSource(); |
| 459 | if (!hlsl.empty()) |
| 460 | { |
| 461 | mActiveOutputVariables = *GetShaderVariables(ShGetOutputVariables(mFragmentCompiler)); |
| 462 | return true; |
| 463 | } |
| 464 | return false; |
| 465 | } |
| 466 | |
| 467 | void FragmentShaderD3D::uncompile() |
| 468 | { |
| 469 | ShaderD3D::uncompile(); |
| 470 | |
| 471 | mActiveOutputVariables.clear(); |
| 472 | } |
| 473 | |
| 474 | } |