1 // 2 // Copyright 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 // ShaderImpl.h: Defines the abstract rx::ShaderImpl class. 8 9 #ifndef LIBANGLE_RENDERER_SHADERIMPL_H_ 10 #define LIBANGLE_RENDERER_SHADERIMPL_H_ 11 12 #include <functional> 13 14 #include "common/CompiledShaderState.h" 15 #include "common/WorkerThread.h" 16 #include "common/angleutils.h" 17 #include "libANGLE/Shader.h" 18 19 namespace gl 20 { 21 class ShCompilerInstance; 22 } // namespace gl 23 24 namespace rx 25 { 26 // The compile task is generally just a call to the translator. However, different backends behave 27 // differently afterwards: 28 // 29 // - The Vulkan backend which generates binary (i.e. SPIR-V), does nothing more 30 // - The backends that generate text (HLSL, MSL, WGSL), do nothing at this stage, but modify the 31 // text at link time before invoking the native compiler. These expensive calls are handled in 32 // link sub-tasks (see LinkSubTask in ProgramImpl.h). 33 // - The GL backend needs to invoke the native driver, which is problematic when done in another 34 // thread (and is avoided). 35 // 36 // The call to the translator can thus be done in a separate thread or without holding the share 37 // group lock on all backends except GL. On the GL backend, the translator call is done on the main 38 // thread followed by a call to the native driver. If the driver supports 39 // GL_KHR_parallel_shader_compile, ANGLE still delays post-processing of the results to when 40 // compilation is done (just as if it was ANGLE itself that was doing the compilation in a thread). 41 class ShaderTranslateTask 42 { 43 public: 44 virtual ~ShaderTranslateTask() = default; 45 46 // Used for compile() 47 virtual bool translate(ShHandle compiler, 48 const ShCompileOptions &options, 49 const std::string &source); postTranslate(ShHandle compiler,const gl::CompiledShaderState & compiledState)50 virtual void postTranslate(ShHandle compiler, const gl::CompiledShaderState &compiledState) {} 51 52 // Used for load() load(const gl::CompiledShaderState & compiledState)53 virtual void load(const gl::CompiledShaderState &compiledState) {} 54 55 // Used by the GL backend to query whether the driver is compiling in parallel internally. isCompilingInternally()56 virtual bool isCompilingInternally() { return false; } 57 // Used by the GL backend to finish internal compilation and return results. getResult(std::string & infoLog)58 virtual angle::Result getResult(std::string &infoLog) { return angle::Result::Continue; } 59 }; 60 61 class ShaderImpl : angle::NonCopyable 62 { 63 public: ShaderImpl(const gl::ShaderState & state)64 ShaderImpl(const gl::ShaderState &state) : mState(state) {} ~ShaderImpl()65 virtual ~ShaderImpl() {} 66 onDestroy(const gl::Context * context)67 virtual void onDestroy(const gl::Context *context) {} 68 69 virtual std::shared_ptr<ShaderTranslateTask> compile(const gl::Context *context, 70 ShCompileOptions *options) = 0; 71 virtual std::shared_ptr<ShaderTranslateTask> load(const gl::Context *context, 72 gl::BinaryInputStream *stream) = 0; 73 74 virtual std::string getDebugInfo() const = 0; 75 getState()76 const gl::ShaderState &getState() const { return mState; } 77 78 virtual angle::Result onLabelUpdate(const gl::Context *context); 79 80 protected: 81 const gl::ShaderState &mState; 82 }; 83 84 } // namespace rx 85 86 #endif // LIBANGLE_RENDERER_SHADERIMPL_H_ 87