| 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/program_binding.h" | 5 #include "cc/program_binding.h" |
| 6 | 6 |
| 7 #include "base/debug/trace_event.h" | 7 #include "base/debug/trace_event.h" |
| 8 #include "cc/geometry_binding.h" | 8 #include "cc/geometry_binding.h" |
| 9 #include "cc/gl_renderer.h" // For the GLC() macro. | 9 #include "cc/gl_renderer.h" // For the GLC() macro. |
| 10 #include "third_party/khronos/GLES2/gl2.h" | 10 #include "third_party/khronos/GLES2/gl2.h" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 | 24 |
| 25 ProgramBindingBase::~ProgramBindingBase() | 25 ProgramBindingBase::~ProgramBindingBase() |
| 26 { | 26 { |
| 27 // If you hit these asserts, you initialized but forgot to call cleanup(). | 27 // If you hit these asserts, you initialized but forgot to call cleanup(). |
| 28 DCHECK(!m_program); | 28 DCHECK(!m_program); |
| 29 DCHECK(!m_vertexShaderId); | 29 DCHECK(!m_vertexShaderId); |
| 30 DCHECK(!m_fragmentShaderId); | 30 DCHECK(!m_fragmentShaderId); |
| 31 DCHECK(!m_initialized); | 31 DCHECK(!m_initialized); |
| 32 } | 32 } |
| 33 | 33 |
| 34 static bool contextLost(WebGraphicsContext3D* context) | |
| 35 { | |
| 36 return (context->getGraphicsResetStatusARB() != GL_NO_ERROR); | |
| 37 } | |
| 38 | |
| 39 | |
| 40 void ProgramBindingBase::init(WebGraphicsContext3D* context, const std::string&
vertexShader, const std::string& fragmentShader) | 34 void ProgramBindingBase::init(WebGraphicsContext3D* context, const std::string&
vertexShader, const std::string& fragmentShader) |
| 41 { | 35 { |
| 42 TRACE_EVENT0("cc", "ProgramBindingBase::init"); | 36 TRACE_EVENT0("cc", "ProgramBindingBase::init"); |
| 43 m_vertexShaderId = loadShader(context, GL_VERTEX_SHADER, vertexShader); | 37 m_vertexShaderId = loadShader(context, GL_VERTEX_SHADER, vertexShader); |
| 44 if (!m_vertexShaderId) { | 38 if (!m_vertexShaderId) { |
| 45 if (!contextLost(context)) | 39 if (!IsContextLost(context)) |
| 46 LOG(ERROR) << "Failed to create vertex shader"; | 40 LOG(ERROR) << "Failed to create vertex shader"; |
| 47 return; | 41 return; |
| 48 } | 42 } |
| 49 | 43 |
| 50 m_fragmentShaderId = loadShader(context, GL_FRAGMENT_SHADER, fragmentShader)
; | 44 m_fragmentShaderId = loadShader(context, GL_FRAGMENT_SHADER, fragmentShader)
; |
| 51 if (!m_fragmentShaderId) { | 45 if (!m_fragmentShaderId) { |
| 52 GLC(context, context->deleteShader(m_vertexShaderId)); | 46 GLC(context, context->deleteShader(m_vertexShaderId)); |
| 53 m_vertexShaderId = 0; | 47 m_vertexShaderId = 0; |
| 54 if (!contextLost(context)) | 48 if (!IsContextLost(context)) |
| 55 LOG(ERROR) << "Failed to create fragment shader"; | 49 LOG(ERROR) << "Failed to create fragment shader"; |
| 56 return; | 50 return; |
| 57 } | 51 } |
| 58 | 52 |
| 59 m_program = createShaderProgram(context, m_vertexShaderId, m_fragmentShaderI
d); | 53 m_program = createShaderProgram(context, m_vertexShaderId, m_fragmentShaderI
d); |
| 60 DCHECK(m_program || contextLost(context)); | 54 DCHECK(m_program || IsContextLost(context)); |
| 61 } | 55 } |
| 62 | 56 |
| 63 void ProgramBindingBase::link(WebGraphicsContext3D* context) | 57 void ProgramBindingBase::link(WebGraphicsContext3D* context) |
| 64 { | 58 { |
| 65 GLC(context, context->linkProgram(m_program)); | 59 GLC(context, context->linkProgram(m_program)); |
| 66 cleanupShaders(context); | 60 cleanupShaders(context); |
| 67 #ifndef NDEBUG | 61 #ifndef NDEBUG |
| 68 int linked = 0; | 62 int linked = 0; |
| 69 GLC(context, context->getProgramiv(m_program, GL_LINK_STATUS, &linked)); | 63 GLC(context, context->getProgramiv(m_program, GL_LINK_STATUS, &linked)); |
| 70 if (!linked) { | 64 if (!linked) { |
| 71 if (!contextLost(context)) | 65 if (!IsContextLost(context)) |
| 72 LOG(ERROR) << "Failed to link shader program"; | 66 LOG(ERROR) << "Failed to link shader program"; |
| 73 GLC(context, context->deleteProgram(m_program)); | 67 GLC(context, context->deleteProgram(m_program)); |
| 74 } | 68 } |
| 75 #endif | 69 #endif |
| 76 } | 70 } |
| 77 | 71 |
| 78 void ProgramBindingBase::cleanup(WebGraphicsContext3D* context) | 72 void ProgramBindingBase::cleanup(WebGraphicsContext3D* context) |
| 79 { | 73 { |
| 80 m_initialized = false; | 74 m_initialized = false; |
| 81 if (!m_program) | 75 if (!m_program) |
| (...skipping 21 matching lines...) Expand all Loading... |
| 103 return 0; | 97 return 0; |
| 104 } | 98 } |
| 105 #endif | 99 #endif |
| 106 return shader; | 100 return shader; |
| 107 } | 101 } |
| 108 | 102 |
| 109 unsigned ProgramBindingBase::createShaderProgram(WebGraphicsContext3D* context,
unsigned vertexShader, unsigned fragmentShader) | 103 unsigned ProgramBindingBase::createShaderProgram(WebGraphicsContext3D* context,
unsigned vertexShader, unsigned fragmentShader) |
| 110 { | 104 { |
| 111 unsigned programObject = context->createProgram(); | 105 unsigned programObject = context->createProgram(); |
| 112 if (!programObject) { | 106 if (!programObject) { |
| 113 if (!contextLost(context)) | 107 if (!IsContextLost(context)) |
| 114 LOG(ERROR) << "Failed to create shader program"; | 108 LOG(ERROR) << "Failed to create shader program"; |
| 115 return 0; | 109 return 0; |
| 116 } | 110 } |
| 117 | 111 |
| 118 GLC(context, context->attachShader(programObject, vertexShader)); | 112 GLC(context, context->attachShader(programObject, vertexShader)); |
| 119 GLC(context, context->attachShader(programObject, fragmentShader)); | 113 GLC(context, context->attachShader(programObject, fragmentShader)); |
| 120 | 114 |
| 121 // Bind the common attrib locations. | 115 // Bind the common attrib locations. |
| 122 GLC(context, context->bindAttribLocation(programObject, GeometryBinding::pos
itionAttribLocation(), "a_position")); | 116 GLC(context, context->bindAttribLocation(programObject, GeometryBinding::pos
itionAttribLocation(), "a_position")); |
| 123 GLC(context, context->bindAttribLocation(programObject, GeometryBinding::tex
CoordAttribLocation(), "a_texCoord")); | 117 GLC(context, context->bindAttribLocation(programObject, GeometryBinding::tex
CoordAttribLocation(), "a_texCoord")); |
| 124 | 118 |
| 125 return programObject; | 119 return programObject; |
| 126 } | 120 } |
| 127 | 121 |
| 128 void ProgramBindingBase::cleanupShaders(WebGraphicsContext3D* context) | 122 void ProgramBindingBase::cleanupShaders(WebGraphicsContext3D* context) |
| 129 { | 123 { |
| 130 if (m_vertexShaderId) { | 124 if (m_vertexShaderId) { |
| 131 GLC(context, context->deleteShader(m_vertexShaderId)); | 125 GLC(context, context->deleteShader(m_vertexShaderId)); |
| 132 m_vertexShaderId = 0; | 126 m_vertexShaderId = 0; |
| 133 } | 127 } |
| 134 if (m_fragmentShaderId) { | 128 if (m_fragmentShaderId) { |
| 135 GLC(context, context->deleteShader(m_fragmentShaderId)); | 129 GLC(context, context->deleteShader(m_fragmentShaderId)); |
| 136 m_fragmentShaderId = 0; | 130 m_fragmentShaderId = 0; |
| 137 } | 131 } |
| 138 } | 132 } |
| 139 | 133 |
| 134 bool ProgramBindingBase::IsContextLost(WebGraphicsContext3D* context) { |
| 135 return (context->getGraphicsResetStatusARB() != GL_NO_ERROR); |
| 136 } |
| 137 |
| 140 } // namespace cc | 138 } // namespace cc |
| OLD | NEW |