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