ngscopeclient v0.1
TextureManager.h
Go to the documentation of this file.
1/***********************************************************************************************************************
2* *
3* ngscopeclient *
4* *
5* Copyright (c) 2012-2025 Andrew D. Zonenberg *
6* All rights reserved. *
7* *
8* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the *
9* following conditions are met: *
10* *
11* * Redistributions of source code must retain the above copyright notice, this list of conditions, and the *
12* following disclaimer. *
13* *
14* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the *
15* following disclaimer in the documentation and/or other materials provided with the distribution. *
16* *
17* * Neither the name of the author nor the names of any contributors may be used to endorse or promote products *
18* derived from this software without specific prior written permission. *
19* *
20* THIS SOFTWARE IS PROVIDED BY THE AUTHORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
21* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL *
22* THE AUTHORS BE HELD LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES *
23* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR *
24* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
25* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
26* POSSIBILITY OF SUCH DAMAGE. *
27* *
28***********************************************************************************************************************/
29
35#ifndef TextureManager_h
36#define TextureManager_h
37
38class TextureManager;
39
40#include <png.h>
41
48{
49public:
50 Texture(
51 const vk::raii::Device& device,
52 const vk::ImageCreateInfo& imageInfo,
53 const vk::raii::Buffer& srcBuf,
54 int width,
55 int height,
56 TextureManager* mgr,
57 const std::string& name = "",
58 bool upsampleLinear = true //false for nearest neighbor upsampling instead
59 );
60
61 Texture(
62 const vk::raii::Device& device,
63 const vk::ImageCreateInfo& imageInfo,
64 TextureManager* mgr,
65 const std::string& name = ""
66 );
67
68 ~Texture();
69
70 ImTextureID GetTexture()
71 { return m_texture; }
72
73 vk::ImageView GetView()
74 { return **m_view; }
75
76 vk::Image GetImage()
77 { return *m_image; }
78
79 void SetName(const std::string& name);
80
81protected:
82 void LayoutTransition(
83 vk::raii::CommandBuffer& cmdBuf,
84 vk::AccessFlags src,
85 vk::AccessFlags dst,
86 vk::ImageLayout from,
87 vk::ImageLayout to);
88
90 vk::raii::Image m_image;
91
93 std::unique_ptr<vk::raii::ImageView> m_view;
94
95 ImTextureID m_texture;
96
98 std::unique_ptr<vk::raii::DeviceMemory> m_deviceMemory;
99};
100
105{
106public:
107 TextureManager(std::shared_ptr<QueueHandle> queue);
108 virtual ~TextureManager();
109
110 void LoadTexture(
111 const std::string& name,
112 const std::string& path);
113
114 GLFWimage LoadPNGToGLFWImage(const std::string& path);
115
116 ImTextureID GetTexture(const std::string& name)
117 {
118 auto it = m_textures.find(name);
119 if(it == m_textures.end())
120 {
121 LogFatal(
122 "Texture \"%s\" not found. This is probably the result of a developer mistyping a texture ID.\n",
123 name.c_str());
124 }
125 else
126 return it->second->GetTexture();
127 }
128
129 std::unique_ptr<vk::raii::Sampler>& GetSampler()
130 { return m_sampler; }
131
132 std::unique_ptr<vk::raii::Sampler>& GetNearestSampler()
133 { return m_nearestSampler; }
134
135 void clear()
136 { m_textures.clear(); }
137
138 vk::raii::CommandBuffer& GetCmdBuffer()
139 { return *m_cmdBuf; }
140
141 std::shared_ptr<QueueHandle> GetQueue()
142 { return m_queue; }
143
144 vk::ImageView GetView(const std::string& name)
145 { return m_textures[name]->GetView(); }
146
147protected:
148
149 png_structp LoadPNG(
150 const std::string& path,
151 size_t& width,
152 size_t& height,
153 FILE*& fp,
154 png_infop& info,
155 png_infop& end);
156
157 std::map<std::string, std::shared_ptr<Texture> > m_textures;
158
160 std::unique_ptr<vk::raii::Sampler> m_sampler;
161
162 std::unique_ptr<vk::raii::Sampler> m_nearestSampler;
163
164 std::shared_ptr<QueueHandle> m_queue;
165 std::unique_ptr<vk::raii::CommandPool> m_cmdPool;
166 std::unique_ptr<vk::raii::CommandBuffer> m_cmdBuf;
167};
168
169#endif
Manages loading and saving texture resources to files.
Definition: TextureManager.h:105
png_structp LoadPNG(const std::string &path, size_t &width, size_t &height, FILE *&fp, png_infop &info, png_infop &end)
Helper function to initialize libpng and get header info.
Definition: TextureManager.cpp:343
std::unique_ptr< vk::raii::Sampler > m_sampler
Sampler for textures.
Definition: TextureManager.h:160
void LoadTexture(const std::string &name, const std::string &path)
Loads a texture from a file into a named resource.
Definition: TextureManager.cpp:467
GLFWimage LoadPNGToGLFWImage(const std::string &path)
Loads a PNG to a GLFWimage.
Definition: TextureManager.cpp:427
Encapsulates the various Vulkan objects we need to represent texture image memory.
Definition: TextureManager.h:48
Texture(const vk::raii::Device &device, const vk::ImageCreateInfo &imageInfo, const vk::raii::Buffer &srcBuf, int width, int height, TextureManager *mgr, const std::string &name="", bool upsampleLinear=true)
Creates a texture from an externally supplied staging buffer.
Definition: TextureManager.cpp:47
std::unique_ptr< vk::raii::ImageView > m_view
View of the image.
Definition: TextureManager.h:93
std::unique_ptr< vk::raii::DeviceMemory > m_deviceMemory
Device memory backing the image.
Definition: TextureManager.h:98
vk::raii::Image m_image
Image object for our texture.
Definition: TextureManager.h:90