OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "gpu/command_buffer/service/gles2_cmd_clear_framebuffer.h" | 5 #include "gpu/command_buffer/service/gles2_cmd_clear_framebuffer.h" |
6 | 6 |
| 7 #include <string> |
| 8 |
7 #include "gpu/command_buffer/service/gl_utils.h" | 9 #include "gpu/command_buffer/service/gl_utils.h" |
8 #include "gpu/command_buffer/service/gles2_cmd_decoder.h" | 10 #include "gpu/command_buffer/service/gles2_cmd_decoder.h" |
| 11 #include "gpu/command_buffer/service/shader_translator.h" |
9 #include "ui/gfx/geometry/size.h" | 12 #include "ui/gfx/geometry/size.h" |
| 13 #include "ui/gl/gl_version_info.h" |
10 | 14 |
11 namespace { | 15 namespace { |
12 | 16 |
13 #define SHADER(src) \ | 17 #define SHADER(src) \ |
14 "#ifdef GL_ES\n" \ | 18 "#ifdef GL_ES\n" \ |
15 "precision mediump float;\n" \ | 19 "precision mediump float;\n" \ |
16 "#endif\n" #src | 20 "#endif\n" #src |
17 | 21 |
18 const char* g_vertex_shader_source = { | 22 const char* g_vertex_shader_source = { |
19 SHADER( | 23 SHADER( |
(...skipping 30 matching lines...) Expand all Loading... |
50 } | 54 } |
51 #endif | 55 #endif |
52 } | 56 } |
53 | 57 |
54 } // namespace | 58 } // namespace |
55 | 59 |
56 namespace gpu { | 60 namespace gpu { |
57 namespace gles2 { | 61 namespace gles2 { |
58 | 62 |
59 ClearFramebufferResourceManager::ClearFramebufferResourceManager( | 63 ClearFramebufferResourceManager::ClearFramebufferResourceManager( |
60 const gles2::GLES2Decoder* decoder) | 64 const gles2::GLES2Decoder* decoder, |
61 : initialized_(false), program_(0u), buffer_id_(0u) { | 65 const gl::GLVersionInfo& gl_version_info) |
| 66 : initialized_(false), |
| 67 is_desktop_core_profile_(gl_version_info.is_desktop_core_profile), |
| 68 program_(0u), |
| 69 vao_(0u), |
| 70 buffer_id_(0u) { |
62 Initialize(decoder); | 71 Initialize(decoder); |
63 } | 72 } |
64 | 73 |
65 ClearFramebufferResourceManager::~ClearFramebufferResourceManager() { | 74 ClearFramebufferResourceManager::~ClearFramebufferResourceManager() { |
66 Destroy(); | 75 Destroy(); |
67 DCHECK(!buffer_id_); | 76 DCHECK(!buffer_id_); |
68 } | 77 } |
69 | 78 |
70 void ClearFramebufferResourceManager::Initialize( | 79 void ClearFramebufferResourceManager::Initialize( |
71 const gles2::GLES2Decoder* decoder) { | 80 const gles2::GLES2Decoder* decoder) { |
| 81 name_map_["a_position"] = "a_position"; |
| 82 name_map_["u_clear_depth"] = "u_clear_depth"; |
| 83 name_map_["u_clear_color"] = "u_clear_color"; |
| 84 |
72 static_assert( | 85 static_assert( |
73 kVertexPositionAttrib == 0u, | 86 kVertexPositionAttrib == 0u, |
74 "kVertexPositionAttrib must be 0"); | 87 "kVertexPositionAttrib must be 0"); |
75 DCHECK(!buffer_id_); | 88 DCHECK(!buffer_id_); |
76 | 89 |
77 glGenBuffersARB(1, &buffer_id_); | 90 glGenBuffersARB(1, &buffer_id_); |
78 glBindBuffer(GL_ARRAY_BUFFER, buffer_id_); | 91 glBindBuffer(GL_ARRAY_BUFFER, buffer_id_); |
79 const GLfloat kQuadVertices[] = {-1.0f, -1.0f, | 92 const GLfloat kQuadVertices[] = {-1.0f, -1.0f, |
80 1.0f, -1.0f, | 93 1.0f, -1.0f, |
81 1.0f, 1.0f, | 94 1.0f, 1.0f, |
82 -1.0f, 1.0f}; | 95 -1.0f, 1.0f}; |
83 glBufferData( | 96 glBufferData( |
84 GL_ARRAY_BUFFER, sizeof(kQuadVertices), kQuadVertices, GL_STATIC_DRAW); | 97 GL_ARRAY_BUFFER, sizeof(kQuadVertices), kQuadVertices, GL_STATIC_DRAW); |
| 98 |
| 99 if (is_desktop_core_profile_) { |
| 100 glGenVertexArraysOES(1, &vao_); |
| 101 glBindVertexArrayOES(vao_); |
| 102 glEnableVertexAttribArray(kVertexPositionAttrib); |
| 103 glVertexAttribPointer(kVertexPositionAttrib, 2, GL_FLOAT, GL_FALSE, 0, 0); |
| 104 decoder->RestoreAllAttributes(); |
| 105 } |
85 decoder->RestoreBufferBindings(); | 106 decoder->RestoreBufferBindings(); |
86 initialized_ = true; | 107 initialized_ = true; |
87 } | 108 } |
88 | 109 |
| 110 void ClearFramebufferResourceManager::InitShader(const GLES2Decoder* decoder, |
| 111 GLenum type) { |
| 112 std::string source_string(type == GL_VERTEX_SHADER |
| 113 ? g_vertex_shader_source |
| 114 : g_fragment_shader_source); |
| 115 std::string& translated_string(type == GL_VERTEX_SHADER |
| 116 ? vertex_shader_source_ |
| 117 : fragment_shader_source_); |
| 118 if (is_desktop_core_profile_) { |
| 119 scoped_refptr<ShaderTranslatorInterface> translator = |
| 120 decoder->GetTranslator(type); |
| 121 int shader_version = 0; |
| 122 NameMap map; |
| 123 translator->Translate(source_string, NULL, &translated_string, |
| 124 &shader_version, 0, 0, 0, 0, 0, &map); |
| 125 for (const auto& pair : map) { |
| 126 name_map_[pair.second] = pair.first; |
| 127 } |
| 128 } else { |
| 129 translated_string = std::string(source_string); |
| 130 } |
| 131 } |
| 132 |
89 void ClearFramebufferResourceManager::Destroy() { | 133 void ClearFramebufferResourceManager::Destroy() { |
90 if (!initialized_) | 134 if (!initialized_) |
91 return; | 135 return; |
92 | 136 |
93 glDeleteProgram(program_); | 137 glDeleteProgram(program_); |
| 138 if (vao_ != 0) { |
| 139 glDeleteVertexArraysOES(1, &vao_); |
| 140 vao_ = 0; |
| 141 } |
94 glDeleteBuffersARB(1, &buffer_id_); | 142 glDeleteBuffersARB(1, &buffer_id_); |
95 buffer_id_ = 0; | 143 buffer_id_ = 0; |
96 } | 144 } |
97 | 145 |
98 void ClearFramebufferResourceManager::ClearFramebuffer( | 146 void ClearFramebufferResourceManager::ClearFramebuffer( |
99 const gles2::GLES2Decoder* decoder, | 147 const gles2::GLES2Decoder* decoder, |
100 const gfx::Size& framebuffer_size, | 148 const gfx::Size& framebuffer_size, |
101 GLbitfield mask, | 149 GLbitfield mask, |
102 GLfloat clear_color_red, | 150 GLfloat clear_color_red, |
103 GLfloat clear_color_green, | 151 GLfloat clear_color_green, |
104 GLfloat clear_color_blue, | 152 GLfloat clear_color_blue, |
105 GLfloat clear_color_alpha, | 153 GLfloat clear_color_alpha, |
106 GLfloat clear_depth_value, | 154 GLfloat clear_depth_value, |
107 GLint clear_stencil_value) { | 155 GLint clear_stencil_value) { |
108 if (!initialized_) { | 156 if (!initialized_) { |
109 DLOG(ERROR) << "Uninitialized manager."; | 157 DLOG(ERROR) << "Uninitialized manager."; |
110 return; | 158 return; |
111 } | 159 } |
112 | 160 |
113 if (!program_) { | 161 if (!program_) { |
114 program_ = glCreateProgram(); | 162 program_ = glCreateProgram(); |
| 163 InitShader(decoder, GL_VERTEX_SHADER); |
115 GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER); | 164 GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER); |
116 CompileShader(vertex_shader, g_vertex_shader_source); | 165 CompileShader(vertex_shader, vertex_shader_source_.c_str()); |
117 glAttachShader(program_, vertex_shader); | 166 glAttachShader(program_, vertex_shader); |
| 167 InitShader(decoder, GL_FRAGMENT_SHADER); |
118 GLuint fragment_shader = glCreateShader(GL_FRAGMENT_SHADER); | 168 GLuint fragment_shader = glCreateShader(GL_FRAGMENT_SHADER); |
119 CompileShader(fragment_shader, g_fragment_shader_source); | 169 CompileShader(fragment_shader, fragment_shader_source_.c_str()); |
120 glAttachShader(program_, fragment_shader); | 170 glAttachShader(program_, fragment_shader); |
121 glBindAttribLocation(program_, kVertexPositionAttrib, "a_position"); | 171 glBindAttribLocation(program_, kVertexPositionAttrib, |
| 172 name_map_["a_position"].c_str()); |
122 glLinkProgram(program_); | 173 glLinkProgram(program_); |
123 #if DCHECK_IS_ON() | 174 #if DCHECK_IS_ON() |
124 GLint linked = GL_FALSE; | 175 GLint linked = GL_FALSE; |
125 glGetProgramiv(program_, GL_LINK_STATUS, &linked); | 176 glGetProgramiv(program_, GL_LINK_STATUS, &linked); |
126 if (GL_TRUE != linked) | 177 if (GL_TRUE != linked) |
127 DLOG(ERROR) << "Program link failure."; | 178 DLOG(ERROR) << "Program link failure."; |
128 #endif | 179 #endif |
129 depth_handle_ = glGetUniformLocation(program_, "u_clear_depth"); | 180 depth_handle_ = |
130 color_handle_ = glGetUniformLocation(program_, "u_clear_color"); | 181 glGetUniformLocation(program_, name_map_["u_clear_depth"].c_str()); |
| 182 color_handle_ = |
| 183 glGetUniformLocation(program_, name_map_["u_clear_color"].c_str()); |
| 184 DCHECK(depth_handle_ != -1); |
| 185 DCHECK(color_handle_ != -1); |
131 glDeleteShader(fragment_shader); | 186 glDeleteShader(fragment_shader); |
132 glDeleteShader(vertex_shader); | 187 glDeleteShader(vertex_shader); |
133 } | 188 } |
134 glUseProgram(program_); | 189 glUseProgram(program_); |
135 | 190 |
136 #if DCHECK_IS_ON() | 191 #if DCHECK_IS_ON() |
137 glValidateProgram(program_); | 192 glValidateProgram(program_); |
138 GLint validation_status = GL_FALSE; | 193 GLint validation_status = GL_FALSE; |
139 glGetProgramiv(program_, GL_VALIDATE_STATUS, &validation_status); | 194 glGetProgramiv(program_, GL_VALIDATE_STATUS, &validation_status); |
140 if (GL_TRUE != validation_status) | 195 if (GL_TRUE != validation_status) |
141 DLOG(ERROR) << "Invalid shader."; | 196 DLOG(ERROR) << "Invalid shader."; |
142 #endif | 197 #endif |
143 | 198 |
144 decoder->ClearAllAttributes(); | 199 if (vao_) { |
145 glEnableVertexAttribArray(kVertexPositionAttrib); | 200 glBindVertexArrayOES(vao_); |
| 201 } else { |
| 202 decoder->ClearAllAttributes(); |
| 203 glEnableVertexAttribArray(kVertexPositionAttrib); |
146 | 204 |
147 glBindBuffer(GL_ARRAY_BUFFER, buffer_id_); | 205 glBindBuffer(GL_ARRAY_BUFFER, buffer_id_); |
148 glVertexAttribPointer(kVertexPositionAttrib, 2, GL_FLOAT, GL_FALSE, 0, 0); | 206 glVertexAttribPointer(kVertexPositionAttrib, 2, GL_FLOAT, GL_FALSE, 0, 0); |
| 207 } |
149 | 208 |
150 glUniform1f(depth_handle_, clear_depth_value); | 209 glUniform1f(depth_handle_, clear_depth_value); |
151 glUniform4f(color_handle_, clear_color_red, clear_color_green, | 210 glUniform4f(color_handle_, clear_color_red, clear_color_green, |
152 clear_color_blue, clear_color_alpha); | 211 clear_color_blue, clear_color_alpha); |
153 | 212 |
154 if (!(mask & GL_COLOR_BUFFER_BIT)) { | 213 if (!(mask & GL_COLOR_BUFFER_BIT)) { |
155 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); | 214 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); |
156 } | 215 } |
157 | 216 |
158 if (mask & GL_DEPTH_BUFFER_BIT) { | 217 if (mask & GL_DEPTH_BUFFER_BIT) { |
(...skipping 14 matching lines...) Expand all Loading... |
173 glStencilMask(0); | 232 glStencilMask(0); |
174 } | 233 } |
175 | 234 |
176 glDisable(GL_CULL_FACE); | 235 glDisable(GL_CULL_FACE); |
177 glDisable(GL_BLEND); | 236 glDisable(GL_BLEND); |
178 glDisable(GL_POLYGON_OFFSET_FILL); | 237 glDisable(GL_POLYGON_OFFSET_FILL); |
179 | 238 |
180 glViewport(0, 0, framebuffer_size.width(), framebuffer_size.height()); | 239 glViewport(0, 0, framebuffer_size.width(), framebuffer_size.height()); |
181 glDrawArrays(GL_TRIANGLE_FAN, 0, 4); | 240 glDrawArrays(GL_TRIANGLE_FAN, 0, 4); |
182 | 241 |
| 242 if (vao_ == 0) { |
| 243 decoder->RestoreBufferBindings(); |
| 244 } |
183 decoder->RestoreAllAttributes(); | 245 decoder->RestoreAllAttributes(); |
184 decoder->RestoreProgramBindings(); | 246 decoder->RestoreProgramBindings(); |
185 decoder->RestoreBufferBindings(); | |
186 decoder->RestoreGlobalState(); | 247 decoder->RestoreGlobalState(); |
187 } | 248 } |
188 | 249 |
189 } // namespace gles2 | 250 } // namespace gles2 |
190 } // namespace gpu | 251 } // namespace gpu |
OLD | NEW |