Gamp v0.0.7-36-g24b1eb6
Gamp: Graphics, Audio, Multimedia and Processing
Loading...
Searching...
No Matches
GLMappedBuffer.hpp
Go to the documentation of this file.
1/*
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright Gothel Software e.K.
4 *
5 * SPDX-License-Identifier: MIT
6 *
7 * This Source Code Form is subject to the terms of the MIT License
8 * If a copy of the MIT was not distributed with this file,
9 * you can obtain one at https://opensource.org/license/mit/.
10 */
11
12#ifndef GAMP_GLMAPPEDBUFFER_HPP_
13#define GAMP_GLMAPPEDBUFFER_HPP_
14
15#include <cmath>
17#include <memory>
18
19#include <jau/basic_types.hpp>
20#include <jau/float_types.hpp>
21#include <jau/string_util.hpp>
22
23namespace gamp::render::gl::data {
24 using namespace gamp::render::gl;
25
26 /** \addtogroup Gamp_GLData
27 *
28 * @{
29 */
30
31 /**
32 * OpenGL mapped buffer storage object reflecting it's
33 * <ul>
34 * <li>storage size</li>
35 * <li>storage memory if mapped</li>
36 * <li>mutable usage or immutable flags</li>
37 * </ul>
38 * <p>
39 * Buffer storage is created via:
40 * <ul>
41 * <li>{@link GL#glBufferData(int, long, java.nio.Buffer, int)} - storage creation via target</li>
42 * <li>{@link GL2#glNamedBufferDataEXT(int, long, java.nio.Buffer, int)} - storage creation, direct</li>
43 * <li>{@link GL4#glNamedBufferData(int, long, java.nio.Buffer, int)} - storage creation, direct</li>
44 * <li>{@link GL4#glBufferStorage(int, long, Buffer, int)} - storage creation via target</li>
45 * <li>{@link GL4#glNamedBufferStorage(int, long, Buffer, int)} - storage creation, direct</li>
46 * </ul>
47 * Note that storage <i>recreation</i> as mentioned above also invalidate a previous storage instance,
48 * i.e. disposed the buffer's current storage if exist and attaches a new storage instance.
49 * </p>
50 * <p>
51 * Buffer storage is disposed via:
52 * <ul>
53 * <li>{@link GL#glDeleteBuffers(int, IntBuffer)} - explicit, direct, via {@link #notifyBuffersDeleted(int, IntBuffer)} or {@link #notifyBuffersDeleted(int, int[], int)}</li>
54 * <li>{@link GL#glBufferData(int, long, java.nio.Buffer, int)} - storage recreation via target</li>
55 * <li>{@link GL2#glNamedBufferDataEXT(int, long, java.nio.Buffer, int)} - storage recreation, direct</li>
56 * <li>{@link GL4#glNamedBufferData(int, long, java.nio.Buffer, int)} - storage recreation, direct</li>
57 * <li>{@link GL4#glBufferStorage(int, long, Buffer, int)} - storage recreation via target</li>
58 * <li>{@link GL4#glNamedBufferStorage(int, long, Buffer, int)} - storage recreation, direct</li>
59 * </ul>
60 * </p>
61 * <p>
62 * GL buffer storage is mapped via
63 * <ul>
64 * <li>{@link GL#mapBuffer(int, int)}</li>
65 * <li>{@link GL#mapBufferRange(int, long, long, int)}</li>
66 * <li>{@link GL2#mapNamedBufferEXT(int, int)}</li>
67 * <li>{@link GL2#mapNamedBufferRangeEXT(int, long, long, int)}</li>
68 * <li>{@link GL4#mapNamedBuffer(int, int)}</li>
69 * <li>{@link GL4#mapNamedBufferRange(int, long, long, int)}</li>
70 * </ul>
71 * </p>
72 * <p>
73 * GL buffer storage is unmapped via
74 * <ul>
75 * <li>{@link GL#glDeleteBuffers(int, IntBuffer)} - buffer deletion</li>
76 * <li>{@link GL#glUnmapBuffer(int)} - explicit via target</li>
77 * <li>{@link GL2#glUnmapNamedBufferEXT(int)} - explicit direct</li>
78 * <li>{@link GL4#glUnmapNamedBuffer(int)} - explicit direct</li>
79 * <li>{@link GL#glBufferData(int, long, java.nio.Buffer, int)} - storage recreation via target</li>
80 * <li>{@link GL2#glNamedBufferDataEXT(int, long, java.nio.Buffer, int)} - storage recreation, direct</li>
81 * <li>{@link GL4#glNamedBufferData(int, long, java.nio.Buffer, int)} - storage recreation, direct</li>
82 * <li>{@link GL4#glBufferStorage(int, long, Buffer, int)} - storage creation via target</li>
83 * <li>{@link GL4#glNamedBufferStorage(int, long, Buffer, int)} - storage creation, direct</li>
84 * </ul>
85 * </p>
86 */
88 private:
89 GLuint m_name;
90 glmemsize_t m_size;
91 GLbitfield m_mutableUsage;
92 GLbitfield m_immutableFlags;
93 void* m_mappedBuffer;
94
95 public:
96 GLMappedBuffer(GLuint name, glmemsize_t size, GLbitfield mutableUsage, GLbitfield immutableFlags) noexcept {
97 m_name = name;
98 m_size = size;
99 m_mutableUsage = mutableUsage;
100 m_immutableFlags = immutableFlags;
101 m_mappedBuffer = nullptr;
102 }
103
104 void reset(glmemsize_t size, GLbitfield mutableUsage, GLbitfield immutableFlags) noexcept {
105 m_name = 0;
106 m_size = size;
107 m_mutableUsage = mutableUsage;
108 m_immutableFlags = immutableFlags;
109 m_mappedBuffer = nullptr;
110 }
111 void setMappedBuffer(void* buffer) noexcept {
112 m_mappedBuffer = buffer;
113 }
114
115 /** Return the buffer name */
116 GLuint name() const noexcept { return m_name; }
117
118 /** Return the buffer's storage size. */
119 glmemsize_t size() const noexcept { return m_size; }
120
121 /**
122 * Returns <code>true</code> if buffer's storage is mutable, i.e.
123 * created via {@link GL#glBufferData(int, long, java.nio.Buffer, int)}.
124 * <p>
125 * Returns <code>false</code> if buffer's storage is immutable, i.e.
126 * created via {@link GL4#glBufferStorage(int, long, Buffer, int)}.
127 * </p>
128 * @return
129 */
130 bool isMutableStorage() const noexcept { return 0 != m_mutableUsage; }
131
132 /**
133 * Returns the mutable storage usage or 0 if storage is not {@link #isMutableStorage() mutable}.
134 */
135 GLbitfield mutableUsage() const noexcept { return m_mutableUsage; }
136
137 /**
138 * Returns the immutable storage flags, invalid if storage is {@link #isMutableStorage() mutable}.
139 */
140 GLbitfield immutableFlags() const noexcept { return m_immutableFlags; }
141
142 /**
143 * Returns the mapped ByteBuffer, or null if not mapped.
144 * Mapping may occur via:
145 * <ul>
146 * <li>{@link GL#glMapBuffer(int, int)}</li>
147 * <li>{@link GL#glMapBufferRange(int, long, long, int)}</li>
148 * <li>{@link GL2#glMapNamedBufferEXT(int, int)}</li>
149 * <li>{@link GL2#glMapNamedBufferRangeEXT(int, long, long, int)}
150 * </ul>
151 */
152 const void* data() const noexcept { return m_mappedBuffer; }
153
154 void* data() noexcept { return m_mappedBuffer; }
155
156 std::string toString(bool skipMappedBuffer=false) const {
157 std::string s0;
158 if( isMutableStorage() ) {
159 s0 = jau::format_string("GLMappedBuffer[name %u, size %zu, mutable usage 0x%X", m_name, m_size, m_mutableUsage);
160 } else {
161 s0 = jau::format_string("GLMappedBuffer[name %u, size %zu, immutable flags 0x%X", m_name, m_size, m_immutableFlags);
162 }
163 if(skipMappedBuffer) {
164 return s0.append("]");
165 } else {
166 return s0.append(", mapped ").append(jau::to_hexstring(m_mappedBuffer)).append("]");
167 }
168 }
169 };
170 typedef std::unique_ptr<GLMappedBuffer> GLMappedBufferPtr;
171
172 /**@}*/
173}
174
175#endif // GAMP_GLMAPPEDBUFFER_HPP_
void reset(glmemsize_t size, GLbitfield mutableUsage, GLbitfield immutableFlags) noexcept
void setMappedBuffer(void *buffer) noexcept
GLMappedBuffer(GLuint name, glmemsize_t size, GLbitfield mutableUsage, GLbitfield immutableFlags) noexcept
const void * data() const noexcept
Returns the mapped ByteBuffer, or null if not mapped.
GLbitfield immutableFlags() const noexcept
Returns the immutable storage flags, invalid if storage is mutable.
std::string toString(bool skipMappedBuffer=false) const
GLuint name() const noexcept
Return the buffer name.
bool isMutableStorage() const noexcept
Returns true if buffer's storage is mutable, i.e.
glmemsize_t size() const noexcept
Return the buffer's storage size.
GLbitfield mutableUsage() const noexcept
Returns the mutable storage usage or 0 if storage is not mutable.
GLsizeiptr glmemsize_t
Compatible with ssize_t.
Definition GLBuffers.hpp:26
std::unique_ptr< GLMappedBuffer > GLMappedBufferPtr
std::string to_hexstring(value_type const &v, const bool skipLeading0x=false) noexcept
Produce a lower-case hexadecimal string representation with leading 0x in MSB of the given pointer.
std::string format_string(const char *format,...)
Returns a string according to printf() formatting rules and variable number of arguments following th...