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 #include "cc/output/program_binding.h"
7 #include "base/debug/trace_event.h"
8 #include "cc/output/geometry_binding.h"
9 #include "gpu/command_buffer/client/gles2_interface.h"
11 using gpu::gles2::GLES2Interface
;
15 ProgramBindingBase::ProgramBindingBase()
18 fragment_shader_id_(0),
19 initialized_(false) {}
21 ProgramBindingBase::~ProgramBindingBase() {
22 // If you hit these asserts, you initialized but forgot to call Cleanup().
24 DCHECK(!vertex_shader_id_
);
25 DCHECK(!fragment_shader_id_
);
26 DCHECK(!initialized_
);
29 bool ProgramBindingBase::Init(GLES2Interface
* context
,
30 const std::string
& vertex_shader
,
31 const std::string
& fragment_shader
) {
32 TRACE_EVENT0("cc", "ProgramBindingBase::init");
33 vertex_shader_id_
= LoadShader(context
, GL_VERTEX_SHADER
, vertex_shader
);
34 if (!vertex_shader_id_
)
38 LoadShader(context
, GL_FRAGMENT_SHADER
, fragment_shader
);
39 if (!fragment_shader_id_
) {
40 context
->DeleteShader(vertex_shader_id_
);
41 vertex_shader_id_
= 0;
46 CreateShaderProgram(context
, vertex_shader_id_
, fragment_shader_id_
);
50 bool ProgramBindingBase::Link(GLES2Interface
* context
) {
51 context
->LinkProgram(program_
);
52 CleanupShaders(context
);
57 context
->GetProgramiv(program_
, GL_LINK_STATUS
, &linked
);
64 void ProgramBindingBase::Cleanup(GLES2Interface
* context
) {
70 context
->DeleteProgram(program_
);
73 CleanupShaders(context
);
76 unsigned ProgramBindingBase::LoadShader(GLES2Interface
* context
,
78 const std::string
& shader_source
) {
79 unsigned shader
= context
->CreateShader(type
);
83 const char* shader_source_str
[] = { shader_source
.data() };
84 int shader_length
[] = { static_cast<int>(shader_source
.length()) };
85 context
->ShaderSource(
89 context
->CompileShader(shader
);
92 context
->GetShaderiv(shader
, GL_COMPILE_STATUS
, &compiled
);
99 unsigned ProgramBindingBase::CreateShaderProgram(GLES2Interface
* context
,
100 unsigned vertex_shader
,
101 unsigned fragment_shader
) {
102 unsigned program_object
= context
->CreateProgram();
106 context
->AttachShader(program_object
, vertex_shader
);
107 context
->AttachShader(program_object
, fragment_shader
);
109 // Bind the common attrib locations.
110 context
->BindAttribLocation(
111 program_object
, GeometryBinding::PositionAttribLocation(), "a_position");
112 context
->BindAttribLocation(
113 program_object
, GeometryBinding::TexCoordAttribLocation(), "a_texCoord");
114 context
->BindAttribLocation(program_object
,
115 GeometryBinding::TriangleIndexAttribLocation(),
118 return program_object
;
121 void ProgramBindingBase::CleanupShaders(GLES2Interface
* context
) {
122 if (vertex_shader_id_
) {
123 context
->DeleteShader(vertex_shader_id_
);
124 vertex_shader_id_
= 0;
126 if (fragment_shader_id_
) {
127 context
->DeleteShader(fragment_shader_id_
);
128 fragment_shader_id_
= 0;