Cogs.Core
TextureInspector.cpp
1#include "Inspectors.h"
2
3#include "imgui.h"
4
5#include "Context.h"
6#include "Renderer/RenderTexture.h"
7#include "Resources/TextureManager.h"
8
9#include "InspectorGuiHelper.h"
10
11namespace {
12 struct TextureInfo {
13 Cogs::Core::Texture* texture;
14 std::string header;
15
16 TextureInfo(Cogs::Core::ResourceBase* r, int& idx) {
17 texture = static_cast<Cogs::Core::Texture*>(r);
18
19 Cogs::Core::RenderResource* attachment = texture->getAttachedResource();
20 const Cogs::TextureDescription* description = &texture->description;
21
22 if (attachment) {
23 if (attachment->getType() == Cogs::Core::RenderResourceType::RenderTexture) {
24 description = &static_cast<Cogs::Core::RenderTexture*>(attachment)->description;
25 }
26 }
27 if (texture->getName().size()) {
28 header = texture->getName();
29 }
30 else if (texture->getId() != Cogs::Core::NoResourceId) {
31 header = "Texture (ID: " + std::to_string(r->getId()) + ")";
32 }
33 else {
34 header = "Texture (" + std::to_string(idx++) + ")";
35 }
36
37 header += " [" + std::to_string(description->width) + "x" + std::to_string(description->height);
38 if (description->depth > 1) {
39 header += " d:" + std::to_string(description->depth);
40 }
41 if (description->samples > 1) {
42 header += " s:" + std::to_string(description->samples);
43 }
44
45 const Cogs::FormatInfo* info = Cogs::getFormatInfo(description->format);
46
47 if (info && info->name) {
48 header += ", ";
49 header += info->name;
50 header += ", " + byteString(description->estimateMemorySize());
51 }
52
53 if (description->flags & Cogs::TextureFlags::RenderTarget) {
54 header += " RenderTarget";
55 }
56 if (description->flags & Cogs::TextureFlags::DepthBuffer) {
57 header += " DepthBuffer";
58 }
59 if (description->flags & Cogs::TextureFlags::GenerateMipMaps) {
60 header += " GenerateMipMaps";
61 }
62 if (description->flags & Cogs::TextureFlags::CubeMap) {
63 header += " CubeMap";
64 }
65 if (description->flags & Cogs::TextureFlags::ReadWriteTexture) {
66 header += " ReadWriteTexture";
67 }
68 if (description->flags & Cogs::TextureFlags::ExternalTexture) {
69 header += " ExternalTexture";
70 }
71 if (description->flags & Cogs::TextureFlags::UsageReadStaging) {
72 header += " UsageReadStaging";
73 }
74 if (description->flags & Cogs::TextureFlags::UsageWriteStaging) {
75 header += " UsageWriteStaging";
76 }
77 if (description->flags & Cogs::TextureFlags::NoDelete) {
78 header += " NoDelete";
79 }
80 header += "]";
81 }
82
83 static std::string byteString(size_t bytes) {
84 if (bytes < 16 * 1024) {
85 return std::to_string(bytes) + " B";
86 }
87 bytes = bytes / 1024;
88 if (bytes < 16 * 1024) {
89 return std::to_string(bytes) + " KiB";
90 }
91 bytes = bytes / 1024;
92 if (bytes < 16 * 1024) {
93 return std::to_string(bytes) + " MiB";
94 }
95 bytes = bytes / 1024;
96 return std::to_string(bytes) + " GiB";
97 }
98 };
99}
100
101void Cogs::Core::textureInspector(Context * context, bool * show)
102{
103 if (*show) {
104 std::vector<ResourceBase*> allocatedTextures = context->textureManager->getAllocatedResources();
105 std::vector<TextureInfo> textures;
106 std::string windowHeader;
107 int idx = 0;
108
109 windowHeader.reserve(50);
110 windowHeader.append("Textures [");
111 windowHeader.append(std::to_string(allocatedTextures.size()));
112 windowHeader.append("]###TextureInspector");
113
114 for (ResourceBase* res : allocatedTextures) {
115 textures.push_back(TextureInfo(res, idx));
116 }
117 std::sort(textures.begin(), textures.end(), [](const TextureInfo& lhs, const TextureInfo& rhs) { return lhs.header < rhs.header; });
118
119 guiBegin(windowHeader, show);
120
121 for (const TextureInfo& info : textures) {
122 if (ImGui::CollapsingHeader(getUniqueHeader(info.header).c_str())) {
123 Renderer* renderer = dynamic_cast<Cogs::Core::Renderer*>(context->renderer);
124 RenderResource* attachment = info.texture->getAttachedResource();
125
126 if (attachment && (attachment->getType() == RenderResourceType::RenderTexture)) {
127 showRenderTexture(context, renderer, static_cast<RenderTexture*>(attachment));
128 }
129 else {
130 showTexture(context, renderer, info.texture);
131 }
132 }
133 }
134 guiEnd();
135 }
136}
Core renderer system.
Definition: Renderer.h:28
constexpr size_t size() const noexcept
Get the size of the string.
Definition: StringView.h:178
Base class for engine resources.
Definition: ResourceBase.h:107
ResourceId getId() const
Get the resource id of this instance.
Definition: ResourceBase.h:215
StringView getName() const
Get the name of the resource.
Definition: ResourceBase.h:307
RenderResource * getAttachedResource() const
Get the attached resource.
Definition: ResourceBase.h:275
Texture resources contain raster bitmap data to use for texturing.
Definition: Texture.h:91
const char * name
Name as a color format (using RGBA as channels).
Definition: DataFormat.h:260
COGSRENDERING_DLL_API size_t estimateMemorySize() const
Attempts to estimate the amount of memory a texture with these attributes will require.
Definition: TextureData.cpp:57
@ DepthBuffer
The texture can be used as a depth target and have depth buffer values written into.
Definition: Flags.h:122
@ NoDelete
The ownership of the underlying texture resource is outside of cogs and cogs will not delete it.
Definition: Flags.h:136
@ RenderTarget
The texture can be used as a render target and drawn into.
Definition: Flags.h:120
@ GenerateMipMaps
The texture supports automatic mipmap generation performed by the graphics device.
Definition: Flags.h:124
@ ReadWriteTexture
The texture can be used as a read/write texture. Can be used to output data from compute shaders.
Definition: Flags.h:128
@ UsageReadStaging
The texture is intended to be used as a staging texture.
Definition: Flags.h:133
@ CubeMap
The texture can be used as a cube map.
Definition: Flags.h:126