ngscopeclient v0.1-rc1
TextureManager.h
Go to the documentation of this file.
1/***********************************************************************************************************************
2* *
3* ngscopeclient *
4* *
5* Copyright (c) 2012-2024 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
46{
47public:
48 Texture(
49 const vk::raii::Device& device,
50 const vk::ImageCreateInfo& imageInfo,
51 const vk::raii::Buffer& srcBuf,
52 int width,
53 int height,
54 TextureManager* mgr,
55 const std::string& name = "",
56 bool upsampleLinear = true //false for nearest neighbor upsampling instead
57 );
58
59 Texture(
60 const vk::raii::Device& device,
61 const vk::ImageCreateInfo& imageInfo,
62 TextureManager* mgr,
63 const std::string& name = ""
64 );
65
66 ~Texture();
67
68 ImTextureID GetTexture()
69 { return m_texture; }
70
71 vk::ImageView GetView()
72 { return **m_view; }
73
74 vk::Image GetImage()
75 { return *m_image; }
76
77 void SetName(const std::string& name);
78
79protected:
80 void LayoutTransition(
81 vk::raii::CommandBuffer& cmdBuf,
82 vk::AccessFlags src,
83 vk::AccessFlags dst,
84 vk::ImageLayout from,
85 vk::ImageLayout to);
86
88 vk::raii::Image m_image;
89
91 std::unique_ptr<vk::raii::ImageView> m_view;
92
93 ImTextureID m_texture;
94
96 std::unique_ptr<vk::raii::DeviceMemory> m_deviceMemory;
97};
98
103{
104public:
105 TextureManager(std::shared_ptr<QueueHandle> queue);
106 virtual ~TextureManager();
107
108 void LoadTexture(
109 const std::string& name,
110 const std::string& path);
111
112 ImTextureID GetTexture(const std::string& name)
113 {
114 auto it = m_textures.find(name);
115 if(it == m_textures.end())
116 {
117 LogFatal(
118 "Texture \"%s\" not found. This is probably the result of a developer mistyping a texture ID.\n",
119 name.c_str());
120 }
121 else
122 return it->second->GetTexture();
123 }
124
125 std::unique_ptr<vk::raii::Sampler>& GetSampler()
126 { return m_sampler; }
127
128 std::unique_ptr<vk::raii::Sampler>& GetNearestSampler()
129 { return m_nearestSampler; }
130
131 void clear()
132 { m_textures.clear(); }
133
134 vk::raii::CommandBuffer& GetCmdBuffer()
135 { return *m_cmdBuf; }
136
137 std::shared_ptr<QueueHandle> GetQueue()
138 { return m_queue; }
139
140 vk::ImageView GetView(const std::string& name)
141 { return m_textures[name]->GetView(); }
142
143protected:
144 std::map<std::string, std::shared_ptr<Texture> > m_textures;
145
147 std::unique_ptr<vk::raii::Sampler> m_sampler;
148
149 std::unique_ptr<vk::raii::Sampler> m_nearestSampler;
150
151 std::shared_ptr<QueueHandle> m_queue;
152 std::unique_ptr<vk::raii::CommandPool> m_cmdPool;
153 std::unique_ptr<vk::raii::CommandBuffer> m_cmdBuf;
154};
155
156#endif
Manages loading and saving texture resources to files.
Definition: TextureManager.h:103
std::unique_ptr< vk::raii::Sampler > m_sampler
Sampler for textures.
Definition: TextureManager.h:147
void LoadTexture(const std::string &name, const std::string &path)
Loads a texture from a file into a named resource.
Definition: TextureManager.cpp:346
Encapsulates the various Vulkan objects we need to represent texture image memory.
Definition: TextureManager.h:46
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:48
std::unique_ptr< vk::raii::ImageView > m_view
View of the image.
Definition: TextureManager.h:91
std::unique_ptr< vk::raii::DeviceMemory > m_deviceMemory
Device memory backing the image.
Definition: TextureManager.h:96
vk::raii::Image m_image
Image object for our texture.
Definition: TextureManager.h:88