• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
7 // ShaderImpl.cpp: Implementation methods of ShaderImpl
8 
9 #include "libANGLE/renderer/ShaderImpl.h"
10 
11 #include "libANGLE/Context.h"
12 #include "libANGLE/trace.h"
13 
14 namespace rx
15 {
16 
WaitableCompileEvent(std::shared_ptr<angle::WaitableEvent> waitableEvent)17 WaitableCompileEvent::WaitableCompileEvent(std::shared_ptr<angle::WaitableEvent> waitableEvent)
18     : mWaitableEvent(waitableEvent)
19 {}
20 
~WaitableCompileEvent()21 WaitableCompileEvent::~WaitableCompileEvent()
22 {
23     mWaitableEvent.reset();
24 }
25 
wait()26 void WaitableCompileEvent::wait()
27 {
28     mWaitableEvent->wait();
29 }
30 
isReady()31 bool WaitableCompileEvent::isReady()
32 {
33     return mWaitableEvent->isReady();
34 }
35 
getInfoLog()36 const std::string &WaitableCompileEvent::getInfoLog()
37 {
38     return mInfoLog;
39 }
40 
41 class TranslateTask : public angle::Closure
42 {
43   public:
TranslateTask(ShHandle handle,ShCompileOptions options,const std::string & source)44     TranslateTask(ShHandle handle, ShCompileOptions options, const std::string &source)
45         : mHandle(handle), mOptions(options), mSource(source), mResult(false)
46     {}
47 
operator ()()48     void operator()() override
49     {
50         ANGLE_TRACE_EVENT1("gpu.angle", "TranslateTask::run", "source", mSource);
51         const char *source = mSource.c_str();
52         mResult            = sh::Compile(mHandle, &source, 1, mOptions);
53     }
54 
getResult()55     bool getResult() { return mResult; }
56 
getHandle()57     ShHandle getHandle() { return mHandle; }
58 
59   private:
60     ShHandle mHandle;
61     ShCompileOptions mOptions;
62     std::string mSource;
63     bool mResult;
64 };
65 
66 class WaitableCompileEventImpl final : public WaitableCompileEvent
67 {
68   public:
WaitableCompileEventImpl(std::shared_ptr<angle::WaitableEvent> waitableEvent,std::shared_ptr<TranslateTask> translateTask)69     WaitableCompileEventImpl(std::shared_ptr<angle::WaitableEvent> waitableEvent,
70                              std::shared_ptr<TranslateTask> translateTask)
71         : WaitableCompileEvent(waitableEvent), mTranslateTask(translateTask)
72     {}
73 
getResult()74     bool getResult() override { return mTranslateTask->getResult(); }
75 
postTranslate(std::string * infoLog)76     bool postTranslate(std::string *infoLog) override { return true; }
77 
78   private:
79     std::shared_ptr<TranslateTask> mTranslateTask;
80 };
81 
compileImpl(const gl::Context * context,gl::ShCompilerInstance * compilerInstance,const std::string & source,ShCompileOptions compileOptions)82 std::shared_ptr<WaitableCompileEvent> ShaderImpl::compileImpl(
83     const gl::Context *context,
84     gl::ShCompilerInstance *compilerInstance,
85     const std::string &source,
86     ShCompileOptions compileOptions)
87 {
88 #if defined(ANGLE_ENABLE_ASSERTS)
89     compileOptions |= SH_VALIDATE_AST;
90 #endif
91 
92     auto workerThreadPool = context->getWorkerThreadPool();
93     auto translateTask =
94         std::make_shared<TranslateTask>(compilerInstance->getHandle(), compileOptions, source);
95 
96     return std::make_shared<WaitableCompileEventImpl>(
97         angle::WorkerThreadPool::PostWorkerTask(workerThreadPool, translateTask), translateTask);
98 }
99 
100 }  // namespace rx
101