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