-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
155 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#ifdef HELLOIMGUI_HAS_METAL | ||
#include "hello_imgui/image_from_asset.h" | ||
#include "imgui.h" | ||
#include "hello_imgui/internal/image_metal.h" | ||
#include "hello_imgui/internal/stb_image.h" | ||
#include "hello_imgui/hello_imgui_assets.h" | ||
|
||
#include <string> | ||
#include <unordered_map> | ||
|
||
namespace HelloImGui | ||
{ | ||
static std::unordered_map<std::string, ImageMetalPtr> gImageFromAssetMap; | ||
|
||
|
||
static ImageMetalPtr _GetCachedImageMetal(const char*assetPath) | ||
{ | ||
if (gImageFromAssetMap.find(assetPath) != gImageFromAssetMap.end()) | ||
return gImageFromAssetMap.at(assetPath); | ||
|
||
// Load the image using stbi_load_from_memory | ||
auto assetData = LoadAssetFileData(assetPath); | ||
assert(assetData.data != nullptr); | ||
int width, height; | ||
unsigned char*image_data_rgba = stbi_load_from_memory( | ||
(unsigned char *)assetData.data, (int)assetData.dataSize, | ||
&width, &height, NULL, 4); | ||
if (image_data_rgba == NULL) | ||
{ | ||
IM_ASSERT(false && "ImageMetal: Failed to load image!"); | ||
return nullptr; | ||
} | ||
|
||
// Create and store the VkImage | ||
if (gImageFromAssetMap.find(assetPath) == gImageFromAssetMap.end()) | ||
gImageFromAssetMap[assetPath] = std::make_shared<ImageMetal>(width, height, image_data_rgba); | ||
|
||
// Release image memory using stb | ||
stbi_image_free(image_data_rgba); | ||
|
||
return gImageFromAssetMap.at(assetPath); | ||
} | ||
|
||
void ImageFromAsset( | ||
const char *assetPath, const ImVec2& size, | ||
const ImVec2& uv0, const ImVec2& uv1, | ||
const ImVec4& tint_col, const ImVec4& border_col) | ||
{ | ||
auto textureId = ImTextureIdFromAsset(assetPath); | ||
auto imageSize = ImageSizeFromAsset(assetPath); | ||
ImVec2 displayedSize = ImageProportionalSize(size, imageSize); | ||
ImGui::Image(textureId, displayedSize, uv0, uv1, tint_col, border_col); | ||
} | ||
|
||
bool ImageButtonFromAsset(const char *assetPath, const ImVec2& size, const ImVec2& uv0, const ImVec2& uv1, int frame_padding, const ImVec4& bg_col, const ImVec4& tint_col) | ||
{ | ||
auto textureId = ImTextureIdFromAsset(assetPath); | ||
auto imageSize = ImageSizeFromAsset(assetPath); | ||
ImVec2 displayedSize = ImageProportionalSize(size, imageSize); | ||
bool clicked = ImGui::ImageButton(textureId, displayedSize, uv0, uv1, frame_padding, bg_col, tint_col); | ||
return clicked; | ||
} | ||
|
||
ImTextureID ImTextureIdFromAsset(const char *assetPath) | ||
{ | ||
auto cachedImage = _GetCachedImageMetal(assetPath); | ||
return (ImTextureID) cachedImage->Texture; | ||
} | ||
|
||
ImVec2 ImageSizeFromAsset(const char *assetPath) | ||
{ | ||
auto cachedImage = _GetCachedImageMetal(assetPath); | ||
return ImVec2((float)cachedImage->Width, (float)cachedImage->Height); | ||
} | ||
|
||
namespace internal | ||
{ | ||
void Free_ImageFromAssetMap() | ||
{ | ||
gImageFromAssetMap.clear(); | ||
} | ||
} | ||
|
||
} // namespace HelloImGui | ||
#endif // #ifdef HELLOIMGUI_HAS_METAL |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#pragma once | ||
#ifdef HELLOIMGUI_HAS_METAL | ||
|
||
#include <Metal/Metal.h> | ||
#include <QuartzCore/CAMetalLayer.h> | ||
#include <memory> | ||
|
||
namespace HelloImGui | ||
{ | ||
struct ImageMetal | ||
{ | ||
ImageMetal(int width, int height, unsigned char* image_data_rgba); | ||
~ImageMetal(); | ||
|
||
id<MTLTexture> Texture; | ||
int Width; | ||
int Height; | ||
}; | ||
|
||
using ImageMetalPtr = std::shared_ptr<ImageMetal>; | ||
} | ||
|
||
#endif // #ifdef HELLOIMGUI_HAS_METAL |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#ifdef HELLOIMGUI_HAS_METAL | ||
#include "image_metal.h" | ||
|
||
#include "imgui.h" | ||
#include "hello_imgui/internal/backend_impls/rendering_metal.h" | ||
|
||
namespace HelloImGui | ||
{ | ||
|
||
ImageMetal::ImageMetal(int width, int height, unsigned char* image_data_rgba) | ||
: Width(width), Height(height) | ||
{ | ||
auto gMetalGlobals = GetMetalGlobals(); | ||
|
||
// Create a MTLTextureDescriptor | ||
MTLTextureDescriptor* textureDescriptor = [[MTLTextureDescriptor alloc] init]; | ||
textureDescriptor.pixelFormat = MTLPixelFormatRGBA8Unorm; // Adjust as needed | ||
textureDescriptor.width = width; | ||
textureDescriptor.height = height; | ||
textureDescriptor.usage = MTLTextureUsageShaderRead | MTLTextureUsageRenderTarget; | ||
|
||
// Create the texture from the descriptor | ||
Texture = [gMetalGlobals.mtlDevice newTextureWithDescriptor:textureDescriptor]; | ||
|
||
// Upload the image data to the texture | ||
MTLRegion region = MTLRegionMake2D(0, 0, width, height); | ||
[Texture replaceRegion:region mipmapLevel:0 withBytes:image_data_rgba bytesPerRow:4 * width]; | ||
|
||
[textureDescriptor release]; | ||
} | ||
|
||
ImageMetal::~ImageMetal() | ||
{ | ||
[Texture release]; | ||
} | ||
} | ||
|
||
#endif // #ifdef HELLOIMGUI_HAS_VULKAN |