OLD | NEW |
| (Empty) |
1 // Copyright 2011 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 CC_PROGRAM_BINDING_H_ | |
6 #define CC_PROGRAM_BINDING_H_ | |
7 | |
8 #include <string> | |
9 | |
10 #include "base/logging.h" | |
11 | |
12 namespace WebKit { class WebGraphicsContext3D; } | |
13 | |
14 namespace cc { | |
15 | |
16 class ProgramBindingBase { | |
17 public: | |
18 ProgramBindingBase(); | |
19 ~ProgramBindingBase(); | |
20 | |
21 void Init(WebKit::WebGraphicsContext3D* context, | |
22 const std::string& vertex_shader, | |
23 const std::string& fragment_shader); | |
24 void Link(WebKit::WebGraphicsContext3D* context); | |
25 void Cleanup(WebKit::WebGraphicsContext3D* context); | |
26 | |
27 unsigned program() const { return program_; } | |
28 bool initialized() const { return initialized_; } | |
29 | |
30 protected: | |
31 unsigned LoadShader(WebKit::WebGraphicsContext3D* context, | |
32 unsigned type, | |
33 const std::string& shader_source); | |
34 unsigned CreateShaderProgram(WebKit::WebGraphicsContext3D* context, | |
35 unsigned vertex_shader, | |
36 unsigned fragment_shader); | |
37 void CleanupShaders(WebKit::WebGraphicsContext3D* context); | |
38 bool IsContextLost(WebKit::WebGraphicsContext3D* context); | |
39 | |
40 unsigned program_; | |
41 unsigned vertex_shader_id_; | |
42 unsigned fragment_shader_id_; | |
43 bool initialized_; | |
44 }; | |
45 | |
46 template <class VertexShader, class FragmentShader> | |
47 class ProgramBinding : public ProgramBindingBase { | |
48 public: | |
49 explicit ProgramBinding(WebKit::WebGraphicsContext3D* context) { | |
50 ProgramBindingBase::Init(context, | |
51 vertex_shader_.getShaderString(), | |
52 fragment_shader_.getShaderString()); | |
53 } | |
54 | |
55 void Initialize(WebKit::WebGraphicsContext3D* context, | |
56 bool using_bind_uniform) { | |
57 DCHECK(context); | |
58 DCHECK(!initialized_); | |
59 | |
60 if (IsContextLost(context)) | |
61 return; | |
62 | |
63 // Need to bind uniforms before linking | |
64 if (!using_bind_uniform) | |
65 Link(context); | |
66 | |
67 int base_uniform_index = 0; | |
68 vertex_shader_.init( | |
69 context, program_, using_bind_uniform, &base_uniform_index); | |
70 fragment_shader_.init( | |
71 context, program_, using_bind_uniform, &base_uniform_index); | |
72 | |
73 // Link after binding uniforms | |
74 if (using_bind_uniform) | |
75 Link(context); | |
76 | |
77 initialized_ = true; | |
78 } | |
79 | |
80 const VertexShader& vertex_shader() const { return vertex_shader_; } | |
81 const FragmentShader& fragment_shader() const { return fragment_shader_; } | |
82 | |
83 private: | |
84 VertexShader vertex_shader_; | |
85 FragmentShader fragment_shader_; | |
86 }; | |
87 | |
88 } // namespace cc | |
89 | |
90 #endif // CC_PROGRAM_BINDING_H_ | |
OLD | NEW |