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