OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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 "media/tools/shader_bench/gpu_painter.h" |
| 6 |
| 7 // Vertices for a full screen quad. |
| 8 static const float kVertices[8] = { |
| 9 -1.f, 1.f, |
| 10 -1.f, -1.f, |
| 11 1.f, 1.f, |
| 12 1.f, -1.f, |
| 13 }; |
| 14 |
| 15 // Texture Coordinates mapping the entire texture. |
| 16 static const float kTextureCoords[8] = { |
| 17 0, 0, |
| 18 0, 1, |
| 19 1, 0, |
| 20 1, 1, |
| 21 }; |
| 22 |
| 23 // Buffer size for compile errors. |
| 24 static const unsigned int kErrorSize = 4096; |
| 25 |
| 26 GPUPainter::GPUPainter() |
| 27 : context_(NULL) { |
| 28 } |
| 29 |
| 30 void GPUPainter::SetGLContext(gfx::GLContext* context) { |
| 31 context_ = context; |
| 32 } |
| 33 |
| 34 GLuint GPUPainter::LoadShader(gfx::GLContext* context, unsigned type, |
| 35 const char* shader_source) { |
| 36 GLuint shader = glCreateShader(type); |
| 37 glShaderSource(shader, 1, &shader_source, NULL); |
| 38 glCompileShader(shader); |
| 39 int result = GL_FALSE; |
| 40 glGetShaderiv(shader, GL_COMPILE_STATUS, &result); |
| 41 if (!result) { |
| 42 char log[kErrorSize]; |
| 43 int len; |
| 44 glGetShaderInfoLog(shader, kErrorSize - 1, &len, log); |
| 45 log[kErrorSize - 1] = 0; |
| 46 printf("Shader did not compile: %s\n", log); |
| 47 } |
| 48 return shader; |
| 49 } |
| 50 |
| 51 GLuint GPUPainter::CreateShaderProgram( |
| 52 gfx::GLContext* context, const char* vertex_shader_source, |
| 53 const char* fragment_shader_source) { |
| 54 |
| 55 // Create vertex and pixel shaders. |
| 56 GLuint vertex_shader = |
| 57 LoadShader(context, GL_VERTEX_SHADER, vertex_shader_source); |
| 58 GLuint fragment_shader = |
| 59 LoadShader(context, GL_FRAGMENT_SHADER, fragment_shader_source); |
| 60 |
| 61 // Create program and attach shaders. |
| 62 GLuint program = glCreateProgram(); |
| 63 glAttachShader(program, vertex_shader); |
| 64 glAttachShader(program, fragment_shader); |
| 65 glDeleteShader(vertex_shader); |
| 66 glDeleteShader(fragment_shader); |
| 67 glLinkProgram(program); |
| 68 int result = GL_FALSE; |
| 69 glGetProgramiv(program, GL_LINK_STATUS, &result); |
| 70 if (!result) { |
| 71 char log[kErrorSize]; |
| 72 int len; |
| 73 glGetProgramInfoLog(program, kErrorSize - 1, &len, log); |
| 74 log[kErrorSize - 1] = 0; |
| 75 printf("Program did not link: %s\n", log); |
| 76 } |
| 77 glUseProgram(program); |
| 78 |
| 79 // Set common vertex parameters. |
| 80 int pos_location = glGetAttribLocation(program, "in_pos"); |
| 81 glEnableVertexAttribArray(pos_location); |
| 82 glVertexAttribPointer(pos_location, 2, GL_FLOAT, GL_FALSE, 0, kVertices); |
| 83 |
| 84 int tc_location = glGetAttribLocation(program, "in_tc"); |
| 85 glEnableVertexAttribArray(tc_location); |
| 86 glVertexAttribPointer(tc_location, 2, GL_FLOAT, GL_FALSE, 0, |
| 87 kTextureCoords); |
| 88 return program; |
| 89 } |
OLD | NEW |