forked from Pissandshittium/pissandshittium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogram_binding.cc
146 lines (124 loc) · 4.3 KB
/
program_binding.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "cc/output/program_binding.h"
#include "base/trace_event/trace_event.h"
#include "cc/output/geometry_binding.h"
#include "gpu/GLES2/gl2extchromium.h"
#include "gpu/command_buffer/client/gles2_interface.h"
using gpu::gles2::GLES2Interface;
namespace cc {
ProgramBindingBase::ProgramBindingBase()
: program_(0),
vertex_shader_id_(0),
fragment_shader_id_(0),
initialized_(false) {}
ProgramBindingBase::~ProgramBindingBase() {
// If you hit these asserts, you initialized but forgot to call Cleanup().
DCHECK(!program_);
DCHECK(!vertex_shader_id_);
DCHECK(!fragment_shader_id_);
DCHECK(!initialized_);
}
bool ProgramBindingBase::Init(GLES2Interface* context,
const std::string& vertex_shader,
const std::string& fragment_shader) {
TRACE_EVENT0("cc", "ProgramBindingBase::init");
vertex_shader_id_ = LoadShader(context, GL_VERTEX_SHADER, vertex_shader);
if (!vertex_shader_id_)
return false;
fragment_shader_id_ =
LoadShader(context, GL_FRAGMENT_SHADER, fragment_shader);
if (!fragment_shader_id_) {
context->DeleteShader(vertex_shader_id_);
vertex_shader_id_ = 0;
return false;
}
program_ =
CreateShaderProgram(context, vertex_shader_id_, fragment_shader_id_);
return !!program_;
}
bool ProgramBindingBase::Link(GLES2Interface* context) {
context->LinkProgram(program_);
CleanupShaders(context);
if (!program_)
return false;
#ifndef NDEBUG
int linked = 0;
context->GetProgramiv(program_, GL_LINK_STATUS, &linked);
if (!linked) {
char buffer[1024];
context->GetProgramInfoLog(program_, sizeof(buffer), nullptr, buffer);
DLOG(ERROR) << "Error compiling shader: " << buffer;
return false;
}
#endif
return true;
}
void ProgramBindingBase::Cleanup(GLES2Interface* context) {
initialized_ = false;
if (!program_)
return;
DCHECK(context);
context->DeleteProgram(program_);
program_ = 0;
CleanupShaders(context);
}
unsigned ProgramBindingBase::LoadShader(GLES2Interface* context,
unsigned type,
const std::string& shader_source) {
unsigned shader = context->CreateShader(type);
if (!shader)
return 0u;
const char* shader_source_str[] = { shader_source.data() };
int shader_length[] = { static_cast<int>(shader_source.length()) };
context->ShaderSource(
shader, 1,
shader_source_str,
shader_length);
context->CompileShader(shader);
#if DCHECK_IS_ON()
int compiled = 0;
context->GetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
if (!compiled) {
char buffer[1024];
context->GetShaderInfoLog(shader, sizeof(buffer), nullptr, buffer);
DLOG(ERROR) << "Error compiling shader: " << buffer
<< "\n shader program: " << shader_source;
return 0u;
}
#endif
return shader;
}
unsigned ProgramBindingBase::CreateShaderProgram(GLES2Interface* context,
unsigned vertex_shader,
unsigned fragment_shader) {
unsigned program_object = context->CreateProgram();
if (!program_object)
return 0;
context->AttachShader(program_object, vertex_shader);
context->AttachShader(program_object, fragment_shader);
// Bind the common attrib locations.
context->BindAttribLocation(
program_object, GeometryBinding::PositionAttribLocation(), "a_position");
context->BindAttribLocation(
program_object, GeometryBinding::TexCoordAttribLocation(), "a_texCoord");
context->BindAttribLocation(program_object,
GeometryBinding::TriangleIndexAttribLocation(),
"a_index");
return program_object;
}
void ProgramBindingBase::CleanupShaders(GLES2Interface* context) {
if (vertex_shader_id_) {
context->DeleteShader(vertex_shader_id_);
vertex_shader_id_ = 0;
}
if (fragment_shader_id_) {
context->DeleteShader(fragment_shader_id_);
fragment_shader_id_ = 0;
}
}
bool ProgramBindingBase::IsContextLost(GLES2Interface* context) {
return context->GetGraphicsResetStatusKHR() != GL_NO_ERROR;
}
} // namespace cc