blob: bc68868bf2746a3763ee0fb151f8f6083c6696c6 [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_SHADER_MANAGER_H_
6#define GPU_COMMAND_BUFFER_SERVICE_SHADER_MANAGER_H_
7
8#include <string>
9#include "base/basictypes.h"
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010010#include "base/containers/hash_tables.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000011#include "base/logging.h"
12#include "base/memory/ref_counted.h"
13#include "base/memory/scoped_ptr.h"
14#include "gpu/command_buffer/service/gl_utils.h"
15#include "gpu/command_buffer/service/shader_translator.h"
16#include "gpu/gpu_export.h"
17
18namespace gpu {
19namespace gles2 {
20
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000021// This is used to keep the source code for a shader. This is because in order
22// to emluate GLES2 the shaders will have to be re-written before passed to
23// the underlying OpenGL. But, when the user calls glGetShaderSource they
24// should get the source they passed in, not the re-written source.
25class GPU_EXPORT Shader : public base::RefCounted<Shader> {
26 public:
27 typedef ShaderTranslator::VariableInfo VariableInfo;
28
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000029 void UpdateSource(const char* source) {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000030 source_.reset(source ? new std::string(source) : NULL);
31 }
32
33 void UpdateTranslatedSource(const char* translated_source) {
34 translated_source_.reset(
35 translated_source ? new std::string(translated_source) : NULL);
36 }
37
38 GLuint service_id() const {
39 return service_id_;
40 }
41
42 GLenum shader_type() const {
43 return shader_type_;
44 }
45
46 const std::string* source() const {
47 return source_.get();
48 }
49
50 const std::string* translated_source() const {
51 return translated_source_.get();
52 }
53
Ben Murdochbb1529c2013-08-08 10:24:53 +010054 const std::string* signature_source() const {
55 return signature_source_.get();
56 }
57
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000058 void SetStatus(
59 bool valid, const char* log,
60 ShaderTranslatorInterface* translator);
61
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000062 const VariableInfo* GetAttribInfo(const std::string& name) const;
63 const VariableInfo* GetUniformInfo(const std::string& name) const;
64
65 // If the original_name is not found, return NULL.
66 const std::string* GetAttribMappedName(
67 const std::string& original_name) const;
68
69 // If the hashed_name is not found, return NULL.
70 const std::string* GetOriginalNameFromHashedName(
71 const std::string& hashed_name) const;
72
73 const std::string* log_info() const {
74 return log_info_.get();
75 }
76
77 bool IsValid() const {
78 return valid_;
79 }
80
81 bool IsDeleted() const {
82 return service_id_ == 0;
83 }
84
85 bool InUse() const {
86 DCHECK_GE(use_count_, 0);
87 return use_count_ != 0;
88 }
89
90 // Used by program cache.
91 const ShaderTranslator::VariableMap& attrib_map() const {
92 return attrib_map_;
93 }
94
95 // Used by program cache.
96 const ShaderTranslator::VariableMap& uniform_map() const {
97 return uniform_map_;
98 }
99
100 // Used by program cache.
101 void set_attrib_map(const ShaderTranslator::VariableMap& attrib_map) {
102 // copied because cache might be cleared
103 attrib_map_ = ShaderTranslator::VariableMap(attrib_map);
104 }
105
106 // Used by program cache.
107 void set_uniform_map(const ShaderTranslator::VariableMap& uniform_map) {
108 // copied because cache might be cleared
109 uniform_map_ = ShaderTranslator::VariableMap(uniform_map);
110 }
111
112 private:
113 typedef ShaderTranslator::VariableMap VariableMap;
114 typedef ShaderTranslator::NameMap NameMap;
115
116 friend class base::RefCounted<Shader>;
117 friend class ShaderManager;
118
119 Shader(GLuint service_id, GLenum shader_type);
120 ~Shader();
121
122 void IncUseCount();
123 void DecUseCount();
124 void MarkAsDeleted();
125
126 int use_count_;
127
128 // The shader this Shader is tracking.
129 GLuint service_id_;
130 // Type of shader - GL_VERTEX_SHADER or GL_FRAGMENT_SHADER.
131 GLenum shader_type_;
132
133 // True if compilation succeeded.
134 bool valid_;
135
136 // The shader source as passed to glShaderSource.
137 scoped_ptr<std::string> source_;
138
Ben Murdochbb1529c2013-08-08 10:24:53 +0100139 // The source the last compile used.
140 scoped_ptr<std::string> signature_source_;
141
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000142 // The translated shader source.
143 scoped_ptr<std::string> translated_source_;
144
145 // The shader translation log.
146 scoped_ptr<std::string> log_info_;
147
148 // The type info when the shader was last compiled.
149 VariableMap attrib_map_;
150 VariableMap uniform_map_;
151
152 // The name hashing info when the shader was last compiled.
153 NameMap name_map_;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000154};
155
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000156// Tracks the Shaders.
157//
158// NOTE: To support shared resources an instance of this class will
159// need to be shared by multiple GLES2Decoders.
160class GPU_EXPORT ShaderManager {
161 public:
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000162 ShaderManager();
163 ~ShaderManager();
164
165 // Must call before destruction.
166 void Destroy(bool have_context);
167
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000168 // Creates a shader for the given shader ID.
169 Shader* CreateShader(
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000170 GLuint client_id,
171 GLuint service_id,
172 GLenum shader_type);
173
174 // Gets an existing shader info for the given shader ID. Returns NULL if none
175 // exists.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000176 Shader* GetShader(GLuint client_id);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000177
178 // Gets a client id for a given service id.
179 bool GetClientId(GLuint service_id, GLuint* client_id) const;
180
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000181 void MarkAsDeleted(Shader* shader);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000182
183 // Mark a shader as used
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000184 void UseShader(Shader* shader);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000185
186 // Unmark a shader as used. If it has been deleted and is not used
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000187 // then we free the shader.
188 void UnuseShader(Shader* shader);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000189
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000190 // Check if a Shader is owned by this ShaderManager.
191 bool IsOwned(Shader* shader);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000192
193 private:
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000194 friend class Shader;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000195
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000196 // Info for each shader by service side shader Id.
197 typedef base::hash_map<GLuint, scoped_refptr<Shader> > ShaderMap;
198 ShaderMap shaders_;
199
200 void RemoveShader(Shader* shader);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000201
202 DISALLOW_COPY_AND_ASSIGN(ShaderManager);
203};
204
205} // namespace gles2
206} // namespace gpu
207
208#endif // GPU_COMMAND_BUFFER_SERVICE_SHADER_MANAGER_H_
209