Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(445)

Side by Side Diff: cc/output/program_binding.cc

Issue 93433004: Use GLES2Interface for shaders and programs (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2011 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "cc/output/program_binding.h" 5 #include "cc/output/program_binding.h"
6 6
7 #include "base/debug/trace_event.h" 7 #include "base/debug/trace_event.h"
8 #include "cc/output/geometry_binding.h" 8 #include "cc/output/geometry_binding.h"
9 #include "cc/output/gl_renderer.h" // For the GLC() macro. 9 #include "gpu/command_buffer/client/gles2_interface.h"
10 #include "third_party/WebKit/public/platform/WebGraphicsContext3D.h"
11 #include "third_party/khronos/GLES2/gl2.h" 10 #include "third_party/khronos/GLES2/gl2.h"
12 11
13 using blink::WebGraphicsContext3D; 12 using gpu::gles2::GLES2Interface;
14 13
15 namespace cc { 14 namespace cc {
16 15
17 ProgramBindingBase::ProgramBindingBase() 16 ProgramBindingBase::ProgramBindingBase()
18 : program_(0), 17 : program_(0),
19 vertex_shader_id_(0), 18 vertex_shader_id_(0),
20 fragment_shader_id_(0), 19 fragment_shader_id_(0),
21 initialized_(false) {} 20 initialized_(false) {}
22 21
23 ProgramBindingBase::~ProgramBindingBase() { 22 ProgramBindingBase::~ProgramBindingBase() {
24 // If you hit these asserts, you initialized but forgot to call Cleanup(). 23 // If you hit these asserts, you initialized but forgot to call Cleanup().
25 DCHECK(!program_); 24 DCHECK(!program_);
26 DCHECK(!vertex_shader_id_); 25 DCHECK(!vertex_shader_id_);
27 DCHECK(!fragment_shader_id_); 26 DCHECK(!fragment_shader_id_);
28 DCHECK(!initialized_); 27 DCHECK(!initialized_);
29 } 28 }
30 29
31 bool ProgramBindingBase::Init(WebGraphicsContext3D* context, 30 bool ProgramBindingBase::Init(GLES2Interface* context,
32 const std::string& vertex_shader, 31 const std::string& vertex_shader,
33 const std::string& fragment_shader) { 32 const std::string& fragment_shader) {
34 TRACE_EVENT0("cc", "ProgramBindingBase::init"); 33 TRACE_EVENT0("cc", "ProgramBindingBase::init");
35 vertex_shader_id_ = LoadShader(context, GL_VERTEX_SHADER, vertex_shader); 34 vertex_shader_id_ = LoadShader(context, GL_VERTEX_SHADER, vertex_shader);
36 if (!vertex_shader_id_) 35 if (!vertex_shader_id_)
37 return false; 36 return false;
38 37
39 fragment_shader_id_ = 38 fragment_shader_id_ =
40 LoadShader(context, GL_FRAGMENT_SHADER, fragment_shader); 39 LoadShader(context, GL_FRAGMENT_SHADER, fragment_shader);
41 if (!fragment_shader_id_) { 40 if (!fragment_shader_id_) {
42 GLC(context, context->deleteShader(vertex_shader_id_)); 41 context->DeleteShader(vertex_shader_id_);
43 vertex_shader_id_ = 0; 42 vertex_shader_id_ = 0;
44 return false; 43 return false;
45 } 44 }
46 45
47 program_ = 46 program_ =
48 CreateShaderProgram(context, vertex_shader_id_, fragment_shader_id_); 47 CreateShaderProgram(context, vertex_shader_id_, fragment_shader_id_);
49 return !!program_; 48 return !!program_;
50 } 49 }
51 50
52 bool ProgramBindingBase::Link(WebGraphicsContext3D* context) { 51 bool ProgramBindingBase::Link(GLES2Interface* context) {
53 GLC(context, context->linkProgram(program_)); 52 context->LinkProgram(program_);
54 CleanupShaders(context); 53 CleanupShaders(context);
55 if (!program_) 54 if (!program_)
56 return false; 55 return false;
57 #ifndef NDEBUG 56 #ifndef NDEBUG
58 int linked = 0; 57 int linked = 0;
59 GLC(context, context->getProgramiv(program_, GL_LINK_STATUS, &linked)); 58 context->GetProgramiv(program_, GL_LINK_STATUS, &linked);
59 DCHECK(linked);
60 if (!linked) { 60 if (!linked) {
61 GLC(context, context->deleteProgram(program_)); 61 context->DeleteProgram(program_);
62 return false; 62 return false;
63 } 63 }
64 #endif 64 #endif
65 return true; 65 return true;
66 } 66 }
67 67
68 void ProgramBindingBase::Cleanup(WebGraphicsContext3D* context) { 68 void ProgramBindingBase::Cleanup(GLES2Interface* context) {
69 if (!initialized_)
70 return;
71
72 initialized_ = false; 69 initialized_ = false;
73 if (!program_) 70 if (!program_)
74 return; 71 return;
75 72
76 DCHECK(context); 73 DCHECK(context);
77 GLC(context, context->deleteProgram(program_)); 74 context->DeleteProgram(program_);
78 program_ = 0; 75 program_ = 0;
79 76
80 CleanupShaders(context); 77 CleanupShaders(context);
81 } 78 }
82 79
83 unsigned ProgramBindingBase::LoadShader(WebGraphicsContext3D* context, 80 unsigned ProgramBindingBase::LoadShader(GLES2Interface* context,
84 unsigned type, 81 unsigned type,
85 const std::string& shader_source) { 82 const std::string& shader_source) {
86 unsigned shader = context->createShader(type); 83 unsigned shader = context->CreateShader(type);
84 DCHECK(shader);
87 if (!shader) 85 if (!shader)
danakj 2013/12/03 19:24:49 If we're going to DCHECK in here, is there any rea
jamesr 2013/12/04 01:50:21 I don't think there is - removed.
danakj 2013/12/04 16:39:44 Ok cool, same question for all the other added DCH
88 return 0; 86 return 0;
89 GLC(context, context->shaderSource(shader, shader_source.data())); 87 const char* shader_source_str[] = { shader_source.data() };
90 GLC(context, context->compileShader(shader)); 88 int shader_length[] = { shader_source.length() };
89 context->ShaderSource(
90 shader, 1,
91 shader_source_str,
92 shader_length);
93 context->CompileShader(shader);
91 #ifndef NDEBUG 94 #ifndef NDEBUG
92 int compiled = 0; 95 int compiled = 0;
93 GLC(context, context->getShaderiv(shader, GL_COMPILE_STATUS, &compiled)); 96 context->GetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
97 DCHECK(compiled);
94 if (!compiled) { 98 if (!compiled) {
95 GLC(context, context->deleteShader(shader)); 99 context->DeleteShader(shader);
96 return 0; 100 return 0;
97 } 101 }
98 #endif 102 #endif
99 return shader; 103 return shader;
100 } 104 }
101 105
102 unsigned ProgramBindingBase::CreateShaderProgram(WebGraphicsContext3D* context, 106 unsigned ProgramBindingBase::CreateShaderProgram(GLES2Interface* context,
103 unsigned vertex_shader, 107 unsigned vertex_shader,
104 unsigned fragment_shader) { 108 unsigned fragment_shader) {
105 unsigned program_object = context->createProgram(); 109 unsigned program_object = context->CreateProgram();
106 if (!program_object) 110 if (!program_object)
107 return 0; 111 return 0;
108 112
109 GLC(context, context->attachShader(program_object, vertex_shader)); 113 context->AttachShader(program_object, vertex_shader);
110 GLC(context, context->attachShader(program_object, fragment_shader)); 114 context->AttachShader(program_object, fragment_shader);
111 115
112 // Bind the common attrib locations. 116 // Bind the common attrib locations.
113 GLC(context, 117 context->BindAttribLocation(
114 context->bindAttribLocation(program_object, 118 program_object, GeometryBinding::PositionAttribLocation(), "a_position");
115 GeometryBinding::PositionAttribLocation(), 119 context->BindAttribLocation(
116 "a_position")); 120 program_object, GeometryBinding::TexCoordAttribLocation(), "a_texCoord");
117 GLC(context, 121 context->BindAttribLocation(program_object,
118 context->bindAttribLocation(program_object, 122 GeometryBinding::TriangleIndexAttribLocation(),
119 GeometryBinding::TexCoordAttribLocation(), 123 "a_index");
120 "a_texCoord"));
121 GLC(context,
122 context->bindAttribLocation(
123 program_object,
124 GeometryBinding::TriangleIndexAttribLocation(),
125 "a_index"));
126 124
127 return program_object; 125 return program_object;
128 } 126 }
129 127
130 void ProgramBindingBase::CleanupShaders(WebGraphicsContext3D* context) { 128 void ProgramBindingBase::CleanupShaders(GLES2Interface* context) {
131 if (vertex_shader_id_) { 129 if (vertex_shader_id_) {
132 GLC(context, context->deleteShader(vertex_shader_id_)); 130 context->DeleteShader(vertex_shader_id_);
133 vertex_shader_id_ = 0; 131 vertex_shader_id_ = 0;
134 } 132 }
135 if (fragment_shader_id_) { 133 if (fragment_shader_id_) {
136 GLC(context, context->deleteShader(fragment_shader_id_)); 134 context->DeleteShader(fragment_shader_id_);
137 fragment_shader_id_ = 0; 135 fragment_shader_id_ = 0;
138 } 136 }
139 } 137 }
140 138
141 } // namespace cc 139 } // namespace cc
OLDNEW
« no previous file with comments | « cc/output/program_binding.h ('k') | cc/output/shader.h » ('j') | cc/output/shader_unittest.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698