• 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 VaryingsAndDdxyShaderTest : public DrawElementsTestFunc {
12  public:
VaryingsAndDdxyShaderTest()13   VaryingsAndDdxyShaderTest() {}
~VaryingsAndDdxyShaderTest()14   virtual ~VaryingsAndDdxyShaderTest() {}
15   virtual bool Run();
Name() const16   virtual const char* Name() const { return "varyings_ddx_shader"; }
Unit() const17   virtual const char* Unit() const { return "mpixels_sec"; }
18 
19  private:
20   DISALLOW_COPY_AND_ASSIGN(VaryingsAndDdxyShaderTest);
21 };
22 
23 const char* vertex_shader_1_varying =
24     "attribute vec4 c;"
25     "varying vec4 v1;"
26     "void main() {"
27     "  gl_Position = c;"
28     "  v1 = c;"
29     "}";
30 
31 const char* vertex_shader_2_varying =
32     "attribute vec4 c;"
33     "varying vec4 v1;"
34     "varying vec4 v2;"
35     "void main() {"
36     "  gl_Position = c;"
37     "  v1 = v2 = c/2.;"
38     "}";
39 
40 const char* vertex_shader_4_varying =
41     "attribute vec4 c;"
42     "varying vec4 v1;"
43     "varying vec4 v2;"
44     "varying vec4 v3;"
45     "varying vec4 v4;"
46     "void main() {"
47     "  gl_Position = c;"
48     "  v1 = v2 = v3 = v4 = c/4.;"
49     "}";
50 
51 const char* vertex_shader_8_varying =
52     "attribute vec4 c;"
53     "varying vec4 v1;"
54     "varying vec4 v2;"
55     "varying vec4 v3;"
56     "varying vec4 v4;"
57     "varying vec4 v5;"
58     "varying vec4 v6;"
59     "varying vec4 v7;"
60     "varying vec4 v8;"
61     "void main() {"
62     "  gl_Position = c;"
63     "  v1 = v2 = v3 = v4 = v5 = v6 = v7 = v8 = c/8.;"
64     "}";
65 
66 const char* fragment_shader_1_varying =
67     "varying vec4 v1;"
68     "void main() {"
69     "  gl_FragColor = v1;"
70     "}";
71 
72 const char* fragment_shader_2_varying =
73     "varying vec4 v1;"
74     "varying vec4 v2;"
75     "void main() {"
76     "  gl_FragColor = v1 + v2;"
77     "}";
78 
79 const char* fragment_shader_4_varying =
80     "varying vec4 v1;"
81     "varying vec4 v2;"
82     "varying vec4 v3;"
83     "varying vec4 v4;"
84     "void main() {"
85     "  gl_FragColor = v1 + v2 + v3 + v4;"
86     "}";
87 
88 const char* fragment_shader_8_varying =
89     "varying vec4 v1;"
90     "varying vec4 v2;"
91     "varying vec4 v3;"
92     "varying vec4 v4;"
93     "varying vec4 v5;"
94     "varying vec4 v6;"
95     "varying vec4 v7;"
96     "varying vec4 v8;"
97     "void main() {"
98     "  gl_FragColor = v1 + v2 + v3 + v4 + v5 + v6 + v7 + v8;"
99     "}";
100 
VaryingsShaderProgram(int varyings_count,GLuint vertex_buffer)101 GLuint VaryingsShaderProgram(int varyings_count, GLuint vertex_buffer) {
102   const char* vertex_shader = NULL;
103   const char* fragment_shader = NULL;
104   switch (varyings_count) {
105     case 1:
106       vertex_shader = vertex_shader_1_varying;
107       fragment_shader = fragment_shader_1_varying;
108       break;
109     case 2:
110       vertex_shader = vertex_shader_2_varying;
111       fragment_shader = fragment_shader_2_varying;
112       break;
113     case 4:
114       vertex_shader = vertex_shader_4_varying;
115       fragment_shader = fragment_shader_4_varying;
116       break;
117     case 8:
118       vertex_shader = vertex_shader_8_varying;
119       fragment_shader = fragment_shader_8_varying;
120       break;
121     default:
122       return 0;
123   }
124   GLuint program = InitShaderProgram(vertex_shader, fragment_shader);
125 
126   int attribute_index = glGetAttribLocation(program, "c");
127   glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
128   glVertexAttribPointer(attribute_index, 2, GL_FLOAT, GL_FALSE, 0, NULL);
129   glEnableVertexAttribArray(attribute_index);
130 
131   return program;
132 }
133 
134 const char* fragment_shader_ddx =
135     "#extension GL_OES_standard_derivatives : enable\n"
136     "varying vec4 v1;"
137     "void main() {"
138     "  gl_FragColor = vec4(dFdx(v1.x), 0., 0., 1.);"
139     "}";
140 
141 const char* fragment_shader_ddy =
142     "#extension GL_OES_standard_derivatives : enable\n"
143     "varying vec4 v1;"
144     "void main() {"
145     "  gl_FragColor = vec4(dFdy(v1.y), 0., 0., 1.);"
146     "}";
147 
DdxDdyShaderProgram(bool ddx,GLuint vertex_buffer)148 GLuint DdxDdyShaderProgram(bool ddx, GLuint vertex_buffer) {
149   GLuint program = InitShaderProgram(
150       vertex_shader_1_varying, ddx ? fragment_shader_ddx : fragment_shader_ddy);
151 
152   int attribute_index = glGetAttribLocation(program, "c");
153   glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
154   glVertexAttribPointer(attribute_index, 2, GL_FLOAT, GL_FALSE, 0, NULL);
155   glEnableVertexAttribArray(attribute_index);
156 
157   return program;
158 }
159 
Run()160 bool VaryingsAndDdxyShaderTest::Run() {
161   glViewport(0, 0, g_width, g_height);
162 
163   const int c = 4;
164   GLfloat* vertices = NULL;
165   GLsizeiptr vertex_buffer_size = 0;
166   CreateLattice(&vertices, &vertex_buffer_size, 1.f / c, 1.f / c, c, c);
167   GLuint vertex_buffer =
168       SetupVBO(GL_ARRAY_BUFFER, vertex_buffer_size, vertices);
169 
170   GLushort* indices = NULL;
171   GLuint index_buffer = 0;
172   GLsizeiptr index_buffer_size = 0;
173 
174   count_ = CreateMesh(&indices, &index_buffer_size, c, c, 0);
175   index_buffer = SetupVBO(GL_ELEMENT_ARRAY_BUFFER, index_buffer_size, indices);
176 
177   GLuint program = VaryingsShaderProgram(1, vertex_buffer);
178   RunTest(this, "varyings_shader_1", g_width * g_height, g_width, g_height,
179           true);
180   glDeleteProgram(program);
181 
182   program = VaryingsShaderProgram(2, vertex_buffer);
183   RunTest(this, "varyings_shader_2", g_width * g_height, g_width, g_height,
184           true);
185   glDeleteProgram(program);
186 
187   program = VaryingsShaderProgram(4, vertex_buffer);
188   RunTest(this, "varyings_shader_4", g_width * g_height, g_width, g_height,
189           true);
190   glDeleteProgram(program);
191 
192   program = VaryingsShaderProgram(8, vertex_buffer);
193   RunTest(this, "varyings_shader_8", g_width * g_height, g_width, g_height,
194           true);
195   glDeleteProgram(program);
196 
197 #if !defined(DISABLE_SOME_TESTS_FOR_INTEL_DRIVER)
198   program = DdxDdyShaderProgram(true, vertex_buffer);
199   RunTest(this, "ddx_shader", g_width * g_height, g_width, g_height, true);
200   glDeleteProgram(program);
201 
202   program = DdxDdyShaderProgram(false, vertex_buffer);
203   RunTest(this, "ddy_shader", g_width * g_height, g_width, g_height, true);
204   glDeleteProgram(program);
205 #endif
206 
207   glDeleteBuffers(1, &index_buffer);
208   delete[] indices;
209 
210   glDeleteBuffers(1, &vertex_buffer);
211   delete[] vertices;
212 
213   return true;
214 }
215 
GetVaryingsAndDdxyShaderTest()216 TestBase* GetVaryingsAndDdxyShaderTest() {
217   return new VaryingsAndDdxyShaderTest;
218 }
219 
220 }  // namespace glbench
221