blob: 3fb5687e8b1d07e7a8212dc91f242f03b529b667 [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// 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)7d4cd472013-06-19 11:58:07 +010011#include "base/containers/hash_tables.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000012#include "base/sha1.h"
13#include "gpu/command_buffer/common/gles2_cmd_format.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000014#include "gpu/command_buffer/service/gles2_cmd_decoder.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000015#include "gpu/command_buffer/service/shader_manager.h"
16
17namespace gpu {
18namespace gles2 {
19
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000020class Shader;
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010021class ShaderTranslator;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000022
Torne (Richard Coles)58218062012-11-14 11:43:16 +000023// Program cache base class for caching linked gpu programs
24class 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)58218062012-11-14 11:43:16 +000030 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)58218062012-11-14 11:43:16 +000043 LinkedProgramStatus GetLinkedProgramStatus(
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010044 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)58218062012-11-14 11:43:16 +000048 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)2a99a7e2013-03-28 15:31:22 +000054 Shader* shader_a,
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010055 const ShaderTranslatorInterface* translator_a,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000056 Shader* shader_b,
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010057 const ShaderTranslatorInterface* translator_b,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010058 const LocationMap* bind_attrib_location_map,
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +010059 const ShaderCacheCallback& shader_callback) = 0;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000060
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)2a99a7e2013-03-28 15:31:22 +000065 const Shader* shader_a,
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010066 const ShaderTranslatorInterface* translator_a,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000067 const Shader* shader_b,
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010068 const ShaderTranslatorInterface* translator_b,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000069 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)58218062012-11-14 11:43:16 +000073
74 // clears the cache
75 void Clear();
76
77 // Only for testing
78 void LinkedProgramCacheSuccess(const std::string& shader_a,
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010079 const ShaderTranslatorInterface* translator_a,
Torne (Richard Coles)58218062012-11-14 11:43:16 +000080 const std::string& shader_b,
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010081 const ShaderTranslatorInterface* translator_b,
Torne (Richard Coles)58218062012-11-14 11:43:16 +000082 const LocationMap* bind_attrib_location_map);
83
84 protected:
85 // called by implementing class after a shader was successfully cached
Ben Murdochbb1529c2013-08-08 10:24:53 +010086 void LinkedProgramCacheSuccess(const std::string& program_hash);
Torne (Richard Coles)58218062012-11-14 11:43:16 +000087
88 // result is not null terminated
89 void ComputeShaderHash(const std::string& shader,
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010090 const ShaderTranslatorInterface* translator,
Torne (Richard Coles)58218062012-11-14 11:43:16 +000091 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 Murdochbb1529c2013-08-08 10:24:53 +0100101 void Evict(const std::string& program_hash);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000102
103 private:
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000104 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)58218062012-11-14 11:43:16 +0000110 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_