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_
10 #include "base/logging.h"
11 #include "cc/output/context_provider.h"
12 #include "cc/output/shader.h"
22 class 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_
; }
37 unsigned LoadShader(gpu::gles2::GLES2Interface
* context
,
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
);
48 unsigned vertex_shader_id_
;
49 unsigned fragment_shader_id_
;
53 DISALLOW_COPY_AND_ASSIGN(ProgramBindingBase
);
56 template <class VertexShader
, class FragmentShader
>
57 class ProgramBinding
: public ProgramBindingBase
{
61 void Initialize(ContextProvider
* context_provider
,
62 TexCoordPrecision precision
,
63 SamplerType sampler
) {
65 context_provider
, precision
, sampler
, BLEND_MODE_NONE
, false);
68 void Initialize(ContextProvider
* context_provider
,
69 TexCoordPrecision precision
,
71 BlendMode blend_mode
) {
73 context_provider
, precision
, sampler
, blend_mode
, false);
76 void Initialize(ContextProvider
* context_provider
,
77 TexCoordPrecision precision
,
80 bool mask_for_background
) {
81 DCHECK(context_provider
);
82 DCHECK(!initialized_
);
84 if (IsContextLost(context_provider
->ContextGL()))
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()));
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()));
113 const VertexShader
& vertex_shader() const { return vertex_shader_
; }
114 const FragmentShader
& fragment_shader() const { return fragment_shader_
; }
117 VertexShader vertex_shader_
;
118 FragmentShader fragment_shader_
;
120 DISALLOW_COPY_AND_ASSIGN(ProgramBinding
);
125 #endif // CC_OUTPUT_PROGRAM_BINDING_H_