NDEVR
API Documentation
GLESImage.h
1/*--------------------------------------------------------------------------------------------
2Copyright (c) 2019, NDEVR LLC
3tyler.parke@ndevr.org
4 __ __ ____ _____ __ __ _______
5 | \ | | | __ \ | ___|\ \ / / | __ \
6 | \ | | | | \ \ | |___ \ \ / / | |__) |
7 | . \| | | |__/ / | |___ \ V / | _ /
8 | |\ |_|_____/__|_____|___\_/____| | \ \
9 |__| \__________________________________| \__\
10
11Subject to the terms of the Enterprise+ Agreement, NDEVR hereby grants
12Licensee a limited, non-exclusive, non-transferable, royalty-free license
13(without the right to sublicense) to use the API solely for the purpose of
14Licensee's internal development efforts to develop applications for which
15the API was provided.
16
17The above copyright notice and this permission notice shall be included in all
18copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
21INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
22PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
23FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25DEALINGS IN THE SOFTWARE.
26
27Library: Graphics
28File: VulkanImage
29Included in API: True
30Author(s): Tyler Parke
31 *-----------------------------------------------------------------------------------------**/
32#pragma once
33#include "DLLInfo.h"
34#include "GLBuffer.h"
35#include "GLESShader.h"
36#include "GLESGraphicsDevice.h"
37#include <NDEVR/ImageFactory.h>
38#include <NDEVR/Image.h>
39#include <NDEVR/Pointer.h>
40#include <NDEVR/File.h>
41#include <NDEVR/RGBColor.h>
42#include <NDEVR/Dictionary.h>
43#include <NDEVR/Buffer.h>
44
45typedef uint32_t VkImageUsageFlags;
46typedef uint32_t VkMemoryPropertyFlags;
47namespace NDEVR
48{
49 class File;
50 class VulkanInstance;
51 class VulkanBuffer;
56 class GLESImageData : public Image
57 {
58 public:
63 : m_image(image_id)
64 {
65
66 update(device);
67 }
68
71 {
72 if (IsValid(m_gles_image))
73 instance->glDeleteTextures(1, &m_gles_image);
74 m_gles_image = Constant<uint04>::Invalid;
75 }
76
79 {
80 destroy(instance);
81 bool has_image = ImageFactory::DefaultFactory().hasImage(m_image);
82 lib_assert(has_image, "Invalid GLES image");
83 if (!has_image)
84 return;
85 instance->glGenTextures(1, &m_gles_image);
86 instance->glBindTexture(GL_TEXTURE_2D, m_gles_image);
87 // set the texture wrapping/filtering options (on the currently bound texture object)
88 instance->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
89 instance->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
90 instance->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
91 instance->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
92
93 //instance->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
94 //instance->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
95 // load and generate the texture
96 {
98 lib_assert(!img.isNull(), "Bad image");
99 if (!img.isNull())
100 {
101 m_size = img->size;
102 uint04 max_size = m_size.dimensionalValue<MAX>();
103
104 m_modified_time = img->modified_time;
105 if (max_size > cast<uint04>(instance->capabilities().max_texture_size))
106 {
107 max_size = cast<uint04>(instance->capabilities().max_texture_size);
108 Vector<2, uint04> new_size = (max_size * m_size) / m_size;
109 Buffer<uint01> resized;
110 ImageFactory::Resize(img->decompressed_data, m_size, new_size, resized);
111 instance->glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, new_size[X], new_size[Y], 0, GL_RGBA, GL_UNSIGNED_BYTE, resized.begin());
112 }
113 else
114 {
115 instance->glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_size[X], m_size[Y], 0, GL_RGBA, GL_UNSIGNED_BYTE, img->decompressed_data.begin());
116 }
117 }
118 }
119 instance->glGenerateMipmap(GL_TEXTURE_2D);
120 instance->glBindTexture(GL_TEXTURE_2D, 0);
121 }
122
125 void setUniformVariables(uint04 bind_position, const DynamicPointer<GLESGraphicsDevice>& instance) const
126 {
127 instance->glActiveTexture(GL_TEXTURE0 + bind_position);
128 instance->glBindTexture(GL_TEXTURE_2D, m_gles_image);
129 }
130
132 bool hasAlpha() const
133 {
134 return m_has_alpha;
135 }
136
138 bool needsUpdate() const
139 {
140 return m_modified_time < ImageFactory().modifiedTime(m_image);
141 }
142 private:
143 String m_image;
144 Time m_modified_time = Time(0);
145 GLuint m_gles_image = Constant<uint04>::Invalid;
147 bool m_has_alpha = false;
148 };
149
154 {
155 public:
159 : m_device(instance)
160 {
161
162 }
163
165 {
166 for (auto& iter : m_file_map)
167 iter.second->destroy(m_device);
168 }
169
172 {
173 bool updated = false;
174 for (auto& iter : m_file_map)
175 {
176 if (iter.second->needsUpdate())
177 {
178 iter.second->update(m_device);
179 updated = true;
180 }
181 }
182 return updated;
183 }
184
186 bool needsImageUpdate() const
187 {
188 for (auto& iter : m_file_map)
189 {
190 if (iter.second->needsUpdate())
191 return true;
192 }
193 return false;
194 }
195
199 {
200 auto iter = m_file_map.find(id);
201 if (iter == m_file_map.end())
202 {
204 if (image->size().magnitudeSquared() > 0 && IsValid(image->size()))
205 m_file_map[String(id)] = image;
206 return image;
207 }
208 return iter.value();
209 }
210
213 };
214}
The equivelent of std::vector but with a bit more control.
Definition Buffer.hpp:58
Provides a constant, unmodifiable pointer that has shared ownership of a dynamically allocated object...
Definition Pointer.hpp:276
A hash-based key-value store, useful for quick associative lookups.
Definition Dictionary.h:64
Provides a modifiable pointer that has shared ownership of a dynamically allocated object.
Definition Pointer.hpp:356
Logic for reading or writing to a file as well as navigating filesystems or other common file operati...
Definition File.h:53
A pointer to an Image and functions relating to that image within the GLES logic space.
Definition GLESImage.h:57
void update(const DynamicPointer< GLESGraphicsDevice > &instance)
Reloads and re-uploads the image texture to GPU.
Definition GLESImage.h:78
void destroy(const DynamicPointer< GLESGraphicsDevice > &instance)
Deletes the GPU texture resource.
Definition GLESImage.h:70
bool needsUpdate() const
Checks whether the source image has been modified since last upload.
Definition GLESImage.h:138
bool hasAlpha() const
Checks whether the image has an alpha channel.
Definition GLESImage.h:132
GLESImageData(const DynamicPointer< GLESGraphicsDevice > &device, const StringView &image_id)
Constructs a GLESImageData and uploads the image to GPU.
Definition GLESImage.h:62
void setUniformVariables(uint04 bind_position, const DynamicPointer< GLESGraphicsDevice > &instance) const
Binds this texture to the given texture unit for shader use.
Definition GLESImage.h:125
bool needsImageUpdate() const
Checks whether any managed image needs re-uploading.
Definition GLESImage.h:186
Dictionary< String, DynamicPointer< GLESImageData > > m_file_map
Map of image IDs to GPU image data.
Definition GLESImage.h:211
ConstPointer< GLESImageData > getImage(const StringView &id)
Retrieves or creates a GPU-backed image by identifier.
Definition GLESImage.h:198
bool updateImages()
Re-uploads any images whose source data has changed.
Definition GLESImage.h:171
GLESImageManager(const DynamicPointer< GLESGraphicsDevice > &instance)
Constructs a GLESImageManager for the given device.
Definition GLESImage.h:158
~GLESImageManager()
Destroys all managed GPU textures.
Definition GLESImage.h:164
DynamicPointer< GLESGraphicsDevice > m_device
The OpenGL graphics device.
Definition GLESImage.h:212
The core class for reading/writing and storing images in an optimized way.
ImageReadPointer getUncompressed(const StringView &id, bool ensure_valid_transparancy=false)
Retrieves the uncompressed (raw ARGB) pixel data for the cached image.
bool hasImage(const StringView &id) const
Checks whether an image with the given identifier exists in the cache.
static void Resize(const Buffer< uint01 > &input, const Vector< 2, uint04 > &in_size, const Vector< 2, uint04 > &out_size, Buffer< uint01 > &output)
Resizes raw pixel data from one resolution to another using bilinear or nearest-neighbor sampling.
Time modifiedTime(const StringView &id) const
Returns the last modification time of the cached image.
static ImageFactory & DefaultFactory()
Returns the singleton default ImageFactory instance.
A root class that stores an an array of pixel data that can be displayed as an Image.
Definition Image.h:40
The core String View class for the NDEVR API.
Definition StringView.h:58
The core String class for the NDEVR API.
Definition String.h:95
Represents a timestamp with utilities for manipulation and conversion.
Definition Time.h:62
A fixed-size array with N dimensions used as the basis for geometric and mathematical types.
Definition Vector.hpp:62
A GPU buffer managed through the Vulkan API, used for vertex, index, and uniform data.
The connection between the NDEVR API and the Vulkan library.
The primary namespace for the NDEVR SDK.
static constexpr bool IsValid(const Angle< t_type > &value)
Checks whether the given Angle holds a valid value.
Definition Angle.h:398
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
constexpr t_to cast(const Angle< t_from > &value)
Casts an Angle from one backing type to another.
Definition Angle.h:408
Defines for a given type (such as sint04, fltp08, UUID, etc) a maximum, minimum, and reserved 'invali...
A read-only smart pointer to ImageCacheData that acquires a read lock on construction.