Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(378)

Side by Side Diff: gpu/command_buffer/service/gles2_cmd_copy_texture_chromium.cc

Issue 9968113: Addition of GL_CHROMIUM_copy_texture extension. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Correcting glEnableVertexAttribArray state. Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 // Copyright (c) 2012 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
5 #include "gpu/command_buffer/service/gles2_cmd_copy_texture_chromium.h"
6 #include "gpu/command_buffer/common/types.h"
7 #include "gpu/command_buffer/service/gl_utils.h"
8
9 #define SHADER0(Src) #Src
10 #define SHADER(Src) SHADER0(Src)
11
12 namespace {
13
14 const GLfloat kQuadVertices[] = { -1.0f, -1.0f, 0.0f, 1.0f,
15 1.0f, -1.0f, 0.0f, 1.0f,
16 1.0f, 1.0f, 0.0f, 1.0f,
17 -1.0f, 1.0f, 0.0f, 1.0f };
18
19 const GLfloat kTextureCoords[] = { 0.0f, 0.0f,
20 1.0f, 0.0f,
21 1.0f, 1.0f,
22 0.0f, 1.0f };
23
24 const int kNumShaders = 5;
25 enum ShaderId {
26 VERTEX_SHADER_POS_TEX,
27 FRAGMENT_SHADER_TEX,
28 FRAGMENT_SHADER_TEX_FLIP_Y,
29 FRAGMENT_SHADER_TEX_PREMULTIPLY_ALPHA,
30 FRAGMENT_SHADER_TEX_PREMULTIPLY_ALPHA_FLIP_Y
31 };
32
33 enum ProgramId {
34 PROGRAM_COPY_TEXTURE,
35 PROGRAM_COPY_TEXTURE_FLIP_Y,
36 PROGRAM_COPY_TEXTURE_PREMULTIPLY_ALPHA,
37 PROGRAM_COPY_TEXTURE_PREMULTIPLY_ALPHA_FLIPY
38 };
39
40 // Returns the correct program to evaluate the copy operation for
41 // the CHROMIUM_flipy and premultiply alpha pixel store settings.
42 ProgramId GetProgram(bool flip_y, bool premultiply_alpha) {
43 if (flip_y && premultiply_alpha)
44 return PROGRAM_COPY_TEXTURE_PREMULTIPLY_ALPHA_FLIPY;
45
46 if (flip_y)
47 return PROGRAM_COPY_TEXTURE_FLIP_Y;
48
49 if (premultiply_alpha)
50 return PROGRAM_COPY_TEXTURE_PREMULTIPLY_ALPHA;
51
52 return PROGRAM_COPY_TEXTURE;
53 }
54
55 const char* GetShaderSource(ShaderId shader) {
56 switch (shader) {
57 case VERTEX_SHADER_POS_TEX:
58 return SHADER(
59 precision mediump float;
60 attribute vec4 a_position;
61 attribute vec2 a_texCoord;
62 varying vec2 v_uv;
63 void main(void) {
64 gl_Position = a_position;
65 v_uv = a_texCoord;
66 });
67 case FRAGMENT_SHADER_TEX:
68 return SHADER(
69 precision mediump float;
70 uniform sampler2D u_texSampler;
71 varying vec2 v_uv;
72 void main(void) {
73 gl_FragColor = texture2D(u_texSampler, v_uv.st);
74 });
75 case FRAGMENT_SHADER_TEX_FLIP_Y:
76 return SHADER(
77 precision mediump float;
78 uniform sampler2D u_texSampler;
79 varying vec2 v_uv;
80 void main(void) {
81 gl_FragColor = texture2D(u_texSampler, vec2(v_uv.s, 1.0 - v_uv.t));
82 });
83 case FRAGMENT_SHADER_TEX_PREMULTIPLY_ALPHA:
84 return SHADER(
85 precision mediump float;
86 uniform sampler2D u_texSampler;
87 varying vec2 v_uv;
88 void main(void) {
89 gl_FragColor = texture2D(u_texSampler, v_uv.st);
90 gl_FragColor.rgb *= gl_FragColor.a;
91 });
92 case FRAGMENT_SHADER_TEX_PREMULTIPLY_ALPHA_FLIP_Y:
93 return SHADER(
94 precision mediump float;
95 uniform sampler2D u_texSampler;
96 varying vec2 v_uv;
97 void main(void) {
98 gl_FragColor = texture2D(u_texSampler, vec2(v_uv.s, 1.0 - v_uv.t));
99 gl_FragColor.rgb *= gl_FragColor.a;
100 });
101 default:
102 return 0;
103 }
104 }
105
106 } // namespace
107
108 void CopyTextureCHROMIUMResourceManager::Initialize() {
109 // Initialize all of the GPU resources required to perform the copy.
110 glGenBuffersARB(2, buffer_ids_);
111 glBindBuffer(GL_ARRAY_BUFFER, buffer_ids_[0]);
112 glBufferData(GL_ARRAY_BUFFER, sizeof(kQuadVertices), kQuadVertices,
113 GL_STATIC_DRAW);
114
115 glBindBuffer(GL_ARRAY_BUFFER, buffer_ids_[1]);
116 glBufferData(GL_ARRAY_BUFFER, sizeof(kTextureCoords), kTextureCoords,
117 GL_STATIC_DRAW);
118
119 glGenFramebuffersEXT(1, &framebuffer_);
120
121 GLuint shaders[kNumShaders];
122 for (int shader = 0; shader < kNumShaders; ++shader) {
123 shaders[shader] = glCreateShader(
124 shader == 0 ? GL_VERTEX_SHADER : GL_FRAGMENT_SHADER);
125 const char* shader_source = GetShaderSource(static_cast<ShaderId>(shader));
126 glShaderSource(shaders[shader], 1, &shader_source, 0);
127 glCompileShader(shaders[shader]);
128 #ifndef NDEBUG
129 GLint compile_status;
130 glGetShaderiv(shaders[shader], GL_COMPILE_STATUS, &compile_status);
131 if (GL_TRUE != compile_status)
132 DLOG(ERROR) << "CopyTextureCHROMIUM: shader compilation failure.";
133 #endif
134 }
135
136 for (int program = 0; program < kNumPrograms; ++program) {
137 programs_[program] = glCreateProgram();
138 glAttachShader(programs_[program], shaders[0]);
139 glAttachShader(programs_[program], shaders[program + 1]);
140
141 glLinkProgram(programs_[program]);
142 #ifndef NDEBUG
143 GLint linked;
144 glGetProgramiv(programs_[program], GL_LINK_STATUS, &linked);
145 if (!linked)
146 DLOG(ERROR) << "CopyTextureCHROMIUM: program link failure.";
147 #endif
148
149 glBindAttribLocation(programs_[program], kVertexPositionAttrib,
greggman 2012/04/17 04:58:06 These BindAttribLocation statements need to come b
Jeff Timanus 2012/04/18 14:59:08 Done.
150 "a_position");
151 glBindAttribLocation(programs_[program], kVertexTextureAttrib,
152 "a_texCoord");
153 sampler_locations_[program] = glGetUniformLocation(programs_[program],
154 "u_texSampler");
155
156 }
157
158 for (int shader = 0; shader < kNumShaders; ++shader)
159 glDeleteShader(shaders[shader]);
160
161 initialized_ = true;
162 }
163
164 void CopyTextureCHROMIUMResourceManager::Destroy() {
165 if (!initialized_)
166 return;
167
168 glDeleteFramebuffersEXT(1, &framebuffer_);
169
170 for (int program = 0; program < kNumPrograms; ++program)
171 glDeleteProgram(programs_[program]);
172
173 glDeleteBuffersARB(2, buffer_ids_);
174 }
175
176 void CopyTextureCHROMIUMResourceManager::DoCopyTexture(
177 GLenum target,
178 GLuint source_id,
179 GLuint dest_id,
180 GLint level,
181 bool flip_y,
182 bool premultiply_alpha) {
183 if (!initialized_) {
184 DLOG(ERROR) << "CopyTextureCHROMIUM: Uninitialized manager.";
185 return;
186 }
187
188 GLuint program = GetProgram(flip_y, premultiply_alpha);
189 glUseProgram(programs_[program]);
190
191 #ifndef NDEBUG
192 glValidateProgram(programs_[program]);
193 GLint validation_status;
194 glGetProgramiv(programs_[program], GL_VALIDATE_STATUS, &validation_status);
195 if (GL_TRUE != validation_status) {
196 DLOG(ERROR) << "CopyTextureCHROMIUM: Invalid shader.";
197 return;
198 }
199 #endif
200
201 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framebuffer_);
202 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, target,
203 dest_id, level);
204
205 #ifndef NDEBUG
206 GLenum fb_status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER);
207 if (GL_FRAMEBUFFER_COMPLETE != fb_status) {
208 DLOG(ERROR) << "CopyTextureCHROMIUM: Incomplete framebuffer.";
209 return;
210 }
211 #endif
212
213 glEnableVertexAttribArray(kVertexPositionAttrib);
214 glEnableVertexAttribArray(kVertexTextureAttrib);
215
216 glBindBuffer(GL_ARRAY_BUFFER, buffer_ids_[0]);
217 glVertexAttribPointer(kVertexPositionAttrib, 4, GL_FLOAT, GL_FALSE,
218 4 * sizeof(GLfloat), 0);
219
220 glBindBuffer(GL_ARRAY_BUFFER, buffer_ids_[1]);
221 glVertexAttribPointer(kVertexTextureAttrib, 2, GL_FLOAT, GL_FALSE,
222 2 * sizeof(GLfloat), 0);
223
224 glActiveTexture(GL_TEXTURE0);
225 glUniform1i(sampler_locations_[program], 0);
226
227 glBindTexture(GL_TEXTURE_2D, source_id);
228 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
229 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
230 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
231 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
232
233 glDisable(GL_DEPTH_TEST);
234 glDisable(GL_SCISSOR_TEST);
235 glDisable(GL_STENCIL_TEST);
236 glDisable(GL_CULL_FACE);
237 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
238 glDepthMask(GL_FALSE);
239 glDisable(GL_BLEND);
240
241 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
242 }
243
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698