Rendering Engine 0.2.0
Modular Graphics Rendering Engine | v0.2.0
Loading...
Searching...
No Matches
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) 2025 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;
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 /**
50 * @brief Destructor. Frees GPU memory if allocated.
51 */
53
54 /**
55 * @brief Uploads the texture data to GPU if not already uploaded.
56 *
57 * This function instantiates GPU resource handlers
58 * and invokes texture uploading routines.
59 */
60 void UploadToGPU();
61
62 /**
63 * @brief Releases the GPU resources (image, sampler, view).
64 *
65 * After this call, the texture will be stored in RAM only.
66 */
67 void ReleaseFromGPU();
68
69 /**
70 * @brief Checks whether the texture is currently loaded in GPU memory.
71 * @return true if texture has been uploaded to GPU, false otherwise.
72 */
73 bool IsOnGPU() const;
74
75 /**
76 * @brief Gets the size of the texture in RAM (in bytes).
77 * @return Estimated RAM usage of the image data.
78 */
79 size_t GetSizeInRAM() const;
80
81 /**
82 * @brief Gets the size of the texture in GPU memory (in bytes).
83 * @return Estimated GPU memory usage if uploaded, 0 otherwise.
84 */
85 size_t GetSizeInGPU() const;
86
87 /**
88 * @brief Returns a const reference to the CPU-side image data.
89 * @return Const reference to the underlying ImageData.
90 */
91 ImageData const& GetCpuImageData() const;
92
93 /**
94 * @brief Returns a modifiable reference to the CPU-side image data.
95 * @return Reference to the underlying ImageData.
96 */
98
99 /**
100 * @brief Returns a pointer to the GPU texture resource interface.
101 * @return Pointer to ITextureRenderResources if uploaded, nullptr otherwise.
102 */
104
105private:
106 const std::string mPath;
107 const IRenderer* mRenderer;
108 std::unique_ptr<ImageData> mImageData;
109 std::unique_ptr<ITextureRenderResources> mGpuHandle;
110};
111
112} // namespace rendering_engine
Defines an abstract interface for rendering backends.
Interface for backend-specific GPU texture resource management.
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.