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
3 // found in the LICENSE file.
5 #include "gpu/command_buffer/service/gles2_cmd_clear_framebuffer.h"
7 #include "base/basictypes.h"
8 #include "gpu/command_buffer/service/gl_utils.h"
9 #include "gpu/command_buffer/service/gles2_cmd_decoder.h"
15 "precision mediump float;\n" \
18 const char* g_vertex_shader_source
= {
20 uniform
float u_clear_depth
;
21 attribute vec4 a_position
;
23 gl_Position
= vec4(a_position
.x
, a_position
.y
, u_clear_depth
, 1.0);
28 const char* g_fragment_shader_source
= {
30 uniform vec4 u_clear_color
;
32 gl_FragColor
= u_clear_color
;
37 void CompileShader(GLuint shader
, const char* shader_source
) {
38 glShaderSource(shader
, 1, &shader_source
, 0);
39 glCompileShader(shader
);
41 GLint compile_status
= GL_FALSE
;
42 glGetShaderiv(shader
, GL_COMPILE_STATUS
, &compile_status
);
43 if (GL_TRUE
!= compile_status
) {
46 glGetShaderInfoLog(shader
, sizeof(buffer
), &length
, buffer
);
47 std::string
log(buffer
, length
);
48 DLOG(ERROR
) << "Error compiling shader: " << log
;
49 DLOG(ERROR
) << "Shader compilation failure.";
58 ClearFramebufferResourceManager::ClearFramebufferResourceManager(
59 const gles2::GLES2Decoder
* decoder
)
60 : initialized_(false), program_(0u), buffer_id_(0u) {
64 ClearFramebufferResourceManager::~ClearFramebufferResourceManager() {
69 void ClearFramebufferResourceManager::Initialize(
70 const gles2::GLES2Decoder
* decoder
) {
72 kVertexPositionAttrib
== 0u,
73 "kVertexPositionAttrib must be 0");
76 glGenBuffersARB(1, &buffer_id_
);
77 glBindBuffer(GL_ARRAY_BUFFER
, buffer_id_
);
78 const GLfloat kQuadVertices
[] = {-1.0f
, -1.0f
,
83 GL_ARRAY_BUFFER
, sizeof(kQuadVertices
), kQuadVertices
, GL_STATIC_DRAW
);
84 decoder
->RestoreBufferBindings();
88 void ClearFramebufferResourceManager::Destroy() {
92 glDeleteProgram(program_
);
93 glDeleteBuffersARB(1, &buffer_id_
);
97 void ClearFramebufferResourceManager::ClearFramebuffer(
98 const gles2::GLES2Decoder
* decoder
,
99 const gfx::Size
& framebuffer_size
,
101 GLfloat clear_color_red
,
102 GLfloat clear_color_green
,
103 GLfloat clear_color_blue
,
104 GLfloat clear_color_alpha
,
105 GLfloat clear_depth_value
,
106 GLint clear_stencil_value
) {
108 DLOG(ERROR
) << "Uninitialized manager.";
113 program_
= glCreateProgram();
114 GLuint vertex_shader
= glCreateShader(GL_VERTEX_SHADER
);
115 CompileShader(vertex_shader
, g_vertex_shader_source
);
116 glAttachShader(program_
, vertex_shader
);
117 GLuint fragment_shader
= glCreateShader(GL_FRAGMENT_SHADER
);
118 CompileShader(fragment_shader
, g_fragment_shader_source
);
119 glAttachShader(program_
, fragment_shader
);
120 glBindAttribLocation(program_
, kVertexPositionAttrib
, "a_position");
121 glLinkProgram(program_
);
123 GLint linked
= GL_FALSE
;
124 glGetProgramiv(program_
, GL_LINK_STATUS
, &linked
);
125 if (GL_TRUE
!= linked
)
126 DLOG(ERROR
) << "Program link failure.";
128 depth_handle_
= glGetUniformLocation(program_
, "u_clear_depth");
129 color_handle_
= glGetUniformLocation(program_
, "u_clear_color");
130 glDeleteShader(fragment_shader
);
131 glDeleteShader(vertex_shader
);
133 glUseProgram(program_
);
136 glValidateProgram(program_
);
137 GLint validation_status
= GL_FALSE
;
138 glGetProgramiv(program_
, GL_VALIDATE_STATUS
, &validation_status
);
139 if (GL_TRUE
!= validation_status
)
140 DLOG(ERROR
) << "Invalid shader.";
143 decoder
->ClearAllAttributes();
144 glEnableVertexAttribArray(kVertexPositionAttrib
);
146 glBindBuffer(GL_ARRAY_BUFFER
, buffer_id_
);
147 glVertexAttribPointer(kVertexPositionAttrib
, 2, GL_FLOAT
, GL_FALSE
, 0, 0);
149 glUniform1f(depth_handle_
, clear_depth_value
);
150 glUniform4f(color_handle_
, clear_color_red
, clear_color_green
,
151 clear_color_blue
, clear_color_alpha
);
153 if (!(mask
& GL_COLOR_BUFFER_BIT
)) {
154 glColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
157 if (mask
& GL_DEPTH_BUFFER_BIT
) {
158 glEnable(GL_DEPTH_TEST
);
159 glDepthFunc(GL_ALWAYS
);
161 glDisable(GL_DEPTH_TEST
);
162 glDepthMask(GL_FALSE
);
165 if (mask
& GL_STENCIL_BUFFER_BIT
) {
166 glEnable(GL_STENCIL_TEST
);
167 glStencilFunc(GL_ALWAYS
, clear_stencil_value
, 0xFF);
168 glStencilOp(GL_KEEP
, GL_KEEP
, GL_REPLACE
);
170 glDisable(GL_STENCIL_TEST
);
171 glStencilOp(GL_KEEP
, GL_KEEP
, GL_KEEP
);
175 glDisable(GL_CULL_FACE
);
177 glDisable(GL_POLYGON_OFFSET_FILL
);
179 glViewport(0, 0, framebuffer_size
.width(), framebuffer_size
.height());
180 glDrawArrays(GL_TRIANGLE_FAN
, 0, 4);
182 decoder
->RestoreAllAttributes();
183 decoder
->RestoreProgramBindings();
184 decoder
->RestoreBufferBindings();
185 decoder
->RestoreGlobalState();