DynamicTexture.cpp
Go to the documentation of this file.
1 #include "DynamicTexture.h"
2 #include "PCH.h"
3 
5  int Width, int Height, EPixelFormat Format, TextureCompressionSettings Compression)
6 {
8 
9  REALSENSE_TRACE(TEXT("+FDynamicTexture %p %s"), this, *Name);
10 
11  this->Name = Name;
12  this->Width = Width;
13  this->Height = Height;
14  this->Bpp = GPixelFormats[Format].BlockBytes;
15  this->Format = Format;
16  this->Compression = Compression;
17 
18  CommandCounter.Increment();
19  ENQUEUE_RENDER_COMMAND(CreateTextureCmd)(
20  [this](FRHICommandListImmediate& RHICmdList)
21  {
23  });
24 }
25 
27 {
29 
30  REALSENSE_TRACE(TEXT("~FDynamicTexture %p %s CommandCounter=%d TudPool=%d"), this, *Name, (int)CommandCounter.GetValue(), (int)TudPool.size());
31 
32  {
33  const float Timeout = 5;
34  const float Granularity = 200.0f / 1000.0f;
35  int NumTries = (int)(Timeout / Granularity);
36 
37  NAMED_PROFILER("WaitCommandCounter");
38  while (CommandCounter.GetValue() > 0 && NumTries > 0)
39  {
40  //REALSENSE_ERR(TEXT("Wait FDynamicTexture CommandCounter=%d"), (int)CommandCounter.GetValue());
41  FPlatformProcess::Sleep(Granularity);
42  NumTries--;
43  }
44  }
45 
46  if (TextureObject)
47  {
48  TextureObject->RemoveFromRoot();
49  TextureObject = nullptr;
50  }
51 
52  for (FTextureUpdateData* Tud : TudPool)
53  {
54  if (Tud->Data) FMemory::Free(Tud->Data);
55  FMemory::Free(Tud);
56  }
57  TudPool.clear();
58 }
59 
61 {
63 
64  if (!TudPool.empty())
65  {
66  FScopeLock Lock(&TudMx);
67  if (!TudPool.empty())
68  {
69  auto Tud = TudPool.back();
70  TudPool.pop_back();
71  return Tud;
72  }
73  }
74 
75  auto Tud = (FTextureUpdateData*)FMemory::Malloc(sizeof(FTextureUpdateData), PLATFORM_CACHE_LINE_SIZE);
76  Tud->Context = this;
77  Tud->DataSize = Width * Height * Bpp;
78  Tud->Data = FMemory::Malloc(Tud->DataSize, PLATFORM_CACHE_LINE_SIZE);
79  Tud->Stride = Width * Bpp;
80  Tud->Width = Width;
81  Tud->Height = Height;
82 
83  return Tud;
84 }
85 
87 {
89 
90  // UE4 is so great!
91  const bool HackIsValidThread = (!GIsThreadedRendering || ENamedThreads::GetRenderThread() != ENamedThreads::GameThread);
92 
93  if (!HackIsValidThread)
94  {
95  REALSENSE_ERR(TEXT("EnqueUpdateCommand: invalid thread"));
96  }
97  else if (TextureObject && TextureObject->Resource)
98  {
99  CommandCounter.Increment();
100  ENQUEUE_RENDER_COMMAND(UpdateTextureCmd)(
101  [Tud](FRHICommandListImmediate& RHICmdList)
102  {
103  Tud->Context->RenderCmd_UpdateTexture(Tud);
104  });
105  }
106 }
107 
109 {
111 
112  const auto fw = Frame.get_width();
113  const auto fh = Frame.get_height();
114  const auto fbpp = Frame.get_bytes_per_pixel();
115 
116  if (Width != fw || Height != fh || Bpp != fbpp)
117  {
118  REALSENSE_ERR(TEXT("Invalid video_frame: %s Width=%d Height=%d Bpp=%d"),
119  *Name, fw, fh, fbpp);
120  return;
121  }
122 
123  auto* Tud = AllocBuffer();
124  CopyData(Tud, Frame);
125  EnqueUpdateCommand(Tud);
126 }
127 
129 {
131 
132  const auto fw = Frame.get_width();
133  const auto fh = Frame.get_height();
134  const auto fbpp = Frame.get_bytes_per_pixel();
135  const auto fs = Frame.get_stride_in_bytes();
136 
137  uint8_t* Dst = (uint8_t*)Tud->Data;
138  const uint8_t* Src = (const uint8_t*)Frame.get_data();
139 
140  for (int y = 0; y < fh; ++y)
141  {
142  FMemory::Memcpy(Dst, Src, fw * fbpp);
143  Dst += (fw * fbpp);
144  Src += fs;
145  }
146 }
147 
149 {
151 
152  REALSENSE_TRACE(TEXT("FDynamicTexture::RenderCmd_CreateTexture %p %s"), this, *Name);
153 
154  auto Tex = UTexture2D::CreateTransient(Width, Height, Format);
155  if (!Tex)
156  {
157  REALSENSE_ERR(TEXT("UTexture2D::CreateTransient failed"));
158  }
159  else
160  {
161  #if WITH_EDITORONLY_DATA
162  Tex->MipGenSettings = TextureMipGenSettings::TMGS_NoMipmaps;
163  #endif
164 
165  Tex->CompressionSettings = Compression;
166  Tex->Filter = TextureFilter::TF_Nearest;
167  Tex->SRGB = 0;
168 
169  Tex->AddToRoot();
170  Tex->UpdateResource();
171 
172  TextureObject = Tex;
173  }
174 
175  CommandCounter.Decrement();
176 }
177 
179 {
181 
182  auto Tex = Tud->Context->TextureObject;
183  if (Tex && Tex->Resource)
184  {
185  RHIUpdateTexture2D(
186  ((FTexture2DResource*)Tex->Resource)->GetTexture2DRHI(),
187  0,
188  FUpdateTextureRegion2D(0, 0, 0, 0, Tud->Width, Tud->Height),
189  Tud->Stride,
190  (const uint8*)Tud->Data
191  );
192  }
193 
194  {
195  FScopeLock Lock(&TudMx);
196  TudPool.push_back(Tud);
197  }
198 
199  CommandCounter.Decrement();
200 }
GLint y
void EnqueUpdateCommand(FTextureUpdateData *Tud)
int get_bytes_per_pixel() const
Definition: rs_frame.hpp:707
void RenderCmd_CreateTexture()
const void * get_data() const
Definition: rs_frame.hpp:545
void Update(const rs2::video_frame &Frame)
#define REALSENSE_ERR(Format,...)
Definition: Shared.h:16
unsigned char uint8_t
Definition: stdint.h:78
virtual ~FDynamicTexture()
EPixelFormat Format
FCriticalSection TudMx
void RenderCmd_UpdateTexture(FTextureUpdateData *Tud)
#define SCOPED_PROFILER
Definition: Rs2Base.h:32
std::vector< FTextureUpdateData * > TudPool
#define NAMED_PROFILER(name)
Definition: Rs2Base.h:31
UTexture2D * TextureObject
FTextureUpdateData * AllocBuffer()
FDynamicTexture(FString Name, int Width, int Height, EPixelFormat Format, TextureCompressionSettings Compression)
void CopyData(FTextureUpdateData *Tud, const rs2::video_frame &Frame)
int get_height() const
Definition: rs_frame.hpp:671
#define REALSENSE_TRACE(Format,...)
Definition: Shared.h:21
FThreadSafeCounter CommandCounter
TextureCompressionSettings Compression
class FDynamicTexture * Context
Definition: DynamicTexture.h:7
int get_stride_in_bytes() const
Definition: rs_frame.hpp:683
int get_width() const
Definition: rs_frame.hpp:659


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:13