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