1// Copyright (c) 2010 The Chromium OS 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 "main.h"
6#include "utils.h"
7#include "testbase.h"
8
9
10namespace glbench {
11
12class AttributeFetchShaderTest : public DrawElementsTestFunc {
13 public:
14  AttributeFetchShaderTest() {}
15  virtual ~AttributeFetchShaderTest() {}
16  virtual bool Run();
17  virtual const char* Name() const { return "attribute_fetch_shader"; }
18  virtual bool IsDrawTest() const { return false; }
19  virtual const char* Unit() const { return "mvtx_sec"; }
20
21 private:
22  DISALLOW_COPY_AND_ASSIGN(AttributeFetchShaderTest);
23};
24
25
26const char *simple_vertex_shader =
27"attribute vec4 c1;"
28"void main() {"
29"    gl_Position = c1;"
30"}";
31
32const char *simple_vertex_shader_2_attr =
33"attribute vec4 c1;"
34"attribute vec4 c2;"
35"void main() {"
36"    gl_Position = c1+c2;"
37"}";
38
39const char *simple_vertex_shader_4_attr =
40"attribute vec4 c1;"
41"attribute vec4 c2;"
42"attribute vec4 c3;"
43"attribute vec4 c4;"
44"void main() {"
45"    gl_Position = c1+c2+c3+c4;"
46"}";
47
48const char *simple_vertex_shader_8_attr =
49"attribute vec4 c1;"
50"attribute vec4 c2;"
51"attribute vec4 c3;"
52"attribute vec4 c4;"
53"attribute vec4 c5;"
54"attribute vec4 c6;"
55"attribute vec4 c7;"
56"attribute vec4 c8;"
57"void main() {"
58"    gl_Position = c1+c2+c3+c4+c5+c6+c7+c8;"
59"}";
60
61const char *simple_fragment_shader =
62"void main() {"
63"    gl_FragColor = vec4(0.5);"
64"}";
65
66GLuint AttributeFetchShaderProgram(int attribute_count,
67                                   GLuint vertex_buffers[]) {
68  const char *vertex_shader = NULL;
69  switch (attribute_count) {
70    case 1: vertex_shader = simple_vertex_shader; break;
71    case 2: vertex_shader = simple_vertex_shader_2_attr; break;
72    case 4: vertex_shader = simple_vertex_shader_4_attr; break;
73    case 8: vertex_shader = simple_vertex_shader_8_attr; break;
74    default: return 0;
75  }
76  GLuint program =
77    InitShaderProgram(vertex_shader, simple_fragment_shader);
78
79  for (int i = 0; i < attribute_count; i++) {
80    char attribute[] = "c_";
81    attribute[1] = '1' + i;
82    int attribute_index = glGetAttribLocation(program, attribute);
83    glBindBuffer(GL_ARRAY_BUFFER, vertex_buffers[i]);
84    glVertexAttribPointer(attribute_index, 2, GL_FLOAT, GL_FALSE, 0, NULL);
85    glEnableVertexAttribArray(attribute_index);
86  }
87
88  return program;
89}
90
91bool AttributeFetchShaderTest::Run() {
92  GLint width = 64;
93  GLint height = 64;
94
95  glViewport(0, 0, g_width, g_height);
96
97  GLfloat *vertices = NULL;
98  GLsizeiptr vertex_buffer_size = 0;
99  CreateLattice(&vertices, &vertex_buffer_size, 1.f / g_width, 1.f / g_height,
100                width, height);
101  GLuint vertex_buffer = SetupVBO(GL_ARRAY_BUFFER,
102                                  vertex_buffer_size, vertices);
103
104  GLushort *indices = NULL;
105  GLuint index_buffer = 0;
106  GLsizeiptr index_buffer_size = 0;
107
108  // Everything will be back-face culled.
109  count_ = CreateMesh(&indices, &index_buffer_size, width, height, 0);
110  index_buffer = SetupVBO(GL_ELEMENT_ARRAY_BUFFER,
111                          index_buffer_size, indices);
112
113  glEnable(GL_CULL_FACE);
114
115  GLuint vertex_buffers[8];
116  for (GLuint i = 0; i < sizeof(vertex_buffers)/sizeof(vertex_buffers[0]); i++)
117    vertex_buffers[i] = vertex_buffer;
118
119  GLuint program = AttributeFetchShaderProgram(1, vertex_buffers);
120  RunTest(this, "attribute_fetch_shader", count_, g_width, g_height, true);
121  glDeleteProgram(program);
122
123  program = AttributeFetchShaderProgram(2, vertex_buffers);
124  RunTest(this,
125          "attribute_fetch_shader_2_attr", count_, g_width, g_height, true);
126  glDeleteProgram(program);
127
128  program = AttributeFetchShaderProgram(4, vertex_buffers);
129  RunTest(this,
130          "attribute_fetch_shader_4_attr", count_, g_width, g_height, true);
131  glDeleteProgram(program);
132
133  program = AttributeFetchShaderProgram(8, vertex_buffers);
134  RunTest(this,
135          "attribute_fetch_shader_8_attr", count_, g_width, g_height, true);
136  glDeleteProgram(program);
137
138  glDeleteBuffers(1, &index_buffer);
139  delete[] indices;
140
141  glDeleteBuffers(1, &vertex_buffer);
142  delete[] vertices;
143  return true;
144}
145
146TestBase* GetAttributeFetchShaderTest() {
147  return new AttributeFetchShaderTest();
148}
149
150} // namespace glbench
151