1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef GPU_COMMAND_BUFFER_SERVICE_SHADER_TRANSLATOR_CACHE_H_
6 #define GPU_COMMAND_BUFFER_SERVICE_SHADER_TRANSLATOR_CACHE_H_
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/singleton.h"
14 #include "gpu/command_buffer/service/shader_translator.h"
15 #include "third_party/angle_dx11/include/GLSLANG/ShaderLang.h"
20 // This singleton and the cache that it implements is NOT thread safe.
21 // We're relying on the fact that the all GLES2DecoderImpl's are used
24 // TODO(backer): Investigate using glReleaseShaderCompiler as an alternative to
26 class ShaderTranslatorCache : public ShaderTranslator::DestructionObserver {
28 static ShaderTranslatorCache* GetInstance();
30 // ShaderTranslator::DestructionObserver implementation
31 virtual void OnDestruct(ShaderTranslator* translator) OVERRIDE;
33 scoped_refptr<ShaderTranslator> GetTranslator(
34 ShShaderType shader_type,
35 ShShaderSpec shader_spec,
36 const ShBuiltInResources* resources,
37 ShaderTranslatorInterface::GlslImplementationType
38 glsl_implementation_type,
39 ShCompileOptions driver_bug_workarounds);
42 ShaderTranslatorCache();
43 virtual ~ShaderTranslatorCache();
45 friend struct DefaultSingletonTraits<ShaderTranslatorCache>;
47 // Parameters passed into ShaderTranslator::Init
48 struct ShaderTranslatorInitParams {
49 ShShaderType shader_type;
50 ShShaderSpec shader_spec;
51 ShBuiltInResources resources;
52 ShaderTranslatorInterface::GlslImplementationType
53 glsl_implementation_type;
54 ShCompileOptions driver_bug_workarounds;
56 ShaderTranslatorInitParams(
57 ShShaderType shader_type,
58 ShShaderSpec shader_spec,
59 const ShBuiltInResources& resources,
60 ShaderTranslatorInterface::GlslImplementationType
61 glsl_implementation_type,
62 ShCompileOptions driver_bug_workarounds)
63 : shader_type(shader_type),
64 shader_spec(shader_spec),
66 glsl_implementation_type(glsl_implementation_type),
67 driver_bug_workarounds(driver_bug_workarounds) {
70 ShaderTranslatorInitParams(const ShaderTranslatorInitParams& params) {
71 memcpy(this, ¶ms, sizeof(*this));
74 bool operator== (const ShaderTranslatorInitParams& params) const {
75 return memcmp(¶ms, this, sizeof(*this)) == 0;
78 bool operator< (const ShaderTranslatorInitParams& params) const {
79 return memcmp(¶ms, this, sizeof(*this)) < 0;
83 typedef std::map<ShaderTranslatorInitParams, ShaderTranslator* > Cache;
86 DISALLOW_COPY_AND_ASSIGN(ShaderTranslatorCache);
92 #endif // GPU_COMMAND_BUFFER_SERVICE_SHADER_TRANSLATOR_CACHE_H_