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

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: fix android_aosp warning 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
« no previous file with comments | « cc/output/program_binding.h ('k') | cc/output/shader.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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);
60 if (!linked) { 59 if (!linked)
61 GLC(context, context->deleteProgram(program_));
62 return false; 60 return false;
63 }
64 #endif 61 #endif
65 return true; 62 return true;
66 } 63 }
67 64
68 void ProgramBindingBase::Cleanup(WebGraphicsContext3D* context) { 65 void ProgramBindingBase::Cleanup(GLES2Interface* context) {
69 if (!initialized_)
70 return;
71
72 initialized_ = false; 66 initialized_ = false;
73 if (!program_) 67 if (!program_)
74 return; 68 return;
75 69
76 DCHECK(context); 70 DCHECK(context);
77 GLC(context, context->deleteProgram(program_)); 71 context->DeleteProgram(program_);
78 program_ = 0; 72 program_ = 0;
79 73
80 CleanupShaders(context); 74 CleanupShaders(context);
81 } 75 }
82 76
83 unsigned ProgramBindingBase::LoadShader(WebGraphicsContext3D* context, 77 unsigned ProgramBindingBase::LoadShader(GLES2Interface* context,
84 unsigned type, 78 unsigned type,
85 const std::string& shader_source) { 79 const std::string& shader_source) {
86 unsigned shader = context->createShader(type); 80 unsigned shader = context->CreateShader(type);
87 if (!shader) 81 if (!shader)
88 return 0; 82 return 0u;
89 GLC(context, context->shaderSource(shader, shader_source.data())); 83
90 GLC(context, context->compileShader(shader)); 84 const char* shader_source_str[] = { shader_source.data() };
85 int shader_length[] = { static_cast<int>(shader_source.length()) };
86 context->ShaderSource(
87 shader, 1,
88 shader_source_str,
89 shader_length);
90 context->CompileShader(shader);
91 #ifndef NDEBUG 91 #ifndef NDEBUG
92 int compiled = 0; 92 int compiled = 0;
93 GLC(context, context->getShaderiv(shader, GL_COMPILE_STATUS, &compiled)); 93 context->GetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
94 if (!compiled) { 94 if (!compiled)
95 GLC(context, context->deleteShader(shader)); 95 return 0u;
96 return 0;
97 }
98 #endif 96 #endif
99 return shader; 97 return shader;
100 } 98 }
101 99
102 unsigned ProgramBindingBase::CreateShaderProgram(WebGraphicsContext3D* context, 100 unsigned ProgramBindingBase::CreateShaderProgram(GLES2Interface* context,
103 unsigned vertex_shader, 101 unsigned vertex_shader,
104 unsigned fragment_shader) { 102 unsigned fragment_shader) {
105 unsigned program_object = context->createProgram(); 103 unsigned program_object = context->CreateProgram();
106 if (!program_object) 104 if (!program_object)
107 return 0; 105 return 0;
108 106
109 GLC(context, context->attachShader(program_object, vertex_shader)); 107 context->AttachShader(program_object, vertex_shader);
110 GLC(context, context->attachShader(program_object, fragment_shader)); 108 context->AttachShader(program_object, fragment_shader);
111 109
112 // Bind the common attrib locations. 110 // Bind the common attrib locations.
113 GLC(context, 111 context->BindAttribLocation(
114 context->bindAttribLocation(program_object, 112 program_object, GeometryBinding::PositionAttribLocation(), "a_position");
115 GeometryBinding::PositionAttribLocation(), 113 context->BindAttribLocation(
116 "a_position")); 114 program_object, GeometryBinding::TexCoordAttribLocation(), "a_texCoord");
117 GLC(context, 115 context->BindAttribLocation(program_object,
118 context->bindAttribLocation(program_object, 116 GeometryBinding::TriangleIndexAttribLocation(),
119 GeometryBinding::TexCoordAttribLocation(), 117 "a_index");
120 "a_texCoord"));
121 GLC(context,
122 context->bindAttribLocation(
123 program_object,
124 GeometryBinding::TriangleIndexAttribLocation(),
125 "a_index"));
126 118
127 return program_object; 119 return program_object;
128 } 120 }
129 121
130 void ProgramBindingBase::CleanupShaders(WebGraphicsContext3D* context) { 122 void ProgramBindingBase::CleanupShaders(GLES2Interface* context) {
131 if (vertex_shader_id_) { 123 if (vertex_shader_id_) {
132 GLC(context, context->deleteShader(vertex_shader_id_)); 124 context->DeleteShader(vertex_shader_id_);
133 vertex_shader_id_ = 0; 125 vertex_shader_id_ = 0;
134 } 126 }
135 if (fragment_shader_id_) { 127 if (fragment_shader_id_) {
136 GLC(context, context->deleteShader(fragment_shader_id_)); 128 context->DeleteShader(fragment_shader_id_);
137 fragment_shader_id_ = 0; 129 fragment_shader_id_ = 0;
138 } 130 }
139 } 131 }
140 132
141 } // namespace cc 133 } // namespace cc
OLDNEW
« no previous file with comments | « cc/output/program_binding.h ('k') | cc/output/shader.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698