Line data Source code
1 :
2 : /* Copyright (c) 2009-2015, Stefan Eilemann <eile@equalizergraphics.com>
3 : * Cedric Stalder <cedric.stalder@gmail.com>
4 : * Daniel Nachbaur <danielnachbaur@gmail.com>
5 : *
6 : * This library is free software; you can redistribute it and/or modify it under
7 : * the terms of the GNU Lesser General Public License version 2.1 as published
8 : * by the Free Software Foundation.
9 : *
10 : * This library is distributed in the hope that it will be useful, but WITHOUT
11 : * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 : * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 : * details.
14 : *
15 : * You should have received a copy of the GNU Lesser General Public License
16 : * along with this library; if not, write to the Free Software Foundation, Inc.,
17 : * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 : */
19 :
20 : #ifndef EQUTIL_TEXTURE_H
21 : #define EQUTIL_TEXTURE_H
22 :
23 : #include <eq/frame.h> // Frame::Buffer enum
24 : #include <lunchbox/thread.h> // thread debug macro
25 :
26 : namespace eq
27 : {
28 : namespace util
29 : {
30 : namespace detail
31 : {
32 : class Texture;
33 : }
34 :
35 : /**
36 : * A wrapper around OpenGL textures.
37 : *
38 : * So far used by the Image and Compositor. The target is assumed to be
39 : * GL_TEXTURE_RECTANGLE_ARB or GL_TEXTURE_2D.
40 : */
41 : class Texture
42 : {
43 : public:
44 : /**
45 : * Construct a new Texture.
46 : *
47 : * A GLEWContext might be provided later using setGLEWContext(). It is
48 : * needed for operations using OpenGL extensions or version 1.2 or later
49 : * functions.
50 : * @version 1.0
51 : */
52 : EQ_API Texture(const unsigned target,
53 : const GLEWContext* const glewContext = 0);
54 :
55 : /** Destruct the texture. @version 1.0 */
56 : EQ_API virtual ~Texture();
57 :
58 : /** @name Data Access. */
59 : //@{
60 : /** @return the target of the texture. @version 1.0 */
61 : EQ_API unsigned getTarget() const;
62 :
63 : /**
64 : * @internal
65 : * @return the compressor target corresponding to the texture target.
66 : */
67 : uint32_t getCompressorTarget() const;
68 :
69 : /**
70 : * @return the internal (GPU) pixel format of the texture.
71 : * @sa init()
72 : * @version 1.0
73 : */
74 : EQ_API unsigned getInternalFormat() const;
75 :
76 : /**
77 : * Set the external data format and type.
78 : *
79 : * @param format the OpenGL format.
80 : * @param type the OpenGL Type.
81 : * @version 1.0
82 : */
83 : EQ_API void setExternalFormat(const uint32_t format, const uint32_t type);
84 :
85 : /**
86 : * @return the external data format of the texture, e.g., GL_RGBA.
87 : * @version 1.0
88 : */
89 : EQ_API unsigned getFormat() const;
90 :
91 : /**
92 : * @return the external data type of the texture, e.g., GL_HALF_FLOAT.
93 : * @version 1.0
94 : */
95 : EQ_API unsigned getType() const;
96 :
97 : /** @return the OpenGL texture name. @version 1.0 */
98 : EQ_API unsigned getName() const;
99 :
100 : /** @return the current width. @version 1.0 */
101 : EQ_API int32_t getWidth() const;
102 :
103 : /** @return the current height. @version 1.0 */
104 : EQ_API int32_t getHeight() const;
105 :
106 : /** @return true if the texture can be bound. @version 1.0 */
107 : EQ_API bool isValid() const;
108 : //@}
109 :
110 : /** @name Operations. */
111 : //@{
112 : /**
113 : * Initialize an OpenGL texture.
114 : *
115 : * @param internalFormat the OpenGL texture internal format.
116 : * @param width the width of the texture.
117 : * @param height the height of the texture.
118 : * @version 1.0
119 : */
120 : EQ_API void init(const unsigned internalFormat, const int32_t width,
121 : const int32_t height);
122 :
123 : /**
124 : * Clear the texture, including deleting the GL texture name.
125 : * @version 1.0
126 : */
127 : EQ_API void flush();
128 :
129 : /** @internal Apply the given texture filtering parameter. */
130 : EQ_API void applyZoomFilter(const ZoomFilter filter) const;
131 :
132 : EQ_API void applyWrap() const; //<! @internal
133 :
134 : /**
135 : * Copy the specified area from the current read buffer to the
136 : * texture at 0,0.
137 : * @version 1.0
138 : */
139 : EQ_API void copyFromFrameBuffer(const unsigned internalFormat,
140 : const fabric::PixelViewport& pvp);
141 :
142 : /** Copy the specified buffer to the texture at 0,0. @version 1.0 */
143 : EQ_API void upload(const int32_t width, const int32_t height,
144 : const void* ptr);
145 :
146 : /**
147 : * Copy the texture data from the GPU to the given memory address.
148 : * @version 1.0
149 : */
150 : EQ_API void download(void* buffer) const;
151 :
152 : /** Bind the texture. @version 1.0 */
153 : EQ_API void bind() const;
154 :
155 : /** Create and bind a texture to the current FBO. @version 1.0 */
156 : EQ_API void bindToFBO(const unsigned target, const int32_t width,
157 : const int32_t height, const int32_t samples = 1);
158 :
159 : /** Resize the texture. @version 1.0 */
160 : EQ_API void resize(const int32_t width, const int32_t height);
161 :
162 : /** Write the texture data as an rgb image file. @version 1.0 */
163 : EQ_API void writeRGB(const std::string& filename) const;
164 : //@}
165 :
166 : EQ_API const GLEWContext* glewGetContext() const;
167 : EQ_API void setGLEWContext(const GLEWContext* context);
168 :
169 : /** @name Wrap existing GL textures */
170 : //@{
171 : /**
172 : * Flush the texture without deleting the GL texture name.
173 : * @version 1.0
174 : */
175 : EQ_API void flushNoDelete();
176 :
177 : /**
178 : * Use an OpenGL texture created externally.
179 : *
180 : * The previous GL texture, if any, is deallocated using flush(). The new
181 : * texture has to be of the correct target, size and internal format. The
182 : * texture is validated by this method.
183 : *
184 : * @param id The OpenGL texture name.
185 : * @param internalFormat the OpenGL texture internal format.
186 : * @param width the width of the texture.
187 : * @param height the height of the texture.
188 : * @version 1.0
189 : */
190 : EQ_API void setGLData(const unsigned id, const unsigned internalFormat,
191 : const int32_t width, const int32_t height);
192 : //@}
193 :
194 : private:
195 : Texture(const Texture&) = delete;
196 : Texture& operator=(const Texture&) = delete;
197 : detail::Texture* const _impl;
198 :
199 : /**
200 : * Set the internal pixel format of the texture, e.g., GL_RGBA16F.
201 : *
202 : * Automatically sets the external format and type to one matching the
203 : * internal format.
204 : *
205 : * @param internalFormat the OpenGL internal texture format.
206 : */
207 : void _setInternalFormat(const unsigned internalFormat);
208 :
209 : /** Generate, if needed, a GL texture name. */
210 : void _generate();
211 :
212 : /** Set the size of the texture, updating the _defined flag. */
213 : void _grow(const int32_t width, const int32_t height);
214 :
215 44 : LB_TS_VAR(_thread);
216 : };
217 : /** Print the texture state to the given output stream. @version 1.7.1 */
218 : EQ_API std::ostream& operator<<(std::ostream&, const Texture&);
219 : }
220 : }
221 :
222 : #endif // EQUTIL_TEXTURE_H
|