| OLD | NEW |
| 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 "cc/output/gl_renderer.h" // For the GLC() macro. |
| 10 #include "third_party/WebKit/public/platform/WebGraphicsContext3D.h" | 10 #include "third_party/WebKit/public/platform/WebGraphicsContext3D.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 initialized_(false) {} | 21 initialized_(false) {} |
| 22 | 22 |
| 23 ProgramBindingBase::~ProgramBindingBase() { | 23 ProgramBindingBase::~ProgramBindingBase() { |
| 24 // If you hit these asserts, you initialized but forgot to call Cleanup(). | 24 // If you hit these asserts, you initialized but forgot to call Cleanup(). |
| 25 DCHECK(!program_); | 25 DCHECK(!program_); |
| 26 DCHECK(!vertex_shader_id_); | 26 DCHECK(!vertex_shader_id_); |
| 27 DCHECK(!fragment_shader_id_); | 27 DCHECK(!fragment_shader_id_); |
| 28 DCHECK(!initialized_); | 28 DCHECK(!initialized_); |
| 29 } | 29 } |
| 30 | 30 |
| 31 void ProgramBindingBase::Init(WebGraphicsContext3D* context, | 31 bool ProgramBindingBase::Init(WebGraphicsContext3D* context, |
| 32 const std::string& vertex_shader, | 32 const std::string& vertex_shader, |
| 33 const std::string& fragment_shader) { | 33 const std::string& fragment_shader) { |
| 34 TRACE_EVENT0("cc", "ProgramBindingBase::init"); | 34 TRACE_EVENT0("cc", "ProgramBindingBase::init"); |
| 35 vertex_shader_id_ = LoadShader(context, GL_VERTEX_SHADER, vertex_shader); | 35 vertex_shader_id_ = LoadShader(context, GL_VERTEX_SHADER, vertex_shader); |
| 36 if (!vertex_shader_id_) { | 36 if (!vertex_shader_id_) |
| 37 if (!IsContextLost(context)) | 37 return false; |
| 38 LOG(ERROR) << "Failed to create vertex shader"; | |
| 39 return; | |
| 40 } | |
| 41 | 38 |
| 42 fragment_shader_id_ = | 39 fragment_shader_id_ = |
| 43 LoadShader(context, GL_FRAGMENT_SHADER, fragment_shader); | 40 LoadShader(context, GL_FRAGMENT_SHADER, fragment_shader); |
| 44 if (!fragment_shader_id_) { | 41 if (!fragment_shader_id_) { |
| 45 GLC(context, context->deleteShader(vertex_shader_id_)); | 42 GLC(context, context->deleteShader(vertex_shader_id_)); |
| 46 vertex_shader_id_ = 0; | 43 vertex_shader_id_ = 0; |
| 47 if (!IsContextLost(context)) | 44 return false; |
| 48 LOG(ERROR) << "Failed to create fragment shader"; | |
| 49 return; | |
| 50 } | 45 } |
| 51 | 46 |
| 52 program_ = | 47 program_ = |
| 53 CreateShaderProgram(context, vertex_shader_id_, fragment_shader_id_); | 48 CreateShaderProgram(context, vertex_shader_id_, fragment_shader_id_); |
| 54 DCHECK(program_ || IsContextLost(context)); | 49 return !!program_; |
| 55 } | 50 } |
| 56 | 51 |
| 57 void ProgramBindingBase::Link(WebGraphicsContext3D* context) { | 52 bool ProgramBindingBase::Link(WebGraphicsContext3D* context) { |
| 58 GLC(context, context->linkProgram(program_)); | 53 GLC(context, context->linkProgram(program_)); |
| 59 CleanupShaders(context); | 54 CleanupShaders(context); |
| 60 if (!program_) | 55 if (!program_) |
| 61 return; | 56 return false; |
| 62 #ifndef NDEBUG | 57 #ifndef NDEBUG |
| 63 int linked = 0; | 58 int linked = 0; |
| 64 GLC(context, context->getProgramiv(program_, GL_LINK_STATUS, &linked)); | 59 GLC(context, context->getProgramiv(program_, GL_LINK_STATUS, &linked)); |
| 65 if (!linked) { | 60 if (!linked) { |
| 66 if (!IsContextLost(context)) | |
| 67 LOG(ERROR) << "Failed to link shader program"; | |
| 68 GLC(context, context->deleteProgram(program_)); | 61 GLC(context, context->deleteProgram(program_)); |
| 62 return false; |
| 69 } | 63 } |
| 70 #endif | 64 #endif |
| 65 return true; |
| 71 } | 66 } |
| 72 | 67 |
| 73 void ProgramBindingBase::Cleanup(WebGraphicsContext3D* context) { | 68 void ProgramBindingBase::Cleanup(WebGraphicsContext3D* context) { |
| 69 if (!initialized_) |
| 70 return; |
| 71 |
| 74 initialized_ = false; | 72 initialized_ = false; |
| 75 if (!program_) | 73 if (!program_) |
| 76 return; | 74 return; |
| 77 | 75 |
| 78 DCHECK(context); | 76 DCHECK(context); |
| 79 GLC(context, context->deleteProgram(program_)); | 77 GLC(context, context->deleteProgram(program_)); |
| 80 program_ = 0; | 78 program_ = 0; |
| 81 | 79 |
| 82 CleanupShaders(context); | 80 CleanupShaders(context); |
| 83 } | 81 } |
| (...skipping 14 matching lines...) Expand all Loading... |
| 98 return 0; | 96 return 0; |
| 99 } | 97 } |
| 100 #endif | 98 #endif |
| 101 return shader; | 99 return shader; |
| 102 } | 100 } |
| 103 | 101 |
| 104 unsigned ProgramBindingBase::CreateShaderProgram(WebGraphicsContext3D* context, | 102 unsigned ProgramBindingBase::CreateShaderProgram(WebGraphicsContext3D* context, |
| 105 unsigned vertex_shader, | 103 unsigned vertex_shader, |
| 106 unsigned fragment_shader) { | 104 unsigned fragment_shader) { |
| 107 unsigned program_object = context->createProgram(); | 105 unsigned program_object = context->createProgram(); |
| 108 if (!program_object) { | 106 if (!program_object) |
| 109 if (!IsContextLost(context)) | |
| 110 LOG(ERROR) << "Failed to create shader program"; | |
| 111 return 0; | 107 return 0; |
| 112 } | |
| 113 | 108 |
| 114 GLC(context, context->attachShader(program_object, vertex_shader)); | 109 GLC(context, context->attachShader(program_object, vertex_shader)); |
| 115 GLC(context, context->attachShader(program_object, fragment_shader)); | 110 GLC(context, context->attachShader(program_object, fragment_shader)); |
| 116 | 111 |
| 117 // Bind the common attrib locations. | 112 // Bind the common attrib locations. |
| 118 GLC(context, | 113 GLC(context, |
| 119 context->bindAttribLocation(program_object, | 114 context->bindAttribLocation(program_object, |
| 120 GeometryBinding::PositionAttribLocation(), | 115 GeometryBinding::PositionAttribLocation(), |
| 121 "a_position")); | 116 "a_position")); |
| 122 GLC(context, | 117 GLC(context, |
| (...skipping 13 matching lines...) Expand all Loading... |
| 136 if (vertex_shader_id_) { | 131 if (vertex_shader_id_) { |
| 137 GLC(context, context->deleteShader(vertex_shader_id_)); | 132 GLC(context, context->deleteShader(vertex_shader_id_)); |
| 138 vertex_shader_id_ = 0; | 133 vertex_shader_id_ = 0; |
| 139 } | 134 } |
| 140 if (fragment_shader_id_) { | 135 if (fragment_shader_id_) { |
| 141 GLC(context, context->deleteShader(fragment_shader_id_)); | 136 GLC(context, context->deleteShader(fragment_shader_id_)); |
| 142 fragment_shader_id_ = 0; | 137 fragment_shader_id_ = 0; |
| 143 } | 138 } |
| 144 } | 139 } |
| 145 | 140 |
| 146 bool ProgramBindingBase::IsContextLost(WebGraphicsContext3D* context) { | |
| 147 return (context->getGraphicsResetStatusARB() != GL_NO_ERROR); | |
| 148 } | |
| 149 | |
| 150 } // namespace cc | 141 } // namespace cc |
| OLD | NEW |