OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <algorithm> | 5 #include <algorithm> |
6 #include <vector> | 6 #include <vector> |
7 | 7 |
8 #include "base/containers/small_map.h" | 8 #include "base/containers/small_map.h" |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
89 // PerfTest to check costs of texture upload at different stages | 89 // PerfTest to check costs of texture upload at different stages |
90 // on different platforms. | 90 // on different platforms. |
91 class TextureUploadPerfTest : public testing::Test { | 91 class TextureUploadPerfTest : public testing::Test { |
92 public: | 92 public: |
93 TextureUploadPerfTest() : size_(512, 512) {} | 93 TextureUploadPerfTest() : size_(512, 512) {} |
94 | 94 |
95 // Overridden from testing::Test | 95 // Overridden from testing::Test |
96 void SetUp() override { | 96 void SetUp() override { |
97 // Initialize an offscreen surface and a gl context. | 97 // Initialize an offscreen surface and a gl context. |
98 gfx::GLSurface::InitializeOneOff(); | 98 gfx::GLSurface::InitializeOneOff(); |
99 surface_ = gfx::GLSurface::CreateOffscreenGLSurface(size_); | 99 surface_ = gfx::GLSurface::CreateOffscreenGLSurface(gfx::Size(4, 4)); |
100 gl_context_ = gfx::GLContext::CreateGLContext(NULL, // share_group | 100 gl_context_ = gfx::GLContext::CreateGLContext(NULL, // share_group |
101 surface_.get(), | 101 surface_.get(), |
102 gfx::PreferIntegratedGpu); | 102 gfx::PreferIntegratedGpu); |
103 ui::ScopedMakeCurrent smc(gl_context_.get(), surface_.get()); | |
104 glGenTextures(1, &color_texture_); | |
105 glBindTexture(GL_TEXTURE_2D, color_texture_); | |
106 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | |
107 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | |
108 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | |
109 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | |
110 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, size_.width(), size_.height(), 0, | |
111 GL_RGBA, GL_UNSIGNED_BYTE, nullptr); | |
103 | 112 |
104 ui::ScopedMakeCurrent smc(gl_context_.get(), surface_.get()); | 113 glGenFramebuffersEXT(1, &framebuffer_object_); |
114 glBindFramebufferEXT(GL_FRAMEBUFFER, framebuffer_object_); | |
115 | |
116 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, | |
117 GL_TEXTURE_2D, color_texture_, 0); | |
118 DCHECK_EQ(static_cast<GLenum>(GL_FRAMEBUFFER_COMPLETE), | |
119 glCheckFramebufferStatusEXT(GL_FRAMEBUFFER)); | |
120 | |
121 glViewport(0, 0, size_.width(), size_.height()); | |
122 | |
105 if (gpu_timing_.Initialize(gl_context_.get())) { | 123 if (gpu_timing_.Initialize(gl_context_.get())) { |
106 LOG(INFO) << "Gpu timing initialized with timer type: " | 124 LOG(INFO) << "Gpu timing initialized with timer type: " |
107 << gpu_timing_.GetTimerTypeName(); | 125 << gpu_timing_.GetTimerTypeName(); |
108 gpu_timing_.CheckAndResetTimerErrors(); | 126 gpu_timing_.CheckAndResetTimerErrors(); |
109 gpu_timing_.InvalidateTimerOffset(); | 127 gpu_timing_.InvalidateTimerOffset(); |
110 } else { | 128 } else { |
111 LOG(WARNING) << "Can't initialize gpu timing"; | 129 LOG(WARNING) << "Can't initialize gpu timing"; |
112 } | 130 } |
113 | |
114 // Prepare a simple program and a vertex buffer that will be | 131 // Prepare a simple program and a vertex buffer that will be |
115 // used to draw a quad on the offscreen surface. | 132 // used to draw a quad on the offscreen surface. |
116 vertex_shader_ = LoadShader(GL_VERTEX_SHADER, kVertexShader); | 133 vertex_shader_ = LoadShader(GL_VERTEX_SHADER, kVertexShader); |
117 fragment_shader_ = LoadShader(GL_FRAGMENT_SHADER, kFragmentShader); | 134 fragment_shader_ = LoadShader(GL_FRAGMENT_SHADER, kFragmentShader); |
118 program_object_ = glCreateProgram(); | 135 program_object_ = glCreateProgram(); |
119 CHECK_NE(0u, program_object_); | 136 CHECK_NE(0u, program_object_); |
120 | 137 |
121 glAttachShader(program_object_, vertex_shader_); | 138 glAttachShader(program_object_, vertex_shader_); |
122 glAttachShader(program_object_, fragment_shader_); | 139 glAttachShader(program_object_, fragment_shader_); |
123 glBindAttribLocation(program_object_, 0, "a_position"); | 140 glBindAttribLocation(program_object_, 0, "a_position"); |
(...skipping 23 matching lines...) Expand all Loading... | |
147 } | 164 } |
148 if (vertex_shader_ != 0) { | 165 if (vertex_shader_ != 0) { |
149 glDeleteShader(vertex_shader_); | 166 glDeleteShader(vertex_shader_); |
150 } | 167 } |
151 if (fragment_shader_ != 0) { | 168 if (fragment_shader_ != 0) { |
152 glDeleteShader(fragment_shader_); | 169 glDeleteShader(fragment_shader_); |
153 } | 170 } |
154 if (vertex_buffer_ != 0) { | 171 if (vertex_buffer_ != 0) { |
155 glDeleteShader(vertex_buffer_); | 172 glDeleteShader(vertex_buffer_); |
156 } | 173 } |
174 if (framebuffer_object_ != 0) { | |
reveman
2015/02/18 16:21:33
nit: I think just "if (framebuffer_object_)" is pr
Daniele Castagna
2015/02/18 18:47:07
We don't need them. I checked the documentation fo
| |
175 glBindFramebufferEXT(GL_FRAMEBUFFER, 0); | |
176 glDeleteFramebuffersEXT(1, &framebuffer_object_); | |
177 } | |
178 if (color_texture_ != 0) { | |
reveman
2015/02/18 16:21:33
nit: maybe remove "!= 0"
Daniele Castagna
2015/02/18 18:47:07
Removed all the if (blabla_ != 0)
| |
179 glDeleteTextures(1, &color_texture_); | |
180 } | |
157 | 181 |
158 gl_context_ = nullptr; | 182 gl_context_ = nullptr; |
159 surface_ = nullptr; | 183 surface_ = nullptr; |
160 } | 184 } |
161 | 185 |
162 protected: | 186 protected: |
163 // Upload and draw on the offscren surface. | 187 // Upload and draw on the offscren surface. |
164 // Return a list of pair. Each pair describe a gl operation and the wall | 188 // Return a list of pair. Each pair describe a gl operation and the wall |
165 // time elapsed in milliseconds. | 189 // time elapsed in milliseconds. |
166 std::vector<Measurement> UploadAndDraw(const std::vector<uint8>& pixels, | 190 std::vector<Measurement> UploadAndDraw(const std::vector<uint8>& pixels, |
167 const GLenum format, | 191 const GLenum format, |
168 const GLenum type) { | 192 const GLenum type) { |
169 ui::ScopedMakeCurrent smc(gl_context_.get(), surface_.get()); | 193 ui::ScopedMakeCurrent smc(gl_context_.get(), surface_.get()); |
194 DCHECK_GT(framebuffer_object_, 0u); | |
reveman
2015/02/18 16:21:33
nit: DCHECK_NE as less than 0 is not possible
Daniele Castagna
2015/02/18 18:47:07
Done.
| |
195 glBindFramebufferEXT(GL_FRAMEBUFFER, framebuffer_object_); | |
170 | 196 |
171 MeasurementTimers total_timers(&gpu_timing_); | 197 MeasurementTimers total_timers(&gpu_timing_); |
172 GLuint texture_id = 0; | 198 GLuint texture_id = 0; |
173 | 199 |
174 MeasurementTimers tex_timers(&gpu_timing_); | 200 MeasurementTimers tex_timers(&gpu_timing_); |
175 glActiveTexture(GL_TEXTURE0); | 201 glActiveTexture(GL_TEXTURE0); |
176 glGenTextures(1, &texture_id); | 202 glGenTextures(1, &texture_id); |
177 glBindTexture(GL_TEXTURE_2D, texture_id); | 203 glBindTexture(GL_TEXTURE_2D, texture_id); |
178 | 204 |
179 glTexImage2D(GL_TEXTURE_2D, 0, format, size_.width(), size_.height(), 0, | 205 glTexImage2D(GL_TEXTURE_2D, 0, format, size_.width(), size_.height(), 0, |
180 format, type, &pixels[0]); | 206 format, type, &pixels[0]); |
181 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | 207 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
182 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | 208 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
183 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | 209 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
184 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | 210 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
185 CheckNoGlError(); | 211 CheckNoGlError(); |
186 tex_timers.Record(); | 212 tex_timers.Record(); |
187 | 213 |
188 MeasurementTimers draw_timers(&gpu_timing_); | 214 MeasurementTimers draw_timers(&gpu_timing_); |
189 glUseProgram(program_object_); | 215 glUseProgram(program_object_); |
190 glUniform1i(sampler_location_, 0); | 216 glUniform1i(sampler_location_, 0); |
191 | 217 |
192 glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_); | 218 glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_); |
(...skipping 22 matching lines...) Expand all Loading... | |
215 EXPECT_EQ(pixels, pixels_rendered); | 241 EXPECT_EQ(pixels, pixels_rendered); |
216 | 242 |
217 std::vector<Measurement> measurements; | 243 std::vector<Measurement> measurements; |
218 measurements.push_back(total_timers.GetAsMeasurement("total")); | 244 measurements.push_back(total_timers.GetAsMeasurement("total")); |
219 measurements.push_back(tex_timers.GetAsMeasurement("teximage2d")); | 245 measurements.push_back(tex_timers.GetAsMeasurement("teximage2d")); |
220 measurements.push_back(draw_timers.GetAsMeasurement("drawarrays")); | 246 measurements.push_back(draw_timers.GetAsMeasurement("drawarrays")); |
221 measurements.push_back(finish_timers.GetAsMeasurement("finish")); | 247 measurements.push_back(finish_timers.GetAsMeasurement("finish")); |
222 return measurements; | 248 return measurements; |
223 } | 249 } |
224 | 250 |
225 const gfx::Size size_; // for the offscreen surface and the texture | 251 const gfx::Size size_; // for the fbo and the texture |
226 scoped_refptr<gfx::GLContext> gl_context_; | 252 scoped_refptr<gfx::GLContext> gl_context_; |
227 scoped_refptr<gfx::GLSurface> surface_; | 253 scoped_refptr<gfx::GLSurface> surface_; |
228 GPUTiming gpu_timing_; | 254 GPUTiming gpu_timing_; |
229 | 255 |
256 GLuint color_texture_ = 0; | |
257 GLuint framebuffer_object_ = 0; | |
230 GLuint vertex_shader_ = 0; | 258 GLuint vertex_shader_ = 0; |
231 GLuint fragment_shader_ = 0; | 259 GLuint fragment_shader_ = 0; |
232 GLuint program_object_ = 0; | 260 GLuint program_object_ = 0; |
233 GLint sampler_location_ = -1; | 261 GLint sampler_location_ = -1; |
234 GLuint vertex_buffer_ = 0; | 262 GLuint vertex_buffer_ = 0; |
235 }; | 263 }; |
236 | 264 |
237 // Perf test that generates, uploads and draws a texture on a surface repeatedly | 265 // Perf test that generates, uploads and draws a texture on a surface repeatedly |
238 // and prints out aggregated measurements for all the runs. | 266 // and prints out aggregated measurements for all the runs. |
239 TEST_F(TextureUploadPerfTest, glTexImage2d) { | 267 TEST_F(TextureUploadPerfTest, glTexImage2d) { |
(...skipping 13 matching lines...) Expand all Loading... | |
253 } | 281 } |
254 | 282 |
255 for (const auto& entry : aggregates) { | 283 for (const auto& entry : aggregates) { |
256 const auto m = entry.second.Divide(kUploadPerfTestRuns); | 284 const auto m = entry.second.Divide(kUploadPerfTestRuns); |
257 m.PrintResult(); | 285 m.PrintResult(); |
258 } | 286 } |
259 } | 287 } |
260 | 288 |
261 } // namespace | 289 } // namespace |
262 } // namespace gpu | 290 } // namespace gpu |
OLD | NEW |