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 "gpu/command_buffer/service/gl_utils.h" | 7 #include "gpu/command_buffer/service/gl_utils.h" |
8 #include "gpu/command_buffer/service/gles2_cmd_decoder.h" | 8 #include "gpu/command_buffer/service/gles2_cmd_decoder.h" |
9 #include "ui/gfx/geometry/size.h" | 9 #include "ui/gfx/geometry/size.h" |
10 #include "ui/gl/gl_version_info.h" | |
10 | 11 |
11 namespace { | 12 namespace { |
12 | 13 |
13 #define SHADER(src) \ | 14 #define SHADER(src) \ |
14 "#ifdef GL_ES\n" \ | 15 "#ifdef GL_ES\n" \ |
15 "precision mediump float;\n" \ | 16 "precision mediump float;\n" \ |
16 "#endif\n" #src | 17 "#endif\n" #src |
17 | 18 |
18 const char* g_vertex_shader_source = { | 19 const char* g_vertex_shader_source = { |
19 SHADER( | 20 SHADER( |
20 uniform float u_clear_depth; | 21 uniform float u_clear_depth; |
21 attribute vec4 a_position; | 22 attribute vec4 a_position; |
22 void main(void) { | 23 void main(void) { |
23 gl_Position = vec4(a_position.x, a_position.y, u_clear_depth, 1.0); | 24 gl_Position = vec4(a_position.x, a_position.y, u_clear_depth, 1.0); |
24 } | 25 } |
25 ), | 26 ), |
26 }; | 27 }; |
27 | 28 |
28 const char* g_fragment_shader_source = { | 29 const char* g_fragment_shader_source = { |
29 SHADER( | 30 SHADER( |
30 uniform vec4 u_clear_color; | 31 uniform vec4 u_clear_color; |
31 void main(void) { | 32 void main(void) { |
32 gl_FragColor = u_clear_color; | 33 gl_FragColor = u_clear_color; |
33 } | 34 } |
34 ), | 35 ), |
35 }; | 36 }; |
36 | 37 |
37 void CompileShader(GLuint shader, const char* shader_source) { | 38 void CompileShader(GLuint shader, |
38 glShaderSource(shader, 1, &shader_source, 0); | 39 const char* shader_source, |
40 bool is_desktop_core_profile) { | |
41 if (is_desktop_core_profile) { | |
42 const char* version = "#version 150\n"; | |
43 const char* shader_sources[2] = {version, shader_source}; | |
44 glShaderSource(shader, 2, shader_sources, 0); | |
45 } else { | |
46 glShaderSource(shader, 1, &shader_source, 0); | |
47 } | |
39 glCompileShader(shader); | 48 glCompileShader(shader); |
40 #if DCHECK_IS_ON() | 49 #if DCHECK_IS_ON() |
41 GLint compile_status = GL_FALSE; | 50 GLint compile_status = GL_FALSE; |
42 glGetShaderiv(shader, GL_COMPILE_STATUS, &compile_status); | 51 glGetShaderiv(shader, GL_COMPILE_STATUS, &compile_status); |
43 if (GL_TRUE != compile_status) { | 52 if (GL_TRUE != compile_status) { |
44 char buffer[1024]; | 53 char buffer[1024]; |
45 GLsizei length = 0; | 54 GLsizei length = 0; |
46 glGetShaderInfoLog(shader, sizeof(buffer), &length, buffer); | 55 glGetShaderInfoLog(shader, sizeof(buffer), &length, buffer); |
47 std::string log(buffer, length); | 56 std::string log(buffer, length); |
48 DLOG(ERROR) << "Error compiling shader: " << log; | 57 DLOG(ERROR) << "Error compiling shader: " << log; |
49 DLOG(ERROR) << "Shader compilation failure."; | 58 DLOG(ERROR) << "Shader compilation failure."; |
50 } | 59 } |
51 #endif | 60 #endif |
52 } | 61 } |
53 | 62 |
54 } // namespace | 63 } // namespace |
55 | 64 |
56 namespace gpu { | 65 namespace gpu { |
57 namespace gles2 { | 66 namespace gles2 { |
58 | 67 |
59 ClearFramebufferResourceManager::ClearFramebufferResourceManager( | 68 ClearFramebufferResourceManager::ClearFramebufferResourceManager( |
60 const gles2::GLES2Decoder* decoder) | 69 const gles2::GLES2Decoder* decoder, |
61 : initialized_(false), program_(0u), buffer_id_(0u) { | 70 const gl::GLVersionInfo& gl_version_info) |
71 : initialized_(false), | |
72 is_desktop_core_profile_(gl_version_info.is_desktop_core_profile), | |
73 program_(0u), | |
74 vao_(0), | |
Zhenyao Mo
2017/03/23 17:11:40
nit: 0u to be consistent with above and below
| |
75 buffer_id_(0u) { | |
62 Initialize(decoder); | 76 Initialize(decoder); |
63 } | 77 } |
64 | 78 |
65 ClearFramebufferResourceManager::~ClearFramebufferResourceManager() { | 79 ClearFramebufferResourceManager::~ClearFramebufferResourceManager() { |
66 Destroy(); | 80 Destroy(); |
67 DCHECK(!buffer_id_); | 81 DCHECK(!buffer_id_); |
68 } | 82 } |
69 | 83 |
70 void ClearFramebufferResourceManager::Initialize( | 84 void ClearFramebufferResourceManager::Initialize( |
71 const gles2::GLES2Decoder* decoder) { | 85 const gles2::GLES2Decoder* decoder) { |
72 static_assert( | 86 static_assert( |
73 kVertexPositionAttrib == 0u, | 87 kVertexPositionAttrib == 0u, |
74 "kVertexPositionAttrib must be 0"); | 88 "kVertexPositionAttrib must be 0"); |
75 DCHECK(!buffer_id_); | 89 DCHECK(!buffer_id_); |
76 | 90 |
77 glGenBuffersARB(1, &buffer_id_); | 91 glGenBuffersARB(1, &buffer_id_); |
78 glBindBuffer(GL_ARRAY_BUFFER, buffer_id_); | 92 glBindBuffer(GL_ARRAY_BUFFER, buffer_id_); |
79 const GLfloat kQuadVertices[] = {-1.0f, -1.0f, | 93 const GLfloat kQuadVertices[] = {-1.0f, -1.0f, |
80 1.0f, -1.0f, | 94 1.0f, -1.0f, |
81 1.0f, 1.0f, | 95 1.0f, 1.0f, |
82 -1.0f, 1.0f}; | 96 -1.0f, 1.0f}; |
83 glBufferData( | 97 glBufferData( |
84 GL_ARRAY_BUFFER, sizeof(kQuadVertices), kQuadVertices, GL_STATIC_DRAW); | 98 GL_ARRAY_BUFFER, sizeof(kQuadVertices), kQuadVertices, GL_STATIC_DRAW); |
99 | |
100 if (is_desktop_core_profile_) { | |
101 glGenVertexArraysOES(1, &vao_); | |
102 glBindVertexArrayOES(vao_); | |
103 glEnableVertexAttribArray(kVertexPositionAttrib); | |
104 glVertexAttribPointer(kVertexPositionAttrib, 2, GL_FLOAT, GL_FALSE, 0, 0); | |
105 decoder->RestoreAllAttributes(); | |
106 } | |
107 | |
85 decoder->RestoreBufferBindings(); | 108 decoder->RestoreBufferBindings(); |
86 initialized_ = true; | 109 initialized_ = true; |
87 } | 110 } |
88 | 111 |
89 void ClearFramebufferResourceManager::Destroy() { | 112 void ClearFramebufferResourceManager::Destroy() { |
90 if (!initialized_) | 113 if (!initialized_) |
91 return; | 114 return; |
92 | 115 |
93 glDeleteProgram(program_); | 116 glDeleteProgram(program_); |
117 if (vao_ != 0) { | |
118 glDeleteVertexArraysOES(1, &vao_); | |
119 vao_ = 0; | |
120 } | |
94 glDeleteBuffersARB(1, &buffer_id_); | 121 glDeleteBuffersARB(1, &buffer_id_); |
95 buffer_id_ = 0; | 122 buffer_id_ = 0; |
96 } | 123 } |
97 | 124 |
98 void ClearFramebufferResourceManager::ClearFramebuffer( | 125 void ClearFramebufferResourceManager::ClearFramebuffer( |
99 const gles2::GLES2Decoder* decoder, | 126 const gles2::GLES2Decoder* decoder, |
100 const gfx::Size& framebuffer_size, | 127 const gfx::Size& framebuffer_size, |
101 GLbitfield mask, | 128 GLbitfield mask, |
102 GLfloat clear_color_red, | 129 GLfloat clear_color_red, |
103 GLfloat clear_color_green, | 130 GLfloat clear_color_green, |
104 GLfloat clear_color_blue, | 131 GLfloat clear_color_blue, |
105 GLfloat clear_color_alpha, | 132 GLfloat clear_color_alpha, |
106 GLfloat clear_depth_value, | 133 GLfloat clear_depth_value, |
107 GLint clear_stencil_value) { | 134 GLint clear_stencil_value) { |
108 if (!initialized_) { | 135 if (!initialized_) { |
109 DLOG(ERROR) << "Uninitialized manager."; | 136 DLOG(ERROR) << "Uninitialized manager."; |
110 return; | 137 return; |
111 } | 138 } |
112 | 139 |
113 if (!program_) { | 140 if (!program_) { |
114 program_ = glCreateProgram(); | 141 program_ = glCreateProgram(); |
115 GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER); | 142 GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER); |
116 CompileShader(vertex_shader, g_vertex_shader_source); | 143 CompileShader(vertex_shader, g_vertex_shader_source, |
144 is_desktop_core_profile_); | |
117 glAttachShader(program_, vertex_shader); | 145 glAttachShader(program_, vertex_shader); |
118 GLuint fragment_shader = glCreateShader(GL_FRAGMENT_SHADER); | 146 GLuint fragment_shader = glCreateShader(GL_FRAGMENT_SHADER); |
119 CompileShader(fragment_shader, g_fragment_shader_source); | 147 CompileShader(fragment_shader, g_fragment_shader_source, |
148 is_desktop_core_profile_); | |
120 glAttachShader(program_, fragment_shader); | 149 glAttachShader(program_, fragment_shader); |
121 glBindAttribLocation(program_, kVertexPositionAttrib, "a_position"); | 150 glBindAttribLocation(program_, kVertexPositionAttrib, "a_position"); |
122 glLinkProgram(program_); | 151 glLinkProgram(program_); |
123 #if DCHECK_IS_ON() | 152 #if DCHECK_IS_ON() |
124 GLint linked = GL_FALSE; | 153 GLint linked = GL_FALSE; |
125 glGetProgramiv(program_, GL_LINK_STATUS, &linked); | 154 glGetProgramiv(program_, GL_LINK_STATUS, &linked); |
126 if (GL_TRUE != linked) | 155 if (GL_TRUE != linked) |
127 DLOG(ERROR) << "Program link failure."; | 156 DLOG(ERROR) << "Program link failure."; |
128 #endif | 157 #endif |
129 depth_handle_ = glGetUniformLocation(program_, "u_clear_depth"); | 158 depth_handle_ = glGetUniformLocation(program_, "u_clear_depth"); |
130 color_handle_ = glGetUniformLocation(program_, "u_clear_color"); | 159 color_handle_ = glGetUniformLocation(program_, "u_clear_color"); |
131 glDeleteShader(fragment_shader); | 160 glDeleteShader(fragment_shader); |
132 glDeleteShader(vertex_shader); | 161 glDeleteShader(vertex_shader); |
133 } | 162 } |
134 glUseProgram(program_); | 163 glUseProgram(program_); |
135 | 164 |
136 #if DCHECK_IS_ON() | 165 #if DCHECK_IS_ON() |
137 glValidateProgram(program_); | 166 glValidateProgram(program_); |
138 GLint validation_status = GL_FALSE; | 167 GLint validation_status = GL_FALSE; |
139 glGetProgramiv(program_, GL_VALIDATE_STATUS, &validation_status); | 168 glGetProgramiv(program_, GL_VALIDATE_STATUS, &validation_status); |
140 if (GL_TRUE != validation_status) | 169 if (GL_TRUE != validation_status) |
141 DLOG(ERROR) << "Invalid shader."; | 170 DLOG(ERROR) << "Invalid shader."; |
142 #endif | 171 #endif |
143 | 172 |
144 decoder->ClearAllAttributes(); | 173 if (vao_) { |
145 glEnableVertexAttribArray(kVertexPositionAttrib); | 174 glBindVertexArrayOES(vao_); |
175 } else { | |
176 decoder->ClearAllAttributes(); | |
177 glEnableVertexAttribArray(kVertexPositionAttrib); | |
146 | 178 |
147 glBindBuffer(GL_ARRAY_BUFFER, buffer_id_); | 179 glBindBuffer(GL_ARRAY_BUFFER, buffer_id_); |
148 glVertexAttribPointer(kVertexPositionAttrib, 2, GL_FLOAT, GL_FALSE, 0, 0); | 180 glVertexAttribPointer(kVertexPositionAttrib, 2, GL_FLOAT, GL_FALSE, 0, 0); |
181 } | |
149 | 182 |
150 glUniform1f(depth_handle_, clear_depth_value); | 183 glUniform1f(depth_handle_, clear_depth_value); |
151 glUniform4f(color_handle_, clear_color_red, clear_color_green, | 184 glUniform4f(color_handle_, clear_color_red, clear_color_green, |
152 clear_color_blue, clear_color_alpha); | 185 clear_color_blue, clear_color_alpha); |
153 | 186 |
154 if (!(mask & GL_COLOR_BUFFER_BIT)) { | 187 if (!(mask & GL_COLOR_BUFFER_BIT)) { |
155 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); | 188 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); |
156 } | 189 } |
157 | 190 |
158 if (mask & GL_DEPTH_BUFFER_BIT) { | 191 if (mask & GL_DEPTH_BUFFER_BIT) { |
(...skipping 14 matching lines...) Expand all Loading... | |
173 glStencilMask(0); | 206 glStencilMask(0); |
174 } | 207 } |
175 | 208 |
176 glDisable(GL_CULL_FACE); | 209 glDisable(GL_CULL_FACE); |
177 glDisable(GL_BLEND); | 210 glDisable(GL_BLEND); |
178 glDisable(GL_POLYGON_OFFSET_FILL); | 211 glDisable(GL_POLYGON_OFFSET_FILL); |
179 | 212 |
180 glViewport(0, 0, framebuffer_size.width(), framebuffer_size.height()); | 213 glViewport(0, 0, framebuffer_size.width(), framebuffer_size.height()); |
181 glDrawArrays(GL_TRIANGLE_FAN, 0, 4); | 214 glDrawArrays(GL_TRIANGLE_FAN, 0, 4); |
182 | 215 |
216 if (vao_ == 0) { | |
217 decoder->RestoreBufferBindings(); | |
218 } | |
183 decoder->RestoreAllAttributes(); | 219 decoder->RestoreAllAttributes(); |
184 decoder->RestoreProgramBindings(); | 220 decoder->RestoreProgramBindings(); |
185 decoder->RestoreBufferBindings(); | |
186 decoder->RestoreGlobalState(); | 221 decoder->RestoreGlobalState(); |
187 } | 222 } |
188 | 223 |
189 } // namespace gles2 | 224 } // namespace gles2 |
190 } // namespace gpu | 225 } // namespace gpu |
OLD | NEW |