Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
DVDTexture.cpp
Go to the documentation of this file.
1
2
3/***********************************************************************
4 created: Sun Jan 11 2009
5 author: Paul D Turner
6*************************************************************************/
7/***************************************************************************
8 * Copyright (C) 2004 - 2009 Paul D Turner & The CEGUI Development Team
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining
11 * a copy of this software and associated documentation files (the
12 * "Software"), to deal in the Software without restriction, including
13 * without limitation the rights to use, copy, modify, merge, publish,
14 * distribute, sublicense, and/or sell copies of the Software, and to
15 * permit persons to whom the Software is furnished to do so, subject to
16 * the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be
19 * included in all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
25 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27 * OTHER DEALINGS IN THE SOFTWARE.
28 ***************************************************************************/
29#include "Headers/DVDTexture.h"
30#include "CEGUI/Exceptions.h"
31#include "CEGUI/System.h"
32#include "CEGUI/ImageCodec.h"
33
34#include "Core/Headers/Kernel.h"
37
40
41// Start of CEGUI namespace section
42namespace CEGUI
43{
44
45DVDTexture::DVDTexture( CEGUIRenderer& owner, const String& name )
46 : _owner(owner)
47 , _name(name)
48 , _format( PF_RGBA )
49{
51}
52
53DVDTexture::DVDTexture( CEGUIRenderer& owner, const String& name, const String& filename, const String& resourceGroup )
54 : DVDTexture(owner, name)
55{
56 loadFromFile(filename, resourceGroup);
57}
58
59DVDTexture::DVDTexture( CEGUIRenderer& owner, const String& name, const Sizef& size )
60 : DVDTexture(owner, name)
61{
62 setTextureSize(size);
63}
64
65DVDTexture::DVDTexture( CEGUIRenderer& owner, const String& name, const Divide::Handle<Divide::Texture> tex, const Sizef& size )
66 : _size(size)
67 , _dataSize(size)
68 , _owner(owner)
69 , _name(name)
70 , _format(PF_RGBA)
71 , _texture(tex)
72{
74}
75
77{
78 DestroyResource( _texture );
79}
80
81void DVDTexture::loadFromFile(const String& filename, const String& resourceGroup)
82{
83 // Note from PDT:
84 // There is somewhat tight coupling here between DVDTexture and the
85 // ImageCodec classes - we have intimate knowledge of how they are
86 // implemented and that knowledge is relied upon in an unhealthy way; this
87 // should be addressed at some stage.
88
89 // load file to memory via resource provider
90 RawDataContainer texFile;
91 System::getSingleton().getResourceProvider()->loadRawDataContainer(filename, texFile, resourceGroup);
92
93 // get and check existence of CEGUI::System (needed to access ImageCodec)
94 System* sys = System::getSingletonPtr();
95 Divide::DIVIDE_ASSERT(sys, "CEGUI::System object has not been created: unable to access ImageCodec.");
96
97 Texture* res = sys->getImageCodec().load(texFile, this);
98
99 // unload file data buffer
100 System::getSingleton().getResourceProvider()->unloadRawDataContainer(texFile);
101
102 Divide::DIVIDE_ASSERT(res, (sys->getImageCodec().getIdentifierString() + " failed to load image '" + filename + "'.").c_str());
103}
104
105void DVDTexture::loadFromMemory(const void* buffer, const Sizef& buffer_size, PixelFormat pixel_format)
106{
107 Divide::DIVIDE_ASSERT(isPixelFormatSupported(pixel_format), "Data was supplied in an unsupported pixel format.");
108
109 setTextureSize_impl(buffer_size, pixel_format);
110
111 // store size of original data we are loading
112 _dataSize = buffer_size;
114
115 blitFromMemory(buffer, Rectf(Vector2f(0, 0), buffer_size));
116}
117
118void DVDTexture::setTextureSize(const Sizef& sz)
119{
120 setTextureSize_impl(sz, PF_RGBA);
121
124}
125
126void DVDTexture::setTextureSize_impl(const Sizef& sz, PixelFormat format)
127{
128 using namespace Divide;
129
130 _format = format;
131
132 switch ( format )
133 {
134 case PF_RGB_DXT1:
135 case PF_RGBA_DXT1:
136 case PF_RGBA_DXT3:
137 case PF_RGBA_DXT5:
138 _isCompressed = true;
139 break;
140
141 case PF_RGB:
142 case PF_RGBA:
143 case PF_RGBA_4444:
144 case PF_RGB_565:
145 _isCompressed = false;
146 break;
147
148 case PF_PVRTC2:
149 case PF_PVRTC4:
150 {
151 DIVIDE_UNEXPECTED_CALL_MSG( "DVDTexture::setTextureSize_impl: PVRTC textures not supported!" );
152 } break;
153 }
154
155 static float maxSize = -1;
156
157 _size = sz;
158
159 // make sure size is within boundaries
160 if (maxSize < 0.f)
161 {
162 maxSize = float(GFXDevice::GetDeviceInformation()._maxTextureSize);
163 }
164
165 Divide::DIVIDE_ASSERT(!( _size.d_width > maxSize || _size.d_height > maxSize), "DVDTexture:: size too big");
166
167 Get(_texture)->createWithData( nullptr, 0u, vec2<U16>( _size.d_width, _size.d_height), {} );
168}
169
170void DVDTexture::blitFromMemory(const void* sourceData, const Rectf& area)
171{
172 using namespace Divide;
173
174 DIVIDE_ASSERT(_format != PF_PVRTC2 && _format != PF_PVRTC4, "DVDTexture::blitFromMemory: PVRTC textures not supported!" );
175
176 size_t image_size = 0u;
177 if (_isCompressed)
178 {
179 size_t blocksize = 16;
180 if ( _format == PF_RGB_DXT1 ||
181 _format == PF_RGBA_DXT3 )
182 {
183 blocksize = 8;
184 }
185
186 image_size = size_t(std::ceil( area.getSize().d_width / 4 ) *
187 std::ceil( area.getSize().d_height / 4 ) *
188 blocksize);
189 }
190 else
191 {
192 U8 bpp = 4u;
193 if ( _format == PF_RGB )
194 {
195 bpp = 3u;
196 }
197 else if ( _format == PF_RGBA_4444 || _format == PF_RGB_565 )
198 {
199 bpp = 2u;
200 }
201
202 image_size = size_t(area.getSize().d_width *
203 area.getSize().d_height) *
204 bpp;
205 }
206
207 const PixelAlignment pixelUnpackAlignment
208 {
209 ._alignment = 1u
210 };
211
212 vec3<U16> offset;
213 offset.x = to_U16(area.left());
214 offset.y = to_U16(area.top());
215 offset.z = 0u;
216
217 vec3<U16> dimensions;
218 dimensions.width = to_U16(area.getWidth());
219 dimensions.height = to_U16(area.getHeight());
220 dimensions.depth = 1u;
221 Get(_texture)->replaceData( (const Byte*)sourceData, image_size, offset, dimensions, pixelUnpackAlignment );
222}
223
224void DVDTexture::blitToMemory(void* targetData) {
225 const Divide::PixelAlignment pixelPackAlignment = {
226 ._alignment = 1u
227 };
228
229 auto data = Get(_texture)->readData(0u, pixelPackAlignment);
230 memcpy(targetData, data._data.data(), data._data.size());
231}
232
234{
235 using namespace Divide;
236
237 thread_local size_t TEXTURE_IDX = 0u;
238
239 GFXDataFormat dataFormat = GFXDataFormat::UNSIGNED_BYTE;
240 GFXImageFormat targetFormat = GFXImageFormat::RGBA;
241 GFXImagePacking targetPacking = GFXImagePacking::NORMALIZED;
242
243 switch ( _format )
244 {
245 case Texture::PixelFormat::PF_RGB:
246 case Texture::PixelFormat::PF_RGB_565:
247 {
248 targetFormat = GFXImageFormat::RGB;
249 if ( _format == Texture::PixelFormat::PF_RGB_565 )
250 {
251 targetPacking = GFXImagePacking::RGB_565;
252 }
253 } break;
254 case Texture::PixelFormat::PF_RGBA:
255 case Texture::PixelFormat::PF_RGBA_4444:
256 {
257 targetFormat = GFXImageFormat::RGBA;
258 if ( _format == Texture::PixelFormat::PF_RGBA_4444 )
259 {
260 targetPacking = GFXImagePacking::RGBA_4444;
261 }
262 } break;
263 case Texture::PixelFormat::PF_RGB_DXT1:
264 {
265 targetFormat = GFXImageFormat::DXT1_RGB;
266 }break;
267 case Texture::PixelFormat::PF_RGBA_DXT1:
268 {
269 targetFormat = GFXImageFormat::DXT1_RGBA;
270 }break;
271 case Texture::PixelFormat::PF_RGBA_DXT3:
272 {
273 targetFormat = GFXImageFormat::DXT3_RGBA;
274 }break;
275 case Texture::PixelFormat::PF_RGBA_DXT5:
276 {
277 targetFormat = GFXImageFormat::DXT5_RGBA;
278 }break;
279 case Texture::PixelFormat::PF_PVRTC2:
280 case Texture::PixelFormat::PF_PVRTC4:
281 {
282 DIVIDE_UNEXPECTED_CALL_MSG("DVDTexture::generateDVDTexture: PVRTC textures not supported!");
283 } break;
284 }
285
286 ResourceDescriptor<Divide::Texture> resDescriptor( Util::StringFormat("CEGUI_texture_{}", TEXTURE_IDX++).c_str() );
287 resDescriptor.waitForReady( true );
288 TextureDescriptor& texDescriptor = resDescriptor._propertyDescriptor;
289 texDescriptor._dataType = dataFormat;
290 texDescriptor._baseFormat = targetFormat;
291 texDescriptor._packing = targetPacking;
292 texDescriptor._allowRegionUpdates = true;
293 texDescriptor._mipMappingState = MipMappingState::OFF;
294
295 DestroyResource( _texture );
296 _texture = CreateResource( resDescriptor );
297
298 _sampler._wrapU = TextureWrap::CLAMP_TO_EDGE;
299 _sampler._wrapV = TextureWrap::CLAMP_TO_EDGE;
300 _sampler._wrapW = TextureWrap::CLAMP_TO_EDGE;
302 _sampler._minFilter = TextureFilter::LINEAR;
303 _sampler._magFilter = TextureFilter::LINEAR;
304 _sampler._mipSampling = TextureMipSampling::NONE;
305}
306
308{
309 DestroyResource( _texture );
310
311 _texture = tex;
312 _dataSize = _size = size;
314}
315
316bool DVDTexture::isPixelFormatSupported(const PixelFormat fmt) const
317{
318 switch (fmt)
319 {
320 case PF_RGBA:
321 case PF_RGB:
322 case PF_RGBA_4444:
323 case PF_RGB_565:
324 case PF_RGB_DXT1:
325 case PF_RGBA_DXT1:
326 case PF_RGBA_DXT3:
327 case PF_RGBA_DXT5:
328 return true;
329
330 case PF_PVRTC2:
331 case PF_PVRTC4:
332 break;
333 }
334
335 return false;
336}
337
338} // End of CEGUI namespace section
#define DIVIDE_ASSERT(...)
#define DIVIDE_UNEXPECTED_CALL_MSG(X)
bool isPixelFormatSupported(PixelFormat fmt) const override
Definition: DVDTexture.cpp:316
DVDTexture(CEGUIRenderer &owner, const String &name)
Basic constructor.
Definition: DVDTexture.cpp:45
bool _isCompressed
Whether Texture format is a compressed format.
Definition: DVDTexture.h:140
void updateCachedScaleValues()
updates cached scale value used to map pixels to texture co-ords.
Definition: DVDTexture.inl:54
Divide::Handle< Divide::Texture > _texture
The Divide texture used for storing this DVDTexture's data.
Definition: DVDTexture.h:142
void blitFromMemory(const void *sourceData, const Rectf &area) override
Definition: DVDTexture.cpp:170
void setTextureSize(const Sizef &sz)
set the size of the internal texture.
Definition: DVDTexture.cpp:118
Divide::SamplerDescriptor _sampler
A Divide sampler hash used for sampling from this texture in shaders.
Definition: DVDTexture.h:144
void setDVDTexture(Divide::Handle< Divide::Texture > tex, const Sizef &size)
set the Divide::Texture that this Texture is based on to the specified texture, with the specified si...
Definition: DVDTexture.cpp:307
void loadFromMemory(const void *buffer, const Sizef &buffer_size, PixelFormat pixel_format) override
Definition: DVDTexture.cpp:105
Sizef _dataSize
original size of pixel data loaded into texture
Definition: DVDTexture.h:130
void generateDVDTexture()
generate the DVD texture and set some initial options.
Definition: DVDTexture.cpp:233
void setTextureSize_impl(const Sizef &sz, PixelFormat format)
internal texture resize function (does not reset format or other fields)
Definition: DVDTexture.cpp:126
void blitToMemory(void *targetData) override
Definition: DVDTexture.cpp:224
void loadFromFile(const String &filename, const String &resourceGroup) override
Definition: DVDTexture.cpp:81
PixelFormat _format
Texture format.
Definition: DVDTexture.h:138
Sizef _size
Size of the texture.
Definition: DVDTexture.h:128
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
std::byte Byte
uint8_t U8
PropertyDescriptor< T > _propertyDescriptor
Definition: Resource.h:151
TextureFilter _minFilter
Texture filtering mode.
TextureWrap _wrapU
Texture wrap mode (Or S-R-T)
U8 _anisotropyLevel
The value must be in the range [0...255] and is automatically clamped by the max HW supported level.
TextureMipSampling _mipSampling