Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 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. |
| 4 | |
| 5 | #ifndef GPU_COMMAND_BUFFER_SERVICE_PROGRAM_CACHE_H_ |
| 6 | #define GPU_COMMAND_BUFFER_SERVICE_PROGRAM_CACHE_H_ |
| 7 | |
| 8 | #include <map> |
| 9 | #include <string> |
| 10 | |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 11 | #include "base/containers/hash_tables.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 12 | #include "base/sha1.h" |
| 13 | #include "gpu/command_buffer/common/gles2_cmd_format.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 14 | #include "gpu/command_buffer/service/gles2_cmd_decoder.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 15 | #include "gpu/command_buffer/service/shader_manager.h" |
| 16 | |
| 17 | namespace gpu { |
| 18 | namespace gles2 { |
| 19 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 20 | class Shader; |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 21 | class ShaderTranslator; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 22 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 23 | // Program cache base class for caching linked gpu programs |
| 24 | class GPU_EXPORT ProgramCache { |
| 25 | public: |
| 26 | static const size_t kHashLength = base::kSHA1Length; |
| 27 | |
| 28 | typedef std::map<std::string, GLint> LocationMap; |
| 29 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 30 | enum LinkedProgramStatus { |
| 31 | LINK_UNKNOWN, |
| 32 | LINK_SUCCEEDED |
| 33 | }; |
| 34 | |
| 35 | enum ProgramLoadResult { |
| 36 | PROGRAM_LOAD_FAILURE, |
| 37 | PROGRAM_LOAD_SUCCESS |
| 38 | }; |
| 39 | |
| 40 | ProgramCache(); |
| 41 | virtual ~ProgramCache(); |
| 42 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 43 | LinkedProgramStatus GetLinkedProgramStatus( |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 44 | const std::string& untranslated_shader_a, |
| 45 | const ShaderTranslatorInterface* translator_a, |
| 46 | const std::string& untranslated_shader_b, |
| 47 | const ShaderTranslatorInterface* translator_b, |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 48 | const LocationMap* bind_attrib_location_map) const; |
| 49 | |
| 50 | // Loads the linked program from the cache. If the program is not found or |
| 51 | // there was an error, PROGRAM_LOAD_FAILURE should be returned. |
| 52 | virtual ProgramLoadResult LoadLinkedProgram( |
| 53 | GLuint program, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 54 | Shader* shader_a, |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 55 | const ShaderTranslatorInterface* translator_a, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 56 | Shader* shader_b, |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 57 | const ShaderTranslatorInterface* translator_b, |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 58 | const LocationMap* bind_attrib_location_map, |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 59 | const ShaderCacheCallback& shader_callback) = 0; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 60 | |
| 61 | // Saves the program into the cache. If successful, the implementation should |
| 62 | // call LinkedProgramCacheSuccess. |
| 63 | virtual void SaveLinkedProgram( |
| 64 | GLuint program, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 65 | const Shader* shader_a, |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 66 | const ShaderTranslatorInterface* translator_a, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 67 | const Shader* shader_b, |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 68 | const ShaderTranslatorInterface* translator_b, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 69 | const LocationMap* bind_attrib_location_map, |
| 70 | const ShaderCacheCallback& shader_callback) = 0; |
| 71 | |
| 72 | virtual void LoadProgram(const std::string& program) = 0; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 73 | |
| 74 | // clears the cache |
| 75 | void Clear(); |
| 76 | |
| 77 | // Only for testing |
| 78 | void LinkedProgramCacheSuccess(const std::string& shader_a, |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 79 | const ShaderTranslatorInterface* translator_a, |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 80 | const std::string& shader_b, |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 81 | const ShaderTranslatorInterface* translator_b, |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 82 | const LocationMap* bind_attrib_location_map); |
| 83 | |
| 84 | protected: |
| 85 | // called by implementing class after a shader was successfully cached |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 86 | void LinkedProgramCacheSuccess(const std::string& program_hash); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 87 | |
| 88 | // result is not null terminated |
| 89 | void ComputeShaderHash(const std::string& shader, |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 90 | const ShaderTranslatorInterface* translator, |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 91 | char* result) const; |
| 92 | |
| 93 | // result is not null terminated. hashed shaders are expected to be |
| 94 | // kHashLength in length |
| 95 | void ComputeProgramHash( |
| 96 | const char* hashed_shader_0, |
| 97 | const char* hashed_shader_1, |
| 98 | const LocationMap* bind_attrib_location_map, |
| 99 | char* result) const; |
| 100 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 101 | void Evict(const std::string& program_hash); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 102 | |
| 103 | private: |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 104 | typedef base::hash_map<std::string, |
| 105 | LinkedProgramStatus> LinkStatusMap; |
| 106 | |
| 107 | // called to clear the backend cache |
| 108 | virtual void ClearBackend() = 0; |
| 109 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 110 | LinkStatusMap link_status_; |
| 111 | |
| 112 | DISALLOW_COPY_AND_ASSIGN(ProgramCache); |
| 113 | }; |
| 114 | |
| 115 | } // namespace gles2 |
| 116 | } // namespace gpu |
| 117 | |
| 118 | #endif // GPU_COMMAND_BUFFER_SERVICE_PROGRAM_CACHE_H_ |