1// 2// Copyright 2019 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// ShaderMtl.mm: 7// Implements the class methods for ShaderMtl. 8// 9 10#include "libANGLE/renderer/metal/ShaderMtl.h" 11 12#include "common/WorkerThread.h" 13#include "common/debug.h" 14#include "libANGLE/Context.h" 15#include "libANGLE/Shader.h" 16#include "libANGLE/renderer/metal/ContextMtl.h" 17#include "libANGLE/renderer/metal/DisplayMtl.h" 18#include "libANGLE/trace.h" 19 20namespace rx 21{ 22namespace 23{ 24class ShaderTranslateTaskMtl final : public ShaderTranslateTask 25{ 26 public: 27 ShaderTranslateTaskMtl(const SharedCompiledShaderStateMtl &shader) : mShader(shader) {} 28 ~ShaderTranslateTaskMtl() override = default; 29 30 void postTranslate(ShHandle compiler, const gl::CompiledShaderState &compiledState) override 31 { 32 sh::TranslatorMSL *translatorMSL = 33 static_cast<sh::TShHandleBase *>(compiler)->getAsTranslatorMSL(); 34 if (translatorMSL != nullptr) 35 { 36 // Copy reflection data from translation 37 mShader->translatorMetalReflection = *translatorMSL->getTranslatorMetalReflection(); 38 translatorMSL->getTranslatorMetalReflection()->reset(); 39 } 40 } 41 42 private: 43 SharedCompiledShaderStateMtl mShader; 44}; 45} // anonymous namespace 46 47ShaderMtl::ShaderMtl(const gl::ShaderState &state) : ShaderImpl(state) {} 48 49ShaderMtl::~ShaderMtl() {} 50 51std::shared_ptr<ShaderTranslateTask> ShaderMtl::compile(const gl::Context *context, 52 ShCompileOptions *options) 53{ 54 ContextMtl *contextMtl = mtl::GetImpl(context); 55 DisplayMtl *displayMtl = contextMtl->getDisplay(); 56 57 // Create a new compiled shader state. Currently running program link jobs will use the 58 // previous state. 59 mCompiledState = std::make_shared<CompiledShaderStateMtl>(); 60 61 // TODO(jcunningham): Remove this workaround once correct fix to move validation to the very end 62 // is in place. https://bugs.webkit.org/show_bug.cgi?id=224991 63 options->validateAST = false; 64 65 options->initializeUninitializedLocals = true; 66 67 if (context->isWebGL() && mState.getShaderType() != gl::ShaderType::Compute) 68 { 69 options->initOutputVariables = true; 70 } 71 72 options->metal.generateShareableShaders = 73 displayMtl->getFeatures().generateShareableShaders.enabled; 74 75 if (displayMtl->getFeatures().intelExplicitBoolCastWorkaround.enabled || 76 options->metal.generateShareableShaders) 77 { 78 options->addExplicitBoolCasts = true; 79 } 80 81 options->clampPointSize = true; 82#if ANGLE_PLATFORM_IOS_FAMILY && !ANGLE_PLATFORM_MACCATALYST 83 options->clampFragDepth = true; 84#endif 85 86 if (displayMtl->getFeatures().emulateAlphaToCoverage.enabled) 87 { 88 options->emulateAlphaToCoverage = true; 89 } 90 91 // Constants: 92 options->metal.driverUniformsBindingIndex = mtl::kDriverUniformsBindingIndex; 93 options->metal.defaultUniformsBindingIndex = mtl::kDefaultUniformsBindingIndex; 94 options->metal.UBOArgumentBufferBindingIndex = mtl::kUBOArgumentBufferBindingIndex; 95 96 // GL_ANGLE_shader_pixel_local_storage. 97 if (displayMtl->getNativeExtensions().shaderPixelLocalStorageANGLE) 98 { 99 options->pls = displayMtl->getNativePixelLocalStorageOptions(); 100 } 101 102 options->preTransformTextureCubeGradDerivatives = 103 displayMtl->getFeatures().preTransformTextureCubeGradDerivatives.enabled; 104 105 options->rescopeGlobalVariables = displayMtl->getFeatures().rescopeGlobalVariables.enabled; 106 107 if (displayMtl->getFeatures().injectAsmStatementIntoLoopBodies.enabled) 108 { 109 options->metal.injectAsmStatementIntoLoopBodies = true; 110 } 111 112 return std::shared_ptr<ShaderTranslateTask>(new ShaderTranslateTaskMtl(mCompiledState)); 113} 114 115std::string ShaderMtl::getDebugInfo() const 116{ 117 return mState.getCompiledState()->translatedSource; 118} 119 120} // namespace rx 121