ngscopeclient 0.1-dev+51fbda87c
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 );
57
58 Texture(
59 const vk::raii::Device& device,
60 const vk::ImageCreateInfo& imageInfo,
61 TextureManager* mgr,
62 const std::string& name = ""
63 );
64
65 ~Texture();
66
67 ImTextureID GetTexture()
68 { return m_texture; }
69
70 vk::ImageView GetView()
71 { return **m_view; }
72
73 vk::Image GetImage()
74 { return *m_image; }
75
76 void SetName(const std::string& name);
77
78protected:
79 void LayoutTransition(
80 vk::raii::CommandBuffer& cmdBuf,
81 vk::AccessFlags src,
82 vk::AccessFlags dst,
83 vk::ImageLayout from,
84 vk::ImageLayout to);
85
87 vk::raii::Image m_image;
88
90 std::unique_ptr<vk::raii::ImageView> m_view;
91
92 ImTextureID m_texture;
93
95 std::unique_ptr<vk::raii::DeviceMemory> m_deviceMemory;
96};
97
102{
103public:
104 TextureManager(std::shared_ptr<QueueHandle> queue);
105 virtual ~TextureManager();
106
107 void LoadTexture(
108 const std::string& name,
109 const std::string& path);
110
111 ImTextureID GetTexture(const std::string& name)
112 {
113 auto it = m_textures.find(name);
114 if(it == m_textures.end())
115 {
116 LogFatal(
117 "Texture \"%s\" not found. This is probably the result of a developer mistyping a texture ID.\n",
118 name.c_str());
119 }
120 else
121 return it->second->GetTexture();
122 }
123
124 std::unique_ptr<vk::raii::Sampler>& GetSampler()
125 { return m_sampler; }
126
127 void clear()
128 { m_textures.clear(); }
129
130 vk::raii::CommandBuffer& GetCmdBuffer()
131 { return *m_cmdBuf; }
132
133 std::shared_ptr<QueueHandle> GetQueue()
134 { return m_queue; }
135
136 vk::ImageView GetView(const std::string& name)
137 { return m_textures[name]->GetView(); }
138
139protected:
140 std::map<std::string, std::shared_ptr<Texture> > m_textures;
141
143 std::unique_ptr<vk::raii::Sampler> m_sampler;
144
145 std::shared_ptr<QueueHandle> m_queue;
146 std::unique_ptr<vk::raii::CommandPool> m_cmdPool;
147 std::unique_ptr<vk::raii::CommandBuffer> m_cmdBuf;
148};
149
150#endif
Manages loading and saving texture resources to files.
Definition: TextureManager.h:102
std::unique_ptr< vk::raii::Sampler > m_sampler
Sampler for textures.
Definition: TextureManager.h:143
void LoadTexture(const std::string &name, const std::string &path)
Loads a texture from a file into a named resource.
Definition: TextureManager.cpp:323
Encapsulates the various Vulkan objects we need to represent texture image memory.
Definition: TextureManager.h:46
std::unique_ptr< vk::raii::ImageView > m_view
View of the image.
Definition: TextureManager.h:90
std::unique_ptr< vk::raii::DeviceMemory > m_deviceMemory
Device memory backing the image.
Definition: TextureManager.h:95
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="")
Creates a texture from an externally supplied staging buffer.
Definition: TextureManager.cpp:48
vk::raii::Image m_image
Image object for our texture.
Definition: TextureManager.h:87