From 49943e13de910445266a19ba823d4126aeb84d61 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Fri, 24 Apr 2026 11:20:45 +0200 Subject: [PATCH] Fix various API issues --- .../Engine/Graphics/Textures/TextureBase.cpp | 33 +++++++++++++++++-- Source/Engine/Graphics/Textures/TextureBase.h | 6 ++++ Source/Engine/Graphics/Textures/TextureData.h | 4 +-- Source/Engine/Platform/Base/WindowBase.h | 2 +- Source/Engine/Platform/SDL/SDLWindow.cpp | 2 +- Source/Engine/Platform/SDL/SDLWindow.h | 2 +- 6 files changed, 41 insertions(+), 8 deletions(-) diff --git a/Source/Engine/Graphics/Textures/TextureBase.cpp b/Source/Engine/Graphics/Textures/TextureBase.cpp index ddd0498bc..7536dadc6 100644 --- a/Source/Engine/Graphics/Textures/TextureBase.cpp +++ b/Source/Engine/Graphics/Textures/TextureBase.cpp @@ -101,6 +101,30 @@ bool TextureMipData::GetPixels(Array& pixels, int32 width, int32 height src += srcRowSize; } } + if (PixelFormatExtensions::IsBGRAOrder(format)) + { + // BGRA -> RGBA + for (int32 y = 0; y < height; y++) + { + for (int32 x = 0; x < width; x++) + { + auto& color = *((Color32*)(dst + y * RowPitch + x * sizeof(Color32))); + color = Color32(color.B, color.G, color.R, color.A); + } + } + } + if (PixelFormatExtensions::IsSRGB(format)) + { + // sRGB -> Linear + for (int32 y = 0; y < height; y++) + { + for (int32 x = 0; x < width; x++) + { + auto& color = *((Color32*)(dst + y * RowPitch + x * sizeof(Color32))); + color = Color32(Color::SrgbToLinear(Color(color))); + } + } + } break; default: { @@ -118,7 +142,10 @@ bool TextureMipData::GetPixels(Array& pixels, int32 width, int32 height } return false; } - LOG(Error, "Unsupported texture data format {0}.", ScriptingEnum::ToString(format)); + if (PixelFormatExtensions::IsCompressed(format)) + LOG(Error, "Unsupported texture data format {0}. Compressed texture data cannot be accessed directly without decompressing first.", ScriptingEnum::ToString(format)); + else + LOG(Error, "Unsupported texture data format {0}.", ScriptingEnum::ToString(format)); return true; } } @@ -221,7 +248,7 @@ void TextureData::Clear() Items.Resize(0, false); } -bool TextureData::GetPixels(Array& pixels, int32 mipIndex, int32 arrayIndex) +bool TextureData::GetPixels(Array& pixels, int32 mipIndex, int32 arrayIndex) const { if (Items.IsValidIndex(arrayIndex) && Items.Get()[arrayIndex].Mips.IsValidIndex(mipIndex)) { @@ -232,7 +259,7 @@ bool TextureData::GetPixels(Array& pixels, int32 mipIndex, int32 arrayI return true; } -bool TextureData::GetPixels(Array& pixels, int32 mipIndex, int32 arrayIndex) +bool TextureData::GetPixels(Array& pixels, int32 mipIndex, int32 arrayIndex) const { if (Items.IsValidIndex(arrayIndex) && Items.Get()[arrayIndex].Mips.IsValidIndex(mipIndex)) { diff --git a/Source/Engine/Graphics/Textures/TextureBase.h b/Source/Engine/Graphics/Textures/TextureBase.h index a461a1018..930078157 100644 --- a/Source/Engine/Graphics/Textures/TextureBase.h +++ b/Source/Engine/Graphics/Textures/TextureBase.h @@ -162,6 +162,7 @@ public: /// /// Gets the mip data. /// + /// Use with caution as this operation loads texture data from the file. /// The mip index (zero-based). /// The data row pitch (in bytes). /// The data slice pitch (in bytes). @@ -171,6 +172,7 @@ public: /// /// Loads the texture data from the asset. /// + /// Use with caution as this operation loads texture data from the file. /// The result data. /// True if copy asset data to the result buffer, otherwise texture data will be linked to the internal storage (then the data is valid while asset is loaded and there is no texture data copy operations - faster). /// True if cannot load data, otherwise false. @@ -179,6 +181,7 @@ public: /// /// Loads the texture data from the asset (single mip). /// + /// Use with caution as this operation loads texture data from the file. /// The result data. /// The mip index (zero-based). /// The array or depth slice index (zero-based). @@ -189,6 +192,7 @@ public: /// /// Gets the texture pixels as Color32 array. /// + /// Use with caution as this operation loads texture data from the file. /// Supported only for 'basic' texture formats (uncompressed, single plane). /// The result texture pixels array. /// The mip index (zero-based). @@ -199,6 +203,7 @@ public: /// /// Gets the texture pixels as Color array. /// + /// Use with caution as this operation loads texture data from the file. /// Supported only for 'basic' texture formats (uncompressed, single plane). /// The result texture pixels array. /// The mip index (zero-based). @@ -252,6 +257,7 @@ private: #if !COMPILE_WITHOUT_CSHARP // Internal bindings API_FUNCTION(NoProxy) bool InitCSharp(void* ptr); + API_FUNCTION(NoProxy) TextureData* GetTextureData(); #endif public: diff --git a/Source/Engine/Graphics/Textures/TextureData.h b/Source/Engine/Graphics/Textures/TextureData.h index 248ca977c..b56b70077 100644 --- a/Source/Engine/Graphics/Textures/TextureData.h +++ b/Source/Engine/Graphics/Textures/TextureData.h @@ -132,7 +132,7 @@ public: /// The mip index (zero-based). /// The array or depth slice index (zero-based). /// True if failed, otherwise false. - API_FUNCTION() bool GetPixels(API_PARAM(Out) Array& pixels, int32 mipIndex = 0, int32 arrayIndex = 0); + API_FUNCTION() bool GetPixels(API_PARAM(Out) Array& pixels, int32 mipIndex = 0, int32 arrayIndex = 0) const; /// /// Gets the texture pixels as Color array. @@ -142,5 +142,5 @@ public: /// The mip index (zero-based). /// The array or depth slice index (zero-based). /// True if failed, otherwise false. - API_FUNCTION() bool GetPixels(API_PARAM(Out) Array& pixels, int32 mipIndex = 0, int32 arrayIndex = 0); + API_FUNCTION() bool GetPixels(API_PARAM(Out) Array& pixels, int32 mipIndex = 0, int32 arrayIndex = 0) const; }; diff --git a/Source/Engine/Platform/Base/WindowBase.h b/Source/Engine/Platform/Base/WindowBase.h index f7f2a4294..21309c9d9 100644 --- a/Source/Engine/Platform/Base/WindowBase.h +++ b/Source/Engine/Platform/Base/WindowBase.h @@ -597,7 +597,7 @@ public: /// Sets the window icon. /// /// The icon. - virtual void SetIcon(TextureData& icon) + virtual void SetIcon(const TextureData& icon) { } diff --git a/Source/Engine/Platform/SDL/SDLWindow.cpp b/Source/Engine/Platform/SDL/SDLWindow.cpp index df8603b61..a87ebbd89 100644 --- a/Source/Engine/Platform/SDL/SDLWindow.cpp +++ b/Source/Engine/Platform/SDL/SDLWindow.cpp @@ -1033,7 +1033,7 @@ void SDLWindow::UpdateCursor() SDL_SetCursor(cursor); } -void SDLWindow::SetIcon(TextureData& icon) +void SDLWindow::SetIcon(const TextureData& icon) { Array colorData; icon.GetPixels(colorData); diff --git a/Source/Engine/Platform/SDL/SDLWindow.h b/Source/Engine/Platform/SDL/SDLWindow.h index 0b24fb154..3c5963f5a 100644 --- a/Source/Engine/Platform/SDL/SDLWindow.h +++ b/Source/Engine/Platform/SDL/SDLWindow.h @@ -107,7 +107,7 @@ public: void EndClippingCursor() override; void SetMousePosition(const Float2& position) const override; void SetCursor(CursorType type) override; - void SetIcon(TextureData& icon) override; + void SetIcon(const TextureData& icon) override; void SetCursorImage(void* image) override; static void* LoadCursorImage(const StringView& path); static void* LoadCursorImage(const TextureData& image, const Int2& hotSpot = Int2::Zero);