| 1 | /* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */ |
| 2 | /* If you are missing that file, acquire a complete release at teeworlds.com. */ |
| 3 | |
| 4 | #include "graphics_threaded.h" |
| 5 | |
| 6 | #include <base/dbg.h> |
| 7 | #include <base/detect.h> |
| 8 | #include <base/io.h> |
| 9 | #include <base/log.h> |
| 10 | #include <base/mem.h> |
| 11 | #include <base/str.h> |
| 12 | #include <base/time.h> |
| 13 | |
| 14 | #include <engine/engine.h> |
| 15 | #include <engine/gfx/image_loader.h> |
| 16 | #include <engine/gfx/image_manipulation.h> |
| 17 | #include <engine/graphics.h> |
| 18 | #include <engine/shared/config.h> |
| 19 | #include <engine/shared/jobs.h> |
| 20 | #include <engine/storage.h> |
| 21 | |
| 22 | #include <generated/data_types.h> |
| 23 | |
| 24 | #include <game/localization.h> |
| 25 | |
| 26 | #if defined(CONF_VIDEORECORDER) |
| 27 | #include <engine/shared/video.h> |
| 28 | #endif |
| 29 | |
| 30 | #include <algorithm> |
| 31 | |
| 32 | class CSemaphore; |
| 33 | |
| 34 | static CVideoMode g_aFakeModes[] = { |
| 35 | {.m_CanvasWidth: 8192, .m_CanvasHeight: 4320, .m_WindowWidth: 8192, .m_WindowHeight: 4320, .m_RefreshRate: 0}, {.m_CanvasWidth: 7680, .m_CanvasHeight: 4320, .m_WindowWidth: 7680, .m_WindowHeight: 4320, .m_RefreshRate: 0}, {.m_CanvasWidth: 5120, .m_CanvasHeight: 2880, .m_WindowWidth: 5120, .m_WindowHeight: 2880, .m_RefreshRate: 0}, |
| 36 | {.m_CanvasWidth: 4096, .m_CanvasHeight: 2160, .m_WindowWidth: 4096, .m_WindowHeight: 2160, .m_RefreshRate: 0}, {.m_CanvasWidth: 3840, .m_CanvasHeight: 2160, .m_WindowWidth: 3840, .m_WindowHeight: 2160, .m_RefreshRate: 0}, {.m_CanvasWidth: 2560, .m_CanvasHeight: 1440, .m_WindowWidth: 2560, .m_WindowHeight: 1440, .m_RefreshRate: 0}, |
| 37 | {.m_CanvasWidth: 2048, .m_CanvasHeight: 1536, .m_WindowWidth: 2048, .m_WindowHeight: 1536, .m_RefreshRate: 0}, {.m_CanvasWidth: 1920, .m_CanvasHeight: 2400, .m_WindowWidth: 1920, .m_WindowHeight: 2400, .m_RefreshRate: 0}, {.m_CanvasWidth: 1920, .m_CanvasHeight: 1440, .m_WindowWidth: 1920, .m_WindowHeight: 1440, .m_RefreshRate: 0}, |
| 38 | {.m_CanvasWidth: 1920, .m_CanvasHeight: 1200, .m_WindowWidth: 1920, .m_WindowHeight: 1200, .m_RefreshRate: 0}, {.m_CanvasWidth: 1920, .m_CanvasHeight: 1080, .m_WindowWidth: 1920, .m_WindowHeight: 1080, .m_RefreshRate: 0}, {.m_CanvasWidth: 1856, .m_CanvasHeight: 1392, .m_WindowWidth: 1856, .m_WindowHeight: 1392, .m_RefreshRate: 0}, |
| 39 | {.m_CanvasWidth: 1800, .m_CanvasHeight: 1440, .m_WindowWidth: 1800, .m_WindowHeight: 1440, .m_RefreshRate: 0}, {.m_CanvasWidth: 1792, .m_CanvasHeight: 1344, .m_WindowWidth: 1792, .m_WindowHeight: 1344, .m_RefreshRate: 0}, {.m_CanvasWidth: 1680, .m_CanvasHeight: 1050, .m_WindowWidth: 1680, .m_WindowHeight: 1050, .m_RefreshRate: 0}, |
| 40 | {.m_CanvasWidth: 1600, .m_CanvasHeight: 1200, .m_WindowWidth: 1600, .m_WindowHeight: 1200, .m_RefreshRate: 0}, {.m_CanvasWidth: 1600, .m_CanvasHeight: 1000, .m_WindowWidth: 1600, .m_WindowHeight: 1000, .m_RefreshRate: 0}, {.m_CanvasWidth: 1440, .m_CanvasHeight: 1050, .m_WindowWidth: 1440, .m_WindowHeight: 1050, .m_RefreshRate: 0}, |
| 41 | {.m_CanvasWidth: 1440, .m_CanvasHeight: 900, .m_WindowWidth: 1440, .m_WindowHeight: 900, .m_RefreshRate: 0}, {.m_CanvasWidth: 1400, .m_CanvasHeight: 1050, .m_WindowWidth: 1400, .m_WindowHeight: 1050, .m_RefreshRate: 0}, {.m_CanvasWidth: 1368, .m_CanvasHeight: 768, .m_WindowWidth: 1368, .m_WindowHeight: 768, .m_RefreshRate: 0}, |
| 42 | {.m_CanvasWidth: 1280, .m_CanvasHeight: 1024, .m_WindowWidth: 1280, .m_WindowHeight: 1024, .m_RefreshRate: 0}, {.m_CanvasWidth: 1280, .m_CanvasHeight: 960, .m_WindowWidth: 1280, .m_WindowHeight: 960, .m_RefreshRate: 0}, {.m_CanvasWidth: 1280, .m_CanvasHeight: 800, .m_WindowWidth: 1280, .m_WindowHeight: 800, .m_RefreshRate: 0}, |
| 43 | {.m_CanvasWidth: 1280, .m_CanvasHeight: 768, .m_WindowWidth: 1280, .m_WindowHeight: 768, .m_RefreshRate: 0}, {.m_CanvasWidth: 1152, .m_CanvasHeight: 864, .m_WindowWidth: 1152, .m_WindowHeight: 864, .m_RefreshRate: 0}, {.m_CanvasWidth: 1024, .m_CanvasHeight: 768, .m_WindowWidth: 1024, .m_WindowHeight: 768, .m_RefreshRate: 0}, |
| 44 | {.m_CanvasWidth: 1024, .m_CanvasHeight: 600, .m_WindowWidth: 1024, .m_WindowHeight: 600, .m_RefreshRate: 0}, {.m_CanvasWidth: 800, .m_CanvasHeight: 600, .m_WindowWidth: 800, .m_WindowHeight: 600, .m_RefreshRate: 0}, {.m_CanvasWidth: 768, .m_CanvasHeight: 576, .m_WindowWidth: 768, .m_WindowHeight: 576, .m_RefreshRate: 0}, |
| 45 | {.m_CanvasWidth: 720, .m_CanvasHeight: 400, .m_WindowWidth: 720, .m_WindowHeight: 400, .m_RefreshRate: 0}, {.m_CanvasWidth: 640, .m_CanvasHeight: 480, .m_WindowWidth: 640, .m_WindowHeight: 480, .m_RefreshRate: 0}, {.m_CanvasWidth: 400, .m_CanvasHeight: 300, .m_WindowWidth: 400, .m_WindowHeight: 300, .m_RefreshRate: 0}, |
| 46 | {.m_CanvasWidth: 320, .m_CanvasHeight: 240, .m_WindowWidth: 320, .m_WindowHeight: 240, .m_RefreshRate: 0}}; |
| 47 | |
| 48 | void CGraphics_Threaded::FlushVertices(bool KeepVertices) |
| 49 | { |
| 50 | CCommandBuffer::SCommand_Render Cmd; |
| 51 | EPrimitiveType PrimType; |
| 52 | size_t PrimCount, NumVerts; |
| 53 | FlushVerticesImpl(KeepVertices, PrimType, PrimCount, NumVerts, Command&: Cmd, VertSize: sizeof(CCommandBuffer::SVertex)); |
| 54 | |
| 55 | if(Cmd.m_pVertices != nullptr) |
| 56 | { |
| 57 | mem_copy(dest: Cmd.m_pVertices, source: m_aVertices, size: sizeof(CCommandBuffer::SVertex) * NumVerts); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | void CGraphics_Threaded::FlushVerticesTex3D() |
| 62 | { |
| 63 | CCommandBuffer::SCommand_RenderTex3D Cmd; |
| 64 | EPrimitiveType PrimType; |
| 65 | size_t PrimCount, NumVerts; |
| 66 | FlushVerticesImpl(KeepVertices: false, PrimType, PrimCount, NumVerts, Command&: Cmd, VertSize: sizeof(CCommandBuffer::SVertexTex3DStream)); |
| 67 | |
| 68 | if(Cmd.m_pVertices != nullptr) |
| 69 | { |
| 70 | mem_copy(dest: Cmd.m_pVertices, source: m_aVerticesTex3D, size: sizeof(CCommandBuffer::SVertexTex3DStream) * NumVerts); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | void CGraphics_Threaded::AddVertices(int Count) |
| 75 | { |
| 76 | m_NumVertices += Count; |
| 77 | if((m_NumVertices + Count) >= CCommandBuffer::MAX_VERTICES) |
| 78 | FlushVertices(); |
| 79 | } |
| 80 | |
| 81 | void CGraphics_Threaded::AddVertices(int Count, CCommandBuffer::SVertex *pVertices) |
| 82 | { |
| 83 | AddVertices(Count); |
| 84 | } |
| 85 | |
| 86 | void CGraphics_Threaded::AddVertices(int Count, CCommandBuffer::SVertexTex3DStream *pVertices) |
| 87 | { |
| 88 | m_NumVertices += Count; |
| 89 | if((m_NumVertices + Count) >= CCommandBuffer::MAX_VERTICES) |
| 90 | FlushVerticesTex3D(); |
| 91 | } |
| 92 | |
| 93 | CGraphics_Threaded::CGraphics_Threaded() |
| 94 | { |
| 95 | m_State.m_ScreenTL.x = 0; |
| 96 | m_State.m_ScreenTL.y = 0; |
| 97 | m_State.m_ScreenBR.x = 0; |
| 98 | m_State.m_ScreenBR.y = 0; |
| 99 | m_State.m_ClipEnable = false; |
| 100 | m_State.m_ClipX = 0; |
| 101 | m_State.m_ClipY = 0; |
| 102 | m_State.m_ClipW = 0; |
| 103 | m_State.m_ClipH = 0; |
| 104 | m_State.m_Texture = -1; |
| 105 | m_State.m_BlendMode = EBlendMode::ALPHA; |
| 106 | m_State.m_WrapMode = EWrapMode::REPEAT; |
| 107 | |
| 108 | m_CurrentCommandBuffer = 0; |
| 109 | m_pCommandBuffer = nullptr; |
| 110 | m_apCommandBuffers[0] = nullptr; |
| 111 | m_apCommandBuffers[1] = nullptr; |
| 112 | |
| 113 | m_NumVertices = 0; |
| 114 | |
| 115 | m_ScreenWidth = -1; |
| 116 | m_ScreenHeight = -1; |
| 117 | m_DrawableWidth = -1; |
| 118 | m_DrawableHeight = -1; |
| 119 | m_ScreenRefreshRate = -1; |
| 120 | |
| 121 | m_Rotation = 0; |
| 122 | m_Drawing = EDrawing::NONE; |
| 123 | |
| 124 | m_TextureMemoryUsage = 0; |
| 125 | |
| 126 | m_RenderEnable = true; |
| 127 | m_DoScreenshot = false; |
| 128 | } |
| 129 | |
| 130 | void CGraphics_Threaded::ClipEnable(int x, int y, int w, int h) |
| 131 | { |
| 132 | if(x < 0) |
| 133 | w += x; |
| 134 | if(y < 0) |
| 135 | h += y; |
| 136 | |
| 137 | x = std::clamp(val: x, lo: 0, hi: ScreenWidth()); |
| 138 | y = std::clamp(val: y, lo: 0, hi: ScreenHeight()); |
| 139 | w = std::clamp(val: w, lo: 0, hi: ScreenWidth() - x); |
| 140 | h = std::clamp(val: h, lo: 0, hi: ScreenHeight() - y); |
| 141 | |
| 142 | m_State.m_ClipEnable = true; |
| 143 | m_State.m_ClipX = x; |
| 144 | m_State.m_ClipY = ScreenHeight() - (y + h); |
| 145 | m_State.m_ClipW = w; |
| 146 | m_State.m_ClipH = h; |
| 147 | } |
| 148 | |
| 149 | void CGraphics_Threaded::ClipDisable() |
| 150 | { |
| 151 | m_State.m_ClipEnable = false; |
| 152 | } |
| 153 | |
| 154 | void CGraphics_Threaded::BlendNone() |
| 155 | { |
| 156 | m_State.m_BlendMode = EBlendMode::NONE; |
| 157 | } |
| 158 | |
| 159 | void CGraphics_Threaded::BlendNormal() |
| 160 | { |
| 161 | m_State.m_BlendMode = EBlendMode::ALPHA; |
| 162 | } |
| 163 | |
| 164 | void CGraphics_Threaded::BlendAdditive() |
| 165 | { |
| 166 | m_State.m_BlendMode = EBlendMode::ADDITIVE; |
| 167 | } |
| 168 | |
| 169 | void CGraphics_Threaded::WrapNormal() |
| 170 | { |
| 171 | m_State.m_WrapMode = EWrapMode::REPEAT; |
| 172 | } |
| 173 | |
| 174 | void CGraphics_Threaded::WrapClamp() |
| 175 | { |
| 176 | m_State.m_WrapMode = EWrapMode::CLAMP; |
| 177 | } |
| 178 | |
| 179 | uint64_t CGraphics_Threaded::TextureMemoryUsage() const |
| 180 | { |
| 181 | return m_pBackend->TextureMemoryUsage(); |
| 182 | } |
| 183 | |
| 184 | uint64_t CGraphics_Threaded::BufferMemoryUsage() const |
| 185 | { |
| 186 | return m_pBackend->BufferMemoryUsage(); |
| 187 | } |
| 188 | |
| 189 | uint64_t CGraphics_Threaded::StreamedMemoryUsage() const |
| 190 | { |
| 191 | return m_pBackend->StreamedMemoryUsage(); |
| 192 | } |
| 193 | |
| 194 | uint64_t CGraphics_Threaded::StagingMemoryUsage() const |
| 195 | { |
| 196 | return m_pBackend->StagingMemoryUsage(); |
| 197 | } |
| 198 | |
| 199 | const TTwGraphicsGpuList &CGraphics_Threaded::GetGpus() const |
| 200 | { |
| 201 | return m_pBackend->GetGpus(); |
| 202 | } |
| 203 | |
| 204 | void CGraphics_Threaded::MapScreen(const CScreenRect &ScreenRect) |
| 205 | { |
| 206 | m_State.m_ScreenTL = ScreenRect.m_TopLeft; |
| 207 | m_State.m_ScreenBR = ScreenRect.m_BottomRight; |
| 208 | } |
| 209 | |
| 210 | CScreenRect CGraphics_Threaded::GetScreen() const |
| 211 | { |
| 212 | return CScreenRect(m_State.m_ScreenTL, m_State.m_ScreenBR); |
| 213 | } |
| 214 | |
| 215 | void CGraphics_Threaded::LinesBegin() |
| 216 | { |
| 217 | dbg_assert(m_Drawing == EDrawing::NONE, "called Graphics()->LinesBegin twice" ); |
| 218 | m_Drawing = EDrawing::LINES; |
| 219 | SetColor(ColorRGBA(1.0f, 1.0f, 1.0f, 1.0f)); |
| 220 | } |
| 221 | |
| 222 | void CGraphics_Threaded::LinesEnd() |
| 223 | { |
| 224 | dbg_assert(m_Drawing == EDrawing::LINES, "called Graphics()->LinesEnd without begin" ); |
| 225 | FlushVertices(); |
| 226 | m_Drawing = EDrawing::NONE; |
| 227 | } |
| 228 | |
| 229 | void CGraphics_Threaded::LinesDraw(const CLineItem *pArray, size_t Num) |
| 230 | { |
| 231 | dbg_assert(m_Drawing == EDrawing::LINES, "called Graphics()->LinesDraw without begin" ); |
| 232 | |
| 233 | size_t VertexIndex = m_NumVertices; |
| 234 | for(size_t i = 0; i < Num; ++i) |
| 235 | { |
| 236 | m_aVertices[VertexIndex].m_Pos.x = pArray[i].m_X0; |
| 237 | m_aVertices[VertexIndex].m_Pos.y = pArray[i].m_Y0; |
| 238 | m_aVertices[VertexIndex].m_Tex = m_aTexture[0]; |
| 239 | m_aVertices[VertexIndex].m_Color = m_aColor[0]; |
| 240 | ++VertexIndex; |
| 241 | |
| 242 | m_aVertices[VertexIndex].m_Pos.x = pArray[i].m_X1; |
| 243 | m_aVertices[VertexIndex].m_Pos.y = pArray[i].m_Y1; |
| 244 | m_aVertices[VertexIndex].m_Tex = m_aTexture[1]; |
| 245 | m_aVertices[VertexIndex].m_Color = m_aColor[1]; |
| 246 | ++VertexIndex; |
| 247 | } |
| 248 | |
| 249 | AddVertices(Count: 2 * Num); |
| 250 | } |
| 251 | |
| 252 | void CGraphics_Threaded::LinesBatchBegin(CLineItemBatch *pBatch) |
| 253 | { |
| 254 | pBatch->m_NumItems = 0; |
| 255 | LinesBegin(); |
| 256 | } |
| 257 | |
| 258 | void CGraphics_Threaded::LinesBatchEnd(CLineItemBatch *pBatch) |
| 259 | { |
| 260 | if(pBatch->m_NumItems > 0) |
| 261 | { |
| 262 | LinesDraw(pArray: pBatch->m_aItems, Num: pBatch->m_NumItems); |
| 263 | pBatch->m_NumItems = 0; |
| 264 | } |
| 265 | LinesEnd(); |
| 266 | } |
| 267 | |
| 268 | void CGraphics_Threaded::LinesBatchDraw(CLineItemBatch *pBatch, const CLineItem *pArray, size_t Num) |
| 269 | { |
| 270 | if(pBatch->m_NumItems + Num >= std::size(pBatch->m_aItems)) |
| 271 | { |
| 272 | LinesDraw(pArray: pBatch->m_aItems, Num: pBatch->m_NumItems); |
| 273 | pBatch->m_NumItems = 0; |
| 274 | } |
| 275 | if(Num >= std::size(pBatch->m_aItems)) |
| 276 | { |
| 277 | LinesDraw(pArray, Num); |
| 278 | return; |
| 279 | } |
| 280 | std::copy(first: pArray, last: pArray + Num, result: pBatch->m_aItems + pBatch->m_NumItems); |
| 281 | pBatch->m_NumItems += Num; |
| 282 | } |
| 283 | |
| 284 | IGraphics::CTextureHandle CGraphics_Threaded::FindFreeTextureIndex() |
| 285 | { |
| 286 | const size_t CurSize = m_vTextureIndices.size(); |
| 287 | if(m_FirstFreeTexture == CurSize) |
| 288 | { |
| 289 | m_vTextureIndices.resize(sz: CurSize * 2); |
| 290 | for(size_t i = 0; i < CurSize; ++i) |
| 291 | m_vTextureIndices[CurSize + i] = CurSize + i + 1; |
| 292 | } |
| 293 | const size_t Tex = m_FirstFreeTexture; |
| 294 | m_FirstFreeTexture = m_vTextureIndices[Tex]; |
| 295 | m_vTextureIndices[Tex] = -1; |
| 296 | return CreateTextureHandle(Index: Tex); |
| 297 | } |
| 298 | |
| 299 | void CGraphics_Threaded::FreeTextureIndex(CTextureHandle *pIndex) |
| 300 | { |
| 301 | dbg_assert(pIndex->IsValid(), "Cannot free invalid texture index" ); |
| 302 | dbg_assert(m_vTextureIndices[pIndex->Id()] == -1, "Cannot free already freed texture index" ); |
| 303 | |
| 304 | m_vTextureIndices[pIndex->Id()] = m_FirstFreeTexture; |
| 305 | m_FirstFreeTexture = pIndex->Id(); |
| 306 | pIndex->Invalidate(); |
| 307 | } |
| 308 | |
| 309 | void CGraphics_Threaded::UnloadTexture(CTextureHandle *pIndex) |
| 310 | { |
| 311 | if(pIndex->IsNullTexture() || !pIndex->IsValid()) |
| 312 | return; |
| 313 | |
| 314 | CCommandBuffer::SCommand_Texture_Destroy Cmd; |
| 315 | Cmd.m_Slot = pIndex->Id(); |
| 316 | AddCmd(Cmd); |
| 317 | |
| 318 | FreeTextureIndex(pIndex); |
| 319 | } |
| 320 | |
| 321 | IGraphics::CTextureHandle CGraphics_Threaded::LoadSpriteTexture(const CImageInfo &FromImageInfo, const std::optional<CImageInfo> &FallbackImageInfo, const CDataSprite *pSprite) |
| 322 | { |
| 323 | int ImageGridX = FromImageInfo.m_Width / pSprite->m_pSet->m_Gridx; |
| 324 | int ImageGridY = FromImageInfo.m_Height / pSprite->m_pSet->m_Gridy; |
| 325 | int x = pSprite->m_X * ImageGridX; |
| 326 | int y = pSprite->m_Y * ImageGridY; |
| 327 | int w = pSprite->m_W * ImageGridX; |
| 328 | int h = pSprite->m_H * ImageGridY; |
| 329 | |
| 330 | // check for invisible texture, maybe due to outdated game assets |
| 331 | if(FallbackImageInfo.has_value() && IsImageSubFullyTransparent(FromImageInfo, x, y, w, h)) |
| 332 | { |
| 333 | log_warn("graphics" , "Asset '%s' appears to be invisible, falling back to default" , pSprite->m_pName); |
| 334 | return LoadSpriteTexture(FromImageInfo: FallbackImageInfo.value(), FallbackImageInfo: std::nullopt, pSprite); |
| 335 | } |
| 336 | |
| 337 | CImageInfo SpriteInfo; |
| 338 | SpriteInfo.m_Width = w; |
| 339 | SpriteInfo.m_Height = h; |
| 340 | SpriteInfo.m_Format = FromImageInfo.m_Format; |
| 341 | SpriteInfo.Allocate(); |
| 342 | SpriteInfo.CopyRectFrom(SrcImage: FromImageInfo, SrcX: x, SrcY: y, Width: w, Height: h, DestX: 0, DestY: 0); |
| 343 | return LoadTextureRawMove(Image&: SpriteInfo, Flags: 0, pTexName: pSprite->m_pName); |
| 344 | } |
| 345 | |
| 346 | bool CGraphics_Threaded::IsImageSubFullyTransparent(const CImageInfo &FromImageInfo, int x, int y, int w, int h) |
| 347 | { |
| 348 | if(FromImageInfo.m_Format == CImageInfo::FORMAT_R || FromImageInfo.m_Format == CImageInfo::FORMAT_RA || FromImageInfo.m_Format == CImageInfo::FORMAT_RGBA) |
| 349 | { |
| 350 | const uint8_t *pImgData = FromImageInfo.m_pData; |
| 351 | const size_t PixelSize = FromImageInfo.PixelSize(); |
| 352 | for(int iy = 0; iy < h; ++iy) |
| 353 | { |
| 354 | for(int ix = 0; ix < w; ++ix) |
| 355 | { |
| 356 | const size_t RealOffset = (x + ix) * PixelSize + (y + iy) * PixelSize * FromImageInfo.m_Width; |
| 357 | if(pImgData[RealOffset + (PixelSize - 1)] > 0) |
| 358 | return false; |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | return true; |
| 363 | } |
| 364 | return false; |
| 365 | } |
| 366 | |
| 367 | bool CGraphics_Threaded::IsSpriteTextureFullyTransparent(const CImageInfo &FromImageInfo, const CDataSprite *pSprite) |
| 368 | { |
| 369 | int ImageGridX = FromImageInfo.m_Width / pSprite->m_pSet->m_Gridx; |
| 370 | int ImageGridY = FromImageInfo.m_Height / pSprite->m_pSet->m_Gridy; |
| 371 | int x = pSprite->m_X * ImageGridX; |
| 372 | int y = pSprite->m_Y * ImageGridY; |
| 373 | int w = pSprite->m_W * ImageGridX; |
| 374 | int h = pSprite->m_H * ImageGridY; |
| 375 | return IsImageSubFullyTransparent(FromImageInfo, x, y, w, h); |
| 376 | } |
| 377 | |
| 378 | void CGraphics_Threaded::LoadTextureAddWarning(size_t Width, size_t Height, int Flags, const char *pTexName) |
| 379 | { |
| 380 | if((Flags & IGraphics::TEXLOAD_TO_2D_ARRAY_TEXTURE) != 0 || (Flags & IGraphics::TEXLOAD_TO_3D_TEXTURE) != 0) |
| 381 | { |
| 382 | if(Width == 0 || (Width % 16) != 0 || Height == 0 || (Height % 16) != 0) |
| 383 | { |
| 384 | SWarning NewWarning; |
| 385 | char aText[128]; |
| 386 | str_format(buffer: aText, buffer_size: sizeof(aText), format: "\"%s\"" , pTexName ? pTexName : "(no name)" ); |
| 387 | str_format(buffer: NewWarning.m_aWarningMsg, buffer_size: sizeof(NewWarning.m_aWarningMsg), format: Localize(pStr: "The width of texture %s is not divisible by %d, or the height is not divisible by %d, which might cause visual bugs." ), aText, 16, 16); |
| 388 | AddWarning(Warning: NewWarning); |
| 389 | } |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | static CCommandBuffer::SCommand_Texture_Create LoadTextureCreateCommand(int TextureId, size_t Width, size_t Height, int Flags) |
| 394 | { |
| 395 | CCommandBuffer::SCommand_Texture_Create Cmd; |
| 396 | Cmd.m_Slot = TextureId; |
| 397 | Cmd.m_Width = Width; |
| 398 | Cmd.m_Height = Height; |
| 399 | |
| 400 | Cmd.m_Flags = 0; |
| 401 | if((Flags & IGraphics::TEXLOAD_TO_2D_ARRAY_TEXTURE) != 0) |
| 402 | Cmd.m_Flags |= TextureFlag::TO_2D_ARRAY_TEXTURE; |
| 403 | if((Flags & IGraphics::TEXLOAD_TO_3D_TEXTURE) != 0) |
| 404 | Cmd.m_Flags |= TextureFlag::TO_3D_TEXTURE; |
| 405 | if((Flags & IGraphics::TEXLOAD_NO_2D_TEXTURE) != 0) |
| 406 | Cmd.m_Flags |= TextureFlag::NO_2D_TEXTURE; |
| 407 | |
| 408 | return Cmd; |
| 409 | } |
| 410 | |
| 411 | IGraphics::CTextureHandle CGraphics_Threaded::LoadTextureRaw(const CImageInfo &Image, int Flags, const char *pTexName) |
| 412 | { |
| 413 | LoadTextureAddWarning(Width: Image.m_Width, Height: Image.m_Height, Flags, pTexName); |
| 414 | |
| 415 | if(Image.m_Width == 0 || Image.m_Height == 0) |
| 416 | return IGraphics::CTextureHandle(); |
| 417 | |
| 418 | IGraphics::CTextureHandle TextureHandle = FindFreeTextureIndex(); |
| 419 | CCommandBuffer::SCommand_Texture_Create Cmd = LoadTextureCreateCommand(TextureId: TextureHandle.Id(), Width: Image.m_Width, Height: Image.m_Height, Flags); |
| 420 | |
| 421 | // Copy texture data and convert if necessary |
| 422 | uint8_t *pTmpData; |
| 423 | if(!ConvertToRgbaAlloc(pDest&: pTmpData, SourceImage: Image)) |
| 424 | { |
| 425 | log_warn("graphics" , "Converted image '%s' to RGBA, consider making its file format RGBA." , pTexName ? pTexName : "(no name)" ); |
| 426 | } |
| 427 | Cmd.m_pData = pTmpData; |
| 428 | |
| 429 | AddCmd(Cmd); |
| 430 | |
| 431 | return TextureHandle; |
| 432 | } |
| 433 | |
| 434 | IGraphics::CTextureHandle CGraphics_Threaded::LoadTextureRawMove(CImageInfo &Image, int Flags, const char *pTexName) |
| 435 | { |
| 436 | if(Image.m_Format != CImageInfo::FORMAT_RGBA) |
| 437 | { |
| 438 | // Moving not possible, texture needs to be converted |
| 439 | IGraphics::CTextureHandle TextureHandle = LoadTextureRaw(Image, Flags, pTexName); |
| 440 | Image.Free(); |
| 441 | return TextureHandle; |
| 442 | } |
| 443 | |
| 444 | LoadTextureAddWarning(Width: Image.m_Width, Height: Image.m_Height, Flags, pTexName); |
| 445 | |
| 446 | if(Image.m_Width == 0 || Image.m_Height == 0) |
| 447 | return IGraphics::CTextureHandle(); |
| 448 | |
| 449 | IGraphics::CTextureHandle TextureHandle = FindFreeTextureIndex(); |
| 450 | CCommandBuffer::SCommand_Texture_Create Cmd = LoadTextureCreateCommand(TextureId: TextureHandle.Id(), Width: Image.m_Width, Height: Image.m_Height, Flags); |
| 451 | Cmd.m_pData = Image.m_pData; |
| 452 | Image.m_pData = nullptr; |
| 453 | Image.Free(); |
| 454 | AddCmd(Cmd); |
| 455 | |
| 456 | return TextureHandle; |
| 457 | } |
| 458 | |
| 459 | IGraphics::CTextureHandle CGraphics_Threaded::LoadTexture(const char *pFilename, int StorageType, int Flags) |
| 460 | { |
| 461 | dbg_assert(pFilename[0] != '\0', "Cannot load texture from file with empty filename" ); // would cause Valgrind to crash otherwise |
| 462 | |
| 463 | CImageInfo Image; |
| 464 | if(LoadPng(Image, pFilename, StorageType)) |
| 465 | { |
| 466 | CTextureHandle Id = LoadTextureRawMove(Image, Flags, pTexName: pFilename); |
| 467 | if(Id.IsValid()) |
| 468 | { |
| 469 | if(g_Config.m_Debug) |
| 470 | log_trace("graphics/texture" , "Loaded texture '%s'" , pFilename); |
| 471 | return Id; |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | return m_NullTexture; |
| 476 | } |
| 477 | |
| 478 | bool CGraphics_Threaded::LoadTextTextures(size_t Width, size_t Height, CTextureHandle &TextTexture, CTextureHandle &TextOutlineTexture, uint8_t *pTextData, uint8_t *pTextOutlineData) |
| 479 | { |
| 480 | if(Width == 0 || Height == 0) |
| 481 | return false; |
| 482 | |
| 483 | TextTexture = FindFreeTextureIndex(); |
| 484 | TextOutlineTexture = FindFreeTextureIndex(); |
| 485 | |
| 486 | CCommandBuffer::SCommand_TextTextures_Create Cmd; |
| 487 | Cmd.m_Slot = TextTexture.Id(); |
| 488 | Cmd.m_SlotOutline = TextOutlineTexture.Id(); |
| 489 | Cmd.m_Width = Width; |
| 490 | Cmd.m_Height = Height; |
| 491 | Cmd.m_pTextData = pTextData; |
| 492 | Cmd.m_pTextOutlineData = pTextOutlineData; |
| 493 | AddCmd(Cmd); |
| 494 | |
| 495 | return true; |
| 496 | } |
| 497 | |
| 498 | bool CGraphics_Threaded::UnloadTextTextures(CTextureHandle &TextTexture, CTextureHandle &TextOutlineTexture) |
| 499 | { |
| 500 | CCommandBuffer::SCommand_TextTextures_Destroy Cmd; |
| 501 | Cmd.m_Slot = TextTexture.Id(); |
| 502 | Cmd.m_SlotOutline = TextOutlineTexture.Id(); |
| 503 | AddCmd(Cmd); |
| 504 | |
| 505 | if(TextTexture.IsValid()) |
| 506 | FreeTextureIndex(pIndex: &TextTexture); |
| 507 | if(TextOutlineTexture.IsValid()) |
| 508 | FreeTextureIndex(pIndex: &TextOutlineTexture); |
| 509 | return true; |
| 510 | } |
| 511 | |
| 512 | bool CGraphics_Threaded::UpdateTextTexture(CTextureHandle TextureId, int x, int y, size_t Width, size_t Height, uint8_t *pData, bool IsMovedPointer) |
| 513 | { |
| 514 | CCommandBuffer::SCommand_TextTexture_Update Cmd; |
| 515 | Cmd.m_Slot = TextureId.Id(); |
| 516 | Cmd.m_X = x; |
| 517 | Cmd.m_Y = y; |
| 518 | Cmd.m_Width = Width; |
| 519 | Cmd.m_Height = Height; |
| 520 | |
| 521 | if(IsMovedPointer) |
| 522 | { |
| 523 | Cmd.m_pData = pData; |
| 524 | } |
| 525 | else |
| 526 | { |
| 527 | const size_t MemSize = Width * Height; |
| 528 | uint8_t *pTmpData = static_cast<uint8_t *>(malloc(size: MemSize)); |
| 529 | mem_copy(dest: pTmpData, source: pData, size: MemSize); |
| 530 | Cmd.m_pData = pTmpData; |
| 531 | } |
| 532 | AddCmd(Cmd); |
| 533 | |
| 534 | return true; |
| 535 | } |
| 536 | |
| 537 | static SWarning FormatPngliteIncompatibilityWarning(int PngliteIncompatible, const char *pContextName) |
| 538 | { |
| 539 | SWarning Warning; |
| 540 | str_format(buffer: Warning.m_aWarningMsg, buffer_size: sizeof(Warning.m_aWarningMsg), format: Localize(pStr: "\"%s\" is not compatible with pnglite and cannot be loaded by old DDNet versions:" ), pContextName); |
| 541 | str_append(dst&: Warning.m_aWarningMsg, src: " " ); |
| 542 | static const int FLAGS[] = {CImageLoader::PNGLITE_COLOR_TYPE, CImageLoader::PNGLITE_BIT_DEPTH, CImageLoader::PNGLITE_INTERLACE_TYPE, CImageLoader::PNGLITE_COMPRESSION_TYPE, CImageLoader::PNGLITE_FILTER_TYPE}; |
| 543 | static const char *const EXPLANATION[] = {"color type" , "bit depth" , "interlace type" , "compression type" , "filter type" }; |
| 544 | |
| 545 | bool First = true; |
| 546 | for(size_t i = 0; i < std::size(FLAGS); ++i) |
| 547 | { |
| 548 | if((PngliteIncompatible & FLAGS[i]) != 0) |
| 549 | { |
| 550 | if(!First) |
| 551 | { |
| 552 | str_append(dst&: Warning.m_aWarningMsg, src: ", " ); |
| 553 | } |
| 554 | str_append(dst&: Warning.m_aWarningMsg, src: EXPLANATION[i]); |
| 555 | First = false; |
| 556 | } |
| 557 | } |
| 558 | str_append(dst&: Warning.m_aWarningMsg, src: " unsupported" ); |
| 559 | return Warning; |
| 560 | } |
| 561 | |
| 562 | bool CGraphics_Threaded::LoadPng(CImageInfo &Image, const char *pFilename, int StorageType) |
| 563 | { |
| 564 | IOHANDLE File = m_pStorage->OpenFile(pFilename, Flags: IOFLAG_READ, Type: StorageType); |
| 565 | |
| 566 | int PngliteIncompatible; |
| 567 | if(!CImageLoader::LoadPng(File, pFilename, Image, PngliteIncompatible)) |
| 568 | return false; |
| 569 | |
| 570 | if(m_WarnPngliteIncompatibleImages && PngliteIncompatible != 0) |
| 571 | { |
| 572 | AddWarning(Warning: FormatPngliteIncompatibilityWarning(PngliteIncompatible, pContextName: pFilename)); |
| 573 | } |
| 574 | |
| 575 | return true; |
| 576 | } |
| 577 | |
| 578 | bool CGraphics_Threaded::LoadPng(CImageInfo &Image, const uint8_t *pData, size_t DataSize, const char *pContextName) |
| 579 | { |
| 580 | CByteBufferReader Reader(pData, DataSize); |
| 581 | int PngliteIncompatible; |
| 582 | if(!CImageLoader::LoadPng(Reader, pContextName, Image, PngliteIncompatible)) |
| 583 | return false; |
| 584 | |
| 585 | if(m_WarnPngliteIncompatibleImages && PngliteIncompatible != 0) |
| 586 | { |
| 587 | AddWarning(Warning: FormatPngliteIncompatibilityWarning(PngliteIncompatible, pContextName)); |
| 588 | } |
| 589 | |
| 590 | return true; |
| 591 | } |
| 592 | |
| 593 | bool CGraphics_Threaded::CheckImageDivisibility(const char *pContextName, CImageInfo &Image, int DivX, int DivY, bool AllowResize) |
| 594 | { |
| 595 | dbg_assert(DivX != 0 && DivY != 0, "Passing 0 to this function is not allowed." ); |
| 596 | bool ImageIsValid = true; |
| 597 | bool WidthBroken = Image.m_Width == 0 || (Image.m_Width % DivX) != 0; |
| 598 | bool HeightBroken = Image.m_Height == 0 || (Image.m_Height % DivY) != 0; |
| 599 | if(WidthBroken || HeightBroken) |
| 600 | { |
| 601 | SWarning NewWarning; |
| 602 | char aContextNameQuoted[128]; |
| 603 | str_format(buffer: aContextNameQuoted, buffer_size: sizeof(aContextNameQuoted), format: "\"%s\"" , pContextName); |
| 604 | str_format(buffer: NewWarning.m_aWarningMsg, buffer_size: sizeof(NewWarning.m_aWarningMsg), |
| 605 | format: Localize(pStr: "The width of texture %s is not divisible by %d, or the height is not divisible by %d, which might cause visual bugs." ), aContextNameQuoted, DivX, DivY); |
| 606 | AddWarning(Warning: NewWarning); |
| 607 | ImageIsValid = false; |
| 608 | } |
| 609 | |
| 610 | if(AllowResize && !ImageIsValid && Image.m_Width > 0 && Image.m_Height > 0) |
| 611 | { |
| 612 | int NewWidth = DivX; |
| 613 | int NewHeight = DivY; |
| 614 | if(WidthBroken) |
| 615 | { |
| 616 | NewWidth = std::max(a: HighestBit(OfVar: Image.m_Width), b: DivX); |
| 617 | NewHeight = (NewWidth / DivX) * DivY; |
| 618 | } |
| 619 | else |
| 620 | { |
| 621 | NewHeight = std::max(a: HighestBit(OfVar: Image.m_Height), b: DivY); |
| 622 | NewWidth = (NewHeight / DivY) * DivX; |
| 623 | } |
| 624 | ResizeImage(Image, NewWidth, NewHeight); |
| 625 | ImageIsValid = true; |
| 626 | } |
| 627 | |
| 628 | return ImageIsValid; |
| 629 | } |
| 630 | |
| 631 | bool CGraphics_Threaded::IsImageFormatRgba(const char *pContextName, const CImageInfo &Image) |
| 632 | { |
| 633 | if(Image.m_Format != CImageInfo::FORMAT_RGBA) |
| 634 | { |
| 635 | SWarning NewWarning; |
| 636 | char aContextNameQuoted[128]; |
| 637 | str_format(buffer: aContextNameQuoted, buffer_size: sizeof(aContextNameQuoted), format: "\"%s\"" , pContextName); |
| 638 | str_format(buffer: NewWarning.m_aWarningMsg, buffer_size: sizeof(NewWarning.m_aWarningMsg), |
| 639 | format: Localize(pStr: "The format of texture %s is not RGBA which will cause visual bugs." ), aContextNameQuoted); |
| 640 | AddWarning(Warning: NewWarning); |
| 641 | return false; |
| 642 | } |
| 643 | return true; |
| 644 | } |
| 645 | |
| 646 | void CGraphics_Threaded::KickCommandBuffer() |
| 647 | { |
| 648 | m_pBackend->RunBuffer(pBuffer: m_pCommandBuffer); |
| 649 | |
| 650 | std::vector<std::string> WarningStrings; |
| 651 | if(m_pBackend->GetWarning(WarningStrings)) |
| 652 | { |
| 653 | SWarning NewWarning; |
| 654 | std::string WarningStr; |
| 655 | for(const auto &WarnStr : WarningStrings) |
| 656 | WarningStr.append(str: (WarnStr + "\n" )); |
| 657 | str_copy(dst&: NewWarning.m_aWarningMsg, src: WarningStr.c_str()); |
| 658 | AddWarning(Warning: NewWarning); |
| 659 | } |
| 660 | |
| 661 | // swap buffer |
| 662 | m_CurrentCommandBuffer ^= 1; |
| 663 | m_pCommandBuffer = m_apCommandBuffers[m_CurrentCommandBuffer]; |
| 664 | m_pCommandBuffer->Reset(); |
| 665 | } |
| 666 | |
| 667 | class CScreenshotSaveJob : public IJob |
| 668 | { |
| 669 | IStorage *m_pStorage; |
| 670 | char m_aName[IO_MAX_PATH_LENGTH]; |
| 671 | CImageInfo m_Image; |
| 672 | |
| 673 | void Run() override |
| 674 | { |
| 675 | static constexpr LOG_COLOR SCREENSHOT_LOG_COLOR = LOG_COLOR{.r: 255, .g: 153, .b: 76}; |
| 676 | char aWholePath[IO_MAX_PATH_LENGTH]; |
| 677 | if(CImageLoader::SavePng(File: m_pStorage->OpenFile(pFilename: m_aName, Flags: IOFLAG_WRITE, Type: IStorage::TYPE_SAVE, pBuffer: aWholePath, BufferSize: sizeof(aWholePath)), pFilename: m_aName, Image: m_Image)) |
| 678 | { |
| 679 | log_info_color(SCREENSHOT_LOG_COLOR, "client" , "Saved screenshot to '%s'" , aWholePath); |
| 680 | } |
| 681 | else |
| 682 | { |
| 683 | log_error_color(SCREENSHOT_LOG_COLOR, "client" , "Failed to save screenshot to '%s'" , aWholePath); |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | public: |
| 688 | CScreenshotSaveJob(IStorage *pStorage, const char *pName, CImageInfo &&Image) : |
| 689 | m_pStorage(pStorage), |
| 690 | m_Image(std::move(Image)) |
| 691 | { |
| 692 | str_copy(dst&: m_aName, src: pName); |
| 693 | } |
| 694 | |
| 695 | ~CScreenshotSaveJob() override |
| 696 | { |
| 697 | m_Image.Free(); |
| 698 | } |
| 699 | }; |
| 700 | |
| 701 | void CGraphics_Threaded::ScreenshotDirect(bool *pSwapped) |
| 702 | { |
| 703 | if(!m_DoScreenshot) |
| 704 | return; |
| 705 | m_DoScreenshot = false; |
| 706 | if(!WindowActive()) |
| 707 | return; |
| 708 | |
| 709 | CImageInfo Image; |
| 710 | |
| 711 | CCommandBuffer::SCommand_TrySwapAndScreenshot Cmd; |
| 712 | Cmd.m_pImage = &Image; |
| 713 | Cmd.m_pSwapped = pSwapped; |
| 714 | AddCmd(Cmd); |
| 715 | |
| 716 | KickCommandBuffer(); |
| 717 | WaitForIdle(); |
| 718 | |
| 719 | if(Image.m_pData) |
| 720 | { |
| 721 | m_pEngine->AddJob(pJob: std::make_shared<CScreenshotSaveJob>(args&: m_pStorage, args&: m_aScreenshotName, args: std::move(Image))); |
| 722 | } |
| 723 | else |
| 724 | { |
| 725 | log_error("graphics" , "Failed to create screenshot" ); |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | void CGraphics_Threaded::TextureSet(CTextureHandle TextureId) |
| 730 | { |
| 731 | dbg_assert(m_Drawing == EDrawing::NONE, "called Graphics()->TextureSet within begin" ); |
| 732 | dbg_assert(!TextureId.IsValid() || m_vTextureIndices[TextureId.Id()] == -1, "Texture handle was not invalid, but also did not correlate to an existing texture." ); |
| 733 | m_State.m_Texture = TextureId.Id(); |
| 734 | } |
| 735 | |
| 736 | void CGraphics_Threaded::Clear(float r, float g, float b, bool ForceClearNow) |
| 737 | { |
| 738 | CCommandBuffer::SCommand_Clear Cmd; |
| 739 | Cmd.m_Color.r = r; |
| 740 | Cmd.m_Color.g = g; |
| 741 | Cmd.m_Color.b = b; |
| 742 | Cmd.m_Color.a = 0; |
| 743 | Cmd.m_ForceClear = ForceClearNow; |
| 744 | AddCmd(Cmd); |
| 745 | } |
| 746 | |
| 747 | void CGraphics_Threaded::QuadsBegin() |
| 748 | { |
| 749 | dbg_assert(m_Drawing == EDrawing::NONE, "called Graphics()->QuadsBegin twice" ); |
| 750 | m_Drawing = EDrawing::QUADS; |
| 751 | |
| 752 | QuadsSetSubset(TlU: 0, TlV: 0, BrU: 1, BrV: 1); |
| 753 | QuadsSetRotation(Angle: 0); |
| 754 | SetColor(ColorRGBA(1.0f, 1.0f, 1.0f, 1.0f)); |
| 755 | } |
| 756 | |
| 757 | void CGraphics_Threaded::QuadsEnd() |
| 758 | { |
| 759 | dbg_assert(m_Drawing == EDrawing::QUADS, "called Graphics()->QuadsEnd without begin" ); |
| 760 | FlushVertices(); |
| 761 | m_Drawing = EDrawing::NONE; |
| 762 | } |
| 763 | |
| 764 | void CGraphics_Threaded::QuadsTex3DBegin() |
| 765 | { |
| 766 | QuadsBegin(); |
| 767 | } |
| 768 | |
| 769 | void CGraphics_Threaded::QuadsTex3DEnd() |
| 770 | { |
| 771 | dbg_assert(m_Drawing == EDrawing::QUADS, "called Graphics()->QuadsEnd without begin" ); |
| 772 | FlushVerticesTex3D(); |
| 773 | m_Drawing = EDrawing::NONE; |
| 774 | } |
| 775 | |
| 776 | void CGraphics_Threaded::TrianglesBegin() |
| 777 | { |
| 778 | dbg_assert(m_Drawing == EDrawing::NONE, "called Graphics()->TrianglesBegin twice" ); |
| 779 | m_Drawing = EDrawing::TRIANGLES; |
| 780 | |
| 781 | QuadsSetSubset(TlU: 0, TlV: 0, BrU: 1, BrV: 1); |
| 782 | QuadsSetRotation(Angle: 0); |
| 783 | SetColor(ColorRGBA(1.0f, 1.0f, 1.0f, 1.0f)); |
| 784 | } |
| 785 | |
| 786 | void CGraphics_Threaded::TrianglesEnd() |
| 787 | { |
| 788 | dbg_assert(m_Drawing == EDrawing::TRIANGLES, "called Graphics()->TrianglesEnd without begin" ); |
| 789 | FlushVertices(); |
| 790 | m_Drawing = EDrawing::NONE; |
| 791 | } |
| 792 | |
| 793 | void CGraphics_Threaded::QuadsEndKeepVertices() |
| 794 | { |
| 795 | dbg_assert(m_Drawing == EDrawing::QUADS, "called Graphics()->QuadsEndKeepVertices without begin" ); |
| 796 | FlushVertices(KeepVertices: true); |
| 797 | m_Drawing = EDrawing::NONE; |
| 798 | } |
| 799 | |
| 800 | void CGraphics_Threaded::QuadsDrawCurrentVertices(bool KeepVertices) |
| 801 | { |
| 802 | m_Drawing = EDrawing::QUADS; |
| 803 | FlushVertices(KeepVertices); |
| 804 | m_Drawing = EDrawing::NONE; |
| 805 | } |
| 806 | |
| 807 | void CGraphics_Threaded::QuadsSetRotation(float Angle) |
| 808 | { |
| 809 | m_Rotation = Angle; |
| 810 | } |
| 811 | |
| 812 | constexpr static unsigned char NormalizeColorComponent(float ColorComponent) |
| 813 | { |
| 814 | return (unsigned char)(std::clamp(val: ColorComponent, lo: 0.0f, hi: 1.0f) * 255.0f + 0.5f); // +0.5f to round to nearest |
| 815 | } |
| 816 | |
| 817 | constexpr static CCommandBuffer::SColor NormalizeColor(ColorRGBA Color) |
| 818 | { |
| 819 | CCommandBuffer::SColor NormalizedColor; |
| 820 | NormalizedColor.r = NormalizeColorComponent(ColorComponent: Color.r); |
| 821 | NormalizedColor.g = NormalizeColorComponent(ColorComponent: Color.g); |
| 822 | NormalizedColor.b = NormalizeColorComponent(ColorComponent: Color.b); |
| 823 | NormalizedColor.a = NormalizeColorComponent(ColorComponent: Color.a); |
| 824 | return NormalizedColor; |
| 825 | } |
| 826 | |
| 827 | void CGraphics_Threaded::SetColor(float r, float g, float b, float a) |
| 828 | { |
| 829 | SetColor(ColorRGBA(r, g, b, a)); |
| 830 | } |
| 831 | |
| 832 | void CGraphics_Threaded::SetColor(ColorRGBA Color) |
| 833 | { |
| 834 | std::fill(first: std::begin(arr&: m_aColor), last: std::end(arr&: m_aColor), value: NormalizeColor(Color)); |
| 835 | } |
| 836 | |
| 837 | void CGraphics_Threaded::SetColor2(ColorRGBA First, ColorRGBA Second) |
| 838 | { |
| 839 | dbg_assert(m_Drawing == EDrawing::LINES, "Called Graphics()->SetColor2 while not drawing lines" ); |
| 840 | |
| 841 | m_aColor[0] = NormalizeColor(Color: First); |
| 842 | m_aColor[1] = NormalizeColor(Color: Second); |
| 843 | } |
| 844 | |
| 845 | void CGraphics_Threaded::SetColor4(ColorRGBA TopLeft, ColorRGBA TopRight, ColorRGBA BottomLeft, ColorRGBA BottomRight) |
| 846 | { |
| 847 | dbg_assert(m_Drawing == EDrawing::QUADS || m_Drawing == EDrawing::TRIANGLES, "Called Graphics()->SetColor4 while not drawing quads or triangles" ); |
| 848 | |
| 849 | m_aColor[0] = NormalizeColor(Color: TopLeft); |
| 850 | m_aColor[1] = NormalizeColor(Color: TopRight); |
| 851 | m_aColor[2] = NormalizeColor(Color: BottomRight); |
| 852 | m_aColor[3] = NormalizeColor(Color: BottomLeft); |
| 853 | } |
| 854 | |
| 855 | void CGraphics_Threaded::ChangeColorOfQuadVertices(size_t QuadOffset, unsigned char r, unsigned char g, unsigned char b, unsigned char a) |
| 856 | { |
| 857 | const CCommandBuffer::SColor Color(r, g, b, a); |
| 858 | const size_t VertNum = g_Config.m_GfxQuadAsTriangle && !m_GLUseTrianglesAsQuad ? 6 : 4; |
| 859 | for(size_t i = 0; i < VertNum; ++i) |
| 860 | { |
| 861 | m_aVertices[QuadOffset * VertNum + i].m_Color = Color; |
| 862 | } |
| 863 | } |
| 864 | |
| 865 | void CGraphics_Threaded::QuadsSetSubset(float TlU, float TlV, float BrU, float BrV) |
| 866 | { |
| 867 | m_aTexture[0].u = TlU; |
| 868 | m_aTexture[1].u = BrU; |
| 869 | m_aTexture[0].v = TlV; |
| 870 | m_aTexture[1].v = TlV; |
| 871 | |
| 872 | m_aTexture[3].u = TlU; |
| 873 | m_aTexture[2].u = BrU; |
| 874 | m_aTexture[3].v = BrV; |
| 875 | m_aTexture[2].v = BrV; |
| 876 | } |
| 877 | |
| 878 | void CGraphics_Threaded::QuadsSetSubsetFree( |
| 879 | float x0, float y0, float x1, float y1, |
| 880 | float x2, float y2, float x3, float y3, int Index) |
| 881 | { |
| 882 | m_aTexture[0].u = x0; |
| 883 | m_aTexture[0].v = y0; |
| 884 | m_aTexture[1].u = x1; |
| 885 | m_aTexture[1].v = y1; |
| 886 | m_aTexture[2].u = x2; |
| 887 | m_aTexture[2].v = y2; |
| 888 | m_aTexture[3].u = x3; |
| 889 | m_aTexture[3].v = y3; |
| 890 | m_CurIndex = Index; |
| 891 | } |
| 892 | |
| 893 | void CGraphics_Threaded::QuadsDraw(CQuadItem *pArray, int Num) |
| 894 | { |
| 895 | for(int i = 0; i < Num; ++i) |
| 896 | { |
| 897 | pArray[i].m_X -= pArray[i].m_Width / 2; |
| 898 | pArray[i].m_Y -= pArray[i].m_Height / 2; |
| 899 | } |
| 900 | |
| 901 | QuadsDrawTL(pArray, Num); |
| 902 | } |
| 903 | |
| 904 | void CGraphics_Threaded::QuadsDrawTL(const CQuadItem *pArray, int Num) |
| 905 | { |
| 906 | QuadsDrawTLImpl(pVertices: m_aVertices, pArray, Num); |
| 907 | } |
| 908 | |
| 909 | void CGraphics_Threaded::QuadsTex3DDrawTL(const CQuadItem *pArray, int Num) |
| 910 | { |
| 911 | const int VertNum = g_Config.m_GfxQuadAsTriangle && !m_GLUseTrianglesAsQuad ? 6 : 4; |
| 912 | const float CurIndex = Uses2DTextureArrays() ? m_CurIndex : (m_CurIndex + 0.5f) / 256.0f; |
| 913 | |
| 914 | for(int i = 0; i < Num; ++i) |
| 915 | { |
| 916 | for(int n = 0; n < VertNum; ++n) |
| 917 | { |
| 918 | m_aVerticesTex3D[m_NumVertices + VertNum * i + n].m_Tex.w = CurIndex; |
| 919 | } |
| 920 | } |
| 921 | |
| 922 | QuadsDrawTLImpl(pVertices: m_aVerticesTex3D, pArray, Num); |
| 923 | } |
| 924 | |
| 925 | void CGraphics_Threaded::QuadsDrawFreeform(const CFreeformItem *pArray, int Num) |
| 926 | { |
| 927 | dbg_assert(m_Drawing == EDrawing::QUADS || m_Drawing == EDrawing::TRIANGLES, "called Graphics()->QuadsDrawFreeform without begin" ); |
| 928 | |
| 929 | if((g_Config.m_GfxQuadAsTriangle && !m_GLUseTrianglesAsQuad) || m_Drawing == EDrawing::TRIANGLES) |
| 930 | { |
| 931 | for(int i = 0; i < Num; ++i) |
| 932 | { |
| 933 | m_aVertices[m_NumVertices + 6 * i].m_Pos.x = pArray[i].m_X0; |
| 934 | m_aVertices[m_NumVertices + 6 * i].m_Pos.y = pArray[i].m_Y0; |
| 935 | m_aVertices[m_NumVertices + 6 * i].m_Tex = m_aTexture[0]; |
| 936 | m_aVertices[m_NumVertices + 6 * i].m_Color = m_aColor[0]; |
| 937 | |
| 938 | m_aVertices[m_NumVertices + 6 * i + 1].m_Pos.x = pArray[i].m_X1; |
| 939 | m_aVertices[m_NumVertices + 6 * i + 1].m_Pos.y = pArray[i].m_Y1; |
| 940 | m_aVertices[m_NumVertices + 6 * i + 1].m_Tex = m_aTexture[1]; |
| 941 | m_aVertices[m_NumVertices + 6 * i + 1].m_Color = m_aColor[1]; |
| 942 | |
| 943 | m_aVertices[m_NumVertices + 6 * i + 2].m_Pos.x = pArray[i].m_X3; |
| 944 | m_aVertices[m_NumVertices + 6 * i + 2].m_Pos.y = pArray[i].m_Y3; |
| 945 | m_aVertices[m_NumVertices + 6 * i + 2].m_Tex = m_aTexture[3]; |
| 946 | m_aVertices[m_NumVertices + 6 * i + 2].m_Color = m_aColor[3]; |
| 947 | |
| 948 | m_aVertices[m_NumVertices + 6 * i + 3].m_Pos.x = pArray[i].m_X0; |
| 949 | m_aVertices[m_NumVertices + 6 * i + 3].m_Pos.y = pArray[i].m_Y0; |
| 950 | m_aVertices[m_NumVertices + 6 * i + 3].m_Tex = m_aTexture[0]; |
| 951 | m_aVertices[m_NumVertices + 6 * i + 3].m_Color = m_aColor[0]; |
| 952 | |
| 953 | m_aVertices[m_NumVertices + 6 * i + 4].m_Pos.x = pArray[i].m_X3; |
| 954 | m_aVertices[m_NumVertices + 6 * i + 4].m_Pos.y = pArray[i].m_Y3; |
| 955 | m_aVertices[m_NumVertices + 6 * i + 4].m_Tex = m_aTexture[3]; |
| 956 | m_aVertices[m_NumVertices + 6 * i + 4].m_Color = m_aColor[3]; |
| 957 | |
| 958 | m_aVertices[m_NumVertices + 6 * i + 5].m_Pos.x = pArray[i].m_X2; |
| 959 | m_aVertices[m_NumVertices + 6 * i + 5].m_Pos.y = pArray[i].m_Y2; |
| 960 | m_aVertices[m_NumVertices + 6 * i + 5].m_Tex = m_aTexture[2]; |
| 961 | m_aVertices[m_NumVertices + 6 * i + 5].m_Color = m_aColor[2]; |
| 962 | } |
| 963 | |
| 964 | AddVertices(Count: 3 * 2 * Num); |
| 965 | } |
| 966 | else |
| 967 | { |
| 968 | for(int i = 0; i < Num; ++i) |
| 969 | { |
| 970 | m_aVertices[m_NumVertices + 4 * i].m_Pos.x = pArray[i].m_X0; |
| 971 | m_aVertices[m_NumVertices + 4 * i].m_Pos.y = pArray[i].m_Y0; |
| 972 | m_aVertices[m_NumVertices + 4 * i].m_Tex = m_aTexture[0]; |
| 973 | m_aVertices[m_NumVertices + 4 * i].m_Color = m_aColor[0]; |
| 974 | |
| 975 | m_aVertices[m_NumVertices + 4 * i + 1].m_Pos.x = pArray[i].m_X1; |
| 976 | m_aVertices[m_NumVertices + 4 * i + 1].m_Pos.y = pArray[i].m_Y1; |
| 977 | m_aVertices[m_NumVertices + 4 * i + 1].m_Tex = m_aTexture[1]; |
| 978 | m_aVertices[m_NumVertices + 4 * i + 1].m_Color = m_aColor[1]; |
| 979 | |
| 980 | m_aVertices[m_NumVertices + 4 * i + 2].m_Pos.x = pArray[i].m_X3; |
| 981 | m_aVertices[m_NumVertices + 4 * i + 2].m_Pos.y = pArray[i].m_Y3; |
| 982 | m_aVertices[m_NumVertices + 4 * i + 2].m_Tex = m_aTexture[3]; |
| 983 | m_aVertices[m_NumVertices + 4 * i + 2].m_Color = m_aColor[3]; |
| 984 | |
| 985 | m_aVertices[m_NumVertices + 4 * i + 3].m_Pos.x = pArray[i].m_X2; |
| 986 | m_aVertices[m_NumVertices + 4 * i + 3].m_Pos.y = pArray[i].m_Y2; |
| 987 | m_aVertices[m_NumVertices + 4 * i + 3].m_Tex = m_aTexture[2]; |
| 988 | m_aVertices[m_NumVertices + 4 * i + 3].m_Color = m_aColor[2]; |
| 989 | } |
| 990 | |
| 991 | AddVertices(Count: 4 * Num); |
| 992 | } |
| 993 | } |
| 994 | |
| 995 | void CGraphics_Threaded::QuadsText(float x, float y, float Size, const char *pText) |
| 996 | { |
| 997 | float StartX = x; |
| 998 | |
| 999 | while(*pText) |
| 1000 | { |
| 1001 | char c = *pText; |
| 1002 | pText++; |
| 1003 | |
| 1004 | if(c == '\n') |
| 1005 | { |
| 1006 | x = StartX; |
| 1007 | y += Size; |
| 1008 | } |
| 1009 | else |
| 1010 | { |
| 1011 | QuadsSetSubset( |
| 1012 | TlU: (c % 16) / 16.0f, |
| 1013 | TlV: (c / 16) / 16.0f, |
| 1014 | BrU: (c % 16) / 16.0f + 1.0f / 16.0f, |
| 1015 | BrV: (c / 16) / 16.0f + 1.0f / 16.0f); |
| 1016 | |
| 1017 | CQuadItem QuadItem(x, y, Size, Size); |
| 1018 | QuadsDrawTL(pArray: &QuadItem, Num: 1); |
| 1019 | x += Size / 2; |
| 1020 | } |
| 1021 | } |
| 1022 | } |
| 1023 | |
| 1024 | class CRoundedRect |
| 1025 | { |
| 1026 | public: |
| 1027 | static constexpr int NUM_SEGMENTS = 8; |
| 1028 | |
| 1029 | IGraphics::CFreeformItem m_aFreeform[NUM_SEGMENTS * 4]; |
| 1030 | int m_aFreeformCorners[NUM_SEGMENTS * 4]; // corner each freeform item belongs to |
| 1031 | int m_NumFreeform = 0; |
| 1032 | IGraphics::CQuadItem m_aQuads[9]; |
| 1033 | int m_NumQuads = 0; |
| 1034 | }; |
| 1035 | |
| 1036 | static CRoundedRect GetRoundedRect(float x, float y, float w, float h, float r, int Corners) |
| 1037 | { |
| 1038 | CRoundedRect Rect; |
| 1039 | const float SegmentsAngle = pi / 2 / CRoundedRect::NUM_SEGMENTS; |
| 1040 | |
| 1041 | for(int i = 0; i < CRoundedRect::NUM_SEGMENTS; i += 2) |
| 1042 | { |
| 1043 | float a1 = i * SegmentsAngle; |
| 1044 | float a2 = (i + 1) * SegmentsAngle; |
| 1045 | float a3 = (i + 2) * SegmentsAngle; |
| 1046 | float Ca1 = std::cos(x: a1); |
| 1047 | float Ca2 = std::cos(x: a2); |
| 1048 | float Ca3 = std::cos(x: a3); |
| 1049 | float Sa1 = std::sin(x: a1); |
| 1050 | float Sa2 = std::sin(x: a2); |
| 1051 | float Sa3 = std::sin(x: a3); |
| 1052 | |
| 1053 | if(Corners & IGraphics::CORNER_TL) |
| 1054 | { |
| 1055 | Rect.m_aFreeformCorners[Rect.m_NumFreeform] = IGraphics::CORNER_TL; |
| 1056 | Rect.m_aFreeform[Rect.m_NumFreeform++] = IGraphics::CFreeformItem( |
| 1057 | x + r, y + r, |
| 1058 | x + (1 - Ca1) * r, y + (1 - Sa1) * r, |
| 1059 | x + (1 - Ca3) * r, y + (1 - Sa3) * r, |
| 1060 | x + (1 - Ca2) * r, y + (1 - Sa2) * r); |
| 1061 | } |
| 1062 | |
| 1063 | if(Corners & IGraphics::CORNER_TR) |
| 1064 | { |
| 1065 | Rect.m_aFreeformCorners[Rect.m_NumFreeform] = IGraphics::CORNER_TR; |
| 1066 | Rect.m_aFreeform[Rect.m_NumFreeform++] = IGraphics::CFreeformItem( |
| 1067 | x + w - r, y + r, |
| 1068 | x + w - r + Ca1 * r, y + (1 - Sa1) * r, |
| 1069 | x + w - r + Ca3 * r, y + (1 - Sa3) * r, |
| 1070 | x + w - r + Ca2 * r, y + (1 - Sa2) * r); |
| 1071 | } |
| 1072 | |
| 1073 | if(Corners & IGraphics::CORNER_BL) |
| 1074 | { |
| 1075 | Rect.m_aFreeformCorners[Rect.m_NumFreeform] = IGraphics::CORNER_BL; |
| 1076 | Rect.m_aFreeform[Rect.m_NumFreeform++] = IGraphics::CFreeformItem( |
| 1077 | x + r, y + h - r, |
| 1078 | x + (1 - Ca1) * r, y + h - r + Sa1 * r, |
| 1079 | x + (1 - Ca3) * r, y + h - r + Sa3 * r, |
| 1080 | x + (1 - Ca2) * r, y + h - r + Sa2 * r); |
| 1081 | } |
| 1082 | |
| 1083 | if(Corners & IGraphics::CORNER_BR) |
| 1084 | { |
| 1085 | Rect.m_aFreeformCorners[Rect.m_NumFreeform] = IGraphics::CORNER_BR; |
| 1086 | Rect.m_aFreeform[Rect.m_NumFreeform++] = IGraphics::CFreeformItem( |
| 1087 | x + w - r, y + h - r, |
| 1088 | x + w - r + Ca1 * r, y + h - r + Sa1 * r, |
| 1089 | x + w - r + Ca3 * r, y + h - r + Sa3 * r, |
| 1090 | x + w - r + Ca2 * r, y + h - r + Sa2 * r); |
| 1091 | } |
| 1092 | } |
| 1093 | |
| 1094 | Rect.m_aQuads[Rect.m_NumQuads++] = IGraphics::CQuadItem(x + r, y + r, w - r * 2, h - r * 2); // center |
| 1095 | Rect.m_aQuads[Rect.m_NumQuads++] = IGraphics::CQuadItem(x + r, y, w - r * 2, r); // top |
| 1096 | Rect.m_aQuads[Rect.m_NumQuads++] = IGraphics::CQuadItem(x + r, y + h - r, w - r * 2, r); // bottom |
| 1097 | Rect.m_aQuads[Rect.m_NumQuads++] = IGraphics::CQuadItem(x, y + r, r, h - r * 2); // left |
| 1098 | Rect.m_aQuads[Rect.m_NumQuads++] = IGraphics::CQuadItem(x + w - r, y + r, r, h - r * 2); // right |
| 1099 | |
| 1100 | if(!(Corners & IGraphics::CORNER_TL)) |
| 1101 | Rect.m_aQuads[Rect.m_NumQuads++] = IGraphics::CQuadItem(x, y, r, r); |
| 1102 | if(!(Corners & IGraphics::CORNER_TR)) |
| 1103 | Rect.m_aQuads[Rect.m_NumQuads++] = IGraphics::CQuadItem(x + w, y, -r, r); |
| 1104 | if(!(Corners & IGraphics::CORNER_BL)) |
| 1105 | Rect.m_aQuads[Rect.m_NumQuads++] = IGraphics::CQuadItem(x, y + h, r, -r); |
| 1106 | if(!(Corners & IGraphics::CORNER_BR)) |
| 1107 | Rect.m_aQuads[Rect.m_NumQuads++] = IGraphics::CQuadItem(x + w, y + h, -r, -r); |
| 1108 | |
| 1109 | return Rect; |
| 1110 | } |
| 1111 | |
| 1112 | void CGraphics_Threaded::DrawRectExt(float x, float y, float w, float h, float r, int Corners) |
| 1113 | { |
| 1114 | const CRoundedRect Rect = GetRoundedRect(x, y, w, h, r, Corners); |
| 1115 | QuadsDrawFreeform(pArray: Rect.m_aFreeform, Num: Rect.m_NumFreeform); |
| 1116 | QuadsDrawTL(pArray: Rect.m_aQuads, Num: Rect.m_NumQuads); |
| 1117 | } |
| 1118 | |
| 1119 | void CGraphics_Threaded::DrawRectExt4(float x, float y, float w, float h, ColorRGBA ColorTopLeft, ColorRGBA ColorTopRight, ColorRGBA ColorBottomLeft, ColorRGBA ColorBottomRight, float r, int Corners) |
| 1120 | { |
| 1121 | if(Corners == 0 || r == 0.0f) |
| 1122 | { |
| 1123 | SetColor4(TopLeft: ColorTopLeft, TopRight: ColorTopRight, BottomLeft: ColorBottomLeft, BottomRight: ColorBottomRight); |
| 1124 | CQuadItem ItemQ = CQuadItem(x, y, w, h); |
| 1125 | QuadsDrawTL(pArray: &ItemQ, Num: 1); |
| 1126 | return; |
| 1127 | } |
| 1128 | |
| 1129 | const CRoundedRect Rect = GetRoundedRect(x, y, w, h, r, Corners); |
| 1130 | |
| 1131 | for(int i = 0; i < Rect.m_NumFreeform; i++) |
| 1132 | { |
| 1133 | switch(Rect.m_aFreeformCorners[i]) |
| 1134 | { |
| 1135 | case IGraphics::CORNER_TL: SetColor(ColorTopLeft); break; |
| 1136 | case IGraphics::CORNER_TR: SetColor(ColorTopRight); break; |
| 1137 | case IGraphics::CORNER_BL: SetColor(ColorBottomLeft); break; |
| 1138 | case IGraphics::CORNER_BR: SetColor(ColorBottomRight); break; |
| 1139 | default: dbg_assert_failed("unknown corner type %d" , Corners); |
| 1140 | } |
| 1141 | QuadsDrawFreeform(pArray: &Rect.m_aFreeform[i], Num: 1); |
| 1142 | } |
| 1143 | |
| 1144 | SetColor4(TopLeft: ColorTopLeft, TopRight: ColorTopRight, BottomLeft: ColorBottomLeft, BottomRight: ColorBottomRight); |
| 1145 | QuadsDrawTL(pArray: &Rect.m_aQuads[0], Num: 1); // center |
| 1146 | |
| 1147 | SetColor4(TopLeft: ColorTopLeft, TopRight: ColorTopRight, BottomLeft: ColorTopLeft, BottomRight: ColorTopRight); |
| 1148 | QuadsDrawTL(pArray: &Rect.m_aQuads[1], Num: 1); // top |
| 1149 | |
| 1150 | SetColor4(TopLeft: ColorBottomLeft, TopRight: ColorBottomRight, BottomLeft: ColorBottomLeft, BottomRight: ColorBottomRight); |
| 1151 | QuadsDrawTL(pArray: &Rect.m_aQuads[2], Num: 1); // bottom |
| 1152 | |
| 1153 | SetColor4(TopLeft: ColorTopLeft, TopRight: ColorTopLeft, BottomLeft: ColorBottomLeft, BottomRight: ColorBottomLeft); |
| 1154 | QuadsDrawTL(pArray: &Rect.m_aQuads[3], Num: 1); // left |
| 1155 | |
| 1156 | SetColor4(TopLeft: ColorTopRight, TopRight: ColorTopRight, BottomLeft: ColorBottomRight, BottomRight: ColorBottomRight); |
| 1157 | QuadsDrawTL(pArray: &Rect.m_aQuads[4], Num: 1); // right |
| 1158 | |
| 1159 | int QuadIndex = 5; |
| 1160 | if(!(Corners & IGraphics::CORNER_TL)) |
| 1161 | { |
| 1162 | SetColor(ColorTopLeft); |
| 1163 | QuadsDrawTL(pArray: &Rect.m_aQuads[QuadIndex++], Num: 1); |
| 1164 | } |
| 1165 | |
| 1166 | if(!(Corners & IGraphics::CORNER_TR)) |
| 1167 | { |
| 1168 | SetColor(ColorTopRight); |
| 1169 | QuadsDrawTL(pArray: &Rect.m_aQuads[QuadIndex++], Num: 1); |
| 1170 | } |
| 1171 | |
| 1172 | if(!(Corners & IGraphics::CORNER_BL)) |
| 1173 | { |
| 1174 | SetColor(ColorBottomLeft); |
| 1175 | QuadsDrawTL(pArray: &Rect.m_aQuads[QuadIndex++], Num: 1); |
| 1176 | } |
| 1177 | |
| 1178 | if(!(Corners & IGraphics::CORNER_BR)) |
| 1179 | { |
| 1180 | SetColor(ColorBottomRight); |
| 1181 | QuadsDrawTL(pArray: &Rect.m_aQuads[QuadIndex++], Num: 1); |
| 1182 | } |
| 1183 | } |
| 1184 | |
| 1185 | int CGraphics_Threaded::CreateRectQuadContainer(float x, float y, float w, float h, float r, int Corners) |
| 1186 | { |
| 1187 | int ContainerIndex = CreateQuadContainer(AutomaticUpload: false); |
| 1188 | |
| 1189 | if(Corners == 0 || r == 0.0f) |
| 1190 | { |
| 1191 | CQuadItem ItemQ = CQuadItem(x, y, w, h); |
| 1192 | QuadContainerAddQuads(ContainerIndex, pArray: &ItemQ, Num: 1); |
| 1193 | QuadContainerUpload(ContainerIndex); |
| 1194 | QuadContainerChangeAutomaticUpload(ContainerIndex, AutomaticUpload: true); |
| 1195 | return ContainerIndex; |
| 1196 | } |
| 1197 | |
| 1198 | const CRoundedRect Rect = GetRoundedRect(x, y, w, h, r, Corners); |
| 1199 | |
| 1200 | if(Rect.m_NumFreeform > 0) |
| 1201 | QuadContainerAddQuads(ContainerIndex, pArray: Rect.m_aFreeform, Num: Rect.m_NumFreeform); |
| 1202 | if(Rect.m_NumQuads > 0) |
| 1203 | QuadContainerAddQuads(ContainerIndex, pArray: Rect.m_aQuads, Num: Rect.m_NumQuads); |
| 1204 | |
| 1205 | QuadContainerUpload(ContainerIndex); |
| 1206 | QuadContainerChangeAutomaticUpload(ContainerIndex, AutomaticUpload: true); |
| 1207 | |
| 1208 | return ContainerIndex; |
| 1209 | } |
| 1210 | |
| 1211 | void CGraphics_Threaded::DrawRect(float x, float y, float w, float h, ColorRGBA Color, int Corners, float Rounding) |
| 1212 | { |
| 1213 | TextureClear(); |
| 1214 | QuadsBegin(); |
| 1215 | SetColor(Color); |
| 1216 | DrawRectExt(x, y, w, h, r: Rounding, Corners); |
| 1217 | QuadsEnd(); |
| 1218 | } |
| 1219 | |
| 1220 | void CGraphics_Threaded::DrawRect4(float x, float y, float w, float h, ColorRGBA ColorTopLeft, ColorRGBA ColorTopRight, ColorRGBA ColorBottomLeft, ColorRGBA ColorBottomRight, int Corners, float Rounding) |
| 1221 | { |
| 1222 | TextureClear(); |
| 1223 | QuadsBegin(); |
| 1224 | DrawRectExt4(x, y, w, h, ColorTopLeft, ColorTopRight, ColorBottomLeft, ColorBottomRight, r: Rounding, Corners); |
| 1225 | QuadsEnd(); |
| 1226 | } |
| 1227 | |
| 1228 | void CGraphics_Threaded::DrawCircle(float CenterX, float CenterY, float Radius, int Segments) |
| 1229 | { |
| 1230 | IGraphics::CFreeformItem aItems[32]; |
| 1231 | size_t NumItems = 0; |
| 1232 | const float SegmentsAngle = 2 * pi / Segments; |
| 1233 | for(int i = 0; i < Segments; i += 2) |
| 1234 | { |
| 1235 | const float a1 = i * SegmentsAngle; |
| 1236 | const float a2 = (i + 1) * SegmentsAngle; |
| 1237 | const float a3 = (i + 2) * SegmentsAngle; |
| 1238 | aItems[NumItems++] = IGraphics::CFreeformItem( |
| 1239 | CenterX, CenterY, |
| 1240 | CenterX + std::cos(x: a1) * Radius, CenterY + std::sin(x: a1) * Radius, |
| 1241 | CenterX + std::cos(x: a3) * Radius, CenterY + std::sin(x: a3) * Radius, |
| 1242 | CenterX + std::cos(x: a2) * Radius, CenterY + std::sin(x: a2) * Radius); |
| 1243 | if(NumItems == std::size(aItems)) |
| 1244 | { |
| 1245 | QuadsDrawFreeform(pArray: aItems, Num: std::size(aItems)); |
| 1246 | NumItems = 0; |
| 1247 | } |
| 1248 | } |
| 1249 | if(NumItems) |
| 1250 | QuadsDrawFreeform(pArray: aItems, Num: NumItems); |
| 1251 | } |
| 1252 | |
| 1253 | void CGraphics_Threaded::RenderTileLayer(int BufferContainerIndex, const ColorRGBA &Color, char **pOffsets, unsigned int *pIndicedVertexDrawNum, size_t NumIndicesOffset) |
| 1254 | { |
| 1255 | if(NumIndicesOffset == 0) |
| 1256 | return; |
| 1257 | |
| 1258 | // add the VertexArrays and draw |
| 1259 | CCommandBuffer::SCommand_RenderTileLayer Cmd; |
| 1260 | Cmd.m_State = m_State; |
| 1261 | Cmd.m_IndicesDrawNum = NumIndicesOffset; |
| 1262 | Cmd.m_BufferContainerIndex = BufferContainerIndex; |
| 1263 | Cmd.m_Color = Color; |
| 1264 | |
| 1265 | void *pData = m_pCommandBuffer->AllocData(WantedSize: (sizeof(char *) + sizeof(unsigned int)) * NumIndicesOffset); |
| 1266 | if(pData == nullptr) |
| 1267 | { |
| 1268 | // kick command buffer and try again |
| 1269 | KickCommandBuffer(); |
| 1270 | |
| 1271 | pData = m_pCommandBuffer->AllocData(WantedSize: (sizeof(char *) + sizeof(unsigned int)) * NumIndicesOffset); |
| 1272 | if(pData == nullptr) |
| 1273 | { |
| 1274 | log_error("graphics" , "Failed to allocate data for tile layer vertices. NumIndicesOffset=%" PRIzu, NumIndicesOffset); |
| 1275 | return; |
| 1276 | } |
| 1277 | } |
| 1278 | Cmd.m_pIndicesOffsets = (char **)pData; |
| 1279 | Cmd.m_pDrawCount = (unsigned int *)(((char *)pData) + (sizeof(char *) * NumIndicesOffset)); |
| 1280 | |
| 1281 | AddCmd(Cmd, FailFunc: [&] { |
| 1282 | pData = m_pCommandBuffer->AllocData(WantedSize: (sizeof(char *) + sizeof(unsigned int)) * NumIndicesOffset); |
| 1283 | if(pData == nullptr) |
| 1284 | return false; |
| 1285 | Cmd.m_pIndicesOffsets = (char **)pData; |
| 1286 | Cmd.m_pDrawCount = (unsigned int *)(((char *)pData) + (sizeof(char *) * NumIndicesOffset)); |
| 1287 | return true; |
| 1288 | }); |
| 1289 | |
| 1290 | mem_copy(dest: Cmd.m_pIndicesOffsets, source: pOffsets, size: sizeof(char *) * NumIndicesOffset); |
| 1291 | mem_copy(dest: Cmd.m_pDrawCount, source: pIndicedVertexDrawNum, size: sizeof(unsigned int) * NumIndicesOffset); |
| 1292 | |
| 1293 | m_pCommandBuffer->AddRenderCalls(RenderCallCountToAdd: NumIndicesOffset); |
| 1294 | // todo max indices group check!! |
| 1295 | } |
| 1296 | |
| 1297 | void CGraphics_Threaded::RenderBorderTiles(int BufferContainerIndex, const ColorRGBA &Color, char *pIndexBufferOffset, const vec2 &Offset, const vec2 &Scale, uint32_t DrawNum) |
| 1298 | { |
| 1299 | if(DrawNum == 0) |
| 1300 | return; |
| 1301 | // Draw a border tile a lot of times |
| 1302 | CCommandBuffer::SCommand_RenderBorderTile Cmd; |
| 1303 | Cmd.m_State = m_State; |
| 1304 | Cmd.m_DrawNum = DrawNum; |
| 1305 | Cmd.m_BufferContainerIndex = BufferContainerIndex; |
| 1306 | Cmd.m_Color = Color; |
| 1307 | |
| 1308 | Cmd.m_pIndicesOffset = pIndexBufferOffset; |
| 1309 | |
| 1310 | Cmd.m_Offset = Offset; |
| 1311 | Cmd.m_Scale = Scale; |
| 1312 | |
| 1313 | AddCmd(Cmd); |
| 1314 | |
| 1315 | m_pCommandBuffer->AddRenderCalls(RenderCallCountToAdd: 1); |
| 1316 | } |
| 1317 | |
| 1318 | void CGraphics_Threaded::RenderQuadLayer(int BufferContainerIndex, SQuadRenderInfo *pQuadInfo, size_t QuadNum, int QuadOffset, bool Grouped) |
| 1319 | { |
| 1320 | if(QuadNum == 0) |
| 1321 | return; |
| 1322 | |
| 1323 | // add the VertexArrays and draw |
| 1324 | CCommandBuffer::SCommand_RenderQuadLayer Cmd(Grouped); |
| 1325 | Cmd.m_State = m_State; |
| 1326 | Cmd.m_QuadNum = QuadNum; |
| 1327 | Cmd.m_QuadOffset = QuadOffset; |
| 1328 | Cmd.m_BufferContainerIndex = BufferContainerIndex; |
| 1329 | if(!Grouped) |
| 1330 | { |
| 1331 | Cmd.m_pQuadInfo = (SQuadRenderInfo *)AllocCommandBufferData(AllocSize: Cmd.m_QuadNum * sizeof(SQuadRenderInfo)); |
| 1332 | AddCmd(Cmd, FailFunc: [&] { |
| 1333 | Cmd.m_pQuadInfo = (SQuadRenderInfo *)m_pCommandBuffer->AllocData(WantedSize: QuadNum * sizeof(SQuadRenderInfo)); |
| 1334 | return Cmd.m_pQuadInfo != nullptr; |
| 1335 | }); |
| 1336 | |
| 1337 | mem_copy(dest: Cmd.m_pQuadInfo, source: pQuadInfo, size: sizeof(SQuadRenderInfo) * QuadNum); |
| 1338 | m_pCommandBuffer->AddRenderCalls(RenderCallCountToAdd: ((QuadNum - 1) / GRAPHICS_MAX_QUADS_RENDER_COUNT) + 1); |
| 1339 | } |
| 1340 | else |
| 1341 | { |
| 1342 | Cmd.m_pQuadInfo = (SQuadRenderInfo *)AllocCommandBufferData(AllocSize: sizeof(SQuadRenderInfo)); |
| 1343 | AddCmd(Cmd, FailFunc: [&] { |
| 1344 | Cmd.m_pQuadInfo = (SQuadRenderInfo *)m_pCommandBuffer->AllocData(WantedSize: sizeof(SQuadRenderInfo)); |
| 1345 | return Cmd.m_pQuadInfo != nullptr; |
| 1346 | }); |
| 1347 | |
| 1348 | *Cmd.m_pQuadInfo = *pQuadInfo; |
| 1349 | m_pCommandBuffer->AddRenderCalls(RenderCallCountToAdd: 1); |
| 1350 | } |
| 1351 | } |
| 1352 | |
| 1353 | void CGraphics_Threaded::RenderText(int BufferContainerIndex, int TextQuadNum, int TextureSize, int TextureTextIndex, int TextureTextOutlineIndex, const ColorRGBA &TextColor, const ColorRGBA &TextOutlineColor) |
| 1354 | { |
| 1355 | if(BufferContainerIndex == -1) |
| 1356 | return; |
| 1357 | |
| 1358 | CCommandBuffer::SCommand_RenderText Cmd; |
| 1359 | Cmd.m_State = m_State; |
| 1360 | Cmd.m_BufferContainerIndex = BufferContainerIndex; |
| 1361 | Cmd.m_DrawNum = TextQuadNum * 6; |
| 1362 | Cmd.m_TextureSize = TextureSize; |
| 1363 | Cmd.m_TextTextureIndex = TextureTextIndex; |
| 1364 | Cmd.m_TextOutlineTextureIndex = TextureTextOutlineIndex; |
| 1365 | Cmd.m_TextColor = TextColor; |
| 1366 | Cmd.m_TextOutlineColor = TextOutlineColor; |
| 1367 | |
| 1368 | AddCmd(Cmd); |
| 1369 | |
| 1370 | m_pCommandBuffer->AddRenderCalls(RenderCallCountToAdd: 1); |
| 1371 | } |
| 1372 | |
| 1373 | int CGraphics_Threaded::CreateQuadContainer(bool AutomaticUpload) |
| 1374 | { |
| 1375 | int Index = -1; |
| 1376 | if(m_FirstFreeQuadContainer == -1) |
| 1377 | { |
| 1378 | Index = m_vQuadContainers.size(); |
| 1379 | m_vQuadContainers.emplace_back(args&: AutomaticUpload); |
| 1380 | } |
| 1381 | else |
| 1382 | { |
| 1383 | Index = m_FirstFreeQuadContainer; |
| 1384 | m_FirstFreeQuadContainer = m_vQuadContainers[Index].m_FreeIndex; |
| 1385 | m_vQuadContainers[Index].m_FreeIndex = Index; |
| 1386 | } |
| 1387 | |
| 1388 | return Index; |
| 1389 | } |
| 1390 | |
| 1391 | void CGraphics_Threaded::QuadContainerChangeAutomaticUpload(int ContainerIndex, bool AutomaticUpload) |
| 1392 | { |
| 1393 | SQuadContainer &Container = m_vQuadContainers[ContainerIndex]; |
| 1394 | Container.m_AutomaticUpload = AutomaticUpload; |
| 1395 | } |
| 1396 | |
| 1397 | void CGraphics_Threaded::QuadContainerUpload(int ContainerIndex) |
| 1398 | { |
| 1399 | if(IsQuadContainerBufferingEnabled()) |
| 1400 | { |
| 1401 | SQuadContainer &Container = m_vQuadContainers[ContainerIndex]; |
| 1402 | if(!Container.m_vQuads.empty()) |
| 1403 | { |
| 1404 | if(Container.m_QuadBufferObjectIndex == -1) |
| 1405 | { |
| 1406 | size_t UploadDataSize = Container.m_vQuads.size() * sizeof(SQuadContainer::SQuad); |
| 1407 | Container.m_QuadBufferObjectIndex = CreateBufferObject(UploadDataSize, pUploadData: Container.m_vQuads.data(), CreateFlags: 0); |
| 1408 | } |
| 1409 | else |
| 1410 | { |
| 1411 | size_t UploadDataSize = Container.m_vQuads.size() * sizeof(SQuadContainer::SQuad); |
| 1412 | RecreateBufferObject(BufferIndex: Container.m_QuadBufferObjectIndex, UploadDataSize, pUploadData: Container.m_vQuads.data(), CreateFlags: 0); |
| 1413 | } |
| 1414 | |
| 1415 | if(Container.m_QuadBufferContainerIndex == -1) |
| 1416 | { |
| 1417 | SBufferContainerInfo Info; |
| 1418 | Info.m_Stride = sizeof(CCommandBuffer::SVertex); |
| 1419 | Info.m_VertBufferBindingIndex = Container.m_QuadBufferObjectIndex; |
| 1420 | |
| 1421 | Info.m_vAttributes.emplace_back(); |
| 1422 | SBufferContainerInfo::SAttribute *pAttr = &Info.m_vAttributes.back(); |
| 1423 | pAttr->m_DataTypeCount = 2; |
| 1424 | pAttr->m_FuncType = 0; |
| 1425 | pAttr->m_Normalized = false; |
| 1426 | pAttr->m_pOffset = nullptr; |
| 1427 | pAttr->m_Type = GRAPHICS_TYPE_FLOAT; |
| 1428 | Info.m_vAttributes.emplace_back(); |
| 1429 | pAttr = &Info.m_vAttributes.back(); |
| 1430 | pAttr->m_DataTypeCount = 2; |
| 1431 | pAttr->m_FuncType = 0; |
| 1432 | pAttr->m_Normalized = false; |
| 1433 | pAttr->m_pOffset = (void *)(sizeof(float) * 2); |
| 1434 | pAttr->m_Type = GRAPHICS_TYPE_FLOAT; |
| 1435 | Info.m_vAttributes.emplace_back(); |
| 1436 | pAttr = &Info.m_vAttributes.back(); |
| 1437 | pAttr->m_DataTypeCount = 4; |
| 1438 | pAttr->m_FuncType = 0; |
| 1439 | pAttr->m_Normalized = true; |
| 1440 | pAttr->m_pOffset = (void *)(sizeof(float) * 2 + sizeof(float) * 2); |
| 1441 | pAttr->m_Type = GRAPHICS_TYPE_UNSIGNED_BYTE; |
| 1442 | |
| 1443 | Container.m_QuadBufferContainerIndex = CreateBufferContainer(pContainerInfo: &Info); |
| 1444 | } |
| 1445 | } |
| 1446 | } |
| 1447 | } |
| 1448 | |
| 1449 | int CGraphics_Threaded::QuadContainerAddQuads(int ContainerIndex, const CQuadItem *pArray, int Num) |
| 1450 | { |
| 1451 | SQuadContainer &Container = m_vQuadContainers[ContainerIndex]; |
| 1452 | |
| 1453 | if((int)Container.m_vQuads.size() > Num + CCommandBuffer::CCommandBuffer::MAX_VERTICES) |
| 1454 | return -1; |
| 1455 | |
| 1456 | int RetOff = (int)Container.m_vQuads.size(); |
| 1457 | |
| 1458 | for(int i = 0; i < Num; ++i) |
| 1459 | { |
| 1460 | Container.m_vQuads.emplace_back(); |
| 1461 | SQuadContainer::SQuad &Quad = Container.m_vQuads.back(); |
| 1462 | |
| 1463 | Quad.m_aVertices[0].m_Pos.x = pArray[i].m_X; |
| 1464 | Quad.m_aVertices[0].m_Pos.y = pArray[i].m_Y; |
| 1465 | Quad.m_aVertices[0].m_Tex = m_aTexture[0]; |
| 1466 | Quad.m_aVertices[0].m_Color = m_aColor[0]; |
| 1467 | |
| 1468 | Quad.m_aVertices[1].m_Pos.x = pArray[i].m_X + pArray[i].m_Width; |
| 1469 | Quad.m_aVertices[1].m_Pos.y = pArray[i].m_Y; |
| 1470 | Quad.m_aVertices[1].m_Tex = m_aTexture[1]; |
| 1471 | Quad.m_aVertices[1].m_Color = m_aColor[1]; |
| 1472 | |
| 1473 | Quad.m_aVertices[2].m_Pos.x = pArray[i].m_X + pArray[i].m_Width; |
| 1474 | Quad.m_aVertices[2].m_Pos.y = pArray[i].m_Y + pArray[i].m_Height; |
| 1475 | Quad.m_aVertices[2].m_Tex = m_aTexture[2]; |
| 1476 | Quad.m_aVertices[2].m_Color = m_aColor[2]; |
| 1477 | |
| 1478 | Quad.m_aVertices[3].m_Pos.x = pArray[i].m_X; |
| 1479 | Quad.m_aVertices[3].m_Pos.y = pArray[i].m_Y + pArray[i].m_Height; |
| 1480 | Quad.m_aVertices[3].m_Tex = m_aTexture[3]; |
| 1481 | Quad.m_aVertices[3].m_Color = m_aColor[3]; |
| 1482 | |
| 1483 | if(m_Rotation != 0) |
| 1484 | { |
| 1485 | CCommandBuffer::SPoint Center; |
| 1486 | Center.x = pArray[i].m_X + pArray[i].m_Width / 2; |
| 1487 | Center.y = pArray[i].m_Y + pArray[i].m_Height / 2; |
| 1488 | |
| 1489 | Rotate(Center, pPoints: Quad.m_aVertices, NumPoints: 4); |
| 1490 | } |
| 1491 | } |
| 1492 | |
| 1493 | if(Container.m_AutomaticUpload) |
| 1494 | QuadContainerUpload(ContainerIndex); |
| 1495 | |
| 1496 | return RetOff; |
| 1497 | } |
| 1498 | |
| 1499 | int CGraphics_Threaded::QuadContainerAddQuads(int ContainerIndex, const CFreeformItem *pArray, int Num) |
| 1500 | { |
| 1501 | SQuadContainer &Container = m_vQuadContainers[ContainerIndex]; |
| 1502 | |
| 1503 | if((int)Container.m_vQuads.size() > Num + CCommandBuffer::CCommandBuffer::MAX_VERTICES) |
| 1504 | return -1; |
| 1505 | |
| 1506 | int RetOff = (int)Container.m_vQuads.size(); |
| 1507 | |
| 1508 | for(int i = 0; i < Num; ++i) |
| 1509 | { |
| 1510 | Container.m_vQuads.emplace_back(); |
| 1511 | SQuadContainer::SQuad &Quad = Container.m_vQuads.back(); |
| 1512 | |
| 1513 | Quad.m_aVertices[0].m_Pos.x = pArray[i].m_X0; |
| 1514 | Quad.m_aVertices[0].m_Pos.y = pArray[i].m_Y0; |
| 1515 | Quad.m_aVertices[0].m_Tex = m_aTexture[0]; |
| 1516 | Quad.m_aVertices[0].m_Color = m_aColor[0]; |
| 1517 | |
| 1518 | Quad.m_aVertices[1].m_Pos.x = pArray[i].m_X1; |
| 1519 | Quad.m_aVertices[1].m_Pos.y = pArray[i].m_Y1; |
| 1520 | Quad.m_aVertices[1].m_Tex = m_aTexture[1]; |
| 1521 | Quad.m_aVertices[1].m_Color = m_aColor[1]; |
| 1522 | |
| 1523 | Quad.m_aVertices[2].m_Pos.x = pArray[i].m_X3; |
| 1524 | Quad.m_aVertices[2].m_Pos.y = pArray[i].m_Y3; |
| 1525 | Quad.m_aVertices[2].m_Tex = m_aTexture[3]; |
| 1526 | Quad.m_aVertices[2].m_Color = m_aColor[3]; |
| 1527 | |
| 1528 | Quad.m_aVertices[3].m_Pos.x = pArray[i].m_X2; |
| 1529 | Quad.m_aVertices[3].m_Pos.y = pArray[i].m_Y2; |
| 1530 | Quad.m_aVertices[3].m_Tex = m_aTexture[2]; |
| 1531 | Quad.m_aVertices[3].m_Color = m_aColor[2]; |
| 1532 | } |
| 1533 | |
| 1534 | if(Container.m_AutomaticUpload) |
| 1535 | QuadContainerUpload(ContainerIndex); |
| 1536 | |
| 1537 | return RetOff; |
| 1538 | } |
| 1539 | |
| 1540 | void CGraphics_Threaded::QuadContainerReset(int ContainerIndex) |
| 1541 | { |
| 1542 | if(ContainerIndex == -1) |
| 1543 | return; |
| 1544 | |
| 1545 | SQuadContainer &Container = m_vQuadContainers[ContainerIndex]; |
| 1546 | if(IsQuadContainerBufferingEnabled()) |
| 1547 | DeleteBufferContainer(ContainerIndex&: Container.m_QuadBufferContainerIndex, DestroyAllBO: true); |
| 1548 | Container.m_vQuads.clear(); |
| 1549 | Container.m_QuadBufferObjectIndex = -1; |
| 1550 | } |
| 1551 | |
| 1552 | void CGraphics_Threaded::DeleteQuadContainer(int &ContainerIndex) |
| 1553 | { |
| 1554 | if(ContainerIndex == -1) |
| 1555 | return; |
| 1556 | |
| 1557 | QuadContainerReset(ContainerIndex); |
| 1558 | |
| 1559 | // also clear the container index |
| 1560 | m_vQuadContainers[ContainerIndex].m_FreeIndex = m_FirstFreeQuadContainer; |
| 1561 | m_FirstFreeQuadContainer = ContainerIndex; |
| 1562 | ContainerIndex = -1; |
| 1563 | } |
| 1564 | |
| 1565 | void CGraphics_Threaded::RenderQuadContainer(int ContainerIndex, int QuadDrawNum) |
| 1566 | { |
| 1567 | RenderQuadContainer(ContainerIndex, QuadOffset: 0, QuadDrawNum); |
| 1568 | } |
| 1569 | |
| 1570 | void CGraphics_Threaded::RenderQuadContainer(int ContainerIndex, int QuadOffset, int QuadDrawNum, bool ChangeWrapMode) |
| 1571 | { |
| 1572 | SQuadContainer &Container = m_vQuadContainers[ContainerIndex]; |
| 1573 | |
| 1574 | if(QuadDrawNum == -1) |
| 1575 | { |
| 1576 | QuadDrawNum = (int)Container.m_vQuads.size() - QuadOffset; |
| 1577 | } |
| 1578 | if(QuadDrawNum == 0) |
| 1579 | { |
| 1580 | return; |
| 1581 | } |
| 1582 | dbg_assert(QuadOffset >= 0 && QuadDrawNum > 0 && QuadOffset + QuadDrawNum <= (int)Container.m_vQuads.size(), |
| 1583 | "Invalid quad container rendered: QuadOffset=%d QuadDrawNum=%d NumQuads=%d" , QuadOffset, QuadDrawNum, (int)Container.m_vQuads.size()); |
| 1584 | |
| 1585 | if(IsQuadContainerBufferingEnabled()) |
| 1586 | { |
| 1587 | if(Container.m_QuadBufferContainerIndex == -1) |
| 1588 | return; |
| 1589 | |
| 1590 | if(ChangeWrapMode) |
| 1591 | WrapClamp(); |
| 1592 | |
| 1593 | CCommandBuffer::SCommand_RenderQuadContainer Cmd; |
| 1594 | Cmd.m_State = m_State; |
| 1595 | Cmd.m_DrawNum = (unsigned int)QuadDrawNum * 6; |
| 1596 | Cmd.m_pOffset = (void *)(QuadOffset * 6 * sizeof(unsigned int)); |
| 1597 | Cmd.m_BufferContainerIndex = Container.m_QuadBufferContainerIndex; |
| 1598 | |
| 1599 | AddCmd(Cmd); |
| 1600 | m_pCommandBuffer->AddRenderCalls(RenderCallCountToAdd: 1); |
| 1601 | } |
| 1602 | else |
| 1603 | { |
| 1604 | if(g_Config.m_GfxQuadAsTriangle) |
| 1605 | { |
| 1606 | for(int i = 0; i < QuadDrawNum; ++i) |
| 1607 | { |
| 1608 | SQuadContainer::SQuad &Quad = Container.m_vQuads[QuadOffset + i]; |
| 1609 | m_aVertices[i * 6] = Quad.m_aVertices[0]; |
| 1610 | m_aVertices[i * 6 + 1] = Quad.m_aVertices[1]; |
| 1611 | m_aVertices[i * 6 + 2] = Quad.m_aVertices[2]; |
| 1612 | m_aVertices[i * 6 + 3] = Quad.m_aVertices[0]; |
| 1613 | m_aVertices[i * 6 + 4] = Quad.m_aVertices[2]; |
| 1614 | m_aVertices[i * 6 + 5] = Quad.m_aVertices[3]; |
| 1615 | m_NumVertices += 6; |
| 1616 | } |
| 1617 | } |
| 1618 | else |
| 1619 | { |
| 1620 | mem_copy(dest: m_aVertices, source: &Container.m_vQuads[QuadOffset], size: sizeof(CCommandBuffer::SVertex) * 4 * QuadDrawNum); |
| 1621 | m_NumVertices += 4 * QuadDrawNum; |
| 1622 | } |
| 1623 | m_Drawing = EDrawing::QUADS; |
| 1624 | if(ChangeWrapMode) |
| 1625 | WrapClamp(); |
| 1626 | FlushVertices(KeepVertices: false); |
| 1627 | m_Drawing = EDrawing::NONE; |
| 1628 | } |
| 1629 | WrapNormal(); |
| 1630 | } |
| 1631 | |
| 1632 | void CGraphics_Threaded::RenderQuadContainerEx(int ContainerIndex, int QuadOffset, int QuadDrawNum, float X, float Y, float ScaleX, float ScaleY) |
| 1633 | { |
| 1634 | SQuadContainer &Container = m_vQuadContainers[ContainerIndex]; |
| 1635 | |
| 1636 | if(QuadDrawNum == -1) |
| 1637 | { |
| 1638 | QuadDrawNum = (int)Container.m_vQuads.size() - QuadOffset; |
| 1639 | } |
| 1640 | if(QuadDrawNum == 0) |
| 1641 | { |
| 1642 | return; |
| 1643 | } |
| 1644 | dbg_assert(QuadOffset >= 0 && QuadDrawNum > 0 && QuadOffset + QuadDrawNum <= (int)Container.m_vQuads.size(), |
| 1645 | "Invalid quad container rendered: QuadOffset=%d QuadDrawNum=%d NumQuads=%d" , QuadOffset, QuadDrawNum, (int)Container.m_vQuads.size()); |
| 1646 | |
| 1647 | if(IsQuadContainerBufferingEnabled()) |
| 1648 | { |
| 1649 | if(Container.m_QuadBufferContainerIndex == -1) |
| 1650 | return; |
| 1651 | |
| 1652 | SQuadContainer::SQuad &Quad = Container.m_vQuads[QuadOffset]; |
| 1653 | CCommandBuffer::SCommand_RenderQuadContainerEx Cmd; |
| 1654 | |
| 1655 | WrapClamp(); |
| 1656 | |
| 1657 | CScreenRect ScreenRect = GetScreen(); |
| 1658 | CScreenRect CommandScreenRect = ScreenRect.Move(Position: {-X, -Y}); |
| 1659 | CommandScreenRect.m_TopLeft /= vec2(ScaleX, ScaleY); |
| 1660 | CommandScreenRect.m_BottomRight /= vec2(ScaleX, ScaleY); |
| 1661 | Cmd.m_State = m_State; |
| 1662 | Cmd.m_State.m_ScreenTL = CommandScreenRect.m_TopLeft; |
| 1663 | Cmd.m_State.m_ScreenBR = CommandScreenRect.m_BottomRight; |
| 1664 | |
| 1665 | Cmd.m_DrawNum = QuadDrawNum * 6; |
| 1666 | Cmd.m_pOffset = (void *)(QuadOffset * 6 * sizeof(unsigned int)); |
| 1667 | Cmd.m_BufferContainerIndex = Container.m_QuadBufferContainerIndex; |
| 1668 | |
| 1669 | Cmd.m_VertexColor.r = (float)m_aColor[0].r / 255.f; |
| 1670 | Cmd.m_VertexColor.g = (float)m_aColor[0].g / 255.f; |
| 1671 | Cmd.m_VertexColor.b = (float)m_aColor[0].b / 255.f; |
| 1672 | Cmd.m_VertexColor.a = (float)m_aColor[0].a / 255.f; |
| 1673 | |
| 1674 | Cmd.m_Rotation = m_Rotation; |
| 1675 | |
| 1676 | // rotate before positioning |
| 1677 | Cmd.m_Center.x = Quad.m_aVertices[0].m_Pos.x + (Quad.m_aVertices[1].m_Pos.x - Quad.m_aVertices[0].m_Pos.x) / 2.f; |
| 1678 | Cmd.m_Center.y = Quad.m_aVertices[0].m_Pos.y + (Quad.m_aVertices[2].m_Pos.y - Quad.m_aVertices[0].m_Pos.y) / 2.f; |
| 1679 | |
| 1680 | AddCmd(Cmd); |
| 1681 | m_pCommandBuffer->AddRenderCalls(RenderCallCountToAdd: 1); |
| 1682 | } |
| 1683 | else |
| 1684 | { |
| 1685 | if(g_Config.m_GfxQuadAsTriangle) |
| 1686 | { |
| 1687 | for(int i = 0; i < QuadDrawNum; ++i) |
| 1688 | { |
| 1689 | SQuadContainer::SQuad &Quad = Container.m_vQuads[QuadOffset + i]; |
| 1690 | m_aVertices[i * 6 + 0] = Quad.m_aVertices[0]; |
| 1691 | m_aVertices[i * 6 + 1] = Quad.m_aVertices[1]; |
| 1692 | m_aVertices[i * 6 + 2] = Quad.m_aVertices[2]; |
| 1693 | m_aVertices[i * 6 + 3] = Quad.m_aVertices[0]; |
| 1694 | m_aVertices[i * 6 + 4] = Quad.m_aVertices[2]; |
| 1695 | m_aVertices[i * 6 + 5] = Quad.m_aVertices[3]; |
| 1696 | |
| 1697 | for(int n = 0; n < 6; ++n) |
| 1698 | { |
| 1699 | m_aVertices[i * 6 + n].m_Pos.x *= ScaleX; |
| 1700 | m_aVertices[i * 6 + n].m_Pos.y *= ScaleY; |
| 1701 | m_aVertices[i * 6 + n].m_Color = m_aColor[0]; |
| 1702 | } |
| 1703 | |
| 1704 | if(m_Rotation != 0) |
| 1705 | { |
| 1706 | CCommandBuffer::SPoint Center; |
| 1707 | Center.x = m_aVertices[i * 6 + 0].m_Pos.x + (m_aVertices[i * 6 + 1].m_Pos.x - m_aVertices[i * 6 + 0].m_Pos.x) / 2.f; |
| 1708 | Center.y = m_aVertices[i * 6 + 0].m_Pos.y + (m_aVertices[i * 6 + 2].m_Pos.y - m_aVertices[i * 6 + 0].m_Pos.y) / 2.f; |
| 1709 | |
| 1710 | Rotate(Center, pPoints: &m_aVertices[i * 6 + 0], NumPoints: 6); |
| 1711 | } |
| 1712 | |
| 1713 | for(int n = 0; n < 6; ++n) |
| 1714 | { |
| 1715 | m_aVertices[i * 6 + n].m_Pos.x += X; |
| 1716 | m_aVertices[i * 6 + n].m_Pos.y += Y; |
| 1717 | } |
| 1718 | m_NumVertices += 6; |
| 1719 | } |
| 1720 | } |
| 1721 | else |
| 1722 | { |
| 1723 | mem_copy(dest: m_aVertices, source: &Container.m_vQuads[QuadOffset], size: sizeof(CCommandBuffer::SVertex) * 4 * QuadDrawNum); |
| 1724 | for(int i = 0; i < QuadDrawNum; ++i) |
| 1725 | { |
| 1726 | for(int n = 0; n < 4; ++n) |
| 1727 | { |
| 1728 | m_aVertices[i * 4 + n].m_Pos.x *= ScaleX; |
| 1729 | m_aVertices[i * 4 + n].m_Pos.y *= ScaleY; |
| 1730 | m_aVertices[i * 4 + n].m_Color = m_aColor[0]; |
| 1731 | } |
| 1732 | |
| 1733 | if(m_Rotation != 0) |
| 1734 | { |
| 1735 | CCommandBuffer::SPoint Center; |
| 1736 | Center.x = m_aVertices[i * 4 + 0].m_Pos.x + (m_aVertices[i * 4 + 1].m_Pos.x - m_aVertices[i * 4 + 0].m_Pos.x) / 2.f; |
| 1737 | Center.y = m_aVertices[i * 4 + 0].m_Pos.y + (m_aVertices[i * 4 + 2].m_Pos.y - m_aVertices[i * 4 + 0].m_Pos.y) / 2.f; |
| 1738 | |
| 1739 | Rotate(Center, pPoints: &m_aVertices[i * 4 + 0], NumPoints: 4); |
| 1740 | } |
| 1741 | |
| 1742 | for(int n = 0; n < 4; ++n) |
| 1743 | { |
| 1744 | m_aVertices[i * 4 + n].m_Pos.x += X; |
| 1745 | m_aVertices[i * 4 + n].m_Pos.y += Y; |
| 1746 | } |
| 1747 | m_NumVertices += 4; |
| 1748 | } |
| 1749 | } |
| 1750 | m_Drawing = EDrawing::QUADS; |
| 1751 | WrapClamp(); |
| 1752 | FlushVertices(KeepVertices: false); |
| 1753 | m_Drawing = EDrawing::NONE; |
| 1754 | } |
| 1755 | WrapNormal(); |
| 1756 | } |
| 1757 | |
| 1758 | void CGraphics_Threaded::RenderQuadContainerAsSprite(int ContainerIndex, int QuadOffset, float X, float Y, float ScaleX, float ScaleY) |
| 1759 | { |
| 1760 | RenderQuadContainerEx(ContainerIndex, QuadOffset, QuadDrawNum: 1, X, Y, ScaleX, ScaleY); |
| 1761 | } |
| 1762 | |
| 1763 | void CGraphics_Threaded::RenderQuadContainerAsSpriteMultiple(int ContainerIndex, int QuadOffset, int DrawCount, SRenderSpriteInfo *pRenderInfo) |
| 1764 | { |
| 1765 | SQuadContainer &Container = m_vQuadContainers[ContainerIndex]; |
| 1766 | |
| 1767 | if(DrawCount == 0) |
| 1768 | return; |
| 1769 | |
| 1770 | if(IsQuadContainerBufferingEnabled()) |
| 1771 | { |
| 1772 | if(Container.m_QuadBufferContainerIndex == -1) |
| 1773 | return; |
| 1774 | |
| 1775 | WrapClamp(); |
| 1776 | SQuadContainer::SQuad &Quad = Container.m_vQuads[0]; |
| 1777 | CCommandBuffer::SCommand_RenderQuadContainerAsSpriteMultiple Cmd; |
| 1778 | |
| 1779 | Cmd.m_State = m_State; |
| 1780 | |
| 1781 | Cmd.m_DrawNum = 1 * 6; |
| 1782 | Cmd.m_DrawCount = DrawCount; |
| 1783 | Cmd.m_pOffset = (void *)(QuadOffset * 6 * sizeof(unsigned int)); |
| 1784 | Cmd.m_BufferContainerIndex = Container.m_QuadBufferContainerIndex; |
| 1785 | |
| 1786 | Cmd.m_VertexColor.r = (float)m_aColor[0].r / 255.f; |
| 1787 | Cmd.m_VertexColor.g = (float)m_aColor[0].g / 255.f; |
| 1788 | Cmd.m_VertexColor.b = (float)m_aColor[0].b / 255.f; |
| 1789 | Cmd.m_VertexColor.a = (float)m_aColor[0].a / 255.f; |
| 1790 | |
| 1791 | // rotate before positioning |
| 1792 | Cmd.m_Center.x = Quad.m_aVertices[0].m_Pos.x + (Quad.m_aVertices[1].m_Pos.x - Quad.m_aVertices[0].m_Pos.x) / 2.f; |
| 1793 | Cmd.m_Center.y = Quad.m_aVertices[0].m_Pos.y + (Quad.m_aVertices[2].m_Pos.y - Quad.m_aVertices[0].m_Pos.y) / 2.f; |
| 1794 | |
| 1795 | Cmd.m_pRenderInfo = (IGraphics::SRenderSpriteInfo *)m_pCommandBuffer->AllocData(WantedSize: sizeof(IGraphics::SRenderSpriteInfo) * DrawCount); |
| 1796 | if(Cmd.m_pRenderInfo == nullptr) |
| 1797 | { |
| 1798 | // kick command buffer and try again |
| 1799 | KickCommandBuffer(); |
| 1800 | |
| 1801 | Cmd.m_pRenderInfo = (IGraphics::SRenderSpriteInfo *)m_pCommandBuffer->AllocData(WantedSize: sizeof(IGraphics::SRenderSpriteInfo) * DrawCount); |
| 1802 | if(Cmd.m_pRenderInfo == nullptr) |
| 1803 | { |
| 1804 | log_error("graphics" , "Failed to allocate data for quad container render info. DrawCount=%d" , DrawCount); |
| 1805 | return; |
| 1806 | } |
| 1807 | } |
| 1808 | |
| 1809 | AddCmd(Cmd, FailFunc: [&] { |
| 1810 | Cmd.m_pRenderInfo = (IGraphics::SRenderSpriteInfo *)m_pCommandBuffer->AllocData(WantedSize: sizeof(IGraphics::SRenderSpriteInfo) * DrawCount); |
| 1811 | return Cmd.m_pRenderInfo != nullptr; |
| 1812 | }); |
| 1813 | |
| 1814 | mem_copy(dest: Cmd.m_pRenderInfo, source: pRenderInfo, size: sizeof(IGraphics::SRenderSpriteInfo) * DrawCount); |
| 1815 | |
| 1816 | m_pCommandBuffer->AddRenderCalls(RenderCallCountToAdd: ((DrawCount - 1) / GRAPHICS_MAX_QUADS_RENDER_COUNT) + 1); |
| 1817 | |
| 1818 | WrapNormal(); |
| 1819 | } |
| 1820 | else |
| 1821 | { |
| 1822 | for(int i = 0; i < DrawCount; ++i) |
| 1823 | { |
| 1824 | QuadsSetRotation(Angle: pRenderInfo[i].m_Rotation); |
| 1825 | RenderQuadContainerAsSprite(ContainerIndex, QuadOffset, X: pRenderInfo[i].m_Pos.x, Y: pRenderInfo[i].m_Pos.y, ScaleX: pRenderInfo[i].m_Scale, ScaleY: pRenderInfo[i].m_Scale); |
| 1826 | } |
| 1827 | } |
| 1828 | } |
| 1829 | |
| 1830 | void *CGraphics_Threaded::AllocCommandBufferData(size_t AllocSize) |
| 1831 | { |
| 1832 | void *pData = m_pCommandBuffer->AllocData(WantedSize: AllocSize); |
| 1833 | if(pData == nullptr) |
| 1834 | { |
| 1835 | // kick command buffer and try again |
| 1836 | KickCommandBuffer(); |
| 1837 | |
| 1838 | pData = m_pCommandBuffer->AllocData(WantedSize: AllocSize); |
| 1839 | dbg_assert(pData, "graphics: failed to allocate data (size %" PRIzu ") for command buffer" , AllocSize); |
| 1840 | } |
| 1841 | return pData; |
| 1842 | } |
| 1843 | |
| 1844 | int CGraphics_Threaded::CreateBufferObject(size_t UploadDataSize, void *pUploadData, int CreateFlags, bool IsMovedPointer) |
| 1845 | { |
| 1846 | int Index = -1; |
| 1847 | if(m_FirstFreeBufferObjectIndex == -1) |
| 1848 | { |
| 1849 | Index = m_vBufferObjectIndices.size(); |
| 1850 | m_vBufferObjectIndices.push_back(x: Index); |
| 1851 | } |
| 1852 | else |
| 1853 | { |
| 1854 | Index = m_FirstFreeBufferObjectIndex; |
| 1855 | m_FirstFreeBufferObjectIndex = m_vBufferObjectIndices[Index]; |
| 1856 | m_vBufferObjectIndices[Index] = Index; |
| 1857 | } |
| 1858 | |
| 1859 | dbg_assert((CreateFlags & EBufferObjectCreateFlags::BUFFER_OBJECT_CREATE_FLAGS_ONE_TIME_USE_BIT) == 0 || (UploadDataSize <= CCommandBuffer::MAX_VERTICES * std::max(sizeof(CCommandBuffer::SVertexTex3DStream), sizeof(CCommandBuffer::SVertexTex3D))), |
| 1860 | "If BUFFER_OBJECT_CREATE_FLAGS_ONE_TIME_USE_BIT is used, then the buffer size must not exceed max(sizeof(CCommandBuffer::SVertexTex3DStream), sizeof(CCommandBuffer::SVertexTex3D)) * CCommandBuffer::MAX_VERTICES" ); |
| 1861 | |
| 1862 | CCommandBuffer::SCommand_CreateBufferObject Cmd; |
| 1863 | Cmd.m_BufferIndex = Index; |
| 1864 | Cmd.m_DataSize = UploadDataSize; |
| 1865 | Cmd.m_DeletePointer = IsMovedPointer; |
| 1866 | Cmd.m_Flags = CreateFlags; |
| 1867 | |
| 1868 | if(IsMovedPointer) |
| 1869 | { |
| 1870 | Cmd.m_pUploadData = pUploadData; |
| 1871 | AddCmd(Cmd); |
| 1872 | } |
| 1873 | else |
| 1874 | { |
| 1875 | if(UploadDataSize <= CMD_BUFFER_DATA_BUFFER_SIZE) |
| 1876 | { |
| 1877 | Cmd.m_pUploadData = AllocCommandBufferData(AllocSize: UploadDataSize); |
| 1878 | |
| 1879 | AddCmd(Cmd, FailFunc: [&] { |
| 1880 | Cmd.m_pUploadData = m_pCommandBuffer->AllocData(WantedSize: UploadDataSize); |
| 1881 | return Cmd.m_pUploadData != nullptr; |
| 1882 | }); |
| 1883 | mem_copy(dest: Cmd.m_pUploadData, source: pUploadData, size: UploadDataSize); |
| 1884 | } |
| 1885 | else |
| 1886 | { |
| 1887 | Cmd.m_pUploadData = nullptr; |
| 1888 | AddCmd(Cmd); |
| 1889 | |
| 1890 | // update the buffer instead |
| 1891 | size_t UploadDataOffset = 0; |
| 1892 | while(UploadDataSize > 0) |
| 1893 | { |
| 1894 | size_t UpdateSize = (UploadDataSize > CMD_BUFFER_DATA_BUFFER_SIZE ? CMD_BUFFER_DATA_BUFFER_SIZE : UploadDataSize); |
| 1895 | |
| 1896 | UpdateBufferObjectInternal(BufferIndex: Index, UploadDataSize: UpdateSize, pUploadData: (((char *)pUploadData) + UploadDataOffset), pOffset: (void *)UploadDataOffset); |
| 1897 | |
| 1898 | UploadDataOffset += UpdateSize; |
| 1899 | UploadDataSize -= UpdateSize; |
| 1900 | } |
| 1901 | } |
| 1902 | } |
| 1903 | |
| 1904 | return Index; |
| 1905 | } |
| 1906 | |
| 1907 | void CGraphics_Threaded::RecreateBufferObject(int BufferIndex, size_t UploadDataSize, void *pUploadData, int CreateFlags, bool IsMovedPointer) |
| 1908 | { |
| 1909 | CCommandBuffer::SCommand_RecreateBufferObject Cmd; |
| 1910 | Cmd.m_BufferIndex = BufferIndex; |
| 1911 | Cmd.m_DataSize = UploadDataSize; |
| 1912 | Cmd.m_DeletePointer = IsMovedPointer; |
| 1913 | Cmd.m_Flags = CreateFlags; |
| 1914 | |
| 1915 | dbg_assert((CreateFlags & EBufferObjectCreateFlags::BUFFER_OBJECT_CREATE_FLAGS_ONE_TIME_USE_BIT) == 0 || (UploadDataSize <= CCommandBuffer::MAX_VERTICES * std::max(sizeof(CCommandBuffer::SVertexTex3DStream), sizeof(CCommandBuffer::SVertexTex3D))), |
| 1916 | "If BUFFER_OBJECT_CREATE_FLAGS_ONE_TIME_USE_BIT is used, then the buffer size must not exceed max(sizeof(CCommandBuffer::SVertexTex3DStream), sizeof(CCommandBuffer::SVertexTex3D)) * CCommandBuffer::MAX_VERTICES" ); |
| 1917 | |
| 1918 | if(IsMovedPointer) |
| 1919 | { |
| 1920 | Cmd.m_pUploadData = pUploadData; |
| 1921 | AddCmd(Cmd); |
| 1922 | } |
| 1923 | else |
| 1924 | { |
| 1925 | if(UploadDataSize <= CMD_BUFFER_DATA_BUFFER_SIZE) |
| 1926 | { |
| 1927 | Cmd.m_pUploadData = AllocCommandBufferData(AllocSize: UploadDataSize); |
| 1928 | |
| 1929 | AddCmd(Cmd, FailFunc: [&] { |
| 1930 | Cmd.m_pUploadData = m_pCommandBuffer->AllocData(WantedSize: UploadDataSize); |
| 1931 | return Cmd.m_pUploadData != nullptr; |
| 1932 | }); |
| 1933 | |
| 1934 | mem_copy(dest: Cmd.m_pUploadData, source: pUploadData, size: UploadDataSize); |
| 1935 | } |
| 1936 | else |
| 1937 | { |
| 1938 | Cmd.m_pUploadData = nullptr; |
| 1939 | AddCmd(Cmd); |
| 1940 | |
| 1941 | // update the buffer instead |
| 1942 | size_t UploadDataOffset = 0; |
| 1943 | while(UploadDataSize > 0) |
| 1944 | { |
| 1945 | size_t UpdateSize = (UploadDataSize > CMD_BUFFER_DATA_BUFFER_SIZE ? CMD_BUFFER_DATA_BUFFER_SIZE : UploadDataSize); |
| 1946 | |
| 1947 | UpdateBufferObjectInternal(BufferIndex, UploadDataSize: UpdateSize, pUploadData: (((char *)pUploadData) + UploadDataOffset), pOffset: (void *)UploadDataOffset); |
| 1948 | |
| 1949 | UploadDataOffset += UpdateSize; |
| 1950 | UploadDataSize -= UpdateSize; |
| 1951 | } |
| 1952 | } |
| 1953 | } |
| 1954 | } |
| 1955 | |
| 1956 | void CGraphics_Threaded::UpdateBufferObjectInternal(int BufferIndex, size_t UploadDataSize, void *pUploadData, void *pOffset, bool IsMovedPointer) |
| 1957 | { |
| 1958 | CCommandBuffer::SCommand_UpdateBufferObject Cmd; |
| 1959 | Cmd.m_BufferIndex = BufferIndex; |
| 1960 | Cmd.m_DataSize = UploadDataSize; |
| 1961 | Cmd.m_pOffset = pOffset; |
| 1962 | Cmd.m_DeletePointer = IsMovedPointer; |
| 1963 | |
| 1964 | if(IsMovedPointer) |
| 1965 | { |
| 1966 | Cmd.m_pUploadData = pUploadData; |
| 1967 | AddCmd(Cmd); |
| 1968 | } |
| 1969 | else |
| 1970 | { |
| 1971 | Cmd.m_pUploadData = AllocCommandBufferData(AllocSize: UploadDataSize); |
| 1972 | |
| 1973 | AddCmd(Cmd, FailFunc: [&] { |
| 1974 | Cmd.m_pUploadData = m_pCommandBuffer->AllocData(WantedSize: UploadDataSize); |
| 1975 | return Cmd.m_pUploadData != nullptr; |
| 1976 | }); |
| 1977 | |
| 1978 | mem_copy(dest: Cmd.m_pUploadData, source: pUploadData, size: UploadDataSize); |
| 1979 | } |
| 1980 | } |
| 1981 | |
| 1982 | void CGraphics_Threaded::CopyBufferObjectInternal(int WriteBufferIndex, int ReadBufferIndex, size_t WriteOffset, size_t ReadOffset, size_t CopyDataSize) |
| 1983 | { |
| 1984 | CCommandBuffer::SCommand_CopyBufferObject Cmd; |
| 1985 | Cmd.m_WriteBufferIndex = WriteBufferIndex; |
| 1986 | Cmd.m_ReadBufferIndex = ReadBufferIndex; |
| 1987 | Cmd.m_WriteOffset = WriteOffset; |
| 1988 | Cmd.m_ReadOffset = ReadOffset; |
| 1989 | Cmd.m_CopySize = CopyDataSize; |
| 1990 | AddCmd(Cmd); |
| 1991 | } |
| 1992 | |
| 1993 | void CGraphics_Threaded::DeleteBufferObject(int BufferIndex) |
| 1994 | { |
| 1995 | if(BufferIndex == -1) |
| 1996 | return; |
| 1997 | |
| 1998 | CCommandBuffer::SCommand_DeleteBufferObject Cmd; |
| 1999 | Cmd.m_BufferIndex = BufferIndex; |
| 2000 | AddCmd(Cmd); |
| 2001 | |
| 2002 | // also clear the buffer object index |
| 2003 | m_vBufferObjectIndices[BufferIndex] = m_FirstFreeBufferObjectIndex; |
| 2004 | m_FirstFreeBufferObjectIndex = BufferIndex; |
| 2005 | } |
| 2006 | |
| 2007 | int CGraphics_Threaded::CreateBufferContainer(SBufferContainerInfo *pContainerInfo) |
| 2008 | { |
| 2009 | int Index = -1; |
| 2010 | if(m_FirstFreeVertexArrayInfo == -1) |
| 2011 | { |
| 2012 | Index = m_vVertexArrayInfo.size(); |
| 2013 | m_vVertexArrayInfo.emplace_back(); |
| 2014 | } |
| 2015 | else |
| 2016 | { |
| 2017 | Index = m_FirstFreeVertexArrayInfo; |
| 2018 | m_FirstFreeVertexArrayInfo = m_vVertexArrayInfo[Index].m_FreeIndex; |
| 2019 | m_vVertexArrayInfo[Index].m_FreeIndex = Index; |
| 2020 | } |
| 2021 | |
| 2022 | CCommandBuffer::SCommand_CreateBufferContainer Cmd; |
| 2023 | Cmd.m_BufferContainerIndex = Index; |
| 2024 | Cmd.m_AttrCount = pContainerInfo->m_vAttributes.size(); |
| 2025 | Cmd.m_Stride = pContainerInfo->m_Stride; |
| 2026 | Cmd.m_VertBufferBindingIndex = pContainerInfo->m_VertBufferBindingIndex; |
| 2027 | Cmd.m_pAttributes = (SBufferContainerInfo::SAttribute *)AllocCommandBufferData(AllocSize: Cmd.m_AttrCount * sizeof(SBufferContainerInfo::SAttribute)); |
| 2028 | |
| 2029 | AddCmd(Cmd, FailFunc: [&] { |
| 2030 | Cmd.m_pAttributes = (SBufferContainerInfo::SAttribute *)m_pCommandBuffer->AllocData(WantedSize: Cmd.m_AttrCount * sizeof(SBufferContainerInfo::SAttribute)); |
| 2031 | return Cmd.m_pAttributes != nullptr; |
| 2032 | }); |
| 2033 | |
| 2034 | mem_copy(dest: Cmd.m_pAttributes, source: pContainerInfo->m_vAttributes.data(), size: Cmd.m_AttrCount * sizeof(SBufferContainerInfo::SAttribute)); |
| 2035 | |
| 2036 | m_vVertexArrayInfo[Index].m_AssociatedBufferObjectIndex = pContainerInfo->m_VertBufferBindingIndex; |
| 2037 | |
| 2038 | return Index; |
| 2039 | } |
| 2040 | |
| 2041 | void CGraphics_Threaded::DeleteBufferContainer(int &ContainerIndex, bool DestroyAllBO) |
| 2042 | { |
| 2043 | if(ContainerIndex == -1) |
| 2044 | return; |
| 2045 | |
| 2046 | CCommandBuffer::SCommand_DeleteBufferContainer Cmd; |
| 2047 | Cmd.m_BufferContainerIndex = ContainerIndex; |
| 2048 | Cmd.m_DestroyAllBO = DestroyAllBO; |
| 2049 | AddCmd(Cmd); |
| 2050 | |
| 2051 | if(DestroyAllBO) |
| 2052 | { |
| 2053 | // delete all associated references |
| 2054 | int BufferObjectIndex = m_vVertexArrayInfo[ContainerIndex].m_AssociatedBufferObjectIndex; |
| 2055 | if(BufferObjectIndex != -1) |
| 2056 | { |
| 2057 | // clear the buffer object index |
| 2058 | m_vBufferObjectIndices[BufferObjectIndex] = m_FirstFreeBufferObjectIndex; |
| 2059 | m_FirstFreeBufferObjectIndex = BufferObjectIndex; |
| 2060 | } |
| 2061 | } |
| 2062 | m_vVertexArrayInfo[ContainerIndex].m_AssociatedBufferObjectIndex = -1; |
| 2063 | |
| 2064 | // also clear the buffer object index |
| 2065 | m_vVertexArrayInfo[ContainerIndex].m_FreeIndex = m_FirstFreeVertexArrayInfo; |
| 2066 | m_FirstFreeVertexArrayInfo = ContainerIndex; |
| 2067 | ContainerIndex = -1; |
| 2068 | } |
| 2069 | |
| 2070 | void CGraphics_Threaded::UpdateBufferContainerInternal(int ContainerIndex, SBufferContainerInfo *pContainerInfo) |
| 2071 | { |
| 2072 | CCommandBuffer::SCommand_UpdateBufferContainer Cmd; |
| 2073 | Cmd.m_BufferContainerIndex = ContainerIndex; |
| 2074 | Cmd.m_AttrCount = pContainerInfo->m_vAttributes.size(); |
| 2075 | Cmd.m_Stride = pContainerInfo->m_Stride; |
| 2076 | Cmd.m_VertBufferBindingIndex = pContainerInfo->m_VertBufferBindingIndex; |
| 2077 | Cmd.m_pAttributes = (SBufferContainerInfo::SAttribute *)AllocCommandBufferData(AllocSize: Cmd.m_AttrCount * sizeof(SBufferContainerInfo::SAttribute)); |
| 2078 | |
| 2079 | AddCmd(Cmd, FailFunc: [&] { |
| 2080 | Cmd.m_pAttributes = (SBufferContainerInfo::SAttribute *)m_pCommandBuffer->AllocData(WantedSize: Cmd.m_AttrCount * sizeof(SBufferContainerInfo::SAttribute)); |
| 2081 | return Cmd.m_pAttributes != nullptr; |
| 2082 | }); |
| 2083 | |
| 2084 | mem_copy(dest: Cmd.m_pAttributes, source: pContainerInfo->m_vAttributes.data(), size: Cmd.m_AttrCount * sizeof(SBufferContainerInfo::SAttribute)); |
| 2085 | |
| 2086 | m_vVertexArrayInfo[ContainerIndex].m_AssociatedBufferObjectIndex = pContainerInfo->m_VertBufferBindingIndex; |
| 2087 | } |
| 2088 | |
| 2089 | void CGraphics_Threaded::IndicesNumRequiredNotify(unsigned int RequiredIndicesCount) |
| 2090 | { |
| 2091 | CCommandBuffer::SCommand_IndicesRequiredNumNotify Cmd; |
| 2092 | Cmd.m_RequiredIndicesNum = RequiredIndicesCount; |
| 2093 | AddCmd(Cmd); |
| 2094 | } |
| 2095 | |
| 2096 | int CGraphics_Threaded::IssueInit() |
| 2097 | { |
| 2098 | // The flags have to be kept consistent with flags set in the CGraphicsBackend_SDL_GL::SetWindowParams function! |
| 2099 | |
| 2100 | bool IsPurelyWindowed = g_Config.m_GfxFullscreen == 0; |
| 2101 | bool IsExclusiveFullscreen = g_Config.m_GfxFullscreen == 1; |
| 2102 | bool IsDesktopFullscreen = g_Config.m_GfxFullscreen == 2; |
| 2103 | #ifndef CONF_FAMILY_WINDOWS |
| 2104 | // Windowed fullscreen is only available on Windows, use desktop fullscreen on other platforms |
| 2105 | IsDesktopFullscreen |= g_Config.m_GfxFullscreen == 3; |
| 2106 | #endif |
| 2107 | |
| 2108 | int Flags = 0; |
| 2109 | if(IsExclusiveFullscreen) |
| 2110 | { |
| 2111 | Flags |= IGraphicsBackend::INITFLAG_FULLSCREEN; |
| 2112 | } |
| 2113 | else if(IsDesktopFullscreen) |
| 2114 | { |
| 2115 | Flags |= IGraphicsBackend::INITFLAG_DESKTOP_FULLSCREEN; |
| 2116 | } |
| 2117 | else if(IsPurelyWindowed) |
| 2118 | { |
| 2119 | Flags |= IGraphicsBackend::INITFLAG_RESIZABLE; |
| 2120 | if(g_Config.m_GfxBorderless) |
| 2121 | { |
| 2122 | Flags |= IGraphicsBackend::INITFLAG_BORDERLESS; |
| 2123 | } |
| 2124 | } |
| 2125 | if(g_Config.m_GfxVsync) |
| 2126 | { |
| 2127 | Flags |= IGraphicsBackend::INITFLAG_VSYNC; |
| 2128 | } |
| 2129 | |
| 2130 | const int Result = m_pBackend->Init(pName: "DDNet Client" , pScreen: &g_Config.m_GfxScreen, pWidth: &g_Config.m_GfxScreenWidth, pHeight: &g_Config.m_GfxScreenHeight, pRefreshRate: &g_Config.m_GfxScreenRefreshRate, pFsaaSamples: &g_Config.m_GfxFsaaSamples, Flags, pDesktopWidth: &m_DesktopSize.x, pDesktopHeight: &m_DesktopSize.y, pCurrentWidth: &m_ScreenWidth, pCurrentHeight: &m_ScreenHeight, pStorage: m_pStorage); |
| 2131 | m_DrawableWidth = m_ScreenWidth; |
| 2132 | m_DrawableHeight = m_ScreenHeight; |
| 2133 | AddBackEndWarningIfExists(); |
| 2134 | if(Result == 0) |
| 2135 | { |
| 2136 | m_GLUseTrianglesAsQuad = m_pBackend->UseTrianglesAsQuad(); |
| 2137 | m_GLTileBufferingEnabled = m_pBackend->HasTileBuffering(); |
| 2138 | m_GLQuadBufferingEnabled = m_pBackend->HasQuadBuffering(); |
| 2139 | m_GLQuadContainerBufferingEnabled = m_pBackend->HasQuadContainerBuffering(); |
| 2140 | m_GLTextBufferingEnabled = (m_GLQuadContainerBufferingEnabled && m_pBackend->HasTextBuffering()); |
| 2141 | m_GLUses2DTextureArrays = m_pBackend->Uses2DTextureArrays(); |
| 2142 | m_GLHasTextureArraysSupport = m_pBackend->HasTextureArraysSupport(); |
| 2143 | m_ScreenHiDPIScale = m_ScreenWidth / (float)g_Config.m_GfxScreenWidth; |
| 2144 | m_ScreenRefreshRate = g_Config.m_GfxScreenRefreshRate; |
| 2145 | } |
| 2146 | return Result; |
| 2147 | } |
| 2148 | |
| 2149 | void CGraphics_Threaded::AdjustViewport(bool SendViewportChangeToBackend) |
| 2150 | { |
| 2151 | // exclude the area covered by the display cutout, as nothing rendered there is visible |
| 2152 | int InsetLeft, InsetRight; |
| 2153 | m_pBackend->GetDisplayCutoutInsets(Left&: InsetLeft, Right&: InsetRight); |
| 2154 | m_ViewportX = InsetLeft; |
| 2155 | m_ScreenWidth = m_DrawableWidth - InsetLeft - InsetRight; |
| 2156 | |
| 2157 | // adjust the viewport to only allow certain aspect ratios |
| 2158 | // keep this in sync with backend_vulkan GetSwapImageSize's check |
| 2159 | if(m_ScreenHeight > 4 * m_ScreenWidth / 5) |
| 2160 | { |
| 2161 | m_IsForcedViewport = true; |
| 2162 | m_ScreenHeight = 4 * m_ScreenWidth / 5; |
| 2163 | } |
| 2164 | else |
| 2165 | { |
| 2166 | m_IsForcedViewport = false; |
| 2167 | } |
| 2168 | |
| 2169 | if(SendViewportChangeToBackend && (m_ScreenWidth != m_DrawableWidth || m_ScreenHeight != m_DrawableHeight)) |
| 2170 | { |
| 2171 | UpdateViewport(X: m_ViewportX, Y: 0, W: m_ScreenWidth, H: m_ScreenHeight, ByResize: true); |
| 2172 | } |
| 2173 | } |
| 2174 | |
| 2175 | void CGraphics_Threaded::UpdateViewport(int X, int Y, int W, int H, bool ByResize) |
| 2176 | { |
| 2177 | CCommandBuffer::SCommand_Update_Viewport Cmd; |
| 2178 | Cmd.m_X = X; |
| 2179 | Cmd.m_Y = Y; |
| 2180 | Cmd.m_Width = W; |
| 2181 | Cmd.m_Height = H; |
| 2182 | Cmd.m_DrawableWidth = m_DrawableWidth; |
| 2183 | Cmd.m_DrawableHeight = m_DrawableHeight; |
| 2184 | Cmd.m_ByResize = ByResize; |
| 2185 | AddCmd(Cmd); |
| 2186 | } |
| 2187 | |
| 2188 | void CGraphics_Threaded::AddBackEndWarningIfExists() |
| 2189 | { |
| 2190 | const char *pErrStr = m_pBackend->GetErrorString(); |
| 2191 | if(pErrStr != nullptr) |
| 2192 | { |
| 2193 | SWarning NewWarning; |
| 2194 | str_copy(dst&: NewWarning.m_aWarningMsg, src: Localize(pStr: pErrStr)); |
| 2195 | AddWarning(Warning: NewWarning); |
| 2196 | } |
| 2197 | } |
| 2198 | |
| 2199 | int CGraphics_Threaded::InitWindow() |
| 2200 | { |
| 2201 | int ErrorCode = IssueInit(); |
| 2202 | if(ErrorCode == 0) |
| 2203 | return 0; |
| 2204 | |
| 2205 | // try disabling fsaa |
| 2206 | while(g_Config.m_GfxFsaaSamples) |
| 2207 | { |
| 2208 | // 4 is the minimum required by OpenGL ES spec (GL_MAX_SAMPLES - https://www.khronos.org/registry/OpenGL-Refpages/es3.0/html/glGet.xhtml), |
| 2209 | // so can probably also be assumed for OpenGL |
| 2210 | if(g_Config.m_GfxFsaaSamples > 4) |
| 2211 | g_Config.m_GfxFsaaSamples = 4; |
| 2212 | else |
| 2213 | g_Config.m_GfxFsaaSamples = 0; |
| 2214 | |
| 2215 | if(g_Config.m_GfxFsaaSamples) |
| 2216 | log_warn("gfx" , "Failed to initialize graphics. Lowering FSAA to %d and trying again." , g_Config.m_GfxFsaaSamples); |
| 2217 | else |
| 2218 | log_warn("gfx" , "Failed to initialize graphics. Disabling FSAA and trying again." ); |
| 2219 | |
| 2220 | ErrorCode = IssueInit(); |
| 2221 | if(ErrorCode == 0) |
| 2222 | return 0; |
| 2223 | } |
| 2224 | |
| 2225 | size_t GLInitTryCount = 0; |
| 2226 | while(ErrorCode == EGraphicsBackendErrorCodes::GRAPHICS_BACKEND_ERROR_CODE_GL_CONTEXT_FAILED || |
| 2227 | ErrorCode == EGraphicsBackendErrorCodes::GRAPHICS_BACKEND_ERROR_CODE_GL_VERSION_FAILED) |
| 2228 | { |
| 2229 | if(ErrorCode == EGraphicsBackendErrorCodes::GRAPHICS_BACKEND_ERROR_CODE_GL_CONTEXT_FAILED) |
| 2230 | { |
| 2231 | // try next smaller major/minor or patch version |
| 2232 | if(g_Config.m_GfxGLMajor >= 4) |
| 2233 | { |
| 2234 | g_Config.m_GfxGLMajor = 3; |
| 2235 | g_Config.m_GfxGLMinor = 3; |
| 2236 | g_Config.m_GfxGLPatch = 0; |
| 2237 | } |
| 2238 | else if(g_Config.m_GfxGLMajor == 3 && g_Config.m_GfxGLMinor >= 1) |
| 2239 | { |
| 2240 | g_Config.m_GfxGLMajor = 3; |
| 2241 | g_Config.m_GfxGLMinor = 0; |
| 2242 | g_Config.m_GfxGLPatch = 0; |
| 2243 | } |
| 2244 | else if(g_Config.m_GfxGLMajor == 3 && g_Config.m_GfxGLMinor == 0) |
| 2245 | { |
| 2246 | g_Config.m_GfxGLMajor = 2; |
| 2247 | g_Config.m_GfxGLMinor = 1; |
| 2248 | g_Config.m_GfxGLPatch = 0; |
| 2249 | } |
| 2250 | else if(g_Config.m_GfxGLMajor == 2 && g_Config.m_GfxGLMinor >= 1) |
| 2251 | { |
| 2252 | g_Config.m_GfxGLMajor = 2; |
| 2253 | g_Config.m_GfxGLMinor = 0; |
| 2254 | g_Config.m_GfxGLPatch = 0; |
| 2255 | } |
| 2256 | else if(g_Config.m_GfxGLMajor == 2 && g_Config.m_GfxGLMinor == 0) |
| 2257 | { |
| 2258 | g_Config.m_GfxGLMajor = 1; |
| 2259 | g_Config.m_GfxGLMinor = 5; |
| 2260 | g_Config.m_GfxGLPatch = 0; |
| 2261 | } |
| 2262 | else if(g_Config.m_GfxGLMajor == 1 && g_Config.m_GfxGLMinor == 5) |
| 2263 | { |
| 2264 | g_Config.m_GfxGLMajor = 1; |
| 2265 | g_Config.m_GfxGLMinor = 4; |
| 2266 | g_Config.m_GfxGLPatch = 0; |
| 2267 | } |
| 2268 | else if(g_Config.m_GfxGLMajor == 1 && g_Config.m_GfxGLMinor == 4) |
| 2269 | { |
| 2270 | g_Config.m_GfxGLMajor = 1; |
| 2271 | g_Config.m_GfxGLMinor = 3; |
| 2272 | g_Config.m_GfxGLPatch = 0; |
| 2273 | } |
| 2274 | else if(g_Config.m_GfxGLMajor == 1 && g_Config.m_GfxGLMinor == 3) |
| 2275 | { |
| 2276 | g_Config.m_GfxGLMajor = 1; |
| 2277 | g_Config.m_GfxGLMinor = 2; |
| 2278 | g_Config.m_GfxGLPatch = 1; |
| 2279 | } |
| 2280 | else if(g_Config.m_GfxGLMajor == 1 && g_Config.m_GfxGLMinor == 2) |
| 2281 | { |
| 2282 | g_Config.m_GfxGLMajor = 1; |
| 2283 | g_Config.m_GfxGLMinor = 1; |
| 2284 | g_Config.m_GfxGLPatch = 0; |
| 2285 | } |
| 2286 | } |
| 2287 | log_warn("gfx" , "Failed to initialize graphics. Setting GL version %d.%d.%d and trying again." , g_Config.m_GfxGLMajor, g_Config.m_GfxGLMinor, g_Config.m_GfxGLPatch); |
| 2288 | |
| 2289 | // new gl version was set by backend, try again |
| 2290 | ErrorCode = IssueInit(); |
| 2291 | if(ErrorCode == 0) |
| 2292 | { |
| 2293 | return 0; |
| 2294 | } |
| 2295 | |
| 2296 | if(++GLInitTryCount >= 9) |
| 2297 | { |
| 2298 | // try something else |
| 2299 | break; |
| 2300 | } |
| 2301 | } |
| 2302 | |
| 2303 | // try lowering the resolution |
| 2304 | if(g_Config.m_GfxScreenWidth != 640 || g_Config.m_GfxScreenHeight != 480) |
| 2305 | { |
| 2306 | g_Config.m_GfxScreenWidth = 640; |
| 2307 | g_Config.m_GfxScreenHeight = 480; |
| 2308 | log_warn("gfx" , "Failed to initialize graphics. Setting resolution to %dx%d and trying again." , g_Config.m_GfxScreenWidth, g_Config.m_GfxScreenHeight); |
| 2309 | |
| 2310 | if(IssueInit() == 0) |
| 2311 | return 0; |
| 2312 | } |
| 2313 | |
| 2314 | // at the very end, just try to set to gl 1.4 |
| 2315 | { |
| 2316 | g_Config.m_GfxGLMajor = 1; |
| 2317 | g_Config.m_GfxGLMinor = 4; |
| 2318 | g_Config.m_GfxGLPatch = 0; |
| 2319 | log_warn("gfx" , "Failed to initialize graphics. Setting GL version %d.%d.%d and trying again." , g_Config.m_GfxGLMajor, g_Config.m_GfxGLMinor, g_Config.m_GfxGLPatch); |
| 2320 | |
| 2321 | if(IssueInit() == 0) |
| 2322 | return 0; |
| 2323 | } |
| 2324 | |
| 2325 | log_error("gfx" , "Failed to initialize graphics. Out of ideas." ); |
| 2326 | return -1; |
| 2327 | } |
| 2328 | |
| 2329 | int CGraphics_Threaded::Init() |
| 2330 | { |
| 2331 | // fetch pointers |
| 2332 | m_pStorage = Kernel()->RequestInterface<IStorage>(); |
| 2333 | m_pEngine = Kernel()->RequestInterface<IEngine>(); |
| 2334 | |
| 2335 | // init textures |
| 2336 | m_FirstFreeTexture = 0; |
| 2337 | m_vTextureIndices.resize(sz: CCommandBuffer::MAX_TEXTURES); |
| 2338 | for(size_t i = 0; i < m_vTextureIndices.size(); ++i) |
| 2339 | m_vTextureIndices[i] = i + 1; |
| 2340 | |
| 2341 | m_FirstFreeVertexArrayInfo = -1; |
| 2342 | m_FirstFreeBufferObjectIndex = -1; |
| 2343 | m_FirstFreeQuadContainer = -1; |
| 2344 | |
| 2345 | m_pBackend = CreateGraphicsBackend(TranslateFunc: Localize); |
| 2346 | if(InitWindow() != 0) |
| 2347 | return -1; |
| 2348 | |
| 2349 | for(auto &FakeMode : g_aFakeModes) |
| 2350 | { |
| 2351 | FakeMode.m_WindowWidth = FakeMode.m_CanvasWidth / m_ScreenHiDPIScale; |
| 2352 | FakeMode.m_WindowHeight = FakeMode.m_CanvasHeight / m_ScreenHiDPIScale; |
| 2353 | FakeMode.m_RefreshRate = g_Config.m_GfxScreenRefreshRate; |
| 2354 | } |
| 2355 | |
| 2356 | // create command buffers |
| 2357 | for(auto &pCommandBuffer : m_apCommandBuffers) |
| 2358 | pCommandBuffer = new CCommandBuffer(CMD_BUFFER_CMD_BUFFER_SIZE, CMD_BUFFER_DATA_BUFFER_SIZE); |
| 2359 | m_pCommandBuffer = m_apCommandBuffers[0]; |
| 2360 | |
| 2361 | // create null texture, will get id=0 |
| 2362 | { |
| 2363 | const size_t PixelSize = 4; |
| 2364 | const unsigned char aRed[] = {0xff, 0x00, 0x00, 0xff}; |
| 2365 | const unsigned char aGreen[] = {0x00, 0xff, 0x00, 0xff}; |
| 2366 | const unsigned char aBlue[] = {0x00, 0x00, 0xff, 0xff}; |
| 2367 | const unsigned char aYellow[] = {0xff, 0xff, 0x00, 0xff}; |
| 2368 | constexpr size_t NullTextureDimension = 16; |
| 2369 | unsigned char aNullTextureData[NullTextureDimension * NullTextureDimension * PixelSize]; |
| 2370 | for(size_t y = 0; y < NullTextureDimension; ++y) |
| 2371 | { |
| 2372 | for(size_t x = 0; x < NullTextureDimension; ++x) |
| 2373 | { |
| 2374 | const unsigned char *pColor; |
| 2375 | if(x < NullTextureDimension / 2 && y < NullTextureDimension / 2) |
| 2376 | pColor = aRed; |
| 2377 | else if(x >= NullTextureDimension / 2 && y < NullTextureDimension / 2) |
| 2378 | pColor = aGreen; |
| 2379 | else if(x < NullTextureDimension / 2 && y >= NullTextureDimension / 2) |
| 2380 | pColor = aBlue; |
| 2381 | else |
| 2382 | pColor = aYellow; |
| 2383 | mem_copy(dest: &aNullTextureData[(y * NullTextureDimension + x) * PixelSize], source: pColor, size: PixelSize); |
| 2384 | } |
| 2385 | } |
| 2386 | CImageInfo NullTextureInfo; |
| 2387 | NullTextureInfo.m_Width = NullTextureDimension; |
| 2388 | NullTextureInfo.m_Height = NullTextureDimension; |
| 2389 | NullTextureInfo.m_Format = CImageInfo::FORMAT_RGBA; |
| 2390 | NullTextureInfo.m_pData = aNullTextureData; |
| 2391 | m_NullTexture.Invalidate(); |
| 2392 | m_NullTexture = LoadTextureRaw(Image: NullTextureInfo, Flags: TextureLoadFlags(), pTexName: "null-texture" ); |
| 2393 | dbg_assert(m_NullTexture.IsNullTexture(), "Null texture invalid" ); |
| 2394 | } |
| 2395 | |
| 2396 | static constexpr LOG_COLOR GPU_INFO_LOG_COLOR = LOG_COLOR{.r: 153, .g: 127, .b: 255}; |
| 2397 | log_info_color(GPU_INFO_LOG_COLOR, "gfx" , "GPU vendor: %s" , GetVendorString()); |
| 2398 | log_info_color(GPU_INFO_LOG_COLOR, "gfx" , "GPU renderer: %s" , GetRendererString()); |
| 2399 | log_info_color(GPU_INFO_LOG_COLOR, "gfx" , "GPU version: %s" , GetVersionString()); |
| 2400 | |
| 2401 | AdjustViewport(SendViewportChangeToBackend: true); |
| 2402 | |
| 2403 | return 0; |
| 2404 | } |
| 2405 | |
| 2406 | void CGraphics_Threaded::Shutdown() |
| 2407 | { |
| 2408 | // shutdown the backend |
| 2409 | m_pBackend->Shutdown(); |
| 2410 | delete m_pBackend; |
| 2411 | m_pBackend = nullptr; |
| 2412 | |
| 2413 | // delete the command buffers |
| 2414 | for(auto &pCommandBuffer : m_apCommandBuffers) |
| 2415 | delete pCommandBuffer; |
| 2416 | } |
| 2417 | |
| 2418 | int CGraphics_Threaded::GetNumScreens() const |
| 2419 | { |
| 2420 | return m_pBackend->GetNumScreens(); |
| 2421 | } |
| 2422 | |
| 2423 | const char *CGraphics_Threaded::GetScreenName(int Screen) const |
| 2424 | { |
| 2425 | return m_pBackend->GetScreenName(Screen); |
| 2426 | } |
| 2427 | |
| 2428 | void CGraphics_Threaded::Minimize() |
| 2429 | { |
| 2430 | m_pBackend->Minimize(); |
| 2431 | |
| 2432 | for(auto &PropChangedListener : m_vPropChangeListeners) |
| 2433 | PropChangedListener(); |
| 2434 | } |
| 2435 | |
| 2436 | void CGraphics_Threaded::WarnPngliteIncompatibleImages(bool Warn) |
| 2437 | { |
| 2438 | m_WarnPngliteIncompatibleImages = Warn; |
| 2439 | } |
| 2440 | |
| 2441 | void CGraphics_Threaded::SetWindowParams(int FullscreenMode, bool IsBorderless) |
| 2442 | { |
| 2443 | g_Config.m_GfxFullscreen = std::clamp(val: FullscreenMode, lo: 0, hi: 3); |
| 2444 | g_Config.m_GfxBorderless = (int)IsBorderless; |
| 2445 | |
| 2446 | m_pBackend->SetWindowParams(FullscreenMode: g_Config.m_GfxFullscreen, IsBorderless: g_Config.m_GfxBorderless); |
| 2447 | CVideoMode CurMode; |
| 2448 | m_pBackend->GetCurrentVideoMode(CurMode, HiDPIScale: m_ScreenHiDPIScale, MaxWindowWidth: m_DesktopSize.x, MaxWindowHeight: m_DesktopSize.y, Screen: g_Config.m_GfxScreen); |
| 2449 | GotResized(w: CurMode.m_WindowWidth, h: CurMode.m_WindowHeight, RefreshRate: CurMode.m_RefreshRate); |
| 2450 | |
| 2451 | for(auto &PropChangedListener : m_vPropChangeListeners) |
| 2452 | PropChangedListener(); |
| 2453 | } |
| 2454 | |
| 2455 | bool CGraphics_Threaded::SetWindowScreen(int Index, bool MoveToCenter) |
| 2456 | { |
| 2457 | if(!m_pBackend->SetWindowScreen(Index, MoveToCenter, pDesktopSize: &m_DesktopSize)) |
| 2458 | { |
| 2459 | return false; |
| 2460 | } |
| 2461 | |
| 2462 | // send a got resized event so that the current canvas size is requested |
| 2463 | GotResized(w: g_Config.m_GfxScreenWidth, h: g_Config.m_GfxScreenHeight, RefreshRate: g_Config.m_GfxScreenRefreshRate); |
| 2464 | |
| 2465 | for(auto &PropChangedListener : m_vPropChangeListeners) |
| 2466 | PropChangedListener(); |
| 2467 | |
| 2468 | return true; |
| 2469 | } |
| 2470 | |
| 2471 | bool CGraphics_Threaded::SwitchWindowScreen(int Index, bool MoveToCenter) |
| 2472 | { |
| 2473 | const int IsFullscreen = g_Config.m_GfxFullscreen; |
| 2474 | const int IsBorderless = g_Config.m_GfxBorderless; |
| 2475 | const bool IsPurelyWindowed = IsFullscreen == 0 && !IsBorderless; |
| 2476 | |
| 2477 | if(!SetWindowScreen(Index, MoveToCenter: !IsPurelyWindowed || MoveToCenter)) |
| 2478 | { |
| 2479 | return false; |
| 2480 | } |
| 2481 | |
| 2482 | if(IsFullscreen != 3 && !IsPurelyWindowed) |
| 2483 | { |
| 2484 | // Prevent window from being stretched over multiple monitors by temporarily switching to |
| 2485 | // windowed fullscreen mode on Windows, which is desktop fullscreen mode on other systems. |
| 2486 | SetWindowParams(FullscreenMode: 3, IsBorderless: false); |
| 2487 | } |
| 2488 | |
| 2489 | // In purely windowed mode we preserve the window's size instead of resizing to the screen. |
| 2490 | if(!IsPurelyWindowed) |
| 2491 | { |
| 2492 | CVideoMode CurMode; |
| 2493 | GetCurrentVideoMode(CurMode, Screen: Index); |
| 2494 | |
| 2495 | g_Config.m_GfxScreenWidth = CurMode.m_WindowWidth; |
| 2496 | g_Config.m_GfxScreenHeight = CurMode.m_WindowHeight; |
| 2497 | g_Config.m_GfxScreenRefreshRate = CurMode.m_RefreshRate; |
| 2498 | |
| 2499 | ResizeToScreen(); |
| 2500 | } |
| 2501 | |
| 2502 | SetWindowParams(FullscreenMode: IsFullscreen, IsBorderless); |
| 2503 | return true; |
| 2504 | } |
| 2505 | |
| 2506 | void CGraphics_Threaded::Move(int x, int y) |
| 2507 | { |
| 2508 | #if defined(CONF_VIDEORECORDER) |
| 2509 | if(IVideo::Current() && IVideo::Current()->IsRecording()) |
| 2510 | return; |
| 2511 | #endif |
| 2512 | |
| 2513 | // Only handling CurScreen != m_GfxScreen doesn't work reliably |
| 2514 | const int CurScreen = m_pBackend->GetWindowScreen(); |
| 2515 | if(!m_pBackend->UpdateDisplayMode(Index: CurScreen, pDesktopSize: &m_DesktopSize)) |
| 2516 | return; |
| 2517 | |
| 2518 | // send a got resized event so that the current canvas size is requested |
| 2519 | GotResized(w: g_Config.m_GfxScreenWidth, h: g_Config.m_GfxScreenHeight, RefreshRate: g_Config.m_GfxScreenRefreshRate); |
| 2520 | |
| 2521 | for(auto &PropChangedListener : m_vPropChangeListeners) |
| 2522 | PropChangedListener(); |
| 2523 | } |
| 2524 | |
| 2525 | void CGraphics_Threaded::SetScreenSize(int Width, int Height) |
| 2526 | { |
| 2527 | m_ScreenWidth = Width; |
| 2528 | m_ScreenHeight = Height; |
| 2529 | UpdateViewport(X: 0, Y: 0, W: m_ScreenWidth, H: m_ScreenHeight, ByResize: true); |
| 2530 | } |
| 2531 | |
| 2532 | bool CGraphics_Threaded::Resize(int w, int h, int RefreshRate) |
| 2533 | { |
| 2534 | #if defined(CONF_VIDEORECORDER) |
| 2535 | if(IVideo::Current() && IVideo::Current()->IsRecording()) |
| 2536 | return false; |
| 2537 | #endif |
| 2538 | |
| 2539 | if(WindowWidth() == w && WindowHeight() == h && RefreshRate == m_ScreenRefreshRate) |
| 2540 | return false; |
| 2541 | |
| 2542 | // if the size is changed manually, only set the window resize, a window size changed event is triggered anyway |
| 2543 | if(m_pBackend->ResizeWindow(w, h, RefreshRate)) |
| 2544 | { |
| 2545 | CVideoMode CurMode; |
| 2546 | m_pBackend->GetCurrentVideoMode(CurMode, HiDPIScale: m_ScreenHiDPIScale, MaxWindowWidth: m_DesktopSize.x, MaxWindowHeight: m_DesktopSize.y, Screen: g_Config.m_GfxScreen); |
| 2547 | GotResized(w, h, RefreshRate); |
| 2548 | return true; |
| 2549 | } |
| 2550 | return false; |
| 2551 | } |
| 2552 | |
| 2553 | void CGraphics_Threaded::ResizeToScreen() |
| 2554 | { |
| 2555 | if(Resize(w: g_Config.m_GfxScreenWidth, h: g_Config.m_GfxScreenHeight, RefreshRate: g_Config.m_GfxScreenRefreshRate)) |
| 2556 | return; |
| 2557 | |
| 2558 | // Revert config variables if the change was not accepted |
| 2559 | g_Config.m_GfxScreenWidth = ScreenWidth(); |
| 2560 | g_Config.m_GfxScreenHeight = ScreenHeight(); |
| 2561 | g_Config.m_GfxScreenRefreshRate = m_ScreenRefreshRate; |
| 2562 | } |
| 2563 | |
| 2564 | void CGraphics_Threaded::GotResized(int w, int h, int RefreshRate) |
| 2565 | { |
| 2566 | #if defined(CONF_VIDEORECORDER) |
| 2567 | if(IVideo::Current() && IVideo::Current()->IsRecording()) |
| 2568 | return; |
| 2569 | #endif |
| 2570 | |
| 2571 | // if RefreshRate is -1 use the current config refresh rate |
| 2572 | if(RefreshRate == -1) |
| 2573 | RefreshRate = g_Config.m_GfxScreenRefreshRate; |
| 2574 | |
| 2575 | // if the size change event is triggered, set all parameters and change the viewport |
| 2576 | auto PrevCanvasWidth = m_ScreenWidth; |
| 2577 | auto PrevCanvasHeight = m_ScreenHeight; |
| 2578 | m_pBackend->GetViewportSize(w&: m_ScreenWidth, h&: m_ScreenHeight); |
| 2579 | m_DrawableWidth = m_ScreenWidth; |
| 2580 | m_DrawableHeight = m_ScreenHeight; |
| 2581 | |
| 2582 | AdjustViewport(SendViewportChangeToBackend: false); |
| 2583 | |
| 2584 | m_ScreenRefreshRate = RefreshRate; |
| 2585 | |
| 2586 | g_Config.m_GfxScreenWidth = w; |
| 2587 | g_Config.m_GfxScreenHeight = h; |
| 2588 | g_Config.m_GfxScreenRefreshRate = m_ScreenRefreshRate; |
| 2589 | |
| 2590 | auto OldDpi = m_ScreenHiDPIScale; |
| 2591 | m_ScreenHiDPIScale = m_DrawableWidth / (float)g_Config.m_GfxScreenWidth; |
| 2592 | |
| 2593 | // A DPI change must notify the listeners, since e.g. video modes |
| 2594 | // currently depend on it. |
| 2595 | if(OldDpi != m_ScreenHiDPIScale) |
| 2596 | { |
| 2597 | for(auto &PropChangedListener : m_vPropChangeListeners) |
| 2598 | PropChangedListener(); |
| 2599 | } |
| 2600 | |
| 2601 | UpdateViewport(X: m_ViewportX, Y: 0, W: m_ScreenWidth, H: m_ScreenHeight, ByResize: true); |
| 2602 | |
| 2603 | // kick the command buffer and wait |
| 2604 | KickCommandBuffer(); |
| 2605 | WaitForIdle(); |
| 2606 | |
| 2607 | if(PrevCanvasWidth != m_ScreenWidth || PrevCanvasHeight != m_ScreenHeight) |
| 2608 | { |
| 2609 | for(auto &ResizeListener : m_vResizeListeners) |
| 2610 | ResizeListener(); |
| 2611 | } |
| 2612 | } |
| 2613 | |
| 2614 | bool CGraphics_Threaded::IsScreenKeyboardShown() |
| 2615 | { |
| 2616 | return m_pBackend->IsScreenKeyboardShown(); |
| 2617 | } |
| 2618 | |
| 2619 | void CGraphics_Threaded::AddWindowResizeListener(WINDOW_RESIZE_FUNC pFunc) |
| 2620 | { |
| 2621 | m_vResizeListeners.emplace_back(args&: pFunc); |
| 2622 | } |
| 2623 | |
| 2624 | void CGraphics_Threaded::AddWindowPropChangeListener(WINDOW_PROPS_CHANGED_FUNC pFunc) |
| 2625 | { |
| 2626 | m_vPropChangeListeners.emplace_back(args&: pFunc); |
| 2627 | } |
| 2628 | |
| 2629 | int CGraphics_Threaded::GetWindowScreen() |
| 2630 | { |
| 2631 | return m_pBackend->GetWindowScreen(); |
| 2632 | } |
| 2633 | |
| 2634 | void CGraphics_Threaded::WindowDestroyNtf(uint32_t WindowId) |
| 2635 | { |
| 2636 | m_pBackend->WindowDestroyNtf(WindowId); |
| 2637 | |
| 2638 | CCommandBuffer::SCommand_WindowDestroyNtf Cmd; |
| 2639 | Cmd.m_WindowId = WindowId; |
| 2640 | AddCmd(Cmd); |
| 2641 | |
| 2642 | // wait |
| 2643 | KickCommandBuffer(); |
| 2644 | WaitForIdle(); |
| 2645 | } |
| 2646 | |
| 2647 | void CGraphics_Threaded::WindowCreateNtf(uint32_t WindowId) |
| 2648 | { |
| 2649 | m_pBackend->WindowCreateNtf(WindowId); |
| 2650 | |
| 2651 | CCommandBuffer::SCommand_WindowCreateNtf Cmd; |
| 2652 | Cmd.m_WindowId = WindowId; |
| 2653 | AddCmd(Cmd); |
| 2654 | |
| 2655 | // wait |
| 2656 | KickCommandBuffer(); |
| 2657 | WaitForIdle(); |
| 2658 | } |
| 2659 | |
| 2660 | int CGraphics_Threaded::WindowActive() |
| 2661 | { |
| 2662 | return m_pBackend->WindowActive(); |
| 2663 | } |
| 2664 | |
| 2665 | int CGraphics_Threaded::WindowOpen() |
| 2666 | { |
| 2667 | return m_pBackend->WindowOpen(); |
| 2668 | } |
| 2669 | |
| 2670 | void CGraphics_Threaded::SetWindowGrab(bool Grab) |
| 2671 | { |
| 2672 | m_pBackend->SetWindowGrab(Grab); |
| 2673 | } |
| 2674 | |
| 2675 | void CGraphics_Threaded::NotifyWindow() |
| 2676 | { |
| 2677 | m_pBackend->NotifyWindow(); |
| 2678 | } |
| 2679 | |
| 2680 | void CGraphics_Threaded::ReadPixel(ivec2 Position, ColorRGBA *pColor) |
| 2681 | { |
| 2682 | dbg_assert(Position.x >= 0 && Position.x < ScreenWidth(), "ReadPixel position x out of range" ); |
| 2683 | dbg_assert(Position.y >= 0 && Position.y < ScreenHeight(), "ReadPixel position y out of range" ); |
| 2684 | |
| 2685 | m_ReadPixelPosition = Position; |
| 2686 | m_pReadPixelColor = pColor; |
| 2687 | } |
| 2688 | |
| 2689 | void CGraphics_Threaded::ReadPixelDirect(bool *pSwapped) |
| 2690 | { |
| 2691 | if(m_pReadPixelColor == nullptr) |
| 2692 | return; |
| 2693 | |
| 2694 | CCommandBuffer::SCommand_TrySwapAndReadPixel Cmd; |
| 2695 | Cmd.m_Position = m_ReadPixelPosition; |
| 2696 | Cmd.m_pColor = m_pReadPixelColor; |
| 2697 | Cmd.m_pSwapped = pSwapped; |
| 2698 | AddCmd(Cmd); |
| 2699 | |
| 2700 | KickCommandBuffer(); |
| 2701 | WaitForIdle(); |
| 2702 | |
| 2703 | m_pReadPixelColor = nullptr; |
| 2704 | } |
| 2705 | |
| 2706 | void CGraphics_Threaded::TakeScreenshot(const char *pFilename) |
| 2707 | { |
| 2708 | // TODO: screenshot support |
| 2709 | char aDate[20]; |
| 2710 | str_timestamp(buffer: aDate, buffer_size: sizeof(aDate)); |
| 2711 | str_format(buffer: m_aScreenshotName, buffer_size: sizeof(m_aScreenshotName), format: "screenshots/%s_%s.png" , pFilename ? pFilename : "screenshot" , aDate); |
| 2712 | m_DoScreenshot = true; |
| 2713 | } |
| 2714 | |
| 2715 | void CGraphics_Threaded::TakeCustomScreenshot(const char *pFilename) |
| 2716 | { |
| 2717 | str_copy(dst&: m_aScreenshotName, src: pFilename); |
| 2718 | m_DoScreenshot = true; |
| 2719 | } |
| 2720 | |
| 2721 | void CGraphics_Threaded::ReadFramebuffer(CImageInfo &Image) |
| 2722 | { |
| 2723 | CCommandBuffer::SCommand_TrySwapAndScreenshot Cmd; |
| 2724 | Cmd.m_pImage = &Image; |
| 2725 | bool Swapped = false; |
| 2726 | Cmd.m_pSwapped = &Swapped; |
| 2727 | AddCmd(Cmd); |
| 2728 | KickCommandBuffer(); |
| 2729 | WaitForIdle(); |
| 2730 | } |
| 2731 | |
| 2732 | void CGraphics_Threaded::Swap() |
| 2733 | { |
| 2734 | #if defined(CONF_PLATFORM_IOS) |
| 2735 | // Rotating the device by 180 degrees moves the cutout to the other side without |
| 2736 | // changing the window size, which would not cause a resize event. |
| 2737 | int InsetLeft, InsetRight; |
| 2738 | m_pBackend->GetDisplayCutoutInsets(InsetLeft, InsetRight); |
| 2739 | if(InsetLeft != m_ViewportX || m_DrawableWidth - InsetLeft - InsetRight != m_ScreenWidth) |
| 2740 | { |
| 2741 | GotResized(g_Config.m_GfxScreenWidth, g_Config.m_GfxScreenHeight, -1); |
| 2742 | } |
| 2743 | #endif |
| 2744 | |
| 2745 | bool Swapped = false; |
| 2746 | ScreenshotDirect(pSwapped: &Swapped); |
| 2747 | ReadPixelDirect(pSwapped: &Swapped); |
| 2748 | |
| 2749 | if(!Swapped) |
| 2750 | { |
| 2751 | CCommandBuffer::SCommand_Swap Cmd; |
| 2752 | AddCmd(Cmd); |
| 2753 | } |
| 2754 | |
| 2755 | KickCommandBuffer(); |
| 2756 | } |
| 2757 | |
| 2758 | bool CGraphics_Threaded::SetVSync(bool State) |
| 2759 | { |
| 2760 | if(!m_pCommandBuffer) |
| 2761 | return true; |
| 2762 | |
| 2763 | // add vsync command |
| 2764 | bool RetOk = false; |
| 2765 | CCommandBuffer::SCommand_VSync Cmd; |
| 2766 | Cmd.m_VSync = State ? 1 : 0; |
| 2767 | Cmd.m_pRetOk = &RetOk; |
| 2768 | AddCmd(Cmd); |
| 2769 | |
| 2770 | // kick the command buffer |
| 2771 | KickCommandBuffer(); |
| 2772 | WaitForIdle(); |
| 2773 | |
| 2774 | if(RetOk) |
| 2775 | { |
| 2776 | g_Config.m_GfxVsync = State; |
| 2777 | } |
| 2778 | return RetOk; |
| 2779 | } |
| 2780 | |
| 2781 | bool CGraphics_Threaded::SetMultiSampling(uint32_t ReqMultiSamplingCount, uint32_t &MultiSamplingCountBackend) |
| 2782 | { |
| 2783 | if(!m_pCommandBuffer) |
| 2784 | return true; |
| 2785 | |
| 2786 | // add multisampling command |
| 2787 | bool RetOk = false; |
| 2788 | CCommandBuffer::SCommand_MultiSampling Cmd; |
| 2789 | Cmd.m_RequestedMultiSamplingCount = ReqMultiSamplingCount; |
| 2790 | Cmd.m_pRetMultiSamplingCount = &MultiSamplingCountBackend; |
| 2791 | Cmd.m_pRetOk = &RetOk; |
| 2792 | AddCmd(Cmd); |
| 2793 | |
| 2794 | // kick the command buffer |
| 2795 | KickCommandBuffer(); |
| 2796 | WaitForIdle(); |
| 2797 | return RetOk; |
| 2798 | } |
| 2799 | |
| 2800 | // synchronization |
| 2801 | void CGraphics_Threaded::InsertSignal(CSemaphore *pSemaphore) |
| 2802 | { |
| 2803 | CCommandBuffer::SCommand_Signal Cmd; |
| 2804 | Cmd.m_pSemaphore = pSemaphore; |
| 2805 | AddCmd(Cmd); |
| 2806 | } |
| 2807 | |
| 2808 | bool CGraphics_Threaded::IsIdle() const |
| 2809 | { |
| 2810 | return m_pBackend->IsIdle(); |
| 2811 | } |
| 2812 | |
| 2813 | void CGraphics_Threaded::WaitForIdle() |
| 2814 | { |
| 2815 | m_pBackend->WaitForIdle(); |
| 2816 | } |
| 2817 | |
| 2818 | void CGraphics_Threaded::AddWarning(const SWarning &Warning) |
| 2819 | { |
| 2820 | const std::unique_lock<std::mutex> Lock(m_WarningsMutex); |
| 2821 | m_vWarnings.emplace_back(args: Warning); |
| 2822 | } |
| 2823 | |
| 2824 | std::optional<SWarning> CGraphics_Threaded::CurrentWarning() |
| 2825 | { |
| 2826 | const std::unique_lock<std::mutex> Lock(m_WarningsMutex); |
| 2827 | if(m_vWarnings.empty()) |
| 2828 | { |
| 2829 | return std::nullopt; |
| 2830 | } |
| 2831 | else |
| 2832 | { |
| 2833 | std::optional<SWarning> Result = std::make_optional(t&: m_vWarnings[0]); |
| 2834 | m_vWarnings.erase(position: m_vWarnings.begin()); |
| 2835 | return Result; |
| 2836 | } |
| 2837 | } |
| 2838 | |
| 2839 | std::optional<int> CGraphics_Threaded::ShowMessageBox(const CMessageBox &MessageBox) |
| 2840 | { |
| 2841 | if(m_pBackend == nullptr) |
| 2842 | { |
| 2843 | return std::nullopt; |
| 2844 | } |
| 2845 | m_pBackend->WaitForIdle(); |
| 2846 | return m_pBackend->ShowMessageBox(MessageBox); |
| 2847 | } |
| 2848 | |
| 2849 | bool CGraphics_Threaded::IsBackendInitialized() |
| 2850 | { |
| 2851 | return m_pBackend != nullptr; |
| 2852 | } |
| 2853 | |
| 2854 | const char *CGraphics_Threaded::GetVendorString() |
| 2855 | { |
| 2856 | return m_pBackend->GetVendorString(); |
| 2857 | } |
| 2858 | |
| 2859 | const char *CGraphics_Threaded::GetVersionString() |
| 2860 | { |
| 2861 | return m_pBackend->GetVersionString(); |
| 2862 | } |
| 2863 | |
| 2864 | const char *CGraphics_Threaded::GetRendererString() |
| 2865 | { |
| 2866 | return m_pBackend->GetRendererString(); |
| 2867 | } |
| 2868 | |
| 2869 | const char *CGraphics_Threaded::GetFatalError() const |
| 2870 | { |
| 2871 | return m_pBackend == nullptr ? "" : m_pBackend->GetFatalError(); |
| 2872 | } |
| 2873 | |
| 2874 | TGLBackendReadPresentedImageData &CGraphics_Threaded::GetReadPresentedImageDataFuncUnsafe() |
| 2875 | { |
| 2876 | return m_pBackend->GetReadPresentedImageDataFuncUnsafe(); |
| 2877 | } |
| 2878 | |
| 2879 | int CGraphics_Threaded::GetVideoModes(CVideoMode *pModes, int MaxModes, int Screen) |
| 2880 | { |
| 2881 | if(g_Config.m_GfxDisplayAllVideoModes) |
| 2882 | { |
| 2883 | const int Count = std::min(a: std::size(g_aFakeModes), b: (size_t)MaxModes); |
| 2884 | mem_copy(dest: pModes, source: g_aFakeModes, size: Count * sizeof(CVideoMode)); |
| 2885 | return Count; |
| 2886 | } |
| 2887 | |
| 2888 | int NumModes = 0; |
| 2889 | m_pBackend->GetVideoModes(pModes, MaxModes, pNumModes: &NumModes, HiDPIScale: m_ScreenHiDPIScale, MaxWindowWidth: m_DesktopSize.x, MaxWindowHeight: m_DesktopSize.y, Screen); |
| 2890 | return NumModes; |
| 2891 | } |
| 2892 | |
| 2893 | void CGraphics_Threaded::GetCurrentVideoMode(CVideoMode &CurMode, int Screen) |
| 2894 | { |
| 2895 | m_pBackend->GetCurrentVideoMode(CurMode, HiDPIScale: m_ScreenHiDPIScale, MaxWindowWidth: m_DesktopSize.x, MaxWindowHeight: m_DesktopSize.y, Screen); |
| 2896 | } |
| 2897 | |
| 2898 | extern IEngineGraphics *CreateEngineGraphicsThreaded() |
| 2899 | { |
| 2900 | return new CGraphics_Threaded(); |
| 2901 | } |
| 2902 | |