blob: 2bc676b5a16c57f24351c7589de37b08b73dded6 [file] [log] [blame]
Alexey Marinichev9c891ef2010-05-21 15:28:59 -07001// 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 "base/logging.h"
6#include "base/scoped_ptr.h"
7#include "base/string_util.h"
8
9#include "main.h"
10#include "testbase.h"
11#include "utils.h"
12
13namespace glbench {
14
15static const int kNumberOfTextures = 8;
16
17class TextureUpdateTest : public TestBase {
18 public:
19 TextureUpdateTest() {}
20 virtual ~TextureUpdateTest() {}
21 bool TestFunc(int iter);
22 virtual bool Run();
Alexey Marinichevf50ecb12010-06-14 15:21:41 -070023 virtual const char* Name() const { return "texture_update"; }
Alexey Marinichev9c891ef2010-05-21 15:28:59 -070024
25 enum UpdateFlavor {
26 TEX_IMAGE,
27 TEX_SUBIMAGE
28 };
29
30 private:
31 GLuint width_;
32 GLuint height_;
33 GLuint program_;
34 int texsize_;
35 scoped_array<char> pixels_[kNumberOfTextures];
36 UpdateFlavor flavor_;
37 DISALLOW_COPY_AND_ASSIGN(TextureUpdateTest);
38};
39
Alexey Marinicheve735df62010-05-24 15:52:07 -070040#if defined(I915_WORKAROUND)
Alexey Marinichev9c891ef2010-05-21 15:28:59 -070041#define V1 "gl_TexCoord[0]"
42#else
43#define V1 "v1"
44#endif
45
46static const char* kVertexShader =
47 "attribute vec4 c1;"
48 "attribute vec4 c2;"
49 "varying vec4 v1;"
50 "void main() {"
51 " gl_Position = c1;"
52 V1 "= c2;"
53 "}";
54
55static const char* kFragmentShader =
56 "varying vec4 v1;"
57 "uniform sampler2D texture;"
58 "void main() {"
59 " gl_FragColor = texture2D(texture, " V1 ".xy);"
60 "}";
61
62bool TextureUpdateTest::TestFunc(int iter) {
63 glGetError();
64 glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, width_, height_,
65 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, NULL);
66 if (glGetError() != 0) {
67 printf("# Failed to allocate %dx%d texture.\n", width_, height_);
68 return false;
69 }
70
71 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
72 glFlush();
73 for (int i = 0; i < iter-1; ++i) {
74 switch (flavor_) {
75 case TEX_IMAGE:
76 glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, width_, height_,
77 0, GL_LUMINANCE, GL_UNSIGNED_BYTE,
78 pixels_[i % kNumberOfTextures].get());
79 break;
80 case TEX_SUBIMAGE:
81 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width_, height_,
82 GL_LUMINANCE, GL_UNSIGNED_BYTE,
83 pixels_[i % kNumberOfTextures].get());
84 break;
85 }
86 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
87 }
88 return true;
89}
90
91bool TextureUpdateTest::Run() {
92 // Two triangles that form one pixel at 0, 0.
93 GLfloat vertices[8] = {
94 0.f, 0.f,
95 2.f / g_width, 0.f,
96 0.f, 2.f / g_height,
97 2.f / g_width, 2.f / g_height,
98 };
99 GLfloat texcoords[8] = {
100 0.f, 0.f, 0.f, 0.f,
101 0.f, 0.f, 0.f, 0.f,
102 };
103
104 program_ = InitShaderProgram(kVertexShader, kFragmentShader);
105
106 int attr1 = glGetAttribLocation(program_, "c1");
107 glVertexAttribPointer(attr1, 2, GL_FLOAT, GL_FALSE, 0, vertices);
108 glEnableVertexAttribArray(attr1);
109
110 int attr2 = glGetAttribLocation(program_, "c2");
111 glVertexAttribPointer(attr2, 2, GL_FLOAT, GL_FALSE, 0, texcoords);
112 glEnableVertexAttribArray(attr2);
113
114 int texture_sampler = glGetUniformLocation(program_, "texture");
115 glUniform1i(texture_sampler, 0);
116 glActiveTexture(GL_TEXTURE0);
117
118 GLuint texname;
119 glGenTextures(1, &texname);
120 glBindTexture(GL_TEXTURE_2D, texname);
121 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
122 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
123
124
125 UpdateFlavor flavors[] = {TEX_IMAGE, TEX_SUBIMAGE};
Alexey Marinichevf50ecb12010-06-14 15:21:41 -0700126 const std::string flavor_names[] = {
127 "texture_update_teximage2d", "texture_update_texsubimage2d"
128 };
Alexey Marinichev9c891ef2010-05-21 15:28:59 -0700129 for (unsigned int f = 0; f < arraysize(flavors); f++) {
130 flavor_ = flavors[f];
131 int sizes[] = {32, 128, 256, 512, 768, 1024, 1536, 2048};
132 for (unsigned int i = 0; i < arraysize(sizes); i++) {
133 std::string name = "mtexel_sec_" + flavor_names[f] + "_" +
134 IntToString(sizes[i]);
135 width_ = height_ = sizes[i];
136 for (int i = 0; i < kNumberOfTextures; ++i)
137 pixels_[i].reset(new char[width_ * height_]);
138 RunTest(this, name.c_str(), width_ * height_, true);
139 }
140 }
141
142 glDeleteTextures(1, &texname);
143 glDeleteProgram(program_);
144 return true;
145}
146
147TestBase* GetTextureUpdateTest() {
148 return new TextureUpdateTest;
149}
150
151} // namespace glbench