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