xref: /aosp_15_r20/external/angle/src/libANGLE/renderer/metal/ShaderMtl.mm (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
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->simplifyLoopConditions = true;
66
67    options->initializeUninitializedLocals = true;
68
69    options->separateCompoundStructDeclarations = true;
70
71    if (context->isWebGL() && mState.getShaderType() != gl::ShaderType::Compute)
72    {
73        options->initOutputVariables = true;
74    }
75
76    options->metal.generateShareableShaders =
77        displayMtl->getFeatures().generateShareableShaders.enabled;
78
79    if (displayMtl->getFeatures().intelExplicitBoolCastWorkaround.enabled ||
80        options->metal.generateShareableShaders)
81    {
82        options->addExplicitBoolCasts = true;
83    }
84
85    options->clampPointSize = true;
86#if TARGET_OS_IPHONE && !TARGET_OS_MACCATALYST
87    options->clampFragDepth = true;
88#endif
89
90    if (displayMtl->getFeatures().emulateAlphaToCoverage.enabled)
91    {
92        options->emulateAlphaToCoverage = true;
93    }
94
95    // Constants:
96    options->metal.driverUniformsBindingIndex    = mtl::kDriverUniformsBindingIndex;
97    options->metal.defaultUniformsBindingIndex   = mtl::kDefaultUniformsBindingIndex;
98    options->metal.UBOArgumentBufferBindingIndex = mtl::kUBOArgumentBufferBindingIndex;
99
100    // GL_ANGLE_shader_pixel_local_storage.
101    if (displayMtl->getNativeExtensions().shaderPixelLocalStorageANGLE)
102    {
103        options->pls = displayMtl->getNativePixelLocalStorageOptions();
104    }
105
106    options->preTransformTextureCubeGradDerivatives =
107        displayMtl->getFeatures().preTransformTextureCubeGradDerivatives.enabled;
108
109    options->rescopeGlobalVariables = displayMtl->getFeatures().rescopeGlobalVariables.enabled;
110
111    if (displayMtl->getFeatures().injectAsmStatementIntoLoopBodies.enabled)
112    {
113        options->metal.injectAsmStatementIntoLoopBodies = true;
114    }
115
116    return std::shared_ptr<ShaderTranslateTask>(new ShaderTranslateTaskMtl(mCompiledState));
117}
118
119std::shared_ptr<ShaderTranslateTask> ShaderMtl::load(const gl::Context *context,
120                                                     gl::BinaryInputStream *stream)
121{
122    UNREACHABLE();
123    return std::shared_ptr<ShaderTranslateTask>(new ShaderTranslateTask);
124}
125
126std::string ShaderMtl::getDebugInfo() const
127{
128    return mState.getCompiledState()->translatedSource;
129}
130
131}  // namespace rx
132