Rendering Engine 0.2.9
Modular Graphics Rendering Engine | v0.2.9
image_data_gpu.hpp
Go to the documentation of this file.
1// This file is part of the Rendering Engine project.
2// Author: Alexander Obzherin <alexanderobzherin@gmail.com>
3// Copyright (c) 2026 Alexander Obzherin
4// Distributed under the terms of the zlib License. See LICENSE.md for details.
5
6#pragma once
7
8#include <string>
9#include <memory>
10#include <vector>
11
12namespace rendering_engine
13{
14class IRenderer;
15class ITextureRenderResources;
16class ImageData;
17
18/**
19 * @class ImageDataGpu
20 * @brief Combines CPU-side and GPU-side texture representations, managing both lifetimes and transitions.
21 *
22 * This class owns the image data in RAM (via ImageData) and optionally uploads/releases
23 * the corresponding GPU-side representation. It provides accessors for both and tracks memory usage.
24 */
26{
27public:
28 /**
29 * @brief Constructs an ImageDataGpu object from an image file path.
30 * @param path Path to the image file (must be supported format such as .png or .jpg).
31 * @param renderer Pointer to the rendering engine interface (IRenderer).
32 */
33 ImageDataGpu(std::string path, IRenderer* renderer);
34
35 /**
36 * @brief Constructs an ImageDataGpu object from raw image file bytes.
37 *
38 * This constructor allows loading GPU-ready image data directly from a memory
39 * buffer rather than from disk. The input is interpreted as an encoded image
40 * (PNG or JPEG). After decoding via ImageData, the pixel data is uploaded to
41 * GPU memory through the provided IRenderer interface.
42 *
43 * @param fileBytes Raw binary contents of an image file (PNG or JPEG).
44 * @param renderer Pointer to the rendering engine interface responsible for
45 * uploading the texture to GPU memory.
46 */
47 ImageDataGpu(std::vector<uint8_t> const& fileBytes, IRenderer* renderer);
48
49 ImageDataGpu(ImageData imageData, IRenderer* renderer);
50
51 /**
52 * @brief Destructor. Frees GPU memory if allocated.
53 */
55
56 /**
57 * @brief Uploads the texture data to GPU if not already uploaded.
58 *
59 * This function instantiates GPU resource handlers
60 * and invokes texture uploading routines.
61 */
62 void UploadToGPU();
63
64 /**
65 * @brief Releases the GPU resources (image, sampler, view).
66 *
67 * After this call, the texture will be stored in RAM only.
68 */
69 void ReleaseFromGPU();
70
71 /**
72 * @brief Checks whether the texture is currently loaded in GPU memory.
73 * @return true if texture has been uploaded to GPU, false otherwise.
74 */
75 bool IsOnGPU() const;
76
77 /**
78 * @brief Gets the size of the texture in RAM (in bytes).
79 * @return Estimated RAM usage of the image data.
80 */
81 size_t GetSizeInRAM() const;
82
83 /**
84 * @brief Gets the size of the texture in GPU memory (in bytes).
85 * @return Estimated GPU memory usage if uploaded, 0 otherwise.
86 */
87 size_t GetSizeInGPU() const;
88
89 /**
90 * @brief Returns a const reference to the CPU-side image data.
91 * @return Const reference to the underlying ImageData.
92 */
93 ImageData const& GetCpuImageData() const;
94
95 /**
96 * @brief Returns a modifiable reference to the CPU-side image data.
97 * @return Reference to the underlying ImageData.
98 */
100
101 /**
102 * @brief Returns a pointer to the GPU texture resource interface.
103 * @return Pointer to ITextureRenderResources if uploaded, nullptr otherwise.
104 */
106
107private:
108 const std::string mPath;
109 const IRenderer* mRenderer;
110 std::unique_ptr<ImageData> mImageData;
111 std::unique_ptr<ITextureRenderResources> mGpuHandle;
112};
113
114} // namespace rendering_engine
Defines an abstract interface for rendering backends.
Definition: i_renderer.hpp:29
Interface for backend-specific GPU texture resource management.
Combines CPU-side and GPU-side texture representations, managing both lifetimes and transitions.
bool IsOnGPU() const
Checks whether the texture is currently loaded in GPU memory.
size_t GetSizeInRAM() const
Gets the size of the texture in RAM (in bytes).
ITextureRenderResources * GetTextureRenderResources() const
Returns a pointer to the GPU texture resource interface.
~ImageDataGpu()
Destructor. Frees GPU memory if allocated.
size_t GetSizeInGPU() const
Gets the size of the texture in GPU memory (in bytes).
void UploadToGPU()
Uploads the texture data to GPU if not already uploaded.
ImageData const & GetCpuImageData() const
Returns a const reference to the CPU-side image data.
void ReleaseFromGPU()
Releases the GPU resources (image, sampler, view).
ImageDataGpu(std::string path, IRenderer *renderer)
Constructs an ImageDataGpu object from an image file path.
Represents raw 2D image data stored in memory.
Definition: image_data.hpp:80