OLD | NEW |
1 // Copyright 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CC_OUTPUT_PROGRAM_BINDING_H_ | 5 #ifndef CC_OUTPUT_PROGRAM_BINDING_H_ |
6 #define CC_OUTPUT_PROGRAM_BINDING_H_ | 6 #define CC_OUTPUT_PROGRAM_BINDING_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "cc/output/context_provider.h" |
11 #include "cc/output/shader.h" | 12 #include "cc/output/shader.h" |
12 | 13 |
13 namespace WebKit { class WebGraphicsContext3D; } | 14 namespace WebKit { class WebGraphicsContext3D; } |
14 | 15 |
15 namespace cc { | 16 namespace cc { |
16 | 17 |
17 class ProgramBindingBase { | 18 class ProgramBindingBase { |
18 public: | 19 public: |
19 ProgramBindingBase(); | 20 ProgramBindingBase(); |
20 ~ProgramBindingBase(); | 21 ~ProgramBindingBase(); |
21 | 22 |
22 void Init(WebKit::WebGraphicsContext3D* context, | 23 bool Init(WebKit::WebGraphicsContext3D* context, |
23 const std::string& vertex_shader, | 24 const std::string& vertex_shader, |
24 const std::string& fragment_shader); | 25 const std::string& fragment_shader); |
25 void Link(WebKit::WebGraphicsContext3D* context); | 26 bool Link(WebKit::WebGraphicsContext3D* context); |
26 void Cleanup(WebKit::WebGraphicsContext3D* context); | 27 void Cleanup(WebKit::WebGraphicsContext3D* context); |
27 | 28 |
28 unsigned program() const { return program_; } | 29 unsigned program() const { return program_; } |
29 bool initialized() const { return initialized_; } | 30 bool initialized() const { return initialized_; } |
30 | 31 |
31 protected: | 32 protected: |
32 unsigned LoadShader(WebKit::WebGraphicsContext3D* context, | 33 unsigned LoadShader(WebKit::WebGraphicsContext3D* context, |
33 unsigned type, | 34 unsigned type, |
34 const std::string& shader_source); | 35 const std::string& shader_source); |
35 unsigned CreateShaderProgram(WebKit::WebGraphicsContext3D* context, | 36 unsigned CreateShaderProgram(WebKit::WebGraphicsContext3D* context, |
36 unsigned vertex_shader, | 37 unsigned vertex_shader, |
37 unsigned fragment_shader); | 38 unsigned fragment_shader); |
38 void CleanupShaders(WebKit::WebGraphicsContext3D* context); | 39 void CleanupShaders(WebKit::WebGraphicsContext3D* context); |
39 bool IsContextLost(WebKit::WebGraphicsContext3D* context); | |
40 | 40 |
41 unsigned program_; | 41 unsigned program_; |
42 unsigned vertex_shader_id_; | 42 unsigned vertex_shader_id_; |
43 unsigned fragment_shader_id_; | 43 unsigned fragment_shader_id_; |
44 bool initialized_; | 44 bool initialized_; |
45 | 45 |
46 private: | 46 private: |
47 DISALLOW_COPY_AND_ASSIGN(ProgramBindingBase); | 47 DISALLOW_COPY_AND_ASSIGN(ProgramBindingBase); |
48 }; | 48 }; |
49 | 49 |
50 template <class VertexShader, class FragmentShader> | 50 template <class VertexShader, class FragmentShader> |
51 class ProgramBinding : public ProgramBindingBase { | 51 class ProgramBinding : public ProgramBindingBase { |
52 public: | 52 public: |
53 explicit ProgramBinding(WebKit::WebGraphicsContext3D* context, | 53 ProgramBinding() {} |
54 TexCoordPrecision precision) { | |
55 ProgramBindingBase::Init( | |
56 context, | |
57 vertex_shader_.GetShaderString(), | |
58 fragment_shader_.GetShaderString(precision)); | |
59 } | |
60 | 54 |
61 void Initialize(WebKit::WebGraphicsContext3D* context, | 55 void Initialize(ContextProvider* context_provider, |
62 bool using_bind_uniform) { | 56 TexCoordPrecision precision) { |
63 DCHECK(context); | 57 DCHECK(context_provider); |
64 DCHECK(!initialized_); | 58 DCHECK(!initialized_); |
65 | 59 |
66 if (IsContextLost(context)) | 60 if (context_provider->IsContextLost()) |
67 return; | 61 return; |
68 | 62 |
69 // Need to bind uniforms before linking | 63 if (!ProgramBindingBase::Init( |
70 if (!using_bind_uniform) | 64 context_provider->Context3d(), |
71 Link(context); | 65 vertex_shader_.GetShaderString(), |
| 66 fragment_shader_.GetShaderString(precision))) { |
| 67 DCHECK(context_provider->IsContextLost()); |
| 68 return; |
| 69 } |
72 | 70 |
73 int base_uniform_index = 0; | 71 int base_uniform_index = 0; |
74 vertex_shader_.Init( | 72 vertex_shader_.Init(context_provider->Context3d(), |
75 context, program_, using_bind_uniform, &base_uniform_index); | 73 program_, &base_uniform_index); |
76 fragment_shader_.Init( | 74 fragment_shader_.Init(context_provider->Context3d(), |
77 context, program_, using_bind_uniform, &base_uniform_index); | 75 program_, &base_uniform_index); |
78 | 76 |
79 // Link after binding uniforms | 77 // Link after binding uniforms |
80 if (using_bind_uniform) | 78 if (!Link(context_provider->Context3d())) { |
81 Link(context); | 79 DCHECK(context_provider->IsContextLost()); |
| 80 return; |
| 81 } |
82 | 82 |
83 initialized_ = true; | 83 initialized_ = true; |
84 } | 84 } |
85 | 85 |
86 const VertexShader& vertex_shader() const { return vertex_shader_; } | 86 const VertexShader& vertex_shader() const { return vertex_shader_; } |
87 const FragmentShader& fragment_shader() const { return fragment_shader_; } | 87 const FragmentShader& fragment_shader() const { return fragment_shader_; } |
88 | 88 |
89 private: | 89 private: |
90 VertexShader vertex_shader_; | 90 VertexShader vertex_shader_; |
91 FragmentShader fragment_shader_; | 91 FragmentShader fragment_shader_; |
92 | 92 |
93 DISALLOW_COPY_AND_ASSIGN(ProgramBinding); | 93 DISALLOW_COPY_AND_ASSIGN(ProgramBinding); |
94 }; | 94 }; |
95 | 95 |
96 } // namespace cc | 96 } // namespace cc |
97 | 97 |
98 #endif // CC_OUTPUT_PROGRAM_BINDING_H_ | 98 #endif // CC_OUTPUT_PROGRAM_BINDING_H_ |
OLD | NEW |