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

Side by Side Diff: cc/output/shader.cc

Issue 2617673002: The great shader refactor: Merge all vertex shaders (Closed)
Patch Set: Delete dead code Created 3 years, 11 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
« no previous file with comments | « cc/output/shader.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2011 The Chromium Authors. All rights reserved. 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 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 "cc/output/shader.h" 5 #include "cc/output/shader.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <vector> 10 #include <vector>
(...skipping 12 matching lines...) Expand all
23 "String passed to StripLambda must be at least 8 characters"); 23 "String passed to StripLambda must be at least 8 characters");
24 DCHECK_EQ(strncmp("[]() {", shader, 6), 0); 24 DCHECK_EQ(strncmp("[]() {", shader, 6), 0);
25 DCHECK_EQ(shader[size - 2], '}'); 25 DCHECK_EQ(shader[size - 2], '}');
26 return std::string(shader + 6, shader + size - 2); 26 return std::string(shader + 6, shader + size - 2);
27 } 27 }
28 28
29 // Shaders are passed in with lambda syntax, which tricks clang-format into 29 // Shaders are passed in with lambda syntax, which tricks clang-format into
30 // handling them correctly. StipLambda removes this. 30 // handling them correctly. StipLambda removes this.
31 #define SHADER0(Src) StripLambda(#Src) 31 #define SHADER0(Src) StripLambda(#Src)
32 32
33 #define HDR(x) \
34 do { \
35 header += x + std::string("\n"); \
36 } while (0)
37 #define SRC(x) \
38 do { \
39 source += std::string(" ") + x + std::string("\n"); \
40 } while (0)
41
33 using gpu::gles2::GLES2Interface; 42 using gpu::gles2::GLES2Interface;
34 43
35 namespace cc { 44 namespace cc {
36 45
37 namespace { 46 namespace {
38 47
39 static void GetProgramUniformLocations(GLES2Interface* context, 48 static void GetProgramUniformLocations(GLES2Interface* context,
40 unsigned program, 49 unsigned program,
41 size_t count, 50 size_t count,
42 const char** uniforms, 51 const char** uniforms,
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 } 170 }
162 171
163 VertexShaderBase::VertexShaderBase() {} 172 VertexShaderBase::VertexShaderBase() {}
164 173
165 void VertexShaderBase::Init(GLES2Interface* context, 174 void VertexShaderBase::Init(GLES2Interface* context,
166 unsigned program, 175 unsigned program,
167 int* base_uniform_index) { 176 int* base_uniform_index) {
168 std::vector<const char*> uniforms; 177 std::vector<const char*> uniforms;
169 std::vector<int> locations; 178 std::vector<int> locations;
170 179
171 if (has_tex_transform_) 180 switch (tex_coord_transform_) {
172 uniforms.push_back("texTransform"); 181 case TEX_COORD_TRANSFORM_NONE:
173 if (has_vertex_tex_transform_) 182 break;
174 uniforms.push_back("vertexTexTransform"); 183 case TEX_COORD_TRANSFORM_VEC4:
175 if (has_tex_matrix_) 184 case TEX_COORD_TRANSFORM_TRANSLATED_VEC4:
176 uniforms.push_back("texMatrix"); 185 uniforms.push_back("vertexTexTransform");
177 if (has_ya_uv_tex_scale_offset_) { 186 break;
187 case TEX_COORD_TRANSFORM_MATRIX:
188 uniforms.push_back("texMatrix");
189 break;
190 }
191 if (is_ya_uv_) {
178 uniforms.push_back("yaTexScale"); 192 uniforms.push_back("yaTexScale");
179 uniforms.push_back("yaTexOffset"); 193 uniforms.push_back("yaTexOffset");
180 uniforms.push_back("uvTexScale"); 194 uniforms.push_back("uvTexScale");
181 uniforms.push_back("uvTexOffset"); 195 uniforms.push_back("uvTexOffset");
182 } 196 }
183 if (has_matrix_) 197 if (has_matrix_)
184 uniforms.push_back("matrix"); 198 uniforms.push_back("matrix");
185 if (has_vertex_opacity_) 199 if (has_vertex_opacity_)
186 uniforms.push_back("opacity"); 200 uniforms.push_back("opacity");
187 if (has_aa_) { 201 if (has_aa_) {
188 uniforms.push_back("viewport"); 202 uniforms.push_back("viewport");
189 uniforms.push_back("edge"); 203 uniforms.push_back("edge");
190 } 204 }
191 if (has_quad_) 205 if (position_source_ == POSITION_SOURCE_ATTRIBUTE_INDEXED_UNIFORM)
192 uniforms.push_back("quad"); 206 uniforms.push_back("quad");
193 207
194 locations.resize(uniforms.size()); 208 locations.resize(uniforms.size());
195 209
196 GetProgramUniformLocations(context, program, uniforms.size(), uniforms.data(), 210 GetProgramUniformLocations(context, program, uniforms.size(), uniforms.data(),
197 locations.data(), base_uniform_index); 211 locations.data(), base_uniform_index);
198 212
199 size_t index = 0; 213 size_t index = 0;
200 if (has_tex_transform_) 214 switch (tex_coord_transform_) {
201 tex_transform_location_ = locations[index++]; 215 case TEX_COORD_TRANSFORM_NONE:
202 if (has_vertex_tex_transform_) 216 break;
203 vertex_tex_transform_location_ = locations[index++]; 217 case TEX_COORD_TRANSFORM_VEC4:
204 if (has_tex_matrix_) 218 case TEX_COORD_TRANSFORM_TRANSLATED_VEC4:
205 tex_matrix_location_ = locations[index++]; 219 vertex_tex_transform_location_ = locations[index++];
206 if (has_ya_uv_tex_scale_offset_) { 220 break;
221 case TEX_COORD_TRANSFORM_MATRIX:
222 tex_matrix_location_ = locations[index++];
223 break;
224 }
225 if (is_ya_uv_) {
207 ya_tex_scale_location_ = locations[index++]; 226 ya_tex_scale_location_ = locations[index++];
208 ya_tex_offset_location_ = locations[index++]; 227 ya_tex_offset_location_ = locations[index++];
209 uv_tex_scale_location_ = locations[index++]; 228 uv_tex_scale_location_ = locations[index++];
210 uv_tex_offset_location_ = locations[index++]; 229 uv_tex_offset_location_ = locations[index++];
211 } 230 }
212 if (has_matrix_) 231 if (has_matrix_)
213 matrix_location_ = locations[index++]; 232 matrix_location_ = locations[index++];
214 if (has_vertex_opacity_) 233 if (has_vertex_opacity_)
215 vertex_opacity_location_ = locations[index++]; 234 vertex_opacity_location_ = locations[index++];
216 if (has_aa_) { 235 if (has_aa_) {
217 viewport_location_ = locations[index++]; 236 viewport_location_ = locations[index++];
218 edge_location_ = locations[index++]; 237 edge_location_ = locations[index++];
219 } 238 }
220 if (has_quad_) 239 if (position_source_ == POSITION_SOURCE_ATTRIBUTE_INDEXED_UNIFORM)
221 quad_location_ = locations[index++]; 240 quad_location_ = locations[index++];
222 } 241 }
223 242
224 void VertexShaderBase::FillLocations(ShaderLocations* locations) const { 243 void VertexShaderBase::FillLocations(ShaderLocations* locations) const {
225 locations->quad = quad_location(); 244 locations->quad = quad_location();
226 locations->edge = edge_location(); 245 locations->edge = edge_location();
227 locations->viewport = viewport_location(); 246 locations->viewport = viewport_location();
228 locations->matrix = matrix_location(); 247 locations->matrix = matrix_location();
229 locations->tex_transform = tex_transform_location(); 248 locations->vertex_tex_transform = vertex_tex_transform_location();
230 } 249 }
231 250
232 std::string VertexShaderBase::GetShaderString() const { 251 std::string VertexShaderBase::GetShaderString() const {
233 // We unconditionally use highp in the vertex shader since 252 // We unconditionally use highp in the vertex shader since
234 // we are unlikely to be vertex shader bound when drawing large quads. 253 // we are unlikely to be vertex shader bound when drawing large quads.
235 // Also, some vertex shaders mutate the texture coordinate in such a 254 // Also, some vertex shaders mutate the texture coordinate in such a
236 // way that the effective precision might be lower than expected. 255 // way that the effective precision might be lower than expected.
237 return base::StringPrintf( 256 std::string header = "#define TexCoordPrecision highp\n";
238 "#define TexCoordPrecision highp\n" 257 std::string source = "void main() {\n";
239 "#define NUM_STATIC_QUADS %d\n", 258
240 StaticGeometryBinding::NUM_QUADS) + 259 // Define the size of quads for attribute indexed uniform arrays.
241 GetShaderSource(); 260 if (use_uniform_arrays_) {
261 header += base::StringPrintf("#define NUM_QUADS %d\n",
262 StaticGeometryBinding::NUM_QUADS);
263 }
264
265 // Read the index variables.
266 if (use_uniform_arrays_ ||
267 position_source_ == POSITION_SOURCE_ATTRIBUTE_INDEXED_UNIFORM) {
268 HDR("attribute float a_index;");
269 SRC("// Compute indices for uniform arrays.");
270 SRC("int vertex_index = int(a_index);");
271 if (use_uniform_arrays_)
272 SRC("int quad_index = int(a_index * 0.25);");
273 SRC("");
274 }
275
276 // Read the position and compute gl_Position.
277 HDR("attribute TexCoordPrecision vec4 a_position;");
278 SRC("// Compute the position.");
279 switch (position_source_) {
280 case POSITION_SOURCE_ATTRIBUTE:
281 SRC("vec4 pos = a_position;");
282 break;
283 case POSITION_SOURCE_ATTRIBUTE_INDEXED_UNIFORM:
284 HDR("uniform TexCoordPrecision vec2 quad[4];");
285 SRC("vec4 pos = vec4(quad[vertex_index], a_position.z, a_position.w);");
286 break;
287 }
288 if (has_matrix_) {
289 if (use_uniform_arrays_) {
290 HDR("uniform mat4 matrix[NUM_QUADS];");
291 SRC("gl_Position = matrix[quad_index] * pos;");
292 } else {
293 HDR("uniform mat4 matrix;");
294 SRC("gl_Position = matrix * pos;");
295 }
296 } else {
297 SRC("gl_Position = pos;");
298 }
299
300 // Compute the anti-aliasing edge distances.
301 if (has_aa_) {
302 HDR("uniform TexCoordPrecision vec3 edge[8];");
303 HDR("uniform vec4 viewport;");
304 HDR("varying TexCoordPrecision vec4 edge_dist[2]; // 8 edge distances.");
305 SRC("// Compute anti-aliasing properties.\n");
306 SRC("vec2 ndc_pos = 0.5 * (1.0 + gl_Position.xy / gl_Position.w);");
307 SRC("vec3 screen_pos = vec3(viewport.xy + viewport.zw * ndc_pos, 1.0);");
308 SRC("edge_dist[0] = vec4(dot(edge[0], screen_pos),");
309 SRC(" dot(edge[1], screen_pos),");
310 SRC(" dot(edge[2], screen_pos),");
311 SRC(" dot(edge[3], screen_pos)) * gl_Position.w;");
312 SRC("edge_dist[1] = vec4(dot(edge[4], screen_pos),");
313 SRC(" dot(edge[5], screen_pos),");
314 SRC(" dot(edge[6], screen_pos),");
315 SRC(" dot(edge[7], screen_pos)) * gl_Position.w;");
316 }
317
318 // Read, transform, and write texture coordinates.
319 if (tex_coord_source_ != TEX_COORD_SOURCE_NONE) {
320 if (is_ya_uv_) {
321 HDR("varying TexCoordPrecision vec2 v_uvTexCoord;");
322 HDR("varying TexCoordPrecision vec2 v_yaTexCoord;");
323 } else {
324 HDR("varying TexCoordPrecision vec2 v_texCoord;");
325 }
326
327 SRC("// Compute texture coordinates.");
328 // Read coordinates.
329 switch (tex_coord_source_) {
330 case TEX_COORD_SOURCE_NONE:
331 break;
332 case TEX_COORD_SOURCE_POSITION:
333 SRC("vec2 texCoord = pos.xy;");
334 break;
335 case TEX_COORD_SOURCE_ATTRIBUTE:
336 HDR("attribute TexCoordPrecision vec2 a_texCoord;");
337 SRC("vec2 texCoord = a_texCoord;");
338 break;
339 }
340 // Transform coordinates (except YUV).
341 switch (tex_coord_transform_) {
342 case TEX_COORD_TRANSFORM_NONE:
343 break;
344 case TEX_COORD_TRANSFORM_TRANSLATED_VEC4:
345 SRC("texCoord = texCoord + vec2(0.5);");
346 // Fall through...
347 case TEX_COORD_TRANSFORM_VEC4:
348 if (use_uniform_arrays_) {
349 HDR("uniform TexCoordPrecision vec4 vertexTexTransform[NUM_QUADS];");
350 SRC("TexCoordPrecision vec4 texTrans =");
351 SRC(" vertexTexTransform[quad_index];");
352 SRC("texCoord = texCoord * texTrans.zw + texTrans.xy;");
353 } else {
354 HDR("uniform TexCoordPrecision vec4 vertexTexTransform;");
355 SRC("texCoord = texCoord * vertexTexTransform.zw +");
356 SRC(" vertexTexTransform.xy;");
357 }
358 break;
359 case TEX_COORD_TRANSFORM_MATRIX:
360 HDR("uniform TexCoordPrecision mat4 texMatrix;");
361 SRC("texCoord = (texMatrix * vec4(texCoord.xy, 0.0, 1.0)).xy;");
362 break;
363 }
364 // Write the output texture coordinates.
365 if (is_ya_uv_) {
366 HDR("uniform TexCoordPrecision vec2 uvTexOffset;");
367 HDR("uniform TexCoordPrecision vec2 uvTexScale;");
368 HDR("uniform TexCoordPrecision vec2 yaTexOffset;");
369 HDR("uniform TexCoordPrecision vec2 yaTexScale;");
370 SRC("v_yaTexCoord = texCoord * yaTexScale + yaTexOffset;");
371 SRC("v_uvTexCoord = texCoord * uvTexScale + uvTexOffset;");
372 } else {
373 SRC("v_texCoord = texCoord;");
374 }
375 }
376
377 // Write varying vertex opacity.
378 if (has_vertex_opacity_) {
379 DCHECK(use_uniform_arrays_);
380 HDR("uniform float opacity[NUM_QUADS * 4];");
381 HDR("varying float v_alpha;");
382 SRC("v_alpha = opacity[quad_index];");
383 }
384
385 // Add cargo-culted dummy variables for Android.
386 if (has_dummy_variables_) {
387 HDR("uniform TexCoordPrecision vec2 dummy_uniform;");
388 HDR("varying TexCoordPrecision vec2 dummy_varying;");
389 SRC("dummy_varying = dummy_uniform;");
390 }
391
392 source += "}\n";
393 return header + source;
242 } 394 }
243 395
244 std::string VertexShaderPosTex::GetShaderSource() const {
245 return SHADER0([]() {
246 attribute vec4 a_position;
247 attribute TexCoordPrecision vec2 a_texCoord;
248 uniform mat4 matrix;
249 varying TexCoordPrecision vec2 v_texCoord;
250 void main() {
251 gl_Position = matrix * a_position;
252 v_texCoord = a_texCoord;
253 }
254 });
255 }
256
257 std::string VertexShaderPosTexYUVStretchOffset::GetShaderSource() const {
258 return SHADER0([]() {
259 precision mediump float;
260 attribute vec4 a_position;
261 attribute TexCoordPrecision vec2 a_texCoord;
262 uniform mat4 matrix;
263 varying TexCoordPrecision vec2 v_yaTexCoord;
264 varying TexCoordPrecision vec2 v_uvTexCoord;
265 uniform TexCoordPrecision vec2 yaTexScale;
266 uniform TexCoordPrecision vec2 yaTexOffset;
267 uniform TexCoordPrecision vec2 uvTexScale;
268 uniform TexCoordPrecision vec2 uvTexOffset;
269 void main() {
270 gl_Position = matrix * a_position;
271 v_yaTexCoord = a_texCoord * yaTexScale + yaTexOffset;
272 v_uvTexCoord = a_texCoord * uvTexScale + uvTexOffset;
273 }
274 });
275 }
276
277 std::string VertexShaderPos::GetShaderSource() const {
278 return SHADER0([]() {
279 attribute vec4 a_position;
280 uniform mat4 matrix;
281 void main() { gl_Position = matrix * a_position; }
282 });
283 }
284
285 std::string VertexShaderPosTexTransform::GetShaderSource() const {
286 return SHADER0([]() {
287 attribute vec4 a_position;
288 attribute TexCoordPrecision vec2 a_texCoord;
289 attribute float a_index;
290 uniform mat4 matrix[NUM_STATIC_QUADS];
291 uniform TexCoordPrecision vec4 texTransform[NUM_STATIC_QUADS];
292 uniform float opacity[NUM_STATIC_QUADS * 4];
293 varying TexCoordPrecision vec2 v_texCoord;
294 varying float v_alpha;
295 void main() {
296 int quad_index = int(a_index * 0.25); // NOLINT
297 gl_Position = matrix[quad_index] * a_position;
298 TexCoordPrecision vec4 texTrans = texTransform[quad_index];
299 v_texCoord = a_texCoord * texTrans.zw + texTrans.xy;
300 v_alpha = opacity[int(a_index)]; // NOLINT
301 }
302 });
303 }
304
305 std::string VertexShaderPosTexIdentity::GetShaderSource() const {
306 return SHADER0([]() {
307 attribute vec4 a_position;
308 varying TexCoordPrecision vec2 v_texCoord;
309 void main() {
310 gl_Position = a_position;
311 v_texCoord = (a_position.xy + vec2(1.0)) * 0.5;
312 }
313 });
314 }
315
316 std::string VertexShaderQuad::GetShaderSource() const {
317 #if defined(OS_ANDROID)
318 // TODO(epenner): Find the cause of this 'quad' uniform
319 // being missing if we don't add dummy variables.
320 // http://crbug.com/240602
321 return SHADER0([]() {
322 attribute TexCoordPrecision vec4 a_position;
323 attribute float a_index;
324 uniform mat4 matrix;
325 uniform TexCoordPrecision vec2 quad[4];
326 uniform TexCoordPrecision vec2 dummy_uniform;
327 varying TexCoordPrecision vec2 dummy_varying;
328 void main() {
329 vec2 pos = quad[int(a_index)]; // NOLINT
330 gl_Position = matrix * vec4(pos, a_position.z, a_position.w);
331 dummy_varying = dummy_uniform;
332 }
333 });
334 #else
335 return SHADER0([]() {
336 attribute TexCoordPrecision vec4 a_position;
337 attribute float a_index;
338 uniform mat4 matrix;
339 uniform TexCoordPrecision vec2 quad[4];
340 void main() {
341 vec2 pos = quad[int(a_index)]; // NOLINT
342 gl_Position = matrix * vec4(pos, a_position.z, a_position.w);
343 }
344 });
345 #endif
346 }
347
348 std::string VertexShaderQuadAA::GetShaderSource() const {
349 return SHADER0([]() {
350 attribute TexCoordPrecision vec4 a_position;
351 attribute float a_index;
352 uniform mat4 matrix;
353 uniform vec4 viewport;
354 uniform TexCoordPrecision vec2 quad[4];
355 uniform TexCoordPrecision vec3 edge[8];
356 varying TexCoordPrecision vec4 edge_dist[2]; // 8 edge distances.
357 void main() {
358 vec2 pos = quad[int(a_index)]; // NOLINT
359 gl_Position = matrix * vec4(pos, a_position.z, a_position.w);
360 vec2 ndc_pos = 0.5 * (1.0 + gl_Position.xy / gl_Position.w);
361 vec3 screen_pos = vec3(viewport.xy + viewport.zw * ndc_pos, 1.0);
362 edge_dist[0] = vec4(dot(edge[0], screen_pos), dot(edge[1], screen_pos),
363 dot(edge[2], screen_pos), dot(edge[3], screen_pos)) *
364 gl_Position.w;
365 edge_dist[1] = vec4(dot(edge[4], screen_pos), dot(edge[5], screen_pos),
366 dot(edge[6], screen_pos), dot(edge[7], screen_pos)) *
367 gl_Position.w;
368 }
369 });
370 }
371
372 std::string VertexShaderQuadTexTransformAA::GetShaderSource() const {
373 return SHADER0([]() {
374 attribute TexCoordPrecision vec4 a_position;
375 attribute float a_index;
376 uniform mat4 matrix;
377 uniform vec4 viewport;
378 uniform TexCoordPrecision vec2 quad[4];
379 uniform TexCoordPrecision vec3 edge[8];
380 uniform TexCoordPrecision vec4 texTransform;
381 varying TexCoordPrecision vec2 v_texCoord;
382 varying TexCoordPrecision vec4 edge_dist[2]; // 8 edge distances.
383 void main() {
384 vec2 pos = quad[int(a_index)]; // NOLINT
385 gl_Position = matrix * vec4(pos, a_position.z, a_position.w);
386 vec2 ndc_pos = 0.5 * (1.0 + gl_Position.xy / gl_Position.w);
387 vec3 screen_pos = vec3(viewport.xy + viewport.zw * ndc_pos, 1.0);
388 edge_dist[0] = vec4(dot(edge[0], screen_pos), dot(edge[1], screen_pos),
389 dot(edge[2], screen_pos), dot(edge[3], screen_pos)) *
390 gl_Position.w;
391 edge_dist[1] = vec4(dot(edge[4], screen_pos), dot(edge[5], screen_pos),
392 dot(edge[6], screen_pos), dot(edge[7], screen_pos)) *
393 gl_Position.w;
394 v_texCoord = (pos.xy + vec2(0.5)) * texTransform.zw + texTransform.xy;
395 }
396 });
397 }
398
399 std::string VertexShaderTile::GetShaderSource() const {
400 return SHADER0([]() {
401 attribute TexCoordPrecision vec4 a_position;
402 attribute TexCoordPrecision vec2 a_texCoord;
403 attribute float a_index;
404 uniform mat4 matrix;
405 uniform TexCoordPrecision vec2 quad[4];
406 uniform TexCoordPrecision vec4 vertexTexTransform;
407 varying TexCoordPrecision vec2 v_texCoord;
408 void main() {
409 vec2 pos = quad[int(a_index)]; // NOLINT
410 gl_Position = matrix * vec4(pos, a_position.z, a_position.w);
411 v_texCoord = a_texCoord * vertexTexTransform.zw + vertexTexTransform.xy;
412 }
413 });
414 }
415
416 std::string VertexShaderTileAA::GetShaderSource() const {
417 return SHADER0([]() {
418 attribute TexCoordPrecision vec4 a_position;
419 attribute float a_index;
420 uniform mat4 matrix;
421 uniform vec4 viewport;
422 uniform TexCoordPrecision vec2 quad[4];
423 uniform TexCoordPrecision vec3 edge[8];
424 uniform TexCoordPrecision vec4 vertexTexTransform;
425 varying TexCoordPrecision vec2 v_texCoord;
426 varying TexCoordPrecision vec4 edge_dist[2]; // 8 edge distances.
427 void main() {
428 vec2 pos = quad[int(a_index)]; // NOLINT
429 gl_Position = matrix * vec4(pos, a_position.z, a_position.w);
430 vec2 ndc_pos = 0.5 * (1.0 + gl_Position.xy / gl_Position.w);
431 vec3 screen_pos = vec3(viewport.xy + viewport.zw * ndc_pos, 1.0);
432 edge_dist[0] = vec4(dot(edge[0], screen_pos), dot(edge[1], screen_pos),
433 dot(edge[2], screen_pos), dot(edge[3], screen_pos)) *
434 gl_Position.w;
435 edge_dist[1] = vec4(dot(edge[4], screen_pos), dot(edge[5], screen_pos),
436 dot(edge[6], screen_pos), dot(edge[7], screen_pos)) *
437 gl_Position.w;
438 v_texCoord = pos.xy * vertexTexTransform.zw + vertexTexTransform.xy;
439 }
440 });
441 }
442
443 std::string VertexShaderVideoTransform::GetShaderSource() const {
444 return SHADER0([]() {
445 attribute vec4 a_position;
446 attribute TexCoordPrecision vec2 a_texCoord;
447 uniform mat4 matrix;
448 uniform TexCoordPrecision mat4 texMatrix;
449 varying TexCoordPrecision vec2 v_texCoord;
450 void main() {
451 gl_Position = matrix * a_position;
452 v_texCoord = (texMatrix * vec4(a_texCoord.xy, 0.0, 1.0)).xy;
453 }
454 });
455 }
456
457 #define BLEND_MODE_UNIFORMS "s_backdropTexture", \
458 "s_originalBackdropTexture", \
459 "backdropRect"
460 #define UNUSED_BLEND_MODE_UNIFORMS (!has_blend_mode() ? 3 : 0)
461 #define BLEND_MODE_SET_LOCATIONS(X, POS) \
462 if (has_blend_mode()) { \
463 DCHECK_LT(static_cast<size_t>(POS) + 2, arraysize(X)); \
464 backdrop_location_ = locations[POS]; \
465 original_backdrop_location_ = locations[POS + 1]; \
466 backdrop_rect_location_ = locations[POS + 2]; \
467 }
468
469 FragmentShaderBase::FragmentShaderBase() {} 396 FragmentShaderBase::FragmentShaderBase() {}
470 397
471 std::string FragmentShaderBase::GetShaderString(TexCoordPrecision precision, 398 std::string FragmentShaderBase::GetShaderString(TexCoordPrecision precision,
472 SamplerType sampler) const { 399 SamplerType sampler) const {
473 // The AA shader values will use TexCoordPrecision. 400 // The AA shader values will use TexCoordPrecision.
474 if (has_aa_ && precision == TEX_COORD_PRECISION_NA) 401 if (has_aa_ && precision == TEX_COORD_PRECISION_NA)
475 precision = TEX_COORD_PRECISION_MEDIUM; 402 precision = TEX_COORD_PRECISION_MEDIUM;
476 return SetFragmentTexCoordPrecision( 403 return SetFragmentTexCoordPrecision(
477 precision, SetFragmentSamplerType( 404 precision, SetFragmentSamplerType(
478 sampler, SetBlendModeFunctions(GetShaderSource()))); 405 sampler, SetBlendModeFunctions(GetShaderSource())));
(...skipping 389 matching lines...) Expand 10 before | Expand all | Expand 10 after
868 case BLEND_MODE_NONE: 795 case BLEND_MODE_NONE:
869 NOTREACHED(); 796 NOTREACHED();
870 } 797 }
871 return "result = vec4(1.0, 0.0, 0.0, 1.0);"; 798 return "result = vec4(1.0, 0.0, 0.0, 1.0);";
872 } 799 }
873 800
874 std::string FragmentShaderBase::GetShaderSource() const { 801 std::string FragmentShaderBase::GetShaderSource() const {
875 std::string header = "precision mediump float;\n"; 802 std::string header = "precision mediump float;\n";
876 std::string source = "void main() {\n"; 803 std::string source = "void main() {\n";
877 804
878 #define HDR(x) \
879 do { \
880 header += x + std::string("\n"); \
881 } while (0)
882 #define SRC(x) \
883 do { \
884 source += std::string(" ") + x + std::string("\n"); \
885 } while (0)
886
887 // Read the input into vec4 texColor. 805 // Read the input into vec4 texColor.
888 switch (input_color_type_) { 806 switch (input_color_type_) {
889 case INPUT_COLOR_SOURCE_RGBA_TEXTURE: 807 case INPUT_COLOR_SOURCE_RGBA_TEXTURE:
890 if (ignore_sampler_type_) 808 if (ignore_sampler_type_)
891 HDR("uniform sampler2D s_texture;"); 809 HDR("uniform sampler2D s_texture;");
892 else 810 else
893 HDR("uniform SamplerType s_texture;"); 811 HDR("uniform SamplerType s_texture;");
894 HDR("varying TexCoordPrecision vec2 v_texCoord;"); 812 HDR("varying TexCoordPrecision vec2 v_texCoord;");
895 if (has_rgba_fragment_tex_transform_) { 813 if (has_rgba_fragment_tex_transform_) {
896 HDR("uniform TexCoordPrecision vec4 fragmentTexTransform;"); 814 HDR("uniform TexCoordPrecision vec4 fragmentTexTransform;");
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
1006 break; 924 break;
1007 case FRAG_COLOR_MODE_APPLY_BLEND_MODE: 925 case FRAG_COLOR_MODE_APPLY_BLEND_MODE:
1008 if (has_mask_sampler_) 926 if (has_mask_sampler_)
1009 SRC("gl_FragColor = ApplyBlendMode(texColor, maskColor.w);"); 927 SRC("gl_FragColor = ApplyBlendMode(texColor, maskColor.w);");
1010 else 928 else
1011 SRC("gl_FragColor = ApplyBlendMode(texColor, 0.0);"); 929 SRC("gl_FragColor = ApplyBlendMode(texColor, 0.0);");
1012 break; 930 break;
1013 } 931 }
1014 source += "}\n"; 932 source += "}\n";
1015 933
1016 #undef HDR
1017 #undef SRC
1018
1019 return header + source; 934 return header + source;
1020 } 935 }
1021 936
1022 FragmentShaderYUVVideo::FragmentShaderYUVVideo() {} 937 FragmentShaderYUVVideo::FragmentShaderYUVVideo() {}
1023 938
1024 void FragmentShaderYUVVideo::SetFeatures(bool use_alpha_texture, 939 void FragmentShaderYUVVideo::SetFeatures(bool use_alpha_texture,
1025 bool use_nv12, 940 bool use_nv12,
1026 bool use_color_lut) { 941 bool use_color_lut) {
1027 use_alpha_texture_ = use_alpha_texture; 942 use_alpha_texture_ = use_alpha_texture;
1028 use_nv12_ = use_nv12; 943 use_nv12_ = use_nv12;
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
1162 max(uv_clamp_rect.xy, min(uv_clamp_rect.zw, v_uvTexCoord)); 1077 max(uv_clamp_rect.xy, min(uv_clamp_rect.zw, v_uvTexCoord));
1163 vec3 yuv = vec3(y_raw, GetUV(uv_clamped)); 1078 vec3 yuv = vec3(y_raw, GetUV(uv_clamped));
1164 gl_FragColor = vec4(yuv2rgb(yuv), 1.0) * GetAlpha(ya_clamped); 1079 gl_FragColor = vec4(yuv2rgb(yuv), 1.0) * GetAlpha(ya_clamped);
1165 } 1080 }
1166 }); 1081 });
1167 1082
1168 return head + functions; 1083 return head + functions;
1169 } 1084 }
1170 1085
1171 } // namespace cc 1086 } // namespace cc
OLDNEW
« no previous file with comments | « cc/output/shader.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698