Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / cc / output / program_binding.h
blob8018cad76e34ca33ef3c6bbdf6abef44aef25d12
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.
5 #ifndef CC_OUTPUT_PROGRAM_BINDING_H_
6 #define CC_OUTPUT_PROGRAM_BINDING_H_
8 #include <string>
10 #include "base/logging.h"
11 #include "cc/output/context_provider.h"
12 #include "cc/output/shader.h"
14 namespace gpu {
15 namespace gles2 {
16 class GLES2Interface;
20 namespace cc {
22 class ProgramBindingBase {
23 public:
24 ProgramBindingBase();
25 ~ProgramBindingBase();
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);
33 unsigned program() const { return program_; }
34 bool initialized() const { return initialized_; }
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);
45 bool IsContextLost(gpu::gles2::GLES2Interface* context);
47 unsigned program_;
48 unsigned vertex_shader_id_;
49 unsigned fragment_shader_id_;
50 bool initialized_;
52 private:
53 DISALLOW_COPY_AND_ASSIGN(ProgramBindingBase);
56 template <class VertexShader, class FragmentShader>
57 class ProgramBinding : public ProgramBindingBase {
58 public:
59 ProgramBinding() {}
61 void Initialize(ContextProvider* context_provider,
62 TexCoordPrecision precision,
63 SamplerType sampler) {
64 return Initialize(
65 context_provider, precision, sampler, BLEND_MODE_NONE, false);
68 void Initialize(ContextProvider* context_provider,
69 TexCoordPrecision precision,
70 SamplerType sampler,
71 BlendMode blend_mode) {
72 return Initialize(
73 context_provider, precision, sampler, blend_mode, false);
76 void Initialize(ContextProvider* context_provider,
77 TexCoordPrecision precision,
78 SamplerType sampler,
79 BlendMode blend_mode,
80 bool mask_for_background) {
81 DCHECK(context_provider);
82 DCHECK(!initialized_);
84 if (IsContextLost(context_provider->ContextGL()))
85 return;
87 fragment_shader_.set_blend_mode(blend_mode);
88 fragment_shader_.set_mask_for_background(mask_for_background);
90 if (!ProgramBindingBase::Init(
91 context_provider->ContextGL(),
92 vertex_shader_.GetShaderString(),
93 fragment_shader_.GetShaderString(precision, sampler))) {
94 DCHECK(IsContextLost(context_provider->ContextGL()));
95 return;
98 int base_uniform_index = 0;
99 vertex_shader_.Init(context_provider->ContextGL(),
100 program_, &base_uniform_index);
101 fragment_shader_.Init(context_provider->ContextGL(),
102 program_, &base_uniform_index);
104 // Link after binding uniforms
105 if (!Link(context_provider->ContextGL())) {
106 DCHECK(IsContextLost(context_provider->ContextGL()));
107 return;
110 initialized_ = true;
113 const VertexShader& vertex_shader() const { return vertex_shader_; }
114 const FragmentShader& fragment_shader() const { return fragment_shader_; }
116 private:
117 VertexShader vertex_shader_;
118 FragmentShader fragment_shader_;
120 DISALLOW_COPY_AND_ASSIGN(ProgramBinding);
123 } // namespace cc
125 #endif // CC_OUTPUT_PROGRAM_BINDING_H_