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