Skip to content

Commit

Permalink
Optimize image copying loop after capturing an image
Browse files Browse the repository at this point in the history
This improves the runtime of this loop from about 10ms to less than 1ms
on my computer.

When repeatedly obtaining images through the API in a loop, this image
copying does not seem to be in the critical path (probably because the GPU
already starts rendering the next frame while the copy is going on), but
it seems to be some low-hanging fruit nonetheless.
  • Loading branch information
aburgm committed Feb 27, 2018
1 parent d37669f commit e8936ee
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions Unreal/Plugins/AirSim/Source/RenderRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,22 +69,25 @@ void RenderRequest::getScreenshot(std::shared_ptr<RenderParams> params[], std::v
for (unsigned int i = 0; i < req_size; ++i) {
if (! params[i]->pixels_as_float) {
if (results[i]->width != 0 && results[i]->height != 0) {
results[i]->image_data_uint8.SetNumUninitialized(results[i]->width * results[i]->height * 4, false);
if (params[i]->compress)
FImageUtils::CompressImageArray(results[i]->width, results[i]->height, results[i]->bmp, results[i]->image_data_uint8);
else {
uint8* ptr = results[i]->image_data_uint8.GetData();
for (const auto& item : results[i]->bmp) {
results[i]->image_data_uint8.Add(item.R);
results[i]->image_data_uint8.Add(item.G);
results[i]->image_data_uint8.Add(item.B);
results[i]->image_data_uint8.Add(item.A);
*ptr++ = item.R;
*ptr++ = item.G;
*ptr++ = item.B;
*ptr++ = item.A;
}
}
}
}
else {
results[i]->image_data_float.SetNumUninitialized(results[i]->width * results[i]->height);
float* ptr = results[i]->image_data_float.GetData();
for (const auto& item : results[i]->bmp_float) {
float fval = item.R.GetFloat();
results[i]->image_data_float.Add(fval);
*ptr++ = item.R.GetFloat();
}
}
}
Expand Down

0 comments on commit e8936ee

Please sign in to comment.