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_OUTPUT_PROGRAM_BINDING_H_ | |
6 #define CC_OUTPUT_PROGRAM_BINDING_H_ | |
7 | |
8 #include <string> | |
9 | |
10 #include "base/logging.h" | |
11 #include "cc/output/context_provider.h" | |
12 #include "cc/output/shader.h" | |
13 | |
14 namespace gpu { | |
15 namespace gles2 { | |
16 class GLES2Interface; | |
17 } | |
18 } | |
19 | |
20 namespace cc { | |
21 | |
22 class ProgramBindingBase { | |
23 public: | |
24 ProgramBindingBase(); | |
25 ~ProgramBindingBase(); | |
26 | |
27 bool Init(gpu::gles2::GLES2Interface* context, | |
28 const std::string& vertex_shader, | |
29 const std::string& fragment_shader); | |
30 bool Link(gpu::gles2::GLES2Interface* context); | |
31 void Cleanup(gpu::gles2::GLES2Interface* context); | |
32 | |
33 unsigned program() const { return program_; } | |
34 bool initialized() const { return initialized_; } | |
35 | |
36 protected: | |
37 unsigned LoadShader(gpu::gles2::GLES2Interface* context, | |
38 unsigned type, | |
39 const std::string& shader_source); | |
40 unsigned CreateShaderProgram(gpu::gles2::GLES2Interface* context, | |
41 unsigned vertex_shader, | |
42 unsigned fragment_shader); | |
43 void CleanupShaders(gpu::gles2::GLES2Interface* context); | |
44 | |
45 unsigned program_; | |
46 unsigned vertex_shader_id_; | |
47 unsigned fragment_shader_id_; | |
48 bool initialized_; | |
49 | |
50 private: | |
51 DISALLOW_COPY_AND_ASSIGN(ProgramBindingBase); | |
52 }; | |
53 | |
54 template <class VertexShader, class FragmentShader> | |
55 class ProgramBinding : public ProgramBindingBase { | |
56 public: | |
57 ProgramBinding() {} | |
58 | |
59 void Initialize(ContextProvider* context_provider, | |
60 TexCoordPrecision precision, | |
61 SamplerType sampler) { | |
62 return Initialize( | |
63 context_provider, precision, sampler, BLEND_MODE_NONE, false); | |
64 } | |
65 | |
66 void Initialize(ContextProvider* context_provider, | |
67 TexCoordPrecision precision, | |
68 SamplerType sampler, | |
69 BlendMode blend_mode) { | |
70 return Initialize( | |
71 context_provider, precision, sampler, blend_mode, false); | |
72 } | |
73 | |
74 void Initialize(ContextProvider* context_provider, | |
75 TexCoordPrecision precision, | |
76 SamplerType sampler, | |
77 BlendMode blend_mode, | |
78 bool mask_for_background) { | |
79 DCHECK(context_provider); | |
80 DCHECK(!initialized_); | |
81 | |
82 if (context_provider->IsContextLost()) | |
83 return; | |
84 | |
85 fragment_shader_.set_blend_mode(blend_mode); | |
86 fragment_shader_.set_mask_for_background(mask_for_background); | |
87 | |
88 if (!ProgramBindingBase::Init( | |
89 context_provider->ContextGL(), | |
90 vertex_shader_.GetShaderString(), | |
91 fragment_shader_.GetShaderString(precision, sampler))) { | |
92 DCHECK(context_provider->IsContextLost()); | |
93 return; | |
94 } | |
95 | |
96 int base_uniform_index = 0; | |
97 vertex_shader_.Init(context_provider->ContextGL(), | |
98 program_, &base_uniform_index); | |
99 fragment_shader_.Init(context_provider->ContextGL(), | |
100 program_, &base_uniform_index); | |
101 | |
102 // Link after binding uniforms | |
103 if (!Link(context_provider->ContextGL())) { | |
104 DCHECK(context_provider->IsContextLost()); | |
105 return; | |
106 } | |
107 | |
108 initialized_ = true; | |
109 } | |
110 | |
111 const VertexShader& vertex_shader() const { return vertex_shader_; } | |
112 const FragmentShader& fragment_shader() const { return fragment_shader_; } | |
113 | |
114 private: | |
115 VertexShader vertex_shader_; | |
116 FragmentShader fragment_shader_; | |
117 | |
118 DISALLOW_COPY_AND_ASSIGN(ProgramBinding); | |
119 }; | |
120 | |
121 } // namespace cc | |
122 | |
123 #endif // CC_OUTPUT_PROGRAM_BINDING_H_ | |
OLD | NEW |