[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | a8461d8 | 2012-10-16 21:11:14 | [diff] [blame] | 5 | #include "cc/program_binding.h" |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 6 | |
[email protected] | 6331a117 | 2012-10-18 11:35:13 | [diff] [blame] | 7 | #include "base/debug/trace_event.h" |
[email protected] | a8461d8 | 2012-10-16 21:11:14 | [diff] [blame] | 8 | #include "cc/geometry_binding.h" |
[email protected] | c4040a52 | 2012-10-21 15:01:40 | [diff] [blame] | 9 | #include "cc/gl_renderer.h" // For the GLC() macro. |
[email protected] | 920caa5 | 2012-12-18 22:39:50 | [diff] [blame] | 10 | #include "third_party/WebKit/Source/Platform/chromium/public/WebGraphicsContext3D.h" |
[email protected] | d9c2852 | 2012-10-18 23:35:43 | [diff] [blame] | 11 | #include "third_party/khronos/GLES2/gl2.h" |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 12 | |
| 13 | using WebKit::WebGraphicsContext3D; |
| 14 | |
[email protected] | 9c88e56 | 2012-09-14 22:21:30 | [diff] [blame] | 15 | namespace cc { |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 16 | |
| 17 | ProgramBindingBase::ProgramBindingBase() |
| 18 | : m_program(0) |
| 19 | , m_vertexShaderId(0) |
| 20 | , m_fragmentShaderId(0) |
| 21 | , m_initialized(false) |
| 22 | { |
| 23 | } |
| 24 | |
| 25 | ProgramBindingBase::~ProgramBindingBase() |
| 26 | { |
| 27 | // If you hit these asserts, you initialized but forgot to call cleanup(). |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 28 | DCHECK(!m_program); |
| 29 | DCHECK(!m_vertexShaderId); |
| 30 | DCHECK(!m_fragmentShaderId); |
| 31 | DCHECK(!m_initialized); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 32 | } |
| 33 | |
[email protected] | 515e8d23 | 2012-09-10 19:15:27 | [diff] [blame] | 34 | void ProgramBindingBase::init(WebGraphicsContext3D* context, const std::string& vertexShader, const std::string& fragmentShader) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 35 | { |
| 36 | TRACE_EVENT0("cc", "ProgramBindingBase::init"); |
[email protected] | d9c2852 | 2012-10-18 23:35:43 | [diff] [blame] | 37 | m_vertexShaderId = loadShader(context, GL_VERTEX_SHADER, vertexShader); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 38 | if (!m_vertexShaderId) { |
[email protected] | d01f0d43 | 2012-11-17 22:24:47 | [diff] [blame] | 39 | if (!IsContextLost(context)) |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 40 | LOG(ERROR) << "Failed to create vertex shader"; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 41 | return; |
| 42 | } |
| 43 | |
[email protected] | d9c2852 | 2012-10-18 23:35:43 | [diff] [blame] | 44 | m_fragmentShaderId = loadShader(context, GL_FRAGMENT_SHADER, fragmentShader); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 45 | if (!m_fragmentShaderId) { |
| 46 | GLC(context, context->deleteShader(m_vertexShaderId)); |
| 47 | m_vertexShaderId = 0; |
[email protected] | d01f0d43 | 2012-11-17 22:24:47 | [diff] [blame] | 48 | if (!IsContextLost(context)) |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 49 | LOG(ERROR) << "Failed to create fragment shader"; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 50 | return; |
| 51 | } |
| 52 | |
| 53 | m_program = createShaderProgram(context, m_vertexShaderId, m_fragmentShaderId); |
[email protected] | d01f0d43 | 2012-11-17 22:24:47 | [diff] [blame] | 54 | DCHECK(m_program || IsContextLost(context)); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | void ProgramBindingBase::link(WebGraphicsContext3D* context) |
| 58 | { |
| 59 | GLC(context, context->linkProgram(m_program)); |
| 60 | cleanupShaders(context); |
[email protected] | e7f87cfb | 2012-10-17 01:25:18 | [diff] [blame] | 61 | #ifndef NDEBUG |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 62 | int linked = 0; |
[email protected] | d9c2852 | 2012-10-18 23:35:43 | [diff] [blame] | 63 | GLC(context, context->getProgramiv(m_program, GL_LINK_STATUS, &linked)); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 64 | if (!linked) { |
[email protected] | d01f0d43 | 2012-11-17 22:24:47 | [diff] [blame] | 65 | if (!IsContextLost(context)) |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 66 | LOG(ERROR) << "Failed to link shader program"; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 67 | GLC(context, context->deleteProgram(m_program)); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 68 | } |
| 69 | #endif |
| 70 | } |
| 71 | |
| 72 | void ProgramBindingBase::cleanup(WebGraphicsContext3D* context) |
| 73 | { |
| 74 | m_initialized = false; |
| 75 | if (!m_program) |
| 76 | return; |
| 77 | |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 78 | DCHECK(context); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 79 | GLC(context, context->deleteProgram(m_program)); |
| 80 | m_program = 0; |
| 81 | |
| 82 | cleanupShaders(context); |
| 83 | } |
| 84 | |
[email protected] | 515e8d23 | 2012-09-10 19:15:27 | [diff] [blame] | 85 | unsigned ProgramBindingBase::loadShader(WebGraphicsContext3D* context, unsigned type, const std::string& shaderSource) |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 86 | { |
| 87 | unsigned shader = context->createShader(type); |
| 88 | if (!shader) |
| 89 | return 0; |
[email protected] | 515e8d23 | 2012-09-10 19:15:27 | [diff] [blame] | 90 | GLC(context, context->shaderSource(shader, shaderSource.data())); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 91 | GLC(context, context->compileShader(shader)); |
[email protected] | e7f87cfb | 2012-10-17 01:25:18 | [diff] [blame] | 92 | #ifndef NDEBUG |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 93 | int compiled = 0; |
[email protected] | d9c2852 | 2012-10-18 23:35:43 | [diff] [blame] | 94 | GLC(context, context->getShaderiv(shader, GL_COMPILE_STATUS, &compiled)); |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 95 | if (!compiled) { |
| 96 | GLC(context, context->deleteShader(shader)); |
| 97 | return 0; |
| 98 | } |
| 99 | #endif |
| 100 | return shader; |
| 101 | } |
| 102 | |
| 103 | unsigned ProgramBindingBase::createShaderProgram(WebGraphicsContext3D* context, unsigned vertexShader, unsigned fragmentShader) |
| 104 | { |
| 105 | unsigned programObject = context->createProgram(); |
| 106 | if (!programObject) { |
[email protected] | d01f0d43 | 2012-11-17 22:24:47 | [diff] [blame] | 107 | if (!IsContextLost(context)) |
[email protected] | 1d99317 | 2012-10-18 18:15:04 | [diff] [blame] | 108 | LOG(ERROR) << "Failed to create shader program"; |
[email protected] | 94f206c1 | 2012-08-25 00:09:14 | [diff] [blame] | 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | GLC(context, context->attachShader(programObject, vertexShader)); |
| 113 | GLC(context, context->attachShader(programObject, fragmentShader)); |
| 114 | |
| 115 | // Bind the common attrib locations. |
| 116 | GLC(context, context->bindAttribLocation(programObject, GeometryBinding::positionAttribLocation(), "a_position")); |
| 117 | GLC(context, context->bindAttribLocation(programObject, GeometryBinding::texCoordAttribLocation(), "a_texCoord")); |
| 118 | |
| 119 | return programObject; |
| 120 | } |
| 121 | |
| 122 | void ProgramBindingBase::cleanupShaders(WebGraphicsContext3D* context) |
| 123 | { |
| 124 | if (m_vertexShaderId) { |
| 125 | GLC(context, context->deleteShader(m_vertexShaderId)); |
| 126 | m_vertexShaderId = 0; |
| 127 | } |
| 128 | if (m_fragmentShaderId) { |
| 129 | GLC(context, context->deleteShader(m_fragmentShaderId)); |
| 130 | m_fragmentShaderId = 0; |
| 131 | } |
| 132 | } |
| 133 | |
[email protected] | d01f0d43 | 2012-11-17 22:24:47 | [diff] [blame] | 134 | bool ProgramBindingBase::IsContextLost(WebGraphicsContext3D* context) { |
| 135 | return (context->getGraphicsResetStatusARB() != GL_NO_ERROR); |
| 136 | } |
| 137 | |
[email protected] | bc5e77c | 2012-11-05 20:00:49 | [diff] [blame] | 138 | } // namespace cc |