| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 <assert.h> | 5 #include <assert.h> |
| 6 #include <math.h> | 6 #include <math.h> |
| 7 #include <ppapi/c/ppb_input_event.h> | 7 #include <ppapi/c/ppb_input_event.h> |
| 8 #include <ppapi/cpp/input_event.h> | 8 #include <ppapi/cpp/input_event.h> |
| 9 #include <ppapi/cpp/var.h> | 9 #include <ppapi/cpp/var.h> |
| 10 #include <ppapi/cpp/var_array.h> | 10 #include <ppapi/cpp/var_array.h> |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 int start_tv_retv = gettimeofday(&start_tv, NULL); | 53 int start_tv_retv = gettimeofday(&start_tv, NULL); |
| 54 | 54 |
| 55 inline double getseconds() { | 55 inline double getseconds() { |
| 56 const double usec_to_sec = 0.000001; | 56 const double usec_to_sec = 0.000001; |
| 57 timeval tv; | 57 timeval tv; |
| 58 if ((0 == start_tv_retv) && (0 == gettimeofday(&tv, NULL))) | 58 if ((0 == start_tv_retv) && (0 == gettimeofday(&tv, NULL))) |
| 59 return (tv.tv_sec - start_tv.tv_sec) + tv.tv_usec * usec_to_sec; | 59 return (tv.tv_sec - start_tv.tv_sec) + tv.tv_usec * usec_to_sec; |
| 60 return 0.0; | 60 return 0.0; |
| 61 } | 61 } |
| 62 | 62 |
| 63 // RGBA helper functions. | 63 // RGBA helper functions, used for extracting color from RGBA source image. |
| 64 inline float ExtractR(uint32_t c) { | 64 inline float ExtractR(uint32_t c) { |
| 65 return static_cast<float>(c & 0xFF) * kOneOver255; | 65 return static_cast<float>(c & 0xFF) * kOneOver255; |
| 66 } | 66 } |
| 67 | 67 |
| 68 inline float ExtractG(uint32_t c) { | 68 inline float ExtractG(uint32_t c) { |
| 69 return static_cast<float>((c & 0xFF00) >> 8) * kOneOver255; | 69 return static_cast<float>((c & 0xFF00) >> 8) * kOneOver255; |
| 70 } | 70 } |
| 71 | 71 |
| 72 inline float ExtractB(uint32_t c) { | 72 inline float ExtractB(uint32_t c) { |
| 73 return static_cast<float>((c & 0xFF0000) >> 16) * kOneOver255; | 73 return static_cast<float>((c & 0xFF0000) >> 16) * kOneOver255; |
| 74 } | 74 } |
| 75 | 75 |
| 76 inline uint32_t MakeRGBA(uint32_t r, uint32_t g, uint32_t b, uint32_t a) { | 76 // BGRA helper function, for constructing a pixel for a BGRA buffer. |
| 77 inline uint32_t MakeBGRA(uint32_t b, uint32_t g, uint32_t r, uint32_t a) { |
| 77 return (((a) << 24) | ((r) << 16) | ((g) << 8) | (b)); | 78 return (((a) << 24) | ((r) << 16) | ((g) << 8) | (b)); |
| 78 } | 79 } |
| 79 | 80 |
| 80 // simple container for earth texture | 81 // simple container for earth texture |
| 81 struct Texture { | 82 struct Texture { |
| 82 int width, height; | 83 int width, height; |
| 83 uint32_t* pixels; | 84 uint32_t* pixels; |
| 84 Texture(int w, int h) : width(w), height(h) { | 85 Texture(int w, int h) : width(w), height(h) { |
| 85 pixels = new uint32_t[w * h]; | 86 pixels = new uint32_t[w * h]; |
| 86 memset(pixels, 0, sizeof(uint32_t) * w * h); | 87 memset(pixels, 0, sizeof(uint32_t) * w * h); |
| (...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 346 | 347 |
| 347 | 348 |
| 348 Planet::Planet() : base_tex_(NULL), night_tex_(NULL), num_threads_(0), | 349 Planet::Planet() : base_tex_(NULL), night_tex_(NULL), num_threads_(0), |
| 349 benchmarking_(false), benchmark_frame_counter_(0) { | 350 benchmarking_(false), benchmark_frame_counter_(0) { |
| 350 | 351 |
| 351 Reset(); | 352 Reset(); |
| 352 RequestTextures(); | 353 RequestTextures(); |
| 353 // By default, render from the dispatch thread. | 354 // By default, render from the dispatch thread. |
| 354 workers_ = new ThreadPool(num_threads_); | 355 workers_ = new ThreadPool(num_threads_); |
| 355 PSEventSetFilter(PSE_ALL); | 356 PSEventSetFilter(PSE_ALL); |
| 356 ps_context_ = PSContext2DAllocate(); | 357 ps_context_ = PSContext2DAllocate(PP_IMAGEDATAFORMAT_BGRA_PREMUL); |
| 357 } | 358 } |
| 358 | 359 |
| 359 Planet::~Planet() { | 360 Planet::~Planet() { |
| 360 delete workers_; | 361 delete workers_; |
| 361 PSContext2DFree(ps_context_); | 362 PSContext2DFree(ps_context_); |
| 362 } | 363 } |
| 363 | 364 |
| 364 // Given a region r, derive a rectangle. | 365 // Given a region r, derive a rectangle. |
| 365 // This rectangle shouldn't overlap with work being done by other workers. | 366 // This rectangle shouldn't overlap with work being done by other workers. |
| 366 // If multithreading, this function is only called by the worker threads. | 367 // If multithreading, this function is only called by the worker threads. |
| 367 void Planet::wMakeRect(int r, int *x, int *y, int *w, int *h) { | 368 void Planet::wMakeRect(int r, int *x, int *y, int *w, int *h) { |
| 368 *x = 0; | 369 *x = 0; |
| 369 *w = ps_context_->width; | 370 *w = ps_context_->width; |
| 370 *y = r; | 371 *y = r; |
| 371 *h = 1; | 372 *h = 1; |
| 372 } | 373 } |
| 373 | 374 |
| 374 | 375 |
| 375 inline uint32_t* Planet::wGetAddr(int x, int y) { | 376 inline uint32_t* Planet::wGetAddr(int x, int y) { |
| 376 return ps_context_->data + x + y * ps_context_->stride / sizeof(uint32_t); | 377 return ps_context_->data + x + y * ps_context_->stride / sizeof(uint32_t); |
| 377 } | 378 } |
| 378 | 379 |
| 379 // This is the meat of the ray tracer. Given a pixel span (x0, x1) on | 380 // This is the meat of the ray tracer. Given a pixel span (x0, x1) on |
| 380 // scanline y, shoot rays into the scene and render what they hit. Use | 381 // scanline y, shoot rays into the scene and render what they hit. Use |
| 381 // scanline coherence to do a few optimizations | 382 // scanline coherence to do a few optimizations |
| 382 void Planet::wRenderPixelSpan(int x0, int x1, int y) { | 383 void Planet::wRenderPixelSpan(int x0, int x1, int y) { |
| 383 if (!base_tex_ || !night_tex_) | 384 if (!base_tex_ || !night_tex_) |
| 384 return; | 385 return; |
| 385 const int kColorBlack = MakeRGBA(0, 0, 0, 0xFF); | 386 const int kColorBlack = MakeBGRA(0, 0, 0, 0xFF); |
| 386 float width = ps_context_->width; | 387 float width = ps_context_->width; |
| 387 float height = ps_context_->height; | 388 float height = ps_context_->height; |
| 388 float min_dim = width < height ? width : height; | 389 float min_dim = width < height ? width : height; |
| 389 float offset_x = width < height ? 0 : (width - min_dim) * 0.5f; | 390 float offset_x = width < height ? 0 : (width - min_dim) * 0.5f; |
| 390 float offset_y = width < height ? (height - min_dim) * 0.5f : 0; | 391 float offset_y = width < height ? (height - min_dim) * 0.5f : 0; |
| 391 float y0 = eye_y_; | 392 float y0 = eye_y_; |
| 392 float z0 = eye_z_; | 393 float z0 = eye_z_; |
| 393 float y1 = (static_cast<float>(y - offset_y) / min_dim) * 2.0f - 1.0f; | 394 float y1 = (static_cast<float>(y - offset_y) / min_dim) * 2.0f - 1.0f; |
| 394 float z1 = 0.0f; | 395 float z1 = 0.0f; |
| 395 float dy = (y1 - y0); | 396 float dy = (y1 - y0); |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 488 uint32_t night_texel = night_tex_->pixels[noffset]; | 489 uint32_t night_texel = night_tex_->pixels[noffset]; |
| 489 float nr = ExtractR(night_texel); | 490 float nr = ExtractR(night_texel); |
| 490 float ng = ExtractG(night_texel); | 491 float ng = ExtractG(night_texel); |
| 491 float nb = ExtractB(night_texel); | 492 float nb = ExtractB(night_texel); |
| 492 | 493 |
| 493 // Final color value is lerp between day and night texels. | 494 // Final color value is lerp between day and night texels. |
| 494 unsigned int ir = Clamp255(pr * tr + nr * ipr); | 495 unsigned int ir = Clamp255(pr * tr + nr * ipr); |
| 495 unsigned int ig = Clamp255(pg * tg + ng * ipg); | 496 unsigned int ig = Clamp255(pg * tg + ng * ipg); |
| 496 unsigned int ib = Clamp255(pb * tb + nb * ipb); | 497 unsigned int ib = Clamp255(pb * tb + nb * ipb); |
| 497 | 498 |
| 498 unsigned int color = MakeRGBA(ir, ig, ib, 0xFF); | 499 unsigned int color = MakeBGRA(ib, ig, ir, 0xFF); |
| 499 | 500 |
| 500 *pixels = color; | 501 *pixels = color; |
| 501 ++pixels; | 502 ++pixels; |
| 502 } | 503 } |
| 503 } | 504 } |
| 504 | 505 |
| 505 // Renders a rectangular area of the screen, scan line at a time | 506 // Renders a rectangular area of the screen, scan line at a time |
| 506 void Planet::wRenderRect(int x, int y, int w, int h) { | 507 void Planet::wRenderRect(int x, int y, int w, int h) { |
| 507 for (int j = y; j < (y + h); ++j) { | 508 for (int j = y; j < (y + h); ++j) { |
| 508 this->wRenderPixelSpan(x, x + w - 1, j); | 509 this->wRenderPixelSpan(x, x + w - 1, j); |
| (...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 789 // Do simulation, render and present. | 790 // Do simulation, render and present. |
| 790 earth.Update(); | 791 earth.Update(); |
| 791 } | 792 } |
| 792 | 793 |
| 793 return 0; | 794 return 0; |
| 794 } | 795 } |
| 795 | 796 |
| 796 // Register the function to call once the Instance Object is initialized. | 797 // Register the function to call once the Instance Object is initialized. |
| 797 // see: pappi_simple/ps_main.h | 798 // see: pappi_simple/ps_main.h |
| 798 PPAPI_SIMPLE_REGISTER_MAIN(example_main); | 799 PPAPI_SIMPLE_REGISTER_MAIN(example_main); |
| OLD | NEW |