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/shader.h"
13 namespace WebKit
{ class WebGraphicsContext3D
; }
17 class ProgramBindingBase
{
20 ~ProgramBindingBase();
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
);
28 unsigned program() const { return program_
; }
29 bool initialized() const { return initialized_
; }
32 unsigned LoadShader(WebKit::WebGraphicsContext3D
* context
,
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
);
42 unsigned vertex_shader_id_
;
43 unsigned fragment_shader_id_
;
47 DISALLOW_COPY_AND_ASSIGN(ProgramBindingBase
);
50 template <class VertexShader
, class FragmentShader
>
51 class ProgramBinding
: public ProgramBindingBase
{
53 explicit ProgramBinding(WebKit::WebGraphicsContext3D
* context
,
54 TexCoordPrecision precision
) {
55 ProgramBindingBase::Init(
57 vertex_shader_
.GetShaderString(),
58 fragment_shader_
.GetShaderString(precision
));
61 void Initialize(WebKit::WebGraphicsContext3D
* context
,
62 bool using_bind_uniform
) {
64 DCHECK(!initialized_
);
66 if (IsContextLost(context
))
69 // Need to bind uniforms before linking
70 if (!using_bind_uniform
)
73 int base_uniform_index
= 0;
75 context
, program_
, using_bind_uniform
, &base_uniform_index
);
76 fragment_shader_
.Init(
77 context
, program_
, using_bind_uniform
, &base_uniform_index
);
79 // Link after binding uniforms
80 if (using_bind_uniform
)
86 const VertexShader
& vertex_shader() const { return vertex_shader_
; }
87 const FragmentShader
& fragment_shader() const { return fragment_shader_
; }
90 VertexShader vertex_shader_
;
91 FragmentShader fragment_shader_
;
93 DISALLOW_COPY_AND_ASSIGN(ProgramBinding
);
98 #endif // CC_OUTPUT_PROGRAM_BINDING_H_