| 46 | } |
| 47 | |
| 48 | GPUTexture* RenderTargetPool::Get(const GPUTextureDescription& desc) |
| 49 | { |
| 50 | PROFILE_CPU(); |
| 51 | |
| 52 | // Initialize render targets with pink color in debug builds to prevent incorrect data usage (GPU doesn't clear texture upon creation) |
| 53 | #if BUILD_DEBUG && PLATFORM_DESKTOP |
| 54 | #define RENDER_TARGET_POOL_CLEAR() if (desc.Dimensions == TextureDimensions::Texture && EnumHasAllFlags(desc.Flags, GPUTextureFlags::RenderTarget) && GPUDevice::Instance->IsRendering() && IsInMainThread()) GPUDevice::Instance->GetMainContext()->Clear(e.RT->View(), Color::Pink); |
| 55 | #else |
| 56 | #define RENDER_TARGET_POOL_CLEAR() |
| 57 | #endif |
| 58 | |
| 59 | // Find free render target with the same properties |
| 60 | const uint32 descHash = GetHash(desc); |
| 61 | for (int32 i = 0; i < TemporaryRTs.Count(); i++) |
| 62 | { |
| 63 | auto& e = TemporaryRTs[i]; |
| 64 | if (!e.IsOccupied && e.DescriptionHash == descHash) |
| 65 | { |
| 66 | // Mark as used |
| 67 | e.IsOccupied = true; |
| 68 | RENDER_TARGET_POOL_CLEAR(); |
| 69 | return e.RT; |
| 70 | } |
| 71 | } |
| 72 | #if !BUILD_RELEASE |
| 73 | if (TemporaryRTs.Count() > 2000) |
| 74 | { |
| 75 | LOG(Fatal, "Too many textures allocated in RenderTargetPool. Know your limits, sir!"); |
| 76 | return nullptr; |
| 77 | } |
| 78 | #endif |
| 79 | |
| 80 | // Create new rt |
| 81 | const String name = TEXT("TemporaryRT_") + StringUtils::ToString(TemporaryRTs.Count()); |
| 82 | GPUTexture* rt = GPUDevice::Instance->CreateTexture(name); |
| 83 | if (rt->Init(desc)) |
| 84 | { |
| 85 | Delete(rt); |
| 86 | LOG(Error, "Cannot create temporary render target. Description: {0}", desc.ToString()); |
| 87 | return nullptr; |
| 88 | } |
| 89 | |
| 90 | // Create temporary rt entry |
| 91 | Entry e; |
| 92 | e.IsOccupied = true; |
| 93 | e.LastFrameReleased = 0; |
| 94 | e.RT = rt; |
| 95 | e.DescriptionHash = descHash; |
| 96 | TemporaryRTs.Add(e); |
| 97 | RENDER_TARGET_POOL_CLEAR(); |
| 98 | |
| 99 | #undef RENDER_TARGET_POOL_CLEAR |
| 100 | return rt; |
| 101 | } |
| 102 | |
| 103 | void RenderTargetPool::Release(GPUTexture* rt) |
| 104 | { |
no test coverage detected